pub struct SparqlEvaluator { /* private fields */ }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()?;Implementations§
Source§impl SparqlEvaluator
impl SparqlEvaluator
pub fn new() -> Self
Sourcepub fn with_base_iri(
self,
base_iri: impl Into<String>,
) -> Result<Self, IriParseError>
pub fn with_base_iri( self, base_iri: impl Into<String>, ) -> Result<Self, 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())
);
}Sourcepub fn with_prefix(
self,
prefix_name: impl Into<String>,
prefix_iri: impl Into<String>,
) -> Result<Self, IriParseError>
pub fn with_prefix( self, prefix_name: impl Into<String>, prefix_iri: impl Into<String>, ) -> Result<Self, 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())
);
}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.
This replaces the default service handler that does HTTP requests to remote endpoints.
See DefaultServiceHandler for an example.
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 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())
);
}Sourcepub fn with_custom_aggregate_function(
self,
name: NamedNode,
evaluator: impl Fn() -> Box<dyn AggregateFunctionAccumulator + Send + Sync> + Send + Sync + 'static,
) -> Self
pub fn with_custom_aggregate_function( self, name: NamedNode, evaluator: impl Fn() -> Box<dyn AggregateFunctionAccumulator + Send + Sync> + Send + Sync + 'static, ) -> Self
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())
);
}Sourcepub fn with_cancellation_token(
self,
cancellation_token: CancellationToken,
) -> Self
pub fn with_cancellation_token( self, cancellation_token: CancellationToken, ) -> Self
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
));
}Sourcepub 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())
);
}Sourcepub 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())
);
}Sourcepub 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()?;Sourcepub 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§
Source§impl Clone for SparqlEvaluator
impl Clone for SparqlEvaluator
Source§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