Am writing a project, my code will be in the form as describe below in the code, Is there any need to include salience and how can I use interface? The software will require the user to answer a question in the form of yes or no. I want to know how to do this in the form of checkboxes, where the user will tick the appropriate symptoms of a disease, then the software will be able to predict the kind of disease. thank you . please help
CLIPS> (defrule bacterial-fruit-blotch-of-watermelon
=>
(print "Is the plant showing a symptom of Oily and water-soaked cotyledons? ")
(assert (OWS (read)))
(print "Is the plant showing a symptom of Yellow halo paralleling veins? ")
(assert (YHP (read)))
(print "Is the plant showing a symptom of Small dark and angled lesion on leaves? ")
(assert (SDA (read)))
(print "Is the plant showing a symptom of Dark green blotch on the upper surface of developing fruit? ")
(assert (DGB (read))))
CLIPS> (defrule check
(OWS yes)
(YHP yes)
(SDA yes)
(DGB yes)
=>
(println "The plant is showing a symptom of bacterial fruit blotch of watermelon disease."))
CLIPS> (defrule bacterial-rind-necrosis
=>
(print " Is the plant showing a symptom of corky, dry necrosis? ")
(assert (corky (read)))
(print " Is the plant showing a symptom of misshapen? ")
(assert (misshaping (read)))
(print " Is the plant showing a symptom of dark ,water-soaked depression? ")
(assert (dark (read))))
CLIPS> (defrule check-two
(corky yes)
(misshaping yes)
(dark yes)
=>
(println "The plant is showing a symptom of bacterial rind necrosis of watermelon disease."))
CLIPS> (reset)
CLIPS> (run)
Is the plant showing a symptom of Oily and water-soaked cotyledons? no
Is the plant showing a symptom of Yellow halo paralleling veins? n
Is the plant showing a symptom of Small dark and angled lesion on leaves? no
Is the plant showing a symptom of Dark green blotch on the upper surface of developing fruit? no
Is the plant showing a symptom of corky, dry necrosis? yes
Is the plant showing a symptom of misshapen? yes
Is the plant showing a symptom of dark ,water-soaked depression? yes
The plant is showing a symptom of bacterial rind necrosis of watermelon disease.
CLIPS>
If you want to provide flexibility in how your program interacts with the user, the best thing to do is to separate the code that reasons about symptoms and diseases from the code that interacts with the user. In your current implementation, these are intermingled.
To reimplement your code, first define a deftemplate for representing the symptoms and their associated questions:
(deftemplate symptom
(slot id)
(slot question)
(slot indicative (default yes)))
The indicative slot represents the response to a question that is indicative of a problem. So if the question is "Are the leaves a sickly yellow?", the indicative response would be yes, and if the question is "Are the leaves a healthy green?", the indicative response would be no.
The symptoms from your rules can now be represented as a group of facts:
(deffacts symptoms
(symptom (id ows)
(question "Is the plant showing a symptom of Oily and water-soaked cotyledons?"))
(symptom (id yhp)
(question "Is the plant showing a symptom of Yellow halo paralleling veins?"))
(symptom (id sda)
(question "Is the plant showing a symptom of Small dark and angled lesion on leaves?"))
(symptom (id dgb)
(question "Is the plant showing a symptom of Dark green blotch on the upper surface of developing fruit?"))
(symptom (id corky)
(question "Is the plant showing a symptom of corky, dry necrosis?"))
(symptom (id misshaping)
(question "Is the plant showing a symptom of misshapen?"))
(symptom (id dark)
(question "Is the plant showing a symptom of dark, water-soaked depression?"))
)
The diseases can be similarly represented as a group of facts:
(deftemplate disease
(slot id)
(multislot symptoms))
(deffacts diseases
(disease (id "bacterial fruit blotch")
(symptoms ows yhp sda dgb))
(disease (id "bacterial rind necrosis")
(symptoms corky misshaping dark))
)
The responses to questions and any disease conclusions will also be represented as facts:
(deftemplate answer
(slot id)
(slot value))
(deftemplate conclusion
(slot id))
With the symptoms and diseases represented as data, we can now add a generic rule which determines if every symptom of a disease is present:
(defrule conclude-disease
;; There is a disease
(disease (id ?disease))
;; For every symptom of that disease
(forall (disease (id ?disease)
(symptoms $? ?symptom $?))
(symptom (id ?symptom)
(indicative ?value))
;; There is a response indicative
;; of a problem
(answer (id ?symptom)
(value ?value)))
=>
;; Conclude the plant has the disease
(assert (conclusion (id ?disease))))
Now we can add code to handle interacting with the user. We'll start with your initial approach of using the keyboard to get responses from the user:
(deffunction ask-question (?question $?allowed-values)
(printout t ?question " " ?allowed-values " ")
(bind ?answer (read))
(if (lexemep ?answer)
then (bind ?answer (lowcase ?answer)))
(while (not (member$ ?answer ?allowed-values)) do
(printout t ?question " " ?allowed-values " ")
(bind ?answer (read))
(if (lexemep ?answer)
then (bind ?answer (lowcase ?answer))))
?answer)
(defrule ask-symptom
;; There is a symptom
(symptom (id ?symptom)
(question ?question))
;; For a disease
(disease (id ?disease)
(symptoms $? ?symptom $?))
;; That we have not determined
(not (answer (id ?symptom)))
;; And there is no prior response to a symptom
;; of that disease that is non-indicative.
(not (and (disease (id ?disease)
(symptoms $? ?other-symptom $?))
(symptom (id ?other-symptom)
(indicative ?value))
(answer (id ?other-symptom)
(value ~?value))))
=>
;; Ask the user the symptom question
(bind ?value (ask-question ?question yes no))
;; And assert the response.
(assert (answer (id ?symptom)
(value ?value))))
The ask-symptom rule improves upon the prior approach by not asking about symptoms for diseases that have a non-indicative response for a symptom. So if the response to necrosis is no, the user will not be asked about misshapen or depression.
Finally, some rules can be added to display the conclusions. These rules are given lower salience so that all of the questions are asked before any conclusion is displayed.
(defrule print-conclusion
(declare (salience -10))
(conclusion (id ?disease))
=>
(println "The plant is showing symptoms of " ?disease " watermelon disease."))
(defrule print-no-conclusion
(declare (salience -10))
(not (conclusion))
=>
(println "The plant is not showing all symptoms of a watermelon disease."))
If you want to use an interface rather than interacting through a console, you'd need to replace the code just added for asking questions and displaying conclusions.
CLIPS provides for querying the fact-list. For example, here's how you'd get a list of all the symptom facts from the CLIPS command prompt:
CLIPS> (find-all-facts ((?s symptom)) TRUE)
(<Fact-1> <Fact-2> <Fact-3> <Fact-4> <Fact-5> <Fact-6> <Fact-7>)
CLIPS>
What you can do in your interface is to query CLIPS for all of the symptom facts and then dynamically create the check boxes to display using the slot values in the facts.
When the user is ready for a response, you could have a diagnose button in your interface. When the user clicks that button, you can assert an answer fact with the appropriate values for each check box. After asserting those facts, you can run CLIPS and then use the query functions to retrieve the conclusion facts and then display the results. Alternately, any time a checkbox is changed you can assert the facts and run CLIPS.
If you download CLIPS from SourceForge, there are examples of integrating CLIPS with GUIs for .NET, Java, and iOS.