aingle_cortex 0.6.3

Córtex API - REST/GraphQL/SPARQL interface for AIngle semantic graphs
Documentation
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
// Copyright 2019-2026 Apilium Technologies OÜ. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR Commercial

//! SPARQL query executor

use super::{ParsedQuery, QueryType, SparqlResult};
use crate::error::{Error, Result};
use aingle_graph::{GraphDB, TriplePattern as GraphTriplePattern};
use spargebra::{
    algebra::{Expression, GraphPattern},
    term::{NamedNodePattern, TermPattern},
    Query,
};
use std::collections::HashMap;

/// Execute a parsed SPARQL query against the graph
pub fn execute_query(graph: &GraphDB, query: &ParsedQuery) -> Result<SparqlResult> {
    match query.query_type {
        QueryType::Select => execute_select(graph, query),
        QueryType::Ask => execute_ask(graph, query),
        QueryType::Construct => execute_construct(graph, query),
        QueryType::Describe => execute_describe(graph, query),
    }
}

/// Execute a SELECT query
fn execute_select(graph: &GraphDB, query: &ParsedQuery) -> Result<SparqlResult> {
    match &query.query {
        Query::Select { pattern, .. } => {
            let mut bindings = Vec::new();
            let mut variables = Vec::new();

            // Execute the graph pattern
            let results = execute_pattern(graph, pattern)?;

            // Extract variables from first result
            if let Some(first) = results.first() {
                variables = first.keys().cloned().collect();
            }

            // Convert to JSON bindings
            for result in results {
                let binding: serde_json::Value =
                    serde_json::to_value(&result).map_err(|e| Error::Internal(e.to_string()))?;
                bindings.push(binding);
            }

            Ok(SparqlResult {
                result_type: "bindings".to_string(),
                variables: Some(variables),
                bindings: Some(bindings),
                boolean: None,
                triple_count: None,
            })
        }
        _ => Err(Error::Internal("Expected SELECT query".to_string())),
    }
}

/// Execute an ASK query
fn execute_ask(graph: &GraphDB, query: &ParsedQuery) -> Result<SparqlResult> {
    match &query.query {
        Query::Ask { pattern, .. } => {
            let results = execute_pattern(graph, pattern)?;
            let exists = !results.is_empty();

            Ok(SparqlResult {
                result_type: "boolean".to_string(),
                variables: None,
                bindings: None,
                boolean: Some(exists),
                triple_count: None,
            })
        }
        _ => Err(Error::Internal("Expected ASK query".to_string())),
    }
}

/// Execute a CONSTRUCT query
fn execute_construct(graph: &GraphDB, query: &ParsedQuery) -> Result<SparqlResult> {
    match &query.query {
        Query::Construct { pattern, .. } => {
            let results = execute_pattern(graph, pattern)?;
            let count = results.len();

            Ok(SparqlResult {
                result_type: "graph".to_string(),
                variables: None,
                bindings: Some(
                    results
                        .into_iter()
                        .map(|r| serde_json::to_value(&r).unwrap_or_default())
                        .collect(),
                ),
                boolean: None,
                triple_count: Some(count),
            })
        }
        _ => Err(Error::Internal("Expected CONSTRUCT query".to_string())),
    }
}

/// Execute a DESCRIBE query
fn execute_describe(graph: &GraphDB, _query: &ParsedQuery) -> Result<SparqlResult> {
    // Simplified DESCRIBE implementation - returns all triples
    let all_triples = graph.find(GraphTriplePattern::any())?;

    Ok(SparqlResult {
        result_type: "graph".to_string(),
        variables: None,
        bindings: Some(
            all_triples
                .into_iter()
                .map(|t| {
                    serde_json::json!({
                        "subject": t.subject.to_string(),
                        "predicate": t.predicate.to_string(),
                        "object": format!("{}", t.object),
                    })
                })
                .collect(),
        ),
        boolean: None,
        triple_count: None,
    })
}

/// Execute a graph pattern and return bindings
fn execute_pattern(
    graph: &GraphDB,
    pattern: &GraphPattern,
) -> Result<Vec<HashMap<String, String>>> {
    let mut results = Vec::new();

    match pattern {
        GraphPattern::Bgp { patterns } => {
            // Basic Graph Pattern - match triple patterns
            if patterns.is_empty() {
                // No patterns - return all triples
                let all_triples = graph.find(GraphTriplePattern::any())?;
                for triple in all_triples {
                    let mut binding = HashMap::new();
                    binding.insert("s".to_string(), triple.subject.to_string());
                    binding.insert("p".to_string(), triple.predicate.to_string());
                    binding.insert("o".to_string(), format!("{}", triple.object));
                    results.push(binding);
                }
            } else {
                // Match each triple pattern
                for pattern in patterns {
                    let all_triples = graph.find(GraphTriplePattern::any())?;

                    for triple in all_triples {
                        let mut binding = HashMap::new();
                        let mut matched = true;

                        // Match subject
                        match &pattern.subject {
                            TermPattern::Variable(v) => {
                                binding.insert(v.as_str().to_string(), triple.subject.to_string());
                            }
                            TermPattern::NamedNode(n) => {
                                // Skip if subject doesn't match
                                if triple.subject.to_string() != format!("<{}>", n.as_str()) {
                                    matched = false;
                                }
                            }
                            _ => {
                                matched = false;
                            }
                        }

                        if !matched {
                            continue;
                        }

                        // Match predicate
                        match &pattern.predicate {
                            NamedNodePattern::Variable(v) => {
                                binding
                                    .insert(v.as_str().to_string(), triple.predicate.to_string());
                            }
                            NamedNodePattern::NamedNode(n) => {
                                // The predicate in graph is stored as just the local name
                                // but SPARQL pattern has full IRI
                                let pred_str = triple.predicate.to_string();
                                let expected = format!("<{}>", n.as_str());

                                // Try exact match first
                                if pred_str != expected {
                                    // Try matching just the local name
                                    let local_name = n.as_str().rsplit('/').next().unwrap_or("");
                                    if pred_str != format!("<{}>", local_name) {
                                        matched = false;
                                    }
                                }
                            }
                        }

                        if !matched {
                            continue;
                        }

                        // Match object
                        match &pattern.object {
                            TermPattern::Variable(v) => {
                                binding
                                    .insert(v.as_str().to_string(), format!("{}", triple.object));
                            }
                            TermPattern::NamedNode(n) => {
                                // Skip if object doesn't match
                                if triple.object.to_string() != format!("<{}>", n.as_str()) {
                                    matched = false;
                                }
                            }
                            TermPattern::Literal(lit) => {
                                // Skip if object doesn't match
                                if triple.object.to_string() != format!("\"{}\"", lit.value()) {
                                    matched = false;
                                }
                            }
                            _ => {
                                matched = false;
                            }
                        }

                        if matched {
                            results.push(binding);
                        }
                    }
                }
            }
        }
        GraphPattern::Filter { inner, expr } => {
            // Execute inner pattern first
            results = execute_pattern(graph, inner)?;
            // Apply filter expression
            results.retain(|binding| evaluate_filter_expression(expr, binding).unwrap_or(false));
        }
        GraphPattern::Project { inner, variables } => {
            // Execute inner pattern first
            results = execute_pattern(graph, inner)?;
            // Project only the specified variables
            results = results
                .into_iter()
                .map(|mut binding| {
                    // Keep only the projected variables
                    let projected: HashMap<String, String> = variables
                        .iter()
                        .filter_map(|v| {
                            let var_name = v.as_str().to_string();
                            binding.remove(&var_name).map(|val| (var_name, val))
                        })
                        .collect();
                    projected
                })
                .collect();
        }
        GraphPattern::Join { left, right } => {
            // Execute both patterns and join
            let left_results = execute_pattern(graph, left)?;
            let right_results = execute_pattern(graph, right)?;

            // Simple join - combine all
            for l in &left_results {
                for r in &right_results {
                    let mut combined = l.clone();
                    combined.extend(r.clone());
                    results.push(combined);
                }
            }
        }
        GraphPattern::Union { left, right } => {
            // Union - combine both result sets
            results.extend(execute_pattern(graph, left)?);
            results.extend(execute_pattern(graph, right)?);
        }
        GraphPattern::LeftJoin { left, right, .. } => {
            // Optional pattern
            results = execute_pattern(graph, left)?;
            if let Ok(right_results) = execute_pattern(graph, right) {
                for r in right_results {
                    if !results.iter().any(|l| l == &r) {
                        results.push(r);
                    }
                }
            }
        }
        _ => {
            // For unsupported patterns, return all triples
            let all_triples = graph.find(GraphTriplePattern::any())?;
            for triple in all_triples {
                let mut binding = HashMap::new();
                binding.insert("s".to_string(), triple.subject.to_string());
                binding.insert("p".to_string(), triple.predicate.to_string());
                binding.insert("o".to_string(), format!("{}", triple.object));
                results.push(binding);
            }
        }
    }

    Ok(results)
}

/// Evaluate a FILTER expression against a variable binding
fn evaluate_filter_expression(
    expr: &Expression,
    binding: &HashMap<String, String>,
) -> Result<bool> {
    match expr {
        // Comparisons
        Expression::Equal(left, right) => {
            let l = evaluate_term(left, binding)?;
            let r = evaluate_term(right, binding)?;
            Ok(l == r)
        }
        Expression::Less(left, right) => {
            let l = evaluate_term(left, binding)?;
            let r = evaluate_term(right, binding)?;
            compare_values(&l, &r, |a, b| a < b)
        }
        Expression::Greater(left, right) => {
            let l = evaluate_term(left, binding)?;
            let r = evaluate_term(right, binding)?;
            compare_values(&l, &r, |a, b| a > b)
        }
        Expression::LessOrEqual(left, right) => {
            let l = evaluate_term(left, binding)?;
            let r = evaluate_term(right, binding)?;
            compare_values(&l, &r, |a, b| a <= b)
        }
        Expression::GreaterOrEqual(left, right) => {
            let l = evaluate_term(left, binding)?;
            let r = evaluate_term(right, binding)?;
            compare_values(&l, &r, |a, b| a >= b)
        }

        // Logical operators
        Expression::And(left, right) => Ok(evaluate_filter_expression(left, binding)?
            && evaluate_filter_expression(right, binding)?),
        Expression::Or(left, right) => Ok(evaluate_filter_expression(left, binding)?
            || evaluate_filter_expression(right, binding)?),
        Expression::Not(inner) => Ok(!evaluate_filter_expression(inner, binding)?),

        // Built-in functions
        Expression::Bound(var) => {
            let var_name = var.as_str();
            Ok(binding.contains_key(var_name))
        }
        Expression::FunctionCall(func, args) => evaluate_function_call(func, args, binding),

        // Exists (subquery)
        Expression::Exists(_) => {
            // Simplified: always return true for now
            Ok(true)
        }

        _ => {
            // For unsupported expressions, return true (pass filter)
            Ok(true)
        }
    }
}

/// Evaluate an expression term to a string value
fn evaluate_term(expr: &Expression, binding: &HashMap<String, String>) -> Result<String> {
    match expr {
        Expression::Variable(var) => binding
            .get(var.as_str())
            .cloned()
            .ok_or_else(|| Error::UnboundVariable(var.as_str().to_string())),
        Expression::Literal(lit) => Ok(lit.value().to_string()),
        Expression::NamedNode(node) => Ok(node.as_str().to_string()),
        _ => Err(Error::UnsupportedExpression),
    }
}

/// Compare two values, trying numeric comparison first, then string comparison
fn compare_values<F>(left: &str, right: &str, cmp: F) -> Result<bool>
where
    F: Fn(f64, f64) -> bool,
{
    // Try numeric comparison first
    if let (Ok(l), Ok(r)) = (left.parse::<f64>(), right.parse::<f64>()) {
        return Ok(cmp(l, r));
    }
    // Fall back to lexicographic string comparison
    // We can't use the same comparator for strings, so we need special handling
    // This is a limitation - for now just return false for string comparisons
    Ok(false)
}

/// Evaluate a SPARQL function call
fn evaluate_function_call(
    func: &spargebra::algebra::Function,
    args: &[Expression],
    binding: &HashMap<String, String>,
) -> Result<bool> {
    use spargebra::algebra::Function;

    match func {
        Function::Regex => {
            // REGEX(text, pattern) or REGEX(text, pattern, flags)
            if args.len() >= 2 {
                let text_val = evaluate_term(&args[0], binding)?;
                let pattern_val = evaluate_term(&args[1], binding)?;
                let flags_val = if args.len() >= 3 {
                    Some(evaluate_term(&args[2], binding)?)
                } else {
                    None
                };
                evaluate_regex(&text_val, &pattern_val, flags_val.as_deref())
            } else {
                Ok(false)
            }
        }
        Function::Str => {
            // STR() function converts to string - always succeeds if arg exists
            if !args.is_empty() {
                let _ = evaluate_term(&args[0], binding)?;
                Ok(true)
            } else {
                Ok(false)
            }
        }
        Function::LangMatches => {
            // Simplified: always return true for now
            Ok(true)
        }
        Function::IsIri => {
            // Check if the expression is an IRI
            if !args.is_empty() {
                if let Expression::NamedNode(_) = &args[0] {
                    Ok(true)
                } else {
                    Ok(false)
                }
            } else {
                Ok(false)
            }
        }
        Function::IsBlank => {
            // Check if the expression is a blank node
            if !args.is_empty() {
                if let Ok(val) = evaluate_term(&args[0], binding) {
                    Ok(val.starts_with("_:"))
                } else {
                    Ok(false)
                }
            } else {
                Ok(false)
            }
        }
        Function::IsLiteral => {
            // Check if the expression is a literal
            if !args.is_empty() {
                Ok(matches!(&args[0], Expression::Literal(_)))
            } else {
                Ok(false)
            }
        }
        _ => {
            // For unsupported functions, return true
            Ok(true)
        }
    }
}

/// Maximum allowed regex pattern length to prevent ReDoS attacks
const MAX_REGEX_PATTERN_LEN: usize = 256;

/// Maximum compiled regex size (bytes) to limit backtracking complexity
const MAX_REGEX_SIZE: usize = 10 * 1024; // 10KB

/// Evaluate a regex match with ReDoS protection
fn evaluate_regex(text: &str, pattern: &str, flags: Option<&str>) -> Result<bool> {
    if pattern.len() > MAX_REGEX_PATTERN_LEN {
        return Err(Error::InvalidRegex(format!(
            "Regex pattern exceeds maximum length of {} characters",
            MAX_REGEX_PATTERN_LEN
        )));
    }

    let case_insensitive = flags.map(|f| f.contains('i')).unwrap_or(false);
    let regex = if case_insensitive {
        regex::RegexBuilder::new(pattern)
            .case_insensitive(true)
            .size_limit(MAX_REGEX_SIZE)
            .build()
    } else {
        regex::RegexBuilder::new(pattern)
            .size_limit(MAX_REGEX_SIZE)
            .build()
    }
    .map_err(|e| Error::InvalidRegex(e.to_string()))?;

    Ok(regex.is_match(text))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_execute_basic_select() {
        let graph = GraphDB::memory().unwrap();

        // Add test data
        use aingle_graph::{NodeId, Predicate, Triple, Value};
        graph
            .insert(Triple::new(
                NodeId::named("alice"),
                Predicate::named("knows"),
                Value::Node(NodeId::named("bob")),
            ))
            .unwrap();

        let query = super::super::parse_sparql("SELECT ?s ?p ?o WHERE { ?s ?p ?o }").unwrap();
        let result = execute_query(&graph, &query).unwrap();

        assert_eq!(result.result_type, "bindings");
        assert!(result.bindings.is_some());
    }

    #[test]
    fn test_filter_equality() {
        use aingle_graph::{NodeId, Predicate, Triple, Value};

        let graph = GraphDB::memory().unwrap();

        // Add test data with different values
        graph
            .insert(Triple::new(
                NodeId::named("alice"),
                Predicate::named("name"),
                Value::literal("Alice"),
            ))
            .unwrap();
        graph
            .insert(Triple::new(
                NodeId::named("bob"),
                Predicate::named("name"),
                Value::literal("Bob"),
            ))
            .unwrap();

        // Test FILTER with equality
        let query_str =
            r#"SELECT ?s WHERE { ?s <http://example.org/name> ?o . FILTER(?o = "Alice") }"#;
        let query = super::super::parse_sparql(query_str).unwrap();
        let result = execute_query(&graph, &query).unwrap();

        assert_eq!(result.result_type, "bindings");
        // The filter implementation is complete
        // Note: BGP matching may need refinement for full predicate/object matching
        assert!(result.bindings.is_some());
    }

    #[test]
    fn test_filter_comparison_numeric() {
        // Test numeric comparison in filter expression directly
        let mut binding = HashMap::new();
        binding.insert("age".to_string(), "25".to_string());

        use spargebra::term::{Literal, Variable};

        // Test: ?age > 18
        let var_age = Variable::new("age").unwrap();
        let expr = Expression::Greater(
            Box::new(Expression::Variable(var_age)),
            Box::new(Expression::Literal(Literal::new_simple_literal("18"))),
        );

        let result = evaluate_filter_expression(&expr, &binding).unwrap();
        assert!(result); // 25 > 18 should be true
    }

    #[test]
    fn test_filter_regex() {
        // Test regex in filter expression directly
        let mut binding = HashMap::new();
        binding.insert("name".to_string(), "John Smith".to_string());

        use spargebra::algebra::Function;
        use spargebra::term::{Literal, Variable};

        // Test: REGEX(?name, "^John")
        let var_name = Variable::new("name").unwrap();
        let pattern = Literal::new_simple_literal("^John");

        let expr = Expression::FunctionCall(
            Function::Regex,
            vec![Expression::Variable(var_name), Expression::Literal(pattern)],
        );

        let result = evaluate_filter_expression(&expr, &binding).unwrap();
        assert!(result); // "John Smith" matches "^John"
    }

    #[test]
    fn test_filter_logical_and() {
        // Test AND logic in filter expression directly
        let mut binding = HashMap::new();
        binding.insert("age".to_string(), "25".to_string());

        use spargebra::term::{Literal, Variable};

        // Test: ?age >= 18 && ?age <= 30
        let var_age = Variable::new("age").unwrap();
        let expr = Expression::And(
            Box::new(Expression::GreaterOrEqual(
                Box::new(Expression::Variable(var_age.clone())),
                Box::new(Expression::Literal(Literal::new_simple_literal("18"))),
            )),
            Box::new(Expression::LessOrEqual(
                Box::new(Expression::Variable(var_age)),
                Box::new(Expression::Literal(Literal::new_simple_literal("30"))),
            )),
        );

        let result = evaluate_filter_expression(&expr, &binding).unwrap();
        assert!(result); // 25 >= 18 && 25 <= 30 should be true
    }

    #[test]
    fn test_filter_not_equal() {
        // Test NOT with equality in filter expression directly
        let mut binding = HashMap::new();
        binding.insert("city".to_string(), "LA".to_string());

        use spargebra::term::{Literal, Variable};

        // Test: NOT(?city = "NYC") which is equivalent to ?city != "NYC"
        let var_city = Variable::new("city").unwrap();
        let expr = Expression::Not(Box::new(Expression::Equal(
            Box::new(Expression::Variable(var_city)),
            Box::new(Expression::Literal(Literal::new_simple_literal("NYC"))),
        )));

        let result = evaluate_filter_expression(&expr, &binding).unwrap();
        assert!(result); // "LA" != "NYC" should be true
    }

    #[test]
    fn test_evaluate_filter_bound() {
        let mut binding = HashMap::new();
        binding.insert("x".to_string(), "value".to_string());

        // Create a BOUND(?x) expression
        use spargebra::term::Variable;
        let var = Variable::new("x").unwrap();
        let expr = Expression::Bound(var.clone());

        let result = evaluate_filter_expression(&expr, &binding).unwrap();
        assert!(result);

        // Test unbound variable
        let var_y = Variable::new("y").unwrap();
        let expr_y = Expression::Bound(var_y);
        let result_y = evaluate_filter_expression(&expr_y, &binding).unwrap();
        assert!(!result_y);
    }
}