I know that rules in CLIPS are usually given names using defrule. This rule is called rule1:
(deftemplate animal (slot species))
(deffacts animal
(animal (species dog)))
(defrule rule1
(animal (species ?name))
=>
(printout t ?name crlf))
(reset)
(run)
(exit)
;
But I still don't know whether it is necessary for the rules to be named. Is it possible to define a rule without giving it a name, like this?
(deftemplate animal (slot species))
(deffacts animal
(animal (species dog)))
(defrule
(animal (species ?name))
=>
(printout t ?name crlf))
(reset)
(run)
(exit)
;
From section 5 of the Basic Programming Guide:
(defrule <rule-name> [<comment>]
[<declaration>]
<conditional-element>*
=>
<action>*)
The rule name is required.
To dynamically generate a rule name, use the gensym* function which creates a unique symbol:
CLIPS>
(deffunction create-anonymous (?conditions ?actions)
(bind ?str (str-cat "(defrule " (gensym*) " "
?conditions " => " ?actions ")"))
(build ?str))
CLIPS> (deftemplate animal (slot species))
CLIPS>
(create-anonymous "(animal (species ?name))"
"(printout t ?name crlf)")
TRUE
CLIPS> (rules)
gen1
For a total of 1 defrule.
CLIPS> (ppdefrule gen1)
(defrule MAIN::gen1
(animal (species ?name))
=>
(printout t ?name crlf))
CLIPS>
Related
Hi i have created a clips program and in my defrule negative i am simulating a propositional law and would like the program to accept any symbol and not just a P symbol. I want the defrule to fire no matter the symbol that is there.
I have tried putting ?symbol instead of the P but that has not solved my problem just yet.
(deftemplate andprop (slot symbol1)(slot symbol2))
(deftemplate orprop (slot symbol1)(slot symbol2))
(deftemplate implies (multislot premise)(multislot implication))
(deftemplate sentence (multislot sent))
(defrule read-from-user
=>
(printout t "Please enter a sentence: Use ~ for not and => for implies please " crlf)
(bind ?response (readline))
(assert (sentence(sent ?response))))
(defrule negative
(sentence(sent "~(~P)"))
=>
(printout t "HI " crlf))
So i want the defrule negative to fire no matter the symbol that is in symbol so nt only if the symbol is a P
I'd suggest using the explode$ function to split the sentence into separate tokens.
CLIPS (6.31 2/3/18)
CLIPS>
(deftemplate andprop
(slot symbol1)
(slot symbol2))
CLIPS>
(deftemplate orprop
(slot symbol1)
(slot symbol2))
CLIPS>
(deftemplate implies
(multislot premise)
(multislot implication))
CLIPS>
(deftemplate sentence
(multislot sent))
CLIPS>
(defrule read-from-user
=>
(printout t "Please enter a sentence: Use ~ for not and => for implies please " crlf)
(bind ?response (readline))
(assert (sentence (sent (explode$ ?response)))))
CLIPS>
(defrule negative
(sentence (sent "~" "(" "~" ?symbol ")"))
=>
(printout t "HI " ?symbol crlf))
CLIPS> (run)
Please enter a sentence: Use ~ for not and => for implies please
~(~P)
HI P
CLIPS> (reset)
CLIPS> (run)
Please enter a sentence: Use ~ for not and => for implies please
~(~XYZ)
HI XYZ
CLIPS> (facts)
f-0 (initial-fact)
f-1 (sentence (sent "~" "(" "~" XYZ ")"))
For a total of 2 facts.
CLIPS>
(deftemplate andprop (slot symbol1)(slot symbol2))
(deftemplate orprop (slot symbol1)(slot symbol2))
(deftemplate implies (multislot premise)(multislot
implication))
(deftemplate sentence (multislot sent))
(defrule read-from-user
=>
(printout t "Please enter a value: " crlf)
(bind ?response (read))
(assert (sentence (sent andprop(symbol1 ?response)
(symbol2 ?response)))))
I am writing a CLIPS program to simulate the propsitional logic identities but when I try to run this piece of code i get the following
[EXPRNPSR3] Missing function declaration for 'symbol1'.
ERROR:
(defrule MAIN::read-from-user
=>
(printout t "Please enter a value: " crlf)
(bind ?response (read))
(assert (sentence (sent andprop (symbol1
You can set the value of a slot to another fact:
CLIPS (6.31 2/3/18)
CLIPS>
(deftemplate andprop
(slot symbol1)
(slot symbol2))
CLIPS>
(deftemplate orprop
(slot symbol1)
(slot symbol2))
CLIPS>
(deftemplate implies
(multislot premise)
(multislot implication))
CLIPS>
(deftemplate sentence
(multislot sent))
CLIPS>
(defrule read-from-user
=>
(printout t "Please enter a value: " crlf)
(bind ?response (read))
(bind ?sent (assert (andprop (symbol1 ?response)
(symbol2 ?response))))
(assert (sentence (sent ?sent))))
CLIPS> (run)
Please enter a value:
45678
CLIPS> (facts)
f-0 (initial-fact)
f-1 (andprop (symbol1 45678) (symbol2 45678))
f-2 (sentence (sent <Fact-1>))
For a total of 3 facts.
CLIPS>
(deftemplate client
(slot idClient (type INTEGER))
(multislot nome)
(multislot birthdate)
(multislot registryCard)
(multislot endOfRegistryCard))
(deffunction reservation(?idfunc)
(build (str-cat"(defrule existsClient
(exists(client(idClient ?idfunc)))
=>
(printout t "exists" ?idfunc crlf))"
))
(run)
)
I made this deffunction and I want to see if exists the client with that idfunc that is received as parameter. what happens is that the defrule inside doesn't process this variable any thoughts how can i resolve?
I think you should set condition (assert your idfunc) and then fire the execution of your "existsClient" rule (defined as rule and not inside your function). That would be a clearer design in my opinion.
Typically you'll directly define your rules rather than using a deffunction, but here's how you can do it both ways:
CLIPS> (clear) ; Create rule and run with deffunction
CLIPS>
(deftemplate client
(slot idClient (type INTEGER))
(multislot nome)
(multislot birthdate)
(multislot registryCard)
(multislot endOfRegistryCard))
CLIPS>
(deffunction reservation (?idfunc)
(build (str-cat
"(defrule existsClient
(exists (client (idClient " ?idfunc ")))
=>
(printout t \"exists " ?idfunc "\" crlf))"))
(assert (client (idClient ?idfunc)))
(run))
CLIPS> (reservation 2)
exists 2
CLIPS> (ppdefrule existsClient)
(defrule MAIN::existsClient
(exists
(client (idClient 2)))
=>
(printout t "exists 2" crlf))
CLIPS> (clear) ; Create rule directly
CLIPS>
(deftemplate client
(slot idClient (type INTEGER))
(multislot nome)
(multislot birthdate)
(multislot registryCard)
(multislot endOfRegistryCard))
CLIPS>
(defrule existsClient
(exists (client (idClient 2)))
=>
(printout t "exists 2" crlf))
CLIPS> (ppdefrule existsClient)
(defrule MAIN::existsClient
(exists
(client (idClient 2)))
=>
(printout t "exists 2" crlf))
CLIPS> (assert (client (idClient 2)))
<Fact-1>
CLIPS> (run)
exists 2
CLIPS>
In my system the user inputs a Y or N to answer simple questions. I call this rule after every question to increment a counter. There are some general problems with my code but i can't see where
(defrule QPain
(initial-fact)
=>
(printout t "Are You In Pain? " crlf)
(bind ?*Answer* (read))
)
(defrule IncSym
(test(=(str-compare (?*Answer*) "y")0))
=>
(bind ?*symcount* (+ ?*symcount* 1))
)
Thanks
The syntactic errors can be corrected as follows:
CLIPS> (clear)
CLIPS> (defglobal ?*Answer* = nil)
CLIPS> (defglobal ?*symcount* = 0)
CLIPS>
(defrule QPain
=>
(printout t "Are you in pain? ")
(bind ?*Answer* (read)))
CLIPS>
(defrule IncSym
(test (eq ?*Answer* y))
=>
(bind ?*symcount* (+ ?*symcount* 1)))
CLIPS> (reset)
CLIPS> (run)
Are you in pain? y
CLIPS> (show-defglobals)
?*Answer* = y
?*symcount* = 0
CLIPS>
This won't produce the behavior you're expecting, however, since ?*symcount* will not be incremented. The behavior of global variables and why you should not be using them in the manner you're attempting has been discussed previously:
How exactly (refresh) works in the clips?
CLIPS: forcing a rule to re-evaluate the value of a global variable?
Number equality test fails in CLIPS pattern matching?
CLIPS constant compiler directive
How can I run the clips with out reset the fact when using CLIPS
Instead of using global variables to track responses and symptoms, you should use facts or instances. Here's one approach:
CLIPS> (clear)
CLIPS>
(deftemplate symptom
(slot id)
(slot response))
CLIPS>
(deftemplate symptom-list
(multislot values))
CLIPS>
(deffacts initial
(symptom-list))
CLIPS>
(defrule QPain
=>
(printout t "Are you in pain? ")
(assert (symptom (id in-pain) (response (read)))))
CLIPS>
(defrule IncSym
(symptom (id ?id) (response y))
?f <- (symptom-list (values $?list))
(test (not (member$ ?id ?list)))
=>
(modify ?f (values ?list ?id)))
CLIPS>
(defrule symptoms-found
(declare (salience -10))
(symptom-list (values $?list))
=>
(printout t "Symptom count: " (length$ ?list) crlf))
CLIPS> (reset)
CLIPS> (run)
Are you in pain? y
Symptom count: 1
CLIPS> (reset)
CLIPS> (run)
Are you in pain? n
Symptom count: 0
CLIPS>
And another:
CLIPS> (clear)
CLIPS>
(deftemplate symptom
(slot id)
(slot response))
CLIPS>
(defrule QPain
=>
(printout t "Are you in pain? ")
(assert (symptom (id in-pain) (response (read)))))
CLIPS>
(defrule symptoms-found
(declare (salience -10))
=>
(bind ?count (find-all-facts ((?f symptom)) (eq ?f:response y)))
(printout t "Symptom count: " (length$ ?count) crlf))
CLIPS> (reset)
CLIPS> (run)
Are you in pain? y
Symptom count: 1
CLIPS> (reset)
CLIPS> (run)
Are you in pain? n
Symptom count: 0
CLIPS>
Is there a way for me to get the fact ID of a fact that I just asserted in the RHS of a rule? Something along the lines of
?f <- (assert (new-fact))
CLIPS>
(defrule example
=>
(bind ?f (assert (new-fact)))
(bind ?i (fact-index ?f))
(printout t "The fact index is " ?i crlf))
CLIPS> (reset)
CLIPS> (run)
The fact index is 1
CLIPS> (facts)
f-0 (initial-fact)
f-1 (new-fact)
For a total of 2 facts.
CLIPS>