eventsdroolsdrools-fusion

Getting the latest event satisfying a condition


Is there any way that I can get the latest event which satisfies a certain predicate ?

For example, if I write this rule:

rule 1:
  when
    myObject: MyObject(id == "id1" && name == "name1" && type == "type1")
  then
    myObject.doSomething();

I want the rule to get fired only if the MyObject object inserted most recently with the id "id1" has the given name and type. Note that at a time, there could be multiple MyObject's with this id.

In essence, I want to do something like this:

rule 1:
  when
    myObject: (Get the latest MyObject with its id == "id1") and
    ( myObject.name == "name1" && myObject.type == "type1") )
  then
    myObject.doSomething();

I am using Drools 6.2.0.


Solution

  • Assuming that you do have events, which requires

    declare MyObject
      @role( event )
    end
    

    you can write this rule:

    rule "latest id1-name1-type1"
    when
      myObj: MyObject( id == "id1", name == "name1", type == "type1" )
      not MyObject( this after myObj, id == "id1" )
    then
       myObj.doSomething();
    end