@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,292 +1,292 @@
|
|
|
1
|
-
import { Literal } from "../Literal";
|
|
2
|
-
import { Link } from "../links/Links";
|
|
3
|
-
import { PerspectiveProxy } from "../perspectives/PerspectiveProxy";
|
|
4
|
-
import { makeRandomPrologAtom } from "./SDNADecorators";
|
|
5
|
-
import { singularToPlural } from "./util";
|
|
6
|
-
|
|
7
|
-
export type QueryPartialEntity<T> = {
|
|
8
|
-
[P in keyof T]?: T[P] | (() => string);
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export class SubjectEntity {
|
|
13
|
-
#baseExpression: string;
|
|
14
|
-
#subjectClass: string;
|
|
15
|
-
#source: string;
|
|
16
|
-
#perspective: PerspectiveProxy
|
|
17
|
-
author: string;
|
|
18
|
-
timestamp: string;
|
|
19
|
-
|
|
20
|
-
constructor(perspective: PerspectiveProxy, baseExpression?: string, source?: string) {
|
|
21
|
-
this.#baseExpression = baseExpression ? baseExpression : Literal.from(makeRandomPrologAtom(24)).toUrl();
|
|
22
|
-
this.#perspective = perspective;
|
|
23
|
-
this.#source = source || "ad4m://self";
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
get baseExpression() {
|
|
27
|
-
return this.#baseExpression
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
private async getData(id?: string) {
|
|
31
|
-
const tempId = id ?? this.#baseExpression;
|
|
32
|
-
let isInstance = await this.#perspective.isSubjectInstance(tempId, this.#subjectClass)
|
|
33
|
-
if (!isInstance) {
|
|
34
|
-
throw `Not a valid subject instance of ${this.#subjectClass} for ${tempId}`
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
let results = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), property(C, Property)`)
|
|
38
|
-
let properties = results.map(result => result.Property)
|
|
39
|
-
|
|
40
|
-
for (let p of properties) {
|
|
41
|
-
const resolveExpressionURI = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), property_resolve(C, "${p}")`)
|
|
42
|
-
const getProperty = async () => {
|
|
43
|
-
let results = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), property_getter(C, "${tempId}", "${p}", Value)`)
|
|
44
|
-
if (results && results.length > 0) {
|
|
45
|
-
let expressionURI = results[0].Value
|
|
46
|
-
if (resolveExpressionURI) {
|
|
47
|
-
try {
|
|
48
|
-
const expression = await this.#perspective.getExpression(expressionURI)
|
|
49
|
-
try {
|
|
50
|
-
return JSON.parse(expression.data)
|
|
51
|
-
} catch (e) {
|
|
52
|
-
return expression.data
|
|
53
|
-
}
|
|
54
|
-
} catch (err) {
|
|
55
|
-
return expressionURI
|
|
56
|
-
}
|
|
57
|
-
} else {
|
|
58
|
-
return expressionURI
|
|
59
|
-
}
|
|
60
|
-
} else if (results) {
|
|
61
|
-
return results
|
|
62
|
-
} else {
|
|
63
|
-
return undefined
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
this[p] = await getProperty()
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
let results2 = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), collection(C, Collection)`)
|
|
71
|
-
if (!results2) results2 = []
|
|
72
|
-
let collections = results2.map(result => result.Collection)
|
|
73
|
-
|
|
74
|
-
for (let c of collections) {
|
|
75
|
-
const getProperty = async () => {
|
|
76
|
-
let results = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), collection_getter(C, "${tempId}", "${c}", Value)`)
|
|
77
|
-
if (results && results.length > 0 && results[0].Value) {
|
|
78
|
-
return eval(results[0].Value)
|
|
79
|
-
} else {
|
|
80
|
-
return []
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
this[c] = await getProperty()
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
this.#baseExpression = tempId;
|
|
88
|
-
|
|
89
|
-
return this
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
private async setProperty(key: string, value: any) {
|
|
93
|
-
const setters = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), property_setter(C, "${key}", Setter)`)
|
|
94
|
-
if (setters && setters.length > 0) {
|
|
95
|
-
const actions = eval(setters[0].Setter)
|
|
96
|
-
const resolveLanguageResults = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), property_resolve_language(C, "${key}", Language)`)
|
|
97
|
-
let resolveLanguage
|
|
98
|
-
if (resolveLanguageResults && resolveLanguageResults.length > 0) {
|
|
99
|
-
resolveLanguage = resolveLanguageResults[0].Language
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (resolveLanguage) {
|
|
103
|
-
value = await this.#perspective.createExpression(value, resolveLanguage)
|
|
104
|
-
}
|
|
105
|
-
await this.#perspective.executeAction(actions, this.#baseExpression, [{ name: "value", value }])
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
private async setCollectionSetter(key: string, value: any) {
|
|
110
|
-
let collectionSetters = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), collection_setter(C, "${singularToPlural(key)}", Setter)`)
|
|
111
|
-
if (!collectionSetters) collectionSetters = []
|
|
112
|
-
|
|
113
|
-
if (collectionSetters.length > 0) {
|
|
114
|
-
const actions = eval(collectionSetters[0].Setter)
|
|
115
|
-
|
|
116
|
-
if (value) {
|
|
117
|
-
if (Array.isArray(value)) {
|
|
118
|
-
await this.#perspective.executeAction(actions, this.#baseExpression, value.map(v => ({ name: "value", value: v })))
|
|
119
|
-
} else {
|
|
120
|
-
await this.#perspective.executeAction(actions, this.#baseExpression, [{ name: "value", value }])
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
private async setCollectionAdder(key: string, value: any) {
|
|
127
|
-
let adders = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), collection_adder(C, "${singularToPlural(key)}", Adder)`)
|
|
128
|
-
if (!adders) adders = []
|
|
129
|
-
|
|
130
|
-
if (adders.length > 0) {
|
|
131
|
-
const actions = eval(adders[0].Adder)
|
|
132
|
-
if (value) {
|
|
133
|
-
if (Array.isArray(value)) {
|
|
134
|
-
await Promise.all(value.map(v => this.#perspective.executeAction(actions, this.#baseExpression, [{ name: "value", value: v }])))
|
|
135
|
-
} else {
|
|
136
|
-
await this.#perspective.executeAction(actions, this.#baseExpression, [{ name: "value", value }])
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
private async setCollectionRemover(key: string, value: any) {
|
|
143
|
-
let removers = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), collection_remover(C, "${singularToPlural(key)}", Remover)`)
|
|
144
|
-
if (!removers) removers = []
|
|
145
|
-
|
|
146
|
-
if (removers.length > 0) {
|
|
147
|
-
const actions = eval(removers[0].Remover)
|
|
148
|
-
if (value) {
|
|
149
|
-
if (Array.isArray(value)) {
|
|
150
|
-
await Promise.all(value.map(v => this.#perspective.executeAction(actions, this.#baseExpression, [{ name: "value", value: v }])))
|
|
151
|
-
} else {
|
|
152
|
-
await this.#perspective.executeAction(actions, this.#baseExpression, [{ name: "value", value }])
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
async save() {
|
|
159
|
-
this.#subjectClass = await this.#perspective.stringOrTemplateObjectToSubjectClass(this)
|
|
160
|
-
|
|
161
|
-
await this.#perspective.createSubject(this, this.#baseExpression);
|
|
162
|
-
|
|
163
|
-
await this.#perspective.add(
|
|
164
|
-
new Link({
|
|
165
|
-
source: this.#source,
|
|
166
|
-
predicate: "ad4m://has_child",
|
|
167
|
-
target: this.baseExpression,
|
|
168
|
-
})
|
|
169
|
-
);
|
|
170
|
-
|
|
171
|
-
await this.update()
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
async update() {
|
|
175
|
-
this.#subjectClass = await this.#perspective.stringOrTemplateObjectToSubjectClass(this)
|
|
176
|
-
|
|
177
|
-
const entries = Object.entries(this);
|
|
178
|
-
|
|
179
|
-
for (const [key, value] of entries) {
|
|
180
|
-
if (value) {
|
|
181
|
-
if (value?.action) {
|
|
182
|
-
switch (value.action) {
|
|
183
|
-
case 'setter':
|
|
184
|
-
await this.setCollectionSetter(key, value.value)
|
|
185
|
-
break;
|
|
186
|
-
case "adder":
|
|
187
|
-
await this.setCollectionAdder(key, value.value)
|
|
188
|
-
break;
|
|
189
|
-
case 'remover':
|
|
190
|
-
await this.setCollectionRemover(key, value.value)
|
|
191
|
-
default:
|
|
192
|
-
await this.setCollectionSetter(key, value.value)
|
|
193
|
-
break;
|
|
194
|
-
}
|
|
195
|
-
} else if (Array.isArray(value) && value.length > 0) {
|
|
196
|
-
await this.setCollectionSetter(key, value)
|
|
197
|
-
} else {
|
|
198
|
-
await this.setProperty(key, value);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
await this.getData();
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
async get() {
|
|
207
|
-
this.#subjectClass = await this.#perspective.stringOrTemplateObjectToSubjectClass(this)
|
|
208
|
-
|
|
209
|
-
return await this.getData()
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
async delete() {
|
|
213
|
-
await this.#perspective.removeSubject(this, this.#baseExpression);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// TODO: implement simple quering like limit, skip etc.
|
|
217
|
-
static async all(perspective: PerspectiveProxy) {
|
|
218
|
-
let subjectClass = await perspective.stringOrTemplateObjectToSubjectClass(this)
|
|
219
|
-
const proxies = await perspective.getAllSubjectProxies(subjectClass)
|
|
220
|
-
|
|
221
|
-
const instances = []
|
|
222
|
-
|
|
223
|
-
if (proxies) {
|
|
224
|
-
for (const proxy of proxies) {
|
|
225
|
-
// @ts-ignore
|
|
226
|
-
const instance = new this(perspective, proxy.X)
|
|
227
|
-
instances.push(await instance.get())
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return instances;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
return []
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
static async query(perspective: PerspectiveProxy, query?: SubjectEntityQueryParam) {
|
|
237
|
-
const source = query?.source || "ad4m://self";
|
|
238
|
-
let subjectClass = await perspective.stringOrTemplateObjectToSubjectClass(this)
|
|
239
|
-
|
|
240
|
-
let res = [];
|
|
241
|
-
|
|
242
|
-
if (query) {
|
|
243
|
-
try {
|
|
244
|
-
const queryResponse = (await perspective.infer(`findall([Timestamp, Base], (subject_class("${subjectClass}", C), instance(C, Base), link("${source}", Predicate, Base, Timestamp, Author)), AllData), sort(AllData, SortedData), length(SortedData, DataLength).`))[0]
|
|
245
|
-
|
|
246
|
-
if (queryResponse.DataLength >= query.size) {
|
|
247
|
-
const mainQuery = `findall([Timestamp, Base], (subject_class("${subjectClass}", C), instance(C, Base), link("${source}", Predicate, Base, Timestamp, Author)), AllData), sort(AllData, SortedData), reverse(SortedData, ReverseSortedData), paginate(ReverseSortedData, ${query.page}, ${query.size}, PageData).`
|
|
248
|
-
|
|
249
|
-
res = await perspective.infer(mainQuery);
|
|
250
|
-
|
|
251
|
-
res = res[0].PageData.map(r => ({
|
|
252
|
-
Base: r[1],
|
|
253
|
-
Timestamp: r[0]
|
|
254
|
-
}))
|
|
255
|
-
} else {
|
|
256
|
-
res = await perspective.infer(
|
|
257
|
-
`subject_class("${subjectClass}", C), instance(C, Base), triple("${source}", Predicate, Base).`
|
|
258
|
-
);
|
|
259
|
-
}
|
|
260
|
-
} catch (e) {
|
|
261
|
-
console.log("Query failed", e);
|
|
262
|
-
}
|
|
263
|
-
} else {
|
|
264
|
-
res = await perspective.infer(
|
|
265
|
-
`subject_class("${subjectClass}", C), instance(C, Base), triple("${source}", Predicate, Base).`
|
|
266
|
-
);
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
if (!res) return [];
|
|
270
|
-
|
|
271
|
-
const data = await Promise.all(
|
|
272
|
-
res.map(async (result) => {
|
|
273
|
-
const instance = new this(perspective, result.Base)
|
|
274
|
-
|
|
275
|
-
return await instance.get();
|
|
276
|
-
})
|
|
277
|
-
);
|
|
278
|
-
|
|
279
|
-
return data;
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
export type SubjectArray<T> = T[] | {
|
|
284
|
-
action: 'setter' | 'adder' | 'remover',
|
|
285
|
-
value: T[]
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
export type SubjectEntityQueryParam = {
|
|
289
|
-
source?: string;
|
|
290
|
-
size?: number;
|
|
291
|
-
page?: number;
|
|
1
|
+
import { Literal } from "../Literal";
|
|
2
|
+
import { Link } from "../links/Links";
|
|
3
|
+
import { PerspectiveProxy } from "../perspectives/PerspectiveProxy";
|
|
4
|
+
import { makeRandomPrologAtom } from "./SDNADecorators";
|
|
5
|
+
import { singularToPlural } from "./util";
|
|
6
|
+
|
|
7
|
+
export type QueryPartialEntity<T> = {
|
|
8
|
+
[P in keyof T]?: T[P] | (() => string);
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
export class SubjectEntity {
|
|
13
|
+
#baseExpression: string;
|
|
14
|
+
#subjectClass: string;
|
|
15
|
+
#source: string;
|
|
16
|
+
#perspective: PerspectiveProxy
|
|
17
|
+
author: string;
|
|
18
|
+
timestamp: string;
|
|
19
|
+
|
|
20
|
+
constructor(perspective: PerspectiveProxy, baseExpression?: string, source?: string) {
|
|
21
|
+
this.#baseExpression = baseExpression ? baseExpression : Literal.from(makeRandomPrologAtom(24)).toUrl();
|
|
22
|
+
this.#perspective = perspective;
|
|
23
|
+
this.#source = source || "ad4m://self";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get baseExpression() {
|
|
27
|
+
return this.#baseExpression
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
private async getData(id?: string) {
|
|
31
|
+
const tempId = id ?? this.#baseExpression;
|
|
32
|
+
let isInstance = await this.#perspective.isSubjectInstance(tempId, this.#subjectClass)
|
|
33
|
+
if (!isInstance) {
|
|
34
|
+
throw `Not a valid subject instance of ${this.#subjectClass} for ${tempId}`
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
let results = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), property(C, Property)`)
|
|
38
|
+
let properties = results.map(result => result.Property)
|
|
39
|
+
|
|
40
|
+
for (let p of properties) {
|
|
41
|
+
const resolveExpressionURI = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), property_resolve(C, "${p}")`)
|
|
42
|
+
const getProperty = async () => {
|
|
43
|
+
let results = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), property_getter(C, "${tempId}", "${p}", Value)`)
|
|
44
|
+
if (results && results.length > 0) {
|
|
45
|
+
let expressionURI = results[0].Value
|
|
46
|
+
if (resolveExpressionURI) {
|
|
47
|
+
try {
|
|
48
|
+
const expression = await this.#perspective.getExpression(expressionURI)
|
|
49
|
+
try {
|
|
50
|
+
return JSON.parse(expression.data)
|
|
51
|
+
} catch (e) {
|
|
52
|
+
return expression.data
|
|
53
|
+
}
|
|
54
|
+
} catch (err) {
|
|
55
|
+
return expressionURI
|
|
56
|
+
}
|
|
57
|
+
} else {
|
|
58
|
+
return expressionURI
|
|
59
|
+
}
|
|
60
|
+
} else if (results) {
|
|
61
|
+
return results
|
|
62
|
+
} else {
|
|
63
|
+
return undefined
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
this[p] = await getProperty()
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
let results2 = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), collection(C, Collection)`)
|
|
71
|
+
if (!results2) results2 = []
|
|
72
|
+
let collections = results2.map(result => result.Collection)
|
|
73
|
+
|
|
74
|
+
for (let c of collections) {
|
|
75
|
+
const getProperty = async () => {
|
|
76
|
+
let results = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), collection_getter(C, "${tempId}", "${c}", Value)`)
|
|
77
|
+
if (results && results.length > 0 && results[0].Value) {
|
|
78
|
+
return eval(results[0].Value)
|
|
79
|
+
} else {
|
|
80
|
+
return []
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
this[c] = await getProperty()
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
this.#baseExpression = tempId;
|
|
88
|
+
|
|
89
|
+
return this
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private async setProperty(key: string, value: any) {
|
|
93
|
+
const setters = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), property_setter(C, "${key}", Setter)`)
|
|
94
|
+
if (setters && setters.length > 0) {
|
|
95
|
+
const actions = eval(setters[0].Setter)
|
|
96
|
+
const resolveLanguageResults = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), property_resolve_language(C, "${key}", Language)`)
|
|
97
|
+
let resolveLanguage
|
|
98
|
+
if (resolveLanguageResults && resolveLanguageResults.length > 0) {
|
|
99
|
+
resolveLanguage = resolveLanguageResults[0].Language
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (resolveLanguage) {
|
|
103
|
+
value = await this.#perspective.createExpression(value, resolveLanguage)
|
|
104
|
+
}
|
|
105
|
+
await this.#perspective.executeAction(actions, this.#baseExpression, [{ name: "value", value }])
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
private async setCollectionSetter(key: string, value: any) {
|
|
110
|
+
let collectionSetters = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), collection_setter(C, "${singularToPlural(key)}", Setter)`)
|
|
111
|
+
if (!collectionSetters) collectionSetters = []
|
|
112
|
+
|
|
113
|
+
if (collectionSetters.length > 0) {
|
|
114
|
+
const actions = eval(collectionSetters[0].Setter)
|
|
115
|
+
|
|
116
|
+
if (value) {
|
|
117
|
+
if (Array.isArray(value)) {
|
|
118
|
+
await this.#perspective.executeAction(actions, this.#baseExpression, value.map(v => ({ name: "value", value: v })))
|
|
119
|
+
} else {
|
|
120
|
+
await this.#perspective.executeAction(actions, this.#baseExpression, [{ name: "value", value }])
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private async setCollectionAdder(key: string, value: any) {
|
|
127
|
+
let adders = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), collection_adder(C, "${singularToPlural(key)}", Adder)`)
|
|
128
|
+
if (!adders) adders = []
|
|
129
|
+
|
|
130
|
+
if (adders.length > 0) {
|
|
131
|
+
const actions = eval(adders[0].Adder)
|
|
132
|
+
if (value) {
|
|
133
|
+
if (Array.isArray(value)) {
|
|
134
|
+
await Promise.all(value.map(v => this.#perspective.executeAction(actions, this.#baseExpression, [{ name: "value", value: v }])))
|
|
135
|
+
} else {
|
|
136
|
+
await this.#perspective.executeAction(actions, this.#baseExpression, [{ name: "value", value }])
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private async setCollectionRemover(key: string, value: any) {
|
|
143
|
+
let removers = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), collection_remover(C, "${singularToPlural(key)}", Remover)`)
|
|
144
|
+
if (!removers) removers = []
|
|
145
|
+
|
|
146
|
+
if (removers.length > 0) {
|
|
147
|
+
const actions = eval(removers[0].Remover)
|
|
148
|
+
if (value) {
|
|
149
|
+
if (Array.isArray(value)) {
|
|
150
|
+
await Promise.all(value.map(v => this.#perspective.executeAction(actions, this.#baseExpression, [{ name: "value", value: v }])))
|
|
151
|
+
} else {
|
|
152
|
+
await this.#perspective.executeAction(actions, this.#baseExpression, [{ name: "value", value }])
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async save() {
|
|
159
|
+
this.#subjectClass = await this.#perspective.stringOrTemplateObjectToSubjectClass(this)
|
|
160
|
+
|
|
161
|
+
await this.#perspective.createSubject(this, this.#baseExpression);
|
|
162
|
+
|
|
163
|
+
await this.#perspective.add(
|
|
164
|
+
new Link({
|
|
165
|
+
source: this.#source,
|
|
166
|
+
predicate: "ad4m://has_child",
|
|
167
|
+
target: this.baseExpression,
|
|
168
|
+
})
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
await this.update()
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async update() {
|
|
175
|
+
this.#subjectClass = await this.#perspective.stringOrTemplateObjectToSubjectClass(this)
|
|
176
|
+
|
|
177
|
+
const entries = Object.entries(this);
|
|
178
|
+
|
|
179
|
+
for (const [key, value] of entries) {
|
|
180
|
+
if (value) {
|
|
181
|
+
if (value?.action) {
|
|
182
|
+
switch (value.action) {
|
|
183
|
+
case 'setter':
|
|
184
|
+
await this.setCollectionSetter(key, value.value)
|
|
185
|
+
break;
|
|
186
|
+
case "adder":
|
|
187
|
+
await this.setCollectionAdder(key, value.value)
|
|
188
|
+
break;
|
|
189
|
+
case 'remover':
|
|
190
|
+
await this.setCollectionRemover(key, value.value)
|
|
191
|
+
default:
|
|
192
|
+
await this.setCollectionSetter(key, value.value)
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
} else if (Array.isArray(value) && value.length > 0) {
|
|
196
|
+
await this.setCollectionSetter(key, value)
|
|
197
|
+
} else {
|
|
198
|
+
await this.setProperty(key, value);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
await this.getData();
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async get() {
|
|
207
|
+
this.#subjectClass = await this.#perspective.stringOrTemplateObjectToSubjectClass(this)
|
|
208
|
+
|
|
209
|
+
return await this.getData()
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async delete() {
|
|
213
|
+
await this.#perspective.removeSubject(this, this.#baseExpression);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// TODO: implement simple quering like limit, skip etc.
|
|
217
|
+
static async all(perspective: PerspectiveProxy) {
|
|
218
|
+
let subjectClass = await perspective.stringOrTemplateObjectToSubjectClass(this)
|
|
219
|
+
const proxies = await perspective.getAllSubjectProxies(subjectClass)
|
|
220
|
+
|
|
221
|
+
const instances = []
|
|
222
|
+
|
|
223
|
+
if (proxies) {
|
|
224
|
+
for (const proxy of proxies) {
|
|
225
|
+
// @ts-ignore
|
|
226
|
+
const instance = new this(perspective, proxy.X)
|
|
227
|
+
instances.push(await instance.get())
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return instances;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return []
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
static async query(perspective: PerspectiveProxy, query?: SubjectEntityQueryParam) {
|
|
237
|
+
const source = query?.source || "ad4m://self";
|
|
238
|
+
let subjectClass = await perspective.stringOrTemplateObjectToSubjectClass(this)
|
|
239
|
+
|
|
240
|
+
let res = [];
|
|
241
|
+
|
|
242
|
+
if (query) {
|
|
243
|
+
try {
|
|
244
|
+
const queryResponse = (await perspective.infer(`findall([Timestamp, Base], (subject_class("${subjectClass}", C), instance(C, Base), link("${source}", Predicate, Base, Timestamp, Author)), AllData), sort(AllData, SortedData), length(SortedData, DataLength).`))[0]
|
|
245
|
+
|
|
246
|
+
if (queryResponse.DataLength >= query.size) {
|
|
247
|
+
const mainQuery = `findall([Timestamp, Base], (subject_class("${subjectClass}", C), instance(C, Base), link("${source}", Predicate, Base, Timestamp, Author)), AllData), sort(AllData, SortedData), reverse(SortedData, ReverseSortedData), paginate(ReverseSortedData, ${query.page}, ${query.size}, PageData).`
|
|
248
|
+
|
|
249
|
+
res = await perspective.infer(mainQuery);
|
|
250
|
+
|
|
251
|
+
res = res[0].PageData.map(r => ({
|
|
252
|
+
Base: r[1],
|
|
253
|
+
Timestamp: r[0]
|
|
254
|
+
}))
|
|
255
|
+
} else {
|
|
256
|
+
res = await perspective.infer(
|
|
257
|
+
`subject_class("${subjectClass}", C), instance(C, Base), triple("${source}", Predicate, Base).`
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
} catch (e) {
|
|
261
|
+
console.log("Query failed", e);
|
|
262
|
+
}
|
|
263
|
+
} else {
|
|
264
|
+
res = await perspective.infer(
|
|
265
|
+
`subject_class("${subjectClass}", C), instance(C, Base), triple("${source}", Predicate, Base).`
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (!res) return [];
|
|
270
|
+
|
|
271
|
+
const data = await Promise.all(
|
|
272
|
+
res.map(async (result) => {
|
|
273
|
+
const instance = new this(perspective, result.Base)
|
|
274
|
+
|
|
275
|
+
return await instance.get();
|
|
276
|
+
})
|
|
277
|
+
);
|
|
278
|
+
|
|
279
|
+
return data;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export type SubjectArray<T> = T[] | {
|
|
284
|
+
action: 'setter' | 'adder' | 'remover',
|
|
285
|
+
value: T[]
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export type SubjectEntityQueryParam = {
|
|
289
|
+
source?: string;
|
|
290
|
+
size?: number;
|
|
291
|
+
page?: number;
|
|
292
292
|
}
|