CLIPS rule unmatch(don't fire) after retract - clips

i have a project in clips with three modules, at the end of the second module i ask to user if he want to retract one of the previous answer, if he retract one of the answer of the second module, i need to retract all the answers of the second module and re-ask again. After i retract all the answers of the second module, i expect that this rule is activated
(defrule SECONDMODULE::domanda-esperto
(declare (salience ?*highest-priority*))
(livello-utente (livello esperto)) ;;assert in FIRSTMODULE and not retract
=>
(something)
)
But this rule is never activeted and it not apper in the AGENDA also if the facts that match the LHS is present in the fact list.
Sorry for my bad english.
EDIT.
#Gary First of all i ask 5 question to user, that are this:
(defrule starting-rule
(declare (salience ?*highest-priority*) (auto-focus TRUE))
=>
(printout t "***Inizio***" crlf)
(focus PROFILO)
(set-strategy random))
(defrule PROFILO::chiedi-se-possiede-auto
(not (domanda (nome possiede-auto) (domanda ?) (risposta ?)))
=>
(bind ?risposta (si-o-no "L'auto e' tua? "))
(assert (domanda (nome possiede-auto) (domanda "L'auto e' tua? ") (risposta ?risposta)))
)
(defrule PROFILO::frequenza-utilizzo-auto
(not(domanda (nome frequenza-utilizzo-auto) (domanda ?) (risposta ?)))
=>
(bind ?risposta (risposte-range "Quante volte a settimana in media utilizzi l'auto? " 0 1-2 3-5 5-7 ))
(assert (domanda (nome frequenza-utilizzo-auto) (domanda "Quante volte a settimana in media utilizzi l'auto? " ) (risposta ?risposta)))
)
(defrule PROFILO::conoscenza-meccanica-auto
(not (domanda (nome conoscenza-meccanica-auto) (domanda ?) (risposta ?)))
=>
(bind ?risposta (risposte-range "Quanto ti consideri esperto della meccanica dell'auto?" 0 1 2 3 4 5))
(assert (domanda (nome conoscenza-meccanica-auto) (domanda "Quanto ti consideri esperto della meccanica dell'auto?") (risposta ?risposta)))
)
(defrule PROFILO::kit-riparazione-rapida
(not (domanda (nome kit-riparazione-rapida) (domanda ?) (risposta ?)))
=>
(bind ?risposta (si-o-no "Possiedi un kit di riparazione rapida?"))
(assert (domanda (nome kit-riparazione-rapida) (domanda "Possiedi un kit di riparazione rapida?") (risposta ?risposta)))
)
(defrule PROFILO::anni-possesso-patente
(not(domanda (nome anni-possesso-patente) (domanda ?) (risposta ?)))
=>
(bind ?risposta (risposte-range "Da quanti anni possiedi la patente? " <1 1-5 >5 ))
(assert (domanda (nome anni-possesso-patente) (domanda "Da quanti anni possiedi la patente? ") (risposta ?risposta)))
)
After this i fire a rule that in according with the user asnwers delineate the profile of the user
(defrule PROFILO::livello-utente
?a<-(domanda (nome possiede-auto) (domanda ?) (risposta ?))
?b<-(domanda (nome anni-possesso-patente) (domanda ?) (risposta ?))
?c<-(domanda (nome conoscenza-meccanica-auto) (domanda ?) (risposta ?))
?d<-(domanda (nome kit-riparazione-rapida) (domanda ?) (risposta ?))
?e<-(domanda (nome frequenza-utilizzo-auto) (domanda ?) (risposta ?))
=>
(switch (fact-slot-value ?a risposta)
(case TRUE then (bind ?*punteggio* (+ ?*punteggio* 1)))
)
(switch (fact-slot-value ?d risposta)
(case TRUE then (bind ?*punteggio* (+ ?*punteggio* 1)))
)
(switch (fact-slot-value ?b risposta)
(case <1 then (bind ?*punteggio* (+ ?*punteggio* 1)))
(case 1-5 then (bind ?*punteggio* (+ ?*punteggio* 2)))
(case >5 then (bind ?*punteggio* (+ ?*punteggio* 3)))
)
(switch (fact-slot-value ?c risposta)
(case 1 then (bind ?*punteggio* (+ ?*punteggio* 1)))
(case 2 then (bind ?*punteggio* (+ ?*punteggio* 2)))
(case 3 then (bind ?*punteggio* (+ ?*punteggio* 3)))
(case 4 then (bind ?*punteggio* (+ ?*punteggio* 4)))
(case 5 then (bind ?*punteggio* (+ ?*punteggio* 5)))
)
(switch (fact-slot-value ?e risposta)
(case 1-2 then (bind ?*punteggio* (+ ?*punteggio* 1)))
(case 3-5 then (bind ?*punteggio* (+ ?*punteggio* 2)))
(case 5-7 then (bind ?*punteggio* (+ ?*punteggio* 3)))
)
(bind ?f ?*punteggio*)
(if (> ?f 9) then (assert (livello-utente (livello esperto))))
(if (< ?f 6) then (assert (livello-utente (livello principiante))))
(if (and (> ?f 5) (< ?f 10)) then (assert (livello-utente(livello medio))))
)
After that i go in the second module where one of this two rule ia actived in according with the profile of the user determinate in the fist module
(defrule DIAGNOSI::domanda-esperto
(declare (salience ?*highest-priority*))
(livello-utente (livello esperto))
=>
(bind ?risposta (risposte-range "In quale tra le seguenti aree e' presente il problema?" Olio-motore Olio-freni Acqua Carburante Altro))
(assert (domanda (nome area-problema) (domanda "In quale tra le seguenti aree e' presente il problema?") (risposta ?risposta)))
)
(defrule DIAGNOSI::domanda-medio
(declare (salience ?*highest-priority*))
(livello-utente (livello medio))
=>
(bind ?risposta (si-o-no "Sapresti indicare l'area di provenienza del problema tra le seguenti: Olio motore, Olio freni, Acqua, Carburante, Altro?"))
(assert (domanda (nome domanda-area-problema) (domanda "Sapresti indicare l'area di provenienza del problema tra le seguenti: Olio motore, Olio freni, Acqua, Carburante, Altro?") (risposta ?risposta)))
(if (eq ?risposta TRUE)
then (bind ?risposta (risposte-range "In quale tra le seguenti aree e' presente il problema?" Olio-motore Olio-freni Acqua Carburante Altro))
(assert (domanda (nome area-problema) (domanda "In quale tra le seguenti aree e' presente il problema?") (risposta ?risposta)))
)
After this there is a series of other question that i do to user. After that i ask if he want to retract one of this, and if he chooseone of the two (domanda-medio, domanda-esperto) i have to retract all the answers of the second module. After i retract all the answers of the second module, this two rule is never activeted and it not apper in the AGENDA also if the facts that match the LHS is present in the fact list (livello-utente (livello ?))

#GaryRiley ok i don't know why but adding `
(not (diagnosi (nome ?)))`
to the two rule it work.

Related

hey i have a problem in this clips expert system code its not printing the result

this is a Diet and Nutrition Expert System i have a problem that i cant print the result to the user after entering the input so can any one help me to make it run correctly?
(defrule read-gender
(initial-fact)
=>
(printout t crlf crlf "Welcome! Diet and Nutrition Expert System" crlf)
(printout t "******************************************" crlf)
(printout t "This output of this program is:" crlf)
(printout t "1. Your Body Mass Index (BMI) and body-status." crlf)
(printout t "2. Recommended daily calories needed based on your body-status." crlf)
(printout t "3. Daily protein needed based on your weight (kgs)." crlf)
(printout t "4. Daily celcium needed based on your age." crlf)
(printout t "5. Daily fiber needed based on your calories needed." crlf)
(printout t "6. Daily carbohydrate needed based on your weight (kgs)." crlf)
(printout t "******************************************" crlf crlf)
(printout t "What is your gender (Female/Male) *case-sensitive*:")
(assert (gender (read))))
(defrule read-age
(gender ?)
=>
(printout t "Please enter your age:")
(assert (age (read))))
(defrule read-height
(gender ?)
=>
(printout t "Please enter your height:")
(assert (height (read))))
(defrule read-weight
(gender ?)
=>
(printout t "Please enter your weight in(KGs):")
(assert (weight (read))))
(defrule read-activity-days
(gender ?)
=>
(printout t "How many day do you exercise for a week:")
(assert (activity-days (read))))
(defrule set-activity-rate-sedentary
(activity-days ?days)
(test (< ?days 2))
=>
(assert (activity-rate "Sedentary"))
(assert (activity-factor 1.2)))
(defrule set-activity-rate-moderate
(activity-days ?days)
(test (and (>= ?days 2) (< ?days 5)))
=>
(assert (activity-rate "Moderate"))
(assert (activity-factor 1.55)))
(defrule set-activity-rate-hard
(activity-days ?days)
(test (>= ?days 5))
=>
(assert (activity-rate "Hard"))
(assert (activity-factor 1.75)))
(defrule calculate-bmi
(weight ?weight)
(height ?height)
=>
(bind ?bmi (* ?weight (/ ?height ?height)))
(assert (bmi ?bmi)))
(defrule set-body-status-underweight
(bmi ?bmi)
(test (< ?bmi 18.5))
=>
(assert (body-status "Underweight")))
(defrule set-body-status-normal-weight
(bmi ?bmi)
(test (and (>= ?bmi 18.5) (< ?bmi 24.9)))
=>
(assert (body-status "Normal-weight")))
(defrule set-body-status-overweight
(bmi ?bmi)
(test (and (>= ?bmi 24.9) (< ?bmi 29.9)))
=>
(assert (body-status "Overweight")))
(defrule calculate-daily-calories-female
(gender "Female")
(activity-factor ?activity-factor)
(weight ?weight)
(age ?age)
(height ?height)
=>
(bind ?bmr (* 655.1 (+ (* 9.563 ?weight) (* 1.85 ?height) (* 4.676 ?age))))
(bind ?daily-calories (* ?bmr ?activity-factor))
(assert (daily-calories ?daily-calories)))
(defrule calculate-daily-calories-male
(gender "Male")
(activity-factor ?activity-factor)
(weight ?weight)
(age ?age)
(height ?height)
=>
(bind ?bmr (* 66.5 (+ (* 13.75 ?weight) (* 5.003 ?height) (* 6.755 ?age))))
(bind ?daily-calories (* ?bmr ?activity-factor))
(assert (daily-calories ?daily-calories)))
(defrule set-daily-calcium-baby
(age ?age)
(test (< ?age 4))
=>
(assert (daily-calcium "210-270mg")))
(defrule set-daily-calcium-child
(age ?age)
(test (and (>= ?age 4) (< ?age 9)))
=>
(assert (daily-calcium "350-450mg")))
(defrule set-daily-calcium-teen
(age ?age)
(test (and (>= ?age 9) (< ?age 19)))
=>
(assert (daily-calcium "800mg")))
(defrule set-daily-calcium-adult
(age ?age)
(test (and (>= ?age 19) (< ?age 51)))
=>
(assert (daily-calcium "1000mg")))
(defrule set-daily-calcium-old
(age ?age)
(test (>= ?age 51))
=>
(assert (daily-calcium "1200mg")))
(defrule calculate-daily-protein-sedentary
(weight ?weight)
(activity-rate "Sedentary")
=>
(bind ?daily-protein (* ?weight 0.8))
(assert (daily-protein ?daily-protein)))
(defrule calculate-daily-protein-moderate
(weight ?weight)
(activity-rate "Moderate")
=>
(bind ?daily-protein (* ?weight 1))
(assert (daily-protein ?daily-protein)))
(defrule calculate-daily-protein-hard
(weight ?weight)
(activity-rate "Hard")
=>
(bind ?daily-protein (* ?weight 1.2))
(assert (daily-protein ?daily-protein)))
(defrule calculate-daily-carbohydrates
(weight ?weight)
=>
(bind ?daily-carbohydrates (* ?weight 2.5))
(assert (daily-carbohydrates ?daily-carbohydrates)))
(defrule protein-advice
(protein-needed ?p)
=>
(printout t crlf crlf" ######## Result ######## " crlf)
(printout t " 1. You need " ?p "g of protein per day." crlf))
(defrule carbohydrate-advice
(carbohydrate-needed ?c)
=>
(printout t " 3. You need " ?c "g of carbohydrate per day." crlf))
(defrule fiber-advice
(fiber-needed ?f)
=>
(printout t " 2. You need " ?f "g of fiber per day." crlf))
(defrule calcium-advice
(calcium-needed ?c)
=>
(printout t " 4. You need " ?c "g of calcium per day." crlf))
(defrule calories-advice-underweight
(body-status underweight)
(calories-needed ?c)
(bmi ?bm)
(body-status ?b)
(carbohydrate-needed ?ca)
(calcium-needed ?ce)
=>
(printout t "
5. Your Body Mass Index (BMI) is " ?bm " (" ?b "), "crlf "
6. You
need " ?c " calories per day. "crlf "
7. For advice from the experts, You may need extra
daily 300 calories (" (+ 300 ?c)") to gain 0.25kg/week." crlf crlf))
(defrule calories-advice-normalweight
(body-status normal-weight)
(calories-needed ?c)
(bmi ?bm)
(body-status ?b)
(carbohydrate-needed ?ca)
(calcium-needed ?ce)
=>
(printout t " 5. Your Body Mass Index (BMI) is " ?bm " (" ?b ")," crlf " 6. You
need " ?c " calories per day to maintain your healthy weight." crlf crlf))
(defrule calories-advice-overweight
(body-status overweight)
(calories-needed ?c)
(bmi ?bm)
(body-status ?b)
(carbohydrate-needed ?ca)
(calcium-needed ?ce)
=>
(printout t " 5. Your Body Mass Index (BMI) is " ?bm " (" ?b ")," crlf " 6. You need " ?c " calories per day. " crlf " 7. For advice from the experts, You may need to reduce your daily calories needed by 300 to " (- ?c 300)))
(defrule calories-advice-obesity
(body-status obesity)
(calories-needed ?c)
(bmi ?bm)
(body-status ?b)
(carbohydrate-needed ?ca)
(calcium-needed ?ce)
=>
(printout t " 5. Your Body Mass Index (BMI) is " ?bm " (" ?b "), "crlf " 6. You
need " ?c " calories per day. "crlf " 7. For advice from the experts, You may need to reduce
your daily calories needed by 500 to (" (- ?c 300)") to loss 0.5kg/week." crlf crlf))
I tried to rewrite the code in different ways but its still working correctly but without any result so can any one please help me to rewrite it correctly?
Don't use an initial-fact pattern in your rules. The initial-fact is
no longer supported in CLIPS 6.4. Just leave the conditions of the
rule empty and it will work in both version 6.3 and 6.4.
The protein-advice defrule expects a protein-needed fact but a
daily-protein fact is asserted by your other rules.
The carbohydrate-advice defrule expects a carbohydrate-needed fact but a daily-carbohydrates fact is asserted by your other rules.
The fiber-advice defrule expects a fiber-needed fact but no fiber
related facts are asserted by any of your rules.
The calcium-advice defrule expects a calcium-needed fact but a daily-calcium fact is asserted by your other rules.
Your other rules that print results have similar issues.

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

CLIPS: Array in CLIPS? (Need some orientation, new in CLIPS)

I'm new in Clips. I'd like to know if it is a way to read an array (chain of numeric or characters with an index, sorry if it's the wrong name) on LHS. I have rules to ask for a value (s,cs,cn,n) then it assert the value to next asking rule, to finally read all the values in an answering rule to get a diagnostic, but in my small example I have 4 questions and 4 options for each one so mixing all the answers would give me 64 rules, and I have so at least 30 questions in my program so I think that would be too much rules (I'm doing my first Expert System an maybe this is normal). In any case I think I could get the values from questions into an array an read it in answering rules, but my questions are:
*How can I bind the values from my function into an array?
*Is it possible to verify that array in LHS?
*Do you have any other idea to verify the answer-rules? Hope you can help me.
(deffunction funcionPregunta (?pregunta $?valoresAceptados) ;;ask-question function
(printout t ?pregunta)
(bind ?respuesta (read))
(if (lexemep ?respuesta)
then (bind ?respuesta (lowcase ?respuesta)))
(while (not (member$ ?respuesta ?valoresAceptados)) do
(printout t ?pregunta)
(bind ?respuesta (read))
(if (lexemep ?respuesta)
then (bind ?respuesta (lowcase ?respuesta))))
?respuesta)
;;===============================================================
;; QUESTION RULES
;;===============================================================
(defrule pregunta1T5 "AGORAFOBIA"
(not (diagnostico ?))
=>
(assert (Pregunta2T5
(funcionPregunta "1.Siente miedo o ansiedad marcada. (always/frecuently/rare/never)? "
s cs cn n))))
(defrule Pregunta2T5 "AGORAPUBLICO"
(not (diagnostico ?))
(Pregunta2T5 ?Pregunta2T5)
=>
(assert (Pregunta3T5
(funcionPregunta "2.Siente miedo en una multitud. (always/frecuently/rare/never)? "
s cs cn n)))
)
(defrule Pregunta3T5 "AGORAMIEDO"
(not (diagnostico ?))
(Pregunta3T5 ?Pregunta3T5)
=>
(assert (Pregunta4T5
(funcionPregunta "3.Miedo de estar en una situacion. (always/frecuently/rare/never)? "
s cs cn n)))
)
(defrule Pregunta4T5 "AGORAANSIEDAD"
... ;; similar rules
;;===============================================================
;; ANSWERS RULES
;;===============================================================
(defrule Respuesta1T6 "RESULTADO 1 TAS"
(not (diagnostico ?))
(Pregunta2T6 s)(Pregunta3T6 s)(Pregunta4T6 s)(Pregunta5T6 s)
=>
(assert (diagnostico "TRASTORNO DE ANSIEDAD SOCIAL"))
)
(defrule Respuesta2T6 "RESULTADO 2 TAS"
(not (diagnostico ?))
(Pregunta2T6 cs)(Pregunta3T6 s)(Pregunta4T6 s)(Pregunta5T6 s)
=>
(assert (diagnostico "TRASTORNO DE ANSIEDAD SOCIAL"))
)
In the case of the two answer rules you've already got, the simplest way to reduce the number of rules is just to combine them:
(defrule Respuesta1T6-2T6
(not (diagnostico ?))
(Pregunta2T6 s | cs) ; s or cs is allowed
(Pregunta3T6 s)
(Pregunta4T6 s)
(Pregunta5T6 s)
=>
(assert (diagnostico "TRASTORNO DE ANSIEDAD SOCIAL")))
If you're creating lots of rules that differ only in the constants matched in the patterns, you should consider representing the rules as a combination of facts containing these constants and generic rules to process that data. For example, you could rewrite your question rules like this:
(deftemplate Pregunta ; question
(slot identidad) ; ID
(slot texto) ; text
(multislot respuestas) ; responses
(slot precursora ; precursor
(default ninguna))) ; none
(deftemplate Responder ; answer
(slot identidad) ; ID
(slot valor)) ; value
(deffacts Preguntas
(Pregunta (identidad AGORAFOBIA)
(texto "1. Siente miedo o ansiedad marcada. (always/frecuently/rare/never)? ")
(respuestas s cs cn n))
(Pregunta (identidad AGORAPUBLICO)
(texto "2. Siente miedo en una multitud. (always/frecuently/rare/never)? ")
(respuestas s cs cn n)
(precursora AGORAFOBIA))
(Pregunta (identidad AGORAMIEDO)
(texto "3. Miedo de estar en una situacion. (always/frecuently/rare/never)? ")
(respuestas s cs cn n)
(precursora AGORAPUBLICO)))
(defrule pedir-pregunta ; ask question
(not (diagnostico ?))
(Pregunta (identidad ?id)
(texto ?t)
(respuestas $?r)
(precursora ?p))
(or (test (eq ?p ninguna))
(Responder (identidad ?p)))
=>
(assert (Responder (identidad ?id)
(valor (funcionPregunta ?t ?r)))))
And your diagnosis rules like this:
(deftemplate Trastorno ; disorder
(slot nombre) ; name
(multislot sintomas)) ; symptoms
(deftemplate Sintoma
(slot identidad) ; ID
(slot responder) ; answer
(multislot valors)) ; values
(deffacts Trastornos
(Trastorno (nombre "TRASTORNO DE ANSIEDAD SOCIAL")
(sintomas AGORAFOBIA-cs-s AGORAPUBLICO-s AGORAMIEDO-s)))
(deffacts Sintomas
(Sintoma (identidad AGORAFOBIA-cs-s)
(responder AGORAFOBIA)
(valors cs s))
(Sintoma (identidad AGORAPUBLICO-s)
(responder AGORAPUBLICO)
(valors s))
(Sintoma (identidad AGORAMIEDO-s)
(responder AGORAMIEDO)
(valors s)))
(defrule Respuesta
(not (diagnostico ?))
(Trastorno (nombre ?n)) ; There is a disorder.
(forall (Trastorno (nombre ?n) ; For every symptom
(sintomas $? ?s $?)) ; of the disorder,
(Sintoma (identidad ?s) ; there is a list
(responder ?r) ; of possible values
(valors $?sv)) ; for that symptom
(Responder (identidad ?r) ; matched by a response.
(valor ?v&:(member$ ?v ?sv))))
=>
(assert (diagnostico ?n)))

Finding the count of repetitions of words (Clips)

I have a task, write a program to count the number of repetitions of a word in the list.
I just started learning the clips so I do not know many things.
I wrote the code, but unfortunately it does not work, what could be the error?
(clear)
(deftemplate list_1
(slot numeral)
)
(deftemplate list_2
(slot numeral)
)
(deftemplate list_3
(slot numeral)
)
(deffacts start
(list_1 (numeral zero))
(list_1 (numeral one))
(list_1 (numeral two))
(list_2 (numeral zero))
(list_2 (numeral two))
(list_2 (numeral three))
(list_3 (numeral zero))
(list_3 (numeral one))
(list_3 (numeral three))
)
(defglobal
?*countword* = 0
)
(defrule inputword
(initial-fact)
=>
(printout t crlf “Enter a word to search for: “)
(bind ?i (read))
(assert (wordforsearch ?i))
)
(defrule searchword
(wordforsearch ?i)
(list_1 (numeral ?i))
(list_2 (numeral ?i))
(list_3 (numeral ?i))
=>
(bind ?*countword* (+ ?*countword* 1))
)
(defrule outputword
(wordforsearch ?i)
=>
(printout t "Number of repetitions for a word: " ?i " = " ?*countword* crlf)
(reset)
(halt)
)
(run)
I really hope the same way that you explain in detail what the error is and maybe tell me another version of the implementation of the code.
P. S. I implemented another version of the program - the search for the number of repetitions of words in the sentence. This code also does not work.
(clear)
(defglobal
?*countword* = 0
)
(defrule inputword
(initial-fact)
=>
(printout t crlf “Enter a sentence: “)
(bind ?s (read))
)
(defrule inputword
(?s)
=>
(printout t crlf “Enter a word to search for: “)
(bind ?i (read))
(assert (wordforsearch ?i))
)
(defrule searchword
(wordforsearch ?i)
(?s ?i)
=>
(bind ?*countword* (+ ?*countword* 1))
)
(defrule outputword
(wordforsearch ?i)
=>
(printout t "Number of repetitions for a word: " ?i " = " ?*countword* crlf)
(reset)
(halt)
)
(run)
I really hope for your help in understanding the clips.
P. P. S. Sorry for my english
I can offer here this option. This has many shortcomings, but as an example this will suitable.
(defglobal
?*i* = 0
?*count* = 0
?*string* = (create$)
?*wordsearch* = ""
?*wordsearch1* = (create$)
)
(defrule Searching
?fact <- (searching)
=>
(retract ?fact)
(bind ?*i* (+ ?*i* 1))
(if (<= ?*i* (length ?*string*))
then
(if (eq ?*wordsearch1* (subseq$ ?*string* ?*i* ?*i*))
then
(bind ?*count* (+ ?*count* 1))
(assert (searching))
else
(assert (searching))
)
else
(printout t "Word " ?*wordsearch* " repeats " ?*count* " times" crlf)
)
)
(defrule Start
=>
(printout t crlf "Enter the string: ")
(bind ?*string* (create$ (explode$ (lowcase (readline)))))
(printout t crlf "Enter a word to search for: ")
(bind ?*wordsearch* (lowcase (readline)))
(bind ?*wordsearch1* (create$ (explode$ ?*wordsearch*)))
(assert (searching))
)

Expert System in Clips doesn't work

In my expert system user must check Developer and Price after he'll see notebook which is suitable for this parameters.
For example with this parameters (on screenshot) I must have result: Model: Noteebok1
But I don't see anything. Where is a problem or bug?
CLP File Code:
(defglobal ?*s* = 0)
(deftemplate Notebook
(slot pModel)
(slot pDeveloper)
(slot pPrice))
;*******************************************************************
(deffunction QuestionOf(?TextQuestion $?variations)
(printout t ?TextQuestion)
(bind ?Answer (read))
(if (lexemep ?Answer)
then (bind ?Answer (lowcase ?Answer)))
(while (not (member ?Answer ?variations)) do
(printout t ?TextQuestion)
(bind ?Answer (read))
(if (lexemep ?Answer)
then (bind ?Answer (lowcase ?Answer))))
?Answer)
;********************************************************************'
(defrule banner
(declare (salience 10))
=>(load-facts D:\fact.txt)
(printout t crlf crlf)
(printout t "Expert system. Nout search")
(printout t crlf crlf))
;******************************************************************'
(defrule QuestionDeveloper
(not (Developer ?))
=>(bind ?asssert(QuestionOf "Check Developer (a-HP,b-Samsung,c-Apple,d-IDontKnow)" a b c d))
(if (eq ?asssert a)then (assert (Developer HP)))
(if (eq ?asssert b)then (assert (Developer Samsung)))
(if (eq ?asssert c)then (assert (Developer Apple)))
(if (eq ?asssert d)then (assert (Developer IDontKnow))))
;******************************************************************'
(defrule QuestionPrice
(not (Price ?))
=>(bind ?asssert(QuestionOf "Price?(a-300,b-400,c-500,d-IDontKnow)" a b c d))
(if (eq ?asssert a)then (assert (Price 300)))
(if (eq ?asssert b)then (assert (Price 400)))
(if (eq ?asssert c)then (assert (Price 500)))
(if (eq ?asssert d)then (assert (Price IDontKnow))))
;******************************************************************'
(defrule Vyvod
(or (Developer ?xDeveloper)(Developer IDontKnow))
(or (Price ?xPrice)(Price IDontKnow))
(Notebook(pModel ?Model)(pDeveloper ?xDeveloper)(pPrice ?xPrice))
=>(bind ?*s*(+ ?*s* 1))
(printout t crlf " " ?*s* ". Model : " ?Model crlf))
;******************************************************************'
Fact.txt:
(Notebook(pModel Notebook1)(pDeveloper HP)(pPrice 500))
(Notebook(pModel Notebook2)(pDeveloper Samsung)(pPrice 400))
(Notebook(pModel Notebook3)(pDeveloper Apple)(pPrice 500))
The facts from the file fact.txt are not being loaded. Check that the file is at the specified path and that you have read access.

Resources