Skip to main content

Fact

Struct Fact 

Source
pub struct Fact<'env> { /* private fields */ }
Expand description

A fact, retained for as long as the handle lives.

Retracting a fact does not invalidate the handle: CLIPS keeps the record alive while it is retained, and Fact::is_retracted reports the change.

Implementations§

Source§

impl<'env> Fact<'env>

Source

pub fn index(&self) -> i64

The fact index, the number CLIPS prints as f-<n>.

Indices are never reused, so one identifies a fact for the lifetime of the environment.

Source

pub fn is_retracted(&self) -> bool

Whether the fact has been retracted. The handle remains usable.

Source

pub fn retract(&mut self) -> Result<()>

Retracts the fact, which may activate or deactivate rules.

Source

pub fn pretty_print(&self, ignore_defaults: bool) -> String

The fact rendered as CLIPS prints it.

With ignore_defaults, slots still holding their default value are omitted. Empty if CLIPS could not allocate the string builder.

Source

pub fn relation(&self) -> String

The fact’s relation name, that is, its deftemplate name.

Source

pub fn template(&self) -> Option<Deftemplate<'env>>

The deftemplate this fact was asserted against.

Source

pub fn slot_names(&self) -> Multifield<'env>

The fact’s slot names, in template order. Empty for implied facts, which have no named slots.

Source

pub fn modifier(&self) -> Result<FactModifier<'env>>

Starts a batched modification.

CLIPS implements a modification as a retract and re-assert, so the result is a new fact with a new index.

Source

pub fn slot(&self, name: &str) -> Result<Value<'env>>

Reads a slot by name. The returned value is retained independently of the fact.

Examples found in repository?
examples/rule_base.rs (line 51)
7fn main() -> Result<(), Error> {
8    let mut environment = Environment::new()?;
9
10    environment.load_str(
11        r#"
12        (deftemplate reading (slot sensor) (slot celsius))
13        (deftemplate alert (slot sensor) (slot level))
14
15        (defrule too-hot
16            (reading (sensor ?s) (celsius ?c&:(> ?c 80)))
17            =>
18            (assert (alert (sensor ?s) (level high))))
19
20        (defrule warm
21            (reading (sensor ?s) (celsius ?c&:(and (> ?c 60) (<= ?c 80))))
22            =>
23            (assert (alert (sensor ?s) (level warning))))
24        "#,
25    )?;
26
27    // Observe firings without touching CLIPS's own trace output.
28    environment
29        .add_rule_fire_hook("trace", RuleFirePhase::Before, 0, |rule| {
30            println!("firing {rule}");
31        })
32        .expect("rule-fire hook name is unused");
33
34    for (sensor, celsius) in [("boiler", 91), ("cabin", 65), ("cellar", 12)] {
35        environment
36            .fact_builder("reading")?
37            .symbol("sensor", sensor)?
38            .integer("celsius", celsius)?
39            .assert()?;
40    }
41
42    let fired = environment.run(None);
43    println!("{fired} rules fired");
44
45    let alerts = environment
46        .find_deftemplate("alert")?
47        .expect("alert exists");
48    for alert in environment.facts_in_template(&alerts) {
49        println!(
50            "alert: {} is {}",
51            alert.slot("sensor")?.as_str().unwrap_or("?"),
52            alert.slot("level")?.as_str().unwrap_or("?"),
53        );
54    }
55
56    Ok(())
57}

Trait Implementations§

Source§

impl Drop for Fact<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<'env> !RefUnwindSafe for Fact<'env>

§

impl<'env> !Send for Fact<'env>

§

impl<'env> !Sync for Fact<'env>

§

impl<'env> !UnwindSafe for Fact<'env>

§

impl<'env> Freeze for Fact<'env>

§

impl<'env> Unpin for Fact<'env>

§

impl<'env> UnsafeUnpin for Fact<'env>

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.