0

I'm looking for a way to delete facts based on a negative condition. For example after creating the following facts:

CLIPS> 
(deffacts Cars
   (color red)
   (color green)
   (color yellow)
   (doors three)
   (doors five))
CLIPS>    
(defrule combinations
   (color ?color)
   (doors ?doors)
   =>
   (assert (car ?color ?doors)))
CLIPS> (reset)
CLIPS> (run)
CLIPS> (facts)
f-0     (initial-fact)
f-1     (color red)
f-2     (color green)
f-3     (color yellow)
f-4     (doors three)
f-5     (doors five)
f-6     (car red five)
f-7     (car green five)
f-8     (car yellow five)
f-9     (car red three)
f-10    (car green three)
f-11    (car yellow three)
For a total of 12 facts.
CLIPS> 

I'm looking at deleting some of the facts with the following statement:

(defrule clear
    ?q1  <- (car ?color~green five)
=>
    (retract ?q1 )
    (printout t "Cars cleared " ?q1 crlf)
)

This should delete cars with five doors and color not green. Therefore ID f-6 and f-8 should be deleted. And print the facts that have been deleted.

The statement doesn't give me an error, but if I execute (run) it doesn't retract or printout the statement. I'm guessing the condition is not right, but I don't see how to write this negative condition otherwise.

Thanks

Selrac
  • 2,203
  • 9
  • 41
  • 84

1 Answers1

0

I found how to write the code properly:

(defrule clear
    ?q1  <- (car ~green five)
=>
    (retract ?q1 )
    (printout t "Cars cleared " ?q1 crlf)
)

Hope it helps someone

Selrac
  • 2,203
  • 9
  • 41
  • 84
  • You could also use ?color&~green in your original rule. With the & added the value in the first position of the fact is bound to ?color and the first position must not be green. Without the &, ?color is bound to the first position in the fact and the second position must not be green. – Gary Riley Sep 25 '16 at 01:42
  • thanks Gary for you insight. I'll get the hang of it eventually :-) – Selrac Sep 25 '16 at 08:19