I have a do-for-all-facts construct with logical conditions to filter only some facts:
(do-for-all-facts ((?hotel albergo))
(eq ?hotel:localita ?current-city)
(>= ?hotel:disponibilita (/ ?number-of-people 2))
(<= (* ?hotel:prezzo ?current-days) ?budget-for-locality)
(printout t (>= ?hotel:disponibilita (/ ?number-of-people 2)) crlf)
(if (> ?hotel:disponibilita ?max)
then
(bind ?most-available-hotel ?hotel)
(bind ?max ?hotel:disponibilita)
)
)
It seems that doesn't filter properly, because the
(printout t (>= ?hotel:disponibilita (/ ?number-of-people 2)) crlf)
line prints FALSE. How can I value in logical AND all the conditions in do-for-all-facts construct?
I solved the problem by using only one condition in the fact-set-query.
Related
I want to add a else part for below code as (assert (diabetes-assume false)). I glad if someone can help me to fix this issue.
(defrule diabetes-adult
(disease ?d)
(body-status ?b)
(age ?a&:(and (eq ?d none) (>= ?a 19) (< ?a 51) (or (eq ?b obesity) (eq ?b overweight) )))
=>
(assert (diabetes-assume true)))
You can assume as part of your initial state that diabetes is false and then define a higher salience rule (than the rules which rely on that assumption) to change the assumption to true:
(deffacts assumptions
(diabetes-assume false))
(defrule diabetes-adult-true
(declare (salience 10))
?f <- (diabetes-assume false)
(disease ?d)
(body-status ?b)
(age ?a&:(and (eq ?d none)
(>= ?a 19)
(< ?a 51)
(or (eq ?b obesity) (eq ?b overweight))))
=>
(retract ?f)
(assert (diabetes-assume true)))
What are the differences in creating the rules between CLIPS and PROLOG?
If I have a CLIPS rule, as in the example, what would be its translation in PROLOG?
(defrule Rule1
(declare (salience 1))
?a<-(factf (classfact agg|sost|nonst|scon)(valore ?x)(idstruttura ?id)(pos ?n) )
?canc<-(fn (inter[ ?sin)(componenti $?comp)(genere ?gen)(numero ?num)(]inter ?des) )
(not (fn (inter[ ?sin)(]inter ?n)))
(test (= ?n (+ 1 ?des)))
=>
(retract ?canc)
(bind ?*idstruct* (+ 1 ?*idstruct*))
(printout t crlf"********** Rule1 **********"crlf)
(assert (fn (inter[ ?sin)(componenti $?comp ?id)(genere ?gen)(numero ?num)(]inter ?n)(idstruttura ?*idstruct*)))
)
when I try to call a defrule that have been used already, clips stop..
some defrule need to be used more than one time, is there any way to do it
here is an example
(
defrule choice-in-powerPlant2
(powerPlant2-question)
=>
(printout t "Are Filter and Carburetor Air working fine(y/n)?" crlf)
(bind ?response (check-YNoptions-input)); Get user input on type of questions
(if (eq ?response y)
then
(assert (powerPlant1-question)
)
)
(if (or(eq ?response q) (eq ?response Q))
then
(output-exitmessage)
)
(if (eq ?response n)
then
(printout t "Have you fixed this(y/n)?" crlf)
(bind ?response (check-YNoptions-input)); Get user input on type of questions
(if (eq ?response y)
then
(assert (powerPlant1-question)
)
)
(if (eq ?response n)
then
(printout req "Please replace Filter and Carburetor Air " crlf)
(assert (powerPlant3-question))
)
)
)
in rule 2
I want to go back to rule 1 when I enter "y"=yes
" running stopped once I enterd "y" "
If you want to retrigger a rule that matches a specific fact, retract that fact as part of the rule action. If another rule then asserts that specific fact, the rule will be retriggered. For example:
(defrule choice-in-powerPlant2
?f <- (powerPlant2-question)
=>
(retract ?f)
(printout t "Are Filter and Carburetor Air working fine(y/n)?" crlf)
.
.
.
)
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.
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))
)
Let's say that I have a rule like this:
(defrule get_next_N_poz
?id <- (get_next_poz $?)
(world (limit $?) (ball ?b1 ?b2) (men $? ?x ?y - $?) (id ?))
(and
(test (= ?x ?b1))
(test (= ?y (- ?b2 1))))
=>
(printout t "north ready position:" ?x ?y)
(modify ?id (get_next_poz 1)))
How do I add a new "and"?
Thank you.
It depends on what logic you're trying to implement. The existing and you have is redundant anyway, but if you wanted a second one, you'd just add it after the end of the last:
(and
(test (= ?x ?b1))
(test (= ?y (- ?b2 1))))
(and
(test (= ?x ?b2))
(test (= ?y (+ ?b1 1))))
If you wanted one or the other of these conditions you'd do this:
(or (and
(test (= ?x ?b1))
(test (= ?y (- ?b2 1))))
(and
(test (= ?x ?b2))
(test (= ?y (+ ?b1 1)))))
Rather than using and/or conditional elements, you could use the and/or boolean functions within a single test conditional element:
(test (or (and (= ?x ?b1)
(= ?y (- ?b2 1)))
(and (= ?x ?b2)
(= ?y (+ ?b1 1)))))