Skip to main content

SignatureMatcher

Struct SignatureMatcher 

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

Indexed structure for fast predicate signature matching.

This provides O(1) lookup of predicates by:

  • Arity (number of arguments)
  • Exact signature (ordered domain types)
  • Domain patterns (unordered domain types)

§Example

use tensorlogic_adapters::{PredicateInfo, SignatureMatcher};

let mut matcher = SignatureMatcher::new();

let knows = PredicateInfo::new(
    "knows",
    vec!["Person".to_string(), "Person".to_string()]
);
matcher.add_predicate(&knows);

// Find by arity
let arity_2 = matcher.find_by_arity(2);
assert_eq!(arity_2.len(), 1);

// Find by exact signature
let signature = vec!["Person".to_string(), "Person".to_string()];
let matches = matcher.find_by_signature(&signature);
assert_eq!(matches.len(), 1);
assert_eq!(matches[0], "knows");

Implementations§

Source§

impl SignatureMatcher

Source

pub fn new() -> Self

Create a new signature matcher.

Source

pub fn add_predicate(&mut self, pred: &PredicateInfo)

Add a predicate to the matcher indices.

Source

pub fn remove_predicate(&mut self, name: &str)

Remove a predicate from all indices.

Source

pub fn find_by_arity(&self, arity: usize) -> Vec<String>

Find all predicates with the given arity.

§Example
use tensorlogic_adapters::{PredicateInfo, SignatureMatcher};

let mut matcher = SignatureMatcher::new();
matcher.add_predicate(&PredicateInfo::new("knows", vec!["Person".into(), "Person".into()]));
matcher.add_predicate(&PredicateInfo::new("age", vec!["Person".into()]));

let unary = matcher.find_by_arity(1);
assert_eq!(unary.len(), 1);
assert!(unary.contains(&"age".to_string()));
Source

pub fn find_by_signature(&self, signature: &[String]) -> Vec<String>

Find all predicates with the exact signature (ordered domain types).

§Example
use tensorlogic_adapters::{PredicateInfo, SignatureMatcher};

let mut matcher = SignatureMatcher::new();
matcher.add_predicate(&PredicateInfo::new("at", vec!["Person".into(), "Location".into()]));

let sig = vec!["Person".to_string(), "Location".to_string()];
let matches = matcher.find_by_signature(&sig);
assert_eq!(matches, vec!["at"]);
Source

pub fn find_by_domain_set(&self, domains: &[String]) -> Vec<String>

Find all predicates with the given domain types (unordered).

This is useful for finding predicates that operate on a set of domains regardless of argument order.

§Example
use tensorlogic_adapters::{PredicateInfo, SignatureMatcher};

let mut matcher = SignatureMatcher::new();
matcher.add_predicate(&PredicateInfo::new("knows", vec!["Person".into(), "Person".into()]));

let domains = vec!["Person".to_string()];
let matches = matcher.find_by_domain_set(&domains);
// "knows" has signature [Person, Person], which when deduplicated matches [Person]
// Note: This requires exact match of sorted signature
Source

pub fn get_predicate(&self, name: &str) -> Option<&PredicateInfo>

Get full predicate information by name.

Source

pub fn contains(&self, name: &str) -> bool

Check if a predicate exists.

Source

pub fn len(&self) -> usize

Get the total number of predicates indexed.

Source

pub fn is_empty(&self) -> bool

Check if the matcher is empty.

Source

pub fn predicate_names(&self) -> Vec<String>

Get all predicate names.

Source

pub fn clear(&mut self)

Clear all indices.

Source

pub fn stats(&self) -> MatcherStats

Get statistics about the index sizes.

Source

pub fn from_predicates<'a>( predicates: impl IntoIterator<Item = &'a PredicateInfo>, ) -> Self

Build a matcher from a collection of predicates.

Trait Implementations§

Source§

impl Clone for SignatureMatcher

Source§

fn clone(&self) -> SignatureMatcher

Returns a duplicate 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 Debug for SignatureMatcher

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SignatureMatcher

Source§

fn default() -> SignatureMatcher

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> 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.