[rules-users] Rules writing best practice?

Pegram, Macon zmpegram at choosehmc.com
Tue Feb 9 08:57:13 EST 2010


Regarding your comment for DAO's in the WHEN clause.   I'd say that's a
very strong anti-pattern.   In general our approach has been to look for
the absence of the fact being inserted, and move the DAO call into the
THEN clause.   I'm sure you're thinking, "what if my DAO doesn't return
anything?"  See the next discussion on breadcrumbing.  Drools will fire
your DAO call on every agenda scan (IE: whenever a fact is inserted,
modified, or retracted) if it's included in the WHEN clause.  

The other two tools we've used are to create what we call a "bootstrap"
agenda and use "breadcrumb" facts.  The bootstrap agenda is made up of a
small set of rules that handle loading up initial facts for our rule
set.  Using simple "Breadcrumb" facts you can cut down on
rescans/execution of rules.  We created a simple RuleBreadcrumb object
that contains a "name" attribute.  Note, we don't use breadcrumbing for
all rules.  We mostly use them in the bootstrap phase because there are
a lot of unrelated changes going on in working memory and there's a risk
that the DAO may not return a fact. Putting it all together it looks
something like this:

rule "Start Bootstrap Agenda"
    salience 100
    when
    	GoalsRequest(memberID != null) 
        not RuleBreadcrumb(name == "Start Bootstrap Agenda")
    then
        drools.setFocus("bootstrap");
        insert(new RuleBreadcrumb("Start Bootstrap Agenda"));
end

rule "Load Some Fact Bootstrap"
	agenda-group "bootstrap"
    when
	   not RuleBreadcrumb(name == "Load Some Fact Bootstrap")
         SomeOtherCriteria(anAttribute != null)
    then
   	   insert(new RuleBreadcrumb("Load Some Fact Bootstrap"));
insert(myDao.getSomeData());		
end


So while I wouldn't call this a "best practices" document, those are at
least 2 patterns we've found to be performant and quite useful in our
own rule development. 

Macon

-----Original Message-----
From: rules-users-bounces at lists.jboss.org
[mailto:rules-users-bounces at lists.jboss.org] On Behalf Of Brice Figureau
Sent: Tuesday, February 09, 2010 4:40 AM
To: Rules Users List
Subject: [rules-users] Rules writing best practice?

Hi,

Is there a rules writing best practice document somewhere?

I'm asking the question because as a newbie rule writer (which doesn't
know anything about the underlying drools matching algorithm), I'm
wondering if my rules will behave gracefully.

For instance, I (ab)use a lot the from/accumulate to bring in the
knowledge some external facts coming from some DAO. 
All my rules use one time or more the same "from
dao.getInformationFromDatabase( <inserted fact> )" structure and I'm not
sure the algorithm is smart enough to cache this information which is
constant for a given inserted fact.

Should I instead insert all this information as facts prior to the rules
firing?
-- 
Brice Figureau
My Blog: http://www.masterzen.fr/

_______________________________________________
rules-users mailing list
rules-users at lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users




More information about the rules-users mailing list