//测试比较操作符contains rule "rule_comparison_contains" when ComparisonOperatorEntity(names contains "张三") ComparisonOperatorEntity(list contains names) then System.out.println("规则rule_comparison_contains触发"); end
//测试比较操作符not contains rule "rule_comparison_notContains" when ComparisonOperatorEntity(names not contains "张三") ComparisonOperatorEntity(list not contains names) then System.out.println("规则rule_comparison_notContains触发"); end
//测试比较操作符memberOf rule "rule_comparison_memberOf" when ComparisonOperatorEntity(names memberOf list) then System.out.println("规则rule_comparison_memberOf触发"); end
//测试比较操作符not memberOf rule "rule_comparison_notMemberOf" when ComparisonOperatorEntity(names not memberOf list) then System.out.println("规则rule_comparison_notMemberOf触发"); end
//测试比较操作符matches rule "rule_comparison_matches" when ComparisonOperatorEntity(names matches "张.*") then System.out.println("规则rule_comparison_matches触发"); end
//测试比较操作符not matches rule "rule_comparison_notMatches" when ComparisonOperatorEntity(names not matches "张.*") then System.out.println("规则rule_comparison_notMatches触发"); end
ComparisonOperatorEntity comparisonOperatorEntity = new ComparisonOperatorEntity(); comparisonOperatorEntity.setNames("张三"); List<String> list = new ArrayList<String>(); list.add("张三"); list.add("李四"); comparisonOperatorEntity.setList(list);
ComparisonOperatorEntity comparisonOperatorEntity = new ComparisonOperatorEntity(); comparisonOperatorEntity.setNames("张三"); List<String> list = new ArrayList<String>(); list.add("张三"); list.add("李四"); comparisonOperatorEntity.setList(list); kieSession.insert(comparisonOperatorEntity);
软关键字包括:lock-on-active date-effective date-expires no-loop auto-focus activation-group agenda-group ruleflow-group entry-point duration package import dialect salience enabled attributes rule extend when then template query declare function global eval not in or and exists forall accumulate collect from action reverse result end over init
rule "rule_student_age小于10岁" when $s:Student(age < 10) then $s.setAge(15); update($s);//更新数据,导致相关的规则会重新匹配 System.out.println("规则rule_student_age小于10岁触发"); end
rule "rule_student_age小于20岁同时大于10岁" when $s:Student(age < 20 && age > 10) then $s.setAge(25); update($s);//更新数据,导致相关的规则会重新匹配 System.out.println("规则rule_student_age小于20岁同时大于10岁触发"); end
rule "rule_student_age大于20岁" when $s:Student(age > 20) then System.out.println("规则rule_student_age大于20岁触发"); end
rule "rule_student_age等于10岁" when $s:Student(age == 10) then Student student = new Student(); student.setAge(5); insert(student);//插入数据,导致相关的规则会重新匹配 System.out.println("规则rule_student_age等于10岁触发"); end
rule "rule_student_age小于10岁" when $s:Student(age < 10) then $s.setAge(15); update($s); System.out.println("规则rule_student_age小于10岁触发"); end
rule "rule_student_age小于20岁同时大于10岁" when $s:Student(age < 20 && age > 10) then $s.setAge(25); update($s); System.out.println("规则rule_student_age小于20岁同时大于10岁触发"); end
rule "rule_student_age大于20岁" when $s:Student(age > 20) then System.out.println("规则rule_student_age大于20岁触发"); end
rule "rule_student_age等于10岁时删除数据" /* salience:设置当前规则的执行优先级,数值越大越优先执行,默认值为0. 因为当前规则的匹配条件和下面规则的匹配条件相同,为了保证先执行当前规则,需要设置优先级 */ salience 100 when $s:Student(age == 10) then retract($s);//retract方法的作用是删除工作内存中的数据,并让相关的规则重新匹配。 System.out.println("规则rule_student_age等于10岁时删除数据触发"); end
rule "rule_student_age等于10岁" when $s:Student(age == 10) then Student student = new Student(); student.setAge(5); insert(student); System.out.println("规则rule_student_age等于10岁触发"); end
rule "rule_student_age小于10岁" when $s:Student(age < 10) then $s.setAge(15); update($s); System.out.println("规则rule_student_age小于10岁触发"); end
rule "rule_student_age小于20岁同时大于10岁" when $s:Student(age < 20 && age > 10) then $s.setAge(25); update($s); System.out.println("规则rule_student_age小于20岁同时大于10岁触发"); end
rule "rule_student_age大于20岁" when $s:Student(age > 20) then System.out.println("规则rule_student_age大于20岁触发"); end
rule "rule_comparison_notMemberOf" //指定当前规则不可用,当前规则无论是否匹配成功都不会执行 enabled false when ComparisonOperatorEntity(names not memberOf list) then System.out.println("规则rule_comparison_notMemberOf触发"); end
package testactivationgroup /* 此规则文件用于测试activation-group属性 */ rule "rule_activationgroup_1" activation-group "mygroup" when then System.out.println("规则rule_activationgroup_1触发"); end
rule "rule_activationgroup_2" activation-group "mygroup" when then System.out.println("规则rule_activationgroup_2触发"); end
package testagendagroup /* 此规则文件用于测试agenda-group属性 */ rule "rule_agendagroup_1" agenda-group "myagendagroup_1" when then System.out.println("规则rule_agendagroup_1触发"); end
rule "rule_agendagroup_2" agenda-group "myagendagroup_1" when then System.out.println("规则rule_agendagroup_2触发"); end //======================================================== rule "rule_agendagroup_3" agenda-group "myagendagroup_2" when then System.out.println("规则rule_agendagroup_3触发"); end
rule "rule_agendagroup_4" agenda-group "myagendagroup_2" when then System.out.println("规则rule_agendagroup_4触发"); end
rule "rule_agendagroup_1" agenda-group "myagendagroup_1" when then System.out.println("规则rule_agendagroup_1触发"); end
rule "rule_agendagroup_2" agenda-group "myagendagroup_1" when then System.out.println("规则rule_agendagroup_2触发"); end //======================================================== rule "rule_agendagroup_3" agenda-group "myagendagroup_2" auto-focus true //自动获取焦点 when then System.out.println("规则rule_agendagroup_3触发"); end
rule "rule_agendagroup_4" agenda-group "myagendagroup_2" auto-focus true //自动获取焦点 when then System.out.println("规则rule_agendagroup_4触发"); end
rule "rule_timer_1" timer (5s 2s) //含义:5秒后触发,然后每隔2秒触发一次 when then System.out.println("规则rule_timer_1触发,触发时间为:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); end
rule "rule_timer_2" timer (cron:0/1 * * * * ?) //含义:每隔1秒触发一次 when then System.out.println("规则rule_timer_2触发,触发时间为:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); end
package testdateeffective /* 此规则文件用于测试date-effective属性 */ rule "rule_dateeffective_1" date-effective "2020-10-01 10:00" when then System.out.println("规则rule_dateeffective_1触发"); end