@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
@@ -248,6 +248,7 @@ type Mutation {
248
248
  perspectiveAddLink(link: LinkInput!, status: String = "shared", uuid: String!): LinkExpression!
249
249
  perspectiveAddLinkExpression(link: LinkExpressionInput!, status: String, uuid: String!): LinkExpression!
250
250
  perspectiveAddLinks(links: [LinkInput!]!, status: String, uuid: String!): [LinkExpression!]!
251
+ perspectiveAddSdna(name: String!, sdnaCode: String!, sdnaType: String!, uuid: String!): Boolean!
251
252
  perspectiveLinkMutations(mutations: LinkMutations!, status: String, uuid: String!): LinkExpressionMutations!
252
253
  perspectivePublishSnapshot(uuid: String!): String
253
254
  perspectiveRemove(uuid: String!): Boolean!
@@ -1,45 +1,45 @@
1
- export declare class PerspectiveAction {
2
- action: string;
3
- source: string;
4
- predicate: string;
5
- target: string;
6
- }
7
- export declare function addLink(source: string, predicate: string, target: string): PerspectiveAction;
8
- export declare function hasLink(predicate: string): string;
9
- interface InstanceQueryParams {
10
- where?: object;
11
- condition?: string;
12
- }
13
- export declare function instanceQuery(options?: InstanceQueryParams): <T>(target: T, key: keyof T, descriptor: PropertyDescriptor) => void;
14
- interface PropertyOptions {
15
- through?: string;
16
- initial?: string;
17
- required?: boolean;
18
- writable?: boolean;
19
- resolveLanguage?: string;
20
- getter?: string;
21
- setter?: string;
22
- local?: boolean;
23
- }
24
- export declare function subjectProperty(opts: PropertyOptions): <T>(target: T, key: keyof T) => void;
25
- interface FlagOptions {
26
- through: string;
27
- value: string;
28
- }
29
- export declare function subjectFlag(opts: FlagOptions): <T>(target: T, key: keyof T) => void;
30
- interface WhereOptions {
31
- isInstance?: any;
32
- condition?: string;
33
- }
34
- interface CollectionOptions {
35
- through: string;
36
- where?: WhereOptions;
37
- local?: boolean;
38
- }
39
- export declare function subjectCollection(opts: CollectionOptions): <T>(target: T, key: keyof T) => void;
40
- export declare function makeRandomPrologAtom(length: number): string;
41
- interface SDNAClassOptions {
42
- name: string;
43
- }
44
- export declare function SDNAClass(opts: SDNAClassOptions): (target: any) => void;
45
- export {};
1
+ export declare class PerspectiveAction {
2
+ action: string;
3
+ source: string;
4
+ predicate: string;
5
+ target: string;
6
+ }
7
+ export declare function addLink(source: string, predicate: string, target: string): PerspectiveAction;
8
+ export declare function hasLink(predicate: string): string;
9
+ interface InstanceQueryParams {
10
+ where?: object;
11
+ condition?: string;
12
+ }
13
+ export declare function instanceQuery(options?: InstanceQueryParams): <T>(target: T, key: keyof T, descriptor: PropertyDescriptor) => void;
14
+ interface PropertyOptions {
15
+ through?: string;
16
+ initial?: string;
17
+ required?: boolean;
18
+ writable?: boolean;
19
+ resolveLanguage?: string;
20
+ getter?: string;
21
+ setter?: string;
22
+ local?: boolean;
23
+ }
24
+ export declare function subjectProperty(opts: PropertyOptions): <T>(target: T, key: keyof T) => void;
25
+ interface FlagOptions {
26
+ through: string;
27
+ value: string;
28
+ }
29
+ export declare function subjectFlag(opts: FlagOptions): <T>(target: T, key: keyof T) => void;
30
+ interface WhereOptions {
31
+ isInstance?: any;
32
+ condition?: string;
33
+ }
34
+ interface CollectionOptions {
35
+ through: string;
36
+ where?: WhereOptions;
37
+ local?: boolean;
38
+ }
39
+ export declare function subjectCollection(opts: CollectionOptions): <T>(target: T, key: keyof T) => void;
40
+ export declare function makeRandomPrologAtom(length: number): string;
41
+ interface SDNAClassOptions {
42
+ name: string;
43
+ }
44
+ export declare function SDNAClass(opts: SDNAClassOptions): (target: any) => void;
45
+ export {};
@@ -1,250 +1,250 @@
1
- import { Subject } from "./Subject";
2
- import { capitalize, propertyNameToSetterName, singularToPlural, stringifyObjectLiteral } from "./util";
3
- export class PerspectiveAction {
4
- }
5
- export function addLink(source, predicate, target) {
6
- return {
7
- action: "addLink",
8
- source,
9
- predicate,
10
- target,
11
- };
12
- }
13
- export function hasLink(predicate) {
14
- return `triple(this, "${predicate}", _)`;
15
- }
16
- export function instanceQuery(options) {
17
- return function (target, key, descriptor) {
18
- const originalMethod = descriptor.value;
19
- if (typeof originalMethod !== "function") {
20
- throw new Error("instanceQuery decorator can only be applied to methods");
21
- }
22
- descriptor.value = async function (perspective) {
23
- let instances = [];
24
- //@ts-ignore
25
- let subjectClassName = target.name;
26
- let query = `subject_class("${subjectClassName}", C), instance(C, Instance)`;
27
- if (options && options.where) {
28
- for (let prop in options.where) {
29
- let value = options.where[prop];
30
- query += `, property_getter(C, Instance, "${prop}", "${value}")`;
31
- }
32
- }
33
- if (options && options.condition) {
34
- query += ', ' + options.condition;
35
- }
36
- let results = await perspective.infer(query);
37
- if (results == false) {
38
- return instances;
39
- }
40
- if (typeof results == "string") {
41
- throw results;
42
- }
43
- for (let result of results) {
44
- let instance = result.Instance;
45
- let subject = new Subject(perspective, instance, subjectClassName);
46
- await subject.init();
47
- instances.push(subject);
48
- }
49
- return instances;
50
- };
51
- };
52
- }
53
- export function subjectProperty(opts) {
54
- return function (target, key) {
55
- target["__properties"] = target["__properties"] || {};
56
- target["__properties"][key] = target["__properties"][key] || {};
57
- target["__properties"][key] = { ...target["__properties"][key], ...opts };
58
- if (opts.writable) {
59
- const value = key;
60
- target[`set${capitalize(value)}`] = () => { };
61
- }
62
- Object.defineProperty(target, key, { configurable: true, writable: true });
63
- };
64
- }
65
- export function subjectFlag(opts) {
66
- return function (target, key) {
67
- target["__properties"] = target["__properties"] || {};
68
- target["__properties"][key] = target["__properties"][key] || {};
69
- target["__properties"][key] = {
70
- ...target["__properties"][key],
71
- through: opts.through,
72
- required: true,
73
- initial: opts.value,
74
- flag: true
75
- };
76
- // @ts-ignore
77
- target[key] = opts.value;
78
- Object.defineProperty(target, key, { configurable: true, writable: true });
79
- };
80
- }
81
- export function subjectCollection(opts) {
82
- return function (target, key) {
83
- target["__collections"] = target["__collections"] || {};
84
- target["__collections"][key] = opts;
85
- const value = key;
86
- target[`add${capitalize(value)}`] = () => { };
87
- target[`remove${capitalize(value)}`] = () => { };
88
- target[`setCollection${capitalize(value)}`] = () => { };
89
- Object.defineProperty(target, key, { configurable: true, writable: true });
90
- };
91
- }
92
- export function makeRandomPrologAtom(length) {
93
- let result = '';
94
- let characters = 'abcdefghijklmnopqrstuvwxyz';
95
- let charactersLength = characters.length;
96
- for (let i = 0; i < length; i++) {
97
- result += characters.charAt(Math.floor(Math.random() * charactersLength));
98
- }
99
- return result;
100
- }
101
- export function SDNAClass(opts) {
102
- return function (target) {
103
- target.prototype.className = opts.name;
104
- target.className = opts.name;
105
- target.generateSDNA = function () {
106
- let sdna = "";
107
- let subjectName = opts.name;
108
- let obj = target.prototype;
109
- let uuid = makeRandomPrologAtom(8);
110
- sdna += `subject_class("${subjectName}", ${uuid}).\n`;
111
- let classRemoverActions = [];
112
- let constructorActions = [];
113
- if (obj.subjectConstructor && obj.subjectConstructor.length) {
114
- constructorActions = constructorActions.concat(obj.subjectConstructor);
115
- }
116
- let instanceConditions = [];
117
- if (obj.isSubjectInstance && obj.isSubjectInstance.length) {
118
- instanceConditions = instanceConditions.concat(obj.isSubjectInstance);
119
- }
120
- let propertiesCode = [];
121
- let properties = obj.__properties || {};
122
- for (let property in properties) {
123
- let propertyCode = `property(${uuid}, "${property}").\n`;
124
- let { through, initial, required, resolveLanguage, writable, flag, getter, setter, local } = properties[property];
125
- if (resolveLanguage) {
126
- propertyCode += `property_resolve(${uuid}, "${property}").\n`;
127
- propertyCode += `property_resolve_language(${uuid}, "${property}", "${resolveLanguage}").\n`;
128
- }
129
- if (getter) {
130
- propertyCode += `property_getter(${uuid}, Base, "${property}", Value) :- ${getter}.\n`;
131
- }
132
- else if (through) {
133
- propertyCode += `property_getter(${uuid}, Base, "${property}", Value) :- triple(Base, "${through}", Value).\n`;
134
- if (required) {
135
- if (flag) {
136
- instanceConditions.push(`triple(Base, "${through}", "${initial}")`);
137
- }
138
- else {
139
- instanceConditions.push(`triple(Base, "${through}", _)`);
140
- }
141
- }
142
- }
143
- if (setter) {
144
- propertyCode += `property_setter(${uuid}, "${property}", Actions) :- ${setter}.\n`;
145
- }
146
- else if (writable) {
147
- let setter = obj[propertyNameToSetterName(property)];
148
- if (typeof setter === "function") {
149
- let action = [{
150
- action: "setSingleTarget",
151
- source: "this",
152
- predicate: through,
153
- target: "value",
154
- ...(local && { local: true })
155
- }];
156
- propertyCode += `property_setter(${uuid}, "${property}", '${stringifyObjectLiteral(action)}').\n`;
157
- }
158
- }
159
- propertiesCode.push(propertyCode);
160
- if (initial) {
161
- constructorActions.push({
162
- action: "addLink",
163
- source: "this",
164
- predicate: through,
165
- target: initial,
166
- });
167
- classRemoverActions.push({
168
- action: "removeLink",
169
- source: "this",
170
- predicate: through,
171
- target: initial,
172
- });
173
- }
174
- }
175
- let collectionsCode = [];
176
- let collections = obj.__collections || {};
177
- for (let collection in collections) {
178
- let collectionCode = `collection(${uuid}, "${collection}").\n`;
179
- let { through, where, local } = collections[collection];
180
- if (through) {
181
- if (where) {
182
- if (!where.isInstance && !where.condition) {
183
- throw "'where' needs one of 'isInstance' or 'condition'";
184
- }
185
- let conditions = [];
186
- if (where.isInstance) {
187
- let otherClass;
188
- if (where.isInstance.name) {
189
- otherClass = where.isInstance.name;
190
- }
191
- else {
192
- otherClass = where.isInstance;
193
- }
194
- conditions.push(`instance(OtherClass, Target), subject_class("${otherClass}", OtherClass)`);
195
- }
196
- if (where.condition) {
197
- conditions.push(where.condition);
198
- }
199
- const conditionString = conditions.join(", ");
200
- collectionCode += `collection_getter(${uuid}, Base, "${collection}", List) :- setof(Target, (triple(Base, "${through}", Target), ${conditionString}), List).\n`;
201
- }
202
- else {
203
- collectionCode += `collection_getter(${uuid}, Base, "${collection}", List) :- findall(C, triple(Base, "${through}", C), List).\n`;
204
- }
205
- let collectionAdderAction = [{
206
- action: "addLink",
207
- source: "this",
208
- predicate: through,
209
- target: "value",
210
- ...(local && { local: true })
211
- }];
212
- let collectionRemoverAction = [{
213
- action: "removeLink",
214
- source: "this",
215
- predicate: through,
216
- target: "value",
217
- }];
218
- let collectionSetterAction = [{
219
- action: "collectionSetter",
220
- source: "this",
221
- predicate: through,
222
- target: "value",
223
- ...(local && { local: true })
224
- }];
225
- collectionCode += `collection_adder(${uuid}, "${singularToPlural(collection)}", '${stringifyObjectLiteral(collectionAdderAction)}').\n`;
226
- collectionCode += `collection_remover(${uuid}, "${singularToPlural(collection)}", '${stringifyObjectLiteral(collectionRemoverAction)}').\n`;
227
- collectionCode += `collection_setter(${uuid}, "${singularToPlural(collection)}", '${stringifyObjectLiteral(collectionSetterAction)}').\n`;
228
- }
229
- collectionsCode.push(collectionCode);
230
- }
231
- let subjectContructorJSONString = stringifyObjectLiteral(constructorActions);
232
- sdna += `constructor(${uuid}, '${subjectContructorJSONString}').\n`;
233
- if (instanceConditions.length > 0) {
234
- let instanceConditionProlog = instanceConditions.join(", ");
235
- sdna += `instance(${uuid}, Base) :- ${instanceConditionProlog}.\n`;
236
- sdna += "\n";
237
- }
238
- sdna += `destructor(${uuid}, '${stringifyObjectLiteral(classRemoverActions)}').\n`;
239
- sdna += "\n";
240
- sdna += propertiesCode.join("\n");
241
- sdna += "\n";
242
- sdna += collectionsCode.join("\n");
243
- return {
244
- sdna,
245
- name: subjectName
246
- };
247
- };
248
- Object.defineProperty(target, 'type', { configurable: true });
249
- };
250
- }
1
+ import { Subject } from "./Subject";
2
+ import { capitalize, propertyNameToSetterName, singularToPlural, stringifyObjectLiteral } from "./util";
3
+ export class PerspectiveAction {
4
+ }
5
+ export function addLink(source, predicate, target) {
6
+ return {
7
+ action: "addLink",
8
+ source,
9
+ predicate,
10
+ target,
11
+ };
12
+ }
13
+ export function hasLink(predicate) {
14
+ return `triple(this, "${predicate}", _)`;
15
+ }
16
+ export function instanceQuery(options) {
17
+ return function (target, key, descriptor) {
18
+ const originalMethod = descriptor.value;
19
+ if (typeof originalMethod !== "function") {
20
+ throw new Error("instanceQuery decorator can only be applied to methods");
21
+ }
22
+ descriptor.value = async function (perspective) {
23
+ let instances = [];
24
+ //@ts-ignore
25
+ let subjectClassName = target.name;
26
+ let query = `subject_class("${subjectClassName}", C), instance(C, Instance)`;
27
+ if (options && options.where) {
28
+ for (let prop in options.where) {
29
+ let value = options.where[prop];
30
+ query += `, property_getter(C, Instance, "${prop}", "${value}")`;
31
+ }
32
+ }
33
+ if (options && options.condition) {
34
+ query += ', ' + options.condition;
35
+ }
36
+ let results = await perspective.infer(query);
37
+ if (results == false) {
38
+ return instances;
39
+ }
40
+ if (typeof results == "string") {
41
+ throw results;
42
+ }
43
+ for (let result of results) {
44
+ let instance = result.Instance;
45
+ let subject = new Subject(perspective, instance, subjectClassName);
46
+ await subject.init();
47
+ instances.push(subject);
48
+ }
49
+ return instances;
50
+ };
51
+ };
52
+ }
53
+ export function subjectProperty(opts) {
54
+ return function (target, key) {
55
+ target["__properties"] = target["__properties"] || {};
56
+ target["__properties"][key] = target["__properties"][key] || {};
57
+ target["__properties"][key] = { ...target["__properties"][key], ...opts };
58
+ if (opts.writable) {
59
+ const value = key;
60
+ target[`set${capitalize(value)}`] = () => { };
61
+ }
62
+ Object.defineProperty(target, key, { configurable: true, writable: true });
63
+ };
64
+ }
65
+ export function subjectFlag(opts) {
66
+ return function (target, key) {
67
+ target["__properties"] = target["__properties"] || {};
68
+ target["__properties"][key] = target["__properties"][key] || {};
69
+ target["__properties"][key] = {
70
+ ...target["__properties"][key],
71
+ through: opts.through,
72
+ required: true,
73
+ initial: opts.value,
74
+ flag: true
75
+ };
76
+ // @ts-ignore
77
+ target[key] = opts.value;
78
+ Object.defineProperty(target, key, { configurable: true, writable: true });
79
+ };
80
+ }
81
+ export function subjectCollection(opts) {
82
+ return function (target, key) {
83
+ target["__collections"] = target["__collections"] || {};
84
+ target["__collections"][key] = opts;
85
+ const value = key;
86
+ target[`add${capitalize(value)}`] = () => { };
87
+ target[`remove${capitalize(value)}`] = () => { };
88
+ target[`setCollection${capitalize(value)}`] = () => { };
89
+ Object.defineProperty(target, key, { configurable: true, writable: true });
90
+ };
91
+ }
92
+ export function makeRandomPrologAtom(length) {
93
+ let result = '';
94
+ let characters = 'abcdefghijklmnopqrstuvwxyz';
95
+ let charactersLength = characters.length;
96
+ for (let i = 0; i < length; i++) {
97
+ result += characters.charAt(Math.floor(Math.random() * charactersLength));
98
+ }
99
+ return result;
100
+ }
101
+ export function SDNAClass(opts) {
102
+ return function (target) {
103
+ target.prototype.className = opts.name;
104
+ target.className = opts.name;
105
+ target.generateSDNA = function () {
106
+ let sdna = "";
107
+ let subjectName = opts.name;
108
+ let obj = target.prototype;
109
+ let uuid = makeRandomPrologAtom(8);
110
+ sdna += `subject_class("${subjectName}", ${uuid}).\n`;
111
+ let classRemoverActions = [];
112
+ let constructorActions = [];
113
+ if (obj.subjectConstructor && obj.subjectConstructor.length) {
114
+ constructorActions = constructorActions.concat(obj.subjectConstructor);
115
+ }
116
+ let instanceConditions = [];
117
+ if (obj.isSubjectInstance && obj.isSubjectInstance.length) {
118
+ instanceConditions = instanceConditions.concat(obj.isSubjectInstance);
119
+ }
120
+ let propertiesCode = [];
121
+ let properties = obj.__properties || {};
122
+ for (let property in properties) {
123
+ let propertyCode = `property(${uuid}, "${property}").\n`;
124
+ let { through, initial, required, resolveLanguage, writable, flag, getter, setter, local } = properties[property];
125
+ if (resolveLanguage) {
126
+ propertyCode += `property_resolve(${uuid}, "${property}").\n`;
127
+ propertyCode += `property_resolve_language(${uuid}, "${property}", "${resolveLanguage}").\n`;
128
+ }
129
+ if (getter) {
130
+ propertyCode += `property_getter(${uuid}, Base, "${property}", Value) :- ${getter}.\n`;
131
+ }
132
+ else if (through) {
133
+ propertyCode += `property_getter(${uuid}, Base, "${property}", Value) :- triple(Base, "${through}", Value).\n`;
134
+ if (required) {
135
+ if (flag) {
136
+ instanceConditions.push(`triple(Base, "${through}", "${initial}")`);
137
+ }
138
+ else {
139
+ instanceConditions.push(`triple(Base, "${through}", _)`);
140
+ }
141
+ }
142
+ }
143
+ if (setter) {
144
+ propertyCode += `property_setter(${uuid}, "${property}", Actions) :- ${setter}.\n`;
145
+ }
146
+ else if (writable) {
147
+ let setter = obj[propertyNameToSetterName(property)];
148
+ if (typeof setter === "function") {
149
+ let action = [{
150
+ action: "setSingleTarget",
151
+ source: "this",
152
+ predicate: through,
153
+ target: "value",
154
+ ...(local && { local: true })
155
+ }];
156
+ propertyCode += `property_setter(${uuid}, "${property}", '${stringifyObjectLiteral(action)}').\n`;
157
+ }
158
+ }
159
+ propertiesCode.push(propertyCode);
160
+ if (initial) {
161
+ constructorActions.push({
162
+ action: "addLink",
163
+ source: "this",
164
+ predicate: through,
165
+ target: initial,
166
+ });
167
+ classRemoverActions.push({
168
+ action: "removeLink",
169
+ source: "this",
170
+ predicate: through,
171
+ target: initial,
172
+ });
173
+ }
174
+ }
175
+ let collectionsCode = [];
176
+ let collections = obj.__collections || {};
177
+ for (let collection in collections) {
178
+ let collectionCode = `collection(${uuid}, "${collection}").\n`;
179
+ let { through, where, local } = collections[collection];
180
+ if (through) {
181
+ if (where) {
182
+ if (!where.isInstance && !where.condition) {
183
+ throw "'where' needs one of 'isInstance' or 'condition'";
184
+ }
185
+ let conditions = [];
186
+ if (where.isInstance) {
187
+ let otherClass;
188
+ if (where.isInstance.name) {
189
+ otherClass = where.isInstance.name;
190
+ }
191
+ else {
192
+ otherClass = where.isInstance;
193
+ }
194
+ conditions.push(`instance(OtherClass, Target), subject_class("${otherClass}", OtherClass)`);
195
+ }
196
+ if (where.condition) {
197
+ conditions.push(where.condition);
198
+ }
199
+ const conditionString = conditions.join(", ");
200
+ collectionCode += `collection_getter(${uuid}, Base, "${collection}", List) :- setof(Target, (triple(Base, "${through}", Target), ${conditionString}), List).\n`;
201
+ }
202
+ else {
203
+ collectionCode += `collection_getter(${uuid}, Base, "${collection}", List) :- findall(C, triple(Base, "${through}", C), List).\n`;
204
+ }
205
+ let collectionAdderAction = [{
206
+ action: "addLink",
207
+ source: "this",
208
+ predicate: through,
209
+ target: "value",
210
+ ...(local && { local: true })
211
+ }];
212
+ let collectionRemoverAction = [{
213
+ action: "removeLink",
214
+ source: "this",
215
+ predicate: through,
216
+ target: "value",
217
+ }];
218
+ let collectionSetterAction = [{
219
+ action: "collectionSetter",
220
+ source: "this",
221
+ predicate: through,
222
+ target: "value",
223
+ ...(local && { local: true })
224
+ }];
225
+ collectionCode += `collection_adder(${uuid}, "${singularToPlural(collection)}", '${stringifyObjectLiteral(collectionAdderAction)}').\n`;
226
+ collectionCode += `collection_remover(${uuid}, "${singularToPlural(collection)}", '${stringifyObjectLiteral(collectionRemoverAction)}').\n`;
227
+ collectionCode += `collection_setter(${uuid}, "${singularToPlural(collection)}", '${stringifyObjectLiteral(collectionSetterAction)}').\n`;
228
+ }
229
+ collectionsCode.push(collectionCode);
230
+ }
231
+ let subjectContructorJSONString = stringifyObjectLiteral(constructorActions);
232
+ sdna += `constructor(${uuid}, '${subjectContructorJSONString}').\n`;
233
+ if (instanceConditions.length > 0) {
234
+ let instanceConditionProlog = instanceConditions.join(", ");
235
+ sdna += `instance(${uuid}, Base) :- ${instanceConditionProlog}.\n`;
236
+ sdna += "\n";
237
+ }
238
+ sdna += `destructor(${uuid}, '${stringifyObjectLiteral(classRemoverActions)}').\n`;
239
+ sdna += "\n";
240
+ sdna += propertiesCode.join("\n");
241
+ sdna += "\n";
242
+ sdna += collectionsCode.join("\n");
243
+ return {
244
+ sdna,
245
+ name: subjectName
246
+ };
247
+ };
248
+ Object.defineProperty(target, 'type', { configurable: true });
249
+ };
250
+ }
@@ -1,7 +1,7 @@
1
- import { PerspectiveProxy } from "../perspectives/PerspectiveProxy";
2
- export declare class Subject {
3
- #private;
4
- constructor(perspective: PerspectiveProxy, baseExpression: string, subjectClass: string);
5
- get baseExpression(): string;
6
- init(): Promise<void>;
7
- }
1
+ import { PerspectiveProxy } from "../perspectives/PerspectiveProxy";
2
+ export declare class Subject {
3
+ #private;
4
+ constructor(perspective: PerspectiveProxy, baseExpression: string, subjectClass: string);
5
+ get baseExpression(): string;
6
+ init(): Promise<void>;
7
+ }