0

I want to count elements in a list that have the same ID

(defrule UniqueIdentifier_testfile
   (P (ID_Jess ?id_context) (k $?ass))

   ?c <- (accumulate (bind ?count 0)                        ;; initializer
            (bind ?count (+ ?count 1))                    ;; action
            ?count                                        ;; result
            (and 
               (P (ID_Jess ?id_as1&:(member$ ?id_as1 ?ass)) (id ?id_ref1))
               (P (ID_Jess ?id_as2&:(member$ ?id_as2 ?ass)) (id ?id_ref2))
               (and (neq ?id_as1 ?id_as2) (eq ?id_ref1 ?id_ref2))
            )
        ) ;; CE

   (test (neq ?c 0))
 => 
   (printout t "UniqueIdentifier_testfile -->  FIRE ! (" ?id_context ":P).id :{"?ass"} -> 'id' not uniques" crlf)
)

As you can see : a fact of type P has a "real" ID which is reified in the slot "ID_Jess" and an other slot "id" which I'd like to test if it is unique too. In modelling words :

(deftemplate P 
  (slot ID_Jess)
  (slot id )
  (slot m )
  (multislot k )
)

It all looks fine, and "compile" allright. But the execution gives me that error :

    Jess reported an error in routine HasLHS.addPattern.
  Message: Variable used before definition: c.
  Program text: ( defrule UniqueIdentifier [...])  at line 70 in file <eval pipe>.
    at jess.HasLHS.a(Unknown Source)
    ...

Anybody got a clue why that ?c is not defined after the accumulate has been executed ? Even if the condition is never fulfilled, the counter (?count) which is binded as result will at least be a defined '0'.. Am I wrong ? If I specify an easier conditional element, the accumulate function executes as expected..

  • The last term in accumulate must be a simple pattern. I don't quite understand what you want to do here; the and CE in your accumulate appears to try and match pairs of different facts of type P, which doesn't agree with your statement "count ... with same ID". Perhaps you add some sets of sample facts and add the result you expect to get? – laune Sep 01 '16 at 02:51
  • Laune, I didn't know a "simple pattern" was expected in the la term of an accumulate function. Conditions are rarely simple to express, are they ? The counter counts the pairs of differents facts of type P w.r.t. an "id" slot. If the result is not 0, it means that this slot cannot be used as a unique reference id. The semantics of the rule is one things I'm gonna work further, take it as a repro example. But the behavior of the accumulate is not the one I expect and I wonder, why ? What do you mean by "a simple pattern" ? – Edouard Batot Sep 02 '16 at 14:40
  • (And I am reading this : http://stackoverflow.com/questions/27665616/complex-conditional-element-in-accumulate-in-jess-rules thanks) – Edouard Batot Sep 02 '16 at 15:51

0 Answers0