pub trait ServiceHandler: Send + Sync {
type Error: Error + Send + Sync + 'static;
// Required method
fn handle(
&self,
pattern: &GraphPattern,
base_iri: Option<&Iri<String>>,
) -> Result<QuerySolutionIter<'static>, Self::Error>;
}Expand description
Handler for SPARQL 1.1 Federated Query SERVICEs.
Should be given to QueryOptions
before evaluating a SPARQL query that uses SERVICE calls.
Note that you can also use DefaultServiceHandler if you need to handle any service and not a specific one.
use oxiri::Iri;
use oxrdf::{Dataset, Literal, NamedNode, Variable};
use sparesults::QuerySolution;
use spareval::{QueryEvaluator, QueryResults, QuerySolutionIter, ServiceHandler};
use spargebra::SparqlParser;
use spargebra::algebra::GraphPattern;
use std::convert::Infallible;
use std::iter::once;
use std::sync::Arc;
struct TestServiceHandler {}
impl ServiceHandler for TestServiceHandler {
type Error = Infallible;
fn handle(
&self,
_pattern: &GraphPattern,
_base_iri: Option<&Iri<String>>,
) -> Result<QuerySolutionIter<'static>, Self::Error> {
// Always return a single binding foo -> 1
let variables = [Variable::new_unchecked("foo")].into();
Ok(QuerySolutionIter::new(
Arc::clone(&variables),
once(Ok(QuerySolution::from((
variables,
vec![Some(Literal::from(1).into())],
)))),
))
}
}
let evaluator = QueryEvaluator::default().with_service_handler(
NamedNode::new("http://example.com/service")?,
TestServiceHandler {},
);
let query = SparqlParser::new()
.parse_query("SELECT ?foo WHERE { SERVICE <http://example.com/service> {} }")?;
if let QueryResults::Solutions(mut solutions) =
evaluator.prepare(&query).execute(&Dataset::new())?
{
assert_eq!(
solutions.next().unwrap()?.get("foo"),
Some(&Literal::from(1).into())
);
}Required Associated Types§
Required Methods§
Sourcefn handle(
&self,
pattern: &GraphPattern,
base_iri: Option<&Iri<String>>,
) -> Result<QuerySolutionIter<'static>, Self::Error>
fn handle( &self, pattern: &GraphPattern, base_iri: Option<&Iri<String>>, ) -> Result<QuerySolutionIter<'static>, Self::Error>
Evaluates a Query against the service.