@coasys/ad4m 0.8.0 → 0.8.2-prerelease
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/lib/index.cjs +29 -53
- package/lib/index.js +8085 -0
- package/lib/shims/reflect-metadata.js +0 -0
- package/lib/shims/type-graphql.js +70 -0
- package/lib/src/Ad4mClient.js +66 -0
- package/lib/src/Address.js +1 -0
- package/lib/src/DID.js +1 -0
- package/lib/src/Exception.js +7 -0
- package/lib/src/Literal.js +76 -0
- package/lib/src/PubSub.js +14 -0
- package/lib/src/SmartLiteral.js +74 -0
- package/lib/src/agent/Agent.js +337 -0
- package/lib/src/agent/AgentClient.js +431 -0
- package/lib/src/agent/AgentResolver.js +325 -0
- package/lib/src/agent/AgentStatus.js +61 -0
- package/lib/src/buildSchema.js +36 -0
- package/lib/src/expression/Expression.js +143 -0
- package/lib/src/expression/ExpressionClient.js +102 -0
- package/lib/src/expression/ExpressionRef.js +67 -0
- package/lib/src/expression/ExpressionResolver.js +106 -0
- package/lib/src/index.js +30 -0
- package/lib/src/jestSetup.js +1 -0
- package/lib/src/language/Icon.js +24 -0
- package/lib/src/language/Language.js +79 -0
- package/lib/src/language/LanguageClient.js +137 -0
- package/lib/src/language/LanguageContext.js +2 -0
- package/lib/src/language/LanguageHandle.js +41 -0
- package/lib/src/language/LanguageMeta.js +89 -0
- package/lib/src/language/LanguageRef.js +30 -0
- package/lib/src/language/LanguageResolver.js +143 -0
- package/lib/src/links/Links.js +148 -0
- package/lib/src/links/Links.test.js +33 -0
- package/lib/src/neighbourhood/Neighbourhood.js +38 -0
- package/lib/src/neighbourhood/NeighbourhoodClient.js +256 -0
- package/lib/src/neighbourhood/NeighbourhoodProxy.js +54 -0
- package/lib/src/neighbourhood/NeighbourhoodResolver.js +180 -0
- package/lib/src/perspectives/LinkQuery.js +80 -0
- package/lib/src/perspectives/Perspective.js +144 -0
- package/lib/src/perspectives/Perspective.test.js +20 -0
- package/lib/src/perspectives/PerspectiveClient.d.ts +1 -0
- package/lib/src/perspectives/PerspectiveClient.js +418 -0
- package/lib/src/perspectives/PerspectiveDiff.js +33 -0
- package/lib/src/perspectives/PerspectiveHandle.js +59 -0
- package/lib/src/perspectives/PerspectiveProxy.d.ts +8 -8
- package/lib/src/perspectives/PerspectiveProxy.js +501 -0
- package/lib/src/perspectives/PerspectiveResolver.d.ts +1 -0
- package/lib/src/perspectives/PerspectiveResolver.js +376 -0
- package/lib/src/runtime/RuntimeClient.js +263 -0
- package/lib/src/runtime/RuntimeResolver.js +328 -0
- package/lib/src/schema.gql +1 -0
- package/lib/src/subject/SDNADecorators.js +250 -0
- package/lib/src/subject/Subject.js +164 -0
- package/lib/src/subject/SubjectEntity.js +250 -0
- package/lib/src/subject/util.js +73 -0
- package/lib/src/typeDefs.js +380 -0
- package/lib/src/unwrapApolloResult.js +14 -0
- package/lib/src/utils.js +21 -0
- package/package.json +9 -2
- package/patches/graphql@15.7.2.patch +39 -0
- package/pnpm-workspace.yaml +19 -0
- package/src/Ad4mClient.test.ts +5 -0
- package/src/perspectives/PerspectiveClient.ts +9 -0
- package/src/perspectives/PerspectiveProxy.ts +22 -56
- package/src/perspectives/PerspectiveResolver.ts +5 -0
- package/src/subject/Subject.ts +9 -6
- package/src/unwrapApolloResult.ts +3 -1
|
@@ -0,0 +1,501 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _PerspectiveProxy_handle, _PerspectiveProxy_client, _PerspectiveProxy_perspectiveLinkAddedCallbacks, _PerspectiveProxy_perspectiveLinkRemovedCallbacks, _PerspectiveProxy_perspectiveLinkUpdatedCallbacks, _PerspectiveProxy_perspectiveSyncStateChangeCallbacks;
|
|
13
|
+
import { Link } from "../links/Links";
|
|
14
|
+
import { LinkQuery } from "./LinkQuery";
|
|
15
|
+
import { Literal } from "../Literal";
|
|
16
|
+
import { Subject } from "../subject/Subject";
|
|
17
|
+
import { collectionAdderToName, collectionRemoverToName, collectionSetterToName } from "../subject/util";
|
|
18
|
+
/** Perspective UI proxy object
|
|
19
|
+
*
|
|
20
|
+
* Convenience object for UIs to interact with a perspective.
|
|
21
|
+
* It is created by some of the methods in the PerspectiveClient class and includes
|
|
22
|
+
* a reference to the PerspectiveClient object that created it.
|
|
23
|
+
*/
|
|
24
|
+
export class PerspectiveProxy {
|
|
25
|
+
constructor(handle, ad4m) {
|
|
26
|
+
_PerspectiveProxy_handle.set(this, void 0);
|
|
27
|
+
_PerspectiveProxy_client.set(this, void 0);
|
|
28
|
+
_PerspectiveProxy_perspectiveLinkAddedCallbacks.set(this, void 0);
|
|
29
|
+
_PerspectiveProxy_perspectiveLinkRemovedCallbacks.set(this, void 0);
|
|
30
|
+
_PerspectiveProxy_perspectiveLinkUpdatedCallbacks.set(this, void 0);
|
|
31
|
+
_PerspectiveProxy_perspectiveSyncStateChangeCallbacks.set(this, void 0);
|
|
32
|
+
__classPrivateFieldSet(this, _PerspectiveProxy_perspectiveLinkAddedCallbacks, [], "f");
|
|
33
|
+
__classPrivateFieldSet(this, _PerspectiveProxy_perspectiveLinkRemovedCallbacks, [], "f");
|
|
34
|
+
__classPrivateFieldSet(this, _PerspectiveProxy_perspectiveLinkUpdatedCallbacks, [], "f");
|
|
35
|
+
__classPrivateFieldSet(this, _PerspectiveProxy_perspectiveSyncStateChangeCallbacks, [], "f");
|
|
36
|
+
__classPrivateFieldSet(this, _PerspectiveProxy_handle, handle, "f");
|
|
37
|
+
__classPrivateFieldSet(this, _PerspectiveProxy_client, ad4m, "f");
|
|
38
|
+
this.uuid = __classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid;
|
|
39
|
+
this.name = __classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").name;
|
|
40
|
+
this.sharedUrl = __classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").sharedUrl;
|
|
41
|
+
this.neighbourhood = __classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").neighbourhood;
|
|
42
|
+
this.state = __classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").state;
|
|
43
|
+
__classPrivateFieldGet(this, _PerspectiveProxy_client, "f").addPerspectiveLinkAddedListener(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, __classPrivateFieldGet(this, _PerspectiveProxy_perspectiveLinkAddedCallbacks, "f"));
|
|
44
|
+
__classPrivateFieldGet(this, _PerspectiveProxy_client, "f").addPerspectiveLinkRemovedListener(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, __classPrivateFieldGet(this, _PerspectiveProxy_perspectiveLinkRemovedCallbacks, "f"));
|
|
45
|
+
__classPrivateFieldGet(this, _PerspectiveProxy_client, "f").addPerspectiveLinkUpdatedListener(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, __classPrivateFieldGet(this, _PerspectiveProxy_perspectiveLinkUpdatedCallbacks, "f"));
|
|
46
|
+
__classPrivateFieldGet(this, _PerspectiveProxy_client, "f").addPerspectiveSyncStateChangeListener(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, __classPrivateFieldGet(this, _PerspectiveProxy_perspectiveSyncStateChangeCallbacks, "f"));
|
|
47
|
+
}
|
|
48
|
+
async executeAction(actions, expression, parameters) {
|
|
49
|
+
const replaceThis = (input) => {
|
|
50
|
+
if (input) {
|
|
51
|
+
if (input === 'this') {
|
|
52
|
+
return expression;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
return input;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
const replaceParameters = (input) => {
|
|
63
|
+
if (parameters) {
|
|
64
|
+
let output = input;
|
|
65
|
+
for (const parameter of parameters) {
|
|
66
|
+
output = output.replace(parameter.name, parameter.value);
|
|
67
|
+
}
|
|
68
|
+
return output;
|
|
69
|
+
}
|
|
70
|
+
else
|
|
71
|
+
return input;
|
|
72
|
+
};
|
|
73
|
+
for (let command of actions) {
|
|
74
|
+
let source = replaceThis(replaceParameters(command.source));
|
|
75
|
+
let predicate = replaceThis(replaceParameters(command.predicate));
|
|
76
|
+
let target = replaceThis(replaceParameters(command.target));
|
|
77
|
+
let local = command?.local ?? false;
|
|
78
|
+
switch (command.action) {
|
|
79
|
+
case 'addLink':
|
|
80
|
+
await this.add(new Link({ source, predicate, target }), local ? 'local' : 'shared');
|
|
81
|
+
break;
|
|
82
|
+
case 'removeLink':
|
|
83
|
+
const linkExpressions = await this.get(new LinkQuery({ source, predicate, target }));
|
|
84
|
+
for (const linkExpression of linkExpressions) {
|
|
85
|
+
await this.remove(linkExpression);
|
|
86
|
+
}
|
|
87
|
+
break;
|
|
88
|
+
case 'setSingleTarget':
|
|
89
|
+
await this.setSingleTarget(new Link({ source, predicate, target }), local ? 'local' : 'shared');
|
|
90
|
+
break;
|
|
91
|
+
case 'collectionSetter':
|
|
92
|
+
const links = await this.get(new LinkQuery({ source, predicate }));
|
|
93
|
+
await this.removeLinks(links);
|
|
94
|
+
await this.addLinks(parameters.map(p => new Link({ source, predicate, target: p.value })), local ? 'local' : 'shared');
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/** Returns all the links of this perspective that matches the LinkQuery */
|
|
100
|
+
async get(query) {
|
|
101
|
+
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").queryLinks(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, query);
|
|
102
|
+
}
|
|
103
|
+
/** Runs a Prolog query on the perspective's Prolog engine */
|
|
104
|
+
async infer(query) {
|
|
105
|
+
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").queryProlog(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, query);
|
|
106
|
+
}
|
|
107
|
+
/** Adds a link to this perspective */
|
|
108
|
+
async add(link, status = 'shared') {
|
|
109
|
+
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").addLink(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, link, status);
|
|
110
|
+
}
|
|
111
|
+
/** Adds multiple links to this perspective **/
|
|
112
|
+
async addLinks(links, status = 'shared') {
|
|
113
|
+
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").addLinks(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, links, status);
|
|
114
|
+
}
|
|
115
|
+
/** Removes multiple links from this perspective **/
|
|
116
|
+
async removeLinks(links) {
|
|
117
|
+
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").removeLinks(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, links);
|
|
118
|
+
}
|
|
119
|
+
/** Adds and removes multiple links from this perspective **/
|
|
120
|
+
async linkMutations(mutations, status = 'shared') {
|
|
121
|
+
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").linkMutations(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, mutations, status);
|
|
122
|
+
}
|
|
123
|
+
/** Adds a linkExpression to this perspective */
|
|
124
|
+
async addLinkExpression(link, status = 'shared') {
|
|
125
|
+
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").addLinkExpression(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, link, status);
|
|
126
|
+
}
|
|
127
|
+
async update(oldLink, newLink) {
|
|
128
|
+
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").updateLink(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, oldLink, newLink);
|
|
129
|
+
}
|
|
130
|
+
async remove(link) {
|
|
131
|
+
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").removeLink(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, link);
|
|
132
|
+
}
|
|
133
|
+
async getExpression(expressionURI) {
|
|
134
|
+
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").getExpression(expressionURI);
|
|
135
|
+
}
|
|
136
|
+
async createExpression(content, languageAddress) {
|
|
137
|
+
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").createExpression(content, languageAddress);
|
|
138
|
+
}
|
|
139
|
+
/** Adds a link listener
|
|
140
|
+
* @param type Can be 'link-added' or 'link-removed'
|
|
141
|
+
* @param cb Callback function that is called when a link is added to the perspective
|
|
142
|
+
*/
|
|
143
|
+
async addListener(type, cb) {
|
|
144
|
+
if (type === 'link-added') {
|
|
145
|
+
__classPrivateFieldGet(this, _PerspectiveProxy_perspectiveLinkAddedCallbacks, "f").push(cb);
|
|
146
|
+
}
|
|
147
|
+
else if (type === 'link-removed') {
|
|
148
|
+
__classPrivateFieldGet(this, _PerspectiveProxy_perspectiveLinkRemovedCallbacks, "f").push(cb);
|
|
149
|
+
}
|
|
150
|
+
else if (type === 'link-updated') {
|
|
151
|
+
__classPrivateFieldGet(this, _PerspectiveProxy_perspectiveLinkUpdatedCallbacks, "f").push(cb);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/** Adds a sync state listener
|
|
155
|
+
* @param cb Callback function that is called when the sync state of the perspective changes
|
|
156
|
+
* @returns A function that can be called to remove the listener
|
|
157
|
+
*/
|
|
158
|
+
async addSyncStateChangeListener(cb) {
|
|
159
|
+
__classPrivateFieldGet(this, _PerspectiveProxy_perspectiveSyncStateChangeCallbacks, "f").push(cb);
|
|
160
|
+
}
|
|
161
|
+
/** Removes a previously added link listener
|
|
162
|
+
* @param type Can be 'link-added' or 'link-removed'
|
|
163
|
+
* @param cb Callback function that is called when a link is added to the perspective
|
|
164
|
+
*/
|
|
165
|
+
async removeListener(type, cb) {
|
|
166
|
+
if (type === 'link-added') {
|
|
167
|
+
const index = __classPrivateFieldGet(this, _PerspectiveProxy_perspectiveLinkAddedCallbacks, "f").indexOf(cb);
|
|
168
|
+
__classPrivateFieldGet(this, _PerspectiveProxy_perspectiveLinkAddedCallbacks, "f").splice(index, 1);
|
|
169
|
+
}
|
|
170
|
+
else if (type === 'link-removed') {
|
|
171
|
+
const index = __classPrivateFieldGet(this, _PerspectiveProxy_perspectiveLinkRemovedCallbacks, "f").indexOf(cb);
|
|
172
|
+
__classPrivateFieldGet(this, _PerspectiveProxy_perspectiveLinkRemovedCallbacks, "f").splice(index, 1);
|
|
173
|
+
}
|
|
174
|
+
else if (type === 'link-updated') {
|
|
175
|
+
const index = __classPrivateFieldGet(this, _PerspectiveProxy_perspectiveLinkUpdatedCallbacks, "f").indexOf(cb);
|
|
176
|
+
__classPrivateFieldGet(this, _PerspectiveProxy_perspectiveLinkUpdatedCallbacks, "f").splice(index, 1);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/** Create and return a snapshot of this perspective
|
|
180
|
+
* A snapshot is a rendered Perspectie object that contains all the links of the perspective.
|
|
181
|
+
*/
|
|
182
|
+
async snapshot() {
|
|
183
|
+
return __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").snapshotByUUID(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
184
|
+
}
|
|
185
|
+
/** Take and load all the links from the given snapshot */
|
|
186
|
+
async loadSnapshot(snapshot) {
|
|
187
|
+
//Clean the input data from __typename
|
|
188
|
+
const cleanedSnapshot = JSON.parse(JSON.stringify(snapshot));
|
|
189
|
+
delete cleanedSnapshot.__typename;
|
|
190
|
+
cleanedSnapshot.links.forEach(link => {
|
|
191
|
+
delete link.data.__typename;
|
|
192
|
+
});
|
|
193
|
+
for (const link of cleanedSnapshot.links) {
|
|
194
|
+
await this.addLinkExpression(link);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
/** Convenience function to get the target of the first link that matches the given query
|
|
198
|
+
* This makes sense when the query is expected to return only one link
|
|
199
|
+
* and the target of that link is what you are looking for.
|
|
200
|
+
*
|
|
201
|
+
* Works best together with @member setSingelTarget()
|
|
202
|
+
*/
|
|
203
|
+
async getSingleTarget(query) {
|
|
204
|
+
delete query.target;
|
|
205
|
+
const foundLinks = await this.get(query);
|
|
206
|
+
if (foundLinks.length)
|
|
207
|
+
return foundLinks[0].data.target;
|
|
208
|
+
else
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
/** Convenience function to ensure there is only one link with given source and predicate
|
|
212
|
+
* This function will remove all links with the same source and predicate as the given link,
|
|
213
|
+
* and then add the given link.
|
|
214
|
+
* This ensures there is only one target for the given source and predicate.
|
|
215
|
+
*
|
|
216
|
+
* Works best together with @member getSingleTarget()
|
|
217
|
+
*/
|
|
218
|
+
async setSingleTarget(link, status = 'shared') {
|
|
219
|
+
const query = new LinkQuery({ source: link.source, predicate: link.predicate });
|
|
220
|
+
const foundLinks = await this.get(query);
|
|
221
|
+
const removals = [];
|
|
222
|
+
for (const l of foundLinks) {
|
|
223
|
+
delete l.__typename;
|
|
224
|
+
delete l.data.__typename;
|
|
225
|
+
delete l.proof.__typename;
|
|
226
|
+
removals.push(l);
|
|
227
|
+
}
|
|
228
|
+
const additions = [link];
|
|
229
|
+
await this.linkMutations({ additions, removals }, status);
|
|
230
|
+
}
|
|
231
|
+
/** Returns all the Social DNA flows defined in this perspective */
|
|
232
|
+
async sdnaFlows() {
|
|
233
|
+
const allFlows = await this.infer("register_sdna_flow(X, _)");
|
|
234
|
+
return allFlows.map(x => x.X);
|
|
235
|
+
}
|
|
236
|
+
/** Returns all Social DNA flows that can be started from the given expression */
|
|
237
|
+
async availableFlows(exprAddr) {
|
|
238
|
+
const availableFlows = await this.infer(`flowable("${exprAddr}", F), register_sdna_flow(X, F)`);
|
|
239
|
+
return availableFlows.map(x => x.X);
|
|
240
|
+
}
|
|
241
|
+
/** Starts the Social DNA flow @param flowName on the expression @param exprAddr */
|
|
242
|
+
async startFlow(flowName, exprAddr) {
|
|
243
|
+
let startAction = await this.infer(`start_action(Action, F), register_sdna_flow("${flowName}", F)`);
|
|
244
|
+
// should always return one solution...
|
|
245
|
+
startAction = eval(startAction[0].Action);
|
|
246
|
+
await this.executeAction(startAction, exprAddr, undefined);
|
|
247
|
+
}
|
|
248
|
+
/** Returns all expressions in the given state of given Social DNA flow */
|
|
249
|
+
async expressionsInFlowState(flowName, flowState) {
|
|
250
|
+
let expressions = await this.infer(`register_sdna_flow("${flowName}", F), flow_state(X, ${flowState}, F)`);
|
|
251
|
+
return expressions.map(r => r.X);
|
|
252
|
+
}
|
|
253
|
+
/** Returns the given expression's flow state with regard to given Social DNA flow */
|
|
254
|
+
async flowState(flowName, exprAddr) {
|
|
255
|
+
let state = await this.infer(`register_sdna_flow("${flowName}", F), flow_state("${exprAddr}", X, F)`);
|
|
256
|
+
return state[0].X;
|
|
257
|
+
}
|
|
258
|
+
/** Returns available action names, with regard to Social DNA flow and expression's flow state */
|
|
259
|
+
async flowActions(flowName, exprAddr) {
|
|
260
|
+
let actionNames = await this.infer(`register_sdna_flow("${flowName}", Flow), flow_state("${exprAddr}", State, Flow), action(State, Name, _, _)`);
|
|
261
|
+
return actionNames.map(r => r.Name);
|
|
262
|
+
}
|
|
263
|
+
/** Runs given Social DNA flow action */
|
|
264
|
+
async runFlowAction(flowName, exprAddr, actionName) {
|
|
265
|
+
let action = await this.infer(`register_sdna_flow("${flowName}", Flow), flow_state("${exprAddr}", State, Flow), action(State, "${actionName}", _, Action)`);
|
|
266
|
+
// should find only one
|
|
267
|
+
action = eval(action[0].Action);
|
|
268
|
+
await this.executeAction(action, exprAddr, undefined);
|
|
269
|
+
}
|
|
270
|
+
/** Returns the perspective's Social DNA code
|
|
271
|
+
* This will return all SDNA code elements in an array.
|
|
272
|
+
*/
|
|
273
|
+
async getSdna() {
|
|
274
|
+
let links = await this.get(new LinkQuery({
|
|
275
|
+
predicate: "ad4m://sdna"
|
|
276
|
+
}));
|
|
277
|
+
return links.map(link => link.data.target).map(t => Literal.fromUrl(t).get());
|
|
278
|
+
}
|
|
279
|
+
/** Adds the given Social DNA code to the perspective's SDNA code */
|
|
280
|
+
async addSdna(name, sdnaCode, sdnaType) {
|
|
281
|
+
return __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").addSdna(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, name, sdnaCode, sdnaType);
|
|
282
|
+
}
|
|
283
|
+
/** Returns all the Subject classes defined in this perspectives SDNA */
|
|
284
|
+
async subjectClasses() {
|
|
285
|
+
try {
|
|
286
|
+
return (await this.infer("subject_class(X, _)")).map(x => x.X);
|
|
287
|
+
}
|
|
288
|
+
catch (e) {
|
|
289
|
+
return [];
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
async stringOrTemplateObjectToSubjectClass(subjectClass) {
|
|
293
|
+
if (typeof subjectClass === "string")
|
|
294
|
+
return subjectClass;
|
|
295
|
+
else {
|
|
296
|
+
let subjectClasses = await this.subjectClassesByTemplate(subjectClass);
|
|
297
|
+
if (subjectClasses[0]) {
|
|
298
|
+
return subjectClasses[0];
|
|
299
|
+
}
|
|
300
|
+
else {
|
|
301
|
+
//@ts-ignore
|
|
302
|
+
return subjectClass.className;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
/** Creates a new subject instance by running its (SDNA defined) constructor,
|
|
307
|
+
* which means adding links around the given expression address so that it
|
|
308
|
+
* conforms to the given subject class.
|
|
309
|
+
*
|
|
310
|
+
* @param subjectClass Either a string with the name of the subject class, or an object
|
|
311
|
+
* with the properties of the subject class. In the latter case, the first subject class
|
|
312
|
+
* that matches the given properties will be used.
|
|
313
|
+
* @param exprAddr The address of the expression to be turned into a subject instance
|
|
314
|
+
*/
|
|
315
|
+
async createSubject(subjectClass, exprAddr) {
|
|
316
|
+
let className = await this.stringOrTemplateObjectToSubjectClass(subjectClass);
|
|
317
|
+
let result = await this.infer(`subject_class("${className}", C), constructor(C, Actions)`);
|
|
318
|
+
if (!result.length) {
|
|
319
|
+
throw "No constructor found for given subject class: " + className;
|
|
320
|
+
}
|
|
321
|
+
let actions = result.map(x => eval(x.Actions));
|
|
322
|
+
await this.executeAction(actions[0], exprAddr, undefined);
|
|
323
|
+
return this.getSubjectProxy(exprAddr, subjectClass);
|
|
324
|
+
}
|
|
325
|
+
/** Removes a subject instance by running its (SDNA defined) destructor,
|
|
326
|
+
* which means removing links around the given expression address
|
|
327
|
+
*
|
|
328
|
+
* @param subjectClass Either a string with the name of the subject class, or an object
|
|
329
|
+
* with the properties of the subject class. In the latter case, the first subject class
|
|
330
|
+
* that matches the given properties will be used.
|
|
331
|
+
* @param exprAddr The address of the expression to be turned into a subject instance
|
|
332
|
+
*/
|
|
333
|
+
async removeSubject(subjectClass, exprAddr) {
|
|
334
|
+
let className = await this.stringOrTemplateObjectToSubjectClass(subjectClass);
|
|
335
|
+
let result = await this.infer(`subject_class("${className}", C), destructor(C, Actions)`);
|
|
336
|
+
if (!result.length) {
|
|
337
|
+
throw "No constructor found for given subject class: " + className;
|
|
338
|
+
}
|
|
339
|
+
let actions = result.map(x => eval(x.Actions));
|
|
340
|
+
await this.executeAction(actions[0], exprAddr, undefined);
|
|
341
|
+
}
|
|
342
|
+
/** Checks if the given expression is a subject instance of the given subject class
|
|
343
|
+
* @param expression The expression to be checked
|
|
344
|
+
* @param subjectClass Either a string with the name of the subject class, or an object
|
|
345
|
+
* with the properties of the subject class. In the latter case, the first subject class
|
|
346
|
+
* that matches the given properties will be used.
|
|
347
|
+
*/
|
|
348
|
+
async isSubjectInstance(expression, subjectClass) {
|
|
349
|
+
let className = await this.stringOrTemplateObjectToSubjectClass(subjectClass);
|
|
350
|
+
return await this.infer(`subject_class("${className}", C), instance(C, "${expression}")`);
|
|
351
|
+
}
|
|
352
|
+
/** For an existing subject instance (existing in the perspective's links)
|
|
353
|
+
* this function returns a proxy object that can be used to access the subject's
|
|
354
|
+
* properties and methods.
|
|
355
|
+
*
|
|
356
|
+
* @param base URI of the subject's root expression
|
|
357
|
+
* @param subjectClass Either a string with the name of the subject class, or an object
|
|
358
|
+
* with the properties of the subject class. In the latter case, the first subject class
|
|
359
|
+
* that matches the given properties will be used.
|
|
360
|
+
*/
|
|
361
|
+
async getSubjectProxy(base, subjectClass) {
|
|
362
|
+
if (!await this.isSubjectInstance(base, subjectClass)) {
|
|
363
|
+
throw `Expression ${base} is not a subject instance of given class: ${JSON.stringify(subjectClass)}`;
|
|
364
|
+
}
|
|
365
|
+
let className = await this.stringOrTemplateObjectToSubjectClass(subjectClass);
|
|
366
|
+
let subject = new Subject(this, base, className);
|
|
367
|
+
await subject.init();
|
|
368
|
+
return subject;
|
|
369
|
+
}
|
|
370
|
+
/** Returns all subject instances of the given subject class as proxy objects.
|
|
371
|
+
* @param subjectClass Either a string with the name of the subject class, or an object
|
|
372
|
+
* with the properties of the subject class. In the latter case, all subject classes
|
|
373
|
+
* that match the given properties will be used.
|
|
374
|
+
*/
|
|
375
|
+
async getAllSubjectInstances(subjectClass) {
|
|
376
|
+
let classes = [];
|
|
377
|
+
if (typeof subjectClass === "string") {
|
|
378
|
+
classes = [subjectClass];
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
classes = await this.subjectClassesByTemplate(subjectClass);
|
|
382
|
+
}
|
|
383
|
+
let instances = [];
|
|
384
|
+
for (let className of classes) {
|
|
385
|
+
let instanceBaseExpressions = await this.infer(`subject_class("${className}", C), instance(C, X)`);
|
|
386
|
+
let newInstances = await Promise.all(instanceBaseExpressions.map(async (x) => await this.getSubjectProxy(x.X, className)));
|
|
387
|
+
instances = instances.concat(newInstances);
|
|
388
|
+
}
|
|
389
|
+
return instances;
|
|
390
|
+
}
|
|
391
|
+
async getAllSubjectProxies(subjectClass) {
|
|
392
|
+
let classes = [];
|
|
393
|
+
if (typeof subjectClass === "string") {
|
|
394
|
+
classes = [subjectClass];
|
|
395
|
+
}
|
|
396
|
+
else {
|
|
397
|
+
classes = await this.subjectClassesByTemplate(subjectClass);
|
|
398
|
+
}
|
|
399
|
+
let instances = [];
|
|
400
|
+
for (let className of classes) {
|
|
401
|
+
instances = await this.infer(`subject_class("${className}", C), instance(C, X)`);
|
|
402
|
+
}
|
|
403
|
+
return instances;
|
|
404
|
+
}
|
|
405
|
+
/** Returns all subject classes that match the given template object.
|
|
406
|
+
* This function looks at the properties of the template object and
|
|
407
|
+
* its setters and collections to create a Prolog query that finds
|
|
408
|
+
* all subject classes that would be converted to a proxy object
|
|
409
|
+
* with exactly the same properties and collections.
|
|
410
|
+
*
|
|
411
|
+
* Since there could be multiple subject classes that match the given
|
|
412
|
+
* criteria, this function returns a list of class names.
|
|
413
|
+
*
|
|
414
|
+
* @param obj The template object
|
|
415
|
+
*/
|
|
416
|
+
async subjectClassesByTemplate(obj) {
|
|
417
|
+
// Collect all string properties of the object in a list
|
|
418
|
+
let properties = [];
|
|
419
|
+
// Collect all collections of the object in a list
|
|
420
|
+
let collections = [];
|
|
421
|
+
// Collect all string properties of the object in a list
|
|
422
|
+
if (Object.getPrototypeOf(obj).__properties) {
|
|
423
|
+
Object.keys(Object.getPrototypeOf(obj).__properties).forEach(p => properties.push(p));
|
|
424
|
+
}
|
|
425
|
+
else {
|
|
426
|
+
properties.push(...Object.keys(obj).filter(key => !Array.isArray(obj[key])));
|
|
427
|
+
}
|
|
428
|
+
// Collect all collections of the object in a list
|
|
429
|
+
if (Object.getPrototypeOf(obj).__collections) {
|
|
430
|
+
Object.keys(Object.getPrototypeOf(obj).__collections).filter(key => key !== 'isSubjectInstance').forEach(c => !collections.includes(c) ?? collections.push(c));
|
|
431
|
+
}
|
|
432
|
+
else {
|
|
433
|
+
collections.push(...Object.keys(obj).filter(key => Array.isArray(obj[key])).filter(key => key !== 'isSubjectInstance'));
|
|
434
|
+
}
|
|
435
|
+
// Collect all set functions of the object in a list
|
|
436
|
+
let setFunctions = Object.getOwnPropertyNames(obj).filter(key => (typeof obj[key] === "function") && key.startsWith("set") && !key.startsWith("setCollection"));
|
|
437
|
+
// Add all set functions of the object's prototype to that list
|
|
438
|
+
setFunctions = setFunctions.concat(Object.getOwnPropertyNames(Object.getPrototypeOf(obj)).filter(key => (typeof obj[key] === "function") && key.startsWith("set") && !key.startsWith("setCollection")));
|
|
439
|
+
// Collect all add functions of the object in a list
|
|
440
|
+
let addFunctions = Object.getOwnPropertyNames(obj).filter(key => (typeof obj[key] === "function") && key.startsWith("add"));
|
|
441
|
+
// Add all add functions of the object's prototype to that list
|
|
442
|
+
addFunctions = addFunctions.concat(Object.getOwnPropertyNames(Object.getPrototypeOf(obj)).filter(key => (typeof obj[key] === "function") && key.startsWith("add")));
|
|
443
|
+
// Collect all remove functions of the object in a list
|
|
444
|
+
let removeFunctions = Object.getOwnPropertyNames(obj).filter(key => (typeof obj[key] === "function") && key.startsWith("remove"));
|
|
445
|
+
// Add all remove functions of the object's prototype to that list
|
|
446
|
+
removeFunctions = removeFunctions.concat(Object.getOwnPropertyNames(Object.getPrototypeOf(obj)).filter(key => (typeof obj[key] === "function") && key.startsWith("remove")));
|
|
447
|
+
// Collect all add functions of the object in a list
|
|
448
|
+
let setCollectionFunctions = Object.getOwnPropertyNames(obj).filter(key => (typeof obj[key] === "function") && key.startsWith("setCollection"));
|
|
449
|
+
// Add all add functions of the object's prototype to that list
|
|
450
|
+
setCollectionFunctions = setCollectionFunctions.concat(Object.getOwnPropertyNames(Object.getPrototypeOf(obj)).filter(key => (typeof obj[key] === "function") && key.startsWith("setCollection")));
|
|
451
|
+
// Construct query to find all subject classes that have the given properties and collections
|
|
452
|
+
let query = `subject_class(Class, C)`;
|
|
453
|
+
for (let property of properties) {
|
|
454
|
+
query += `, property(C, "${property}")`;
|
|
455
|
+
}
|
|
456
|
+
for (let collection of collections) {
|
|
457
|
+
query += `, collection(C, "${collection}")`;
|
|
458
|
+
}
|
|
459
|
+
for (let setFunction of setFunctions) {
|
|
460
|
+
// e.g. "setState" -> "state"
|
|
461
|
+
let property = setFunction.substring(3);
|
|
462
|
+
property = property.charAt(0).toLowerCase() + property.slice(1);
|
|
463
|
+
query += `, property_setter(C, "${property}", _)`;
|
|
464
|
+
}
|
|
465
|
+
for (let addFunction of addFunctions) {
|
|
466
|
+
query += `, collection_adder(C, "${collectionAdderToName(addFunction)}", _)`;
|
|
467
|
+
}
|
|
468
|
+
for (let removeFunction of removeFunctions) {
|
|
469
|
+
query += `, collection_remover(C, "${collectionRemoverToName(removeFunction)}", _)`;
|
|
470
|
+
}
|
|
471
|
+
for (let setCollectionFunction of setCollectionFunctions) {
|
|
472
|
+
query += `, collection_setter(C, "${collectionSetterToName(setCollectionFunction)}", _)`;
|
|
473
|
+
}
|
|
474
|
+
query += ".";
|
|
475
|
+
let result = await this.infer(query);
|
|
476
|
+
if (!result) {
|
|
477
|
+
return [];
|
|
478
|
+
}
|
|
479
|
+
else {
|
|
480
|
+
return result.map(x => x.Class);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
/** Takes a JS class (its constructor) and assumes that it was decorated by
|
|
484
|
+
* the @subjectClass etc. decorators. It then tests if there is a subject class
|
|
485
|
+
* already present in the perspective's SDNA that matches the given class.
|
|
486
|
+
* If there is no such class, it gets the JS class's SDNA by calling its
|
|
487
|
+
* static generateSDNA() function and adds it to the perspective's SDNA.
|
|
488
|
+
*/
|
|
489
|
+
async ensureSDNASubjectClass(jsClass) {
|
|
490
|
+
const subjectClass = await this.subjectClassesByTemplate(new jsClass);
|
|
491
|
+
if (subjectClass.length > 0) {
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
const { name, sdna } = jsClass.generateSDNA();
|
|
495
|
+
await this.addSdna(name, sdna, 'subject_class');
|
|
496
|
+
}
|
|
497
|
+
getNeighbourhoodProxy() {
|
|
498
|
+
return __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").getNeighbourhoodProxy(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
_PerspectiveProxy_handle = new WeakMap(), _PerspectiveProxy_client = new WeakMap(), _PerspectiveProxy_perspectiveLinkAddedCallbacks = new WeakMap(), _PerspectiveProxy_perspectiveLinkRemovedCallbacks = new WeakMap(), _PerspectiveProxy_perspectiveLinkUpdatedCallbacks = new WeakMap(), _PerspectiveProxy_perspectiveSyncStateChangeCallbacks = new WeakMap();
|
|
@@ -27,6 +27,7 @@ export default class PerspectiveResolver {
|
|
|
27
27
|
perspectiveAddLinkExpression(uuid: string, link: LinkExpressionInput, status: LinkStatus, pubSub: any): LinkExpression;
|
|
28
28
|
perspectiveUpdateLink(uuid: string, oldlink: LinkExpressionInput, newlink: LinkInput, pubSub: any): LinkExpression;
|
|
29
29
|
perspectiveRemoveLink(uuid: string, link: LinkExpressionInput, pubSub: any): Boolean;
|
|
30
|
+
perspectiveAddSdna(uuid: string, name: string, sdnaCode: string, sdnaType: string, pubSub: any): Boolean;
|
|
30
31
|
perspectiveAdded(): PerspectiveHandle;
|
|
31
32
|
perspectiveUpdated(): PerspectiveHandle;
|
|
32
33
|
perspectiveRemoved(): string;
|