具体语法如下:
function void/Object functionName(Type arg…) {
/*函数体的业务代码*/
}
示例代码:
import java.util.List;
import java.util.ArrayList;
/*
*一个测试函数
*用来向Customer对象当中添加指定数量的Order对象的函数
*/
function void setOrder(Customer customer,int orderSize) {
List ls=new ArrayList();
for(int i=0;i<orderSize;i++){
Order order=new Order();
ls.add(order);
}
customer.setOrders(ls);
}
调用方式:
1.函数写在规则文件里,规则文件直接调用
/*
测试规则
*/
rule “rule1”
when
$customer :Customer();
then
setOrder($customer,5);
System.out.println(“rule 1 customer has order
size:”+$customer.getOrders().size());
end
/*
测试规则
*/
rule “rule2”
when
$customer :Customer();
then
setOrder($customer,10);
System.out.println(“rule 2 customer has order size:”+$customer.getOrders().size());
end
2.使用java编写函数,规则文件调用(需要引入相关类和函数)
java代码:
package test;
public class RuleTools {
public static void printInfo(String name){
System.out.println(“your name is :”+name);
}
}
规则文件代码:
package test
import function test.RuleTools.printInfo; //引入函数所在
/*
测试规则
*/
rule “rule1”
when
eval(true);
then
System.out.println(“hello word”);
end
转载请注明来源网站:blog.ytso.com谢谢!
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/14846.html