pub trait ServiceHandler: Send + Sync {
type Error: Error + Send + Sync + 'static;
// Required method
fn handle(
&self,
pattern: GraphPattern,
base_iri: Option<String>,
) -> Result<QuerySolutionIter, 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 oxrdf::{Dataset, Literal, NamedNode, Variable};
use sparesults::QuerySolution;
use spareval::{QueryEvaluator, QueryResults, QuerySolutionIter, ServiceHandler};
use spargebra::algebra::GraphPattern;
use spargebra::Query;
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<String>,
) -> Result<QuerySolutionIter, 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 = Query::parse(
"SELECT ?foo WHERE { SERVICE <http://example.com/service> {} }",
None,
)?;
if let QueryResults::Solutions(mut solutions) = evaluator.execute(Dataset::new(), &query)? {
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<String>,
) -> Result<QuerySolutionIter, Self::Error>
fn handle( &self, pattern: GraphPattern, base_iri: Option<String>, ) -> Result<QuerySolutionIter, Self::Error>
Evaluates a Query
against the service.