Skip to content

Commit

Permalink
implement SPARQL Ask
Browse files Browse the repository at this point in the history
  • Loading branch information
pchampin committed Nov 22, 2024
1 parent d02fa2c commit 00d3cf9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
10 changes: 10 additions & 0 deletions sparql/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ impl<'a, D: Dataset> ExecState<'a, D> {
}
}

pub fn ask(
&mut self,
pattern: &GraphPattern,
graph_matcher: &[Option<ArcTerm>],
binding: Option<&Binding>,
) -> Result<bool, SparqlWrapperError<D::Error>> {
self.select(pattern, graph_matcher, binding)
.map(|binding| binding.into_iter().next().is_some())
}

fn bgp(
&mut self,
patterns: &[TriplePattern],
Expand Down
15 changes: 12 additions & 3 deletions sparql/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,24 @@ use test_case::test_case;
vec!["<http://schema.org/name>", "<http://schema.org/name>", "<http://schema.org/performerIn>", "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>", "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>", ];
"predicates"
)]
fn test_select_1(query: &str, exp: Vec<&str>) -> TestResult {
#[test_case(
"SELECT ?x { ?x ?y \"not in the repo\" }",
vec![];
"no result"
)]
fn test_select_1_and_ask(query: &str, exp: Vec<&str>) -> TestResult {
let dataset = dataset_101()?;
let dataset = SparqlWrapper(&dataset);
let query = SparqlQuery::parse(query)?;
let bindings = dataset.query(&query)?.into_bindings();
let parsed_query = SparqlQuery::parse(query)?;
let bindings = dataset.query(&parsed_query)?.into_bindings();
assert_eq!(bindings.variables(), &["x"]);
let mut got = bindings_to_vec(bindings);
got.sort();
assert_eq!(exp, got);

let parsed_query = SparqlQuery::parse(&query.replace("SELECT ?x", "ASK"))?;
let response = dataset.query(&parsed_query)?.into_boolean();
assert_eq!(response, !exp.is_empty());
Ok(())
}

Expand Down
7 changes: 6 additions & 1 deletion sparql/src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ impl<'a, D: Dataset> SparqlDataset for SparqlWrapper<'a, D> {
dataset,
pattern,
base_iri,
} => Err(SparqlWrapperError::NotImplemented("ASK query")),
} => {
let mut exec = ExecState::new(self.0, dataset)?;
let cfg = exec.config_cloned();
exec.ask(pattern, &cfg.default_matcher, None)
.map(SparqlResult::Boolean)
}
}
}
}
Expand Down

0 comments on commit 00d3cf9

Please sign in to comment.