rule "matchesTest001" when $p : Person(name matches "张.*"); then System.out.println("matchesTest001 success"); end
rule "strTest001" when $p : Person(name str[startsWith] "张") then System.out.println("strTest001 success"); end
集合匹配 - in/ not in
判断元素是否(在/不在)集合中
使用
1 2 3 4 5 6 7
rule "in使用" when $s: School($cn: className) $p: Person(className in ("五班", "六班", $cn)) then System.out.println("check in的使用: " + $s + "," + $p); end
或者可以反过来使用contains关键字
实体匹配 - not/ exists
判断实体是否存在
使用
1 2 3 4 5 6 7 8 9 10 11 12 13
rule "测试not" when not Person() then System.out.println("Person不存在"); end
rule "测试exists" when exists Person() then System.out.println("Person存在"); end
将表达式转换为bool值 - eval
使用
1 2 3 4 5 6
rule "测试eval" when eval(1 == 1) then System.out.println("eval true"); end
全匹配- forall
当其中的条件都为true时,为true
使用
1 2 3 4 5 6 7
rule "测试forall" when forall($p : Person(name == "张三") Person(this == $p, age == 30)) then System.out.println("forall true"); end
引用资源 - from
引用指定的资源,用于数据匹配或对集合进行遍历
使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
rule "测试from" when $p: Person($ps: school) $s: School(className == "一班") from $ps; then System.out.println("测试from: " + $p + "," + $s); end
rule "测试from1" when $s: School() $p: Person(className == "一班") from $s.classNameList then System.out.println("测试from1: " + $p + "," + $s); end
收集集合及过滤 - collect
使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14
rule "测试from collect" when $al: ArrayList() from collect($p: Person(className == "一班")) then System.out.println("res size: " + $al.size()); end
rule "测试from collect pattern" when $al: ArrayList(size >= 3) from collect($p: Person(className == "一班")) then System.out.println("res size: " + $al.size()); end
集合的复杂处理 - accumulate
使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
rule "测试accumulate 取最大值、最小值" when accumulate(Person($age: age), $min:min($age), $max:max($age), $sum:sum($age)) then System.out.println("max:" + $max + ", min: " + $min + ",sum:" + $sum); end
rule "测试accumulate 第二种用法" when $total: Integer() from accumulate(Person($value: age), init(Integer total = 0;), action(total += $value;), result(total) ) then System.out.println("total:" + $total); end
then中的后续处理
在when中的条件满足后,进行then的后续逻辑处理,此时可对传进来的数据进行修改等操作
update(对象)
单纯的调用对象的方法修改其中的属性,不会触发其他规则的校验
但修改后调用update则会告诉引擎对象已经改变,如果满足条件则会触发规则
也可以简写为: modify() {},如:
1 2 3 4 5 6 7 8 9
rule "测试更新" when $p : Person(name matches "张.*"); then modify($p){ setName("李四") } System.out.println("matchesTest001 success"); end