Search code examples
javasyntaxdrools

Java Drools Object Assignment With a Colon?


I'm learning Java and Drools so that i can edit an existing codebase, I've encountered some syntax i believe is simply assigning a variable/object but i'm uncertain and i'd appreciate some clarity and insight before i incorrectly apply and rely on it.

RULE "Rule1"

    WHEN
        result : Result()   
        policy : Policy()   

    THEN
        logger.info("Running Rule1");
        retract(result);
END

It's the "xxx : xxx()" bit i'm uncertain about.. i try searching, but i end up with static declaration etc

is it the same as "xxx = new xxx();" or "xxx : xxx();" ?

My previous coding experience is with PHP and C#, thanks.


Solution

  • A pattern element is the most important Conditional Element. It can potentially match on each fact that is inserted in the working memory.

    A pattern contains of zero or more constraints and has an optional pattern binding.

    [patternBinding :] pattern type ( [ constraints ] )

    In its simplest form, with no constraints, a pattern matches against a fact of the given type. In the following case the type is Person, which means that the pattern will match against all Person objects in the Working Memory:

    Person()
    

    For referring to the matched object, use a pattern binding variable such as $p. (The '$' is not mandatory, just a convention.)

    $p: Person()
    

    A property can be bound to a variable:

    Person( $firstAge : age ) // binding
    

    Just note that "binding" isn't "assigning"; a "binding variable" isn't a "variable" as it is known in C# or PHP.