@coasys/ad4m 0.10.0 → 0.10.1-dev-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/lib/index.cjs +3785 -843
- package/lib/index.js +3777 -839
- package/lib/src/Ad4mClient.js +2 -1
- package/lib/src/Literal.js +4 -1
- package/lib/src/ai/AIClient.d.ts +8 -2
- package/lib/src/ai/AIClient.js +25 -14
- package/lib/src/ai/AIResolver.d.ts +25 -6
- package/lib/src/ai/AIResolver.js +102 -18
- package/lib/src/index.d.ts +4 -3
- package/lib/src/index.js +4 -3
- package/lib/src/model/Ad4mModel.d.ts +730 -0
- package/lib/src/model/Ad4mModel.js +1399 -0
- package/lib/src/{subject → model}/Subject.d.ts +2 -2
- package/lib/src/{subject → model}/Subject.js +18 -18
- package/lib/src/model/decorators.d.ts +502 -0
- package/lib/src/model/decorators.js +683 -0
- package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +2 -2
- package/lib/src/neighbourhood/NeighbourhoodClient.js +13 -10
- package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +2 -2
- package/lib/src/neighbourhood/NeighbourhoodProxy.js +4 -4
- package/lib/src/neighbourhood/NeighbourhoodProxy.test.d.ts +1 -0
- package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +73 -0
- package/lib/src/neighbourhood/NeighbourhoodResolver.d.ts +2 -2
- package/lib/src/neighbourhood/NeighbourhoodResolver.js +6 -4
- package/lib/src/perspectives/PerspectiveClient.d.ts +23 -11
- package/lib/src/perspectives/PerspectiveClient.js +147 -51
- package/lib/src/perspectives/PerspectiveProxy.d.ts +494 -54
- package/lib/src/perspectives/PerspectiveProxy.js +684 -67
- package/lib/src/perspectives/PerspectiveResolver.d.ts +24 -9
- package/lib/src/perspectives/PerspectiveResolver.js +151 -46
- package/lib/src/runtime/RuntimeClient.d.ts +8 -2
- package/lib/src/runtime/RuntimeClient.js +64 -0
- package/lib/src/runtime/RuntimeResolver.d.ts +27 -1
- package/lib/src/runtime/RuntimeResolver.js +138 -0
- package/lib/src/schema.gql +55 -16
- package/lib/src/typeDefs.js +55 -16
- package/package.json +4 -3
- package/src/Ad4mClient.test.ts +51 -19
- package/src/Ad4mClient.ts +3 -1
- package/src/Literal.ts +4 -1
- package/src/ai/AIClient.ts +35 -14
- package/src/ai/AIResolver.ts +78 -16
- package/src/index.ts +4 -3
- package/src/model/Ad4mModel.ts +1651 -0
- package/src/{subject → model}/Subject.ts +16 -16
- package/src/{subject/SDNADecorators.ts → model/decorators.ts} +384 -50
- package/src/neighbourhood/NeighbourhoodClient.ts +13 -10
- package/src/neighbourhood/NeighbourhoodProxy.test.ts +100 -0
- package/src/neighbourhood/NeighbourhoodProxy.ts +4 -4
- package/src/neighbourhood/NeighbourhoodResolver.ts +2 -2
- package/src/perspectives/PerspectiveClient.ts +156 -51
- package/src/perspectives/PerspectiveProxy.ts +766 -82
- package/src/perspectives/PerspectiveResolver.ts +134 -23
- package/src/runtime/RuntimeClient.ts +71 -2
- package/src/runtime/RuntimeResolver.ts +91 -1
- package/lib/src/subject/SDNADecorators.d.ts +0 -188
- package/lib/src/subject/SDNADecorators.js +0 -360
- package/lib/src/subject/SubjectEntity.d.ts +0 -102
- package/lib/src/subject/SubjectEntity.js +0 -267
- package/src/subject/SubjectEntity.ts +0 -315
- package/yarn-error.log +0 -90
- /package/lib/src/{subject → model}/util.d.ts +0 -0
- /package/lib/src/{subject → model}/util.js +0 -0
- /package/src/{subject → model}/util.ts +0 -0
|
@@ -0,0 +1,683 @@
|
|
|
1
|
+
import { Subject } from "./Subject";
|
|
2
|
+
import { capitalize, propertyNameToSetterName, singularToPlural, stringifyObjectLiteral } from "./util";
|
|
3
|
+
export class PerspectiveAction {
|
|
4
|
+
}
|
|
5
|
+
export function addLink(source, predicate, target) {
|
|
6
|
+
return {
|
|
7
|
+
action: "addLink",
|
|
8
|
+
source,
|
|
9
|
+
predicate,
|
|
10
|
+
target,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export function hasLink(predicate) {
|
|
14
|
+
return `triple(this, "${predicate}", _)`;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Decorator for querying instances of a model class.
|
|
18
|
+
*
|
|
19
|
+
* @category Decorators
|
|
20
|
+
*
|
|
21
|
+
* @description
|
|
22
|
+
* Allows you to define static query methods on your model class to retrieve instances based on custom conditions.
|
|
23
|
+
* This decorator can only be applied to static async methods that return a Promise of an array of model instances.
|
|
24
|
+
*
|
|
25
|
+
* The query can be constrained using either:
|
|
26
|
+
* - A `where` clause that matches property values
|
|
27
|
+
* - A custom Prolog `condition` for more complex queries
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* class Recipe extends Ad4mModel {
|
|
32
|
+
* @Property({ through: "recipe://name" })
|
|
33
|
+
* name: string = "";
|
|
34
|
+
*
|
|
35
|
+
* @Property({ through: "recipe://rating" })
|
|
36
|
+
* rating: number = 0;
|
|
37
|
+
*
|
|
38
|
+
* // Get all recipes
|
|
39
|
+
* @InstanceQuery()
|
|
40
|
+
* static async all(perspective: PerspectiveProxy): Promise<Recipe[]> { return [] }
|
|
41
|
+
*
|
|
42
|
+
* // Get recipes by name
|
|
43
|
+
* @InstanceQuery({ where: { name: "Chocolate Cake" }})
|
|
44
|
+
* static async findByName(perspective: PerspectiveProxy): Promise<Recipe[]> { return [] }
|
|
45
|
+
*
|
|
46
|
+
* // Get highly rated recipes using a custom condition
|
|
47
|
+
* @InstanceQuery({ condition: "triple(Instance, 'recipe://rating', Rating), Rating > 4" })
|
|
48
|
+
* static async topRated(perspective: PerspectiveProxy): Promise<Recipe[]> { return [] }
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @param {Object} [options] - Query options
|
|
53
|
+
* @param {object} [options.where] - Object with property-value pairs to match
|
|
54
|
+
* @param {string} [options.condition] - Custom Prolog condition for more complex queries
|
|
55
|
+
*/
|
|
56
|
+
export function InstanceQuery(options) {
|
|
57
|
+
return function (target, key, descriptor) {
|
|
58
|
+
const originalMethod = descriptor.value;
|
|
59
|
+
if (typeof originalMethod !== "function") {
|
|
60
|
+
throw new Error("InstanceQuery decorator can only be applied to methods");
|
|
61
|
+
}
|
|
62
|
+
descriptor.value = async function (perspective) {
|
|
63
|
+
let instances = [];
|
|
64
|
+
//@ts-ignore
|
|
65
|
+
let subjectClassName = target.name;
|
|
66
|
+
let query = `subject_class("${subjectClassName}", C), instance(C, Instance)`;
|
|
67
|
+
if (options && options.where) {
|
|
68
|
+
for (let prop in options.where) {
|
|
69
|
+
let value = options.where[prop];
|
|
70
|
+
query += `, property_getter(C, Instance, "${prop}", "${value}")`;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (options && options.condition) {
|
|
74
|
+
query += ', ' + options.condition;
|
|
75
|
+
}
|
|
76
|
+
let results = await perspective.infer(query);
|
|
77
|
+
if (results == false) {
|
|
78
|
+
return instances;
|
|
79
|
+
}
|
|
80
|
+
if (typeof results == "string") {
|
|
81
|
+
throw results;
|
|
82
|
+
}
|
|
83
|
+
for (let result of results) {
|
|
84
|
+
let instance = result.Instance;
|
|
85
|
+
let subject = new Subject(perspective, instance, subjectClassName);
|
|
86
|
+
await subject.init();
|
|
87
|
+
instances.push(subject);
|
|
88
|
+
}
|
|
89
|
+
return instances;
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Decorator for defining optional properties on model classes.
|
|
95
|
+
*
|
|
96
|
+
* @category Decorators
|
|
97
|
+
*
|
|
98
|
+
* @description
|
|
99
|
+
* The most flexible property decorator that allows you to define properties with full control over:
|
|
100
|
+
* - Whether the property is required
|
|
101
|
+
* - Whether the property is writable
|
|
102
|
+
* - How values are stored and retrieved
|
|
103
|
+
* - Custom getter/setter logic
|
|
104
|
+
* - Local vs network storage
|
|
105
|
+
*
|
|
106
|
+
* Both @Property and @ReadOnly are specialized versions of @Optional with preset configurations.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```typescript
|
|
110
|
+
* class Recipe extends Ad4mModel {
|
|
111
|
+
* // Basic optional property
|
|
112
|
+
* @Optional({
|
|
113
|
+
* through: "recipe://description"
|
|
114
|
+
* })
|
|
115
|
+
* description?: string;
|
|
116
|
+
*
|
|
117
|
+
* // Optional property with custom initial value
|
|
118
|
+
* @Optional({
|
|
119
|
+
* through: "recipe://status",
|
|
120
|
+
* initial: "recipe://draft",
|
|
121
|
+
* required: true
|
|
122
|
+
* })
|
|
123
|
+
* status: string = "";
|
|
124
|
+
*
|
|
125
|
+
* // Read-only property with custom getter
|
|
126
|
+
* @Optional({
|
|
127
|
+
* through: "recipe://rating",
|
|
128
|
+
* writable: false,
|
|
129
|
+
* getter: `
|
|
130
|
+
* findall(Rating, triple(Base, "recipe://user_rating", Rating), Ratings),
|
|
131
|
+
* sum_list(Ratings, Sum),
|
|
132
|
+
* length(Ratings, Count),
|
|
133
|
+
* Value is Sum / Count
|
|
134
|
+
* `
|
|
135
|
+
* })
|
|
136
|
+
* averageRating: number = 0;
|
|
137
|
+
*
|
|
138
|
+
* // Property that resolves to a Literal and is stored locally
|
|
139
|
+
* @Optional({
|
|
140
|
+
* through: "recipe://notes",
|
|
141
|
+
* resolveLanguage: "literal",
|
|
142
|
+
* local: true
|
|
143
|
+
* })
|
|
144
|
+
* notes?: string;
|
|
145
|
+
*
|
|
146
|
+
* // Property with custom getter and setter logic
|
|
147
|
+
* @Optional({
|
|
148
|
+
* through: "recipe://ingredients",
|
|
149
|
+
* getter: `
|
|
150
|
+
* triple(Base, "recipe://ingredients", RawValue),
|
|
151
|
+
* atom_json_term(RawValue, Value)
|
|
152
|
+
* `,
|
|
153
|
+
* setter: `
|
|
154
|
+
* atom_json_term(Value, JsonValue),
|
|
155
|
+
* Actions = [{"action": "setSingleTarget", "source": "this", "predicate": "recipe://ingredients", "target": JsonValue}]
|
|
156
|
+
* `
|
|
157
|
+
* })
|
|
158
|
+
* ingredients: string[] = [];
|
|
159
|
+
* }
|
|
160
|
+
* ```
|
|
161
|
+
*
|
|
162
|
+
* @param {PropertyOptions} opts - Property configuration options
|
|
163
|
+
* @param {string} opts.through - The predicate URI for the property
|
|
164
|
+
* @param {string} [opts.initial] - Initial value (required if property is required)
|
|
165
|
+
* @param {boolean} [opts.required] - Whether the property must have a value
|
|
166
|
+
* @param {boolean} [opts.writable=true] - Whether the property can be modified
|
|
167
|
+
* @param {string} [opts.resolveLanguage] - Language to use for value resolution (e.g. "literal")
|
|
168
|
+
* @param {string} [opts.getter] - Custom Prolog code for getting the property value
|
|
169
|
+
* @param {string} [opts.setter] - Custom Prolog code for setting the property value
|
|
170
|
+
* @param {boolean} [opts.local] - Whether the property should only be stored locally
|
|
171
|
+
*/
|
|
172
|
+
export function Optional(opts) {
|
|
173
|
+
return function (target, key) {
|
|
174
|
+
if (typeof opts.writable === "undefined" && opts.through) {
|
|
175
|
+
opts.writable = true;
|
|
176
|
+
}
|
|
177
|
+
if (opts.required && !opts.initial) {
|
|
178
|
+
throw new Error("SubjectProperty requires an 'initial' option if 'required' is true");
|
|
179
|
+
}
|
|
180
|
+
if (!opts.through && !opts.getter) {
|
|
181
|
+
throw new Error("SubjectProperty requires either 'through' or 'getter' option");
|
|
182
|
+
}
|
|
183
|
+
target["__properties"] = target["__properties"] || {};
|
|
184
|
+
target["__properties"][key] = target["__properties"][key] || {};
|
|
185
|
+
target["__properties"][key] = { ...target["__properties"][key], ...opts };
|
|
186
|
+
if (opts.writable) {
|
|
187
|
+
const value = key;
|
|
188
|
+
target[`set${capitalize(value)}`] = () => { };
|
|
189
|
+
}
|
|
190
|
+
Object.defineProperty(target, key, { configurable: true, writable: true });
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Decorator for defining flags on model classes.
|
|
195
|
+
*
|
|
196
|
+
* @category Decorators
|
|
197
|
+
*
|
|
198
|
+
* @description
|
|
199
|
+
* A specialized property decorator for defining immutable type flags or markers on model instances.
|
|
200
|
+
* Flags are always required properties with a fixed value that cannot be changed after creation.
|
|
201
|
+
*
|
|
202
|
+
* Common uses for flags:
|
|
203
|
+
* - Type discrimination between different kinds of models
|
|
204
|
+
* - Marking models with specific capabilities or features
|
|
205
|
+
* - Versioning or compatibility markers
|
|
206
|
+
*
|
|
207
|
+
* Note: Use of Flag is discouraged unless you specifically need type-based filtering or
|
|
208
|
+
* discrimination between different kinds of models. For most cases, regular properties
|
|
209
|
+
* with @Property or @Optional are more appropriate.
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```typescript
|
|
213
|
+
* class Message extends Ad4mModel {
|
|
214
|
+
* // Type flag to identify message models
|
|
215
|
+
* @Flag({
|
|
216
|
+
* through: "ad4m://type",
|
|
217
|
+
* value: "ad4m://message"
|
|
218
|
+
* })
|
|
219
|
+
* type: string = "";
|
|
220
|
+
*
|
|
221
|
+
* // Version flag for compatibility
|
|
222
|
+
* @Flag({
|
|
223
|
+
* through: "ad4m://version",
|
|
224
|
+
* value: "1.0.0"
|
|
225
|
+
* })
|
|
226
|
+
* version: string = "";
|
|
227
|
+
*
|
|
228
|
+
* // Feature flag
|
|
229
|
+
* @Flag({
|
|
230
|
+
* through: "message://feature",
|
|
231
|
+
* value: "message://encrypted"
|
|
232
|
+
* })
|
|
233
|
+
* feature: string = "";
|
|
234
|
+
* }
|
|
235
|
+
*
|
|
236
|
+
* // Later you can query for specific types:
|
|
237
|
+
* const messages = await Message.query(perspective)
|
|
238
|
+
* .where({ type: "ad4m://message" })
|
|
239
|
+
* .run();
|
|
240
|
+
* ```
|
|
241
|
+
*
|
|
242
|
+
* @param {FlagOptions} opts - Flag configuration
|
|
243
|
+
* @param {string} opts.through - The predicate URI for the flag
|
|
244
|
+
* @param {string} opts.value - The fixed value for the flag
|
|
245
|
+
*/
|
|
246
|
+
export function Flag(opts) {
|
|
247
|
+
return function (target, key) {
|
|
248
|
+
if (!opts.through && !opts.value) {
|
|
249
|
+
throw new Error("SubjectFlag requires a 'through' and 'value' option");
|
|
250
|
+
}
|
|
251
|
+
if (!opts.through) {
|
|
252
|
+
throw new Error("SubjectFlag requires a 'through' option");
|
|
253
|
+
}
|
|
254
|
+
if (!opts.value) {
|
|
255
|
+
throw new Error("SubjectFlag requires a 'value' option");
|
|
256
|
+
}
|
|
257
|
+
target["__properties"] = target["__properties"] || {};
|
|
258
|
+
target["__properties"][key] = target["__properties"][key] || {};
|
|
259
|
+
target["__properties"][key] = {
|
|
260
|
+
...target["__properties"][key],
|
|
261
|
+
through: opts.through,
|
|
262
|
+
required: true,
|
|
263
|
+
initial: opts.value,
|
|
264
|
+
flag: true
|
|
265
|
+
};
|
|
266
|
+
// @ts-ignore
|
|
267
|
+
target[key] = opts.value;
|
|
268
|
+
Object.defineProperty(target, key, { configurable: true, writable: true });
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Decorator for defining collections on model classes.
|
|
273
|
+
*
|
|
274
|
+
* @category Decorators
|
|
275
|
+
*
|
|
276
|
+
* @description
|
|
277
|
+
* Defines a property that represents a collection of values linked to the model instance.
|
|
278
|
+
* Collections are always arrays and support operations for adding, removing, and setting values.
|
|
279
|
+
*
|
|
280
|
+
* For each collection property, the following methods are automatically generated:
|
|
281
|
+
* - `addX(value)` - Add a value to the collection
|
|
282
|
+
* - `removeX(value)` - Remove a value from the collection
|
|
283
|
+
* - `setCollectionX(values)` - Replace all values in the collection
|
|
284
|
+
*
|
|
285
|
+
* Where X is the capitalized property name.
|
|
286
|
+
*
|
|
287
|
+
* Collections can be filtered using the `where` option to only include values that:
|
|
288
|
+
* - Are instances of a specific model class
|
|
289
|
+
* - Match a custom Prolog condition
|
|
290
|
+
*
|
|
291
|
+
* @example
|
|
292
|
+
* ```typescript
|
|
293
|
+
* class Recipe extends Ad4mModel {
|
|
294
|
+
* // Basic collection of ingredients
|
|
295
|
+
* @Collection({
|
|
296
|
+
* through: "recipe://ingredient"
|
|
297
|
+
* })
|
|
298
|
+
* ingredients: string[] = [];
|
|
299
|
+
*
|
|
300
|
+
* // Collection that only includes instances of another model
|
|
301
|
+
* @Collection({
|
|
302
|
+
* through: "recipe://comment",
|
|
303
|
+
* where: { isInstance: Comment }
|
|
304
|
+
* })
|
|
305
|
+
* comments: string[] = [];
|
|
306
|
+
*
|
|
307
|
+
* // Collection with custom filter condition
|
|
308
|
+
* @Collection({
|
|
309
|
+
* through: "recipe://step",
|
|
310
|
+
* where: { condition: `triple(Target, "step://order", Order), Order < 3` }
|
|
311
|
+
* })
|
|
312
|
+
* firstSteps: string[] = [];
|
|
313
|
+
*
|
|
314
|
+
* // Local-only collection not shared with network
|
|
315
|
+
* @Collection({
|
|
316
|
+
* through: "recipe://note",
|
|
317
|
+
* local: true
|
|
318
|
+
* })
|
|
319
|
+
* privateNotes: string[] = [];
|
|
320
|
+
* }
|
|
321
|
+
*
|
|
322
|
+
* // Using the generated methods:
|
|
323
|
+
* const recipe = new Recipe(perspective);
|
|
324
|
+
* await recipe.addIngredients("ingredient://flour");
|
|
325
|
+
* await recipe.removeIngredients("ingredient://sugar");
|
|
326
|
+
* await recipe.setCollectionIngredients(["ingredient://butter", "ingredient://eggs"]);
|
|
327
|
+
* ```
|
|
328
|
+
*
|
|
329
|
+
* @param {CollectionOptions} opts - Collection configuration
|
|
330
|
+
* @param {string} opts.through - The predicate URI for collection links
|
|
331
|
+
* @param {WhereOptions} [opts.where] - Filter conditions for collection values
|
|
332
|
+
* @param {any} [opts.where.isInstance] - Model class to filter instances by
|
|
333
|
+
* @param {string} [opts.where.condition] - Custom Prolog condition for filtering
|
|
334
|
+
* @param {boolean} [opts.local] - Whether collection links are stored locally only
|
|
335
|
+
*/
|
|
336
|
+
export function Collection(opts) {
|
|
337
|
+
return function (target, key) {
|
|
338
|
+
target["__collections"] = target["__collections"] || {};
|
|
339
|
+
target["__collections"][key] = opts;
|
|
340
|
+
const value = key;
|
|
341
|
+
target[`add${capitalize(value)}`] = () => { };
|
|
342
|
+
target[`remove${capitalize(value)}`] = () => { };
|
|
343
|
+
target[`setCollection${capitalize(value)}`] = () => { };
|
|
344
|
+
Object.defineProperty(target, key, { configurable: true, writable: true });
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
export function makeRandomPrologAtom(length) {
|
|
348
|
+
let result = '';
|
|
349
|
+
let characters = 'abcdefghijklmnopqrstuvwxyz';
|
|
350
|
+
let charactersLength = characters.length;
|
|
351
|
+
for (let i = 0; i < length; i++) {
|
|
352
|
+
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
353
|
+
}
|
|
354
|
+
return result;
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Decorator for defining model classes in AD4M.
|
|
358
|
+
*
|
|
359
|
+
* @category Decorators
|
|
360
|
+
*
|
|
361
|
+
* @description
|
|
362
|
+
* The root decorator that must be applied to any class that represents a model in AD4M.
|
|
363
|
+
* It registers the class as a Social DNA (SDNA) subject class and provides the infrastructure
|
|
364
|
+
* for storing and retrieving instances.
|
|
365
|
+
*
|
|
366
|
+
* This decorator:
|
|
367
|
+
* - Registers the class with a unique name in the AD4M system
|
|
368
|
+
* - Generates the necessary SDNA code for the model's properties and collections
|
|
369
|
+
* - Enables the use of other model decorators (@Property, @Collection, etc.)
|
|
370
|
+
* - Provides static query methods through the Ad4mModel base class
|
|
371
|
+
*
|
|
372
|
+
* @example
|
|
373
|
+
* ```typescript
|
|
374
|
+
* @ModelOptions({ name: "Recipe" })
|
|
375
|
+
* class Recipe extends Ad4mModel {
|
|
376
|
+
* @Property({
|
|
377
|
+
* through: "recipe://name",
|
|
378
|
+
* resolveLanguage: "literal"
|
|
379
|
+
* })
|
|
380
|
+
* name: string = "";
|
|
381
|
+
*
|
|
382
|
+
* @Collection({ through: "recipe://ingredient" })
|
|
383
|
+
* ingredients: string[] = [];
|
|
384
|
+
*
|
|
385
|
+
* // Static query methods from Ad4mModel:
|
|
386
|
+
* static async findByName(perspective: PerspectiveProxy, name: string) {
|
|
387
|
+
* return Recipe.query(perspective)
|
|
388
|
+
* .where({ name })
|
|
389
|
+
* .run();
|
|
390
|
+
* }
|
|
391
|
+
* }
|
|
392
|
+
*
|
|
393
|
+
* // Using the model:
|
|
394
|
+
* const recipe = new Recipe(perspective);
|
|
395
|
+
* recipe.name = "Chocolate Cake";
|
|
396
|
+
* await recipe.save();
|
|
397
|
+
*
|
|
398
|
+
* // Querying instances:
|
|
399
|
+
* const recipes = await Recipe.query(perspective)
|
|
400
|
+
* .where({ name: "Chocolate Cake" })
|
|
401
|
+
* .run();
|
|
402
|
+
*
|
|
403
|
+
* // Using with PerspectiveProxy:
|
|
404
|
+
* await perspective.ensureSDNASubjectClass(Recipe);
|
|
405
|
+
* ```
|
|
406
|
+
*
|
|
407
|
+
* @param {ModelOptionsOptions} opts - Model configuration
|
|
408
|
+
* @param {string} opts.name - Unique name for the model class in AD4M
|
|
409
|
+
*/
|
|
410
|
+
export function ModelOptions(opts) {
|
|
411
|
+
return function (target) {
|
|
412
|
+
target.prototype.className = opts.name;
|
|
413
|
+
target.className = opts.name;
|
|
414
|
+
target.generateSDNA = function () {
|
|
415
|
+
let sdna = "";
|
|
416
|
+
let subjectName = opts.name;
|
|
417
|
+
let obj = target.prototype;
|
|
418
|
+
let uuid = makeRandomPrologAtom(8);
|
|
419
|
+
sdna += `subject_class("${subjectName}", ${uuid}).\n`;
|
|
420
|
+
let classRemoverActions = [];
|
|
421
|
+
let constructorActions = [];
|
|
422
|
+
if (obj.subjectConstructor && obj.subjectConstructor.length) {
|
|
423
|
+
constructorActions = constructorActions.concat(obj.subjectConstructor);
|
|
424
|
+
}
|
|
425
|
+
let instanceConditions = [];
|
|
426
|
+
if (obj.isSubjectInstance && obj.isSubjectInstance.length) {
|
|
427
|
+
instanceConditions = instanceConditions.concat(obj.isSubjectInstance);
|
|
428
|
+
}
|
|
429
|
+
let propertiesCode = [];
|
|
430
|
+
let properties = obj.__properties || {};
|
|
431
|
+
for (let property in properties) {
|
|
432
|
+
let propertyCode = `property(${uuid}, "${property}").\n`;
|
|
433
|
+
let { through, initial, required, resolveLanguage, writable, flag, getter, setter, local } = properties[property];
|
|
434
|
+
if (resolveLanguage) {
|
|
435
|
+
propertyCode += `property_resolve(${uuid}, "${property}").\n`;
|
|
436
|
+
propertyCode += `property_resolve_language(${uuid}, "${property}", "${resolveLanguage}").\n`;
|
|
437
|
+
}
|
|
438
|
+
if (getter) {
|
|
439
|
+
propertyCode += `property_getter(${uuid}, Base, "${property}", Value) :- ${getter}.\n`;
|
|
440
|
+
}
|
|
441
|
+
else if (through) {
|
|
442
|
+
propertyCode += `property_getter(${uuid}, Base, "${property}", Value) :- triple(Base, "${through}", Value).\n`;
|
|
443
|
+
if (required) {
|
|
444
|
+
if (flag) {
|
|
445
|
+
instanceConditions.push(`triple(Base, "${through}", "${initial}")`);
|
|
446
|
+
}
|
|
447
|
+
else {
|
|
448
|
+
instanceConditions.push(`triple(Base, "${through}", _)`);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
if (setter) {
|
|
453
|
+
propertyCode += `property_setter(${uuid}, "${property}", Actions) :- ${setter}.\n`;
|
|
454
|
+
}
|
|
455
|
+
else if (writable && through) {
|
|
456
|
+
let setter = obj[propertyNameToSetterName(property)];
|
|
457
|
+
if (typeof setter === "function") {
|
|
458
|
+
let action = [{
|
|
459
|
+
action: "setSingleTarget",
|
|
460
|
+
source: "this",
|
|
461
|
+
predicate: through,
|
|
462
|
+
target: "value",
|
|
463
|
+
...(local && { local: true })
|
|
464
|
+
}];
|
|
465
|
+
propertyCode += `property_setter(${uuid}, "${property}", '${stringifyObjectLiteral(action)}').\n`;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
propertiesCode.push(propertyCode);
|
|
469
|
+
if (initial) {
|
|
470
|
+
constructorActions.push({
|
|
471
|
+
action: "addLink",
|
|
472
|
+
source: "this",
|
|
473
|
+
predicate: through,
|
|
474
|
+
target: initial,
|
|
475
|
+
});
|
|
476
|
+
classRemoverActions.push({
|
|
477
|
+
action: "removeLink",
|
|
478
|
+
source: "this",
|
|
479
|
+
predicate: through,
|
|
480
|
+
target: "*",
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
let collectionsCode = [];
|
|
485
|
+
let collections = obj.__collections || {};
|
|
486
|
+
for (let collection in collections) {
|
|
487
|
+
let collectionCode = `collection(${uuid}, "${collection}").\n`;
|
|
488
|
+
let { through, where, local } = collections[collection];
|
|
489
|
+
if (through) {
|
|
490
|
+
if (where) {
|
|
491
|
+
if (!where.isInstance && !where.condition) {
|
|
492
|
+
throw "'where' needs one of 'isInstance' or 'condition'";
|
|
493
|
+
}
|
|
494
|
+
let conditions = [];
|
|
495
|
+
if (where.isInstance) {
|
|
496
|
+
let otherClass;
|
|
497
|
+
if (where.isInstance.name) {
|
|
498
|
+
otherClass = where.isInstance.name;
|
|
499
|
+
}
|
|
500
|
+
else {
|
|
501
|
+
otherClass = where.isInstance;
|
|
502
|
+
}
|
|
503
|
+
conditions.push(`instance(OtherClass, Target), subject_class("${otherClass}", OtherClass)`);
|
|
504
|
+
}
|
|
505
|
+
if (where.condition) {
|
|
506
|
+
conditions.push(where.condition);
|
|
507
|
+
}
|
|
508
|
+
const conditionString = conditions.join(", ");
|
|
509
|
+
collectionCode += `collection_getter(${uuid}, Base, "${collection}", List) :- setof(Target, (triple(Base, "${through}", Target), ${conditionString}), List).\n`;
|
|
510
|
+
}
|
|
511
|
+
else {
|
|
512
|
+
collectionCode += `collection_getter(${uuid}, Base, "${collection}", List) :- findall(C, triple(Base, "${through}", C), List).\n`;
|
|
513
|
+
}
|
|
514
|
+
let collectionAdderAction = [{
|
|
515
|
+
action: "addLink",
|
|
516
|
+
source: "this",
|
|
517
|
+
predicate: through,
|
|
518
|
+
target: "value",
|
|
519
|
+
...(local && { local: true })
|
|
520
|
+
}];
|
|
521
|
+
let collectionRemoverAction = [{
|
|
522
|
+
action: "removeLink",
|
|
523
|
+
source: "this",
|
|
524
|
+
predicate: through,
|
|
525
|
+
target: "value",
|
|
526
|
+
}];
|
|
527
|
+
let collectionSetterAction = [{
|
|
528
|
+
action: "collectionSetter",
|
|
529
|
+
source: "this",
|
|
530
|
+
predicate: through,
|
|
531
|
+
target: "value",
|
|
532
|
+
...(local && { local: true })
|
|
533
|
+
}];
|
|
534
|
+
collectionCode += `collection_adder(${uuid}, "${singularToPlural(collection)}", '${stringifyObjectLiteral(collectionAdderAction)}').\n`;
|
|
535
|
+
collectionCode += `collection_remover(${uuid}, "${singularToPlural(collection)}", '${stringifyObjectLiteral(collectionRemoverAction)}').\n`;
|
|
536
|
+
collectionCode += `collection_setter(${uuid}, "${singularToPlural(collection)}", '${stringifyObjectLiteral(collectionSetterAction)}').\n`;
|
|
537
|
+
}
|
|
538
|
+
collectionsCode.push(collectionCode);
|
|
539
|
+
}
|
|
540
|
+
let subjectContructorJSONString = stringifyObjectLiteral(constructorActions);
|
|
541
|
+
sdna += `constructor(${uuid}, '${subjectContructorJSONString}').\n`;
|
|
542
|
+
if (instanceConditions.length > 0) {
|
|
543
|
+
let instanceConditionProlog = instanceConditions.join(", ");
|
|
544
|
+
sdna += `instance(${uuid}, Base) :- ${instanceConditionProlog}.\n`;
|
|
545
|
+
sdna += "\n";
|
|
546
|
+
}
|
|
547
|
+
sdna += `destructor(${uuid}, '${stringifyObjectLiteral(classRemoverActions)}').\n`;
|
|
548
|
+
sdna += "\n";
|
|
549
|
+
sdna += propertiesCode.join("\n");
|
|
550
|
+
sdna += "\n";
|
|
551
|
+
sdna += collectionsCode.join("\n");
|
|
552
|
+
return {
|
|
553
|
+
sdna,
|
|
554
|
+
name: subjectName
|
|
555
|
+
};
|
|
556
|
+
};
|
|
557
|
+
Object.defineProperty(target, 'type', { configurable: true });
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Decorator for defining required and writable properties on model classes.
|
|
562
|
+
*
|
|
563
|
+
* @category Decorators
|
|
564
|
+
*
|
|
565
|
+
* @description
|
|
566
|
+
* A convenience decorator that defines a required property that must have an initial value and is writable by default.
|
|
567
|
+
* This is equivalent to using @Optional with `required: true` and `writable: true`.
|
|
568
|
+
*
|
|
569
|
+
* Properties defined with this decorator:
|
|
570
|
+
* - Must have a value (required)
|
|
571
|
+
* - Can be modified after creation (writable)
|
|
572
|
+
* - Default to "literal://string:uninitialized" if no initial value is provided
|
|
573
|
+
*
|
|
574
|
+
* @example
|
|
575
|
+
* ```typescript
|
|
576
|
+
* class User extends Ad4mModel {
|
|
577
|
+
* // Basic required property with default initial value
|
|
578
|
+
* @Property({
|
|
579
|
+
* through: "user://name"
|
|
580
|
+
* })
|
|
581
|
+
* name: string = "";
|
|
582
|
+
*
|
|
583
|
+
* // Required property with custom initial value
|
|
584
|
+
* @Property({
|
|
585
|
+
* through: "user://status",
|
|
586
|
+
* initial: "user://active"
|
|
587
|
+
* })
|
|
588
|
+
* status: string = "";
|
|
589
|
+
*
|
|
590
|
+
* // Required property with literal resolution
|
|
591
|
+
* @Property({
|
|
592
|
+
* through: "user://bio",
|
|
593
|
+
* resolveLanguage: "literal"
|
|
594
|
+
* })
|
|
595
|
+
* bio: string = "";
|
|
596
|
+
*
|
|
597
|
+
* // Required property with custom getter/setter
|
|
598
|
+
* @Property({
|
|
599
|
+
* through: "user://age",
|
|
600
|
+
* getter: `triple(Base, "user://birthYear", Year), Value is 2024 - Year`,
|
|
601
|
+
* setter: `Year is 2024 - Value, Actions = [{"action": "setSingleTarget", "source": "this", "predicate": "user://birthYear", "target": Year}]`
|
|
602
|
+
* })
|
|
603
|
+
* age: number = 0;
|
|
604
|
+
* }
|
|
605
|
+
* ```
|
|
606
|
+
*
|
|
607
|
+
* @param {PropertyOptions} opts - Property configuration
|
|
608
|
+
* @param {string} opts.through - The predicate URI for the property
|
|
609
|
+
* @param {string} [opts.initial] - Initial value (defaults to "literal://string:uninitialized")
|
|
610
|
+
* @param {string} [opts.resolveLanguage] - Language to use for value resolution (e.g. "literal")
|
|
611
|
+
* @param {string} [opts.getter] - Custom Prolog code for getting the property value
|
|
612
|
+
* @param {string} [opts.setter] - Custom Prolog code for setting the property value
|
|
613
|
+
* @param {boolean} [opts.local] - Whether the property should only be stored locally
|
|
614
|
+
*/
|
|
615
|
+
export function Property(opts) {
|
|
616
|
+
return Optional({
|
|
617
|
+
...opts,
|
|
618
|
+
required: true,
|
|
619
|
+
writable: true,
|
|
620
|
+
initial: opts.initial || "literal://string:uninitialized"
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* Decorator for defining read-only properties on model classes.
|
|
625
|
+
*
|
|
626
|
+
* @category Decorators
|
|
627
|
+
*
|
|
628
|
+
* @description
|
|
629
|
+
* A convenience decorator that defines a property that can only be read and cannot be modified after initialization.
|
|
630
|
+
* This is equivalent to using @Optional with `writable: false`.
|
|
631
|
+
*
|
|
632
|
+
* Read-only properties are ideal for:
|
|
633
|
+
* - Computed or derived values
|
|
634
|
+
* - Properties that should never change after creation
|
|
635
|
+
* - Properties that are set by the system
|
|
636
|
+
* - Properties that represent immutable data
|
|
637
|
+
*
|
|
638
|
+
* @example
|
|
639
|
+
* ```typescript
|
|
640
|
+
* class Post extends Ad4mModel {
|
|
641
|
+
* // Read-only property with custom getter for computed value
|
|
642
|
+
* @ReadOnly({
|
|
643
|
+
* through: "post://likes",
|
|
644
|
+
* getter: `findall(User, triple(Base, "post://liked_by", User), Users), length(Users, Value)`
|
|
645
|
+
* })
|
|
646
|
+
* likeCount: number = 0;
|
|
647
|
+
*
|
|
648
|
+
* // Read-only property for creation timestamp
|
|
649
|
+
* @ReadOnly({
|
|
650
|
+
* through: "post://created_at",
|
|
651
|
+
* initial: new Date().toISOString()
|
|
652
|
+
* })
|
|
653
|
+
* createdAt: string = "";
|
|
654
|
+
*
|
|
655
|
+
* // Read-only property that resolves to a Literal
|
|
656
|
+
* @ReadOnly({
|
|
657
|
+
* through: "post://author",
|
|
658
|
+
* resolveLanguage: "literal"
|
|
659
|
+
* })
|
|
660
|
+
* author: string = "";
|
|
661
|
+
*
|
|
662
|
+
* // Read-only property for system-managed data
|
|
663
|
+
* @ReadOnly({
|
|
664
|
+
* through: "post://version",
|
|
665
|
+
* initial: "1.0.0"
|
|
666
|
+
* })
|
|
667
|
+
* version: string = "";
|
|
668
|
+
* }
|
|
669
|
+
* ```
|
|
670
|
+
*
|
|
671
|
+
* @param {PropertyOptions} opts - Property configuration
|
|
672
|
+
* @param {string} opts.through - The predicate URI for the property
|
|
673
|
+
* @param {string} [opts.initial] - Initial value (if property should have one)
|
|
674
|
+
* @param {string} [opts.resolveLanguage] - Language to use for value resolution (e.g. "literal")
|
|
675
|
+
* @param {string} [opts.getter] - Custom Prolog code for getting the property value
|
|
676
|
+
* @param {boolean} [opts.local] - Whether the property should only be stored locally
|
|
677
|
+
*/
|
|
678
|
+
export function ReadOnly(opts) {
|
|
679
|
+
return Optional({
|
|
680
|
+
...opts,
|
|
681
|
+
writable: false
|
|
682
|
+
});
|
|
683
|
+
}
|
|
@@ -16,8 +16,8 @@ export declare class NeighbourhoodClient {
|
|
|
16
16
|
setOnlineStatusU(perspectiveUUID: string, status: PerspectiveUnsignedInput): Promise<boolean>;
|
|
17
17
|
sendSignal(perspectiveUUID: string, remoteAgentDid: string, payload: Perspective): Promise<boolean>;
|
|
18
18
|
sendSignalU(perspectiveUUID: string, remoteAgentDid: string, payload: PerspectiveUnsignedInput): Promise<boolean>;
|
|
19
|
-
sendBroadcast(perspectiveUUID: string, payload: Perspective): Promise<boolean>;
|
|
20
|
-
sendBroadcastU(perspectiveUUID: string, payload: PerspectiveUnsignedInput): Promise<boolean>;
|
|
19
|
+
sendBroadcast(perspectiveUUID: string, payload: Perspective, loopback?: boolean): Promise<boolean>;
|
|
20
|
+
sendBroadcastU(perspectiveUUID: string, payload: PerspectiveUnsignedInput, loopback?: boolean): Promise<boolean>;
|
|
21
21
|
dispatchSignal(perspectiveUUID: string, signal: any): void;
|
|
22
22
|
subscribeToSignals(perspectiveUUID: string): Promise<void>;
|
|
23
23
|
addSignalHandler(perspectiveUUID: string, handler: TelepresenceSignalCallback): Promise<void>;
|