Struct QueryEvaluator

Source
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

Source

pub fn new() -> Self

Source

pub fn execute( &self, dataset: impl QueryableDataset, query: &Query, ) -> Result<QueryResults, QueryEvaluationError>

Source

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());
}
Source

pub fn explain( &self, dataset: impl QueryableDataset, query: &Query, ) -> (Result<QueryResults, QueryEvaluationError>, QueryExplanation)

Source

pub fn explain_with_substituted_variables( &self, dataset: impl QueryableDataset, query: &Query, substitutions: impl IntoIterator<Item = (Variable, Term)>, ) -> (Result<QueryResults, QueryEvaluationError>, QueryExplanation)

Source

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.

Source

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.

Source

pub fn has_default_service_handler(&self) -> bool

Source

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())
    );
}
Source

pub fn without_optimizations(self) -> Self

Disables query optimizations and runs the query as it is.

Source

pub fn compute_statistics(self) -> Self

Compute statistics during evaluation and fills them in the explanation tree.

Trait Implementations§

Source§

impl Clone for QueryEvaluator

Source§

fn clone(&self) -> QueryEvaluator

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for QueryEvaluator

Source§

fn default() -> QueryEvaluator

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V