pub struct Instance<'env> { /* private fields */ }Expand description
A COOL instance, retained for as long as the handle lives.
Borrowing the environment is what keeps the handle sound: operations that
could invalidate it, such as clear or reset, take &mut Environment and
so cannot run while one exists.
Implementations§
Source§impl<'env> Instance<'env>
impl<'env> Instance<'env>
Sourcepub fn name(&self) -> &str
pub fn name(&self) -> &str
The instance’s name, or "" once the instance has been unmade.
Retaining an instance keeps its record alive but not its name symbol,
which CLIPS releases on unmake-instance; reading the name afterwards
would dereference freed memory, so it is checked first.
Sourcepub fn is_deleted(&self) -> bool
pub fn is_deleted(&self) -> bool
Whether the instance has been deleted.
The handle stays valid: CLIPS keeps the record alive while it is retained, so a deleted instance can still be queried rather than dangling.
Sourcepub fn pretty_print(&self) -> String
pub fn pretty_print(&self) -> String
The instance rendered as make-instance syntax.
Empty if CLIPS could not allocate the string builder it prints through.
Sourcepub fn class(&self) -> Option<Defclass<'env>>
pub fn class(&self) -> Option<Defclass<'env>>
The class this instance belongs to.
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}Sourcepub fn send(&self, message: &str) -> Result<MessageCall<'env>>
pub fn send(&self, message: &str) -> Result<MessageCall<'env>>
Starts a message send to this instance.
Arguments are appended with the typed methods of MessageCall, so no
CLIPS argument text is assembled or parsed by the caller.
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}Sourcepub fn modifier(&self) -> Result<InstanceModifier<'env>>
pub fn modifier(&self) -> Result<InstanceModifier<'env>>
Starts a batched modification.
Unlike the put_* methods, slot writes are collected and applied as one
change when InstanceModifier::modify is called, so daemons and
pattern matching run once rather than per slot.
Sourcepub fn put_integer(&self, slot: &str, value: i64) -> Result<&Self>
pub fn put_integer(&self, slot: &str, value: i64) -> Result<&Self>
Writes a slot directly, bypassing message passing.
Unlike InstanceModifier, each call takes effect immediately, so
slot-level daemons and pattern matching run per assignment.
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}Sourcepub fn put_float(&self, slot: &str, value: f64) -> Result<&Self>
pub fn put_float(&self, slot: &str, value: f64) -> Result<&Self>
Writes a float slot directly. See Instance::put_integer.
Sourcepub fn put_symbol(&self, slot: &str, value: &str) -> Result<&Self>
pub fn put_symbol(&self, slot: &str, value: &str) -> Result<&Self>
Writes a symbol slot directly. See Instance::put_integer.
Sourcepub fn put_string(&self, slot: &str, value: &str) -> Result<&Self>
pub fn put_string(&self, slot: &str, value: &str) -> Result<&Self>
Writes a string slot directly. See Instance::put_integer.
Sourcepub fn put_instance_name(&self, slot: &str, value: &str) -> Result<&Self>
pub fn put_instance_name(&self, slot: &str, value: &str) -> Result<&Self>
Writes an instance-name slot directly. See Instance::put_integer.
Sourcepub fn put_fact(&self, slot: &str, value: &Fact<'env>) -> Result<&Self>
pub fn put_fact(&self, slot: &str, value: &Fact<'env>) -> Result<&Self>
Writes a fact address to a slot directly. See Instance::put_integer.
Sourcepub fn put_instance(&self, slot: &str, value: &Instance<'env>) -> Result<&Self>
pub fn put_instance(&self, slot: &str, value: &Instance<'env>) -> Result<&Self>
Writes an instance address to a slot directly. See
Instance::put_integer.
Sourcepub fn put_multifield(
&self,
slot: &str,
value: &Multifield<'env>,
) -> Result<&Self>
pub fn put_multifield( &self, slot: &str, value: &Multifield<'env>, ) -> Result<&Self>
Writes a multifield slot directly. See Instance::put_integer.