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