[rules-users] Better ways to write left hand sides

Dave Schweisguth dave at schweisguth.org
Mon Sep 28 18:43:55 EDT 2009


Greetings fellow Droolers,

I've just begun using Drools and am very happy with it so far as it seems to
be a great fit for what we want to do. My left hand sides are not quite
beautiful enough, however, and I wondered if anyone could suggest
improvements.

1) One of my facts' properties is a set of strings. The goal is to "grep"
   them for some search string. The most convenient syntax I've come up with
   so far is to give the fact a method anyTextContains which loops over the
   set and calls contains(searchString) on each member, and then use

fact: Fact() eval(fact.anyTextContains("searchString"))

in my LHS. It's compact enough, but I wondered whether it could be done
without a custom method or eval. "from" seems not to be a possibility since
the collection is of strings, not objects. Any suggestions?

2) Another of my facts' properties, "attributes", is a Map of Map of String.
   Unfortunately the second level of keys is sparse. If the attribute I want
   is present,

Fact(attributes.KeyOne.KeyTwo == 42)

looks and works great. If KeyTwo is absent, however, I get

org.drools.RuntimeDroolsException: Exception executing predicate attributes.KeyOne.KeyTwo == 42 [...] Caused by: [Error: unable to resolve method: java.util.HashMap.KeyTwo() [arglength=0]] [Near : {... Unknown ....}]

The best syntax I've found which is null-safe is

Fact(attributes.KeyOne["KeyTwo"] == 42)

which works but hurts the eyes somewhat. I'm not clear whether MVEL-style
null-safe traversal should work here; my experiments suggest it does not.
Again, any suggestions?

Thanks,

-- 
| Dave Schweisguth                           http://schweisguth.org/~dave/ |
| Home: dave at schweisguth.org            Work: http://www.nileguide.com/ |
| For compliance with the NJ Right to Know Act: Contents partially unknown |



More information about the rules-users mailing list