Struct SparqlEvaluator
pub struct SparqlEvaluator {
parser: SparqlParser,
inner: QueryEvaluator,
}rdf only.Expand description
SPARQL evaluator.
It supports SPARQL 1.1 query and SPARQL 1.1 update.
If the "http-client" optional feature is enabled,
a simple HTTP 1.1 client is used to execute SPARQL 1.1 Federated Query SERVICE calls.
Usage example disabling the federated query support:
use oxigraph::model::NamedNode;
use oxigraph::sparql::SparqlEvaluator;
use oxigraph::store::Store;
SparqlEvaluator::new()
.with_custom_function(NamedNode::new("http://example.com/identity")?, |args| {
args.get(0).cloned()
})
.parse_query("SELECT (<http://example.com/identity>('foo') AS ?r) WHERE {}")?
.on_store(&Store::new()?)
.execute()?;Fieldsยง
ยงparser: SparqlParserยงinner: QueryEvaluatorImplementationsยง
ยงimpl SparqlEvaluator
impl SparqlEvaluator
pub fn new() -> SparqlEvaluator
pub fn with_base_iri(
self,
base_iri: impl Into<String>,
) -> Result<SparqlEvaluator, IriParseError>
pub fn with_base_iri( self, base_iri: impl Into<String>, ) -> Result<SparqlEvaluator, IriParseError>
Provides an IRI that could be used to resolve the operation relative IRIs.
use oxigraph::model::*;
use oxigraph::sparql::{QueryResults, SparqlEvaluator};
use oxigraph::store::Store;
if let QueryResults::Solutions(mut solutions) = SparqlEvaluator::new()
.with_base_iri("http://example.com/")?
.parse_query("SELECT (<> AS ?r) WHERE {}")?
.on_store(&Store::new()?)
.execute()?
{
assert_eq!(
solutions.next().unwrap()?.get("r"),
Some(&NamedNode::new("http://example.com/")?.into())
);
}pub fn with_prefix(
self,
prefix_name: impl Into<String>,
prefix_iri: impl Into<String>,
) -> Result<SparqlEvaluator, IriParseError>
pub fn with_prefix( self, prefix_name: impl Into<String>, prefix_iri: impl Into<String>, ) -> Result<SparqlEvaluator, IriParseError>
Set a default IRI prefix used during parsing.
use oxigraph::model::*;
use oxigraph::sparql::{QueryResults, SparqlEvaluator};
use oxigraph::store::Store;
if let QueryResults::Solutions(mut solutions) = SparqlEvaluator::new()
.with_prefix("ex", "http://example.com/")?
.parse_query("SELECT (ex: AS ?r) WHERE {}")?
.on_store(&Store::new()?)
.execute()?
{
assert_eq!(
solutions.next().unwrap()?.get("r"),
Some(&NamedNode::new("http://example.com/")?.into())
);
}pub fn with_service_handler(
self,
service_name: impl Into<NamedNode>,
handler: impl ServiceHandler + 'static,
) -> SparqlEvaluator
pub fn with_service_handler( self, service_name: impl Into<NamedNode>, handler: impl ServiceHandler + 'static, ) -> SparqlEvaluator
Use a given ServiceHandler to execute SPARQL 1.1 Federated Query SERVICE calls.
See ServiceHandler for an example.
pub fn with_default_service_handler(
self,
handler: impl DefaultServiceHandler + 'static,
) -> SparqlEvaluator
pub fn with_default_service_handler( self, handler: impl DefaultServiceHandler + 'static, ) -> SparqlEvaluator
Use a given DefaultServiceHandler to execute SPARQL 1.1 Federated Query SERVICE calls if no explicit service handler is defined for the service.
This replaces the default service handler that does HTTP requests to remote endpoints.
See DefaultServiceHandler for an example.
pub fn with_custom_function(
self,
name: NamedNode,
evaluator: impl Fn(&[Term]) -> Option<Term> + Send + Sync + 'static,
) -> SparqlEvaluator
pub fn with_custom_function( self, name: NamedNode, evaluator: impl Fn(&[Term]) -> Option<Term> + Send + Sync + 'static, ) -> SparqlEvaluator
Adds a custom SPARQL evaluation function.
Example with a function serializing terms to N-Triples:
use oxigraph::model::*;
use oxigraph::sparql::{QueryResults, SparqlEvaluator};
use oxigraph::store::Store;
if let QueryResults::Solutions(mut solutions) = SparqlEvaluator::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()),
)
.parse_query("SELECT (<http://www.w3.org/ns/formats/N-Triples>(1) AS ?nt) WHERE {}")?
.on_store(&Store::new()?)
.execute()?
{
assert_eq!(
solutions.next().unwrap()?.get("nt"),
Some(&Literal::from("\"1\"^^<http://www.w3.org/2001/XMLSchema#integer>").into())
);
}pub fn with_custom_aggregate_function(
self,
name: NamedNode,
evaluator: impl Fn() -> Box<dyn AggregateFunctionAccumulator + Send + Sync> + Send + Sync + 'static,
) -> SparqlEvaluator
pub fn with_custom_aggregate_function( self, name: NamedNode, evaluator: impl Fn() -> Box<dyn AggregateFunctionAccumulator + Send + Sync> + Send + Sync + 'static, ) -> SparqlEvaluator
Adds a custom SPARQL evaluation aggregate function.
Example with a function doing concatenation:
use oxigraph::model::{Literal, NamedNode, Term};
use oxigraph::sparql::{AggregateFunctionAccumulator, QueryResults, SparqlEvaluator};
use oxigraph::store::Store;
use std::mem::take;
struct ConcatAccumulator {
value: String,
}
impl AggregateFunctionAccumulator for ConcatAccumulator {
fn accumulate(&mut self, element: Term) {
if let Term::Literal(v) = element {
if !self.value.is_empty() {
self.value.push(' ');
}
self.value.push_str(v.value());
}
}
fn finish(&mut self) -> Option<Term> {
Some(Literal::new_simple_literal(take(&mut self.value)).into())
}
}
if let QueryResults::Solutions(mut solutions) = SparqlEvaluator::new()
.with_custom_aggregate_function(NamedNode::new("http://example.com/concat")?, || {
Box::new(ConcatAccumulator {
value: String::new(),
})
})
.parse_query(
"SELECT (<http://example.com/concat>(?v) AS ?r) WHERE { VALUES ?v { 1 2 3 } }",
)?
.on_store(&Store::new()?)
.execute()?
{
assert_eq!(
solutions.next().unwrap()?.get("r"),
Some(&Literal::new_simple_literal("1 2 3").into())
);
}pub fn with_cancellation_token(
self,
cancellation_token: CancellationToken,
) -> SparqlEvaluator
pub fn with_cancellation_token( self, cancellation_token: CancellationToken, ) -> SparqlEvaluator
Inject a cancellation token to the SPARQL evaluation.
Might be used to abort a query cleanly.
use oxigraph::model::*;
use oxigraph::sparql::{
CancellationToken, QueryEvaluationError, QueryResults, SparqlEvaluator,
};
use oxigraph::store::Store;
let store = Store::new()?;
store.insert(QuadRef::new(
NamedNodeRef::new("http://example.com/s")?,
NamedNodeRef::new("http://example.com/p")?,
NamedNodeRef::new("http://example.com/o")?,
GraphNameRef::DefaultGraph,
))?;
let cancellation_token = CancellationToken::new();
if let QueryResults::Solutions(mut solutions) = SparqlEvaluator::new()
.with_cancellation_token(cancellation_token.clone())
.parse_query("SELECT * WHERE { ?s ?p ?o }")?
.on_store(&store)
.execute()?
{
cancellation_token.cancel(); // We cancel
assert!(matches!(
solutions.next().unwrap().unwrap_err(),
QueryEvaluationError::Cancelled
));
}pub fn parse_query(
self,
query: &(impl AsRef<str> + ?Sized),
) -> Result<PreparedSparqlQuery, SparqlSyntaxError>
pub fn parse_query( self, query: &(impl AsRef<str> + ?Sized), ) -> Result<PreparedSparqlQuery, SparqlSyntaxError>
Parse a query and returns a PreparedSparqlQuery for the current evaluator.
Usage example:
use oxigraph::model::*;
use oxigraph::sparql::{QueryResults, SparqlEvaluator};
use oxigraph::store::Store;
let store = Store::new()?;
let ex = NamedNodeRef::new("http://example.com")?;
store.insert(QuadRef::new(ex, ex, ex, GraphNameRef::DefaultGraph))?;
let prepared_query = SparqlEvaluator::new().parse_query("SELECT ?s WHERE { ?s ?p ?o }")?;
if let QueryResults::Solutions(mut solutions) = prepared_query.on_store(&store).execute()? {
assert_eq!(
solutions.next().unwrap()?.get("s"),
Some(&ex.into_owned().into())
);
}pub fn for_query(self, query: impl Into<Query>) -> PreparedSparqlQuery
pub fn for_query(self, query: impl Into<Query>) -> PreparedSparqlQuery
Returns a PreparedSparqlQuery for the current evaluator and SPARQL query.
Usage example:
use oxigraph::model::*;
use oxigraph::sparql::{QueryResults, SparqlEvaluator};
use oxigraph::store::Store;
use spargebra::SparqlParser;
let store = Store::new()?;
let ex = NamedNodeRef::new("http://example.com")?;
store.insert(QuadRef::new(ex, ex, ex, GraphNameRef::DefaultGraph))?;
let query = SparqlParser::new().parse_query("SELECT ?s WHERE { ?s ?p ?o }")?;
let prepared_query = SparqlEvaluator::new().for_query(query);
if let QueryResults::Solutions(mut solutions) = prepared_query.on_store(&store).execute()? {
assert_eq!(
solutions.next().unwrap()?.get("s"),
Some(&ex.into_owned().into())
);
}pub fn parse_update(
self,
query: &(impl AsRef<str> + ?Sized),
) -> Result<PreparedSparqlUpdate, SparqlSyntaxError>
pub fn parse_update( self, query: &(impl AsRef<str> + ?Sized), ) -> Result<PreparedSparqlUpdate, SparqlSyntaxError>
Parse an update and returns a PreparedSparqlUpdate for the current evaluator.
Usage example:
use oxigraph::sparql::SparqlEvaluator;
use oxigraph::store::Store;
SparqlEvaluator::new()
.parse_update(
"INSERT DATA { <http://example.com> <http://example.com> <http://example.com> }",
)?
.on_store(&Store::new()?)
.execute()?;pub fn for_update(self, update: impl Into<Update>) -> PreparedSparqlUpdate
pub fn for_update(self, update: impl Into<Update>) -> PreparedSparqlUpdate
Returns a PreparedSparqlUpdate for the current evaluator and SPARQL update.
Usage example:
use oxigraph::sparql::SparqlEvaluator;
use oxigraph::store::Store;
use spargebra::SparqlParser;
let update = SparqlParser::new().parse_update(
"INSERT DATA { <http://example.com> <http://example.com> <http://example.com> }",
)?;
SparqlEvaluator::new()
.for_update(update)
.on_store(&Store::new()?)
.execute()?;Trait Implementationsยง
ยงimpl Clone for SparqlEvaluator
impl Clone for SparqlEvaluator
ยงfn clone(&self) -> SparqlEvaluator
fn clone(&self) -> SparqlEvaluator
1.0.0 ยท Sourceยงfn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreยงimpl Default for SparqlEvaluator
impl Default for SparqlEvaluator
ยงfn default() -> SparqlEvaluator
fn default() -> SparqlEvaluator
Auto Trait Implementationsยง
impl Freeze for SparqlEvaluator
impl !RefUnwindSafe for SparqlEvaluator
impl Send for SparqlEvaluator
impl Sync for SparqlEvaluator
impl Unpin for SparqlEvaluator
impl !UnwindSafe for SparqlEvaluator
Blanket Implementationsยง
Sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Sourceยงimpl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
ยงimpl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
ยงfn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.ยงfn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.ยงfn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Anyโs vtable from &Traitโs.ยงfn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Anyโs vtable from &mut Traitโs.ยงimpl<T> DowncastSend for T
impl<T> DowncastSend for T
ยงimpl<T> DowncastSync for T
impl<T> DowncastSync for T
ยงimpl<E, T, Request, Encoding> FromReq<Patch<Encoding>, Request, E> for Twhere
Request: Req<E> + Send + 'static,
Encoding: Decodes<T>,
E: FromServerFnError,
impl<E, T, Request, Encoding> FromReq<Patch<Encoding>, Request, E> for Twhere
Request: Req<E> + Send + 'static,
Encoding: Decodes<T>,
E: FromServerFnError,
ยงimpl<E, T, Request, Encoding> FromReq<Post<Encoding>, Request, E> for Twhere
Request: Req<E> + Send + 'static,
Encoding: Decodes<T>,
E: FromServerFnError,
impl<E, T, Request, Encoding> FromReq<Post<Encoding>, Request, E> for Twhere
Request: Req<E> + Send + 'static,
Encoding: Decodes<T>,
E: FromServerFnError,
ยงimpl<E, T, Request, Encoding> FromReq<Put<Encoding>, Request, E> for Twhere
Request: Req<E> + Send + 'static,
Encoding: Decodes<T>,
E: FromServerFnError,
impl<E, T, Request, Encoding> FromReq<Put<Encoding>, Request, E> for Twhere
Request: Req<E> + Send + 'static,
Encoding: Decodes<T>,
E: FromServerFnError,
ยงimpl<E, Encoding, Response, T> FromRes<Patch<Encoding>, Response, E> for Twhere
Response: ClientRes<E> + Send,
Encoding: Decodes<T>,
E: FromServerFnError,
impl<E, Encoding, Response, T> FromRes<Patch<Encoding>, Response, E> for Twhere
Response: ClientRes<E> + Send,
Encoding: Decodes<T>,
E: FromServerFnError,
ยงimpl<E, Encoding, Response, T> FromRes<Post<Encoding>, Response, E> for Twhere
Response: ClientRes<E> + Send,
Encoding: Decodes<T>,
E: FromServerFnError,
impl<E, Encoding, Response, T> FromRes<Post<Encoding>, Response, E> for Twhere
Response: ClientRes<E> + Send,
Encoding: Decodes<T>,
E: FromServerFnError,
ยงimpl<E, Encoding, Response, T> FromRes<Put<Encoding>, Response, E> for Twhere
Response: ClientRes<E> + Send,
Encoding: Decodes<T>,
E: FromServerFnError,
impl<E, Encoding, Response, T> FromRes<Put<Encoding>, Response, E> for Twhere
Response: ClientRes<E> + Send,
Encoding: Decodes<T>,
E: FromServerFnError,
ยงimpl<T> Instrument for T
impl<T> Instrument for T
ยงfn instrument(self, span: Span) -> Instrumented<Self> โ
fn instrument(self, span: Span) -> Instrumented<Self> โ
Sourceยงimpl<T> IntoEither for T
impl<T> IntoEither for T
Sourceยงfn into_either(self, into_left: bool) -> Either<Self, Self> โ
fn into_either(self, into_left: bool) -> Either<Self, Self> โ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSourceยงfn into_either_with<F>(self, into_left: F) -> Either<Self, Self> โ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> โ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more