theprogrammablemind 8.0.0 → 8.1.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/semantics.js CHANGED
@@ -49,82 +49,55 @@ class Semantic {
49
49
  }
50
50
  }
51
51
 
52
- matches (baseArgs, context, options = {}) {
53
- const hierarchy = baseArgs.hierarchy
54
- const config = baseArgs.config
55
-
56
- const objects = baseArgs.getObjects(this.uuid)
57
- // const ask = baseArgs.getAsk(this.uuid)
58
-
59
- // return this.matcher(Object.assign({}, argsBase, {args: contextArgs(context, hierarchy), objects: objects, global: objects, context: context, hierarchy: hierarchy, api: this.getAPI(config)})
60
- const callId = baseArgs.calls.current()
61
- const moreArgs = {
62
- uuid: this.uuid,
63
- args: contextArgs(context, hierarchy),
64
- objects,
65
- global: objects,
66
- context: context,
67
- // hierarchy: hierarchy,
68
- callId,
69
- api: this.getAPI(config),
70
- apis: this.getAPIs(config)
52
+ fixUpArgs (args, context) {
53
+ args.uuid = this.uuid
54
+ args.callId = args.calls.current()
55
+ const objects = args.getObjects(this.uuid)
56
+ args.objects = objects
57
+ args.global = objects
58
+ const config = args.config
59
+ args.api = this.getAPI(config)
60
+ args.apis = this.getAPIs(config)
61
+ args.args = contextArgs(context, args.hierarchy)
62
+ args.context = context
63
+ let n = (id) => id
64
+ if (config && 'nsToString' in config) {
65
+ n = (id) => config.nsToString(id)
71
66
  }
72
- const args = Object.assign({}, baseArgs, moreArgs, (baseArgs.getUUIDScoped || (() => { return {} }))(this.uuid))
67
+ args.n = n
68
+ args.uuid = this.uuid
69
+ Object.assign(args, (args.getUUIDScoped || (() => { return {} }))(this.uuid))
70
+ }
73
71
 
74
- const matches = this.matcher(args)
75
- if (matches && (options.debug || {}).match ||
76
- callId == this.callId) {
72
+ async matches (args, context, options = {}) {
73
+ this.fixUpArgs(args, context)
74
+ const matches = await this.matcher(args)
75
+ if (matches && (options.debug || {}).match || args.callId == this.callId) {
77
76
  debugger // next line is the matcher
78
- this.matcher(args)
77
+ await this.matcher(args)
79
78
  }
80
79
  return matches
81
80
  }
82
81
 
83
- apply (baseArgs, context, s, log, options = {}) {
84
- const { hierarchy, config, response } = baseArgs
85
- const objects = baseArgs.getObjects(this.uuid)
82
+ async apply (args, context, s, options = {}) {
83
+ const { config } = args
86
84
  if (config && config.debugLoops) {
87
85
  console.log('apply', this.toLabel())
88
86
  }
89
- if (baseArgs.calls && config && baseArgs.calls.stack.length > config.maxDepth) {
87
+ if (args.calls && config && args.calls.stack.length > config.maxDepth) {
90
88
  throw new Error(`Max depth of ${config.maxDepth} for calls has been exceeded. maxDepth can be set on the config object. To see the calls run with the --dl or set the debugLoops property on the config`)
91
89
  }
92
90
 
93
- // const ask = baseArgs.getAsk(this.uuid)
94
- if (!log) {
95
- console.trace()
96
- throw new Error('log is a required argument')
97
- }
98
91
  const contextPrime = Object.assign({}, context)
99
- let n = (id) => id
100
- if (config && 'nsToString' in config) {
101
- n = (id) => config.nsToString(id)
102
- }
103
- const callId = baseArgs.calls.current()
104
- const moreArgs = {
105
- uuid: this.uuid,
106
- callId,
107
- args: contextArgs(context, hierarchy),
108
- objects,
109
- log,
110
- global: objects,
111
- n,
112
- context: contextPrime,
113
- uuid: this.uuid,
114
- // config,
115
- response,
116
- api: this.getAPI(config),
117
- apis: this.getAPIs(config)
118
- }
119
- const args = Object.assign({}, baseArgs, moreArgs, (baseArgs.getUUIDScoped || (() => { return {} }))(this.uuid))
120
- if ((options.debug || {}).apply ||
121
- callId == this.callId) {
92
+ this.fixUpArgs(args, contextPrime)
93
+
94
+ if ((options.debug || {}).apply || args.callId == this.callId) {
122
95
  debugger
123
96
  }
124
97
  if (args.breakOnSemantics) {
125
98
  debugger
126
99
  }
127
- this._apply(args)
100
+ await this._apply(args)
128
101
  return contextPrime
129
102
  }
130
103
  }
@@ -176,11 +149,12 @@ class Semantics {
176
149
  return this.semantics[maxOrdinal]
177
150
  }
178
151
 
179
- applyToContext (args, context, options) {
152
+ async applyToContext (args, context, options) {
180
153
  // let context_prime = {}
181
154
  if (!(context instanceof Array || context instanceof Object)) {
182
155
  return context
183
156
  }
157
+ args = { ...args }
184
158
  const config = args.config
185
159
  let contextPrime = Object.assign({}, context)
186
160
  const s = (context, options) => this.apply(args, context, options)
@@ -188,6 +162,8 @@ class Semantics {
188
162
  const stack = args.calls.push()
189
163
  let counter = 0
190
164
  let seenQuestion = false
165
+ const deferred = []
166
+ args.log = (message) => { this.logs.push(message) }
191
167
  for (const isemantic in this.semantics) {
192
168
  const semantic = this.semantics[isemantic]
193
169
  if (!semantic) {
@@ -197,22 +173,33 @@ class Semantics {
197
173
  if (semantic.isQuestion && seenQuestion) {
198
174
  continue
199
175
  }
200
- if (semantic.matches(args, context, options)) {
176
+ if (await semantic.matches(args, context, options)) {
201
177
  if (!this.calls[counter]) {
202
178
  this.calls[counter] = 0
203
179
  }
204
180
  this.calls[counter] += 1
205
- const log = (message) => { this.logs.push(message) }
206
181
  try {
207
- contextPrime = semantic.apply(args, context, s, log, options)
182
+ let deferWasCalled = false
183
+ const defer = (listener) => {
184
+ deferred.push({ semantic, listener })
185
+ deferWasCalled = true
186
+ }
187
+ args.defer = defer
188
+ contextPrime = await semantic.apply(args, context, s, options)
189
+ if (deferWasCalled) {
190
+ continue
191
+ }
208
192
  if (!contextPrime.controlKeepMotivation && semantic.oneShot) {
209
193
  // semantic.tied_ids.forEach((tied_id) => args.config.removeSemantic(tied_id))
210
194
  args.config.removeSemantic(semantic)
211
195
  }
196
+ for (const { listener } of deferred) {
197
+ listener(args)
198
+ }
212
199
  } catch (e) {
213
200
  contextPrime = null
214
201
  let errorMessage
215
- e.retryCall = () => semantic.apply(args, context, s, log, options)
202
+ e.retryCall = () => semantic.apply(args, context, s, options)
216
203
  const help = 'The error has a retryCall property that will recall the function that failed.'
217
204
  if (e.stack && e.message) {
218
205
  const info = `${semantic.notes ? semantic.notes : ''}${semantic.where ? semantic.where : ''}`
@@ -280,6 +267,13 @@ class Semantics {
280
267
  lines.setElement(0, 1, 'RESULT')
281
268
  lines.setElement(0, 2, `(HASHCODE ${helpers.hashCode(JSON.stringify(helpers.sortJson(contextPrime, { depth: 25 })))})`)
282
269
  lines.setElement(1, 2, JSON.stringify(contextPrime, null, 2))
270
+ for (const { semantic } of deferred) {
271
+ lines.setElement(0, 1, 'DEFERRED')
272
+ lines.setElement(0, 2, semantic.toLabel())
273
+ lines.newRow()
274
+ lines.setElement(0, 2, semantic.toString())
275
+ lines.newRow()
276
+ }
283
277
  this.logs.push(lines.toString())
284
278
  }
285
279
  applied = true
@@ -310,19 +304,19 @@ class Semantics {
310
304
  return contextPrime
311
305
  }
312
306
 
313
- applyToContexts (args, contexts, options) {
307
+ async applyToContexts (args, contexts, options) {
314
308
  const contextsPrime = []
315
- contexts.forEach((context) => {
316
- contextsPrime.push(this.applyToContext(args, context, options))
317
- })
309
+ for (const context of contexts) {
310
+ contextsPrime.push(await this.applyToContext(args, context, options))
311
+ }
318
312
  return contextsPrime
319
313
  }
320
314
 
321
- apply (args, context, options) {
315
+ async apply (args, context, options) {
322
316
  if (Array.isArray(context)) {
323
- return this.applyToContexts(args, context, options)
317
+ return await this.applyToContexts(args, context, options)
324
318
  } else if (context instanceof Object) {
325
- return this.applyToContext(args, context, options)
319
+ return await this.applyToContext(args, context, options)
326
320
  } else {
327
321
  return context
328
322
  }