How to do a logical or in CLIPS? Syntax error - clips

I need to use the logical operator or in this case but I get this syntax error. What is the exactly syntax to write this?
CLIPS>
(defrule case2
(or ((PNP Y) (PLF Y) (PIU Y))
((PNP Y) (PLF N) (PIU Y)))
=>
(printout t "- Check the printer-computer cable" crlf))
[PRNTUTIL2] Syntax Error: Check appropriate syntax for the first field of a pattern.
ERROR:
(defrule MAIN::case2
(or ((
CLIPS>

Any of the following ways are syntactically correct and will produce the same results:
(defrule case2
(or (and (PNP Y) (PLF Y) (PIU Y))
(and (PNP Y) (PLF N) (PIU Y)))
=>
(printout t "- Check the printer-computer cable" crlf))
(defrule case2
(PNP Y)
(or (PLF Y)
(PLF N))
(PIU Y)
=>
(printout t "- Check the printer-computer cable" crlf))
(defrule case2
(PNP Y)
(PLF Y | N)
(PIU Y)
=>
(printout t "- Check the printer-computer cable" crlf))

Related

how to modify ordered (non template ) fact, say the nth value?

modify is not working. I expected fact 1 to be (a x y z).
Further, if I want to change the second element c of the fact 1 to say g, i.e the new fact should be (a b g d) is there a way using modify ?
Snippet attached below.
CLIPS> (assert (a b c d))
<Fact-1>
CLIPS> (bind ?s x y z)
(x y z)
CLIPS> (facts)
f-1 (a b c d)
For a total of 1 fact.
CLIPS> ?s
(x y z)
CLIPS> (modify 1 (implied ?s))
FALSE
CLIPS> (facts)
f-1 (a b c d)
For a total of 1 fact.
CLIPS>
Modify only works with template facts. If you're using ordered facts, you need to do a retract and assert:
CLIPS> (assert (a b c d))
<Fact-1>
CLIPS> (bind ?s x y z)
(x y z)
CLIPS> (retract 1)
CLIPS> (assert (a ?s))
<Fact-2>
CLIPS> (facts)
f-2 (a x y z)
For a total of 1 fact.
CLIPS>
Use the replace$ function to replace values in a multifield value before asserting it as part of a fact:
CLIPS> (bind ?s (replace$ ?s 2 2 g))
(x g z)
CLIPS>

Expected argument #1 to be of type integer or float on CLIPS code

I have a problem with my code that is to recommend a herbicide and an application rate for it as appropriate in a given field situation.
(deftemplate plant
(multislot weed)
(multislot crop))
(deftemplate Herbicide
(slot orgmatter)
(slot sencor)
(slot lasso)
(slot bicep))
(deffacts p
(plant (weed B) (crop C S))
(plant (weed B G) (crop C S))
(plant (weed B G) (crop C)))
(deffacts H
(Herbicide (orgmatter 1) (sencor 0.0) (lasso 2.0) (bicep 1.5))
(Herbicide (orgmatter 2) (sencor 0.75) (lasso 1.0) (bicep 2.5))
(Herbicide (orgmatter 3) (sencor 0.75) (lasso 0.5) (bicep 3.0)))
(defrule read-input
=>
(printout t "what is type of crop? (C:Corn , S:Soyabeans): ")
(assert (crop(read)))
(printout t "what is type of weed? (B:broadleaf , G:gress): ")
(assert (weed(read)))
(printout t "what is the organic matter? (1:<2% ,2: 2-4%, 3: >4%: ")
(assert (orgmatter(read))))
(defrule check-input
(crop ?crop)
(weed ?weed)
(orgmatter ? orgmatter)
(plant (weed $?weed1) (crop $?crop1))
(Herbicide (orgmatter ?orgmatter1) (sencor ?sencor1) (lasso ?lasso1)(bicep ?bicep1))
(test (member$ ?crop ?crop1))
(test (member$ ?weed ?weed1))
(test (= orgmatter ?orgmatter1))
=>
(printout t "you can use" ?sencor1 " pt/ac of sencor" crlf)
(printout t "you can use" ?lasso1 " pt/ac of lasso" crlf)
(printout t "you can use" ?bicep1 " pt/ac of bicep" crlf)))
The error is the following: Function = expected argument#1 to be of type integer or float
Your code has an extra ) at the end.
In defrule check-input, you have a test:
(test (= orgmatter ?orgmatter1))
Which is comparing a SYMBOL orgmatter with the variable ?orgmatter1. The = test works only with numerals. If you want to compare SYMBOLs or STRINGs, you need to use the eq function.
(test (eq orgmatter ?orgmatter1))
Nevertheless if you are not using ?orgmatter1 anywhere else, it is more effective to do a literal match rather than a test.
(Herbicide (orgmatter orgmatter)
(sencor ?sencor1)
(lasso ?lasso1)
(bicep ?bicep1))

Rule CLIPS to PROLOG

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

Get back to above rule in clips

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

CLIPS defrules compare the value of two variables

I want to write a rule that says the following
if x > y => assert x
where x and y are variables and their values are given as facts.
How do I do it?
If x already exists as a fact, then asserting it again from the actions of the rule would be unnecessary, but if you want to assert a fact indicating that x is greater than y then you could do it this way:
CLIPS>
(defrule greater-than
(x ?x)
(y ?y)
(test (> ?x ?y))
=>
(assert (x-is-greater-than-y)))
CLIPS> (assert (x 4))
<Fact-1>
CLIPS> (assert (y 1))
<Fact-2>
CLIPS> (agenda)
0 greater-than: f-1,f-2
For a total of 1 activation.
CLIPS> (run)
CLIPS> (facts)
f-0 (initial-fact)
f-1 (x 4)
f-2 (y 1)
f-3 (x-is-greater-than-y)
For a total of 4 facts.
CLIPS>

Resources