Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bin/c.js

Large diffs are not rendered by default.

64 changes: 51 additions & 13 deletions src/SimplexSolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ c.SimplexSolver = c.inherit({
this._stkCedcns = [0]; // Stack
if (c.trace)
c.traceprint("objective expr == " + this.rows.get(this._objective));

this.externalState = {};
this._observers = [];
},

addLowerBound: function(v /*c.AbstractVariable*/, lower /*double*/) {
Expand Down Expand Up @@ -916,7 +919,7 @@ c.SimplexSolver = c.inherit({
_setExternalVariables: function() {
if (c.trace) c.fnenterprint("_setExternalVariables:");
if (c.trace) c.traceprint(this.toString());
var changed = {};
var externalState = {};

// console.log("this._externalParametricVars:", this._externalParametricVars);
this._externalParametricVars.each(function(v) {
Expand All @@ -925,7 +928,7 @@ c.SimplexSolver = c.inherit({
console.log("Error: variable" + v + " in _externalParametricVars is basic");
} else {
v.value = 0;
changed[v.name] = 0;
externalState[v.name] = 0;
}
}, this);
// console.log("this._externalRows:", this._externalRows);
Expand All @@ -934,32 +937,67 @@ c.SimplexSolver = c.inherit({
if (v.value != expr.constant) {
// console.log(v.toString(), v.value, expr.constant);
v.value = expr.constant;
changed[v.name] = expr.constant;
}
externalState[v.name] = v.value;
// if (c.trace) console.log("v == " + v);
// if (c.trace) console.log("expr == " + expr);
}, this);
this._changed = changed;
this._queueChanges(externalState);
this._fNeedsSolving = false;
this._informCallbacks();
this.onsolved();
},

onsolved: function() {
// Lifecycle stub. Here for dirty, dirty monkey patching.
},

_informCallbacks: function() {
if(!this._callbacks) return;
_queueChanges: function(newState) {
console.log("finding changes between", this.externalState, newState);
var previousState = this.externalState;
this.externalState = newState;

var changed = this._changed;
this._callbacks.forEach(function(fn) {
fn(changed);
});
if(this._observers.length > 0) {
var changes = [];

Object.keys(newState).forEach(function(key) {
if(key in previousState) {
if(previousState[key] != newState[key]) {
changes.push({ type: "updated", name: key, object: this, oldValue: previousState[key] });
}
delete previousState[key];
} else {
changes.push({ type: "new", name: key, object: this })
}
}, this);

Object.keys(previousState).forEach(function(key) {
changes.push({ type: "deleted", name: key, object: this });
}, this);

this._observers.forEach(function(o){
o._pendingChangeRecords = o._pendingChangeRecords.concat(changes);
});
}

// FIXME: should be setting up a promise to deliver at end of call stack
this.informObservers();
},

_addCallback: function(fn) {
(this._callbacks || (this._callbacks = [])).push(fn);
informObservers: function() {
this._observers.forEach(function (observer) {
var records = observer._pendingChangeRecords
observer._pendingChangeRecords = [];
observer(records);
}, this);
},

observe: function(callback) {
if (typeof callback !== "function") {
throw new TypeError("Solver observers must be functions.")
}

callback._pendingChangeRecords = [];
this._observers.push(callback);
},

insertErrorVar: function(cn /*c.Constraint*/, aVar /*c.AbstractVariable*/) {
Expand Down
6 changes: 5 additions & 1 deletion src/parser/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ c._api = function() {
var r = c.parser.parse(args[0]);
return compile(r);
} else if(typeof args[0] == "function") {
solver._addCallback(args[0]);
solver.observe(args[0]);
}
}
};

c._reset = function() {
solver = new c.SimplexSolver();
}

})(this["c"]||module.parent.exports||{});
5 changes: 5 additions & 0 deletions tests/SimplexSolver-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ define([
describe('addPointStays', function () {

});

describe('#observe', function () {
var solver = c.SimplexSolver();
var eq = 2;
});
});
});
27 changes: 15 additions & 12 deletions tests/parser/apitest.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,29 @@ define([
var describe = bdd.describe,
it = bdd.it;

// NOTE: the below change-tracking code doesn't actually work yet!
return;

describe('new api', function () {
it('informs on variable changes', function () {
console.log("!!!starting api test!!!")
c._reset();
var changes = [];
c('a+b==c');
c(function (change) {
var callback = function (change) {
changes.push(change);
});
};
c(callback);

c('a+b==c');
c('a==1');
c('b==0');

assert.equal(2, changes.length);
assert.property(changes[0], 'a');
assert.property(changes[0], 'b');
assert.property(changes[0], 'c');
// FIXME: will be in one array and delivered outside this call stack.
// Test should return a promise resolved with these assertions inside callback.

assert.equal(3, changes[0].length);
assert.deepEqual(["new a", "new b", "new c"], changes[0].map(function(record){ return record.type + " " + record.name; }).sort());

assert.deepEqual(["updated a", "updated b"], changes[1].map(function(record){ return record.type + " " + record.name }).sort());

assert.property(changes[1], 'b');
assert.property(changes[1], 'c');
assert.deepEqual(["updated b", "updated c"], changes[2].map(function(record){ return record.type + " " + record.name }).sort());
});
});
});