js/src/jit-test/tests/v8-v5/check-deltablue.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jit-test/tests/v8-v5/check-deltablue.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,883 @@
     1.4 +// Copyright 2008 the V8 project authors. All rights reserved.
     1.5 +// Copyright 1996 John Maloney and Mario Wolczko.
     1.6 +
     1.7 +// This program is free software; you can redistribute it and/or modify
     1.8 +// it under the terms of the GNU General Public License as published by
     1.9 +// the Free Software Foundation; either version 2 of the License, or
    1.10 +// (at your option) any later version.
    1.11 +//
    1.12 +// This program is distributed in the hope that it will be useful,
    1.13 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 +// GNU General Public License for more details.
    1.16 +//
    1.17 +// You should have received a copy of the GNU General Public License
    1.18 +// along with this program; if not, write to the Free Software
    1.19 +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.20 +
    1.21 +
    1.22 +// This implementation of the DeltaBlue benchmark is derived
    1.23 +// from the Smalltalk implementation by John Maloney and Mario
    1.24 +// Wolczko. Some parts have been translated directly, whereas
    1.25 +// others have been modified more aggresively to make it feel
    1.26 +// more like a JavaScript program.
    1.27 +
    1.28 +
    1.29 +//var DeltaBlue = new BenchmarkSuite('DeltaBlue', 71104, [
    1.30 +//  new Benchmark('DeltaBlue', deltaBlue)
    1.31 +//]);
    1.32 +
    1.33 +
    1.34 +/**
    1.35 + * A JavaScript implementation of the DeltaBlue constrain-solving
    1.36 + * algorithm, as described in:
    1.37 + *
    1.38 + * "The DeltaBlue Algorithm: An Incremental Constraint Hierarchy Solver"
    1.39 + *   Bjorn N. Freeman-Benson and John Maloney
    1.40 + *   January 1990 Communications of the ACM,
    1.41 + *   also available as University of Washington TR 89-08-06.
    1.42 + *
    1.43 + * Beware: this benchmark is written in a grotesque style where
    1.44 + * the constraint model is built by side-effects from constructors.
    1.45 + * I've kept it this way to avoid deviating too much from the original
    1.46 + * implementation.
    1.47 + */
    1.48 +
    1.49 +function alert(msg) {
    1.50 +    print(msg);
    1.51 +    assertEq(false, true);
    1.52 +}
    1.53 +
    1.54 +/* --- O b j e c t   M o d e l --- */
    1.55 +
    1.56 +Object.prototype.inheritsFrom = function (shuper) {
    1.57 +  function Inheriter() { }
    1.58 +  Inheriter.prototype = shuper.prototype;
    1.59 +  this.prototype = new Inheriter();
    1.60 +  this.superConstructor = shuper;
    1.61 +}
    1.62 +
    1.63 +function OrderedCollection() {
    1.64 +  this.elms = new Array();
    1.65 +}
    1.66 +
    1.67 +OrderedCollection.prototype.add = function (elm) {
    1.68 +  this.elms.push(elm);
    1.69 +}
    1.70 +
    1.71 +OrderedCollection.prototype.at = function (index) {
    1.72 +  return this.elms[index];
    1.73 +}
    1.74 +
    1.75 +OrderedCollection.prototype.size = function () {
    1.76 +  return this.elms.length;
    1.77 +}
    1.78 +
    1.79 +OrderedCollection.prototype.removeFirst = function () {
    1.80 +  return this.elms.pop();
    1.81 +}
    1.82 +
    1.83 +OrderedCollection.prototype.remove = function (elm) {
    1.84 +  var index = 0, skipped = 0;
    1.85 +  for (var i = 0; i < this.elms.length; i++) {
    1.86 +    var value = this.elms[i];
    1.87 +    if (value != elm) {
    1.88 +      this.elms[index] = value;
    1.89 +      index++;
    1.90 +    } else {
    1.91 +      skipped++;
    1.92 +    }
    1.93 +  }
    1.94 +  for (var i = 0; i < skipped; i++)
    1.95 +    this.elms.pop();
    1.96 +}
    1.97 +
    1.98 +/* --- *
    1.99 + * S t r e n g t h
   1.100 + * --- */
   1.101 +
   1.102 +/**
   1.103 + * Strengths are used to measure the relative importance of constraints.
   1.104 + * New strengths may be inserted in the strength hierarchy without
   1.105 + * disrupting current constraints.  Strengths cannot be created outside
   1.106 + * this class, so pointer comparison can be used for value comparison.
   1.107 + */
   1.108 +function Strength(strengthValue, name) {
   1.109 +  this.strengthValue = strengthValue;
   1.110 +  this.name = name;
   1.111 +}
   1.112 +
   1.113 +Strength.stronger = function (s1, s2) {
   1.114 +  return s1.strengthValue < s2.strengthValue;
   1.115 +}
   1.116 +
   1.117 +Strength.weaker = function (s1, s2) {
   1.118 +  return s1.strengthValue > s2.strengthValue;
   1.119 +}
   1.120 +
   1.121 +Strength.weakestOf = function (s1, s2) {
   1.122 +  return this.weaker(s1, s2) ? s1 : s2;
   1.123 +}
   1.124 +
   1.125 +Strength.strongest = function (s1, s2) {
   1.126 +  return this.stronger(s1, s2) ? s1 : s2;
   1.127 +}
   1.128 +
   1.129 +Strength.prototype.nextWeaker = function () {
   1.130 +  switch (this.strengthValue) {
   1.131 +    case 0: return Strength.WEAKEST;
   1.132 +    case 1: return Strength.WEAK_DEFAULT;
   1.133 +    case 2: return Strength.NORMAL;
   1.134 +    case 3: return Strength.STRONG_DEFAULT;
   1.135 +    case 4: return Strength.PREFERRED;
   1.136 +    case 5: return Strength.REQUIRED;
   1.137 +  }
   1.138 +}
   1.139 +
   1.140 +// Strength constants.
   1.141 +Strength.REQUIRED        = new Strength(0, "required");
   1.142 +Strength.STONG_PREFERRED = new Strength(1, "strongPreferred");
   1.143 +Strength.PREFERRED       = new Strength(2, "preferred");
   1.144 +Strength.STRONG_DEFAULT  = new Strength(3, "strongDefault");
   1.145 +Strength.NORMAL          = new Strength(4, "normal");
   1.146 +Strength.WEAK_DEFAULT    = new Strength(5, "weakDefault");
   1.147 +Strength.WEAKEST         = new Strength(6, "weakest");
   1.148 +
   1.149 +/* --- *
   1.150 + * C o n s t r a i n t
   1.151 + * --- */
   1.152 +
   1.153 +/**
   1.154 + * An abstract class representing a system-maintainable relationship
   1.155 + * (or "constraint") between a set of variables. A constraint supplies
   1.156 + * a strength instance variable; concrete subclasses provide a means
   1.157 + * of storing the constrained variables and other information required
   1.158 + * to represent a constraint.
   1.159 + */
   1.160 +function Constraint(strength) {
   1.161 +  this.strength = strength;
   1.162 +}
   1.163 +
   1.164 +/**
   1.165 + * Activate this constraint and attempt to satisfy it.
   1.166 + */
   1.167 +Constraint.prototype.addConstraint = function () {
   1.168 +  this.addToGraph();
   1.169 +  planner.incrementalAdd(this);
   1.170 +}
   1.171 +
   1.172 +/**
   1.173 + * Attempt to find a way to enforce this constraint. If successful,
   1.174 + * record the solution, perhaps modifying the current dataflow
   1.175 + * graph. Answer the constraint that this constraint overrides, if
   1.176 + * there is one, or nil, if there isn't.
   1.177 + * Assume: I am not already satisfied.
   1.178 + */
   1.179 +Constraint.prototype.satisfy = function (mark) {
   1.180 +  this.chooseMethod(mark);
   1.181 +  if (!this.isSatisfied()) {
   1.182 +    if (this.strength == Strength.REQUIRED)
   1.183 +      alert("Could not satisfy a required constraint!");
   1.184 +    return null;
   1.185 +  }
   1.186 +  this.markInputs(mark);
   1.187 +  var out = this.output();
   1.188 +  var overridden = out.determinedBy;
   1.189 +  if (overridden != null) overridden.markUnsatisfied();
   1.190 +  out.determinedBy = this;
   1.191 +  if (!planner.addPropagate(this, mark))
   1.192 +    alert("Cycle encountered");
   1.193 +  out.mark = mark;
   1.194 +  return overridden;
   1.195 +}
   1.196 +
   1.197 +Constraint.prototype.destroyConstraint = function () {
   1.198 +  if (this.isSatisfied()) planner.incrementalRemove(this);
   1.199 +  else this.removeFromGraph();
   1.200 +}
   1.201 +
   1.202 +/**
   1.203 + * Normal constraints are not input constraints.  An input constraint
   1.204 + * is one that depends on external state, such as the mouse, the
   1.205 + * keybord, a clock, or some arbitraty piece of imperative code.
   1.206 + */
   1.207 +Constraint.prototype.isInput = function () {
   1.208 +  return false;
   1.209 +}
   1.210 +
   1.211 +/* --- *
   1.212 + * U n a r y   C o n s t r a i n t
   1.213 + * --- */
   1.214 +
   1.215 +/**
   1.216 + * Abstract superclass for constraints having a single possible output
   1.217 + * variable.
   1.218 + */
   1.219 +function UnaryConstraint(v, strength) {
   1.220 +  UnaryConstraint.superConstructor.call(this, strength);
   1.221 +  this.myOutput = v;
   1.222 +  this.satisfied = false;
   1.223 +  this.addConstraint();
   1.224 +}
   1.225 +
   1.226 +UnaryConstraint.inheritsFrom(Constraint);
   1.227 +
   1.228 +/**
   1.229 + * Adds this constraint to the constraint graph
   1.230 + */
   1.231 +UnaryConstraint.prototype.addToGraph = function () {
   1.232 +  this.myOutput.addConstraint(this);
   1.233 +  this.satisfied = false;
   1.234 +}
   1.235 +
   1.236 +/**
   1.237 + * Decides if this constraint can be satisfied and records that
   1.238 + * decision.
   1.239 + */
   1.240 +UnaryConstraint.prototype.chooseMethod = function (mark) {
   1.241 +  this.satisfied = (this.myOutput.mark != mark)
   1.242 +    && Strength.stronger(this.strength, this.myOutput.walkStrength);
   1.243 +}
   1.244 +
   1.245 +/**
   1.246 + * Returns true if this constraint is satisfied in the current solution.
   1.247 + */
   1.248 +UnaryConstraint.prototype.isSatisfied = function () {
   1.249 +  return this.satisfied;
   1.250 +}
   1.251 +
   1.252 +UnaryConstraint.prototype.markInputs = function (mark) {
   1.253 +  // has no inputs
   1.254 +}
   1.255 +
   1.256 +/**
   1.257 + * Returns the current output variable.
   1.258 + */
   1.259 +UnaryConstraint.prototype.output = function () {
   1.260 +  return this.myOutput;
   1.261 +}
   1.262 +
   1.263 +/**
   1.264 + * Calculate the walkabout strength, the stay flag, and, if it is
   1.265 + * 'stay', the value for the current output of this constraint. Assume
   1.266 + * this constraint is satisfied.
   1.267 + */
   1.268 +UnaryConstraint.prototype.recalculate = function () {
   1.269 +  this.myOutput.walkStrength = this.strength;
   1.270 +  this.myOutput.stay = !this.isInput();
   1.271 +  if (this.myOutput.stay) this.execute(); // Stay optimization
   1.272 +}
   1.273 +
   1.274 +/**
   1.275 + * Records that this constraint is unsatisfied
   1.276 + */
   1.277 +UnaryConstraint.prototype.markUnsatisfied = function () {
   1.278 +  this.satisfied = false;
   1.279 +}
   1.280 +
   1.281 +UnaryConstraint.prototype.inputsKnown = function () {
   1.282 +  return true;
   1.283 +}
   1.284 +
   1.285 +UnaryConstraint.prototype.removeFromGraph = function () {
   1.286 +  if (this.myOutput != null) this.myOutput.removeConstraint(this);
   1.287 +  this.satisfied = false;
   1.288 +}
   1.289 +
   1.290 +/* --- *
   1.291 + * S t a y   C o n s t r a i n t
   1.292 + * --- */
   1.293 +
   1.294 +/**
   1.295 + * Variables that should, with some level of preference, stay the same.
   1.296 + * Planners may exploit the fact that instances, if satisfied, will not
   1.297 + * change their output during plan execution.  This is called "stay
   1.298 + * optimization".
   1.299 + */
   1.300 +function StayConstraint(v, str) {
   1.301 +  StayConstraint.superConstructor.call(this, v, str);
   1.302 +}
   1.303 +
   1.304 +StayConstraint.inheritsFrom(UnaryConstraint);
   1.305 +
   1.306 +StayConstraint.prototype.execute = function () {
   1.307 +  // Stay constraints do nothing
   1.308 +}
   1.309 +
   1.310 +/* --- *
   1.311 + * E d i t   C o n s t r a i n t
   1.312 + * --- */
   1.313 +
   1.314 +/**
   1.315 + * A unary input constraint used to mark a variable that the client
   1.316 + * wishes to change.
   1.317 + */
   1.318 +function EditConstraint(v, str) {
   1.319 +  EditConstraint.superConstructor.call(this, v, str);
   1.320 +}
   1.321 +
   1.322 +EditConstraint.inheritsFrom(UnaryConstraint);
   1.323 +
   1.324 +/**
   1.325 + * Edits indicate that a variable is to be changed by imperative code.
   1.326 + */
   1.327 +EditConstraint.prototype.isInput = function () {
   1.328 +  return true;
   1.329 +}
   1.330 +
   1.331 +EditConstraint.prototype.execute = function () {
   1.332 +  // Edit constraints do nothing
   1.333 +}
   1.334 +
   1.335 +/* --- *
   1.336 + * B i n a r y   C o n s t r a i n t
   1.337 + * --- */
   1.338 +
   1.339 +var Direction = new Object();
   1.340 +Direction.NONE     = 0;
   1.341 +Direction.FORWARD  = 1;
   1.342 +Direction.BACKWARD = -1;
   1.343 +
   1.344 +/**
   1.345 + * Abstract superclass for constraints having two possible output
   1.346 + * variables.
   1.347 + */
   1.348 +function BinaryConstraint(var1, var2, strength) {
   1.349 +  BinaryConstraint.superConstructor.call(this, strength);
   1.350 +  this.v1 = var1;
   1.351 +  this.v2 = var2;
   1.352 +  this.direction = Direction.NONE;
   1.353 +  this.addConstraint();
   1.354 +}
   1.355 +
   1.356 +BinaryConstraint.inheritsFrom(Constraint);
   1.357 +
   1.358 +/**
   1.359 + * Decides if this constratint can be satisfied and which way it
   1.360 + * should flow based on the relative strength of the variables related,
   1.361 + * and record that decision.
   1.362 + */
   1.363 +BinaryConstraint.prototype.chooseMethod = function (mark) {
   1.364 +  if (this.v1.mark == mark) {
   1.365 +    this.direction = (this.v1.mark != mark && Strength.stronger(this.strength, this.v2.walkStrength))
   1.366 +      ? Direction.FORWARD
   1.367 +      : Direction.NONE;
   1.368 +  }
   1.369 +  if (this.v2.mark == mark) {
   1.370 +    this.direction = (this.v1.mark != mark && Strength.stronger(this.strength, this.v1.walkStrength))
   1.371 +      ? Direction.BACKWARD
   1.372 +      : Direction.NONE;
   1.373 +  }
   1.374 +  if (Strength.weaker(this.v1.walkStrength, this.v2.walkStrength)) {
   1.375 +    this.direction = Strength.stronger(this.strength, this.v1.walkStrength)
   1.376 +      ? Direction.BACKWARD
   1.377 +      : Direction.NONE;
   1.378 +  } else {
   1.379 +    this.direction = Strength.stronger(this.strength, this.v2.walkStrength)
   1.380 +      ? Direction.FORWARD
   1.381 +      : Direction.BACKWARD
   1.382 +  }
   1.383 +}
   1.384 +
   1.385 +/**
   1.386 + * Add this constraint to the constraint graph
   1.387 + */
   1.388 +BinaryConstraint.prototype.addToGraph = function () {
   1.389 +  this.v1.addConstraint(this);
   1.390 +  this.v2.addConstraint(this);
   1.391 +  this.direction = Direction.NONE;
   1.392 +}
   1.393 +
   1.394 +/**
   1.395 + * Answer true if this constraint is satisfied in the current solution.
   1.396 + */
   1.397 +BinaryConstraint.prototype.isSatisfied = function () {
   1.398 +  return this.direction != Direction.NONE;
   1.399 +}
   1.400 +
   1.401 +/**
   1.402 + * Mark the input variable with the given mark.
   1.403 + */
   1.404 +BinaryConstraint.prototype.markInputs = function (mark) {
   1.405 +  this.input().mark = mark;
   1.406 +}
   1.407 +
   1.408 +/**
   1.409 + * Returns the current input variable
   1.410 + */
   1.411 +BinaryConstraint.prototype.input = function () {
   1.412 +  return (this.direction == Direction.FORWARD) ? this.v1 : this.v2;
   1.413 +}
   1.414 +
   1.415 +/**
   1.416 + * Returns the current output variable
   1.417 + */
   1.418 +BinaryConstraint.prototype.output = function () {
   1.419 +  return (this.direction == Direction.FORWARD) ? this.v2 : this.v1;
   1.420 +}
   1.421 +
   1.422 +/**
   1.423 + * Calculate the walkabout strength, the stay flag, and, if it is
   1.424 + * 'stay', the value for the current output of this
   1.425 + * constraint. Assume this constraint is satisfied.
   1.426 + */
   1.427 +BinaryConstraint.prototype.recalculate = function () {
   1.428 +  var ihn = this.input(), out = this.output();
   1.429 +  out.walkStrength = Strength.weakestOf(this.strength, ihn.walkStrength);
   1.430 +  out.stay = ihn.stay;
   1.431 +  if (out.stay) this.execute();
   1.432 +}
   1.433 +
   1.434 +/**
   1.435 + * Record the fact that this constraint is unsatisfied.
   1.436 + */
   1.437 +BinaryConstraint.prototype.markUnsatisfied = function () {
   1.438 +  this.direction = Direction.NONE;
   1.439 +}
   1.440 +
   1.441 +BinaryConstraint.prototype.inputsKnown = function (mark) {
   1.442 +  var i = this.input();
   1.443 +  return i.mark == mark || i.stay || i.determinedBy == null;
   1.444 +}
   1.445 +
   1.446 +BinaryConstraint.prototype.removeFromGraph = function () {
   1.447 +  if (this.v1 != null) this.v1.removeConstraint(this);
   1.448 +  if (this.v2 != null) this.v2.removeConstraint(this);
   1.449 +  this.direction = Direction.NONE;
   1.450 +}
   1.451 +
   1.452 +/* --- *
   1.453 + * S c a l e   C o n s t r a i n t
   1.454 + * --- */
   1.455 +
   1.456 +/**
   1.457 + * Relates two variables by the linear scaling relationship: "v2 =
   1.458 + * (v1 * scale) + offset". Either v1 or v2 may be changed to maintain
   1.459 + * this relationship but the scale factor and offset are considered
   1.460 + * read-only.
   1.461 + */
   1.462 +function ScaleConstraint(src, scale, offset, dest, strength) {
   1.463 +  this.direction = Direction.NONE;
   1.464 +  this.scale = scale;
   1.465 +  this.offset = offset;
   1.466 +  ScaleConstraint.superConstructor.call(this, src, dest, strength);
   1.467 +}
   1.468 +
   1.469 +ScaleConstraint.inheritsFrom(BinaryConstraint);
   1.470 +
   1.471 +/**
   1.472 + * Adds this constraint to the constraint graph.
   1.473 + */
   1.474 +ScaleConstraint.prototype.addToGraph = function () {
   1.475 +  ScaleConstraint.superConstructor.prototype.addToGraph.call(this);
   1.476 +  this.scale.addConstraint(this);
   1.477 +  this.offset.addConstraint(this);
   1.478 +}
   1.479 +
   1.480 +ScaleConstraint.prototype.removeFromGraph = function () {
   1.481 +  ScaleConstraint.superConstructor.prototype.removeFromGraph.call(this);
   1.482 +  if (this.scale != null) this.scale.removeConstraint(this);
   1.483 +  if (this.offset != null) this.offset.removeConstraint(this);
   1.484 +}
   1.485 +
   1.486 +ScaleConstraint.prototype.markInputs = function (mark) {
   1.487 +  ScaleConstraint.superConstructor.prototype.markInputs.call(this, mark);
   1.488 +  this.scale.mark = this.offset.mark = mark;
   1.489 +}
   1.490 +
   1.491 +/**
   1.492 + * Enforce this constraint. Assume that it is satisfied.
   1.493 + */
   1.494 +ScaleConstraint.prototype.execute = function () {
   1.495 +  if (this.direction == Direction.FORWARD) {
   1.496 +    this.v2.value = this.v1.value * this.scale.value + this.offset.value;
   1.497 +  } else {
   1.498 +    this.v1.value = (this.v2.value - this.offset.value) / this.scale.value;
   1.499 +  }
   1.500 +}
   1.501 +
   1.502 +/**
   1.503 + * Calculate the walkabout strength, the stay flag, and, if it is
   1.504 + * 'stay', the value for the current output of this constraint. Assume
   1.505 + * this constraint is satisfied.
   1.506 + */
   1.507 +ScaleConstraint.prototype.recalculate = function () {
   1.508 +  var ihn = this.input(), out = this.output();
   1.509 +  out.walkStrength = Strength.weakestOf(this.strength, ihn.walkStrength);
   1.510 +  out.stay = ihn.stay && this.scale.stay && this.offset.stay;
   1.511 +  if (out.stay) this.execute();
   1.512 +}
   1.513 +
   1.514 +/* --- *
   1.515 + * E q u a l i t  y   C o n s t r a i n t
   1.516 + * --- */
   1.517 +
   1.518 +/**
   1.519 + * Constrains two variables to have the same value.
   1.520 + */
   1.521 +function EqualityConstraint(var1, var2, strength) {
   1.522 +  EqualityConstraint.superConstructor.call(this, var1, var2, strength);
   1.523 +}
   1.524 +
   1.525 +EqualityConstraint.inheritsFrom(BinaryConstraint);
   1.526 +
   1.527 +/**
   1.528 + * Enforce this constraint. Assume that it is satisfied.
   1.529 + */
   1.530 +EqualityConstraint.prototype.execute = function () {
   1.531 +  this.output().value = this.input().value;
   1.532 +}
   1.533 +
   1.534 +/* --- *
   1.535 + * V a r i a b l e
   1.536 + * --- */
   1.537 +
   1.538 +/**
   1.539 + * A constrained variable. In addition to its value, it maintain the
   1.540 + * structure of the constraint graph, the current dataflow graph, and
   1.541 + * various parameters of interest to the DeltaBlue incremental
   1.542 + * constraint solver.
   1.543 + **/
   1.544 +function Variable(name, initialValue) {
   1.545 +  this.value = initialValue || 0;
   1.546 +  this.constraints = new OrderedCollection();
   1.547 +  this.determinedBy = null;
   1.548 +  this.mark = 0;
   1.549 +  this.walkStrength = Strength.WEAKEST;
   1.550 +  this.stay = true;
   1.551 +  this.name = name;
   1.552 +}
   1.553 +
   1.554 +/**
   1.555 + * Add the given constraint to the set of all constraints that refer
   1.556 + * this variable.
   1.557 + */
   1.558 +Variable.prototype.addConstraint = function (c) {
   1.559 +  this.constraints.add(c);
   1.560 +}
   1.561 +
   1.562 +/**
   1.563 + * Removes all traces of c from this variable.
   1.564 + */
   1.565 +Variable.prototype.removeConstraint = function (c) {
   1.566 +  this.constraints.remove(c);
   1.567 +  if (this.determinedBy == c) this.determinedBy = null;
   1.568 +}
   1.569 +
   1.570 +/* --- *
   1.571 + * P l a n n e r
   1.572 + * --- */
   1.573 +
   1.574 +/**
   1.575 + * The DeltaBlue planner
   1.576 + */
   1.577 +function Planner() {
   1.578 +  this.currentMark = 0;
   1.579 +}
   1.580 +
   1.581 +/**
   1.582 + * Attempt to satisfy the given constraint and, if successful,
   1.583 + * incrementally update the dataflow graph.  Details: If satifying
   1.584 + * the constraint is successful, it may override a weaker constraint
   1.585 + * on its output. The algorithm attempts to resatisfy that
   1.586 + * constraint using some other method. This process is repeated
   1.587 + * until either a) it reaches a variable that was not previously
   1.588 + * determined by any constraint or b) it reaches a constraint that
   1.589 + * is too weak to be satisfied using any of its methods. The
   1.590 + * variables of constraints that have been processed are marked with
   1.591 + * a unique mark value so that we know where we've been. This allows
   1.592 + * the algorithm to avoid getting into an infinite loop even if the
   1.593 + * constraint graph has an inadvertent cycle.
   1.594 + */
   1.595 +Planner.prototype.incrementalAdd = function (c) {
   1.596 +  var mark = this.newMark();
   1.597 +  var overridden = c.satisfy(mark);
   1.598 +  while (overridden != null)
   1.599 +    overridden = overridden.satisfy(mark);
   1.600 +}
   1.601 +
   1.602 +/**
   1.603 + * Entry point for retracting a constraint. Remove the given
   1.604 + * constraint and incrementally update the dataflow graph.
   1.605 + * Details: Retracting the given constraint may allow some currently
   1.606 + * unsatisfiable downstream constraint to be satisfied. We therefore collect
   1.607 + * a list of unsatisfied downstream constraints and attempt to
   1.608 + * satisfy each one in turn. This list is traversed by constraint
   1.609 + * strength, strongest first, as a heuristic for avoiding
   1.610 + * unnecessarily adding and then overriding weak constraints.
   1.611 + * Assume: c is satisfied.
   1.612 + */
   1.613 +Planner.prototype.incrementalRemove = function (c) {
   1.614 +  var out = c.output();
   1.615 +  c.markUnsatisfied();
   1.616 +  c.removeFromGraph();
   1.617 +  var unsatisfied = this.removePropagateFrom(out);
   1.618 +  var strength = Strength.REQUIRED;
   1.619 +  do {
   1.620 +    for (var i = 0; i < unsatisfied.size(); i++) {
   1.621 +      var u = unsatisfied.at(i);
   1.622 +      if (u.strength == strength)
   1.623 +        this.incrementalAdd(u);
   1.624 +    }
   1.625 +    strength = strength.nextWeaker();
   1.626 +  } while (strength != Strength.WEAKEST);
   1.627 +}
   1.628 +
   1.629 +/**
   1.630 + * Select a previously unused mark value.
   1.631 + */
   1.632 +Planner.prototype.newMark = function () {
   1.633 +  return ++this.currentMark;
   1.634 +}
   1.635 +
   1.636 +/**
   1.637 + * Extract a plan for resatisfaction starting from the given source
   1.638 + * constraints, usually a set of input constraints. This method
   1.639 + * assumes that stay optimization is desired; the plan will contain
   1.640 + * only constraints whose output variables are not stay. Constraints
   1.641 + * that do no computation, such as stay and edit constraints, are
   1.642 + * not included in the plan.
   1.643 + * Details: The outputs of a constraint are marked when it is added
   1.644 + * to the plan under construction. A constraint may be appended to
   1.645 + * the plan when all its input variables are known. A variable is
   1.646 + * known if either a) the variable is marked (indicating that has
   1.647 + * been computed by a constraint appearing earlier in the plan), b)
   1.648 + * the variable is 'stay' (i.e. it is a constant at plan execution
   1.649 + * time), or c) the variable is not determined by any
   1.650 + * constraint. The last provision is for past states of history
   1.651 + * variables, which are not stay but which are also not computed by
   1.652 + * any constraint.
   1.653 + * Assume: sources are all satisfied.
   1.654 + */
   1.655 +Planner.prototype.makePlan = function (sources) {
   1.656 +  var mark = this.newMark();
   1.657 +  var plan = new Plan();
   1.658 +  var todo = sources;
   1.659 +  while (todo.size() > 0) {
   1.660 +    var c = todo.removeFirst();
   1.661 +    if (c.output().mark != mark && c.inputsKnown(mark)) {
   1.662 +      plan.addConstraint(c);
   1.663 +      c.output().mark = mark;
   1.664 +      this.addConstraintsConsumingTo(c.output(), todo);
   1.665 +    }
   1.666 +  }
   1.667 +  return plan;
   1.668 +}
   1.669 +
   1.670 +/**
   1.671 + * Extract a plan for resatisfying starting from the output of the
   1.672 + * given constraints, usually a set of input constraints.
   1.673 + */
   1.674 +Planner.prototype.extractPlanFromConstraints = function (constraints) {
   1.675 +  var sources = new OrderedCollection();
   1.676 +  for (var i = 0; i < constraints.size(); i++) {
   1.677 +    var c = constraints.at(i);
   1.678 +    if (c.isInput() && c.isSatisfied())
   1.679 +      // not in plan already and eligible for inclusion
   1.680 +      sources.add(c);
   1.681 +  }
   1.682 +  return this.makePlan(sources);
   1.683 +}
   1.684 +
   1.685 +/**
   1.686 + * Recompute the walkabout strengths and stay flags of all variables
   1.687 + * downstream of the given constraint and recompute the actual
   1.688 + * values of all variables whose stay flag is true. If a cycle is
   1.689 + * detected, remove the given constraint and answer
   1.690 + * false. Otherwise, answer true.
   1.691 + * Details: Cycles are detected when a marked variable is
   1.692 + * encountered downstream of the given constraint. The sender is
   1.693 + * assumed to have marked the inputs of the given constraint with
   1.694 + * the given mark. Thus, encountering a marked node downstream of
   1.695 + * the output constraint means that there is a path from the
   1.696 + * constraint's output to one of its inputs.
   1.697 + */
   1.698 +Planner.prototype.addPropagate = function (c, mark) {
   1.699 +  var todo = new OrderedCollection();
   1.700 +  todo.add(c);
   1.701 +  while (todo.size() > 0) {
   1.702 +    var d = todo.removeFirst();
   1.703 +    if (d.output().mark == mark) {
   1.704 +      this.incrementalRemove(c);
   1.705 +      return false;
   1.706 +    }
   1.707 +    d.recalculate();
   1.708 +    this.addConstraintsConsumingTo(d.output(), todo);
   1.709 +  }
   1.710 +  return true;
   1.711 +}
   1.712 +
   1.713 +
   1.714 +/**
   1.715 + * Update the walkabout strengths and stay flags of all variables
   1.716 + * downstream of the given constraint. Answer a collection of
   1.717 + * unsatisfied constraints sorted in order of decreasing strength.
   1.718 + */
   1.719 +Planner.prototype.removePropagateFrom = function (out) {
   1.720 +  out.determinedBy = null;
   1.721 +  out.walkStrength = Strength.WEAKEST;
   1.722 +  out.stay = true;
   1.723 +  var unsatisfied = new OrderedCollection();
   1.724 +  var todo = new OrderedCollection();
   1.725 +  todo.add(out);
   1.726 +  while (todo.size() > 0) {
   1.727 +    var v = todo.removeFirst();
   1.728 +    for (var i = 0; i < v.constraints.size(); i++) {
   1.729 +      var c = v.constraints.at(i);
   1.730 +      if (!c.isSatisfied())
   1.731 +        unsatisfied.add(c);
   1.732 +    }
   1.733 +    var determining = v.determinedBy;
   1.734 +    for (var i = 0; i < v.constraints.size(); i++) {
   1.735 +      var next = v.constraints.at(i);
   1.736 +      if (next != determining && next.isSatisfied()) {
   1.737 +        next.recalculate();
   1.738 +        todo.add(next.output());
   1.739 +      }
   1.740 +    }
   1.741 +  }
   1.742 +  return unsatisfied;
   1.743 +}
   1.744 +
   1.745 +Planner.prototype.addConstraintsConsumingTo = function (v, coll) {
   1.746 +  var determining = v.determinedBy;
   1.747 +  var cc = v.constraints;
   1.748 +  for (var i = 0; i < cc.size(); i++) {
   1.749 +    var c = cc.at(i);
   1.750 +    if (c != determining && c.isSatisfied())
   1.751 +      coll.add(c);
   1.752 +  }
   1.753 +}
   1.754 +
   1.755 +/* --- *
   1.756 + * P l a n
   1.757 + * --- */
   1.758 +
   1.759 +/**
   1.760 + * A Plan is an ordered list of constraints to be executed in sequence
   1.761 + * to resatisfy all currently satisfiable constraints in the face of
   1.762 + * one or more changing inputs.
   1.763 + */
   1.764 +function Plan() {
   1.765 +  this.v = new OrderedCollection();
   1.766 +}
   1.767 +
   1.768 +Plan.prototype.addConstraint = function (c) {
   1.769 +  this.v.add(c);
   1.770 +}
   1.771 +
   1.772 +Plan.prototype.size = function () {
   1.773 +  return this.v.size();
   1.774 +}
   1.775 +
   1.776 +Plan.prototype.constraintAt = function (index) {
   1.777 +  return this.v.at(index);
   1.778 +}
   1.779 +
   1.780 +Plan.prototype.execute = function () {
   1.781 +  for (var i = 0; i < this.size(); i++) {
   1.782 +    var c = this.constraintAt(i);
   1.783 +    c.execute();
   1.784 +  }
   1.785 +}
   1.786 +
   1.787 +/* --- *
   1.788 + * M a i n
   1.789 + * --- */
   1.790 +
   1.791 +/**
   1.792 + * This is the standard DeltaBlue benchmark. A long chain of equality
   1.793 + * constraints is constructed with a stay constraint on one end. An
   1.794 + * edit constraint is then added to the opposite end and the time is
   1.795 + * measured for adding and removing this constraint, and extracting
   1.796 + * and executing a constraint satisfaction plan. There are two cases.
   1.797 + * In case 1, the added constraint is stronger than the stay
   1.798 + * constraint and values must propagate down the entire length of the
   1.799 + * chain. In case 2, the added constraint is weaker than the stay
   1.800 + * constraint so it cannot be accomodated. The cost in this case is,
   1.801 + * of course, very low. Typical situations lie somewhere between these
   1.802 + * two extremes.
   1.803 + */
   1.804 +function chainTest(n) {
   1.805 +  planner = new Planner();
   1.806 +  var prev = null, first = null, last = null;
   1.807 +
   1.808 +  // Build chain of n equality constraints
   1.809 +  for (var i = 0; i <= n; i++) {
   1.810 +    var name = "v" + i;
   1.811 +    var v = new Variable(name);
   1.812 +    if (prev != null)
   1.813 +      new EqualityConstraint(prev, v, Strength.REQUIRED);
   1.814 +    if (i == 0) first = v;
   1.815 +    if (i == n) last = v;
   1.816 +    prev = v;
   1.817 +  }
   1.818 +
   1.819 +  new StayConstraint(last, Strength.STRONG_DEFAULT);
   1.820 +  var edit = new EditConstraint(first, Strength.PREFERRED);
   1.821 +  var edits = new OrderedCollection();
   1.822 +  edits.add(edit);
   1.823 +  var plan = planner.extractPlanFromConstraints(edits);
   1.824 +  for (var i = 0; i < 100; i++) {
   1.825 +    first.value = i;
   1.826 +    plan.execute();
   1.827 +    assertEq(last.value, i);
   1.828 +  }
   1.829 +}
   1.830 +
   1.831 +/**
   1.832 + * This test constructs a two sets of variables related to each
   1.833 + * other by a simple linear transformation (scale and offset). The
   1.834 + * time is measured to change a variable on either side of the
   1.835 + * mapping and to change the scale and offset factors.
   1.836 + */
   1.837 +function projectionTest(n) {
   1.838 +  planner = new Planner();
   1.839 +  var scale = new Variable("scale", 10);
   1.840 +  var offset = new Variable("offset", 1000);
   1.841 +  var src = null, dst = null;
   1.842 +
   1.843 +  var dests = new OrderedCollection();
   1.844 +  for (var i = 0; i < n; i++) {
   1.845 +    src = new Variable("src" + i, i);
   1.846 +    dst = new Variable("dst" + i, i);
   1.847 +    dests.add(dst);
   1.848 +    new StayConstraint(src, Strength.NORMAL);
   1.849 +    new ScaleConstraint(src, scale, offset, dst, Strength.REQUIRED);
   1.850 +  }
   1.851 +
   1.852 +  change(src, 17);
   1.853 +  assertEq(dst.value, 1170);
   1.854 +  change(dst, 1050);
   1.855 +  assertEq(src.value, 5);
   1.856 +  change(scale, 5);
   1.857 +  for (var i = 0; i < n - 1; i++) {
   1.858 +    assertEq(dests.at(i).value, i * 5 + 1000);
   1.859 +  }
   1.860 +  change(offset, 2000);
   1.861 +  for (var i = 0; i < n - 1; i++) {
   1.862 +    assertEq(dests.at(i).value, i * 5 + 2000);
   1.863 +  }
   1.864 +}
   1.865 +
   1.866 +function change(v, newValue) {
   1.867 +  var edit = new EditConstraint(v, Strength.PREFERRED);
   1.868 +  var edits = new OrderedCollection();
   1.869 +  edits.add(edit);
   1.870 +  var plan = planner.extractPlanFromConstraints(edits);
   1.871 +  for (var i = 0; i < 10; i++) {
   1.872 +    v.value = newValue;
   1.873 +    plan.execute();
   1.874 +  }
   1.875 +  edit.destroyConstraint();
   1.876 +}
   1.877 +
   1.878 +// Global variable holding the current planner.
   1.879 +var planner = null;
   1.880 +
   1.881 +function deltaBlue() {
   1.882 +  chainTest(100);
   1.883 +  projectionTest(100);
   1.884 +}
   1.885 +
   1.886 +deltaBlue();

mercurial