I am trying to make a program that asks is the claimant homeless, unemployed has no having etc and get a either level 1 or 2 benefit support as the answer but I can't load this in Clips. I get the error below
code
(defrule Claimant
(Claimant-is homeless)
(Claimant-is unemployed)
(Claimant-is nosavings)
(Claimant-is dependants)
(Claimant-is disabled))
(deftemplate Benefit
(slot benefit))
(defrule Level1
Claimant(homesless yes) (unemployed yes) (nosavings no) (dependants yes) (disabled yes))
=>
(assert (Benefit (benefit level1)))
(printout t "You get level 1 benefit support" crlf))
(defrule Level2
Claimant(homesless yes) (unemployed yes) (nosavings no) (dependants no) (disabled no))
=>
(assert (Benefit (benefit level2)))
(printout t "You get level 2 benefit support" crlf))
error
Defining defrule: Claimant
[PRNTUTIL2] Syntax Error: Check appropriate syntax for defrule.
ERROR:
(defrule MAIN::Claimant
(Claimant-is homeless)
(Claimant-is unemployed)
(Claimant-is nosavings)
(Claimant-is dependants)
(Claimant-is disabled)
)
Defining deftemplate: Benefit
Defining defrule: Level1
[PRNTUTIL2] Syntax Error: Check appropriate syntax for defrule.
ERROR:
(defrule MAIN::Level1
Claimant
Defining defrule: Level2
[PRNTUTIL2] Syntax Error: Check appropriate syntax for defrule.
ERROR:
(defrule MAIN::Level2
Claimant
FALSE
CLIPS>
Your syntax has several problems that must be corrected.
The design could also be simplified (but I will not talk about that aspect).
The following shows an equivalent version with corrections.
homeless.clp
(deftemplate Claimant
(slot homeless (type SYMBOL) (allowed-values yes no))
(slot unemployed (type SYMBOL) (allowed-values yes no))
(slot nosavings (type SYMBOL) (allowed-values yes no))
(slot dependants (type SYMBOL) (allowed-values yes no))
(slot disabled (type SYMBOL) (allowed-values yes no))
)
(deftemplate Benefit
(slot benefit (type SYMBOL) (allowed-values level1 level2))
)
(defrule Level1
(Claimant (homeless yes) (unemployed yes) (nosavings no)
(dependants yes) (disabled yes))
=>
(assert (Benefit (benefit level1)))
(printout t "You get level 1 benefit support" crlf)
)
(defrule Level2
(Claimant (homeless yes) (unemployed yes) (nosavings no)
(dependants no) (disabled no))
=>
(assert (Benefit (benefit level2)))
(printout t "You get level 2 benefit support" crlf)
)
Now you can test with
(clear)
(load "homeless.clp")
(assert (Claimant (homeless yes) (unemployed yes) (nosavings no) (dependants no) (disabled no)))
(run)
and get
You get level 2 benefit support
Related
this is a clips expert system code to make a diagnosis for a disease so i am trying to find out whats the problem in that code and i really dont know what to do
[CSTRCPSR1]
[CSTRCPSR2]
error
defrule diagnosis
(symptom ?s)
(disease (symptoms ?s) (name ?d))
=>
(printout t "The patient may have " ?d "." crlf))
(defrule ask-symptoms
(not (symptom ?))
=>
(bind ?symptom (read))
(assert (symptom ?symptom)))
(defrule ask-symptoms
(not (symptom ?))
=>
(printout t "What are the patient's symptoms?" crlf)
(bind ?symptom (read))
(assert (symptom ?symptom)))
(defrule ask-symptoms-2
(symptom ?s)
(not (symptom ?))
=>
(printout t "Any other symptom? " crlf)
(bind ?symptom (read))
(assert (symptom ?symptom)))
(defclass disease
(is-a USER)
(role concrete)
(multislot symptoms)
(slot name))
(definstances diseases
(disease (symptoms fever headache) (name "influenza"))
(disease (symptoms fever sorethroat) (name "strep throat"))
(disease (symptoms cough shortness-of-breath) (name "pneumonia"))
(disease (symptoms stomachache nausea) (name "food poisoning")))
i tried everything to fix it but i really dont know where is the problem
The diagnosis defrule is missing an opening parenthesis.
The diagnosis defrule references the disease defclass before the
class is defined.
Classes automatically have a name slot defined.
You have two ask-symptoms rules.
The conditions of the ask-symptoms-2 rule will never be satisfied.
Any fact matching the first pattern will cause the following pattern
to fail.
If you want to read multiple symptoms, use the readline function
along with the explode$ function.
In the diagnosis rule, you need to use the object keyword to match
an instance of a defclass.
In the diagnosis rule, the disease pattern can only be matched by
diseases with exactly one symptom.
For example:
CLIPS (6.4 2/9/21)
CLIPS>
(defclass disease
(is-a USER)
(role concrete)
(multislot symptoms)
(slot text))
CLIPS>
(defrule diagnosis
(object (is-a disease) (name ?name) (text ?d))
(exists
(symptoms $? ?s $?)
(object (is-a disease) (name ?name) (symptoms $? ?s $?)))
=>
(printout t "The patient may have " ?d "." crlf))
CLIPS>
(defrule ask-symptoms
=>
(printout t "What are the patient's symptoms? ")
(bind ?symptoms (readline))
(assert (symptoms (explode$ ?symptoms))))
CLIPS>
(definstances diseases
(influenza of disease (symptoms fever headache) (text "influenza"))
(strep-throat of disease (symptoms fever sorethroat) (text "strep throat"))
(pneumonia of disease (symptoms cough shortness-of-breath) (text "pneumonia"))
(food-poisoning of disease (symptoms stomachache nausea) (text "food poisoning")))
CLIPS> (reset)
CLIPS> (run)
What are the patient's symptoms? cough nausea
The patient may have pneumonia.
The patient may have food poisoning.
CLIPS>
So here's my CLIPS code
CLIPS> (deftemplate animal
(slot name)
(slot favourite-food)
(slot habitat)
(slot main-prey)
(multislot predators)
(slot distinctive-features))
CLIPS> (deffacts animal
(animal (name Sumatran-Elephant)
(favourite-food grass)
(habitat "Rainforest and tropical woodland")
(main-prey "grass, fruit, roots")
(predators Human Tiger)
(distinctive-features "Long trunk and large feet"))
(animal (name Monkey)
(favourite-food fruit)
(habitat "Tropical forests, grasslands and mountainous plains")
(main-prey "Fruit, Seeds, Insects")
(predators Birds Snakes Wildcats)
(distinctive-features "Long, agile tail and loud vocal calls"))
(animal (name Magpie)
(favourite-food fruit)
(habitat "Open woodland, grasslands and savannas")
(main-prey "Fruit, Nuts, Seeds, Insects")
(predators Foxes Cats Coyote)
(distinctive-features "Black and white markings and long wedge-shaped tail")))
CLIPS> (deftemplate find-predators(slot predators))
CLIPS> (defrule find-predators
(find-predators(predators ?predator_name))
(animal(name ?name)
(predators $?other1 ?predator_name $?other2))
=> (printout t ?predator_name " is the predator of " ?name crlf
"Other predators are " ?other1 ?other2 crlf))
CLIPS> (reset)
CLIPS> (assert(find-predators(predators Human)))
<Fact-4>
CLIPS> (run)
Human is the predator of Sumatran-Elephant
Other predators are ()(Tiger)
CLIPS> (assert(find-predators(predators Coyote)))
<Fact-5>
CLIPS> (run)
Coyote is the predator of Magpie
Other predators are (Foxes Cats)()
But the answer should be like this
Coyote is the predator of Magpie
Other predators are (Foxes) (Cats)
How do I split the facts for the multi slots above?
Need helps if anyone know the best default that can be replaced from the above code
Combine the multifield values in the variables $?other and $?other2 into a single multifield value and then use the implode$ function to convert that value into a string with spaces between the predator names.
(defrule find-predators
(find-predators (predators ?predator_name))
(animal (name ?name)
(predators $?other1 ?predator_name $?other2))
=>
(printout t ?predator_name " is the predator of " ?name crlf
"Other predators are " (implode$ (create$ ?other1 ?other2)) crlf))
I have an expert system to figure out the corresponding animal. I have inserted all of the facts, however when I try to (run) the Expert System no question is asked, when the first question should be displayed as 'Is the animal big?' Does anyone have any idea please.
Many thanks in advance
(deffacts startup (animal mouse) (animal squirrel) (animal hamster) (animal giraffe) (animal elephant) (animal hippo)
(animal rhino) (animal big) (neck-long giraffe) (nose-long elephant) (swim-alot hippo) (animal-squeak mouse) (tail-bushy squirrel))
=>
(defrule is-animal-big
(not (animal-big ?))
=> (printout t "Is the animal big (yes or no)?")
(assert (animal-big (read))))
(defrule yes-big-check-neck
(animal-big yes)
(not (neck-long ?))
=>
(printout t "Is the animals neck long (yes or no)?")
(assert (neck-long (read))
)
(defrule neck-long-yes
(neck-long yes)
=>
(printout t "your animal is a giraffe" crlf)
(bind ?animal giraffe)
(assert (animal-is ? animal))
)
;;;========================================
(defrule neck-long-no-check-nose
(neck-long no)
(not (nose-long ?))
=>
(printout t "Is the animals nose long (yes or no)?")
(assert (nose-long (read))
)
(defrule nose-long-yes
(nose-long yes)
=>
(printout t "your animal is an elephant" crlf)
(bind ?animal elephant)
(assert (animal-is ? animal))
)
;;;=======================================
(defrule nose-long-no-check-swim
(nose-long no)
(not (swim-alot ?))
=>
(printout t "Does the animal swim a lot (yes or no)?")
(assert swim-alot (read((
)
(defrule swim-alot-yes
(swim-alot yes)
=>
(printout t "your animal is a hippo" crlf)
(bind ?animal hippo)
(assert (animal-is ? animal))
)
;;;=====================================
(defrule swim-alot-no
(swim-alot no)
=>
(printout t "your animal is a rhino" crlf)
(bind ?animal rhino)
(assert (animal-is ? animal))
)
;;;======================================
(defrule no-big-check-squeak
(animal-big no)
(not (animal-squeak ?))
=>
(printout t "Does the animal squeak (yes or no)?")
(assert animal-squeak (read))
)
(defrule animal-squeak-yes
(animal-squeak yes)
=>
(printout t "your animal is a mouse" crlf)
(bind ?animal mouse)
(assert (animal-is ? animal))
)
;;;=======================================
(defrule squeak-no-check-tail
(animal-squeak no)
(not (tail-bushy ?))
=>
(printout t "Is the animals tail bushy (yes or no)?")
(assert tail-bushy (read))
)
(defrule tail-bushy-yes
(tail-bushy yes)
=>
(printout t "your animal is a squirrel" crlf)
(bind ?animal squirrel)
(assert (animal-is ? animal))
)
;;;=======================================
(defrule tail-bushy-no
(tail-bushy no)
=>
(printout t "your animal is a hamster" crlf)
(bind ?animal hamster)
(assert (animal is ? animal)))
;;;==============================
;;;******************************
Your code is riddled with syntax errors, but the startup deffacts and is-animal-big defrule compile without errors. You're probably not issuing a (reset) command to assert the facts within your deffacts before issuing a (run) command.
Here is my code
(deffacts startup
(bird canary)
(colour-canary yellow)
(bird ostrich)
(can-not-fly ostrich)
)
(defrule r-bird-test
(bird ?var)
(not (bird ostrich))
=>
(printout t ?var " ****" crlf)
)
Now, when i (reset) and (run) it doesn't print "canary ****". Am i not using the not condition properly? Can anyone point out what i am missing here? Thanks.
As written the not conditional element prevents the rule from executing if the fact (bird ostrich) is present. Since that fact is present once you perform a (reset), the rule does not execute. If you want the rule to execute for each bird fact where ?var is not ostrich, you need to write the rule this way:
CLIPS>
(deffacts startup
(bird canary)
(colour-canary yellow)
(bird ostrich)
(can-not-fly ostrich))
CLIPS>
(defrule r-bird-test
(bird ?var&~ostrich)
=>
(printout t ?var " ****" crlf))
CLIPS> (reset)
CLIPS> (run)
canary ****
CLIPS>
I am new to CLIPS and I thought of looking over existing solved problems for a start and try to figure it backwards. There is the following problem found in Giarratano-Riley: Expert Systems and Programming 3rd edition and it states the following:
Fires are classified according to the principal burning material.
Translate the following information to rules for determining fire
class.
Type A fires involve ordinary combustibles such as paper, wood, and cloth.
Type B fires involve flammable and combustible liquids (such as
oil and gas), greases, and similar materials.
Type C fires involve energized electrical equipment.
Type D fires involve combustible metals such as magnesium, sodium, and potassium.
The type of extinguisher that should be used on a fire depends on the fire class.
Translate the following information to rules.
Class A fires should be extinguished with heat-absorbing or
combustion-retarding extinguishers such as water or water-based
liquids and dry chemicals.
Class B fires should be extinguisher by mexcluding air, inhibiting the release of
combustible vapors, or interrupting the combustion chain reaction. Extinguishers
include dry chemicals, carbon dioxide, foam, and bromotrifluoromethane.
Class C fires should be extinguished with a non conducting agent to prevent
short circuits. If possible the power should be cut. Extinguishers
include dry chemicals, carbon dioxide, and bromotrifluoromethane.
Class D fires should be extinguished with smothering and heat- absorbing chemicals > that do not react with the burning metals. Such chemicals include trimethoxyboroxine
and screened graphitized coke.
Describe the facts used in the rules. The input to the program should be made by asserting the type of burning material as a fact. The output should indicate which extinguishers may be used and other actions that should be taken, such as cutting off the power. Show that your program works for one material form each of the fire classes.
And then it is solved by Berkely and the code is the following. My question is, how do I call these rules and make the program work? I loaded the buffer, reset, run and it only loads the rules into the command-line.
; Define templates used in rules
(deftemplate fire (slot burning-material))
(deftemplate extinguisher-system (multislot function) (multislot extinguisher))
(deftemplate response (multislot actions))
(deftemplate fire-class (slot class))
; Define rules for determining fire classes
(defrule class-A-fire
(fire (burning-material paper | wood | cloth | other-ordinary-combustibles)) =>
(assert (fire-class (class A))))
(defrule class-B-fire
(fire (burning-material oil | gas | greases | other-flammable-combustible-liquids)) =>
(assert (fire-class (class B))))
(defrule class-C-fire
(fire (burning-material energized-electrical-equipment)) =>
(assert (fire-class (class C))))
(defrule class-D-fire
(fire (burning-material magnesium | sodium | potassium | other-combustible-metals)) =>
(assert (fire-class (class D))))
; Define rules for determining the type of extinguisher that should be used on a fire
(defrule class-A-emergency
(fire-class (class A))
=>
(assert (response (actions activate-extinguisher-A)))
(assert (extinguisher-system (function heat-absorbing combustion-retarding) (extinguisher water water-based-liquids dry-chemicals))))
(defrule class-B-emergency
(fire-class (class B))
=>
(assert (response (actions activate-extinguisher-B)))
(assert (extinguisher-system (function excluding-air inhibiting-release-of-combustible-vapors interrupting-combustion-chain-reaction) (extinguisher dry-chemicals carbon-dioxide foam bromotrifluoromethane))))
(defrule class-C-emergency
(fire-class (class C))
=>
(assert (response (actions activate-extinguisher-C power-cut)))
(assert (extinguisher-system (function nonconducting-agent) (extinguisher dry-chemicals carbon-dioxide bromotrifluoromethoane))))
(defrule class-D-emergency
(fire-class (class D))
=>
(assert (response (actions activate-extinguisher-D)))
(assert (extinguisher-system (function smothering-heatabsorbing-chemicals) (extinguisher trimethoxyboroxine screened-graphitized-coke))))
I guess you made it until here:
CLIPS> Loading Selection...
Defining deftemplate: fire
Defining deftemplate: extinguisher-system
Defining deftemplate: response
Defining deftemplate: fire-class
Defining defrule: class-A-fire +j+j
Defining defrule: class-B-fire +j+j
Defining defrule: class-C-fire +j+j
Defining defrule: class-D-fire +j+j
Defining defrule: class-A-emergency +j+j
Defining defrule: class-B-emergency +j+j
Defining defrule: class-C-emergency +j+j
Defining defrule: class-D-emergency +j+j
CLIPS> (reset)
Now you need to load the problem data. For example, for a wood fire:
CLIPS> (assert (fire (burning-material wood)))
<Fact-1>
CLIPS> (facts)
f-0 (initial-fact)
f-1 (fire (burning-material wood))
And the, run the rules engine
CLIPS> (run)
CLIPS> (facts)
f-0 (initial-fact)
f-1 (fire (burning-material wood))
f-2 (fire-class (class A))
f-3 (response (actions activate-extinguisher-A))
f-4 (extinguisher-system (function heat-absorbing combustion-retarding) (extinguisher water water-based-liquids dry-chemicals))
And clean it to check the next problem
CLIPS> (reset)
CLIPS> (assert (fire (burning-material gas)))
<Fact-1>
CLIPS> (run)
...