mcventur
mcventur

Reputation: 71

mantain CLIPS running when asserting new facts

It's possible to keep CLIPS running while asserting new facts?

example of execution

It would be possible not to have to make (run) to test this program after each assert command? I mean, making only one (run) and then, fire the rules automatically when i insert new facts.

Thanks in advance!

Upvotes: 0

Views: 55

Answers (1)

Gary Riley
Gary Riley

Reputation: 10757

You can use helper functions to perform both the assertion and the subsequent run:

         CLIPS (6.4 2/9/21)
CLIPS> 
(deffunction push (?value)
   (assert-string (str-cat "(push-value " ?value ")"))
   (run))
CLIPS> 
(deffunction assert* (?value)
   (assert-string ?value)
   (run))
CLIPS> 
(defrule push-to-stack
   ?s <- (stack $?stack)
   ?p <- (push-value ?v)
   =>
   (println "Pushing value " ?v)
   (retract ?s ?p)
   (assert (stack ?v ?stack)))
CLIPS> (assert (stack))
<Fact-1>
CLIPS> (push 3)
Pushing value 3
CLIPS> (assert* "(push-value 4)")
Pushing value 4
CLIPS> 

For testing, you can also create a file containing the commands (as follows), and then use the batch command to automatically execute all the commands in your file.

(assert (stack))
(assert (push-value 3))
(assert (run))
(assert (push-value 4))
(run)
(assert (push-value 9))
(assert (push-value 2))
(run)

Upvotes: 0

Related Questions