Wednesday, June 25, 2008

Drools Clips progress

Made some progress over the weekend with Drools Clips, which will provide a Clips like language for Drools. Deftemplates are now working and I did some work on PackageBuilder so that it's now able to handle multiple namespaces and have a RuleBase attached to provide a more "shell" like environment suitable for Clips. Michael Neale also got a basic command line shell working. So what does it support?
  • deftemplate
  • defrule
  • deffuction
  • and/or/not/exists/test Conditional Elements
  • Literal, Variable, Return Value and Predicate field constraints
You can look at the ClipsShellTest and LhsClipsParserTest get an idea of the full support. It's still early stages and it's very rough in places, especially on error handling and feedback as well as no view commands to display data. For a little fun here is a screenshot of the shell in action:

The screen shot is a contrived example but it does show a shell environment cleanly mixing deftemplates and pojos - note that Drools 5.0 does not require shadow facts, due to the new asymmetrical Rete algorithm. It also shows deffunction in use. This will be part of Milestone1, that I'm hoping to tag tomorrow.

Monday, June 23, 2008

IDE re-license from ASL to EPL

We are looking to re-license our Eclipse IDE code from Apache Software License to Eclipse Public License. The reason for this is to simply align with the bulk of the Eclipse projects out there, to make bundling, deployment and code re-use much easier. The runtime work will remain as ASL.

I don't consider this change any issue as the EPL license is not a controversial one, unlike the LGPL. If anyone can see any issues I guess we can always dual license under ASL and EPL, but I don't want to needlessly confuse things.

I'm looking for community feedback on this before I do the change.


Tuesday, June 17, 2008

Why you need Business Rules in your workflow

Tom Baeyens has a good post on the jBPM (JBoss workflow) community day that was held at the Guinness brewery in Dublin. As you'd expect, the slides have plenty of pictures of people drinking beer, and one or two pictures of people talking about workflow and business processes. (Do not adjust your TV set. This blogpost is about business rules).

What you might not expect is that Business Rules and Rule Engines were key to one of the presentations on the day. In case you missed How to combine (jBPM) Workflow and (Drools) Business Rules - here's the summary (with the slideset available on this blogpost).

  • Workflow (e.g. JBoss jBPM) is great - it allows you to take spaghetti code and draw it as a workflow diagram (flowchart) so that it can be reviewed by the business (the nice people who pay our wages). You then attach standard (Java) actions to these steps.
  • Only problem is when you come to a decision node (the one circled in red below): How do you decide to go left or right (in the workflow)? Normally this is coded in Java - good for us, but hidden from those nice business people (which means that this is more room for errors-in-translation).
  • Business Rules allow you to keep those decision making rules in Plain English: When something is true , then do this. That's it. The rule engine does most of the hard work.
  • Integrating Workflow and Rules is easy. Use JBoss Seam (link) or do it by hand (link). And it works on non-JBoss web / app servers such as Websphere, Oracle Application Server, Tomcat and Weblogic.
  • Repeat x6 : Use workflow and rules. Use workflow and rules ...

Maybe it's co-incidence but, Tom Baeyens is now using strangely Rules-y like examples over on his workflow blog .

In this case Tom talks about task assignment; You may have a step such as 'Signoff Loan' in your workflow - but who does this task? Often these rules are complicated (e.g. loans less than 10,000 Dollars go to a junior officer, loans over 10 Million go to a bank president etc.). Business Rules allow you to keep these workflow task assignments in (almost) plain English.


Friday, June 13, 2008

Drools Job Posting - Drools Designer (Croatia)

Another job posting came in, so thought I'd highlight it in the blog.

Drools Designer
We are currently recruiting for an experienced software engineer with specific knowledge of Java and Rules engines, ideally DROOLS. You must have several years experience as a developer/ designer in enterprise environments, have an excellent grasp of rules engine concepts, supporting database design, and understand current web technologies.

The job is in Zagreb, but home based working is offered.

The opportunity is to work for a very innovative company in the media area on the developmen of a new technical platform. long term prospects very good. The position is that of designer, working with the software architect.

You must be able to demonstrate a very high level of intellectual curiosity about technologies and their use and deployment.

You must be fluent in english.

Click here for more details and to apply for this job.

Thursday, June 12, 2008

Google Summer of Code project

Anton Arhipov (a drools contributor who has worked on some Eclipse stuff in the past) - got a proposal accepted as a Google Summer of Code project.

His project is to make Guvnor (the BRMS) work with multiple content types - and more importantly have "pluggable" content editors (something that is kind of new to do in GWT - as GWT code is statically compiled into javascript).

He blogs about his experiences here.

Wednesday, June 11, 2008

JBoss Benelux User Group

I will be speaking next Friday (June 20th) at the JBoss Benelux User Group in Rotterdam (Netherlands) about Drools in general. So if you want a gentle introduction to Drools or want to chat afterwards, you can find the event details here:


Summary and programme:
The fourth Benelux JBoss User Group
- JBoss Portal - Julien Viet and Thomas Heute - JBoss
- Hibernate Search - Emmanuel Bernard - JBoss
- Woman in IT - Clara Ko and Linda van der Pal -
- JBoss Drools - Kris Verlaenen - JBoss

Date: Friday, 20 June 2008.
Arrive 13.30-14.00, presentations start 14.00, open bar 18.45 - 20.00.
Location: Staal Rotterdam, World Trade Center, Beursplein 33, 3011 AA Rotterdam

Friday, June 06, 2008

Allowing variable "masks" in DSL grammar - by Matt Geis

Revisiting some of the concepts Edson posted earlier, we decided to put ANTLR to use in how we handle DSLs.

At a high level, the logic used to parse a DSL entry was pretty straightforward. We were matching a pattern of "name=value", identifying a few groups inside the "name" block as variables (text surrounded by brackets, like "{name}"), and storing those variables for later use when the "value" block is substituted into the actual rule that needs to be expanded from a DSL into the DRL format that Drools knows how to work with.

However, we found that in certain circumstances, we'd pushed the regular expression to its limits. Its lifecycle consisted of the construction of a pattern string, followed by an optional capture group prepended to the DSL entry, and an optional capture group appended to it. Additionally, because Drools DSLs can handle regular expressions in the body of a DSL entry, we also had to allow our regular expression (and the corresponding escape codes) to identify embedded regular expressions. These pattern strings were defined in java files, so any spaces or backslashes had to be escaped as well. Add it all together, and you get a regular expression that was difficult to read, and more difficult to modify without causing an unintended ripple effect through the rest of the expression. Finally, we learned that the resulting expression was greedier than we wanted it to be. It was easy enough to write a line like

a user exists named "{name}" = User (name == "{name}")

But, if you have an object with a lot of attributes and want to support arbitrary creation of rules, you can't write a DSL expression for every possible permutation of attributes used in the LHS of your rule. Moreover, you may want to include constraints inside of a "from" or "exists" clause. To do so, you would want a DSL entry like

a user exists with {attribute} {value} = User (attribute == {value})

The pattern ended up matching not only the first constraint, but also part of subsequent constraints applied (there were DSL entries for the first constraint on any object, and for subsequent constraints), because any variable used was translated to a capture group of "(.*?)".

Edson had the idea of allowing users to define the exact nature of a variable, in other words...

a user exists with social security number "{ssn:\d{3}-\d{2}-\d{4}}" = User (ssn == "{ssn}")

Using ANTLR, we were able to parse the DSL entries and accurately isolate variable definitions, patterns within definitions, variable usage, and literal text. So now, it's straightforward to create a very strict matching such as...

[condition][]user has contact where {constraints}= u : User and exists (f: Person(where {constraints}) from u.contacts)
[condition][]where {attr:[A-Za-z0-9]+} is "{value}"={attr} == "{value}"
[condition][]and {attr:[A-Za-z0-9]+} is "{value}"=, {attr} == "{value}"

which could support a rule like

user has contact where firstName is "Edson" and country is "Brazil"

just as easily as it could support

user has contact where lastName is "Tirelli" and company is "Red Hat"

An even more user-friendly DSL can be built by the correct ordering of your DSL statements. For example, the addition of the following lines

[condition][]first name=firstName
[condition][]last name=lastName

would allow the user to rewrite the above rules as

user has contact where first name is "Edson" and country is "Brazil"

user has contact where last name is "Tirelli" and company is "Red Hat"

Take that approach, and create some simple token replacements like

[condition][]greater than = >
[condition][]less than = <
[condition][]is = ==
[condition][]where {attr:[A-Za-z0-9]+} is {value:[0-9]+}={attr} == {value}
[condition][]and {attr:[A-Za-z0-9]+} is {value:[0-9]+}=, {attr} == {value}

and a universe of flexibility opens up in front of you, as you can now accurately construct sentence fragments and phrases.

(note that attention to the ordering of such DSL entries is CRITICAL, as expansion of one DSL entry will affect the matching of subsequent entries, so if you were to match line 3 above, lines 4 and 5 would never match -- you'd have to change "is" to "==" to make them match)

With the new ANTLR model, we were able to easily support the requirement to limit matches by user-defined patterns (on a variable-by-variable basis), as well as pave the way for future enhancements to DSL usage.

Finally, now that we are liberated from the usage of a few regular expressions that capture everything, we can move ahead with other features for Drools DSLs. What started as

and progressed to
could be extended do something like
Examples of a possible attribute include a literal enumeration of allowable values for that variable, the name of a function to invoke to validate that the captured variable is allowed, the name of a function to invoke that provides a list of allowable values (a feature that could be leveraged by both the Eclipse plugin and in Guvnor).

Take the pattern matching for a test drive, let us know how it works for you (or doesn't, if that's the case), and tell us if there are any features you'd like implemented.

Caveat emptor: My rewrite of the DSL engine to use ANTLR was the result of just such a feature request. The need for tighter variable binding (basically "named capture groups") was acknowledged, and I was invited to make the necessary changes. I had no idea that it'd be so involved, or that the effort would be as rewarding as it has been.

Matt Geis


Sunday, June 01, 2008

Drools Puzzle Round 3: Mastermind

Important: Participation Rules. The rules are updated. Links to past puzzles and result reports can be found at the end of that entry.

Difficulty of this round: middle++

Submission deadline: August 16th, 2008 (since there hasn't been any submission for this puzzle till August 16th, the deadline is now October 16th, 2008. The T-shirts as prizes will have the new Drools logo printed on them. Added on October 15th: there still isn't any submission this puzzle really that difficult? The new deadline is now December 16th. )
Submission email:

This time we'd like to see implementation of the codebreaker for a variation of the classic game "Mastermind".

Please refer to the wikipedia Mastermind page for the game's basic rules. Our variation employs 12 different colors and 8 code slots. The is provided here. Participators must use this class as codemaker. The inline documentation of this class explains the judging rules in detail. The main method in the demonstrates how to call the code getter and how to use the answer judge.

Participators are required to write the codebreaker part of the mastermind application. Maximal trials is not specified in our codemaker, your should design a strategy which can minimize the number of trials.

Your test code should break 1000 different codes and output
  • the number of average trials for breaking a code,
  • the average time used for breaking a code
Something looks like this:
long totalTrials = 0;
long startTime = System.currentTimeMillis();

for (int i = 0; i < 1000, i++){
CodeMaker maker = new CodeMaker();
CodePeg[] code = maker.getCode();

// call your code breaker routine here
long endTime = System.currentTimeMillis();

System.out.println("Average trials per code-breaking: " + totalTrials/1000);
System.out.println("Average time per code-breaking: " + (endTime - startTime) / 1000 + " ms");
Your code should be reasonably packaged and documented. Please by all means document your strategy as comprehensible as necessary.