Re: 3/5 Drools - Grasshoppa to Guru question - THIS operator

16 views
Skip to first unread message

Jeremy Ary

unread,
Mar 6, 2013, 11:29:37 AM3/6/13
to nashvi...@googlegroups.com
Thanks for the question. The reasons for allowing a 'this' constraint operator are a few. Unfortunately, the context of the presentation doesn't allow a lot of elaboration. Good chance to peer into some of the capabilities you have within rules, so let's check out the various uses for the operator, growing in rule complexity as we go...

-----------------------------

First, it help match multiple facts of the same object type when we would like to test for commonality. An example that may better describe the reason for inclusion would be "at least two cars exist with the same color paint":

when
   $car : Car ( $color : paintColor )
   Car ( this != $car, paintColor == $color )
then
   # >= 2 cars exist of the same color
end

Since each consequence pattern would be taken in step, without the 'this' check, one car would always satisfy the rule, as the one car is guaranteed to always have the same color as itself. By using 'this', we can ensure that at least 2 cars exist with the same paint color.

-----------------------------

this' also allows simplistic rules around some of the natively-supported types in rules, such as String:

when
    String ( this == "foo" )
then
    # string matches
end

-----------------------------

'this' allows for sub-type checking when dealing with inheritance. Assume class Stinson is an extension of Cheese:

when
    Cheese ( this isA Stinson )
then
    # Cheese is of class Stinson
end

-----------------------------

'this' can also assist in more complex condition sets. Take the following example that states that every Car in the system of type sedan must also have a 5+ year warranty in order to trigger the rule.

when
    forall ( $car : Car ( type = 'sedan', $warranty : warranty )
                       Warranty ( this == $warranty, durationInMonths >= 60 ) )
then
    # all sedans have a 5+ year warranty
end

You could complete this same evaluation with a one-line forall using MVEL's nested accessors of warranty, but for use case, I've carried it over two lines.

-----------------------------

Once you get into the world of CEP, 'this' allows for time interval comparisons, where events are associated with timestamps (so to speak):

when
    $landing1 : PlaneLandEvent (  )
    PlaneLandEvent ( this after[ 1s, 10s] $landing1 )
then
    # second plane landed between 1 and 10 seconds after the first
end

-----------------------------

'this' is also handy for collection checking when working with the collect and accumulate functionalities:

when
    $list : ArrayList( ) from collect ( Student (  class == "math" ) )
    $student : Student ( id == 1, this memberOf $list )
then
    #Student exists within a collection of students in math class
end

-----------------------------

Hope this helps! There's certainly more uses, but this is likely enough to get the general idea across. Ask away if more come to mind.

Jeremy

On Wednesday, March 6, 2013 8:56:10 AM UTC-6, Bailin wrote:
Thanks for a great talk Jeremy.

I noticed that on slide 13 of the presentation, there was a bit that said that THIS is an operator that allows us to reference facts, but will then disallow the same fact from satisfying multiple conditions. That seems like a strange effect to have, one that will easily trip up unaware developers. Do you have any idea why that is?
Reply all
Reply to author
Forward
0 new messages