OLD | NEW |
1 var DeltaBlue; | 1 var DeltaBlue; |
2 (function (DeltaBlue) { | 2 (function(DeltaBlue) { |
3 'use strict'; | 3 'use strict'; |
4 // Function main: () → dynamic | 4 // Function main: () → dynamic |
5 function main() { | 5 function main() { |
6 new DeltaBlue().report(); | 6 new DeltaBlue().report(); |
7 } | 7 } |
8 | |
9 class DeltaBlue extends BenchmarkBase.BenchmarkBase { | 8 class DeltaBlue extends BenchmarkBase.BenchmarkBase { |
10 DeltaBlue() { | 9 DeltaBlue() { |
11 super.BenchmarkBase("DeltaBlue"); | 10 super.BenchmarkBase("DeltaBlue"); |
12 } | 11 } |
13 run() { | 12 run() { |
14 chainTest(100); | 13 chainTest(100); |
15 projectionTest(100); | 14 projectionTest(100); |
16 } | 15 } |
17 } | 16 } |
18 | |
19 class Strength extends dart.Object { | 17 class Strength extends dart.Object { |
20 Strength(value, name) { | 18 Strength(value, name) { |
21 this.value = value; | 19 this.value = value; |
22 this.name = name; | 20 this.name = name; |
23 } | 21 } |
24 nextWeaker() { return /* Unimplemented const */new List.from([STRONG_PREFERR
ED, PREFERRED, STRONG_DEFAULT, NORMAL, WEAK_DEFAULT, WEAKEST]).get(this.value);
} | 22 nextWeaker() { |
| 23 return /* Unimplemented const */new List.from([STRONG_PREFERRED, PREFERRED
, STRONG_DEFAULT, NORMAL, WEAK_DEFAULT, WEAKEST]).get(this.value); |
| 24 } |
25 static stronger(s1, s2) { | 25 static stronger(s1, s2) { |
26 return s1.value < s2.value; | 26 return s1.value < s2.value; |
27 } | 27 } |
28 static weaker(s1, s2) { | 28 static weaker(s1, s2) { |
29 return s1.value > s2.value; | 29 return s1.value > s2.value; |
30 } | 30 } |
31 static weakest(s1, s2) { | 31 static weakest(s1, s2) { |
32 return weaker(s1, s2) ? s1 : s2; | 32 return weaker(s1, s2) ? s1 : s2; |
33 } | 33 } |
34 static strongest(s1, s2) { | 34 static strongest(s1, s2) { |
35 return stronger(s1, s2) ? s1 : s2; | 35 return stronger(s1, s2) ? s1 : s2; |
36 } | 36 } |
37 } | 37 } |
38 | |
39 let REQUIRED = new Strength(0, "required"); | 38 let REQUIRED = new Strength(0, "required"); |
40 let STRONG_PREFERRED = new Strength(1, "strongPreferred"); | 39 let STRONG_PREFERRED = new Strength(1, "strongPreferred"); |
41 let PREFERRED = new Strength(2, "preferred"); | 40 let PREFERRED = new Strength(2, "preferred"); |
42 let STRONG_DEFAULT = new Strength(3, "strongDefault"); | 41 let STRONG_DEFAULT = new Strength(3, "strongDefault"); |
43 let NORMAL = new Strength(4, "normal"); | 42 let NORMAL = new Strength(4, "normal"); |
44 let WEAK_DEFAULT = new Strength(5, "weakDefault"); | 43 let WEAK_DEFAULT = new Strength(5, "weakDefault"); |
45 let WEAKEST = new Strength(6, "weakest"); | 44 let WEAKEST = new Strength(6, "weakest"); |
46 class Constraint extends dart.Object { | 45 class Constraint extends dart.Object { |
47 Constraint(strength) { | 46 Constraint(strength) { |
48 this.strength = strength; | 47 this.strength = strength; |
49 } | 48 } |
50 addConstraint() { | 49 addConstraint() { |
51 this.addToGraph(); | 50 this.addToGraph(); |
52 DeltaBlue.planner.incrementalAdd(this); | 51 DeltaBlue.planner.incrementalAdd(this); |
53 } | 52 } |
54 satisfy(mark) { | 53 satisfy(mark) { |
55 this.chooseMethod(dart.as(mark, core.int)); | 54 this.chooseMethod(dart.as(mark, core.int)); |
56 if (!dart.notNull(this.isSatisfied())) { | 55 if (!dart.notNull(this.isSatisfied())) { |
57 if (dart.equals(this.strength, REQUIRED)) { | 56 if (dart.equals(this.strength, REQUIRED)) { |
58 core.print("Could not satisfy a required constraint!"); | 57 core.print("Could not satisfy a required constraint!"); |
59 } | 58 } |
60 return null; | 59 return null; |
61 } | 60 } |
62 this.markInputs(dart.as(mark, core.int)); | 61 this.markInputs(dart.as(mark, core.int)); |
63 let out = this.output(); | 62 let out = this.output(); |
64 let overridden = out.determinedBy; | 63 let overridden = out.determinedBy; |
65 if (overridden !== null) overridden.markUnsatisfied(); | 64 if (overridden !== null) |
| 65 overridden.markUnsatisfied(); |
66 out.determinedBy = this; | 66 out.determinedBy = this; |
67 if (!dart.notNull(DeltaBlue.planner.addPropagate(this, dart.as(mark, core.
int)))) core.print("Cycle encountered"); | 67 if (!dart.notNull(DeltaBlue.planner.addPropagate(this, dart.as(mark, core.
int)))) |
| 68 core.print("Cycle encountered"); |
68 out.mark = dart.as(mark, core.int); | 69 out.mark = dart.as(mark, core.int); |
69 return overridden; | 70 return overridden; |
70 } | 71 } |
71 destroyConstraint() { | 72 destroyConstraint() { |
72 if (this.isSatisfied()) DeltaBlue.planner.incrementalRemove(this); | 73 if (this.isSatisfied()) |
| 74 DeltaBlue.planner.incrementalRemove(this); |
73 this.removeFromGraph(); | 75 this.removeFromGraph(); |
74 } | 76 } |
75 isInput() { return false; } | 77 isInput() { |
| 78 return false; |
| 79 } |
76 } | 80 } |
77 | |
78 class UnaryConstraint extends Constraint { | 81 class UnaryConstraint extends Constraint { |
79 UnaryConstraint(myOutput, strength) { | 82 UnaryConstraint(myOutput, strength) { |
80 this.myOutput = myOutput; | 83 this.myOutput = myOutput; |
81 this.satisfied = false; | 84 this.satisfied = false; |
82 super.Constraint(strength); | 85 super.Constraint(strength); |
83 this.addConstraint(); | 86 this.addConstraint(); |
84 } | 87 } |
85 addToGraph() { | 88 addToGraph() { |
86 this.myOutput.addConstraint(this); | 89 this.myOutput.addConstraint(this); |
87 this.satisfied = false; | 90 this.satisfied = false; |
88 } | 91 } |
89 chooseMethod(mark) { | 92 chooseMethod(mark) { |
90 this.satisfied = dart.notNull((this.myOutput.mark !== mark)) && dart.notNu
ll(Strength.stronger(this.strength, this.myOutput.walkStrength)); | 93 this.satisfied = dart.notNull(this.myOutput.mark !== mark) && dart.notNull
(Strength.stronger(this.strength, this.myOutput.walkStrength)); |
91 } | 94 } |
92 isSatisfied() { return this.satisfied; } | 95 isSatisfied() { |
93 markInputs(mark) { | 96 return this.satisfied; |
94 } | 97 } |
95 output() { return this.myOutput; } | 98 markInputs(mark) {} |
| 99 output() { |
| 100 return this.myOutput; |
| 101 } |
96 recalculate() { | 102 recalculate() { |
97 this.myOutput.walkStrength = this.strength; | 103 this.myOutput.walkStrength = this.strength; |
98 this.myOutput.stay = !dart.notNull(this.isInput()); | 104 this.myOutput.stay = !dart.notNull(this.isInput()); |
99 if (this.myOutput.stay) this.execute(); | 105 if (this.myOutput.stay) |
| 106 this.execute(); |
100 } | 107 } |
101 markUnsatisfied() { | 108 markUnsatisfied() { |
102 this.satisfied = false; | 109 this.satisfied = false; |
103 } | 110 } |
104 inputsKnown(mark) { return true; } | 111 inputsKnown(mark) { |
| 112 return true; |
| 113 } |
105 removeFromGraph() { | 114 removeFromGraph() { |
106 if (this.myOutput !== null) this.myOutput.removeConstraint(this); | 115 if (this.myOutput !== null) |
| 116 this.myOutput.removeConstraint(this); |
107 this.satisfied = false; | 117 this.satisfied = false; |
108 } | 118 } |
109 } | 119 } |
110 | |
111 class StayConstraint extends UnaryConstraint { | 120 class StayConstraint extends UnaryConstraint { |
112 StayConstraint(v, str) { | 121 StayConstraint(v, str) { |
113 super.UnaryConstraint(v, str); | 122 super.UnaryConstraint(v, str); |
114 } | 123 } |
115 execute() { | 124 execute() {} |
116 } | |
117 } | 125 } |
118 | |
119 class EditConstraint extends UnaryConstraint { | 126 class EditConstraint extends UnaryConstraint { |
120 EditConstraint(v, str) { | 127 EditConstraint(v, str) { |
121 super.UnaryConstraint(v, str); | 128 super.UnaryConstraint(v, str); |
122 } | 129 } |
123 isInput() { return true; } | 130 isInput() { |
124 execute() { | 131 return true; |
125 } | 132 } |
| 133 execute() {} |
126 } | 134 } |
127 | |
128 let NONE = 1; | 135 let NONE = 1; |
129 let FORWARD = 2; | 136 let FORWARD = 2; |
130 let BACKWARD = 0; | 137 let BACKWARD = 0; |
131 class BinaryConstraint extends Constraint { | 138 class BinaryConstraint extends Constraint { |
132 BinaryConstraint(v1, v2, strength) { | 139 BinaryConstraint(v1, v2, strength) { |
133 this.v1 = v1; | 140 this.v1 = v1; |
134 this.v2 = v2; | 141 this.v2 = v2; |
135 this.direction = NONE; | 142 this.direction = NONE; |
136 super.Constraint(strength); | 143 super.Constraint(strength); |
137 this.addConstraint(); | 144 this.addConstraint(); |
138 } | 145 } |
139 chooseMethod(mark) { | 146 chooseMethod(mark) { |
140 if (this.v1.mark === mark) { | 147 if (this.v1.mark === mark) { |
141 this.direction = (dart.notNull(this.v2.mark !== mark) && dart.notNull(St
rength.stronger(this.strength, this.v2.walkStrength))) ? FORWARD : NONE; | 148 this.direction = dart.notNull(this.v2.mark !== mark) && dart.notNull(Str
ength.stronger(this.strength, this.v2.walkStrength)) ? FORWARD : NONE; |
142 } | 149 } |
143 if (this.v2.mark === mark) { | 150 if (this.v2.mark === mark) { |
144 this.direction = (dart.notNull(this.v1.mark !== mark) && dart.notNull(St
rength.stronger(this.strength, this.v1.walkStrength))) ? BACKWARD : NONE; | 151 this.direction = dart.notNull(this.v1.mark !== mark) && dart.notNull(Str
ength.stronger(this.strength, this.v1.walkStrength)) ? BACKWARD : NONE; |
145 } | 152 } |
146 if (Strength.weaker(this.v1.walkStrength, this.v2.walkStrength)) { | 153 if (Strength.weaker(this.v1.walkStrength, this.v2.walkStrength)) { |
147 this.direction = Strength.stronger(this.strength, this.v1.walkStrength)
? BACKWARD : NONE; | 154 this.direction = Strength.stronger(this.strength, this.v1.walkStrength)
? BACKWARD : NONE; |
148 } else { | 155 } else { |
149 this.direction = Strength.stronger(this.strength, this.v2.walkStrength)
? FORWARD : BACKWARD; | 156 this.direction = Strength.stronger(this.strength, this.v2.walkStrength)
? FORWARD : BACKWARD; |
150 } | 157 } |
151 } | 158 } |
152 addToGraph() { | 159 addToGraph() { |
153 this.v1.addConstraint(this); | 160 this.v1.addConstraint(this); |
154 this.v2.addConstraint(this); | 161 this.v2.addConstraint(this); |
155 this.direction = NONE; | 162 this.direction = NONE; |
156 } | 163 } |
157 isSatisfied() { return this.direction !== NONE; } | 164 isSatisfied() { |
| 165 return this.direction !== NONE; |
| 166 } |
158 markInputs(mark) { | 167 markInputs(mark) { |
159 this.input().mark = mark; | 168 this.input().mark = mark; |
160 } | 169 } |
161 input() { return this.direction === FORWARD ? this.v1 : this.v2; } | 170 input() { |
162 output() { return this.direction === FORWARD ? this.v2 : this.v1; } | 171 return this.direction === FORWARD ? this.v1 : this.v2; |
| 172 } |
| 173 output() { |
| 174 return this.direction === FORWARD ? this.v2 : this.v1; |
| 175 } |
163 recalculate() { | 176 recalculate() { |
164 let ihn = this.input(), out = this.output(); | 177 let ihn = this.input(), out = this.output(); |
165 out.walkStrength = Strength.weakest(this.strength, ihn.walkStrength); | 178 out.walkStrength = Strength.weakest(this.strength, ihn.walkStrength); |
166 out.stay = ihn.stay; | 179 out.stay = ihn.stay; |
167 if (out.stay) this.execute(); | 180 if (out.stay) |
| 181 this.execute(); |
168 } | 182 } |
169 markUnsatisfied() { | 183 markUnsatisfied() { |
170 this.direction = NONE; | 184 this.direction = NONE; |
171 } | 185 } |
172 inputsKnown(mark) { | 186 inputsKnown(mark) { |
173 let i = this.input(); | 187 let i = this.input(); |
174 return dart.notNull(dart.notNull(i.mark === mark) || dart.notNull(i.stay))
|| dart.notNull(i.determinedBy === null); | 188 return dart.notNull(dart.notNull(i.mark === mark) || dart.notNull(i.stay))
|| dart.notNull(i.determinedBy === null); |
175 } | 189 } |
176 removeFromGraph() { | 190 removeFromGraph() { |
177 if (this.v1 !== null) this.v1.removeConstraint(this); | 191 if (this.v1 !== null) |
178 if (this.v2 !== null) this.v2.removeConstraint(this); | 192 this.v1.removeConstraint(this); |
| 193 if (this.v2 !== null) |
| 194 this.v2.removeConstraint(this); |
179 this.direction = NONE; | 195 this.direction = NONE; |
180 } | 196 } |
181 } | 197 } |
182 | |
183 class ScaleConstraint extends BinaryConstraint { | 198 class ScaleConstraint extends BinaryConstraint { |
184 ScaleConstraint(src, scale, offset, dest, strength) { | 199 ScaleConstraint(src, scale, offset, dest, strength) { |
185 this.scale = scale; | 200 this.scale = scale; |
186 this.offset = offset; | 201 this.offset = offset; |
187 super.BinaryConstraint(src, dest, strength); | 202 super.BinaryConstraint(src, dest, strength); |
188 } | 203 } |
189 addToGraph() { | 204 addToGraph() { |
190 super.addToGraph(); | 205 super.addToGraph(); |
191 this.scale.addConstraint(this); | 206 this.scale.addConstraint(this); |
192 this.offset.addConstraint(this); | 207 this.offset.addConstraint(this); |
193 } | 208 } |
194 removeFromGraph() { | 209 removeFromGraph() { |
195 super.removeFromGraph(); | 210 super.removeFromGraph(); |
196 if (this.scale !== null) this.scale.removeConstraint(this); | 211 if (this.scale !== null) |
197 if (this.offset !== null) this.offset.removeConstraint(this); | 212 this.scale.removeConstraint(this); |
| 213 if (this.offset !== null) |
| 214 this.offset.removeConstraint(this); |
198 } | 215 } |
199 markInputs(mark) { | 216 markInputs(mark) { |
200 super.markInputs(mark); | 217 super.markInputs(mark); |
201 this.scale.mark = this.offset.mark = mark; | 218 this.scale.mark = this.offset.mark = mark; |
202 } | 219 } |
203 execute() { | 220 execute() { |
204 if (this.direction === FORWARD) { | 221 if (this.direction === FORWARD) { |
205 this.v2.value = this.v1.value * this.scale.value + this.offset.value; | 222 this.v2.value = this.v1.value * this.scale.value + this.offset.value; |
206 } else { | 223 } else { |
207 this.v1.value = ((this.v2.value - this.offset.value) / this.scale.value)
.truncate(); | 224 this.v1.value = ((this.v2.value - this.offset.value) / this.scale.value)
.truncate(); |
208 } | 225 } |
209 } | 226 } |
210 recalculate() { | 227 recalculate() { |
211 let ihn = this.input(), out = this.output(); | 228 let ihn = this.input(), out = this.output(); |
212 out.walkStrength = Strength.weakest(this.strength, ihn.walkStrength); | 229 out.walkStrength = Strength.weakest(this.strength, ihn.walkStrength); |
213 out.stay = dart.notNull(dart.notNull(ihn.stay) && dart.notNull(this.scale.
stay)) && dart.notNull(this.offset.stay); | 230 out.stay = dart.notNull(dart.notNull(ihn.stay) && dart.notNull(this.scale.
stay)) && dart.notNull(this.offset.stay); |
214 if (out.stay) this.execute(); | 231 if (out.stay) |
| 232 this.execute(); |
215 } | 233 } |
216 } | 234 } |
217 | |
218 class EqualityConstraint extends BinaryConstraint { | 235 class EqualityConstraint extends BinaryConstraint { |
219 EqualityConstraint(v1, v2, strength) { | 236 EqualityConstraint(v1, v2, strength) { |
220 super.BinaryConstraint(v1, v2, strength); | 237 super.BinaryConstraint(v1, v2, strength); |
221 } | 238 } |
222 execute() { | 239 execute() { |
223 this.output().value = this.input().value; | 240 this.output().value = this.input().value; |
224 } | 241 } |
225 } | 242 } |
226 | |
227 class Variable extends dart.Object { | 243 class Variable extends dart.Object { |
228 Variable(name, value) { | 244 Variable(name, value) { |
229 this.constraints = new List.from([]); | 245 this.constraints = new List.from([]); |
230 this.name = name; | 246 this.name = name; |
231 this.value = value; | 247 this.value = value; |
232 this.determinedBy = null; | 248 this.determinedBy = null; |
233 this.mark = 0; | 249 this.mark = 0; |
234 this.walkStrength = WEAKEST; | 250 this.walkStrength = WEAKEST; |
235 this.stay = true; | 251 this.stay = true; |
236 } | 252 } |
237 addConstraint(c) { | 253 addConstraint(c) { |
238 this.constraints.add(c); | 254 this.constraints.add(c); |
239 } | 255 } |
240 removeConstraint(c) { | 256 removeConstraint(c) { |
241 this.constraints.remove(c); | 257 this.constraints.remove(c); |
242 if (dart.equals(this.determinedBy, c)) this.determinedBy = null; | 258 if (dart.equals(this.determinedBy, c)) |
| 259 this.determinedBy = null; |
243 } | 260 } |
244 } | 261 } |
245 | |
246 class Planner extends dart.Object { | 262 class Planner extends dart.Object { |
247 Planner() { | 263 Planner() { |
248 this.currentMark = 0; | 264 this.currentMark = 0; |
249 } | 265 } |
250 incrementalAdd(c) { | 266 incrementalAdd(c) { |
251 let mark = this.newMark(); | 267 let mark = this.newMark(); |
252 for (let overridden = c.satisfy(mark); overridden !== null; overridden = o
verridden.satisfy(mark)) ; | 268 for (let overridden = c.satisfy(mark); overridden !== null; overridden = o
verridden.satisfy(mark)) |
| 269 ; |
253 } | 270 } |
254 incrementalRemove(c) { | 271 incrementalRemove(c) { |
255 let out = c.output(); | 272 let out = c.output(); |
256 c.markUnsatisfied(); | 273 c.markUnsatisfied(); |
257 c.removeFromGraph(); | 274 c.removeFromGraph(); |
258 let unsatisfied = this.removePropagateFrom(out); | 275 let unsatisfied = this.removePropagateFrom(out); |
259 let strength = REQUIRED; | 276 let strength = REQUIRED; |
260 do { | 277 do { |
261 for (let i = 0; i < unsatisfied.length; i++) { | 278 for (let i = 0; i < unsatisfied.length; i++) { |
262 let u = unsatisfied.get(i); | 279 let u = unsatisfied.get(i); |
263 if (dart.equals(u.strength, strength)) this.incrementalAdd(u); | 280 if (dart.equals(u.strength, strength)) |
| 281 this.incrementalAdd(u); |
264 } | 282 } |
265 strength = strength.nextWeaker(); | 283 strength = strength.nextWeaker(); |
266 } | 284 } while (!dart.equals(strength, WEAKEST)); |
267 while (!dart.equals(strength, WEAKEST)); | |
268 } | 285 } |
269 newMark() { return ++this.currentMark; } | 286 newMark() { |
| 287 return ++this.currentMark; |
| 288 } |
270 makePlan(sources) { | 289 makePlan(sources) { |
271 let mark = this.newMark(); | 290 let mark = this.newMark(); |
272 let plan = new Plan(); | 291 let plan = new Plan(); |
273 let todo = sources; | 292 let todo = sources; |
274 while (todo.length > 0) { | 293 while (todo.length > 0) { |
275 let c = todo.removeLast(); | 294 let c = todo.removeLast(); |
276 if (dart.notNull(c.output().mark !== mark) && dart.notNull(c.inputsKnown
(mark))) { | 295 if (dart.notNull(c.output().mark !== mark) && dart.notNull(c.inputsKnown
(mark))) { |
277 plan.addConstraint(c); | 296 plan.addConstraint(c); |
278 c.output().mark = mark; | 297 c.output().mark = mark; |
279 this.addConstraintsConsumingTo(c.output(), todo); | 298 this.addConstraintsConsumingTo(c.output(), todo); |
280 } | 299 } |
281 } | 300 } |
282 return plan; | 301 return plan; |
283 } | 302 } |
284 extractPlanFromConstraints(constraints) { | 303 extractPlanFromConstraints(constraints) { |
285 let sources = new List.from([]); | 304 let sources = new List.from([]); |
286 for (let i = 0; i < constraints.length; i++) { | 305 for (let i = 0; i < constraints.length; i++) { |
287 let c = constraints.get(i); | 306 let c = constraints.get(i); |
288 if (dart.notNull(c.isInput()) && dart.notNull(c.isSatisfied())) sources.
add(c); | 307 if (dart.notNull(c.isInput()) && dart.notNull(c.isSatisfied())) |
| 308 sources.add(c); |
289 } | 309 } |
290 return this.makePlan(sources); | 310 return this.makePlan(sources); |
291 } | 311 } |
292 addPropagate(c, mark) { | 312 addPropagate(c, mark) { |
293 let todo = new List.from([c]); | 313 let todo = new List.from([c]); |
294 while (todo.length > 0) { | 314 while (todo.length > 0) { |
295 let d = todo.removeLast(); | 315 let d = todo.removeLast(); |
296 if (d.output().mark === mark) { | 316 if (d.output().mark === mark) { |
297 this.incrementalRemove(c); | 317 this.incrementalRemove(c); |
298 return false; | 318 return false; |
299 } | 319 } |
300 d.recalculate(); | 320 d.recalculate(); |
301 this.addConstraintsConsumingTo(d.output(), todo); | 321 this.addConstraintsConsumingTo(d.output(), todo); |
302 } | 322 } |
303 return true; | 323 return true; |
304 } | 324 } |
305 removePropagateFrom(out) { | 325 removePropagateFrom(out) { |
306 out.determinedBy = null; | 326 out.determinedBy = null; |
307 out.walkStrength = WEAKEST; | 327 out.walkStrength = WEAKEST; |
308 out.stay = true; | 328 out.stay = true; |
309 let unsatisfied = new List.from([]); | 329 let unsatisfied = new List.from([]); |
310 let todo = new List.from([out]); | 330 let todo = new List.from([out]); |
311 while (todo.length > 0) { | 331 while (todo.length > 0) { |
312 let v = todo.removeLast(); | 332 let v = todo.removeLast(); |
313 for (let i = 0; i < v.constraints.length; i++) { | 333 for (let i = 0; i < v.constraints.length; i++) { |
314 let c = v.constraints.get(i); | 334 let c = v.constraints.get(i); |
315 if (!dart.notNull(c.isSatisfied())) unsatisfied.add(c); | 335 if (!dart.notNull(c.isSatisfied())) |
| 336 unsatisfied.add(c); |
316 } | 337 } |
317 let determining = v.determinedBy; | 338 let determining = v.determinedBy; |
318 for (let i = 0; i < v.constraints.length; i++) { | 339 for (let i = 0; i < v.constraints.length; i++) { |
319 let next = v.constraints.get(i); | 340 let next = v.constraints.get(i); |
320 if (dart.notNull(!dart.equals(next, determining)) && dart.notNull(next
.isSatisfied())) { | 341 if (dart.notNull(!dart.equals(next, determining)) && dart.notNull(next
.isSatisfied())) { |
321 next.recalculate(); | 342 next.recalculate(); |
322 todo.add(next.output()); | 343 todo.add(next.output()); |
323 } | 344 } |
324 } | 345 } |
325 } | 346 } |
326 return unsatisfied; | 347 return unsatisfied; |
327 } | 348 } |
328 addConstraintsConsumingTo(v, coll) { | 349 addConstraintsConsumingTo(v, coll) { |
329 let determining = v.determinedBy; | 350 let determining = v.determinedBy; |
330 for (let i = 0; i < v.constraints.length; i++) { | 351 for (let i = 0; i < v.constraints.length; i++) { |
331 let c = v.constraints.get(i); | 352 let c = v.constraints.get(i); |
332 if (dart.notNull(!dart.equals(c, determining)) && dart.notNull(c.isSatis
fied())) coll.add(c); | 353 if (dart.notNull(!dart.equals(c, determining)) && dart.notNull(c.isSatis
fied())) |
| 354 coll.add(c); |
333 } | 355 } |
334 } | 356 } |
335 } | 357 } |
336 | |
337 class Plan extends dart.Object { | 358 class Plan extends dart.Object { |
338 Plan() { | 359 Plan() { |
339 this.list = new List.from([]); | 360 this.list = new List.from([]); |
340 } | 361 } |
341 addConstraint(c) { | 362 addConstraint(c) { |
342 this.list.add(c); | 363 this.list.add(c); |
343 } | 364 } |
344 size() { return this.list.length; } | 365 size() { |
| 366 return this.list.length; |
| 367 } |
345 execute() { | 368 execute() { |
346 for (let i = 0; i < this.list.length; i++) { | 369 for (let i = 0; i < this.list.length; i++) { |
347 this.list.get(i).execute(); | 370 this.list.get(i).execute(); |
348 } | 371 } |
349 } | 372 } |
350 } | 373 } |
351 | |
352 // Function chainTest: (int) → void | 374 // Function chainTest: (int) → void |
353 function chainTest(n) { | 375 function chainTest(n) { |
354 DeltaBlue.planner = new Planner(); | 376 DeltaBlue.planner = new Planner(); |
355 let prev = null, first = null, last = null; | 377 let prev = null, first = null, last = null; |
356 for (let i = 0; i <= n; i++) { | 378 for (let i = 0; i <= n; i++) { |
357 let v = new Variable("v", 0); | 379 let v = new Variable("v", 0); |
358 if (prev !== null) new EqualityConstraint(prev, v, REQUIRED); | 380 if (prev !== null) |
359 if (i === 0) first = v; | 381 new EqualityConstraint(prev, v, REQUIRED); |
360 if (i === n) last = v; | 382 if (i === 0) |
| 383 first = v; |
| 384 if (i === n) |
| 385 last = v; |
361 prev = v; | 386 prev = v; |
362 } | 387 } |
363 new StayConstraint(last, STRONG_DEFAULT); | 388 new StayConstraint(last, STRONG_DEFAULT); |
364 let edit = new EditConstraint(first, PREFERRED); | 389 let edit = new EditConstraint(first, PREFERRED); |
365 let plan = DeltaBlue.planner.extractPlanFromConstraints(new List.from([edit]
)); | 390 let plan = DeltaBlue.planner.extractPlanFromConstraints(new List.from([edit]
)); |
366 for (let i = 0; i < 100; i++) { | 391 for (let i = 0; i < 100; i++) { |
367 first.value = i; | 392 first.value = i; |
368 plan.execute(); | 393 plan.execute(); |
369 if (last.value !== i) { | 394 if (last.value !== i) { |
370 core.print("Chain test failed:"); | 395 core.print("Chain test failed:"); |
371 core.print(`Expected last value to be ${i} but it was ${last.value}.`); | 396 core.print(`Expected last value to be ${i} but it was ${last.value}.`); |
372 } | 397 } |
373 } | 398 } |
374 } | 399 } |
375 | |
376 // Function projectionTest: (int) → void | 400 // Function projectionTest: (int) → void |
377 function projectionTest(n) { | 401 function projectionTest(n) { |
378 DeltaBlue.planner = new Planner(); | 402 DeltaBlue.planner = new Planner(); |
379 let scale = new Variable("scale", 10); | 403 let scale = new Variable("scale", 10); |
380 let offset = new Variable("offset", 1000); | 404 let offset = new Variable("offset", 1000); |
381 let src = null, dst = null; | 405 let src = null, dst = null; |
382 let dests = new List.from([]); | 406 let dests = new List.from([]); |
383 for (let i = 0; i < n; i++) { | 407 for (let i = 0; i < n; i++) { |
384 src = new Variable("src", i); | 408 src = new Variable("src", i); |
385 dst = new Variable("dst", i); | 409 dst = new Variable("dst", i); |
386 dests.add(dst); | 410 dests.add(dst); |
387 new StayConstraint(src, NORMAL); | 411 new StayConstraint(src, NORMAL); |
388 new ScaleConstraint(src, scale, offset, dst, REQUIRED); | 412 new ScaleConstraint(src, scale, offset, dst, REQUIRED); |
389 } | 413 } |
390 change(src, 17); | 414 change(src, 17); |
391 if (dst.value !== 1170) core.print("Projection 1 failed"); | 415 if (dst.value !== 1170) |
| 416 core.print("Projection 1 failed"); |
392 change(dst, 1050); | 417 change(dst, 1050); |
393 if (src.value !== 5) core.print("Projection 2 failed"); | 418 if (src.value !== 5) |
| 419 core.print("Projection 2 failed"); |
394 change(scale, 5); | 420 change(scale, 5); |
395 for (let i = 0; i < n - 1; i++) { | 421 for (let i = 0; i < n - 1; i++) { |
396 if (dests.get(i).value !== i * 5 + 1000) core.print("Projection 3 failed")
; | 422 if (dests.get(i).value !== i * 5 + 1000) |
| 423 core.print("Projection 3 failed"); |
397 } | 424 } |
398 change(offset, 2000); | 425 change(offset, 2000); |
399 for (let i = 0; i < n - 1; i++) { | 426 for (let i = 0; i < n - 1; i++) { |
400 if (dests.get(i).value !== i * 5 + 2000) core.print("Projection 4 failed")
; | 427 if (dests.get(i).value !== i * 5 + 2000) |
| 428 core.print("Projection 4 failed"); |
401 } | 429 } |
402 } | 430 } |
403 | |
404 // Function change: (Variable, int) → void | 431 // Function change: (Variable, int) → void |
405 function change(v, newValue) { | 432 function change(v, newValue) { |
406 let edit = new EditConstraint(v, PREFERRED); | 433 let edit = new EditConstraint(v, PREFERRED); |
407 let plan = DeltaBlue.planner.extractPlanFromConstraints(new List.from([edit]
)); | 434 let plan = DeltaBlue.planner.extractPlanFromConstraints(new List.from([edit]
)); |
408 for (let i = 0; i < 10; i++) { | 435 for (let i = 0; i < 10; i++) { |
409 v.value = newValue; | 436 v.value = newValue; |
410 plan.execute(); | 437 plan.execute(); |
411 } | 438 } |
412 edit.destroyConstraint(); | 439 edit.destroyConstraint(); |
413 } | 440 } |
414 | |
415 DeltaBlue.planner = null; | 441 DeltaBlue.planner = null; |
416 // Exports: | 442 // Exports: |
417 DeltaBlue.main = main; | 443 DeltaBlue.main = main; |
418 DeltaBlue.DeltaBlue = DeltaBlue; | 444 DeltaBlue.DeltaBlue = DeltaBlue; |
419 DeltaBlue.Strength = Strength; | 445 DeltaBlue.Strength = Strength; |
420 DeltaBlue.REQUIRED = REQUIRED; | 446 DeltaBlue.REQUIRED = REQUIRED; |
421 DeltaBlue.STRONG_PREFERRED = STRONG_PREFERRED; | 447 DeltaBlue.STRONG_PREFERRED = STRONG_PREFERRED; |
422 DeltaBlue.PREFERRED = PREFERRED; | 448 DeltaBlue.PREFERRED = PREFERRED; |
423 DeltaBlue.STRONG_DEFAULT = STRONG_DEFAULT; | 449 DeltaBlue.STRONG_DEFAULT = STRONG_DEFAULT; |
424 DeltaBlue.NORMAL = NORMAL; | 450 DeltaBlue.NORMAL = NORMAL; |
425 DeltaBlue.WEAK_DEFAULT = WEAK_DEFAULT; | 451 DeltaBlue.WEAK_DEFAULT = WEAK_DEFAULT; |
426 DeltaBlue.WEAKEST = WEAKEST; | 452 DeltaBlue.WEAKEST = WEAKEST; |
427 DeltaBlue.Constraint = Constraint; | 453 DeltaBlue.Constraint = Constraint; |
428 DeltaBlue.UnaryConstraint = UnaryConstraint; | 454 DeltaBlue.UnaryConstraint = UnaryConstraint; |
429 DeltaBlue.StayConstraint = StayConstraint; | 455 DeltaBlue.StayConstraint = StayConstraint; |
430 DeltaBlue.EditConstraint = EditConstraint; | 456 DeltaBlue.EditConstraint = EditConstraint; |
431 DeltaBlue.NONE = NONE; | 457 DeltaBlue.NONE = NONE; |
432 DeltaBlue.FORWARD = FORWARD; | 458 DeltaBlue.FORWARD = FORWARD; |
433 DeltaBlue.BACKWARD = BACKWARD; | 459 DeltaBlue.BACKWARD = BACKWARD; |
434 DeltaBlue.BinaryConstraint = BinaryConstraint; | 460 DeltaBlue.BinaryConstraint = BinaryConstraint; |
435 DeltaBlue.ScaleConstraint = ScaleConstraint; | 461 DeltaBlue.ScaleConstraint = ScaleConstraint; |
436 DeltaBlue.EqualityConstraint = EqualityConstraint; | 462 DeltaBlue.EqualityConstraint = EqualityConstraint; |
437 DeltaBlue.Variable = Variable; | 463 DeltaBlue.Variable = Variable; |
438 DeltaBlue.Planner = Planner; | 464 DeltaBlue.Planner = Planner; |
439 DeltaBlue.Plan = Plan; | 465 DeltaBlue.Plan = Plan; |
440 DeltaBlue.chainTest = chainTest; | 466 DeltaBlue.chainTest = chainTest; |
441 DeltaBlue.projectionTest = projectionTest; | 467 DeltaBlue.projectionTest = projectionTest; |
442 DeltaBlue.change = change; | 468 DeltaBlue.change = change; |
443 })(DeltaBlue || (DeltaBlue = {})); | 469 })(DeltaBlue || (DeltaBlue = {})); |
OLD | NEW |