java drools material

http://blog.csdn.net/joeyshi/article/details/4153339

http://www.ibm.com/developerworks/cn/java/j-drools/

http://www.ibm.com/developerworks/cn/java/j-drools5/

https://access.redhat.com/site/documentation/en-US/JBoss_Enterprise_SOA_Platform/4.2/html/JBoss_Rules_Manual/sect-JBoss_Rules_Reference_Manual-The_Drools_Rule_Engine.html

近期研究一下 

1.salience, 这个属性在某些情况下可以用来规定规则的执行顺序(就不用再画规则流图来确定顺序了)
功能:设置规制执行的优先级
值:数字(数字越大执行优先级越高)
示例:
rule "rule1" 
  salience 
  when 
eval(true) 
  then  
System.out.println("rule1");
end 
 
2.no-loop
功能:控制已经执行的规则条件再次满足是否再次执行
值:true/false
示例:
rule "rule1" 
  no-loop true 
  when 
  $customer:Customer(name=="张三") 
  then  
   update($customer); 
  System.out.println("customer name:"+$customer.getName()); 
  End 
 
3.date-effective
功能:设置规则的生效时间,当系统时间>=date-effective后才会触发
值:日期默认格式为dd-MMM-yyyy
可以设置其它时间格式如yyyy-MM-dd,需在代码设置系统时间格式System.setProperty("drools.dateformat", "yyyy-MM-dd");
 
示例:
rule "rule1" 
  date-effective"2009-09-25" 
  when 
  eval(true); 
  then  
  System.out.println("rule1 is execution!"); 
  end
 
4.date-expires
功能:设置规则的过期时间,当系统时间<=date-expires后才会触发
值:日期默认格式为dd-MMM-yyyy
可以设置其它时间格式如yyyy-MM-dd,需在代码设置系统时间格式System.setProperty("drools.dateformat", "yyyy-MM-dd");
 
示例:
rule "rule1" 
  date-expires"2009-09-27" 
  when 
  eval(true); 
  then  
  System.out.println("rule1 is execution!");  

  end 

 
5.enabled
功能:设置规制是否可用
值:true/false
 
示例:
rule "rule1" 
  enabled false 
  when 
   eval(true); 
  then  
  System.out.println("rule1 is execution!"); 
  end
 
6.dialect
功能:规则当中要使用的语言类型 
值:Java/mevl(默认为java)
 
示例:
rule "rule3"
dialect "mvel"
when
   $app:Applicant(age == 24);
then
   System.out.println("rule3----" + $app.name);
end
 
7.duration
功能:设定时间之后在另外一个线程里触发 ,规则定时,duration 3000   3秒后执行规则
值:一个长整型,单位是毫秒
 
示例: 
rule "rule1" 
  duration 3000 
  when 
  eval(true) 
  then  
  System.out.println("rule thread id:"+Thread.currentThread().getId()); 
  end 
 
8.activation-group
功能:若干个规则划分成一个组
值:分组名称
 
示例:
rule "rule2"
 activation-group "test"
 salience 10 
 when
  eval(true)
  then
    System.out.println("rule2 execute");
 end
 
 rule "rule1"
 activation-group "test"
 salience 9
 when
 eval(true)
 then
 System.out.println("rule1 execute");
end 
 
note:
如果同一组规则,谁的salience高就执行谁,没有则按顺序执行最后同组最后那个规则
 
9.agenda-group
功能:Agenda Group 是用来在 Agenda的基础之上,对现在的规则进行再次分组.
Agenda Group 得到 Focus(焦点),这样位于该 Agenda Group当中的规则才会
触发执行,否则将不执行。
值:一个字符串
 
示例:
rule "rule1"
    agenda-group "001"
when
eval(true)
then 
System.out.println("rule1 execute");
end
 
rule "rule2"
    agenda-group "002"
when
eval(true)
then 
System.out.println("rule2 execute");
end
 
10:auto-focus
功能:跟agenda-group一起使用,设置该规则是否可以自动独取 Focus,如果该属性设置为 true,那么在引擎执行时,就不需要
显示的为某个Agenda Group 设置 Focus,否则需要。
值:true/false
 
示例:
rule "rule1"
    agenda-group "001"
    auto-focus true
when
eval(true)
then 
System.out.println("rule1 execute");
end
 
rule "rule2"
    agenda-group "002"
    auto-focus true
when
eval(true)
then 
System.out.println("rule2 execute");

end

规则的条件部分,即LHS部分:

when:规则条件开始。条件可以单个,也可以多个,多个条件一次排列,比如

 when

         eval(true)

         $customer:Customer()

         $message:Message(status==0)

上述罗列了三个条件,当前规则只有在这三个条件都匹配的时候才会执行RHS部分,三个条件中第一个

eval(true):是一个默认的api,true 无条件执行,类似于 while(true)

条件可以有组合,比如:

Message(status==0 || (status > 1 && status <=100))

RHS中对Fact对象private属性的操作必须使用getter和setter方法,而RHS中则必须要直接用.的方法去使用,比如

  $order:Order(name=="qu")
  $message:Message(status==0 && orders contains $order && $order.name=="qu")

特别的是,如果条件全部是 &&关系,可以使用“,”来替代,但是两者不能混用

contains:对比是否包含操作,操作的被包含目标可以是一个复杂对象也可以是一个简单的值。 

Drools提供了十二中类型比较操作符: 

>  >=  <  <=  ==  !=  contains / not contains / memberOf / not memberOf /matches/ not matches

not contains:与contains相反。

memberOf:判断某个Fact属性值是否在某个集合中,与contains不同的是他被比较的对象是一个集合,而contains被比较的对象是单个值或者对象。

not memberOf:正好相反。

matches:正则表达式匹配,与java不同的是,不用考虑'/'的转义问题

not matches:正好相反。

结果部分也有drools提供的方法:

insert:往当前workingMemory中插入一个新的Fact对象,会触发规则的再次执行,除非使用no-loop限定;

update:更新

modify:修改,与update语法不同,结果都是更新操作

retract:删除

RHS部分除了调用Drools提供的api和Fact对象的方法,也可以调用规则文件中定义的方法,方法的定义使用 function 关键字

functionvoid console {

   System.out.println();

   StringUtils.getId();// 调用外部静态方法,StringUtils必须使用import导入,getId()必须是静态方法

}

Drools还有一个可以定义类的关键字:

declare可以再规则文件中定义一个class,使用起来跟普通java对象相似,你可以在RHS部分中new一个并且使用getter和setter方法去操作其属性。

declare Address
 @author(quzishen) // 元数据,仅用于描述信息

 @createTime(2011-1-24)
 city : String @maxLengh(100)
 postno : int
end

上述的'@'是什么呢?是元数据定义,用于描述数据的数据~,没什么执行含义

你可以在RHS部分中使用Address address = new Address()的方法来定义一个对象。

原文地址:https://www.cnblogs.com/vigarbuaa/p/3029955.html