Some problems with using "deftemplate"

I'm trying to change the code of the expert system (The Engine Diagnosis Expert System) Add disordered patterns - . Clip does not produce errors, but the questions are not loaded. What am I doing wrong?

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

Upvotes: 1

Views: 527

Answers (1)

Gary Riley
Gary Riley

Reputation: 10757

A deftemplate defines the structure of a fact, but it does not create them. Add a deffacts to your program after the deftemplate definition.

(deffacts start
   (your_car))

When a (reset) command is issued, this will assert the facts contained in any deffacts constructs.

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> 

Upvotes: 1

Related Questions