js/src/v8/deltablue.js

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

mercurial