pub struct Value<'env> { /* private fields */ }Expand description
A retained CLIPS value.
The as_* accessors return None when the value holds a different type,
rather than converting, so a type mismatch is visible instead of silent.
Implementations§
Source§impl<'env> Value<'env>
impl<'env> Value<'env>
Sourcepub fn type_code(&self) -> u16
pub fn type_code(&self) -> u16
The raw CLIPS type tag: 0 float, 1 integer, 2 symbol, 3 string, 4 multifield, 5 external address, 6 fact, 7 instance, 8 instance name, 9 void.
Sourcepub fn as_i64(&self) -> Option<i64>
pub fn as_i64(&self) -> Option<i64>
The value as an integer, or None if it is not one.
Examples found in repository?
7fn main() -> Result<(), Error> {
8 let mut environment = Environment::new()?;
9
10 environment.load_str(
11 r#"
12 (defclass ACCOUNT (is-a USER)
13 (slot holder)
14 (slot balance (type INTEGER) (default 0)))
15
16 (defmessage-handler ACCOUNT deposit (?amount)
17 (bind ?self:balance (+ ?self:balance ?amount))
18 ?self:balance)
19
20 (defmessage-handler ACCOUNT describe ()
21 (str-cat (send ?self get-holder) ": " (send ?self get-balance)))
22 "#,
23 )?;
24
25 let account = environment.make_instance("(ada of ACCOUNT (holder Ada) (balance 100))")?;
26 println!(
27 "class: {}",
28 account.class().expect("instance has a class").name()
29 );
30
31 let balance = account.send("deposit")?.integer(250).send()?;
32 println!("after deposit: {:?}", balance.as_i64());
33 drop(balance);
34
35 // A direct slot write bypasses message passing entirely.
36 account.put_integer("balance", 1_000)?;
37
38 let description = account.send("describe")?.send()?;
39 println!("{}", description.as_str().unwrap_or(""));
40 drop(description);
41 drop(account);
42
43 let class = environment.find_defclass("ACCOUNT")?.expect("class exists");
44 for slot in class.slots(false) {
45 let descriptor = class.slot(&slot).expect("slot name has no NUL");
46 if let Some(descriptor) = descriptor {
47 println!(
48 "slot {slot}: types {:?} default {:?}",
49 descriptor.allowed_types(),
50 descriptor.default_value()
51 );
52 }
53 }
54
55 Ok(())
56}More examples
42fn main() -> Result<(), Error> {
43 let mut environment = Environment::new()?;
44 environment.build("(deftemplate reading (slot sensor) (slot celsius))")?;
45
46 // Lifecycle hooks: watch what reset and the fact list do.
47 let events = Rc::new(RefCell::new(Vec::new()));
48 let recorded = events.clone();
49 environment
50 .add_hook("on-reset", HookKind::Reset, 0, move || {
51 recorded.borrow_mut().push("reset".to_owned());
52 })
53 .expect("hook name is unused");
54
55 let recorded = events.clone();
56 environment
57 .add_fact_hook("on-assert", FactEventKind::Asserted, 0, move |change| {
58 if let FactChange::Asserted(fact) = change {
59 recorded
60 .borrow_mut()
61 .push(format!("asserted {} #{}", fact.relation, fact.index));
62 }
63 })
64 .expect("hook name is unused");
65
66 environment.assert_string("(reading (sensor boiler) (celsius 91))")?;
67 environment.assert_string("(reading (sensor cabin) (celsius 65))")?;
68
69 // Persistence: save to a file, clear, and load it back.
70 let path = std::env::temp_dir().join("clips-bindings-example-facts.clp");
71 let path = path.to_str().expect("temp path is UTF-8");
72 let saved = environment.save_facts(path, SaveScope::Local)?;
73 println!("saved {saved} facts");
74
75 environment.reset();
76 println!("after reset: {} facts", environment.fact_count());
77
78 let loaded = environment.load_facts(path)?;
79 println!("loaded {loaded} facts");
80 std::fs::remove_file(path).expect("temp file exists");
81
82 // Input routers: CLIPS reads from Rust rather than the terminal.
83 environment
84 .register_router("scripted", 40, ScriptedInput::new("42 "))
85 .expect("router name is unused");
86 let read = environment.eval("(read)")?;
87 println!("read from Rust: {:?}", read.as_i64());
88 drop(read);
89 environment
90 .remove_router("scripted")
91 .expect("router is registered");
92
93 println!("hook events: {:?}", events.borrow());
94 environment.remove_hook("on-reset").expect("hook exists");
95 environment.remove_hook("on-assert").expect("hook exists");
96 Ok(())
97}Sourcepub fn as_f64(&self) -> Option<f64>
pub fn as_f64(&self) -> Option<f64>
The value as a float, or None if it is not one. Integers are not
converted.
Sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
The text of a symbol, string, or instance name.
None for other types, and for text that is not valid UTF-8.
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}More examples
7fn main() -> Result<(), Error> {
8 let mut environment = Environment::new()?;
9
10 environment.load_str(
11 r#"
12 (defclass ACCOUNT (is-a USER)
13 (slot holder)
14 (slot balance (type INTEGER) (default 0)))
15
16 (defmessage-handler ACCOUNT deposit (?amount)
17 (bind ?self:balance (+ ?self:balance ?amount))
18 ?self:balance)
19
20 (defmessage-handler ACCOUNT describe ()
21 (str-cat (send ?self get-holder) ": " (send ?self get-balance)))
22 "#,
23 )?;
24
25 let account = environment.make_instance("(ada of ACCOUNT (holder Ada) (balance 100))")?;
26 println!(
27 "class: {}",
28 account.class().expect("instance has a class").name()
29 );
30
31 let balance = account.send("deposit")?.integer(250).send()?;
32 println!("after deposit: {:?}", balance.as_i64());
33 drop(balance);
34
35 // A direct slot write bypasses message passing entirely.
36 account.put_integer("balance", 1_000)?;
37
38 let description = account.send("describe")?.send()?;
39 println!("{}", description.as_str().unwrap_or(""));
40 drop(description);
41 drop(account);
42
43 let class = environment.find_defclass("ACCOUNT")?.expect("class exists");
44 for slot in class.slots(false) {
45 let descriptor = class.slot(&slot).expect("slot name has no NUL");
46 if let Some(descriptor) = descriptor {
47 println!(
48 "slot {slot}: types {:?} default {:?}",
49 descriptor.allowed_types(),
50 descriptor.default_value()
51 );
52 }
53 }
54
55 Ok(())
56}Sourcepub fn as_bool(&self) -> Option<bool>
pub fn as_bool(&self) -> Option<bool>
A symbol’s truth value.
CLIPS treats every symbol except FALSE as true, so this returns
Some(true) for any other symbol, and None for non-symbols.
Sourcepub fn as_multifield(&self) -> Option<Multifield<'env>>
pub fn as_multifield(&self) -> Option<Multifield<'env>>
The value as a multifield, or None if it is not one.
Sourcepub fn as_fact(&self) -> Option<Fact<'env>>
pub fn as_fact(&self) -> Option<Fact<'env>>
The value as a retained fact handle, or None if it is not a fact
address.
Sourcepub fn as_instance(&self) -> Option<Instance<'env>>
pub fn as_instance(&self) -> Option<Instance<'env>>
The value as a retained instance handle, or None if it is not an
instance address.