Skip to main content

Defclass

Struct Defclass 

Source
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<'_>

Source

pub fn is_abstract(&self) -> bool

Whether the class is abstract, meaning it cannot have direct instances.

Source

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.

Source

pub fn is_reactive(&self) -> bool

Whether instances of the class take part in rule pattern matching.

Source

pub fn slots(&self, inherit: bool) -> Vec<String>

Returns slot names, optionally including inherited slots.

Examples found in repository?
examples/objects_and_messages.rs (line 44)
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

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.

Source

pub fn subclasses(&self, inherit: bool) -> Vec<String>

Returns subclass names, optionally walking the full descendant tree.

Source

pub fn has_slot(&self, name: &str, inherit: bool) -> Result<bool, ClassError>

Whether the class defines or inherits a slot of this name.

Source

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?
examples/objects_and_messages.rs (line 45)
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

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.

Source

pub fn is_subclass_of(&self, other: &Defclass<'_>) -> bool

Whether self is a subclass of other.

Source

pub fn precedence_list(&self) -> Vec<String>

The class precedence list, most specific first, as CLIPS resolves it.

Source

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.

Source

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<'_>

Source

pub fn name(&self) -> &str

The construct’s name, without any module prefix.

Examples found in repository?
examples/objects_and_messages.rs (line 28)
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<'_>

Source

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.

Source

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.

Source

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<'_>

Source

pub fn watch_instances(&self) -> bool

Whether CLIPS traces creation and deletion of this class’s instances.

Source

pub fn set_watch_instances(&mut self, enabled: bool)

Enables or disables tracing of this class’s instance lifecycle.

Source

pub fn watch_slots(&self) -> bool

Whether CLIPS traces writes to this class’s slots.

Source

pub fn set_watch_slots(&mut self, enabled: bool)

Enables or disables tracing of writes to this class’s slots.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<'env> Freeze for Defclass<'env>

§

impl<'env> Unpin for Defclass<'env>

§

impl<'env> UnsafeUnpin for Defclass<'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.