I am working on a drool(drl) POC in which I create java beans at run time using reflection. I have set the below property in the config:
KnowledgeBuilderConfiguration config = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
config.setProperty("drools.dialect.default", "mvel");
//drl sample:
package script.demo
dialect "mvel"
import Employee;
rule "Rule - 1"
when
$emp: Employee($emp_id: emp_id)
then
System.out.println("emp id: "+$emp.emp_id);
end
But I am getting below error:
Field Reader does not exist for declaration '$emp_id' in '$emp_id: emp_id' in the rule 'Rule - 1' : [Rule name='Rule - 1'] @line [I@4cb9v654...............
Request help, how to solve this?
The error is trying to say that your Employee class has no field or method that it can access to map to the $emp_id
variable you've declared.
It looks for either a public method prefixed with 'get', or a public variable named as-is.
An Employee class definition that looks like either of the following will resolve the error.
Option 1: declare a public variable emp_id
.
public class Employee {
public String emp_id;
}
Option 2: declare a public method called getEmp_id.
public class Employee {
public String getEmp_id() { return "..."; }
}
Either of these options will resolve the issue and allow you to bind the $emp_id
variable in your rule:
Employee( $emp_id: emp_id )
(I would recommend, of course, renaming to empId
and then implementing either such a variable or a getEmpId
method, since that follows Java naming conventions. You can still call your declared variable in the drools $emp_id
of course.)