class
Cassowary::Solver
- Cassowary::Solver
- Reference
- Object
Overview
An incremental solver for a system of linear constraints.
Add and remove constraints at any time, steer edit variables with #suggest, and call
#update to write the solution into the variables.
x = Variable.new("x")
solver = Solver.new
solver.add(x >= 10)
solver.add((x == 0).strength(:weak))
solver.update
x.value # => 10.0
Defined in:
cassowary/solver.crInstance Method Summary
-
#[](variable : Variable) : Float64
Returns the value the solver assigns variable, without writing it into the variable; zero for a variable no constraint uses.
-
#add(constraint : Constraint) : Constraint
Adds constraint to the solver and returns it.
-
#add(constraints : Enumerable(Constraint)) : Nil
Adds every constraint of constraints, in order.
-
#add(first : Constraint, *rest : Constraint) : Nil
Adds every constraint of constraints, in order.
-
#add_edit(variable : Variable, strength : Strength | Strength::Preset = Strength::STRONG) : Nil
Makes variable an edit variable with strength, so
#suggestcan steer it. -
#constraints : Array(Constraint)
Returns the constraints added with
#add, in the order they were added. -
#dump(io : IO) : Nil
Writes the solver's internal state to io, for debugging.
-
#dump : String
Writes the solver's internal state to io, for debugging.
-
#edit(variable : Variable, strength : Strength | Strength::Preset = Strength::STRONG, & : Edit -> ) : Nil
Makes variable an edit variable with strength for the duration of the block, and yields an
Editthat steers it. -
#explain(constraint : Constraint) : Array(Constraint)
Returns a minimal set of required constraints in the solver that constraint conflicts with: together they reject it, and without any one of them they accept it.
-
#has?(constraint : Constraint) : Bool
Returns whether constraint is in the solver.
-
#has_edit?(variable : Variable) : Bool
Returns whether variable is an edit variable.
-
#remove(constraint : Constraint) : Nil
Removes constraint from the solver.
-
#remove(constraints : Enumerable(Constraint)) : Nil
Removes every constraint of constraints.
-
#remove(first : Constraint, *rest : Constraint) : Nil
Removes every constraint of constraints.
-
#remove_edit(variable : Variable) : Nil
Stops variable from being an edit variable.
-
#reset : Nil
Removes every constraint and edit variable.
-
#suggest(variable : Variable, value : Number) : Nil
Suggests value for the edit variable variable.
-
#suggest(values : Enumerable(Tuple(Variable, Number))) : Nil
Suggests a value for each edit variable in values, a collection of
{variable, value}pairs, at once. -
#update : Nil
Writes the current solution into the value of every variable in the solver.
-
#update(& : Variable -> ) : Nil
Writes the current solution into the value of every variable in the solver, and yields each variable whose value changed.
Instance Method Detail
Returns the value the solver assigns variable, without writing it into the variable; zero for a variable no constraint uses.
Unlike #update, this lets several solvers share a variable.
Adds constraint to the solver and returns it.
Raises DuplicateConstraint if the constraint is already in the solver and
UnsatisfiableConstraint if it is required and conflicts with the required constraints.
A rejected constraint leaves the solver unchanged.
Adds every constraint of constraints, in order.
Raises like #add; the constraints added before the failing one are removed again,
so a failure leaves the solver's constraints unchanged.
Adds every constraint of constraints, in order.
Raises like #add; the constraints added before the failing one are removed again,
so a failure leaves the solver's constraints unchanged.
Makes variable an edit variable with strength, so #suggest can steer it.
Raises DuplicateEdit if variable is already an edit variable and
BadRequiredStrength if strength is required.
Returns the constraints added with #add, in the order they were added.
Writes the solver's internal state to io, for debugging.
The format is not stable.
Writes the solver's internal state to io, for debugging.
The format is not stable.
Makes variable an edit variable with strength for the duration of the block, and
yields an Edit that steers it.
Raises like #add_edit. The edit variable is removed when the block returns or
raises.
solver.edit(width) do |edit|
edit.suggest(250)
solver.update
end
Returns a minimal set of required constraints in the solver that constraint conflicts with: together they reject it, and without any one of them they accept it. Other required constraints may conflict with it as well.
The set is empty when constraint can be added or contradicts itself on its own. Leaves the solver unchanged. It costs one trial solve per constraint involved in the conflict, so call it to diagnose a rejection: a few hundred constraints take well under a second, a conflict through a thousand takes seconds.
Returns whether variable is an edit variable.
Removes constraint from the solver.
Raises UnknownConstraint if the constraint is not in the solver.
Removes every constraint of constraints.
Raises UnknownConstraint before removing anything if one of them is not in the
solver.
Removes every constraint of constraints.
Raises UnknownConstraint before removing anything if one of them is not in the
solver.
Stops variable from being an edit variable.
Raises UnknownEdit if variable is not an edit variable.
Suggests value for the edit variable variable.
Raises UnknownEdit if variable is not an edit variable.
Suggests a value for each edit variable in values, a collection of
{variable, value} pairs, at once.
Raises UnknownEdit, before changing anything, if a variable is not an edit
variable.
solver.suggest([{width, 250}, {height, 100}])
Writes the current solution into the value of every variable in the solver, and yields each variable whose value changed.