@coasys/ad4m 0.10.0 → 0.10.1-multi-user-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 +3730 -906
- package/lib/index.js +3722 -902
- package/lib/src/Ad4mClient.js +2 -1
- package/lib/src/Literal.js +4 -1
- package/lib/src/agent/Agent.d.ts +12 -0
- package/lib/src/agent/Agent.js +48 -0
- package/lib/src/agent/AgentClient.d.ts +3 -1
- package/lib/src/agent/AgentClient.js +23 -0
- 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/language/Language.d.ts +12 -1
- package/lib/src/model/Ad4mModel.d.ts +622 -0
- package/lib/src/model/Ad4mModel.js +1118 -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 +11 -2
- package/lib/src/runtime/RuntimeClient.js +94 -0
- package/lib/src/runtime/RuntimeResolver.d.ts +36 -1
- package/lib/src/runtime/RuntimeResolver.js +193 -0
- package/lib/src/schema.gql +65 -16
- package/lib/src/typeDefs.js +65 -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/agent/Agent.ts +36 -0
- package/src/agent/AgentClient.ts +30 -0
- package/src/ai/AIClient.ts +35 -14
- package/src/ai/AIResolver.ts +78 -16
- package/src/index.ts +4 -3
- package/src/language/Language.ts +16 -4
- package/src/model/Ad4mModel.ts +1279 -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 +104 -2
- package/src/runtime/RuntimeResolver.ts +126 -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
|
@@ -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,102 +0,0 @@
|
|
|
1
|
-
import { PerspectiveProxy } from "../perspectives/PerspectiveProxy";
|
|
2
|
-
export type QueryPartialEntity<T> = {
|
|
3
|
-
[P in keyof T]?: T[P] | (() => string);
|
|
4
|
-
};
|
|
5
|
-
/**
|
|
6
|
-
* Class representing a subject entity.
|
|
7
|
-
* Can extend this class to create a new subject entity to add methods interact with SDNA and much better experience then using the bare bone methods.
|
|
8
|
-
*/
|
|
9
|
-
export declare class SubjectEntity {
|
|
10
|
-
#private;
|
|
11
|
-
author: string;
|
|
12
|
-
timestamp: string;
|
|
13
|
-
/**
|
|
14
|
-
* Constructs a new subject.
|
|
15
|
-
* @param perspective - The perspective that the subject belongs to.
|
|
16
|
-
* @param baseExpression - The base expression of the subject.
|
|
17
|
-
* @param soruce - The source of the subject, the expression this instance is linked too.
|
|
18
|
-
*/
|
|
19
|
-
constructor(perspective: PerspectiveProxy, baseExpression?: string, source?: string);
|
|
20
|
-
/**
|
|
21
|
-
* Gets the base expression of the subject.
|
|
22
|
-
*/
|
|
23
|
-
get baseExpression(): string;
|
|
24
|
-
private getData;
|
|
25
|
-
private setProperty;
|
|
26
|
-
private setCollectionSetter;
|
|
27
|
-
private setCollectionAdder;
|
|
28
|
-
private setCollectionRemover;
|
|
29
|
-
/**
|
|
30
|
-
* Save the subject entity.
|
|
31
|
-
* This method will create a new subject with the base expression and add a new link from the source to the base expression with the predicate "ad4m://has_child".
|
|
32
|
-
*
|
|
33
|
-
* If a property has an action, it will perform the action (Only for collections).
|
|
34
|
-
* If a property is an array and is not empty, it will set the collection.
|
|
35
|
-
* If a property is not undefined, not null, and not an empty string, it will set the property.
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* @throws Will throw an error if the subject entity cannot be converted to a subject class, or if the subject cannot be created, or if the link cannot be added, or if the subject entity cannot be updated.
|
|
39
|
-
*/
|
|
40
|
-
save(): Promise<void>;
|
|
41
|
-
/**
|
|
42
|
-
* Update the subject entity.
|
|
43
|
-
*
|
|
44
|
-
* It will iterate over the properties of the subject entity.
|
|
45
|
-
*
|
|
46
|
-
* If a property has an action, it will perform the action (Only for collections).
|
|
47
|
-
* If a property is an array and is not empty, it will set the collection.
|
|
48
|
-
* If a property is not undefined, not null, and not an empty string, it will set the property.
|
|
49
|
-
*
|
|
50
|
-
* @throws Will throw an error if the subject entity cannot be converted to a subject class, or if a property cannot be set, or if a collection cannot be set, or if the data of the subject entity cannot be gotten.
|
|
51
|
-
*/
|
|
52
|
-
update(): Promise<void>;
|
|
53
|
-
/**
|
|
54
|
-
* Get the subject entity with all the properties & collection populated.
|
|
55
|
-
*
|
|
56
|
-
* @returns The subject entity.
|
|
57
|
-
*
|
|
58
|
-
* @throws Will throw an error if the subject entity cannot be converted to a subject class, or if the data of the subject entity cannot be gotten.
|
|
59
|
-
*/
|
|
60
|
-
get(): Promise<this>;
|
|
61
|
-
/**
|
|
62
|
-
* Delete the subject entity.
|
|
63
|
-
* This method will remove the subject from the perspective.
|
|
64
|
-
*
|
|
65
|
-
* @throws Will throw an error if the subject entity cannot be removed.
|
|
66
|
-
*/
|
|
67
|
-
delete(): Promise<void>;
|
|
68
|
-
/**
|
|
69
|
-
* Get all the subject entities of the subject class.
|
|
70
|
-
*
|
|
71
|
-
* NOTE: this is a static method and should be called on the class itself.
|
|
72
|
-
*
|
|
73
|
-
* @param perspective - The perspective that the subject belongs to.
|
|
74
|
-
*
|
|
75
|
-
* @returns The subject entities.
|
|
76
|
-
*
|
|
77
|
-
* @throws Will throw an error if the subject entity cannot be converted to a subject class, or if the subject proxies cannot be gotten.
|
|
78
|
-
*/
|
|
79
|
-
static all(perspective: PerspectiveProxy): Promise<any[]>;
|
|
80
|
-
/**
|
|
81
|
-
* Query the subject entities of the subject class.
|
|
82
|
-
*
|
|
83
|
-
* NOTE: this is a static method and should be called on the class itself.
|
|
84
|
-
*
|
|
85
|
-
* @param perspective - The perspective that the subject belongs to.
|
|
86
|
-
* @param query - The query of the subject entities.
|
|
87
|
-
*
|
|
88
|
-
* @returns The subject entities.
|
|
89
|
-
*
|
|
90
|
-
* @throws Will throw an error if the subject entity cannot be converted to a subject class, or if the query cannot be inferred, or if the data of the subject entities cannot be gotten.
|
|
91
|
-
*/
|
|
92
|
-
static query(perspective: PerspectiveProxy, query?: SubjectEntityQueryParam): Promise<SubjectEntity[]>;
|
|
93
|
-
}
|
|
94
|
-
export type SubjectArray<T> = T[] | {
|
|
95
|
-
action: 'setter' | 'adder' | 'remover';
|
|
96
|
-
value: T[];
|
|
97
|
-
};
|
|
98
|
-
export type SubjectEntityQueryParam = {
|
|
99
|
-
source?: string;
|
|
100
|
-
size?: number;
|
|
101
|
-
page?: number;
|
|
102
|
-
};
|