0

I have xml similar to below:

<Beans>
        <Bean>
                <Type>D</Type> <!--type can be D, B, C-->  
           <Name>Dental</Name> 
           <Transaction>121</Transaction>  
           <Amount></Amount> 
        </Bean>
        <Bean>
           <Type>D</Type> <!--type can be D, B, C-->
           <Name>Dental</Name>
           <Transaction>12312</Transaction>
           <Amount>123.45</Amount>
         </Bean>
</Beans>

Business rules for this xml: For each bean, if type is D 1:Name shouldn't be null 2:Amount and transaction shouldn't be null 3: Amount and transaction should match existing values for the same transaction in database table. 4: If type is other than D, then there are different rules.

How do i represent this in Drools rule language.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
pavanlapr
  • 279
  • 1
  • 5
  • 15

1 Answers1

0

My suggestion is to use one rule for each constraint:

rule "D Type - Name shouldnt be null"
when
    Bean(type == "D", name == null)
then
    //do whatever you want
end

... (you can figure out the other null checking rules)

rule "D Type - Amount must match DB value"
when
    $b: Bean(type == "D", amount != null)
    Double(doubleValue != $b.amount) from someGlobalService.getAmmount($b) 
then
    //do whatever you want
end

...

In the second rule I suggest to use a global service to retrieve the desired information from the DB and even implement some kind of cache. Another possibility could be to have the values from the db prepopulated in the ksession either as facts or globals.

Best Regards,

Esteban Aliverti
  • 6,259
  • 2
  • 19
  • 31
  • Thanks Esteban, I was thinking in the same lines.. Now, I will go ahead and code the rules. – pavanlapr Nov 16 '12 at 15:38
  • How to ensure rule is executed once.. From the above xml.. if first rule is executed I want to add error to errorlist and want to execute second rule and if it satisfies add another error to error list. What is happening is after two rules are getting executed again first rule is getting executed and is going into infinite loop.. – pavanlapr Nov 19 '12 at 18:04
  • I just wrote a post about infinite loops in Drools a couple of days ago: [about-drools-and-infinite-execution-loops](http://ilesteban.wordpress.com/2012/11/16/about-drools-and-infinite-execution-loops/). In the post I try to explain why are these loops present in Drools and different techniques we have to deal with them. – Esteban Aliverti Nov 21 '12 at 10:22
  • There is one excellent post that i have read posted in some blog for which the link is [drools-and-infinite-execution-loops](http://www.plugtree.com/about-drools-and-infinite-execution-loops/). I loved it a lot - very informative and explains in a pretty simple manner. – Shiva Komuravelly Dec 04 '12 at 12:00
  • 1
    Not all are allowed to access the link that you have provided @EstebanAliverti - So I have posted another link – Shiva Komuravelly Dec 04 '12 at 12:02