Drools learning

975 views
Skip to first unread message

Ananth R Y

unread,
Nov 27, 2014, 11:10:02 AM11/27/14
to drools...@googlegroups.com
Hi,

I am very much new to Drools and have never used it before, so don't have any context on this. 

We are working on a product where we have a set of around 3500 odd criteria's, that is we have to execute these criterias for each input object in an order until a match is found and populate the result accordingly. So if we do a custom implementation, it might be a performance overhead, and hence we thought of using drools. Can someone please guide me or suggest me on how to use drools or how drools fit into this requirement.

I would like to get some clarity on the following points,
1. How to dynamically create rules in drools.
2. How to run a set of rules and stop at once a match is found.

Regards,
Ananth Raman.

Stephen Masters

unread,
Nov 27, 2014, 12:09:49 PM11/27/14
to drools...@googlegroups.com
1. How to dynamically create rules in drools.
For dynamically creating lots of rules, take a look at decision tables:

2. How to run a set of rules and stop once a match is found.
You write the rules to match on whether a result has already been provided. In which case as soon as one rule matches, everything stops.

Evaluating rules in a specific order until a match is found, is not really how things work. Rules that don't match don't get evaluated. You could think of it a bit like an indexed RDBMS query. When you do a search for objects with a specific property, a database doesn't iterate through every row in the table, checking whether or not it has a matching value. Instead it makes use of indexes to shortcut its way to the rows which do match. But you can use properties such as 'salience' to indicate which rules have the higher priority. i.e. If two or more rules match, the one with the highest salience activates first.

Steve

Ananth R Y

unread,
Nov 27, 2014, 3:32:25 PM11/27/14
to drools...@googlegroups.com
Hi Stephen,

Thanks a lot for your quick reply. That really helps. 

I went through the decision tables and at first thought that it exactly fits for my scenario, but further analyzing, i could see some complications, as follows,
1. My criteria file, has a criteria column, which contains a query string like, (A=(1,2,3) and B=(20) and C=(1:9,30:39,56)), where A, B and C are different fields.
2. Each row has a different query string, with different set of fields and values. for eg, a row with (A=something and B=something and D=something) or  (D=something and E=something and A=something) etc.,
3. The main complexity is, some query has "=" and some has "<>". for eg, (A<>(1,2,3) and B=(20) and C=(11:19,40:49,61))

So if i need to use a decision table, then i need to restructure my existing file, by parsing the query and splitting the fields into separate columns, but still after that, due to point 3 complexity, i think it will not be possible to use this decision table feature.

Regards,
Ananth Raman.

Davide Sottara

unread,
Nov 27, 2014, 3:44:09 PM11/27/14
to drools...@googlegroups.com
Your existing criteria file is essentially a serialized rule metamodel. You will have to parse it and translate it into something that Drools can consume.
If decision tables are not an option, you'll have to generate DRL automatically.

You can use a template-based approach (mvel, velocity, etc..)  MVEL is already part of Drools
http://mvel.codehaus.org/MVEL+2.0+Templating+Guide

Or you can use the internal Drools builder API
https://github.com/droolsjbpm/drools/blob/master/drools-compiler/src/main/java/org/drools/compiler/lang/api/DescrFactory.java
https://github.com/droolsjbpm/drools/blob/master/drools-compiler/src/test/java/org/drools/compiler/lang/api/DescrBuilderTest.java

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.
To post to this group, send email to drools...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/drools-usage/9fa9306e-91d2-42bd-870b-d95e9ccb3666%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ananth R Y

unread,
Nov 28, 2014, 2:07:27 AM11/28/14
to drools...@googlegroups.com
Hi Davide,

Thanks for your suggestions. I will look into it and in case of any doubts i will post again. Thanks again.

Regards,
Ananth Raman.

On Thursday, November 27, 2014 9:40:02 PM UTC+5:30, Ananth R Y wrote:

Ananth R Y

unread,
Nov 28, 2014, 9:01:55 AM11/28/14
to drools...@googlegroups.com
Hi Davide,

I went through the test methods in the below url,

and when i tried to do a sample with the "testNamedConsequence" method, i am getting a noSuchElementException. It is because, the knowledge package collection seems to be empty,

PackageDescr pkg = DescrFactory.newPackage()
               .name( "org.drools.compiler" )
                               .newRule().name( "test" )
                   .lhs()
                       .pattern("Cheese").constraint( "type == \"stilton\"" ).end()
                       .end()
                   .rhs( "// do something" )
               .end()
               .getDescr();

KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newDescrResource( pkg ), ResourceType.DESCR );

Collection<KnowledgePackage> kpkgs = kbuilder.getKnowledgePackages();

As the kpkgs is empty, the following piece is throwing a noSuchElementException
kpkgs.iterator().next()

Please suggest.

Regards,
Ananth Raman.

On Thursday, November 27, 2014 9:40:02 PM UTC+5:30, Ananth R Y wrote:

Mark Proctor

unread,
Nov 28, 2014, 12:36:05 PM11/28/14
to drools...@googlegroups.com
I would recommend using template generation over this - you’ll have more control and DRL is more stable target. MVEL is a nice templating language.
-- 
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.
To post to this group, send email to drools...@googlegroups.com.

Davide Sottara

unread,
Nov 30, 2014, 11:17:18 PM11/30/14
to drools...@googlegroups.com
Ananth, I tried to run your code snipped in a test case, using Drools master (6.3.0-SNAPSHOT),
and it seems to work fine. Do you have a full, self-contained reproducer?


Ananth R Y

unread,
Dec 1, 2014, 3:55:33 AM12/1/14
to drools...@googlegroups.com
Hi Davide,

I am using the 6.1.0-Final version of drools and its failing.

Regards,
Ananth Raman.

On Thursday, November 27, 2014 9:40:02 PM UTC+5:30, Ananth R Y wrote:

Davide Sottara

unread,
Dec 1, 2014, 4:06:51 AM12/1/14
to drools...@googlegroups.com
This is very weird, because your code is essentially the same as one of the unit tests that have not been touched since 5.x, DescrBuilderTest
There has to be something else we're overlooking.. would you be able to share your code?


--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.
To post to this group, send email to drools...@googlegroups.com.

Ananth R Y

unread,
Dec 1, 2014, 7:55:18 AM12/1/14
to drools...@googlegroups.com
Hi David,

Please find the attached file that has the code to reproduce this.

Regards,
Ananth Raman.

On Thursday, November 27, 2014 9:40:02 PM UTC+5:30, Ananth R Y wrote:
DroolsCriteriaTest.java

Davide Sottara

unread,
Dec 1, 2014, 3:33:43 PM12/1/14
to drools...@googlegroups.com
Your system.out in the RHS is missing the final ";"
Rules in java dialect still require that :)

Call kbuilder.getErrors() after invoking kbuilder.add,
it will show you any errors found during the compilation process.

Notice that a package with errors is NOT built, resulting in
the empty collection you get.
Davide

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.
To post to this group, send email to drools...@googlegroups.com.

Ananth R Y

unread,
Dec 2, 2014, 2:10:19 AM12/2/14
to drools...@googlegroups.com
Thank you Davide, now its working.


On Thursday, November 27, 2014 9:40:02 PM UTC+5:30, Ananth R Y wrote:
Reply all
Reply to author
Forward
0 new messages