@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.
- package/README.md +229 -229
- package/jest.config.js +5 -5
- package/jsdoc.json +27 -27
- package/lib/index.cjs +4522 -4571
- package/lib/index.js +4522 -4571
- package/lib/shims/type-graphql.d.ts +32 -32
- package/lib/shims/type-graphql.js +70 -70
- package/lib/src/Ad4mClient.d.ts +26 -26
- package/lib/src/Ad4mClient.js +66 -66
- package/lib/src/Address.d.ts +1 -1
- package/lib/src/Address.js +1 -1
- package/lib/src/DID.d.ts +1 -1
- package/lib/src/DID.js +1 -1
- package/lib/src/Exception.d.ts +6 -6
- package/lib/src/Exception.js +7 -7
- package/lib/src/Literal.d.ts +7 -7
- package/lib/src/Literal.js +76 -76
- package/lib/src/PubSub.d.ts +14 -14
- package/lib/src/PubSub.js +14 -14
- package/lib/src/SmartLiteral.d.ts +12 -12
- package/lib/src/SmartLiteral.js +74 -74
- package/lib/src/agent/Agent.d.ts +104 -104
- package/lib/src/agent/Agent.js +337 -337
- package/lib/src/agent/AgentClient.d.ts +57 -57
- package/lib/src/agent/AgentClient.js +431 -431
- package/lib/src/agent/AgentResolver.d.ts +30 -30
- package/lib/src/agent/AgentResolver.js +325 -325
- package/lib/src/agent/AgentStatus.d.ts +8 -8
- package/lib/src/agent/AgentStatus.js +61 -61
- package/lib/src/buildSchema.d.ts +1 -1
- package/lib/src/buildSchema.js +36 -36
- package/lib/src/expression/Expression.d.ts +28 -28
- package/lib/src/expression/Expression.js +143 -143
- package/lib/src/expression/ExpressionClient.d.ts +13 -13
- package/lib/src/expression/ExpressionClient.js +102 -102
- package/lib/src/expression/ExpressionRef.d.ts +9 -9
- package/lib/src/expression/ExpressionRef.js +67 -67
- package/lib/src/expression/ExpressionResolver.d.ts +10 -10
- package/lib/src/expression/ExpressionResolver.js +106 -106
- package/lib/src/index.d.ts +30 -30
- package/lib/src/index.js +30 -30
- package/lib/src/jestSetup.d.ts +1 -1
- package/lib/src/jestSetup.js +1 -1
- package/lib/src/language/Icon.d.ts +4 -4
- package/lib/src/language/Icon.js +24 -24
- package/lib/src/language/Language.d.ts +195 -195
- package/lib/src/language/Language.js +79 -79
- package/lib/src/language/LanguageClient.d.ts +17 -17
- package/lib/src/language/LanguageClient.js +137 -137
- package/lib/src/language/LanguageContext.d.ts +37 -37
- package/lib/src/language/LanguageContext.js +2 -2
- package/lib/src/language/LanguageHandle.d.ts +9 -9
- package/lib/src/language/LanguageHandle.js +41 -41
- package/lib/src/language/LanguageMeta.d.ts +35 -35
- package/lib/src/language/LanguageMeta.js +89 -89
- package/lib/src/language/LanguageRef.d.ts +6 -6
- package/lib/src/language/LanguageRef.js +30 -30
- package/lib/src/language/LanguageResolver.d.ts +19 -19
- package/lib/src/language/LanguageResolver.js +143 -143
- package/lib/src/links/Links.d.ts +39 -39
- package/lib/src/links/Links.js +148 -148
- package/lib/src/links/Links.test.d.ts +1 -1
- package/lib/src/links/Links.test.js +33 -33
- package/lib/src/neighbourhood/Neighbourhood.d.ts +10 -10
- package/lib/src/neighbourhood/Neighbourhood.js +38 -38
- package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +25 -25
- package/lib/src/neighbourhood/NeighbourhoodClient.js +256 -256
- package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +19 -19
- package/lib/src/neighbourhood/NeighbourhoodProxy.js +54 -54
- package/lib/src/neighbourhood/NeighbourhoodResolver.d.ts +24 -24
- package/lib/src/neighbourhood/NeighbourhoodResolver.js +180 -180
- package/lib/src/perspectives/LinkQuery.d.ts +11 -11
- package/lib/src/perspectives/LinkQuery.js +80 -80
- package/lib/src/perspectives/Perspective.d.ts +38 -38
- package/lib/src/perspectives/Perspective.js +144 -144
- package/lib/src/perspectives/Perspective.test.d.ts +1 -1
- package/lib/src/perspectives/Perspective.test.js +20 -20
- package/lib/src/perspectives/PerspectiveClient.d.ts +55 -54
- package/lib/src/perspectives/PerspectiveClient.js +418 -410
- package/lib/src/perspectives/PerspectiveDiff.d.ts +9 -9
- package/lib/src/perspectives/PerspectiveDiff.js +33 -33
- package/lib/src/perspectives/PerspectiveHandle.d.ts +16 -16
- package/lib/src/perspectives/PerspectiveHandle.js +59 -59
- package/lib/src/perspectives/PerspectiveProxy.d.ts +178 -178
- package/lib/src/perspectives/PerspectiveProxy.js +501 -540
- package/lib/src/perspectives/PerspectiveResolver.d.ts +38 -37
- package/lib/src/perspectives/PerspectiveResolver.js +376 -362
- package/lib/src/runtime/RuntimeClient.d.ts +33 -33
- package/lib/src/runtime/RuntimeClient.js +263 -263
- package/lib/src/runtime/RuntimeResolver.d.ts +47 -47
- package/lib/src/runtime/RuntimeResolver.js +328 -328
- package/lib/src/schema.gql +1 -0
- package/lib/src/subject/SDNADecorators.d.ts +45 -45
- package/lib/src/subject/SDNADecorators.js +250 -250
- package/lib/src/subject/Subject.d.ts +7 -7
- package/lib/src/subject/Subject.js +160 -160
- package/lib/src/subject/SubjectEntity.d.ts +31 -31
- package/lib/src/subject/SubjectEntity.js +250 -250
- package/lib/src/subject/util.d.ts +12 -12
- package/lib/src/subject/util.js +73 -73
- package/lib/src/typeDefs.d.ts +1 -1
- package/lib/src/typeDefs.js +1 -0
- package/lib/src/unwrapApolloResult.d.ts +2 -2
- package/lib/src/unwrapApolloResult.js +14 -12
- package/lib/src/utils.d.ts +2 -2
- package/lib/src/utils.js +21 -21
- package/package.json +61 -61
- package/rollup.config.js +26 -26
- package/shims/type-graphql.ts +75 -75
- package/src/Ad4mClient.test.ts +1089 -1084
- package/src/Ad4mClient.ts +62 -62
- package/src/Address.ts +1 -1
- package/src/Exception.ts +6 -6
- package/src/Literal.test.ts +30 -30
- package/src/Literal.ts +80 -80
- package/src/PubSub.ts +13 -13
- package/src/SmartLiteral.ts +74 -74
- package/src/agent/Agent.ts +293 -293
- package/src/agent/AgentClient.ts +531 -531
- package/src/agent/AgentResolver.ts +263 -263
- package/src/agent/AgentStatus.ts +43 -43
- package/src/buildSchema.ts +41 -41
- package/src/expression/Expression.ts +99 -99
- package/src/expression/ExpressionClient.ts +98 -98
- package/src/expression/ExpressionRef.ts +62 -62
- package/src/expression/ExpressionResolver.ts +63 -63
- package/src/index.ts +30 -30
- package/src/language/Icon.ts +10 -10
- package/src/language/Language.ts +266 -266
- package/src/language/LanguageClient.ts +159 -159
- package/src/language/LanguageContext.ts +37 -37
- package/src/language/LanguageHandle.ts +23 -23
- package/src/language/LanguageMeta.ts +69 -69
- package/src/language/LanguageRef.ts +18 -18
- package/src/language/LanguageResolver.ts +98 -98
- package/src/links/Links.test.ts +39 -39
- package/src/links/Links.ts +107 -107
- package/src/neighbourhood/Neighbourhood.ts +22 -22
- package/src/neighbourhood/NeighbourhoodClient.ts +275 -275
- package/src/neighbourhood/NeighbourhoodProxy.ts +58 -58
- package/src/neighbourhood/NeighbourhoodResolver.ts +99 -99
- package/src/perspectives/LinkQuery.ts +65 -65
- package/src/perspectives/Perspective.test.ts +22 -22
- package/src/perspectives/Perspective.ts +118 -118
- package/src/perspectives/PerspectiveClient.ts +452 -443
- package/src/perspectives/PerspectiveDiff.ts +15 -15
- package/src/perspectives/PerspectiveHandle.ts +39 -39
- package/src/perspectives/PerspectiveProxy.ts +576 -610
- package/src/perspectives/PerspectiveResolver.ts +224 -219
- package/src/runtime/RuntimeClient.ts +282 -282
- package/src/runtime/RuntimeResolver.ts +192 -192
- package/src/subject/SDNADecorators.ts +333 -333
- package/src/subject/Subject.ts +152 -152
- package/src/subject/SubjectEntity.ts +291 -291
- package/src/subject/util.ts +84 -84
- package/src/typeDefs.ts +5 -5
- package/src/unwrapApolloResult.ts +15 -13
- package/src/utils.ts +25 -25
- package/tsconfig-browser.json +82 -82
- package/tsconfig.json +80 -80
- 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
|
}
|