How can I print ordered facts in my function in CLIPS?

62 Views Asked by At

I have the following facts: (assert(name "Sara")) (assert(last_name "Jones"))

I would like to print them in a custom format by calling my function.

I have got this function (deffunction print-them () (bind ?n (find-fact ((?n name)) TRUE)) ) This function binds the found fact to a multi field variable ?n which prints like Fact-1, but I do not know how to print the details of the fact such as name is Sara and last_name is Jones. Would you be able to help me? Thanks.

1

There are 1 best solutions below

4
Gary Riley On

You can reference the data inside an ordered (implied deftemplate) fact using the slot name implied. Alternately you can just use the ppfact function to print that data.

CLIPS> (assert (name "Sara"))
<Fact-1>
CLIPS> (assert (name "Joe"))
<Fact-2>
CLIPS> 
(deffunction print-them ()
   (do-for-all-facts ((?n name)) TRUE 
      (println (implode$ ?n:implied))))
CLIPS> (print-them)      
"Sara"
"Joe"
CLIPS> 
(deffunction print-them (?name)
   (do-for-all-facts ((?n ?name)) TRUE 
      (ppfact ?n)))
CLIPS> (print-them name)
(name "Sara")
(name "Joe")
CLIPS>