Search code examples
clips

Can a deftemplate construct have another template in a slot?


I have a template defined as follows:

(deftemplate fact
    (slot name)
    (multislot field)
    (multislot value))

Can I have this above defined fact as a slot/multislot in another template?

For example:

(deftemplate collection1
    (slot fact1))

(deftemplate collection2
    (slot fact1)
    (slot fact2))

In the above desired template, each slot fact should be of defined template fact. I want to have such collection of many number of slots. Or at least the desired outcome is to assert collection with fields similar to fact template. So that I could have a logically link between name, fields and values.

(collection2
    (fact1 (name 'ID')
        (field 'Name' 'Company' 'Role')
        (value 'Bob' 'ABC' 'Admin'))
    (fact2 (name 'Event')
        (field 'Name' 'Place')
        (value 'Conference' 'USA')))

Expected outcome : CLIPS could assert the above recursive fact. Is this possible, or any alternative to the problem.

Thank you in advance.


Solution

  • Sure you can, you can also restrict the slot type to specifically be a fact address.

    (deftemplate fact
        (slot name)
        (multislot field)
        (multislot value))
    
    (deftemplate collection
      (slot fact (type FACT-ADDRESS)))
    
    (assert (collection (fact (assert (fact (name "ID")
                                            (field "Name" "Company" "Role")
                                            (value "Bob" "ABC" "Admin"))))))
    

    The facts list:

    (facts)          
    
    f-0     (initial-fact)
    f-1     (fact (name "ID") (field "Name" "Company" "Role") (value "Bob" "ABC" "Admin"))
    f-2     (collection (fact <Fact-1>))