CLIPS:运行时出现“fact-X”,循环选择

时间:2016-12-07 18:42:50

标签: clips expert-system

我正在做一个基于link的车辆诊断专家系统。我已经设法运行我的系统并成功运行,但最后它显示“事实-x”(X是事实数字)。这是一个Decision Tree和代码供参考:

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

我的问题如下:

1)如何在系统运行后不显示事实-X。显示的结果如下:(注意事实-X)

Results

2)如果我想根据选择1-1中的规则1的NO来循环我的问题。 (例如,当规则1触发NO时,它跳转到下一个问题(规则6),如决策树中所示。但是当规则6触发No时,它不会跳回规则1.)如何循环它们?

1 个答案:

答案 0 :(得分:0)

您的代码中包含不正确的平衡括号。加载代码时会生成多个警告:

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> 

以下是导致事实5被打印的规则:

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

我缩进代码来说明开括号和右括号的平衡。 assert语句作为参数包含在第一个printout命令中,这就是为什么你看到Fact-5打印出来的原因。第二个打印输出和缩进命令不包含在规则正文中,因此在执行规则时不会执行它们。

更正后的规则:

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