theprogrammablemind 9.8.0 → 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 +65 -50
- package/index.js +2 -1
- package/lines.js +1 -1
- package/package.json +4 -2
- package/src/config.js +70 -52
- package/src/configHelpers.js +56 -26
- package/src/digraph.js +99 -11
- package/src/digraph_internal.js +135 -20
- package/src/fragments.js +17 -2
- package/src/generators.js +22 -13
- package/src/helpers.js +82 -9
- package/src/project2.js +24 -18
- package/src/semantics.js +25 -15
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,13 +103,25 @@ 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
|
|
|
113
|
+
async wrap(call, w) {
|
|
114
|
+
this.push(w)
|
|
115
|
+
const p = await call()
|
|
116
|
+
if (p && typeof p.finally === 'function') {
|
|
117
|
+
return p.finally(() => {
|
|
118
|
+
this.pop()
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
this.pop()
|
|
122
|
+
return p
|
|
123
|
+
}
|
|
124
|
+
|
|
103
125
|
toString() {
|
|
104
126
|
let str = ''
|
|
105
127
|
for (let i = this.handlers.length - 1; i >= 0; --i) {
|
|
@@ -185,7 +207,7 @@ const setupArgs = (args, config, logs, hierarchy, uuidForScoping) => {
|
|
|
185
207
|
args.km = (name) => config.getConfig(name)
|
|
186
208
|
args.api = (name) => config.getConfig(name).api
|
|
187
209
|
args.error = (context) => {
|
|
188
|
-
throw new ErrorReason(context)
|
|
210
|
+
throw new ErrorReason(context, [...args.stack.handlers, where()])
|
|
189
211
|
}
|
|
190
212
|
args.kms = config.getConfigs()
|
|
191
213
|
args.config = config.getPseudoConfig(uuidForScoping, config)
|
|
@@ -198,15 +220,15 @@ const setupArgs = (args, config, logs, hierarchy, uuidForScoping) => {
|
|
|
198
220
|
args.fragments = (query, mappings) => {
|
|
199
221
|
return config.fragment(args, query, mappings)
|
|
200
222
|
}
|
|
223
|
+
/*
|
|
201
224
|
args.fragmentMapper = (values, fromModelQuery, toModelQuery) => {
|
|
202
225
|
return config.fragmentMapper(args, values, fromModelQuery, toModelQuery)
|
|
203
226
|
}
|
|
227
|
+
*/
|
|
204
228
|
args.breakOnSemantics = false
|
|
205
229
|
args.theDebugger = {
|
|
206
230
|
breakOnSemantics: (value) => args.breakOnSemantics = value
|
|
207
231
|
}
|
|
208
|
-
if (!logs) {
|
|
209
|
-
}
|
|
210
232
|
args.log = (message) => logs.push(message)
|
|
211
233
|
|
|
212
234
|
args.addWord = (word, def) => config.addWord(word, def, args.uuid)
|
|
@@ -221,7 +243,6 @@ const setupArgs = (args, config, logs, hierarchy, uuidForScoping) => {
|
|
|
221
243
|
return { ...args, assumed: Object.assign({}, assumed, (args.assumed || {}), ...moreAssumed) }
|
|
222
244
|
}
|
|
223
245
|
|
|
224
|
-
args.s = (c, options = {}) => config.getSemantics(logs).apply(args, c, options)
|
|
225
246
|
args.g = (c, rest = {}) => {
|
|
226
247
|
// if (JSON.stringify(rest) !== '{}' && !(rest.assumed || rest.options)) {
|
|
227
248
|
// debugger
|
|
@@ -256,12 +277,18 @@ const setupArgs = (args, config, logs, hierarchy, uuidForScoping) => {
|
|
|
256
277
|
const { assumed = {}, options = {} } = rest
|
|
257
278
|
return config.getGenerators(logs).apply(addAssumed(args, assumed, { paraphrase: false, isResponse: true }), { ...c, paraphrase: false, isResponse: true }, options)
|
|
258
279
|
}
|
|
259
|
-
|
|
280
|
+
|
|
281
|
+
args.e = (c, where) => {
|
|
260
282
|
if (!c) {
|
|
261
283
|
return
|
|
262
284
|
}
|
|
263
|
-
|
|
285
|
+
if (where) {
|
|
286
|
+
return args.stack.wrap(() => config.evaluate(args, logs, c), where)
|
|
287
|
+
} else {
|
|
288
|
+
return config.evaluate(args, logs, c)
|
|
289
|
+
}
|
|
264
290
|
}
|
|
291
|
+
|
|
265
292
|
args.gs = gs(args.g)
|
|
266
293
|
args.gsp = gs(args.gp)
|
|
267
294
|
args.gsr = gs(args.gr)
|
|
@@ -308,7 +335,7 @@ const run = async (config, handler) => {
|
|
|
308
335
|
config = config
|
|
309
336
|
}
|
|
310
337
|
|
|
311
|
-
const hierarchy = new
|
|
338
|
+
const hierarchy = new Digraph((config.config || {}).hierarchy || [])
|
|
312
339
|
|
|
313
340
|
const objects = config.config.objects.namespaced[config.uuid]
|
|
314
341
|
const logs = []
|
|
@@ -330,7 +357,7 @@ const processContext = async (context, { objects = {}, config, logs = [] }) => {
|
|
|
330
357
|
const response = {} // NA but passed in
|
|
331
358
|
// generators = new Generators(generators.map((g) => new Generator(normalizeGenerator(g))))
|
|
332
359
|
// semantics = new Semantics(semantics.map((g) => new Semantic(normalizeSemantic(g))))
|
|
333
|
-
const hierarchy = new
|
|
360
|
+
const hierarchy = new Digraph((config.config || {}).hierarchy || [])
|
|
334
361
|
|
|
335
362
|
const args = { objects, response, getObjects: getObjects(objects) }
|
|
336
363
|
setupArgs(args, config, logs, hierarchy)
|
|
@@ -346,7 +373,7 @@ const processContext = async (context, { objects = {}, config, logs = [] }) => {
|
|
|
346
373
|
return { context, generated, paraphrases, responses }
|
|
347
374
|
}
|
|
348
375
|
|
|
349
|
-
const setupProcessB = ({ config, initializer, allowDelta = false, rebuildingTemplate = false } = {}) => {
|
|
376
|
+
const setupProcessB = ({ config, initializer, isFragment, allowDelta = false, rebuildingTemplate = false } = {}) => {
|
|
350
377
|
const key = config._key
|
|
351
378
|
const data = Object.assign({ key, version: '3' }, { uuid: config._uuid })
|
|
352
379
|
/*
|
|
@@ -375,7 +402,7 @@ const setupProcessB = ({ config, initializer, allowDelta = false, rebuildingTemp
|
|
|
375
402
|
delete data.generators
|
|
376
403
|
// const semantics = new Semantics((data.semantics || []).map((g) => new Semantic(normalizeSemantic(g))))
|
|
377
404
|
delete data.semantics
|
|
378
|
-
// const hierarchy = new
|
|
405
|
+
// const hierarchy = new Digraph((config.config || {}).hierarchy || [])
|
|
379
406
|
const hierarchy = config.hierarchy
|
|
380
407
|
|
|
381
408
|
return {
|
|
@@ -411,7 +438,7 @@ const setupContexts = (rawContexts) => {
|
|
|
411
438
|
return contexts
|
|
412
439
|
}
|
|
413
440
|
|
|
414
|
-
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 }) => {
|
|
415
442
|
// TODO fix this name to contextsPrime
|
|
416
443
|
const contextsPrime = []
|
|
417
444
|
const generatedPrime = []
|
|
@@ -419,6 +446,7 @@ const processContextsB = async ({ config, calls, hierarchy, logs, semantics, gen
|
|
|
419
446
|
const paraphrasesParenthesizedPrime = []
|
|
420
447
|
const generatedParenthesizedPrime = []
|
|
421
448
|
const responsesPrime = []
|
|
449
|
+
|
|
422
450
|
const contexts = setupContexts(json.contexts)
|
|
423
451
|
|
|
424
452
|
const objects = config.get('objects')
|
|
@@ -440,7 +468,7 @@ const processContextsB = async ({ config, calls, hierarchy, logs, semantics, gen
|
|
|
440
468
|
throw new Error('There are errors in the logs. Run with the -d flag and grep for Error')
|
|
441
469
|
}
|
|
442
470
|
const generateParenthesized = isTest || (commandLineArgs && commandLineArgs.save)
|
|
443
|
-
if (!config.get('skipSemantics')) {
|
|
471
|
+
if (!config.get('skipSemantics') && !skipSemantics) {
|
|
444
472
|
const semantics = config.getSemantics(logs)
|
|
445
473
|
try {
|
|
446
474
|
contextPrime = await semantics.apply(args, context)
|
|
@@ -455,17 +483,18 @@ const processContextsB = async ({ config, calls, hierarchy, logs, semantics, gen
|
|
|
455
483
|
if (isInstance) {
|
|
456
484
|
console.log('error', e.error)
|
|
457
485
|
}
|
|
486
|
+
|
|
458
487
|
let reason = e.reason
|
|
459
488
|
if (!reason) {
|
|
460
489
|
if (e.error) {
|
|
461
490
|
reason = e.error[0]
|
|
462
491
|
}
|
|
463
492
|
}
|
|
493
|
+
|
|
464
494
|
contextPrime = await semantics.apply(args, {
|
|
465
495
|
marker: 'error',
|
|
466
496
|
context,
|
|
467
|
-
text: e
|
|
468
|
-
// reason: e.reason,
|
|
497
|
+
text: e.toString(),
|
|
469
498
|
reason,
|
|
470
499
|
error: e.stack || e.error
|
|
471
500
|
})
|
|
@@ -578,14 +607,14 @@ const loadInstance = async (config, instance) => {
|
|
|
578
607
|
// used to do a CLONE
|
|
579
608
|
config.addInternal(instance.template.configs[i], { uuid, km: instance.name, addFirst: true, handleCalculatedProps: true })
|
|
580
609
|
} else if (results.apply) {
|
|
581
|
-
const
|
|
610
|
+
const uuid = config.nameToUUID(instance.name)
|
|
611
|
+
const objects = getObjects(config.get('objects'))(uuid)
|
|
582
612
|
const args = { objects, getObjects: getObjects(objects) }
|
|
583
613
|
if (instance.configs) {
|
|
584
614
|
args.isInstance = `instance${i}`
|
|
585
615
|
args.instance = instance.configs[i]
|
|
586
616
|
}
|
|
587
617
|
|
|
588
|
-
const uuid = config.nameToUUID(instance.name)
|
|
589
618
|
setupArgs(args, config, config.logs, hierarchy, uuid)
|
|
590
619
|
if (config.name == instance.name) {
|
|
591
620
|
args.isModule = !!config.isModule
|
|
@@ -609,6 +638,7 @@ const loadInstance = async (config, instance) => {
|
|
|
609
638
|
args.instance = ''
|
|
610
639
|
args.isProcess = !config.isModule
|
|
611
640
|
args.isModule = !!config.isModule
|
|
641
|
+
args.logs = config.logs
|
|
612
642
|
if (instance.name == config.testConfig.testModuleName) {
|
|
613
643
|
args.isTesting = true
|
|
614
644
|
}
|
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) {
|
|
@@ -23,17 +24,28 @@ function createChangeTracker(obj) {
|
|
|
23
24
|
};
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
|
|
27
27
|
function fragmentInstantiator (args, contexts) {
|
|
28
28
|
return new Object({
|
|
29
29
|
contexts: () => {
|
|
30
30
|
return _.cloneDeep(contexts)
|
|
31
31
|
},
|
|
32
|
-
|
|
32
|
+
// preserve property not to be changed.
|
|
33
|
+
instantiate: async (mappings, preserve) => {
|
|
34
|
+
preserve = preserve || []
|
|
33
35
|
const instantiated = _.cloneDeep(contexts)
|
|
34
36
|
const todo = [{ context: instantiated, path: [] }]
|
|
35
37
|
args = { ...args }
|
|
36
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
|
+
}
|
|
37
49
|
while (todo.length > 0) {
|
|
38
50
|
const { context, path } = todo.pop()
|
|
39
51
|
const { proxy, getChanges } = createChangeTracker(context);
|
|
@@ -58,6 +70,9 @@ function fragmentInstantiator (args, contexts) {
|
|
|
58
70
|
}
|
|
59
71
|
}
|
|
60
72
|
}
|
|
73
|
+
for (const { path, value } of preserved) {
|
|
74
|
+
helpers.setByPath(instantiated, path, value)
|
|
75
|
+
}
|
|
61
76
|
if (contexts.length == 1 && instantiated.length == 1) {
|
|
62
77
|
return instantiated[0]
|
|
63
78
|
} else {
|
package/src/generators.js
CHANGED
|
@@ -125,7 +125,7 @@ class Generator {
|
|
|
125
125
|
args.g = args.gp
|
|
126
126
|
}
|
|
127
127
|
return await debug.hitScoped(args.callId, async () => {
|
|
128
|
-
if ((options.debug || {}).apply || process.env.
|
|
128
|
+
if ((options.debug || {}).apply || process.env.DEBUG_BREAKAT == args.callId || callId === this.callId) {
|
|
129
129
|
debugger // eslint-disable-line no-debugger
|
|
130
130
|
}
|
|
131
131
|
return await this._apply(args) // DEBUG: This is the line you want to step into
|
|
@@ -184,6 +184,9 @@ class Generators {
|
|
|
184
184
|
let generated = ''
|
|
185
185
|
let applied = false
|
|
186
186
|
const stack = args.calls.push()
|
|
187
|
+
const pOrRTag = assumed.isResponse ? "(RESPONSE)" : "(PARAPHRASE)"
|
|
188
|
+
const finallyList = []; // called after the generator is processed
|
|
189
|
+
args._finally = (call) => finallyList.unshift(call)
|
|
187
190
|
for (let igenerator = 0; igenerator < this.generators.length; ++igenerator) {
|
|
188
191
|
const generator = this.generators[igenerator]
|
|
189
192
|
if (await generator.matches(args, objects, context, hierarchy, config, options)) {
|
|
@@ -194,7 +197,7 @@ class Generators {
|
|
|
194
197
|
console.error("TPMKMS_TRACE", debug.counter("TPMKMS_TRACE"))
|
|
195
198
|
console.error(generator.toLabel())
|
|
196
199
|
console.error(generator.toString())
|
|
197
|
-
console.log(`To debug this use debug.breakAt('${args.calls.current()}') or
|
|
200
|
+
console.log(`To debug this use debug.breakAt('${args.calls.current()}') or DEBUG_BREAKAT="${args.calls.current()}"`)
|
|
198
201
|
}
|
|
199
202
|
|
|
200
203
|
try {
|
|
@@ -218,21 +221,22 @@ class Generators {
|
|
|
218
221
|
const widths = Lines.addRemainder([10, 10])
|
|
219
222
|
const lines = new Lines(widths)
|
|
220
223
|
lines.setElement(0, 0, 'Generator')
|
|
224
|
+
lines.setElement(0, 1, `#${context.context_id || "?"}`)
|
|
221
225
|
const source = `${generator.km}/#${generator.index}`
|
|
222
226
|
lines.setElement(0, 2, `ERROR while applying (${source}) ${generator.toLabel()}`)
|
|
223
227
|
lines.newRow()
|
|
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()}"`)
|
|
230
|
+
lines.newRow()
|
|
231
|
+
lines.setElement(0, 1, 'STACK')
|
|
232
|
+
lines.setElement(0, 2, stack)
|
|
233
|
+
lines.newRow()
|
|
224
234
|
lines.setElement(0, 2, generator.toString())
|
|
225
235
|
lines.newRow()
|
|
226
236
|
lines.setElement(0, 1, 'TO')
|
|
227
237
|
lines.setElement(0, 2, `context_id: ${context.context_id}`)
|
|
228
238
|
lines.setElement(1, 2, JSON.stringify(helpers.sortJson(context, { depth: 10 }), null, 2))
|
|
229
239
|
lines.newRow()
|
|
230
|
-
lines.setElement(0, 1, 'STACK')
|
|
231
|
-
lines.setElement(0, 2, stack)
|
|
232
|
-
lines.newRow()
|
|
233
|
-
lines.setElement(0, 1, 'DEBUG')
|
|
234
|
-
lines.setElement(0, 2, `To debug this use debug.breakAt('${args.calls.current()}') or DEBUG_CALLID="${args.calls.current()}"`)
|
|
235
|
-
lines.newRow()
|
|
236
240
|
lines.setElement(0, 1, 'ERROR')
|
|
237
241
|
lines.setElement(0, 2, errorMessage)
|
|
238
242
|
this.logs.push(lines.toString())
|
|
@@ -246,24 +250,25 @@ class Generators {
|
|
|
246
250
|
const widths = Lines.addRemainder([10, 10])
|
|
247
251
|
const lines = new Lines(widths)
|
|
248
252
|
lines.setElement(0, 0, 'Generator')
|
|
253
|
+
lines.setElement(0, 1, `#${context.context_id || "?"}`)
|
|
249
254
|
if (generator.index > -1 && generator.km) {
|
|
250
255
|
// lines.setElement(0, 2, `KM '${generator.km}' ordinal: ${generator.index}`)
|
|
251
256
|
lines.setElement(0, 2, generator.toLabel())
|
|
252
257
|
}
|
|
253
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()
|
|
254
262
|
lines.setElement(0, 1, 'APPLIED')
|
|
255
263
|
lines.setElement(0, 2, generator.toLabel())
|
|
256
264
|
lines.newRow()
|
|
257
|
-
lines.setElement(0, 2, generator.toString())
|
|
258
|
-
lines.newRow()
|
|
259
265
|
lines.setElement(0, 1, 'RESULT')
|
|
260
|
-
lines.setElement(0, 2, generated)
|
|
266
|
+
lines.setElement(0, 2, generated + " " + pOrRTag)
|
|
261
267
|
lines.newRow()
|
|
262
268
|
lines.setElement(0, 1, 'STACK')
|
|
263
269
|
lines.setElement(0, 2, stack)
|
|
264
270
|
lines.newRow()
|
|
265
|
-
lines.setElement(0,
|
|
266
|
-
lines.setElement(0, 2, `To debug this use debug.breakAt('${args.calls.current()}')`)
|
|
271
|
+
lines.setElement(0, 2, generator.toString())
|
|
267
272
|
lines.newRow()
|
|
268
273
|
lines.setElement(0, 1, 'TO')
|
|
269
274
|
lines.setElement(0, 2, `context_id: ${context.context_id}`)
|
|
@@ -274,11 +279,15 @@ class Generators {
|
|
|
274
279
|
break
|
|
275
280
|
}
|
|
276
281
|
}
|
|
282
|
+
for (const f of finallyList) {
|
|
283
|
+
await f()
|
|
284
|
+
}
|
|
277
285
|
args.calls.pop()
|
|
278
286
|
if (!applied && config.getDebug()) {
|
|
279
287
|
const widths = Lines.addRemainder([10, 10])
|
|
280
288
|
const lines = new Lines(widths)
|
|
281
289
|
lines.setElement(0, 0, 'Generator')
|
|
290
|
+
lines.setElement(0, 1, `#${context.context_id || "?"}`)
|
|
282
291
|
lines.setElement(0, 2, 'No generator applied')
|
|
283
292
|
lines.newRow()
|
|
284
293
|
lines.setElement(0, 1, 'STACK')
|