-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathquery.rs
More file actions
41 lines (33 loc) · 1.05 KB
/
query.rs
File metadata and controls
41 lines (33 loc) · 1.05 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
extern crate alloc;
use alloc::vec::Vec;
use crate::{pattern::Pattern, solutions::Solutions, traits::queryable::Queryable};
pub struct Query {
patterns: Vec<Pattern>,
}
impl Query {
pub fn new(patterns: Vec<Pattern>) -> Self {
Self { patterns }
}
/// Executes the query on the given graph.
///
/// If the query nas no patterns, it returns a single empty solution as per
/// SPARQL 1.1 Empty Group Pattern.
pub fn execute<Q: Queryable>(&self, queryable: &Q) -> Solutions {
if self.empty() {
return Solutions::empty();
}
let solutions: Vec<_> = self
.patterns
.iter()
.flat_map(|pattern| {
let statements = pattern.execute(queryable);
statements.filter_map(|res| res.ok().map(|stmt| pattern.solution(stmt)))
})
.collect();
Solutions::new(solutions.into_iter())
}
/// Returns true if the query has no patterns.
pub fn empty(&self) -> bool {
self.patterns.is_empty()
}
}