使用" deftemplate"的一些问题

时间:2015-02-28 14:45:14

标签: clips

我试图更改专家系统的代码(引擎诊断专家系统)添加无序模式 - 。 Clip不会产生错误,但不会加载问题。我做错了什么?

(deftemplate your_car "This is template for describing condition car"
    (slot working-state (default undefined))
    (slot rotation-state (default undefined))   
    (slot spark-state (default undefined))
    (slot charge-state (default undefined))
    (slot symptom (default undefined))
    (slot repair(default undefined))
)

(deffunction ask-question (?question $?allowed-values)
(printout t ?question)
(bind ?answer (read))
(if (lexemep ?answer)
then
(bind ?answer (lowcase ?answer)))
(while (not (member ?answer ?allowed-values)) do
(printout t ?question)
(bind ?answer (read))
(if (lexemep ?answer)
then
(bind ?answer (lowcase ?answer))))
?answer
)
;-----------------------------------------------------------------------------------
(deffunction yes-or-no-p (?question)
(bind ?response (ask-question ?question yes no у n))
(if (or (eq ?response yes) (eq ?response y))
then
TRUE
else
FALSE)
)

;-----------------------------------------------------------------------------------

(defrule determine-engine-state ""
;(your_car (working-state undefined))
;(your_car (repair undefined))
?f1 <- (your_car (working-state undefined)(repair undefined))
=>
(if (yes-or-no-p "Does the engine start (yes/no)? ")
then
(if (yes-or-no-p "Does the engine run normally (yes/no)? ")
then
(modify ?f1 (working-state "engine normal"))
else
(modify ?f1 (working-state "engine unsatisfactory")))
else
(modify ?f1 (working-state "engine does-not-start"))))

;...
;-----------------------------------------------------------------------------------

(defrule no-repairs ""
(declare (salience -10))
;(your_car (repair undefined))
?f1 <- (your_car (repair undefined))
=>
(modify ?f1 (repair "Take your car to a mechanic."))
)

(defrule print-repair ""
(declare (salience 10))
;(your_car (repair ?item))
?f1 <- (your_car (repair ?item))
=>
(printout t crlf crlf)
(printout t "Suggested Repair:")
(printout t crlf crlf)
(format t " %s%n%n%n" ?item)
)

;-----------------------------------------------------------------------------------

(defrule system-banner ""
(declare (salience 10))
=>
(printout t crlf crlf)
(printout t "****************************************" crlf)
(printout t "*  The Engine Diagnosis Expert System  *" crlf)
(printout t "****************************************" crlf)
(printout t crlf crlf)
)

1 个答案:

答案 0 :(得分:0)

deftemplate定义事实的结构,但它不会创建它们。在deftemplate定义之后向程序添加deffacts。

(deffacts start
   (your_car))

当发出(重置)命令时,这将断言任何deffacts构造中包含的事实。

CLIPS> (clear)
CLIPS> 
(deftemplate your_car "This is template for describing condition car"
    (slot working-state (default undefined))
    (slot rotation-state (default undefined))   
    (slot spark-state (default undefined))
    (slot charge-state (default undefined))
    (slot symptom (default undefined))
    (slot repair(default undefined))
)
CLIPS> 
(deffacts start
   (your_car))
CLIPS>    
(deffunction ask-question (?question $?allowed-values)
(printout t ?question)
(bind ?answer (read))
(if (lexemep ?answer)
then
(bind ?answer (lowcase ?answer)))
(while (not (member ?answer ?allowed-values)) do
(printout t ?question)
(bind ?answer (read))
(if (lexemep ?answer)
then
(bind ?answer (lowcase ?answer))))
?answer
)
CLIPS> 
(deffunction yes-or-no-p (?question)
(bind ?response (ask-question ?question yes no у n))
(if (or (eq ?response yes) (eq ?response y))
then
TRUE
else
FALSE)
)
CLIPS> 
(defrule determine-engine-state ""
?f1 <- (your_car (working-state undefined)(repair undefined))
=>
(if (yes-or-no-p "Does the engine start (yes/no)? ")
then
(if (yes-or-no-p "Does the engine run normally (yes/no)? ")
then
(modify ?f1 (working-state "engine normal"))
else
(modify ?f1 (working-state "engine unsatisfactory")))
else
(modify ?f1 (working-state "engine does-not-start"))))
CLIPS> 

(defrule no-repairs ""
(declare (salience -10))
?f1 <- (your_car (repair undefined))
=>
(modify ?f1 (repair "Take your car to a mechanic."))
)
CLIPS> 
(defrule print-repair ""
(declare (salience 10))
?f1 <- (your_car (repair ?item))
=>
(printout t crlf crlf)
(printout t "Suggested Repair:")
(printout t crlf crlf)
(format t " %s%n%n%n" ?item)
)
CLIPS> 
(defrule system-banner ""
(declare (salience 10))
=>
(printout t crlf crlf)
(printout t "****************************************" crlf)
(printout t "*  The Engine Diagnosis Expert System  *" crlf)
(printout t "****************************************" crlf)
(printout t crlf crlf)
)
CLIPS> (reset)
CLIPS> (run)


Suggested Repair:

 undefined




****************************************
*  The Engine Diagnosis Expert System  *
****************************************


Does the engine start (yes/no)? yes
Does the engine run normally (yes/no)? yes


Suggested Repair:

 undefined




Suggested Repair:

 Take your car to a mechanic.


CLIPS>