timeNtrack
timeNtrack

Reputation: 27

'this' equivalent keyword in Drools rule left-hand side

Given a configuration class Config that contains an attribute excludeShifts, which is a list of class Shift, is there a keyword like 'this' that represents the current object within the matching process? For example,

rule "Match but not if excluded"
  when
     $config : Config(...)  // additional matching criteria deleted

     $shift : Shift(this not memberOf $config.excludeShifts,
                    ...     // additional criteria deleted
                    )
     ... // additional criteria deleted
  then
     ...
end

I recognize that functionally I can reverse the ordering to achieve this match using:

   $shift : Shift()
   $config : Config(excludeShifts not contains $shift
                   )

Upvotes: 0

Views: 305

Answers (1)

Roddy of the Frozen Peas
Roddy of the Frozen Peas

Reputation: 15219

this is a recognized keyword, and it is used just in the same way you've indicated, as a reference to the current object being evaluated.

Some examples:

// An even integer in working memory
$even: Integer( this % 2 == 0 )

// A string with a specific value
$animal: String(this in ("cat", "dog", "fish"))

// A value from a map
Map( $value: this['key'] )

Your example works just as you've written it.

rule "Match but not if excluded"
when
  $config : Config($excluded: excludedShifts)
  $shift : Shift(this not memberOf $excluded)
then
end

I poked through the documentation and I don't see if this is ever called out as a keyword (though admittedly it's a little hard to search for.) It is used in a few examples, for example, in the forall operator example code. As Esteban suggested in the comments, you are welcome to open a bug report or submit a fix for the documentation to remedy this oversight.

Upvotes: 1

Related Questions