基础语义模块
基础语义模块提供了一种在XML种构建RuleSet(规则集)的语言,单独地,基础语义模块不能写一个完整而有效地规则文件,它必须和别的语义模块结合,这些模块包括Java语义模块、Groovy语义模块、Pythos语义模块或者是一个自定义地,指定域地语义模块。
语法指南
rule-set 元素
每个DRL文件必须有一个rule-set元素,而且名字在规则基础和定义绑定命名空间是唯一的。
attribute | optional? | description |
name | no | This string must uniquely identify the rule-set in the rule base |
<rule-set name="HelloWorld"
xmlns="http://drools.org/rules"
xmlns:java="http://drools.org/semantics/java"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="http://drools.org/rules rules.xsd
http://drools.org/semantics/java java.xsd">
....
....
<rule-set>
Import元素
对于任何想引用一个Class的元素,通过Imports来指定。
元素的实体可以是一个全路径引用:
<import>java.util.HashMap</import>
或者是一个动态的import:
<import>java.util.*</import>
application-data 元素
application-data元素允许对象不需要将它们断言进入Working Memory,而对conditions和consequences是可见的。
如果没有在{{rule-set}}里定义,则Application data就不能被设置。Application data设置:
workingMemory.setApplicationData("amount", new Integer(3));
在rule-set里定义一个 application data:
<application-data identifier="amount">java.lang.Integer</application-data>
Rule元素
每个rule-set必须包含至少一个rule元素。在这个rule-set里,每个rule元素必须具有唯一的名字,也可以包含可选的salience和no-loop属性。如果duration元素被指定,则no-loop就会被忽略。
attribute | optional? | description |
name | no | This string must uniquely identify the rule in the rule-set. |
salience | yes | Default to 0. Must be numeric, integral, signed. Must be numeric, non-float values, can be negative. Example: salience="-42", salience="2". |
no-loop | yes | Default to "false". Must be boolean. Example: no-loop="true", no-loop="false". |
xor-group | yes | Allow only a single rule within a group to fire, all other rules in that group are then removed from the agenda. A rule can be in only a single xor group. |
<rule name="Hello World" salience="10" no-loop="true" xor-group="group1">
...
...
</rule>
Parameter元素
每个rule元素必须包含至少一个parameter元素,在此rule元素的范围里,parameter元素必须为其identifer属性使用一个唯一的名字。单独的,parameter元素不能做任何事。它需要一个级连的元素来声明一个对象类型,象class,class-field,和semaphore object。
attribute | optional? | description |
identifier | no | This string must uniquely identify the parameter in the rule |
<parameter identifier="goodbye">
<class>java.lang.String</class>
</parameter>
class元素
class元素是用在parameter元素内部的一种对象类型的实现。它没有属性,它的内容既可以是一个全路径类,或者是一个从预先导入的包中的类的名字。
attribute | optional? | description |
<class>java.util.HashMap</class>
<class>HashMap</class>
class-field元素
class-field元素是用在parameter元素内部的一种对象类型的实现。它不仅仅限制要给出class参数,而且规定需要为给定的字段(field)指定一个值(value),这个字段别限定是字符串类型,字段的值也是一个静态字符串。
attribute | optional? | description |
field | no | The name of the field of the type String, written to JavaBean specifications with setters and getters |
value | no | Static value constraint for the given field |
<class-field field="name" value="A">State</class-field>
这和下面代码段是类似的:
<parameter identifier="state">
<class>State</class>
</parameter>
<java:condition>state.getName().equals("A")<java:condition>
Semaphore元素
Semaphore元素用在parameter元素内部的一种特别对象类型的实现。它提供了在规则里使用的全局的命名变量,在那里,identifier就是这个全局名字。这对于控制执行是非常有用的。
attribute | optional? | description |
type | no | The type of semaphore |
声明一个没有初始化值的semaphore:
workingMemory.assertObject( new StringSemaphore( "state" ) );
声明一个有初始化值的semaphore:
workingMemory.assertObject( new StringSemaphore( "state", "START" ) );
在一个参数里指定一个semaphore:
<parameter identifier="state">
<semaphore type="String" />
</parameter>
支持的semphore为:
- CharSemaphore
- DoubleSemaphore
- FloatSemaphore
- IntegerSemaphore
- ListSemaphore
- LongSemaphore
- MapSemaphore
- SetSemaphore
- ShortSemaphore
- StringSemaphore
duration元素
duration元素能够使Temporal Rules,如果在一个给定的时间内,temporal Rules仍旧是true。
attribute | optional? | description |
seconds | yes | Unit of time |
minutes | yes | Unit of time |
hours | yes | Unit of time |
days | yes | Unit of time |
<duration seconds="2" />