简体   繁体   中英

How to make the complement and difference operation between two sets in CLIPS?

I need to make complement and difference operations between two sets. I've a example, to do union between two sets, I can reuse this code to make these two other operations.

Thanks

The union example, that I've is:

(deffacts datos-iniciales
(conjunto B C A D E E B C E)
(conjunto E E B F D E))

(defrule inicio
=>
(assert (union)))
(defrule union
?h <- (union $?u)
(conjunto ? $? ?e $?)
(not (union $? ?e $?))
=>
(retract ?h)
(assert (union ?e $?u)))

Specifically, which part of the program should be changed? Thx

Here's how you can compute all three leaving the set-1 and set-2 facts unmodified, ignoring duplicate members, and sorting the results.

         CLIPS (6.31 6/12/19)
CLIPS> 
(deffacts datos-iniciales
   (set-1 B C A D E E B C E)
   (set-2 E E B F D E))
CLIPS>    
(deffacts universe
   (universe A B C D E F G H I J K))
CLIPS>    
(deffunction str-sort (?a ?b)
   (> (str-compare (sym-cat ?a) (sym-cat ?b)) 0))
CLIPS>    
(defrule calcula
   =>
   (assert (union)
           (complement)
           (difference)))
CLIPS> 
(defrule add-to-union
   ?union <- (union $?u)
   (or (set-1 $? ?v $?)
       (set-2 $? ?v $?))
   (test (not (member$ ?v ?u)))
   =>
   (retract ?union)
   (assert (union ?u ?v)))
CLIPS>    
(defrule add-to-complement
   ?complement <- (complement $?c)
   (universe $?u1 ?v $?u2)
   (set-1 $?s)
   (test (and (not (member$ ?v ?c))
              (not (member$ ?v ?s))))
   =>
   (retract ?complement)
   (assert (complement ?c ?v)))
CLIPS> 
(defrule add-to-difference
   ?difference <- (difference $?d)
   (set-1 $? ?v $?)
   (set-2 $?set2)
   (test (and (not (member$ ?v ?d))
              (not (member$ ?v ?set2))))
   =>
   (retract ?difference)
   (assert (difference ?d ?v)))
CLIPS> 
(defrule write-union
   (declare (salience -10))
   (union $?u)
   =>
   (printout t "The union is " (sort str-sort ?u) crlf))
CLIPS>    
(defrule write-complement
   (declare (salience -10))
   (complement $?c)
   =>
   (printout t "The complement is " (sort str-sort ?c) crlf))
CLIPS> 
(defrule write-difference
   (declare (salience -10))
   (difference $?d)
   =>
   (printout t "The difference is " (sort str-sort ?d) crlf))
CLIPS> (reset)
CLIPS> (run)
The union is (A B C D E F)
The complement is (F G H I J K)
The difference is (A C)
CLIPS> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM