- store your data in a database (or any other format)
- conditionally generate rules based on the values in the data
- use data for any part of your rules (e.g. condition operator, class name, property name)
- run different templates over the same data
The first example is SimpleRuleTemplateExample. We start with two files - the data and the template. In this case the data is the spreadsheet from an example you may be familiar with.
ExampleCheese.xls
If this was a regular decision table there would be hidden rows before row 1 and between rows 1 and 2 containing rule metadata. With rule templates the data is completely separate from the rules. This has two handy consequences - you can apply multiple rule templates to the same data and your data is not tied to your rules at all. So what does the template look like?
Cheese.drt
1 template headerLine 1: all rule templates start with "template header"
2 age
3 type
4 log
5
6 package org.drools.examples.templates;
7
8 global java.util.List list;
9
10 template "cheesefans"
11
12 rule "Cheese fans_@{row.rowNumber}"
13 when
14 Person(age == @{age})
15 Cheese(type == "@{type}")
16 then
17 list.add("@{log}");
18 end
19
20 end template
Lines 2-4: following the header is the list of columns in the order they appear in the data. In this case we are calling the first column "age", the second "type" and the third "log".
Lines 5: empty line signifying the end of the column definitions
Lines 6-9: standard rule header text. This is standard rule DRL and will appear at the top of the generated DRL. Put the package statement and any imports and global definitions
Line 10: The "template" keyword signals the start of a rule template. There can be more than one template in a template file. The template should have a unique name.
Lines 11-18: The rule template - see below
Line 20: "end template" signifies the end of the template.
The rule templates rely on MVEL to do substitution using the syntax @{token_name}. There is currently one built-in expression, @{row.rowNumber} which gives a unique number for each row of data and enables you to generate unique rule names. For each row of data a rule will be generated with the values in the data substituted for the tokens in the template. With the example data above the following rule file would be generated.
package org.drools.examples.templates;The code to run this is simple
global java.util.List list;
rule "Cheese fans_1"
when
Person(age == 42)
Cheese(type == "stilton")
then
list.add("Old man stilton");
end
rule "Cheese fans_2"
when
Person(age == 21)
Cheese(type == "cheddar")
then
list.add("Young man cheddar");
end
//first we compile the spreadsheet with the templateWe create an ExternalSpreadsheetCompiler object and use it to merge the spreadsheet with the rules. The two integer parameters indicate the column and row where the data actually starts - in our case column 2, row 2 (i.e. B2)
//to create a whole lot of rules.
final ExternalSpreadsheetCompiler converter = new ExternalSpreadsheetCompiler();
//the data we are interested in starts at row 2, column 2 (e.g. B2)
final String drl = converter.compile(getSpreadsheetStream(), getRulesStream(), 2, 2);

It's good to see drools finally get rule template functionality. It's a decent start, but there's a whole world of rule template techniques far beyond the sample find/replace approach.
ReplyDeleteI recall seeing an email where you elaborated on some of this (not sure where), but it would be great if you could shoot me your thoughts (or post them here) as I would like to keep extending this functionality.
ReplyDeletethere's samples on my blog, but it's totally disorganized. I've discussed this stuff with mike and mark quite a bit in the past. I thought either mark or mike made a list of potential features back in 2006.
ReplyDeleteI may have some old write ups. if I find it, i'll post it on my blog.
Since this feature was not available in version 4.0 we had to write out our own templates (seperating data from the actual logic).It is great to see that Drools has incorporated support for templates which seperates data from the logic.
ReplyDeleteHello,
ReplyDeleteDo you have an example using Templates with Data stored in database ?
Thanks
Can we have the source code for ExternalSpreadsheetCompiler object?
ReplyDeleteall source code can be found here:
ReplyDeletehttp://anonsvn.labs.jboss.com/labs/jbossrules/trunk/
My company is absolutely chomping at the bit for Rules Templates. Any time frame on when the might be released?
ReplyDeleteWhen I tried executing this example I got null pointer exception at the line
ReplyDeletethis.getClass().getResourceAsStream("Cheese.drt");
I am using Drools5.0.1. I am unable to read drt file as InputStream.Can someone tell what I need to change
It runs fine now.
ReplyDeletehttp://anonsvn.labs.jboss.com/labs/jbossrules/trunk/ this link is not working. Please post new link.
ReplyDeleteHowdy! Would you mind if I share your blog with my twitter group? There’s a lot of people that I think would really enjoy your content. Please let me know. Thanks ipad psd
ReplyDeleteI gotta favorite this website it seems invaluable extremely helpful ipad photoshop
ReplyDeleteyou got to work hard to earn lots of money because it is not very easy to earn:: website design san francisco
ReplyDeleteThis post is very simple to read and appreciate without leaving any details out. Great work!
ReplyDeletemachine learning course training in Guwahti