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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright 2008 the V8 project authors. All rights reserved.
michael@0 2 // Copyright 1996 John Maloney and Mario Wolczko.
michael@0 3
michael@0 4 // This program is free software; you can redistribute it and/or modify
michael@0 5 // it under the terms of the GNU General Public License as published by
michael@0 6 // the Free Software Foundation; either version 2 of the License, or
michael@0 7 // (at your option) any later version.
michael@0 8 //
michael@0 9 // This program is distributed in the hope that it will be useful,
michael@0 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
michael@0 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
michael@0 12 // GNU General Public License for more details.
michael@0 13 //
michael@0 14 // You should have received a copy of the GNU General Public License
michael@0 15 // along with this program; if not, write to the Free Software
michael@0 16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
michael@0 17
michael@0 18
michael@0 19 // This implementation of the DeltaBlue benchmark is derived
michael@0 20 // from the Smalltalk implementation by John Maloney and Mario
michael@0 21 // Wolczko. Some parts have been translated directly, whereas
michael@0 22 // others have been modified more aggresively to make it feel
michael@0 23 // more like a JavaScript program.
michael@0 24
michael@0 25
michael@0 26 //var DeltaBlue = new BenchmarkSuite('DeltaBlue', 71104, [
michael@0 27 // new Benchmark('DeltaBlue', deltaBlue)
michael@0 28 //]);
michael@0 29
michael@0 30
michael@0 31 /**
michael@0 32 * A JavaScript implementation of the DeltaBlue constrain-solving
michael@0 33 * algorithm, as described in:
michael@0 34 *
michael@0 35 * "The DeltaBlue Algorithm: An Incremental Constraint Hierarchy Solver"
michael@0 36 * Bjorn N. Freeman-Benson and John Maloney
michael@0 37 * January 1990 Communications of the ACM,
michael@0 38 * also available as University of Washington TR 89-08-06.
michael@0 39 *
michael@0 40 * Beware: this benchmark is written in a grotesque style where
michael@0 41 * the constraint model is built by side-effects from constructors.
michael@0 42 * I've kept it this way to avoid deviating too much from the original
michael@0 43 * implementation.
michael@0 44 */
michael@0 45
michael@0 46 function alert(msg) {
michael@0 47 print(msg);
michael@0 48 assertEq(false, true);
michael@0 49 }
michael@0 50
michael@0 51 /* --- O b j e c t M o d e l --- */
michael@0 52
michael@0 53 Object.prototype.inheritsFrom = function (shuper) {
michael@0 54 function Inheriter() { }
michael@0 55 Inheriter.prototype = shuper.prototype;
michael@0 56 this.prototype = new Inheriter();
michael@0 57 this.superConstructor = shuper;
michael@0 58 }
michael@0 59
michael@0 60 function OrderedCollection() {
michael@0 61 this.elms = new Array();
michael@0 62 }
michael@0 63
michael@0 64 OrderedCollection.prototype.add = function (elm) {
michael@0 65 this.elms.push(elm);
michael@0 66 }
michael@0 67
michael@0 68 OrderedCollection.prototype.at = function (index) {
michael@0 69 return this.elms[index];
michael@0 70 }
michael@0 71
michael@0 72 OrderedCollection.prototype.size = function () {
michael@0 73 return this.elms.length;
michael@0 74 }
michael@0 75
michael@0 76 OrderedCollection.prototype.removeFirst = function () {
michael@0 77 return this.elms.pop();
michael@0 78 }
michael@0 79
michael@0 80 OrderedCollection.prototype.remove = function (elm) {
michael@0 81 var index = 0, skipped = 0;
michael@0 82 for (var i = 0; i < this.elms.length; i++) {
michael@0 83 var value = this.elms[i];
michael@0 84 if (value != elm) {
michael@0 85 this.elms[index] = value;
michael@0 86 index++;
michael@0 87 } else {
michael@0 88 skipped++;
michael@0 89 }
michael@0 90 }
michael@0 91 for (var i = 0; i < skipped; i++)
michael@0 92 this.elms.pop();
michael@0 93 }
michael@0 94
michael@0 95 /* --- *
michael@0 96 * S t r e n g t h
michael@0 97 * --- */
michael@0 98
michael@0 99 /**
michael@0 100 * Strengths are used to measure the relative importance of constraints.
michael@0 101 * New strengths may be inserted in the strength hierarchy without
michael@0 102 * disrupting current constraints. Strengths cannot be created outside
michael@0 103 * this class, so pointer comparison can be used for value comparison.
michael@0 104 */
michael@0 105 function Strength(strengthValue, name) {
michael@0 106 this.strengthValue = strengthValue;
michael@0 107 this.name = name;
michael@0 108 }
michael@0 109
michael@0 110 Strength.stronger = function (s1, s2) {
michael@0 111 return s1.strengthValue < s2.strengthValue;
michael@0 112 }
michael@0 113
michael@0 114 Strength.weaker = function (s1, s2) {
michael@0 115 return s1.strengthValue > s2.strengthValue;
michael@0 116 }
michael@0 117
michael@0 118 Strength.weakestOf = function (s1, s2) {
michael@0 119 return this.weaker(s1, s2) ? s1 : s2;
michael@0 120 }
michael@0 121
michael@0 122 Strength.strongest = function (s1, s2) {
michael@0 123 return this.stronger(s1, s2) ? s1 : s2;
michael@0 124 }
michael@0 125
michael@0 126 Strength.prototype.nextWeaker = function () {
michael@0 127 switch (this.strengthValue) {
michael@0 128 case 0: return Strength.WEAKEST;
michael@0 129 case 1: return Strength.WEAK_DEFAULT;
michael@0 130 case 2: return Strength.NORMAL;
michael@0 131 case 3: return Strength.STRONG_DEFAULT;
michael@0 132 case 4: return Strength.PREFERRED;
michael@0 133 case 5: return Strength.REQUIRED;
michael@0 134 }
michael@0 135 }
michael@0 136
michael@0 137 // Strength constants.
michael@0 138 Strength.REQUIRED = new Strength(0, "required");
michael@0 139 Strength.STONG_PREFERRED = new Strength(1, "strongPreferred");
michael@0 140 Strength.PREFERRED = new Strength(2, "preferred");
michael@0 141 Strength.STRONG_DEFAULT = new Strength(3, "strongDefault");
michael@0 142 Strength.NORMAL = new Strength(4, "normal");
michael@0 143 Strength.WEAK_DEFAULT = new Strength(5, "weakDefault");
michael@0 144 Strength.WEAKEST = new Strength(6, "weakest");
michael@0 145
michael@0 146 /* --- *
michael@0 147 * C o n s t r a i n t
michael@0 148 * --- */
michael@0 149
michael@0 150 /**
michael@0 151 * An abstract class representing a system-maintainable relationship
michael@0 152 * (or "constraint") between a set of variables. A constraint supplies
michael@0 153 * a strength instance variable; concrete subclasses provide a means
michael@0 154 * of storing the constrained variables and other information required
michael@0 155 * to represent a constraint.
michael@0 156 */
michael@0 157 function Constraint(strength) {
michael@0 158 this.strength = strength;
michael@0 159 }
michael@0 160
michael@0 161 /**
michael@0 162 * Activate this constraint and attempt to satisfy it.
michael@0 163 */
michael@0 164 Constraint.prototype.addConstraint = function () {
michael@0 165 this.addToGraph();
michael@0 166 planner.incrementalAdd(this);
michael@0 167 }
michael@0 168
michael@0 169 /**
michael@0 170 * Attempt to find a way to enforce this constraint. If successful,
michael@0 171 * record the solution, perhaps modifying the current dataflow
michael@0 172 * graph. Answer the constraint that this constraint overrides, if
michael@0 173 * there is one, or nil, if there isn't.
michael@0 174 * Assume: I am not already satisfied.
michael@0 175 */
michael@0 176 Constraint.prototype.satisfy = function (mark) {
michael@0 177 this.chooseMethod(mark);
michael@0 178 if (!this.isSatisfied()) {
michael@0 179 if (this.strength == Strength.REQUIRED)
michael@0 180 alert("Could not satisfy a required constraint!");
michael@0 181 return null;
michael@0 182 }
michael@0 183 this.markInputs(mark);
michael@0 184 var out = this.output();
michael@0 185 var overridden = out.determinedBy;
michael@0 186 if (overridden != null) overridden.markUnsatisfied();
michael@0 187 out.determinedBy = this;
michael@0 188 if (!planner.addPropagate(this, mark))
michael@0 189 alert("Cycle encountered");
michael@0 190 out.mark = mark;
michael@0 191 return overridden;
michael@0 192 }
michael@0 193
michael@0 194 Constraint.prototype.destroyConstraint = function () {
michael@0 195 if (this.isSatisfied()) planner.incrementalRemove(this);
michael@0 196 else this.removeFromGraph();
michael@0 197 }
michael@0 198
michael@0 199 /**
michael@0 200 * Normal constraints are not input constraints. An input constraint
michael@0 201 * is one that depends on external state, such as the mouse, the
michael@0 202 * keybord, a clock, or some arbitraty piece of imperative code.
michael@0 203 */
michael@0 204 Constraint.prototype.isInput = function () {
michael@0 205 return false;
michael@0 206 }
michael@0 207
michael@0 208 /* --- *
michael@0 209 * U n a r y C o n s t r a i n t
michael@0 210 * --- */
michael@0 211
michael@0 212 /**
michael@0 213 * Abstract superclass for constraints having a single possible output
michael@0 214 * variable.
michael@0 215 */
michael@0 216 function UnaryConstraint(v, strength) {
michael@0 217 UnaryConstraint.superConstructor.call(this, strength);
michael@0 218 this.myOutput = v;
michael@0 219 this.satisfied = false;
michael@0 220 this.addConstraint();
michael@0 221 }
michael@0 222
michael@0 223 UnaryConstraint.inheritsFrom(Constraint);
michael@0 224
michael@0 225 /**
michael@0 226 * Adds this constraint to the constraint graph
michael@0 227 */
michael@0 228 UnaryConstraint.prototype.addToGraph = function () {
michael@0 229 this.myOutput.addConstraint(this);
michael@0 230 this.satisfied = false;
michael@0 231 }
michael@0 232
michael@0 233 /**
michael@0 234 * Decides if this constraint can be satisfied and records that
michael@0 235 * decision.
michael@0 236 */
michael@0 237 UnaryConstraint.prototype.chooseMethod = function (mark) {
michael@0 238 this.satisfied = (this.myOutput.mark != mark)
michael@0 239 && Strength.stronger(this.strength, this.myOutput.walkStrength);
michael@0 240 }
michael@0 241
michael@0 242 /**
michael@0 243 * Returns true if this constraint is satisfied in the current solution.
michael@0 244 */
michael@0 245 UnaryConstraint.prototype.isSatisfied = function () {
michael@0 246 return this.satisfied;
michael@0 247 }
michael@0 248
michael@0 249 UnaryConstraint.prototype.markInputs = function (mark) {
michael@0 250 // has no inputs
michael@0 251 }
michael@0 252
michael@0 253 /**
michael@0 254 * Returns the current output variable.
michael@0 255 */
michael@0 256 UnaryConstraint.prototype.output = function () {
michael@0 257 return this.myOutput;
michael@0 258 }
michael@0 259
michael@0 260 /**
michael@0 261 * Calculate the walkabout strength, the stay flag, and, if it is
michael@0 262 * 'stay', the value for the current output of this constraint. Assume
michael@0 263 * this constraint is satisfied.
michael@0 264 */
michael@0 265 UnaryConstraint.prototype.recalculate = function () {
michael@0 266 this.myOutput.walkStrength = this.strength;
michael@0 267 this.myOutput.stay = !this.isInput();
michael@0 268 if (this.myOutput.stay) this.execute(); // Stay optimization
michael@0 269 }
michael@0 270
michael@0 271 /**
michael@0 272 * Records that this constraint is unsatisfied
michael@0 273 */
michael@0 274 UnaryConstraint.prototype.markUnsatisfied = function () {
michael@0 275 this.satisfied = false;
michael@0 276 }
michael@0 277
michael@0 278 UnaryConstraint.prototype.inputsKnown = function () {
michael@0 279 return true;
michael@0 280 }
michael@0 281
michael@0 282 UnaryConstraint.prototype.removeFromGraph = function () {
michael@0 283 if (this.myOutput != null) this.myOutput.removeConstraint(this);
michael@0 284 this.satisfied = false;
michael@0 285 }
michael@0 286
michael@0 287 /* --- *
michael@0 288 * S t a y C o n s t r a i n t
michael@0 289 * --- */
michael@0 290
michael@0 291 /**
michael@0 292 * Variables that should, with some level of preference, stay the same.
michael@0 293 * Planners may exploit the fact that instances, if satisfied, will not
michael@0 294 * change their output during plan execution. This is called "stay
michael@0 295 * optimization".
michael@0 296 */
michael@0 297 function StayConstraint(v, str) {
michael@0 298 StayConstraint.superConstructor.call(this, v, str);
michael@0 299 }
michael@0 300
michael@0 301 StayConstraint.inheritsFrom(UnaryConstraint);
michael@0 302
michael@0 303 StayConstraint.prototype.execute = function () {
michael@0 304 // Stay constraints do nothing
michael@0 305 }
michael@0 306
michael@0 307 /* --- *
michael@0 308 * E d i t C o n s t r a i n t
michael@0 309 * --- */
michael@0 310
michael@0 311 /**
michael@0 312 * A unary input constraint used to mark a variable that the client
michael@0 313 * wishes to change.
michael@0 314 */
michael@0 315 function EditConstraint(v, str) {
michael@0 316 EditConstraint.superConstructor.call(this, v, str);
michael@0 317 }
michael@0 318
michael@0 319 EditConstraint.inheritsFrom(UnaryConstraint);
michael@0 320
michael@0 321 /**
michael@0 322 * Edits indicate that a variable is to be changed by imperative code.
michael@0 323 */
michael@0 324 EditConstraint.prototype.isInput = function () {
michael@0 325 return true;
michael@0 326 }
michael@0 327
michael@0 328 EditConstraint.prototype.execute = function () {
michael@0 329 // Edit constraints do nothing
michael@0 330 }
michael@0 331
michael@0 332 /* --- *
michael@0 333 * B i n a r y C o n s t r a i n t
michael@0 334 * --- */
michael@0 335
michael@0 336 var Direction = new Object();
michael@0 337 Direction.NONE = 0;
michael@0 338 Direction.FORWARD = 1;
michael@0 339 Direction.BACKWARD = -1;
michael@0 340
michael@0 341 /**
michael@0 342 * Abstract superclass for constraints having two possible output
michael@0 343 * variables.
michael@0 344 */
michael@0 345 function BinaryConstraint(var1, var2, strength) {
michael@0 346 BinaryConstraint.superConstructor.call(this, strength);
michael@0 347 this.v1 = var1;
michael@0 348 this.v2 = var2;
michael@0 349 this.direction = Direction.NONE;
michael@0 350 this.addConstraint();
michael@0 351 }
michael@0 352
michael@0 353 BinaryConstraint.inheritsFrom(Constraint);
michael@0 354
michael@0 355 /**
michael@0 356 * Decides if this constratint can be satisfied and which way it
michael@0 357 * should flow based on the relative strength of the variables related,
michael@0 358 * and record that decision.
michael@0 359 */
michael@0 360 BinaryConstraint.prototype.chooseMethod = function (mark) {
michael@0 361 if (this.v1.mark == mark) {
michael@0 362 this.direction = (this.v1.mark != mark && Strength.stronger(this.strength, this.v2.walkStrength))
michael@0 363 ? Direction.FORWARD
michael@0 364 : Direction.NONE;
michael@0 365 }
michael@0 366 if (this.v2.mark == mark) {
michael@0 367 this.direction = (this.v1.mark != mark && Strength.stronger(this.strength, this.v1.walkStrength))
michael@0 368 ? Direction.BACKWARD
michael@0 369 : Direction.NONE;
michael@0 370 }
michael@0 371 if (Strength.weaker(this.v1.walkStrength, this.v2.walkStrength)) {
michael@0 372 this.direction = Strength.stronger(this.strength, this.v1.walkStrength)
michael@0 373 ? Direction.BACKWARD
michael@0 374 : Direction.NONE;
michael@0 375 } else {
michael@0 376 this.direction = Strength.stronger(this.strength, this.v2.walkStrength)
michael@0 377 ? Direction.FORWARD
michael@0 378 : Direction.BACKWARD
michael@0 379 }
michael@0 380 }
michael@0 381
michael@0 382 /**
michael@0 383 * Add this constraint to the constraint graph
michael@0 384 */
michael@0 385 BinaryConstraint.prototype.addToGraph = function () {
michael@0 386 this.v1.addConstraint(this);
michael@0 387 this.v2.addConstraint(this);
michael@0 388 this.direction = Direction.NONE;
michael@0 389 }
michael@0 390
michael@0 391 /**
michael@0 392 * Answer true if this constraint is satisfied in the current solution.
michael@0 393 */
michael@0 394 BinaryConstraint.prototype.isSatisfied = function () {
michael@0 395 return this.direction != Direction.NONE;
michael@0 396 }
michael@0 397
michael@0 398 /**
michael@0 399 * Mark the input variable with the given mark.
michael@0 400 */
michael@0 401 BinaryConstraint.prototype.markInputs = function (mark) {
michael@0 402 this.input().mark = mark;
michael@0 403 }
michael@0 404
michael@0 405 /**
michael@0 406 * Returns the current input variable
michael@0 407 */
michael@0 408 BinaryConstraint.prototype.input = function () {
michael@0 409 return (this.direction == Direction.FORWARD) ? this.v1 : this.v2;
michael@0 410 }
michael@0 411
michael@0 412 /**
michael@0 413 * Returns the current output variable
michael@0 414 */
michael@0 415 BinaryConstraint.prototype.output = function () {
michael@0 416 return (this.direction == Direction.FORWARD) ? this.v2 : this.v1;
michael@0 417 }
michael@0 418
michael@0 419 /**
michael@0 420 * Calculate the walkabout strength, the stay flag, and, if it is
michael@0 421 * 'stay', the value for the current output of this
michael@0 422 * constraint. Assume this constraint is satisfied.
michael@0 423 */
michael@0 424 BinaryConstraint.prototype.recalculate = function () {
michael@0 425 var ihn = this.input(), out = this.output();
michael@0 426 out.walkStrength = Strength.weakestOf(this.strength, ihn.walkStrength);
michael@0 427 out.stay = ihn.stay;
michael@0 428 if (out.stay) this.execute();
michael@0 429 }
michael@0 430
michael@0 431 /**
michael@0 432 * Record the fact that this constraint is unsatisfied.
michael@0 433 */
michael@0 434 BinaryConstraint.prototype.markUnsatisfied = function () {
michael@0 435 this.direction = Direction.NONE;
michael@0 436 }
michael@0 437
michael@0 438 BinaryConstraint.prototype.inputsKnown = function (mark) {
michael@0 439 var i = this.input();
michael@0 440 return i.mark == mark || i.stay || i.determinedBy == null;
michael@0 441 }
michael@0 442
michael@0 443 BinaryConstraint.prototype.removeFromGraph = function () {
michael@0 444 if (this.v1 != null) this.v1.removeConstraint(this);
michael@0 445 if (this.v2 != null) this.v2.removeConstraint(this);
michael@0 446 this.direction = Direction.NONE;
michael@0 447 }
michael@0 448
michael@0 449 /* --- *
michael@0 450 * S c a l e C o n s t r a i n t
michael@0 451 * --- */
michael@0 452
michael@0 453 /**
michael@0 454 * Relates two variables by the linear scaling relationship: "v2 =
michael@0 455 * (v1 * scale) + offset". Either v1 or v2 may be changed to maintain
michael@0 456 * this relationship but the scale factor and offset are considered
michael@0 457 * read-only.
michael@0 458 */
michael@0 459 function ScaleConstraint(src, scale, offset, dest, strength) {
michael@0 460 this.direction = Direction.NONE;
michael@0 461 this.scale = scale;
michael@0 462 this.offset = offset;
michael@0 463 ScaleConstraint.superConstructor.call(this, src, dest, strength);
michael@0 464 }
michael@0 465
michael@0 466 ScaleConstraint.inheritsFrom(BinaryConstraint);
michael@0 467
michael@0 468 /**
michael@0 469 * Adds this constraint to the constraint graph.
michael@0 470 */
michael@0 471 ScaleConstraint.prototype.addToGraph = function () {
michael@0 472 ScaleConstraint.superConstructor.prototype.addToGraph.call(this);
michael@0 473 this.scale.addConstraint(this);
michael@0 474 this.offset.addConstraint(this);
michael@0 475 }
michael@0 476
michael@0 477 ScaleConstraint.prototype.removeFromGraph = function () {
michael@0 478 ScaleConstraint.superConstructor.prototype.removeFromGraph.call(this);
michael@0 479 if (this.scale != null) this.scale.removeConstraint(this);
michael@0 480 if (this.offset != null) this.offset.removeConstraint(this);
michael@0 481 }
michael@0 482
michael@0 483 ScaleConstraint.prototype.markInputs = function (mark) {
michael@0 484 ScaleConstraint.superConstructor.prototype.markInputs.call(this, mark);
michael@0 485 this.scale.mark = this.offset.mark = mark;
michael@0 486 }
michael@0 487
michael@0 488 /**
michael@0 489 * Enforce this constraint. Assume that it is satisfied.
michael@0 490 */
michael@0 491 ScaleConstraint.prototype.execute = function () {
michael@0 492 if (this.direction == Direction.FORWARD) {
michael@0 493 this.v2.value = this.v1.value * this.scale.value + this.offset.value;
michael@0 494 } else {
michael@0 495 this.v1.value = (this.v2.value - this.offset.value) / this.scale.value;
michael@0 496 }
michael@0 497 }
michael@0 498
michael@0 499 /**
michael@0 500 * Calculate the walkabout strength, the stay flag, and, if it is
michael@0 501 * 'stay', the value for the current output of this constraint. Assume
michael@0 502 * this constraint is satisfied.
michael@0 503 */
michael@0 504 ScaleConstraint.prototype.recalculate = function () {
michael@0 505 var ihn = this.input(), out = this.output();
michael@0 506 out.walkStrength = Strength.weakestOf(this.strength, ihn.walkStrength);
michael@0 507 out.stay = ihn.stay && this.scale.stay && this.offset.stay;
michael@0 508 if (out.stay) this.execute();
michael@0 509 }
michael@0 510
michael@0 511 /* --- *
michael@0 512 * E q u a l i t y C o n s t r a i n t
michael@0 513 * --- */
michael@0 514
michael@0 515 /**
michael@0 516 * Constrains two variables to have the same value.
michael@0 517 */
michael@0 518 function EqualityConstraint(var1, var2, strength) {
michael@0 519 EqualityConstraint.superConstructor.call(this, var1, var2, strength);
michael@0 520 }
michael@0 521
michael@0 522 EqualityConstraint.inheritsFrom(BinaryConstraint);
michael@0 523
michael@0 524 /**
michael@0 525 * Enforce this constraint. Assume that it is satisfied.
michael@0 526 */
michael@0 527 EqualityConstraint.prototype.execute = function () {
michael@0 528 this.output().value = this.input().value;
michael@0 529 }
michael@0 530
michael@0 531 /* --- *
michael@0 532 * V a r i a b l e
michael@0 533 * --- */
michael@0 534
michael@0 535 /**
michael@0 536 * A constrained variable. In addition to its value, it maintain the
michael@0 537 * structure of the constraint graph, the current dataflow graph, and
michael@0 538 * various parameters of interest to the DeltaBlue incremental
michael@0 539 * constraint solver.
michael@0 540 **/
michael@0 541 function Variable(name, initialValue) {
michael@0 542 this.value = initialValue || 0;
michael@0 543 this.constraints = new OrderedCollection();
michael@0 544 this.determinedBy = null;
michael@0 545 this.mark = 0;
michael@0 546 this.walkStrength = Strength.WEAKEST;
michael@0 547 this.stay = true;
michael@0 548 this.name = name;
michael@0 549 }
michael@0 550
michael@0 551 /**
michael@0 552 * Add the given constraint to the set of all constraints that refer
michael@0 553 * this variable.
michael@0 554 */
michael@0 555 Variable.prototype.addConstraint = function (c) {
michael@0 556 this.constraints.add(c);
michael@0 557 }
michael@0 558
michael@0 559 /**
michael@0 560 * Removes all traces of c from this variable.
michael@0 561 */
michael@0 562 Variable.prototype.removeConstraint = function (c) {
michael@0 563 this.constraints.remove(c);
michael@0 564 if (this.determinedBy == c) this.determinedBy = null;
michael@0 565 }
michael@0 566
michael@0 567 /* --- *
michael@0 568 * P l a n n e r
michael@0 569 * --- */
michael@0 570
michael@0 571 /**
michael@0 572 * The DeltaBlue planner
michael@0 573 */
michael@0 574 function Planner() {
michael@0 575 this.currentMark = 0;
michael@0 576 }
michael@0 577
michael@0 578 /**
michael@0 579 * Attempt to satisfy the given constraint and, if successful,
michael@0 580 * incrementally update the dataflow graph. Details: If satifying
michael@0 581 * the constraint is successful, it may override a weaker constraint
michael@0 582 * on its output. The algorithm attempts to resatisfy that
michael@0 583 * constraint using some other method. This process is repeated
michael@0 584 * until either a) it reaches a variable that was not previously
michael@0 585 * determined by any constraint or b) it reaches a constraint that
michael@0 586 * is too weak to be satisfied using any of its methods. The
michael@0 587 * variables of constraints that have been processed are marked with
michael@0 588 * a unique mark value so that we know where we've been. This allows
michael@0 589 * the algorithm to avoid getting into an infinite loop even if the
michael@0 590 * constraint graph has an inadvertent cycle.
michael@0 591 */
michael@0 592 Planner.prototype.incrementalAdd = function (c) {
michael@0 593 var mark = this.newMark();
michael@0 594 var overridden = c.satisfy(mark);
michael@0 595 while (overridden != null)
michael@0 596 overridden = overridden.satisfy(mark);
michael@0 597 }
michael@0 598
michael@0 599 /**
michael@0 600 * Entry point for retracting a constraint. Remove the given
michael@0 601 * constraint and incrementally update the dataflow graph.
michael@0 602 * Details: Retracting the given constraint may allow some currently
michael@0 603 * unsatisfiable downstream constraint to be satisfied. We therefore collect
michael@0 604 * a list of unsatisfied downstream constraints and attempt to
michael@0 605 * satisfy each one in turn. This list is traversed by constraint
michael@0 606 * strength, strongest first, as a heuristic for avoiding
michael@0 607 * unnecessarily adding and then overriding weak constraints.
michael@0 608 * Assume: c is satisfied.
michael@0 609 */
michael@0 610 Planner.prototype.incrementalRemove = function (c) {
michael@0 611 var out = c.output();
michael@0 612 c.markUnsatisfied();
michael@0 613 c.removeFromGraph();
michael@0 614 var unsatisfied = this.removePropagateFrom(out);
michael@0 615 var strength = Strength.REQUIRED;
michael@0 616 do {
michael@0 617 for (var i = 0; i < unsatisfied.size(); i++) {
michael@0 618 var u = unsatisfied.at(i);
michael@0 619 if (u.strength == strength)
michael@0 620 this.incrementalAdd(u);
michael@0 621 }
michael@0 622 strength = strength.nextWeaker();
michael@0 623 } while (strength != Strength.WEAKEST);
michael@0 624 }
michael@0 625
michael@0 626 /**
michael@0 627 * Select a previously unused mark value.
michael@0 628 */
michael@0 629 Planner.prototype.newMark = function () {
michael@0 630 return ++this.currentMark;
michael@0 631 }
michael@0 632
michael@0 633 /**
michael@0 634 * Extract a plan for resatisfaction starting from the given source
michael@0 635 * constraints, usually a set of input constraints. This method
michael@0 636 * assumes that stay optimization is desired; the plan will contain
michael@0 637 * only constraints whose output variables are not stay. Constraints
michael@0 638 * that do no computation, such as stay and edit constraints, are
michael@0 639 * not included in the plan.
michael@0 640 * Details: The outputs of a constraint are marked when it is added
michael@0 641 * to the plan under construction. A constraint may be appended to
michael@0 642 * the plan when all its input variables are known. A variable is
michael@0 643 * known if either a) the variable is marked (indicating that has
michael@0 644 * been computed by a constraint appearing earlier in the plan), b)
michael@0 645 * the variable is 'stay' (i.e. it is a constant at plan execution
michael@0 646 * time), or c) the variable is not determined by any
michael@0 647 * constraint. The last provision is for past states of history
michael@0 648 * variables, which are not stay but which are also not computed by
michael@0 649 * any constraint.
michael@0 650 * Assume: sources are all satisfied.
michael@0 651 */
michael@0 652 Planner.prototype.makePlan = function (sources) {
michael@0 653 var mark = this.newMark();
michael@0 654 var plan = new Plan();
michael@0 655 var todo = sources;
michael@0 656 while (todo.size() > 0) {
michael@0 657 var c = todo.removeFirst();
michael@0 658 if (c.output().mark != mark && c.inputsKnown(mark)) {
michael@0 659 plan.addConstraint(c);
michael@0 660 c.output().mark = mark;
michael@0 661 this.addConstraintsConsumingTo(c.output(), todo);
michael@0 662 }
michael@0 663 }
michael@0 664 return plan;
michael@0 665 }
michael@0 666
michael@0 667 /**
michael@0 668 * Extract a plan for resatisfying starting from the output of the
michael@0 669 * given constraints, usually a set of input constraints.
michael@0 670 */
michael@0 671 Planner.prototype.extractPlanFromConstraints = function (constraints) {
michael@0 672 var sources = new OrderedCollection();
michael@0 673 for (var i = 0; i < constraints.size(); i++) {
michael@0 674 var c = constraints.at(i);
michael@0 675 if (c.isInput() && c.isSatisfied())
michael@0 676 // not in plan already and eligible for inclusion
michael@0 677 sources.add(c);
michael@0 678 }
michael@0 679 return this.makePlan(sources);
michael@0 680 }
michael@0 681
michael@0 682 /**
michael@0 683 * Recompute the walkabout strengths and stay flags of all variables
michael@0 684 * downstream of the given constraint and recompute the actual
michael@0 685 * values of all variables whose stay flag is true. If a cycle is
michael@0 686 * detected, remove the given constraint and answer
michael@0 687 * false. Otherwise, answer true.
michael@0 688 * Details: Cycles are detected when a marked variable is
michael@0 689 * encountered downstream of the given constraint. The sender is
michael@0 690 * assumed to have marked the inputs of the given constraint with
michael@0 691 * the given mark. Thus, encountering a marked node downstream of
michael@0 692 * the output constraint means that there is a path from the
michael@0 693 * constraint's output to one of its inputs.
michael@0 694 */
michael@0 695 Planner.prototype.addPropagate = function (c, mark) {
michael@0 696 var todo = new OrderedCollection();
michael@0 697 todo.add(c);
michael@0 698 while (todo.size() > 0) {
michael@0 699 var d = todo.removeFirst();
michael@0 700 if (d.output().mark == mark) {
michael@0 701 this.incrementalRemove(c);
michael@0 702 return false;
michael@0 703 }
michael@0 704 d.recalculate();
michael@0 705 this.addConstraintsConsumingTo(d.output(), todo);
michael@0 706 }
michael@0 707 return true;
michael@0 708 }
michael@0 709
michael@0 710
michael@0 711 /**
michael@0 712 * Update the walkabout strengths and stay flags of all variables
michael@0 713 * downstream of the given constraint. Answer a collection of
michael@0 714 * unsatisfied constraints sorted in order of decreasing strength.
michael@0 715 */
michael@0 716 Planner.prototype.removePropagateFrom = function (out) {
michael@0 717 out.determinedBy = null;
michael@0 718 out.walkStrength = Strength.WEAKEST;
michael@0 719 out.stay = true;
michael@0 720 var unsatisfied = new OrderedCollection();
michael@0 721 var todo = new OrderedCollection();
michael@0 722 todo.add(out);
michael@0 723 while (todo.size() > 0) {
michael@0 724 var v = todo.removeFirst();
michael@0 725 for (var i = 0; i < v.constraints.size(); i++) {
michael@0 726 var c = v.constraints.at(i);
michael@0 727 if (!c.isSatisfied())
michael@0 728 unsatisfied.add(c);
michael@0 729 }
michael@0 730 var determining = v.determinedBy;
michael@0 731 for (var i = 0; i < v.constraints.size(); i++) {
michael@0 732 var next = v.constraints.at(i);
michael@0 733 if (next != determining && next.isSatisfied()) {
michael@0 734 next.recalculate();
michael@0 735 todo.add(next.output());
michael@0 736 }
michael@0 737 }
michael@0 738 }
michael@0 739 return unsatisfied;
michael@0 740 }
michael@0 741
michael@0 742 Planner.prototype.addConstraintsConsumingTo = function (v, coll) {
michael@0 743 var determining = v.determinedBy;
michael@0 744 var cc = v.constraints;
michael@0 745 for (var i = 0; i < cc.size(); i++) {
michael@0 746 var c = cc.at(i);
michael@0 747 if (c != determining && c.isSatisfied())
michael@0 748 coll.add(c);
michael@0 749 }
michael@0 750 }
michael@0 751
michael@0 752 /* --- *
michael@0 753 * P l a n
michael@0 754 * --- */
michael@0 755
michael@0 756 /**
michael@0 757 * A Plan is an ordered list of constraints to be executed in sequence
michael@0 758 * to resatisfy all currently satisfiable constraints in the face of
michael@0 759 * one or more changing inputs.
michael@0 760 */
michael@0 761 function Plan() {
michael@0 762 this.v = new OrderedCollection();
michael@0 763 }
michael@0 764
michael@0 765 Plan.prototype.addConstraint = function (c) {
michael@0 766 this.v.add(c);
michael@0 767 }
michael@0 768
michael@0 769 Plan.prototype.size = function () {
michael@0 770 return this.v.size();
michael@0 771 }
michael@0 772
michael@0 773 Plan.prototype.constraintAt = function (index) {
michael@0 774 return this.v.at(index);
michael@0 775 }
michael@0 776
michael@0 777 Plan.prototype.execute = function () {
michael@0 778 for (var i = 0; i < this.size(); i++) {
michael@0 779 var c = this.constraintAt(i);
michael@0 780 c.execute();
michael@0 781 }
michael@0 782 }
michael@0 783
michael@0 784 /* --- *
michael@0 785 * M a i n
michael@0 786 * --- */
michael@0 787
michael@0 788 /**
michael@0 789 * This is the standard DeltaBlue benchmark. A long chain of equality
michael@0 790 * constraints is constructed with a stay constraint on one end. An
michael@0 791 * edit constraint is then added to the opposite end and the time is
michael@0 792 * measured for adding and removing this constraint, and extracting
michael@0 793 * and executing a constraint satisfaction plan. There are two cases.
michael@0 794 * In case 1, the added constraint is stronger than the stay
michael@0 795 * constraint and values must propagate down the entire length of the
michael@0 796 * chain. In case 2, the added constraint is weaker than the stay
michael@0 797 * constraint so it cannot be accomodated. The cost in this case is,
michael@0 798 * of course, very low. Typical situations lie somewhere between these
michael@0 799 * two extremes.
michael@0 800 */
michael@0 801 function chainTest(n) {
michael@0 802 planner = new Planner();
michael@0 803 var prev = null, first = null, last = null;
michael@0 804
michael@0 805 // Build chain of n equality constraints
michael@0 806 for (var i = 0; i <= n; i++) {
michael@0 807 var name = "v" + i;
michael@0 808 var v = new Variable(name);
michael@0 809 if (prev != null)
michael@0 810 new EqualityConstraint(prev, v, Strength.REQUIRED);
michael@0 811 if (i == 0) first = v;
michael@0 812 if (i == n) last = v;
michael@0 813 prev = v;
michael@0 814 }
michael@0 815
michael@0 816 new StayConstraint(last, Strength.STRONG_DEFAULT);
michael@0 817 var edit = new EditConstraint(first, Strength.PREFERRED);
michael@0 818 var edits = new OrderedCollection();
michael@0 819 edits.add(edit);
michael@0 820 var plan = planner.extractPlanFromConstraints(edits);
michael@0 821 for (var i = 0; i < 100; i++) {
michael@0 822 first.value = i;
michael@0 823 plan.execute();
michael@0 824 assertEq(last.value, i);
michael@0 825 }
michael@0 826 }
michael@0 827
michael@0 828 /**
michael@0 829 * This test constructs a two sets of variables related to each
michael@0 830 * other by a simple linear transformation (scale and offset). The
michael@0 831 * time is measured to change a variable on either side of the
michael@0 832 * mapping and to change the scale and offset factors.
michael@0 833 */
michael@0 834 function projectionTest(n) {
michael@0 835 planner = new Planner();
michael@0 836 var scale = new Variable("scale", 10);
michael@0 837 var offset = new Variable("offset", 1000);
michael@0 838 var src = null, dst = null;
michael@0 839
michael@0 840 var dests = new OrderedCollection();
michael@0 841 for (var i = 0; i < n; i++) {
michael@0 842 src = new Variable("src" + i, i);
michael@0 843 dst = new Variable("dst" + i, i);
michael@0 844 dests.add(dst);
michael@0 845 new StayConstraint(src, Strength.NORMAL);
michael@0 846 new ScaleConstraint(src, scale, offset, dst, Strength.REQUIRED);
michael@0 847 }
michael@0 848
michael@0 849 change(src, 17);
michael@0 850 assertEq(dst.value, 1170);
michael@0 851 change(dst, 1050);
michael@0 852 assertEq(src.value, 5);
michael@0 853 change(scale, 5);
michael@0 854 for (var i = 0; i < n - 1; i++) {
michael@0 855 assertEq(dests.at(i).value, i * 5 + 1000);
michael@0 856 }
michael@0 857 change(offset, 2000);
michael@0 858 for (var i = 0; i < n - 1; i++) {
michael@0 859 assertEq(dests.at(i).value, i * 5 + 2000);
michael@0 860 }
michael@0 861 }
michael@0 862
michael@0 863 function change(v, newValue) {
michael@0 864 var edit = new EditConstraint(v, Strength.PREFERRED);
michael@0 865 var edits = new OrderedCollection();
michael@0 866 edits.add(edit);
michael@0 867 var plan = planner.extractPlanFromConstraints(edits);
michael@0 868 for (var i = 0; i < 10; i++) {
michael@0 869 v.value = newValue;
michael@0 870 plan.execute();
michael@0 871 }
michael@0 872 edit.destroyConstraint();
michael@0 873 }
michael@0 874
michael@0 875 // Global variable holding the current planner.
michael@0 876 var planner = null;
michael@0 877
michael@0 878 function deltaBlue() {
michael@0 879 chainTest(100);
michael@0 880 projectionTest(100);
michael@0 881 }
michael@0 882
michael@0 883 deltaBlue();

mercurial