سؤال

How do you remove identical facts in CLIPS?

Presuming I have

(fact 2) (fact 3) (fact 2) (fact 4)

I want to remain only with (fact 2), (fact 3) and (fact 4). How can I do that?

هل كانت مفيدة؟

المحلول 3

Well I did it like this

(defrule removeduplicates
    ?f1 <- (fact ?number1)
    ?f2 <- (fact ?number2)
    (test (eq ?number1 ?number2))
    =>
    (retract ?f1)
)

نصائح أخرى

CLIPS allow duplicate facts by doing:

        (set-fact-duplication TRUE)

As mentioned in another answer, you must enable fact duplication before you can create duplicate facts, so the easiest way to remove duplicate facts is to not allow their duplication in the first place. If you want to allow duplicates, but remove a select few, then compare fact addresses to prevent the rule from being activated by one fact matching both patterns:

         CLIPS (6.31 6/12/19)
CLIPS> (set-fact-duplication TRUE)
FALSE
CLIPS> (assert (fact 2) (fact 3) (fact 2) (fact 4))
<Fact-4>
CLIPS> 
(defrule remove
   ?f1 <- (fact ?number1)
   ?f2 <- (fact ?number2)
   (test (and (eq ?number1 ?number2)
              (neq ?f1 ?f2)))
   =>
   (retract ?f1))
CLIPS> (agenda)
0      remove: f-3,f-1
0      remove: f-1,f-3
For a total of 2 activations.
CLIPS> (run)
CLIPS> (facts)
f-0     (initial-fact)
f-1     (fact 2)
f-2     (fact 3)
f-4     (fact 4)
For a total of 4 facts.
CLIPS> 
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top