theprogrammablemind_4wp 7.5.8-beta.7 → 7.5.8-beta.71

Sign up to get free protection for your applications and to get access to all the features.
package/src/config.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // lookup = (name) => returns <config>
2
2
  const { Semantics, normalizeGenerator } = require('./semantics')
3
3
  const { Generators } = require('./generators')
4
- // const { uuid: uuidv4 } = require('uuidv4')
4
+ const { v4 : uuidv4 } = require('uuid');
5
5
  const client = require('../client')
6
6
  const DigraphInternal = require('./digraph_internal')
7
7
  const helpers = require('./helpers')
@@ -22,7 +22,210 @@ const indent = (string, indent) => {
22
22
  return string.replace(/^/gm, ' '.repeat(indent));
23
23
  }
24
24
 
25
- const handleBridgeProps = (config, bridge) => {
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 debugPriority = (priority) => {
34
+ if (global.entodictonDebugPriority) {
35
+ if (helpers.safeEquals(entodictonDebugPriority, priority)) {
36
+ debugger; // debug hierarchy hit
37
+ }
38
+ }
39
+ }
40
+
41
+ const debugAssociation = (association) => {
42
+ if (global.entodictonDebugAssociation) {
43
+ if (helpers.safeEquals(global.entodictonDebugAssociation, association)) {
44
+ debugger; // debug association hit
45
+ }
46
+ }
47
+ }
48
+
49
+ const debugContextualPriority = (contextual_priority) => {
50
+ if (global.entodictonDebugContextualPriority) {
51
+ if (helpers.safeEquals(entodictonDebugContextualPriority, contextual_priorities)) {
52
+ debugger; // debug hierarchy hit
53
+ }
54
+ }
55
+ }
56
+
57
+ const debugHierarchy = (pair) => {
58
+ if (global.entodictonDebugHierarchy) {
59
+ if (helpers.safeEquals(global.entodictonDebugHierarchy, pair)) {
60
+ debugger; // debug hierarchy hit
61
+ }
62
+ }
63
+ }
64
+
65
+ const debugBridge = (bridge) => {
66
+ if (global.entodictonDebugBridge) {
67
+ if (global.entodictonDebugBridge[0] == bridge.id && global.entodictonDebugBridge[1] == bridge.level) {
68
+ debugger; // debug hierarchy hit
69
+ }
70
+ }
71
+ }
72
+
73
+ const debugOperator = (operator) => {
74
+ if (global.entodictonDebugOperator) {
75
+ if ((operator.pattern || operator) === global.entodictonDebugOperator) {
76
+ debugger; // debug operator hit
77
+ }
78
+ }
79
+ }
80
+
81
+ const debugConfigProps = (config) => {
82
+ const checkProps = [
83
+ { property: 'priorities', check: (v) => debugPriority(v) },
84
+ { property: 'association', check: (v) => debugAssociation(v) },
85
+ { property: 'contextual_priorities', check: (v) => debugContextualPriority(v) },
86
+ { property: 'hierarchy', check: (v) => debugHierarchy(v) },
87
+ { property: 'operators', check: (v) => debugOperator(v) },
88
+ { property: 'bridges', check: (v) => debugBridge(v) },
89
+ ]
90
+ for (const { property, check } of checkProps) {
91
+ if (config[property]) {
92
+ for (const value of config[property]) {
93
+ check(value)
94
+ }
95
+ }
96
+ }
97
+ }
98
+
99
+ const validConfigProps = (config) => {
100
+ const valid = [
101
+ 'hierarchy',
102
+ 'objects',
103
+ 'bridges',
104
+ 'operators',
105
+ 'words',
106
+ 'priorities',
107
+ 'contextual_priorities',
108
+ 'associations',
109
+ 'name',
110
+ 'version',
111
+ 'generatorp',
112
+ 'generators',
113
+ 'semantics',
114
+ 'where',
115
+ 'floaters',
116
+ 'debug',
117
+
118
+ // TODO Fix these from the test app
119
+ 'implicits',
120
+ 'convolution',
121
+ 'expected_generated',
122
+ 'expected_results',
123
+ 'skipSemantics',
124
+ 'description',
125
+ 'contexts',
126
+ 'utterances',
127
+ 'flatten',
128
+
129
+ 'namespaces',
130
+ 'eqClasses',
131
+ ]
132
+ helpers.validProps(valid, config, 'config')
133
+ }
134
+
135
+ const setupInitializerFNArgs = (config, args) => {
136
+ const aw = (word, def) => config.addWord(word, def, args.uuid)
137
+ const ag = (generator) => config.addGenerator(generator, args.uuid, config.name)
138
+ const km = (name) => config.getConfig(name)
139
+ const apis = (name) => config.getConfig(name).api
140
+
141
+ return {
142
+ ...args,
143
+ addWord: aw,
144
+ addGenerator: ag,
145
+ config: config.getPseudoConfig(args.uuid, args.currentConfig),
146
+ km,
147
+ baseConfig: config,
148
+ apis,
149
+ }
150
+ }
151
+
152
+ const contextual_priorities_toServer = (cp) => {
153
+ if (cp.context && cp.choose) {
154
+ return [cp.context, cp.choose]
155
+ }
156
+ return cp
157
+ }
158
+
159
+ const contextual_priorities_toClient = (cp) => {
160
+ if (cp.context && cp.choose) {
161
+ return cp
162
+ }
163
+ return { context: cp[0], choose: cp[1] }
164
+ }
165
+
166
+ const operatorKey_valid = (key) => {
167
+ if (
168
+ !_.isArray(key) ||
169
+ key.length != 2 ||
170
+ !_.isString(key[0]) ||
171
+ !_.isInteger(key[1]) ||
172
+ key[1] < 0
173
+ ) {
174
+
175
+ let details = ''
176
+ if (!_.isArray(key)) {
177
+ details = "Expected an array."
178
+ } else if (key.length != 2) {
179
+ details = "Expected an array of length two."
180
+ } else if (!_.isString(key[0])) {
181
+ details = "Expected element zero to be a string that is an operator id."
182
+ } else if (!_.isInteger(key[1])) {
183
+ details = "Expected element one to be a number that is an operator level."
184
+ } else if (key[1] < 0) {
185
+ details = "Expected element one to be a number that is an operator level which is greater than zero."
186
+ }
187
+ throw new Error(`${JSON.stringify(key)} is not a valid operator key. Values are of the form [<operatorId>, <operatorLevel>]. ${details}`)
188
+ }
189
+ }
190
+
191
+ const elist = (list, check, prefix) => {
192
+ for ([index, element] of list.entries()) {
193
+ try {
194
+ check(element)
195
+ } catch( e ) {
196
+ throw new Error(prefix(index, e))
197
+ }
198
+ }
199
+ }
200
+ const contextual_priorities_valid = (cps) => {
201
+ elist(cps, (cp) => contextual_priority_valid(cp), (index, e) => `contextual_priorities has an invalid contextual priority at position ${index}. ${e}`)
202
+ }
203
+
204
+ const contextual_priority_valid = (cp) => {
205
+ if (!cp.context) {
206
+ 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.`)
207
+ }
208
+ if (!_.isArray(cp.context)) {
209
+ 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.`)
210
+ }
211
+ elist(cp.context, (element) => operatorKey_valid(element), (index, e) => `The contextual priority ${JSON.stringify(cp)} has an invalid operator key at position ${index}. ${e}`)
212
+ if (!_.isArray(cp.choose)) {
213
+ throw new Error(`The contextual priority ${JSON.stringify(cp)} has an invalid "choose" value. The value should be a list of the operators in the context to consider for prioritization.`)
214
+ }
215
+ elist(cp.choose,
216
+ (element) => {
217
+ if (!element && element !== 0) {
218
+ throw new Error(`The value should be an index into the "context" property of the operator that is to be considered for prioritization.`)
219
+ }
220
+ if (!_.isInteger(element) || element < 0 || element >= cp.context.length) {
221
+ throw new Error(`The value should be an index into the "context" property of the operator that is to be considered for prioritization. Valid values are between 0 and ${cp.context.length-1}.`)
222
+ }
223
+ },
224
+ (index, e) => `The choose property in the contextual priority ${JSON.stringify(cp)} has an invalid index at position ${index}. ${e}`
225
+ )
226
+ }
227
+
228
+ const handleBridgeProps = (config, bridge, addFirst) => {
26
229
  ecatch(`While processing the bridge for ${bridge.id}#${bridge.level}`,
27
230
  () => {
28
231
  if (!bridge.bridge) {
@@ -51,7 +254,7 @@ const handleBridgeProps = (config, bridge) => {
51
254
  if (typeof after == 'string') {
52
255
  after = [after, 0]
53
256
  }
54
- config.addPriorities([after, [bridge.id, bridge.level]])
257
+ config.addPriorities([[bridge.id, bridge.level], after])
55
258
  }
56
259
  }
57
260
  if (bridge.after) {
@@ -59,7 +262,7 @@ const handleBridgeProps = (config, bridge) => {
59
262
  if (typeof before == 'string') {
60
263
  before = [before, 0]
61
264
  }
62
- config.addPriorities([[bridge.id, bridge.level], before])
265
+ config.addPriorities([before, [bridge.id, bridge.level]])
63
266
  }
64
267
  }
65
268
  if (bridge.words) {
@@ -68,19 +271,29 @@ const handleBridgeProps = (config, bridge) => {
68
271
  config.addWordInternal(def, {"id": bridge.id, "initial": `{ value: "${def}"}` })
69
272
  } else {
70
273
  const word = def.word
71
- def = { initial: JSON.stringify(def), id: bridge.id, word: undefined }
274
+ def = { initial: JSON.stringify(def), id: bridge.id, word }
72
275
  config.addWordInternal(word, def)
73
276
  }
74
277
  }
75
278
  }
279
+ /*
76
280
  if (bridge.generator) {
77
- config.config.generators.unshift(bridge.generator)
281
+ if (addFirst) {
282
+ config.config.generators.unshift(bridge.generator)
283
+ } else {
284
+ config.config.generators.push(bridge.generator)
285
+ }
78
286
  }
287
+ */
79
288
  if (bridge.generators) {
80
289
  const generators = [...bridge.generators]
81
290
  generators.reverse()
82
291
  for (let generator of generators) {
83
- config.config.generators.unshift(generator)
292
+ if (addFirst) {
293
+ config.config.generators.unshift(generator)
294
+ } else {
295
+ config.config.generators.push(generator)
296
+ }
84
297
  }
85
298
  }
86
299
  if (bridge.generatorpr) {
@@ -91,66 +304,76 @@ const handleBridgeProps = (config, bridge) => {
91
304
  const match = bridge.generatorp.match || (() => true)
92
305
  const apply = typeof bridge.generatorp == 'function' ? bridge.generatorp : bridge.generatorp.apply || bridge.generatorp
93
306
  const level = bridge.generatorp.level >= 0 ? bridge.generatorp.level : bridge.level + 1
94
- config.config.generators.unshift({
307
+
308
+ const generator = {
95
309
  where: bridge.generatorp.where || bridge.where || client.where(4),
96
310
  match: (args) => bridge.id == args.context.marker && args.context.level == level && args.context.paraphrase && match(args),
97
311
  apply: (args) => apply(args),
98
312
  applyWrapped: apply,
99
313
  property: 'generatorp',
100
- })
314
+ }
315
+ if (addFirst) {
316
+ config.config.generators.unshift(generator)
317
+ } else {
318
+ config.config.generators.push(generator)
319
+ }
320
+
101
321
  }
102
322
  if (bridge.generatorr) {
103
323
  const match = bridge.generatorr.match || (() => true)
104
324
  const apply = typeof bridge.generatorr == 'function' ? bridge.generatorr : bridge.generatorr.apply || bridge.generatorr
105
325
  const level = bridge.generatorr.level >= 0 ? bridge.generatorr.level : bridge.level + 1
106
- config.config.generators.unshift({
326
+ const generator = {
107
327
  where: bridge.generatorr.where || bridge.where || client.where(4),
108
328
  match: (args) => bridge.id == args.context.marker && args.context.level == level && !args.context.paraphrase && (args.context.response || args.context.isResponse) && match(args),
109
329
  apply: (args) => apply(args),
110
330
  applyWrapped: apply,
111
331
  property: 'generatorr',
112
- })
113
- }
114
- /*
115
- if (bridge.generatorr) {
116
- config.config.generators.unshift({
117
- // TODO merge response and isResponse
118
- where: bridge.generatorr.where || bridge.where || client.where(3),
119
- match: ({context}) => bridge.id == context.marker && !context.paraphrase && (context.response || context.isResponse),
120
- apply: (args) => bridge.generatorr(args),
121
- applyWrapped: bridge.generatorr,
122
- property: 'generatorr',
123
- })
332
+ }
333
+ if (addFirst) {
334
+ config.config.generators.unshift(generator)
335
+ } else {
336
+ config.config.generators.push(generator)
337
+ }
124
338
  }
125
- */
126
339
  if (bridge.evaluator) {
127
- config.config.semantics.unshift({
340
+ const semantic = {
128
341
  where: bridge.evaluator.where || bridge.where || client.where(3),
129
342
  match: ({context}) => bridge.id == context.marker && context.evaluate,
130
343
  apply: (args) => bridge.evaluator(args),
131
344
  applyWrapped: bridge.evaluator,
132
345
  property: 'evaluator',
133
- })
346
+ }
347
+ if (addFirst) {
348
+ config.config.semantics.unshift(semantic)
349
+ } else {
350
+ config.config.semantics.push(semantic)
351
+ }
134
352
  }
135
353
  if (bridge.semantic) {
136
- config.config.semantics.unshift({
354
+ const semantic = {
137
355
  where: bridge.semantic.where || bridge.where || client.where(3),
138
356
  match: ({context}) => bridge.id == context.marker,
139
357
  apply: (args) => bridge.semantic(args),
140
358
  applyWrapped: bridge.semantic,
141
359
  property: 'semantic',
142
- })
360
+ }
361
+ if (addFirst) {
362
+ config.config.semantics.unshift(semantic)
363
+ } else {
364
+ config.config.semantics.push(semantic)
365
+ }
143
366
  }
144
367
  }
145
368
  )
146
369
  }
147
370
 
148
- const handleCalculatedProps = (baseConfig, moreConfig) => {
149
- for (let bridge of moreConfig.bridges) {
371
+ const handleCalculatedProps = (baseConfig, moreConfig, addFirst) => {
372
+ for (let bridge of (moreConfig.bridges || [])) {
150
373
  const valid = [ 'after', 'before', 'bridge', 'development', 'evaluator', 'generatorp', 'generatorr', 'generatorpr', 'generators', 'id', 'convolution', 'inverted', 'isA', 'children', 'parents',
151
- 'level', 'optional', 'selector', 'semantic', 'words', /Bridge$/, 'localHierarchy', 'levelSpecificHierarchy', 'where' ]
374
+ 'level', 'optional', 'selector', 'semantic', 'words', /Bridge$/, 'localHierarchy', 'levelSpecificHierarchy', 'where', 'uuid' ]
152
375
  helpers.validProps(valid, bridge, 'bridge')
153
- handleBridgeProps(baseConfig, bridge)
376
+ handleBridgeProps(baseConfig, bridge, addFirst)
154
377
  }
155
378
  if (moreConfig.operators) {
156
379
  moreConfig.operators = moreConfig.operators.map((operator) => {
@@ -171,6 +394,10 @@ if (runtime.process.env.DEBUG_PRIORITY) {
171
394
  global.entodictonDebugPriority = JSON.parse(runtime.process.env.DEBUG_PRIORITY)
172
395
  }
173
396
 
397
+ if (runtime.process.env.DEBUG_CONTEXTUAL_PRIORITY) {
398
+ global.entodictonDebugContextualPriority = JSON.parse(runtime.process.env.DEBUG_CONTEXTUAL_PRIORITY)
399
+ }
400
+
174
401
  if (runtime.process.env.DEBUG_ASSOCIATION) {
175
402
  global.entodictonDebugAssociation = JSON.parse(runtime.process.env.DEBUG_ASSOCIATION)
176
403
  }
@@ -252,6 +479,14 @@ const normalizeConfig = (config) => {
252
479
  }
253
480
  }
254
481
  }
482
+
483
+ if (config.semantics) {
484
+ for (let semantic of config.semantics) {
485
+ if (semantic.oneShot) {
486
+ semantic.id = uuid()
487
+ }
488
+ }
489
+ }
255
490
  }
256
491
  }
257
492
 
@@ -459,8 +694,8 @@ const multiApiImpl = (initializer) => {
459
694
  initializer(config, api)
460
695
  const name = api.getName()
461
696
  multiApi.apis[name] = api
462
- api.objects = config.get('objects')
463
- api.config = () => config
697
+ // api.objects = config.get('objects')
698
+ // api.config = () => config
464
699
  multiApi.current = name
465
700
  },
466
701
 
@@ -471,11 +706,13 @@ const multiApiImpl = (initializer) => {
471
706
  }
472
707
  },
473
708
 
709
+ /*
474
710
  set objects (value) {
475
711
  for (const key in Object.keys(this.apis)) {
476
712
  this.apis[key].objects = value
477
713
  }
478
714
  },
715
+ */
479
716
 
480
717
  // "product1": apiInstance(testData1),
481
718
  apis: {
@@ -489,6 +726,46 @@ const multiApiImpl = (initializer) => {
489
726
 
490
727
  class Config {
491
728
 
729
+ toServer (config) {
730
+ return config_toServer(config)
731
+ }
732
+
733
+ base () {
734
+ const base = new Config()
735
+ for (let km of this.configs.reverse()) {
736
+ if (km.isSelf) {
737
+ continue
738
+ }
739
+ base.add(km.config)
740
+ }
741
+ return base
742
+ }
743
+
744
+ getPseudoConfig (uuid, config) {
745
+ return {
746
+ description: "this is a pseudo config that has limited functionality due to being available in the initializer function context",
747
+ addAssociation: (...args) => this.addAssociation(...args),
748
+ addAssociations: (...args) => this.addAssociations(...args),
749
+ addBridge: (...args) => this.addBridge(...args, uuid),
750
+ addContextualPriority: (...args) => this.addContextualPriority(...args),
751
+ addGenerator: (...args) => this.addGenerator(...args, uuid, config.name),
752
+ addHierarchy: (...args) => this.addHierarchy(...args),
753
+ addOperator: (...args) => this.addOperator(...args, uuid),
754
+ addPriorities: (...args) => this.addPriorities(...args),
755
+ addSemantic: (...args) => this.addSemantic(...args, uuid, config.name),
756
+ removeSemantic: (...args) => this.removeSemantic(...args, uuid, config.name),
757
+ addWord: (...args) => this.addWord(...args, uuid),
758
+
759
+ getHierarchy: (...args) => this.config.hierarchy,
760
+ getBridges: (...args) => this.config.bridges,
761
+
762
+ addArgs: (...args) => this.addArgs(...args),
763
+ getBridge: (...args) => this.getBridge(...args),
764
+ fragment: (...args) => this.fragment(...args),
765
+ addAPI: (...args) => this.addAPI(...args),
766
+ }
767
+ }
768
+
492
769
  inDevelopmentMode (call) {
493
770
  config.developmentModeOn += 1
494
771
  try {
@@ -527,9 +804,6 @@ class Config {
527
804
  }
528
805
 
529
806
  setTestConfig(testConfig) {
530
- if (this.name == 'ui') {
531
- console.log('ui setting testConfig')
532
- }
533
807
  this.testConfig = testConfig
534
808
  }
535
809
 
@@ -548,6 +822,7 @@ class Config {
548
822
  },
549
823
  eqClasses: [],
550
824
  priorities: [], // Done
825
+ contextual_priorities: [],
551
826
  version: '3',
552
827
  debug: false,
553
828
  associations: { // Done
@@ -586,6 +861,7 @@ class Config {
586
861
  'namespaces',
587
862
  'eqClasses',
588
863
  'priorities',
864
+ 'contextual_priorities',
589
865
  'associations',
590
866
  'words',
591
867
  'floaters',
@@ -709,7 +985,8 @@ class Config {
709
985
  }
710
986
  }
711
987
 
712
- needsRebuild(template, instance, options = { rebuild: false }) {
988
+ // { rebuild: false, isModule: false }
989
+ needsRebuild(template, instance, options) {
713
990
  if (options.rebuild) {
714
991
  return true
715
992
  }
@@ -723,7 +1000,41 @@ class Config {
723
1000
  const instanceFragments = (instance.fragments || []).map((fragment) => fragment.key || fragment.query).map( toCanonical )
724
1001
  const templateFragments = (template.fragments || []).concat(this.dynamicFragments).map( toCanonical )
725
1002
  const sameFragments = helpers.safeEquals(templateFragments, instanceFragments)
726
- const sameQueries = helpers.safeEquals((template.queries || []).map(helpers.updateQueries), (instance.queries || []))
1003
+ const toCanonicalQuery = (queryOrConfig) => {
1004
+ if (typeof queryOrConfig == 'string') {
1005
+ const query = queryOrConfig
1006
+ return query
1007
+ } else {
1008
+ const config = { ...queryOrConfig }
1009
+ delete config.where
1010
+ if (options.isModule) {
1011
+ // things like webpack rewrite the functions if there are constants so this compare does not work
1012
+ delete config.generators
1013
+ delete config.semantics
1014
+ } else {
1015
+ config.generators = (config.generators || []).map((generator) => {
1016
+ generator = {...generator}
1017
+ delete generator.where
1018
+ generator.match = generator.match.toString()
1019
+ generator.apply = generator.apply.toString()
1020
+ return generator
1021
+ })
1022
+ config.semantics = (config.semantics || []).map((semantic) => {
1023
+ semantic = {...semantic}
1024
+ delete semantic.where
1025
+ semantic.match = semantic.match.toString()
1026
+ semantic.apply = semantic.apply.toString()
1027
+ return semantic
1028
+ })
1029
+ }
1030
+ return config
1031
+ }
1032
+ }
1033
+ const toCanonicalQueries = (elements) => {
1034
+ return elements.map( toCanonicalQuery )
1035
+ }
1036
+
1037
+ const sameQueries = helpers.safeEquals(toCanonicalQueries(template.queries || []).map(helpers.updateQueries), toCanonicalQueries(instance.queries || []))
727
1038
  return !(instance && sameQueries && sameFragments)
728
1039
  }
729
1040
 
@@ -739,6 +1050,12 @@ class Config {
739
1050
  }
740
1051
  }
741
1052
 
1053
+ toData (data) {
1054
+ Object.assign(data, this.config)
1055
+ config_toServer(data)
1056
+ }
1057
+
1058
+ // loadTemplate
742
1059
  load (template, instance, options = { rebuild: false } ) {
743
1060
  this.validifyTemplate(template)
744
1061
  instance.template = template
@@ -750,7 +1067,7 @@ class Config {
750
1067
  // TODO fix beforeQuery
751
1068
  template = { fragments: [], queries: [], ...template }
752
1069
  template.fragments = template.fragments.concat(this.dynamicFragments)
753
- client.build({ config: this, target: this.name, beforeQuery: () => {}, template, ...options })
1070
+ client.rebuildTemplate({ config: this, target: this.name, beforeQuery: () => {}, template, ...options })
754
1071
  } else {
755
1072
  // no change
756
1073
  // this.initInstances.push({ ...instance, name: config.name })
@@ -768,7 +1085,7 @@ class Config {
768
1085
  instance.name = this.name
769
1086
  this.initInstances.push(instance)
770
1087
  this.instances.push(instance)
771
- client.processInstance(this, instance)
1088
+ client.loadInstance(this, instance)
772
1089
  }
773
1090
  }
774
1091
  }
@@ -795,6 +1112,9 @@ class Config {
795
1112
  }
796
1113
  }
797
1114
 
1115
+ debugConfig() {
1116
+ }
1117
+
798
1118
  addAssociation (association) {
799
1119
  if (!this.config.associations) {
800
1120
  this.config.associations = {
@@ -802,29 +1122,34 @@ class Config {
802
1122
  positive: []
803
1123
  }
804
1124
  }
805
- if (global.entodictonDebugAssociation) {
806
- if (helpers.safeEquals(global.entodictonDebugAssociation, association)) {
807
- debugger; // debug association hit
808
- }
809
- }
1125
+ debugAssociation(association)
810
1126
  this.config.associations.positive.push(association)
811
1127
  this._delta.json.associations.push({ action: 'add', association })
812
1128
  }
813
1129
 
814
1130
  // TODO add more error checking to these like addHierarchy has
1131
+ // TODO change name from priorities to priority
815
1132
  addPriorities (priorities) {
816
1133
  if (!this.config.priorities) {
817
1134
  this.config.priorities = []
818
1135
  }
819
- if (global.entodictonDebugPriority) {
820
- if (helpers.safeEquals(entodictonDebugPriority, priorities)) {
821
- debugger; // debug hierarchy hit
822
- }
823
- }
1136
+ debugPriority(priorities)
824
1137
  this.config.priorities.push(priorities)
825
1138
  this._delta.json.priorities.push({ action: 'add', priorities })
826
1139
  }
827
1140
 
1141
+ // [ operators: <list of [id, level]>, select: <index of prioritized operator> ]
1142
+ addContextualPriority(contextual_priority) {
1143
+ if (!this.config.contextual_priorities) {
1144
+ this.config.contextual_priorities = []
1145
+ }
1146
+ debugContextualPriority(contextual_priority)
1147
+ contextual_priority_valid(contextual_priority)
1148
+ this.config.contextual_priorities.push(contextual_priority)
1149
+ const cpServer = contextual_priorities_toServer(contextual_priority)
1150
+ this._delta.json.contextual_priorities.push({ action: 'add', contextual_priority: cpServer })
1151
+ }
1152
+
828
1153
  addHierarchy (child, parent) {
829
1154
  if (child && parent || !child || Array.isArray(child) || (typeof child == 'string' && !parent)) {
830
1155
  this.addHierarchyChildParent(child, parent)
@@ -842,11 +1167,7 @@ class Config {
842
1167
  if (typeof parent !== 'string') {
843
1168
  throw new Error(`addHierarchy expected parent property to be a string. got ${JSON.stringify(parent)}`)
844
1169
  }
845
- if (global.entodictonDebugHierarchy) {
846
- if (helpers.safeEquals(entodictonDebugHierarchy, [child, parent])) {
847
- debugger; // debug hierarchy hit
848
- }
849
- }
1170
+ debugHierarchy([child, parent])
850
1171
  this.config.hierarchy.push(edge)
851
1172
  // TODO greg11 this.hierarchy.addEdge(edge)
852
1173
  this._delta.json.hierarchy.push([child, parent])
@@ -859,13 +1180,7 @@ class Config {
859
1180
  if (typeof parent !== 'string') {
860
1181
  throw new Error(`addHierarchy expected parent to be a string. got ${JSON.stringify(parent)}`)
861
1182
  }
862
-
863
- if (global.entodictonDebugHierarchy) {
864
- if (helpers.safeEquals(global.entodictonDebugHierarchy, [child, parent])) {
865
- debugger; // debug hierarchy hit
866
- }
867
- }
868
-
1183
+ debugHierarchy([child, parent])
869
1184
  if (this.config.hierarchy.find( (element) => {
870
1185
  const hc = hierarchyCanonical(element)
871
1186
  if (child == hc.child && parent == hc.parent) {
@@ -881,22 +1196,21 @@ class Config {
881
1196
  }
882
1197
 
883
1198
  getBridge (id, level) {
884
- return this.config.bridges.find( (bridge) => bridge.id == id && bridge.level == level )
1199
+ if (level) {
1200
+ return this.config.bridges.find( (bridge) => bridge.id == id && bridge.level == level )
1201
+ } else {
1202
+ return this.config.bridges.find( (bridge) => bridge.id == id)
1203
+ }
885
1204
  }
886
1205
 
887
- addBridge (bridge) {
1206
+ addBridge (bridge, uuid) {
888
1207
  if (!this.config.bridges) {
889
1208
  this.config.bridges = []
890
1209
  }
891
1210
  const bridges = this.config.bridges
892
- const def = Object.assign({}, bridge, { uuid: this._uuid })
893
-
894
- if (global.entodictonDebugBridge) {
895
- if (global.entodictonDebugBridge[0] == bridge.id && global.entodictonDebugBridge[1] == bridge.level) {
896
- debugger; // debug hierarchy hit
897
- }
898
- }
899
-
1211
+ const def = Object.assign({}, bridge, { uuid: uuid || this._uuid })
1212
+
1213
+ debugBridge(bridge)
900
1214
  if (bridge.allowDups) {
901
1215
  if (bridges.find( (b) => b.id == bridge.id && b.level == bridge.level && b.bridge == bridge.bridge )) {
902
1216
  return;
@@ -911,12 +1225,7 @@ class Config {
911
1225
  this._delta.json.bridges.push({ action: 'add', bridge: def })
912
1226
  }
913
1227
 
914
- addGenerator (match, apply) {
915
- let generator = match
916
- if ((typeof match === 'function') && (typeof apply === 'function')) {
917
- generator = { match, apply }
918
- }
919
-
1228
+ addGenerator (generator, uuid, name) {
920
1229
  if (!(typeof generator.match === 'function')) {
921
1230
  throw new Error('addGenerator: Expected matcher to be a function')
922
1231
  }
@@ -933,20 +1242,16 @@ class Config {
933
1242
  }
934
1243
 
935
1244
  const generators = this.config.generators
936
- Object.assign(generator, { uuid: this._uuid, km: this.name, index: generators.length })
1245
+ Object.assign(generator, { uuid: uuid || this._uuid, km: name || this.name, index: generators.length })
937
1246
  // used to be unshift
938
1247
  generators.unshift(generator)
939
1248
  }
940
1249
 
941
- addSemantic (match, apply) {
942
- let semantic = match
943
- if ((typeof match === 'function') && (typeof apply === 'function')) {
944
- semantic = { match, apply }
945
- }
946
-
1250
+ addSemantic (semantic, uuid, name) {
947
1251
  if (!(typeof semantic.match === 'function')) {
948
1252
  throw new Error('addSemantic: Expected match to be a function')
949
1253
  }
1254
+
950
1255
  if (!(typeof semantic.apply === 'function')) {
951
1256
  throw new Error('addSemantic: Expected apply to be a function')
952
1257
  }
@@ -960,11 +1265,18 @@ class Config {
960
1265
  }
961
1266
 
962
1267
  const semantics = this.config.semantics
963
- Object.assign(semantic, { uuid: this._uuid, km: this.name, index: semantics.length })
1268
+ Object.assign(semantic, { uuid: uuid || semantic.uuid || this._uuid, km: name || this.name, index: semantics.length, id: semantic.id || uuidv4() })
964
1269
  semantics.unshift(semantic)
965
1270
  }
966
1271
 
967
- addOperator (objectOrPattern) {
1272
+ removeSemantic(deleteSemantic) {
1273
+ const index = this.config.semantics.findIndex( (semantic) => semantic.id === deleteSemantic.id )
1274
+ if (index >= 0) {
1275
+ this.config.semantics.splice(index, 1)
1276
+ }
1277
+ }
1278
+
1279
+ addOperator (objectOrPattern, uuid) {
968
1280
  if (!this.config.operators) {
969
1281
  this.config.operators = []
970
1282
  }
@@ -973,16 +1285,12 @@ class Config {
973
1285
 
974
1286
  let operator;
975
1287
  if (typeof objectOrPattern === 'string') {
976
- operator = { pattern: objectOrPattern, uuid: this._uuid }
1288
+ operator = { pattern: objectOrPattern, uuid: uuid || this._uuid }
977
1289
  } else {
978
- operator = Object.assign({}, objectOrPattern, { uuid: this._uuid })
1290
+ operator = Object.assign({}, objectOrPattern, { uuid: uuid || this._uuid })
979
1291
  }
980
1292
 
981
- if (global.entodictonDebugOperator) {
982
- if (operator.pattern === global.entodictonDebugOperator) {
983
- debugger; // debug operator hit
984
- }
985
- }
1293
+ debugOperator(operator)
986
1294
 
987
1295
  if (operator.allowDups) {
988
1296
  if (operators.find( (o) => o.pattern == operator.pattern )) {
@@ -996,16 +1304,16 @@ class Config {
996
1304
  this._delta.json.operators.push({ action: 'add', operator })
997
1305
  }
998
1306
 
999
- addWord (word, def) {
1000
- this.addWordInternal(word, def)
1307
+ addWord (word, def, uuid) {
1308
+ this.addWordInternal(word, def, uuid)
1001
1309
  }
1002
1310
 
1003
- addWordInternal (word, def) {
1311
+ addWordInternal (word, def, uuid) {
1004
1312
  if (!this.config.words) {
1005
1313
  this.config.words = {}
1006
1314
  }
1007
1315
  const words = this.config.words
1008
- def = Object.assign({}, def, { uuid: this._uuid })
1316
+ def = Object.assign({}, def, { uuid: uuid || this._uuid })
1009
1317
  if (words[word]) {
1010
1318
  if (!words[word].some((e) => helpers.safeEquals(e, def))) {
1011
1319
  words[word].unshift(def)
@@ -1226,33 +1534,7 @@ class Config {
1226
1534
  }
1227
1535
 
1228
1536
  if (config) {
1229
- const valid = [
1230
- 'hierarchy',
1231
- 'objects',
1232
- 'bridges',
1233
- 'operators',
1234
- 'words',
1235
- 'priorities',
1236
- 'associations',
1237
- 'name',
1238
- 'version',
1239
- 'generators',
1240
- 'semantics',
1241
- 'floaters',
1242
- 'debug',
1243
-
1244
- // TODO Fix these from the test app
1245
- 'implicits',
1246
- 'convolution',
1247
- 'expected_generated',
1248
- 'expected_results',
1249
- 'skipSemantics',
1250
- 'description',
1251
- 'contexts',
1252
- 'utterances',
1253
- 'flatten',
1254
- ]
1255
- helpers.validProps(valid, config, 'config')
1537
+ validConfigProps(config)
1256
1538
 
1257
1539
  config.operators = config.operators || []
1258
1540
  config.bridges = config.bridges || []
@@ -1262,6 +1544,7 @@ class Config {
1262
1544
  config.hierarchy = config.hierarchy || []
1263
1545
  config.associations = config.associations || { negative: [], positive: [] }
1264
1546
  config.priorities = config.priorities || []
1547
+ config.contextual_priorities = config.contextual_priorities || []
1265
1548
  }
1266
1549
 
1267
1550
  this.maxDepth = 20 // for generators and semantics
@@ -1315,6 +1598,10 @@ class Config {
1315
1598
  }
1316
1599
  }
1317
1600
 
1601
+ if (config && config.contextual_priorities) {
1602
+ contextual_priorities_valid(config.contextual_priorities)
1603
+ }
1604
+
1318
1605
  normalizeConfig(config)
1319
1606
 
1320
1607
  // set the default server so stuff just works
@@ -1330,7 +1617,6 @@ class Config {
1330
1617
  if (config) {
1331
1618
  this.name = config.name
1332
1619
  }
1333
- this.motivations = []
1334
1620
  this.loadOrder = new DigraphInternal()
1335
1621
  this.wasInitialized = false
1336
1622
  this.configs = []
@@ -1385,7 +1671,10 @@ class Config {
1385
1671
  }
1386
1672
 
1387
1673
  delta () {
1388
- return { cacheKey: this._delta.cacheKey, json: this._delta.json }
1674
+ return {
1675
+ cacheKey: this._delta.cacheKey,
1676
+ json: this._delta.json
1677
+ }
1389
1678
  }
1390
1679
 
1391
1680
  resetDelta (cacheKey) {
@@ -1397,6 +1686,7 @@ class Config {
1397
1686
  bridges: [],
1398
1687
  associations: [],
1399
1688
  priorities: [],
1689
+ contextual_priorities: [],
1400
1690
  hierarchy: [],
1401
1691
  }
1402
1692
  }
@@ -1431,11 +1721,13 @@ class Config {
1431
1721
  this._api.add(this, this._api, value)
1432
1722
  } else {
1433
1723
  this._api = _.cloneDeep(value)
1724
+ /*
1434
1725
  if (this._api) {
1435
- this._api.objects = this.config.objects
1436
- this._api.config = () => this
1437
- this._api.uuid = this._uuid
1726
+ // this._api.objects = this.config.objects
1727
+ // this._api.config = () => this
1728
+ // this._api.uuid = this._uuid
1438
1729
  }
1730
+ */
1439
1731
  this.rebuild()
1440
1732
  }
1441
1733
  }
@@ -1478,40 +1770,6 @@ class Config {
1478
1770
  // this.valid() init was not run because the kms are not all setup yet
1479
1771
  }
1480
1772
 
1481
- // motivation === { match, apply, uuid }
1482
- addMotivation (motivation) {
1483
- if (!motivation.uuid) {
1484
- motivation.uuid = this.uuid
1485
- }
1486
- this.motivations.push(motivation)
1487
- }
1488
-
1489
- resetMotivations () {
1490
- this.motivations = []
1491
- }
1492
-
1493
- doMotivations (args, context) {
1494
- args = Object.assign({}, args, { context, api: this.api })
1495
- // console.log('src/config doMotivations this.uuid', this.uuid)
1496
- // args.objects = args.getObjects(this.uuid)
1497
- const motivations = this.motivations
1498
- this.motivations = []
1499
- let done = false
1500
- for (const motivation of motivations) {
1501
- args.objects = args.getObjects(motivation.uuid)
1502
- if (!done && motivation.match(args)) {
1503
- motivation.apply(args)
1504
- if (args.context.controlKeepMotivation || motivation.repeat) {
1505
- this.motivations.push(motivation)
1506
- }
1507
- done = true
1508
- } else {
1509
- this.motivations.push(motivation)
1510
- }
1511
- }
1512
- return done
1513
- }
1514
-
1515
1773
  // TODO add more details
1516
1774
  equal(config) {
1517
1775
  if (JSON.stringify(this.config) != JSON.stringify(config.config)) {
@@ -1525,6 +1783,9 @@ class Config {
1525
1783
  runtime.fs.writeFileSync(fn, JSON.stringify(this.config, 0, 2))
1526
1784
  }
1527
1785
 
1786
+ copy (options = { callInitializers: true }) {
1787
+ }
1788
+
1528
1789
  copy (options = { callInitializers: true }) {
1529
1790
  this.valid()
1530
1791
  const cp = new Config()
@@ -1534,15 +1795,14 @@ class Config {
1534
1795
  cp.transitoryMode = this.transitoryMode
1535
1796
  cp.configs = this.configs.map((km) => km.copy2(Object.assign({}, options, { getCounter: (name) => cp.getCounter(name), callInitializers: false })))
1536
1797
  cp._uuid = cp.configs[0]._uuid
1798
+ // update uuid here set the uuid in the objects and add error checking
1537
1799
  cp.initializerFn = this.initializerFn
1538
- cp.initAfterApi = this.initAfterApi
1539
1800
  cp._api = _.cloneDeep(this._api)
1540
1801
  cp._namespace = this._namespace
1541
1802
  cp._eqClasses = this._eqClasses
1542
1803
  cp.name = this.name
1543
1804
  cp.description = this.description
1544
1805
  cp.tests = this.tests
1545
- cp.motivations = [...this.motivations]
1546
1806
  cp.isModule = this.isModule
1547
1807
  cp.loadedForTesting = this.loadedForTesting
1548
1808
  cp.initInstances = this.initInstances.slice()
@@ -1567,25 +1827,38 @@ class Config {
1567
1827
  }
1568
1828
  cp.mapUUIDs(map)
1569
1829
 
1830
+ if (cp._uuid == 'concept2') {
1831
+ // debugger
1832
+ }
1570
1833
  if (options.callInitializers) {
1571
1834
  cp.rebuild(options)
1572
- }
1573
- if (cp._api) {
1574
- cp._api.objects = cp.config.objects
1575
- cp._api.config = () => (cp instanceof Config) ? cp : cp.config
1576
- cp._api.uuid = cp._uuid
1577
- }
1835
+ } else {
1836
+ // this mess is for duplicate into a KM after resetToOne was called
1837
+ /*
1838
+ if (cp._api) {
1839
+ // cp._api.objects = cp.config.objects
1840
+ // cp._api.config = () => (cp instanceof Config) ? cp : cp.config
1841
+ // cp._api.uuid = cp._uuid
1842
+ }
1843
+ */
1578
1844
 
1579
- if (!cp.config.objects) {
1580
- cp.config.objects = { namespaced: {} }
1581
- } else if (!cp.config.objects.namespaced) {
1582
- cp.config.objects.namespaced = {}
1845
+ if (!cp.config.objects) {
1846
+ cp.config.objects = { namespaced: {} }
1847
+ } else if (!cp.config.objects.namespaced) {
1848
+ cp.config.objects.namespaced = {}
1849
+ }
1850
+ cp.configs.forEach((km) => {
1851
+ // const namespace = km.namespace
1852
+ cp.config.objects.namespaced[km._uuid] = {}
1853
+ })
1854
+ /*
1855
+ if (cp._uuid == 'concept2') {
1856
+ if (!cp.api.objects.defaultTypesForHierarchy) {
1857
+ debugger
1858
+ }
1859
+ }
1860
+ */
1583
1861
  }
1584
- cp.configs.forEach((km) => {
1585
- // const namespace = km.namespace
1586
- cp.config.objects.namespaced[km._uuid] = {}
1587
- })
1588
-
1589
1862
  cp.valid()
1590
1863
  return cp
1591
1864
  }
@@ -1685,38 +1958,33 @@ class Config {
1685
1958
  }
1686
1959
  */
1687
1960
  const objects = {}
1688
- const km = (name) => this.getConfig(name)
1689
1961
  if (config instanceof Config) {
1690
- // const aw = addWord(this.config, config.uuid)
1691
- const aw = (word, def) => this.addWord(word, def)
1692
1962
  this.get('objects').namespaced[config._uuid] = objects
1963
+ /*
1693
1964
  if (config._api) {
1694
- config._api.objects = objects
1695
- config._api.config = () => this
1965
+ // config._api.objects = objects
1966
+ // config._api.config = () => this
1696
1967
  }
1697
- config.initializerFn({ addWord: aw, km, config, baseConfig: this, currentConfig: config, objects, namespace, uuid, api: config.api })
1968
+ */
1969
+ config.initializerFn(setupInitializerFNArgs(this, { testConfig: config, currentConfig: config, objects, namespace, uuid }))
1698
1970
  } else {
1699
- // const aw = addWord(this.config, this.uuid)
1700
- const aw = (word, def) => this.addWord(word, def)
1701
1971
  this.get('objects').namespaced[this._uuid] = objects
1972
+ /*
1702
1973
  if (config._api) {
1703
- config._api.objects = objects
1704
- config._api.config = () => this
1974
+ // config._api.objects = objects
1975
+ // config._api.config = () => this
1705
1976
  }
1706
- this.initializerFn({ addWord: aw, km, config: this, baseConfig: this, currentConfig: this, objects, namespace, uuid, api: this.api })
1977
+ */
1978
+ this.initializerFn(setupInitializerFNArgs(this, { testConfig: this, currentConfig: this, objects, namespace, uuid }))
1707
1979
  }
1708
1980
  })
1709
- this.instances.forEach((instance) => client.processInstance(this, instance))
1981
+ this.instances.forEach((instance) => client.loadInstance(this, instance))
1710
1982
  }
1711
1983
 
1712
1984
  initialize ({ force = true } = {}) {
1713
1985
  if (force || !this.wasInitialized) {
1714
- // const aw = addWord(this.config, this.uuid)
1715
- const aw = (word, def) => this.addWord(word, def)
1716
- const km = (name) => this.getConfig(name)
1717
- // this.initializerFn({ addWord: aw, km, config: this, baseConfig: this, currentConfig: this, objects: this.get('objects'), uuid: this._uuid, namespace: '', api: this.api })
1718
1986
  const objects = this.config.objects.namespaced[this._uuid]
1719
- this.initializerFn({ addWord: aw, km, config: this, baseConfig: this, currentConfig: this, objects, uuid: this._uuid, namespace: '', api: this.api })
1987
+ this.initializerFn(setupInitializerFNArgs(this, { testConfig: this, currentConfig: this, objects, uuid: this._uuid, namespace: '' }))
1720
1988
  this.wasInitialized = true
1721
1989
  }
1722
1990
  }
@@ -1724,29 +1992,22 @@ class Config {
1724
1992
  initializer (fn, options = {}) {
1725
1993
  if (options) {
1726
1994
  for (let option of Object.keys(options)) {
1727
- const validOptions = ['initAfterApi']
1728
- if (!['initAfterApi'].includes(option)) {
1995
+ const validOptions = []
1996
+ if (!validOptions.includes(option)) {
1729
1997
  throw new Error(`For Config.initializer, unrecognized option ${option}. The valid options are ${validOptions}`)
1730
1998
  }
1731
1999
  }
1732
2000
  }
1733
- const { initAfterApi = false } = options;
1734
2001
  this.wasInitialized = false
1735
- this.initAfterApi = initAfterApi
1736
- this.initializerFn = (args) => {
2002
+ this.initializerFn = (args, { dontCallFn } = {}) => {
1737
2003
  const transitoryMode = global.transitoryMode
1738
2004
  global.transitoryMode = false
1739
2005
  // const baseConfig = args.baseConfig
1740
2006
  const currentConfig = args.currentConfig
1741
2007
 
1742
- if (currentConfig.api) {
1743
- currentConfig.api.objects = args.objects
1744
- // GREG42 currentConfig.api.config = () => this
1745
- currentConfig.api.config = () => args.baseConfig
1746
- currentConfig.api.uuid = currentConfig._uuid
2008
+ if (args.isAfterApi) {
2009
+ fn(args)
1747
2010
  }
1748
- // this.instances.forEach( (instance) => client.processInstance(this, instance) )
1749
- fn(args)
1750
2011
  currentConfig.wasInitialized = true
1751
2012
  global.transitoryMode = transitoryMode
1752
2013
  }
@@ -1913,7 +2174,6 @@ class Config {
1913
2174
  }
1914
2175
  this.config.objects.namespaced = {}
1915
2176
  this.resetWasInitialized()
1916
- this.resetMotivations()
1917
2177
 
1918
2178
  // reorder configs base on load ordering
1919
2179
  {
@@ -1939,8 +2199,8 @@ class Config {
1939
2199
  this.config.objects.namespaced[km._uuid] = {}
1940
2200
  const namespacedObjects = this.config.objects.namespaced[km._uuid]
1941
2201
  this.setupNamespace(km)
1942
- // const aw = addWord(km.config.config ? km.config.config : km.config, km.config.uuid)
1943
- const aw = (word, def) => this.addWord(word, def)
2202
+ // const aw = (word, def) => this.addWord(word, def)
2203
+ // const ag = (matchOrGenerator, applyOrNothing) => this.addGenerator(matchOrGenerator, applyOrNothing)
1944
2204
  let config = km.config
1945
2205
 
1946
2206
  if (config.addedArgss) {
@@ -1961,45 +2221,51 @@ class Config {
1961
2221
  }
1962
2222
  config.wasInitialized = false
1963
2223
  // TODO change name of config: to baseConfig:
1964
- const kmFn = (name) => this.getConfig(name)
2224
+ const kmFn = (name) => {
2225
+ const config = this.getConfig(name)
2226
+ return config
2227
+ }
1965
2228
  // const hierarchy = new DigraphInternal((config.config || {}).hierarchy)
1966
- const args = {
2229
+ const args = new Object(setupInitializerFNArgs(this, {
1967
2230
  isModule,
1968
- addWord: aw,
1969
- km: kmFn,
1970
2231
  hierarchy: this.hierarchy,
1971
- config,
1972
- baseConfig: this,
2232
+ testConfig: config,
1973
2233
  currentConfig: config,
1974
2234
  uuid: config._uuid,
1975
2235
  objects: namespacedObjects,
1976
2236
  namespace,
1977
- motivation: (m) => config.addMotivation(m),
1978
- api: config.api
2237
+ api: config.api,
2238
+ }))
2239
+
2240
+ const currentConfig = args.currentConfig
2241
+
2242
+ /*
2243
+ if (args.currentConfig.api) {
2244
+ // args.currentConfig.api.objects = args.objects
2245
+ // TODO assign pseudo config?
2246
+ // args.currentConfig.api.config = () => args.baseConfig
2247
+ // args.currentConfig.api.uuid = args.currentConfig._uuid
2248
+ args.currentConfig.wasInitialized = true
1979
2249
  }
1980
- config.initializerFn(args)
1981
- if (config.initAfterApi) {
1982
- // reverse the list
2250
+ */
2251
+ // debugger
2252
+ // greg55
2253
+ config.initializerFn(args, { dontCallFn: true })
1983
2254
  initAfterApis.unshift({ config, args })
1984
- } else {
1985
- if (interleaved) {
1986
- initAfterApis.unshift(null)
1987
- }
1988
- }
1989
- // greg
1990
2255
  if (config._api) {
1991
2256
  if (config._api.initialize) {
1992
2257
  // reverse the list
1993
- inits.unshift( () => config._api.initialize({ config: this, km: kmFn, api: config._api }) )
2258
+ // TODO sync up the args with initialize of config
2259
+ inits.unshift( () => config._api.initialize({ config: this, km: kmFn, ...args, api: config._api }) )
1994
2260
  // config._api.initialize({ config, api: config._api })
1995
2261
  } else {
1996
2262
  if (interleaved) {
1997
2263
  inits.unshift(null)
1998
2264
  }
1999
2265
  }
2000
- config._api.objects = namespacedObjects
2001
- config._api.config = () => this
2002
- config._api.uuid = config._uuid
2266
+ // config._api.objects = namespacedObjects
2267
+ // config._api.config = () => this
2268
+ // config._api.uuid = config._uuid
2003
2269
  } else {
2004
2270
  if (interleaved) {
2005
2271
  inits.unshift(null)
@@ -2041,16 +2307,16 @@ class Config {
2041
2307
  init()
2042
2308
  }
2043
2309
  for (let init of initAfterApis) {
2044
- // init.args.isAfterApi = true
2045
2310
  init.config.initializerFn({ ...init.args, kms: this.getConfigs(), isAfterApi: true })
2046
2311
  }
2047
- this.instances.forEach((instance) => client.processInstance(this, instance))
2312
+ this.instances.forEach((instance) => client.loadInstance(this, instance))
2048
2313
  } else {
2049
2314
  const base = {
2050
2315
  operators: this.config.operators,
2051
2316
  bridges: this.config.bridges,
2052
2317
  hierarchy: this.config.hierarchy,
2053
2318
  priorities: this.config.priorities,
2319
+ contextual_priorities: this.config.contextual_priorities,
2054
2320
  associations: this.config.associations,
2055
2321
  words: this.config.words
2056
2322
  }
@@ -2059,6 +2325,7 @@ class Config {
2059
2325
  this.config.bridges = []
2060
2326
  this.config.hierarchy = []
2061
2327
  this.config.priorities = []
2328
+ this.config.contextual_priorities = []
2062
2329
  this.config.associations = { positive: [], negative: [] }
2063
2330
  this.config.words = {}
2064
2331
 
@@ -2073,19 +2340,19 @@ class Config {
2073
2340
  }
2074
2341
  // console.log('name -------------', name)
2075
2342
  if (inits[i]) {
2343
+ // greg55
2076
2344
  inits[i]()
2077
2345
  }
2078
2346
  if (initAfterApis[i]) {
2079
2347
  const init = initAfterApis[i]
2080
- init.config.initializerFn({ ...init.args, kms: this.getConfigs(), isAfterApi: true })
2348
+ init.config.initializerFn({ ...init.args, kms: this.getConfigs(), isAfterApi: true})
2081
2349
  }
2082
2350
  const instance = this.instances.find((instance) => instance.name == name)
2083
2351
  if (instance) {
2084
- client.processInstance(this, instance)
2352
+ client.loadInstance(this, instance)
2085
2353
  }
2086
2354
  this.hierarchy.edges = this.config.hierarchy
2087
2355
  }
2088
- // this.instances.forEach((instance) => client.processInstance(this, instance))
2089
2356
  }
2090
2357
 
2091
2358
  if (reverseIt) {
@@ -2208,13 +2475,9 @@ class Config {
2208
2475
  }
2209
2476
  const km = (name) => this.getConfig(name)
2210
2477
  if (config instanceof Config) {
2211
- // const aw = addWord(this.config, config.uuid)
2212
- const aw = (word, def) => this.addWord(word, def)
2213
- config.initializerFn({ isModule: this.isModule, addWord: aw, baseConfig: this, km, currentConfig: config, config, objects: nsobjects, namespace, uuid, api: config.api })
2478
+ config.initializerFn(setupInitializerFNArgs(this, { isModule: this.isModule, currentConfig: config, testConfig: config, objects: nsobjects, namespace, uuid }))
2214
2479
  } else {
2215
- // const aw = addWord(this.config, this.uuid)
2216
- const aw = (word, def) => this.addWord(word, def)
2217
- this.initializerFn({ isModule: this.isModule, addWord: aw, baseConfig: this, km, currentConfig: this, config: this, objects: nsobjects, namespace, uuid, api: this.api })
2480
+ this.initializerFn(setupInitializerFNArgs(this, { isModule: this.isModule, currentConfig: this, testConfig: this, objects: nsobjects, namespace, uuid }))
2218
2481
  }
2219
2482
  })
2220
2483
  }
@@ -2279,6 +2542,20 @@ class Config {
2279
2542
  config.priorities = priorities
2280
2543
  }
2281
2544
 
2545
+ if (config.contextual_priorities) {
2546
+ let contextual_priorities = config.contextual_priorities
2547
+ contextual_priorities = contextual_priorities.map((cp) => {
2548
+ const { context, choose } = cp
2549
+ return {
2550
+ context: context.map((id) => {
2551
+ return [toNS(id[0]), id[1]]
2552
+ }),
2553
+ choose
2554
+ }
2555
+ })
2556
+ config.contextual_priorities = contextual_priorities
2557
+ }
2558
+
2282
2559
  for (const bag of bags) {
2283
2560
  if (config[bag]) {
2284
2561
  config[bag] = config[bag].map((b) => {
@@ -2473,7 +2750,8 @@ class Config {
2473
2750
  }
2474
2751
 
2475
2752
  // TODO get rid of useOldVersion arg
2476
- addInternal (more, { useOldVersion = true, skipObjects = false, includeNamespaces = true, allowNameToBeNull = false, handleCalculatedProps : hcps = false } = {}) {
2753
+ addInternal (more, { addFirst = false, useOldVersion = true, skipObjects = false, includeNamespaces = true, allowNameToBeNull = false, handleCalculatedProps : hcps = false } = {}) {
2754
+ validConfigProps(more)
2477
2755
  if (more instanceof Config) {
2478
2756
  more.initialize({ force: false })
2479
2757
  if (useOldVersion) {
@@ -2483,8 +2761,10 @@ class Config {
2483
2761
  more = _.cloneDeep(more.initConfig)
2484
2762
  }
2485
2763
  }
2764
+ debugConfigProps(more)
2765
+
2486
2766
  if (hcps) {
2487
- handleCalculatedProps(this, more)
2767
+ handleCalculatedProps(this, more, addFirst)
2488
2768
  applyUUID(more, this._uuid)
2489
2769
  }
2490
2770
  for (const key of Object.keys(more)) {
@@ -2509,7 +2789,11 @@ class Config {
2509
2789
  if (!configWords[word]) {
2510
2790
  configWords[word] = []
2511
2791
  }
2512
- configWords[word] = configWords[word].concat(moreWords[word])
2792
+ if (addFirst) {
2793
+ configWords[word] = moreWords[word].concat(configWords[word])
2794
+ } else {
2795
+ configWords[word] = configWords[word].concat(moreWords[word])
2796
+ }
2513
2797
  }
2514
2798
  } else if (key === 'name') {
2515
2799
  /*
@@ -2571,13 +2855,18 @@ class Config {
2571
2855
  this.config[key].splice(iOldOne, 1)
2572
2856
  break;
2573
2857
  }
2574
- }
2858
+ }
2575
2859
  }
2576
2860
  }
2577
2861
  }
2862
+
2578
2863
  // console.log('key', key, 'XXX')
2579
2864
  // console.log('more', JSON.stringify(more, null, 2))
2580
- this.config[key] = this.config[key].concat(more[key])
2865
+ if (addFirst) {
2866
+ this.config[key] = more[key].concat(this.config[key])
2867
+ } else {
2868
+ this.config[key] = this.config[key].concat(more[key])
2869
+ }
2581
2870
  } else {
2582
2871
  if (!(key in this.config)) {
2583
2872
  throw new Error(`Unexpected property in config ${key}`)
@@ -2708,5 +2997,8 @@ class Config {
2708
2997
  }
2709
2998
 
2710
2999
  module.exports = {
2711
- Config
3000
+ Config,
3001
+ config_toServer,
3002
+ operatorKey_valid,
3003
+ handleBridgeProps,
2712
3004
  }