theprogrammablemind 7.5.8 → 7.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/flatten.js CHANGED
@@ -89,7 +89,15 @@ const flatten = (markers, value) => {
89
89
  const split = markers.includes(marker)
90
90
  if (split) {
91
91
  if ('value' in properties) {
92
- return [properties.value, true]
92
+ flattenedValues = []
93
+ for (let v of properties.value) {
94
+ if (v.flatten) {
95
+ flattenedValues = flattenedValues.concat(flatten(markers, v)[0])
96
+ } else {
97
+ flattenedValues.push(v)
98
+ }
99
+ }
100
+ return [flattenedValues, true]
93
101
  } else {
94
102
  return [[value], false]
95
103
  }
package/src/generators.js CHANGED
@@ -75,7 +75,7 @@ class Generator {
75
75
 
76
76
  apply (baseArgs, objects, g, gs, context, hierarchy, config, response, log, options = {}) {
77
77
  if (!log) {
78
- throw 'generators.apply argument log is required'
78
+ throw new Error('generators.apply argument log is required')
79
79
  }
80
80
  if (baseArgs.call && config && sbaseArgs.calls.stack.length > config.maxDepth) {
81
81
  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`)
@@ -292,7 +292,7 @@ class Generators {
292
292
  lines.setElement(0, 2, JSON.stringify(context, null, 2))
293
293
  this.logs.push(lines.toString())
294
294
  }
295
- contextsPrime.push(generated)
295
+ contextsPrime.push((config || {}).parenthesized ? "(" + generated + ")" : generated)
296
296
  }
297
297
  return contextsPrime
298
298
  }
package/src/helpers.js CHANGED
@@ -43,6 +43,16 @@ const appendNoDups = (l1, l2) => {
43
43
  }
44
44
  }
45
45
 
46
+ const safeNoDups = (list) => {
47
+ noDups = []
48
+ for (const element of list) {
49
+ if (!noDups.find((e) => safeEquals(e, element))) {
50
+ noDups.push(element)
51
+ }
52
+ }
53
+ return noDups
54
+ }
55
+
46
56
  const safeEquals = (v1, v2) => {
47
57
  if (typeof v1 !== typeof v2) {
48
58
  return false
@@ -53,7 +63,12 @@ const safeEquals = (v1, v2) => {
53
63
  return v1 == v2
54
64
  } else if (type == 'function') {
55
65
  return v1.toString() == v2.toString()
66
+ } else if (v1 == undefined || v2 == undefined) {
67
+ return v1 == v2
56
68
  } else {
69
+ if (v1.length != v2.length) {
70
+ return false
71
+ }
57
72
  for (let key in v1) {
58
73
  if (!safeEquals(v1[key], v2[key])) {
59
74
  return false
@@ -167,15 +182,13 @@ const isCompound = (value) => {
167
182
  return isArray(value) || isObject(value)
168
183
  }
169
184
 
170
- nextCallId = 0
171
- nextContextId = 0
172
-
173
185
  class InitCalls {
174
186
 
175
- constructor() {
187
+ constructor(name) {
176
188
  this.nextCallId = 0
177
189
  this.nextContextId = 0
178
190
  this.stack = []
191
+ this.name = name
179
192
  }
180
193
 
181
194
  start() {
@@ -192,13 +205,14 @@ class InitCalls {
192
205
  // this.nextCallId += 1
193
206
  // this.stack.push(this.nextCallId)
194
207
  this.stack.push(this.nextCallId)
195
- const calls = this.stack.map( (call) => `call${call}` )
208
+ // TODO put the nextContextId in the context for debugging
209
+ const calls = this.stack.map( (call) => `${this.name}#call${call}` )
196
210
  // return `Context#${this.nextContextId}: ${calls}`
197
- return `Context#${nextContextId}: ${calls}`
211
+ return `Context#${this.nextContextId}: ${calls}`
198
212
  }
199
213
 
200
214
  current() {
201
- return `call${this.stack[this.stack.length-1]}`
215
+ return `${this.name}#call${this.stack[this.stack.length-1]}`
202
216
  }
203
217
 
204
218
  touch(context) {
@@ -234,13 +248,17 @@ const validProps = (valids, object, type) => {
234
248
  for (let prop of Object.keys(object)) {
235
249
  let okay = false
236
250
  for (valid of valids) {
237
- if (prop.match(valid)) {
238
- okay = true
251
+ if (typeof valid == 'string') {
252
+ okay = prop == valid
253
+ } else {
254
+ okay = prop.match(valid)
255
+ }
256
+ if (okay) {
239
257
  break
240
258
  }
241
259
  }
242
260
  if (!okay) {
243
- throw `Unknown property "${prop}" in the ${type}. Valid properties are ${valids}. The ${type} is ${JSON.stringify(object)}`
261
+ throw new Error(`Unknown property "${prop}" in the ${type}. Valid properties are ${valids}. The ${type} is ${JSON.stringify(object)}`)
244
262
  }
245
263
  }
246
264
  }
@@ -308,12 +326,22 @@ const functionsToStrings = (config) => {
308
326
  return config
309
327
  }
310
328
 
329
+ const ecatch = (where, call) => {
330
+ try {
331
+ return call()
332
+ } catch( e ) {
333
+ throw new Error(`${where} ${e.stack}`)
334
+ }
335
+ }
336
+
311
337
  module.exports = {
338
+ ecatch,
312
339
  functionsToStrings,
313
340
  updateQueries,
314
341
  mapInPlace,
315
342
  validProps,
316
343
  args,
344
+ safeNoDups,
317
345
  safeEquals,
318
346
  appendNoDups,
319
347
  hashIndexesGet,
package/src/project.js ADDED
@@ -0,0 +1,81 @@
1
+
2
+ const project = (object, filter) => {
3
+ if (!object) {
4
+ return
5
+ }
6
+
7
+ let projection = {}
8
+ const set = (property, value) => {
9
+ if (value === null || value === undefined) {
10
+ return
11
+ }
12
+ projection[property] = value
13
+ }
14
+ if (Array.isArray(filter)) {
15
+ if (Array.isArray(object)) {
16
+ return object.map( element => project(element, filter) )
17
+ } else {
18
+ for (let properties of filter) {
19
+ if (typeof properties == 'object') {
20
+ if (properties.propertyLists) {
21
+ for (const propertyList in properties.propertyLists) {
22
+ if (object[propertyList]) {
23
+ for (const property of object[propertyList]) {
24
+ set(property, project(object[property], properties.propertyLists[propertyList]))
25
+ }
26
+ }
27
+ }
28
+ }
29
+ if (properties.valueLists) {
30
+ for (const listProperty in properties.valueLists) {
31
+ const old = object[listProperty]
32
+ if (Array.isArray(old)) {
33
+ set(listProperty, old.map((element) => project(element, properties.valueLists[listProperty])))
34
+ } else {
35
+ set(listProperty, project(old, properties.valueLists[listProperty]))
36
+ }
37
+ }
38
+ }
39
+ if (properties.properties) {
40
+ for (const property in properties.properties) {
41
+ const old = object[property]
42
+ set(property, project(old, properties.properties[property]))
43
+ }
44
+ }
45
+ if (properties.property) {
46
+ const property = properties.property
47
+ if (properties.isPropertyList) {
48
+ debugger
49
+ if (!Array.isArray(object[property])) {
50
+ return projection
51
+ }
52
+ for (const propertyRef of object[property]) {
53
+ const old = object[propertyRef]
54
+ set(propertyRef, project(old, properties.filter))
55
+ }
56
+ } else {
57
+ const old = object[property]
58
+ set(property, project(old, properties.filter))
59
+ }
60
+ }
61
+ } else {
62
+ if (!Array.isArray(properties)) {
63
+ properties = [properties]
64
+ }
65
+ for (const property of properties) {
66
+ set(property, object[property])
67
+ }
68
+ }
69
+ }
70
+ }
71
+ } else if (typeof filter == 'object') {
72
+ for (const property of Object.keys(filter)) {
73
+ set(property, project(object[property], filter[property]))
74
+ }
75
+ }
76
+ return projection
77
+ }
78
+
79
+ module.exports = { project }
80
+
81
+
package/src/semantics.js CHANGED
@@ -6,7 +6,7 @@ class Semantic {
6
6
  // constructor ({match, apply, uuid, index, km, notes}) {
7
7
  constructor (semantic) {
8
8
  semantic = normalizeSemantic(semantic)
9
- const { match, apply, uuid, index, km, notes, priority, debug, where, applyWrapped, property } = semantic
9
+ const { match, apply, uuid, index, km, notes, priority, debug, where, applyWrapped, property, oneShot, id } = semantic
10
10
  this.matcher = match
11
11
  this._apply = apply
12
12
  this._applyWrapped = applyWrapped
@@ -18,6 +18,8 @@ class Semantic {
18
18
  this.notes = notes
19
19
  this.callId = debug
20
20
  this.where = where
21
+ this.oneShot = oneShot
22
+ this.id = id
21
23
  }
22
24
 
23
25
  toLabel () {
@@ -89,7 +91,7 @@ class Semantic {
89
91
  // const ask = baseArgs.getAsk(this.uuid)
90
92
  if (!log) {
91
93
  console.trace()
92
- throw 'log is a required argument'
94
+ throw new Error('log is a required argument')
93
95
  }
94
96
  const contextPrime = Object.assign({}, context)
95
97
  let n = (id) => id
@@ -197,6 +199,9 @@ class Semantics {
197
199
  const log = (message) => { this.logs.push(message) }
198
200
  try {
199
201
  contextPrime = semantic.apply(args, context, s, log, options)
202
+ if (!contextPrime.controlKeepMotivation && semantic.oneShot) {
203
+ args.config.removeSemantic(semantic)
204
+ }
200
205
  } catch( e ) {
201
206
  contextPrime = null
202
207
  let errorMessage
@@ -237,7 +242,7 @@ class Semantics {
237
242
  // this.logs.push(message)
238
243
  // return [message]
239
244
  args.calls.pop()
240
- throw { error: [message], logs: this.logs }
245
+ throw { error: [message], logs: this.logs, reason: e.reason }
241
246
  }
242
247
  args.calls.touch(contextPrime)
243
248
  // this.logs.push(`Semantics: applied ${semantic.toString()}\n to\n ${JSON.stringify(context)}\n the result was ${JSON.stringify(contextPrime)}\n`)