CLIPS does not recognize deftemplate name - 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))))

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.

How to use and operator and or operator together in CLIPS programming

I want to add a else part for below code as (assert (diabetes-assume false)). I glad if someone can help me to fix this issue.
(defrule diabetes-adult
(disease ?d)
(body-status ?b)
(age ?a&:(and (eq ?d none) (>= ?a 19) (< ?a 51) (or (eq ?b obesity) (eq ?b overweight) )))
=>
(assert (diabetes-assume true)))
You can assume as part of your initial state that diabetes is false and then define a higher salience rule (than the rules which rely on that assumption) to change the assumption to true:
(deffacts assumptions
(diabetes-assume false))
(defrule diabetes-adult-true
(declare (salience 10))
?f <- (diabetes-assume false)
(disease ?d)
(body-status ?b)
(age ?a&:(and (eq ?d none)
(>= ?a 19)
(< ?a 51)
(or (eq ?b obesity) (eq ?b overweight))))
=>
(retract ?f)
(assert (diabetes-assume true)))

How to make the complement and difference operation between two sets in CLIPS?

I need to make complement and difference operations between two sets. I've a example, to do union between two sets, I can reuse this code to make these two other operations.
Thanks
The union example, that I've is:
(deffacts datos-iniciales
(conjunto B C A D E E B C E)
(conjunto E E B F D E))
(defrule inicio
=>
(assert (union)))
(defrule union
?h <- (union $?u)
(conjunto ? $? ?e $?)
(not (union $? ?e $?))
=>
(retract ?h)
(assert (union ?e $?u)))
Specifically, which part of the program should be changed? Thx
Here's how you can compute all three leaving the set-1 and set-2 facts unmodified, ignoring duplicate members, and sorting the results.
CLIPS (6.31 6/12/19)
CLIPS>
(deffacts datos-iniciales
(set-1 B C A D E E B C E)
(set-2 E E B F D E))
CLIPS>
(deffacts universe
(universe A B C D E F G H I J K))
CLIPS>
(deffunction str-sort (?a ?b)
(> (str-compare (sym-cat ?a) (sym-cat ?b)) 0))
CLIPS>
(defrule calcula
=>
(assert (union)
(complement)
(difference)))
CLIPS>
(defrule add-to-union
?union <- (union $?u)
(or (set-1 $? ?v $?)
(set-2 $? ?v $?))
(test (not (member$ ?v ?u)))
=>
(retract ?union)
(assert (union ?u ?v)))
CLIPS>
(defrule add-to-complement
?complement <- (complement $?c)
(universe $?u1 ?v $?u2)
(set-1 $?s)
(test (and (not (member$ ?v ?c))
(not (member$ ?v ?s))))
=>
(retract ?complement)
(assert (complement ?c ?v)))
CLIPS>
(defrule add-to-difference
?difference <- (difference $?d)
(set-1 $? ?v $?)
(set-2 $?set2)
(test (and (not (member$ ?v ?d))
(not (member$ ?v ?set2))))
=>
(retract ?difference)
(assert (difference ?d ?v)))
CLIPS>
(defrule write-union
(declare (salience -10))
(union $?u)
=>
(printout t "The union is " (sort str-sort ?u) crlf))
CLIPS>
(defrule write-complement
(declare (salience -10))
(complement $?c)
=>
(printout t "The complement is " (sort str-sort ?c) crlf))
CLIPS>
(defrule write-difference
(declare (salience -10))
(difference $?d)
=>
(printout t "The difference is " (sort str-sort ?d) crlf))
CLIPS> (reset)
CLIPS> (run)
The union is (A B C D E F)
The complement is (F G H I J K)
The difference is (A C)
CLIPS>

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