How do you remove one facts in CLIPS? - clips

How do you remove one facts in CLIPS? The fact would be entered by a person and compared with the base was present, it deletes.
I tried so:
(defrule Deleting::ruleDeleteOneSynSoftgoal "This rule delete one synsoftagoal found in the basis of fact."
(declare (salience 42))
(printout t "Enter below the two softgoals field that want to be deleting:" crlf crlf
"the synonyms of the <[TYPE]QUALITY ATTRIBUTE> and the <[TOPIC]SUBJECT/OBJECT LAL> need to be entered one per line." crlf crlf)
(bind ?dsyntype (readline))
(bind ?dsyntopic (readline))
?fact3 <- (synSoftgoal
(ttId ?ttId3)
(syntopic ?syntopic3)
(syntype ?syntype3)
)
(test (and (eq ?dsyntopic ?syntopic3) (eq ?dsyntype ?syntype3)))
=>
(retract ?fact3
)
But, it is show this erro:
[PRNTUTIL2] Syntax Error: Check appropriate syntax for defrule.
ERROR:
(defrule Deleting::ruleDeleteOneSynSoftgoal "This rule delete one synsoftagoal found in the basis of fact."
(declare (salience 42))
(printout t "Enter below the two softgoals field that want to be deleting:" crlf crlf "the synonyms of the <[TYPE]QUALITY ATTRIBUTE> and the <[TOPIC]SUBJECT/OBJECT LAL> need to be entered one per line." crlf crlf)
(bind ?dsyntype (
Can you help me?

The condition of a rule should be used for matching facts/instances and the action portion of the rule is where you would perform actions such as printing output and receiving input.
You can either use a separate rule to query the user and another to delete the fact, or use the fact set query functions to search and delete the fact from the query rule.
CLIPS>
(deftemplate synSoftgoal
(slot ttId)
(slot syntopic)
(slot syntype))
CLIPS>
(deffacts initial
(synSoftgoal (ttId 1) (syntopic "A") (syntype "1"))
(synSoftgoal (ttId 2) (syntopic "A") (syntype "2"))
(synSoftgoal (ttId 3) (syntopic "B") (syntype "1"))
(synSoftgoal (ttId 4) (syntopic "B") (syntype "2")))
CLIPS>
(defrule QueryRule
=>
(printout t "Enter below the two softgoals field that want to be deleting:" crlf crlf
"the synonyms of the <[TYPE]QUALITY ATTRIBUTE> and the <[TOPIC]SUBJECT/OBJECT LAL> need to be entered one per line." crlf crlf)
(assert (dsyntype (readline)))
(assert (dsyntopic (readline))))
CLIPS>
(defrule DeleteRule
?fact1 <- (dsyntype ?dsyntype)
?fact2 <- (dsyntopic ?dsyntopic)
?fact3 <- (synSoftgoal
(ttId ?ttId3)
(syntopic ?dsyntopic)
(syntype ?dsyntype))
=>
(retract ?fact1 ?fact2 ?fact3))
CLIPS> (reset)
CLIPS> (facts)
f-0 (initial-fact)
f-1 (synSoftgoal (ttId 1) (syntopic "A") (syntype "1"))
f-2 (synSoftgoal (ttId 2) (syntopic "A") (syntype "2"))
f-3 (synSoftgoal (ttId 3) (syntopic "B") (syntype "1"))
f-4 (synSoftgoal (ttId 4) (syntopic "B") (syntype "2"))
For a total of 5 facts.
CLIPS> (run)
Enter below the two softgoals field that want to be deleting:
the synonyms of the <[TYPE]QUALITY ATTRIBUTE> and the <[TOPIC]SUBJECT/OBJECT LAL> need to be entered one per line.
2
A
CLIPS> (facts)
f-0 (initial-fact)
f-1 (synSoftgoal (ttId 1) (syntopic "A") (syntype "1"))
f-3 (synSoftgoal (ttId 3) (syntopic "B") (syntype "1"))
f-4 (synSoftgoal (ttId 4) (syntopic "B") (syntype "2"))
For a total of 4 facts.
CLIPS> (clear)
CLIPS>
(deftemplate synSoftgoal
(slot ttId)
(slot syntopic)
(slot syntype))
CLIPS>
(deffacts initial
(synSoftgoal (ttId 1) (syntopic "A") (syntype "1"))
(synSoftgoal (ttId 2) (syntopic "A") (syntype "2"))
(synSoftgoal (ttId 3) (syntopic "B") (syntype "1"))
(synSoftgoal (ttId 4) (syntopic "B") (syntype "2")))
CLIPS>
(defrule QueryAndDeleteRule
=>
(printout t "Enter below the two softgoals field that want to be deleting:" crlf crlf
"the synonyms of the <[TYPE]QUALITY ATTRIBUTE> and the <[TOPIC]SUBJECT/OBJECT LAL> need to be entered one per line." crlf crlf)
(bind ?dsyntype (readline))
(bind ?dsyntopic (readline))
(do-for-all-facts ((?f synSoftgoal)) (and (eq ?f:syntopic ?dsyntopic) (eq ?f:syntype ?dsyntype))
(retract ?f)))
CLIPS> (reset)
CLIPS> (facts)
f-0 (initial-fact)
f-1 (synSoftgoal (ttId 1) (syntopic "A") (syntype "1"))
f-2 (synSoftgoal (ttId 2) (syntopic "A") (syntype "2"))
f-3 (synSoftgoal (ttId 3) (syntopic "B") (syntype "1"))
f-4 (synSoftgoal (ttId 4) (syntopic "B") (syntype "2"))
For a total of 5 facts.
CLIPS> (run)
Enter below the two softgoals field that want to be deleting:
the synonyms of the <[TYPE]QUALITY ATTRIBUTE> and the <[TOPIC]SUBJECT/OBJECT LAL> need to be entered one per line.
2
A
CLIPS> (facts)
f-0 (initial-fact)
f-1 (synSoftgoal (ttId 1) (syntopic "A") (syntype "1"))
f-3 (synSoftgoal (ttId 3) (syntopic "B") (syntype "1"))
f-4 (synSoftgoal (ttId 4) (syntopic "B") (syntype "2"))
For a total of 4 facts.
CLIPS>

Related

CLIPS does not recognize deftemplate name

I am trying to retract a deftemplate fact but when I do this CLIPS keeps saying I have to first declare the deffunction yet it is the appropriate deftemplate.What seems to be the problem?
I have attached the related code:
I get this error:
[EXPRNPSR3] Missing function declaration for Agriculture.
What seems to be the problem?
(deftemplate Agriculture
(slot weed
(type SYMBOL)
(allowed-symbols B G))
(slot crop
(type SYMBOL)
(allowed-symbols C S))
(slot organic-matter
(type INTEGER)
(allowed-values 1 2 3)))
(defrule Sencor-1
(and (Agriculture(weed B))
(Agriculture(crop C|S))
(Agriculture(organic-matter 1)))
=>
(printout t "Do not use Sencor!!"crlf))
(defrule Sencor-2
(and (Agriculture(weed B))
(Agriculture(crop C|S))
(Agriculture(organic-matter 2|3)))
=>
(printout t " " crlf "Use 3/4 pt/ac of Sencor" crlf ))
(defrule Lasso-1
(and (Agriculture(weed B|G))
(Agriculture(crop C|S))
(Agriculture(organic-matter 1)))
=>
(printout t crlf"Use 2 pt/ac of Lasso" crlf))
(defrule Lasso-2
(and (Agriculture(weed B|G))
(Agriculture(crop C|S))
(Agriculture(organic-matter 2)))
=>
(printout t crlf "Use 1 pt/ac of Lasso" crlf))
(defrule Lasso-3
(and (Agriculture(weed B|G))
(Agriculture(crop C|S))
(Agriculture(organic-matter 3)))
=>
(printout t crlf "Use 0.5 pt/ac of Lasso" crlf))
(defrule Bicep-1
(and (Agriculture(weed B|G))
(Agriculture(crop C))
(Agriculture(organic-matter 1)))
=>
(printout t crlf "Use 1.5 pt/ac of Bicep" crlf))
(defrule Bicep-2
(and (Agriculture(weed B|G))
(Agriculture(crop C))
(Agriculture(organic-matter 2)))
=>
(printout t crlf"Use 2.5 pt/ac of Bicep" crlf))
(defrule Bicep-3
(and (Agriculture(weed B|G))
(Agriculture(crop C))
(Agriculture(organic-matter 3)))
=>
(printout t crlf "Use 3 pt/ac of Bicep" crlf))
(defrule input
(initial-fact)
=>
(printout t crlf "What is the crop? (C:corn,S:soybean)")
(bind ?a (read))
(assert(Agriculture(crop ?a))) ;gets input from user
(printout t crlf "What is the weed problem? (B:broadleaf, G:grass)")
(bind ?b (read))
(assert(Agriculture(weed ?b)))
(printout t crlf "What is the % of organic matter content? (1:<2%,2:2-4%,3:>4%)")
(bind ?c (read))
(assert(Agriculture(organic-matter ?c)))
?d <- (Agriculture(crop ?a) (weed ?b) (organic-matter ?c))
(printout t ""crlf crlf "RECOMMENDATIONS:"crlf)
(retract ?d))
In the RHS of the input rule you state:
?d <- (Agriculture(crop ?a) (weed ?b) (organic-matter ?c))
This is interpreted as "Run function Agriculture and bind its results into ?d".
What you probably are trying to do is:
(bind ?d (assert (Agriculture (crop ?a) (weed ?b) (organic-matter ?c))))

check multiple fields in if() CLIPS

I'm trying to do a comparation in a rule on CLIPS thah check if one of three conditions it's true to assert a new fact. The code is:
(defrule empresa_cae_mucho
(Empresa (nombre ?n)(var_anio ?anio)(var_sem ?sem)(var_tri ?tri))
=>
(or (or (test(> ?anio 30))(test (> ?sem 30))(test (> ?tri 30))))
(assert valor_infravalorado
(nombre ?n))
(assert (Explicacion
(nombre ?n)
(motivo "la empresa ha caido bastante aunque no en el ultimo mes
pero su PER es bajo")))
)
But it doesn't work and I can't find the right form of do this in internet. Any help?
CLIPS> (clear)
CLIPS>
(deftemplate Empresa
(slot nombre)
(slot var_anio)
(slot var_sem)
(slot var_tri))
CLIPS>
(deftemplate valor_infravalorado
(slot nombre))
CLIPS>
(deftemplate Explicacion
(slot nombre)
(slot motivo))
CLIPS>
(deffacts start
(Empresa (nombre 1) (var_anio 40) (var_sem 10) (var_tri 25))
(Empresa (nombre 2) (var_anio 0) (var_sem 35) (var_tri 10))
(Empresa (nombre 3) (var_anio 30) (var_sem 20) (var_tri 55))
(Empresa (nombre 4) (var_anio 30) (var_sem 30) (var_tri 30)))
CLIPS>
(defrule empresa_cae_mucho
(Empresa (nombre ?n)
(var_anio ?anio)
(var_sem ?sem)
(var_tri ?tri))
(test (or (> ?anio 30)
(> ?sem 30)
(> ?tri 30)))
=>
(assert (valor_infravalorado (nombre ?n)))
(assert (Explicacion
(nombre ?n)
(motivo "la empresa ..."))))
CLIPS> (reset)
CLIPS> (watch rules)
CLIPS> (watch facts)
CLIPS> (run)
FIRE 1 empresa_cae_mucho: f-3
==> f-5 (valor_infravalorado (nombre 3))
==> f-6 (Explicacion (nombre 3) (motivo "la empresa ..."))
FIRE 2 empresa_cae_mucho: f-2
==> f-7 (valor_infravalorado (nombre 2))
==> f-8 (Explicacion (nombre 2) (motivo "la empresa ..."))
FIRE 3 empresa_cae_mucho: f-1
==> f-9 (valor_infravalorado (nombre 1))
==> f-10 (Explicacion (nombre 1) (motivo "la empresa ..."))
CLIPS> (facts)
f-0 (initial-fact)
f-1 (Empresa (nombre 1) (var_anio 40) (var_sem 10) (var_tri 25))
f-2 (Empresa (nombre 2) (var_anio 0) (var_sem 35) (var_tri 10))
f-3 (Empresa (nombre 3) (var_anio 30) (var_sem 20) (var_tri 55))
f-4 (Empresa (nombre 4) (var_anio 30) (var_sem 30) (var_tri 30))
f-5 (valor_infravalorado (nombre 3))
f-6 (Explicacion (nombre 3) (motivo "la empresa ..."))
f-7 (valor_infravalorado (nombre 2))
f-8 (Explicacion (nombre 2) (motivo "la empresa ..."))
f-9 (valor_infravalorado (nombre 1))
f-10 (Explicacion (nombre 1) (motivo "la empresa ..."))
For a total of 11 facts.
CLIPS>

how to get the index of facts in RHS of rule?

I'm asking if there is a possibility of accessing a get the index of fact in RHS of defrule ?
It gives me that undefined every time I try to index a fact in a RHS of defrule.
because I have a while loop , I want to be able to modify elevator fact depending on my input data.
(deftemplate elevator
(slot goal))
(deffacts elevator
(elevator (goal 0)))
(defrule read-data
=>
?f1 <- (elevator)
(modify ?f2 (goal 1))
)
this an example of my code , since I can't put all online :
(deftemplate data
(slot data)
)
(deffacts data
(data (data 1))
)
(defrule rule1
?f1 <-(data)
=>
(bind ?value (readline input) )
(while (neq ?value EOF)
do
(bind ?data (fact-slot-value ?f1 data))
(printout t "data " ?data crlf )
(retract ?f1)
(modify ?f1 (data ?value))
(bind ?value (readline input)
)
)
)
this is my input file :
2
3
4
5
6
7
this is what I'm getting :
CLIPS> (run)
data 1
data FALSE
data FALSE
data FALSE
data FALSE
data FALSE
CLIPS>
I want it to print out
data 2
data 3
data 4 ..ect
You can do it this way from the RHS of a rule, but if your rule actually has no LHS conditions, it's pointless to use a rule to change the value of the fact. Just use a function.
CLIPS>
(deftemplate elevator
(slot goal))
CLIPS>
(deffacts elevator
(elevator (goal 0)))
CLIPS>
(defrule read-data
=>
(do-for-fact ((?f elevator)) TRUE
(modify ?f (goal 1))))
CLIPS> (watch facts)
CLIPS> (reset)
<== f-0 (initial-fact)
==> f-0 (initial-fact)
==> f-1 (elevator (goal 0))
CLIPS> (run)
<== f-1 (elevator (goal 0))
==> f-2 (elevator (goal 1))
CLIPS>
Alternately, you can bind the fact you want to modify in the conditions of the rule:
CLIPS> (clear)
CLIPS>
(deftemplate elevator
(slot goal))
CLIPS>
(deffacts elevator
(elevator (goal 0)))
CLIPS>
(defrule read-data
?f <- (elevator (goal 0))
=>
(modify ?f (goal 1)))
CLIPS> (watch facts)
CLIPS> (reset)
<== f-0 (initial-fact)
==> f-0 (initial-fact)
==> f-1 (elevator (goal 0))
CLIPS> (run)
<== f-1 (elevator (goal 0))
==> f-2 (elevator (goal 1))
CLIPS>
Updated:
You can get your original rule to "work" by removing the retract and rebinding ?f1 to the value returned by modify:
CLIPS> (clear)
CLIPS>
(deftemplate data
(slot data))
CLIPS>
(deffacts data
(data (data 1)))
CLIPS>
(defrule rule1
?f1 <- (data)
=>
(bind ?value (readline input))
(while (neq ?value EOF)
(bind ?data (fact-slot-value ?f1 data))
(printout t "data " ?data crlf )
(bind ?f1 (modify ?f1 (data ?value)))
(bind ?value (readline input))))
CLIPS> (reset)
CLIPS> (open input.txt input)
TRUE
CLIPS> (run)
data 1
data 2
data 3
data 4
data 5
data 6
CLIPS> (close input)
TRUE
CLIPS>
It's still suspiciously complicated to be modifying the same fact multiple times on the RHS.

Using less than on CLIPS program

I am trying to return a message if the user types in a value within certain range. Is this possible on CLIPS? In addition the system should only accept values in increments of 10.
If the user types in a number less or equal to 10 it should say "A"
If the user types in a number greater than 10 and less than 40 it should say "B"
- so it should only accept values 10,20,30,40
This is the code I have so far:
(defrule b-a1
(b-a "a")
=>
(bind ?reply (get-text-from-user "How many points did you achieve?"))
(assert (b-a1 ?reply )))
(defrule b-a2
(b-a1 <= 10)
=>
(assert (conclusion "A")))
(defrule b-a2
(10 < b-a1 < 40)
=>
(assert (conclusion "B")))
Any ideas on how I can get this working?
CLIPS>
(defrule b-a1
(b-a "a")
=>
(printout t "How many points did you achieve? ")
(bind ?reply (read))
(assert (b-a1 ?reply )))
CLIPS>
(defrule b-a2
(b-a1 ?v&:(<= ?v 10))
=>
(assert (conclusion "A")))
CLIPS>
(defrule b-a2
(b-a1 ?v&:(< 10 ?v)&:(< ?v 40))
=>
(assert (conclusion "B")))
CLIPS> (assert (b-a "a"))
<Fact-1>
CLIPS> (run)
How many points did you achieve? 24
CLIPS> (facts)
f-0 (initial-fact)
f-1 (b-a "a")
f-2 (b-a1 24)
f-3 (conclusion "B")
For a total of 4 facts.
CLIPS>

clips simple rule

does anybody have any idea why this:
(defrule initx
(declare(salience 90))
=>
(assert (blabla 10))
)
(defrule gigel
(declare(salience 89))
=>
(printout t "test" crlf)
)
is not working?
When I delete first rule it works.
Thank you.
What's the behavior that you're seeing? The behavior of the gigel rule is the same regardless of whether the initx rule is present or not.
CLIPS>
(defrule initx
(declare(salience 90))
=>
(assert (blabla 10)))
CLIPS>
(defrule gigel
(declare(salience 89))
=>
(printout t "test" crlf))
CLIPS> (reset)
CLIPS> (run)
test
CLIPS> (facts)
f-0 (initial-fact)
f-1 (blabla 10)
For a total of 2 facts.
CLIPS> (clear)
CLIPS>
(defrule gigel
(declare(salience 89))
=>
(printout t "test" crlf))
CLIPS> (reset)
CLIPS> (run)
test
CLIPS> (facts)
f-0 (initial-fact)
For a total of 1 fact.
CLIPS>

Resources