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