Wednesday, January 18, 2012

Fine Grained Property Change Listeners (Slot Specific) (Mario Fusco)

Just a quick recap of what I did until now to check if we are all on the same page and also agree with the naming convention I used.

The property specific feature is off by default in order to make the behavior of the rule engine backward compatible with the former releases. If you want to activate it on a specific bean you have to annotate it with @propSpecific. This annotation works both on drl type declarations:

declare Person
firstName : String
lastName : String

and on Java classes:

public static class Person {
private String firstName;
private String lastName;

Moreover on Java classes you can also annotate any method to say that its invocation actually modifies other properties. For instance in the former Person class you could have a method like:

@Modifies( "firstName, lastName" )
public void setName(String name) {
String[] names = name.split("\\s");
this.firstName = names[0];
this.lastName = names[1];

That means that if a rule has a RHS like the following:

modify($person) { setName("Mario Fusco") }

it will correctly recognize that both the firstName and lastName have been modified and act accordingly. Of course the @Modifies annotation on a method has no effect if the declaring class isn't annotated with @PropSpecific.

The third annotation I have introduced is on patterns and allows you to modify the inferred set of properties "listened" by it. So, for example, you can annotate a pattern in the LHS of a rule like:

Person( firstName == $expectedFirstName ) @watch( lastName ) // --> listens for changes on both firstName (inferred) and lastName
Person( firstName == $expectedFirstName ) @watch( * ) // --> listens for all the properties of the Person bean
Person( firstName == $expectedFirstName ) @watch( lastName, !firstName ) // --> listens for changes on lastName and explicitly exclude firstName
Person( firstName == $expectedFirstName ) @watch( *, !age ) // --> listens for changes on all the properties except the age one

Once again this annotation has no effect if the corresponding pattern's type hasn't been annotated with @PropSpecific.

I've almost finished with the development of this feature (at the moment I am missing the compile-time check of the properties named in the @watch annotation together with some more exhaustive tests), so if you think that I misunderstood something or there is room for any improvement (or you just don't like the annotation's names I chose) please let me know as soon as possible.




  1. Thank you, this feature looks excellent. It should make rule authoring significantly easier with specific domain models I've wanted to use in the past.

  2. Cheers for a very good post. You have given me satisfaction to every post i read here. Hope you keep on sharing relevant information to all your readers.

  3. Knowledge giving Article! I appreciate you. I completely agree with you. If we talk about current scenario then it is must be update. I enjoyed reading. I would like to visit more for more queries.

  4. I know its too late to comment on this post. But, I need some information regarding @PropSpecific . If I am correct @PropSpecific has been renamed to @PropertySpecific. When I tried to use this annotation in my Facts(java class) Eclipse IDE is not able to identify or resolve the package for @PropertySpecific annotation. I am using Drools 5.6.0. Is this @PropertySpecific annotation still supported in Drools, if yes then where is it ? in which package and in which jar ?

  5. Best to use the mailing list for Qs.