0
votes

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.

2

2 Answers

2
votes

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,

0
votes

This is due to the fact that by default the kie-server rest interface works like this :

1) On the client side A related to B (A.b=b)

2) You insert A and then B

3) on server-side, there is going to be for each insert command :

3.1) an A object (inserted into the kie-session) related to a B1 instance (that is not inserted)

3.2) another B2 instance (exact copy content of B1 but not related to A)

Here two solutions :

1) Live with that and use from

2) make your own kie-server extension and it is what I do on my open source project to make life easier with drools . All my customer are using this kind of kie-server woth inserting by reflection all java objects connected to a top object (called request most of the time)

regards