Search code examples
oopclips

How to define a class within another class in CLIPS?


Suppose, I have a defined class Coin in CLIPS and also I have a class Board

(defclass Coin 
          (is-a USER)
          (role concrete)
   (slot Side (type SYMBOL) (allowed-symbols Head Tail))
)

How to properly defined a class Board with three coins on it? Something like this:

(defclass Board
          (is-a USER)
          (role concrete)
   (slot CoinOne (type Coin))
   (slot CoinTwo (type Coin))
   (slot CoinThree (type Coin))
)
              

Solution

  • Here's one way you can automatically populate instances of one class within another class:

             CLIPS (6.31 6/12/19)
    CLIPS> 
    (defclass Coin 
       (is-a USER)
       (role concrete)
       (slot Side (type SYMBOL) (allowed-symbols Head Tail)))
    CLIPS> 
    (defclass Board
       (is-a USER)
       (role concrete)
       (slot CoinOne 
          (type INSTANCE)
          (allowed-classes Coin)
          (default-dynamic (make-instance of Coin)))
       (slot CoinTwo
          (type INSTANCE)
          (allowed-classes Coin)
          (default-dynamic (make-instance of Coin)))
       (slot CoinThree
          (type INSTANCE)
          (allowed-classes Coin)
          (default-dynamic (make-instance of Coin))))
    CLIPS> (make-instance [b1] of Board)
    [b1]
    CLIPS> (instances)
    [initial-object] of INITIAL-OBJECT
    [b1] of Board
    [gen1] of Coin
    [gen2] of Coin
    [gen3] of Coin
    For a total of 5 instances.
    CLIPS> (send [b1] print)
    [b1] of Board
    (CoinOne [gen1])
    (CoinTwo [gen2])
    (CoinThree [gen3])
    CLIPS> (send [gen1] print)
    [gen1] of Coin
    (Side Head)
    CLIPS>