好!那我们就说一下通过正则的方式 做一个比较复杂例子
DSLR文件
package rules.testdsl; import com.drools.api.rule.Person; import com.drools.api.rule.School; expander PersonDSL.dsl rule "rule2" when 我们要找一个人 - 姓名 等于 "张三" - 年龄 等于 30 - 年小 等于 "李四小" 我们要找一家学校 - 学名 等于 "新一中" - 人数 等于 50 then 公司给予你"《高级项目经理的职位》"的荣誉称号 输出这家学校的名字 1 学名 等于 "新一中" endDSL文件
[when][]我们要找一个人=$p:Person() [when][]我们要找一家学校=$s:School() [when][]姓名=name [when][]年龄=age [when][]学名=name [when][]年小=aGess [when][]人数=count [when][]等于=== [when][]- {field:[\u4e00-\u9fa5_a-zA-Z0-9^"]+} {operator} {value:[\u4e00-\u9fa5_a-zA-Z0-9^"]+}={field} {operator} {value} [then]公司给予你"{post}"的荣誉称号=$p.setPost("{post}"); [then]输出这家学校的名字= System.out.println("这家学校的名字是"+$s.getName()); System.out.println(kcontext.getRule().getName()); [then]学名=name66 [then][]1 {field:[\u4e00-\u9fa5_a-zA-Z0-9^"]+} {operator} {value:[\u4e00-\u9fa5_a-zA-Z0-9^"]+} =System.out.println("{field} +++++"); System.out.println("{operator} ++++"); System.out.println({value});
rule "name" when then hello zhangsan end
[when] //备注一下:这里因为没有条件部分 所以这里空的 [then] hello {name:\w*}=System.out.println("{name}");
rule "name" when Person - is a zhangsan then hello zhangsan end
[when] Person=Person() [when] - is a {name:\w*}= name=="{name}" [then]hello {name:\w*}=System.out.println("{name}");
[when][]我们要找一个人=$p:Person() [when][]姓名=name [when][]年龄=age [when][]校名=$sname:school.name [when][]人数=school.count [when][]小于或等于=< [when][]小于=< [when][]大于或等于==> [when][]大于=> [when][]等于=== [when][]- {conditions}={conditions} [then]公司给予你"{post}"的荣誉称号=$p.setPost("{post}"); [then]输出这家学校的名字= System.out.println("这家学校的名字是"+$sname);
package rules.testdsl; import com.drools.api.rule.Person; import com.drools.api.rule.School; expander PersonDSL.dsl rule "rule2" when 我们要找一个人 - 姓名 等于 "张三" - 年龄 等于 30 - 校名 等于 "新一中" - 人数 等于 50 then 公司给予你"《高级项目经理的职位》"的荣誉称号 输出这家学校的名字 end
rule "rule2" when 我们要找一个人 - 姓名 等于 "张三" - 年龄 等于 30 并 年小 等于 "李四小" 我们要找一家学校 - 学名 等于 "新一中" - 人数 等于 50 then 公司给予你"《高级项目经理的职位》"的荣誉称号 输出这家学校的名字 1 学名 并 "新一中" end
[when][]我们要找一个人=$p:Person() [when][]我们要找一家学校=$s:School() [when][]姓名=name [when][]年龄=age [when][]学名=name [when][]年小=aGess [when][]人数=count [when][]小于或等于=< [when][]小于=< [when][]大于或等于==> [when][]大于=> [when][]等于=== [when][]或者=or [when][]并=&& [when][]或=|| [when][]- {field:[\u4e00-\u9fa5_a-zA-Z0-9^"]+} {operator} {value:[\u4e00-\u9fa5_a-zA-Z0-9^"]+}={field} {operator} {value}
[when][]我们要找一个人=$p:Person() [when][]我们要找一家学校=$s:School() [when][]姓名=name [when][]年龄=age [when][]学名=name [when][]年小=aGess [when][]人数=count [when][]小于或等于=< [when][]小于=< [when][]大于或等于==> [when][]大于=> [when][]等于=== [when][]或者=or [when][]并且=and [when][]- {field:[\u4e00-\u9fa5_a-zA-Z0-9^"]+} {operator} {value:[\u4e00-\u9fa5_a-zA-Z0-9^"]+}={field} {operator} {value} [then]公司给予你"{post}"的荣誉称号=$p.setPost("{post}"); [then]输出这家学校的名字= System.out.println("这家学校的名字是"+$s.getName()); System.out.println(kcontext.getRule().getName());
rule "rule2" when 我们要找一个人 - 姓名 等于 "张三" - 年龄 等于 30 - 年小 等于 "李四小" 或者 我们要找一家学校 - 学名 等于 "新一中" - 人数 等于 50 then 公司给予你"《高级项目经理的职位》"的荣誉称号 输出这家学校的名字 end
下面是小编的微信转帐二维码,小编再次谢谢读者的支持,小编会更努力的
----请看下方↓↓↓↓↓↓↓
百度搜索 Drools从入门到精通:可下载开源全套Drools教程
深度Drools教程不段更新中:
更多Drools实战陆续发布中………
扫描下方二维码关注公众号 ↓↓↓↓↓↓↓↓↓↓