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(); NamedNode::from_str("http://www.w3.org/1999/02/22-rdf-syntax-ns#subject").unwrap();
pub static ref TYPE: NamedNode = pub static ref TYPE: NamedNode =
NamedNode::from_str("http://www.w3.org/1999/02/22-rdf-syntax-ns#type").unwrap(); 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 = pub static ref XML_LITERAL: NamedNode =
NamedNode::from_str("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral").unwrap(); 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> { fn eval_plan<'a>(&self, node: &'a PlanNode, from: EncodedTuple) -> EncodedTuplesIterator<'a> {
match node { match node {
PlanNode::Init => Box::new(once(Ok(from))), PlanNode::Init => Box::new(once(Ok(from))),

@ -43,6 +43,14 @@ impl<S: EncodedQuadsStore> SparqlDataset for StoreDataset<S> {
evaluator: SimpleEvaluator::new(store), 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!(), _ => unimplemented!(),
}) })
} }
@ -54,6 +62,10 @@ pub enum SimplePreparedQuery<S: EncodedQuadsStore> {
variables: Vec<Variable>, variables: Vec<Variable>,
evaluator: SimpleEvaluator<S>, evaluator: SimpleEvaluator<S>,
}, },
Ask {
plan: PlanNode,
evaluator: SimpleEvaluator<S>,
},
} }
impl<S: EncodedQuadsStore> PreparedQuery for SimplePreparedQuery<S> { impl<S: EncodedQuadsStore> PreparedQuery for SimplePreparedQuery<S> {
@ -64,6 +76,7 @@ impl<S: EncodedQuadsStore> PreparedQuery for SimplePreparedQuery<S> {
variables, variables,
evaluator, evaluator,
} => evaluator.evaluate_select_plan(&plan, &variables), } => 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![ let manifest_10_urls = vec![
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/algebra/manifest.ttl") Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/algebra/manifest.ttl")
.unwrap(), .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") Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/basic/manifest.ttl")
.unwrap(), .unwrap(),
Url::parse( Url::parse(
@ -111,6 +113,8 @@ fn sparql_w3c_query_evaluation_testsuite() {
.unwrap(), .unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/triple-match/manifest.ttl") Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/triple-match/manifest.ttl")
.unwrap(), .unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/type-promotion/manifest.ttl")
.unwrap(),
]; ];
let test_blacklist = vec![ let test_blacklist = vec![
//Multiple writing of the same xsd:integer. Our system does strong normalization. //Multiple writing of the same xsd:integer. Our system does strong normalization.
@ -320,13 +324,30 @@ mod rs {
pub static ref INDEX: NamedNode = pub static ref INDEX: NamedNode =
NamedNode::from_str("http://www.w3.org/2001/sw/DataAccess/tests/result-set#index") NamedNode::from_str("http://www.w3.org/2001/sw/DataAccess/tests/result-set#index")
.unwrap(); .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> { fn to_graph(result: QueryResult) -> Result<MemoryGraph> {
match result { match result {
QueryResult::Graph(graph) => Ok(graph), 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) => { QueryResult::Bindings(bindings) => {
let graph = MemoryGraph::default(); let graph = MemoryGraph::default();
let result_set = BlankNode::default(); let result_set = BlankNode::default();

Loading…
Cancel
Save