14.3. DRL 中的功能
DRL 文件中的功能将语义代码放到规则源文件中,而不是放在 Java 类中。如果重复使用规则的操作(then)部分并且只有参数因每个规则而异,则函数特别有用。在 DRL 文件中的规则之上,您可以声明函数或从帮助程序类导入静态方法作为函数,然后在规则部分操作(then)部分中使用函数。
以下示例演示了在 DRL 文件中声明或导入的功能:
带有规则的功能声明示例(选项 1)
function String hello(String applicantName) {
return "Hello " + applicantName + "!";
}
rule "Using a function"
when
// Empty
then
System.out.println( hello( "James" ) );
end
使用规则导入功能示例(选择 2)
import function my.package.applicant.hello;
rule "Using a function"
when
// Empty
then
System.out.println( hello( "James" ) );
end