1、背景
此处介绍一下drools
中then
部分的写法,以及一些内置的方法,比如insert/delete/modify
等等。同时也介绍一下rule
的继承,和在when
中实现if else if
等操作。
2、支持的方法
drools提供了一些内置的方法,这些方法会修改drools的工作内存
中Fact
对象的值。从而会导致重新进行模式匹配。
2.1 insert 插入对象到工作内存中
insert
是向工作内存中插入对象,同时会导致重新进行规则的模式匹配。同时当规则不满足时,不会自动删除。
2.1.1 需求
当发生火灾Fire
时,向工作内存中插入一个Alarm
对象,告警发生后,删除工作内存中的Fire
对象,然后检测Alarm
对象是否还存在。
2.1.2 drl文件编写
package rules
import com.huan.drools.insertmethod.Fire
import com.huan.drools.insertmethod.Alarm
rule "insert_发生火灾时,往工作内存中插入告警对象"
when
$fire: Fire()
then
System.out.println("1、发生火灾时,往工作内存中插入告警对象");
insert(new Alarm($fire));
end
rule "insert_当规则内存中存在告警对象,进行告警,然后删除火灾对象"
when
$fire: Fire()
$alarm: Alarm( fire == $fire )
then
System.out.println("2、进行告警,然后删除对应的火灾对象");
end
rule "insert_检测告警对象是否还是存在-01"
when
Alarm()
then
System.out.println("3、insert 插入的告警对象还存在");
// 删除工作内存中的Fire对象
delete($fire);
end
rule "insert_检测告警对象不存在"
when
not Alarm()
then
System.out.println("3、insert 插入的告警对象不存在");
end
此处使用的是 insert
进行插入
2.1.3 部分java代码编写
// 将火灾对象插入到工作内存中
kieSession.insert(new Fire());
// 只触发规则名称以 insert_ 开头的规则
kieSession.fireAllRules(new RuleNameStartsWithAgendaFilter("insert_"));
2.1.4 运行结果
1、发生火灾时,往工作内存中插入告警对象
2、进行告警,然后删除对应的火灾对象
3、insert 插入的告警对象还存在
2.1.5 结论
insert
可以向工作内存中插入Fact
对象。insert
方法调用后,会导致模式的重新匹配,导致之前不会执行的规则,重新执行。insert
方法插入到工作内存的对象,在规则不成立时,不会自动删除,需要手动删除,注意和insertLogical
的区别
2.2 insertLogical 插入对象到工作内存中
insert
是向工作内存中插入对象,同时会导致重新进行规则的模式匹配。同时当规则不满足时,会自动删除。
2.2.1 需求
当发生火灾Fire
时,向工作内存中插入一个Alarm
对象,告警发生后,删除工作内存中的Fire
对象,然后检测Alarm
对象是否还存在。
2.2.2 drl文件编写
package rules
import com.huan.drools.Fire
import com.huan.drools.Alarm
rule "insertLogical_发生火灾时,往工作内存中插入告警对象"
when
$fire: Fire()
then
System.out.println("1、发生火灾时,往工作内存中插入告警对象");
insertLogical(new Alarm($fire));
end
rule "insertLogical_当规则内存中存在告警对象,进行告警,然后删除火灾对象"
when
$fire: Fire()
$alarm: Alarm( fire == $fire )
then
System.out.println("2、进行告警,然后删除对应的火灾对象");
delete($fire);
end
rule "insertLogical_检测告警对象是否还是存在-01"
when
Alarm()
then
System.out.println("3、insertLogical 插入的告警对象还存在");
end
rule "insertLogical_检测告警对象不存在"
when
not Alarm()
then
System.out.println("3、insertLogical 插入的告警对象不存在");
end
此处使用的是insertLogical
插入
2.2.3 部分java代码编写
kieSession.insert(new Fire());
kieSession.fireAllRules(new RuleNameStartsWithAgendaFilter("insertLogical_"));
2.2.4 运行结果
1、发生火灾时,往工作内存中插入告警对象
2、进行告警,然后删除对应的火灾对象
3、insertLogical 插入的告警对象不存在
2.2.5 结论
insertLogical
可以向工作内存中插入Fact
对象。insertLogical
方法调用后,会导致模式的重新匹配,导致之前不会执行的规则,重新执行。insertLogical
方法插入到工作内存的对象,在规则不成立时,会自动删除注意和insert
的区别
2.3、update 更新工作内存中的对象
update:
使用它来指定要更新的字段和整个相关Fact,并将更改通知 Drools 引擎。 Fact发生更改后,您必须在更改可能受更新值影响的另一个事实之前调用 update。 为避免此添加步骤,请改用 modify 方法。
2.3.1 需求
规则一: 当工作内存中存在火灾对象Fire
,并且名字name为空时触发规则,同时在设置火灾的名字为大火灾
。
规则二: 当火灾存在名字时,输出火灾名字
2.3.2 drl 文件编写
package rules
import com.huan.drools.Fire
import com.huan.drools.Alarm
rule "update_当存在火灾对象时,设置一个火灾的名字"
when
$fire: Fire(name == null)
then
System.out.println("1、设置火灾名字");
$fire.setName("大火灾");
update($fire)
end
rule "update_当火灾对象存在名字时触发"
when
$fire: Fire(name != null)
then
System.out.println("2、火灾对象的名字为: " + $fire.getName());
end
2.3.3 部分java文件编写
kieSession.insert(new Fire());
kieSession.fireAllRules(new RuleNameStartsWithAgendaFilter("update_"));
2.3.4 运行结果
1、设置火灾名字
2、火灾对象的名字为: 大火灾
2.3.4 结论
update
会导致模式的重新匹配。update
会修改工作对象内存中的值。
2.4、modify 更新工作内存中的对象
modify:
使用它来指定要为Fact对象修改的字段并将更改通知 Drools 引擎。 此方法提供了一种结构化的事实更新方法。 它将更新操作与 setter 调用相结合以更改对象字段。
2.4.1 需求
规则一: 当工作内存中存在火灾对象Fire
,并且名字name为空时触发规则,同时在设置火灾的名字为大火灾
。
规则二: 当火灾存在名字时,输出火灾名字
2.4.2 drl 文件编写
package rules
import com.huan.drools.Fire
import com.huan.drools.Alarm
rule "modify_当存在火灾对象时,设置一个火灾的名字"
when
$fire: Fire(name == null)
then
System.out.println("1、设置火灾名字");
modify($fire){
setName("大火灾")
}
end
rule "modify_当火灾对象存在名字时触发"
when
$fire: Fire(name != null)
then
System.out.println("2、火灾对象的名字为: " + $fire.getName());
end
2.4.3 部分java文件编写
kieSession.insert(new Fire());
kieSession.fireAllRules(new RuleNameStartsWithAgendaFilter("modify_"));
2.4.4 运行结果
1、设置火灾名字
2、火灾对象的名字为: 大火灾
2.4.5 结论
modify
会导致模式的重新匹配。modify
会修改工作对象内存中的值。- 一般情况下使用
modify
,不要使用update
。
2.5 delete 删除工作内存中的对象
用法:delete(<object>)
retract
也是和delete
一样的效果,但是推荐使用delete
。
3、drools变量的简单使用
package rules
rule "drools_变量的使用"
when
eval(true)
then
System.out.println("Match激活的当前触发规则: " + drools.getMatch());
System.out.println("当前触发规则的名字: " + drools.getRule().getName());
// System.out.println("终止规则执行fireUntilHalt(): " + drools.getKieRuntime().halt());
// System.out.println("激活AgendaGroup组: " + drools.getKieRuntime().getAgenda().getAgendaGroup( "CleanUp" ).setFocus());
System.out.println("获取所有全局变量: " + drools.getKieRuntime().getGlobals());
// System.out.println("设置全局变量:" + drools.getKieRuntime().setGlobal("username","huan"); );
// System.out.println("获取查询结果:" + drools.getKieRuntime().getQueryResults());
end
4、规则的继承
4.1 需求
规则一: 如果用户(customer
)的年龄(age
)大于60岁,则打0.9
折。
规则二: 在规则一的基础上,如果用户有车(car
),则可以免费停车(freeParking
)。
4.2 drl文件编写
package rules
import com.huan.drools.Customer
import com.huan.drools.Car
rule "rule_extends_规则一"
when
$c: Customer(age > 60)
then
modify($c){
setDiscount(0.9)
}
System.out.println("触发规则一:用户年龄>60岁,打0.9折");
end
// 规则二继承规则一的条件
rule "rule_extends_规则二" extends "rule_extends_规则一"
when
$car: Car()
then
modify($car){
setFreeParking(true)
}
System.out.println("触发规则二:用户有车,免费停车");
end
此处rule_extends_规则二
继承了rule_extends_规则一
,所以规则一的条件也继承了。
4.3 部分java代码
Car car = new Car();
Customer customer = new Customer();
customer.setAge(65);
kieSession.insert(customer);
kieSession.insert(car);
kieSession.fireAllRules(new RuleNameStartsWithAgendaFilter("rule_extends_"));
客户有车,并且年龄是65岁,满足上方的规则一和规则二
4.4 运行结果
触发规则一:用户年龄>60岁,打0.9折
触发规则二:用户有车,免费停车
4.5 结论
可以看到在rule
上使用extends
关键字,可以实现规则的继承。
5、使用do[...]语法重写上方继承的例子
5.1 需求
只要用户大于60岁,直接打0.9折,如果还有车,则可以免费停车。
5.2 drl文件编写
package rules
import com.huan.drools.Customer
import com.huan.drools.Car
rule "命名结果_rule"
when
$c: Customer(age > 60)
do[giveDiscount] // 当上方的条件成立时执行 then [giveDiscount]
$car: Car() // 此条件成立时,执行默认的 then
then
modify($car){
setFreeParking(true)
};
System.out.println("用户有车,免费停车");
then [giveDiscount]
modify($c){
setDiscount(0.9)
};
System.out.println("用户年龄>60岁,打0.9折");
end
解释:
见上方的规则文件里的注视
5.3 部分java代码编写
Car car = new Car();
Customer customer = new Customer();
customer.setAge(65);
kieSession.insert(customer);
kieSession.insert(car);
kieSession.fireAllRules(new RuleNameStartsWithAgendaFilter("命名结果_"));
5.4 运行结果
用户年龄>60岁,打0.9折
用户有车,免费停车
也实现了需求
5.5 结论
通过when
中使用 do[名字]
然后 then
then 名字
也可以实现。
6、实现 if else if 的效果
https://www.36dianping.com/vs/d75w.html
https://www.36dianping.com/vs/d75x.html
https://www.36dianping.com/vs/d75y.html
https://www.36dianping.com/vs/d75z.html
https://www.36dianping.com/vs/d750.html
https://www.36dianping.com/vs/d751.html
https://www.36dianping.com/vs/d752.html
https://www.36dianping.com/vs/d753.html
https://www.36dianping.com/vs/d754.html
https://www.36dianping.com/vs/d755.html
https://www.36dianping.com/vs/d756.html
https://www.36dianping.com/vs/d757.html
https://www.36dianping.com/vs/d758.html
https://www.36dianping.com/vs/d759.html
https://www.36dianping.com/vs/d76a.html
https://www.36dianping.com/vs/d76b.html
https://www.36dianping.com/vs/d76c.html
https://www.36dianping.com/vs/d76d.html
https://www.36dianping.com/vs/d76e.html
https://www.36dianping.com/vs/d76f.html
https://www.36dianping.com/vs/d76g.html
https://www.36dianping.com/vs/d76h.html
https://www.36dianping.com/vs/d76i.html
https://www.36dianping.com/vs/d76j.html
https://www.36dianping.com/vs/d76k.html
https://www.36dianping.com/vs/d76l.html
https://www.36dianping.com/vs/d76m.html
https://www.36dianping.com/vs/d76n.html
https://www.36dianping.com/vs/d76o.html
https://www.36dianping.com/vs/d76p.html
https://www.36dianping.com/vs/d76q.html
https://www.36dianping.com/vs/d76r.html
https://www.36dianping.com/vs/d76s.html
https://www.36dianping.com/vs/d76t.html
https://www.36dianping.com/vs/d76u.html
https://www.36dianping.com/vs/d76v.html
https://www.36dianping.com/vs/d76w.html
https://www.36dianping.com/vs/d76x.html
https://www.36dianping.com/vs/d76y.html
https://www.36dianping.com/vs/d76z.html
https://www.36dianping.com/vs/d760.html
https://www.36dianping.com/vs/d761.html
https://www.36dianping.com/vs/d762.html
https://www.36dianping.com/vs/d763.html
https://www.36dianping.com/vs/d764.html
https://www.36dianping.com/vs/d765.html
https://www.36dianping.com/vs/d766.html
https://www.36dianping.com/vs/d767.html
https://www.36dianping.com/vs/d768.html
https://www.36dianping.com/vs/d769.html
https://www.36dianping.com/vs/d77a.html
https://www.36dianping.com/vs/d77b.html
https://www.36dianping.com/vs/d77c.html
https://www.36dianping.com/vs/d77d.html
https://www.36dianping.com/vs/d77e.html
https://www.36dianping.com/vs/d77f.html
https://www.36dianping.com/vs/d77g.html
https://www.36dianping.com/vs/d77h.html
https://www.36dianping.com/vs/d77i.html
https://www.36dianping.com/vs/d77j.html
https://www.36dianping.com/vs/d77k.html
https://www.36dianping.com/vs/d77l.html
https://www.36dianping.com/vs/d77m.html
https://www.36dianping.com/vs/d77n.html
https://www.36dianping.com/vs/d77o.html
https://www.36dianping.com/vs/d77p.html
https://www.36dianping.com/vs/d77q.html
https://www.36dianping.com/vs/d77r.html
https://www.36dianping.com/vs/d77s.html
https://www.36dianping.com/vs/d77t.html
https://www.36dianping.com/vs/d77u.html
https://www.36dianping.com/vs/d77v.html
https://www.36dianping.com/vs/d77w.html
https://www.36dianping.com/vs/d77x.html
https://www.36dianping.com/vs/d77y.html
https://www.36dianping.com/vs/d770.html
https://www.36dianping.com/vs/d771.html
https://www.36dianping.com/vs/d772.html
https://www.36dianping.com/vs/d773.html
https://www.36dianping.com/vs/d774.html
https://www.36dianping.com/vs/d775.html
https://www.36dianping.com/vs/d776.html
https://www.36dianping.com/vs/d777.html
https://www.36dianping.com/vs/d778.html
https://www.36dianping.com/vs/d779.html
https://www.36dianping.com/vs/d78a.html
https://www.36dianping.com/vs/d78b.html
https://www.36dianping.com/vs/d78c.html
https://www.36dianping.com/vs/d78d.html
https://www.36dianping.com/vs/d78e.html
https://www.36dianping.com/vs/d78f.html
https://www.36dianping.com/vs/d78g.html
https://www.36dianping.com/vs/d78h.html
https://www.36dianping.com/vs/d78i.html
https://www.36dianping.com/vs/d78j.html
https://www.36dianping.com/vs/d78k.html
https://www.36dianping.com/vs/d78l.html
https://www.36dianping.com/vs/d78m.html
https://www.36dianping.com/vs/d78n.html
https://www.36dianping.com/vs/d78o.html
https://www.36dianping.com/vs/d78p.html
https://www.36dianping.com/vs/d78q.html
https://www.36dianping.com/vs/d78r.html
https://www.36dianping.com/vs/d78s.html
https://www.36dianping.com/vs/d78t.html
https://www.36dianping.com/vs/d78u.html
https://www.36dianping.com/vs/d78v.html
https://www.36dianping.com/vs/d78w.html
https://www.36dianping.com/vs/d78x.html
https://www.36dianping.com/vs/d78y.html
https://www.36dianping.com/vs/d78z.html
https://www.36dianping.com/vs/d780.html
https://www.36dianping.com/vs/d781.html
https://www.36dianping.com/vs/d782.html
https://www.36dianping.com/vs/d783.html
https://www.36dianping.com/vs/d784.html
https://www.36dianping.com/vs/d785.html
https://www.36dianping.com/vs/d786.html
https://www.36dianping.com/vs/d787.html
https://www.36dianping.com/vs/d788.html
https://www.36dianping.com/vs/d789.html
https://www.36dianping.com/vs/d79b.html
https://www.36dianping.com/vs/d79c.html
https://www.36dianping.com/vs/d79d.html
https://www.36dianping.com/vs/d79e.html
https://www.36dianping.com/vs/d79f.html
https://www.36dianping.com/vs/d79g.html
https://www.36dianping.com/vs/d79h.html
https://www.36dianping.com/vs/d79i.html
https://www.36dianping.com/vs/d79j.html
https://www.36dianping.com/vs/d79k.html
https://www.36dianping.com/vs/d79l.html
https://www.36dianping.com/vs/d79m.html
https://www.36dianping.com/vs/d79n.html
https://www.36dianping.com/vs/d79o.html
https://www.36dianping.com/vs/d79p.html
https://www.36dianping.com/vs/d79q.html
https://www.36dianping.com/vs/d79r.html
https://www.36dianping.com/vs/d79s.html
https://www.36dianping.com/vs/d79t.html
https://www.36dianping.com/vs/d79u.html
https://www.36dianping.com/vs/d79v.html
https://www.36dianping.com/vs/d79w.html
https://www.36dianping.com/vs/d79x.html
https://www.36dianping.com/vs/d79y.html
https://www.36dianping.com/vs/d79z.html
https://www.36dianping.com/vs/d790.html
https://www.36dianping.com/vs/d791.html
https://www.36dianping.com/vs/d792.html
https://www.36dianping.com/vs/d793.html
https://www.36dianping.com/vs/d794.html
https://www.36dianping.com/vs/d795.html
https://www.36dianping.com/vs/d796.html
https://www.36dianping.com/vs/d797.html
https://www.36dianping.com/vs/d798.html
https://www.36dianping.com/vs/d799.html
https://www.36dianping.com/vs/d8aa.html
https://www.36dianping.com/vs/d8ab.html
https://www.36dianping.com/vs/d8ac.html
https://www.36dianping.com/vs/d8ad.html
https://www.36dianping.com/vs/d8ae.html
https://www.36dianping.com/vs/d8af.html
https://www.36dianping.com/vs/d8ag.html
https://www.36dianping.com/vs/d8ah.html
https://www.36dianping.com/vs/d8ai.html
https://www.36dianping.com/vs/d8aj.html
https://www.36dianping.com/vs/d8ak.html
https://www.36dianping.com/vs/d8al.html
https://www.36dianping.com/vs/d8am.html
https://www.36dianping.com/vs/d8an.html
https://www.36dianping.com/vs/d8ao.html
https://www.36dianping.com/vs/d8ap.html
https://www.36dianping.com/vs/d8aq.html
https://www.36dianping.com/vs/d8ar.html
https://www.36dianping.com/vs/d8as.html
https://www.36dianping.com/vs/d8at.html
https://www.36dianping.com/vs/d8au.html
https://www.36dianping.com/vs/d8aw.html
https://www.36dianping.com/vs/d8ax.html
https://www.36dianping.com/vs/d8ay.html
https://www.36dianping.com/vs/d8az.html
https://www.36dianping.com/vs/d8a0.html
https://www.36dianping.com/vs/d8a1.html
https://www.36dianping.com/vs/d8a2.html
https://www.36dianping.com/vs/d8a3.html
https://www.36dianping.com/vs/d8a4.html
https://www.36dianping.com/vs/d8a5.html
https://www.36dianping.com/vs/d8a6.html
https://www.36dianping.com/vs/d8a7.html
https://www.36dianping.com/vs/d8a8.html
https://www.36dianping.com/vs/d8a9.html
https://www.36dianping.com/vs/d8ba.html
https://www.36dianping.com/vs/d8bb.html
https://www.36dianping.com/vs/d8bc.html
https://www.36dianping.com/vs/d8bd.html
https://www.36dianping.com/vs/d8be.html
https://www.36dianping.com/vs/d8bf.html
https://www.36dianping.com/vs/d8bg.html
https://www.36dianping.com/vs/d8bh.html
https://www.36dianping.com/vs/d8bi.html
https://www.36dianping.com/vs/d8bj.html
https://www.36dianping.com/vs/d8bk.html
https://www.36dianping.com/vs/d8bl.html
https://www.36dianping.com/vs/d8bm.html
https://www.36dianping.com/vs/d8bn.html
https://www.36dianping.com/vs/d8bo.html
https://www.36dianping.com/vs/d8bp.html
https://www.36dianping.com/vs/d8bq.html
https://www.36dianping.com/vs/d8br.html
https://www.36dianping.com/vs/d8bs.html
https://www.36dianping.com/vs/d8bt.html
https://www.36dianping.com/vs/d8bu.html
https://www.36dianping.com/vs/d8bv.html
https://www.36dianping.com/vs/d8bw.html
https://www.36dianping.com/vs/d8bx.html
https://www.36dianping.com/vs/d8by.html
https://www.36dianping.com/vs/d8bz.html
https://www.36dianping.com/vs/d8b0.html
https://www.36dianping.com/vs/d8b1.html
https://www.36dianping.com/vs/d8b2.html
https://www.36dianping.com/vs/d8b3.html
https://www.36dianping.com/vs/d8b4.html
https://www.36dianping.com/vs/d8b5.html
https://www.36dianping.com/vs/d8b6.html
https://www.36dianping.com/vs/d8b7.html
https://www.36dianping.com/vs/d8b8.html
https://www.36dianping.com/vs/d8b9.html
https://www.36dianping.com/vs/d8ca.html
https://www.36dianping.com/vs/d8cb.html
https://www.36dianping.com/vs/d8cc.html
https://www.36dianping.com/vs/d8cd.html
https://www.36dianping.com/vs/d8ce.html
https://www.36dianping.com/vs/d8cf.html
https://www.36dianping.com/vs/d8cg.html
https://www.36dianping.com/vs/d8ch.html
https://www.36dianping.com/vs/d8ci.html
https://www.36dianping.com/vs/d8cj.html
https://www.36dianping.com/vs/d8ck.html
https://www.36dianping.com/vs/d8cl.html
https://www.36dianping.com/vs/d8cm.html
https://www.36dianping.com/vs/d8cn.html
https://www.36dianping.com/vs/d8co.html
https://www.36dianping.com/vs/d8cp.html
https://www.36dianping.com/vs/d8cq.html
https://www.36dianping.com/vs/d8cr.html
https://www.36dianping.com/vs/d8cs.html
https://www.36dianping.com/vs/d8ct.html
https://www.36dianping.com/vs/d8cu.html
https://www.36dianping.com/vs/d8cv.html
https://www.36dianping.com/vs/d8cw.html
https://www.36dianping.com/vs/d8cx.html
https://www.36dianping.com/vs/d8cy.html
https://www.36dianping.com/vs/d8cz.html
https://www.36dianping.com/vs/d8c0.html
https://www.36dianping.com/vs/d8c1.html
https://www.36dianping.com/vs/d8c2.html
https://www.36dianping.com/vs/d8c3.html
https://www.36dianping.com/vs/d8c4.html
https://www.36dianping.com/vs/d8c5.html
https://www.36dianping.com/vs/d8c6.html
https://www.36dianping.com/vs/d8c7.html
https://www.36dianping.com/vs/d8c8.html
https://www.36dianping.com/vs/d8c9.html
https://www.36dianping.com/vs/d8da.html
https://www.36dianping.com/vs/d8db.html
https://www.36dianping.com/vs/d8dc.html
https://www.36dianping.com/vs/d8dd.html
https://www.36dianping.com/vs/d8de.html
https://www.36dianping.com/vs/d8dg.html
https://www.36dianping.com/vs/d8dh.html
https://www.36dianping.com/vs/d8di.html
https://www.36dianping.com/vs/d8dj.html
https://www.36dianping.com/vs/d8dk.html
https://www.36dianping.com/vs/d8dl.html
https://www.36dianping.com/vs/d8dm.html
https://www.36dianping.com/vs/d8dn.html
https://www.36dianping.com/vs/d8do.html
https://www.36dianping.com/vs/d8dp.html
https://www.36dianping.com/vs/d8dq.html
https://www.36dianping.com/vs/d8dt.html
https://www.36dianping.com/vs/d8du.html
https://www.36dianping.com/vs/d8dv.html
https://www.36dianping.com/vs/d8dw.html
https://www.36dianping.com/vs/d8dx.html
https://www.36dianping.com/vs/d8dy.html
https://www.36dianping.com/vs/d8dz.html
https://www.36dianping.com/vs/d8d0.html
https://www.36dianping.com/vs/d8d1.html
https://www.36dianping.com/vs/d8d2.html
https://www.36dianping.com/vs/d8d3.html
https://www.36dianping.com/vs/d8d4.html
https://www.36dianping.com/vs/d8d5.html
https://www.36dianping.com/vs/d8d6.html
https://www.36dianping.com/vs/d8d7.html
https://www.36dianping.com/vs/d8d8.html
https://www.36dianping.com/vs/d8d9.html
https://www.36dianping.com/vs/d8ea.html
https://www.36dianping.com/vs/d8eb.html
https://www.36dianping.com/vs/d8ec.html
https://www.36dianping.com/vs/d8ed.html
https://www.36dianping.com/vs/d8ee.html
https://www.36dianping.com/vs/d8ef.html
https://www.36dianping.com/vs/d8eg.html
https://www.36dianping.com/vs/d8eh.html
https://www.36dianping.com/vs/d8ei.html
https://www.36dianping.com/vs/d8ej.html
https://www.36dianping.com/vs/d8ek.html
https://www.36dianping.com/vs/d8el.html
https://www.36dianping.com/vs/d8em.html
https://www.36dianping.com/vs/d8en.html
https://www.36dianping.com/vs/d8eo.html
https://www.36dianping.com/vs/d8ep.html
https://www.36dianping.com/vs/d8eq.html
https://www.36dianping.com/vs/d8ds.html
https://www.36dianping.com/vs/d8er.html
https://www.36dianping.com/vs/d8es.html
https://www.36dianping.com/vs/d8et.html
https://www.36dianping.com/vs/d8eu.html
6.1 需求
完成类似 if else if else
效果。见下方的各种执行结果。
6.2 规则文件
rule "if else-if"
when
$customer: Customer(age > 60) // 规则内存中存在Customer对象,并且age>60
if($customer.getLevel() == 1) do[level1] // 用户的级别是1,执行then[level1],然后继续执行下方的条件
else if ($customer.getLevel() == 2) break[level2] // 用户的级别是2,执行then[level2],不在执行下方的条件
else do[levelOther] // 其他的level级别,执行then[levelOther],然后在执行下方的条件
Car()
then
System.out.println("我执行了");
then[level1]
System.out.println("level1");
then[level2]
System.out.println("level2");
then[levelOther]
System.out.println("levelOther");
end
6.3 执行结果图
6.4 各种执行结果-也就是上图的解释
1、Customer
的age
小于60
。输出:
没有输出。
2、Customer
的age
大于60
并且level=1
,没有Car
。输出:
level1
3、Customer
的age
大于60
并且level=1
,有Car
。输出:
level1 我执行了
4、Customer
的age
大于60
并且level=2
,没有Car
。输出:
level2
5、Customer
的age
大于60
并且level=2
,有Car
。输出:
level2
6、Customer
的age
大于60
并且level=3
,没有Car
。输出:
levelOther
7、Customer
的age
大于60
并且level=3
,有Car
。输出:
levelOther 我执行了
6.5 do和 break 有什么区别
do
:执行完之后,还会继续判断后面的执行条件。 (即还会执行后面的Car判断,根据是否有Car获取不同的结果
)break
:执行完之后,不会在判断后面的执行条件。(即忽略了后面的Car判断,rule执行完了
)