tests working

pull/11/head
Dustin Whitney 5 years ago
parent 75c786b350
commit 5912e4c40b
  1. 5
      lib/src/lib.rs
  2. 11
      lib/src/repository.rs
  3. 27
      lib/src/sparql/eval.rs
  4. 41
      lib/src/sparql/mod.rs
  5. 5
      lib/src/store/memory.rs
  6. 3
      lib/src/store/mod.rs
  7. 2
      lib/tests/service_test_cases.rs
  8. 2
      lib/tests/sparql_test_cases.rs

@ -27,8 +27,9 @@
//! assert_eq!(vec![quad], results.unwrap());
//!
//! // SPARQL query
//! let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", QueryOptions::default()).unwrap();
//! let results = prepared_query.exec().unwrap();
//! let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", None).unwrap();
//! let options = QueryOptions::default();
//! let results = prepared_query.exec(&options).unwrap();
//! if let QueryResult::Bindings(results) = results {
//! assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into()));
//! }

@ -30,8 +30,9 @@ use std::io::BufRead;
/// assert_eq!(vec![quad], results.unwrap());
///
/// // SPARQL query
/// let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", QueryOptions::default()).unwrap();
/// let results = prepared_query.exec().unwrap();
/// let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", None).unwrap();
/// let options = QueryOptions::default();
/// let results = prepared_query.exec(&options).unwrap();
/// if let QueryResult::Bindings(results) = results {
/// assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into()));
/// }
@ -75,8 +76,9 @@ pub trait RepositoryConnection: Clone {
/// connection.insert(&Quad::new(ex.clone(), ex.clone(), ex.clone(), None));
///
/// // SPARQL query
/// let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", QueryOptions::default()).unwrap();
/// let results = prepared_query.exec().unwrap();
/// let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", None).unwrap();
/// let options = QueryOptions::default();
/// let results = prepared_query.exec(&options).unwrap();
/// if let QueryResult::Bindings(results) = results {
/// assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into()));
/// }
@ -87,6 +89,7 @@ pub trait RepositoryConnection: Clone {
fn prepare_query_from_pattern<'a>(
&'a self,
graph_pattern: &'a GraphPattern,
base_iri: Option<&str>,
) -> Result<Self::PreparedQuery>;
/// Retrieves quads with a filter on each quad component

@ -40,15 +40,17 @@ type EncodedTuplesIterator<'a> = Box<dyn Iterator<Item = Result<EncodedTuple>> +
pub struct SimpleEvaluator<S: StoreConnection> {
dataset: DatasetView<S>,
base_iri: Option<Iri<String>>,
bnodes_map: Mutex<BTreeMap<u128, u128>>,
now: DateTime<FixedOffset>,
}
impl<'a, S: StoreConnection + 'a> SimpleEvaluator<S> {
pub fn new(dataset: DatasetView<S>) -> Self {
pub fn new(dataset: DatasetView<S>, base_iri: Option<Iri<String>>) -> Self {
Self {
dataset,
bnodes_map: Mutex::new(BTreeMap::default()),
base_iri,
now: Utc::now().with_timezone(&FixedOffset::east(0)),
}
}
@ -948,22 +950,15 @@ impl<'a, S: StoreConnection + 'a> SimpleEvaluator<S> {
_ => None,
}?;
let iri = self.dataset.get_str(iri_id).ok()??;
match options.base_iri {
None => {
Iri::parse(iri).ok()?;
Some(EncodedTerm::NamedNode { iri_id })
},
Some(str_iri) => {
match Iri::parse(str_iri) {
Ok(base_iri) => self.build_named_node(&base_iri.resolve(&iri).ok()?.into_inner()),
_ => {
Iri::parse(iri).ok()?;
Some(EncodedTerm::NamedNode { iri_id })
}
}
}
let base_iri = options.base_iri
.map(|base_iri| Iri::parse(base_iri.to_string()))
.or(self.base_iri.as_ref().map(|iri| Ok(iri.clone())));
if let Some(Ok(base_iri)) = base_iri {
self.build_named_node(&base_iri.resolve(&iri).ok()?.into_inner())
} else {
Iri::parse(iri).ok()?;
Some(EncodedTerm::NamedNode { iri_id })
}
}
PlanExpression::BNode(id) => match id {
Some(id) => {

@ -18,6 +18,8 @@ use crate::sparql::plan::{DatasetView, PlanNode};
use crate::sparql::plan_builder::PlanBuilder;
use crate::store::StoreConnection;
use crate::Result;
use failure::format_err;
use rio_api::iri::Iri;
use std::fmt;
pub use crate::sparql::algebra::GraphPattern;
@ -64,31 +66,31 @@ impl<'a, S: StoreConnection + 'a> SimplePreparedQuery<S> {
QueryVariants::Select {
algebra,
dataset: _,
..
base_iri,
} => {
let (plan, variables) = PlanBuilder::build(dataset.encoder(), &algebra)?;
SimplePreparedQueryAction::Select {
plan,
variables,
evaluator: SimpleEvaluator::new(dataset),
evaluator: SimpleEvaluator::new(dataset, base_iri),
}
}
QueryVariants::Ask {
algebra,
dataset: _,
..
base_iri,
} => {
let (plan, _) = PlanBuilder::build(dataset.encoder(), &algebra)?;
SimplePreparedQueryAction::Ask {
plan,
evaluator: SimpleEvaluator::new(dataset),
evaluator: SimpleEvaluator::new(dataset, base_iri),
}
}
QueryVariants::Construct {
construct,
algebra,
dataset: _,
..
base_iri,
} => {
let (plan, variables) = PlanBuilder::build(dataset.encoder(), &algebra)?;
SimplePreparedQueryAction::Construct {
@ -98,18 +100,18 @@ impl<'a, S: StoreConnection + 'a> SimplePreparedQuery<S> {
&construct,
variables,
)?,
evaluator: SimpleEvaluator::new(dataset),
evaluator: SimpleEvaluator::new(dataset, base_iri),
}
}
QueryVariants::Describe {
algebra,
dataset: _,
..
base_iri,
} => {
let (plan, _) = PlanBuilder::build(dataset.encoder(), &algebra)?;
SimplePreparedQueryAction::Describe {
plan,
evaluator: SimpleEvaluator::new(dataset),
evaluator: SimpleEvaluator::new(dataset, base_iri),
}
}
}))
@ -118,14 +120,27 @@ impl<'a, S: StoreConnection + 'a> SimplePreparedQuery<S> {
pub(crate) fn new_from_pattern(
connection: S,
pattern: &GraphPattern,
base_iri: Option<&'a str>
) -> Result<Self> {
let dataset = DatasetView::new(connection);
let (plan, variables) = PlanBuilder::build(dataset.encoder(), pattern)?;
Ok(Self(SimplePreparedQueryAction::Select {
plan,
variables,
evaluator: SimpleEvaluator::new(dataset),
}))
let base_iri = base_iri.map(|str_iri| Iri::parse(str_iri.to_string()));
match base_iri {
Some(Err(_)) => Err(format_err!("Failed to parse base_iri")),
Some(Ok(base_iri)) =>
Ok(Self(SimplePreparedQueryAction::Select {
plan,
variables,
evaluator: SimpleEvaluator::new(dataset, Some(base_iri)),
})),
None =>
Ok(Self(SimplePreparedQueryAction::Select {
plan,
variables,
evaluator: SimpleEvaluator::new(dataset, None),
}))
}
}
}

@ -29,8 +29,9 @@ use std::sync::{PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard};
/// assert_eq!(vec![quad], results.unwrap());
///
/// // SPARQL query
/// let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", QueryOptions::default()).unwrap();
/// let results = prepared_query.exec().unwrap();
/// let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", None).unwrap();
/// let options = QueryOptions::default();
/// let results = prepared_query.exec(&options).unwrap();
/// if let QueryResult::Bindings(results) = results {
/// assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into()));
/// }

@ -101,8 +101,9 @@ impl<S: StoreConnection> RepositoryConnection for StoreRepositoryConnection<S> {
fn prepare_query_from_pattern<'a>(
&'a self,
pattern: &GraphPattern,
base_iri: Option<&'a str>
) -> Result<Self::PreparedQuery> {
SimplePreparedQuery::new_from_pattern(self.inner.clone(), pattern) //TODO: avoid clone
SimplePreparedQuery::new_from_pattern(self.inner.clone(), pattern, base_iri) //TODO: avoid clone
}
fn load_graph(

@ -217,7 +217,7 @@ fn query_repository<'a>(repository: MemoryRepository, query: String, options: Qu
fn pattern_repository<'a>(repository: MemoryRepository, pattern: GraphPattern, options: QueryOptions<'a>) -> Result<BindingsIterator<'a>> {
let connection = repository.connection()?;
let prepared_query = connection.prepare_query_from_pattern(&pattern)?;
let prepared_query = connection.prepare_query_from_pattern(&pattern, None)?;
let result = prepared_query.exec(&options)?;
match result {
QueryResult::Bindings(iterator) => {

@ -164,7 +164,7 @@ fn sparql_w3c_query_evaluation_testsuite() -> Result<()> {
"Failure to parse query of {} with error: {}",
test, error
)),
Ok(query) => match query.exec(&QueryOptions::default().with_base_iri(&test.query)) {
Ok(query) => match query.exec(&QueryOptions::default()) {
Err(error) => Err(format_err!(
"Failure to execute query of {} with error: {}",
test, error

Loading…
Cancel
Save