How to use user input using CLIPS - clips

I am a beginner in CLIPS and cannot figure out how to get the above code to work right despite spending hours on it. Any help would be appreciated.Thank you.
(deftemplate gem
(players <number_robots>)
(Goal_posts <number_goal_posts>)
(Field <dimension>)
(players goalkeeper <number_goalkeepers> <goalkeeper_linear_speed> <goalkeeper_angular_speed> {static, running})
(players defenders <number_defenders> <defender_linear_speed> <defender_angular_speed> {static, running})
(players midfielder <number_midfielders> <midfielder_linear_speed> <midfielder_angular_speed> {static, running})
(players striker <number_strikers> <striker_linear_speed> <striker_angular_speed> {static, running})
(ball <poz_x> <poz_y)
(oponents <nume> <poz_oponents_x> <poz_oponents_y>))
(deffacts initial-state
(player goalkeeper 1 10 6 static)
(player defenders number_defenders 15 10 running)
(player midfielders number_midfielders 17 12 running)
(player strikers number_strikers 9 10 running)
(ball 5 5)
(oponents Robot1 1 2)
(oponents Robot2 2 4)
(oponents Robot3 7 2)
(oponents Robot4 10 20)
(oponents Robot5 11 8)
(oponents Robot6 20 10)
(oponents Robot7 5 9)
)
;The number of robots per team must be variable and the user has to be able to set or adjust it.
(defrule number_of_robots
=>
(printout t "Type the number of robots per team"
crlf)
(assert (number_robots (read)))
(printout t "Type the number of goal posts" crlf)
(assert (number_goal_posts (read))))
(defrule check-input
?number_robots <-
(number ?number_robots)
(test (integerp ?number_robots))
=>
(retract ?number_robots)
(printout t "Correct number of robots" crlf))
(defrule check-input1
?number_goal_posts <-
(number ?number_goal_posts)
(test (integerp ?number_goal_posts))
=>
(retract ?number_goal_posts)
(printout t "Correct number of goal posts" crlf))
;The user will initially input data on the dimension of the fotball field and goal post.
(defrule dimension_field
=>
(printout t "Type the football field dimension" crlf)
(assert (dimension (read))))
(defrule check-input1
?dimension <-
(number ?dimension)
(test (integerp ?dimension))
=>
(retract ?dimension)
(printout t "Correct dimension of footbal field" crlf))
;Each team must have a goalkeeper, while the number of defenders, midfielders and strikers is to be set by the user to match the stated number of players
(defrule reading-input
=>
printout t "Type the number of defenders" crlf
(assert (number_defenders(read)))
printout t "Type the number of midfielders" crlf
(assert (number_midfielders(read)))
printout t "Type the number of strikers" crlf
(assert (number_strikers(read)))
)
(defrule check-input2
?number_defenders <-
(number ?number_defenders)
(test (integerp ?number_defenders))
=>
(retract ?number_defenders)
(printout t "Correct number of defenders " crlf))
(defrule check-input3
?number_midfielders <-
(number ?number_midfielders)
(test (integerp ?number_midfielders))
=>
(retract ?number_midfielders)
(printout t "Correct number of midfielders " crlf))
(defrule check-input4
?number_strikers <-
(number ?number_strikers)
(test (integerp ?number_strikers))
=>
(retract ?number_strikers)
(printout t "Correct number of strikers " crlf))
)
My errors after running the code are:
[PRNTUTIL2] Syntax Error: Check appropriate syntax for deftemplate.
ERROR:
(deftemplate MAIN::gem
(players
[CSTRCPSR1] WARNING: Redefining deffacts: initial-state
[CSTRCPSR1] WARNING: Redefining defrule: number_of_robots +j Defining
defrule: check-input =j+j
[CSTRCPSR1] WARNING: Redefining defrule: check-input1 =j+j
[CSTRCPSR1] WARNING: Redefining defrule: dimension_field +j
[CSTRCPSR1] WARNING: Redefining defrule: check-input1 =j+j
[CSTRCPSR1] WARNING: Redefining defrule: reading-input +j
[CSTRCPSR1] WARNING: Redefining defrule: check-input2 =j+j
[CSTRCPSR1] WARNING: Redefining defrule: check-input3 =j+j
[CSTRCPSR1] WARNING: Redefining defrule: check-input4 =j+j
[CSTRCPSR1] Expected the beginning of a construct.

CLIPS>
(defrule number_of_robots
=>
(printout t "Type the number of robots per team: ")
(assert (number_robots (read)))
(printout t "Type the number of goal posts: ")
(assert (number_goal_posts (read))))
CLIPS>
(defrule check-robots
?number <- (number_robots ?number_robots)
(test (not (integerp ?number_robots)))
=>
(retract ?number)
(printout t "Correct number of robots." crlf))
CLIPS>
(defrule check-goal-posts
?number <- (number_goal_posts ?number_goal_posts)
(test (not (integerp ?number_goal_posts)))
=>
(retract ?number)
(printout t "Correct number of goal posts." crlf))
CLIPS>
(defrule dimension_field
=>
(printout t "Type the football field dimension: ")
(assert (dimension (read))))
CLIPS>
(defrule check-dimension
?dim <- (dimension ?dimension)
(test (not (integerp ?dimension)))
=>
(retract ?dim)
(printout t "Correct dimension of football field." crlf))
CLIPS>
(defrule reading-input
=>
(printout t "Type the number of defenders: ")
(assert (number_defenders (read)))
(printout t "Type the number of midfielders: ")
(assert (number_midfielders (read)))
(printout t "Type the number of strikers: ")
(assert (number_strikers (read))))
CLIPS>
(defrule check-defenders
?number <- (number_defenders ?number_defenders)
(test (not (integerp ?number_defenders)))
=>
(retract ?number)
(printout t "Correct number of defenders." crlf))
CLIPS>
(defrule check-midfielders
?number <- (number_midfielders ?number_midfielders)
(test (not (integerp ?number_midfielders)))
=>
(retract ?number)
(printout t "Correct number of midfielders." crlf))
CLIPS>
(defrule check-strikers
?number <- (number_strikers ?number_strikers)
(test (not (integerp ?number_strikers)))
=>
(retract ?number)
(printout t "Correct number of strikers." crlf))
CLIPS> (reset)
CLIPS> (run)
Type the number of robots per team: 1
Type the number of goal posts: 2
Type the football field dimension: 3
Type the number of defenders: 4
Type the number of midfielders: 5
Type the number of strikers: 6
CLIPS> (reset)
CLIPS> (run)
Type the number of robots per team: a
Type the number of goal posts: b
Correct number of goal posts.
Correct number of robots.
Type the football field dimension: c
Correct dimension of football field.
Type the number of defenders: d
Type the number of midfielders: e
Type the number of strikers: f
Correct number of strikers.
Correct number of midfielders.
Correct number of defenders.
CLIPS>

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

How to compare a global variable to a string in 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>

Obtain ID of fact I just asserted

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>

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>

Resources