Drools Rule Templates

本文详细介绍了Drools5中的新特性——规则模板,它允许用户设置模板并将其与数据合并,使得数据与规则分离,提高了灵活性和功能强大性。通过实例展示了如何使用规则模板实现与决策表相同的功能,以及如何利用模板处理数组字段和可选字段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Drools 5 sees the addition of a new feature - rule templates. Rule templates allow you to set up templates (!) that may then be merged with data. You can think of them as similar to decision tables but far more powerful. With Rule Templates the data is separated from the rule and there are no restrictions on which part of the rule is data-driven. So whilst you can do everything you could do in decision tables you can also do the following:

  • store your data in a database (or any other format)
  • conditionally generate rules based on the values in the data
  • use data for any part of your rules (e.g. condition operator, class name, property name)
  • run different templates over the same data

I find the best way to explain things is via example so over a few posts I will take you through three of the examples in the drools-examples project. The first shows how to achieve exactly the same functionality as a decision table using a rule template. The second example extends the first to include array fields and optional fields. The third gives an example of using a rule template with a different datasource - in this case Plain Old Java Objects (POJOs), and conditional templates.

template header//必须以“template header”开头
age
type
log
//空格,column名称结束
package org.drools.examples.templates;
global java.util.List list;
template "cheesefans"//开始template
rule "Cheese fans_@{row.rowNumber}"
    when
        Person(age == @{age})
        Cheese(type == "@{type}")
    then
        list.add("@{log}");
end
end template//结束template

Line 1: all rule templates start with "template header"
Lines 2-4: following the header is the list of columns in the order they appear in the data. In this case we are calling the first column "age", the second "type" and the third "log".
Lines 5: empty line signifying the end of the column definitions
Lines 6-9: standard rule header text. This is standard rule DRL and will appear at the top of the generated DRL. Put the package statement and any imports and global definitions
Line 10: The "template" keyword signals the start of a rule template. There can be more than one template in a template file. The template should have a unique name.
Lines 11-18: The rule template - see below
Line 20: "end template" signifies the end of the template.

Drools 5 中增加的新功能,解决规则中的数据来源问题,使用数据库中定义的数据替换模版中相应的占位符,批量生成规则,很实用。

现在使用的Drools 5.3.0.Final中只是发布了核心类和接口的API,但具体实现类并没有文档说明,只能自己看源代码。

例如整个decisiontable.jar,这里用到ExternalSpreadsheetCompiler类,编译xls、csv表格文件到drl语句。

 

原文出自http://planet.jboss.org/post/drools_rule_templates ,发表于2008-08-26 


 


转载于:https://www.cnblogs.com/kaqike/archive/2012/02/21/2361454.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值