droolsdrools-guvnorkie-workbenchdrools-kie-workbench

Iterating List of Class Objects Issue in Drools Rule Engine


I learning Drools using Kie Workbench. my input will be name inside opportunityProduct class and i am expecting the output in Ip inside Opportunity class . after rule engine execution, i am getting only null value in output.

Pojo Struture :

Opportunity.Java :
    private java.lang.Boolean result;
    private java.lang.String ip;
    private java.util.List<com.sample2.sampledemo2.OpportunityProduct> productlist;

OpportunityProduct.Java
    private java.lang.String name;

Input:

{
   "commands":[
      {
         "insert":{
            "out-identifier":"com.sample2.sampledemo2.Opportunity",
            "return-object":true,
            "object":{
               "com.sample2.sampledemo2.Opportunity":{
                  "productlist":[
                     {
                        "name":"abc"
                     }
                  ]
               }
            }
         }
      },
      {
         "fire-all-rules":""
      }
   ]
}

Output:

{
   "type":"SUCCESS",
   "msg":"Container abcdef successfully called.",
   "result":{
      "execution-results":{
         "results":[
            {
               "key":"",
               "value":0
            },
            {
               "key":"com.sample2.sampledemo2.Opportunity",
               "value":{
                  "com..sample2.sampledemo2.Opportunity":{
                     "result":null,
                     "ip":null,
                     "productlist":[
                        {
                           "name":"abc"
                        }
                     ]
                  }
               }
            }
         ],
         "facts":[
            {
               "key":"com.sample2.sampledemo2.Opportunity",
               "value":{
                  "org.drools.core.common.DefaultFactHandle":{
                     "external-form":"0:2:1366747666:1366747666:2:DEFAULT:NON_TRAIT:com.sample2.sampledemo2.Opportunity"
                  }
               }
            }
         ]
      }
   }
}

Decision Table :

NAME                  CONDITION                               ACTION

           o:Opportunity(pd: productlist)
             OpportunityProduct
      (name == $param , this memberOf pd)      o.setIp($param);

Origination  "abc"                                  "IPP"

Drl :

rule "Origination"
    when
        o:Opportunity(pd: productlist)
        (OpportunityProduct(name == "abc" , this memberOf pd))
    then
        o.setIp("IPP");
end

I could not able to identify whether my input is wrong or condition in decision table is wrong.


Solution

  • In your input, you are sending a Opportunity object to be inserted, but you are never inserting a OpportunityProduct fact. Your rule needs both facts in order to fire.

    Remember that, in Drools, you can only reason about facts that have been inserted into your session and that nested objects inside a fact are not facts themselves.

    If you don't plan to insert OpportunityProduct as independent facts, one thing you can do is to use the from conditional element to reason about data that is not a fact:

    rule "Origination"
        when
            o:Opportunity(pd: productlist)
            OpportunityProduct(name == "abc") from pd
        then
            o.setIp("IPP");
    end
    

    Hope it helps,