Search code examples
expert-systemclips

Access a slot from multifield in deffunction


I have this function which calculates some value based on multiple slots of multiple multifield facts.

Because quite some slots are involved and all of them are needed in the function I was thinking if I could pass a whole fact to a function and access its slots in it, like so:

(deftemplate a-fact
    (slot id)
    (slot name)
    (slot ...)
    ...
)

(deffunction a-funciton (?factadr)
    (switch ?factadr:name
        (case bla then ...)
    )

    (return ?calculated-value)
)

(defrule a-rule
    ?factadr <- (a-fact (id ?i))
    =>
    (if (> **(a-function ?factadr) 20) then ... )
)

I saw this ?fact-adrres:slot-name in this example and thought it will work but it doesn't. So, is it possible and how to do it?

(bind ?facts (find-all-facts ((?f attribute))
                               (and (eq ?f:name wine)
                                    (>= ?f:certainty 20))))

Clips 6.3 is used.


Solution

  • Use the fact-slot-value function.

    CLIPS> 
    (deftemplate a-fact
       (slot id)
       (slot name))
    CLIPS>  
    (defrule a-rule
       ?f <- (a-fact)
       =>
       (printout t (fact-slot-value ?f id) " " (fact-slot-value ?f name) crlf))
    CLIPS> (assert (a-fact (id 3) (name x)))
    <Fact-1>
    CLIPS> (assert (a-fact (id  7) (name y)))
    <Fact-2>
    CLIPS> (run)
    7 y
    3 x
    CLIPS>