theprogrammablemind 9.8.0-beta.9 → 9.9.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client.js +52 -33
- package/lines.js +1 -1
- package/package.json +4 -2
- package/src/config.js +60 -62
- package/src/configHelpers.js +43 -48
- package/src/digraph.js +99 -11
- package/src/digraph_internal.js +135 -20
- package/src/fragments.js +17 -1
- package/src/generators.js +8 -8
- package/src/helpers.js +29 -12
- package/src/project2.js +24 -18
- package/src/semantics.js +12 -5
package/src/configHelpers.js
CHANGED
|
@@ -1,7 +1,24 @@
|
|
|
1
|
-
const { InitCalls } = require('./helpers')
|
|
2
|
-
const
|
|
1
|
+
const { InitCalls, where } = require('./helpers')
|
|
2
|
+
const Digraph = require('./digraph')
|
|
3
3
|
const debug = require('./debug')
|
|
4
4
|
|
|
5
|
+
class ErrorReason extends Error {
|
|
6
|
+
constructor (context, stack) {
|
|
7
|
+
super(JSON.stringify(context))
|
|
8
|
+
this.reason = context
|
|
9
|
+
this.stack = stack
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
toString() {
|
|
13
|
+
if (this.reason) {
|
|
14
|
+
return this.reason
|
|
15
|
+
} else {
|
|
16
|
+
return super.toString()
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
5
22
|
const gs = (g) => async (contexts, separator, lastSeparator) => {
|
|
6
23
|
if (!Array.isArray(contexts)) {
|
|
7
24
|
throw new Error('Expected a list')
|
|
@@ -63,13 +80,6 @@ const isA = (hierarchy) => (child, parent, { strict = false } = {}) => {
|
|
|
63
80
|
}
|
|
64
81
|
}
|
|
65
82
|
|
|
66
|
-
class ErrorReason extends Error {
|
|
67
|
-
constructor (context) {
|
|
68
|
-
super(JSON.stringify(context))
|
|
69
|
-
this.reason = context
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
83
|
const cleanAssign = (dest, ...srcs) => {
|
|
74
84
|
for (const key in dest) {
|
|
75
85
|
let found = false
|
|
@@ -93,29 +103,16 @@ class HandlerStack {
|
|
|
93
103
|
}
|
|
94
104
|
|
|
95
105
|
push(handler) {
|
|
96
|
-
this.handlers.push(handler)
|
|
106
|
+
this.handlers.push(handler.where || handler)
|
|
97
107
|
}
|
|
98
108
|
|
|
99
109
|
pop() {
|
|
100
110
|
this.handlers.pop()
|
|
101
111
|
}
|
|
102
112
|
|
|
103
|
-
/*
|
|
104
113
|
async wrap(call, w) {
|
|
105
|
-
const s = w
|
|
106
114
|
this.push(w)
|
|
107
|
-
const p = call()
|
|
108
|
-
if (p) {
|
|
109
|
-
return p.finally(() => {
|
|
110
|
-
this.pop()
|
|
111
|
-
})
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
*/
|
|
115
|
-
|
|
116
|
-
async wrap(call, w) {
|
|
117
|
-
this.push(w)
|
|
118
|
-
const p = call()
|
|
115
|
+
const p = await call()
|
|
119
116
|
if (p && typeof p.finally === 'function') {
|
|
120
117
|
return p.finally(() => {
|
|
121
118
|
this.pop()
|
|
@@ -210,7 +207,7 @@ const setupArgs = (args, config, logs, hierarchy, uuidForScoping) => {
|
|
|
210
207
|
args.km = (name) => config.getConfig(name)
|
|
211
208
|
args.api = (name) => config.getConfig(name).api
|
|
212
209
|
args.error = (context) => {
|
|
213
|
-
throw new ErrorReason(context)
|
|
210
|
+
throw new ErrorReason(context, [...args.stack.handlers, where()])
|
|
214
211
|
}
|
|
215
212
|
args.kms = config.getConfigs()
|
|
216
213
|
args.config = config.getPseudoConfig(uuidForScoping, config)
|
|
@@ -223,9 +220,11 @@ const setupArgs = (args, config, logs, hierarchy, uuidForScoping) => {
|
|
|
223
220
|
args.fragments = (query, mappings) => {
|
|
224
221
|
return config.fragment(args, query, mappings)
|
|
225
222
|
}
|
|
223
|
+
/*
|
|
226
224
|
args.fragmentMapper = (values, fromModelQuery, toModelQuery) => {
|
|
227
225
|
return config.fragmentMapper(args, values, fromModelQuery, toModelQuery)
|
|
228
226
|
}
|
|
227
|
+
*/
|
|
229
228
|
args.breakOnSemantics = false
|
|
230
229
|
args.theDebugger = {
|
|
231
230
|
breakOnSemantics: (value) => args.breakOnSemantics = value
|
|
@@ -278,24 +277,18 @@ const setupArgs = (args, config, logs, hierarchy, uuidForScoping) => {
|
|
|
278
277
|
const { assumed = {}, options = {} } = rest
|
|
279
278
|
return config.getGenerators(logs).apply(addAssumed(args, assumed, { paraphrase: false, isResponse: true }), { ...c, paraphrase: false, isResponse: true }, options)
|
|
280
279
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
return
|
|
286
|
-
}
|
|
287
|
-
return config.getEvaluator(args.s, args.calls, logs, c, { isModule: args.isModule, isProcess: args.isProcess })
|
|
280
|
+
|
|
281
|
+
args.e = (c, where) => {
|
|
282
|
+
if (!c) {
|
|
283
|
+
return
|
|
288
284
|
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
}
|
|
294
|
-
const s = (c, options = {}) => config.getSemantics(logs).apply(args, c, options)
|
|
295
|
-
return config.getEvaluator(s, args.calls, logs, c, { isModule: args.isModule, isProcess: args.isProcess })
|
|
285
|
+
if (where) {
|
|
286
|
+
return args.stack.wrap(() => config.evaluate(args, logs, c), where)
|
|
287
|
+
} else {
|
|
288
|
+
return config.evaluate(args, logs, c)
|
|
296
289
|
}
|
|
297
|
-
args.s = args.e
|
|
298
290
|
}
|
|
291
|
+
|
|
299
292
|
args.gs = gs(args.g)
|
|
300
293
|
args.gsp = gs(args.gp)
|
|
301
294
|
args.gsr = gs(args.gr)
|
|
@@ -342,7 +335,7 @@ const run = async (config, handler) => {
|
|
|
342
335
|
config = config
|
|
343
336
|
}
|
|
344
337
|
|
|
345
|
-
const hierarchy = new
|
|
338
|
+
const hierarchy = new Digraph((config.config || {}).hierarchy || [])
|
|
346
339
|
|
|
347
340
|
const objects = config.config.objects.namespaced[config.uuid]
|
|
348
341
|
const logs = []
|
|
@@ -364,7 +357,7 @@ const processContext = async (context, { objects = {}, config, logs = [] }) => {
|
|
|
364
357
|
const response = {} // NA but passed in
|
|
365
358
|
// generators = new Generators(generators.map((g) => new Generator(normalizeGenerator(g))))
|
|
366
359
|
// semantics = new Semantics(semantics.map((g) => new Semantic(normalizeSemantic(g))))
|
|
367
|
-
const hierarchy = new
|
|
360
|
+
const hierarchy = new Digraph((config.config || {}).hierarchy || [])
|
|
368
361
|
|
|
369
362
|
const args = { objects, response, getObjects: getObjects(objects) }
|
|
370
363
|
setupArgs(args, config, logs, hierarchy)
|
|
@@ -380,7 +373,7 @@ const processContext = async (context, { objects = {}, config, logs = [] }) => {
|
|
|
380
373
|
return { context, generated, paraphrases, responses }
|
|
381
374
|
}
|
|
382
375
|
|
|
383
|
-
const setupProcessB = ({ config, initializer, allowDelta = false, rebuildingTemplate = false } = {}) => {
|
|
376
|
+
const setupProcessB = ({ config, initializer, isFragment, allowDelta = false, rebuildingTemplate = false } = {}) => {
|
|
384
377
|
const key = config._key
|
|
385
378
|
const data = Object.assign({ key, version: '3' }, { uuid: config._uuid })
|
|
386
379
|
/*
|
|
@@ -409,7 +402,7 @@ const setupProcessB = ({ config, initializer, allowDelta = false, rebuildingTemp
|
|
|
409
402
|
delete data.generators
|
|
410
403
|
// const semantics = new Semantics((data.semantics || []).map((g) => new Semantic(normalizeSemantic(g))))
|
|
411
404
|
delete data.semantics
|
|
412
|
-
// const hierarchy = new
|
|
405
|
+
// const hierarchy = new Digraph((config.config || {}).hierarchy || [])
|
|
413
406
|
const hierarchy = config.hierarchy
|
|
414
407
|
|
|
415
408
|
return {
|
|
@@ -445,7 +438,7 @@ const setupContexts = (rawContexts) => {
|
|
|
445
438
|
return contexts
|
|
446
439
|
}
|
|
447
440
|
|
|
448
|
-
const processContextsB = async ({ config, calls, hierarchy, logs, semantics, generators, json, isTest, isProcess, isModule, rebuildingTemplate, isInstance, instance, query, data, retries, url, commandLineArgs, forTemplate, contextIdCounter }) => {
|
|
441
|
+
const processContextsB = async ({ skipSemantics, isFragment, config, calls, hierarchy, logs, semantics, generators, json, isTest, isProcess, isModule, rebuildingTemplate, isInstance, instance, query, data, retries, url, commandLineArgs, forTemplate, contextIdCounter }) => {
|
|
449
442
|
// TODO fix this name to contextsPrime
|
|
450
443
|
const contextsPrime = []
|
|
451
444
|
const generatedPrime = []
|
|
@@ -453,6 +446,7 @@ const processContextsB = async ({ config, calls, hierarchy, logs, semantics, gen
|
|
|
453
446
|
const paraphrasesParenthesizedPrime = []
|
|
454
447
|
const generatedParenthesizedPrime = []
|
|
455
448
|
const responsesPrime = []
|
|
449
|
+
|
|
456
450
|
const contexts = setupContexts(json.contexts)
|
|
457
451
|
|
|
458
452
|
const objects = config.get('objects')
|
|
@@ -474,7 +468,7 @@ const processContextsB = async ({ config, calls, hierarchy, logs, semantics, gen
|
|
|
474
468
|
throw new Error('There are errors in the logs. Run with the -d flag and grep for Error')
|
|
475
469
|
}
|
|
476
470
|
const generateParenthesized = isTest || (commandLineArgs && commandLineArgs.save)
|
|
477
|
-
if (!config.get('skipSemantics')) {
|
|
471
|
+
if (!config.get('skipSemantics') && !skipSemantics) {
|
|
478
472
|
const semantics = config.getSemantics(logs)
|
|
479
473
|
try {
|
|
480
474
|
contextPrime = await semantics.apply(args, context)
|
|
@@ -489,17 +483,18 @@ const processContextsB = async ({ config, calls, hierarchy, logs, semantics, gen
|
|
|
489
483
|
if (isInstance) {
|
|
490
484
|
console.log('error', e.error)
|
|
491
485
|
}
|
|
486
|
+
|
|
492
487
|
let reason = e.reason
|
|
493
488
|
if (!reason) {
|
|
494
489
|
if (e.error) {
|
|
495
490
|
reason = e.error[0]
|
|
496
491
|
}
|
|
497
492
|
}
|
|
493
|
+
|
|
498
494
|
contextPrime = await semantics.apply(args, {
|
|
499
495
|
marker: 'error',
|
|
500
496
|
context,
|
|
501
|
-
text: e
|
|
502
|
-
// reason: e.reason,
|
|
497
|
+
text: e.toString(),
|
|
503
498
|
reason,
|
|
504
499
|
error: e.stack || e.error
|
|
505
500
|
})
|
package/src/digraph.js
CHANGED
|
@@ -58,7 +58,7 @@ class Digraph {
|
|
|
58
58
|
|
|
59
59
|
addEdge (edge) {
|
|
60
60
|
edge = toA(edge)
|
|
61
|
-
if (this._edges.find((existing) =>
|
|
61
|
+
if (this._edges.find((existing) => edge[0] === existing[0] && edge[1] === existing[1])) {
|
|
62
62
|
return false
|
|
63
63
|
}
|
|
64
64
|
this._edges.push(edge)
|
|
@@ -189,7 +189,7 @@ class Digraph {
|
|
|
189
189
|
}
|
|
190
190
|
|
|
191
191
|
exists (child, parent) {
|
|
192
|
-
return this._edges.
|
|
192
|
+
return this._edges.findIndex((edge) => edge[0] === child && edge[1] === parent) != -1
|
|
193
193
|
}
|
|
194
194
|
|
|
195
195
|
addList (l) {
|
|
@@ -200,17 +200,105 @@ class Digraph {
|
|
|
200
200
|
}
|
|
201
201
|
}
|
|
202
202
|
|
|
203
|
-
order (
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
203
|
+
order (nodes) {
|
|
204
|
+
const nodeSet = new Set(nodes);
|
|
205
|
+
const successors = new Map(); // lesser -> Set of greaters
|
|
206
|
+
|
|
207
|
+
for (const node of nodes) {
|
|
208
|
+
successors.set(node, new Set());
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Build subgraph of edges that connect nodes in the input list
|
|
212
|
+
for (const [lesser, greater] of this._edges) {
|
|
213
|
+
if (nodeSet.has(lesser) && nodeSet.has(greater) && lesser !== greater) {
|
|
214
|
+
successors.get(lesser).add(greater);
|
|
210
215
|
}
|
|
211
|
-
// ordered = ordered.concat([...nodes])
|
|
212
216
|
}
|
|
213
|
-
|
|
217
|
+
|
|
218
|
+
const WHITE = 0; // unvisited
|
|
219
|
+
const GRAY = 1; // visiting (on current path)
|
|
220
|
+
const BLACK = 2; // done
|
|
221
|
+
|
|
222
|
+
const color = new Map();
|
|
223
|
+
const parent = new Map();
|
|
224
|
+
for (const node of nodes) {
|
|
225
|
+
color.set(node, WHITE);
|
|
226
|
+
parent.set(node, null);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
let cycle = null;
|
|
230
|
+
|
|
231
|
+
const dfs = (node) => {
|
|
232
|
+
if (cycle) return;
|
|
233
|
+
color.set(node, GRAY);
|
|
234
|
+
|
|
235
|
+
for (const next of successors.get(node) || []) {
|
|
236
|
+
if (color.get(next) === GRAY) {
|
|
237
|
+
// Found a back edge → reconstruct cycle
|
|
238
|
+
const path = [];
|
|
239
|
+
let cur = node;
|
|
240
|
+
while (cur !== next && cur !== null) {
|
|
241
|
+
path.push(cur);
|
|
242
|
+
cur = parent.get(cur);
|
|
243
|
+
}
|
|
244
|
+
path.push(next);
|
|
245
|
+
path.reverse();
|
|
246
|
+
path.push(next); // close the cycle
|
|
247
|
+
cycle = path;
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (color.get(next) === WHITE) {
|
|
251
|
+
parent.set(next, node);
|
|
252
|
+
dfs(next);
|
|
253
|
+
if (cycle) return;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
color.set(node, BLACK);
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
// Detect cycle
|
|
261
|
+
for (const node of nodes) {
|
|
262
|
+
if (color.get(node) === WHITE) {
|
|
263
|
+
dfs(node);
|
|
264
|
+
if (cycle) {
|
|
265
|
+
const err = new Error('Cycle detected: ' + cycle.join(' -> '));
|
|
266
|
+
err.cycle = cycle;
|
|
267
|
+
throw err;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// No cycle → Kahn's algorithm (preserves relative order of independent nodes)
|
|
273
|
+
const indegree = new Map();
|
|
274
|
+
for (const node of nodes) indegree.set(node, 0);
|
|
275
|
+
|
|
276
|
+
for (const [from, toSet] of successors) {
|
|
277
|
+
for (const to of toSet) {
|
|
278
|
+
indegree.set(to, (indegree.get(to) || 0) + 1);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const queue = [];
|
|
283
|
+
for (const node of nodes) {
|
|
284
|
+
if (indegree.get(node) === 0) queue.push(node);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const result = [];
|
|
288
|
+
let head = 0;
|
|
289
|
+
|
|
290
|
+
while (head < queue.length) {
|
|
291
|
+
const current = queue[head++];
|
|
292
|
+
result.push(current);
|
|
293
|
+
|
|
294
|
+
for (const next of successors.get(current) || []) {
|
|
295
|
+
const deg = indegree.get(next) - 1;
|
|
296
|
+
indegree.set(next, deg);
|
|
297
|
+
if (deg === 0) queue.push(next);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return result;
|
|
214
302
|
}
|
|
215
303
|
}
|
|
216
304
|
|
package/src/digraph_internal.js
CHANGED
|
@@ -15,6 +15,41 @@ class DigraphInternal {
|
|
|
15
15
|
this._edges = edges
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
// BFS
|
|
19
|
+
path (from, to) {
|
|
20
|
+
const frontier = { [from]: [[]] }
|
|
21
|
+
const done = new Set()
|
|
22
|
+
while (Object.keys(frontier).length > 0) {
|
|
23
|
+
const n = Object.keys(frontier)[0]
|
|
24
|
+
const ps = frontier[n]
|
|
25
|
+
if (to === n) {
|
|
26
|
+
return ps[0]
|
|
27
|
+
}
|
|
28
|
+
if (done.has(n)) {
|
|
29
|
+
delete frontier[n]
|
|
30
|
+
continue
|
|
31
|
+
}
|
|
32
|
+
done.add(n)
|
|
33
|
+
for (const edge of this._edges) {
|
|
34
|
+
if (edge.child == n) {
|
|
35
|
+
if (!frontier[edge.parent]) {
|
|
36
|
+
frontier[edge.parent] = []
|
|
37
|
+
}
|
|
38
|
+
for (const path of ps) {
|
|
39
|
+
if (edge.parent == to) {
|
|
40
|
+
return [...path, edge]
|
|
41
|
+
}
|
|
42
|
+
frontier[edge.parent].push([...path, edge])
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
get length () {
|
|
50
|
+
return this._edges.length
|
|
51
|
+
}
|
|
52
|
+
|
|
18
53
|
addEdges (edges) {
|
|
19
54
|
for (const edge of edges) {
|
|
20
55
|
this.addEdge(edge)
|
|
@@ -23,7 +58,6 @@ class DigraphInternal {
|
|
|
23
58
|
|
|
24
59
|
addEdge (edge) {
|
|
25
60
|
edge = toA(edge)
|
|
26
|
-
// if (this._edges.find((existing) => _.isEqual(edge, existing))) {
|
|
27
61
|
if (this._edges.find((existing) => edge[0] === existing[0] && edge[1] === existing[1])) {
|
|
28
62
|
return false
|
|
29
63
|
}
|
|
@@ -59,17 +93,10 @@ class DigraphInternal {
|
|
|
59
93
|
return froms
|
|
60
94
|
}
|
|
61
95
|
|
|
62
|
-
get length () {
|
|
63
|
-
return this._edges.length
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/*
|
|
67
|
-
set edges(edges) {
|
|
68
|
-
this._edges = edges.map( toA )
|
|
69
|
-
}
|
|
70
|
-
*/
|
|
71
|
-
|
|
72
96
|
acdcs (s, from, to) {
|
|
97
|
+
if (!s) {
|
|
98
|
+
return new Set()
|
|
99
|
+
}
|
|
73
100
|
const todo = [s]
|
|
74
101
|
const seen = new Set([s])
|
|
75
102
|
const acdcs = new Set([])
|
|
@@ -173,17 +200,105 @@ class DigraphInternal {
|
|
|
173
200
|
}
|
|
174
201
|
}
|
|
175
202
|
|
|
176
|
-
order (
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
203
|
+
order (nodes) {
|
|
204
|
+
const nodeSet = new Set(nodes);
|
|
205
|
+
const successors = new Map(); // lesser -> Set of greaters
|
|
206
|
+
|
|
207
|
+
for (const node of nodes) {
|
|
208
|
+
successors.set(node, new Set());
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Build subgraph of edges that connect nodes in the input list
|
|
212
|
+
for (const [lesser, greater] of this._edges) {
|
|
213
|
+
if (nodeSet.has(lesser) && nodeSet.has(greater) && lesser !== greater) {
|
|
214
|
+
successors.get(lesser).add(greater);
|
|
183
215
|
}
|
|
184
|
-
// ordered = ordered.concat([...nodes])
|
|
185
216
|
}
|
|
186
|
-
|
|
217
|
+
|
|
218
|
+
const WHITE = 0; // unvisited
|
|
219
|
+
const GRAY = 1; // visiting (on current path)
|
|
220
|
+
const BLACK = 2; // done
|
|
221
|
+
|
|
222
|
+
const color = new Map();
|
|
223
|
+
const parent = new Map();
|
|
224
|
+
for (const node of nodes) {
|
|
225
|
+
color.set(node, WHITE);
|
|
226
|
+
parent.set(node, null);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
let cycle = null;
|
|
230
|
+
|
|
231
|
+
const dfs = (node) => {
|
|
232
|
+
if (cycle) return;
|
|
233
|
+
color.set(node, GRAY);
|
|
234
|
+
|
|
235
|
+
for (const next of successors.get(node) || []) {
|
|
236
|
+
if (color.get(next) === GRAY) {
|
|
237
|
+
// Found a back edge → reconstruct cycle
|
|
238
|
+
const path = [];
|
|
239
|
+
let cur = node;
|
|
240
|
+
while (cur !== next && cur !== null) {
|
|
241
|
+
path.push(cur);
|
|
242
|
+
cur = parent.get(cur);
|
|
243
|
+
}
|
|
244
|
+
path.push(next);
|
|
245
|
+
path.reverse();
|
|
246
|
+
path.push(next); // close the cycle
|
|
247
|
+
cycle = path;
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (color.get(next) === WHITE) {
|
|
251
|
+
parent.set(next, node);
|
|
252
|
+
dfs(next);
|
|
253
|
+
if (cycle) return;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
color.set(node, BLACK);
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
// Detect cycle
|
|
261
|
+
for (const node of nodes) {
|
|
262
|
+
if (color.get(node) === WHITE) {
|
|
263
|
+
dfs(node);
|
|
264
|
+
if (cycle) {
|
|
265
|
+
const err = new Error('Cycle detected: ' + cycle.join(' -> '));
|
|
266
|
+
err.cycle = cycle;
|
|
267
|
+
throw err;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// No cycle → Kahn's algorithm (preserves relative order of independent nodes)
|
|
273
|
+
const indegree = new Map();
|
|
274
|
+
for (const node of nodes) indegree.set(node, 0);
|
|
275
|
+
|
|
276
|
+
for (const [from, toSet] of successors) {
|
|
277
|
+
for (const to of toSet) {
|
|
278
|
+
indegree.set(to, (indegree.get(to) || 0) + 1);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const queue = [];
|
|
283
|
+
for (const node of nodes) {
|
|
284
|
+
if (indegree.get(node) === 0) queue.push(node);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const result = [];
|
|
288
|
+
let head = 0;
|
|
289
|
+
|
|
290
|
+
while (head < queue.length) {
|
|
291
|
+
const current = queue[head++];
|
|
292
|
+
result.push(current);
|
|
293
|
+
|
|
294
|
+
for (const next of successors.get(current) || []) {
|
|
295
|
+
const deg = indegree.get(next) - 1;
|
|
296
|
+
indegree.set(next, deg);
|
|
297
|
+
if (deg === 0) queue.push(next);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return result;
|
|
187
302
|
}
|
|
188
303
|
}
|
|
189
304
|
|
package/src/fragments.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const _ = require('lodash')
|
|
2
|
+
const debug = require('./debug')
|
|
2
3
|
const helpers = require('./helpers')
|
|
3
4
|
|
|
4
5
|
function createChangeTracker(obj) {
|
|
@@ -28,11 +29,23 @@ function fragmentInstantiator (args, contexts) {
|
|
|
28
29
|
contexts: () => {
|
|
29
30
|
return _.cloneDeep(contexts)
|
|
30
31
|
},
|
|
31
|
-
|
|
32
|
+
// preserve property not to be changed.
|
|
33
|
+
instantiate: async (mappings, preserve) => {
|
|
34
|
+
preserve = preserve || []
|
|
32
35
|
const instantiated = _.cloneDeep(contexts)
|
|
33
36
|
const todo = [{ context: instantiated, path: [] }]
|
|
34
37
|
args = { ...args }
|
|
35
38
|
args.pathEquals = helpers.pathEquals
|
|
39
|
+
const preserved = preserve.map((path) => {
|
|
40
|
+
if (contexts.length == 1) {
|
|
41
|
+
path = ['0', path]
|
|
42
|
+
}
|
|
43
|
+
const value = helpers.getByPath(contexts, path)
|
|
44
|
+
return { path, value }
|
|
45
|
+
})
|
|
46
|
+
for (const context of contexts) {
|
|
47
|
+
context.context_id = args.config.nextFragmentContextId()
|
|
48
|
+
}
|
|
36
49
|
while (todo.length > 0) {
|
|
37
50
|
const { context, path } = todo.pop()
|
|
38
51
|
const { proxy, getChanges } = createChangeTracker(context);
|
|
@@ -57,6 +70,9 @@ function fragmentInstantiator (args, contexts) {
|
|
|
57
70
|
}
|
|
58
71
|
}
|
|
59
72
|
}
|
|
73
|
+
for (const { path, value } of preserved) {
|
|
74
|
+
helpers.setByPath(instantiated, path, value)
|
|
75
|
+
}
|
|
60
76
|
if (contexts.length == 1 && instantiated.length == 1) {
|
|
61
77
|
return instantiated[0]
|
|
62
78
|
} else {
|
package/src/generators.js
CHANGED
|
@@ -225,18 +225,18 @@ class Generators {
|
|
|
225
225
|
const source = `${generator.km}/#${generator.index}`
|
|
226
226
|
lines.setElement(0, 2, `ERROR while applying (${source}) ${generator.toLabel()}`)
|
|
227
227
|
lines.newRow()
|
|
228
|
-
lines.setElement(0,
|
|
228
|
+
lines.setElement(0, 1, 'DEBUG')
|
|
229
|
+
lines.setElement(0, 2, `To debug this use debug.breakAt('${args.calls.current()}') or DEBUG_BREAKAT="${args.calls.current()}"`)
|
|
229
230
|
lines.newRow()
|
|
230
231
|
lines.setElement(0, 1, 'STACK')
|
|
231
232
|
lines.setElement(0, 2, stack)
|
|
232
233
|
lines.newRow()
|
|
234
|
+
lines.setElement(0, 2, generator.toString())
|
|
235
|
+
lines.newRow()
|
|
233
236
|
lines.setElement(0, 1, 'TO')
|
|
234
237
|
lines.setElement(0, 2, `context_id: ${context.context_id}`)
|
|
235
238
|
lines.setElement(1, 2, JSON.stringify(helpers.sortJson(context, { depth: 10 }), null, 2))
|
|
236
239
|
lines.newRow()
|
|
237
|
-
lines.setElement(0, 1, 'DEBUG')
|
|
238
|
-
lines.setElement(0, 2, `To debug this use debug.breakAt('${args.calls.current()}') or DEBUG_BREAKAT="${args.calls.current()}"`)
|
|
239
|
-
lines.newRow()
|
|
240
240
|
lines.setElement(0, 1, 'ERROR')
|
|
241
241
|
lines.setElement(0, 2, errorMessage)
|
|
242
242
|
this.logs.push(lines.toString())
|
|
@@ -256,19 +256,19 @@ class Generators {
|
|
|
256
256
|
lines.setElement(0, 2, generator.toLabel())
|
|
257
257
|
}
|
|
258
258
|
lines.newRow()
|
|
259
|
+
lines.setElement(0, 1, 'DEBUG')
|
|
260
|
+
lines.setElement(0, 2, `To debug this use debug.breakAt('${args.calls.current()}')`)
|
|
261
|
+
lines.newRow()
|
|
259
262
|
lines.setElement(0, 1, 'APPLIED')
|
|
260
263
|
lines.setElement(0, 2, generator.toLabel())
|
|
261
264
|
lines.newRow()
|
|
262
|
-
lines.setElement(0, 2, generator.toString())
|
|
263
|
-
lines.newRow()
|
|
264
265
|
lines.setElement(0, 1, 'RESULT')
|
|
265
266
|
lines.setElement(0, 2, generated + " " + pOrRTag)
|
|
266
267
|
lines.newRow()
|
|
267
268
|
lines.setElement(0, 1, 'STACK')
|
|
268
269
|
lines.setElement(0, 2, stack)
|
|
269
270
|
lines.newRow()
|
|
270
|
-
lines.setElement(0,
|
|
271
|
-
lines.setElement(0, 2, `To debug this use debug.breakAt('${args.calls.current()}')`)
|
|
271
|
+
lines.setElement(0, 2, generator.toString())
|
|
272
272
|
lines.newRow()
|
|
273
273
|
lines.setElement(0, 1, 'TO')
|
|
274
274
|
lines.setElement(0, 2, `context_id: ${context.context_id}`)
|
package/src/helpers.js
CHANGED
|
@@ -22,12 +22,30 @@ class OverrideCheck {
|
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
function pathEquals (p1, p2) {
|
|
25
|
+
function pathEquals (p1, p2, n) {
|
|
26
|
+
if (n !== undefined) {
|
|
27
|
+
if (n < 0) {
|
|
28
|
+
p1 = p1.slice(n)
|
|
29
|
+
p2 = p2.slice(n)
|
|
30
|
+
} else {
|
|
31
|
+
p1 = p1.slice(0, n)
|
|
32
|
+
p2 = p2.slice(0, n)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
26
36
|
if (p1.length !== p2.length) {
|
|
27
37
|
return false
|
|
28
38
|
}
|
|
29
39
|
for (let i = 0; i < p1.length; ++i) {
|
|
30
|
-
if (
|
|
40
|
+
if (p1[i].integer) {
|
|
41
|
+
if (!Number.isInteger(p2[i])) {
|
|
42
|
+
return false
|
|
43
|
+
}
|
|
44
|
+
} else if (p2[i].integer) {
|
|
45
|
+
if (!Number.isInteger(p1[i])) {
|
|
46
|
+
return false
|
|
47
|
+
}
|
|
48
|
+
} else if (typeof p2[i] == 'function') {
|
|
31
49
|
if (!p2[i](p1[i])) {
|
|
32
50
|
return false
|
|
33
51
|
}
|
|
@@ -72,7 +90,14 @@ function where (goUp = 2) {
|
|
|
72
90
|
let match
|
|
73
91
|
for (line of lines.slice(1)) {
|
|
74
92
|
// if (!(line.includes('config.js:') || line.includes('client.js:') || line.includes('<anonymous>'))) {
|
|
75
|
-
|
|
93
|
+
const ignore = [
|
|
94
|
+
'config.js:',
|
|
95
|
+
'client.js:',
|
|
96
|
+
'helpers.js:',
|
|
97
|
+
'configHelpers.js:',
|
|
98
|
+
]
|
|
99
|
+
// if (!(line.includes('config.js:') || line.includes('client.js:') || line.includes('helpers.js:'))) {
|
|
100
|
+
if(!ignore.some(p => line.includes(p))) {
|
|
76
101
|
match = regexForm1.exec(line) || regexForm2.exec(line)
|
|
77
102
|
if (!match) {
|
|
78
103
|
continue
|
|
@@ -477,15 +502,6 @@ const functionsToStrings = (config) => {
|
|
|
477
502
|
if (bridge.semantics) {
|
|
478
503
|
bridge.semantics = bridge.semantics.toString()
|
|
479
504
|
}
|
|
480
|
-
if (bridge.evaluator) {
|
|
481
|
-
bridge.evaluator = bridge.evaluator.toString()
|
|
482
|
-
}
|
|
483
|
-
if (bridge.evaluator2) {
|
|
484
|
-
bridge.evaluator2 = bridge.evaluator2.toString()
|
|
485
|
-
}
|
|
486
|
-
if (bridge.evaluators) {
|
|
487
|
-
bridge.evaluators = bridge.evaluators.toString()
|
|
488
|
-
}
|
|
489
505
|
return bridge
|
|
490
506
|
})
|
|
491
507
|
}
|
|
@@ -718,4 +734,5 @@ module.exports = {
|
|
|
718
734
|
watchProperty,
|
|
719
735
|
pathEquals,
|
|
720
736
|
OverrideCheck,
|
|
737
|
+
normalizePath,
|
|
721
738
|
}
|