@coasys/ad4m 0.8.0-fix → 0.8.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.
Files changed (161) hide show
  1. package/README.md +229 -229
  2. package/jest.config.js +5 -5
  3. package/jsdoc.json +27 -27
  4. package/lib/index.cjs +4522 -4571
  5. package/lib/index.js +4522 -4571
  6. package/lib/shims/type-graphql.d.ts +32 -32
  7. package/lib/shims/type-graphql.js +70 -70
  8. package/lib/src/Ad4mClient.d.ts +26 -26
  9. package/lib/src/Ad4mClient.js +66 -66
  10. package/lib/src/Address.d.ts +1 -1
  11. package/lib/src/Address.js +1 -1
  12. package/lib/src/DID.d.ts +1 -1
  13. package/lib/src/DID.js +1 -1
  14. package/lib/src/Exception.d.ts +6 -6
  15. package/lib/src/Exception.js +7 -7
  16. package/lib/src/Literal.d.ts +7 -7
  17. package/lib/src/Literal.js +76 -76
  18. package/lib/src/PubSub.d.ts +14 -14
  19. package/lib/src/PubSub.js +14 -14
  20. package/lib/src/SmartLiteral.d.ts +12 -12
  21. package/lib/src/SmartLiteral.js +74 -74
  22. package/lib/src/agent/Agent.d.ts +104 -104
  23. package/lib/src/agent/Agent.js +337 -337
  24. package/lib/src/agent/AgentClient.d.ts +57 -57
  25. package/lib/src/agent/AgentClient.js +431 -431
  26. package/lib/src/agent/AgentResolver.d.ts +30 -30
  27. package/lib/src/agent/AgentResolver.js +325 -325
  28. package/lib/src/agent/AgentStatus.d.ts +8 -8
  29. package/lib/src/agent/AgentStatus.js +61 -61
  30. package/lib/src/buildSchema.d.ts +1 -1
  31. package/lib/src/buildSchema.js +36 -36
  32. package/lib/src/expression/Expression.d.ts +28 -28
  33. package/lib/src/expression/Expression.js +143 -143
  34. package/lib/src/expression/ExpressionClient.d.ts +13 -13
  35. package/lib/src/expression/ExpressionClient.js +102 -102
  36. package/lib/src/expression/ExpressionRef.d.ts +9 -9
  37. package/lib/src/expression/ExpressionRef.js +67 -67
  38. package/lib/src/expression/ExpressionResolver.d.ts +10 -10
  39. package/lib/src/expression/ExpressionResolver.js +106 -106
  40. package/lib/src/index.d.ts +30 -30
  41. package/lib/src/index.js +30 -30
  42. package/lib/src/jestSetup.d.ts +1 -1
  43. package/lib/src/jestSetup.js +1 -1
  44. package/lib/src/language/Icon.d.ts +4 -4
  45. package/lib/src/language/Icon.js +24 -24
  46. package/lib/src/language/Language.d.ts +195 -195
  47. package/lib/src/language/Language.js +79 -79
  48. package/lib/src/language/LanguageClient.d.ts +17 -17
  49. package/lib/src/language/LanguageClient.js +137 -137
  50. package/lib/src/language/LanguageContext.d.ts +37 -37
  51. package/lib/src/language/LanguageContext.js +2 -2
  52. package/lib/src/language/LanguageHandle.d.ts +9 -9
  53. package/lib/src/language/LanguageHandle.js +41 -41
  54. package/lib/src/language/LanguageMeta.d.ts +35 -35
  55. package/lib/src/language/LanguageMeta.js +89 -89
  56. package/lib/src/language/LanguageRef.d.ts +6 -6
  57. package/lib/src/language/LanguageRef.js +30 -30
  58. package/lib/src/language/LanguageResolver.d.ts +19 -19
  59. package/lib/src/language/LanguageResolver.js +143 -143
  60. package/lib/src/links/Links.d.ts +39 -39
  61. package/lib/src/links/Links.js +148 -148
  62. package/lib/src/links/Links.test.d.ts +1 -1
  63. package/lib/src/links/Links.test.js +33 -33
  64. package/lib/src/neighbourhood/Neighbourhood.d.ts +10 -10
  65. package/lib/src/neighbourhood/Neighbourhood.js +38 -38
  66. package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +25 -25
  67. package/lib/src/neighbourhood/NeighbourhoodClient.js +256 -256
  68. package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +19 -19
  69. package/lib/src/neighbourhood/NeighbourhoodProxy.js +54 -54
  70. package/lib/src/neighbourhood/NeighbourhoodResolver.d.ts +24 -24
  71. package/lib/src/neighbourhood/NeighbourhoodResolver.js +180 -180
  72. package/lib/src/perspectives/LinkQuery.d.ts +11 -11
  73. package/lib/src/perspectives/LinkQuery.js +80 -80
  74. package/lib/src/perspectives/Perspective.d.ts +38 -38
  75. package/lib/src/perspectives/Perspective.js +144 -144
  76. package/lib/src/perspectives/Perspective.test.d.ts +1 -1
  77. package/lib/src/perspectives/Perspective.test.js +20 -20
  78. package/lib/src/perspectives/PerspectiveClient.d.ts +55 -54
  79. package/lib/src/perspectives/PerspectiveClient.js +418 -410
  80. package/lib/src/perspectives/PerspectiveDiff.d.ts +9 -9
  81. package/lib/src/perspectives/PerspectiveDiff.js +33 -33
  82. package/lib/src/perspectives/PerspectiveHandle.d.ts +16 -16
  83. package/lib/src/perspectives/PerspectiveHandle.js +59 -59
  84. package/lib/src/perspectives/PerspectiveProxy.d.ts +178 -178
  85. package/lib/src/perspectives/PerspectiveProxy.js +501 -540
  86. package/lib/src/perspectives/PerspectiveResolver.d.ts +38 -37
  87. package/lib/src/perspectives/PerspectiveResolver.js +376 -362
  88. package/lib/src/runtime/RuntimeClient.d.ts +33 -33
  89. package/lib/src/runtime/RuntimeClient.js +263 -263
  90. package/lib/src/runtime/RuntimeResolver.d.ts +47 -47
  91. package/lib/src/runtime/RuntimeResolver.js +328 -328
  92. package/lib/src/schema.gql +1 -0
  93. package/lib/src/subject/SDNADecorators.d.ts +45 -45
  94. package/lib/src/subject/SDNADecorators.js +250 -250
  95. package/lib/src/subject/Subject.d.ts +7 -7
  96. package/lib/src/subject/Subject.js +160 -160
  97. package/lib/src/subject/SubjectEntity.d.ts +31 -31
  98. package/lib/src/subject/SubjectEntity.js +250 -250
  99. package/lib/src/subject/util.d.ts +12 -12
  100. package/lib/src/subject/util.js +73 -73
  101. package/lib/src/typeDefs.d.ts +1 -1
  102. package/lib/src/typeDefs.js +1 -0
  103. package/lib/src/unwrapApolloResult.d.ts +2 -2
  104. package/lib/src/unwrapApolloResult.js +14 -12
  105. package/lib/src/utils.d.ts +2 -2
  106. package/lib/src/utils.js +21 -21
  107. package/package.json +61 -61
  108. package/rollup.config.js +26 -26
  109. package/shims/type-graphql.ts +75 -75
  110. package/src/Ad4mClient.test.ts +1089 -1084
  111. package/src/Ad4mClient.ts +62 -62
  112. package/src/Address.ts +1 -1
  113. package/src/Exception.ts +6 -6
  114. package/src/Literal.test.ts +30 -30
  115. package/src/Literal.ts +80 -80
  116. package/src/PubSub.ts +13 -13
  117. package/src/SmartLiteral.ts +74 -74
  118. package/src/agent/Agent.ts +293 -293
  119. package/src/agent/AgentClient.ts +531 -531
  120. package/src/agent/AgentResolver.ts +263 -263
  121. package/src/agent/AgentStatus.ts +43 -43
  122. package/src/buildSchema.ts +41 -41
  123. package/src/expression/Expression.ts +99 -99
  124. package/src/expression/ExpressionClient.ts +98 -98
  125. package/src/expression/ExpressionRef.ts +62 -62
  126. package/src/expression/ExpressionResolver.ts +63 -63
  127. package/src/index.ts +30 -30
  128. package/src/language/Icon.ts +10 -10
  129. package/src/language/Language.ts +266 -266
  130. package/src/language/LanguageClient.ts +159 -159
  131. package/src/language/LanguageContext.ts +37 -37
  132. package/src/language/LanguageHandle.ts +23 -23
  133. package/src/language/LanguageMeta.ts +69 -69
  134. package/src/language/LanguageRef.ts +18 -18
  135. package/src/language/LanguageResolver.ts +98 -98
  136. package/src/links/Links.test.ts +39 -39
  137. package/src/links/Links.ts +107 -107
  138. package/src/neighbourhood/Neighbourhood.ts +22 -22
  139. package/src/neighbourhood/NeighbourhoodClient.ts +275 -275
  140. package/src/neighbourhood/NeighbourhoodProxy.ts +58 -58
  141. package/src/neighbourhood/NeighbourhoodResolver.ts +99 -99
  142. package/src/perspectives/LinkQuery.ts +65 -65
  143. package/src/perspectives/Perspective.test.ts +22 -22
  144. package/src/perspectives/Perspective.ts +118 -118
  145. package/src/perspectives/PerspectiveClient.ts +452 -443
  146. package/src/perspectives/PerspectiveDiff.ts +15 -15
  147. package/src/perspectives/PerspectiveHandle.ts +39 -39
  148. package/src/perspectives/PerspectiveProxy.ts +576 -610
  149. package/src/perspectives/PerspectiveResolver.ts +224 -219
  150. package/src/runtime/RuntimeClient.ts +282 -282
  151. package/src/runtime/RuntimeResolver.ts +192 -192
  152. package/src/subject/SDNADecorators.ts +333 -333
  153. package/src/subject/Subject.ts +152 -152
  154. package/src/subject/SubjectEntity.ts +291 -291
  155. package/src/subject/util.ts +84 -84
  156. package/src/typeDefs.ts +5 -5
  157. package/src/unwrapApolloResult.ts +15 -13
  158. package/src/utils.ts +25 -25
  159. package/tsconfig-browser.json +82 -82
  160. package/tsconfig.json +80 -80
  161. package/yarn-error.log +0 -22814
@@ -1,334 +1,334 @@
1
- import { PerspectiveProxy } from "../perspectives/PerspectiveProxy";
2
- import { Subject } from "./Subject";
3
- import { capitalize, propertyNameToSetterName, singularToPlural, stringifyObjectLiteral } from "./util";
4
-
5
- export class PerspectiveAction {
6
- action: string
7
- source: string
8
- predicate: string
9
- target: string
10
- }
11
-
12
- export function addLink(source: string, predicate: string, target: string): PerspectiveAction {
13
- return {
14
- action: "addLink",
15
- source,
16
- predicate,
17
- target,
18
- };
19
- }
20
-
21
- export function hasLink(predicate: string): string {
22
- return `triple(this, "${predicate}", _)`
23
- }
24
-
25
- interface InstanceQueryParams {
26
- where?: object;
27
- condition?: string;
28
- }
29
-
30
- export function instanceQuery(options?: InstanceQueryParams) {
31
- return function <T>(target: T, key: keyof T, descriptor: PropertyDescriptor) {
32
- const originalMethod = descriptor.value;
33
- if(typeof originalMethod !== "function") {
34
- throw new Error("instanceQuery decorator can only be applied to methods");
35
- }
36
-
37
- descriptor.value = async function(perspective: PerspectiveProxy): Promise<T[]> {
38
- let instances: T[] = []
39
- //@ts-ignore
40
- let subjectClassName = target.name
41
- let query = `subject_class("${subjectClassName}", C), instance(C, Instance)`
42
- if(options && options.where) {
43
- for(let prop in options.where) {
44
- let value = options.where[prop]
45
- query += `, property_getter(C, Instance, "${prop}", "${value}")`
46
- }
47
- }
48
-
49
- if(options && options.condition) {
50
- query += ', ' + options.condition
51
- }
52
-
53
- let results = await perspective.infer(query)
54
- if(results == false) {
55
- return instances
56
- }
57
- if(typeof results == "string") {
58
- throw results
59
- }
60
- for(let result of results) {
61
- let instance = result.Instance
62
- let subject = new Subject(perspective, instance, subjectClassName)
63
- await subject.init()
64
- instances.push(subject as T)
65
- }
66
-
67
- return instances
68
- }
69
- };
70
- }
71
-
72
-
73
- interface PropertyOptions {
74
- through?: string;
75
- initial?: string,
76
- required?: boolean,
77
- writable?: boolean,
78
- resolveLanguage?: string;
79
- getter?: string;
80
- setter?: string;
81
- local?: boolean
82
- }
83
- export function subjectProperty(opts: PropertyOptions) {
84
- return function <T>(target: T, key: keyof T) {
85
- target["__properties"] = target["__properties"] || {};
86
- target["__properties"][key] = target["__properties"][key] || {};
87
- target["__properties"][key] = { ...target["__properties"][key], ...opts }
88
-
89
- if (opts.writable) {
90
- const value = key as string
91
- target[`set${capitalize(value)}`] = () => {}
92
- }
93
-
94
- Object.defineProperty(target, key, {configurable: true, writable: true});
95
- };
96
- }
97
-
98
- interface FlagOptions {
99
- through: string;
100
- value: string;
101
- }
102
- export function subjectFlag(opts: FlagOptions) {
103
- return function <T>(target: T, key: keyof T) {
104
- target["__properties"] = target["__properties"] || {};
105
- target["__properties"][key] = target["__properties"][key] || {};
106
- target["__properties"][key] = {
107
- ...target["__properties"][key],
108
- through: opts.through,
109
- required: true,
110
- initial: opts.value,
111
- flag: true
112
- }
113
-
114
- // @ts-ignore
115
- target[key] = opts.value;
116
-
117
- Object.defineProperty(target, key, {configurable: true, writable: true});
118
- };
119
- }
120
-
121
- interface WhereOptions {
122
- isInstance?: any
123
- condition?: string
124
- }
125
- interface CollectionOptions {
126
- through: string,
127
- where?: WhereOptions,
128
- local?: boolean
129
- }
130
-
131
- export function subjectCollection(opts: CollectionOptions) {
132
- return function <T>(target: T, key: keyof T) {
133
- target["__collections"] = target["__collections"] || {};
134
- target["__collections"][key] = opts;
135
-
136
- const value = key as string
137
- target[`add${capitalize(value)}`] = () => {}
138
- target[`remove${capitalize(value)}`] = () => {}
139
- target[`setCollection${capitalize(value)}`] = () => {}
140
-
141
- Object.defineProperty(target, key, {configurable: true, writable: true});
142
- };
143
- }
144
-
145
- export function makeRandomPrologAtom(length: number): string {
146
- let result = '';
147
- let characters = 'abcdefghijklmnopqrstuvwxyz';
148
- let charactersLength = characters.length;
149
- for (let i = 0; i < length; i++) {
150
- result += characters.charAt(Math.floor(Math.random() * charactersLength));
151
- }
152
- return result;
153
- }
154
-
155
- interface SDNAClassOptions {
156
- name: string;
157
- }
158
-
159
- export function SDNAClass(opts: SDNAClassOptions) {
160
- return function (target: any) {
161
- target.prototype.className = opts.name;
162
- target.className = opts.name;
163
-
164
- target.generateSDNA = function() {
165
- let sdna = ""
166
- let subjectName = opts.name
167
- let obj = target.prototype;
168
-
169
- let uuid = makeRandomPrologAtom(8)
170
-
171
- sdna += `subject_class("${subjectName}", ${uuid}).\n`
172
-
173
-
174
- let classRemoverActions = []
175
-
176
- let constructorActions = []
177
- if(obj.subjectConstructor && obj.subjectConstructor.length) {
178
- constructorActions = constructorActions.concat(obj.subjectConstructor)
179
- }
180
-
181
- let instanceConditions = []
182
- if(obj.isSubjectInstance && obj.isSubjectInstance.length) {
183
- instanceConditions = instanceConditions.concat(obj.isSubjectInstance)
184
- }
185
-
186
- let propertiesCode = []
187
- let properties = obj.__properties || {}
188
- for(let property in properties) {
189
- let propertyCode = `property(${uuid}, "${property}").\n`
190
-
191
- let { through, initial, required, resolveLanguage, writable, flag, getter, setter, local } = properties[property]
192
-
193
- if(resolveLanguage) {
194
- propertyCode += `property_resolve(${uuid}, "${property}").\n`
195
- propertyCode += `property_resolve_language(${uuid}, "${property}", "${resolveLanguage}").\n`
196
- }
197
-
198
- if(getter) {
199
- propertyCode += `property_getter(${uuid}, Base, "${property}", Value) :- ${getter}.\n`
200
- } else if(through) {
201
- propertyCode += `property_getter(${uuid}, Base, "${property}", Value) :- triple(Base, "${through}", Value).\n`
202
-
203
- if(required) {
204
- if(flag) {
205
- instanceConditions.push(`triple(Base, "${through}", "${initial}")`)
206
- } else {
207
- instanceConditions.push(`triple(Base, "${through}", _)`)
208
- }
209
- }
210
- }
211
-
212
- if(setter) {
213
- propertyCode += `property_setter(${uuid}, "${property}", Actions) :- ${setter}.\n`
214
- } else if (writable) {
215
- let setter = obj[propertyNameToSetterName(property)]
216
- if(typeof setter === "function") {
217
- let action = [{
218
- action: "setSingleTarget",
219
- source: "this",
220
- predicate: through,
221
- target: "value",
222
- ...(local && { local: true })
223
- }]
224
- propertyCode += `property_setter(${uuid}, "${property}", '${stringifyObjectLiteral(action)}').\n`
225
- }
226
- }
227
-
228
- propertiesCode.push(propertyCode)
229
-
230
- if(initial) {
231
- constructorActions.push({
232
- action: "addLink",
233
- source: "this",
234
- predicate: through,
235
- target: initial,
236
- })
237
-
238
- classRemoverActions.push({
239
- action: "removeLink",
240
- source: "this",
241
- predicate: through,
242
- target: initial,
243
- })
244
- }
245
- }
246
-
247
- let collectionsCode = []
248
- let collections = obj.__collections || {}
249
- for(let collection in collections) {
250
- let collectionCode = `collection(${uuid}, "${collection}").\n`
251
-
252
- let { through, where, local} = collections[collection]
253
-
254
- if(through) {
255
- if(where) {
256
- if(!where.isInstance && !where.condition) {
257
- throw "'where' needs one of 'isInstance' or 'condition'"
258
- }
259
-
260
- let conditions = []
261
-
262
- if(where.isInstance) {
263
- let otherClass
264
- if(where.isInstance.name) {
265
- otherClass = where.isInstance.name
266
- } else {
267
- otherClass = where.isInstance
268
- }
269
- conditions.push(`instance(OtherClass, Target), subject_class("${otherClass}", OtherClass)`)
270
- }
271
-
272
- if(where.condition) {
273
- conditions.push(where.condition)
274
- }
275
-
276
- const conditionString = conditions.join(", ")
277
-
278
- collectionCode += `collection_getter(${uuid}, Base, "${collection}", List) :- setof(Target, (triple(Base, "${through}", Target), ${conditionString}), List).\n`
279
- } else {
280
- collectionCode += `collection_getter(${uuid}, Base, "${collection}", List) :- findall(C, triple(Base, "${through}", C), List).\n`
281
- }
282
-
283
- let collectionAdderAction = [{
284
- action: "addLink",
285
- source: "this",
286
- predicate: through,
287
- target: "value",
288
- ...(local && { local: true })
289
- }]
290
-
291
- let collectionRemoverAction = [{
292
- action: "removeLink",
293
- source: "this",
294
- predicate: through,
295
- target: "value",
296
- }]
297
-
298
- let collectionSetterAction = [{
299
- action: "collectionSetter",
300
- source: "this",
301
- predicate: through,
302
- target: "value",
303
- ...(local && { local: true })
304
- }]
305
- collectionCode += `collection_adder(${uuid}, "${singularToPlural(collection)}", '${stringifyObjectLiteral(collectionAdderAction)}').\n`
306
- collectionCode += `collection_remover(${uuid}, "${singularToPlural(collection)}", '${stringifyObjectLiteral(collectionRemoverAction)}').\n`
307
- collectionCode += `collection_setter(${uuid}, "${singularToPlural(collection)}", '${stringifyObjectLiteral(collectionSetterAction)}').\n`
308
- }
309
-
310
- collectionsCode.push(collectionCode)
311
- }
312
-
313
- let subjectContructorJSONString = stringifyObjectLiteral(constructorActions)
314
- sdna += `constructor(${uuid}, '${subjectContructorJSONString}').\n`
315
- if(instanceConditions.length > 0) {
316
- let instanceConditionProlog = instanceConditions.join(", ")
317
- sdna += `instance(${uuid}, Base) :- ${instanceConditionProlog}.\n`
318
- sdna += "\n"
319
- }
320
- sdna += `destructor(${uuid}, '${stringifyObjectLiteral(classRemoverActions)}').\n`
321
- sdna += "\n"
322
- sdna += propertiesCode.join("\n")
323
- sdna += "\n"
324
- sdna += collectionsCode.join("\n")
325
-
326
- return {
327
- sdna,
328
- name: subjectName
329
- }
330
- }
331
-
332
- Object.defineProperty(target, 'type', {configurable: true});
333
- }
1
+ import { PerspectiveProxy } from "../perspectives/PerspectiveProxy";
2
+ import { Subject } from "./Subject";
3
+ import { capitalize, propertyNameToSetterName, singularToPlural, stringifyObjectLiteral } from "./util";
4
+
5
+ export class PerspectiveAction {
6
+ action: string
7
+ source: string
8
+ predicate: string
9
+ target: string
10
+ }
11
+
12
+ export function addLink(source: string, predicate: string, target: string): PerspectiveAction {
13
+ return {
14
+ action: "addLink",
15
+ source,
16
+ predicate,
17
+ target,
18
+ };
19
+ }
20
+
21
+ export function hasLink(predicate: string): string {
22
+ return `triple(this, "${predicate}", _)`
23
+ }
24
+
25
+ interface InstanceQueryParams {
26
+ where?: object;
27
+ condition?: string;
28
+ }
29
+
30
+ export function instanceQuery(options?: InstanceQueryParams) {
31
+ return function <T>(target: T, key: keyof T, descriptor: PropertyDescriptor) {
32
+ const originalMethod = descriptor.value;
33
+ if(typeof originalMethod !== "function") {
34
+ throw new Error("instanceQuery decorator can only be applied to methods");
35
+ }
36
+
37
+ descriptor.value = async function(perspective: PerspectiveProxy): Promise<T[]> {
38
+ let instances: T[] = []
39
+ //@ts-ignore
40
+ let subjectClassName = target.name
41
+ let query = `subject_class("${subjectClassName}", C), instance(C, Instance)`
42
+ if(options && options.where) {
43
+ for(let prop in options.where) {
44
+ let value = options.where[prop]
45
+ query += `, property_getter(C, Instance, "${prop}", "${value}")`
46
+ }
47
+ }
48
+
49
+ if(options && options.condition) {
50
+ query += ', ' + options.condition
51
+ }
52
+
53
+ let results = await perspective.infer(query)
54
+ if(results == false) {
55
+ return instances
56
+ }
57
+ if(typeof results == "string") {
58
+ throw results
59
+ }
60
+ for(let result of results) {
61
+ let instance = result.Instance
62
+ let subject = new Subject(perspective, instance, subjectClassName)
63
+ await subject.init()
64
+ instances.push(subject as T)
65
+ }
66
+
67
+ return instances
68
+ }
69
+ };
70
+ }
71
+
72
+
73
+ interface PropertyOptions {
74
+ through?: string;
75
+ initial?: string,
76
+ required?: boolean,
77
+ writable?: boolean,
78
+ resolveLanguage?: string;
79
+ getter?: string;
80
+ setter?: string;
81
+ local?: boolean
82
+ }
83
+ export function subjectProperty(opts: PropertyOptions) {
84
+ return function <T>(target: T, key: keyof T) {
85
+ target["__properties"] = target["__properties"] || {};
86
+ target["__properties"][key] = target["__properties"][key] || {};
87
+ target["__properties"][key] = { ...target["__properties"][key], ...opts }
88
+
89
+ if (opts.writable) {
90
+ const value = key as string
91
+ target[`set${capitalize(value)}`] = () => {}
92
+ }
93
+
94
+ Object.defineProperty(target, key, {configurable: true, writable: true});
95
+ };
96
+ }
97
+
98
+ interface FlagOptions {
99
+ through: string;
100
+ value: string;
101
+ }
102
+ export function subjectFlag(opts: FlagOptions) {
103
+ return function <T>(target: T, key: keyof T) {
104
+ target["__properties"] = target["__properties"] || {};
105
+ target["__properties"][key] = target["__properties"][key] || {};
106
+ target["__properties"][key] = {
107
+ ...target["__properties"][key],
108
+ through: opts.through,
109
+ required: true,
110
+ initial: opts.value,
111
+ flag: true
112
+ }
113
+
114
+ // @ts-ignore
115
+ target[key] = opts.value;
116
+
117
+ Object.defineProperty(target, key, {configurable: true, writable: true});
118
+ };
119
+ }
120
+
121
+ interface WhereOptions {
122
+ isInstance?: any
123
+ condition?: string
124
+ }
125
+ interface CollectionOptions {
126
+ through: string,
127
+ where?: WhereOptions,
128
+ local?: boolean
129
+ }
130
+
131
+ export function subjectCollection(opts: CollectionOptions) {
132
+ return function <T>(target: T, key: keyof T) {
133
+ target["__collections"] = target["__collections"] || {};
134
+ target["__collections"][key] = opts;
135
+
136
+ const value = key as string
137
+ target[`add${capitalize(value)}`] = () => {}
138
+ target[`remove${capitalize(value)}`] = () => {}
139
+ target[`setCollection${capitalize(value)}`] = () => {}
140
+
141
+ Object.defineProperty(target, key, {configurable: true, writable: true});
142
+ };
143
+ }
144
+
145
+ export function makeRandomPrologAtom(length: number): string {
146
+ let result = '';
147
+ let characters = 'abcdefghijklmnopqrstuvwxyz';
148
+ let charactersLength = characters.length;
149
+ for (let i = 0; i < length; i++) {
150
+ result += characters.charAt(Math.floor(Math.random() * charactersLength));
151
+ }
152
+ return result;
153
+ }
154
+
155
+ interface SDNAClassOptions {
156
+ name: string;
157
+ }
158
+
159
+ export function SDNAClass(opts: SDNAClassOptions) {
160
+ return function (target: any) {
161
+ target.prototype.className = opts.name;
162
+ target.className = opts.name;
163
+
164
+ target.generateSDNA = function() {
165
+ let sdna = ""
166
+ let subjectName = opts.name
167
+ let obj = target.prototype;
168
+
169
+ let uuid = makeRandomPrologAtom(8)
170
+
171
+ sdna += `subject_class("${subjectName}", ${uuid}).\n`
172
+
173
+
174
+ let classRemoverActions = []
175
+
176
+ let constructorActions = []
177
+ if(obj.subjectConstructor && obj.subjectConstructor.length) {
178
+ constructorActions = constructorActions.concat(obj.subjectConstructor)
179
+ }
180
+
181
+ let instanceConditions = []
182
+ if(obj.isSubjectInstance && obj.isSubjectInstance.length) {
183
+ instanceConditions = instanceConditions.concat(obj.isSubjectInstance)
184
+ }
185
+
186
+ let propertiesCode = []
187
+ let properties = obj.__properties || {}
188
+ for(let property in properties) {
189
+ let propertyCode = `property(${uuid}, "${property}").\n`
190
+
191
+ let { through, initial, required, resolveLanguage, writable, flag, getter, setter, local } = properties[property]
192
+
193
+ if(resolveLanguage) {
194
+ propertyCode += `property_resolve(${uuid}, "${property}").\n`
195
+ propertyCode += `property_resolve_language(${uuid}, "${property}", "${resolveLanguage}").\n`
196
+ }
197
+
198
+ if(getter) {
199
+ propertyCode += `property_getter(${uuid}, Base, "${property}", Value) :- ${getter}.\n`
200
+ } else if(through) {
201
+ propertyCode += `property_getter(${uuid}, Base, "${property}", Value) :- triple(Base, "${through}", Value).\n`
202
+
203
+ if(required) {
204
+ if(flag) {
205
+ instanceConditions.push(`triple(Base, "${through}", "${initial}")`)
206
+ } else {
207
+ instanceConditions.push(`triple(Base, "${through}", _)`)
208
+ }
209
+ }
210
+ }
211
+
212
+ if(setter) {
213
+ propertyCode += `property_setter(${uuid}, "${property}", Actions) :- ${setter}.\n`
214
+ } else if (writable) {
215
+ let setter = obj[propertyNameToSetterName(property)]
216
+ if(typeof setter === "function") {
217
+ let action = [{
218
+ action: "setSingleTarget",
219
+ source: "this",
220
+ predicate: through,
221
+ target: "value",
222
+ ...(local && { local: true })
223
+ }]
224
+ propertyCode += `property_setter(${uuid}, "${property}", '${stringifyObjectLiteral(action)}').\n`
225
+ }
226
+ }
227
+
228
+ propertiesCode.push(propertyCode)
229
+
230
+ if(initial) {
231
+ constructorActions.push({
232
+ action: "addLink",
233
+ source: "this",
234
+ predicate: through,
235
+ target: initial,
236
+ })
237
+
238
+ classRemoverActions.push({
239
+ action: "removeLink",
240
+ source: "this",
241
+ predicate: through,
242
+ target: initial,
243
+ })
244
+ }
245
+ }
246
+
247
+ let collectionsCode = []
248
+ let collections = obj.__collections || {}
249
+ for(let collection in collections) {
250
+ let collectionCode = `collection(${uuid}, "${collection}").\n`
251
+
252
+ let { through, where, local} = collections[collection]
253
+
254
+ if(through) {
255
+ if(where) {
256
+ if(!where.isInstance && !where.condition) {
257
+ throw "'where' needs one of 'isInstance' or 'condition'"
258
+ }
259
+
260
+ let conditions = []
261
+
262
+ if(where.isInstance) {
263
+ let otherClass
264
+ if(where.isInstance.name) {
265
+ otherClass = where.isInstance.name
266
+ } else {
267
+ otherClass = where.isInstance
268
+ }
269
+ conditions.push(`instance(OtherClass, Target), subject_class("${otherClass}", OtherClass)`)
270
+ }
271
+
272
+ if(where.condition) {
273
+ conditions.push(where.condition)
274
+ }
275
+
276
+ const conditionString = conditions.join(", ")
277
+
278
+ collectionCode += `collection_getter(${uuid}, Base, "${collection}", List) :- setof(Target, (triple(Base, "${through}", Target), ${conditionString}), List).\n`
279
+ } else {
280
+ collectionCode += `collection_getter(${uuid}, Base, "${collection}", List) :- findall(C, triple(Base, "${through}", C), List).\n`
281
+ }
282
+
283
+ let collectionAdderAction = [{
284
+ action: "addLink",
285
+ source: "this",
286
+ predicate: through,
287
+ target: "value",
288
+ ...(local && { local: true })
289
+ }]
290
+
291
+ let collectionRemoverAction = [{
292
+ action: "removeLink",
293
+ source: "this",
294
+ predicate: through,
295
+ target: "value",
296
+ }]
297
+
298
+ let collectionSetterAction = [{
299
+ action: "collectionSetter",
300
+ source: "this",
301
+ predicate: through,
302
+ target: "value",
303
+ ...(local && { local: true })
304
+ }]
305
+ collectionCode += `collection_adder(${uuid}, "${singularToPlural(collection)}", '${stringifyObjectLiteral(collectionAdderAction)}').\n`
306
+ collectionCode += `collection_remover(${uuid}, "${singularToPlural(collection)}", '${stringifyObjectLiteral(collectionRemoverAction)}').\n`
307
+ collectionCode += `collection_setter(${uuid}, "${singularToPlural(collection)}", '${stringifyObjectLiteral(collectionSetterAction)}').\n`
308
+ }
309
+
310
+ collectionsCode.push(collectionCode)
311
+ }
312
+
313
+ let subjectContructorJSONString = stringifyObjectLiteral(constructorActions)
314
+ sdna += `constructor(${uuid}, '${subjectContructorJSONString}').\n`
315
+ if(instanceConditions.length > 0) {
316
+ let instanceConditionProlog = instanceConditions.join(", ")
317
+ sdna += `instance(${uuid}, Base) :- ${instanceConditionProlog}.\n`
318
+ sdna += "\n"
319
+ }
320
+ sdna += `destructor(${uuid}, '${stringifyObjectLiteral(classRemoverActions)}').\n`
321
+ sdna += "\n"
322
+ sdna += propertiesCode.join("\n")
323
+ sdna += "\n"
324
+ sdna += collectionsCode.join("\n")
325
+
326
+ return {
327
+ sdna,
328
+ name: subjectName
329
+ }
330
+ }
331
+
332
+ Object.defineProperty(target, 'type', {configurable: true});
333
+ }
334
334
  }