Search code examples
javadrools

Pass object to DROOLS


I am very new to DROOLS. I am working with Activiti-Explorer and I need DROOLS for decision making. I have created an Instance object of the class and added the data fetched from the API.

LoanApplicant loanApplicant = new LoanApplicant();
loanApplicant.setUserID(obj.getJSONObject("data").getJSONObject("ownership_details").getInt("user_id"));
loanApplicant.setApplicantName(obj.getJSONObject("data").getJSONObject("ownership_details").getString("name"));

Similarly I have set few more fields in to the class object and then I have set it in to Java delegate variable arg0 to Activiti memory

arg0.setVariable("loanApplication", loanApplicant);

And Similarly, as DROOLS decisions outputs are further required, I have created an Output java file recording its decisions.

Output java file: RulesOutput.java: -

private String testValue = null;
public String getTestValue(){
    return this.getTestValue();
}
public void setTestValue(String testValue){
    this.testValue = testValue;
}

Similarly to loanApplicant, I have set this class in to memory:

arg0.setVariable("RulesOutput", new RulesOutput());

Now I am passing these fields from Process BPMN Diagram to Drools Task Work Step.

enter image description here

This is my DROOLS .drl file:

import com.LOS.*;
rule "FLS_1"
when
    $loanApplication : loanApplication(age >= 20 && age < 60)
    $rulesOutput : RulesOutput (isApproved == false || isApproved == true)
then
    rulesOutput.setAgeScore(100);

end

Now the Problem: I am getting Unable to resolve ObjectType even though I have passed all the required objects to DROOLS. Error Log:

06:39:47,288 [http-bio-8480-exec-15] INFO  org.activiti.engine.impl.bpmn.deployer.BpmnDeployer  - Processing resource RuleSet_1.drl
06:39:47,288 [http-bio-8480-exec-15] INFO  org.activiti.engine.impl.bpmn.deployer.BpmnDeployer  - Processing resource LOS_1.bpmn20.xml'
06:39:47,645 [http-bio-8480-exec-15] INFO  org.activiti.engine.impl.rules.RulesDeployer  - Processing resource RuleSet_1.drl
06:39:47,702 [http-bio-8480-exec-15] INFO  org.activiti.engine.impl.rules.RulesDeployer  - Processing resource LOS_1.bpmn20.xml
06:39:47,704 [http-bio-8480-exec-15] ERROR org.drools.compiler.builder.impl.KnowledgeBuilderImpl  - Unable to resolve ObjectType '$loanApplication' : [Rule name='FLS_1']
06:39:47,704 [http-bio-8480-exec-15] ERROR org.drools.compiler.builder.impl.KnowledgeBuilderImpl  - Unable to resolve ObjectType '$RulesOutput' : [Rule name='FLS_1']
06:39:47,704 [http-bio-8480-exec-15] ERROR org.drools.compiler.builder.impl.KnowledgeBuilderImpl  - Rule Compilation error : [Rule name='FLS_1']
[http-bio-8480-exec-15] INFO  org.activiti.engine.impl.bpmn.deployer.BpmnDeployer  - Processing resource RuleSet_1.drl
[http-bio-8480-exec-15] INFO  org.activiti.engine.impl.bpmn.deployer.BpmnDeployer  - Processing resource LOS_1.bpmn20.xml
[http-bio-8480-exec-15] INFO  org.activiti.engine.impl.rules.RulesDeployer  - Processing resource RuleSet_1.drl
[http-bio-8480-exec-15] INFO  org.activiti.engine.impl.rules.RulesDeployer  - Processing resource LOS_1.bpmn20.xml
[http-bio-8480-exec-15] ERROR org.drools.compiler.builder.impl.KnowledgeBuilderImpl  - Unable to resolve ObjectType '$loanApplication' : [Rule name='FLS_1']
[http-bio-8480-exec-15] ERROR org.drools.compiler.builder.impl.KnowledgeBuilderImpl  - Unable to resolve ObjectType '$RulesOutput' : [Rule name='FLS_1']
[http-bio-8480-exec-15] ERROR org.drools.compiler.builder.impl.KnowledgeBuilderImpl  - Rule Compilation error : [Rule name='FLS_1']
com/LOS/Rule_FLS_1842423443.java (7:323) : \RulesOutput cannot be resolved

I am stuck here for almost 2 days. Please any suggestions are welcome.


Solution

  • import com.LOS.*;
    

    If loanApplication and RulesOutput are in package com.LOS.approc.dao, you have to use

    import com.LOS.approc.dao.*; 
    

    although individual imports are usually preferred. (Talking about style: you should stick to conventions and use first letter in uppercase for class names.

    $loanApplication : loanApplication(age >= 20 && age < 60)
    

    If you don't need to refer to this object, a binding ($loanApplication:) can be omitted.

        $rulesOutput : RulesOutput (isApproved == false || isApproved == true)
    then
        rulesOutput.setAgeScore(100); ## missing dollar
    

    If you use $rulesOutput as the name for the binding variable, you have to stick with that name. The $ is part of the (Java) name of the variable, not a macro expansion operator as your remark on trying ${loanApplication} and ${Rules} suggests.