Generate drools drl file by using java

安稳与你 提交于 2021-02-11 12:49:47

问题


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:


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");


来源:https://stackoverflow.com/questions/62831913/generate-drools-drl-file-by-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!