jinaga-server 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +103 -0
- package/dist/authentication/authentication-device.d.ts +3 -1
- package/dist/authentication/authentication-device.js +3 -0
- package/dist/authentication/authentication-device.js.map +1 -1
- package/dist/authentication/authentication-impl.d.ts +3 -2
- package/dist/authentication/authentication-impl.js +3 -0
- package/dist/authentication/authentication-impl.js.map +1 -1
- package/dist/authentication/authentication-session.d.ts +4 -2
- package/dist/authentication/authentication-session.js +7 -2
- package/dist/authentication/authentication-session.js.map +1 -1
- package/dist/authentication.d.ts +5 -4
- package/dist/authentication.js +3 -0
- package/dist/authentication.js.map +1 -1
- package/dist/authorization/authorization-keystore.d.ts +5 -3
- package/dist/authorization/authorization-keystore.js +12 -9
- package/dist/authorization/authorization-keystore.js.map +1 -1
- package/dist/http/feed-cache.d.ts +5 -0
- package/dist/http/feed-cache.js +3 -0
- package/dist/http/feed-cache.js.map +1 -0
- package/dist/http/node-http.js +4 -1
- package/dist/http/node-http.js.map +1 -1
- package/dist/http/router.d.ts +5 -1
- package/dist/http/router.js +157 -24
- package/dist/http/router.js.map +1 -1
- package/dist/jinaga-server.js +6 -4
- package/dist/jinaga-server.js.map +1 -1
- package/dist/memory/memory-feed-cache.d.ts +7 -0
- package/dist/memory/memory-feed-cache.js +17 -0
- package/dist/memory/memory-feed-cache.js.map +1 -0
- package/dist/postgres/postgres-store.d.ts +5 -2
- package/dist/postgres/postgres-store.js +53 -28
- package/dist/postgres/postgres-store.js.map +1 -1
- package/dist/postgres/specification-result-sql.d.ts +11 -4
- package/dist/postgres/specification-result-sql.js +302 -5
- package/dist/postgres/specification-result-sql.js.map +1 -1
- package/dist/postgres/specification-sql.d.ts +14 -3
- package/dist/postgres/specification-sql.js +233 -100
- package/dist/postgres/specification-sql.js.map +1 -1
- package/package.json +2 -2
- package/src/authentication/authentication-device.ts +6 -0
- package/src/authentication/authentication-impl.ts +6 -3
- package/src/authentication/authentication-session.ts +11 -3
- package/src/authentication.ts +18 -5
- package/src/authorization/authorization-keystore.ts +14 -8
- package/src/http/feed-cache.ts +6 -0
- package/src/http/node-http.ts +5 -2
- package/src/http/router.ts +179 -31
- package/src/jinaga-server.ts +11 -9
- package/src/memory/memory-feed-cache.ts +15 -0
- package/src/postgres/postgres-store.ts +59 -31
- package/src/postgres/specification-result-sql.ts +450 -10
- package/src/postgres/specification-sql.ts +335 -115
- package/test/authorization/authorizationSpec.ts +11 -2
- package/test/postgres/postgresSpec.ts +31 -25
- package/test/postgres/postgresSpecificationSpec.ts +40 -68
- package/dist/postgres/query-description-builder.d.ts +0 -13
- package/dist/postgres/query-description-builder.js +0 -104
- package/dist/postgres/query-description-builder.js.map +0 -1
- package/dist/postgres/query-description.d.ts +0 -71
- package/dist/postgres/query-description.js +0 -256
- package/dist/postgres/query-description.js.map +0 -1
- package/src/postgres/query-description-builder.ts +0 -116
- package/src/postgres/query-description.ts +0 -385
|
@@ -2,18 +2,356 @@ import {
|
|
|
2
2
|
ChildProjections,
|
|
3
3
|
ElementProjection,
|
|
4
4
|
FactReference,
|
|
5
|
+
Label,
|
|
5
6
|
Match,
|
|
7
|
+
PathCondition,
|
|
6
8
|
ResultProjection,
|
|
7
9
|
SingularProjection,
|
|
8
10
|
Specification,
|
|
9
11
|
SpecificationProjection,
|
|
10
12
|
} from "jinaga";
|
|
11
13
|
|
|
12
|
-
import { FactTypeMap, getFactTypeId, RoleMap } from "./maps";
|
|
13
|
-
import { FactDescription, InputDescription, QueryDescription, SpecificationSqlQuery } from "./query-description";
|
|
14
|
-
import { QueryDescriptionBuilder } from "./query-description-builder";
|
|
14
|
+
import { FactTypeMap, getFactTypeId, getRoleId, RoleMap } from "./maps";
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
interface SpecificationLabel {
|
|
17
|
+
name: string;
|
|
18
|
+
type: string;
|
|
19
|
+
index: number;
|
|
20
|
+
}
|
|
21
|
+
interface FactDescription {
|
|
22
|
+
type: string;
|
|
23
|
+
factIndex: number;
|
|
24
|
+
}
|
|
25
|
+
interface EdgeDescription {
|
|
26
|
+
edgeIndex: number;
|
|
27
|
+
predecessorFactIndex: number;
|
|
28
|
+
successorFactIndex: number;
|
|
29
|
+
roleParameter: number;
|
|
30
|
+
}
|
|
31
|
+
interface NotExistsConditionDescription {
|
|
32
|
+
edges: EdgeDescription[];
|
|
33
|
+
notExistsConditions: NotExistsConditionDescription[];
|
|
34
|
+
}
|
|
35
|
+
interface SpecificationSqlQuery {
|
|
36
|
+
sql: string;
|
|
37
|
+
parameters: (string | number | number[])[];
|
|
38
|
+
labels: SpecificationLabel[];
|
|
39
|
+
bookmark: string;
|
|
40
|
+
};
|
|
41
|
+
interface InputDescription {
|
|
42
|
+
label: string;
|
|
43
|
+
factIndex: number;
|
|
44
|
+
factTypeId: number;
|
|
45
|
+
factHash: string;
|
|
46
|
+
factTypeParameter: number;
|
|
47
|
+
factHashParameter: number;
|
|
48
|
+
}
|
|
49
|
+
interface OutputDescription {
|
|
50
|
+
label: string;
|
|
51
|
+
type: string;
|
|
52
|
+
factIndex: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function countEdges(notExistsConditions: NotExistsConditionDescription[]): number {
|
|
56
|
+
return notExistsConditions.reduce((count, c) => count + c.edges.length + countEdges(c.notExistsConditions),
|
|
57
|
+
0);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
class QueryDescription {
|
|
61
|
+
// An unsatisfiable query description will produce no results.
|
|
62
|
+
static unsatisfiable: QueryDescription = new QueryDescription(
|
|
63
|
+
[], [], [], [], [], []
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
constructor(
|
|
67
|
+
private readonly inputs: InputDescription[],
|
|
68
|
+
private readonly parameters: (string | number)[],
|
|
69
|
+
private readonly outputs: OutputDescription[],
|
|
70
|
+
private readonly facts: FactDescription[],
|
|
71
|
+
private readonly edges: EdgeDescription[],
|
|
72
|
+
private readonly notExistsConditions: NotExistsConditionDescription[] = []
|
|
73
|
+
) {}
|
|
74
|
+
|
|
75
|
+
public withParameter(parameter: string | number): { query: QueryDescription; parameterIndex: number; } {
|
|
76
|
+
const parameterIndex = this.parameters.length + 1;
|
|
77
|
+
const query = new QueryDescription(
|
|
78
|
+
this.inputs,
|
|
79
|
+
this.parameters.concat(parameter),
|
|
80
|
+
this.outputs,
|
|
81
|
+
this.facts,
|
|
82
|
+
this.edges,
|
|
83
|
+
this.notExistsConditions
|
|
84
|
+
);
|
|
85
|
+
return { query, parameterIndex };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
public withInputParameter(label: string): QueryDescription {
|
|
89
|
+
const factTypeParameter = this.parameters.length + 1;
|
|
90
|
+
const factHashParameter = factTypeParameter + 1;
|
|
91
|
+
const input = this.inputs.find(i => i.label === label);
|
|
92
|
+
const inputs = this.inputs.map(input => input.label === label
|
|
93
|
+
? { ...input, factTypeParameter, factHashParameter }
|
|
94
|
+
: input
|
|
95
|
+
);
|
|
96
|
+
return new QueryDescription(
|
|
97
|
+
inputs,
|
|
98
|
+
this.parameters.concat(input.factTypeId, input.factHash),
|
|
99
|
+
this.outputs,
|
|
100
|
+
this.facts,
|
|
101
|
+
this.edges,
|
|
102
|
+
this.notExistsConditions
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public withFact(type: string): { query: QueryDescription; factIndex: number; } {
|
|
107
|
+
const factIndex = this.facts.length + 1;
|
|
108
|
+
const fact = { factIndex, type };
|
|
109
|
+
const query = new QueryDescription(
|
|
110
|
+
this.inputs,
|
|
111
|
+
this.parameters,
|
|
112
|
+
this.outputs,
|
|
113
|
+
this.facts.concat(fact),
|
|
114
|
+
this.edges,
|
|
115
|
+
this.notExistsConditions
|
|
116
|
+
);
|
|
117
|
+
return { query, factIndex };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
public withOutput(label: string, type: string, factIndex: number): QueryDescription {
|
|
121
|
+
const output = { label, type, factIndex };
|
|
122
|
+
const query = new QueryDescription(
|
|
123
|
+
this.inputs,
|
|
124
|
+
this.parameters,
|
|
125
|
+
this.outputs.concat(output),
|
|
126
|
+
this.facts,
|
|
127
|
+
this.edges,
|
|
128
|
+
this.notExistsConditions
|
|
129
|
+
);
|
|
130
|
+
return query;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
public withEdge(predecessorFactIndex: number, successorFactIndex: number, roleParameter: number, path: number[]) {
|
|
134
|
+
const edge = {
|
|
135
|
+
edgeIndex: this.edges.length + countEdges(this.notExistsConditions) + 1,
|
|
136
|
+
predecessorFactIndex,
|
|
137
|
+
successorFactIndex,
|
|
138
|
+
roleParameter
|
|
139
|
+
};
|
|
140
|
+
const query = (path.length === 0)
|
|
141
|
+
? new QueryDescription(
|
|
142
|
+
this.inputs,
|
|
143
|
+
this.parameters,
|
|
144
|
+
this.outputs,
|
|
145
|
+
this.facts,
|
|
146
|
+
this.edges.concat(edge),
|
|
147
|
+
this.notExistsConditions
|
|
148
|
+
)
|
|
149
|
+
: new QueryDescription(
|
|
150
|
+
this.inputs,
|
|
151
|
+
this.parameters,
|
|
152
|
+
this.outputs,
|
|
153
|
+
this.facts,
|
|
154
|
+
this.edges,
|
|
155
|
+
notExistsWithEdge(this.notExistsConditions, edge, path)
|
|
156
|
+
);
|
|
157
|
+
return query;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
public withNotExistsCondition(path: number[]): { query: QueryDescription; path: number[]; } {
|
|
161
|
+
const { notExistsConditions: newNotExistsConditions, path: newPath } = notExistsWithCondition(this.notExistsConditions, path);
|
|
162
|
+
const query = new QueryDescription(
|
|
163
|
+
this.inputs,
|
|
164
|
+
this.parameters,
|
|
165
|
+
this.outputs,
|
|
166
|
+
this.facts,
|
|
167
|
+
this.edges,
|
|
168
|
+
newNotExistsConditions
|
|
169
|
+
);
|
|
170
|
+
return { query, path: newPath };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
isSatisfiable() {
|
|
174
|
+
return this.inputs.length > 0;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
hasOutput(label: string) {
|
|
178
|
+
return this.outputs.some(o => o.label === label);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
inputByLabel(label: string): InputDescription | undefined {
|
|
182
|
+
return this.inputs.find(i => i.label === label);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
outputLength(): number {
|
|
186
|
+
return this.outputs.length;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
generateResultSqlQuery(): SpecificationSqlQuery {
|
|
190
|
+
const columns = this.outputs
|
|
191
|
+
.map(output => `f${output.factIndex}.hash as hash${output.factIndex}, f${output.factIndex}.fact_id as id${output.factIndex}, f${output.factIndex}.data as data${output.factIndex}`)
|
|
192
|
+
.join(", ");
|
|
193
|
+
const firstEdge = this.edges[0];
|
|
194
|
+
const predecessorFact = this.inputs.find(i => i.factIndex === firstEdge.predecessorFactIndex);
|
|
195
|
+
const successorFact = this.inputs.find(i => i.factIndex === firstEdge.successorFactIndex);
|
|
196
|
+
const firstFactIndex = predecessorFact ? predecessorFact.factIndex : successorFact.factIndex;
|
|
197
|
+
const writtenFactIndexes = new Set<number>().add(firstFactIndex);
|
|
198
|
+
const joins: string[] = generateJoins(this.edges, writtenFactIndexes);
|
|
199
|
+
const inputWhereClauses = this.inputs
|
|
200
|
+
.filter(input => input.factTypeParameter !== 0)
|
|
201
|
+
.map(input => `f${input.factIndex}.fact_type_id = $${input.factTypeParameter} AND f${input.factIndex}.hash = $${input.factHashParameter}`)
|
|
202
|
+
.join(" AND ");
|
|
203
|
+
const notExistsWhereClauses = this.notExistsConditions
|
|
204
|
+
.map(notExistsWhereClause => ` AND NOT EXISTS (${generateNotExistsWhereClause(notExistsWhereClause, writtenFactIndexes)})`)
|
|
205
|
+
.join("");
|
|
206
|
+
const orderByClause = this.outputs
|
|
207
|
+
.map(output => `f${output.factIndex}.fact_id ASC`)
|
|
208
|
+
.join(", ");
|
|
209
|
+
const sql = `SELECT ${columns} FROM public.fact f${firstFactIndex}${joins.join("")} WHERE ${inputWhereClauses}${notExistsWhereClauses} ORDER BY ${orderByClause}`;
|
|
210
|
+
return {
|
|
211
|
+
sql,
|
|
212
|
+
parameters: this.parameters,
|
|
213
|
+
labels: this.outputs.map(output => ({
|
|
214
|
+
name: output.label,
|
|
215
|
+
type: output.type,
|
|
216
|
+
index: output.factIndex
|
|
217
|
+
})),
|
|
218
|
+
bookmark: "[]"
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function notExistsWithEdge(notExistsConditions: NotExistsConditionDescription[], edge: { edgeIndex: number; predecessorFactIndex: number; successorFactIndex: number; roleParameter: number; }, path: number[]): NotExistsConditionDescription[] {
|
|
224
|
+
if (path.length === 1) {
|
|
225
|
+
return notExistsConditions.map((c, i) => i === path[0] ?
|
|
226
|
+
{
|
|
227
|
+
edges: [...c.edges, edge],
|
|
228
|
+
notExistsConditions: c.notExistsConditions
|
|
229
|
+
} :
|
|
230
|
+
c
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
return notExistsConditions.map((c, i) => i === path[0] ?
|
|
235
|
+
{
|
|
236
|
+
edges: c.edges,
|
|
237
|
+
notExistsConditions: notExistsWithEdge(c.notExistsConditions, edge, path.slice(1))
|
|
238
|
+
} :
|
|
239
|
+
c
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function notExistsWithCondition(notExistsConditions: NotExistsConditionDescription[], path: number[]): { notExistsConditions: NotExistsConditionDescription[]; path: number[]; } {
|
|
245
|
+
if (path.length === 0) {
|
|
246
|
+
path = [notExistsConditions.length];
|
|
247
|
+
notExistsConditions = [
|
|
248
|
+
...notExistsConditions,
|
|
249
|
+
{
|
|
250
|
+
edges: [],
|
|
251
|
+
notExistsConditions: []
|
|
252
|
+
}
|
|
253
|
+
];
|
|
254
|
+
return { notExistsConditions, path };
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
const { notExistsConditions: newNotExistsConditions, path: newPath } = notExistsWithCondition(notExistsConditions[path[0]].notExistsConditions, path.slice(1));
|
|
258
|
+
notExistsConditions = notExistsConditions.map((c, i) => i === path[0] ?
|
|
259
|
+
{
|
|
260
|
+
edges: c.edges,
|
|
261
|
+
notExistsConditions: newNotExistsConditions
|
|
262
|
+
} :
|
|
263
|
+
c
|
|
264
|
+
);
|
|
265
|
+
path = [path[0], ...newPath];
|
|
266
|
+
return { notExistsConditions, path };
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function generateJoins(edges: EdgeDescription[], writtenFactIndexes: Set<number>) {
|
|
271
|
+
const joins: string[] = [];
|
|
272
|
+
edges.forEach(edge => {
|
|
273
|
+
if (writtenFactIndexes.has(edge.predecessorFactIndex)) {
|
|
274
|
+
if (writtenFactIndexes.has(edge.successorFactIndex)) {
|
|
275
|
+
joins.push(
|
|
276
|
+
` JOIN public.edge e${edge.edgeIndex}` +
|
|
277
|
+
` ON e${edge.edgeIndex}.predecessor_fact_id = f${edge.predecessorFactIndex}.fact_id` +
|
|
278
|
+
` AND e${edge.edgeIndex}.successor_fact_id = f${edge.successorFactIndex}.fact_id` +
|
|
279
|
+
` AND e${edge.edgeIndex}.role_id = $${edge.roleParameter}`
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
joins.push(
|
|
284
|
+
` JOIN public.edge e${edge.edgeIndex}` +
|
|
285
|
+
` ON e${edge.edgeIndex}.predecessor_fact_id = f${edge.predecessorFactIndex}.fact_id` +
|
|
286
|
+
` AND e${edge.edgeIndex}.role_id = $${edge.roleParameter}`
|
|
287
|
+
);
|
|
288
|
+
joins.push(
|
|
289
|
+
` JOIN public.fact f${edge.successorFactIndex}` +
|
|
290
|
+
` ON f${edge.successorFactIndex}.fact_id = e${edge.edgeIndex}.successor_fact_id`
|
|
291
|
+
);
|
|
292
|
+
writtenFactIndexes.add(edge.successorFactIndex);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
else if (writtenFactIndexes.has(edge.successorFactIndex)) {
|
|
296
|
+
joins.push(
|
|
297
|
+
` JOIN public.edge e${edge.edgeIndex}` +
|
|
298
|
+
` ON e${edge.edgeIndex}.successor_fact_id = f${edge.successorFactIndex}.fact_id` +
|
|
299
|
+
` AND e${edge.edgeIndex}.role_id = $${edge.roleParameter}`
|
|
300
|
+
);
|
|
301
|
+
joins.push(
|
|
302
|
+
` JOIN public.fact f${edge.predecessorFactIndex}` +
|
|
303
|
+
` ON f${edge.predecessorFactIndex}.fact_id = e${edge.edgeIndex}.predecessor_fact_id`
|
|
304
|
+
);
|
|
305
|
+
writtenFactIndexes.add(edge.predecessorFactIndex);
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
throw new Error("Neither predecessor nor successor fact has been written");
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
return joins;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function generateNotExistsWhereClause(notExistsWhereClause: NotExistsConditionDescription, outerFactIndexes: Set<number>): string {
|
|
315
|
+
const firstEdge = notExistsWhereClause.edges[0];
|
|
316
|
+
const writtenFactIndexes = new Set<number>(outerFactIndexes);
|
|
317
|
+
const firstJoin: string[] = [];
|
|
318
|
+
const whereClause: string[] = [];
|
|
319
|
+
if (writtenFactIndexes.has(firstEdge.predecessorFactIndex)) {
|
|
320
|
+
if (writtenFactIndexes.has(firstEdge.successorFactIndex)) {
|
|
321
|
+
throw new Error("Not yet implemented");
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
whereClause.push(
|
|
325
|
+
`e${firstEdge.edgeIndex}.predecessor_fact_id = f${firstEdge.predecessorFactIndex}.fact_id` +
|
|
326
|
+
` AND e${firstEdge.edgeIndex}.role_id = $${firstEdge.roleParameter}`
|
|
327
|
+
);
|
|
328
|
+
firstJoin.push(
|
|
329
|
+
` JOIN public.fact f${firstEdge.successorFactIndex}` +
|
|
330
|
+
` ON f${firstEdge.successorFactIndex}.fact_id = e${firstEdge.edgeIndex}.successor_fact_id`
|
|
331
|
+
);
|
|
332
|
+
writtenFactIndexes.add(firstEdge.successorFactIndex);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
else if (writtenFactIndexes.has(firstEdge.successorFactIndex)) {
|
|
336
|
+
whereClause.push(
|
|
337
|
+
`e${firstEdge.edgeIndex}.successor_fact_id = f${firstEdge.successorFactIndex}.fact_id` +
|
|
338
|
+
` AND e${firstEdge.edgeIndex}.role_id = $${firstEdge.roleParameter}`
|
|
339
|
+
);
|
|
340
|
+
firstJoin.push(
|
|
341
|
+
` JOIN public.fact f${firstEdge.predecessorFactIndex}` +
|
|
342
|
+
` ON f${firstEdge.predecessorFactIndex}.fact_id = e${firstEdge.edgeIndex}.predecessor_fact_id`
|
|
343
|
+
);
|
|
344
|
+
writtenFactIndexes.add(firstEdge.predecessorFactIndex);
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
throw new Error("Neither predecessor nor successor fact has been written");
|
|
348
|
+
}
|
|
349
|
+
const tailJoins: string[] = generateJoins(notExistsWhereClause.edges.slice(1), writtenFactIndexes);
|
|
350
|
+
const joins = firstJoin.concat(tailJoins);
|
|
351
|
+
return `SELECT 1 FROM public.edge e${firstEdge.edgeIndex}${joins.join("")} WHERE ${whereClause.join(" AND ")}`;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
type FactByIdentifier = {
|
|
17
355
|
[identifier: string]: FactDescription;
|
|
18
356
|
};
|
|
19
357
|
|
|
@@ -206,13 +544,11 @@ interface NamedResultComposer {
|
|
|
206
544
|
resultComposer: ResultComposer;
|
|
207
545
|
}
|
|
208
546
|
|
|
209
|
-
class ResultDescriptionBuilder
|
|
547
|
+
class ResultDescriptionBuilder {
|
|
210
548
|
constructor(
|
|
211
|
-
factTypes: FactTypeMap,
|
|
212
|
-
roleMap: RoleMap
|
|
213
|
-
) {
|
|
214
|
-
super(factTypes, roleMap);
|
|
215
|
-
}
|
|
549
|
+
private factTypes: FactTypeMap,
|
|
550
|
+
private roleMap: RoleMap
|
|
551
|
+
) { }
|
|
216
552
|
|
|
217
553
|
buildDescription(start: FactReference[], specification: Specification): ResultDescription {
|
|
218
554
|
// Verify that the number of start facts equals the number of inputs
|
|
@@ -333,6 +669,110 @@ class ResultDescriptionBuilder extends QueryDescriptionBuilder {
|
|
|
333
669
|
knownFacts
|
|
334
670
|
};
|
|
335
671
|
}
|
|
672
|
+
|
|
673
|
+
private addPathCondition(queryDescription: QueryDescription, knownFacts: FactByIdentifier, path: number[], unknown: Label, prefix: string, condition: PathCondition): { queryDescription: QueryDescription, knownFacts: FactByIdentifier } {
|
|
674
|
+
// If no input parameter has been allocated, allocate one now.
|
|
675
|
+
const input = queryDescription.inputByLabel(condition.labelRight);
|
|
676
|
+
if (input && input.factTypeParameter === 0) {
|
|
677
|
+
queryDescription = queryDescription.withInputParameter(input.label);
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
// Determine whether we have already written the output.
|
|
681
|
+
const knownFact = knownFacts[unknown.name];
|
|
682
|
+
const roleCount = condition.rolesLeft.length + condition.rolesRight.length;
|
|
683
|
+
|
|
684
|
+
// Walk up the right-hand side.
|
|
685
|
+
// This generates predecessor joins from a given or prior label.
|
|
686
|
+
let fact = knownFacts[condition.labelRight];
|
|
687
|
+
if (!fact) {
|
|
688
|
+
throw new Error(`Label ${condition.labelRight} not found. Known labels: ${Object.keys(knownFacts).join(", ")}`);
|
|
689
|
+
}
|
|
690
|
+
let type = fact.type;
|
|
691
|
+
let factIndex = fact.factIndex;
|
|
692
|
+
for (const [i, role] of condition.rolesRight.entries()) {
|
|
693
|
+
// If the type or role is not known, then no facts matching the condition can
|
|
694
|
+
// exist. The query is unsatisfiable.
|
|
695
|
+
const typeId = getFactTypeId(this.factTypes, type);
|
|
696
|
+
if (!typeId) {
|
|
697
|
+
return { queryDescription: QueryDescription.unsatisfiable, knownFacts };
|
|
698
|
+
}
|
|
699
|
+
const roleId = getRoleId(this.roleMap, typeId, role.name);
|
|
700
|
+
if (!roleId) {
|
|
701
|
+
return { queryDescription: QueryDescription.unsatisfiable, knownFacts };
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
const { query: queryWithParameter, parameterIndex: roleParameter } = queryDescription.withParameter(roleId);
|
|
705
|
+
if (i === roleCount - 1 && knownFact) {
|
|
706
|
+
// If we have already written the output, we can use the fact index.
|
|
707
|
+
queryDescription = queryWithParameter.withEdge(knownFact.factIndex, factIndex, roleParameter, path);
|
|
708
|
+
factIndex = knownFact.factIndex;
|
|
709
|
+
}
|
|
710
|
+
else {
|
|
711
|
+
// If we have not written the fact, we need to write it now.
|
|
712
|
+
const { query, factIndex: predecessorFactIndex } = queryWithParameter.withFact(role.targetType);
|
|
713
|
+
queryDescription = query.withEdge(predecessorFactIndex, factIndex, roleParameter, path);
|
|
714
|
+
factIndex = predecessorFactIndex;
|
|
715
|
+
}
|
|
716
|
+
type = role.targetType;
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
const rightType = type;
|
|
720
|
+
|
|
721
|
+
// Walk up the left-hand side.
|
|
722
|
+
// We will need to reverse this walk to generate successor joins.
|
|
723
|
+
type = unknown.type;
|
|
724
|
+
const newEdges: {
|
|
725
|
+
roleId: number;
|
|
726
|
+
declaringType: string;
|
|
727
|
+
}[] = [];
|
|
728
|
+
for (const role of condition.rolesLeft) {
|
|
729
|
+
// If the type or role is not known, then no facts matching the condition can
|
|
730
|
+
// exist. The query is unsatisfiable.
|
|
731
|
+
const typeId = getFactTypeId(this.factTypes, type);
|
|
732
|
+
if (!typeId) {
|
|
733
|
+
return { queryDescription: QueryDescription.unsatisfiable, knownFacts };
|
|
734
|
+
}
|
|
735
|
+
const roleId = getRoleId(this.roleMap, typeId, role.name);
|
|
736
|
+
if (!roleId) {
|
|
737
|
+
return { queryDescription: QueryDescription.unsatisfiable, knownFacts };
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
newEdges.push({
|
|
741
|
+
roleId,
|
|
742
|
+
declaringType: type
|
|
743
|
+
});
|
|
744
|
+
type = role.targetType;
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
if (type !== rightType) {
|
|
748
|
+
throw new Error(`Type mismatch: ${type} is compared to ${rightType}`);
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
newEdges.reverse().forEach(({ roleId, declaringType }, i) => {
|
|
752
|
+
const { query: queryWithParameter, parameterIndex: roleParameter } = queryDescription.withParameter(roleId);
|
|
753
|
+
if (condition.rolesRight.length + i === roleCount - 1 && knownFact) {
|
|
754
|
+
queryDescription = queryWithParameter.withEdge(factIndex, knownFact.factIndex, roleParameter, path);
|
|
755
|
+
factIndex = knownFact.factIndex;
|
|
756
|
+
}
|
|
757
|
+
else {
|
|
758
|
+
const { query: queryWithFact, factIndex: successorFactIndex } = queryWithParameter.withFact(declaringType);
|
|
759
|
+
queryDescription = queryWithFact.withEdge(factIndex, successorFactIndex, roleParameter, path);
|
|
760
|
+
factIndex = successorFactIndex;
|
|
761
|
+
}
|
|
762
|
+
});
|
|
763
|
+
|
|
764
|
+
// If we have not captured the known fact, add it now.
|
|
765
|
+
if (!knownFact) {
|
|
766
|
+
knownFacts = { ...knownFacts, [unknown.name]: { factIndex, type: unknown.type } };
|
|
767
|
+
// If we have not written the output, write it now.
|
|
768
|
+
// Only write the output if we are not inside of an existential condition.
|
|
769
|
+
// Use the prefix, which will be set for projections.
|
|
770
|
+
if (path.length === 0) {
|
|
771
|
+
queryDescription = queryDescription.withOutput(prefix + unknown.name, unknown.type, factIndex);
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
return { queryDescription, knownFacts };
|
|
775
|
+
}
|
|
336
776
|
}
|
|
337
777
|
|
|
338
778
|
function idsEqual(a: number[], b: number[]) {
|