pub struct Defclass<'env> { /* private fields */ }Expand description
A handle to one Defclass construct.
Borrowing the environment is what keeps it sound: undefining a
construct, clearing, or loading a binary image all need
&mut Environment and so cannot run while a handle is alive.
Implementations§
Source§impl Defclass<'_>
impl Defclass<'_>
Sourcepub fn is_abstract(&self) -> bool
pub fn is_abstract(&self) -> bool
Whether the class is abstract, meaning it cannot have direct instances.
Sourcepub fn preview_send(&self, logical_name: &str, message: &str) -> Result<()>
pub fn preview_send(&self, logical_name: &str, message: &str) -> Result<()>
Prints the handlers a send of message would run, in execution order,
to the logical_name output stream.
CLIPS only offers this as printed text, so capture it with a
crate::Router registered for logical_name. Nothing is printed if
no handler applies.
Sourcepub fn is_reactive(&self) -> bool
pub fn is_reactive(&self) -> bool
Whether instances of the class take part in rule pattern matching.
Sourcepub fn slots(&self, inherit: bool) -> Vec<String>
pub fn slots(&self, inherit: bool) -> Vec<String>
Returns slot names, optionally including inherited slots.
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 superclasses(&self, inherit: bool) -> Vec<String>
pub fn superclasses(&self, inherit: bool) -> Vec<String>
Returns superclass names, nearest first when inherit is false and the
full precedence chain when it is true.
Sourcepub fn subclasses(&self, inherit: bool) -> Vec<String>
pub fn subclasses(&self, inherit: bool) -> Vec<String>
Returns subclass names, optionally walking the full descendant tree.
Sourcepub fn has_slot(&self, name: &str, inherit: bool) -> Result<bool, ClassError>
pub fn has_slot(&self, name: &str, inherit: bool) -> Result<bool, ClassError>
Whether the class defines or inherits a slot of this name.
Sourcepub fn slot(&self, name: &str) -> Result<Option<ClassSlot<'_>>, ClassError>
pub fn slot(&self, name: &str) -> Result<Option<ClassSlot<'_>>, ClassError>
Borrows a slot for constraint and access inspection.
Returns None when the class has no such slot, including inherited ones.
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 is_superclass_of(&self, other: &Defclass<'_>) -> bool
pub fn is_superclass_of(&self, other: &Defclass<'_>) -> bool
Whether self is a superclass of other, that is, whether other
inherits from it.
Both handles borrow the same environment, so no cross-environment comparison is expressible.
Sourcepub fn is_subclass_of(&self, other: &Defclass<'_>) -> bool
pub fn is_subclass_of(&self, other: &Defclass<'_>) -> bool
Whether self is a subclass of other.
Sourcepub fn precedence_list(&self) -> Vec<String>
pub fn precedence_list(&self) -> Vec<String>
The class precedence list, most specific first, as CLIPS resolves it.
Sourcepub fn message_handlers(&self) -> MessageHandlers<'_> ⓘ
pub fn message_handlers(&self) -> MessageHandlers<'_> ⓘ
Iterates the handlers defined on this class itself.
Inherited handlers are not included; walk
Defclass::precedence_list to see those.
Sourcepub fn find_message_handler(
&self,
name: &str,
kind: HandlerType,
) -> Result<Option<MessageHandler<'_>>, ClassError>
pub fn find_message_handler( &self, name: &str, kind: HandlerType, ) -> Result<Option<MessageHandler<'_>>, ClassError>
Looks up one of this class’s own handlers by message name and type.
Source§impl Defclass<'_>
impl Defclass<'_>
Sourcepub fn name(&self) -> &str
pub fn name(&self) -> &str
The construct’s name, without any module prefix.
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}Source§impl Defclass<'_>
impl Defclass<'_>
Sourcepub fn module_name(&self) -> &str
pub fn module_name(&self) -> &str
The name of the defmodule this construct belongs to.
Empty if CLIPS reports no module, which the safe API should not be able to produce.
Sourcepub fn pretty_print(&self) -> Option<&str>
pub fn pretty_print(&self) -> Option<&str>
The construct’s source text as CLIPS reformats it.
None when no pretty-print form is stored: CLIPS discards them
for constructs defined while pretty-printing was disabled, and
for everything restored from a binary image.
Sourcepub fn is_deletable(&self) -> bool
pub fn is_deletable(&self) -> bool
Whether the construct can currently be undefined.
CLIPS refuses while the construct is in use, for example a deftemplate with existing facts or a class with instances.
Source§impl Defclass<'_>
impl Defclass<'_>
Sourcepub fn watch_instances(&self) -> bool
pub fn watch_instances(&self) -> bool
Whether CLIPS traces creation and deletion of this class’s instances.
Sourcepub fn set_watch_instances(&mut self, enabled: bool)
pub fn set_watch_instances(&mut self, enabled: bool)
Enables or disables tracing of this class’s instance lifecycle.
Sourcepub fn watch_slots(&self) -> bool
pub fn watch_slots(&self) -> bool
Whether CLIPS traces writes to this class’s slots.
Sourcepub fn set_watch_slots(&mut self, enabled: bool)
pub fn set_watch_slots(&mut self, enabled: bool)
Enables or disables tracing of writes to this class’s slots.