excuse my English, I'm Spanish.
I have a problem with my expert system computer configurations made in CLIPS.
My rule "preciogamingoc" does not run, I tried a thousand ways but do not work.
I leave the code here.
I dont know how to put it better, sorry for that.
;;****TEMPLATES*****
(deftemplate pc
(slot tipo)
(slot oc)
(multislot procesador)
(multislot ram)
(multislot placabase)
(multislot discoduro)
(multislot grafica)
(multislot fuente)
(slot precio))
;;****FUNCTIONS*****
(deffunction pregunta (?pregunta $?respuestas-posibles)
(printout t ?pregunta)
(bind ?respuesta (read))
(if (lexemep ?respuesta)
then (bind ?respuesta (lowcase ?respuesta)))
(while (not (member ?respuesta ?respuestas-posibles)) do
(printout t ?pregunta)
(bind ?respuesta (read))
(if (lexemep ?respuesta)
then (bind ?respuesta (lowcase ?respuesta))))
?respuesta)
(deffunction sino (?pregunta)
(bind ?respuesta (pregunta ?pregunta si no s n))
(if (or (eq ?respuesta si) (eq ?respuesta s))
then TRUE
else FALSE))
(deffunction tipo (?pregunta)
(bind ?respuesta (pregunta ?pregunta g d o))
(switch ?respuesta
(case g then (return g))
(case d then (return d))
(case o then (return o))
))
;;******FACTS*******
(deffacts inicia
(pc))
;;******RULES*******
(defrule tipopc
?A <- (pc (procesador)(ram)(placabase)(discoduro)(grafica)(fuente))
=>
(switch (tipo "Tipo (juegos:g/diseño:d/otros:o)? ")
(case g then ( modify ?A (tipo g)(oc nose)(procesador "i5 4690_Intel")
(ram "2x4GB 2133 cl9")
(placabase "Gigabyte_H97M-HD3 ejemplo")
(discoduro "1Tb")
(grafica "GTX 960")
(fuente "700W+ silver")
(precio 550)))
(case d then ( modify ?A (tipo d)(oc nose)(procesador "i7 4790_Intel")
(ram "2x8GB 2133 cl9")
(placabase "Gigabyte_H97M-HD3 ejemplo")
(discoduro "1Tb HDD + 120gb SSD")
(grafica "kuadro k620")
(fuente "700W+ silver")
(precio 700)))
(case o then ( modify ?A (tipo o)(oc nose)(procesador "i3 6100_Intel")
(ram "2x4GB 2400 cl12 DDR4")
(placabase "Msi H110M PRO-VH")
(discoduro "1Tb")
(grafica "no incluida")
(fuente "450W+ bronze")
(precio 300)))))
(defrule oc
?A <- (pc(tipo g)(oc nose))
=>
(if (sino "Hará overclock al procesador (si/no)?: ")
then ( modify ?A (oc si)))
)
(defrule preciogamingoc
?A <- (pc (tipo g) (oc si) (procesador) (ram) (placabase) (discoduro) (grafica) (fuente))
=>
(printout t "Veo que su sistema será destinado a juegos y que además se le podrá hacer overclock." crlf
"Ahora debe introducir el presupuesto medio para que podamos darle una configuración mas concreta." crlf
"Introduzca su presupuesto de referencia(>= 750): ")
(bind ?precio (read))
(if (and(>= ?precio 750)(< ?precio 850))
;;then ((printout t "bien"crlf))
then (modify ?A (procesador "i5 4690K_Intel")
(ram "2x8GB 2133 cl9")
(placabase "Gigabyte_Z97X gaming 3 ejemplo")
(discoduro "1Tb")
(grafica "GTX 970")
(fuente "700W+ silver")
(precio 750)))
;;(else (if (>= ?precio 850)
;;then (modify ?C (procesador "i7 4790K_Intel")
;;(ram "2x8GB 2133 cl9")
;;(placabase "Gigabyte_Z97X gaming 5 ejemplo")
;;(discoduro "1Tb HDD")
;;(grafica "GTX 970")
;;(fuente "700W+ silver")
;(precio 850))))
)
;;*******MAIN*******
(defrule inicio
(declare (salience 1))
=>
(printout t crlf)
(printout t "Sistema Experto de Configuracion de Ordenadores")
(printout t crlf crlf))
(defrule presupuesto
(declare (salience -1))
(pc (procesador ?A)(ram ?F)(placabase ?B)(discoduro ?M)(grafica ?G)(fuente ?P)(precio ?O))
=>
(printout t crlf crlf "-.PRESUPUESTO.-" crlf crlf)
(printout t "PROCESADOR: " ?A crlf)
(printout t "RAM: " ?F crlf)
(printout t "PLACA BASE: " ?B crlf)
(printout t "DISCO DURO:" ?M crlf)
(printout t "GRAFICA: " ?G crlf)
(printout t "FUENTE: " ?P crlf crlf)
(printout t "PRECIO: " ?O crlf)
)
The preciogamingoc rule matches a pc fact, so you can run the program, check to see which rules execute, and examine the pc fact to see which values have been assigned to it:
CLIPS> (reset)
CLIPS> (watch rules)
CLIPS> (run)
FIRE 1 inicio: *
Sistema Experto de Configuracion de Ordenadores
FIRE 2 tipopc: f-1
Tipo (juegos:g/diseño:d/otros:o)? g
FIRE 3 oc: f-2
Hará overclock al procesador (si/no)?: si
FIRE 4 presupuesto: f-3
-.PRESUPUESTO.-
PROCESADOR: i5 4690_Intel
RAM: 2x4GB 2133 cl9
PLACA BASE: Gigabyte_H97M-HD3 ejemplo
DISCO DURO:1Tb
GRAFICA: GTX 960
FUENTE: 700W+ silver
PRECIO: 550
CLIPS> (facts)
f-0 (initial-fact)
f-3 (pc (tipo g) (oc si) (procesador "i5 4690_Intel") (ram "2x4GB 2133 cl9") (placabase "Gigabyte_H97M-HD3 ejemplo") (discoduro "1Tb") (grafica "GTX 960") (fuente "700W+ silver") (precio 550))
For a total of 2 facts.
CLIPS> (ppfact 3)
(pc
(tipo g)
(oc si)
(procesador "i5 4690_Intel")
(ram "2x4GB 2133 cl9")
(placabase "Gigabyte_H97M-HD3 ejemplo")
(discoduro "1Tb")
(grafica "GTX 960")
(fuente "700W+ silver")
(precio 550))
CLIPS>
The pattern from the preciogamingoc rule is:
?A <- (pc (tipo g)
(oc si)
(procesador)
(ram)
(placabase)
(discoduro)
(grafica)
(fuente))
The fact values for the tipo and oc slots match the pattern, but the values for the processor, ram, placabase, discoduro, grafica, and fuente slots do not. These slots can have zero or more values. The fact has a single value for each, but your pattern specifies that each of these slots be empty so the pattern is not matched.
You need to either remove these slots from the pattern or check for specific values. You also need to prevent the rule from executing again once the pc fact has been modified in the actions of the rule (perhaps by changing the value of oc to si-actualizado to indicate the rule has been applied):
(defrule preciogamingoc
?A <- (pc (tipo g) (oc si))
=>
(printout t "Veo que su sistema será destinado a juegos y que además se le podrá hacer overclock." crlf
"Ahora debe introducir el presupuesto medio para que podamos darle una configuración mas concreta." crlf
"Introduzca su presupuesto de referencia(>= 750): ")
(bind ?precio (read))
(if (and (>= ?precio 750)(< ?precio 850))
then
(modify ?A (oc si-actualizado)
(procesador "i5 4690K_Intel")
(ram "2x8GB 2133 cl9")
(placabase "Gigabyte_Z97X gaming 3 ejemplo")
(discoduro "1Tb")
(grafica "GTX 970")
(fuente "700W+ silver")
(precio 750))))
Related
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.
How do I test if all variables in template equal variables in different templates or if they don't. Need to have different outcomes from each output.
Attaching my code below:
``;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Sablony pre vstupy ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(deftemplate mobily
(slot znacka (type SYMBOL))
(slot model (type STRING) )
(slot rozlisenie (type SYMBOL) (allowed-values HD FULL_HD Q_HD))
(slot SD_Karta (type SYMBOL) (allowed-values ano nie))
(slot interna_pamat (type INTEGER) (range 32 512))
(slot operacny_system (type SYMBOL) (allowed-values iOS android ine))
(slot uhlopriecka (type FLOAT) (range 4.0 7.0))
(slot rozlisenie_fotoaparatu (type INTEGER) (range 4 128))
(slot kapacita_baterie (type INTEGER) (range 5 25)))
(deftemplate zakaznik
(slot znacka (type SYMBOL))
(slot model (type STRING))
(slot rozlisenie (type SYMBOL) (allowed-values HD FULL_HD Q_HD))
(slot SD_Karta (type SYMBOL) (allowed-values ano nie))
(slot interna_pamat (type INTEGER) (range 32 512))
(slot operacny_system (type SYMBOL) (allowed-values iOS android ine))
(slot uhlopriecka (type FLOAT) (range 4.0 7.0))
(slot rozlisenie_fotoaparatu (type INTEGER) (range 4 128))
(slot kapacita_baterie (type INTEGER) (range 5 25)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Vstupy ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(deffacts mobily_na_sklade
(mobily(znacka Motorola)(model "Defy")(rozlisenie HD)(SD_Karta ano)(interna_pamat 64)(operacny_system android)(uhlopriecka 6.5)(rozlisenie_fotoaparatu 60)(kapacita_baterie 20)) (mobily(znacka Samsung)(model "Galaxy")(rozlisenie FULL_HD)(SD_Karta ano)(interna_pamat 128)(operacny_system android)(uhlopriecka 6.5)(rozlisenie_fotoaparatu 32)(kapacita_baterie 25))
(mobily(znacka Iphone)(model "14")(rozlisenie FULL_HD)(SD_Karta nie)(interna_pamat 128)(operacny_system iOS)(uhlopriecka 6.1)(rozlisenie_fotoaparatu 12)(kapacita_baterie 25))
(mobily(znacka Iphone)(model "13")(rozlisenie Q_HD)(SD_Karta nie)(interna_pamat 128)(operacny_system iOS)(uhlopriecka 6.1)(rozlisenie_fotoaparatu 12)(kapacita_baterie 20))
(mobily(znacka Asus)(model "Zenfone")(rozlisenie HD)(SD_Karta ano)(interna_pamat 32)(operacny_system android)(uhlopriecka 5.9)(rozlisenie_fotoaparatu 64)(kapacita_baterie 14))
(mobily(znacka Samsung)(model "Galaxy")(rozlisenie Q_HD)(SD_Karta nie)(interna_pamat 128)(operacny_system android)(uhlopriecka 6.1)(rozlisenie_fotoaparatu 12)(kapacita_baterie 25))
(mobily(znacka Motorola)(model "Moto")(rozlisenie FULL_HD)(SD_Karta ano)(interna_pamat 32)(operacny_system android)(uhlopriecka 6.3)(rozlisenie_fotoaparatu 13)(kapacita_baterie 15))
(mobily(znacka Xiaomi)(model "Redmi")(rozlisenie HD)(SD_Karta ano)(interna_pamat 64)(operacny_system android)(uhlopriecka 6.5)(rozlisenie_fotoaparatu 108)(kapacita_baterie 16))
)
(defrule vypis_uvod
=>
(printout t "Vitajte v programe na vyber telefonu" crlf)
(printout t "Prosim vyberte vase specifikacie: " crlf))
(defrule osobne_hodnoty
=>
(printout t "Zadajte znacku" crlf)
(bind ?znacka(read))
(printout t "Zadajte model" crlf)
(bind ?model(read))
(printout t "Zadajte ci vas vybrany telefon podporuje SD Karty (ano/nie)" crlf)
(bind ?SD_Karta(read))
(printout t "Zadajte rozlisenie displeja (HD, FULL_HD, Q_HD)" crlf)
(bind ?rozlisenie(read))
(printout t "Zadajte velkost internej pamete (32 - 512)" crlf)
(bind ?interna_pamat(read))
(printout t "Zadajte uhlopriecku (4.0 - 7.0)" crlf)
(bind ?uhlopriecka(read))
(printout t "Zadajte operacny system(iOS, android, ine)" crlf)
(bind ?operacny_system(read))
(printout t "Zadajte rozlisenie_fotoaparatu (4 - 128)" crlf)
(bind ?rozlisenie_fotoaparatu(read))
(printout t "Zadajte kapacitu baterie v hodinach (5 - 25" crlf)
(bind ?kapacita_baterie(read))
(assert (zakaznik (znacka ?znacka)(model ?model)(rozlisenie ?rozlisenie)(SD_Karta ?SD_Karta)(interna_pamat ?interna_pamat)(operacny_system ?operacny_system) (uhlopriecka ?uhlopriecka) (rozlisenie_fotoaparatu ?rozlisenie_fotoaparatu)(kapacita_baterie ?kapacita_baterie))))
(defrule telefon_je_na_sklade
(and
(zakaznik (znacka ?znacka)(model ?model)(rozlisenie ?rozlisenie)(SD_Karta ?SD_Karta)(interna_pamat ?interna_pamat)(operacny_system ?operacny_system) (uhlopriecka ?uhlopriecka) (rozlisenie_fotoaparatu ?rozlisenie_fotoaparatu)(kapacita_baterie ?kapacita_baterie))
(mobily (znacka ?znacka)(model ?model)(rozlisenie ?rozlisenie)(SD_Karta ?SD_Karta)(interna_pamat ?interna_pamat)(operacny_system ?operacny_system) (uhlopriecka ?uhlopriecka) (rozlisenie_fotoaparatu ?rozlisenie_fotoaparatu)(kapacita_baterie ?kapacita_baterie)))
=>
(printout t "Telefon je na sklade:" ?znacka ?model crlf))
(defrule telefon_nie_je_na_sklade
(and
(zakaznik (znacka ?znacka)(model ?model)(rozlisenie ?rozlisenie)(SD_Karta ?SD_Karta)(interna_pamat ?interna_pamat)(operacny_system ?operacny_system) (uhlopriecka ?uhlopriecka) (rozlisenie_fotoaparatu ?rozlisenie_fotoaparatu)(kapacita_baterie ?kapacita_baterie))
(not(mobily (znacka ?znacka)(model ?model)(rozlisenie ?rozlisenie)(SD_Karta ?SD_Karta)(interna_pamat ?interna_pamat)(operacny_system ?operacny_system) (uhlopriecka ?uhlopriecka) (rozlisenie_fotoaparatu ?rozlisenie_fotoaparatu)(kapacita_baterie ?kapacita_baterie))))
=>
(printout t "Bohuzial zadany telefon nemame na sklade" crlf))
`
`
I tried using AND in the rules but when I did this only the last rule was implemented even if the variables were actually the same.
Both of your rules work. You just need to use the exact values specified in your mobily facts including the same case for symbols/strings and the enclosing quotation marks if you are matching a string.
CLIPS> (reset)
CLIPS> (run)
Vitajte v programe na vyber telefonu
Prosim vyberte vase specifikacie:
Zadajte znacku
Xiaomi
Zadajte model
"Redmi"
Zadajte ci vas vybrany telefon podporuje SD Karty (ano/nie)
ano
Zadajte rozlisenie displeja (HD, FULL_HD, Q_HD)
HD
Zadajte velkost internej pamete (32 - 512)
64
Zadajte uhlopriecku (4.0 - 7.0)
6.5
Zadajte operacny system(iOS, android, ine)
android
Zadajte rozlisenie_fotoaparatu (4 - 128)
108
Zadajte kapacitu baterie v hodinach (5 - 25
16
Telefon je na sklade:XiaomiRedmi
CLIPS> (reset)
CLIPS> (run)
Vitajte v programe na vyber telefonu
Prosim vyberte vase specifikacie:
Zadajte znacku
Xiaomi
Zadajte model
"Redmi"
Zadajte ci vas vybrany telefon podporuje SD Karty (ano/nie)
ano
Zadajte rozlisenie displeja (HD, FULL_HD, Q_HD)
HD
Zadajte velkost internej pamete (32 - 512)
64
Zadajte uhlopriecku (4.0 - 7.0)
6.5
Zadajte operacny system(iOS, android, ine)
android
Zadajte rozlisenie_fotoaparatu (4 - 128)
108
Zadajte kapacitu baterie v hodinach (5 - 25
20
Bohuzial zadany telefon nemame na sklade
CLIPS>
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))))
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))
)
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.