Search code examples
clipsexpert-system

CLIPS: How to test if a variable's type is a certain class?


I know there are several predicate functions that allow to test if a variable is of a certain type. For example,

(test (integerp ?my_var))

returns TRUE if my_var is of type integer.

Say I have a class A. How do I test if a variable is of type A? That is, I want to test if ?my_var is an instance of A.

I am looking for something like this:

(test (instance_of_A_p ?my_var))

or

(test (is-a (?my_var A))

Solution

  • Use the type function:

    CLIPS> (bind ?a 3)
    3
    CLIPS> (type a)
    SYMBOL
    CLIPS> (defclass A (is-a USER))
    CLIPS> (bind ?a (make-instance a1 of A))
    [a1]
    CLIPS> (type ?a)
    A
    CLIPS> (eq (type ?a) A)
    TRUE
    CLIPS> 
    

    So in the LHS of the rule:

    (test (eq (type ?a) A))