0

i am doing a Vehicle diagnosis expert system based on link. I have managed to run my system and runs successfully but at the end it displays "Fact-x"(X being the facts number). Here's a Decision Tree and code for reference:

;; initialize
(deffacts init (start))

;;Main Menu
(defrule Menu
     ?ml  <- (start)
    =>
(printout t crlf crlf crlf 
     "Choose one of the problem areas listed below" crlf crlf
  " 1-  Brake Pedal System. "crlf crlf
  " 2-  Gearbox. "crlf crlf
  " 3-          ." crlf crlf
  " 4-  END SYSTEM. "crlf crlf crlf 
  " Enter no. of your choice: ")
    (bind ?response (read))
    (switch ?response 
        (case 1 then (assert (type 0-1)))
        (case 2 then (assert (type 0-2)))
        (case 3 then (assert (type 0-3)))
        (case 4 then (assert (type quit))))
    (printout t crlf)
    (retract ?ml)))

   ;; submenu1
   (defrule subMenu1 
   ?ml <-  (type 0-1)
  =>
  (printout t crlf crlf crlf 
     "Choose which topic best relates to your problem? "crlf crlf
  " 1-1 Car Pulls One Side When Braking. "crlf crlf 
  " 1-2 Rear Brake Drag. "crlf crlf
  " 1-3 Brake squeal. "crlf crlf 
  " 1-4 END SYSTEM. "crlf crlf crlf
  " Enter no. of your choice: ")
    (bind ?response (read))
    (switch ?response 
        (case 1-1 then (assert (type 1-1)))
        (case 1-2 then (assert (type 1-2)))
        (case 1-3 then (assert (type 1-3)))
        (case 1-4 then (assert (type quit))))
    (printout t crlf)
    (retract ?ml)))

;; END System
(defrule user-quits
(type quit)
=>
(printout t "You have EXIT the System." crlf)
(halt))

  ;; Rule 1 based on choice 1-1
  (defrule car_pulls_one_side_when_braking
   ?ml <-  (type 1-1)
     =>
     (printout t crlf crlf crlf 
     " Was your tyre uneven? (yes|no) "crlf crlf
     " Your answer: ")
     (assert (ifYesNochoice (read))))
    (printout t crlf)
    (retract ?ml)))

 ;;Rule 2 based on Yes answer in Rule 1
 (defrule car_pulls_one_side_when_braking1
    ?ml <- (ifYesNochoice yes)
    =>
    (printout t crlf crlf crlf 
    " Please check your tyre pressure "crlf crlf
    " Is it in good condition? (yes|no) "crlf crlf
    " Your answer: "
    (assert (ifYesNochoice1 (read)))))
    (printout t crlf)
    (retract ?ml)))

 ;;Rule 3 based on Yes answer in Rule 2
 (defrule car_pulls_one_side_when_braking2
  ?ml <-  (ifYesNochoice1 yes)
    =>
    (printout t crlf crlf crlf 
    " Then your car should be no problem. " crlf crlf
    " Thanks for using Vehicle Diagnosis Failure System. " crlf crlf))
    (printout t crlf)
    (retract ?ml)
    (halt)))

 ;; Rule 4 based on NO answer in Rule 2
 (defrule car_pulls_one_side_when_braking3
  ?ml <-  (ifYesNochoice1 no)
    =>
    (printout t crlf crlf crlf
    " Please inflate all the tyres according to the tyre plycard. "crlf crlf
    " Please check again with your technician if problem is solved. "crlf crlf
    " Thanks for using Vehicle Diagnosis Failure System. "crlf crlf))
    (printout t crlf)
    (retract ?ml)
    (halt)))

 ;; Rule 5 based on NO answer in Rule 1
 (defrule car_pulls_one_side_when_braking4
  ?ml <-  (ifYesNochoice no)
    =>
     (printout t crlf crlf crlf 
     " Do you feel a stuck calliper? (yes|no) "crlf crlf
     " Your answer: ")
     (assert (ifYesNochoice2 (read))))
    (printout t crlf)
    (retract ?ml)))

 ;; Rule 6 based on NO answer to Rule 5 
 (defrule car_pulls_one_side_when_braking5
  ?ml <-  (ifYesNochoice2 no)
    =>
    (assert (type1-1))
    (printout t crlf)
    (retract ?ml))

  ;; Rule 7 based on yes answer in Rule 6
  (defrule car_pulls_one_side_when_braking6
  ?ml <-  (ifYesNochoice2 yes)
    =>
     (printout t crlf crlf crlf 
     " Did you service your brake calliper? (yes|no) "crlf crlf
     " Your answer: ")
     (assert (ifYesNochoice3 (read))))
    (printout t crlf)
    (retract ?ml)))

My question is as follows:

1) How do i NOT display the facts-X after the system is run. The results shown are like these: (Note the Fact-X)

Results

2) If i want to do a looping for my question based on NO for Rule 1 in choice 1-1. (e.g when NO is fired for Rule 1, it jumps to next problem(Rule 6) as shown in decision tree. However when No is fired for Rule 6, it does not jumps back to Rule 1.) How do I loop them?

Community
  • 1
  • 1
Isaac
  • 1
  • 1
  • I can't reproduce the issue from your code and description of the problem. Several of your rules (such as car_pulls_one_side_when_braking) do not have properly balanced parentheses. – Gary Riley Dec 07 '16 at 20:36
  • Hi Gary, thanks for your prompt response. i have edited the post with a screenshot of the results of how it looks like. I was able to reproduce it using the current code that i have posted. Not sure why you are unable to reproduce them. I will double check again in regards to paranthesis. But would like to know whether is it possible to loop the choices i make as posted in question 2? Thanks again. Do lemme know if there is any clarification for my above problem. Thanks! – Isaac Dec 09 '16 at 14:35

1 Answers1

0

Your code is full of improperly balanced parentheses. There are multiple warnings generated when the code is loaded:

CLIPS> (clear)
CLIPS> (load "rules.clp")
$*
[CSTRCPSR1] Expected the beginning of a construct.
*
[CSTRCPSR1] Expected the beginning of a construct.
**
[CSTRCPSR1] Expected the beginning of a construct.
*
[CSTRCPSR1] Expected the beginning of a construct.
*
[CSTRCPSR1] Expected the beginning of a construct.
*
[CSTRCPSR1] Expected the beginning of a construct.
*
[CSTRCPSR1] Expected the beginning of a construct.
**
[CSTRCPSR1] Expected the beginning of a construct.

FALSE
CLIPS> 

Here's the rule that's causing Fact-5 to be printed:

(defrule car_pulls_one_side_when_braking1
   ?ml <- (ifYesNochoice yes)
   =>
   (printout t 
      crlf crlf crlf 
      " Please check your tyre pressure "crlf crlf
      " Is it in good condition? (yes|no) "crlf crlf
      " Your answer: "
      (assert (ifYesNochoice1 (read)))
   )
)
(printout t crlf)
(retract ?ml)))

I indented the code to illustrate the balancing of opening and closing parentheses. The assert statement is included as an argument to the first printout command so that is why you are seeing Fact-5 printed. The second printout and retract commands are not included within the body of the rule so they will not be executed when the rule is executed.

The corrected rules:

;; initialize
(deffacts init (start))

;; Main Menu
(defrule Menu
   ?ml  <- (start)
   =>
   (printout t crlf crlf crlf 
      "Choose one of the problem areas listed below" crlf crlf
      " 1-  Brake Pedal System. "crlf crlf
      " 2-  Gearbox. "crlf crlf
      " 3-          ." crlf crlf
      " 4-  END SYSTEM. "crlf crlf crlf 
      " Enter no. of your choice: ")
   (bind ?response (read))
   (switch ?response 
      (case 1 then (assert (type 0-1)))
      (case 2 then (assert (type 0-2)))
      (case 3 then (assert (type 0-3)))
      (case 4 then (assert (type quit))))
   (printout t crlf)
   (retract ?ml))

;; submenu1
(defrule subMenu1 
   ?ml <- (type 0-1)
   =>
   (printout t crlf crlf crlf 
      "Choose which topic best relates to your problem? "crlf crlf
      " 1-1 Car Pulls One Side When Braking. "crlf crlf 
      " 1-2 Rear Brake Drag. "crlf crlf
      " 1-3 Brake squeal. "crlf crlf 
      " 1-4 END SYSTEM. "crlf crlf crlf
      " Enter no. of your choice: ")
   (bind ?response (read))
   (switch ?response 
      (case 1-1 then (assert (type 1-1)))
      (case 1-2 then (assert (type 1-2)))
      (case 1-3 then (assert (type 1-3)))
      (case 1-4 then (assert (type quit))))
   (printout t crlf)
   (retract ?ml))

;; END System
(defrule user-quits
   (type quit)
   =>
   (printout t "You have EXIT the System." crlf)
   (halt))

;; Rule 1 based on choice 1-1
(defrule car_pulls_one_side_when_braking
   ?ml <- (type 1-1)
   =>
   (printout t crlf crlf crlf 
      " Was your tyre uneven? (yes|no) "crlf crlf
      " Your answer: ")
   (assert (ifYesNochoice (read)))
   (printout t crlf)
   (retract ?ml))

;; Rule 2 based on Yes answer in Rule 1
(defrule car_pulls_one_side_when_braking1
   ?ml <- (ifYesNochoice yes)
   =>
   (printout t crlf crlf crlf 
      " Please check your tyre pressure "crlf crlf
      " Is it in good condition? (yes|no) "crlf crlf
      " Your answer: ")
   (assert (ifYesNochoice1 (read)))
   (printout t crlf)
   (retract ?ml))

;; Rule 3 based on Yes answer in Rule 2
(defrule car_pulls_one_side_when_braking2
   ?ml <-  (ifYesNochoice1 yes)
   =>
   (printout t crlf crlf crlf 
      " Then your car should be no problem. " crlf crlf
      " Thanks for using Vehicle Diagnosis Failure System. " crlf crlf)
   (printout t crlf)
   (retract ?ml)
   (halt))

 ;; Rule 4 based on NO answer in Rule 2
(defrule car_pulls_one_side_when_braking3
   ?ml <-  (ifYesNochoice1 no)
   =>
   (printout t crlf crlf crlf
      " Please inflate all the tyres according to the tyre plycard. "crlf crlf
      " Please check again with your technician if problem is solved. "crlf crlf
      " Thanks for using Vehicle Diagnosis Failure System. "crlf crlf)
   (printout t crlf)
   (retract ?ml)
   (halt))

;; Rule 5 based on NO answer in Rule 1
(defrule car_pulls_one_side_when_braking4
   ?ml <-  (ifYesNochoice no)
   =>
   (printout t crlf crlf crlf 
      " Do you feel a stuck calliper? (yes|no) "crlf crlf
      " Your answer: ")
   (assert (ifYesNochoice2 (read)))
   (printout t crlf)
   (retract ?ml))

;; Rule 6 based on NO answer to Rule 5 
(defrule car_pulls_one_side_when_braking5
   ?ml <-  (ifYesNochoice2 no)
   =>
   (assert (type 1-1))
   (printout t crlf)
   (retract ?ml))

;; Rule 7 based on yes answer in Rule 6
(defrule car_pulls_one_side_when_braking6
   ?ml <-  (ifYesNochoice2 yes)
   =>
   (printout t crlf crlf crlf 
      " Did you service your brake calliper? (yes|no) "crlf crlf
      " Your answer: ")
   (assert (ifYesNochoice3 (read)))
   (printout t crlf)
   (retract ?ml))
Gary Riley
  • 10,130
  • 2
  • 19
  • 34
  • Hi Gary, Thanks for pointing it out! My mistake on the bad paranthesis part. As i only written the code inside word and paste it in CLIPS without saving the file. As such, i did not notice the warnings. Out of topic for this: But then, i was able to run it? shouldn't CLIPS be able to detect my error in parentheses? Thanks for the clarification regarding the second printout and retract commands. I am able to understand better now. Last but not least, is there a way that i can loop my rules based on my choices in choice 1-1(in question 2)? – Isaac Dec 10 '16 at 07:59
  • There aren't separate stages in CLIPS for compiling, linking, and executing code as there is in C. You can incrementally add rules at any point. If a rule has errors in it, it doesn't prevent you from adding additional rules after that rule is processed or running the rules that were successfully loaded. For looping behavior, you need to be consistent in the fact that's asserted and the pattern that matches it. Your car_pulls_one_side_when_braking5 asserts (type1-1) with no space, but your pattern in rule car_pulls_one_side_when_braking matches (type 1-1) which includes a space. – Gary Riley Dec 11 '16 at 20:23
  • I see. I have already corrected proper parenthesis in my code. However when loading it still shows an error FALSE like your results. Is there any suggestion you could provide? Ohh, I didn't notice that. Thanks! So basically, it is possible to loop then! – Isaac Dec 20 '16 at 09:45
  • I've attached the corrected rules that load properly. – Gary Riley Dec 20 '16 at 17:41
  • Hi Gary, i've compared the difference between yours and mine. It seems that i still made many mistakes. Thanks for pointing it out! Problem all solved! Have a good day ahead! :) – Isaac Dec 30 '16 at 13:59