@nahisaho/yata-local 1.6.6
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/dist/auto-updater.d.ts +101 -0
- package/dist/auto-updater.d.ts.map +1 -0
- package/dist/auto-updater.js +402 -0
- package/dist/auto-updater.js.map +1 -0
- package/dist/database.d.ts +229 -0
- package/dist/database.d.ts.map +1 -0
- package/dist/database.js +959 -0
- package/dist/database.js.map +1 -0
- package/dist/index.d.ts +298 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +436 -0
- package/dist/index.js.map +1 -0
- package/dist/io.d.ts +77 -0
- package/dist/io.d.ts.map +1 -0
- package/dist/io.js +403 -0
- package/dist/io.js.map +1 -0
- package/dist/kgpr/diff-engine.d.ts +110 -0
- package/dist/kgpr/diff-engine.d.ts.map +1 -0
- package/dist/kgpr/diff-engine.js +335 -0
- package/dist/kgpr/diff-engine.js.map +1 -0
- package/dist/kgpr/index.d.ts +16 -0
- package/dist/kgpr/index.d.ts.map +1 -0
- package/dist/kgpr/index.js +14 -0
- package/dist/kgpr/index.js.map +1 -0
- package/dist/kgpr/kgpr-manager.d.ts +122 -0
- package/dist/kgpr/kgpr-manager.d.ts.map +1 -0
- package/dist/kgpr/kgpr-manager.js +344 -0
- package/dist/kgpr/kgpr-manager.js.map +1 -0
- package/dist/kgpr/privacy-filter.d.ts +109 -0
- package/dist/kgpr/privacy-filter.d.ts.map +1 -0
- package/dist/kgpr/privacy-filter.js +295 -0
- package/dist/kgpr/privacy-filter.js.map +1 -0
- package/dist/kgpr/types.d.ts +234 -0
- package/dist/kgpr/types.d.ts.map +1 -0
- package/dist/kgpr/types.js +54 -0
- package/dist/kgpr/types.js.map +1 -0
- package/dist/query-engine.d.ts +78 -0
- package/dist/query-engine.d.ts.map +1 -0
- package/dist/query-engine.js +368 -0
- package/dist/query-engine.js.map +1 -0
- package/dist/reasoning.d.ts +112 -0
- package/dist/reasoning.d.ts.map +1 -0
- package/dist/reasoning.js +455 -0
- package/dist/reasoning.js.map +1 -0
- package/dist/types.d.ts +580 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +37 -0
- package/dist/types.js.map +1 -0
- package/dist/wake-sleep/cycle-manager.d.ts +76 -0
- package/dist/wake-sleep/cycle-manager.d.ts.map +1 -0
- package/dist/wake-sleep/cycle-manager.js +291 -0
- package/dist/wake-sleep/cycle-manager.js.map +1 -0
- package/dist/wake-sleep/index.d.ts +15 -0
- package/dist/wake-sleep/index.d.ts.map +1 -0
- package/dist/wake-sleep/index.js +19 -0
- package/dist/wake-sleep/index.js.map +1 -0
- package/dist/wake-sleep/pattern-compressor.d.ts +86 -0
- package/dist/wake-sleep/pattern-compressor.d.ts.map +1 -0
- package/dist/wake-sleep/pattern-compressor.js +333 -0
- package/dist/wake-sleep/pattern-compressor.js.map +1 -0
- package/dist/wake-sleep/sleep-phase.d.ts +79 -0
- package/dist/wake-sleep/sleep-phase.d.ts.map +1 -0
- package/dist/wake-sleep/sleep-phase.js +329 -0
- package/dist/wake-sleep/sleep-phase.js.map +1 -0
- package/dist/wake-sleep/types.d.ts +244 -0
- package/dist/wake-sleep/types.d.ts.map +1 -0
- package/dist/wake-sleep/types.js +35 -0
- package/dist/wake-sleep/types.js.map +1 -0
- package/dist/wake-sleep/wake-phase.d.ts +83 -0
- package/dist/wake-sleep/wake-phase.d.ts.map +1 -0
- package/dist/wake-sleep/wake-phase.js +457 -0
- package/dist/wake-sleep/wake-phase.js.map +1 -0
- package/dist/yata-bridge.d.ts +78 -0
- package/dist/yata-bridge.d.ts.map +1 -0
- package/dist/yata-bridge.js +193 -0
- package/dist/yata-bridge.js.map +1 -0
- package/package.json +60 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query-engine.d.ts","sourceRoot":"","sources":["../src/query-engine.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,MAAM,EACN,UAAU,EACV,YAAY,EACZ,YAAY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,GAAE;QACP,SAAS,CAAC,EAAE,IAAI,GAAG,KAAK,GAAG,MAAM,CAAC;QAClC,iBAAiB,CAAC,EAAE,YAAY,EAAE,CAAC;QACnC,WAAW,CAAC,EAAE,UAAU,EAAE,CAAC;KACvB,GACL,KAAK,CAAC;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,YAAY,CAAA;KAAE,CAAC;IA4BxD;;OAEG;IACH,YAAY,CACV,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,EACb,OAAO,GAAE;QACP,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,iBAAiB,CAAC,EAAE,YAAY,EAAE,CAAC;KAC/B,GACL,IAAI,EAAE;CAmDV"}
|
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* YATA Local - Query Engine
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
* @module @nahisaho/yata-local/query-engine
|
|
6
|
+
*
|
|
7
|
+
* @see REQ-YL-QUERY-001 ~ REQ-YL-QUERY-005
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Query engine for traversing and querying the knowledge graph
|
|
11
|
+
*/
|
|
12
|
+
export class QueryEngine {
|
|
13
|
+
db;
|
|
14
|
+
constructor(db) {
|
|
15
|
+
this.db = db;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Execute graph query
|
|
19
|
+
* @see REQ-YL-QUERY-001
|
|
20
|
+
*/
|
|
21
|
+
query(query, options = {}) {
|
|
22
|
+
const startTime = Date.now();
|
|
23
|
+
const limit = options.limit ?? 100;
|
|
24
|
+
const offset = options.offset ?? 0;
|
|
25
|
+
let entities = [];
|
|
26
|
+
let relationships = [];
|
|
27
|
+
let totalCount = 0;
|
|
28
|
+
// Execute entity query
|
|
29
|
+
if (query.entityFilters) {
|
|
30
|
+
const result = this.db.queryEntities({
|
|
31
|
+
type: query.entityFilters.types,
|
|
32
|
+
namePattern: query.entityFilters.namePattern,
|
|
33
|
+
namespace: query.entityFilters.namespace,
|
|
34
|
+
}, limit, offset);
|
|
35
|
+
entities = result.entities;
|
|
36
|
+
totalCount = result.totalCount;
|
|
37
|
+
}
|
|
38
|
+
// Execute text search
|
|
39
|
+
if (query.textSearch) {
|
|
40
|
+
const searchResults = this.db.searchEntities(query.textSearch, limit);
|
|
41
|
+
entities = [...entities, ...searchResults];
|
|
42
|
+
totalCount = entities.length;
|
|
43
|
+
}
|
|
44
|
+
// Get relationships for result entities
|
|
45
|
+
if (query.includeRelationships !== false && entities.length > 0) {
|
|
46
|
+
const entityIds = new Set(entities.map(e => e.id));
|
|
47
|
+
const relSet = new Map();
|
|
48
|
+
for (const entity of entities) {
|
|
49
|
+
const rels = this.db.getRelationships(entity.id);
|
|
50
|
+
for (const rel of rels) {
|
|
51
|
+
// Filter by relationship type if specified
|
|
52
|
+
if (query.relationshipFilters?.types) {
|
|
53
|
+
if (!query.relationshipFilters.types.includes(rel.type)) {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
// Only include if source or target is in result
|
|
58
|
+
if (entityIds.has(rel.sourceId) || entityIds.has(rel.targetId)) {
|
|
59
|
+
relSet.set(rel.id, rel);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
relationships = Array.from(relSet.values());
|
|
64
|
+
}
|
|
65
|
+
const executionTime = Date.now() - startTime;
|
|
66
|
+
return {
|
|
67
|
+
entities,
|
|
68
|
+
relationships,
|
|
69
|
+
totalCount,
|
|
70
|
+
hasMore: offset + entities.length < totalCount,
|
|
71
|
+
executionTime,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Find shortest path between two entities
|
|
76
|
+
* @see REQ-YL-QUERY-002
|
|
77
|
+
*/
|
|
78
|
+
findPath(startId, endId, options = {}) {
|
|
79
|
+
const maxDepth = options.maxDepth ?? 10;
|
|
80
|
+
const direction = options.direction ?? 'both';
|
|
81
|
+
// BFS for shortest path
|
|
82
|
+
const visited = new Set();
|
|
83
|
+
const queue = [{ entityId: startId, path: [startId], relationships: [] }];
|
|
84
|
+
visited.add(startId);
|
|
85
|
+
while (queue.length > 0) {
|
|
86
|
+
const current = queue.shift();
|
|
87
|
+
if (current.entityId === endId) {
|
|
88
|
+
// Found path
|
|
89
|
+
const pathEntities = current.path.map(id => this.db.getEntity(id)).filter(Boolean);
|
|
90
|
+
return {
|
|
91
|
+
entities: pathEntities,
|
|
92
|
+
relationships: current.relationships,
|
|
93
|
+
length: current.relationships.length,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
if (current.path.length > maxDepth) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
// Get neighbors
|
|
100
|
+
const relDirection = direction === 'forward' ? 'out' : direction === 'backward' ? 'in' : 'both';
|
|
101
|
+
const rels = this.db.getRelationships(current.entityId, relDirection);
|
|
102
|
+
for (const rel of rels) {
|
|
103
|
+
// Filter by relationship type
|
|
104
|
+
if (options.relationshipTypes && !options.relationshipTypes.includes(rel.type)) {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
const nextId = rel.sourceId === current.entityId ? rel.targetId : rel.sourceId;
|
|
108
|
+
if (!visited.has(nextId)) {
|
|
109
|
+
visited.add(nextId);
|
|
110
|
+
queue.push({
|
|
111
|
+
entityId: nextId,
|
|
112
|
+
path: [...current.path, nextId],
|
|
113
|
+
relationships: [...current.relationships, rel],
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return null; // No path found
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Extract subgraph around entity
|
|
122
|
+
* @see REQ-YL-QUERY-003
|
|
123
|
+
*/
|
|
124
|
+
extractSubgraph(rootId, options = {}) {
|
|
125
|
+
const depth = options.depth ?? 2;
|
|
126
|
+
const direction = options.direction ?? 'both';
|
|
127
|
+
const entityTypes = options.entityTypes ? new Set(options.entityTypes) : null;
|
|
128
|
+
const relTypes = options.relationshipTypes ? new Set(options.relationshipTypes) : null;
|
|
129
|
+
const entities = new Map();
|
|
130
|
+
const relationships = new Map();
|
|
131
|
+
// BFS to collect subgraph
|
|
132
|
+
const visited = new Set();
|
|
133
|
+
let currentLevel = [rootId];
|
|
134
|
+
let currentDepth = 0;
|
|
135
|
+
while (currentLevel.length > 0 && currentDepth <= depth) {
|
|
136
|
+
const nextLevel = [];
|
|
137
|
+
for (const entityId of currentLevel) {
|
|
138
|
+
if (visited.has(entityId))
|
|
139
|
+
continue;
|
|
140
|
+
visited.add(entityId);
|
|
141
|
+
const entity = this.db.getEntity(entityId);
|
|
142
|
+
if (!entity)
|
|
143
|
+
continue;
|
|
144
|
+
// Filter by entity type
|
|
145
|
+
if (entityTypes && !entityTypes.has(entity.type)) {
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
entities.set(entity.id, entity);
|
|
149
|
+
// Get relationships
|
|
150
|
+
const rels = this.db.getRelationships(entityId, direction);
|
|
151
|
+
for (const rel of rels) {
|
|
152
|
+
// Filter by relationship type
|
|
153
|
+
if (relTypes && !relTypes.has(rel.type)) {
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
relationships.set(rel.id, rel);
|
|
157
|
+
// Add to next level
|
|
158
|
+
const nextId = rel.sourceId === entityId ? rel.targetId : rel.sourceId;
|
|
159
|
+
if (!visited.has(nextId)) {
|
|
160
|
+
nextLevel.push(nextId);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
currentLevel = nextLevel;
|
|
165
|
+
currentDepth++;
|
|
166
|
+
}
|
|
167
|
+
return {
|
|
168
|
+
entities: Array.from(entities.values()),
|
|
169
|
+
relationships: Array.from(relationships.values()),
|
|
170
|
+
rootId,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Pattern matching on graph
|
|
175
|
+
* @see REQ-YL-QUERY-004
|
|
176
|
+
*/
|
|
177
|
+
matchPattern(pattern) {
|
|
178
|
+
const matches = [];
|
|
179
|
+
// Get candidate entities for first pattern node
|
|
180
|
+
const firstPattern = pattern.nodes[0];
|
|
181
|
+
if (!firstPattern)
|
|
182
|
+
return matches;
|
|
183
|
+
const { entities: candidates } = this.db.queryEntities({
|
|
184
|
+
type: firstPattern.type,
|
|
185
|
+
namePattern: firstPattern.namePattern,
|
|
186
|
+
}, 1000 // Limit candidates
|
|
187
|
+
);
|
|
188
|
+
// Try to match pattern starting from each candidate
|
|
189
|
+
for (const startEntity of candidates) {
|
|
190
|
+
const bindings = new Map();
|
|
191
|
+
bindings.set(firstPattern.variable, startEntity.id);
|
|
192
|
+
const result = this.tryMatchPattern(pattern, 1, bindings);
|
|
193
|
+
if (result) {
|
|
194
|
+
matches.push({
|
|
195
|
+
bindings: Object.fromEntries(result),
|
|
196
|
+
confidence: 1.0,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return matches;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Recursive pattern matching helper
|
|
204
|
+
*/
|
|
205
|
+
tryMatchPattern(pattern, nodeIndex, bindings) {
|
|
206
|
+
if (nodeIndex >= pattern.nodes.length) {
|
|
207
|
+
// All nodes matched, now check edges
|
|
208
|
+
for (const edge of pattern.edges) {
|
|
209
|
+
const sourceId = bindings.get(edge.sourceVar);
|
|
210
|
+
const targetId = bindings.get(edge.targetVar);
|
|
211
|
+
if (!sourceId || !targetId)
|
|
212
|
+
return null;
|
|
213
|
+
// Check if relationship exists
|
|
214
|
+
const rels = this.db.getRelationships(sourceId, 'out');
|
|
215
|
+
const hasEdge = rels.some(r => r.targetId === targetId && (edge.type === undefined || r.type === edge.type));
|
|
216
|
+
if (!hasEdge)
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
return bindings;
|
|
220
|
+
}
|
|
221
|
+
const nodePattern = pattern.nodes[nodeIndex];
|
|
222
|
+
// Find edges connecting to already bound nodes
|
|
223
|
+
const connectingEdges = pattern.edges.filter(e => bindings.has(e.sourceVar) && e.targetVar === nodePattern.variable);
|
|
224
|
+
if (connectingEdges.length > 0) {
|
|
225
|
+
// Follow edges to find candidates
|
|
226
|
+
const edge = connectingEdges[0];
|
|
227
|
+
const sourceId = bindings.get(edge.sourceVar);
|
|
228
|
+
const rels = this.db.getRelationships(sourceId, 'out').filter(r => edge.type === undefined || r.type === edge.type);
|
|
229
|
+
for (const rel of rels) {
|
|
230
|
+
const targetEntity = this.db.getEntity(rel.targetId);
|
|
231
|
+
if (!targetEntity)
|
|
232
|
+
continue;
|
|
233
|
+
// Check if target matches pattern
|
|
234
|
+
if (nodePattern.type && targetEntity.type !== nodePattern.type)
|
|
235
|
+
continue;
|
|
236
|
+
if (nodePattern.namePattern &&
|
|
237
|
+
!this.matchNamePattern(targetEntity.name, nodePattern.namePattern))
|
|
238
|
+
continue;
|
|
239
|
+
// Try binding this entity
|
|
240
|
+
const newBindings = new Map(bindings);
|
|
241
|
+
newBindings.set(nodePattern.variable, targetEntity.id);
|
|
242
|
+
const result = this.tryMatchPattern(pattern, nodeIndex + 1, newBindings);
|
|
243
|
+
if (result)
|
|
244
|
+
return result;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
// No connecting edges, search all matching entities
|
|
249
|
+
const { entities } = this.db.queryEntities({
|
|
250
|
+
type: nodePattern.type,
|
|
251
|
+
namePattern: nodePattern.namePattern,
|
|
252
|
+
}, 100);
|
|
253
|
+
for (const entity of entities) {
|
|
254
|
+
const newBindings = new Map(bindings);
|
|
255
|
+
newBindings.set(nodePattern.variable, entity.id);
|
|
256
|
+
const result = this.tryMatchPattern(pattern, nodeIndex + 1, newBindings);
|
|
257
|
+
if (result)
|
|
258
|
+
return result;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Check if name matches pattern
|
|
265
|
+
*/
|
|
266
|
+
matchNamePattern(name, pattern) {
|
|
267
|
+
const regex = new RegExp('^' + pattern.replace(/\*/g, '.*') + '$');
|
|
268
|
+
return regex.test(name);
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Find entities by traversing relationships
|
|
272
|
+
*/
|
|
273
|
+
traverse(startId, relationshipTypes, direction, maxHops = 5) {
|
|
274
|
+
const result = [];
|
|
275
|
+
const visited = new Set();
|
|
276
|
+
let current = [startId];
|
|
277
|
+
for (let hop = 0; hop < maxHops && current.length > 0; hop++) {
|
|
278
|
+
const next = [];
|
|
279
|
+
for (const entityId of current) {
|
|
280
|
+
if (visited.has(entityId))
|
|
281
|
+
continue;
|
|
282
|
+
visited.add(entityId);
|
|
283
|
+
const rels = this.db.getRelationships(entityId, direction === 'forward' ? 'out' : 'in');
|
|
284
|
+
for (const rel of rels) {
|
|
285
|
+
if (!relationshipTypes.includes(rel.type))
|
|
286
|
+
continue;
|
|
287
|
+
const nextId = direction === 'forward' ? rel.targetId : rel.sourceId;
|
|
288
|
+
if (!visited.has(nextId)) {
|
|
289
|
+
const entity = this.db.getEntity(nextId);
|
|
290
|
+
if (entity) {
|
|
291
|
+
result.push(entity);
|
|
292
|
+
next.push(nextId);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
current = next;
|
|
298
|
+
}
|
|
299
|
+
return result;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Get entity neighbors
|
|
303
|
+
*/
|
|
304
|
+
getNeighbors(entityId, options = {}) {
|
|
305
|
+
const direction = options.direction ?? 'both';
|
|
306
|
+
const rels = this.db.getRelationships(entityId, direction);
|
|
307
|
+
const results = [];
|
|
308
|
+
for (const rel of rels) {
|
|
309
|
+
// Filter by relationship type
|
|
310
|
+
if (options.relationshipTypes && !options.relationshipTypes.includes(rel.type)) {
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
const neighborId = rel.sourceId === entityId ? rel.targetId : rel.sourceId;
|
|
314
|
+
const neighbor = this.db.getEntity(neighborId);
|
|
315
|
+
if (!neighbor)
|
|
316
|
+
continue;
|
|
317
|
+
// Filter by entity type
|
|
318
|
+
if (options.entityTypes && !options.entityTypes.includes(neighbor.type)) {
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
results.push({ entity: neighbor, relationship: rel });
|
|
322
|
+
}
|
|
323
|
+
return results;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Find all paths between two entities (up to limit)
|
|
327
|
+
*/
|
|
328
|
+
findAllPaths(startId, endId, options = {}) {
|
|
329
|
+
const maxDepth = options.maxDepth ?? 5;
|
|
330
|
+
const maxPaths = options.maxPaths ?? 10;
|
|
331
|
+
const paths = [];
|
|
332
|
+
const dfs = (currentId, visited, path, relationships) => {
|
|
333
|
+
if (paths.length >= maxPaths)
|
|
334
|
+
return;
|
|
335
|
+
if (path.length > maxDepth)
|
|
336
|
+
return;
|
|
337
|
+
if (currentId === endId) {
|
|
338
|
+
const entities = path.map(id => this.db.getEntity(id)).filter(Boolean);
|
|
339
|
+
paths.push({
|
|
340
|
+
entities,
|
|
341
|
+
relationships: [...relationships],
|
|
342
|
+
length: relationships.length,
|
|
343
|
+
});
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
const rels = this.db.getRelationships(currentId, 'out');
|
|
347
|
+
for (const rel of rels) {
|
|
348
|
+
if (options.relationshipTypes && !options.relationshipTypes.includes(rel.type)) {
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
const nextId = rel.targetId;
|
|
352
|
+
if (visited.has(nextId))
|
|
353
|
+
continue;
|
|
354
|
+
visited.add(nextId);
|
|
355
|
+
path.push(nextId);
|
|
356
|
+
relationships.push(rel);
|
|
357
|
+
dfs(nextId, visited, path, relationships);
|
|
358
|
+
visited.delete(nextId);
|
|
359
|
+
path.pop();
|
|
360
|
+
relationships.pop();
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
const visited = new Set([startId]);
|
|
364
|
+
dfs(startId, visited, [startId], []);
|
|
365
|
+
return paths;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
//# sourceMappingURL=query-engine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query-engine.js","sourceRoot":"","sources":["../src/query-engine.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAiBH;;GAEG;AACH,MAAM,OAAO,WAAW;IACF;IAApB,YAAoB,EAAgB;QAAhB,OAAE,GAAF,EAAE,CAAc;IAAG,CAAC;IAExC;;;OAGG;IACH,KAAK,CAAC,KAAiB,EAAE,UAAiC,EAAE;QAC1D,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAC7B,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,GAAG,CAAC;QACnC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,CAAC,CAAC;QAEnC,IAAI,QAAQ,GAAa,EAAE,CAAC;QAC5B,IAAI,aAAa,GAAmB,EAAE,CAAC;QACvC,IAAI,UAAU,GAAG,CAAC,CAAC;QAEnB,uBAAuB;QACvB,IAAI,KAAK,CAAC,aAAa,EAAE,CAAC;YACxB,MAAM,MAAM,GAAG,IAAI,CAAC,EAAE,CAAC,aAAa,CAClC;gBACE,IAAI,EAAE,KAAK,CAAC,aAAa,CAAC,KAAK;gBAC/B,WAAW,EAAE,KAAK,CAAC,aAAa,CAAC,WAAW;gBAC5C,SAAS,EAAE,KAAK,CAAC,aAAa,CAAC,SAAS;aACzC,EACD,KAAK,EACL,MAAM,CACP,CAAC;YACF,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC;YAC3B,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC;QACjC,CAAC;QAED,sBAAsB;QACtB,IAAI,KAAK,CAAC,UAAU,EAAE,CAAC;YACrB,M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,CAAC;gBAClB,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;gBAExB,GAAG,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,aAAa,CAAC,CAAC;gBAE1C,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;gBACvB,IAAI,CAAC,GAAG,EAAE,CAAC;gBACX,aAAa,CAAC,GAAG,EAAE,CAAC;YACtB,CAAC;QACH,CAAC,CAAC;QAEF,MAAM,OAAO,GAAG,IAAI,GAAG,CAAS,CAAC,OAAO,CAAC,CAAC,CAAC;QAC3C,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,CAAC,OAAO,CAAC,EAAE,EAAE,CAAC,CAAC;QAErC,OAAO,KAAK,CAAC;IACf,CAAC;CACF"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* YATA Local - Reasoning Engine
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
* @module @nahisaho/yata-local/reasoning
|
|
6
|
+
*
|
|
7
|
+
* @see REQ-YL-REASON-001 ~ REQ-YL-REASON-004
|
|
8
|
+
*/
|
|
9
|
+
import type { RelationType, InferenceRule, Constraint, ValidationResult } from './types.js';
|
|
10
|
+
import type { YataDatabase } from './database.js';
|
|
11
|
+
import type { QueryEngine } from './query-engine.js';
|
|
12
|
+
/**
|
|
13
|
+
* Inference result from reasoning engine
|
|
14
|
+
*/
|
|
15
|
+
export interface InferenceResult {
|
|
16
|
+
/** Inferred relationships */
|
|
17
|
+
inferred: Array<{
|
|
18
|
+
sourceId: string;
|
|
19
|
+
targetId: string;
|
|
20
|
+
type: RelationType;
|
|
21
|
+
confidence: number;
|
|
22
|
+
rule: string;
|
|
23
|
+
}>;
|
|
24
|
+
/** Applied rules */
|
|
25
|
+
appliedRules: string[];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Reasoning engine for inference and constraint validation
|
|
29
|
+
*/
|
|
30
|
+
export declare class ReasoningEngine {
|
|
31
|
+
private db;
|
|
32
|
+
private queryEngine;
|
|
33
|
+
private rules;
|
|
34
|
+
private constraints;
|
|
35
|
+
constructor(db: YataDatabase, queryEngine: QueryEngine);
|
|
36
|
+
/**
|
|
37
|
+
* Initialize built-in inference rules
|
|
38
|
+
* @see REQ-YL-REASON-001
|
|
39
|
+
*/
|
|
40
|
+
private initializeBuiltInRules;
|
|
41
|
+
/**
|
|
42
|
+
* Initialize built-in constraints
|
|
43
|
+
* @see REQ-YL-REASON-003
|
|
44
|
+
*/
|
|
45
|
+
private initializeBuiltInConstraints;
|
|
46
|
+
/**
|
|
47
|
+
* Add custom inference rule
|
|
48
|
+
*/
|
|
49
|
+
addRule(rule: InferenceRule): void;
|
|
50
|
+
/**
|
|
51
|
+
* Remove inference rule
|
|
52
|
+
*/
|
|
53
|
+
removeRule(ruleId: string): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Get all rules
|
|
56
|
+
*/
|
|
57
|
+
getRules(): InferenceRule[];
|
|
58
|
+
/**
|
|
59
|
+
* Add custom constraint
|
|
60
|
+
*/
|
|
61
|
+
addConstraint(constraint: Constraint): void;
|
|
62
|
+
/**
|
|
63
|
+
* Remove constraint
|
|
64
|
+
*/
|
|
65
|
+
removeConstraint(constraintId: string): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Get all constraints
|
|
68
|
+
*/
|
|
69
|
+
getConstraints(): Constraint[];
|
|
70
|
+
/**
|
|
71
|
+
* Run inference on the graph
|
|
72
|
+
* @see REQ-YL-REASON-001
|
|
73
|
+
*/
|
|
74
|
+
infer(options?: {
|
|
75
|
+
rules?: string[];
|
|
76
|
+
maxIterations?: number;
|
|
77
|
+
}): InferenceResult;
|
|
78
|
+
/**
|
|
79
|
+
* Validate graph against constraints
|
|
80
|
+
* @see REQ-YL-REASON-003
|
|
81
|
+
*/
|
|
82
|
+
validate(options?: {
|
|
83
|
+
constraints?: string[];
|
|
84
|
+
}): Promise<ValidationResult>;
|
|
85
|
+
/**
|
|
86
|
+
* Compute confidence score for entity relationship
|
|
87
|
+
* @see REQ-YL-REASON-004
|
|
88
|
+
*/
|
|
89
|
+
computeConfidence(sourceId: string, targetId: string, relType: RelationType): number;
|
|
90
|
+
/**
|
|
91
|
+
* Find potential relationships based on structural similarity
|
|
92
|
+
* @see REQ-YL-REASON-002
|
|
93
|
+
*/
|
|
94
|
+
suggestRelationships(entityId: string, options?: {
|
|
95
|
+
maxSuggestions?: number;
|
|
96
|
+
minConfidence?: number;
|
|
97
|
+
}): Array<{
|
|
98
|
+
targetId: string;
|
|
99
|
+
type: RelationType;
|
|
100
|
+
confidence: number;
|
|
101
|
+
reason: string;
|
|
102
|
+
}>;
|
|
103
|
+
/**
|
|
104
|
+
* Compute name similarity using Levenshtein distance
|
|
105
|
+
*/
|
|
106
|
+
private computeNameSimilarity;
|
|
107
|
+
/**
|
|
108
|
+
* Levenshtein distance calculation
|
|
109
|
+
*/
|
|
110
|
+
private levenshteinDistance;
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=reasoning.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reasoning.d.ts","sourceRoot":"","sources":["../src/reasoning.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,YAAY,EACZ,aAAa,EACb,UAAU,EACV,gBAAgB,EAEjB,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAErD;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,6BAA6B;IAC7B,QAAQ,EAAE,KAAK,CAAC;QACd,QAAQ,EAAE,MAAM,CAAC;QACjB,QAAQ,EAAE,MAAM,CAAC;QACjB,IAAI,EAAE,YAAY,CAAC;QACnB,UAAU,EAAE,MAAM,CAAC;QACnB,IAAI,EAAE,MAAM,CAAC;KACd,CAAC,CAAC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