Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

How can I run the clips with out reset the fact when using CLIPS

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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))
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...