Generate drools drl file by using java

1.4k Views Asked by At

I m trying to create the drools drl file using java programmatically by following method.

I can able to create the below simple rules by simple rule by java program.

rule "Demo_testing"
when
    $employee : EmployeeModel( department contains "Accounts" )
then
//

And this in one working fine for me, but i need get the employee information from list. Like $employee : EmployeeModel( department contains "Accounts", role = "manager" ) from $employeeList

I found the list on descriptor available in drools compiler here But i don't know which descriptor i needs to use and how to define.?

Please any one help me to relove this one. Thanks in advance.

PatternDescr employeePatternDescr=new PatternDescr();
employeePatternDescr.setIdentifier("$employee");
employeePatternDescr.setObjectType("EmployeeModel");
RelationalExprDescr relationalExprDescr = null;
constraintDescr.setExpression("department");
ExprConstraintDescr constraintDescr2=new ExprConstraintDescr();
constraintDescr2.setExpression("Accounts" );
relationalExprDescr = new RelationalExprDescr("contains" ,false, null, constraintDescr, constraintDescr2);
employeePatternDescr.addConstraint(relationalExprDescr);
andDescr.addDescr(employeePatternDescr);
ruleDescr.setLhs(andDescr);
1

There are 1 best solutions below

0
Thilakar Raj On BEST ANSWER

Hi thanks for your suggestions finally i done by using FromDescr.

As per my requirement i can genrate Rule drl file by using below java code.

PatternDescr employeePatternDescr=new PatternDescr();
employeePatternDescr.setIdentifier("$employee");
employeePatternDescr.setObjectType("EmployeeModel");
**FromDescr fromDescr = new FromDescr();
fromDescr.setDataSource( new MVELExprDescr( "$employeeList") );
employeePatternDescr.setSource(fromDescr);**
RelationalExprDescr relationalExprDescr = null;
constraintDescr.setExpression("department");
ExprConstraintDescr constraintDescr2=new ExprConstraintDescr();
constraintDescr2.setExpression("Accounts" );
relationalExprDescr = new RelationalExprDescr("contains" ,false, null, 
constraintDescr, constraintDescr2);
employeePatternDescr.addConstraint(relationalExprDescr);
andDescr.addDescr(employeePatternDescr);
ruleDescr.setLhs(andDescr);

This code generate the rule as follow

rule "Demo_testing"
when
    $employee : EmployeeModel( department contains "Accounts" ) from $employeeList
then
System.out.println("Rule executed");