pub struct QueryEvaluator { /* private fields */ }
Expand description
Evaluates a query against a given RDF dataset
Note that this evaluator does not handle the FROM
and FROM NAMED
part of the query.
You must select the proper dataset before using this struct.
To adapt this software to work on your own RDF dataset, you need to implement the QueryableDataset
trait.
use oxrdf::{Dataset, GraphName, NamedNode, Quad};
use spareval::{QueryEvaluator, QueryResults};
use spargebra::Query;
let ex = NamedNode::new("http://example.com")?;
let dataset = Dataset::from_iter([Quad::new(
ex.clone(),
ex.clone(),
ex.clone(),
GraphName::DefaultGraph,
)]);
let query = Query::parse("SELECT * WHERE { ?s ?p ?o }", None)?;
let results = QueryEvaluator::new().execute(dataset, &query);
if let QueryResults::Solutions(solutions) = results? {
let solutions = solutions.collect::<Result<Vec<_>, _>>()?;
assert_eq!(solutions.len(), 1);
assert_eq!(solutions[0]["s"], ex.into());
}
Implementations§
Source§impl QueryEvaluator
impl QueryEvaluator
pub fn new() -> Self
pub fn execute( &self, dataset: impl QueryableDataset, query: &Query, ) -> Result<QueryResults, QueryEvaluationError>
Sourcepub fn execute_with_substituted_variables(
&self,
dataset: impl QueryableDataset,
query: &Query,
substitutions: impl IntoIterator<Item = (Variable, Term)>,
) -> Result<QueryResults, QueryEvaluationError>
pub fn execute_with_substituted_variables( &self, dataset: impl QueryableDataset, query: &Query, substitutions: impl IntoIterator<Item = (Variable, Term)>, ) -> Result<QueryResults, QueryEvaluationError>
Executes a SPARQL query while substituting some variables with the given values.
Substitution follows RDF-dev SEP-0007.
use oxrdf::{Dataset, GraphName, NamedNode, Quad, Variable};
use spareval::{QueryEvaluator, QueryResults};
use spargebra::Query;
let ex = NamedNode::new("http://example.com")?;
let dataset = Dataset::from_iter([Quad::new(
ex.clone(),
ex.clone(),
ex.clone(),
GraphName::DefaultGraph,
)]);
let query = Query::parse("SELECT * WHERE { ?s ?p ?o }", None)?;
let results = QueryEvaluator::new().execute_with_substituted_variables(
dataset,
&query,
[(Variable::new("s")?, ex.clone().into())],
);
if let QueryResults::Solutions(solutions) = results? {
let solutions = solutions.collect::<Result<Vec<_>, _>>()?;
assert_eq!(solutions.len(), 1);
assert_eq!(solutions[0]["s"], ex.into());
}
pub fn explain( &self, dataset: impl QueryableDataset, query: &Query, ) -> (Result<QueryResults, QueryEvaluationError>, QueryExplanation)
pub fn explain_with_substituted_variables( &self, dataset: impl QueryableDataset, query: &Query, substitutions: impl IntoIterator<Item = (Variable, Term)>, ) -> (Result<QueryResults, QueryEvaluationError>, QueryExplanation)
Sourcepub fn with_service_handler(
self,
service_name: impl Into<NamedNode>,
handler: impl ServiceHandler + 'static,
) -> Self
pub fn with_service_handler( self, service_name: impl Into<NamedNode>, handler: impl ServiceHandler + 'static, ) -> Self
Use a given ServiceHandler
to execute SPARQL 1.1 Federated Query SERVICE calls.
See ServiceHandler
for an example.
Sourcepub fn with_default_service_handler(
self,
handler: impl DefaultServiceHandler + 'static,
) -> Self
pub fn with_default_service_handler( self, handler: impl DefaultServiceHandler + 'static, ) -> Self
Use a given DefaultServiceHandler
to execute SPARQL 1.1 Federated Query SERVICE calls if no explicit service handler is defined for the service.
See DefaultServiceHandler
for an example.
pub fn has_default_service_handler(&self) -> bool
Sourcepub fn with_custom_function(
self,
name: NamedNode,
evaluator: impl Fn(&[Term]) -> Option<Term> + Send + Sync + 'static,
) -> Self
pub fn with_custom_function( self, name: NamedNode, evaluator: impl Fn(&[Term]) -> Option<Term> + Send + Sync + 'static, ) -> Self
Adds a custom SPARQL evaluation function.
Example with a function serializing terms to N-Triples:
use oxrdf::{Dataset, Literal, NamedNode};
use spareval::{QueryEvaluator, QueryResults};
use spargebra::Query;
let evaluator = QueryEvaluator::new().with_custom_function(
NamedNode::new("http://www.w3.org/ns/formats/N-Triples")?,
|args| args.get(0).map(|t| Literal::from(t.to_string()).into()),
);
let query = Query::parse(
"SELECT (<http://www.w3.org/ns/formats/N-Triples>(1) AS ?nt) WHERE {}",
None,
)?;
if let QueryResults::Solutions(mut solutions) = evaluator.execute(Dataset::new(), &query)? {
assert_eq!(
solutions.next().unwrap()?.get("nt"),
Some(&Literal::from("\"1\"^^<http://www.w3.org/2001/XMLSchema#integer>").into())
);
}
Sourcepub fn without_optimizations(self) -> Self
pub fn without_optimizations(self) -> Self
Disables query optimizations and runs the query as it is.
Sourcepub fn compute_statistics(self) -> Self
pub fn compute_statistics(self) -> Self
Compute statistics during evaluation and fills them in the explanation tree.
Trait Implementations§
Source§impl Clone for QueryEvaluator
impl Clone for QueryEvaluator
Source§fn clone(&self) -> QueryEvaluator
fn clone(&self) -> QueryEvaluator
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more