drl get nested class instance to call setter method

506 views
Skip to first unread message

tarun kumar

unread,
Jun 6, 2017, 3:03:04 AM6/6/17
to Drools Usage
Hi Everyone,
I am newbie to drool usage and i have small query. 
I have map as below which i have created pojo using schemaJson2Pojo java api :
{
"svo": [
      {
        "clause": "Sunanda Pushkar died of poisoning",
        "svoMetadata": {
          "person": "sunanda pushkar",
          "subjectType": "person_coref"
        },
        "objectPOS": "IN VBG;",
        "isRelatedKeySVO": false,
        "subjectPOS": " NNP NNP",
        "subject": "Sunanda Pushkar",
        "confidence": 0.9266232318014763,
        "verb": "died",
        "verbPOS": "VBN",
        "source": "OpenIE",
        "isKeySVO": true,
        "object": "of poisoning;"
      }
}

So, on using schemaJson2Pojo api , following classes are generated :
class RootDoc
{
 private List<Svo> svo = new ArrayList<Svo>();
 
 public List<Svo> getSvo() {
        return svo;
    }


    public void setSvo(List<Svo> svo) {
        this.svo = svo;
    }
}

class Svo
{
  private SvoMetadata svoMetadata;
  
public SvoMetadata getSvoMetadata() {
        return svoMetadata;
    }


    public void setSvoMetadata(SvoMetadata svoMetadata) {
        this.svoMetadata = svoMetadata;
    }
}

class SvoMetadata
{
     private String person;
 
    public void setPerson(String person) {
        this.person = person;
    }

    public SvoMetadata withPerson(String person) {
        this.person = person;
        return this;
    }

}
i forward above base document Pojo as RootDoc to drl and i am trying get an instance of "svoMetadata" as below as wish to update person in drl RHS :

rule in drl is as below:
when 
        $mastermap : EsRootDoc($svolist:svo)
$svo : Svo($svoMetadata2 : svoMetadata) from $svolist
         // $svoMetadata: SvoMetadata($t: $svo.getSvoMetadata())  // this gives me error 
then 
       $svoMetadata.setPerson("Tarun KUmar");  // how do i get SvoMetadata instance to perform setPerson method????

Best Regards
Tarun Kumar

Mario Fusco

unread,
Jun 6, 2017, 4:15:58 AM6/6/17
to Drools Usage
Hi,

maybe I'm missing something, but you already have a declaration for the instance of "svoMetadata".
I believe that this should work.

when 
        $mastermap : EsRootDoc($svolist:svo)
$svo : Svo($svoMetadata : svoMetadata) from $svolist
then
        $svoMetadata.setPerson("Tarun KUmar");

Regards,
Mario

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage+unsubscribe@googlegroups.com.
To post to this group, send email to drools...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/drools-usage/19eb120a-8e97-4f86-9d2f-9a3a8f2ae13a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

tarun kumar

unread,
Jun 6, 2017, 4:46:13 AM6/6/17
to Drools Usage
Hi mario , thanks for reply.
Its giving me below exception :
pool-1-thread-1" Exception executing consequence for rule "tre" in defaultpkg: java.lang.RuntimeException: cannot invoke method: setPerson
at org.drools.core.runtime.rule.impl.DefaultConsequenceExceptionHandler.handleException(DefaultConsequenceExceptionHandler.java:39)
at org.drools.core.common.DefaultAgenda.fireActivation(DefaultAgenda.java:1026)
at org.drools.core.phreak.RuleExecutor.fire(RuleExecutor.java:128)
at org.drools.core.phreak.RuleExecutor.evaluateNetworkAndFire(RuleExecutor.java:68)
at org.drools.core.common.DefaultAgenda.fireNextItem(DefaultAgenda.java:936)
at org.drools.core.common.DefaultAgenda.fireAllRules(DefaultAgenda.java:1183)
at org.drools.core.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:935)
at org.drools.core.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:909)
at org.drools.core.impl.StatefulKnowledgeSessionImpl.fireAllRules(StatefulKnowledgeSessionImpl.java:233)
at controller.RuleRunner.runRules(RuleRunner.java:83)
at com.orkash.EnrichmentService.EnrichmentController.Indexer.applyDrlOnMapWithPojo(Indexer.java:641)
at com.orkash.EnrichmentService.EnrichmentController.Indexer.run(Indexer.java:340)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: cannot invoke method: setPerson
at org.mvel2.optimizers.impl.refl.nodes.MethodAccessor.getValue(MethodAccessor.java:63)
at org.mvel2.optimizers.impl.refl.nodes.VariableAccessor.getValue(VariableAccessor.java:37)
at org.mvel2.ast.ASTNode.getReducedValueAccelerated(ASTNode.java:108)
at org.mvel2.MVELRuntime.execute(MVELRuntime.java:86)
at org.mvel2.compiler.CompiledExpression.getDirectValue(CompiledExpression.java:123)
at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:119)
at org.mvel2.MVEL.executeExpression(MVEL.java:930)
at org.drools.core.base.mvel.MVELConsequence.evaluate(MVELConsequence.java:100)
at org.drools.core.common.DefaultAgenda.fireActivation(DefaultAgenda.java:1016)
... 13 more
Caused by: java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.mvel2.optimizers.impl.refl.nodes.MethodAccessor.getValue(MethodAccessor.java:48)
... 21 more

  I feel reason is just a variable but for getting a class instance i need to as done for 
   Svo as Svo($svoMetadata : svoMetadata) from $svolist
and 
 EsRootDoc($svolist:svo) gives EsRootDoc instance in $mastermap.

So, i feel i further need to do some thing this as :
$svoMetadata: SvoMetadata($t: $svo.getSvoMetadata())

, i even tried this as below :
$svoMetadata :SvoMetadata($svoData: (Svo($svoMetadata2 : svoMetadata) from $svolist))

but still syntactical issue exists.


To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.

Mario Fusco

unread,
Jun 6, 2017, 4:56:32 AM6/6/17
to Drools Usage
The rule's syntax is ok. You're getting that error at runtime, not at rule compile time and the reason why you're getting it is because you have a Svo which has no SvoMetadata. This implies that $svoMetadata declaration is bound to a null value and then the firing of the consequence caused that NPE. To avoid this problem just make sure that $svoMetadata isn't null like in:

when 
        $mastermap : EsRootDoc($svolist:svo)
$svo : Svo($svoMetadata : svoMetadata != null) from $svolist
then
        $svoMetadata.setPerson("Tarun KUmar");

Mario

To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage+unsubscribe@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

tarun kumar

unread,
Jun 6, 2017, 5:10:29 AM6/6/17
to Drools Usage
Thanks a lot Mario , 
This worked for me now. You been really helpful :-)
Reply all
Reply to author
Forward
0 new messages