js/src/devtools/jint/v8/deltablue.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/devtools/jint/v8/deltablue.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1196 @@
     1.4 +// Copyright 2008 the V8 project authors. All rights reserved.
     1.5 +// Redistribution and use in source and binary forms, with or without
     1.6 +// modification, are permitted provided that the following conditions are
     1.7 +// met:
     1.8 +//
     1.9 +//     * Redistributions of source code must retain the above copyright
    1.10 +//       notice, this list of conditions and the following disclaimer.
    1.11 +//     * Redistributions in binary form must reproduce the above
    1.12 +//       copyright notice, this list of conditions and the following
    1.13 +//       disclaimer in the documentation and/or other materials provided
    1.14 +//       with the distribution.
    1.15 +//     * Neither the name of Google Inc. nor the names of its
    1.16 +//       contributors may be used to endorse or promote products derived
    1.17 +//       from this software without specific prior written permission.
    1.18 +//
    1.19 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.20 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.21 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.22 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.23 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.24 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.25 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.26 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.27 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.28 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.29 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.30 +
    1.31 +
    1.32 +// Simple framework for running the benchmark suites and
    1.33 +// computing a score based on the timing measurements.
    1.34 +
    1.35 +
    1.36 +// A benchmark has a name (string) and a function that will be run to
    1.37 +// do the performance measurement. The optional setup and tearDown
    1.38 +// arguments are functions that will be invoked before and after
    1.39 +// running the benchmark, but the running time of these functions will
    1.40 +// not be accounted for in the benchmark score.
    1.41 +function Benchmark(name, run, setup, tearDown) {
    1.42 +  this.name = name;
    1.43 +  this.run = run;
    1.44 +  this.Setup = setup ? setup : function() { };
    1.45 +  this.TearDown = tearDown ? tearDown : function() { };
    1.46 +}
    1.47 +
    1.48 +
    1.49 +// Benchmark results hold the benchmark and the measured time used to
    1.50 +// run the benchmark. The benchmark score is computed later once a
    1.51 +// full benchmark suite has run to completion.
    1.52 +function BenchmarkResult(benchmark, time) {
    1.53 +  this.benchmark = benchmark;
    1.54 +  this.time = time;
    1.55 +}
    1.56 +
    1.57 +
    1.58 +// Automatically convert results to numbers. Used by the geometric
    1.59 +// mean computation.
    1.60 +BenchmarkResult.prototype.valueOf = function() {
    1.61 +  return this.time;
    1.62 +}
    1.63 +
    1.64 +
    1.65 +// Suites of benchmarks consist of a name and the set of benchmarks in
    1.66 +// addition to the reference timing that the final score will be based
    1.67 +// on. This way, all scores are relative to a reference run and higher
    1.68 +// scores implies better performance.
    1.69 +function BenchmarkSuite(name, reference, benchmarks) {
    1.70 +  this.name = name;
    1.71 +  this.reference = reference;
    1.72 +  this.benchmarks = benchmarks;
    1.73 +  BenchmarkSuite.suites.push(this);
    1.74 +}
    1.75 +
    1.76 +
    1.77 +// Keep track of all declared benchmark suites.
    1.78 +BenchmarkSuite.suites = [];
    1.79 +
    1.80 +
    1.81 +// Scores are not comparable across versions. Bump the version if
    1.82 +// you're making changes that will affect that scores, e.g. if you add
    1.83 +// a new benchmark or change an existing one.
    1.84 +BenchmarkSuite.version = '5';
    1.85 +
    1.86 +
    1.87 +// To make the benchmark results predictable, we replace Math.random
    1.88 +// with a 100% deterministic alternative.
    1.89 +Math.random = (function() {
    1.90 +  var seed = 49734321;
    1.91 +  return function() {
    1.92 +    // Robert Jenkins' 32 bit integer hash function.
    1.93 +    seed = ((seed + 0x7ed55d16) + (seed << 12))  & 0xffffffff;
    1.94 +    seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff;
    1.95 +    seed = ((seed + 0x165667b1) + (seed << 5))   & 0xffffffff;
    1.96 +    seed = ((seed + 0xd3a2646c) ^ (seed << 9))   & 0xffffffff;
    1.97 +    seed = ((seed + 0xfd7046c5) + (seed << 3))   & 0xffffffff;
    1.98 +    seed = ((seed ^ 0xb55a4f09) ^ (seed >>> 16)) & 0xffffffff;
    1.99 +    return (seed & 0xfffffff) / 0x10000000;
   1.100 +  };
   1.101 +})();
   1.102 +
   1.103 +
   1.104 +// Runs all registered benchmark suites and optionally yields between
   1.105 +// each individual benchmark to avoid running for too long in the
   1.106 +// context of browsers. Once done, the final score is reported to the
   1.107 +// runner.
   1.108 +BenchmarkSuite.RunSuites = function(runner) {
   1.109 +  var continuation = null;
   1.110 +  var suites = BenchmarkSuite.suites;
   1.111 +  var length = suites.length;
   1.112 +  BenchmarkSuite.scores = [];
   1.113 +  var index = 0;
   1.114 +  function RunStep() {
   1.115 +    while (continuation || index < length) {
   1.116 +      if (continuation) {
   1.117 +        continuation = continuation();
   1.118 +      } else {
   1.119 +        var suite = suites[index++];
   1.120 +        if (runner.NotifyStart) runner.NotifyStart(suite.name);
   1.121 +        continuation = suite.RunStep(runner);
   1.122 +      }
   1.123 +      if (continuation && typeof window != 'undefined' && window.setTimeout) {
   1.124 +        window.setTimeout(RunStep, 25);
   1.125 +        return;
   1.126 +      }
   1.127 +    }
   1.128 +    if (runner.NotifyScore) {
   1.129 +      var score = BenchmarkSuite.GeometricMean(BenchmarkSuite.scores);
   1.130 +      var formatted = BenchmarkSuite.FormatScore(100 * score);
   1.131 +      runner.NotifyScore(formatted);
   1.132 +    }
   1.133 +  }
   1.134 +  RunStep();
   1.135 +}
   1.136 +
   1.137 +
   1.138 +// Counts the total number of registered benchmarks. Useful for
   1.139 +// showing progress as a percentage.
   1.140 +BenchmarkSuite.CountBenchmarks = function() {
   1.141 +  var result = 0;
   1.142 +  var suites = BenchmarkSuite.suites;
   1.143 +  for (var i = 0; i < suites.length; i++) {
   1.144 +    result += suites[i].benchmarks.length;
   1.145 +  }
   1.146 +  return result;
   1.147 +}
   1.148 +
   1.149 +
   1.150 +// Computes the geometric mean of a set of numbers.
   1.151 +BenchmarkSuite.GeometricMean = function(numbers) {
   1.152 +  var log = 0;
   1.153 +  for (var i = 0; i < numbers.length; i++) {
   1.154 +    log += Math.log(numbers[i]);
   1.155 +  }
   1.156 +  return Math.pow(Math.E, log / numbers.length);
   1.157 +}
   1.158 +
   1.159 +
   1.160 +// Converts a score value to a string with at least three significant
   1.161 +// digits.
   1.162 +BenchmarkSuite.FormatScore = function(value) {
   1.163 +  if (value > 100) {
   1.164 +    return value.toFixed(0);
   1.165 +  } else {
   1.166 +    return value.toPrecision(3);
   1.167 +  }
   1.168 +}
   1.169 +
   1.170 +// Notifies the runner that we're done running a single benchmark in
   1.171 +// the benchmark suite. This can be useful to report progress.
   1.172 +BenchmarkSuite.prototype.NotifyStep = function(result) {
   1.173 +  this.results.push(result);
   1.174 +  if (this.runner.NotifyStep) this.runner.NotifyStep(result.benchmark.name);
   1.175 +}
   1.176 +
   1.177 +
   1.178 +// Notifies the runner that we're done with running a suite and that
   1.179 +// we have a result which can be reported to the user if needed.
   1.180 +BenchmarkSuite.prototype.NotifyResult = function() {
   1.181 +  var mean = BenchmarkSuite.GeometricMean(this.results);
   1.182 +  var score = this.reference / mean;
   1.183 +  BenchmarkSuite.scores.push(score);
   1.184 +  if (this.runner.NotifyResult) {
   1.185 +    var formatted = BenchmarkSuite.FormatScore(100 * score);
   1.186 +    this.runner.NotifyResult(this.name, formatted);
   1.187 +  }
   1.188 +}
   1.189 +
   1.190 +
   1.191 +// Notifies the runner that running a benchmark resulted in an error.
   1.192 +BenchmarkSuite.prototype.NotifyError = function(error) {
   1.193 +  if (this.runner.NotifyError) {
   1.194 +    this.runner.NotifyError(this.name, error);
   1.195 +  }
   1.196 +  if (this.runner.NotifyStep) {
   1.197 +    this.runner.NotifyStep(this.name);
   1.198 +  }
   1.199 +}
   1.200 +
   1.201 +
   1.202 +// Runs a single benchmark for at least a second and computes the
   1.203 +// average time it takes to run a single iteration.
   1.204 +BenchmarkSuite.prototype.RunSingleBenchmark = function(benchmark) {
   1.205 +  var elapsed = 0;
   1.206 +  var start = new Date();
   1.207 +  for (var n = 0; elapsed < 200; n++) {
   1.208 +    benchmark.run();
   1.209 +    elapsed = new Date() - start;
   1.210 +  }
   1.211 +  var usec = (elapsed * 1000) / n;
   1.212 +  this.NotifyStep(new BenchmarkResult(benchmark, usec));
   1.213 +}
   1.214 +
   1.215 +
   1.216 +// This function starts running a suite, but stops between each
   1.217 +// individual benchmark in the suite and returns a continuation
   1.218 +// function which can be invoked to run the next benchmark. Once the
   1.219 +// last benchmark has been executed, null is returned.
   1.220 +BenchmarkSuite.prototype.RunStep = function(runner) {
   1.221 +  this.results = [];
   1.222 +  this.runner = runner;
   1.223 +  var length = this.benchmarks.length;
   1.224 +  var index = 0;
   1.225 +  var suite = this;
   1.226 +
   1.227 +  // Run the setup, the actual benchmark, and the tear down in three
   1.228 +  // separate steps to allow the framework to yield between any of the
   1.229 +  // steps.
   1.230 +
   1.231 +  function RunNextSetup() {
   1.232 +    if (index < length) {
   1.233 +      try {
   1.234 +        suite.benchmarks[index].Setup();
   1.235 +      } catch (e) {
   1.236 +        suite.NotifyError(e);
   1.237 +        return null;
   1.238 +      }
   1.239 +      return RunNextBenchmark;
   1.240 +    }
   1.241 +    suite.NotifyResult();
   1.242 +    return null;
   1.243 +  }
   1.244 +
   1.245 +  function RunNextBenchmark() {
   1.246 +    try {
   1.247 +      suite.RunSingleBenchmark(suite.benchmarks[index]);
   1.248 +    } catch (e) {
   1.249 +      suite.NotifyError(e);
   1.250 +      return null;
   1.251 +    }
   1.252 +    return RunNextTearDown;
   1.253 +  }
   1.254 +
   1.255 +  function RunNextTearDown() {
   1.256 +    try {
   1.257 +      suite.benchmarks[index++].TearDown();
   1.258 +    } catch (e) {
   1.259 +      suite.NotifyError(e);
   1.260 +      return null;
   1.261 +    }
   1.262 +    return RunNextSetup;
   1.263 +  }
   1.264 +
   1.265 +  // Start out running the setup.
   1.266 +  return RunNextSetup();
   1.267 +}
   1.268 +
   1.269 +
   1.270 +// Copyright 2008 Google Inc. All Rights Reserved.
   1.271 +// Copyright 1996 John Maloney and Mario Wolczko.
   1.272 +
   1.273 +// This program is free software; you can redistribute it and/or modify
   1.274 +// it under the terms of the GNU General Public License as published by
   1.275 +// the Free Software Foundation; either version 2 of the License, or
   1.276 +// (at your option) any later version.
   1.277 +//
   1.278 +// This program is distributed in the hope that it will be useful,
   1.279 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
   1.280 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   1.281 +// GNU General Public License for more details.
   1.282 +//
   1.283 +// You should have received a copy of the GNU General Public License
   1.284 +// along with this program; if not, write to the Free Software
   1.285 +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   1.286 +
   1.287 +
   1.288 +// This implementation of the DeltaBlue benchmark is derived 
   1.289 +// from the Smalltalk implementation by John Maloney and Mario 
   1.290 +// Wolczko. Some parts have been translated directly, whereas 
   1.291 +// others have been modified more aggresively to make it feel 
   1.292 +// more like a JavaScript program.
   1.293 +
   1.294 +
   1.295 +var DeltaBlue = new BenchmarkSuite('DeltaBlue', 71104, [
   1.296 +  new Benchmark('DeltaBlue', deltaBlue)
   1.297 +]);
   1.298 +
   1.299 +
   1.300 +/**
   1.301 + * A JavaScript implementation of the DeltaBlue constrain-solving
   1.302 + * algorithm, as described in:
   1.303 + *
   1.304 + * "The DeltaBlue Algorithm: An Incremental Constraint Hierarchy Solver"
   1.305 + *   Bjorn N. Freeman-Benson and John Maloney
   1.306 + *   January 1990 Communications of the ACM,
   1.307 + *   also available as University of Washington TR 89-08-06.
   1.308 + *
   1.309 + * Beware: this benchmark is written in a grotesque style where
   1.310 + * the constraint model is built by side-effects from constructors.
   1.311 + * I've kept it this way to avoid deviating too much from the original
   1.312 + * implementation.
   1.313 + */
   1.314 +
   1.315 +
   1.316 +/* --- O b j e c t   M o d e l --- */
   1.317 +
   1.318 +Object.prototype.inherits = function (shuper) {
   1.319 +  function Inheriter() { }
   1.320 +  Inheriter.prototype = shuper.prototype;
   1.321 +  this.prototype = new Inheriter();
   1.322 +  this.superConstructor = shuper;
   1.323 +}
   1.324 +
   1.325 +function OrderedCollection() {
   1.326 +  this.elms = new Array();
   1.327 +}
   1.328 +
   1.329 +OrderedCollection.prototype.add = function (elm) {
   1.330 +  this.elms.push(elm);
   1.331 +}
   1.332 +
   1.333 +OrderedCollection.prototype.at = function (index) {
   1.334 +  return this.elms[index];
   1.335 +}
   1.336 +
   1.337 +OrderedCollection.prototype.size = function () {
   1.338 +  return this.elms.length;
   1.339 +}
   1.340 +
   1.341 +OrderedCollection.prototype.removeFirst = function () {
   1.342 +  return this.elms.pop();
   1.343 +}
   1.344 +
   1.345 +OrderedCollection.prototype.remove = function (elm) {
   1.346 +  var index = 0, skipped = 0;
   1.347 +  /* BEGIN LOOP */
   1.348 +  for (var i = 0; i < this.elms.length; i++) {
   1.349 +    var value = this.elms[i];
   1.350 +    if (value != elm) {
   1.351 +      this.elms[index] = value;
   1.352 +      index++;
   1.353 +    } else {
   1.354 +      skipped++;
   1.355 +    }
   1.356 +  }
   1.357 +  /* END LOOP */
   1.358 +  /* BEGIN LOOP */
   1.359 +  for (var i = 0; i < skipped; i++)
   1.360 +    this.elms.pop();
   1.361 +  /* END LOOP */
   1.362 +}
   1.363 +
   1.364 +/* --- *
   1.365 + * S t r e n g t h
   1.366 + * --- */
   1.367 +
   1.368 +/**
   1.369 + * Strengths are used to measure the relative importance of constraints.
   1.370 + * New strengths may be inserted in the strength hierarchy without
   1.371 + * disrupting current constraints.  Strengths cannot be created outside
   1.372 + * this class, so pointer comparison can be used for value comparison.
   1.373 + */
   1.374 +function Strength(strengthValue, name) {
   1.375 +  this.strengthValue = strengthValue;
   1.376 +  this.name = name;
   1.377 +}
   1.378 +
   1.379 +Strength.stronger = function (s1, s2) {
   1.380 +  return s1.strengthValue < s2.strengthValue;
   1.381 +}
   1.382 +
   1.383 +Strength.weaker = function (s1, s2) {
   1.384 +  return s1.strengthValue > s2.strengthValue;
   1.385 +}
   1.386 +
   1.387 +Strength.weakestOf = function (s1, s2) {
   1.388 +  return this.weaker(s1, s2) ? s1 : s2;
   1.389 +}
   1.390 +
   1.391 +Strength.strongest = function (s1, s2) {
   1.392 +  return this.stronger(s1, s2) ? s1 : s2;
   1.393 +}
   1.394 +
   1.395 +Strength.prototype.nextWeaker = function () {
   1.396 +  switch (this.strengthValue) {
   1.397 +    case 0: return Strength.WEAKEST;
   1.398 +    case 1: return Strength.WEAK_DEFAULT;
   1.399 +    case 2: return Strength.NORMAL;
   1.400 +    case 3: return Strength.STRONG_DEFAULT;
   1.401 +    case 4: return Strength.PREFERRED;
   1.402 +    case 5: return Strength.REQUIRED;
   1.403 +  }
   1.404 +}
   1.405 +
   1.406 +// Strength constants.
   1.407 +Strength.REQUIRED        = new Strength(0, "required");
   1.408 +Strength.STONG_PREFERRED = new Strength(1, "strongPreferred");
   1.409 +Strength.PREFERRED       = new Strength(2, "preferred");
   1.410 +Strength.STRONG_DEFAULT  = new Strength(3, "strongDefault");
   1.411 +Strength.NORMAL          = new Strength(4, "normal");
   1.412 +Strength.WEAK_DEFAULT    = new Strength(5, "weakDefault");
   1.413 +Strength.WEAKEST         = new Strength(6, "weakest");
   1.414 +
   1.415 +/* --- *
   1.416 + * C o n s t r a i n t
   1.417 + * --- */
   1.418 +
   1.419 +/**
   1.420 + * An abstract class representing a system-maintainable relationship
   1.421 + * (or "constraint") between a set of variables. A constraint supplies
   1.422 + * a strength instance variable; concrete subclasses provide a means
   1.423 + * of storing the constrained variables and other information required
   1.424 + * to represent a constraint.
   1.425 + */
   1.426 +function Constraint(strength) {
   1.427 +  this.strength = strength;
   1.428 +}
   1.429 +
   1.430 +/**
   1.431 + * Activate this constraint and attempt to satisfy it.
   1.432 + */
   1.433 +Constraint.prototype.addConstraint = function () {
   1.434 +  this.addToGraph();
   1.435 +  planner.incrementalAdd(this);
   1.436 +}
   1.437 +
   1.438 +/**
   1.439 + * Attempt to find a way to enforce this constraint. If successful,
   1.440 + * record the solution, perhaps modifying the current dataflow
   1.441 + * graph. Answer the constraint that this constraint overrides, if
   1.442 + * there is one, or nil, if there isn't.
   1.443 + * Assume: I am not already satisfied.
   1.444 + */
   1.445 +Constraint.prototype.satisfy = function (mark) {
   1.446 +  this.chooseMethod(mark);
   1.447 +  if (!this.isSatisfied()) {
   1.448 +    if (this.strength == Strength.REQUIRED)
   1.449 +      alert("Could not satisfy a required constraint!");
   1.450 +    return null;
   1.451 +  }
   1.452 +  this.markInputs(mark);
   1.453 +  var out = this.output();
   1.454 +  var overridden = out.determinedBy;
   1.455 +  if (overridden != null) overridden.markUnsatisfied();
   1.456 +  out.determinedBy = this;
   1.457 +  if (!planner.addPropagate(this, mark))
   1.458 +    alert("Cycle encountered");
   1.459 +  out.mark = mark;
   1.460 +  return overridden;
   1.461 +}
   1.462 +
   1.463 +Constraint.prototype.destroyConstraint = function () {
   1.464 +  if (this.isSatisfied()) planner.incrementalRemove(this);
   1.465 +  else this.removeFromGraph();
   1.466 +}
   1.467 +
   1.468 +/**
   1.469 + * Normal constraints are not input constraints.  An input constraint
   1.470 + * is one that depends on external state, such as the mouse, the
   1.471 + * keybord, a clock, or some arbitraty piece of imperative code.
   1.472 + */
   1.473 +Constraint.prototype.isInput = function () {
   1.474 +  return false;
   1.475 +}
   1.476 +
   1.477 +/* --- *
   1.478 + * U n a r y   C o n s t r a i n t
   1.479 + * --- */
   1.480 +
   1.481 +/**
   1.482 + * Abstract superclass for constraints having a single possible output
   1.483 + * variable.
   1.484 + */
   1.485 +function UnaryConstraint(v, strength) {
   1.486 +  UnaryConstraint.superConstructor.call(this, strength);
   1.487 +  this.myOutput = v;
   1.488 +  this.satisfied = false;
   1.489 +  this.addConstraint();
   1.490 +}
   1.491 +
   1.492 +UnaryConstraint.inherits(Constraint);
   1.493 +
   1.494 +/**
   1.495 + * Adds this constraint to the constraint graph
   1.496 + */
   1.497 +UnaryConstraint.prototype.addToGraph = function () {
   1.498 +  this.myOutput.addConstraint(this);
   1.499 +  this.satisfied = false;
   1.500 +}
   1.501 +
   1.502 +/**
   1.503 + * Decides if this constraint can be satisfied and records that
   1.504 + * decision.
   1.505 + */
   1.506 +UnaryConstraint.prototype.chooseMethod = function (mark) {
   1.507 +  this.satisfied = (this.myOutput.mark != mark)
   1.508 +    && Strength.stronger(this.strength, this.myOutput.walkStrength);
   1.509 +}
   1.510 +
   1.511 +/**
   1.512 + * Returns true if this constraint is satisfied in the current solution.
   1.513 + */
   1.514 +UnaryConstraint.prototype.isSatisfied = function () {
   1.515 +  return this.satisfied;
   1.516 +}
   1.517 +
   1.518 +UnaryConstraint.prototype.markInputs = function (mark) {
   1.519 +  // has no inputs
   1.520 +}
   1.521 +
   1.522 +/**
   1.523 + * Returns the current output variable.
   1.524 + */
   1.525 +UnaryConstraint.prototype.output = function () {
   1.526 +  return this.myOutput;
   1.527 +}
   1.528 +
   1.529 +/**
   1.530 + * Calculate the walkabout strength, the stay flag, and, if it is
   1.531 + * 'stay', the value for the current output of this constraint. Assume
   1.532 + * this constraint is satisfied.
   1.533 + */
   1.534 +UnaryConstraint.prototype.recalculate = function () {
   1.535 +  this.myOutput.walkStrength = this.strength;
   1.536 +  this.myOutput.stay = !this.isInput();
   1.537 +  if (this.myOutput.stay) this.execute(); // Stay optimization
   1.538 +}
   1.539 +
   1.540 +/**
   1.541 + * Records that this constraint is unsatisfied
   1.542 + */
   1.543 +UnaryConstraint.prototype.markUnsatisfied = function () {
   1.544 +  this.satisfied = false;
   1.545 +}
   1.546 +
   1.547 +UnaryConstraint.prototype.inputsKnown = function () {
   1.548 +  return true;
   1.549 +}
   1.550 +
   1.551 +UnaryConstraint.prototype.removeFromGraph = function () {
   1.552 +  if (this.myOutput != null) this.myOutput.removeConstraint(this);
   1.553 +  this.satisfied = false;
   1.554 +}
   1.555 +
   1.556 +/* --- *
   1.557 + * S t a y   C o n s t r a i n t
   1.558 + * --- */
   1.559 +
   1.560 +/**
   1.561 + * Variables that should, with some level of preference, stay the same.
   1.562 + * Planners may exploit the fact that instances, if satisfied, will not
   1.563 + * change their output during plan execution.  This is called "stay
   1.564 + * optimization".
   1.565 + */
   1.566 +function StayConstraint(v, str) {
   1.567 +  StayConstraint.superConstructor.call(this, v, str);
   1.568 +}
   1.569 +
   1.570 +StayConstraint.inherits(UnaryConstraint);
   1.571 +
   1.572 +StayConstraint.prototype.execute = function () {
   1.573 +  // Stay constraints do nothing
   1.574 +}
   1.575 +
   1.576 +/* --- *
   1.577 + * E d i t   C o n s t r a i n t
   1.578 + * --- */
   1.579 +
   1.580 +/**
   1.581 + * A unary input constraint used to mark a variable that the client
   1.582 + * wishes to change.
   1.583 + */
   1.584 +function EditConstraint(v, str) {
   1.585 +  EditConstraint.superConstructor.call(this, v, str);
   1.586 +}
   1.587 +
   1.588 +EditConstraint.inherits(UnaryConstraint);
   1.589 +
   1.590 +/**
   1.591 + * Edits indicate that a variable is to be changed by imperative code.
   1.592 + */
   1.593 +EditConstraint.prototype.isInput = function () {
   1.594 +  return true;
   1.595 +}
   1.596 +
   1.597 +EditConstraint.prototype.execute = function () {
   1.598 +  // Edit constraints do nothing
   1.599 +}
   1.600 +
   1.601 +/* --- *
   1.602 + * B i n a r y   C o n s t r a i n t
   1.603 + * --- */
   1.604 +
   1.605 +var Direction = new Object();
   1.606 +Direction.NONE     = 0;
   1.607 +Direction.FORWARD  = 1;
   1.608 +Direction.BACKWARD = -1;
   1.609 +
   1.610 +/**
   1.611 + * Abstract superclass for constraints having two possible output
   1.612 + * variables.
   1.613 + */
   1.614 +function BinaryConstraint(var1, var2, strength) {
   1.615 +  BinaryConstraint.superConstructor.call(this, strength);
   1.616 +  this.v1 = var1;
   1.617 +  this.v2 = var2;
   1.618 +  this.direction = Direction.NONE;
   1.619 +  this.addConstraint();
   1.620 +}
   1.621 +
   1.622 +BinaryConstraint.inherits(Constraint);
   1.623 +
   1.624 +/**
   1.625 + * Decides if this constratint can be satisfied and which way it
   1.626 + * should flow based on the relative strength of the variables related,
   1.627 + * and record that decision.
   1.628 + */
   1.629 +BinaryConstraint.prototype.chooseMethod = function (mark) {
   1.630 +  if (this.v1.mark == mark) {
   1.631 +    this.direction = (this.v1.mark != mark && Strength.stronger(this.strength, this.v2.walkStrength))
   1.632 +      ? Direction.FORWARD
   1.633 +      : Direction.NONE;
   1.634 +  }
   1.635 +  if (this.v2.mark == mark) {
   1.636 +    this.direction = (this.v1.mark != mark && Strength.stronger(this.strength, this.v1.walkStrength))
   1.637 +      ? Direction.BACKWARD
   1.638 +      : Direction.NONE;
   1.639 +  }
   1.640 +  if (Strength.weaker(this.v1.walkStrength, this.v2.walkStrength)) {
   1.641 +    this.direction = Strength.stronger(this.strength, this.v1.walkStrength)
   1.642 +      ? Direction.BACKWARD
   1.643 +      : Direction.NONE;
   1.644 +  } else {
   1.645 +    this.direction = Strength.stronger(this.strength, this.v2.walkStrength)
   1.646 +      ? Direction.FORWARD
   1.647 +      : Direction.BACKWARD
   1.648 +  }
   1.649 +}
   1.650 +
   1.651 +/**
   1.652 + * Add this constraint to the constraint graph
   1.653 + */
   1.654 +BinaryConstraint.prototype.addToGraph = function () {
   1.655 +  this.v1.addConstraint(this);
   1.656 +  this.v2.addConstraint(this);
   1.657 +  this.direction = Direction.NONE;
   1.658 +}
   1.659 +
   1.660 +/**
   1.661 + * Answer true if this constraint is satisfied in the current solution.
   1.662 + */
   1.663 +BinaryConstraint.prototype.isSatisfied = function () {
   1.664 +  return this.direction != Direction.NONE;
   1.665 +}
   1.666 +
   1.667 +/**
   1.668 + * Mark the input variable with the given mark.
   1.669 + */
   1.670 +BinaryConstraint.prototype.markInputs = function (mark) {
   1.671 +  this.input().mark = mark;
   1.672 +}
   1.673 +
   1.674 +/**
   1.675 + * Returns the current input variable
   1.676 + */
   1.677 +BinaryConstraint.prototype.input = function () {
   1.678 +  return (this.direction == Direction.FORWARD) ? this.v1 : this.v2;
   1.679 +}
   1.680 +
   1.681 +/**
   1.682 + * Returns the current output variable
   1.683 + */
   1.684 +BinaryConstraint.prototype.output = function () {
   1.685 +  return (this.direction == Direction.FORWARD) ? this.v2 : this.v1;
   1.686 +}
   1.687 +
   1.688 +/**
   1.689 + * Calculate the walkabout strength, the stay flag, and, if it is
   1.690 + * 'stay', the value for the current output of this
   1.691 + * constraint. Assume this constraint is satisfied.
   1.692 + */
   1.693 +BinaryConstraint.prototype.recalculate = function () {
   1.694 +  var ihn = this.input(), out = this.output();
   1.695 +  out.walkStrength = Strength.weakestOf(this.strength, ihn.walkStrength);
   1.696 +  out.stay = ihn.stay;
   1.697 +  if (out.stay) this.execute();
   1.698 +}
   1.699 +
   1.700 +/**
   1.701 + * Record the fact that this constraint is unsatisfied.
   1.702 + */
   1.703 +BinaryConstraint.prototype.markUnsatisfied = function () {
   1.704 +  this.direction = Direction.NONE;
   1.705 +}
   1.706 +
   1.707 +BinaryConstraint.prototype.inputsKnown = function (mark) {
   1.708 +  var i = this.input();
   1.709 +  return i.mark == mark || i.stay || i.determinedBy == null;
   1.710 +}
   1.711 +
   1.712 +BinaryConstraint.prototype.removeFromGraph = function () {
   1.713 +  if (this.v1 != null) this.v1.removeConstraint(this);
   1.714 +  if (this.v2 != null) this.v2.removeConstraint(this);
   1.715 +  this.direction = Direction.NONE;
   1.716 +}
   1.717 +
   1.718 +/* --- *
   1.719 + * S c a l e   C o n s t r a i n t
   1.720 + * --- */
   1.721 +
   1.722 +/**
   1.723 + * Relates two variables by the linear scaling relationship: "v2 =
   1.724 + * (v1 * scale) + offset". Either v1 or v2 may be changed to maintain
   1.725 + * this relationship but the scale factor and offset are considered
   1.726 + * read-only.
   1.727 + */
   1.728 +function ScaleConstraint(src, scale, offset, dest, strength) {
   1.729 +  this.direction = Direction.NONE;
   1.730 +  this.scale = scale;
   1.731 +  this.offset = offset;
   1.732 +  ScaleConstraint.superConstructor.call(this, src, dest, strength);
   1.733 +}
   1.734 +
   1.735 +ScaleConstraint.inherits(BinaryConstraint);
   1.736 +
   1.737 +/**
   1.738 + * Adds this constraint to the constraint graph.
   1.739 + */
   1.740 +ScaleConstraint.prototype.addToGraph = function () {
   1.741 +  ScaleConstraint.superConstructor.prototype.addToGraph.call(this);
   1.742 +  this.scale.addConstraint(this);
   1.743 +  this.offset.addConstraint(this);
   1.744 +}
   1.745 +
   1.746 +ScaleConstraint.prototype.removeFromGraph = function () {
   1.747 +  ScaleConstraint.superConstructor.prototype.removeFromGraph.call(this);
   1.748 +  if (this.scale != null) this.scale.removeConstraint(this);
   1.749 +  if (this.offset != null) this.offset.removeConstraint(this);
   1.750 +}
   1.751 +
   1.752 +ScaleConstraint.prototype.markInputs = function (mark) {
   1.753 +  ScaleConstraint.superConstructor.prototype.markInputs.call(this, mark);
   1.754 +  this.scale.mark = this.offset.mark = mark;
   1.755 +}
   1.756 +
   1.757 +/**
   1.758 + * Enforce this constraint. Assume that it is satisfied.
   1.759 + */
   1.760 +ScaleConstraint.prototype.execute = function () {
   1.761 +  if (this.direction == Direction.FORWARD) {
   1.762 +    this.v2.value = this.v1.value * this.scale.value + this.offset.value;
   1.763 +  } else {
   1.764 +    this.v1.value = (this.v2.value - this.offset.value) / this.scale.value;
   1.765 +  }
   1.766 +}
   1.767 +
   1.768 +/**
   1.769 + * Calculate the walkabout strength, the stay flag, and, if it is
   1.770 + * 'stay', the value for the current output of this constraint. Assume
   1.771 + * this constraint is satisfied.
   1.772 + */
   1.773 +ScaleConstraint.prototype.recalculate = function () {
   1.774 +  var ihn = this.input(), out = this.output();
   1.775 +  out.walkStrength = Strength.weakestOf(this.strength, ihn.walkStrength);
   1.776 +  out.stay = ihn.stay && this.scale.stay && this.offset.stay;
   1.777 +  if (out.stay) this.execute();
   1.778 +}
   1.779 +
   1.780 +/* --- *
   1.781 + * E q u a l i t  y   C o n s t r a i n t
   1.782 + * --- */
   1.783 +
   1.784 +/**
   1.785 + * Constrains two variables to have the same value.
   1.786 + */
   1.787 +function EqualityConstraint(var1, var2, strength) {
   1.788 +  EqualityConstraint.superConstructor.call(this, var1, var2, strength);
   1.789 +}
   1.790 +
   1.791 +EqualityConstraint.inherits(BinaryConstraint);
   1.792 +
   1.793 +/**
   1.794 + * Enforce this constraint. Assume that it is satisfied.
   1.795 + */
   1.796 +EqualityConstraint.prototype.execute = function () {
   1.797 +  this.output().value = this.input().value;
   1.798 +}
   1.799 +
   1.800 +/* --- *
   1.801 + * V a r i a b l e
   1.802 + * --- */
   1.803 +
   1.804 +/**
   1.805 + * A constrained variable. In addition to its value, it maintain the
   1.806 + * structure of the constraint graph, the current dataflow graph, and
   1.807 + * various parameters of interest to the DeltaBlue incremental
   1.808 + * constraint solver.
   1.809 + **/
   1.810 +function Variable(name, initialValue) {
   1.811 +  this.value = initialValue || 0;
   1.812 +  this.constraints = new OrderedCollection();
   1.813 +  this.determinedBy = null;
   1.814 +  this.mark = 0;
   1.815 +  this.walkStrength = Strength.WEAKEST;
   1.816 +  this.stay = true;
   1.817 +  this.name = name;
   1.818 +}
   1.819 +
   1.820 +/**
   1.821 + * Add the given constraint to the set of all constraints that refer
   1.822 + * this variable.
   1.823 + */
   1.824 +Variable.prototype.addConstraint = function (c) {
   1.825 +  this.constraints.add(c);
   1.826 +}
   1.827 +
   1.828 +/**
   1.829 + * Removes all traces of c from this variable.
   1.830 + */
   1.831 +Variable.prototype.removeConstraint = function (c) {
   1.832 +  this.constraints.remove(c);
   1.833 +  if (this.determinedBy == c) this.determinedBy = null;
   1.834 +}
   1.835 +
   1.836 +/* --- *
   1.837 + * P l a n n e r
   1.838 + * --- */
   1.839 +
   1.840 +/**
   1.841 + * The DeltaBlue planner
   1.842 + */
   1.843 +function Planner() {
   1.844 +  this.currentMark = 0;
   1.845 +}
   1.846 +
   1.847 +/**
   1.848 + * Attempt to satisfy the given constraint and, if successful,
   1.849 + * incrementally update the dataflow graph.  Details: If satifying
   1.850 + * the constraint is successful, it may override a weaker constraint
   1.851 + * on its output. The algorithm attempts to resatisfy that
   1.852 + * constraint using some other method. This process is repeated
   1.853 + * until either a) it reaches a variable that was not previously
   1.854 + * determined by any constraint or b) it reaches a constraint that
   1.855 + * is too weak to be satisfied using any of its methods. The
   1.856 + * variables of constraints that have been processed are marked with
   1.857 + * a unique mark value so that we know where we've been. This allows
   1.858 + * the algorithm to avoid getting into an infinite loop even if the
   1.859 + * constraint graph has an inadvertent cycle.
   1.860 + */
   1.861 +Planner.prototype.incrementalAdd = function (c) {
   1.862 +  var mark = this.newMark();
   1.863 +  var overridden = c.satisfy(mark);
   1.864 +  /* BEGIN LOOP */
   1.865 +  while (overridden != null)
   1.866 +    overridden = overridden.satisfy(mark);
   1.867 +  /* END LOOP */
   1.868 +}
   1.869 +
   1.870 +/**
   1.871 + * Entry point for retracting a constraint. Remove the given
   1.872 + * constraint and incrementally update the dataflow graph.
   1.873 + * Details: Retracting the given constraint may allow some currently
   1.874 + * unsatisfiable downstream constraint to be satisfied. We therefore collect
   1.875 + * a list of unsatisfied downstream constraints and attempt to
   1.876 + * satisfy each one in turn. This list is traversed by constraint
   1.877 + * strength, strongest first, as a heuristic for avoiding
   1.878 + * unnecessarily adding and then overriding weak constraints.
   1.879 + * Assume: c is satisfied.
   1.880 + */
   1.881 +Planner.prototype.incrementalRemove = function (c) {
   1.882 +  var out = c.output();
   1.883 +  c.markUnsatisfied();
   1.884 +  c.removeFromGraph();
   1.885 +  var unsatisfied = this.removePropagateFrom(out);
   1.886 +  var strength = Strength.REQUIRED;
   1.887 +  /* BEGIN LOOP */
   1.888 +  do {
   1.889 +  /* BEGIN LOOP */
   1.890 +    for (var i = 0; i < unsatisfied.size(); i++) {
   1.891 +      var u = unsatisfied.at(i);
   1.892 +      if (u.strength == strength)
   1.893 +        this.incrementalAdd(u);
   1.894 +    }
   1.895 +  /* END LOOP */
   1.896 +    strength = strength.nextWeaker();
   1.897 +  } while (strength != Strength.WEAKEST);
   1.898 +  /* END LOOP */
   1.899 +}
   1.900 +
   1.901 +/**
   1.902 + * Select a previously unused mark value.
   1.903 + */
   1.904 +Planner.prototype.newMark = function () {
   1.905 +  return ++this.currentMark;
   1.906 +}
   1.907 +
   1.908 +/**
   1.909 + * Extract a plan for resatisfaction starting from the given source
   1.910 + * constraints, usually a set of input constraints. This method
   1.911 + * assumes that stay optimization is desired; the plan will contain
   1.912 + * only constraints whose output variables are not stay. Constraints
   1.913 + * that do no computation, such as stay and edit constraints, are
   1.914 + * not included in the plan.
   1.915 + * Details: The outputs of a constraint are marked when it is added
   1.916 + * to the plan under construction. A constraint may be appended to
   1.917 + * the plan when all its input variables are known. A variable is
   1.918 + * known if either a) the variable is marked (indicating that has
   1.919 + * been computed by a constraint appearing earlier in the plan), b)
   1.920 + * the variable is 'stay' (i.e. it is a constant at plan execution
   1.921 + * time), or c) the variable is not determined by any
   1.922 + * constraint. The last provision is for past states of history
   1.923 + * variables, which are not stay but which are also not computed by
   1.924 + * any constraint.
   1.925 + * Assume: sources are all satisfied.
   1.926 + */
   1.927 +Planner.prototype.makePlan = function (sources) {
   1.928 +  var mark = this.newMark();
   1.929 +  var plan = new Plan();
   1.930 +  var todo = sources;
   1.931 +  /* BEGIN LOOP */
   1.932 +  while (todo.size() > 0) {
   1.933 +    var c = todo.removeFirst();
   1.934 +    if (c.output().mark != mark && c.inputsKnown(mark)) {
   1.935 +      plan.addConstraint(c);
   1.936 +      c.output().mark = mark;
   1.937 +      this.addConstraintsConsumingTo(c.output(), todo);
   1.938 +    }
   1.939 +  }
   1.940 +  /* END LOOP */
   1.941 +  return plan;
   1.942 +}
   1.943 +
   1.944 +/**
   1.945 + * Extract a plan for resatisfying starting from the output of the
   1.946 + * given constraints, usually a set of input constraints.
   1.947 + */
   1.948 +Planner.prototype.extractPlanFromConstraints = function (constraints) {
   1.949 +  var sources = new OrderedCollection();
   1.950 +  /* BEGIN LOOP */
   1.951 +  for (var i = 0; i < constraints.size(); i++) {
   1.952 +    var c = constraints.at(i);
   1.953 +    if (c.isInput() && c.isSatisfied())
   1.954 +      // not in plan already and eligible for inclusion
   1.955 +      sources.add(c);
   1.956 +  }
   1.957 +  /* END LOOP */
   1.958 +  return this.makePlan(sources);
   1.959 +}
   1.960 +
   1.961 +/**
   1.962 + * Recompute the walkabout strengths and stay flags of all variables
   1.963 + * downstream of the given constraint and recompute the actual
   1.964 + * values of all variables whose stay flag is true. If a cycle is
   1.965 + * detected, remove the given constraint and answer
   1.966 + * false. Otherwise, answer true.
   1.967 + * Details: Cycles are detected when a marked variable is
   1.968 + * encountered downstream of the given constraint. The sender is
   1.969 + * assumed to have marked the inputs of the given constraint with
   1.970 + * the given mark. Thus, encountering a marked node downstream of
   1.971 + * the output constraint means that there is a path from the
   1.972 + * constraint's output to one of its inputs.
   1.973 + */
   1.974 +Planner.prototype.addPropagate = function (c, mark) {
   1.975 +  var todo = new OrderedCollection();
   1.976 +  todo.add(c);
   1.977 +  /* BEGIN LOOP */
   1.978 +  while (todo.size() > 0) {
   1.979 +    var d = todo.removeFirst();
   1.980 +    if (d.output().mark == mark) {
   1.981 +      this.incrementalRemove(c);
   1.982 +      return false;
   1.983 +    }
   1.984 +    d.recalculate();
   1.985 +    this.addConstraintsConsumingTo(d.output(), todo);
   1.986 +  }
   1.987 +  /* END LOOP */
   1.988 +  return true;
   1.989 +}
   1.990 +
   1.991 +
   1.992 +/**
   1.993 + * Update the walkabout strengths and stay flags of all variables
   1.994 + * downstream of the given constraint. Answer a collection of
   1.995 + * unsatisfied constraints sorted in order of decreasing strength.
   1.996 + */
   1.997 +Planner.prototype.removePropagateFrom = function (out) {
   1.998 +  out.determinedBy = null;
   1.999 +  out.walkStrength = Strength.WEAKEST;
  1.1000 +  out.stay = true;
  1.1001 +  var unsatisfied = new OrderedCollection();
  1.1002 +  var todo = new OrderedCollection();
  1.1003 +  todo.add(out);
  1.1004 +  /* BEGIN LOOP */
  1.1005 +  while (todo.size() > 0) {
  1.1006 +    var v = todo.removeFirst();
  1.1007 +  /* BEGIN LOOP */
  1.1008 +    for (var i = 0; i < v.constraints.size(); i++) {
  1.1009 +      var c = v.constraints.at(i);
  1.1010 +      if (!c.isSatisfied())
  1.1011 +        unsatisfied.add(c);
  1.1012 +    }
  1.1013 +  /* END LOOP */
  1.1014 +    var determining = v.determinedBy;
  1.1015 +  /* BEGIN LOOP */
  1.1016 +    for (var i = 0; i < v.constraints.size(); i++) {
  1.1017 +      var next = v.constraints.at(i);
  1.1018 +      if (next != determining && next.isSatisfied()) {
  1.1019 +        next.recalculate();
  1.1020 +        todo.add(next.output());
  1.1021 +      }
  1.1022 +    }
  1.1023 +  /* END LOOP */
  1.1024 +  }
  1.1025 +  /* END LOOP */
  1.1026 +  return unsatisfied;
  1.1027 +}
  1.1028 +
  1.1029 +Planner.prototype.addConstraintsConsumingTo = function (v, coll) {
  1.1030 +  var determining = v.determinedBy;
  1.1031 +  var cc = v.constraints;
  1.1032 +  /* BEGIN LOOP */
  1.1033 +  for (var i = 0; i < cc.size(); i++) {
  1.1034 +    var c = cc.at(i);
  1.1035 +    if (c != determining && c.isSatisfied())
  1.1036 +      coll.add(c);
  1.1037 +  }
  1.1038 +  /* END LOOP */
  1.1039 +}
  1.1040 +
  1.1041 +/* --- *
  1.1042 + * P l a n
  1.1043 + * --- */
  1.1044 +
  1.1045 +/**
  1.1046 + * A Plan is an ordered list of constraints to be executed in sequence
  1.1047 + * to resatisfy all currently satisfiable constraints in the face of
  1.1048 + * one or more changing inputs.
  1.1049 + */
  1.1050 +function Plan() {
  1.1051 +  this.v = new OrderedCollection();
  1.1052 +}
  1.1053 +
  1.1054 +Plan.prototype.addConstraint = function (c) {
  1.1055 +  this.v.add(c);
  1.1056 +}
  1.1057 +
  1.1058 +Plan.prototype.size = function () {
  1.1059 +  return this.v.size();
  1.1060 +}
  1.1061 +
  1.1062 +Plan.prototype.constraintAt = function (index) {
  1.1063 +  return this.v.at(index);
  1.1064 +}
  1.1065 +
  1.1066 +Plan.prototype.execute = function () {
  1.1067 +  /* BEGIN LOOP */
  1.1068 +  for (var i = 0; i < this.size(); i++) {
  1.1069 +    var c = this.constraintAt(i);
  1.1070 +    c.execute();
  1.1071 +  }
  1.1072 +  /* END LOOP */
  1.1073 +}
  1.1074 +
  1.1075 +/* --- *
  1.1076 + * M a i n
  1.1077 + * --- */
  1.1078 +
  1.1079 +/**
  1.1080 + * This is the standard DeltaBlue benchmark. A long chain of equality
  1.1081 + * constraints is constructed with a stay constraint on one end. An
  1.1082 + * edit constraint is then added to the opposite end and the time is
  1.1083 + * measured for adding and removing this constraint, and extracting
  1.1084 + * and executing a constraint satisfaction plan. There are two cases.
  1.1085 + * In case 1, the added constraint is stronger than the stay
  1.1086 + * constraint and values must propagate down the entire length of the
  1.1087 + * chain. In case 2, the added constraint is weaker than the stay
  1.1088 + * constraint so it cannot be accomodated. The cost in this case is,
  1.1089 + * of course, very low. Typical situations lie somewhere between these
  1.1090 + * two extremes.
  1.1091 + */
  1.1092 +function chainTest(n) {
  1.1093 +  planner = new Planner();
  1.1094 +  var prev = null, first = null, last = null;
  1.1095 +
  1.1096 +  // Build chain of n equality constraints
  1.1097 +  /* BEGIN LOOP */
  1.1098 +  for (var i = 0; i <= n; i++) {
  1.1099 +    var name = "v" + i;
  1.1100 +    var v = new Variable(name);
  1.1101 +    if (prev != null)
  1.1102 +      new EqualityConstraint(prev, v, Strength.REQUIRED);
  1.1103 +    if (i == 0) first = v;
  1.1104 +    if (i == n) last = v;
  1.1105 +    prev = v;
  1.1106 +  }
  1.1107 +  /* END LOOP */
  1.1108 +
  1.1109 +  new StayConstraint(last, Strength.STRONG_DEFAULT);
  1.1110 +  var edit = new EditConstraint(first, Strength.PREFERRED);
  1.1111 +  var edits = new OrderedCollection();
  1.1112 +  edits.add(edit);
  1.1113 +  var plan = planner.extractPlanFromConstraints(edits);
  1.1114 +  /* BEGIN LOOP */
  1.1115 +  for (var i = 0; i < 100; i++) {
  1.1116 +    first.value = i;
  1.1117 +    plan.execute();
  1.1118 +    if (last.value != i)
  1.1119 +      alert("Chain test failed.");
  1.1120 +  }
  1.1121 +  /* END LOOP */
  1.1122 +}
  1.1123 +
  1.1124 +/**
  1.1125 + * This test constructs a two sets of variables related to each
  1.1126 + * other by a simple linear transformation (scale and offset). The
  1.1127 + * time is measured to change a variable on either side of the
  1.1128 + * mapping and to change the scale and offset factors.
  1.1129 + */
  1.1130 +function projectionTest(n) {
  1.1131 +  planner = new Planner();
  1.1132 +  var scale = new Variable("scale", 10);
  1.1133 +  var offset = new Variable("offset", 1000);
  1.1134 +  var src = null, dst = null;
  1.1135 +
  1.1136 +  var dests = new OrderedCollection();
  1.1137 +  /* BEGIN LOOP */
  1.1138 +  for (var i = 0; i < n; i++) {
  1.1139 +    src = new Variable("src" + i, i);
  1.1140 +    dst = new Variable("dst" + i, i);
  1.1141 +    dests.add(dst);
  1.1142 +    new StayConstraint(src, Strength.NORMAL);
  1.1143 +    new ScaleConstraint(src, scale, offset, dst, Strength.REQUIRED);
  1.1144 +  }
  1.1145 +  /* END LOOP */
  1.1146 +
  1.1147 +  change(src, 17);
  1.1148 +  if (dst.value != 1170) alert("Projection 1 failed");
  1.1149 +  change(dst, 1050);
  1.1150 +  if (src.value != 5) alert("Projection 2 failed");
  1.1151 +  change(scale, 5);
  1.1152 +  /* BEGIN LOOP */
  1.1153 +  for (var i = 0; i < n - 1; i++) {
  1.1154 +    if (dests.at(i).value != i * 5 + 1000)
  1.1155 +      alert("Projection 3 failed");
  1.1156 +  }
  1.1157 +  /* END LOOP */
  1.1158 +  change(offset, 2000);
  1.1159 +  /* BEGIN LOOP */
  1.1160 +  for (var i = 0; i < n - 1; i++) {
  1.1161 +    if (dests.at(i).value != i * 5 + 2000)
  1.1162 +      alert("Projection 4 failed");
  1.1163 +  }
  1.1164 +  /* END LOOP */
  1.1165 +}
  1.1166 +
  1.1167 +function change(v, newValue) {
  1.1168 +  var edit = new EditConstraint(v, Strength.PREFERRED);
  1.1169 +  var edits = new OrderedCollection();
  1.1170 +  edits.add(edit);
  1.1171 +  var plan = planner.extractPlanFromConstraints(edits);
  1.1172 +  /* BEGIN LOOP */
  1.1173 +  for (var i = 0; i < 10; i++) {
  1.1174 +    v.value = newValue;
  1.1175 +    plan.execute();
  1.1176 +  }
  1.1177 +  /* END LOOP */
  1.1178 +  edit.destroyConstraint();
  1.1179 +}
  1.1180 +
  1.1181 +// Global variable holding the current planner.
  1.1182 +var planner = null;
  1.1183 +
  1.1184 +function deltaBlue() {
  1.1185 +  chainTest(100);
  1.1186 +  projectionTest(100);
  1.1187 +}
  1.1188 +
  1.1189 +function PrintResult(name, result) {
  1.1190 +}
  1.1191 +
  1.1192 +
  1.1193 +function PrintScore(score) {
  1.1194 +}
  1.1195 +
  1.1196 +
  1.1197 +BenchmarkSuite.RunSuites({ NotifyResult: PrintResult,
  1.1198 +                           NotifyScore: PrintScore });
  1.1199 +

mercurial