Sanjay,
Those nested if/else blocks means someone already got your "business
rules" and modelled into a "procedural algorithm". What I mean is, for
example, your first "if" (the external one) is a "shared" condition
for all
your rules and that is the reason the others are nested inside it. This is
the way, in a procedural way, to "code" efficiently, as the external if will
be executed only once.
When using a declarative rules engine, you don't need to think like that.
Go back one step, work with higher level rule specifications, and let the
engine do the "optimization" magic for you. Don't be afraid of stating the
same constraint in more than one rule... the engine will verify and optimize
always possible.
You will probably need to refactor your code a little to work in a more
declarative fashion. Michael gave you good examples.
[]s
Edson
2007/3/26, Anstis, Michael (M.) <manstis1(a)ford.com>:
Hi Sanjay,
To firstly answer your questions (in my opinion):
1. You can't do nested whens in the LHS but you can do nested if / else in
the RHS (although you'd fore go the benefits of a RETE-based inference
engine; but possible if you've got very few rules and facts...). I believe
that there is work to have an "otherwise" statement for rule syntax, how
progressed this is and how it will work I don't know - over to Mark and
team?!?
2. Yes, take a look at the "from" statement (new for 3.1-M1, see
http://wiki.jboss.org/wiki/Wiki.jsp?page=3.1M1ReleaseNotes)
3. Yes, take a look at rule-flow at
http://labs.jboss.com/portal/index.html?ctrl:cmd=render&ctrl:window=d...
(although
I don't know if this is complete if 3.1-M1 - I think it's not). Although I
don't think your rules really need rule-flow; you just need to identify and
isolate the patterns (IF's) that would cause each "Do Something" to be ran
and create a rule for each - this has the benefit of separating each action
into a separate rule making maintenance a whole bunch easier).
Anyway, I'd try some rules like the following examples (the rest are based
upon these):-
Rule "Rule 1"
when
NewItem ( itemStatusCode == null)
then
//do something
end
Rule "Rule 2"
when
$u : User( )
$ni : NewItem ( $isc : itemStatusCode != null )
not Codes ( codes contains $isc ) from
valDao.getValidItemStatusCodesForUser($u)
then
//Do something
end
Rule "Rule 3"
when
$u : User( )
$ni : NewItem( itemStatusCode == ItemVO.DELETED )
Codes ( codes contains $isc ) from
valDao.getValidCrossReferencePrimeCodesForUser($u)
then
//Do something
end
I've posted to the news group so everyone can contribute - don't be shy
;-)
With kind regards,
Mike
------------------------------
*From:* Sanjay Singh - s0singh [mailto:Sanjay.Singh@wal-mart.com]
*Sent:* 26 March 2007 14:24
*To:* Anstis, Michael (M.)
*Subject:* Hi.
Hey Mike ... I am sorry to bother you but you seemed to be pretty good
with rules and i just could not think of anyone else to ask abt it ... I am
sorry if i am taking much of your time .. Here is my problem if you can have
a quick look ..
Look at this code once .. its very easy but it involves tons of if else
blocks conditions ..
if( newItem.getItemStatusCode() == null){
//do something
}
else{
* if( !valDao.isValidItemStatus(
newItem.getItemStatusCode(),user) ){*
* //do something*
* }*
* if( newItem.getItemStatusCode().equals(
ItemVo.DELETED)||getItemStatusCode().equals(ItemVo.INACTIVE) ){*
* if( valDao.isCrossReferencePrime(
newItem.getItemNbr(),user)){*
* //do something*
* }*
* else if(
valDao.isFutrCrossReferencePrimeOrSecond(newItem.getItemNbr(),user) ){*
* //do something*
* }*
* else if( valDao.isComponentLink(
newItem.getItemNbr(),user)){*
* //do something*
* }*
* else if( valDao.hasWhseOnHandsOrOnOrders(
newItem.getItemNbr(),user)&&CountryCode().equals("CA") ){*
* //do something*
* }*
* }*
* else{*
* if(
valDao.isUnlikeAsstParentWithNonActiveChildren(newItemNbr,user) ){*
* //do something;*
* }else if
(isLikeAssortmentBaby(newItemNbr,user)&&!alDao.isLikeAsstParentActive(newItemNbr,
user)){*
* //do something
*
* }else{*
* //do something*
* }*
* }*
* }*
* * }* *
What i am trying to understand is the right way of putting it in drl files
..
1.What are the options for putting if else conditions ? Will i have to
write all the rules anyway and they will be called as well all the time
depending on the criteria ... or is there a way where when my first if check
which was if item number is null failed i should not go for any other rules
.. Is there a way where we could have like "when" inside "when" ..
kinda
looping of conditions so if one does not meet you dont go to the next ..
jsut like in above blocks ? Whats the good way of doing it ?
2.Is it possible to make database calls like from my dao instance in LHS
(condition) .. i know we can do it in RHS and i have tested that as well but
can we do it in LHS as sometimes we need to do that ..like with null checks
for the item status code we also want to check something from the database
like a boolean value and if that meets the criteria we will run the rules
..as u can see above we have basic state check with nulls etc plus we have
some dao calls as well in the condition part and then we //do something
3.Can i call rules within other rules .. so something like in rule1 i want
to call rule2 .. is that possible ?
Can you suggest a decent way of doing these if else blocks .. this is the
core stuff which we do for our rules here .. if i can handle this kind of
conditions i guess i can handle anything out here ...
Thanks for all the help ..
Sanjay
------------------------------
*
*******************************************************************
*** This email and any files transmitted with it are confidential
and intended solely for the individual or entity to whom they are
addressed. If you have received this email in error destroy it
immediately. ******************************************************
**************** Wal-Mart Confidential ****************************
******************************************
*
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users
--
Edson Tirelli
Software Engineer - JBoss Rules Core Developer
Office: +55 11 3124-6000
Mobile: +55 11 9218-4151
JBoss, a division of Red Hat @
www.jboss.com