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

51 Views Asked by At

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?

1

There are 1 best solutions below

0
Gary Riley On
  1. 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.
  2. The protein-advice defrule expects a protein-needed fact but a daily-protein fact is asserted by your other rules.
  3. The carbohydrate-advice defrule expects a carbohydrate-needed fact but a daily-carbohydrates fact is asserted by your other rules.
  4. The fiber-advice defrule expects a fiber-needed fact but no fiber related facts are asserted by any of your rules.
  5. The calcium-advice defrule expects a calcium-needed fact but a daily-calcium fact is asserted by your other rules.
  6. Your other rules that print results have similar issues.