Drools, how to check if an object with a specific property exists more than once in the list -


i have number of personentitys in working memory. need write rule check if 2 personentitys same someproperty existed in working memory. have written follows, problem is, when there 2 personentitys same someproperty, sys.out executing twice:

when      $person : personentity(person.persontype == persontype.legalperson);     $personlist : arraylist( size > 1 ) collect( personentity(someproperty.id == $legalperson.someproperty.id))          system.out.println("error occured"); 

i tried this, think since working memory has number of personentities, rule executing 4times (number of personentitys) , "error occurred " sentence appears 4times in console:

  personentity(some conditions, $relatedid :relatedperson.id);    exists personentity(some conditions, relatedperson.id > $relatedid) 

the standard solution problem add constraint using unique property guarantees ordering.

when    personentity(persontype == persontype.legalperson, $pid: personid)    exists personentity(persontype == persontype.legalperson, personid > $pid)    // 2 legalperson 

if expect 3 or more of same, may have add pattern block multiple activations:

when    personentity(persontype == persontype.legalperson, $pid: personid)    not personentity(persontype == persontype.legalperson, personid < $pid )    exists personentity(persontype == persontype.legalperson, personid > $pid)    // 2 or more legalperson 

(the suggestion use collect isn't unless need of these.)

this work when need check persontype:

 when    personentity( $pt: persontype, $pid: personid)    not personentity(persontype == $pt, personid < $pid )    exists personentity(persontype == $pt, personid > $pid)    // 2 or more $pt 

here may add collect instead of third pattern such personentity facts.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -