Struct MaterializedDatalogView

Source
pub struct MaterializedDatalogView { /* private fields */ }
Expand description

A incrementally maintained view of a graph over a property graph

Implementations§

Source§

impl MaterializedDatalogView

Source

pub fn push_fact( &mut self, relation_symbol: &'_ str, fact: impl Into<Fact>, ) -> bool

Pushes a fact into the materialized view.

§Examples
use materialized_view::*;

let query = program! {
reaches(?x, ?y) <- [edge(?x, ?y)],
};
let mut dynamic_view = MaterializedDatalogView::new(query);

dynamic_view.push_fact("edge", (1, 2));
dynamic_view.poll();

assert!(dynamic_view.contains("edge", (1, 2)).unwrap());
assert!(dynamic_view.contains("reaches", (1, 2)).unwrap());
Source

pub fn retract_fact( &mut self, relation_symbol: &'_ str, fact: impl Into<Fact>, ) -> bool

Retracts facts from the materialized view.

§Examples
use materialized_view::*;

let query = program! {
reaches(?x, ?y) <- [edge(?x, ?y)],
};
let mut dynamic_view = MaterializedDatalogView::new(query);

dynamic_view.push_fact("edge", (1, 2));
dynamic_view.poll();
assert!(dynamic_view.contains("edge", (1, 2)).unwrap());
assert!(dynamic_view.contains("reaches", (1, 2)).unwrap());

dynamic_view.retract_fact("edge", (1, 2));
dynamic_view.poll();
assert!(!dynamic_view.contains("edge", (1, 2)).unwrap());
assert!(!dynamic_view.contains("reaches", (1, 2)).unwrap());

let expected_update = vec![(-1, (1, 2))];
let actual_update_edge: Vec<((isize, (i32, i32)))> = dynamic_view
    .query_frontier_binary("edge", (ANY_VALUE, ANY_VALUE))
    .unwrap()
    .map(|((x, y), weight)| (weight, (*x, *y)))
    .collect();
assert_eq!(expected_update, actual_update_edge);

let actual_update_reaches: Vec<((isize, (i32, i32)))> = dynamic_view
    .query_frontier_binary("reaches", (ANY_VALUE, ANY_VALUE))
    .unwrap()
    .map(|((x, y), weight)| (weight, (*x, *y)))
    .collect();
assert_eq!(expected_update, actual_update_reaches);
Source

pub fn push_rule(&mut self, rule: impl Into<Rule>)

Pushes a rule into the materialized view.

§Examples
use materialized_view::*;

let query = program! {
reaches(?x, ?y) <- [edge(?x, ?y)],
};
let mut dynamic_view = MaterializedDatalogView::new(query);

dynamic_view.push_fact("edge", (1, 2));
dynamic_view.push_fact("edge", (2, 3));
dynamic_view.poll();
assert!(!dynamic_view.contains("reaches", (1, 3)).unwrap());

dynamic_view.push_rule(rule!{ reaches(?x, ?z) <- [edge(?x, ?y), reaches(?y, ?z)] });
dynamic_view.poll();
assert!(dynamic_view.contains("reaches", (1, 3)).unwrap());
Source

pub fn retract_rule(&mut self, rule: impl Into<Rule>)

Retracts a rule from the materialized view.

§Examples
use materialized_view::*;

let query = program! {
reaches(?x, ?y) <- [edge(?x, ?y)],
reaches(?x, ?z) <- [edge(?x, ?y), reaches(?y, ?z)]
};
let mut dynamic_view = MaterializedDatalogView::new(query);

dynamic_view.push_fact("edge", (1, 2));
dynamic_view.push_fact("edge", (2, 3));
dynamic_view.poll();
assert!(dynamic_view.contains("reaches", (1, 3)).unwrap());

dynamic_view.retract_rule(rule!{ reaches(?x, ?z) <- [edge(?x, ?y), reaches(?y, ?z)] });
dynamic_view.poll();
assert!(!dynamic_view.contains("reaches", (1, 3)).unwrap());
Source

pub fn contains( &self, relation_symbol: &'_ str, fact: impl Into<Fact>, ) -> Result<bool, PollingError>

Checks whether a fact is true in the consolidated view. Throws an error if a poll is needed to obtain correct results.

§Examples
use materialized_view::*;

let query = program! {
reaches(?x, ?y) <- [edge(?x, ?y)],
};
let mut dynamic_view = MaterializedDatalogView::new(query);

dynamic_view.push_fact("edge", (1, 2));
dynamic_view.push_fact("edge", (2, 3));
println!("{}", dynamic_view.contains("reaches", (2, 3)).unwrap_err());
dynamic_view.poll();
println!("{}", dynamic_view.contains("reaches", (2, 3)).unwrap());
Source

pub fn query_unary<'a, T: 'static>( &self, relation_symbol: &'_ str, goal: impl Into<Goal>, ) -> Result<impl Iterator<Item = (&T,)>, PollingError>

Queries the consolidated view of a unary relation.

§Examples
use materialized_view::*;

let query = program! {
rdfType(?o) <- [RDF(?s, "rdf:type", ?o)],
};
let mut dynamic_view = MaterializedDatalogView::new(query);

dynamic_view.push_fact("RDF", ("rust", "rdf:type", "programmingLanguage".to_string()));
dynamic_view.poll();
let query_result: Vec<((String,))> = dynamic_view
    .query_unary::<String>("rdfType", (ANY_VALUE,))
    .unwrap()
    .map(|((x,))| (x.clone(),))
    .collect();
let expected_query_result = vec![("programmingLanguage".to_string(),)];
assert_eq!(expected_query_result, query_result)
Source

pub fn query_frontier_unary<'a, T: 'static>( &self, relation_symbol: &'_ str, goal: impl Into<Goal>, ) -> Result<impl Iterator<Item = ((&T,), isize)>, PollingError>

Queries the frontier view of a unary relation.

§Examples
use materialized_view::*;

let query = program! {
rdfType(?o) <- [RDF(?s, "rdf:type", ?o)],
};
let mut dynamic_view = MaterializedDatalogView::new(query);

dynamic_view.push_fact("RDF", ("rust", "rdf:type", "programmingLanguage".to_string()));
dynamic_view.poll();
dynamic_view.push_fact("RDF", ("emacs", "rdf:type", "operatingSystem".to_string()));
dynamic_view.poll();
let query_result: Vec<(isize, (String,))> = dynamic_view
    .query_frontier_unary::<String>("rdfType", (ANY_VALUE,))
    .unwrap()
    .map(|((x,), weight)| (weight, (x.clone(),)))
    .collect();
let expected_query_result = vec![(1, ("operatingSystem".to_string(),))];
assert_eq!(expected_query_result, query_result)
Source

pub fn query_binary<'a, T: 'static, R: 'static>( &self, relation_symbol: &'_ str, goal: impl Into<Goal>, ) -> Result<impl Iterator<Item = (&T, &R)>, PollingError>

Queries the consolidated view of a binary relation.

§Examples
use std::collections::HashSet;
use materialized_view::*;

let query = program! {
subClassOf(?s, ?o) <- [RDF(?s, "rdfs:subClassOf", ?o)],
subClassOf(?s, ?t) <- [subClassOf(?s, ?o), subClassOf(?o, ?t)],
};
let mut dynamic_view = MaterializedDatalogView::new(query);

dynamic_view.push_fact("RDF", ("animal".to_string(), "rdfs:subClassOf", "livingForm".to_string()));
dynamic_view.push_fact("RDF", ("bird".to_string(), "rdfs:subClassOf", "animal".to_string()));
dynamic_view.poll();
let query_result: HashSet<((String, String))> = dynamic_view
    .query_binary::<String, String>("subClassOf", (ANY_VALUE, ANY_VALUE))
    .unwrap()
    .map(|((x, y))| (x.clone(), y.clone()))
    .collect();
let expected_query_result: HashSet<_> = vec![
    ("animal".to_string(), "livingForm".to_string()),
    ("bird".to_string(), "animal".to_string()),
    ("bird".to_string(), "livingForm".to_string())].into_iter().collect();
assert_eq!(expected_query_result, query_result);
Source

pub fn query_frontier_binary<'a, T: 'static, R: 'static>( &self, relation_symbol: &'_ str, goal: impl Into<Goal>, ) -> Result<impl Iterator<Item = ((&T, &R), isize)>, PollingError>

Queries the frontier of a binary relation.

§Examples
use std::collections::HashSet;
use materialized_view::*;

let query = program! {
subClassOf(?s, ?o) <- [RDF(?s, "rdfs:subClassOf", ?o)],
subClassOf(?s, ?t) <- [subClassOf(?s, ?o), subClassOf(?o, ?t)],
};
let mut dynamic_view = MaterializedDatalogView::new(query);

dynamic_view.push_fact("RDF", ("animal".to_string(), "rdfs:subClassOf", "livingForm".to_string()));
dynamic_view.push_fact("RDF", ("bird".to_string(), "rdfs:subClassOf", "animal".to_string()));
dynamic_view.poll();
dynamic_view.retract_fact("RDF", ("animal".to_string(), "rdfs:subClassOf", "livingForm".to_string()));
dynamic_view.poll();
let query_result: HashSet<((isize, (String, String)))> = dynamic_view
    .query_frontier_binary::<String, String>("subClassOf", (ANY_VALUE, ANY_VALUE))
    .unwrap()
    .map(|((x, y), weight)| (weight, (x.clone(), y.clone())))
    .collect();
let expected_query_result: HashSet<_> = vec![
    (-1, ("animal".to_string(), "livingForm".to_string())),
    (-1, ("bird".to_string(), "livingForm".to_string()))].into_iter().collect();
assert_eq!(expected_query_result, query_result);
Source

pub fn query_ternary<'a, T: 'static, R: 'static, S: 'static>( &self, relation_symbol: &'_ str, goal: impl Into<Goal>, ) -> Result<impl Iterator<Item = (&T, &R, &S)>, PollingError>

Queries the consolidated view of a ternary relation.

§Examples
use std::collections::HashSet;
use materialized_view::*;

let query = program! {
triangle(?a, ?b, ?c) <- [edge(?a, ?b), edge(?b, ?c), edge(?c, ?a)],
};
let mut dynamic_view = MaterializedDatalogView::new(query);

dynamic_view.push_fact("edge", (1, 2));
dynamic_view.push_fact("edge", (2, 3));
dynamic_view.push_fact("edge", (3, 1));
dynamic_view.push_fact("edge", (4, 5));
dynamic_view.push_fact("edge", (5, 6));
dynamic_view.poll();
let query_result: HashSet<((i32, i32, i32))> = dynamic_view
    .query_ternary::<i32, i32, i32>("triangle", (ANY_VALUE, ANY_VALUE, ANY_VALUE))
    .unwrap()
    .map(|((a, b, c))| (*a, *b, *c))
    .collect();
let expected_query_result: HashSet<_> = vec![(1, 2, 3), (2, 3, 1), (3, 1, 2)].into_iter().collect();
assert_eq!(expected_query_result, query_result);
Source

pub fn query_frontier_ternary<'a, T: 'static, R: 'static, S: 'static>( &self, relation_symbol: &'_ str, goal: impl Into<Goal>, ) -> Result<impl Iterator<Item = ((&T, &R, &S), isize)>, PollingError>

Queries the frontier of a ternary relation.

§Examples
use std::collections::HashSet;
use materialized_view::*;

let query = program! {
triangle(?a, ?b, ?c) <- [edge(?a, ?b), edge(?b, ?c), edge(?c, ?a)],
};
let mut dynamic_view = MaterializedDatalogView::new(query);

dynamic_view.push_fact("edge", (1, 2));
dynamic_view.push_fact("edge", (2, 3));
dynamic_view.push_fact("edge", (3, 1));
dynamic_view.push_fact("edge", (4, 5));
dynamic_view.push_fact("edge", (5, 6));
dynamic_view.poll();
let query_result: HashSet<((isize, (i32, i32, i32)))> = dynamic_view
    .query_frontier_ternary::<i32, i32, i32>("triangle", (ANY_VALUE, ANY_VALUE, ANY_VALUE))
    .unwrap()
    .map(|(((a, b, c), weight))| (weight, (*a, *b, *c)))
    .collect();
let expected_query_result: HashSet<_> = vec![(1, (1, 2, 3)), (1, (2, 3, 1)), (1, (3, 1, 2))].into_iter().collect();
assert_eq!(expected_query_result, query_result);
Source

pub fn poll(&mut self)

Triggers a incremental materialization update

Source

pub fn new(program: Vec<impl Into<Rule>>) -> Self

Creates a new dynamic materialised view

Source

pub fn safe(&self) -> bool

Returns whether all incoming updates have been taken into account

Source

pub fn len(&self) -> usize

Returns the number of consolidated updates

Trait Implementations§

Source§

impl<'a, R> Extend<(&'a str, R)> for MaterializedDatalogView
where R: Into<Fact>,

Source§

fn extend<T: IntoIterator<Item = (&'a str, R)>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<R> Extend<R> for MaterializedDatalogView
where R: Into<Rule>,

Source§

fn extend<T: IntoIterator<Item = R>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. 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> ArchivePointee for T

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
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<F, W, T, D> Deserialize<With<T, W>, D> for F
where W: DeserializeWith<F, T, D>, D: Fallible + ?Sized, F: ?Sized,

Source§

fn deserialize( &self, deserializer: &mut D, ) -> Result<With<T, W>, <D as Fallible>::Error>

Deserializes using the given deserializer
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The type for metadata in pointers and references to Self.
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

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more