new Babelsberg()
The interface to create, maintain and remove constraints.
Methods
-
(static) compile(code) → {string}
-
Transforms the given BabelsbergScript source code as regular JavaScript.
Parameters:
Name Type Description code
string The source code that should be transformed.
- Source:
Returns:
A transformed version of the given source code.
- Type
- string
-
(static) execute(code, scope)
-
Transforms and executes the given BabelsbergScript source code.
Parameters:
Name Type Description code
string The source code that should be executed.
scope
Object The scope in which the given code is executed.
- Source:
-
always(opts, func)
-
Creates a constraint equivalent to the given function.
Parameters:
Name Type Description opts
Object An options object to configure the constraint construction.
Properties
Name Type Attributes Default Description ctx
Object The local scope in which the given function is executed.
solver
Object <optional>
The solver to maintain the constraint.
allowTests
boolean <optional>
false If true, allows to specify assertions rather than solvable constraints.
allowUnsolvableOperations
boolean <optional>
false If true, allows the use of operations that are not supported by the solver.
debugging
boolean <optional>
false If true, calls debugger at certain points during constraint construction.
func
function The constraint to be fulfilled.
-
edit(obj, accessors) → {function}
-
Some solvers, like Cassowary and DeltaBlue, handle assignments by using temporary constraint that reflects the assignments. The creation and deletion of these constraints can be costly if assignments are done frequently. The edit function is one way to deal with this issue. Use it on attributes that are frequently modified for better performance.
Parameters:
Name Type Description obj
Object An object that is modified quite often.
accessors
Array.<string> The property names of the properties that are modified.
Returns:
A callback that can be used to assign new values to the given properties.
- Type
- function
Example
Example usage of bbb.edit var s = new DBPlanner(), obj = {int: 42, str: "42"}; // Keep the attributes 'str' and 'int' in sync. bbb.always({ solver: deltablue, ctx: { obj: obj }, methods: function() { obj.int.formula([obj.str], function (str) { return parseInt(str); }); obj.str.formula([obj.int], function (int) { return int + ""; }) } }, function () { return obj.int + "" === obj.str; }); // Create an edit constraint for frequent assignments on obj.int. var callback = bbb.edit(obj, ["int"]); // Assign 17 as the new value of obj.int. Constraints are solved automatically. callback([17]);
-
readonly(obj)
-
Marks the given object as readonly. This functionality is only supported for some solvers.
Parameters:
Name Type Description obj
Object The object that should not be modified.
Example
Example usage of bbb.readonly var s = new ClSimplexSolver(), pt = {x: 1, y: 2, z: 3}; // The x and y coordinate of the point should sum up to its z coordinate. // Cassowary is not allowed to change the value of pt.z in order to fulfil this constraint. bbb.always({ solver: s, ctx: { pt: pt, ro: bbb.readonly, _$_self: this.doitContext || this } }, function() { return pt.x + ro(pt.y) == pt.z;; }); // This assignment cannot modify pt.y, but rather changes pt.z pt.x = 4;
-
unconstrain(obj, accessor)
-
Removes the listener on the given property of the given object.
Parameters:
Name Type Description obj
Object The object whose property should be unconstrained.
accessor
string The name of the property to be unconstrained.
-
unconstrainAll(obj)
-
Removes all listener on the given object.
Parameters:
Name Type Description obj
Object The object whose property should be unconstrained.