Skip to main content

KnowledgeBase

Struct KnowledgeBase 

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

A set of rules, kept in salience order and compiled for execution.

A RuleEngine holds one; rules are usually added through the engine’s add_rules_from_* methods.

Implementations§

Source§

impl KnowledgeBase

Source

pub fn new() -> Self

An empty knowledge base.

Source

pub fn add_rule(&mut self, rule: Rule) -> Result<(), String>

Adds a rule. Fails if a rule with the same name exists.

Source

pub fn add_rules_from_str(&mut self, grl: &str) -> Result<usize, String>

Parses GRL text and adds all of its rules. Returns how many were added. Nothing is added if any rule fails to parse or clashes with an existing name.

Source

pub fn add_rules_from_file( &mut self, path: impl AsRef<Path>, ) -> Result<usize, String>

Loads all rules from a .grl file. Returns how many were added.

Source

pub fn add_rules_from_dir( &mut self, dir: impl AsRef<Path>, ) -> Result<usize, String>

Loads all .grl files under dir, recursively, in path order. Returns how many rules were added.

Source

pub fn get_rule(&self, name: &str) -> Option<&Rule>

The rule with the given name.

Source

pub fn get_rules(&self) -> &[Rule]

All rules, in the order they were added.

Source

pub fn get_rules_sorted_by_salience(&self) -> Vec<&Rule>

All rules, highest salience first; rules with equal salience in the order they were added.

Source

pub fn rules_by_salience(&self) -> impl Iterator<Item = &Rule> + '_

Iterates rules highest salience first; rules with equal salience come in the order they were added.

Source

pub fn remove_rule(&mut self, name: &str) -> Option<Rule>

Removes and returns the rule with the given name.

Source

pub fn clear(&mut self)

Removes all rules.

Source

pub fn len(&self) -> usize

The number of rules.

Source

pub fn is_empty(&self) -> bool

Whether there are no rules.

Trait Implementations§

Source§

impl Debug for KnowledgeBase

Source§

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

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

impl Default for KnowledgeBase

Source§

fn default() -> Self

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.