Yes, if you have 229 rules like that, breaking them up is advised. :)
The thing is, when you insert objects into working memory, with rules like that you're
most likely doing way more criteria matches than you need to. And while I'm pretty
sure drools does some rule optimization in the background (like breaking rules like one
below into several subrules) it can only go so far. (And the devs will have to address
exactly what drools is doing in that regard.) Also, I'm pretty sure drools treats
&& and "," differently behind the scenes for separating rule conditions.
Any reason why your rule doesn't look like this:
$bookingRule : BTExecutionBookingRuleModel (
priority == 1,
//
when a criterion is not set, it is considered as satisfied.
(traderCriterion == null|| $dealModel.trader ==
traderCriterion),
(portfolioCriterion == null|| $dealModel.portfolio ==
portfolioCriterion),
//
when a product type criterion is set to unknown, it is considered as satisfied
whatever deal product type is.
(productTypeStringCriterion == null|| productTypeCriterion ==
ProductType.Unknown
|| $dealModel.product.productType ==
productTypeCriterion),
(listedIndexCriterion == null|| $dealProductRelatedIndexes.relatedIndexes
containslistedIndexCriterion)
)
Search the drools-expert docs for "constraintGroup":
https://hudson.jboss.org/hudson/job/drools/lastSuccessfulBuild/artifact/t...
I believe that behind the scenes, drools creates alpha nodes in the rete based on the
constraint groups. So, as your big rules are structured now, you may be getting no benefit
from drools' rete optimizations because you have one big group per
BTExecutionBookingRuleModel constraint. (Specifically, you may be losing the alpha node
sharing, which would cause a big performance hit.)
Some other optimization considerations:
1) do your == checks use identity whenever possible? In the case of
"productTypeCriterion ==
ProductType.Unknown" is ProductType.Unknown an enum. (Or, less desirable, an int
constant)
2) When you use collections for contains checks, are the collections Sets? (or better yet,
EnumSets)
________________________________
From: Costigliola Joel (EXT) <joel.costigliola-ext(a)natixis.com>
To: Rules Users List <rules-users(a)lists.jboss.org>
Sent: Monday, September 28, 2009 9:57:49 AM
Subject: [rules-users] Struggling to improve my rules performance !
Hello,
I would like to improve the performance of my
Drools process, I have already done some enhancements like removing the use of “collect”
and “eval”.
I must say I’m a Drools newbie struggling to determine
what causes my performance problems.
Before asking, I need to explain in few words the
problem I want to solve with Drools :
My company is a bank where traders are making deals
on markets, these deals must be classified in book, this is what we call
“booking process”.
Booking is done according to booking criteria : which
trader has made the deal ? on which product ? etc …
A booking rule defines a set of criteria and the
target book where the deal will classified.
I’m using Drools to :
1. find all booking rules matching deals made by traders
2. apply the right booking rule on the deal among the ones matching the deal (i.e. that
is book the deal)
I’m testing the booking process with 1000
deals and 229 booking rule.
It takes 60 seconds for Drools to book the 1000
deals, which is too slow to be put in production.
On the 60 seconds, I have 7 seconds for facts
insertion and the remaining for process (after session.fireAllRules)
Do you think such numbers are normal/expected ?
Like I said I don’t really know where the
performance bottlenecks are … I’m thinking of exploring the
following possibilities :
* splitting my rules into very simple rules
* starting the booking process with fewer data and running it several times (instead of
one booking process on 1000, run 10 booking processes on 100 deals each)
I have put my rules code below (for courageous people),
if you see some obvious performance problems or if you can give some hint to
explore, you would be very welcome.
Thanks in advance for your lights,
Regards,
Joël Costigliola
Ps : rules details
I have defined a flow composed of 3 steps/rules
group, each group has one rule, here’s the flow and the rules.
Flow :
------
"Find
matching booking rules" -> "Refresh" -> "Apply
booking rule group"
Rules :
------
The job of this rule is to store in DealMatchingBookingRules all
the booking rule matching a specific deal (each DealMatchingBookingRules instance
has a unique Deal).
rule"Find
matching level 1 booking rules by deal"
salience10
no-loop
ruleflow-group"Find
level 1 matching booking rules by deal group"
when
$dealMatchingBookingRules :
DealMatchingBookingRules($dealModel : deal, $dealProductRelatedIndexes :
dealProductRelatedIndexes)
$bookingRule : BTExecutionBookingRuleModel (
priority == 1
//
when a criterion is not set, it is considered as satisfied.
&& (traderCriterion == null|| $dealModel.trader ==
traderCriterion)
&& (portfolioCriterion == null|| $dealModel.portfolio ==
portfolioCriterion)
//
when a product type criterion is set to unknown, it is considered as satisfied
whatever deal product type is.
&& (productTypeStringCriterion == null|| productTypeCriterion ==
ProductType.Unknown
|| $dealModel.product.productType ==
productTypeCriterion)
&& (listedIndexCriterion == null|| $dealProductRelatedIndexes.relatedIndexes
containslistedIndexCriterion)
)
then
Logger log = LoggerFactory.getLogger("Matching booking rules
LOGGER");
log.info("Found a level 1 matching rule for deal : "+ $dealModel + ",
rule is "+ $bookingRule);
$dealMatchingBookingRules.addMatchingBookingRule($bookingRule);
end
// used to tell Drools that our $dealMatchingBookingRules are
ready for next step in flow
rule"Refresh
facts in level 1 booking rule process"
salience5
no-loop
ruleflow-group"Refresh
facts in level 1 booking rule process group"
when
$dealMatchingBookingRules : DealMatchingBookingRules()
then
update($dealMatchingBookingRules);
end
rule"Apply
level 1 booking rule"
no-loop
ruleflow-group"Apply
level 1 booking rule group"
when
$dealMatchingBookingRules :
DealMatchingBookingRules(hasSingleMatchingLevel1BookingRule == true)
then
BTExecutionBookingRuleModel effectiveBookingRuleModel =
$dealMatchingBookingRules.getSingleMatchingLevel1BookingRule();
effectiveBookingRuleModel.applyRuleOnDeal($dealMatchingBookingRules.getDeal());
retract($dealMatchingBookingRules);
end
________________________________
Ce courriel et toutes les pièces jointes sont confidentiels et peuvent être couverts par
un privilège ou une protection légale. Il est établi à l’attention exclusive de ses
destinataires. Toute utilisation de ce courriel non conforme à sa destination, toute
diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation
expresse préalable. Toutes opinions exprimées dans ce courriel ne sauraient nécessairement
refléter celle de Natixis, de ses filiales. Elles sont aussi susceptibles de modification
sans notification préalable. Si vous recevez ce courriel par erreur, merci de le détruire
et d’en avertir immédiatement l’expéditeur. L’Internet ne permettant pas d’assurer
l’intégrité de ce courriel, Natixis décline toute responsabilité s’il a été altéré,
déformé ou falsifié et chaque destinataire qui utilise ce mode de communication est
supposé en accepter les risques.
This email and any attachment are confidential and may be legally privileged or otherwise
protected from disclosure. It is intended only for the stated addressee(s) and access to
it by any other person(s) is unauthorised. Any use, dissemination or disclosure not in
accordance with its purpose, either in whole or in part, is prohibited without our prior
formal approval. Any opinion expressed in this email may not necessarily reflect the
opinion of Natixis, its affiliates. It may also be subject to change without prior notice.
If you are not an addressee, you must not disclose, copy, circulate or in any other way
use or rely on the information contained in this email. If you have received it in error,
please inform us immediately and delete all copies. The Internet can not guarantee the
integrity of this email therefore Natixis shall not be liable for the email if altered,
changed or falsified and anyone who communicates with us by e-mail is taken to
accept these risks.
________________________________