theprogrammablemind_4wp 7.5.8-beta.14 → 7.5.8-beta.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. package/client.js +11 -7
  2. package/package.json +1 -1
  3. package/src/config.js +130 -2
package/client.js CHANGED
@@ -109,7 +109,7 @@ class ErrorReason extends Error {
109
109
 
110
110
  const setupArgs = (args, config, logs, hierarchy) => {
111
111
  config.setArgs(args)
112
- args.calls = new InitCalls(config.name)
112
+ args.calls = new InitCalls(args.isInstance ? `${args.isInstance}#${config.name}` : config.name)
113
113
  if (global.theprogrammablemind && global.theprogrammablemind.loadForTesting) {
114
114
  args.calls = new InitCalls(Object.keys(global.theprogrammablemind.loadForTesting)[0])
115
115
  }
@@ -367,7 +367,7 @@ const setupContexts = (rawContexts) => {
367
367
  return contexts
368
368
  }
369
369
 
370
- const processContextsB = ({ config, hierarchy, semantics, generators, json, isTest, query, data, retries, url, commandLineArgs }) => {
370
+ const processContextsB = ({ config, hierarchy, semantics, generators, json, isTest, isInstance, query, data, retries, url, commandLineArgs }) => {
371
371
  // TODO fix this name to contextsPrime
372
372
  const contextsPrime = []
373
373
  const generatedPrime = []
@@ -378,7 +378,7 @@ const processContextsB = ({ config, hierarchy, semantics, generators, json, isTe
378
378
  const contexts = setupContexts(json.contexts)
379
379
 
380
380
  const objects = config.get('objects')
381
- const args = { objects, isResponse: true, response: json, isTest, getObjects: getObjects(objects) }
381
+ const args = { objects, isResponse: true, response: json, isTest, isInstance, getObjects: getObjects(objects), isInstance }
382
382
  if (!json.logs) {
383
383
  json.logs = []
384
384
  }
@@ -551,6 +551,8 @@ const setupProcessB = ({ config, initializer, allowDelta=false } = {}) => {
551
551
  } else {
552
552
  Object.assign(data, config.config)
553
553
  }
554
+
555
+ config.toServer(data)
554
556
 
555
557
  if (data.namespaces) {
556
558
  for (const uuid of Object.keys(data.namespaces)) {
@@ -586,7 +588,7 @@ const processInstance = (config, instance) => {
586
588
  // config.addInternal(config.template.queries[i], { handleCalculatedProps: true } )
587
589
  config.addInternal(instance.template.queries[i], { handleCalculatedProps: true } )
588
590
  } else {
589
- processContextsB({ config, hierarchy, json: results/*, generators, semantics */, commandLineArgs: {} })
591
+ processContextsB({ config, hierarchy, json: results/*, generators, semantics */, commandLineArgs: {}, isInstance: `instance${i}` })
590
592
  }
591
593
  }
592
594
  global.transitoryMode = transitoryMode
@@ -1447,9 +1449,10 @@ const knowledgeModule = async ({
1447
1449
  description: 'Entodicton knowledge module'
1448
1450
  })
1449
1451
 
1450
- const helpDebugAssociation = 'In order to get a debug break when a specific association is created set the DEBUG_ASSOCIATION environment variable to the JSON of the association to break on. For example DEBUG_ASSOCIATION=\'["the#0", "mammel#0"]\' })'
1451
- const helpDebugHierarchy = 'In order to get a debug break when a specific hierarchy is created set the DEBUG_HIERARCHY environment variable to the JSON of the child-parent pair to break on. For example DEBUG_HIERARCHY=\'["cat#1", "mammel#1"]\' })'
1452
- const helpDebugPriority = 'In order to get a debug break when a specific set of priorities is created set set DEBUG_PRIORITY environment variable to the JSON of the priorities that you want to break on. For example DEBUG_PRIORITY=\'["verb#0", "article#0"]\' })'
1452
+ const helpDebugAssociation = 'In order to get a debug break when a specific association is created set the DEBUG_ASSOCIATION environment variable to the JSON of the association to break on. For example DEBUG_ASSOCIATION=\'[["the", 0], ["mammal", 1]]\' })'
1453
+ const helpDebugHierarchy = 'In order to get a debug break when a specific hierarchy is created set the DEBUG_HIERARCHY environment variable to the JSON of the child-parent pair to break on. For example DEBUG_HIERARCHY=\'[["cat", 1], ["mammel", 1]]\' })'
1454
+ const helpDebugPriority = 'In order to get a debug break when a specific set of priorities is created set set DEBUG_PRIORITY environment variable to the JSON of the priorities that you want to break on. For example DEBUG_PRIORITY=\'[["verb", 0], ["article", 0]]\' })'
1455
+ const helpDebugContextualPriority = 'In order to get a debug break when a specific set of contextual priorities is created set set DEBUG_CONTEXTUAL_PRIORITY environment variable to the JSON of the priorities that you want to break on. For example DEBUG_CONTEXTUAL_PRIORITY=\'{ context: [["verb", 0], ["article", 0], select: 1}\' })'
1453
1456
  const helpDebugBridge = 'In order to get a debug break when a specific bridge is created set the DEBUG_BRIDGE environment variable to id/level to break on. For example DEBUG_BRIDGE=\'id#level\' })'
1454
1457
  const helpDebugOperator = 'In order to get a debug break when a specific hierarcy is created set the DEBUG_OPERATOR environment variable to debug any config loaded. For example DEBUG_OPERATOR=\'([operator] ([arg]))\' })'
1455
1458
 
@@ -1484,6 +1487,7 @@ const knowledgeModule = async ({
1484
1487
  parser.add_argument('-da', '--debugAssociation', { help: helpDebugAssociation })
1485
1488
  parser.add_argument('-dh', '--debugHierarchy', { help: helpDebugHierarchy })
1486
1489
  parser.add_argument('-dp', '--debugPriority', { help: helpDebugPriority })
1490
+ parser.add_argument('-dcp', '--debugContextualPriority', { help: helpDebugContextualPriority })
1487
1491
  parser.add_argument('-db', '--debugBridge', { help: helpDebugBridge })
1488
1492
  parser.add_argument('-do', '--debugOperator', { help: helpDebugOperator })
1489
1493
 
package/package.json CHANGED
@@ -63,6 +63,6 @@
63
63
  "json-stable-stringify": "^1.0.1",
64
64
  "node-fetch": "^2.6.1"
65
65
  },
66
- "version": "7.5.8-beta.14",
66
+ "version": "7.5.8-beta.16",
67
67
  "license": "ISC"
68
68
  }
package/src/config.js CHANGED
@@ -22,6 +22,82 @@ const indent = (string, indent) => {
22
22
  return string.replace(/^/gm, ' '.repeat(indent));
23
23
  }
24
24
 
25
+ const config_toServer = (config) => {
26
+ if (config.contextual_priorities) {
27
+ config.contextual_priorities = config.contextual_priorities.map((cp) => {
28
+ return [cp.context, cp.choose]
29
+ })
30
+ }
31
+ }
32
+
33
+ const contextual_priorities_toServer = (cp) => {
34
+ if (cp.context && cp.choose) {
35
+ return [cp.context, cp.choose]
36
+ }
37
+ return cp
38
+ }
39
+
40
+ const contextual_priorities_toClient = (cp) => {
41
+ if (cp.context && cp.choose) {
42
+ return cp
43
+ }
44
+ return { context: cp[0], choose: cp[1] }
45
+ }
46
+
47
+ const operatorKey_valid = (key) => {
48
+ if (
49
+ !_.isArray(key) ||
50
+ key.length != 2 ||
51
+ !_.isString(key[0]) ||
52
+ !_.isInteger(key[1]) ||
53
+ key[1] < 0
54
+ ) {
55
+
56
+ let details = ''
57
+ if (!_.isArray(key)) {
58
+ details = "Expected an array."
59
+ } else if (key.length != 2) {
60
+ details = "Expected an array of length two."
61
+ } else if (!_.isString(key[0])) {
62
+ details = "Expected element zero to be a string that is an operator id."
63
+ } else if (!_.isInteger(key[1])) {
64
+ details = "Expected element one to be a number that is an operator level."
65
+ } else if (key[1] < 0) {
66
+ details = "Expected element one to be a number that is an operator level which is greater than zero."
67
+ }
68
+ throw new Error(`${JSON.stringify(key)} is not a valid operator key. Values are of the form [<operatorId>, <operatorLevel>]. ${details}`)
69
+ }
70
+ }
71
+
72
+ const elist = (list, check, prefix) => {
73
+ for ([index, element] of list.entries()) {
74
+ try {
75
+ check(element)
76
+ } catch( e ) {
77
+ throw new Error(prefix(index, e))
78
+ }
79
+ }
80
+ }
81
+ const contextual_priorities_valid = (cps) => {
82
+ elist(cps, (cp) => contextual_priority_valid(cp), (index, e) => `contextual_priorities has an invalid contextual priority at position ${index}. ${e}`)
83
+ }
84
+
85
+ const contextual_priority_valid = (cp) => {
86
+ if (!cp.context) {
87
+ throw new Error(`The contextual priority ${JSON.stringify(cp)} is missing the "context" property. That is a list of the operator keys that are to be prioritized differently.`)
88
+ }
89
+ if (!_.isArray(cp.context)) {
90
+ throw new Error(`The contextual priority ${JSON.stringify(cp)} has an invalid "context" value. That is a list of the operator keys that are to be prioritized differently.`)
91
+ }
92
+ elist(cp.context, (element) => operatorKey_valid(element), (index, e) => `The contextual priority ${JSON.stringify(cp)} has an invalid operator key at position ${index}. ${e}`)
93
+ if (!cp.choose && cp.choose !== 0) {
94
+ throw new Error(`The contextual priority ${JSON.stringify(cp)} is missing the "choose" property. That is an index into the "context" property of the operator that is to be prioritized.`)
95
+ }
96
+ if (!_.isInteger(cp.choose) || cp.choose < 0 || cp.choose >= cp.context.length) {
97
+ throw new Error(`The contextual priority ${JSON.stringify(cp)} is missing the "choose" property. That is an index into the "context" property of the operator that is to be prioritized. Valid values are between 0 and ${cp.context.length-1}.`)
98
+ }
99
+ }
100
+
25
101
  const handleBridgeProps = (config, bridge) => {
26
102
  ecatch(`While processing the bridge for ${bridge.id}#${bridge.level}`,
27
103
  () => {
@@ -171,6 +247,10 @@ if (runtime.process.env.DEBUG_PRIORITY) {
171
247
  global.entodictonDebugPriority = JSON.parse(runtime.process.env.DEBUG_PRIORITY)
172
248
  }
173
249
 
250
+ if (runtime.process.env.DEBUG_CONTEXTUAL_PRIORITY) {
251
+ global.entodictonDebugContextualPriority = JSON.parse(runtime.process.env.DEBUG_CONTEXTUAL_PRIORITY)
252
+ }
253
+
174
254
  if (runtime.process.env.DEBUG_ASSOCIATION) {
175
255
  global.entodictonDebugAssociation = JSON.parse(runtime.process.env.DEBUG_ASSOCIATION)
176
256
  }
@@ -489,6 +569,10 @@ const multiApiImpl = (initializer) => {
489
569
 
490
570
  class Config {
491
571
 
572
+ toServer (config) {
573
+ return config_toServer(config)
574
+ }
575
+
492
576
  inDevelopmentMode (call) {
493
577
  config.developmentModeOn += 1
494
578
  try {
@@ -548,6 +632,7 @@ class Config {
548
632
  },
549
633
  eqClasses: [],
550
634
  priorities: [], // Done
635
+ contextual_priorities: [],
551
636
  version: '3',
552
637
  debug: false,
553
638
  associations: { // Done
@@ -586,6 +671,7 @@ class Config {
586
671
  'namespaces',
587
672
  'eqClasses',
588
673
  'priorities',
674
+ 'contextual_priorities',
589
675
  'associations',
590
676
  'words',
591
677
  'floaters',
@@ -825,6 +911,22 @@ class Config {
825
911
  this._delta.json.priorities.push({ action: 'add', priorities })
826
912
  }
827
913
 
914
+ // [ operators: <list of [id, level]>, select: <index of prioritized operator> ]
915
+ addContextualPriority(contextual_priority) {
916
+ if (!this.config.contextual_priorities) {
917
+ this.config.contextual_priorities = []
918
+ }
919
+ if (global.entodictonDebugContextualPriority) {
920
+ if (helpers.safeEquals(entodictonDebugContextualPriority, contextual_priorities)) {
921
+ debugger; // debug hierarchy hit
922
+ }
923
+ }
924
+ contextual_priority_valid(contextual_priority)
925
+ const cpServer = contextual_priorities_toServer(contextual_priority)
926
+ this.config.contextual_priorities.push(cpServer)
927
+ this._delta.json.contextual_priorities.push({ action: 'add', contextual_priority: cpServer })
928
+ }
929
+
828
930
  addHierarchy (child, parent) {
829
931
  if (child && parent || !child || Array.isArray(child) || (typeof child == 'string' && !parent)) {
830
932
  this.addHierarchyChildParent(child, parent)
@@ -1233,6 +1335,7 @@ class Config {
1233
1335
  'operators',
1234
1336
  'words',
1235
1337
  'priorities',
1338
+ 'contextual_priorities',
1236
1339
  'associations',
1237
1340
  'name',
1238
1341
  'version',
@@ -1262,6 +1365,7 @@ class Config {
1262
1365
  config.hierarchy = config.hierarchy || []
1263
1366
  config.associations = config.associations || { negative: [], positive: [] }
1264
1367
  config.priorities = config.priorities || []
1368
+ config.contextual_priorities = config.contextual_priorities || []
1265
1369
  }
1266
1370
 
1267
1371
  this.maxDepth = 20 // for generators and semantics
@@ -1315,6 +1419,10 @@ class Config {
1315
1419
  }
1316
1420
  }
1317
1421
 
1422
+ if (config && config.contextual_priorities) {
1423
+ contextual_priorities_valid(config.contextual_priorities)
1424
+ }
1425
+
1318
1426
  normalizeConfig(config)
1319
1427
 
1320
1428
  // set the default server so stuff just works
@@ -1385,7 +1493,10 @@ class Config {
1385
1493
  }
1386
1494
 
1387
1495
  delta () {
1388
- return { cacheKey: this._delta.cacheKey, json: this._delta.json }
1496
+ return {
1497
+ cacheKey: this._delta.cacheKey,
1498
+ json: this._delta.json
1499
+ }
1389
1500
  }
1390
1501
 
1391
1502
  resetDelta (cacheKey) {
@@ -1397,6 +1508,7 @@ class Config {
1397
1508
  bridges: [],
1398
1509
  associations: [],
1399
1510
  priorities: [],
1511
+ contextual_priorities: [],
1400
1512
  hierarchy: [],
1401
1513
  }
1402
1514
  }
@@ -2051,6 +2163,7 @@ class Config {
2051
2163
  bridges: this.config.bridges,
2052
2164
  hierarchy: this.config.hierarchy,
2053
2165
  priorities: this.config.priorities,
2166
+ contextual_priorities: this.config.contextual_priorities,
2054
2167
  associations: this.config.associations,
2055
2168
  words: this.config.words
2056
2169
  }
@@ -2059,6 +2172,7 @@ class Config {
2059
2172
  this.config.bridges = []
2060
2173
  this.config.hierarchy = []
2061
2174
  this.config.priorities = []
2175
+ this.config.contextual_priorities = []
2062
2176
  this.config.associations = { positive: [], negative: [] }
2063
2177
  this.config.words = {}
2064
2178
 
@@ -2279,6 +2393,18 @@ class Config {
2279
2393
  config.priorities = priorities
2280
2394
  }
2281
2395
 
2396
+ if (config.contextual_priorities) {
2397
+ let contextual_priorities = config.contextual_priorities
2398
+ contextual_priorities = contextual_priorities.map((cp) => {
2399
+ const [ context, choose ] = cp
2400
+ const contextPrime = context.map((id) => {
2401
+ return [toNS(id[0]), id[1]]
2402
+ })
2403
+ return [ contextPrime, choose ]
2404
+ })
2405
+ config.contextual_priorities = contextual_priorities
2406
+ }
2407
+
2282
2408
  for (const bag of bags) {
2283
2409
  if (config[bag]) {
2284
2410
  config[bag] = config[bag].map((b) => {
@@ -2708,5 +2834,7 @@ class Config {
2708
2834
  }
2709
2835
 
2710
2836
  module.exports = {
2711
- Config
2837
+ Config,
2838
+ config_toServer,
2839
+ operatorKey_valid,
2712
2840
  }