theprogrammablemind_4wp 7.5.8-beta.8 → 7.5.8-beta.80

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,184 @@ 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
+ }
27
+
28
+ const debugPriority = (priority) => {
29
+ if (global.entodictonDebugPriority) {
30
+ if (helpers.safeEquals(entodictonDebugPriority, priority)) {
31
+ debugger; // debug hierarchy hit
32
+ }
33
+ }
34
+ }
35
+
36
+ const debugAssociation = (association) => {
37
+ if (global.entodictonDebugAssociation) {
38
+ if (helpers.safeEquals(global.entodictonDebugAssociation, association)) {
39
+ debugger; // debug association hit
40
+ }
41
+ }
42
+ }
43
+
44
+ const debugHierarchy = (pair) => {
45
+ if (global.entodictonDebugHierarchy) {
46
+ if (helpers.safeEquals(global.entodictonDebugHierarchy, pair)) {
47
+ debugger; // debug hierarchy hit
48
+ }
49
+ }
50
+ }
51
+
52
+ const debugBridge = (bridge) => {
53
+ if (global.entodictonDebugBridge) {
54
+ if (global.entodictonDebugBridge[0] == bridge.id && global.entodictonDebugBridge[1] == bridge.level) {
55
+ debugger; // debug hierarchy hit
56
+ }
57
+ }
58
+ }
59
+
60
+ const debugOperator = (operator) => {
61
+ if (global.entodictonDebugOperator) {
62
+ if ((operator.pattern || operator) === global.entodictonDebugOperator) {
63
+ debugger; // debug operator hit
64
+ }
65
+ }
66
+ }
67
+
68
+ const debugConfigProps = (config) => {
69
+ if (!config) {
70
+ return
71
+ }
72
+ const checkProps = [
73
+ { property: 'priorities', check: (v) => debugPriority(v) },
74
+ { property: 'association', check: (v) => debugAssociation(v) },
75
+ { property: 'hierarchy', check: (v) => debugHierarchy(v) },
76
+ { property: 'operators', check: (v) => debugOperator(v) },
77
+ { property: 'bridges', check: (v) => debugBridge(v) },
78
+ ]
79
+ for (const { property, check } of checkProps) {
80
+ if (config[property]) {
81
+ for (const value of config[property]) {
82
+ check(value)
83
+ }
84
+ }
85
+ }
86
+ }
87
+
88
+ const validConfigProps = (config) => {
89
+ const valid = [
90
+ 'hierarchy',
91
+ 'objects',
92
+ 'bridges',
93
+ 'operators',
94
+ 'words',
95
+ 'priorities',
96
+ 'associations',
97
+ 'name',
98
+ 'version',
99
+ 'generatorp',
100
+ 'generators',
101
+ 'semantics',
102
+ 'where',
103
+ 'floaters',
104
+ 'debug',
105
+
106
+ // TODO Fix these from the test app
107
+ 'implicits',
108
+ 'convolution',
109
+ 'expected_generated',
110
+ 'expected_results',
111
+ 'skipSemantics',
112
+ 'description',
113
+ 'contexts',
114
+ 'utterances',
115
+ 'flatten',
116
+
117
+ 'namespaces',
118
+ 'eqClasses',
119
+ ]
120
+ helpers.validProps(valid, config, 'config')
121
+ }
122
+
123
+ const setupInitializerFNArgs = (config, args) => {
124
+ const aw = (word, def) => config.addWord(word, def, args.uuid)
125
+ const ag = (generator) => config.addGenerator(generator, args.uuid, config.name)
126
+ const km = (name) => config.getConfig(name)
127
+ const apis = (name) => config.getConfig(name).api
128
+
129
+ return {
130
+ ...args,
131
+ addWord: aw,
132
+ addGenerator: ag,
133
+ config: config.getPseudoConfig(args.uuid, args.currentConfig),
134
+ km,
135
+ baseConfig: config,
136
+ apis,
137
+ }
138
+ }
139
+
140
+ const operatorKey_valid = (key) => {
141
+ if (
142
+ !_.isArray(key) ||
143
+ key.length != 2 ||
144
+ !_.isString(key[0]) ||
145
+ !_.isInteger(key[1]) ||
146
+ key[1] < 0
147
+ ) {
148
+
149
+ let details = ''
150
+ if (!_.isArray(key)) {
151
+ details = "Expected an array."
152
+ } else if (key.length != 2) {
153
+ details = "Expected an array of length two."
154
+ } else if (!_.isString(key[0])) {
155
+ details = "Expected element zero to be a string that is an operator id."
156
+ } else if (!_.isInteger(key[1])) {
157
+ details = "Expected element one to be a number that is an operator level."
158
+ } else if (key[1] < 0) {
159
+ details = "Expected element one to be a number that is an operator level which is greater than zero."
160
+ }
161
+ throw new Error(`${JSON.stringify(key)} is not a valid operator key. Values are of the form [<operatorId>, <operatorLevel>]. ${details}`)
162
+ }
163
+ }
164
+
165
+ const elist = (list, check, prefix) => {
166
+ for ([index, element] of list.entries()) {
167
+ try {
168
+ check(element)
169
+ } catch( e ) {
170
+ throw new Error(prefix(index, e))
171
+ }
172
+ }
173
+ }
174
+ const priorities_valid = (cps) => {
175
+ elist(cps, (cp) => priority_valid(cp), (index, e) => `priorities has an invalid priority at position ${index}. ${e}`)
176
+ }
177
+
178
+ const priority_valid = (cp) => {
179
+ if (!cp.context) {
180
+ throw new Error(`The priority ${JSON.stringify(cp)} is missing the "context" property. That is a list of the operator keys that are to be prioritized differently.`)
181
+ }
182
+ if (!_.isArray(cp.context)) {
183
+ throw new Error(`The priority ${JSON.stringify(cp)} has an invalid "context" value. That is a list of the operator keys that are to be prioritized differently.`)
184
+ }
185
+ elist(cp.context, (element) => operatorKey_valid(element), (index, e) => `The priority ${JSON.stringify(cp)} has an invalid operator key at position ${index}. ${e}`)
186
+ if (!_.isArray(cp.choose)) {
187
+ throw new Error(`The 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.`)
188
+ }
189
+ elist(cp.choose,
190
+ (element) => {
191
+ if (!element && element !== 0) {
192
+ throw new Error(`The value should be an index into the "context" property of the operator that is to be considered for prioritization.`)
193
+ }
194
+ if (!_.isInteger(element) || element < 0 || element >= cp.context.length) {
195
+ 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}.`)
196
+ }
197
+ },
198
+ (index, e) => `The choose property in the priority ${JSON.stringify(cp)} has an invalid index at position ${index}. ${e}`
199
+ )
200
+ }
201
+
202
+ const handleBridgeProps = (config, bridge, addFirst) => {
26
203
  ecatch(`While processing the bridge for ${bridge.id}#${bridge.level}`,
27
204
  () => {
28
205
  if (!bridge.bridge) {
@@ -51,7 +228,7 @@ const handleBridgeProps = (config, bridge) => {
51
228
  if (typeof after == 'string') {
52
229
  after = [after, 0]
53
230
  }
54
- config.addPriorities([after, [bridge.id, bridge.level]])
231
+ config.addPriority({ context: [[bridge.id, bridge.level], after], choose: [0] })
55
232
  }
56
233
  }
57
234
  if (bridge.after) {
@@ -59,7 +236,7 @@ const handleBridgeProps = (config, bridge) => {
59
236
  if (typeof before == 'string') {
60
237
  before = [before, 0]
61
238
  }
62
- config.addPriorities([[bridge.id, bridge.level], before])
239
+ config.addPriority({ context: [before, [bridge.id, bridge.level]], choose: [0] })
63
240
  }
64
241
  }
65
242
  if (bridge.words) {
@@ -68,19 +245,29 @@ const handleBridgeProps = (config, bridge) => {
68
245
  config.addWordInternal(def, {"id": bridge.id, "initial": `{ value: "${def}"}` })
69
246
  } else {
70
247
  const word = def.word
71
- def = { initial: JSON.stringify(def), id: bridge.id, word: undefined }
248
+ def = { initial: JSON.stringify(def), id: bridge.id, word }
72
249
  config.addWordInternal(word, def)
73
250
  }
74
251
  }
75
252
  }
253
+ /*
76
254
  if (bridge.generator) {
77
- config.config.generators.unshift(bridge.generator)
255
+ if (addFirst) {
256
+ config.config.generators.unshift(bridge.generator)
257
+ } else {
258
+ config.config.generators.push(bridge.generator)
259
+ }
78
260
  }
261
+ */
79
262
  if (bridge.generators) {
80
263
  const generators = [...bridge.generators]
81
264
  generators.reverse()
82
265
  for (let generator of generators) {
83
- config.config.generators.unshift(generator)
266
+ if (addFirst) {
267
+ config.config.generators.unshift(generator)
268
+ } else {
269
+ config.config.generators.push(generator)
270
+ }
84
271
  }
85
272
  }
86
273
  if (bridge.generatorpr) {
@@ -91,66 +278,76 @@ const handleBridgeProps = (config, bridge) => {
91
278
  const match = bridge.generatorp.match || (() => true)
92
279
  const apply = typeof bridge.generatorp == 'function' ? bridge.generatorp : bridge.generatorp.apply || bridge.generatorp
93
280
  const level = bridge.generatorp.level >= 0 ? bridge.generatorp.level : bridge.level + 1
94
- config.config.generators.unshift({
281
+
282
+ const generator = {
95
283
  where: bridge.generatorp.where || bridge.where || client.where(4),
96
284
  match: (args) => bridge.id == args.context.marker && args.context.level == level && args.context.paraphrase && match(args),
97
285
  apply: (args) => apply(args),
98
286
  applyWrapped: apply,
99
287
  property: 'generatorp',
100
- })
288
+ }
289
+ if (addFirst) {
290
+ config.config.generators.unshift(generator)
291
+ } else {
292
+ config.config.generators.push(generator)
293
+ }
294
+
101
295
  }
102
296
  if (bridge.generatorr) {
103
297
  const match = bridge.generatorr.match || (() => true)
104
298
  const apply = typeof bridge.generatorr == 'function' ? bridge.generatorr : bridge.generatorr.apply || bridge.generatorr
105
299
  const level = bridge.generatorr.level >= 0 ? bridge.generatorr.level : bridge.level + 1
106
- config.config.generators.unshift({
300
+ const generator = {
107
301
  where: bridge.generatorr.where || bridge.where || client.where(4),
108
302
  match: (args) => bridge.id == args.context.marker && args.context.level == level && !args.context.paraphrase && (args.context.response || args.context.isResponse) && match(args),
109
303
  apply: (args) => apply(args),
110
304
  applyWrapped: apply,
111
305
  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
- })
306
+ }
307
+ if (addFirst) {
308
+ config.config.generators.unshift(generator)
309
+ } else {
310
+ config.config.generators.push(generator)
311
+ }
124
312
  }
125
- */
126
313
  if (bridge.evaluator) {
127
- config.config.semantics.unshift({
314
+ const semantic = {
128
315
  where: bridge.evaluator.where || bridge.where || client.where(3),
129
316
  match: ({context}) => bridge.id == context.marker && context.evaluate,
130
317
  apply: (args) => bridge.evaluator(args),
131
318
  applyWrapped: bridge.evaluator,
132
319
  property: 'evaluator',
133
- })
320
+ }
321
+ if (addFirst) {
322
+ config.config.semantics.unshift(semantic)
323
+ } else {
324
+ config.config.semantics.push(semantic)
325
+ }
134
326
  }
135
327
  if (bridge.semantic) {
136
- config.config.semantics.unshift({
328
+ const semantic = {
137
329
  where: bridge.semantic.where || bridge.where || client.where(3),
138
330
  match: ({context}) => bridge.id == context.marker,
139
331
  apply: (args) => bridge.semantic(args),
140
332
  applyWrapped: bridge.semantic,
141
333
  property: 'semantic',
142
- })
334
+ }
335
+ if (addFirst) {
336
+ config.config.semantics.unshift(semantic)
337
+ } else {
338
+ config.config.semantics.push(semantic)
339
+ }
143
340
  }
144
341
  }
145
342
  )
146
343
  }
147
344
 
148
- const handleCalculatedProps = (baseConfig, moreConfig) => {
149
- for (let bridge of moreConfig.bridges) {
345
+ const handleCalculatedProps = (baseConfig, moreConfig, addFirst) => {
346
+ for (let bridge of (moreConfig.bridges || [])) {
150
347
  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' ]
348
+ 'level', 'optional', 'selector', 'semantic', 'words', /Bridge$/, 'localHierarchy', 'levelSpecificHierarchy', 'where', 'uuid' ]
152
349
  helpers.validProps(valid, bridge, 'bridge')
153
- handleBridgeProps(baseConfig, bridge)
350
+ handleBridgeProps(baseConfig, bridge, addFirst)
154
351
  }
155
352
  if (moreConfig.operators) {
156
353
  moreConfig.operators = moreConfig.operators.map((operator) => {
@@ -167,10 +364,19 @@ if (runtime.process.env.DEBUG_HIERARCHY) {
167
364
  global.entodictonDebugHierarchy = JSON.parse(runtime.process.env.DEBUG_HIERARCHY)
168
365
  }
169
366
 
367
+
368
+ // i keep randomly doing one of the other so I will just make both work the same way
369
+ if (runtime.process.env.DEBUG_PRIORITIES) {
370
+ global.entodictonDebugPriority = JSON.parse(runtime.process.env.DEBUG_PRIORITIES)
371
+ }
170
372
  if (runtime.process.env.DEBUG_PRIORITY) {
171
373
  global.entodictonDebugPriority = JSON.parse(runtime.process.env.DEBUG_PRIORITY)
172
374
  }
173
375
 
376
+ if (runtime.process.env.DEBUG_CONTEXTUAL_PRIORITY) {
377
+ global.entodictonDebugContextualPriority = JSON.parse(runtime.process.env.DEBUG_CONTEXTUAL_PRIORITY)
378
+ }
379
+
174
380
  if (runtime.process.env.DEBUG_ASSOCIATION) {
175
381
  global.entodictonDebugAssociation = JSON.parse(runtime.process.env.DEBUG_ASSOCIATION)
176
382
  }
@@ -180,6 +386,7 @@ if (runtime.process.env.DEBUG_BRIDGE) {
180
386
  global.entodictonDebugBridge = runtime.process.env.DEBUG_BRIDGE.split('/')
181
387
  if (global.entodictonDebugBridge.length !== 2) {
182
388
  console.log('Expected DEBUG_BRIDGE to be of the form "id/level"');
389
+ process.exit(-1)
183
390
  }
184
391
  global.entodictonDebugBridge[1] = parseInt(global.entodictonDebugBridge[1])
185
392
  }
@@ -252,6 +459,14 @@ const normalizeConfig = (config) => {
252
459
  }
253
460
  }
254
461
  }
462
+
463
+ if (config.semantics) {
464
+ for (let semantic of config.semantics) {
465
+ if (semantic.oneShot) {
466
+ semantic.id = uuid()
467
+ }
468
+ }
469
+ }
255
470
  }
256
471
  }
257
472
 
@@ -459,8 +674,8 @@ const multiApiImpl = (initializer) => {
459
674
  initializer(config, api)
460
675
  const name = api.getName()
461
676
  multiApi.apis[name] = api
462
- api.objects = config.get('objects')
463
- api.config = () => config
677
+ // api.objects = config.get('objects')
678
+ // api.config = () => config
464
679
  multiApi.current = name
465
680
  },
466
681
 
@@ -471,11 +686,13 @@ const multiApiImpl = (initializer) => {
471
686
  }
472
687
  },
473
688
 
689
+ /*
474
690
  set objects (value) {
475
691
  for (const key in Object.keys(this.apis)) {
476
692
  this.apis[key].objects = value
477
693
  }
478
694
  },
695
+ */
479
696
 
480
697
  // "product1": apiInstance(testData1),
481
698
  apis: {
@@ -489,6 +706,47 @@ const multiApiImpl = (initializer) => {
489
706
 
490
707
  class Config {
491
708
 
709
+ toServer (config) {
710
+ return config_toServer(config)
711
+ }
712
+
713
+ base () {
714
+ const base = new Config()
715
+ for (let km of this.configs.reverse()) {
716
+ if (km.isSelf) {
717
+ continue
718
+ }
719
+ base.add(km.config)
720
+ }
721
+ return base
722
+ }
723
+
724
+ getPseudoConfig (uuid, config) {
725
+ return {
726
+ description: "this is a pseudo config that has limited functionality due to being available in the initializer function context",
727
+ addAssociation: (...args) => this.addAssociation(...args),
728
+ addAssociations: (...args) => this.addAssociations(...args),
729
+ addBridge: (...args) => this.addBridge(...args, uuid),
730
+ addGenerator: (...args) => this.addGenerator(...args, uuid, config.name),
731
+ addHierarchy: (...args) => this.addHierarchy(...args),
732
+ addOperator: (...args) => this.addOperator(...args, uuid),
733
+ addPriority: (...args) => this.addPriority(...args),
734
+ addPriorities: (...args) => this.addPriorities(...args),
735
+ addSemantic: (...args) => this.addSemantic(...args, uuid, config.name),
736
+ removeSemantic: (...args) => this.removeSemantic(...args, uuid, config.name),
737
+ addWord: (...args) => this.addWord(...args, uuid),
738
+
739
+ getHierarchy: (...args) => this.config.hierarchy,
740
+ getBridges: (...args) => this.config.bridges,
741
+
742
+ addArgs: (...args) => this.addArgs(...args),
743
+ getBridge: (...args) => this.getBridge(...args),
744
+ fragment: (...args) => this.fragment(...args),
745
+ exists: (...args) => this.exists(...args),
746
+ addAPI: (...args) => this.addAPI(...args),
747
+ }
748
+ }
749
+
492
750
  inDevelopmentMode (call) {
493
751
  config.developmentModeOn += 1
494
752
  try {
@@ -527,9 +785,6 @@ class Config {
527
785
  }
528
786
 
529
787
  setTestConfig(testConfig) {
530
- if (this.name == 'ui') {
531
- console.log('ui setting testConfig')
532
- }
533
788
  this.testConfig = testConfig
534
789
  }
535
790
 
@@ -709,7 +964,8 @@ class Config {
709
964
  }
710
965
  }
711
966
 
712
- needsRebuild(template, instance, options = { rebuild: false }) {
967
+ // { rebuild: false, isModule: false }
968
+ needsRebuild(template, instance, options) {
713
969
  if (options.rebuild) {
714
970
  return true
715
971
  }
@@ -723,7 +979,52 @@ class Config {
723
979
  const instanceFragments = (instance.fragments || []).map((fragment) => fragment.key || fragment.query).map( toCanonical )
724
980
  const templateFragments = (template.fragments || []).concat(this.dynamicFragments).map( toCanonical )
725
981
  const sameFragments = helpers.safeEquals(templateFragments, instanceFragments)
726
- const sameQueries = helpers.safeEquals((template.queries || []).map(helpers.updateQueries), (instance.queries || []))
982
+ const toCanonicalQuery = (queryOrConfig) => {
983
+ if (typeof queryOrConfig == 'string') {
984
+ const query = queryOrConfig
985
+ return query
986
+ } else {
987
+ const config = { ...queryOrConfig }
988
+ delete config.where
989
+ if (options.isModule) {
990
+ // things like webpack rewrite the functions if there are constants so this compare does not work
991
+ delete config.generators
992
+ delete config.semantics
993
+ } else {
994
+ config.generators = (config.generators || []).map((generator) => {
995
+ generator = {...generator}
996
+ delete generator.where
997
+ generator.match = generator.match.toString()
998
+ generator.apply = generator.apply.toString()
999
+ return generator
1000
+ })
1001
+ config.semantics = (config.semantics || []).map((semantic) => {
1002
+ semantic = {...semantic}
1003
+ delete semantic.where
1004
+ semantic.match = semantic.match.toString()
1005
+ semantic.apply = semantic.apply.toString()
1006
+ return semantic
1007
+ })
1008
+ }
1009
+ return config
1010
+ }
1011
+ }
1012
+ const toCanonicalQueries = (elements) => {
1013
+ return elements.map( toCanonicalQuery )
1014
+ }
1015
+
1016
+ const sameQueries = helpers.safeEquals(toCanonicalQueries(template.queries || []).map(helpers.updateQueries), toCanonicalQueries(instance.queries || []))
1017
+
1018
+ const debug = true
1019
+ if (debug) {
1020
+ console.log("instance", instance)
1021
+ console.log("sameQueries", sameQueries)
1022
+ console.log("sameFragments", sameFragments)
1023
+ console.log("templateFragments", templateFragments)
1024
+ console.log("instanceFragments", instanceFragments)
1025
+ console.log('template.queries', toCanonicalQueries(template.queries || []).map(helpers.updateQueries))
1026
+ console.log("instance.queries", toCanonicalQueries(instance.queries || []))
1027
+ }
727
1028
  return !(instance && sameQueries && sameFragments)
728
1029
  }
729
1030
 
@@ -739,18 +1040,21 @@ class Config {
739
1040
  }
740
1041
  }
741
1042
 
1043
+ toData (data) {
1044
+ Object.assign(data, this.config)
1045
+ config_toServer(data)
1046
+ }
1047
+
1048
+ // loadTemplate
742
1049
  load (template, instance, options = { rebuild: false } ) {
743
1050
  this.validifyTemplate(template)
744
1051
  instance.template = template
745
1052
  this.logs.push(`loading template for ${this.name}`)
746
- if (instance && instance.associations && !options.rebuild) {
747
- this.addAssociations(instance.associations)
748
- }
749
1053
  if (options.rebuild) {
750
1054
  // TODO fix beforeQuery
751
1055
  template = { fragments: [], queries: [], ...template }
752
1056
  template.fragments = template.fragments.concat(this.dynamicFragments)
753
- client.build({ config: this, target: this.name, beforeQuery: () => {}, template, ...options })
1057
+ client.rebuildTemplate({ config: this, target: this.name, beforeQuery: () => {}, template, ...options })
754
1058
  } else {
755
1059
  // no change
756
1060
  // this.initInstances.push({ ...instance, name: config.name })
@@ -768,7 +1072,7 @@ class Config {
768
1072
  instance.name = this.name
769
1073
  this.initInstances.push(instance)
770
1074
  this.instances.push(instance)
771
- client.processInstance(this, instance)
1075
+ client.loadInstance(this, instance)
772
1076
  }
773
1077
  }
774
1078
  }
@@ -788,13 +1092,15 @@ class Config {
788
1092
  }
789
1093
  }
790
1094
 
791
-
792
1095
  addAssociations (associations) {
793
1096
  for (let association of associations) {
794
1097
  this.addAssociation(association)
795
1098
  }
796
1099
  }
797
1100
 
1101
+ debugConfig() {
1102
+ }
1103
+
798
1104
  addAssociation (association) {
799
1105
  if (!this.config.associations) {
800
1106
  this.config.associations = {
@@ -802,27 +1108,28 @@ class Config {
802
1108
  positive: []
803
1109
  }
804
1110
  }
805
- if (global.entodictonDebugAssociation) {
806
- if (helpers.safeEquals(global.entodictonDebugAssociation, association)) {
807
- debugger; // debug association hit
808
- }
809
- }
1111
+ debugAssociation(association)
810
1112
  this.config.associations.positive.push(association)
811
1113
  this._delta.json.associations.push({ action: 'add', association })
812
1114
  }
813
1115
 
814
1116
  // TODO add more error checking to these like addHierarchy has
815
- addPriorities (priorities) {
1117
+ // TODO change name from priorities to priority
1118
+ // [ context: <list of [id, level]>, choose: [<indexes of prioritized operator>], [ordered: [true|false]] ]
1119
+ addPriority (priority) {
816
1120
  if (!this.config.priorities) {
817
1121
  this.config.priorities = []
818
1122
  }
819
- if (global.entodictonDebugPriority) {
820
- if (helpers.safeEquals(entodictonDebugPriority, priorities)) {
821
- debugger; // debug hierarchy hit
822
- }
1123
+ debugPriority(priority)
1124
+ priority_valid(priority)
1125
+ this.config.priorities.push(priority)
1126
+ this._delta.json.priorities.push({ action: 'add', priority })
1127
+ }
1128
+
1129
+ addPriorities (priorities) {
1130
+ for (let priority of priorities) {
1131
+ this.addPriority(priority)
823
1132
  }
824
- this.config.priorities.push(priorities)
825
- this._delta.json.priorities.push({ action: 'add', priorities })
826
1133
  }
827
1134
 
828
1135
  addHierarchy (child, parent) {
@@ -842,11 +1149,7 @@ class Config {
842
1149
  if (typeof parent !== 'string') {
843
1150
  throw new Error(`addHierarchy expected parent property to be a string. got ${JSON.stringify(parent)}`)
844
1151
  }
845
- if (global.entodictonDebugHierarchy) {
846
- if (helpers.safeEquals(entodictonDebugHierarchy, [child, parent])) {
847
- debugger; // debug hierarchy hit
848
- }
849
- }
1152
+ debugHierarchy([child, parent])
850
1153
  this.config.hierarchy.push(edge)
851
1154
  // TODO greg11 this.hierarchy.addEdge(edge)
852
1155
  this._delta.json.hierarchy.push([child, parent])
@@ -859,13 +1162,7 @@ class Config {
859
1162
  if (typeof parent !== 'string') {
860
1163
  throw new Error(`addHierarchy expected parent to be a string. got ${JSON.stringify(parent)}`)
861
1164
  }
862
-
863
- if (global.entodictonDebugHierarchy) {
864
- if (helpers.safeEquals(global.entodictonDebugHierarchy, [child, parent])) {
865
- debugger; // debug hierarchy hit
866
- }
867
- }
868
-
1165
+ debugHierarchy([child, parent])
869
1166
  if (this.config.hierarchy.find( (element) => {
870
1167
  const hc = hierarchyCanonical(element)
871
1168
  if (child == hc.child && parent == hc.parent) {
@@ -881,24 +1178,24 @@ class Config {
881
1178
  }
882
1179
 
883
1180
  getBridge (id, level) {
884
- return this.config.bridges.find( (bridge) => bridge.id == id && bridge.level == level )
1181
+ if (level) {
1182
+ return this.config.bridges.find( (bridge) => bridge.id == id && bridge.level == level )
1183
+ } else {
1184
+ return this.config.bridges.find( (bridge) => bridge.id == id)
1185
+ }
885
1186
  }
886
1187
 
887
- addBridge (bridge) {
1188
+ addBridge (bridge, uuid) {
888
1189
  if (!this.config.bridges) {
889
1190
  this.config.bridges = []
890
1191
  }
891
1192
  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
-
1193
+ const def = Object.assign({}, bridge, { uuid: uuid || this._uuid })
1194
+
1195
+ debugBridge(bridge)
900
1196
  if (bridge.allowDups) {
901
- if (bridges.find( (b) => b.id == bridge.id && b.level == bridge.level && b.bridge == bridge.bridge )) {
1197
+ // if (bridges.find( (b) => b.id == bridge.id && b.level == bridge.level && b.bridge == bridge.bridge )) {
1198
+ if (bridges.find( (b) => b.id == bridge.id && b.level == bridge.level)) {
902
1199
  return;
903
1200
  }
904
1201
  }
@@ -911,12 +1208,7 @@ class Config {
911
1208
  this._delta.json.bridges.push({ action: 'add', bridge: def })
912
1209
  }
913
1210
 
914
- addGenerator (match, apply) {
915
- let generator = match
916
- if ((typeof match === 'function') && (typeof apply === 'function')) {
917
- generator = { match, apply }
918
- }
919
-
1211
+ addGenerator (generator, uuid, name) {
920
1212
  if (!(typeof generator.match === 'function')) {
921
1213
  throw new Error('addGenerator: Expected matcher to be a function')
922
1214
  }
@@ -933,20 +1225,16 @@ class Config {
933
1225
  }
934
1226
 
935
1227
  const generators = this.config.generators
936
- Object.assign(generator, { uuid: this._uuid, km: this.name, index: generators.length })
1228
+ Object.assign(generator, { uuid: uuid || this._uuid, km: name || this.name, index: generators.length })
937
1229
  // used to be unshift
938
1230
  generators.unshift(generator)
939
1231
  }
940
1232
 
941
- addSemantic (match, apply) {
942
- let semantic = match
943
- if ((typeof match === 'function') && (typeof apply === 'function')) {
944
- semantic = { match, apply }
945
- }
946
-
1233
+ addSemantic (semantic, uuid, name) {
947
1234
  if (!(typeof semantic.match === 'function')) {
948
1235
  throw new Error('addSemantic: Expected match to be a function')
949
1236
  }
1237
+
950
1238
  if (!(typeof semantic.apply === 'function')) {
951
1239
  throw new Error('addSemantic: Expected apply to be a function')
952
1240
  }
@@ -960,11 +1248,18 @@ class Config {
960
1248
  }
961
1249
 
962
1250
  const semantics = this.config.semantics
963
- Object.assign(semantic, { uuid: this._uuid, km: this.name, index: semantics.length })
1251
+ Object.assign(semantic, { uuid: uuid || semantic.uuid || this._uuid, km: name || this.name, index: semantics.length, id: semantic.id || uuidv4() })
964
1252
  semantics.unshift(semantic)
965
1253
  }
966
1254
 
967
- addOperator (objectOrPattern) {
1255
+ removeSemantic(deleteSemantic) {
1256
+ const index = this.config.semantics.findIndex( (semantic) => semantic.id === deleteSemantic.id )
1257
+ if (index >= 0) {
1258
+ this.config.semantics.splice(index, 1)
1259
+ }
1260
+ }
1261
+
1262
+ addOperator (objectOrPattern, uuid) {
968
1263
  if (!this.config.operators) {
969
1264
  this.config.operators = []
970
1265
  }
@@ -973,16 +1268,12 @@ class Config {
973
1268
 
974
1269
  let operator;
975
1270
  if (typeof objectOrPattern === 'string') {
976
- operator = { pattern: objectOrPattern, uuid: this._uuid }
1271
+ operator = { pattern: objectOrPattern, uuid: uuid || this._uuid }
977
1272
  } else {
978
- operator = Object.assign({}, objectOrPattern, { uuid: this._uuid })
1273
+ operator = Object.assign({}, objectOrPattern, { uuid: uuid || this._uuid })
979
1274
  }
980
1275
 
981
- if (global.entodictonDebugOperator) {
982
- if (operator.pattern === global.entodictonDebugOperator) {
983
- debugger; // debug operator hit
984
- }
985
- }
1276
+ debugOperator(operator)
986
1277
 
987
1278
  if (operator.allowDups) {
988
1279
  if (operators.find( (o) => o.pattern == operator.pattern )) {
@@ -996,16 +1287,16 @@ class Config {
996
1287
  this._delta.json.operators.push({ action: 'add', operator })
997
1288
  }
998
1289
 
999
- addWord (word, def) {
1000
- this.addWordInternal(word, def)
1290
+ addWord (word, def, uuid) {
1291
+ this.addWordInternal(word, def, uuid)
1001
1292
  }
1002
1293
 
1003
- addWordInternal (word, def) {
1294
+ addWordInternal (word, def, uuid) {
1004
1295
  if (!this.config.words) {
1005
1296
  this.config.words = {}
1006
1297
  }
1007
1298
  const words = this.config.words
1008
- def = Object.assign({}, def, { uuid: this._uuid })
1299
+ def = Object.assign({}, def, { uuid: uuid || this._uuid })
1009
1300
  if (words[word]) {
1010
1301
  if (!words[word].some((e) => helpers.safeEquals(e, def))) {
1011
1302
  words[word].unshift(def)
@@ -1226,33 +1517,7 @@ class Config {
1226
1517
  }
1227
1518
 
1228
1519
  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')
1520
+ validConfigProps(config)
1256
1521
 
1257
1522
  config.operators = config.operators || []
1258
1523
  config.bridges = config.bridges || []
@@ -1315,6 +1580,10 @@ class Config {
1315
1580
  }
1316
1581
  }
1317
1582
 
1583
+ if (config && config.priorities) {
1584
+ priorities_valid(config.priorities)
1585
+ }
1586
+
1318
1587
  normalizeConfig(config)
1319
1588
 
1320
1589
  // set the default server so stuff just works
@@ -1330,7 +1599,6 @@ class Config {
1330
1599
  if (config) {
1331
1600
  this.name = config.name
1332
1601
  }
1333
- this.motivations = []
1334
1602
  this.loadOrder = new DigraphInternal()
1335
1603
  this.wasInitialized = false
1336
1604
  this.configs = []
@@ -1355,6 +1623,7 @@ class Config {
1355
1623
  }
1356
1624
  this.get('objects').namespaced[this._uuid] = {}
1357
1625
  this.valid()
1626
+ debugConfigProps(this.config)
1358
1627
  }
1359
1628
 
1360
1629
  addArgs(moreArgs) {
@@ -1385,7 +1654,10 @@ class Config {
1385
1654
  }
1386
1655
 
1387
1656
  delta () {
1388
- return { cacheKey: this._delta.cacheKey, json: this._delta.json }
1657
+ return {
1658
+ cacheKey: this._delta.cacheKey,
1659
+ json: this._delta.json
1660
+ }
1389
1661
  }
1390
1662
 
1391
1663
  resetDelta (cacheKey) {
@@ -1431,11 +1703,13 @@ class Config {
1431
1703
  this._api.add(this, this._api, value)
1432
1704
  } else {
1433
1705
  this._api = _.cloneDeep(value)
1706
+ /*
1434
1707
  if (this._api) {
1435
- this._api.objects = this.config.objects
1436
- this._api.config = () => this
1437
- this._api.uuid = this._uuid
1708
+ // this._api.objects = this.config.objects
1709
+ // this._api.config = () => this
1710
+ // this._api.uuid = this._uuid
1438
1711
  }
1712
+ */
1439
1713
  this.rebuild()
1440
1714
  }
1441
1715
  }
@@ -1478,40 +1752,6 @@ class Config {
1478
1752
  // this.valid() init was not run because the kms are not all setup yet
1479
1753
  }
1480
1754
 
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
1755
  // TODO add more details
1516
1756
  equal(config) {
1517
1757
  if (JSON.stringify(this.config) != JSON.stringify(config.config)) {
@@ -1525,6 +1765,9 @@ class Config {
1525
1765
  runtime.fs.writeFileSync(fn, JSON.stringify(this.config, 0, 2))
1526
1766
  }
1527
1767
 
1768
+ copy (options = { callInitializers: true }) {
1769
+ }
1770
+
1528
1771
  copy (options = { callInitializers: true }) {
1529
1772
  this.valid()
1530
1773
  const cp = new Config()
@@ -1534,15 +1777,14 @@ class Config {
1534
1777
  cp.transitoryMode = this.transitoryMode
1535
1778
  cp.configs = this.configs.map((km) => km.copy2(Object.assign({}, options, { getCounter: (name) => cp.getCounter(name), callInitializers: false })))
1536
1779
  cp._uuid = cp.configs[0]._uuid
1780
+ // update uuid here set the uuid in the objects and add error checking
1537
1781
  cp.initializerFn = this.initializerFn
1538
- cp.initAfterApi = this.initAfterApi
1539
1782
  cp._api = _.cloneDeep(this._api)
1540
1783
  cp._namespace = this._namespace
1541
1784
  cp._eqClasses = this._eqClasses
1542
1785
  cp.name = this.name
1543
1786
  cp.description = this.description
1544
1787
  cp.tests = this.tests
1545
- cp.motivations = [...this.motivations]
1546
1788
  cp.isModule = this.isModule
1547
1789
  cp.loadedForTesting = this.loadedForTesting
1548
1790
  cp.initInstances = this.initInstances.slice()
@@ -1567,25 +1809,38 @@ class Config {
1567
1809
  }
1568
1810
  cp.mapUUIDs(map)
1569
1811
 
1812
+ if (cp._uuid == 'concept2') {
1813
+ // debugger
1814
+ }
1570
1815
  if (options.callInitializers) {
1571
1816
  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
- }
1817
+ } else {
1818
+ // this mess is for duplicate into a KM after resetToOne was called
1819
+ /*
1820
+ if (cp._api) {
1821
+ // cp._api.objects = cp.config.objects
1822
+ // cp._api.config = () => (cp instanceof Config) ? cp : cp.config
1823
+ // cp._api.uuid = cp._uuid
1824
+ }
1825
+ */
1578
1826
 
1579
- if (!cp.config.objects) {
1580
- cp.config.objects = { namespaced: {} }
1581
- } else if (!cp.config.objects.namespaced) {
1582
- cp.config.objects.namespaced = {}
1827
+ if (!cp.config.objects) {
1828
+ cp.config.objects = { namespaced: {} }
1829
+ } else if (!cp.config.objects.namespaced) {
1830
+ cp.config.objects.namespaced = {}
1831
+ }
1832
+ cp.configs.forEach((km) => {
1833
+ // const namespace = km.namespace
1834
+ cp.config.objects.namespaced[km._uuid] = {}
1835
+ })
1836
+ /*
1837
+ if (cp._uuid == 'concept2') {
1838
+ if (!cp.api.objects.defaultTypesForHierarchy) {
1839
+ debugger
1840
+ }
1841
+ }
1842
+ */
1583
1843
  }
1584
- cp.configs.forEach((km) => {
1585
- // const namespace = km.namespace
1586
- cp.config.objects.namespaced[km._uuid] = {}
1587
- })
1588
-
1589
1844
  cp.valid()
1590
1845
  return cp
1591
1846
  }
@@ -1685,38 +1940,33 @@ class Config {
1685
1940
  }
1686
1941
  */
1687
1942
  const objects = {}
1688
- const km = (name) => this.getConfig(name)
1689
1943
  if (config instanceof Config) {
1690
- // const aw = addWord(this.config, config.uuid)
1691
- const aw = (word, def) => this.addWord(word, def)
1692
1944
  this.get('objects').namespaced[config._uuid] = objects
1945
+ /*
1693
1946
  if (config._api) {
1694
- config._api.objects = objects
1695
- config._api.config = () => this
1947
+ // config._api.objects = objects
1948
+ // config._api.config = () => this
1696
1949
  }
1697
- config.initializerFn({ addWord: aw, km, config, baseConfig: this, currentConfig: config, objects, namespace, uuid, api: config.api })
1950
+ */
1951
+ config.initializerFn(setupInitializerFNArgs(this, { testConfig: config, currentConfig: config, objects, namespace, uuid }))
1698
1952
  } else {
1699
- // const aw = addWord(this.config, this.uuid)
1700
- const aw = (word, def) => this.addWord(word, def)
1701
1953
  this.get('objects').namespaced[this._uuid] = objects
1954
+ /*
1702
1955
  if (config._api) {
1703
- config._api.objects = objects
1704
- config._api.config = () => this
1956
+ // config._api.objects = objects
1957
+ // config._api.config = () => this
1705
1958
  }
1706
- this.initializerFn({ addWord: aw, km, config: this, baseConfig: this, currentConfig: this, objects, namespace, uuid, api: this.api })
1959
+ */
1960
+ this.initializerFn(setupInitializerFNArgs(this, { testConfig: this, currentConfig: this, objects, namespace, uuid }))
1707
1961
  }
1708
1962
  })
1709
- this.instances.forEach((instance) => client.processInstance(this, instance))
1963
+ this.instances.forEach((instance) => client.loadInstance(this, instance))
1710
1964
  }
1711
1965
 
1712
1966
  initialize ({ force = true } = {}) {
1713
1967
  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
1968
  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 })
1969
+ this.initializerFn(setupInitializerFNArgs(this, { testConfig: this, currentConfig: this, objects, uuid: this._uuid, namespace: '' }))
1720
1970
  this.wasInitialized = true
1721
1971
  }
1722
1972
  }
@@ -1724,29 +1974,22 @@ class Config {
1724
1974
  initializer (fn, options = {}) {
1725
1975
  if (options) {
1726
1976
  for (let option of Object.keys(options)) {
1727
- const validOptions = ['initAfterApi']
1728
- if (!['initAfterApi'].includes(option)) {
1977
+ const validOptions = []
1978
+ if (!validOptions.includes(option)) {
1729
1979
  throw new Error(`For Config.initializer, unrecognized option ${option}. The valid options are ${validOptions}`)
1730
1980
  }
1731
1981
  }
1732
1982
  }
1733
- const { initAfterApi = false } = options;
1734
1983
  this.wasInitialized = false
1735
- this.initAfterApi = initAfterApi
1736
- this.initializerFn = (args) => {
1984
+ this.initializerFn = (args, { dontCallFn } = {}) => {
1737
1985
  const transitoryMode = global.transitoryMode
1738
1986
  global.transitoryMode = false
1739
1987
  // const baseConfig = args.baseConfig
1740
1988
  const currentConfig = args.currentConfig
1741
1989
 
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
1990
+ if (args.isAfterApi) {
1991
+ fn(args)
1747
1992
  }
1748
- // this.instances.forEach( (instance) => client.processInstance(this, instance) )
1749
- fn(args)
1750
1993
  currentConfig.wasInitialized = true
1751
1994
  global.transitoryMode = transitoryMode
1752
1995
  }
@@ -1913,7 +2156,6 @@ class Config {
1913
2156
  }
1914
2157
  this.config.objects.namespaced = {}
1915
2158
  this.resetWasInitialized()
1916
- this.resetMotivations()
1917
2159
 
1918
2160
  // reorder configs base on load ordering
1919
2161
  {
@@ -1939,8 +2181,8 @@ class Config {
1939
2181
  this.config.objects.namespaced[km._uuid] = {}
1940
2182
  const namespacedObjects = this.config.objects.namespaced[km._uuid]
1941
2183
  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)
2184
+ // const aw = (word, def) => this.addWord(word, def)
2185
+ // const ag = (matchOrGenerator, applyOrNothing) => this.addGenerator(matchOrGenerator, applyOrNothing)
1944
2186
  let config = km.config
1945
2187
 
1946
2188
  if (config.addedArgss) {
@@ -1961,45 +2203,51 @@ class Config {
1961
2203
  }
1962
2204
  config.wasInitialized = false
1963
2205
  // TODO change name of config: to baseConfig:
1964
- const kmFn = (name) => this.getConfig(name)
2206
+ const kmFn = (name) => {
2207
+ const config = this.getConfig(name)
2208
+ return config
2209
+ }
1965
2210
  // const hierarchy = new DigraphInternal((config.config || {}).hierarchy)
1966
- const args = {
2211
+ const args = new Object(setupInitializerFNArgs(this, {
1967
2212
  isModule,
1968
- addWord: aw,
1969
- km: kmFn,
1970
2213
  hierarchy: this.hierarchy,
1971
- config,
1972
- baseConfig: this,
2214
+ testConfig: config,
1973
2215
  currentConfig: config,
1974
2216
  uuid: config._uuid,
1975
2217
  objects: namespacedObjects,
1976
2218
  namespace,
1977
- motivation: (m) => config.addMotivation(m),
1978
- api: config.api
2219
+ api: config.api,
2220
+ }))
2221
+
2222
+ const currentConfig = args.currentConfig
2223
+
2224
+ /*
2225
+ if (args.currentConfig.api) {
2226
+ // args.currentConfig.api.objects = args.objects
2227
+ // TODO assign pseudo config?
2228
+ // args.currentConfig.api.config = () => args.baseConfig
2229
+ // args.currentConfig.api.uuid = args.currentConfig._uuid
2230
+ args.currentConfig.wasInitialized = true
1979
2231
  }
1980
- config.initializerFn(args)
1981
- if (config.initAfterApi) {
1982
- // reverse the list
2232
+ */
2233
+ // debugger
2234
+ // greg55
2235
+ config.initializerFn(args, { dontCallFn: true })
1983
2236
  initAfterApis.unshift({ config, args })
1984
- } else {
1985
- if (interleaved) {
1986
- initAfterApis.unshift(null)
1987
- }
1988
- }
1989
- // greg
1990
2237
  if (config._api) {
1991
2238
  if (config._api.initialize) {
1992
2239
  // reverse the list
1993
- inits.unshift( () => config._api.initialize({ config: this, km: kmFn, api: config._api }) )
2240
+ // TODO sync up the args with initialize of config
2241
+ inits.unshift( () => config._api.initialize({ config: this, km: kmFn, ...args, api: config._api }) )
1994
2242
  // config._api.initialize({ config, api: config._api })
1995
2243
  } else {
1996
2244
  if (interleaved) {
1997
2245
  inits.unshift(null)
1998
2246
  }
1999
2247
  }
2000
- config._api.objects = namespacedObjects
2001
- config._api.config = () => this
2002
- config._api.uuid = config._uuid
2248
+ // config._api.objects = namespacedObjects
2249
+ // config._api.config = () => this
2250
+ // config._api.uuid = config._uuid
2003
2251
  } else {
2004
2252
  if (interleaved) {
2005
2253
  inits.unshift(null)
@@ -2041,10 +2289,9 @@ class Config {
2041
2289
  init()
2042
2290
  }
2043
2291
  for (let init of initAfterApis) {
2044
- // init.args.isAfterApi = true
2045
2292
  init.config.initializerFn({ ...init.args, kms: this.getConfigs(), isAfterApi: true })
2046
2293
  }
2047
- this.instances.forEach((instance) => client.processInstance(this, instance))
2294
+ this.instances.forEach((instance) => client.loadInstance(this, instance))
2048
2295
  } else {
2049
2296
  const base = {
2050
2297
  operators: this.config.operators,
@@ -2073,19 +2320,19 @@ class Config {
2073
2320
  }
2074
2321
  // console.log('name -------------', name)
2075
2322
  if (inits[i]) {
2323
+ // greg55
2076
2324
  inits[i]()
2077
2325
  }
2078
2326
  if (initAfterApis[i]) {
2079
2327
  const init = initAfterApis[i]
2080
- init.config.initializerFn({ ...init.args, kms: this.getConfigs(), isAfterApi: true })
2328
+ init.config.initializerFn({ ...init.args, kms: this.getConfigs(), isAfterApi: true})
2081
2329
  }
2082
2330
  const instance = this.instances.find((instance) => instance.name == name)
2083
2331
  if (instance) {
2084
- client.processInstance(this, instance)
2332
+ client.loadInstance(this, instance)
2085
2333
  }
2086
2334
  this.hierarchy.edges = this.config.hierarchy
2087
2335
  }
2088
- // this.instances.forEach((instance) => client.processInstance(this, instance))
2089
2336
  }
2090
2337
 
2091
2338
  if (reverseIt) {
@@ -2208,13 +2455,9 @@ class Config {
2208
2455
  }
2209
2456
  const km = (name) => this.getConfig(name)
2210
2457
  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 })
2458
+ config.initializerFn(setupInitializerFNArgs(this, { isModule: this.isModule, currentConfig: config, testConfig: config, objects: nsobjects, namespace, uuid }))
2214
2459
  } 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 })
2460
+ this.initializerFn(setupInitializerFNArgs(this, { isModule: this.isModule, currentConfig: this, testConfig: this, objects: nsobjects, namespace, uuid }))
2218
2461
  }
2219
2462
  })
2220
2463
  }
@@ -2271,10 +2514,18 @@ class Config {
2271
2514
 
2272
2515
  if (config.priorities) {
2273
2516
  let priorities = config.priorities
2274
- priorities = priorities.map((p) => {
2275
- return p.map((id) => {
2276
- return [toNS(id[0]), id[1]]
2277
- })
2517
+ priorities = priorities.map((cp) => {
2518
+ const { context, choose, ordered } = cp
2519
+ const priority = {
2520
+ context: context.map((id) => {
2521
+ return [toNS(id[0]), id[1]]
2522
+ }),
2523
+ choose,
2524
+ }
2525
+ if (ordered) {
2526
+ priority.ordered = ordered
2527
+ }
2528
+ return priority
2278
2529
  })
2279
2530
  config.priorities = priorities
2280
2531
  }
@@ -2473,7 +2724,8 @@ class Config {
2473
2724
  }
2474
2725
 
2475
2726
  // TODO get rid of useOldVersion arg
2476
- addInternal (more, { useOldVersion = true, skipObjects = false, includeNamespaces = true, allowNameToBeNull = false, handleCalculatedProps : hcps = false } = {}) {
2727
+ addInternal (more, { addFirst = false, useOldVersion = true, skipObjects = false, includeNamespaces = true, allowNameToBeNull = false, handleCalculatedProps : hcps = false } = {}) {
2728
+ validConfigProps(more)
2477
2729
  if (more instanceof Config) {
2478
2730
  more.initialize({ force: false })
2479
2731
  if (useOldVersion) {
@@ -2483,8 +2735,10 @@ class Config {
2483
2735
  more = _.cloneDeep(more.initConfig)
2484
2736
  }
2485
2737
  }
2738
+ debugConfigProps(more)
2739
+
2486
2740
  if (hcps) {
2487
- handleCalculatedProps(this, more)
2741
+ handleCalculatedProps(this, more, addFirst)
2488
2742
  applyUUID(more, this._uuid)
2489
2743
  }
2490
2744
  for (const key of Object.keys(more)) {
@@ -2509,7 +2763,11 @@ class Config {
2509
2763
  if (!configWords[word]) {
2510
2764
  configWords[word] = []
2511
2765
  }
2512
- configWords[word] = configWords[word].concat(moreWords[word])
2766
+ if (addFirst) {
2767
+ configWords[word] = moreWords[word].concat(configWords[word])
2768
+ } else {
2769
+ configWords[word] = configWords[word].concat(moreWords[word])
2770
+ }
2513
2771
  }
2514
2772
  } else if (key === 'name') {
2515
2773
  /*
@@ -2571,13 +2829,18 @@ class Config {
2571
2829
  this.config[key].splice(iOldOne, 1)
2572
2830
  break;
2573
2831
  }
2574
- }
2832
+ }
2575
2833
  }
2576
2834
  }
2577
2835
  }
2836
+
2578
2837
  // console.log('key', key, 'XXX')
2579
2838
  // console.log('more', JSON.stringify(more, null, 2))
2580
- this.config[key] = this.config[key].concat(more[key])
2839
+ if (addFirst) {
2840
+ this.config[key] = more[key].concat(this.config[key])
2841
+ } else {
2842
+ this.config[key] = this.config[key].concat(more[key])
2843
+ }
2581
2844
  } else {
2582
2845
  if (!(key in this.config)) {
2583
2846
  throw new Error(`Unexpected property in config ${key}`)
@@ -2708,5 +2971,8 @@ class Config {
2708
2971
  }
2709
2972
 
2710
2973
  module.exports = {
2711
- Config
2974
+ Config,
2975
+ config_toServer,
2976
+ operatorKey_valid,
2977
+ handleBridgeProps,
2712
2978
  }