Search code examples
restdrools

Drools - Response XML from multiple rule execution


I have the below two rules:

global Response myResponse;

rule "rule1"
    when
        Loan(processId == "1")
    then
        myResponse.setRuleId("rule1");
        myResponse.setPmtStatus("valid");
end

rule "rule2"
when
    Loan(amount > 1000)
then
    myResponse.setRuleId("rule2");
    myResponse.setPmtStatus("invalid");
end

When I access Drools through REST sending the below request XML, according to the data inserted, both rules should fire.

<batch-execution lookup="testsession">
  <set-global identifier="myResponse" out-identifier="response">
      <com.sample.Response></com.sample.Response>
  </set-global>

  <insert out-identifier = "loan"> 
   <com.sample.Loan>
      <loanId>11112222</loanId>
      <processId>1</processId>
      <amount>2000.00</amount>
   </com.sample.Loan>
  </insert>
 <fire-all-rules/>
</batch-execution>

In my response XML, I would like to receive the consequence information from both rules. For example, I would like to get one response node with ruleID = rule1 and pmtStatus = valid, and another node with ruleId=rule2 and pmtStatus = invalid.

Right now, I am getting the results only from the rule executed last. Please would you advise how I should make my request in order to receive results from all fired rules in my return XML.

Thanks


Solution

  • If the number of rules are limited to two and won't scale in future you can have 2 global response objects created for each of the rule respectively. Or else you can pass a List object by reference to the DRL file.

    rule "rule1"
    when
        Loan(processId == "1")
        $list: ArrayList<Response>
        myResponse:Response()
    then
        myResponse.setRuleId("rule1");
        myResponse.setPmtStatus("valid");
        $list.add(myResponse);
    

    end