Commit 704d0174 authored by Chinmay Garde's avatar Chinmay Garde

Solver.flush update returns the set of context associated with parameters in play

parent 4568b088
...@@ -6,12 +6,17 @@ part of cassowary; ...@@ -6,12 +6,17 @@ part of cassowary;
class Param extends _EquationMember { class Param extends _EquationMember {
final Variable variable; final Variable variable;
final dynamic context;
Param.withVariable(this.variable) { Param([double value = 0.0])
: variable = new Variable(value),
context = null {
variable._owner = this; variable._owner = this;
} }
Param([double value = 0.0]) : this.variable = new Variable(value) { Param.withContext(ctx)
: variable = new Variable(0.0),
context = ctx {
variable._owner = this; variable._owner = this;
} }
......
...@@ -168,8 +168,8 @@ class Solver { ...@@ -168,8 +168,8 @@ class Solver {
return _dualOptimize(); return _dualOptimize();
} }
List<Param> flushParameterUpdates() { Set flushUpdates() {
List<Param> updates = new List<Param>(); Set updates = new Set();
for (Variable variable in _vars.keys) { for (Variable variable in _vars.keys) {
_Symbol symbol = _vars[variable]; _Symbol symbol = _vars[variable];
...@@ -178,9 +178,14 @@ class Solver { ...@@ -178,9 +178,14 @@ class Solver {
double updatedValue = row == null ? 0.0 : row.constant; double updatedValue = row == null ? 0.0 : row.constant;
if (variable._applyUpdate(updatedValue) && variable._owner != null) { if (variable._applyUpdate(updatedValue) && variable._owner != null) {
updates.add(variable._owner); dynamic context = variable._owner.context;
if (context != null) {
updates.add(context);
}
} }
} }
return updates; return updates;
} }
......
...@@ -427,7 +427,7 @@ void main() { ...@@ -427,7 +427,7 @@ void main() {
var left = new Param(-20.0); var left = new Param(-20.0);
Solver s = new Solver(); Solver s = new Solver();
s.addConstraint(left >= CM(0.0)); s.addConstraint(left >= CM(0.0));
s.flushParameterUpdates(); s.flushUpdates();
expect(left.value, 0.0); expect(left.value, 0.0);
}); });
...@@ -443,7 +443,7 @@ void main() { ...@@ -443,7 +443,7 @@ void main() {
expect(s.addConstraint(right - left >= CM(100.0)), Result.success); expect(s.addConstraint(right - left >= CM(100.0)), Result.success);
expect(s.addConstraint(left >= CM(0.0)), Result.success); expect(s.addConstraint(left >= CM(0.0)), Result.success);
s.flushParameterUpdates(); s.flushUpdates();
expect(left.value, 0.0); expect(left.value, 0.0);
expect(mid.value, 50.0); expect(mid.value, 50.0);
...@@ -484,7 +484,7 @@ void main() { ...@@ -484,7 +484,7 @@ void main() {
expect(s.addEditVariable(mid.variable, Priority.strong), Result.success); expect(s.addEditVariable(mid.variable, Priority.strong), Result.success);
expect(s.suggestValueForVariable(mid.variable, 300.0), Result.success); expect(s.suggestValueForVariable(mid.variable, 300.0), Result.success);
s.flushParameterUpdates(); s.flushUpdates();
expect(left.value, 0.0); expect(left.value, 0.0);
expect(mid.value, 300.0); expect(mid.value, 300.0);
...@@ -523,7 +523,7 @@ void main() { ...@@ -523,7 +523,7 @@ void main() {
solver.addConstraint((p2 == CM(2.0) * p1) as Constraint); solver.addConstraint((p2 == CM(2.0) * p1) as Constraint);
solver.addConstraint((container == (p1 + p2 + p3)) as Constraint); solver.addConstraint((container == (p1 + p2 + p3)) as Constraint);
solver.flushParameterUpdates(); solver.flushUpdates();
expect(container.value, 100.0); expect(container.value, 100.0);
...@@ -533,9 +533,9 @@ void main() { ...@@ -533,9 +533,9 @@ void main() {
}); });
test('test_updates_collection', () { test('test_updates_collection', () {
Param left = new Param(); Param left = new Param.withContext("left");
Param mid = new Param(); Param mid = new Param.withContext("mid");
Param right = new Param(); Param right = new Param.withContext("right");
Solver s = new Solver(); Solver s = new Solver();
...@@ -547,13 +547,36 @@ void main() { ...@@ -547,13 +547,36 @@ void main() {
expect(s.suggestValueForVariable(mid.variable, 50.0), Result.success); expect(s.suggestValueForVariable(mid.variable, 50.0), Result.success);
var updates = s.flushParameterUpdates(); var updates = s.flushUpdates();
expect(updates.length, 2); expect(updates.length, 2);
expect(updates[0] is Param, true);
expect(left.value, 0.0); expect(left.value, 0.0);
expect(mid.value, 50.0); expect(mid.value, 50.0);
expect(right.value, 100.0); expect(right.value, 100.0);
}); });
test('test_updates_collection_is_set', () {
Param left = new Param.withContext("a");
Param mid = new Param.withContext("a");
Param right = new Param.withContext("a");
Solver s = new Solver();
expect(s.addEditVariable(mid.variable, Priority.strong), Result.success);
expect(s.addConstraint((mid * CM(2.0) == left + right) as Constraint),
Result.success);
expect(s.addConstraint(left >= CM(10.0)), Result.success);
expect(s.suggestValueForVariable(mid.variable, 50.0), Result.success);
var updates = s.flushUpdates();
expect(updates.length, 1);
expect(left.value, 10.0);
expect(mid.value, 50.0);
expect(right.value, 90.0);
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment