Ok I think I get it.
I think we can enhance
the proposed solution (My turn to share insight !)
We can get rid of
inserting Deal in session since Score already has a reference on deal.
It leads to a performance
gain since we insert less facts in session and avoid a join in rule (see
enhanced rules below).
Enhanced Rules :
/* level 2 rules */
rule "BR2 trader
and product"
salience 10
when
$s : Score( level ==
2, deal.trader=="Alex", deal.product == "GOOG")
then
$s.setCount(
$s.getCount() + 1 );
$s.setBook(
"B2" );
end
rule "BR3 trader
and market"
salience 10
when
$s : Score( level ==
2, deal.trader=="Alex", deal.market == "NY")
then
$s.setCount(
$s.getCount() + 1 );
$s.setBook(
"B3" );
end
/* level 1 rules : less
prioritary than level 2 */
rule "BR1
trader"
salience 5
when
$s : Score( level ==
1, deal.trader=="Alex")
then
$s.setCount(
$s.getCount() + 1 );
$s.setBook(
"B1" );
End
---------
I also come to another way
of handing success and failure (I don’t know if it’s a better solution)
We could change Score
to make it a Collector of Book by level, something like :
class Score{
Map<level, Book list>
booksByLevel // store list of target Book by level
Deal deal;
}
Our rules need to be
modified, for example :
rule "BR2 trader
and product"
salience 10
when
$s : Score(deal.trader=="Alex",
deal.product == "GOOG")
then
$s.addTargetBook(level2,
"B2" );
end
new way of handling success/failure
rule "book
deal" salience 0
when
$s : Score()
then
if
$s.hasUniqueLevel2Book() // LEVEL 2 handling
assign $s.deal to
$s.booksByLevel().get(level2).get(0);
retract( $s );
else if
$s.hasUniqueLevel1Book() // LEVEL 1 handling
assign $s.deal to
$s.booksByLevel().get(level1).get(0);
retract( $s );
else
print("unable
to book deal" + $s.deal)
retract( $s );
end
I like this solution
because :
- booking rules (BR2)
are very simple
- success/failure is
done in one rule
- success/failure id
done without calling modify($s) which can lead to performance problem since
Drools reevaluate rules against modified fact
Regards,
Joel
De :
rules-users-bounces@lists.jboss.org
[mailto:rules-users-bounces@lists.jboss.org] De
la part de Costigliola Joel (EXT)
Envoyé : jeudi 29 octobre
2009 10:30
À : '
Objet : Re: [rules-users]
Specific Agenda strategy to control which rulesto fire
Hi Wolgang,
Interesting advice !
I need to study it a
little bit to fully understand it and to see if it can meet my future
requirements.
Thanks for taking the
time to share your insight,
Regards,
Joel
De :
rules-users-bounces@lists.jboss.org
[mailto:rules-users-bounces@lists.jboss.org] De
la part de Wolfgang Laun
Envoyé : jeudi 29 octobre
2009 08:09
À :
Objet : Re: [rules-users]
Specific Agenda strategy to control which rules to fire
I don't think you should
consider an agenda strategy for this.
Add a simple class:
class Score{
int level; int coun; Deal deal; Book book;
Score( Deal deal, int level ){...}
}
and insert an instance along with the Deal to be classified:
insert( deal );
insert( new Score( deal ) );
Rules for level 2 would be written according to:
rule "trader and product"
salience 10
when
$s : Score( level == 2, $d : deal )
Deal( this == $d, trader=="Alex",
product == "GOOG" )
then
$s.setCount( $s.getCount() + 1 );
$s.setBook( "B2" );
end
Then you'll need a couple of rules handling success and failure:
rule "post level success"
salience 5
when
$s : Score( $l : level, count == 1, $d : deal, $b : book )
then
assign $d to $b, retract $d
retract( $s );
end
rule "post level failure"
salience 5
when
$s : Score( $l : level, count != 1 )
then
modify( $s ){
setLevel( $l - 1 );
}
end
Rules for level 1 would also be at salience 10.
A rule for level == 0 should catch Deals "gone down" through all
levels.
-W
2009/10/28 Costigliola Joel (EXT) <joel.costigliola-ext@natixis.com>
Hello
all,
I
need some help to to set a specific Agenda strategy in order to control finely
which activated rules will be fired.
Problem
context :
-----------------
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 ? wich market ? etc …
A
booking rule defines a set of criteria and the target book where the deal will
classified, it also has a priority, note that it is ok that two booking rule
have same priority.
I
want to implement booking rule as Drools rule.
Several
booking rule can be applied to a deal, in that case choosing the right booking
rule to fire depends on the following algorithm :
-
look all the activated booking rule of the highest priority,
---
if there is a unique rule apply it
---
if there is no unique rule (0 rule or more than one), look at rules of a lesser
priority and apply the same logic.
Next
section is an example that will clear things (I hope).
Example
:
---------
A
deal D1 has been done by Alex on NY market to buy Google stocks.
We
have 3 booking rules :
-
BR1 : criteria = trader=Alex / book = B1
-
BR2 : criteria = trader=Alex and product = google stock / book = B2
As
BR2 is more precise than BR1, il will matches the deal and book it in B1
If
the deal was on another product, BR1 would have been applied.
Things
gets more complicated when 2 rules of same priority can be applied.
Let's
imagine we add the following booking rule
-
BR3 : criteria = trader=Alex and market = NY / book = B3
We
have a problem to book D1 since BR2 and BR3 can be applied but have same
priority.
We
can't choose one over the other thus we must apply a less precise/prioritary
rule (if unique at its own precision level).
In
my example, that would lead to apply BR1.
Question
:
----------
If
I define BR1,BR2,BR3 as Drools rules, how can I tell Drools :
-
to execute a rule only if there is no other active rule with same precision that
could be applied ?
-
to look for a unique matching rule with less precision level ?
I
think it's the Agenda responsibility to take this decision, but I don't know
how implement that.
Can
you give some advices on that ?
Thanks
for your lights in advance,
Regards,
Joel
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.
_______________________________________________
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users
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.