Pergunta

Here is my situation: I want to run the CLIPS periodically and the system can record how many times it runs. for example: I type in the terminal "run" many times to call the system periodically. then the system can record how many the system runs and show it on the screen. Here is my .clp file

(defglobal ?*lock* = 0)

(deftemplate counter
    (slot number))

(deffacts initial_data
    (counter (number 0))
)

(defrule set_counter
    ?f<-(counter (number ?x))
    (test (= ?*lock* 0))
=>
    (bind ?*lock* 1)
    (printout t "plus 1" crlf)
    (modify ?f (number (+ ?x 1)))
)

(defrule show_result
    ?f<-(counter (number ?x))
    (test (= ?*lock* 1))
=>
    (printout t "the counter value has been changed:" crlf)
    (ppfact (fact-index ?f) t)
    (bind ?*lock* 0)
)

I use a global value as a lock to control the rules and store the running times in the fact named counter. Now is my problem: Once the system finishes running for the first time. There are no rules in the agenda any more. I want the system can run again with out resetting the facts and the system can process the facts saved form the first running process. How can I refresh the agenda or rematch the rule without reset the facts?

I have find some commands like (refresh rule-name) and (refresh-agenda) but they can not solve my problem. I just type in "(refresh set_counter)" and "(refresh-agenda)" after "(run)". However, no rules are added into agenda.

I don't know if there are solution to my problem or clips can not work like this?

Another question is I try (refresh rule-name) with

(defglobal ?*lock* = 0)

(deftemplate counter
    (slot number))

(deftemplate set
    (slot number))

(deffacts initial_data
    (counter (number 0))
)
    (defrule set_counter
        (initial-fact)
    =>
        (bind ?*lock* (+ ?*lock* 1))
        (printout t ?*lock* crlf)
    )

It works fine. I don't know why it doesn't work in the first example? really thanks for any advice.

Foi útil?

Solução

Global variables don't trigger pattern matching so you shouldn't use them in the condition of a rule unless the value is never changed. If you use a fact to represent the lock, the rules will execute for as many cycles as you specify:

(deftemplate lock
    (slot value))

(deftemplate counter
    (slot number))

(deffacts initial_data
    (counter (number 0))
    (lock (value 0))
)

(defrule set_counter
    ?f <- (counter (number ?x))
    ?l <- (lock (value 0))
=>
    (modify ?l (value 1))
    (printout t "plus 1" crlf)
    (modify ?f (number (+ ?x 1)))
)

(defrule show_result
    ?f <- (counter (number ?x))
    ?l <- (lock (value 1))
=>
    (printout t "the counter value has been changed:" crlf)
    (ppfact (fact-index ?f) t)
    (modify ?l (value 0))
)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top