js/src/v8/deltablue.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial