使用java生成drools drl文件

Generate drools drl file by using java

我正在尝试通过以下方法以编程方式使用 java 创建 drools drl 文件。

我可以通过java程序的简单规则创建以下简单规则。

1
2
3
4
5
rule"Demo_testing"
when
    $employee : EmployeeModel( department contains"Accounts" )
then
//

这对我来说很好用,但我需要从列表中获取员工信息。喜欢
$employee : EmployeeModel( 部门包含 "Accounts", role = "manager" ) 来自 $employeeList

我在这里找到了 drools 编译器中可用的描述符列表
但是我不知道我需要使用哪个描述符以及如何定义。?

请任何人帮助我重新爱上这个。提前致谢。

1
2
3
4
5
6
7
8
9
10
11
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);

相关讨论

  • 您没有指定 Drools 的版本,但您链接到 drools 6 文档。是你用的那个版本吗?在 Drools 7 中,我会使用构建器——尽管老实说我永远不会走这条路,因为它似乎是一个非常简单问题的极其缓慢的解决方案。并不是说我知道您要解决什么问题。
  • 嗨,我使用的是 drools 7.36.1,有没有使用 java 创建 drl 文件的示例?
  • 询问示例不是主题。这听起来确实像是一个 XY 问题……您要解决的问题是什么?

您好,感谢您的建议,最后我使用 FromDescr 完成了。

根据我的要求,我可以使用下面的 java 代码生成 Rule drl 文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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);

此代码生成规则如下

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

以上是使用java生成drools drl文件的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>