0

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);
Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120
Thilakar Raj
  • 37
  • 13
  • You don't specify the version of Drools, but you linked to the drools 6 documentation. Is that the version you're using? In Drools 7 I'd use the [builders](https://github.com/kiegroup/drools/tree/master/drools-compiler/src/main/java/org/drools/compiler/rule/builder) -- though honestly I'd never go down this route because it seems like an extremely slow solution to a very simple problem. Not that I know what problem you're trying to solve. – Roddy of the Frozen Peas Jul 10 '20 at 22:15
  • Hi i m using drools 7.36.1, is there any example to create the drl file using java.? – Thilakar Raj Jul 12 '20 at 05:42
  • Asking for examples is not on topic. This really sounds like an XY problem ... _what_ is the problem you're trying to solve? – Roddy of the Frozen Peas Jul 12 '20 at 16:02

1 Answers1

0

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");
Thilakar Raj
  • 37
  • 13