Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: js/deltablue.js

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

Powered by Google App Engine
This is Rietveld 408576698