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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//! SPARQL CONSTRUCT query validation tests
//!
//! Verifies that CONSTRUCT queries produce valid RDF triples that can be
//! re-inserted into the graph. This is critical because ggen uses CONSTRUCT
//! as its inference rule mechanism.
//!
//! Chicago TDD: Black-box behavior verification with real Graph instances.
use ggen_core::graph::{CachedResult, ConstructExecutor, Graph};
// ---------------------------------------------------------------------------
// Test 1: CONSTRUCT produces valid N-Triples
// ---------------------------------------------------------------------------
#[test]
fn test_construct_query_produces_valid_triples() {
// Arrange
let graph = Graph::new().expect("Failed to create graph");
graph
.insert_turtle(
r#"
@prefix ex: <http://example.org/> .
ex:alice ex:knows ex:bob .
ex:alice ex:knows ex:carol .
"#,
)
.expect("Failed to insert turtle");
let executor = ConstructExecutor::new(&graph);
// Act
let triples = executor
.execute(
r#"
PREFIX ex: <http://example.org/>
CONSTRUCT { ?s ex:related ?o }
WHERE { ?s ex:knows ?o }
"#,
)
.expect("CONSTRUCT query should succeed");
// Assert: Should produce exactly 2 inferred triples
assert_eq!(
triples.len(),
2,
"Expected 2 inferred triples, got {}",
triples.len()
);
// Each triple must match the N-Triples structural pattern:
// <subject> <predicate> <object> (N-Quads format without trailing dot)
let ntriples_pattern =
regex::Regex::new(r#"^<[^>]+>\s+<[^>]+>\s+(<[^>]+>|"[^"]*").*$"#).unwrap();
for triple in &triples {
assert!(
ntriples_pattern.is_match(triple),
"Triple does not match N-Triples pattern: {}",
triple
);
}
// Also verify via query_cached returns CachedResult::Graph variant
let cached = graph
.query_cached(
r#"
PREFIX ex: <http://example.org/>
CONSTRUCT { ?s ex:related ?o }
WHERE { ?s ex:knows ?o }
"#,
)
.expect("Cached CONSTRUCT should succeed");
match cached {
CachedResult::Graph(triples_from_cache) => {
assert_eq!(triples_from_cache.len(), 2);
}
other => panic!(
"Expected CachedResult::Graph, got {:?}",
std::mem::discriminant(&other)
),
}
}
// ---------------------------------------------------------------------------
// Test 2: CONSTRUCT triples can be re-inserted into a new graph
// ---------------------------------------------------------------------------
#[test]
fn test_construct_triples_can_be_reinserted() {
// Arrange
let source_graph = Graph::new().expect("Failed to create graph");
source_graph
.insert_turtle(
r#"
@prefix ex: <http://example.org/> .
ex:alice ex:knows ex:bob .
"#,
)
.expect("Failed to insert turtle");
let executor = ConstructExecutor::new(&source_graph);
// Act: Execute CONSTRUCT to produce inferred triples
let triples = executor
.execute(
r#"
PREFIX ex: <http://example.org/>
CONSTRUCT { ?s ex:related ?o }
WHERE { ?s ex:knows ?o }
"#,
)
.expect("CONSTRUCT query should succeed");
assert!(
!triples.is_empty(),
"Should have produced at least one triple"
);
// Build N-Triples string with trailing dots for Turtle parser
let ntriples: String = triples
.iter()
.map(|t| format!("{} .", t))
.collect::<Vec<_>>()
.join("\n");
// Act: Load into a brand-new graph
let target_graph = Graph::new().expect("Failed to create target graph");
target_graph
.insert_turtle(&ntriples)
.expect("Re-inserted triples should parse without error");
// Assert: The new graph contains the inferred triple
assert!(
!target_graph.is_empty(),
"Target graph should not be empty after re-insert"
);
assert_eq!(
target_graph.len(),
1,
"Target graph should have exactly 1 triple"
);
// Verify the inferred triple is queryable in the target graph
let check = target_graph
.query_cached(
r#"
PREFIX ex: <http://example.org/>
ASK { ?s ex:related ?o }
"#,
)
.expect("ASK query should succeed");
match check {
CachedResult::Boolean(true) => {} // pass
other => panic!("Expected ASK true for re-inserted triple, got {:?}", other),
}
}
// ---------------------------------------------------------------------------
// Test 3: CONSTRUCT with OPTIONAL produces partial results (no error)
// ---------------------------------------------------------------------------
#[test]
fn test_construct_with_optional_produces_partial_results() {
// Arrange: alice has a name but no email; bob has both
let graph = Graph::new().expect("Failed to create graph");
graph
.insert_turtle(
r#"
@prefix ex: <http://example.org/> .
ex:alice a ex:Person ;
ex:name "Alice" .
ex:bob a ex:Person ;
ex:name "Bob" ;
ex:email "bob@example.org" .
"#,
)
.expect("Failed to insert turtle");
let executor = ConstructExecutor::new(&graph);
// Act: CONSTRUCT with OPTIONAL — only matches for persons with an email
let triples = executor
.execute(
r#"
PREFIX ex: <http://example.org/>
CONSTRUCT { ?person ex:contact ?email }
WHERE {
?person a ex:Person ;
ex:name ?name .
OPTIONAL { ?person ex:email ?email }
FILTER(BOUND(?email))
}
"#,
)
.expect("CONSTRUCT with OPTIONAL should not error");
// Assert: Only bob has an email, so only 1 triple
assert_eq!(
triples.len(),
1,
"Expected exactly 1 triple (bob), got {}",
triples.len()
);
// The triple must reference bob
let triple_str = &triples[0];
assert!(
triple_str.contains("bob")
|| triple_str.contains("Bob")
|| triple_str.contains("http://example.org/bob"),
"Triple should reference bob: {}",
triple_str
);
}
// ---------------------------------------------------------------------------
// Test 4: Chain two CONSTRUCT rules — rule 2 depends on rule 1 output
// ---------------------------------------------------------------------------
#[test]
fn test_construct_chain_two_rules() {
// Arrange
let graph = Graph::new().expect("Failed to create graph");
graph
.insert_turtle(
r#"
@prefix ex: <http://example.org/> .
ex:alice ex:knows ex:bob .
ex:bob ex:knows ex:carol .
"#,
)
.expect("Failed to insert turtle");
let executor = ConstructExecutor::new(&graph);
// Act — Rule 1: infer ex:related from ex:knows
let rule1_count = executor
.execute_and_materialize(
r#"
PREFIX ex: <http://example.org/>
CONSTRUCT { ?s ex:related ?o }
WHERE { ?s ex:knows ?o }
"#,
)
.expect("Rule 1 materialization should succeed");
assert_eq!(rule1_count, 2, "Rule 1 should infer 2 triples");
// Act — Rule 2: infer ex:indirectConnection from ex:related (depends on rule 1)
let rule2_count = executor
.execute_and_materialize(
r#"
PREFIX ex: <http://example.org/>
CONSTRUCT { ?s ex:indirectConnection ?o }
WHERE { ?s ex:related ?o }
"#,
)
.expect("Rule 2 materialization should succeed");
assert_eq!(
rule2_count, 2,
"Rule 2 should find 2 inferred triples from rule 1"
);
// Assert: Total graph should now have original (2) + rule1 (2) + rule2 (2) = 6 triples
// (note: oxigraph deduplicates, but these are all distinct triples)
let total = graph.len();
assert!(
total >= 6,
"Graph should have at least 6 triples after chaining, got {}",
total
);
// Verify rule 2 output is queryable
let check = graph
.query_cached(
r#"
PREFIX ex: <http://example.org/>
ASK { ?s ex:indirectConnection ?o }
"#,
)
.expect("ASK query should succeed");
match check {
CachedResult::Boolean(true) => {} // pass
other => panic!("Expected ASK true for rule 2 output, got {:?}", other),
}
}
// ---------------------------------------------------------------------------
// Test 5: CONSTRUCT on empty graph returns empty (no error)
// ---------------------------------------------------------------------------
#[test]
fn test_construct_empty_graph_returns_empty() {
// Arrange
let graph = Graph::new().expect("Failed to create graph");
assert!(graph.is_empty(), "Graph should start empty");
let executor = ConstructExecutor::new(&graph);
// Act
let triples = executor
.execute(
r#"
PREFIX ex: <http://example.org/>
CONSTRUCT { ?s ex:related ?o }
WHERE { ?s ex:knows ?o }
"#,
)
.expect("CONSTRUCT on empty graph should not error");
// Assert
assert!(
triples.is_empty(),
"CONSTRUCT on empty graph should return zero triples, got {}",
triples.len()
);
// Also verify via query_cached
let cached = graph
.query_cached(
r#"
PREFIX ex: <http://example.org/>
CONSTRUCT { ?s ex:related ?o }
WHERE { ?s ex:knows ?o }
"#,
)
.expect("Cached CONSTRUCT on empty graph should not error");
match cached {
CachedResult::Graph(t) => {
assert!(t.is_empty(), "Cached result should be empty");
}
other => panic!(
"Expected CachedResult::Graph, got {:?}",
std::mem::discriminant(&other)
),
}
}