Well, the title says it all I'm a student and I was sent to make an expert system in CLIPS with 100 questions. So I decided to do 10 main questions (let's call them A, B, C, D, E, F, G, H, I, J) and each one is broken down (A1, B1, C1, D1, E1, F1, G1, H1 , I1, J1) then if A1 is true must come A2 and then A3 and so, but I do not know how to do it if I start it right. First A if I say B does not come and if I say if B1 comes but if in B1 I say whether or not it jumps to C and loses the order. Here I leave a photo and all the code. I wait for your help :c
(sorry if my English is bad)
;;;==============================================================
;;; Sistema Experto de Triaje
;;;
;;; CLIPS Version 6.3 Example
;;;==============================================================
;;*************************
;;*DEFINICION DE FUNCIONES*
;;*************************
(deffunction preguntas (?pregunta $?valor)
(printout t ?pregunta)
(bind ?respuesta (read))
(if (lexemep ?respuesta)
then (bind ?respuesta (lowcase ?respuesta)))
(while (not (member ?respuesta ?valor)) do
(printout t ?pregunta)
(bind ?respuesta (read))
(if (lexemep ?respuesta)
then (bind ?respuesta (lowcase ?respuesta))))
?respuesta)
(deffunction si-o-no-p (?pregunta)
(bind ?responde (preguntas ?pregunta si no s n))
(if (or (eq ?responde si) (eq ?responde s))
then si
else no))
;;;********************
;;;*REGLAS DE CONSULTA*
;;;********************
(defrule determina-emergencia ""
(not (usuario-emergencia ?))
(not (color ?))
=>
(assert (usuario-emergencia (si-o-no-p "¿Posee el paciente emergencia alguna? (Si/No)"))))
(defrule determina-signos ""
(usuario-emergencia si)
(not (usuario-vivo ?))
(not (color ?))
=>
(assert (usuario-vivo (si-o-no-p "¿Posee el paciente signos vitales? (Si/No)"))))
(defrule determina-sangre ""
(usuario-vivo si)
(not (color ?))
=>
(assert (usuario-sangre (si-o-no-p "¿Tiene el paciente insiciones o hemorragias? (Si/No)"))))
(defrule determina-combulsion ""
(usuario-vivo si)
(usuario-sangre no)
(not (color ?))
=>
(assert (usuario-combulsion (si-o-no-p "¿Tiene el paciente estado de combulsion alguno? (Si/No)"))))
(defrule determina-conciencia ""
(usuario-vivo si)
(usuario-sangre no)
(usuario-combulsion no)
(not (color ?))
=>
(assert (usuario-conciente (si-o-no-p "¿Esta el paciente en estado conciente? (Si/No)"))))
(defrule determina-quemadura ""
(usuario-vivo si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(not (color ?))
=>
(assert (usuario-quemadura (si-o-no-p "¿Tiene el paciente quemaduras? (Si/No)"))))
(defrule determina-dolor ""
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(not (color ?))
=>
(assert (usuario-dolor (si-o-no-p "¿Posee el paciente dolor alguno? (Si/No)"))))
(defrule determina-toxico ""
(usuario-vivo si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor no)
(not (color ?))
=>
(assert (usuario-toxico (si-o-no-p "¿Ha ingerido el paciente alguna sustancia toxica? (Si/No)"))))
(defrule determina-vomito ""
(usuario-vivo si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor no)
(usuario-toxico no)
(not (color ?))
=>
(assert (usuario-vomito (si-o-no-p "¿Posee el paciente vomitos severos y persistentes? (Si/No)"))))
(defrule determina-respiracion ""
(usuario-vivo si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor no)
(usuario-toxico no)
(usuario-vomito no)
(not (color ?))
=>
(assert (usuario-respiracion (si-o-no-p "¿Tiene el paciente dificultad al respirar en estado de reposo?(Si/No)"))))
(defrule determina-enfermedad ""
(usuario-vivo si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor no)
(usuario-toxico no)
(usuario-vomito no)
(usuario-respiracion no)
(not (color ?))
=>
(assert (usuario-enfermedad (si-o-no-p "¿Considera que el paciente esta enfermo? (Si/No)"))))
;;============
;;=Emorragias=
;;============
(defrule determina-tipo-sangre ""
(usuario-sangre si)
(not (color ?))
=>
(assert (usuario-tipo-sangre (preguntas "¿Puede definir el tipo de hemorragia? Externa(Heridas)/Exteriorizada(sangre que sale por los orificios del cuerpo)" externa exteriorizada))))
(defrule determina-zona-sangre ""
(usuario-sangre si)
(usuario-sangre-tipo exteriorizada)
(not (color ?))
=>
(assert (usuario-zona-sangre (preguntas "¿Puede expecificar por donde emerge la hemorragia? Boca/Nariz/Recto/Vagina/uretra/Oido/Ojo" boca nariz recto vagina uretra oido ojo))))
(defrule determina-boca-sangre ""
(usuario-sangre si)
(usuario-sangre-tipo exteriorizada)
(usuario-zona-sangre boca)
(not (color ?))
=>
(assert (usuario-boca-sangre (preguntas "¿De que manera expulsa sangre por la boca? Tosiendo/Vomitando" tosiendo vomitando))))
(defrule determina-sangre-externa ""
(usuario-sangre si)
(usuario-sangre-tipo externa)
(not (color ?))
=>
(assert (usuario-sangre-externa (preguntas "¿De que manera se ha producido la herida? Vehicular/Caida/Agresion" vehicular caida agresion))))
;;============
;;=Combulsion=
;;============
(defrule determina-tiempo-combulsion ""
(usuario-vivo si)
(usuario-combulsion si)
(not (color ?))
=>
(assert (usuario-tiempo-combulsion (preguntas "¿Hace cuanto fue la ultima combulsion? Recientemente/1h/4h/8h/Ayer" recientemente 1h 4h 8h ayer))))
;;============
;;=Quemaduras=
;;============
(defrule determina-tipo-quemadura ""
(usuario-vivo si)
(usuario-quemadura si)
(not (color ?))
=>
(assert (usuario-tipo-quemadura (preguntas "¿Puede definir el grado de la quemadura? Primera/Segunda/Tercera" primera segundo tercera))))
;;=======
;;=Dolor=
;;=======
(defrule determina-tipo-dolor ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(not (color ?))
=>
(assert (usuario-tipo-dolor (preguntas "¿Que tipo de dolor padece? Muscular/Oseo/Cefalico/Visceral" muscular oseo cefalico visceral))))
;;========
;;=DolorM=
;;========
(defrule determina-sintoma-dolor-m ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor muscular)
(not (color ?))
=>
(assert (usuario-sintoma-M (preguntas "¿Posee sintomas como Hinchazon/Enrojecimiento/Erupcion/Entumecimiento?" hinchazon enrojecimiento erupcion entumecimiento))))
(defrule determina-esguince ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor muscular)
(usuario-sintoma enrojecimiento)
(usuario-sintoma entumecimiento)
(not (color ?))
=>
(assert (usuario-esguince (si-o-no-p "¿Considera usted que posee un esguince ?(Si/No)"))))
;;========
;;=DolorO=
;;========
(defrule determina-cancer ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor oseo)
(not (color ?))
=>
(assert (usuario-cancer (si-o-no-p "¿Tiene usted cancer en los huesos o posee antecedentes familiares?(Si/No)"))))
(defrule determina-columna ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor oseo)
(not (color ?))
=>
(assert (usuario-columna (si-o-no-p "¿Su dolor radica en la columna?(Si/No)"))))
(defrule determina-sintoma-dolor-o ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor oseo)
(usuario-cancer no)
(usuario-columna si)
(usuario-columna no)
(not (color ?))
=>
(assert (usuario-sintoma-o (preguntas "¿cual considera que es su caso Fractura/Gangrena(falta de sangre)/Infeccion/Dislocacion?" fractura gangrena infeccion dislocacion))))
(defrule determina-zona-fractura ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor oseo)
(usuario-cancer no)
(usuario-columna no)
(not (color ?))
=>
(assert (usuario-zona-fractura (preguntas "¿En que region se encuentra la fractura? Brazos/Piernas/Torso/Espalda/Cabeza" brazos piernas torso espalda cabeza ))))
(defrule determina-fractura ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor oseo)
(usuario-sintoma-o fractura)
(usuario-zona-fractura brazo)
(usuario-zona-fractura piernas)
(not (color ?))
=>
(assert (usuario-fractura (si-o-no-p "¿El hueso emerge del cuerpo?(Si/No)"))))
;;==========
;;=Visceral=
;;==========
(defrule determina-sintoma-dolor-v ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor visceral)
(not (color ?))
=>
(assert (usuario-sintoma-dolor-v (preguntas "¿En que region posee el dolor Toracica/Abdominal/Genital?" Toracica Abdominal Genital))))
(defrule determina-dolor-abdomen ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor visceral)
(usuario-sintoma-dolor-v abdominal)
(not (color ?))
=>
(assert (usuario-abdomen (preguntas "¿En que region se intencifica el dolor Superior/Inferior/Derecha/Izquierda?" superior inferior derecha izquierda ))))
(defrule determina-organo ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor visceral)
(usuario-sintoma-dolor-v toracica)
(not (color ?))
=>
(assert (usuario-organo (preguntas "¿Considera que su dolor se relaciona con los pulmones o con el corazon?" corazon pulmones ))))
(defrule determina-dificultad ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor visceral)
(usuario-sintoma-o fractura)
(usuario-sintoma-dolor-v toracica)
(not (color ?))
=>
(assert (usuario-dificultad (si-o-no-p "¿Tiene dificultad al respirar?(Si/No)"))))
(defrule determina-caida ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor visceral)
(usuario-sintoma-o fractura)
(usuario-sintoma-dolor-v toracica)
(usuario-respiracion si)
(usuario-respiracion no)
(not (color ?))
=>
(assert (usuario-caida (si-o-no-p "¿Ha sufrido caidas o recibido golpes recientes?(Si/No)"))))
;;;=========
;;;=Genital=
;;;=========
(defrule determina-genital ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor genital)
(not (color ?))
=>
(assert (usuario-genital (si-o-no-p "¿Considera usted que se trata de una infeccion presenta irritacion erupcion o sintomas similares?(Si/No)"))))
(defrule determina-orina ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor genital)
(not (color ?))
=>
(assert (usuario-orina (si-o-no-p "¿Ha orinado sangre?(Si/No)"))))
(defrule determina-gestacion ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor genital)
(not (color ?))
=>
(assert (usuario-gestacion (si-o-no-p "¿Se encuentra usted en estado de gestacion (Embarazo)?(Si/No)"))))
;;==========
;;=Cefalico=
;;==========
(defrule determina-sintoma-dolor-c ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor cefalico)
(not (color ?))
=>
(assert (usuario-region-dolor-c (preguntas "¿En que region se intencifica el dolor Frente/Cuello/Nariz/Cerebro?" frente cuello nariz cerebro))))
(defrule determina-mucosa ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor cefalico)
(usuario-region-dolor-c frente)
(usuario-region-dolor-c nariz)
(not (color ?))
=>
(assert (usuario-mucosa (si-o-no-p "¿Posee dificultad para respirar u ojos rojizos o llorosos?(Si/No)"))))
(defrule determina-migraña ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor cefalico)
(usuario-region-dolor-c cerebro)
(not (color ?))
=>
(assert (usuario-migraña (si-o-no-p "¿Considera usted que el dolor interfiere con sus actividades diarias ?(Si/No)"))))
(defrule determina-vision ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor cefalico)
(usuario-region-dolor-c cerebro)
(usuario-migraña si)
(not (color ?))
=>
(assert (usuario-vision(si-o-no-p "¿Presenta sintomas como perdida de vision problemas al habla o mala articulacion?(Si/No)"))))
(defrule determina-cuello-debil ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor cefalico)
(usuario-region-dolor-c cuello)
(not (color ?))
=>
(assert (usuario-cuello-debil (si-o-no-p "¿Siente debilidad en los brazos u hombros ?(Si/No)"))))
(defrule determina-rotacion ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor cefalico)
(usuario-region-dolor-c cuello)
(usuario-cuello-debil no)
(not (color ?))
=>
(assert (usuario-rotacion (si-o-no-p "¿Puede rotar el cuello con facilidad?(Si/No)"))))
(defrule determina-cuello ""
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-dolor si)
(usuario-tipo-dolor cefalico)
(usuario-region-dolor-c cuello)
(usuario-cuello-debil no)
(usuario-rotacion no)
(not (color ?))
=>
(assert (usuario-cuello (si-o-no-p "¿Esta su dolor asociado con la garganta?(Si/No)"))))
(defrule determina-cuello-sintoma ""
(usuario-cuello si)
(usuario-vivo si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(not (color ?))
=>
(assert (usuario-cuello-sintoma (si-o-no-p "¿Presenta sintomas como falta del habla o dificultad al tragar?(Si/No)"))))
(defrule determina-cuello-columna ""
(usuario-cuello-debil si)
(usuario-conciencia si)
(usuario-sangre no)
(usuario-combulsion no)
(usuario-conciente si)
(usuario-quemadura no)
(usuario-rotacion si)
(not (color ?))
=>
(assert (usuario-cuello-columna (si-o-no-p "¿Relaciona usted el dolor con la columna?(Si/No)"))))
;;=========
;;=Toxicos=
;;=========
(defrule determina-tipo-toxico ""
(usuario-toxico si)
(not (color ?))
=>
(assert (usuario-tipo-toxico (si-o-no-p "¿Ha ingerido elementos como Gasolina Cloro o Detergentes?(Si/No)"))))
(defrule determina-toxico-daños ""
(usuario-toxico si)
(usuario-tipo-toxico no)
(not (color ?))
=>
(assert (usuario-daños (si-o-no-p "¿Presenta perdida de vision/Dolor de garganta/Ardor en los ojos, nariz o labios?(Si/No)"))))
(defrule determina-sangre-heces ""
(usuario-toxico si)
(usuario-daños no)
(not (color ?))
=>
(assert (usuario-haces (si-o-no-p "¿Presenta sangre en las heces?(Si/No)"))))
(defrule determina-toxico-vomitos ""
(usuario-toxico si)
(usuario-daños no)
(usuario-haces no)
(not (color ?))
=>
(assert (usuario-vomitos (si-o-no-p "¿Ha presentado vomitos?(Si/No)"))))
(defrule determina-toxico-inflamacion ""
(usuario-toxico si)
(usuario-daños no)
(usuario-haces no)
(usuario-vomitos no)
(not (color ?))
=>
(assert (usuario-inflamacion (si-o-no-p "¿Posee dificultad respiratoria o inflamacion en la garganta?(Si/No)"))))
(defrule determina-huecos ""
(usuario-toxico si)
(usuario-daños no)
(usuario-haces no)
(usuario-vomitos no)
(usuario-inflamacion)
(not (color ?))
=>
(assert (usuario-huecos (si-o-no-p "¿Presenta irritacion u orificion en al piel?(Si/No)"))))
;;======
;;=Azul=
;;======
(defrule determina-actividad ""
(usuario-emergencia no)
(not (color ?))
=>
(assert (usuario-actividad (si-o-no-p "¿Viene a entregar documentos como resultados u otros? (Si/No)"))))
;;;**************
;;;*DIAGNOSTICOS*
;;;**************
(defrule estado-vivo ""
(usuario-emergencia si)
(usuario-vivo no)
(not (color ?))
=>
(assert (color "Codigo Rojo")))
(defrule estado-inutil ""
(usuario-emergencia no)
(usuario-actividad no)
(not (color ?))
=>
(assert (color "Por favor no moleste")))
(defrule no-color ""
(declare (salience -20))
(not (color ?))
=>
(assert (color "Lo lamento no estoy capacitado para ayudarlo")))
;;;************************
;;;*REGLAS DE CONCLUCIONES*
;;;************************
(defrule system-banner ""
(declare (salience 20))
=>
(printout t crlf crlf)
(printout t "Sistema Experto de Triaje")
(printout t crlf crlf))
(defrule print-color ""
(declare (salience 20))
(color ?item)
=>
(printout t crlf crlf)
(printout t "Su color es:")
(printout t crlf crlf)
(format t " %s%n%n%n" ?item))
enter image description here
If you run up to the point that you expect the determina-zona-sangre rule to execute, you can enter a matches command for that rule and see that the second pattern is unmatched. A facts command shows that the last fact asserted is (usuario-tipo-sangre exteriorizada). The rule however matches (usuario-sangre-tipo exteriorizada). Since the fact doesn't match the pattern, the rule will not be executed.
CLIPS> (reset)
CLIPS> (watch rules)
CLIPS> (run 5)
FIRE 1 system-banner: *
Sistema Experto de Triaje
FIRE 2 determina-emergencia: *,*
¿Posee el paciente emergencia alguna? (Si/No)si
FIRE 3 determina-signos: f-1,*,*
¿Posee el paciente signos vitales? (Si/No)si
FIRE 4 determina-sangre: f-2,*
¿Tiene el paciente insiciones o hemorragias? (Si/No)si
FIRE 5 determina-tipo-sangre: f-3,*
¿Puede definir el tipo de hemorragia? Externa(Heridas)/Exteriorizada(sangre que sale por los orificios del cuerpo)exteriorizada
CLIPS> (matches determina-zona-sangre)
Matches for Pattern 1
f-3
Matches for Pattern 2
None
Matches for Pattern 3
None
Partial matches for CEs 1 - 2
None
Partial matches for CEs 1 - 3
None
Activations
None
(1 0 0)
CLIPS> (facts)
f-0 (initial-fact)
f-1 (usuario-emergencia si)
f-2 (usuario-vivo si)
f-3 (usuario-sangre si)
f-4 (usuario-tipo-sangre exteriorizada)
For a total of 5 facts.
CLIPS> (ppdefrule determina-zona-sangre)
(defrule MAIN::determina-zona-sangre ""
(usuario-sangre si)
(usuario-sangre-tipo exteriorizada)
(not (color ?))
=>
(assert (usuario-zona-sangre (preguntas "¿Puede expecificar por donde emerge la hemorragia? Boca/Nariz/Recto/Vagina/uretra/Oido/Ojo" boca nariz recto vagina uretra oido ojo))))
CLIPS>
Related
I'm new in Clips. I'd like to know if it is a way to read an array (chain of numeric or characters with an index, sorry if it's the wrong name) on LHS. I have rules to ask for a value (s,cs,cn,n) then it assert the value to next asking rule, to finally read all the values in an answering rule to get a diagnostic, but in my small example I have 4 questions and 4 options for each one so mixing all the answers would give me 64 rules, and I have so at least 30 questions in my program so I think that would be too much rules (I'm doing my first Expert System an maybe this is normal). In any case I think I could get the values from questions into an array an read it in answering rules, but my questions are:
*How can I bind the values from my function into an array?
*Is it possible to verify that array in LHS?
*Do you have any other idea to verify the answer-rules? Hope you can help me.
(deffunction funcionPregunta (?pregunta $?valoresAceptados) ;;ask-question function
(printout t ?pregunta)
(bind ?respuesta (read))
(if (lexemep ?respuesta)
then (bind ?respuesta (lowcase ?respuesta)))
(while (not (member$ ?respuesta ?valoresAceptados)) do
(printout t ?pregunta)
(bind ?respuesta (read))
(if (lexemep ?respuesta)
then (bind ?respuesta (lowcase ?respuesta))))
?respuesta)
;;===============================================================
;; QUESTION RULES
;;===============================================================
(defrule pregunta1T5 "AGORAFOBIA"
(not (diagnostico ?))
=>
(assert (Pregunta2T5
(funcionPregunta "1.Siente miedo o ansiedad marcada. (always/frecuently/rare/never)? "
s cs cn n))))
(defrule Pregunta2T5 "AGORAPUBLICO"
(not (diagnostico ?))
(Pregunta2T5 ?Pregunta2T5)
=>
(assert (Pregunta3T5
(funcionPregunta "2.Siente miedo en una multitud. (always/frecuently/rare/never)? "
s cs cn n)))
)
(defrule Pregunta3T5 "AGORAMIEDO"
(not (diagnostico ?))
(Pregunta3T5 ?Pregunta3T5)
=>
(assert (Pregunta4T5
(funcionPregunta "3.Miedo de estar en una situacion. (always/frecuently/rare/never)? "
s cs cn n)))
)
(defrule Pregunta4T5 "AGORAANSIEDAD"
... ;; similar rules
;;===============================================================
;; ANSWERS RULES
;;===============================================================
(defrule Respuesta1T6 "RESULTADO 1 TAS"
(not (diagnostico ?))
(Pregunta2T6 s)(Pregunta3T6 s)(Pregunta4T6 s)(Pregunta5T6 s)
=>
(assert (diagnostico "TRASTORNO DE ANSIEDAD SOCIAL"))
)
(defrule Respuesta2T6 "RESULTADO 2 TAS"
(not (diagnostico ?))
(Pregunta2T6 cs)(Pregunta3T6 s)(Pregunta4T6 s)(Pregunta5T6 s)
=>
(assert (diagnostico "TRASTORNO DE ANSIEDAD SOCIAL"))
)
In the case of the two answer rules you've already got, the simplest way to reduce the number of rules is just to combine them:
(defrule Respuesta1T6-2T6
(not (diagnostico ?))
(Pregunta2T6 s | cs) ; s or cs is allowed
(Pregunta3T6 s)
(Pregunta4T6 s)
(Pregunta5T6 s)
=>
(assert (diagnostico "TRASTORNO DE ANSIEDAD SOCIAL")))
If you're creating lots of rules that differ only in the constants matched in the patterns, you should consider representing the rules as a combination of facts containing these constants and generic rules to process that data. For example, you could rewrite your question rules like this:
(deftemplate Pregunta ; question
(slot identidad) ; ID
(slot texto) ; text
(multislot respuestas) ; responses
(slot precursora ; precursor
(default ninguna))) ; none
(deftemplate Responder ; answer
(slot identidad) ; ID
(slot valor)) ; value
(deffacts Preguntas
(Pregunta (identidad AGORAFOBIA)
(texto "1. Siente miedo o ansiedad marcada. (always/frecuently/rare/never)? ")
(respuestas s cs cn n))
(Pregunta (identidad AGORAPUBLICO)
(texto "2. Siente miedo en una multitud. (always/frecuently/rare/never)? ")
(respuestas s cs cn n)
(precursora AGORAFOBIA))
(Pregunta (identidad AGORAMIEDO)
(texto "3. Miedo de estar en una situacion. (always/frecuently/rare/never)? ")
(respuestas s cs cn n)
(precursora AGORAPUBLICO)))
(defrule pedir-pregunta ; ask question
(not (diagnostico ?))
(Pregunta (identidad ?id)
(texto ?t)
(respuestas $?r)
(precursora ?p))
(or (test (eq ?p ninguna))
(Responder (identidad ?p)))
=>
(assert (Responder (identidad ?id)
(valor (funcionPregunta ?t ?r)))))
And your diagnosis rules like this:
(deftemplate Trastorno ; disorder
(slot nombre) ; name
(multislot sintomas)) ; symptoms
(deftemplate Sintoma
(slot identidad) ; ID
(slot responder) ; answer
(multislot valors)) ; values
(deffacts Trastornos
(Trastorno (nombre "TRASTORNO DE ANSIEDAD SOCIAL")
(sintomas AGORAFOBIA-cs-s AGORAPUBLICO-s AGORAMIEDO-s)))
(deffacts Sintomas
(Sintoma (identidad AGORAFOBIA-cs-s)
(responder AGORAFOBIA)
(valors cs s))
(Sintoma (identidad AGORAPUBLICO-s)
(responder AGORAPUBLICO)
(valors s))
(Sintoma (identidad AGORAMIEDO-s)
(responder AGORAMIEDO)
(valors s)))
(defrule Respuesta
(not (diagnostico ?))
(Trastorno (nombre ?n)) ; There is a disorder.
(forall (Trastorno (nombre ?n) ; For every symptom
(sintomas $? ?s $?)) ; of the disorder,
(Sintoma (identidad ?s) ; there is a list
(responder ?r) ; of possible values
(valors $?sv)) ; for that symptom
(Responder (identidad ?r) ; matched by a response.
(valor ?v&:(member$ ?v ?sv))))
=>
(assert (diagnostico ?n)))
i have a project in clips with three modules, at the end of the second module i ask to user if he want to retract one of the previous answer, if he retract one of the answer of the second module, i need to retract all the answers of the second module and re-ask again. After i retract all the answers of the second module, i expect that this rule is activated
(defrule SECONDMODULE::domanda-esperto
(declare (salience ?*highest-priority*))
(livello-utente (livello esperto)) ;;assert in FIRSTMODULE and not retract
=>
(something)
)
But this rule is never activeted and it not apper in the AGENDA also if the facts that match the LHS is present in the fact list.
Sorry for my bad english.
EDIT.
#Gary First of all i ask 5 question to user, that are this:
(defrule starting-rule
(declare (salience ?*highest-priority*) (auto-focus TRUE))
=>
(printout t "***Inizio***" crlf)
(focus PROFILO)
(set-strategy random))
(defrule PROFILO::chiedi-se-possiede-auto
(not (domanda (nome possiede-auto) (domanda ?) (risposta ?)))
=>
(bind ?risposta (si-o-no "L'auto e' tua? "))
(assert (domanda (nome possiede-auto) (domanda "L'auto e' tua? ") (risposta ?risposta)))
)
(defrule PROFILO::frequenza-utilizzo-auto
(not(domanda (nome frequenza-utilizzo-auto) (domanda ?) (risposta ?)))
=>
(bind ?risposta (risposte-range "Quante volte a settimana in media utilizzi l'auto? " 0 1-2 3-5 5-7 ))
(assert (domanda (nome frequenza-utilizzo-auto) (domanda "Quante volte a settimana in media utilizzi l'auto? " ) (risposta ?risposta)))
)
(defrule PROFILO::conoscenza-meccanica-auto
(not (domanda (nome conoscenza-meccanica-auto) (domanda ?) (risposta ?)))
=>
(bind ?risposta (risposte-range "Quanto ti consideri esperto della meccanica dell'auto?" 0 1 2 3 4 5))
(assert (domanda (nome conoscenza-meccanica-auto) (domanda "Quanto ti consideri esperto della meccanica dell'auto?") (risposta ?risposta)))
)
(defrule PROFILO::kit-riparazione-rapida
(not (domanda (nome kit-riparazione-rapida) (domanda ?) (risposta ?)))
=>
(bind ?risposta (si-o-no "Possiedi un kit di riparazione rapida?"))
(assert (domanda (nome kit-riparazione-rapida) (domanda "Possiedi un kit di riparazione rapida?") (risposta ?risposta)))
)
(defrule PROFILO::anni-possesso-patente
(not(domanda (nome anni-possesso-patente) (domanda ?) (risposta ?)))
=>
(bind ?risposta (risposte-range "Da quanti anni possiedi la patente? " <1 1-5 >5 ))
(assert (domanda (nome anni-possesso-patente) (domanda "Da quanti anni possiedi la patente? ") (risposta ?risposta)))
)
After this i fire a rule that in according with the user asnwers delineate the profile of the user
(defrule PROFILO::livello-utente
?a<-(domanda (nome possiede-auto) (domanda ?) (risposta ?))
?b<-(domanda (nome anni-possesso-patente) (domanda ?) (risposta ?))
?c<-(domanda (nome conoscenza-meccanica-auto) (domanda ?) (risposta ?))
?d<-(domanda (nome kit-riparazione-rapida) (domanda ?) (risposta ?))
?e<-(domanda (nome frequenza-utilizzo-auto) (domanda ?) (risposta ?))
=>
(switch (fact-slot-value ?a risposta)
(case TRUE then (bind ?*punteggio* (+ ?*punteggio* 1)))
)
(switch (fact-slot-value ?d risposta)
(case TRUE then (bind ?*punteggio* (+ ?*punteggio* 1)))
)
(switch (fact-slot-value ?b risposta)
(case <1 then (bind ?*punteggio* (+ ?*punteggio* 1)))
(case 1-5 then (bind ?*punteggio* (+ ?*punteggio* 2)))
(case >5 then (bind ?*punteggio* (+ ?*punteggio* 3)))
)
(switch (fact-slot-value ?c risposta)
(case 1 then (bind ?*punteggio* (+ ?*punteggio* 1)))
(case 2 then (bind ?*punteggio* (+ ?*punteggio* 2)))
(case 3 then (bind ?*punteggio* (+ ?*punteggio* 3)))
(case 4 then (bind ?*punteggio* (+ ?*punteggio* 4)))
(case 5 then (bind ?*punteggio* (+ ?*punteggio* 5)))
)
(switch (fact-slot-value ?e risposta)
(case 1-2 then (bind ?*punteggio* (+ ?*punteggio* 1)))
(case 3-5 then (bind ?*punteggio* (+ ?*punteggio* 2)))
(case 5-7 then (bind ?*punteggio* (+ ?*punteggio* 3)))
)
(bind ?f ?*punteggio*)
(if (> ?f 9) then (assert (livello-utente (livello esperto))))
(if (< ?f 6) then (assert (livello-utente (livello principiante))))
(if (and (> ?f 5) (< ?f 10)) then (assert (livello-utente(livello medio))))
)
After that i go in the second module where one of this two rule ia actived in according with the profile of the user determinate in the fist module
(defrule DIAGNOSI::domanda-esperto
(declare (salience ?*highest-priority*))
(livello-utente (livello esperto))
=>
(bind ?risposta (risposte-range "In quale tra le seguenti aree e' presente il problema?" Olio-motore Olio-freni Acqua Carburante Altro))
(assert (domanda (nome area-problema) (domanda "In quale tra le seguenti aree e' presente il problema?") (risposta ?risposta)))
)
(defrule DIAGNOSI::domanda-medio
(declare (salience ?*highest-priority*))
(livello-utente (livello medio))
=>
(bind ?risposta (si-o-no "Sapresti indicare l'area di provenienza del problema tra le seguenti: Olio motore, Olio freni, Acqua, Carburante, Altro?"))
(assert (domanda (nome domanda-area-problema) (domanda "Sapresti indicare l'area di provenienza del problema tra le seguenti: Olio motore, Olio freni, Acqua, Carburante, Altro?") (risposta ?risposta)))
(if (eq ?risposta TRUE)
then (bind ?risposta (risposte-range "In quale tra le seguenti aree e' presente il problema?" Olio-motore Olio-freni Acqua Carburante Altro))
(assert (domanda (nome area-problema) (domanda "In quale tra le seguenti aree e' presente il problema?") (risposta ?risposta)))
)
After this there is a series of other question that i do to user. After that i ask if he want to retract one of this, and if he chooseone of the two (domanda-medio, domanda-esperto) i have to retract all the answers of the second module. After i retract all the answers of the second module, this two rule is never activeted and it not apper in the AGENDA also if the facts that match the LHS is present in the fact list (livello-utente (livello ?))
#GaryRiley ok i don't know why but adding `
(not (diagnosi (nome ?)))`
to the two rule it work.
I'm trying to do a comparation in a rule on CLIPS thah check if one of three conditions it's true to assert a new fact. The code is:
(defrule empresa_cae_mucho
(Empresa (nombre ?n)(var_anio ?anio)(var_sem ?sem)(var_tri ?tri))
=>
(or (or (test(> ?anio 30))(test (> ?sem 30))(test (> ?tri 30))))
(assert valor_infravalorado
(nombre ?n))
(assert (Explicacion
(nombre ?n)
(motivo "la empresa ha caido bastante aunque no en el ultimo mes
pero su PER es bajo")))
)
But it doesn't work and I can't find the right form of do this in internet. Any help?
CLIPS> (clear)
CLIPS>
(deftemplate Empresa
(slot nombre)
(slot var_anio)
(slot var_sem)
(slot var_tri))
CLIPS>
(deftemplate valor_infravalorado
(slot nombre))
CLIPS>
(deftemplate Explicacion
(slot nombre)
(slot motivo))
CLIPS>
(deffacts start
(Empresa (nombre 1) (var_anio 40) (var_sem 10) (var_tri 25))
(Empresa (nombre 2) (var_anio 0) (var_sem 35) (var_tri 10))
(Empresa (nombre 3) (var_anio 30) (var_sem 20) (var_tri 55))
(Empresa (nombre 4) (var_anio 30) (var_sem 30) (var_tri 30)))
CLIPS>
(defrule empresa_cae_mucho
(Empresa (nombre ?n)
(var_anio ?anio)
(var_sem ?sem)
(var_tri ?tri))
(test (or (> ?anio 30)
(> ?sem 30)
(> ?tri 30)))
=>
(assert (valor_infravalorado (nombre ?n)))
(assert (Explicacion
(nombre ?n)
(motivo "la empresa ..."))))
CLIPS> (reset)
CLIPS> (watch rules)
CLIPS> (watch facts)
CLIPS> (run)
FIRE 1 empresa_cae_mucho: f-3
==> f-5 (valor_infravalorado (nombre 3))
==> f-6 (Explicacion (nombre 3) (motivo "la empresa ..."))
FIRE 2 empresa_cae_mucho: f-2
==> f-7 (valor_infravalorado (nombre 2))
==> f-8 (Explicacion (nombre 2) (motivo "la empresa ..."))
FIRE 3 empresa_cae_mucho: f-1
==> f-9 (valor_infravalorado (nombre 1))
==> f-10 (Explicacion (nombre 1) (motivo "la empresa ..."))
CLIPS> (facts)
f-0 (initial-fact)
f-1 (Empresa (nombre 1) (var_anio 40) (var_sem 10) (var_tri 25))
f-2 (Empresa (nombre 2) (var_anio 0) (var_sem 35) (var_tri 10))
f-3 (Empresa (nombre 3) (var_anio 30) (var_sem 20) (var_tri 55))
f-4 (Empresa (nombre 4) (var_anio 30) (var_sem 30) (var_tri 30))
f-5 (valor_infravalorado (nombre 3))
f-6 (Explicacion (nombre 3) (motivo "la empresa ..."))
f-7 (valor_infravalorado (nombre 2))
f-8 (Explicacion (nombre 2) (motivo "la empresa ..."))
f-9 (valor_infravalorado (nombre 1))
f-10 (Explicacion (nombre 1) (motivo "la empresa ..."))
For a total of 11 facts.
CLIPS>
excuse my English, I'm Spanish.
I have a problem with my expert system computer configurations made in CLIPS.
My rule "preciogamingoc" does not run, I tried a thousand ways but do not work.
I leave the code here.
I dont know how to put it better, sorry for that.
;;****TEMPLATES*****
(deftemplate pc
(slot tipo)
(slot oc)
(multislot procesador)
(multislot ram)
(multislot placabase)
(multislot discoduro)
(multislot grafica)
(multislot fuente)
(slot precio))
;;****FUNCTIONS*****
(deffunction pregunta (?pregunta $?respuestas-posibles)
(printout t ?pregunta)
(bind ?respuesta (read))
(if (lexemep ?respuesta)
then (bind ?respuesta (lowcase ?respuesta)))
(while (not (member ?respuesta ?respuestas-posibles)) do
(printout t ?pregunta)
(bind ?respuesta (read))
(if (lexemep ?respuesta)
then (bind ?respuesta (lowcase ?respuesta))))
?respuesta)
(deffunction sino (?pregunta)
(bind ?respuesta (pregunta ?pregunta si no s n))
(if (or (eq ?respuesta si) (eq ?respuesta s))
then TRUE
else FALSE))
(deffunction tipo (?pregunta)
(bind ?respuesta (pregunta ?pregunta g d o))
(switch ?respuesta
(case g then (return g))
(case d then (return d))
(case o then (return o))
))
;;******FACTS*******
(deffacts inicia
(pc))
;;******RULES*******
(defrule tipopc
?A <- (pc (procesador)(ram)(placabase)(discoduro)(grafica)(fuente))
=>
(switch (tipo "Tipo (juegos:g/diseño:d/otros:o)? ")
(case g then ( modify ?A (tipo g)(oc nose)(procesador "i5 4690_Intel")
(ram "2x4GB 2133 cl9")
(placabase "Gigabyte_H97M-HD3 ejemplo")
(discoduro "1Tb")
(grafica "GTX 960")
(fuente "700W+ silver")
(precio 550)))
(case d then ( modify ?A (tipo d)(oc nose)(procesador "i7 4790_Intel")
(ram "2x8GB 2133 cl9")
(placabase "Gigabyte_H97M-HD3 ejemplo")
(discoduro "1Tb HDD + 120gb SSD")
(grafica "kuadro k620")
(fuente "700W+ silver")
(precio 700)))
(case o then ( modify ?A (tipo o)(oc nose)(procesador "i3 6100_Intel")
(ram "2x4GB 2400 cl12 DDR4")
(placabase "Msi H110M PRO-VH")
(discoduro "1Tb")
(grafica "no incluida")
(fuente "450W+ bronze")
(precio 300)))))
(defrule oc
?A <- (pc(tipo g)(oc nose))
=>
(if (sino "Hará overclock al procesador (si/no)?: ")
then ( modify ?A (oc si)))
)
(defrule preciogamingoc
?A <- (pc (tipo g) (oc si) (procesador) (ram) (placabase) (discoduro) (grafica) (fuente))
=>
(printout t "Veo que su sistema será destinado a juegos y que además se le podrá hacer overclock." crlf
"Ahora debe introducir el presupuesto medio para que podamos darle una configuración mas concreta." crlf
"Introduzca su presupuesto de referencia(>= 750): ")
(bind ?precio (read))
(if (and(>= ?precio 750)(< ?precio 850))
;;then ((printout t "bien"crlf))
then (modify ?A (procesador "i5 4690K_Intel")
(ram "2x8GB 2133 cl9")
(placabase "Gigabyte_Z97X gaming 3 ejemplo")
(discoduro "1Tb")
(grafica "GTX 970")
(fuente "700W+ silver")
(precio 750)))
;;(else (if (>= ?precio 850)
;;then (modify ?C (procesador "i7 4790K_Intel")
;;(ram "2x8GB 2133 cl9")
;;(placabase "Gigabyte_Z97X gaming 5 ejemplo")
;;(discoduro "1Tb HDD")
;;(grafica "GTX 970")
;;(fuente "700W+ silver")
;(precio 850))))
)
;;*******MAIN*******
(defrule inicio
(declare (salience 1))
=>
(printout t crlf)
(printout t "Sistema Experto de Configuracion de Ordenadores")
(printout t crlf crlf))
(defrule presupuesto
(declare (salience -1))
(pc (procesador ?A)(ram ?F)(placabase ?B)(discoduro ?M)(grafica ?G)(fuente ?P)(precio ?O))
=>
(printout t crlf crlf "-.PRESUPUESTO.-" crlf crlf)
(printout t "PROCESADOR: " ?A crlf)
(printout t "RAM: " ?F crlf)
(printout t "PLACA BASE: " ?B crlf)
(printout t "DISCO DURO:" ?M crlf)
(printout t "GRAFICA: " ?G crlf)
(printout t "FUENTE: " ?P crlf crlf)
(printout t "PRECIO: " ?O crlf)
)
The preciogamingoc rule matches a pc fact, so you can run the program, check to see which rules execute, and examine the pc fact to see which values have been assigned to it:
CLIPS> (reset)
CLIPS> (watch rules)
CLIPS> (run)
FIRE 1 inicio: *
Sistema Experto de Configuracion de Ordenadores
FIRE 2 tipopc: f-1
Tipo (juegos:g/diseño:d/otros:o)? g
FIRE 3 oc: f-2
Hará overclock al procesador (si/no)?: si
FIRE 4 presupuesto: f-3
-.PRESUPUESTO.-
PROCESADOR: i5 4690_Intel
RAM: 2x4GB 2133 cl9
PLACA BASE: Gigabyte_H97M-HD3 ejemplo
DISCO DURO:1Tb
GRAFICA: GTX 960
FUENTE: 700W+ silver
PRECIO: 550
CLIPS> (facts)
f-0 (initial-fact)
f-3 (pc (tipo g) (oc si) (procesador "i5 4690_Intel") (ram "2x4GB 2133 cl9") (placabase "Gigabyte_H97M-HD3 ejemplo") (discoduro "1Tb") (grafica "GTX 960") (fuente "700W+ silver") (precio 550))
For a total of 2 facts.
CLIPS> (ppfact 3)
(pc
(tipo g)
(oc si)
(procesador "i5 4690_Intel")
(ram "2x4GB 2133 cl9")
(placabase "Gigabyte_H97M-HD3 ejemplo")
(discoduro "1Tb")
(grafica "GTX 960")
(fuente "700W+ silver")
(precio 550))
CLIPS>
The pattern from the preciogamingoc rule is:
?A <- (pc (tipo g)
(oc si)
(procesador)
(ram)
(placabase)
(discoduro)
(grafica)
(fuente))
The fact values for the tipo and oc slots match the pattern, but the values for the processor, ram, placabase, discoduro, grafica, and fuente slots do not. These slots can have zero or more values. The fact has a single value for each, but your pattern specifies that each of these slots be empty so the pattern is not matched.
You need to either remove these slots from the pattern or check for specific values. You also need to prevent the rule from executing again once the pc fact has been modified in the actions of the rule (perhaps by changing the value of oc to si-actualizado to indicate the rule has been applied):
(defrule preciogamingoc
?A <- (pc (tipo g) (oc si))
=>
(printout t "Veo que su sistema será destinado a juegos y que además se le podrá hacer overclock." crlf
"Ahora debe introducir el presupuesto medio para que podamos darle una configuración mas concreta." crlf
"Introduzca su presupuesto de referencia(>= 750): ")
(bind ?precio (read))
(if (and (>= ?precio 750)(< ?precio 850))
then
(modify ?A (oc si-actualizado)
(procesador "i5 4690K_Intel")
(ram "2x8GB 2133 cl9")
(placabase "Gigabyte_Z97X gaming 3 ejemplo")
(discoduro "1Tb")
(grafica "GTX 970")
(fuente "700W+ silver")
(precio 750))))
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)
)
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>