michael@0: // Copyright 2008 the V8 project authors. All rights reserved. michael@0: // Copyright 1996 John Maloney and Mario Wolczko. michael@0: michael@0: // This program is free software; you can redistribute it and/or modify michael@0: // it under the terms of the GNU General Public License as published by michael@0: // the Free Software Foundation; either version 2 of the License, or michael@0: // (at your option) any later version. michael@0: // michael@0: // This program is distributed in the hope that it will be useful, michael@0: // but WITHOUT ANY WARRANTY; without even the implied warranty of michael@0: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the michael@0: // GNU General Public License for more details. michael@0: // michael@0: // You should have received a copy of the GNU General Public License michael@0: // along with this program; if not, write to the Free Software michael@0: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA michael@0: michael@0: michael@0: // This implementation of the DeltaBlue benchmark is derived michael@0: // from the Smalltalk implementation by John Maloney and Mario michael@0: // Wolczko. Some parts have been translated directly, whereas michael@0: // others have been modified more aggresively to make it feel michael@0: // more like a JavaScript program. michael@0: michael@0: michael@0: var DeltaBlue = new BenchmarkSuite('DeltaBlue', 66118, [ michael@0: new Benchmark('DeltaBlue', deltaBlue) michael@0: ]); michael@0: michael@0: michael@0: /** michael@0: * A JavaScript implementation of the DeltaBlue constraint-solving michael@0: * algorithm, as described in: michael@0: * michael@0: * "The DeltaBlue Algorithm: An Incremental Constraint Hierarchy Solver" michael@0: * Bjorn N. Freeman-Benson and John Maloney michael@0: * January 1990 Communications of the ACM, michael@0: * also available as University of Washington TR 89-08-06. michael@0: * michael@0: * Beware: this benchmark is written in a grotesque style where michael@0: * the constraint model is built by side-effects from constructors. michael@0: * I've kept it this way to avoid deviating too much from the original michael@0: * implementation. michael@0: */ michael@0: michael@0: michael@0: /* --- O b j e c t M o d e l --- */ michael@0: michael@0: Object.prototype.inheritsFrom = function (shuper) { michael@0: function Inheriter() { } michael@0: Inheriter.prototype = shuper.prototype; michael@0: this.prototype = new Inheriter(); michael@0: this.superConstructor = shuper; michael@0: } michael@0: michael@0: function OrderedCollection() { michael@0: this.elms = new Array(); michael@0: } michael@0: michael@0: OrderedCollection.prototype.add = function (elm) { michael@0: this.elms.push(elm); michael@0: } michael@0: michael@0: OrderedCollection.prototype.at = function (index) { michael@0: return this.elms[index]; michael@0: } michael@0: michael@0: OrderedCollection.prototype.size = function () { michael@0: return this.elms.length; michael@0: } michael@0: michael@0: OrderedCollection.prototype.removeFirst = function () { michael@0: return this.elms.pop(); michael@0: } michael@0: michael@0: OrderedCollection.prototype.remove = function (elm) { michael@0: var index = 0, skipped = 0; michael@0: for (var i = 0; i < this.elms.length; i++) { michael@0: var value = this.elms[i]; michael@0: if (value != elm) { michael@0: this.elms[index] = value; michael@0: index++; michael@0: } else { michael@0: skipped++; michael@0: } michael@0: } michael@0: for (var i = 0; i < skipped; i++) michael@0: this.elms.pop(); michael@0: } michael@0: michael@0: /* --- * michael@0: * S t r e n g t h michael@0: * --- */ michael@0: michael@0: /** michael@0: * Strengths are used to measure the relative importance of constraints. michael@0: * New strengths may be inserted in the strength hierarchy without michael@0: * disrupting current constraints. Strengths cannot be created outside michael@0: * this class, so pointer comparison can be used for value comparison. michael@0: */ michael@0: function Strength(strengthValue, name) { michael@0: this.strengthValue = strengthValue; michael@0: this.name = name; michael@0: } michael@0: michael@0: Strength.stronger = function (s1, s2) { michael@0: return s1.strengthValue < s2.strengthValue; michael@0: } michael@0: michael@0: Strength.weaker = function (s1, s2) { michael@0: return s1.strengthValue > s2.strengthValue; michael@0: } michael@0: michael@0: Strength.weakestOf = function (s1, s2) { michael@0: return this.weaker(s1, s2) ? s1 : s2; michael@0: } michael@0: michael@0: Strength.strongest = function (s1, s2) { michael@0: return this.stronger(s1, s2) ? s1 : s2; michael@0: } michael@0: michael@0: Strength.prototype.nextWeaker = function () { michael@0: switch (this.strengthValue) { michael@0: case 0: return Strength.WEAKEST; michael@0: case 1: return Strength.WEAK_DEFAULT; michael@0: case 2: return Strength.NORMAL; michael@0: case 3: return Strength.STRONG_DEFAULT; michael@0: case 4: return Strength.PREFERRED; michael@0: case 5: return Strength.REQUIRED; michael@0: } michael@0: } michael@0: michael@0: // Strength constants. michael@0: Strength.REQUIRED = new Strength(0, "required"); michael@0: Strength.STONG_PREFERRED = new Strength(1, "strongPreferred"); michael@0: Strength.PREFERRED = new Strength(2, "preferred"); michael@0: Strength.STRONG_DEFAULT = new Strength(3, "strongDefault"); michael@0: Strength.NORMAL = new Strength(4, "normal"); michael@0: Strength.WEAK_DEFAULT = new Strength(5, "weakDefault"); michael@0: Strength.WEAKEST = new Strength(6, "weakest"); michael@0: michael@0: /* --- * michael@0: * C o n s t r a i n t michael@0: * --- */ michael@0: michael@0: /** michael@0: * An abstract class representing a system-maintainable relationship michael@0: * (or "constraint") between a set of variables. A constraint supplies michael@0: * a strength instance variable; concrete subclasses provide a means michael@0: * of storing the constrained variables and other information required michael@0: * to represent a constraint. michael@0: */ michael@0: function Constraint(strength) { michael@0: this.strength = strength; michael@0: } michael@0: michael@0: /** michael@0: * Activate this constraint and attempt to satisfy it. michael@0: */ michael@0: Constraint.prototype.addConstraint = function () { michael@0: this.addToGraph(); michael@0: planner.incrementalAdd(this); michael@0: } michael@0: michael@0: /** michael@0: * Attempt to find a way to enforce this constraint. If successful, michael@0: * record the solution, perhaps modifying the current dataflow michael@0: * graph. Answer the constraint that this constraint overrides, if michael@0: * there is one, or nil, if there isn't. michael@0: * Assume: I am not already satisfied. michael@0: */ michael@0: Constraint.prototype.satisfy = function (mark) { michael@0: this.chooseMethod(mark); michael@0: if (!this.isSatisfied()) { michael@0: if (this.strength == Strength.REQUIRED) michael@0: alert("Could not satisfy a required constraint!"); michael@0: return null; michael@0: } michael@0: this.markInputs(mark); michael@0: var out = this.output(); michael@0: var overridden = out.determinedBy; michael@0: if (overridden != null) overridden.markUnsatisfied(); michael@0: out.determinedBy = this; michael@0: if (!planner.addPropagate(this, mark)) michael@0: alert("Cycle encountered"); michael@0: out.mark = mark; michael@0: return overridden; michael@0: } michael@0: michael@0: Constraint.prototype.destroyConstraint = function () { michael@0: if (this.isSatisfied()) planner.incrementalRemove(this); michael@0: else this.removeFromGraph(); michael@0: } michael@0: michael@0: /** michael@0: * Normal constraints are not input constraints. An input constraint michael@0: * is one that depends on external state, such as the mouse, the michael@0: * keybord, a clock, or some arbitraty piece of imperative code. michael@0: */ michael@0: Constraint.prototype.isInput = function () { michael@0: return false; michael@0: } michael@0: michael@0: /* --- * michael@0: * U n a r y C o n s t r a i n t michael@0: * --- */ michael@0: michael@0: /** michael@0: * Abstract superclass for constraints having a single possible output michael@0: * variable. michael@0: */ michael@0: function UnaryConstraint(v, strength) { michael@0: UnaryConstraint.superConstructor.call(this, strength); michael@0: this.myOutput = v; michael@0: this.satisfied = false; michael@0: this.addConstraint(); michael@0: } michael@0: michael@0: UnaryConstraint.inheritsFrom(Constraint); michael@0: michael@0: /** michael@0: * Adds this constraint to the constraint graph michael@0: */ michael@0: UnaryConstraint.prototype.addToGraph = function () { michael@0: this.myOutput.addConstraint(this); michael@0: this.satisfied = false; michael@0: } michael@0: michael@0: /** michael@0: * Decides if this constraint can be satisfied and records that michael@0: * decision. michael@0: */ michael@0: UnaryConstraint.prototype.chooseMethod = function (mark) { michael@0: this.satisfied = (this.myOutput.mark != mark) michael@0: && Strength.stronger(this.strength, this.myOutput.walkStrength); michael@0: } michael@0: michael@0: /** michael@0: * Returns true if this constraint is satisfied in the current solution. michael@0: */ michael@0: UnaryConstraint.prototype.isSatisfied = function () { michael@0: return this.satisfied; michael@0: } michael@0: michael@0: UnaryConstraint.prototype.markInputs = function (mark) { michael@0: // has no inputs michael@0: } michael@0: michael@0: /** michael@0: * Returns the current output variable. michael@0: */ michael@0: UnaryConstraint.prototype.output = function () { michael@0: return this.myOutput; michael@0: } michael@0: michael@0: /** michael@0: * Calculate the walkabout strength, the stay flag, and, if it is michael@0: * 'stay', the value for the current output of this constraint. Assume michael@0: * this constraint is satisfied. michael@0: */ michael@0: UnaryConstraint.prototype.recalculate = function () { michael@0: this.myOutput.walkStrength = this.strength; michael@0: this.myOutput.stay = !this.isInput(); michael@0: if (this.myOutput.stay) this.execute(); // Stay optimization michael@0: } michael@0: michael@0: /** michael@0: * Records that this constraint is unsatisfied michael@0: */ michael@0: UnaryConstraint.prototype.markUnsatisfied = function () { michael@0: this.satisfied = false; michael@0: } michael@0: michael@0: UnaryConstraint.prototype.inputsKnown = function () { michael@0: return true; michael@0: } michael@0: michael@0: UnaryConstraint.prototype.removeFromGraph = function () { michael@0: if (this.myOutput != null) this.myOutput.removeConstraint(this); michael@0: this.satisfied = false; michael@0: } michael@0: michael@0: /* --- * michael@0: * S t a y C o n s t r a i n t michael@0: * --- */ michael@0: michael@0: /** michael@0: * Variables that should, with some level of preference, stay the same. michael@0: * Planners may exploit the fact that instances, if satisfied, will not michael@0: * change their output during plan execution. This is called "stay michael@0: * optimization". michael@0: */ michael@0: function StayConstraint(v, str) { michael@0: StayConstraint.superConstructor.call(this, v, str); michael@0: } michael@0: michael@0: StayConstraint.inheritsFrom(UnaryConstraint); michael@0: michael@0: StayConstraint.prototype.execute = function () { michael@0: // Stay constraints do nothing michael@0: } michael@0: michael@0: /* --- * michael@0: * E d i t C o n s t r a i n t michael@0: * --- */ michael@0: michael@0: /** michael@0: * A unary input constraint used to mark a variable that the client michael@0: * wishes to change. michael@0: */ michael@0: function EditConstraint(v, str) { michael@0: EditConstraint.superConstructor.call(this, v, str); michael@0: } michael@0: michael@0: EditConstraint.inheritsFrom(UnaryConstraint); michael@0: michael@0: /** michael@0: * Edits indicate that a variable is to be changed by imperative code. michael@0: */ michael@0: EditConstraint.prototype.isInput = function () { michael@0: return true; michael@0: } michael@0: michael@0: EditConstraint.prototype.execute = function () { michael@0: // Edit constraints do nothing michael@0: } michael@0: michael@0: /* --- * michael@0: * B i n a r y C o n s t r a i n t michael@0: * --- */ michael@0: michael@0: var Direction = new Object(); michael@0: Direction.NONE = 0; michael@0: Direction.FORWARD = 1; michael@0: Direction.BACKWARD = -1; michael@0: michael@0: /** michael@0: * Abstract superclass for constraints having two possible output michael@0: * variables. michael@0: */ michael@0: function BinaryConstraint(var1, var2, strength) { michael@0: BinaryConstraint.superConstructor.call(this, strength); michael@0: this.v1 = var1; michael@0: this.v2 = var2; michael@0: this.direction = Direction.NONE; michael@0: this.addConstraint(); michael@0: } michael@0: michael@0: BinaryConstraint.inheritsFrom(Constraint); michael@0: michael@0: /** michael@0: * Decides if this constraint can be satisfied and which way it michael@0: * should flow based on the relative strength of the variables related, michael@0: * and record that decision. michael@0: */ michael@0: BinaryConstraint.prototype.chooseMethod = function (mark) { michael@0: if (this.v1.mark == mark) { michael@0: this.direction = (this.v2.mark != mark && Strength.stronger(this.strength, this.v2.walkStrength)) michael@0: ? Direction.FORWARD michael@0: : Direction.NONE; michael@0: } michael@0: if (this.v2.mark == mark) { michael@0: this.direction = (this.v1.mark != mark && Strength.stronger(this.strength, this.v1.walkStrength)) michael@0: ? Direction.BACKWARD michael@0: : Direction.NONE; michael@0: } michael@0: if (Strength.weaker(this.v1.walkStrength, this.v2.walkStrength)) { michael@0: this.direction = Strength.stronger(this.strength, this.v1.walkStrength) michael@0: ? Direction.BACKWARD michael@0: : Direction.NONE; michael@0: } else { michael@0: this.direction = Strength.stronger(this.strength, this.v2.walkStrength) michael@0: ? Direction.FORWARD michael@0: : Direction.BACKWARD michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Add this constraint to the constraint graph michael@0: */ michael@0: BinaryConstraint.prototype.addToGraph = function () { michael@0: this.v1.addConstraint(this); michael@0: this.v2.addConstraint(this); michael@0: this.direction = Direction.NONE; michael@0: } michael@0: michael@0: /** michael@0: * Answer true if this constraint is satisfied in the current solution. michael@0: */ michael@0: BinaryConstraint.prototype.isSatisfied = function () { michael@0: return this.direction != Direction.NONE; michael@0: } michael@0: michael@0: /** michael@0: * Mark the input variable with the given mark. michael@0: */ michael@0: BinaryConstraint.prototype.markInputs = function (mark) { michael@0: this.input().mark = mark; michael@0: } michael@0: michael@0: /** michael@0: * Returns the current input variable michael@0: */ michael@0: BinaryConstraint.prototype.input = function () { michael@0: return (this.direction == Direction.FORWARD) ? this.v1 : this.v2; michael@0: } michael@0: michael@0: /** michael@0: * Returns the current output variable michael@0: */ michael@0: BinaryConstraint.prototype.output = function () { michael@0: return (this.direction == Direction.FORWARD) ? this.v2 : this.v1; michael@0: } michael@0: michael@0: /** michael@0: * Calculate the walkabout strength, the stay flag, and, if it is michael@0: * 'stay', the value for the current output of this michael@0: * constraint. Assume this constraint is satisfied. michael@0: */ michael@0: BinaryConstraint.prototype.recalculate = function () { michael@0: var ihn = this.input(), out = this.output(); michael@0: out.walkStrength = Strength.weakestOf(this.strength, ihn.walkStrength); michael@0: out.stay = ihn.stay; michael@0: if (out.stay) this.execute(); michael@0: } michael@0: michael@0: /** michael@0: * Record the fact that this constraint is unsatisfied. michael@0: */ michael@0: BinaryConstraint.prototype.markUnsatisfied = function () { michael@0: this.direction = Direction.NONE; michael@0: } michael@0: michael@0: BinaryConstraint.prototype.inputsKnown = function (mark) { michael@0: var i = this.input(); michael@0: return i.mark == mark || i.stay || i.determinedBy == null; michael@0: } michael@0: michael@0: BinaryConstraint.prototype.removeFromGraph = function () { michael@0: if (this.v1 != null) this.v1.removeConstraint(this); michael@0: if (this.v2 != null) this.v2.removeConstraint(this); michael@0: this.direction = Direction.NONE; michael@0: } michael@0: michael@0: /* --- * michael@0: * S c a l e C o n s t r a i n t michael@0: * --- */ michael@0: michael@0: /** michael@0: * Relates two variables by the linear scaling relationship: "v2 = michael@0: * (v1 * scale) + offset". Either v1 or v2 may be changed to maintain michael@0: * this relationship but the scale factor and offset are considered michael@0: * read-only. michael@0: */ michael@0: function ScaleConstraint(src, scale, offset, dest, strength) { michael@0: this.direction = Direction.NONE; michael@0: this.scale = scale; michael@0: this.offset = offset; michael@0: ScaleConstraint.superConstructor.call(this, src, dest, strength); michael@0: } michael@0: michael@0: ScaleConstraint.inheritsFrom(BinaryConstraint); michael@0: michael@0: /** michael@0: * Adds this constraint to the constraint graph. michael@0: */ michael@0: ScaleConstraint.prototype.addToGraph = function () { michael@0: ScaleConstraint.superConstructor.prototype.addToGraph.call(this); michael@0: this.scale.addConstraint(this); michael@0: this.offset.addConstraint(this); michael@0: } michael@0: michael@0: ScaleConstraint.prototype.removeFromGraph = function () { michael@0: ScaleConstraint.superConstructor.prototype.removeFromGraph.call(this); michael@0: if (this.scale != null) this.scale.removeConstraint(this); michael@0: if (this.offset != null) this.offset.removeConstraint(this); michael@0: } michael@0: michael@0: ScaleConstraint.prototype.markInputs = function (mark) { michael@0: ScaleConstraint.superConstructor.prototype.markInputs.call(this, mark); michael@0: this.scale.mark = this.offset.mark = mark; michael@0: } michael@0: michael@0: /** michael@0: * Enforce this constraint. Assume that it is satisfied. michael@0: */ michael@0: ScaleConstraint.prototype.execute = function () { michael@0: if (this.direction == Direction.FORWARD) { michael@0: this.v2.value = this.v1.value * this.scale.value + this.offset.value; michael@0: } else { michael@0: this.v1.value = (this.v2.value - this.offset.value) / this.scale.value; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Calculate the walkabout strength, the stay flag, and, if it is michael@0: * 'stay', the value for the current output of this constraint. Assume michael@0: * this constraint is satisfied. michael@0: */ michael@0: ScaleConstraint.prototype.recalculate = function () { michael@0: var ihn = this.input(), out = this.output(); michael@0: out.walkStrength = Strength.weakestOf(this.strength, ihn.walkStrength); michael@0: out.stay = ihn.stay && this.scale.stay && this.offset.stay; michael@0: if (out.stay) this.execute(); michael@0: } michael@0: michael@0: /* --- * michael@0: * E q u a l i t y C o n s t r a i n t michael@0: * --- */ michael@0: michael@0: /** michael@0: * Constrains two variables to have the same value. michael@0: */ michael@0: function EqualityConstraint(var1, var2, strength) { michael@0: EqualityConstraint.superConstructor.call(this, var1, var2, strength); michael@0: } michael@0: michael@0: EqualityConstraint.inheritsFrom(BinaryConstraint); michael@0: michael@0: /** michael@0: * Enforce this constraint. Assume that it is satisfied. michael@0: */ michael@0: EqualityConstraint.prototype.execute = function () { michael@0: this.output().value = this.input().value; michael@0: } michael@0: michael@0: /* --- * michael@0: * V a r i a b l e michael@0: * --- */ michael@0: michael@0: /** michael@0: * A constrained variable. In addition to its value, it maintain the michael@0: * structure of the constraint graph, the current dataflow graph, and michael@0: * various parameters of interest to the DeltaBlue incremental michael@0: * constraint solver. michael@0: **/ michael@0: function Variable(name, initialValue) { michael@0: this.value = initialValue || 0; michael@0: this.constraints = new OrderedCollection(); michael@0: this.determinedBy = null; michael@0: this.mark = 0; michael@0: this.walkStrength = Strength.WEAKEST; michael@0: this.stay = true; michael@0: this.name = name; michael@0: } michael@0: michael@0: /** michael@0: * Add the given constraint to the set of all constraints that refer michael@0: * this variable. michael@0: */ michael@0: Variable.prototype.addConstraint = function (c) { michael@0: this.constraints.add(c); michael@0: } michael@0: michael@0: /** michael@0: * Removes all traces of c from this variable. michael@0: */ michael@0: Variable.prototype.removeConstraint = function (c) { michael@0: this.constraints.remove(c); michael@0: if (this.determinedBy == c) this.determinedBy = null; michael@0: } michael@0: michael@0: /* --- * michael@0: * P l a n n e r michael@0: * --- */ michael@0: michael@0: /** michael@0: * The DeltaBlue planner michael@0: */ michael@0: function Planner() { michael@0: this.currentMark = 0; michael@0: } michael@0: michael@0: /** michael@0: * Attempt to satisfy the given constraint and, if successful, michael@0: * incrementally update the dataflow graph. Details: If satifying michael@0: * the constraint is successful, it may override a weaker constraint michael@0: * on its output. The algorithm attempts to resatisfy that michael@0: * constraint using some other method. This process is repeated michael@0: * until either a) it reaches a variable that was not previously michael@0: * determined by any constraint or b) it reaches a constraint that michael@0: * is too weak to be satisfied using any of its methods. The michael@0: * variables of constraints that have been processed are marked with michael@0: * a unique mark value so that we know where we've been. This allows michael@0: * the algorithm to avoid getting into an infinite loop even if the michael@0: * constraint graph has an inadvertent cycle. michael@0: */ michael@0: Planner.prototype.incrementalAdd = function (c) { michael@0: var mark = this.newMark(); michael@0: var overridden = c.satisfy(mark); michael@0: while (overridden != null) michael@0: overridden = overridden.satisfy(mark); michael@0: } michael@0: michael@0: /** michael@0: * Entry point for retracting a constraint. Remove the given michael@0: * constraint and incrementally update the dataflow graph. michael@0: * Details: Retracting the given constraint may allow some currently michael@0: * unsatisfiable downstream constraint to be satisfied. We therefore collect michael@0: * a list of unsatisfied downstream constraints and attempt to michael@0: * satisfy each one in turn. This list is traversed by constraint michael@0: * strength, strongest first, as a heuristic for avoiding michael@0: * unnecessarily adding and then overriding weak constraints. michael@0: * Assume: c is satisfied. michael@0: */ michael@0: Planner.prototype.incrementalRemove = function (c) { michael@0: var out = c.output(); michael@0: c.markUnsatisfied(); michael@0: c.removeFromGraph(); michael@0: var unsatisfied = this.removePropagateFrom(out); michael@0: var strength = Strength.REQUIRED; michael@0: do { michael@0: for (var i = 0; i < unsatisfied.size(); i++) { michael@0: var u = unsatisfied.at(i); michael@0: if (u.strength == strength) michael@0: this.incrementalAdd(u); michael@0: } michael@0: strength = strength.nextWeaker(); michael@0: } while (strength != Strength.WEAKEST); michael@0: } michael@0: michael@0: /** michael@0: * Select a previously unused mark value. michael@0: */ michael@0: Planner.prototype.newMark = function () { michael@0: return ++this.currentMark; michael@0: } michael@0: michael@0: /** michael@0: * Extract a plan for resatisfaction starting from the given source michael@0: * constraints, usually a set of input constraints. This method michael@0: * assumes that stay optimization is desired; the plan will contain michael@0: * only constraints whose output variables are not stay. Constraints michael@0: * that do no computation, such as stay and edit constraints, are michael@0: * not included in the plan. michael@0: * Details: The outputs of a constraint are marked when it is added michael@0: * to the plan under construction. A constraint may be appended to michael@0: * the plan when all its input variables are known. A variable is michael@0: * known if either a) the variable is marked (indicating that has michael@0: * been computed by a constraint appearing earlier in the plan), b) michael@0: * the variable is 'stay' (i.e. it is a constant at plan execution michael@0: * time), or c) the variable is not determined by any michael@0: * constraint. The last provision is for past states of history michael@0: * variables, which are not stay but which are also not computed by michael@0: * any constraint. michael@0: * Assume: sources are all satisfied. michael@0: */ michael@0: Planner.prototype.makePlan = function (sources) { michael@0: var mark = this.newMark(); michael@0: var plan = new Plan(); michael@0: var todo = sources; michael@0: while (todo.size() > 0) { michael@0: var c = todo.removeFirst(); michael@0: if (c.output().mark != mark && c.inputsKnown(mark)) { michael@0: plan.addConstraint(c); michael@0: c.output().mark = mark; michael@0: this.addConstraintsConsumingTo(c.output(), todo); michael@0: } michael@0: } michael@0: return plan; michael@0: } michael@0: michael@0: /** michael@0: * Extract a plan for resatisfying starting from the output of the michael@0: * given constraints, usually a set of input constraints. michael@0: */ michael@0: Planner.prototype.extractPlanFromConstraints = function (constraints) { michael@0: var sources = new OrderedCollection(); michael@0: for (var i = 0; i < constraints.size(); i++) { michael@0: var c = constraints.at(i); michael@0: if (c.isInput() && c.isSatisfied()) michael@0: // not in plan already and eligible for inclusion michael@0: sources.add(c); michael@0: } michael@0: return this.makePlan(sources); michael@0: } michael@0: michael@0: /** michael@0: * Recompute the walkabout strengths and stay flags of all variables michael@0: * downstream of the given constraint and recompute the actual michael@0: * values of all variables whose stay flag is true. If a cycle is michael@0: * detected, remove the given constraint and answer michael@0: * false. Otherwise, answer true. michael@0: * Details: Cycles are detected when a marked variable is michael@0: * encountered downstream of the given constraint. The sender is michael@0: * assumed to have marked the inputs of the given constraint with michael@0: * the given mark. Thus, encountering a marked node downstream of michael@0: * the output constraint means that there is a path from the michael@0: * constraint's output to one of its inputs. michael@0: */ michael@0: Planner.prototype.addPropagate = function (c, mark) { michael@0: var todo = new OrderedCollection(); michael@0: todo.add(c); michael@0: while (todo.size() > 0) { michael@0: var d = todo.removeFirst(); michael@0: if (d.output().mark == mark) { michael@0: this.incrementalRemove(c); michael@0: return false; michael@0: } michael@0: d.recalculate(); michael@0: this.addConstraintsConsumingTo(d.output(), todo); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Update the walkabout strengths and stay flags of all variables michael@0: * downstream of the given constraint. Answer a collection of michael@0: * unsatisfied constraints sorted in order of decreasing strength. michael@0: */ michael@0: Planner.prototype.removePropagateFrom = function (out) { michael@0: out.determinedBy = null; michael@0: out.walkStrength = Strength.WEAKEST; michael@0: out.stay = true; michael@0: var unsatisfied = new OrderedCollection(); michael@0: var todo = new OrderedCollection(); michael@0: todo.add(out); michael@0: while (todo.size() > 0) { michael@0: var v = todo.removeFirst(); michael@0: for (var i = 0; i < v.constraints.size(); i++) { michael@0: var c = v.constraints.at(i); michael@0: if (!c.isSatisfied()) michael@0: unsatisfied.add(c); michael@0: } michael@0: var determining = v.determinedBy; michael@0: for (var i = 0; i < v.constraints.size(); i++) { michael@0: var next = v.constraints.at(i); michael@0: if (next != determining && next.isSatisfied()) { michael@0: next.recalculate(); michael@0: todo.add(next.output()); michael@0: } michael@0: } michael@0: } michael@0: return unsatisfied; michael@0: } michael@0: michael@0: Planner.prototype.addConstraintsConsumingTo = function (v, coll) { michael@0: var determining = v.determinedBy; michael@0: var cc = v.constraints; michael@0: for (var i = 0; i < cc.size(); i++) { michael@0: var c = cc.at(i); michael@0: if (c != determining && c.isSatisfied()) michael@0: coll.add(c); michael@0: } michael@0: } michael@0: michael@0: /* --- * michael@0: * P l a n michael@0: * --- */ michael@0: michael@0: /** michael@0: * A Plan is an ordered list of constraints to be executed in sequence michael@0: * to resatisfy all currently satisfiable constraints in the face of michael@0: * one or more changing inputs. michael@0: */ michael@0: function Plan() { michael@0: this.v = new OrderedCollection(); michael@0: } michael@0: michael@0: Plan.prototype.addConstraint = function (c) { michael@0: this.v.add(c); michael@0: } michael@0: michael@0: Plan.prototype.size = function () { michael@0: return this.v.size(); michael@0: } michael@0: michael@0: Plan.prototype.constraintAt = function (index) { michael@0: return this.v.at(index); michael@0: } michael@0: michael@0: Plan.prototype.execute = function () { michael@0: for (var i = 0; i < this.size(); i++) { michael@0: var c = this.constraintAt(i); michael@0: c.execute(); michael@0: } michael@0: } michael@0: michael@0: /* --- * michael@0: * M a i n michael@0: * --- */ michael@0: michael@0: /** michael@0: * This is the standard DeltaBlue benchmark. A long chain of equality michael@0: * constraints is constructed with a stay constraint on one end. An michael@0: * edit constraint is then added to the opposite end and the time is michael@0: * measured for adding and removing this constraint, and extracting michael@0: * and executing a constraint satisfaction plan. There are two cases. michael@0: * In case 1, the added constraint is stronger than the stay michael@0: * constraint and values must propagate down the entire length of the michael@0: * chain. In case 2, the added constraint is weaker than the stay michael@0: * constraint so it cannot be accomodated. The cost in this case is, michael@0: * of course, very low. Typical situations lie somewhere between these michael@0: * two extremes. michael@0: */ michael@0: function chainTest(n) { michael@0: planner = new Planner(); michael@0: var prev = null, first = null, last = null; michael@0: michael@0: // Build chain of n equality constraints michael@0: for (var i = 0; i <= n; i++) { michael@0: var name = "v" + i; michael@0: var v = new Variable(name); michael@0: if (prev != null) michael@0: new EqualityConstraint(prev, v, Strength.REQUIRED); michael@0: if (i == 0) first = v; michael@0: if (i == n) last = v; michael@0: prev = v; michael@0: } michael@0: michael@0: new StayConstraint(last, Strength.STRONG_DEFAULT); michael@0: var edit = new EditConstraint(first, Strength.PREFERRED); michael@0: var edits = new OrderedCollection(); michael@0: edits.add(edit); michael@0: var plan = planner.extractPlanFromConstraints(edits); michael@0: for (var i = 0; i < 100; i++) { michael@0: first.value = i; michael@0: plan.execute(); michael@0: if (last.value != i) michael@0: alert("Chain test failed."); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * This test constructs a two sets of variables related to each michael@0: * other by a simple linear transformation (scale and offset). The michael@0: * time is measured to change a variable on either side of the michael@0: * mapping and to change the scale and offset factors. michael@0: */ michael@0: function projectionTest(n) { michael@0: planner = new Planner(); michael@0: var scale = new Variable("scale", 10); michael@0: var offset = new Variable("offset", 1000); michael@0: var src = null, dst = null; michael@0: michael@0: var dests = new OrderedCollection(); michael@0: for (var i = 0; i < n; i++) { michael@0: src = new Variable("src" + i, i); michael@0: dst = new Variable("dst" + i, i); michael@0: dests.add(dst); michael@0: new StayConstraint(src, Strength.NORMAL); michael@0: new ScaleConstraint(src, scale, offset, dst, Strength.REQUIRED); michael@0: } michael@0: michael@0: change(src, 17); michael@0: if (dst.value != 1170) alert("Projection 1 failed"); michael@0: change(dst, 1050); michael@0: if (src.value != 5) alert("Projection 2 failed"); michael@0: change(scale, 5); michael@0: for (var i = 0; i < n - 1; i++) { michael@0: if (dests.at(i).value != i * 5 + 1000) michael@0: alert("Projection 3 failed"); michael@0: } michael@0: change(offset, 2000); michael@0: for (var i = 0; i < n - 1; i++) { michael@0: if (dests.at(i).value != i * 5 + 2000) michael@0: alert("Projection 4 failed"); michael@0: } michael@0: } michael@0: michael@0: function change(v, newValue) { michael@0: var edit = new EditConstraint(v, Strength.PREFERRED); michael@0: var edits = new OrderedCollection(); michael@0: edits.add(edit); michael@0: var plan = planner.extractPlanFromConstraints(edits); michael@0: for (var i = 0; i < 10; i++) { michael@0: v.value = newValue; michael@0: plan.execute(); michael@0: } michael@0: edit.destroyConstraint(); michael@0: } michael@0: michael@0: // Global variable holding the current planner. michael@0: var planner = null; michael@0: michael@0: function deltaBlue() { michael@0: chainTest(100); michael@0: projectionTest(100); michael@0: }