Adds support of SPARQL ASK queries

pull/10/head
Tpt 6 years ago
parent fc040a4e45
commit 6e2471de0b
  1. 2
      lib/src/model/vocab.rs
  2. 8
      lib/src/sparql/eval.rs
  3. 13
      lib/src/sparql/mod.rs
  4. 23
      lib/tests/sparql_test_cases.rs

@ -26,6 +26,8 @@ pub mod rdf {
NamedNode::from_str("http://www.w3.org/1999/02/22-rdf-syntax-ns#subject").unwrap();
pub static ref TYPE: NamedNode =
NamedNode::from_str("http://www.w3.org/1999/02/22-rdf-syntax-ns#type").unwrap();
pub static ref VALUE: NamedNode =
NamedNode::from_str("http://www.w3.org/1999/02/22-rdf-syntax-ns#value").unwrap();
pub static ref XML_LITERAL: NamedNode =
NamedNode::from_str("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral").unwrap();
}

@ -51,6 +51,14 @@ impl<S: EncodedQuadsStore> SimpleEvaluator<S> {
))
}
pub fn evaluate_ask_plan<'a>(&'a self, plan: &'a PlanNode) -> Result<QueryResult<'a>> {
match self.eval_plan(plan, vec![]).next() {
Some(Ok(_)) => Ok(QueryResult::Boolean(true)),
Some(Err(error)) => Err(error),
None => Ok(QueryResult::Boolean(false)),
}
}
fn eval_plan<'a>(&self, node: &'a PlanNode, from: EncodedTuple) -> EncodedTuplesIterator<'a> {
match node {
PlanNode::Init => Box::new(once(Ok(from))),

@ -43,6 +43,14 @@ impl<S: EncodedQuadsStore> SparqlDataset for StoreDataset<S> {
evaluator: SimpleEvaluator::new(store),
}
}
Query::Ask { algebra, dataset } => {
let store = self.encoded();
let (plan, _) = PlanBuilder::build(&*store, &algebra)?;
SimplePreparedQuery::Ask {
plan,
evaluator: SimpleEvaluator::new(store),
}
}
_ => unimplemented!(),
})
}
@ -54,6 +62,10 @@ pub enum SimplePreparedQuery<S: EncodedQuadsStore> {
variables: Vec<Variable>,
evaluator: SimpleEvaluator<S>,
},
Ask {
plan: PlanNode,
evaluator: SimpleEvaluator<S>,
},
}
impl<S: EncodedQuadsStore> PreparedQuery for SimplePreparedQuery<S> {
@ -64,6 +76,7 @@ impl<S: EncodedQuadsStore> PreparedQuery for SimplePreparedQuery<S> {
variables,
evaluator,
} => evaluator.evaluate_select_plan(&plan, &variables),
SimplePreparedQuery::Ask { plan, evaluator } => evaluator.evaluate_ask_plan(&plan),
}
}
}

@ -83,6 +83,8 @@ fn sparql_w3c_query_evaluation_testsuite() {
let manifest_10_urls = vec![
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/algebra/manifest.ttl")
.unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/ask/manifest.ttl")
.unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/basic/manifest.ttl")
.unwrap(),
Url::parse(
@ -111,6 +113,8 @@ fn sparql_w3c_query_evaluation_testsuite() {
.unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/triple-match/manifest.ttl")
.unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/type-promotion/manifest.ttl")
.unwrap(),
];
let test_blacklist = vec![
//Multiple writing of the same xsd:integer. Our system does strong normalization.
@ -320,13 +324,30 @@ mod rs {
pub static ref INDEX: NamedNode =
NamedNode::from_str("http://www.w3.org/2001/sw/DataAccess/tests/result-set#index")
.unwrap();
pub static ref BOOLEAN: NamedNode =
NamedNode::from_str("http://www.w3.org/2001/sw/DataAccess/tests/result-set#boolean")
.unwrap();
}
}
fn to_graph(result: QueryResult) -> Result<MemoryGraph> {
match result {
QueryResult::Graph(graph) => Ok(graph),
QueryResult::Boolean(_) => unimplemented!(),
QueryResult::Boolean(value) => {
let graph = MemoryGraph::default();
let result_set = BlankNode::default();
graph.insert(&Triple::new(
result_set.clone(),
rdf::TYPE.clone(),
rs::RESULT_SET.clone(),
))?;
graph.insert(&Triple::new(
result_set.clone(),
rs::BOOLEAN.clone(),
Literal::from(value),
))?;
Ok(graph)
}
QueryResult::Bindings(bindings) => {
let graph = MemoryGraph::default();
let result_set = BlankNode::default();

Loading…
Cancel
Save