0
votes

I'm a beginner to the Expert System language called CLIPS. I need help writing a CLIPS script to decide, given three facts regarding properties A, B, and C (density, conductivity, etc), whether the object having these facts/properties is of a certain type, say TYPE1. I need to code in the rules necessary to make this decision. Thank you for your help.

KB

1
What have you got so far? We're not big on doing your homework, but if you get stuck on something, we can often help. - Topological Sort
I know the syntax and how to write rules but I'm not sure how to do it in an elegant way. For example - user6004863
I know the syntax and can write rules but I'm not sure how to do it in an elegant way. I thought of using deftemplate as: (deftemplate material (slot name) (slot density) (slot conductivity) (slot specific_heat ). Then I can use deffacts to create the objects in the system's "database". Then, when the user enters 3 facts corresponding to the properties density, conductivity, specific_heat, my rules decide the name of the material. Does that sound about right? If so, could you please suggest the "structure" of the script that I'm supposed to write and I'll try to fill in the details. Thanks. - user6004863
Maybe you could post your code -- a minimal but complete example of what you're trying to do, and we can comment on it. - Topological Sort

1 Answers

0
votes

Elegance is subjective, but if you want code that is reusable and extensible, represent as much of the information as you can as data so that you can write generic rules.

CLIPS> 
(deftemplate obj
   (slot obj-name)
   (slot print-name))
CLIPS> 
(deftemplate opv 
   (slot obj-name)
   (slot prop)
   (slot val))
CLIPS>       
(deftemplate type
   (slot type-name)
   (slot print-name))
CLIPS>       
(deftemplate type-prop
   (slot type-name)
   (slot prop-name)
   (slot prop-test)
   (multislot values))
CLIPS>       
(defrule is-type
   ?o <- (obj (obj-name ?obj-name))
   ?t <- (type (type-name ?type-name))    
   (forall (type-prop (type-name ?type-name) 
                      (prop-name ?prop-name)
                      (prop-test ?prop-test)
                      (values $?values))
           (opv (obj-name ?obj-name)
                (prop ?prop-name)
                (val ?val))
           (test (funcall ?prop-test ?val (expand$ ?values))))
   =>
   (printout t (fact-slot-value ?o print-name) 
               " is "
               (fact-slot-value ?t print-name)
               crlf))
CLIPS>            
(deffacts objects
   (obj (obj-name thing1) (print-name "Thing 1"))
   (opv (obj-name thing1) (prop color) (val red))
   (opv (obj-name thing1) (prop weight) (val 5))
   (obj (obj-name thing2) (print-name "Thing 2"))
   (opv (obj-name thing2) (prop color) (val blue))
   (opv (obj-name thing2) (prop weight) (val 20))
   (obj (obj-name thing3) (print-name "Thing 3"))
   (opv (obj-name thing3) (prop color) (val white))
   (opv (obj-name thing3) (prop weight) (val 10))) 
CLIPS>       
(deffacts types
   (type (type-name light) (print-name "light"))
   (type-prop (type-name light) (prop-name weight) (prop-test <=) (values 5))
   (type (type-name heavy) (print-name "heavy"))
   (type-prop (type-name heavy) (prop-name weight) (prop-test >=) (values 20))
   (type (type-name just-right) (print-name "just right"))
   (type-prop (type-name just-right) (prop-name weight) (prop-test >=) (values 20))
   (type-prop (type-name just-right) (prop-name color) (prop-test one-of) (values red green blue)))
CLIPS> 
(deffunction one-of (?val $?rest)
   (member$ ?val ?rest))
CLIPS> (reset)
CLIPS> (run)
Thing 2 is just right
Thing 2 is heavy
Thing 1 is light
CLIPS>