js/src/devtools/jint/v8/deltablue.js

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 // Copyright 2008 the V8 project authors. All rights reserved.
michael@0 2 // Redistribution and use in source and binary forms, with or without
michael@0 3 // modification, are permitted provided that the following conditions are
michael@0 4 // met:
michael@0 5 //
michael@0 6 // * Redistributions of source code must retain the above copyright
michael@0 7 // notice, this list of conditions and the following disclaimer.
michael@0 8 // * Redistributions in binary form must reproduce the above
michael@0 9 // copyright notice, this list of conditions and the following
michael@0 10 // disclaimer in the documentation and/or other materials provided
michael@0 11 // with the distribution.
michael@0 12 // * Neither the name of Google Inc. nor the names of its
michael@0 13 // contributors may be used to endorse or promote products derived
michael@0 14 // from this software without specific prior written permission.
michael@0 15 //
michael@0 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 27
michael@0 28
michael@0 29 // Simple framework for running the benchmark suites and
michael@0 30 // computing a score based on the timing measurements.
michael@0 31
michael@0 32
michael@0 33 // A benchmark has a name (string) and a function that will be run to
michael@0 34 // do the performance measurement. The optional setup and tearDown
michael@0 35 // arguments are functions that will be invoked before and after
michael@0 36 // running the benchmark, but the running time of these functions will
michael@0 37 // not be accounted for in the benchmark score.
michael@0 38 function Benchmark(name, run, setup, tearDown) {
michael@0 39 this.name = name;
michael@0 40 this.run = run;
michael@0 41 this.Setup = setup ? setup : function() { };
michael@0 42 this.TearDown = tearDown ? tearDown : function() { };
michael@0 43 }
michael@0 44
michael@0 45
michael@0 46 // Benchmark results hold the benchmark and the measured time used to
michael@0 47 // run the benchmark. The benchmark score is computed later once a
michael@0 48 // full benchmark suite has run to completion.
michael@0 49 function BenchmarkResult(benchmark, time) {
michael@0 50 this.benchmark = benchmark;
michael@0 51 this.time = time;
michael@0 52 }
michael@0 53
michael@0 54
michael@0 55 // Automatically convert results to numbers. Used by the geometric
michael@0 56 // mean computation.
michael@0 57 BenchmarkResult.prototype.valueOf = function() {
michael@0 58 return this.time;
michael@0 59 }
michael@0 60
michael@0 61
michael@0 62 // Suites of benchmarks consist of a name and the set of benchmarks in
michael@0 63 // addition to the reference timing that the final score will be based
michael@0 64 // on. This way, all scores are relative to a reference run and higher
michael@0 65 // scores implies better performance.
michael@0 66 function BenchmarkSuite(name, reference, benchmarks) {
michael@0 67 this.name = name;
michael@0 68 this.reference = reference;
michael@0 69 this.benchmarks = benchmarks;
michael@0 70 BenchmarkSuite.suites.push(this);
michael@0 71 }
michael@0 72
michael@0 73
michael@0 74 // Keep track of all declared benchmark suites.
michael@0 75 BenchmarkSuite.suites = [];
michael@0 76
michael@0 77
michael@0 78 // Scores are not comparable across versions. Bump the version if
michael@0 79 // you're making changes that will affect that scores, e.g. if you add
michael@0 80 // a new benchmark or change an existing one.
michael@0 81 BenchmarkSuite.version = '5';
michael@0 82
michael@0 83
michael@0 84 // To make the benchmark results predictable, we replace Math.random
michael@0 85 // with a 100% deterministic alternative.
michael@0 86 Math.random = (function() {
michael@0 87 var seed = 49734321;
michael@0 88 return function() {
michael@0 89 // Robert Jenkins' 32 bit integer hash function.
michael@0 90 seed = ((seed + 0x7ed55d16) + (seed << 12)) & 0xffffffff;
michael@0 91 seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff;
michael@0 92 seed = ((seed + 0x165667b1) + (seed << 5)) & 0xffffffff;
michael@0 93 seed = ((seed + 0xd3a2646c) ^ (seed << 9)) & 0xffffffff;
michael@0 94 seed = ((seed + 0xfd7046c5) + (seed << 3)) & 0xffffffff;
michael@0 95 seed = ((seed ^ 0xb55a4f09) ^ (seed >>> 16)) & 0xffffffff;
michael@0 96 return (seed & 0xfffffff) / 0x10000000;
michael@0 97 };
michael@0 98 })();
michael@0 99
michael@0 100
michael@0 101 // Runs all registered benchmark suites and optionally yields between
michael@0 102 // each individual benchmark to avoid running for too long in the
michael@0 103 // context of browsers. Once done, the final score is reported to the
michael@0 104 // runner.
michael@0 105 BenchmarkSuite.RunSuites = function(runner) {
michael@0 106 var continuation = null;
michael@0 107 var suites = BenchmarkSuite.suites;
michael@0 108 var length = suites.length;
michael@0 109 BenchmarkSuite.scores = [];
michael@0 110 var index = 0;
michael@0 111 function RunStep() {
michael@0 112 while (continuation || index < length) {
michael@0 113 if (continuation) {
michael@0 114 continuation = continuation();
michael@0 115 } else {
michael@0 116 var suite = suites[index++];
michael@0 117 if (runner.NotifyStart) runner.NotifyStart(suite.name);
michael@0 118 continuation = suite.RunStep(runner);
michael@0 119 }
michael@0 120 if (continuation && typeof window != 'undefined' && window.setTimeout) {
michael@0 121 window.setTimeout(RunStep, 25);
michael@0 122 return;
michael@0 123 }
michael@0 124 }
michael@0 125 if (runner.NotifyScore) {
michael@0 126 var score = BenchmarkSuite.GeometricMean(BenchmarkSuite.scores);
michael@0 127 var formatted = BenchmarkSuite.FormatScore(100 * score);
michael@0 128 runner.NotifyScore(formatted);
michael@0 129 }
michael@0 130 }
michael@0 131 RunStep();
michael@0 132 }
michael@0 133
michael@0 134
michael@0 135 // Counts the total number of registered benchmarks. Useful for
michael@0 136 // showing progress as a percentage.
michael@0 137 BenchmarkSuite.CountBenchmarks = function() {
michael@0 138 var result = 0;
michael@0 139 var suites = BenchmarkSuite.suites;
michael@0 140 for (var i = 0; i < suites.length; i++) {
michael@0 141 result += suites[i].benchmarks.length;
michael@0 142 }
michael@0 143 return result;
michael@0 144 }
michael@0 145
michael@0 146
michael@0 147 // Computes the geometric mean of a set of numbers.
michael@0 148 BenchmarkSuite.GeometricMean = function(numbers) {
michael@0 149 var log = 0;
michael@0 150 for (var i = 0; i < numbers.length; i++) {
michael@0 151 log += Math.log(numbers[i]);
michael@0 152 }
michael@0 153 return Math.pow(Math.E, log / numbers.length);
michael@0 154 }
michael@0 155
michael@0 156
michael@0 157 // Converts a score value to a string with at least three significant
michael@0 158 // digits.
michael@0 159 BenchmarkSuite.FormatScore = function(value) {
michael@0 160 if (value > 100) {
michael@0 161 return value.toFixed(0);
michael@0 162 } else {
michael@0 163 return value.toPrecision(3);
michael@0 164 }
michael@0 165 }
michael@0 166
michael@0 167 // Notifies the runner that we're done running a single benchmark in
michael@0 168 // the benchmark suite. This can be useful to report progress.
michael@0 169 BenchmarkSuite.prototype.NotifyStep = function(result) {
michael@0 170 this.results.push(result);
michael@0 171 if (this.runner.NotifyStep) this.runner.NotifyStep(result.benchmark.name);
michael@0 172 }
michael@0 173
michael@0 174
michael@0 175 // Notifies the runner that we're done with running a suite and that
michael@0 176 // we have a result which can be reported to the user if needed.
michael@0 177 BenchmarkSuite.prototype.NotifyResult = function() {
michael@0 178 var mean = BenchmarkSuite.GeometricMean(this.results);
michael@0 179 var score = this.reference / mean;
michael@0 180 BenchmarkSuite.scores.push(score);
michael@0 181 if (this.runner.NotifyResult) {
michael@0 182 var formatted = BenchmarkSuite.FormatScore(100 * score);
michael@0 183 this.runner.NotifyResult(this.name, formatted);
michael@0 184 }
michael@0 185 }
michael@0 186
michael@0 187
michael@0 188 // Notifies the runner that running a benchmark resulted in an error.
michael@0 189 BenchmarkSuite.prototype.NotifyError = function(error) {
michael@0 190 if (this.runner.NotifyError) {
michael@0 191 this.runner.NotifyError(this.name, error);
michael@0 192 }
michael@0 193 if (this.runner.NotifyStep) {
michael@0 194 this.runner.NotifyStep(this.name);
michael@0 195 }
michael@0 196 }
michael@0 197
michael@0 198
michael@0 199 // Runs a single benchmark for at least a second and computes the
michael@0 200 // average time it takes to run a single iteration.
michael@0 201 BenchmarkSuite.prototype.RunSingleBenchmark = function(benchmark) {
michael@0 202 var elapsed = 0;
michael@0 203 var start = new Date();
michael@0 204 for (var n = 0; elapsed < 200; n++) {
michael@0 205 benchmark.run();
michael@0 206 elapsed = new Date() - start;
michael@0 207 }
michael@0 208 var usec = (elapsed * 1000) / n;
michael@0 209 this.NotifyStep(new BenchmarkResult(benchmark, usec));
michael@0 210 }
michael@0 211
michael@0 212
michael@0 213 // This function starts running a suite, but stops between each
michael@0 214 // individual benchmark in the suite and returns a continuation
michael@0 215 // function which can be invoked to run the next benchmark. Once the
michael@0 216 // last benchmark has been executed, null is returned.
michael@0 217 BenchmarkSuite.prototype.RunStep = function(runner) {
michael@0 218 this.results = [];
michael@0 219 this.runner = runner;
michael@0 220 var length = this.benchmarks.length;
michael@0 221 var index = 0;
michael@0 222 var suite = this;
michael@0 223
michael@0 224 // Run the setup, the actual benchmark, and the tear down in three
michael@0 225 // separate steps to allow the framework to yield between any of the
michael@0 226 // steps.
michael@0 227
michael@0 228 function RunNextSetup() {
michael@0 229 if (index < length) {
michael@0 230 try {
michael@0 231 suite.benchmarks[index].Setup();
michael@0 232 } catch (e) {
michael@0 233 suite.NotifyError(e);
michael@0 234 return null;
michael@0 235 }
michael@0 236 return RunNextBenchmark;
michael@0 237 }
michael@0 238 suite.NotifyResult();
michael@0 239 return null;
michael@0 240 }
michael@0 241
michael@0 242 function RunNextBenchmark() {
michael@0 243 try {
michael@0 244 suite.RunSingleBenchmark(suite.benchmarks[index]);
michael@0 245 } catch (e) {
michael@0 246 suite.NotifyError(e);
michael@0 247 return null;
michael@0 248 }
michael@0 249 return RunNextTearDown;
michael@0 250 }
michael@0 251
michael@0 252 function RunNextTearDown() {
michael@0 253 try {
michael@0 254 suite.benchmarks[index++].TearDown();
michael@0 255 } catch (e) {
michael@0 256 suite.NotifyError(e);
michael@0 257 return null;
michael@0 258 }
michael@0 259 return RunNextSetup;
michael@0 260 }
michael@0 261
michael@0 262 // Start out running the setup.
michael@0 263 return RunNextSetup();
michael@0 264 }
michael@0 265
michael@0 266
michael@0 267 // Copyright 2008 Google Inc. All Rights Reserved.
michael@0 268 // Copyright 1996 John Maloney and Mario Wolczko.
michael@0 269
michael@0 270 // This program is free software; you can redistribute it and/or modify
michael@0 271 // it under the terms of the GNU General Public License as published by
michael@0 272 // the Free Software Foundation; either version 2 of the License, or
michael@0 273 // (at your option) any later version.
michael@0 274 //
michael@0 275 // This program is distributed in the hope that it will be useful,
michael@0 276 // but WITHOUT ANY WARRANTY; without even the implied warranty of
michael@0 277 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
michael@0 278 // GNU General Public License for more details.
michael@0 279 //
michael@0 280 // You should have received a copy of the GNU General Public License
michael@0 281 // along with this program; if not, write to the Free Software
michael@0 282 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
michael@0 283
michael@0 284
michael@0 285 // This implementation of the DeltaBlue benchmark is derived
michael@0 286 // from the Smalltalk implementation by John Maloney and Mario
michael@0 287 // Wolczko. Some parts have been translated directly, whereas
michael@0 288 // others have been modified more aggresively to make it feel
michael@0 289 // more like a JavaScript program.
michael@0 290
michael@0 291
michael@0 292 var DeltaBlue = new BenchmarkSuite('DeltaBlue', 71104, [
michael@0 293 new Benchmark('DeltaBlue', deltaBlue)
michael@0 294 ]);
michael@0 295
michael@0 296
michael@0 297 /**
michael@0 298 * A JavaScript implementation of the DeltaBlue constrain-solving
michael@0 299 * algorithm, as described in:
michael@0 300 *
michael@0 301 * "The DeltaBlue Algorithm: An Incremental Constraint Hierarchy Solver"
michael@0 302 * Bjorn N. Freeman-Benson and John Maloney
michael@0 303 * January 1990 Communications of the ACM,
michael@0 304 * also available as University of Washington TR 89-08-06.
michael@0 305 *
michael@0 306 * Beware: this benchmark is written in a grotesque style where
michael@0 307 * the constraint model is built by side-effects from constructors.
michael@0 308 * I've kept it this way to avoid deviating too much from the original
michael@0 309 * implementation.
michael@0 310 */
michael@0 311
michael@0 312
michael@0 313 /* --- O b j e c t M o d e l --- */
michael@0 314
michael@0 315 Object.prototype.inherits = function (shuper) {
michael@0 316 function Inheriter() { }
michael@0 317 Inheriter.prototype = shuper.prototype;
michael@0 318 this.prototype = new Inheriter();
michael@0 319 this.superConstructor = shuper;
michael@0 320 }
michael@0 321
michael@0 322 function OrderedCollection() {
michael@0 323 this.elms = new Array();
michael@0 324 }
michael@0 325
michael@0 326 OrderedCollection.prototype.add = function (elm) {
michael@0 327 this.elms.push(elm);
michael@0 328 }
michael@0 329
michael@0 330 OrderedCollection.prototype.at = function (index) {
michael@0 331 return this.elms[index];
michael@0 332 }
michael@0 333
michael@0 334 OrderedCollection.prototype.size = function () {
michael@0 335 return this.elms.length;
michael@0 336 }
michael@0 337
michael@0 338 OrderedCollection.prototype.removeFirst = function () {
michael@0 339 return this.elms.pop();
michael@0 340 }
michael@0 341
michael@0 342 OrderedCollection.prototype.remove = function (elm) {
michael@0 343 var index = 0, skipped = 0;
michael@0 344 /* BEGIN LOOP */
michael@0 345 for (var i = 0; i < this.elms.length; i++) {
michael@0 346 var value = this.elms[i];
michael@0 347 if (value != elm) {
michael@0 348 this.elms[index] = value;
michael@0 349 index++;
michael@0 350 } else {
michael@0 351 skipped++;
michael@0 352 }
michael@0 353 }
michael@0 354 /* END LOOP */
michael@0 355 /* BEGIN LOOP */
michael@0 356 for (var i = 0; i < skipped; i++)
michael@0 357 this.elms.pop();
michael@0 358 /* END LOOP */
michael@0 359 }
michael@0 360
michael@0 361 /* --- *
michael@0 362 * S t r e n g t h
michael@0 363 * --- */
michael@0 364
michael@0 365 /**
michael@0 366 * Strengths are used to measure the relative importance of constraints.
michael@0 367 * New strengths may be inserted in the strength hierarchy without
michael@0 368 * disrupting current constraints. Strengths cannot be created outside
michael@0 369 * this class, so pointer comparison can be used for value comparison.
michael@0 370 */
michael@0 371 function Strength(strengthValue, name) {
michael@0 372 this.strengthValue = strengthValue;
michael@0 373 this.name = name;
michael@0 374 }
michael@0 375
michael@0 376 Strength.stronger = function (s1, s2) {
michael@0 377 return s1.strengthValue < s2.strengthValue;
michael@0 378 }
michael@0 379
michael@0 380 Strength.weaker = function (s1, s2) {
michael@0 381 return s1.strengthValue > s2.strengthValue;
michael@0 382 }
michael@0 383
michael@0 384 Strength.weakestOf = function (s1, s2) {
michael@0 385 return this.weaker(s1, s2) ? s1 : s2;
michael@0 386 }
michael@0 387
michael@0 388 Strength.strongest = function (s1, s2) {
michael@0 389 return this.stronger(s1, s2) ? s1 : s2;
michael@0 390 }
michael@0 391
michael@0 392 Strength.prototype.nextWeaker = function () {
michael@0 393 switch (this.strengthValue) {
michael@0 394 case 0: return Strength.WEAKEST;
michael@0 395 case 1: return Strength.WEAK_DEFAULT;
michael@0 396 case 2: return Strength.NORMAL;
michael@0 397 case 3: return Strength.STRONG_DEFAULT;
michael@0 398 case 4: return Strength.PREFERRED;
michael@0 399 case 5: return Strength.REQUIRED;
michael@0 400 }
michael@0 401 }
michael@0 402
michael@0 403 // Strength constants.
michael@0 404 Strength.REQUIRED = new Strength(0, "required");
michael@0 405 Strength.STONG_PREFERRED = new Strength(1, "strongPreferred");
michael@0 406 Strength.PREFERRED = new Strength(2, "preferred");
michael@0 407 Strength.STRONG_DEFAULT = new Strength(3, "strongDefault");
michael@0 408 Strength.NORMAL = new Strength(4, "normal");
michael@0 409 Strength.WEAK_DEFAULT = new Strength(5, "weakDefault");
michael@0 410 Strength.WEAKEST = new Strength(6, "weakest");
michael@0 411
michael@0 412 /* --- *
michael@0 413 * C o n s t r a i n t
michael@0 414 * --- */
michael@0 415
michael@0 416 /**
michael@0 417 * An abstract class representing a system-maintainable relationship
michael@0 418 * (or "constraint") between a set of variables. A constraint supplies
michael@0 419 * a strength instance variable; concrete subclasses provide a means
michael@0 420 * of storing the constrained variables and other information required
michael@0 421 * to represent a constraint.
michael@0 422 */
michael@0 423 function Constraint(strength) {
michael@0 424 this.strength = strength;
michael@0 425 }
michael@0 426
michael@0 427 /**
michael@0 428 * Activate this constraint and attempt to satisfy it.
michael@0 429 */
michael@0 430 Constraint.prototype.addConstraint = function () {
michael@0 431 this.addToGraph();
michael@0 432 planner.incrementalAdd(this);
michael@0 433 }
michael@0 434
michael@0 435 /**
michael@0 436 * Attempt to find a way to enforce this constraint. If successful,
michael@0 437 * record the solution, perhaps modifying the current dataflow
michael@0 438 * graph. Answer the constraint that this constraint overrides, if
michael@0 439 * there is one, or nil, if there isn't.
michael@0 440 * Assume: I am not already satisfied.
michael@0 441 */
michael@0 442 Constraint.prototype.satisfy = function (mark) {
michael@0 443 this.chooseMethod(mark);
michael@0 444 if (!this.isSatisfied()) {
michael@0 445 if (this.strength == Strength.REQUIRED)
michael@0 446 alert("Could not satisfy a required constraint!");
michael@0 447 return null;
michael@0 448 }
michael@0 449 this.markInputs(mark);
michael@0 450 var out = this.output();
michael@0 451 var overridden = out.determinedBy;
michael@0 452 if (overridden != null) overridden.markUnsatisfied();
michael@0 453 out.determinedBy = this;
michael@0 454 if (!planner.addPropagate(this, mark))
michael@0 455 alert("Cycle encountered");
michael@0 456 out.mark = mark;
michael@0 457 return overridden;
michael@0 458 }
michael@0 459
michael@0 460 Constraint.prototype.destroyConstraint = function () {
michael@0 461 if (this.isSatisfied()) planner.incrementalRemove(this);
michael@0 462 else this.removeFromGraph();
michael@0 463 }
michael@0 464
michael@0 465 /**
michael@0 466 * Normal constraints are not input constraints. An input constraint
michael@0 467 * is one that depends on external state, such as the mouse, the
michael@0 468 * keybord, a clock, or some arbitraty piece of imperative code.
michael@0 469 */
michael@0 470 Constraint.prototype.isInput = function () {
michael@0 471 return false;
michael@0 472 }
michael@0 473
michael@0 474 /* --- *
michael@0 475 * U n a r y C o n s t r a i n t
michael@0 476 * --- */
michael@0 477
michael@0 478 /**
michael@0 479 * Abstract superclass for constraints having a single possible output
michael@0 480 * variable.
michael@0 481 */
michael@0 482 function UnaryConstraint(v, strength) {
michael@0 483 UnaryConstraint.superConstructor.call(this, strength);
michael@0 484 this.myOutput = v;
michael@0 485 this.satisfied = false;
michael@0 486 this.addConstraint();
michael@0 487 }
michael@0 488
michael@0 489 UnaryConstraint.inherits(Constraint);
michael@0 490
michael@0 491 /**
michael@0 492 * Adds this constraint to the constraint graph
michael@0 493 */
michael@0 494 UnaryConstraint.prototype.addToGraph = function () {
michael@0 495 this.myOutput.addConstraint(this);
michael@0 496 this.satisfied = false;
michael@0 497 }
michael@0 498
michael@0 499 /**
michael@0 500 * Decides if this constraint can be satisfied and records that
michael@0 501 * decision.
michael@0 502 */
michael@0 503 UnaryConstraint.prototype.chooseMethod = function (mark) {
michael@0 504 this.satisfied = (this.myOutput.mark != mark)
michael@0 505 && Strength.stronger(this.strength, this.myOutput.walkStrength);
michael@0 506 }
michael@0 507
michael@0 508 /**
michael@0 509 * Returns true if this constraint is satisfied in the current solution.
michael@0 510 */
michael@0 511 UnaryConstraint.prototype.isSatisfied = function () {
michael@0 512 return this.satisfied;
michael@0 513 }
michael@0 514
michael@0 515 UnaryConstraint.prototype.markInputs = function (mark) {
michael@0 516 // has no inputs
michael@0 517 }
michael@0 518
michael@0 519 /**
michael@0 520 * Returns the current output variable.
michael@0 521 */
michael@0 522 UnaryConstraint.prototype.output = function () {
michael@0 523 return this.myOutput;
michael@0 524 }
michael@0 525
michael@0 526 /**
michael@0 527 * Calculate the walkabout strength, the stay flag, and, if it is
michael@0 528 * 'stay', the value for the current output of this constraint. Assume
michael@0 529 * this constraint is satisfied.
michael@0 530 */
michael@0 531 UnaryConstraint.prototype.recalculate = function () {
michael@0 532 this.myOutput.walkStrength = this.strength;
michael@0 533 this.myOutput.stay = !this.isInput();
michael@0 534 if (this.myOutput.stay) this.execute(); // Stay optimization
michael@0 535 }
michael@0 536
michael@0 537 /**
michael@0 538 * Records that this constraint is unsatisfied
michael@0 539 */
michael@0 540 UnaryConstraint.prototype.markUnsatisfied = function () {
michael@0 541 this.satisfied = false;
michael@0 542 }
michael@0 543
michael@0 544 UnaryConstraint.prototype.inputsKnown = function () {
michael@0 545 return true;
michael@0 546 }
michael@0 547
michael@0 548 UnaryConstraint.prototype.removeFromGraph = function () {
michael@0 549 if (this.myOutput != null) this.myOutput.removeConstraint(this);
michael@0 550 this.satisfied = false;
michael@0 551 }
michael@0 552
michael@0 553 /* --- *
michael@0 554 * S t a y C o n s t r a i n t
michael@0 555 * --- */
michael@0 556
michael@0 557 /**
michael@0 558 * Variables that should, with some level of preference, stay the same.
michael@0 559 * Planners may exploit the fact that instances, if satisfied, will not
michael@0 560 * change their output during plan execution. This is called "stay
michael@0 561 * optimization".
michael@0 562 */
michael@0 563 function StayConstraint(v, str) {
michael@0 564 StayConstraint.superConstructor.call(this, v, str);
michael@0 565 }
michael@0 566
michael@0 567 StayConstraint.inherits(UnaryConstraint);
michael@0 568
michael@0 569 StayConstraint.prototype.execute = function () {
michael@0 570 // Stay constraints do nothing
michael@0 571 }
michael@0 572
michael@0 573 /* --- *
michael@0 574 * E d i t C o n s t r a i n t
michael@0 575 * --- */
michael@0 576
michael@0 577 /**
michael@0 578 * A unary input constraint used to mark a variable that the client
michael@0 579 * wishes to change.
michael@0 580 */
michael@0 581 function EditConstraint(v, str) {
michael@0 582 EditConstraint.superConstructor.call(this, v, str);
michael@0 583 }
michael@0 584
michael@0 585 EditConstraint.inherits(UnaryConstraint);
michael@0 586
michael@0 587 /**
michael@0 588 * Edits indicate that a variable is to be changed by imperative code.
michael@0 589 */
michael@0 590 EditConstraint.prototype.isInput = function () {
michael@0 591 return true;
michael@0 592 }
michael@0 593
michael@0 594 EditConstraint.prototype.execute = function () {
michael@0 595 // Edit constraints do nothing
michael@0 596 }
michael@0 597
michael@0 598 /* --- *
michael@0 599 * B i n a r y C o n s t r a i n t
michael@0 600 * --- */
michael@0 601
michael@0 602 var Direction = new Object();
michael@0 603 Direction.NONE = 0;
michael@0 604 Direction.FORWARD = 1;
michael@0 605 Direction.BACKWARD = -1;
michael@0 606
michael@0 607 /**
michael@0 608 * Abstract superclass for constraints having two possible output
michael@0 609 * variables.
michael@0 610 */
michael@0 611 function BinaryConstraint(var1, var2, strength) {
michael@0 612 BinaryConstraint.superConstructor.call(this, strength);
michael@0 613 this.v1 = var1;
michael@0 614 this.v2 = var2;
michael@0 615 this.direction = Direction.NONE;
michael@0 616 this.addConstraint();
michael@0 617 }
michael@0 618
michael@0 619 BinaryConstraint.inherits(Constraint);
michael@0 620
michael@0 621 /**
michael@0 622 * Decides if this constratint can be satisfied and which way it
michael@0 623 * should flow based on the relative strength of the variables related,
michael@0 624 * and record that decision.
michael@0 625 */
michael@0 626 BinaryConstraint.prototype.chooseMethod = function (mark) {
michael@0 627 if (this.v1.mark == mark) {
michael@0 628 this.direction = (this.v1.mark != mark && Strength.stronger(this.strength, this.v2.walkStrength))
michael@0 629 ? Direction.FORWARD
michael@0 630 : Direction.NONE;
michael@0 631 }
michael@0 632 if (this.v2.mark == mark) {
michael@0 633 this.direction = (this.v1.mark != mark && Strength.stronger(this.strength, this.v1.walkStrength))
michael@0 634 ? Direction.BACKWARD
michael@0 635 : Direction.NONE;
michael@0 636 }
michael@0 637 if (Strength.weaker(this.v1.walkStrength, this.v2.walkStrength)) {
michael@0 638 this.direction = Strength.stronger(this.strength, this.v1.walkStrength)
michael@0 639 ? Direction.BACKWARD
michael@0 640 : Direction.NONE;
michael@0 641 } else {
michael@0 642 this.direction = Strength.stronger(this.strength, this.v2.walkStrength)
michael@0 643 ? Direction.FORWARD
michael@0 644 : Direction.BACKWARD
michael@0 645 }
michael@0 646 }
michael@0 647
michael@0 648 /**
michael@0 649 * Add this constraint to the constraint graph
michael@0 650 */
michael@0 651 BinaryConstraint.prototype.addToGraph = function () {
michael@0 652 this.v1.addConstraint(this);
michael@0 653 this.v2.addConstraint(this);
michael@0 654 this.direction = Direction.NONE;
michael@0 655 }
michael@0 656
michael@0 657 /**
michael@0 658 * Answer true if this constraint is satisfied in the current solution.
michael@0 659 */
michael@0 660 BinaryConstraint.prototype.isSatisfied = function () {
michael@0 661 return this.direction != Direction.NONE;
michael@0 662 }
michael@0 663
michael@0 664 /**
michael@0 665 * Mark the input variable with the given mark.
michael@0 666 */
michael@0 667 BinaryConstraint.prototype.markInputs = function (mark) {
michael@0 668 this.input().mark = mark;
michael@0 669 }
michael@0 670
michael@0 671 /**
michael@0 672 * Returns the current input variable
michael@0 673 */
michael@0 674 BinaryConstraint.prototype.input = function () {
michael@0 675 return (this.direction == Direction.FORWARD) ? this.v1 : this.v2;
michael@0 676 }
michael@0 677
michael@0 678 /**
michael@0 679 * Returns the current output variable
michael@0 680 */
michael@0 681 BinaryConstraint.prototype.output = function () {
michael@0 682 return (this.direction == Direction.FORWARD) ? this.v2 : this.v1;
michael@0 683 }
michael@0 684
michael@0 685 /**
michael@0 686 * Calculate the walkabout strength, the stay flag, and, if it is
michael@0 687 * 'stay', the value for the current output of this
michael@0 688 * constraint. Assume this constraint is satisfied.
michael@0 689 */
michael@0 690 BinaryConstraint.prototype.recalculate = function () {
michael@0 691 var ihn = this.input(), out = this.output();
michael@0 692 out.walkStrength = Strength.weakestOf(this.strength, ihn.walkStrength);
michael@0 693 out.stay = ihn.stay;
michael@0 694 if (out.stay) this.execute();
michael@0 695 }
michael@0 696
michael@0 697 /**
michael@0 698 * Record the fact that this constraint is unsatisfied.
michael@0 699 */
michael@0 700 BinaryConstraint.prototype.markUnsatisfied = function () {
michael@0 701 this.direction = Direction.NONE;
michael@0 702 }
michael@0 703
michael@0 704 BinaryConstraint.prototype.inputsKnown = function (mark) {
michael@0 705 var i = this.input();
michael@0 706 return i.mark == mark || i.stay || i.determinedBy == null;
michael@0 707 }
michael@0 708
michael@0 709 BinaryConstraint.prototype.removeFromGraph = function () {
michael@0 710 if (this.v1 != null) this.v1.removeConstraint(this);
michael@0 711 if (this.v2 != null) this.v2.removeConstraint(this);
michael@0 712 this.direction = Direction.NONE;
michael@0 713 }
michael@0 714
michael@0 715 /* --- *
michael@0 716 * S c a l e C o n s t r a i n t
michael@0 717 * --- */
michael@0 718
michael@0 719 /**
michael@0 720 * Relates two variables by the linear scaling relationship: "v2 =
michael@0 721 * (v1 * scale) + offset". Either v1 or v2 may be changed to maintain
michael@0 722 * this relationship but the scale factor and offset are considered
michael@0 723 * read-only.
michael@0 724 */
michael@0 725 function ScaleConstraint(src, scale, offset, dest, strength) {
michael@0 726 this.direction = Direction.NONE;
michael@0 727 this.scale = scale;
michael@0 728 this.offset = offset;
michael@0 729 ScaleConstraint.superConstructor.call(this, src, dest, strength);
michael@0 730 }
michael@0 731
michael@0 732 ScaleConstraint.inherits(BinaryConstraint);
michael@0 733
michael@0 734 /**
michael@0 735 * Adds this constraint to the constraint graph.
michael@0 736 */
michael@0 737 ScaleConstraint.prototype.addToGraph = function () {
michael@0 738 ScaleConstraint.superConstructor.prototype.addToGraph.call(this);
michael@0 739 this.scale.addConstraint(this);
michael@0 740 this.offset.addConstraint(this);
michael@0 741 }
michael@0 742
michael@0 743 ScaleConstraint.prototype.removeFromGraph = function () {
michael@0 744 ScaleConstraint.superConstructor.prototype.removeFromGraph.call(this);
michael@0 745 if (this.scale != null) this.scale.removeConstraint(this);
michael@0 746 if (this.offset != null) this.offset.removeConstraint(this);
michael@0 747 }
michael@0 748
michael@0 749 ScaleConstraint.prototype.markInputs = function (mark) {
michael@0 750 ScaleConstraint.superConstructor.prototype.markInputs.call(this, mark);
michael@0 751 this.scale.mark = this.offset.mark = mark;
michael@0 752 }
michael@0 753
michael@0 754 /**
michael@0 755 * Enforce this constraint. Assume that it is satisfied.
michael@0 756 */
michael@0 757 ScaleConstraint.prototype.execute = function () {
michael@0 758 if (this.direction == Direction.FORWARD) {
michael@0 759 this.v2.value = this.v1.value * this.scale.value + this.offset.value;
michael@0 760 } else {
michael@0 761 this.v1.value = (this.v2.value - this.offset.value) / this.scale.value;
michael@0 762 }
michael@0 763 }
michael@0 764
michael@0 765 /**
michael@0 766 * Calculate the walkabout strength, the stay flag, and, if it is
michael@0 767 * 'stay', the value for the current output of this constraint. Assume
michael@0 768 * this constraint is satisfied.
michael@0 769 */
michael@0 770 ScaleConstraint.prototype.recalculate = function () {
michael@0 771 var ihn = this.input(), out = this.output();
michael@0 772 out.walkStrength = Strength.weakestOf(this.strength, ihn.walkStrength);
michael@0 773 out.stay = ihn.stay && this.scale.stay && this.offset.stay;
michael@0 774 if (out.stay) this.execute();
michael@0 775 }
michael@0 776
michael@0 777 /* --- *
michael@0 778 * E q u a l i t y C o n s t r a i n t
michael@0 779 * --- */
michael@0 780
michael@0 781 /**
michael@0 782 * Constrains two variables to have the same value.
michael@0 783 */
michael@0 784 function EqualityConstraint(var1, var2, strength) {
michael@0 785 EqualityConstraint.superConstructor.call(this, var1, var2, strength);
michael@0 786 }
michael@0 787
michael@0 788 EqualityConstraint.inherits(BinaryConstraint);
michael@0 789
michael@0 790 /**
michael@0 791 * Enforce this constraint. Assume that it is satisfied.
michael@0 792 */
michael@0 793 EqualityConstraint.prototype.execute = function () {
michael@0 794 this.output().value = this.input().value;
michael@0 795 }
michael@0 796
michael@0 797 /* --- *
michael@0 798 * V a r i a b l e
michael@0 799 * --- */
michael@0 800
michael@0 801 /**
michael@0 802 * A constrained variable. In addition to its value, it maintain the
michael@0 803 * structure of the constraint graph, the current dataflow graph, and
michael@0 804 * various parameters of interest to the DeltaBlue incremental
michael@0 805 * constraint solver.
michael@0 806 **/
michael@0 807 function Variable(name, initialValue) {
michael@0 808 this.value = initialValue || 0;
michael@0 809 this.constraints = new OrderedCollection();
michael@0 810 this.determinedBy = null;
michael@0 811 this.mark = 0;
michael@0 812 this.walkStrength = Strength.WEAKEST;
michael@0 813 this.stay = true;
michael@0 814 this.name = name;
michael@0 815 }
michael@0 816
michael@0 817 /**
michael@0 818 * Add the given constraint to the set of all constraints that refer
michael@0 819 * this variable.
michael@0 820 */
michael@0 821 Variable.prototype.addConstraint = function (c) {
michael@0 822 this.constraints.add(c);
michael@0 823 }
michael@0 824
michael@0 825 /**
michael@0 826 * Removes all traces of c from this variable.
michael@0 827 */
michael@0 828 Variable.prototype.removeConstraint = function (c) {
michael@0 829 this.constraints.remove(c);
michael@0 830 if (this.determinedBy == c) this.determinedBy = null;
michael@0 831 }
michael@0 832
michael@0 833 /* --- *
michael@0 834 * P l a n n e r
michael@0 835 * --- */
michael@0 836
michael@0 837 /**
michael@0 838 * The DeltaBlue planner
michael@0 839 */
michael@0 840 function Planner() {
michael@0 841 this.currentMark = 0;
michael@0 842 }
michael@0 843
michael@0 844 /**
michael@0 845 * Attempt to satisfy the given constraint and, if successful,
michael@0 846 * incrementally update the dataflow graph. Details: If satifying
michael@0 847 * the constraint is successful, it may override a weaker constraint
michael@0 848 * on its output. The algorithm attempts to resatisfy that
michael@0 849 * constraint using some other method. This process is repeated
michael@0 850 * until either a) it reaches a variable that was not previously
michael@0 851 * determined by any constraint or b) it reaches a constraint that
michael@0 852 * is too weak to be satisfied using any of its methods. The
michael@0 853 * variables of constraints that have been processed are marked with
michael@0 854 * a unique mark value so that we know where we've been. This allows
michael@0 855 * the algorithm to avoid getting into an infinite loop even if the
michael@0 856 * constraint graph has an inadvertent cycle.
michael@0 857 */
michael@0 858 Planner.prototype.incrementalAdd = function (c) {
michael@0 859 var mark = this.newMark();
michael@0 860 var overridden = c.satisfy(mark);
michael@0 861 /* BEGIN LOOP */
michael@0 862 while (overridden != null)
michael@0 863 overridden = overridden.satisfy(mark);
michael@0 864 /* END LOOP */
michael@0 865 }
michael@0 866
michael@0 867 /**
michael@0 868 * Entry point for retracting a constraint. Remove the given
michael@0 869 * constraint and incrementally update the dataflow graph.
michael@0 870 * Details: Retracting the given constraint may allow some currently
michael@0 871 * unsatisfiable downstream constraint to be satisfied. We therefore collect
michael@0 872 * a list of unsatisfied downstream constraints and attempt to
michael@0 873 * satisfy each one in turn. This list is traversed by constraint
michael@0 874 * strength, strongest first, as a heuristic for avoiding
michael@0 875 * unnecessarily adding and then overriding weak constraints.
michael@0 876 * Assume: c is satisfied.
michael@0 877 */
michael@0 878 Planner.prototype.incrementalRemove = function (c) {
michael@0 879 var out = c.output();
michael@0 880 c.markUnsatisfied();
michael@0 881 c.removeFromGraph();
michael@0 882 var unsatisfied = this.removePropagateFrom(out);
michael@0 883 var strength = Strength.REQUIRED;
michael@0 884 /* BEGIN LOOP */
michael@0 885 do {
michael@0 886 /* BEGIN LOOP */
michael@0 887 for (var i = 0; i < unsatisfied.size(); i++) {
michael@0 888 var u = unsatisfied.at(i);
michael@0 889 if (u.strength == strength)
michael@0 890 this.incrementalAdd(u);
michael@0 891 }
michael@0 892 /* END LOOP */
michael@0 893 strength = strength.nextWeaker();
michael@0 894 } while (strength != Strength.WEAKEST);
michael@0 895 /* END LOOP */
michael@0 896 }
michael@0 897
michael@0 898 /**
michael@0 899 * Select a previously unused mark value.
michael@0 900 */
michael@0 901 Planner.prototype.newMark = function () {
michael@0 902 return ++this.currentMark;
michael@0 903 }
michael@0 904
michael@0 905 /**
michael@0 906 * Extract a plan for resatisfaction starting from the given source
michael@0 907 * constraints, usually a set of input constraints. This method
michael@0 908 * assumes that stay optimization is desired; the plan will contain
michael@0 909 * only constraints whose output variables are not stay. Constraints
michael@0 910 * that do no computation, such as stay and edit constraints, are
michael@0 911 * not included in the plan.
michael@0 912 * Details: The outputs of a constraint are marked when it is added
michael@0 913 * to the plan under construction. A constraint may be appended to
michael@0 914 * the plan when all its input variables are known. A variable is
michael@0 915 * known if either a) the variable is marked (indicating that has
michael@0 916 * been computed by a constraint appearing earlier in the plan), b)
michael@0 917 * the variable is 'stay' (i.e. it is a constant at plan execution
michael@0 918 * time), or c) the variable is not determined by any
michael@0 919 * constraint. The last provision is for past states of history
michael@0 920 * variables, which are not stay but which are also not computed by
michael@0 921 * any constraint.
michael@0 922 * Assume: sources are all satisfied.
michael@0 923 */
michael@0 924 Planner.prototype.makePlan = function (sources) {
michael@0 925 var mark = this.newMark();
michael@0 926 var plan = new Plan();
michael@0 927 var todo = sources;
michael@0 928 /* BEGIN LOOP */
michael@0 929 while (todo.size() > 0) {
michael@0 930 var c = todo.removeFirst();
michael@0 931 if (c.output().mark != mark && c.inputsKnown(mark)) {
michael@0 932 plan.addConstraint(c);
michael@0 933 c.output().mark = mark;
michael@0 934 this.addConstraintsConsumingTo(c.output(), todo);
michael@0 935 }
michael@0 936 }
michael@0 937 /* END LOOP */
michael@0 938 return plan;
michael@0 939 }
michael@0 940
michael@0 941 /**
michael@0 942 * Extract a plan for resatisfying starting from the output of the
michael@0 943 * given constraints, usually a set of input constraints.
michael@0 944 */
michael@0 945 Planner.prototype.extractPlanFromConstraints = function (constraints) {
michael@0 946 var sources = new OrderedCollection();
michael@0 947 /* BEGIN LOOP */
michael@0 948 for (var i = 0; i < constraints.size(); i++) {
michael@0 949 var c = constraints.at(i);
michael@0 950 if (c.isInput() && c.isSatisfied())
michael@0 951 // not in plan already and eligible for inclusion
michael@0 952 sources.add(c);
michael@0 953 }
michael@0 954 /* END LOOP */
michael@0 955 return this.makePlan(sources);
michael@0 956 }
michael@0 957
michael@0 958 /**
michael@0 959 * Recompute the walkabout strengths and stay flags of all variables
michael@0 960 * downstream of the given constraint and recompute the actual
michael@0 961 * values of all variables whose stay flag is true. If a cycle is
michael@0 962 * detected, remove the given constraint and answer
michael@0 963 * false. Otherwise, answer true.
michael@0 964 * Details: Cycles are detected when a marked variable is
michael@0 965 * encountered downstream of the given constraint. The sender is
michael@0 966 * assumed to have marked the inputs of the given constraint with
michael@0 967 * the given mark. Thus, encountering a marked node downstream of
michael@0 968 * the output constraint means that there is a path from the
michael@0 969 * constraint's output to one of its inputs.
michael@0 970 */
michael@0 971 Planner.prototype.addPropagate = function (c, mark) {
michael@0 972 var todo = new OrderedCollection();
michael@0 973 todo.add(c);
michael@0 974 /* BEGIN LOOP */
michael@0 975 while (todo.size() > 0) {
michael@0 976 var d = todo.removeFirst();
michael@0 977 if (d.output().mark == mark) {
michael@0 978 this.incrementalRemove(c);
michael@0 979 return false;
michael@0 980 }
michael@0 981 d.recalculate();
michael@0 982 this.addConstraintsConsumingTo(d.output(), todo);
michael@0 983 }
michael@0 984 /* END LOOP */
michael@0 985 return true;
michael@0 986 }
michael@0 987
michael@0 988
michael@0 989 /**
michael@0 990 * Update the walkabout strengths and stay flags of all variables
michael@0 991 * downstream of the given constraint. Answer a collection of
michael@0 992 * unsatisfied constraints sorted in order of decreasing strength.
michael@0 993 */
michael@0 994 Planner.prototype.removePropagateFrom = function (out) {
michael@0 995 out.determinedBy = null;
michael@0 996 out.walkStrength = Strength.WEAKEST;
michael@0 997 out.stay = true;
michael@0 998 var unsatisfied = new OrderedCollection();
michael@0 999 var todo = new OrderedCollection();
michael@0 1000 todo.add(out);
michael@0 1001 /* BEGIN LOOP */
michael@0 1002 while (todo.size() > 0) {
michael@0 1003 var v = todo.removeFirst();
michael@0 1004 /* BEGIN LOOP */
michael@0 1005 for (var i = 0; i < v.constraints.size(); i++) {
michael@0 1006 var c = v.constraints.at(i);
michael@0 1007 if (!c.isSatisfied())
michael@0 1008 unsatisfied.add(c);
michael@0 1009 }
michael@0 1010 /* END LOOP */
michael@0 1011 var determining = v.determinedBy;
michael@0 1012 /* BEGIN LOOP */
michael@0 1013 for (var i = 0; i < v.constraints.size(); i++) {
michael@0 1014 var next = v.constraints.at(i);
michael@0 1015 if (next != determining && next.isSatisfied()) {
michael@0 1016 next.recalculate();
michael@0 1017 todo.add(next.output());
michael@0 1018 }
michael@0 1019 }
michael@0 1020 /* END LOOP */
michael@0 1021 }
michael@0 1022 /* END LOOP */
michael@0 1023 return unsatisfied;
michael@0 1024 }
michael@0 1025
michael@0 1026 Planner.prototype.addConstraintsConsumingTo = function (v, coll) {
michael@0 1027 var determining = v.determinedBy;
michael@0 1028 var cc = v.constraints;
michael@0 1029 /* BEGIN LOOP */
michael@0 1030 for (var i = 0; i < cc.size(); i++) {
michael@0 1031 var c = cc.at(i);
michael@0 1032 if (c != determining && c.isSatisfied())
michael@0 1033 coll.add(c);
michael@0 1034 }
michael@0 1035 /* END LOOP */
michael@0 1036 }
michael@0 1037
michael@0 1038 /* --- *
michael@0 1039 * P l a n
michael@0 1040 * --- */
michael@0 1041
michael@0 1042 /**
michael@0 1043 * A Plan is an ordered list of constraints to be executed in sequence
michael@0 1044 * to resatisfy all currently satisfiable constraints in the face of
michael@0 1045 * one or more changing inputs.
michael@0 1046 */
michael@0 1047 function Plan() {
michael@0 1048 this.v = new OrderedCollection();
michael@0 1049 }
michael@0 1050
michael@0 1051 Plan.prototype.addConstraint = function (c) {
michael@0 1052 this.v.add(c);
michael@0 1053 }
michael@0 1054
michael@0 1055 Plan.prototype.size = function () {
michael@0 1056 return this.v.size();
michael@0 1057 }
michael@0 1058
michael@0 1059 Plan.prototype.constraintAt = function (index) {
michael@0 1060 return this.v.at(index);
michael@0 1061 }
michael@0 1062
michael@0 1063 Plan.prototype.execute = function () {
michael@0 1064 /* BEGIN LOOP */
michael@0 1065 for (var i = 0; i < this.size(); i++) {
michael@0 1066 var c = this.constraintAt(i);
michael@0 1067 c.execute();
michael@0 1068 }
michael@0 1069 /* END LOOP */
michael@0 1070 }
michael@0 1071
michael@0 1072 /* --- *
michael@0 1073 * M a i n
michael@0 1074 * --- */
michael@0 1075
michael@0 1076 /**
michael@0 1077 * This is the standard DeltaBlue benchmark. A long chain of equality
michael@0 1078 * constraints is constructed with a stay constraint on one end. An
michael@0 1079 * edit constraint is then added to the opposite end and the time is
michael@0 1080 * measured for adding and removing this constraint, and extracting
michael@0 1081 * and executing a constraint satisfaction plan. There are two cases.
michael@0 1082 * In case 1, the added constraint is stronger than the stay
michael@0 1083 * constraint and values must propagate down the entire length of the
michael@0 1084 * chain. In case 2, the added constraint is weaker than the stay
michael@0 1085 * constraint so it cannot be accomodated. The cost in this case is,
michael@0 1086 * of course, very low. Typical situations lie somewhere between these
michael@0 1087 * two extremes.
michael@0 1088 */
michael@0 1089 function chainTest(n) {
michael@0 1090 planner = new Planner();
michael@0 1091 var prev = null, first = null, last = null;
michael@0 1092
michael@0 1093 // Build chain of n equality constraints
michael@0 1094 /* BEGIN LOOP */
michael@0 1095 for (var i = 0; i <= n; i++) {
michael@0 1096 var name = "v" + i;
michael@0 1097 var v = new Variable(name);
michael@0 1098 if (prev != null)
michael@0 1099 new EqualityConstraint(prev, v, Strength.REQUIRED);
michael@0 1100 if (i == 0) first = v;
michael@0 1101 if (i == n) last = v;
michael@0 1102 prev = v;
michael@0 1103 }
michael@0 1104 /* END LOOP */
michael@0 1105
michael@0 1106 new StayConstraint(last, Strength.STRONG_DEFAULT);
michael@0 1107 var edit = new EditConstraint(first, Strength.PREFERRED);
michael@0 1108 var edits = new OrderedCollection();
michael@0 1109 edits.add(edit);
michael@0 1110 var plan = planner.extractPlanFromConstraints(edits);
michael@0 1111 /* BEGIN LOOP */
michael@0 1112 for (var i = 0; i < 100; i++) {
michael@0 1113 first.value = i;
michael@0 1114 plan.execute();
michael@0 1115 if (last.value != i)
michael@0 1116 alert("Chain test failed.");
michael@0 1117 }
michael@0 1118 /* END LOOP */
michael@0 1119 }
michael@0 1120
michael@0 1121 /**
michael@0 1122 * This test constructs a two sets of variables related to each
michael@0 1123 * other by a simple linear transformation (scale and offset). The
michael@0 1124 * time is measured to change a variable on either side of the
michael@0 1125 * mapping and to change the scale and offset factors.
michael@0 1126 */
michael@0 1127 function projectionTest(n) {
michael@0 1128 planner = new Planner();
michael@0 1129 var scale = new Variable("scale", 10);
michael@0 1130 var offset = new Variable("offset", 1000);
michael@0 1131 var src = null, dst = null;
michael@0 1132
michael@0 1133 var dests = new OrderedCollection();
michael@0 1134 /* BEGIN LOOP */
michael@0 1135 for (var i = 0; i < n; i++) {
michael@0 1136 src = new Variable("src" + i, i);
michael@0 1137 dst = new Variable("dst" + i, i);
michael@0 1138 dests.add(dst);
michael@0 1139 new StayConstraint(src, Strength.NORMAL);
michael@0 1140 new ScaleConstraint(src, scale, offset, dst, Strength.REQUIRED);
michael@0 1141 }
michael@0 1142 /* END LOOP */
michael@0 1143
michael@0 1144 change(src, 17);
michael@0 1145 if (dst.value != 1170) alert("Projection 1 failed");
michael@0 1146 change(dst, 1050);
michael@0 1147 if (src.value != 5) alert("Projection 2 failed");
michael@0 1148 change(scale, 5);
michael@0 1149 /* BEGIN LOOP */
michael@0 1150 for (var i = 0; i < n - 1; i++) {
michael@0 1151 if (dests.at(i).value != i * 5 + 1000)
michael@0 1152 alert("Projection 3 failed");
michael@0 1153 }
michael@0 1154 /* END LOOP */
michael@0 1155 change(offset, 2000);
michael@0 1156 /* BEGIN LOOP */
michael@0 1157 for (var i = 0; i < n - 1; i++) {
michael@0 1158 if (dests.at(i).value != i * 5 + 2000)
michael@0 1159 alert("Projection 4 failed");
michael@0 1160 }
michael@0 1161 /* END LOOP */
michael@0 1162 }
michael@0 1163
michael@0 1164 function change(v, newValue) {
michael@0 1165 var edit = new EditConstraint(v, Strength.PREFERRED);
michael@0 1166 var edits = new OrderedCollection();
michael@0 1167 edits.add(edit);
michael@0 1168 var plan = planner.extractPlanFromConstraints(edits);
michael@0 1169 /* BEGIN LOOP */
michael@0 1170 for (var i = 0; i < 10; i++) {
michael@0 1171 v.value = newValue;
michael@0 1172 plan.execute();
michael@0 1173 }
michael@0 1174 /* END LOOP */
michael@0 1175 edit.destroyConstraint();
michael@0 1176 }
michael@0 1177
michael@0 1178 // Global variable holding the current planner.
michael@0 1179 var planner = null;
michael@0 1180
michael@0 1181 function deltaBlue() {
michael@0 1182 chainTest(100);
michael@0 1183 projectionTest(100);
michael@0 1184 }
michael@0 1185
michael@0 1186 function PrintResult(name, result) {
michael@0 1187 }
michael@0 1188
michael@0 1189
michael@0 1190 function PrintScore(score) {
michael@0 1191 }
michael@0 1192
michael@0 1193
michael@0 1194 BenchmarkSuite.RunSuites({ NotifyResult: PrintResult,
michael@0 1195 NotifyScore: PrintScore });
michael@0 1196

mercurial