-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeltablue_ext.js
332 lines (304 loc) · 10.4 KB
/
deltablue_ext.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
module('users.timfelgentreff.babelsberg.deltablue_ext').
requires('users.timfelgentreff.deltablue.deltablue',
'users.timfelgentreff.jsinterpreter.Interpreter').
toRun(function() {
DBPlanner.addMethods({
isConstraintObject: true,
constraintVariableFor: function(value, ivarname) {
return new DBVariable(ivarname, value, this);
},
get strength() {
return DBStrength;
},
always: function(opts, func) {
if (Object.isString(opts.priority)) {
opts.priority = this.strength[opts.priority];
}
// XXX TODO: we should not actually need the next thing,
// but the implementation of DBVariable>>cnEquals is
// not complete. It needs to evaluate the RHS/argument
// only using allowUnsolveable...
func.allowUnsolvableOperations = true;
var planner = this,
ctx = opts.ctx,
priority = opts.priority,
methods = opts.methods;
func.varMapping = ctx;
if (!methods) {
methods = func;
func = undefined;
}
methods.varMapping = ctx;
var cobj = new Constraint(methods, planner);
var formulas = cobj.constraintvariables.collect(function(v) {
var v = v.externalVariables(planner);
return v ? v.removeFormula() : null;
}).compact();
if (formulas.length > 0) {
var constraint = new UserDBConstraint(priority, func, function(c) {
formulas.each(function(m) {
var inputs = m.inputs.select(function(input) {
return input instanceof DBVariable;
});
dbgOn(inputs.length !== m.inputs.length);
c.formula(m.output, inputs, m.func);
});
}, planner);
cobj.addPrimitiveConstraint(constraint);
}
cobj.priority = priority;
return cobj;
},
weight: 100,
addEditVar: function(v) {
if (!this.currentEdits) {
this.currentEdits = new DBOrderedCollection();
}
var edit = new EditDBConstraint(v, DBStrength.required, this);
this.currentEdits.add(edit);
return edit;
},
solve: function() {
// not required
},
beginEdit: function() {
if (this.currentEditPlan) {
throw "Trying to run nested edits - this isn't supported";
}
if (!this.currentEdits) {
throw 'No edit variables - cannot beginEdit';
}
this.currentEditPlan = this.extractDBPlanFromDBConstraints(this.currentEdits);
},
endEdit: function() {
if (this.currentEdits && this.currentEdits.length !== 0) {
this.currentEdits.elms.each(function(edit) {
edit.destroyDBConstraint();
});
}
this.currentEditPlan = null;
},
resolveArray: function(newValues) {
if (!this.currentEdits) {
throw 'resolveArray only valid in edit';
}
this.currentEdits.elms.each(function(edit, idx) {
edit.myOutput.value = newValues[idx];
});
this.currentEditPlan.execute();
},
solverName: 'DeltaBlue',
supportsMethods: function() { return true; },
supportsSoftConstraints: function() { return true; },
supportsFiniteDomains: function() { return false; },
supportedDataTypes: function() {
return ['number', 'boolean', 'string', 'object'];
}
});
Object.extend(DBPlanner, {
getInstance: function() {
if (!this['$$instance']) {
this['$$instance'] = new DBPlanner();
}
return this['$$instance'];
},
resetInstance: function() {
this['$$instance'] = undefined;
}
});
Object.extend(DBStrength, {
required: DBStrength.REQUIRED,
strong: DBStrength.STRONG_DEFAULT,
medium: DBStrength.NORMAL,
weak: DBStrength.WEAK_DEFAULT
});
DBVariable.addMethods({
isConstraintObject: true,
stay: function(strength) {
var cn = new StayDBConstraint(
this,
strength || DBStrength.WEAK_DEFAULT,
this.planner
);
cn.enable();
this._stayConstraint = cn;
return cn;
},
setReadonly: function(bool) {
if (bool && !this.readonlyConstraint) {
var cn = new StayDBConstraint(this, DBStrength.STONG_PREFERRED, this.planner);
cn.enable();
this.readonlyConstraint = cn;
return cn;
} else if (!bool && this.readonlyConstraint) {
this.readonlyConstraint.disable();
this.readonlyConstraint = undefined;
}
},
isReadonly: function() {
return !!this.readonlyConstraint;
},
formula: function(inputs, func) {
console.warn('Deprecated: Using DBVariable>>formula');
if (!Constraint.current) {
throw 'invalid outside constraint construction';
}
// var constraint = new Constraint(func, Constraint.current.solver),
// inputs = constraint.constraintvariables
if (this.__formula__) {
throw 'two formulas for the same variable ' + this;
}
this.__formula__ = {output: this, inputs: inputs, func: func};
},
removeFormula: function() {
var f = this.__formula__;
this.__formula__ = undefined;
return f;
},
removeStay: function() {
if (this._stayConstraint) {
try {
this.planner.removeConstraint(this._stayConstraint);
} catch (_) {
this._stayConstraint = null;
}
}
},
suggestValue: function(value) {
this.assignValue(value);
},
prepareEdit: function() {
if (this.editConstraint) {
// ignore?
} else {
this.editConstraint = this.planner.addEditVar(this);
}
},
finishEdit: function() {
this.editConstraint = null;
},
cnIdentical: function(other) {
if (!(other instanceof DBVariable)) {
other = new DBVariable('constant/' + other, other, this.planner);
var stay = new StayDBConstraint(other, DBStrength.required, this.planner);
stay.enable(DBStrength.required);
}
return new EqualityDBConstraint(
this,
other,
DBStrength.required,
Constraint.current.solver
);
},
cnEquals: function(other) {
if (!(other instanceof DBVariable)) {
var formulaNode = bbb.currentNode &&
((bbb.currentNode.name === '==') ||
(bbb.currentNode.property &&
bbb.currentNode.property.value === 'equals')) &&
(bbb.currentNode.right || bbb.currentNode.args[0]);
if (formulaNode) {
var self = this;
// let's generate a formula
var argumentString = cop.withLayers([PrintOMetaVariableAsBBBField],
function() {
return formulaNode.asJS();
}
);
var varMapping = bbb.currentInterpreter.getCurrentScope();
var func = (function() {
window.$$bbbVarMapping = varMapping;
try {
return eval(argumentString);
} finally {
delete window.$$bbbVarMapping;
}
});
// TODO: Trace through formulaNode and only add the actual dependencies
var inputs = Constraint.current.constraintvariables.map(function(cvar) {
return cvar.externalVariable;
}).filter(function(evar) {
return evar && evar !== this && typeof(evar.name) == 'string';
}.bind(this));
var c = new UserDBConstraint(function() {}, Constraint.current.solver);
c.formula(this, inputs, func);
return c;
} else {
other = new DBVariable('constant/' + other, other, this.planner);
Constraint.current.addPrimitiveConstraint(
new StayDBConstraint(other, DBStrength.required, this.planner)
);
}
}
var self = this;
cloneFunc = function(fromObj) {
if (fromObj.clone) {
return fromObj.clone();
} else if (fromObj.copy) {
return fromObj.copy();
} else {
return fromObj; // regress to identity constraint
}
};
return new UserDBConstraint(
function(c) {
c.formula(self, [other], cloneFunc);
c.formula(other, [self], cloneFunc);
},
Constraint.current.solver
);
},
cnOr: function(other) {
return this.value ? this : other;
},
equals: function() {
return this.cnEquals.apply(this, arguments);
}
});
UserDBConstraint.addMethods({
cnAnd: function(r) {
if (r instanceof UserDBConstraint) {
for (var i = 0; i < r.formulas.length; i++) {
var output = r.outputs[i],
formula = r.formulas[i],
inputs = formula.inputs,
func = formula.func;
this.formula(output, inputs, func);
}
return this;
} else {
Constraint.current.addPrimitiveConstraint(this);
return r;
}
}
});
cop.create('PrintOMetaVariableAsBBBField').
refineClass(users.timfelgentreff.jsinterpreter.Variable, {
asJS: function() {
var result = cop.proceed();
return '(window.$$bbbVarMapping.' + result + ')';
}
});
DBConstraint.addMethods({
isConstraintObject: true,
enable: function(priority) {
this.strength = priority || this.strength;
this.addDBConstraint();
},
disable: function() {
this.destroyDBConstraint();
},
cnOr: function(other) {
return this; // assume we can satisfy this
}
});
EqualityDBConstraint.addMethods({
setExecuteFunction: function(func) {
var orig = this.execute.$originalFunction || this.execute;
func.$originalFunction = orig;
this.execute = func;
},
unsetExecuteFunction: function() {
this.execute = this.execute.$originalFunction || this.execute;
}
});}); // end of module