I found function CLIPS Validate Text Entry there and I want to extend it this way:
(deffunction ask-question (?mark ?question $?allowed-values)
(printout t ?question)
(bind ?answer (read))
(if (lexemep ?answer)
then (bind ?answer (lowcase ?answer))
**(assert car-mark(name ?mark))**
) (while (not (member ?answer ?allowed-values)) do
(printout t ?question)
(bind ?answer (read))
(if (lexemep ?answer)
then (bind ?answer (lowcase ?answer))
**(assert car-mark(name ?mark))**
)
) ?answer)
So, I plan to add new car-mark in my facts if user input yes/y - else - not to add facts. But clips gives the error:
[PRNTUTIL2] Syntax Error: Check appropriate syntax for RHS patterns.
ERROR:
(deffunction MAIN::ask-question
(?mark ?question $?allowed-values)
(printout t ?question)
(bind ?answer (read))
(if (lexemep ?answer)
then
(bind ?answer (lowcase ?answer))
(assert car-mark
There is my template for car-mark:
(deftemplate car-mark
(slot name)
)
How to correctly add new fact and consider user input (yes - add, no - no add)?
(assert (car-mark (name "foo")))
Related
(deftemplate startup-rule
(multislot output)
(slot state))
(defrule end-state
=>
(open "output.dat" theFile "r")
(bind ?data (readline theFile))
(while (neq ?data EOF)
(bind ?data (?data readline theFile)))
(assert (startup-rule (output ?data)(state final)))
(close theFile) )
In this defrule I am trying to bind all the lines being read from the file into the ?data variable to be asserted into the deftemplate startup-rule afterwards, however that does not seem to be working.
(defrule end-state
=>
(open "output.dat" theFile "r")
(bind ?str "")
(bind ?data (readline theFile))
(while (neq ?data EOF)
(bind ?str (str-cat ?str ?data))
(bind ?data (readline theFile)))
(assert (startup-rule (output ?str) (state final)))
(close theFile))
I need to write a method of a function that does the following:
Divides the text into words;
Prints words that are different from the first word;
And before that converts each word according to the following rule:
If the word is odd, then removes its middle letter.
The result is displayed on the screen and in a text file.
Here's a function that will give you the list of different words:
CLIPS>
(deffunction munge (?text)
(bind ?w1 (explode$ ?text))
(bind ?w2 (create$))
(progn$ (?w ?w1)
(bind ?len (str-length ?w))
(if (oddp ?len)
then
(bind ?nw (str-cat (sub-string 1 (div ?len 2) ?w)
(sub-string (+ (div ?len 2) 2) ?len ?w)))
(bind ?w2 (create$ ?w2 ?nw))
else
(bind ?w2 (create$ ?w2 (str-cat ?w)))))
(bind ?first (nth$ 1 ?w2))
(bind ?rest (rest$ ?w2))
(bind ?w3 (create$))
(progn$ (?w ?w2)
(if (neq ?w ?first)
then
(bind ?w3 (create$ ?w3 ?w))))
?w3)
CLIPS> (munge "red green blue purple brown green white red black blue")
("gren" "blue" "purple" "brwn" "gren" "whte" "blck" "blue")
CLIPS>
I suggest you to start with some basic documentation.
An example:
http://www2.cs.siu.edu/~rahimi/cs537/slides/big-2.pdf
You should look at multi-field built-in functions.
In my expert system user must check Developer and Price after he'll see notebook which is suitable for this parameters.
For example with this parameters (on screenshot) I must have result: Model: Noteebok1
But I don't see anything. Where is a problem or bug?
CLP File Code:
(defglobal ?*s* = 0)
(deftemplate Notebook
(slot pModel)
(slot pDeveloper)
(slot pPrice))
;*******************************************************************
(deffunction QuestionOf(?TextQuestion $?variations)
(printout t ?TextQuestion)
(bind ?Answer (read))
(if (lexemep ?Answer)
then (bind ?Answer (lowcase ?Answer)))
(while (not (member ?Answer ?variations)) do
(printout t ?TextQuestion)
(bind ?Answer (read))
(if (lexemep ?Answer)
then (bind ?Answer (lowcase ?Answer))))
?Answer)
;********************************************************************'
(defrule banner
(declare (salience 10))
=>(load-facts D:\fact.txt)
(printout t crlf crlf)
(printout t "Expert system. Nout search")
(printout t crlf crlf))
;******************************************************************'
(defrule QuestionDeveloper
(not (Developer ?))
=>(bind ?asssert(QuestionOf "Check Developer (a-HP,b-Samsung,c-Apple,d-IDontKnow)" a b c d))
(if (eq ?asssert a)then (assert (Developer HP)))
(if (eq ?asssert b)then (assert (Developer Samsung)))
(if (eq ?asssert c)then (assert (Developer Apple)))
(if (eq ?asssert d)then (assert (Developer IDontKnow))))
;******************************************************************'
(defrule QuestionPrice
(not (Price ?))
=>(bind ?asssert(QuestionOf "Price?(a-300,b-400,c-500,d-IDontKnow)" a b c d))
(if (eq ?asssert a)then (assert (Price 300)))
(if (eq ?asssert b)then (assert (Price 400)))
(if (eq ?asssert c)then (assert (Price 500)))
(if (eq ?asssert d)then (assert (Price IDontKnow))))
;******************************************************************'
(defrule Vyvod
(or (Developer ?xDeveloper)(Developer IDontKnow))
(or (Price ?xPrice)(Price IDontKnow))
(Notebook(pModel ?Model)(pDeveloper ?xDeveloper)(pPrice ?xPrice))
=>(bind ?*s*(+ ?*s* 1))
(printout t crlf " " ?*s* ". Model : " ?Model crlf))
;******************************************************************'
Fact.txt:
(Notebook(pModel Notebook1)(pDeveloper HP)(pPrice 500))
(Notebook(pModel Notebook2)(pDeveloper Samsung)(pPrice 400))
(Notebook(pModel Notebook3)(pDeveloper Apple)(pPrice 500))
The facts from the file fact.txt are not being loaded. Check that the file is at the specified path and that you have read access.
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>
Morning, Excuse the silly question but I am busy building a expert system much like the "21 Questions" game that uses questions asked to the user in order to determine the right dog for them. The expert system is coded in CLIPS / .CPS language and one of the requirements I am looking to include is that when the user is asked a yes/no question they are required to input "y" or "n".
In all the resources we have been taught we have only been tough number validation and not a specific character validation and I cannot find any resources that do this either.
This is an example of the number validation I did in order to ensure they input a valid number on one of my questions
(defrule test-integer
(number-in ?number&:(integerp ?number))
=>
(printout t ?number "is valid"
(defrule test-non-int
?number-address <- (number-in ?number&:(not (integerp ?number)))
=>
(printout t ?number " not valid int" crlf)
(retract ?number-address))
This is how you'd do it using rules:
CLIPS>
(defrule test-response
(response-in ?response&y|n)
=>
(printout t ?response " is valid" crlf))
CLIPS>
(defrule test-non-response
?response-address <- (response-in ?response&~y&~n)
=>
(printout t ?response " not valid response" crlf)
(retract ?response-address))
CLIPS> (assert (response-in xyz))
<Fact-1>
CLIPS> (run)
xyz not valid response
CLIPS> (assert (response-in n))
<Fact-2>
CLIPS> (run)
n is valid
CLIPS>
I'd suggest using a function that only accepts correct responses:
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> (ask-question "Continue? " y n yes no)
Continue? k
Continue? l
Continue? ye
Continue? YeS
yes
CLIPS>
What i figured out was to link the answer from the one defrule to that of another defrule first to check if the answer was valid and then again if that answer was valid to link it to the correct defrule then that will proceed with the next question.
Code is from my own Expert System:
(defrule Small-CoatType-Full
(Small-Coat f)
(person (name ?name))
=>
(open "result.txt" result "a")
(printout result ?name " Likes Smaller, Fury Dogs" crlf)
(close result)
(printout t "Would you like a low energetic(l) or high energetic(h) breed?" crlf)
(assert (Small-Energy-Level(lowcase(read)))))
(defrule Small-Energy-Level-Wrong
(Small-Energy-Level ?var &~l&~h)
=>
(printout t crlf "Plesae Only Choose (l) or (h)")
(assert (Small-Energy-Level (lowcase(read)))))`