Vaggelis Poul
Vaggelis Poul

Reputation: 1

Import rules to CLIPS and evaluate its performance

Can someone explain me how can I import the WEKA created rules in CLIPS and evaluate its efficience in TRS and TES data?

The data I use

I have written 7 rules out of 20 from WEKA tree. I include also 3 instances from the glass datasheet

small test code

(deftemplate glass
 (slot n(type FLOAT))
 (slot m(type FLOAT))
 (slot a(type FLOAT))
 (slot b(type FLOAT))
 (slot r(type FLOAT))
 (slot s(type FLOAT))
 (slot k(type FLOAT))
 (slot c(type FLOAT)))


(deftemplate Type
 (slot type))


(deffacts instances1
 (glass (n 13.00)   
        (m 2.28)         
        (a 1.00)
        (b 0.00)))

(deffacts instances2
 (glass (n 13.70)   
        (m 1.80)         
        (a 1.40)
        (b 0.00)))

(deffacts instances3
 (glass (n 13.70)   
        (m 1.90)         
        (a 1.40)
        (b 0.00)))

(defrule R1
 (glass (b ?b))
 (test (<= ?b 0.27))
 (glass (m ?m))
 (test (<= ?m 2.41))
 (glass (n ?n))
 (test (<= ?n 13.78))
 (glass (a ?a))
 (test (<= ?a 1.38))   
 =>
 (assert (Type (type buildwindnonfloat1)))
 (printout t "buildwindnonfloat1 detected" crlf))

(defrule R2
 (glass (b ?b))
 (test (<= ?b 0.27))
 (glass (m ?m))
 (test (<= ?m 2.41))
 (glass (n ?n))
 (test (<= ?n 13.78))
 (glass (a ?a))
 (test (> ?a 1.38))
 (glass (m ?m))
 (test (<= ?m 1.88))  
 =>
 (assert (Type (type containers2)))
 (printout t "containers2 detected" crlf))

(defrule R3
 (glass (b ?b))
 (test (<= ?b 0.27))
 (glass (m ?m))
 (test (<= ?m 2.41))
 (glass (n ?n))
 (test (<= ?n 13.78))
 (glass (a ?a))
 (test (> ?a 1.38))
 (glass (m ?m))
 (test (> ?m 1.88))  
 =>
 (assert (Type (type buildwindnonfloat3)))
 (printout t "buildwindnonfloat3 detected" crlf))

(defrule R4
 (glass (b ?b))
 (test (<= ?b 0.27))
 (glass (m ?m))
 (test (<= ?m 2.41))
 (glass (n ?n))
 (test (> ?n 13.78))   
 =>
 (assert (Type (type tableware4)))
 (printout t "tableware detected" crlf))

(defrule R5
 (glass (b ?b))
 (test (<= ?b 0.27))
 (glass (m ?m))
 (test (> ?m 2.41))
 (glass (a ?a))
 (test (<= ?a 1.4))
 (glass (m ?m))
 (test (<= ?m 3.34))
 (glass (a ?a))
 (test (<= ?a 1.25))  
 =>
 (assert (Type (type buildwindnonfloat5)))
 (printout t "buildwindnonfloat5 detected" crlf))

(defrule R6
 (glass (b ?b))
 (test (<= ?b 0.27))
 (glass (m ?m))
 (test (> ?m 2.41))
 (glass (a ?a))
 (test (<= ?a 1.4))
 (glass (m ?m))
 (test (<= ?m 3.34))
 (glass (a ?a))
 (test (> ?a 1.25))  
 =>
 (assert (Type (type buildwindfloat6)))
 (printout t "buildwindfloat6 detected" crlf))

(defrule R7
 (glass (b ?b))
 (test (<= ?b 0.27))
 (glass (m ?m))
 (test (> ?m 2.41))
 (glass (a ?a))
 (test (<= ?a 1.4))
 (glass (m ?m))
 (test (> ?m 3.34))
 (glass (m ?m))
 (test (<= ?m 3.82))
 (glass (r ?r))
 (test (<= ?r 1.51707))
 (glass (r ?r))
 (test (<= ?r 51596))  
 =>
 (assert (Type (type buildwindfloat7)))
 (printout t "buildwindfloat7 detected" crlf))

Upvotes: 0

Views: 207

Answers (1)

Gary Riley
Gary Riley

Reputation: 10757

To convert your data, it's easiest to read the data from the file when your program is running and directly assert the facts. So if your data looks like the following with each entry on its own line

1.5159,13.24,3.34,1.47,73.1,0.39,8.22,0,0,'build wind non-float' 
1.5167,13.24,3.57,1.38,72.7,0.56,8.44,0,0.1,'vehic wind float'

then your can read your data by reading each line as a single string, replacing the commas with spaces, and then splitting the string into multiple values. You can then have a separate rule map the values from your file to the appropriate slots in your deftemplate facts.

Store the expected result with each glass fact and then you can compare that value to the value that your rule is proposing.

         CLIPS (6.31 6/12/19)
CLIPS> 
(deftemplate glass
  (slot n (type FLOAT))
  (slot m (type FLOAT))
  (slot a (type FLOAT))
  (slot b (type FLOAT))
  (slot r (type FLOAT))
  (slot s (type FLOAT))
  (slot k (type FLOAT))
  (slot c (type FLOAT))
  (slot f (type FLOAT))
  (slot type))
CLIPS> 
(deftemplate input
   (multislot data))
CLIPS>    
(deffunction str-rpl (?str ?find ?replace)
   (if (eq ?find "")
      then
      (return ?str))
   (bind ?rs "")
   (bind ?fl (str-length ?find))
   (bind ?i (str-index ?find ?str))
   (while (neq ?i FALSE)
      (bind ?rs (str-cat ?rs (sub-string 1 (- ?i 1) ?str) ?replace))
      (bind ?str (sub-string (+ ?i ?fl) (str-length ?str) ?str))
      (bind ?i (str-index ?find ?str)))
   (bind ?rs (str-cat ?rs ?str))
   ?rs)
CLIPS>  
(defrule get-data
   =>
   (printout t "Input File? ")
   (bind ?file (readline))
   (if (not (open ?file data))
      then
      (printout t "Unable to open file" crlf)
      (return))
   (bind ?line (readline data))
   (while (neq ?line EOF)
      (bind ?line (str-rpl ?line "," " "))
      (bind ?line (str-rpl ?line "'" "\""))
      (assert (input (data (explode$ ?line))))
      (bind ?line (readline data)))
   (close data))
CLIPS> 
(defrule convert-data
   ?i <- (input (data ?r ?n ?m ?a ?s ?k ?c ?b ?f ?type))
   =>
   (retract ?i)
   (assert (glass (r ?r) (n ?n) (m ?m) (a ?a) (s ?s) (k ?k) (c ?c) (b ?b) (f ?f) (type ?type))))
CLIPS> 
(defrule R1
   (glass (b ?b) 
          (m ?m)
          (n ?n)
          (a ?a)
          (type ?type))
 (test (<= ?b 0.27))
 (test (<= ?m 2.41))
 (test (<= ?n 13.78))
 (test (<= ?a 1.38))   
 =>
 (printout t "buildwindnonfloat1 detected type = " ?type crlf))
CLIPS> 
(defrule R2
 (glass (b ?b)
        (m ?m)
        (n ?n)
        (a ?a)
        (type ?type))
 (test (<= ?b 0.27))
 (test (<= ?m 2.41))
 (test (<= ?n 13.78))
 (test (> ?a 1.38))
 (test (<= ?m 1.88))  
 =>
 (printout t "containers2 detected type = " ?type crlf))
CLIPS> 
(defrule R3
 (glass (b ?b)
        (m ?m)
        (n ?n)
        (a ?a)
        (type ?type))
 (test (<= ?b 0.27))
 (test (<= ?m 2.41))
 (test (<= ?n 13.78))
 (test (> ?a 1.38))
 (test (> ?m 1.88))  
 =>
 (printout t "buildwindnonfloat3 detected type = " ?type crlf))
CLIPS> 
(defrule R4
  (glass (b ?b)
        (m ?m)
        (n ?n)
        (type ?type))
 (test (<= ?b 0.27))
 (test (<= ?m 2.41))
 (test (> ?n 13.78))   
 =>
 (printout t "tableware detected type = " ?type crlf))
CLIPS> 
(defrule R5
  (glass (b ?b)
        (m ?m)
        (a ?a)
        (type ?type))
 (test (<= ?b 0.27))
 (test (> ?m 2.41))
 (test (<= ?a 1.4))
 (test (<= ?m 3.34))
 (test (<= ?a 1.25))  
 =>
 (printout t "buildwindnonfloat5 detected type = " ?type crlf))
CLIPS> 
(defrule R6
 (glass (b ?b)
        (m ?m)
        (a ?a)
        (type ?type))
 (test (<= ?b 0.27))
 (test (> ?m 2.41))
 (test (<= ?a 1.4))
 (test (<= ?m 3.34))
 (test (> ?a 1.25))  
 =>
 (printout t "buildwindfloat6 detected type = " ?type crlf))
CLIPS> 
(defrule R7
 (glass (b ?b)
        (m ?m)
        (a ?a)
        (r ?r)
        (type ?type))
 (test (<= ?b 0.27))
 (test (> ?m 2.41))
 (test (<= ?a 1.4))
 (test (> ?m 3.34))
 (test (<= ?m 3.82))
 (test (<= ?r 1.51707))
 (test (<= ?r 51596))  
 =>
 (printout t "buildwindfloat7 detected type = " ?type crlf))
CLIPS> (reset)
CLIPS> (run)
Input File? weka.txt
buildwindfloat7 detected type = vehic wind float
CLIPS> 

Upvotes: 1

Related Questions