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>
impl<'env> Fact<'env>
Sourcepub fn index(&self) -> i64
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.
Sourcepub fn is_retracted(&self) -> bool
pub fn is_retracted(&self) -> bool
Whether the fact has been retracted. The handle remains usable.
Sourcepub fn retract(&mut self) -> Result<()>
pub fn retract(&mut self) -> Result<()>
Retracts the fact, which may activate or deactivate rules.
Sourcepub fn pretty_print(&self, ignore_defaults: bool) -> String
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.
Sourcepub fn template(&self) -> Option<Deftemplate<'env>>
pub fn template(&self) -> Option<Deftemplate<'env>>
The deftemplate this fact was asserted against.
Sourcepub fn slot_names(&self) -> Multifield<'env>
pub fn slot_names(&self) -> Multifield<'env>
The fact’s slot names, in template order. Empty for implied facts, which have no named slots.
Sourcepub fn modifier(&self) -> Result<FactModifier<'env>>
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.
Sourcepub fn slot(&self, name: &str) -> Result<Value<'env>>
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?
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}