-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace-parse-compile.ts
More file actions
60 lines (48 loc) · 1.9 KB
/
trace-parse-compile.ts
File metadata and controls
60 lines (48 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env bun
import { EAVStore, jsonEntityFacts } from '../src/eav-engine.js';
import { EQLSParser, EQLSCompiler } from '../src/query/eqls-parser.js';
import { DatalogEvaluator } from '../src/query/datalog-evaluator.js';
console.log('🔍 Tracing EQL-S Parse->Compile Pipeline\n');
const sampleData = [
{ id: 1, title: "Hello World", views: 1500, reactions: { likes: 100 } }
];
const store = new EAVStore();
const parser = new EQLSParser();
const compiler = new EQLSCompiler();
// Load data
sampleData.forEach((item, index) => {
const entityId = `post:${item.id}`;
const facts = jsonEntityFacts(entityId, item, 'post');
store.addFacts(facts);
});
console.log('📊 Facts with type attribute:');
const typeFacts = store.getAllFacts().filter(f => f.a === 'type');
console.log(typeFacts);
const testQuery = 'FIND post AS ?p RETURN ?p';
console.log(`\n🧪 Tracing: ${testQuery}`);
// Step 1: Parse
console.log('\n1️⃣ Parsing...');
const parseResult = parser.parse(testQuery);
if (parseResult.errors.length > 0) {
console.log('❌ Parse errors:', parseResult.errors);
} else {
console.log('✅ Parsed EQL-S query:');
console.log(JSON.stringify(parseResult.query, null, 2));
// Step 2: Compile
console.log('\n2️⃣ Compiling...');
const compiledQuery = compiler.compile(parseResult.query!);
console.log('✅ Compiled Datalog query:');
console.log('Variables:', Array.from(compiledQuery.variables));
console.log('Goals:');
compiledQuery.goals.forEach((goal, i) => {
console.log(` ${i}: ${JSON.stringify(goal)}`);
});
// Step 3: Evaluate
console.log('\n3️⃣ Evaluating...');
const evaluator = new DatalogEvaluator(store);
const result = evaluator.evaluate(compiledQuery);
console.log(`Results: ${result.bindings.length}`);
result.bindings.forEach((binding, i) => {
console.log(` ${i}: ${JSON.stringify(binding)}`);
});
}