### Modify Block for Java Dialect

In one more post in the series: “What are you guys doing right now?”, 🙂 we just added support to modify block in Java dialect consequences.

This is a feature that was already supported by MVEL and we decided to port it to Java so that it would benefit everyone, independently of the chosen dialect.

Basically, a modify block is a block of code that allow attributes of a given fact to be changed in the consequence of your rules, even with shadow facts turned off. In other words, if you don’t want to pay for the shadow fact performance and memory cost, you can disabled them as long as you only change fact attributes inside modify blocks.

Example:

rule "Eat Cheese"when  $p: Person( status == "hungry" )$c: Cheese( )then  retract( $c ); modify($p ) {      setStatus( "full" ),      setAge( \$p.getAge() + 1 )  }end

So, inside of the modify block, you may write a comma separated list of Java expressions (set attribute values, call methods, etc) that are applied to the object scoped to that modify call. All of them are executed in sequence and inside a single fact update scope.

The grammar pseudo BNF looks like this:

modify ::= 'modify' '(' <expression> ')' '{' [ <expression>  [ ',' <expression> ]* ] '}'

This feature was added both to trunk and 4.0.x branches and will be released in Drools 4.0.4 and Drools 4.1.0.

Happy drooling,
Edson

Comments are closed.