droolsdrools-fusion

Drools LHS check against optional fact


I need to write a rule in Drools 6.5 that checks for the existence of an event of type A. There is a second class named B which has a field date.

While checking for existence of an event A, if at least one event of type B exists, A must happen after the latest B.date in order for the rule to fire; otherwise the rule should fire regardless of any B events.

Both event types of A and B have their own explicit timestamp field.

when
        // TODO if at least one event of type B exists, A must happen after max(b.date). Otherwise, the rule must fire regardless of any B
        $a : A( ... )
then
    ...

How do I perform this check?

EDIT: If no B is present in the working memory, and A meets the requirements, the rule must fire regardless.


Solution

  • This will fire for each A meeting the temporal constraint that it should happen after all Bs.

    $b: B()
    not B(this after $b)
    $a : A( this after $b )
    

    If you want to fire this only once, for any number of As, use exists in front of A and omit the binding.