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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! SPARQL pattern matching: OPTIONAL and UNION clauses
use crate::model::{Quad, Term};
use crate::rdf_store::VariableBinding;
use crate::Result;
/// Simple triple pattern for matching
#[derive(Debug, Clone)]
pub struct SimpleTriplePattern {
pub subject: Option<String>,
pub predicate: Option<String>,
pub object: Option<String>,
}
/// Pattern group (required or optional)
#[derive(Debug, Clone)]
pub struct PatternGroup {
pub patterns: Vec<SimpleTriplePattern>,
pub optional: bool,
}
/// Union group for SPARQL UNION clause
#[derive(Debug, Clone)]
pub struct UnionGroup {
pub branches: Vec<Vec<PatternGroup>>,
}
/// Check if query contains UNION
pub fn has_union(sparql: &str) -> bool {
let sparql_upper = sparql.to_uppercase();
sparql_upper.contains(" UNION ")
|| sparql_upper.contains("\nUNION\n")
|| sparql_upper.contains("{UNION")
}
/// Find matching closing brace
pub fn find_matching_brace(text: &str, start_pos: usize) -> Option<usize> {
let chars: Vec<char> = text.chars().collect();
if start_pos >= chars.len() || chars[start_pos] != '{' {
return None;
}
let mut brace_count = 1;
for (i, &ch) in chars.iter().enumerate().skip(start_pos + 1) {
if ch == '{' {
brace_count += 1;
} else if ch == '}' {
brace_count -= 1;
if brace_count == 0 {
return Some(i);
}
}
}
None
}
/// Parse a simple triple pattern from text
pub fn parse_simple_pattern(text: &str) -> Option<SimpleTriplePattern> {
// Simple pattern: ?s ?p ?o . or <uri> <uri> "literal" .
let text = text.trim();
// Split by periods and process each potential pattern
for line in text.split('.') {
let line = line.trim();
if line.is_empty() {
continue;
}
// Skip FILTER, BIND, VALUES, UNION keywords
let line_upper = line.to_uppercase();
if line_upper.contains("FILTER")
|| line_upper.contains("BIND")
|| line_upper.contains("VALUES")
|| line_upper.contains("UNION")
{
continue;
}
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 3 {
return Some(SimpleTriplePattern {
subject: Some(parts[0].to_string()),
predicate: Some(parts[1].to_string()),
object: Some(parts[2..].join(" ")),
});
}
}
None
}
/// Extract pattern groups (required and optional) from WHERE clause
pub fn extract_pattern_groups(sparql: &str) -> Result<Vec<PatternGroup>> {
let mut groups = Vec::new();
if let Some(where_start) = sparql.to_uppercase().find("WHERE") {
let where_clause = &sparql[where_start + 5..];
// Find the main WHERE block
if let Some(start_brace) = where_clause.find('{') {
if let Some(end_brace) = find_matching_brace(where_clause, start_brace) {
let pattern_text = &where_clause[start_brace + 1..end_brace];
// Check for OPTIONAL blocks
let sparql_upper = pattern_text.to_uppercase();
if sparql_upper.contains("OPTIONAL") {
// Parse with OPTIONAL support
let mut pos = 0;
let mut required_patterns = Vec::new();
while pos < pattern_text.len() {
// Look for OPTIONAL keyword
if let Some(opt_pos) = pattern_text[pos..].to_uppercase().find("OPTIONAL") {
let abs_pos = pos + opt_pos;
// Add any required patterns before OPTIONAL
let before_optional = &pattern_text[pos..abs_pos];
if let Some(req_pattern) = parse_simple_pattern(before_optional) {
required_patterns.push(req_pattern);
}
// Find OPTIONAL block
let after_optional = &pattern_text[abs_pos + 8..];
if let Some(opt_brace) = after_optional.find('{') {
if let Some(opt_end) =
find_matching_brace(after_optional, opt_brace)
{
let optional_content = &after_optional[opt_brace + 1..opt_end];
// Parse optional patterns
if let Some(opt_pattern) =
parse_simple_pattern(optional_content)
{
groups.push(PatternGroup {
patterns: vec![opt_pattern],
optional: true,
});
}
pos = abs_pos + 8 + opt_end + 1;
} else {
break;
}
} else {
break;
}
} else {
// No more OPTIONAL, add remaining as required
if let Some(req_pattern) = parse_simple_pattern(&pattern_text[pos..]) {
required_patterns.push(req_pattern);
}
break;
}
}
// Add required patterns group
if !required_patterns.is_empty() {
groups.push(PatternGroup {
patterns: required_patterns,
optional: false,
});
}
} else {
// No OPTIONAL - all patterns are required
if let Some(pattern) = parse_simple_pattern(pattern_text) {
groups.push(PatternGroup {
patterns: vec![pattern],
optional: false,
});
}
}
}
}
}
Ok(groups)
}
/// Apply optional patterns to extend existing bindings
pub fn apply_optional_patterns<F>(
bindings: Vec<VariableBinding>,
patterns: &[SimpleTriplePattern],
query_quads: F,
) -> Result<Vec<VariableBinding>>
where
F: Fn(&SimpleTriplePattern) -> Result<Vec<Quad>>,
{
let mut new_results = Vec::new();
for binding in bindings {
let mut extended = false;
// Try to extend this binding with optional patterns
for pattern in patterns {
let matching_quads = query_quads(pattern)?;
for quad in matching_quads {
let mut new_binding = binding.clone();
let mut compatible = true;
// Check subject compatibility
if let Some(var) = &pattern.subject {
if let Some(var_name) = var.strip_prefix('?') {
if let Some(existing) = binding.get(var_name) {
let new_term = Term::from(quad.subject().clone());
if format!("{:?}", existing) != format!("{:?}", new_term) {
compatible = false;
}
} else {
new_binding
.bind(var_name.to_string(), Term::from(quad.subject().clone()));
}
}
}
// Check predicate compatibility
if compatible {
if let Some(var) = &pattern.predicate {
if let Some(var_name) = var.strip_prefix('?') {
if let Some(existing) = binding.get(var_name) {
let new_term = Term::from(quad.predicate().clone());
if format!("{:?}", existing) != format!("{:?}", new_term) {
compatible = false;
}
} else {
new_binding.bind(
var_name.to_string(),
Term::from(quad.predicate().clone()),
);
}
}
}
}
// Check object compatibility
if compatible {
if let Some(var) = &pattern.object {
if let Some(var_name) = var.strip_prefix('?') {
if let Some(existing) = binding.get(var_name) {
let new_term = Term::from(quad.object().clone());
if format!("{:?}", existing) != format!("{:?}", new_term) {
compatible = false;
}
} else {
new_binding
.bind(var_name.to_string(), Term::from(quad.object().clone()));
}
}
}
}
if compatible {
new_results.push(new_binding);
extended = true;
}
}
}
// If no optional pattern matched, keep original binding
if !extended {
new_results.push(binding);
}
}
Ok(new_results)
}
/// Extract UNION groups from WHERE clause
pub fn extract_union_groups(sparql: &str) -> Result<Option<UnionGroup>> {
if !has_union(sparql) {
return Ok(None);
}
if let Some(where_start) = sparql.to_uppercase().find("WHERE") {
let where_clause = &sparql[where_start + 5..];
if let Some(start_brace) = where_clause.find('{') {
if let Some(end_brace) = find_matching_brace(where_clause, start_brace) {
let content = &where_clause[start_brace + 1..end_brace];
// Split by UNION keyword
let mut branches = Vec::new();
let mut current_branch = String::new();
let mut pos = 0;
while pos < content.len() {
if let Some(union_pos) = content[pos..].to_uppercase().find(" UNION ") {
let abs_pos = pos + union_pos;
current_branch.push_str(&content[pos..abs_pos]);
// Parse the branch we accumulated
if let Some(branch) = parse_union_branch(¤t_branch)? {
branches.push(branch);
}
current_branch.clear();
pos = abs_pos + 7; // Skip " UNION "
} else {
// Last branch
current_branch.push_str(&content[pos..]);
break;
}
}
// Parse final branch
if !current_branch.trim().is_empty() {
if let Some(branch) = parse_union_branch(¤t_branch)? {
branches.push(branch);
}
}
if !branches.is_empty() {
return Ok(Some(UnionGroup { branches }));
}
}
}
}
Ok(None)
}
/// Parse a single UNION branch
pub fn parse_union_branch(branch_text: &str) -> Result<Option<Vec<PatternGroup>>> {
let branch_text = branch_text.trim();
// Branch can be either { pattern } or just pattern
let pattern_text = if branch_text.starts_with('{') && branch_text.ends_with('}') {
&branch_text[1..branch_text.len() - 1]
} else {
branch_text
};
let mut groups = Vec::new();
// Check for OPTIONAL in the branch
if pattern_text.to_uppercase().contains("OPTIONAL") {
// Parse with OPTIONAL support
let mut pos = 0;
let mut required_patterns = Vec::new();
while pos < pattern_text.len() {
if let Some(opt_pos) = pattern_text[pos..].to_uppercase().find("OPTIONAL") {
let abs_pos = pos + opt_pos;
// Add required patterns before OPTIONAL
let before_optional = &pattern_text[pos..abs_pos];
if let Some(req_pattern) = parse_simple_pattern(before_optional) {
required_patterns.push(req_pattern);
}
// Find OPTIONAL block
let after_optional = &pattern_text[abs_pos + 8..];
if let Some(opt_brace) = after_optional.find('{') {
if let Some(opt_end) = find_matching_brace(after_optional, opt_brace) {
let optional_content = &after_optional[opt_brace + 1..opt_end];
if let Some(opt_pattern) = parse_simple_pattern(optional_content) {
groups.push(PatternGroup {
patterns: vec![opt_pattern],
optional: true,
});
}
pos = abs_pos + 8 + opt_end + 1;
} else {
break;
}
} else {
break;
}
} else {
// No more OPTIONAL
if let Some(req_pattern) = parse_simple_pattern(&pattern_text[pos..]) {
required_patterns.push(req_pattern);
}
break;
}
}
if !required_patterns.is_empty() {
groups.push(PatternGroup {
patterns: required_patterns,
optional: false,
});
}
} else {
// No OPTIONAL - simple pattern
if let Some(pattern) = parse_simple_pattern(pattern_text) {
groups.push(PatternGroup {
patterns: vec![pattern],
optional: false,
});
}
}
if groups.is_empty() {
Ok(None)
} else {
Ok(Some(groups))
}
}
/// Execute a SELECT query with UNION (needs to be implemented in RdfStore)
/// This is a placeholder - actual implementation stays in RdfStore
pub fn execute_union_query_placeholder() {
// This function signature is here for reference
// The actual execute_union_query must stay in RdfStore because it needs access to self
}