Question

CLIPS is quite new to me - I have been trying to dig deeply into this language for 2 days. A question came to my mind, namely: how (if possible) can I create/add new rules dynamically? I would like to do for example sth like this:

(deftemplate action
 (slot prev)
 (slot curr)
)

(defrule test
   (action (prev ?p))
 =>
   (defrule test_inner
       (action (curr ?p))
     =>
       (printout t "Result of a newly created rule.")
    ) 
)

Please do not pay special attention to the logic of these rules - it is just an example. After invoking above presented commands I receive:

[EXPRNPSR3] Missing function declaration for defrule.

ERROR:
(defrule MAIN::test
   (action (prev ?p))
   =>
   (defrule

Is this error, a matter of command syntax or I can not define new rules "dynamically"?

Was it helpful?

Solution

First create a string containing the rule (or any other construct) and then use the build function:

CLIPS> 
(deftemplate action
   (slot prev)
   (slot curr)
)
CLIPS> 
(defrule test
   (action (prev ?p))
=>
   (build (str-cat
            "(defrule test_inner
                (action (curr " ?p "))
                =>
                (printout t \"Result of a newly created rule.\")
             )"
          ) 
   )
)
CLIPS> (reset)
CLIPS> (assert (action (prev move)))
<Fact-1>
CLIPS> (agenda)
0      test: f-1
For a total of 1 activation.
CLIPS> (run)
CLIPS> (rules)
test
test_inner
For a total of 2 defrules.
CLIPS> (ppdefrule test_inner)
(defrule MAIN::test_inner
   (action (curr move))
   =>
   (printout t "Result of a newly created rule."))
CLIPS> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top