@coasys/ad4m 0.10.1-release-candidate-3-test-2 → 0.10.1-release-candidate-4-debug-strings

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.
@@ -1,188 +0,0 @@
1
- export declare class PerspectiveAction {
2
- action: string;
3
- source: string;
4
- predicate: string;
5
- target: string;
6
- }
7
- export declare function addLink(source: string, predicate: string, target: string): PerspectiveAction;
8
- export declare function hasLink(predicate: string): string;
9
- export interface InstanceQueryParams {
10
- /**
11
- * An object representing the WHERE clause of the query.
12
- */
13
- where?: object;
14
- /**
15
- * A string representing the condition clause of the query.
16
- */
17
- condition?: string;
18
- }
19
- /**
20
- * Decorator for querying instances of a subject class.
21
- *
22
- * @category Decorators
23
- *
24
- * @description
25
- * NOTE: Only works on methods that return a promise and will throw an error if not used on a method that returns a promise.
26
- * This will allow you to query for instances of a subject class with custom clauses within the instance clauses.
27
- *
28
- * @example
29
- * // Usage with where clause
30
- * InstanceQuery({ where: { name: "John" }}) // this will return all the properties of the subject class with the name "John"
31
- *
32
- * @example
33
- * // Usage with condition clause
34
- * InstanceQuery({ condition: "triple(Instance, 'age', Age), Age > 18" }) // this will return all the properties of the subject class with the age greater than 18
35
- *
36
- * @param {Object} [options] - Query options.
37
- */
38
- export declare function InstanceQuery(options?: InstanceQueryParams): <T>(target: T, key: keyof T, descriptor: PropertyDescriptor) => void;
39
- export interface PropertyOptions {
40
- /**
41
- * The predicate of the property. All properties must have this option.
42
- */
43
- through?: string;
44
- /**
45
- * The initial value of the property. Required if the property is marked as required.
46
- */
47
- initial?: string;
48
- /**
49
- * Indicates whether the property is required. If true, an initial value must be provided.
50
- */
51
- required?: boolean;
52
- /**
53
- * Indicates whether the property is writable. If true, a setter will be available in the prolog engine.
54
- */
55
- writable?: boolean;
56
- /**
57
- * The language used to store the property. Can be the default `Literal` Language or a custom language address.
58
- */
59
- resolveLanguage?: string;
60
- /**
61
- * Custom getter to get the value of the property in the prolog engine. If not provided, the default getter will be used.
62
- */
63
- getter?: string;
64
- /**
65
- * Custom setter to set the value of the property in the prolog engine. Only available if the property is writable.
66
- */
67
- setter?: string;
68
- /**
69
- * Indicates whether the property is stored locally in the perspective and not in the network. Useful for properties that are not meant to be shared with the network.
70
- */
71
- local?: boolean;
72
- }
73
- /**
74
- * Decorator for defining properties of a subject class.
75
- *
76
- * @category Decorators
77
- *
78
- * @description
79
- * This will allow you to define properties with different conditions and how they would be defined in proflog engine.
80
- *
81
- * - All properties must have a `through` option which is the predicate of the property.
82
- * -e If the property is required, it must have an `initial` option which is the initial value of the property.
83
- * - If the property is writable, it will have a setter in prolog engine. A custom setter can be defined with the `setter` option.
84
- * - If resolveLanguage is defined, you can use the default `Literal` Language or use your custom language address that can be used to store the property.
85
- * - If a custom getter is defined, it will be used to get the value of the property in prolog engine. If not, the default getter will be used.
86
- * - If local is defined, the property will be stored locally in the perspective and not in the network. This is useful for properties that are not meant to be shared with the network
87
- *
88
- * @example
89
- * // Usage
90
- * SubjectProperty({ through: "ad4m://name", initial: "John", required: true }) // this will define a property with the name "ad4m://name" and the initial value "John"
91
- *
92
- * @param {PropertyOptions} [opts] - Property options.
93
- */
94
- export declare function SubjectProperty(opts: PropertyOptions): <T>(target: T, key: keyof T) => void;
95
- export interface FlagOptions {
96
- /**
97
- * The predicate of the property. All properties must have this option.
98
- */
99
- through: string;
100
- /**
101
- * The value of the property.
102
- */
103
- value: string;
104
- }
105
- /**
106
- * Decorator for defining flags of a subject class
107
- *
108
- * @category Decorators
109
- *
110
- * @description
111
- * The idea behind flag decorator is to define a property that is required and has an initial value.
112
- * This will allow you to define a strict instance query. This behaviour can also be achieved with the `SubjectProperty` decorator but the `SubjectFlag` decorator is a shorthand for that.
113
- *
114
- * NOTE: Use of Flag is discouraged and should be used only when necessary.
115
- *
116
- * - All properties must have a `through` & `initial` option which is the predicate of the property.
117
- *
118
- * @example
119
- * // Usage
120
- * SubjectFlag({ through: "ad4m://name", value: "John" }) // this will define a flag with the name "ad4m://name" and the initial value "John"
121
- *
122
- * @param {FlagOptions} [opts] Flag options.
123
- */
124
- export declare function SubjectFlag(opts: FlagOptions): <T>(target: T, key: keyof T) => void;
125
- interface WhereOptions {
126
- isInstance?: any;
127
- condition?: string;
128
- }
129
- export interface CollectionOptions {
130
- /**
131
- * The predicate of the property. All properties must have this option.
132
- */
133
- through: string;
134
- /**
135
- * An object representing the WHERE clause of the query.
136
- */
137
- where?: WhereOptions;
138
- /**
139
- * Indicates whether the property is stored locally in the perspective and not in the network. Useful for properties that are not meant to be shared with the network.
140
- */
141
- local?: boolean;
142
- }
143
- /**
144
- * Decorator for defining collections of a subject class.
145
- *
146
- * @category Decorators
147
- *
148
- * @description
149
- * This will allow you to define collections with different conditions and how they would be defined in proflog engine.
150
- *
151
- * NOTE: The property needs to be an array for it to picked up during the initialization phase.
152
- *
153
- * - All collections must have a `through` option which is the predicate of the collection.
154
- * - If the collection has a `where` option, it can be used to define a custom condition for the collection.
155
- * - If the collection has a `local` option, the collection will be stored locally in the perspective and not in the network. This is useful for collections that are not meant to be shared with the network.
156
- *
157
- * @example
158
- * // Usage
159
- * SubjectCollection({ through: "ad4m://friends" }) // this will define a collection with the name "ad4m://friends"
160
- *
161
- * @param opts Collection options.
162
- */
163
- export declare function SubjectCollection(opts: CollectionOptions): <T>(target: T, key: keyof T) => void;
164
- export declare function makeRandomPrologAtom(length: number): string;
165
- export interface SDNAClassOptions {
166
- /**
167
- * The name of the entity.
168
- */
169
- name: string;
170
- }
171
- /**
172
- * Decorator for defining an SDNA class.
173
- *
174
- * @category Decorators
175
- *
176
- * @description
177
- * This will create a new SDNA class with the given name and add custom methods to generate the SDNA for the class, for this to work the class need to have the properties defined using the decorators like `SubjectProperty`.
178
- *
179
- * Note: This decorator is required for the class to be picked up during the initialization phase.
180
- *
181
- * @example
182
- * // Usage
183
- * SDNAClass({ name: "Person" }) // this will create a new SDNA class with the name "Person"
184
- *
185
- * @param opts SDNA class options.
186
- */
187
- export declare function SDNAClass(opts: SDNAClassOptions): (target: any) => void;
188
- export {};
@@ -1,360 +0,0 @@
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 subject class.
18
- *
19
- * @category Decorators
20
- *
21
- * @description
22
- * NOTE: Only works on methods that return a promise and will throw an error if not used on a method that returns a promise.
23
- * This will allow you to query for instances of a subject class with custom clauses within the instance clauses.
24
- *
25
- * @example
26
- * // Usage with where clause
27
- * InstanceQuery({ where: { name: "John" }}) // this will return all the properties of the subject class with the name "John"
28
- *
29
- * @example
30
- * // Usage with condition clause
31
- * InstanceQuery({ condition: "triple(Instance, 'age', Age), Age > 18" }) // this will return all the properties of the subject class with the age greater than 18
32
- *
33
- * @param {Object} [options] - Query options.
34
- */
35
- export function InstanceQuery(options) {
36
- return function (target, key, descriptor) {
37
- const originalMethod = descriptor.value;
38
- if (typeof originalMethod !== "function") {
39
- throw new Error("InstanceQuery decorator can only be applied to methods");
40
- }
41
- descriptor.value = async function (perspective) {
42
- let instances = [];
43
- //@ts-ignore
44
- let subjectClassName = target.name;
45
- let query = `subject_class("${subjectClassName}", C), instance(C, Instance)`;
46
- if (options && options.where) {
47
- for (let prop in options.where) {
48
- let value = options.where[prop];
49
- query += `, property_getter(C, Instance, "${prop}", "${value}")`;
50
- }
51
- }
52
- if (options && options.condition) {
53
- query += ', ' + options.condition;
54
- }
55
- let results = await perspective.infer(query);
56
- if (results == false) {
57
- return instances;
58
- }
59
- if (typeof results == "string") {
60
- throw results;
61
- }
62
- for (let result of results) {
63
- let instance = result.Instance;
64
- let subject = new Subject(perspective, instance, subjectClassName);
65
- await subject.init();
66
- instances.push(subject);
67
- }
68
- return instances;
69
- };
70
- };
71
- }
72
- /**
73
- * Decorator for defining properties of a subject class.
74
- *
75
- * @category Decorators
76
- *
77
- * @description
78
- * This will allow you to define properties with different conditions and how they would be defined in proflog engine.
79
- *
80
- * - All properties must have a `through` option which is the predicate of the property.
81
- * -e If the property is required, it must have an `initial` option which is the initial value of the property.
82
- * - If the property is writable, it will have a setter in prolog engine. A custom setter can be defined with the `setter` option.
83
- * - If resolveLanguage is defined, you can use the default `Literal` Language or use your custom language address that can be used to store the property.
84
- * - If a custom getter is defined, it will be used to get the value of the property in prolog engine. If not, the default getter will be used.
85
- * - If local is defined, the property will be stored locally in the perspective and not in the network. This is useful for properties that are not meant to be shared with the network
86
- *
87
- * @example
88
- * // Usage
89
- * SubjectProperty({ through: "ad4m://name", initial: "John", required: true }) // this will define a property with the name "ad4m://name" and the initial value "John"
90
- *
91
- * @param {PropertyOptions} [opts] - Property options.
92
- */
93
- export function SubjectProperty(opts) {
94
- return function (target, key) {
95
- if (opts.required && !opts.initial) {
96
- throw new Error("SubjectProperty requires an 'initial' option if 'required' is true");
97
- }
98
- if (!opts.through && !opts.getter) {
99
- throw new Error("SubjectProperty requires either 'through' or 'getter' option");
100
- }
101
- target["__properties"] = target["__properties"] || {};
102
- target["__properties"][key] = target["__properties"][key] || {};
103
- target["__properties"][key] = { ...target["__properties"][key], ...opts };
104
- if (opts.writable) {
105
- const value = key;
106
- target[`set${capitalize(value)}`] = () => { };
107
- }
108
- Object.defineProperty(target, key, { configurable: true, writable: true });
109
- };
110
- }
111
- /**
112
- * Decorator for defining flags of a subject class
113
- *
114
- * @category Decorators
115
- *
116
- * @description
117
- * The idea behind flag decorator is to define a property that is required and has an initial value.
118
- * This will allow you to define a strict instance query. This behaviour can also be achieved with the `SubjectProperty` decorator but the `SubjectFlag` decorator is a shorthand for that.
119
- *
120
- * NOTE: Use of Flag is discouraged and should be used only when necessary.
121
- *
122
- * - All properties must have a `through` & `initial` option which is the predicate of the property.
123
- *
124
- * @example
125
- * // Usage
126
- * SubjectFlag({ through: "ad4m://name", value: "John" }) // this will define a flag with the name "ad4m://name" and the initial value "John"
127
- *
128
- * @param {FlagOptions} [opts] Flag options.
129
- */
130
- export function SubjectFlag(opts) {
131
- return function (target, key) {
132
- if (!opts.through && !opts.value) {
133
- throw new Error("SubjectFlag requires a 'through' and 'value' option");
134
- }
135
- if (!opts.through) {
136
- throw new Error("SubjectFlag requires a 'through' option");
137
- }
138
- if (!opts.value) {
139
- throw new Error("SubjectFlag requires a 'value' option");
140
- }
141
- target["__properties"] = target["__properties"] || {};
142
- target["__properties"][key] = target["__properties"][key] || {};
143
- target["__properties"][key] = {
144
- ...target["__properties"][key],
145
- through: opts.through,
146
- required: true,
147
- initial: opts.value,
148
- flag: true
149
- };
150
- // @ts-ignore
151
- target[key] = opts.value;
152
- Object.defineProperty(target, key, { configurable: true, writable: true });
153
- };
154
- }
155
- /**
156
- * Decorator for defining collections of a subject class.
157
- *
158
- * @category Decorators
159
- *
160
- * @description
161
- * This will allow you to define collections with different conditions and how they would be defined in proflog engine.
162
- *
163
- * NOTE: The property needs to be an array for it to picked up during the initialization phase.
164
- *
165
- * - All collections must have a `through` option which is the predicate of the collection.
166
- * - If the collection has a `where` option, it can be used to define a custom condition for the collection.
167
- * - If the collection has a `local` option, the collection will be stored locally in the perspective and not in the network. This is useful for collections that are not meant to be shared with the network.
168
- *
169
- * @example
170
- * // Usage
171
- * SubjectCollection({ through: "ad4m://friends" }) // this will define a collection with the name "ad4m://friends"
172
- *
173
- * @param opts Collection options.
174
- */
175
- export function SubjectCollection(opts) {
176
- return function (target, key) {
177
- target["__collections"] = target["__collections"] || {};
178
- target["__collections"][key] = opts;
179
- const value = key;
180
- target[`add${capitalize(value)}`] = () => { };
181
- target[`remove${capitalize(value)}`] = () => { };
182
- target[`setCollection${capitalize(value)}`] = () => { };
183
- Object.defineProperty(target, key, { configurable: true, writable: true });
184
- };
185
- }
186
- export function makeRandomPrologAtom(length) {
187
- let result = '';
188
- let characters = 'abcdefghijklmnopqrstuvwxyz';
189
- let charactersLength = characters.length;
190
- for (let i = 0; i < length; i++) {
191
- result += characters.charAt(Math.floor(Math.random() * charactersLength));
192
- }
193
- return result;
194
- }
195
- /**
196
- * Decorator for defining an SDNA class.
197
- *
198
- * @category Decorators
199
- *
200
- * @description
201
- * This will create a new SDNA class with the given name and add custom methods to generate the SDNA for the class, for this to work the class need to have the properties defined using the decorators like `SubjectProperty`.
202
- *
203
- * Note: This decorator is required for the class to be picked up during the initialization phase.
204
- *
205
- * @example
206
- * // Usage
207
- * SDNAClass({ name: "Person" }) // this will create a new SDNA class with the name "Person"
208
- *
209
- * @param opts SDNA class options.
210
- */
211
- export function SDNAClass(opts) {
212
- return function (target) {
213
- target.prototype.className = opts.name;
214
- target.className = opts.name;
215
- target.generateSDNA = function () {
216
- let sdna = "";
217
- let subjectName = opts.name;
218
- let obj = target.prototype;
219
- let uuid = makeRandomPrologAtom(8);
220
- sdna += `subject_class("${subjectName}", ${uuid}).\n`;
221
- let classRemoverActions = [];
222
- let constructorActions = [];
223
- if (obj.subjectConstructor && obj.subjectConstructor.length) {
224
- constructorActions = constructorActions.concat(obj.subjectConstructor);
225
- }
226
- let instanceConditions = [];
227
- if (obj.isSubjectInstance && obj.isSubjectInstance.length) {
228
- instanceConditions = instanceConditions.concat(obj.isSubjectInstance);
229
- }
230
- let propertiesCode = [];
231
- let properties = obj.__properties || {};
232
- for (let property in properties) {
233
- let propertyCode = `property(${uuid}, "${property}").\n`;
234
- let { through, initial, required, resolveLanguage, writable, flag, getter, setter, local } = properties[property];
235
- if (resolveLanguage) {
236
- propertyCode += `property_resolve(${uuid}, "${property}").\n`;
237
- propertyCode += `property_resolve_language(${uuid}, "${property}", "${resolveLanguage}").\n`;
238
- }
239
- if (getter) {
240
- propertyCode += `property_getter(${uuid}, Base, "${property}", Value) :- ${getter}.\n`;
241
- }
242
- else if (through) {
243
- propertyCode += `property_getter(${uuid}, Base, "${property}", Value) :- triple(Base, "${through}", Value).\n`;
244
- if (required) {
245
- if (flag) {
246
- instanceConditions.push(`triple(Base, "${through}", "${initial}")`);
247
- }
248
- else {
249
- instanceConditions.push(`triple(Base, "${through}", _)`);
250
- }
251
- }
252
- }
253
- if (setter) {
254
- propertyCode += `property_setter(${uuid}, "${property}", Actions) :- ${setter}.\n`;
255
- }
256
- else if (writable) {
257
- let setter = obj[propertyNameToSetterName(property)];
258
- if (typeof setter === "function") {
259
- let action = [{
260
- action: "setSingleTarget",
261
- source: "this",
262
- predicate: through,
263
- target: "value",
264
- ...(local && { local: true })
265
- }];
266
- propertyCode += `property_setter(${uuid}, "${property}", '${stringifyObjectLiteral(action)}').\n`;
267
- }
268
- }
269
- propertiesCode.push(propertyCode);
270
- if (initial) {
271
- constructorActions.push({
272
- action: "addLink",
273
- source: "this",
274
- predicate: through,
275
- target: initial,
276
- });
277
- classRemoverActions.push({
278
- action: "removeLink",
279
- source: "this",
280
- predicate: through,
281
- target: initial,
282
- });
283
- }
284
- }
285
- let collectionsCode = [];
286
- let collections = obj.__collections || {};
287
- for (let collection in collections) {
288
- let collectionCode = `collection(${uuid}, "${collection}").\n`;
289
- let { through, where, local } = collections[collection];
290
- if (through) {
291
- if (where) {
292
- if (!where.isInstance && !where.condition) {
293
- throw "'where' needs one of 'isInstance' or 'condition'";
294
- }
295
- let conditions = [];
296
- if (where.isInstance) {
297
- let otherClass;
298
- if (where.isInstance.name) {
299
- otherClass = where.isInstance.name;
300
- }
301
- else {
302
- otherClass = where.isInstance;
303
- }
304
- conditions.push(`instance(OtherClass, Target), subject_class("${otherClass}", OtherClass)`);
305
- }
306
- if (where.condition) {
307
- conditions.push(where.condition);
308
- }
309
- const conditionString = conditions.join(", ");
310
- collectionCode += `collection_getter(${uuid}, Base, "${collection}", List) :- setof(Target, (triple(Base, "${through}", Target), ${conditionString}), List).\n`;
311
- }
312
- else {
313
- collectionCode += `collection_getter(${uuid}, Base, "${collection}", List) :- findall(C, triple(Base, "${through}", C), List).\n`;
314
- }
315
- let collectionAdderAction = [{
316
- action: "addLink",
317
- source: "this",
318
- predicate: through,
319
- target: "value",
320
- ...(local && { local: true })
321
- }];
322
- let collectionRemoverAction = [{
323
- action: "removeLink",
324
- source: "this",
325
- predicate: through,
326
- target: "value",
327
- }];
328
- let collectionSetterAction = [{
329
- action: "collectionSetter",
330
- source: "this",
331
- predicate: through,
332
- target: "value",
333
- ...(local && { local: true })
334
- }];
335
- collectionCode += `collection_adder(${uuid}, "${singularToPlural(collection)}", '${stringifyObjectLiteral(collectionAdderAction)}').\n`;
336
- collectionCode += `collection_remover(${uuid}, "${singularToPlural(collection)}", '${stringifyObjectLiteral(collectionRemoverAction)}').\n`;
337
- collectionCode += `collection_setter(${uuid}, "${singularToPlural(collection)}", '${stringifyObjectLiteral(collectionSetterAction)}').\n`;
338
- }
339
- collectionsCode.push(collectionCode);
340
- }
341
- let subjectContructorJSONString = stringifyObjectLiteral(constructorActions);
342
- sdna += `constructor(${uuid}, '${subjectContructorJSONString}').\n`;
343
- if (instanceConditions.length > 0) {
344
- let instanceConditionProlog = instanceConditions.join(", ");
345
- sdna += `instance(${uuid}, Base) :- ${instanceConditionProlog}.\n`;
346
- sdna += "\n";
347
- }
348
- sdna += `destructor(${uuid}, '${stringifyObjectLiteral(classRemoverActions)}').\n`;
349
- sdna += "\n";
350
- sdna += propertiesCode.join("\n");
351
- sdna += "\n";
352
- sdna += collectionsCode.join("\n");
353
- return {
354
- sdna,
355
- name: subjectName
356
- };
357
- };
358
- Object.defineProperty(target, 'type', { configurable: true });
359
- };
360
- }
@@ -1,25 +0,0 @@
1
- import { PerspectiveProxy } from "../perspectives/PerspectiveProxy";
2
- /**
3
- * Represents a subject in the perspective.
4
- * A subject is an entity that has properties and collections.
5
- */
6
- export declare class Subject {
7
- #private;
8
- /**
9
- * Constructs a new subject.
10
- * @param perspective - The perspective that the subject belongs to.
11
- * @param baseExpression - The base expression of the subject.
12
- * @param subjectClassName - The class name of the subject.
13
- */
14
- constructor(perspective: PerspectiveProxy, baseExpression: string, subjectClassName: string);
15
- /**
16
- * Gets the base expression of the subject.
17
- */
18
- get baseExpression(): string;
19
- /**
20
- * Initializes the subject by validating it and defining its properties and collections dynamically.
21
- *
22
- * NOTE: This method should be called before using the subject. All the properties and collections of the subject defined are not type-checked.
23
- */
24
- init(): Promise<void>;
25
- }