分享

规则引擎 Drools(九):条件元素 exists 和 forall

 邵飞翔 2018-02-01

exists

exists 用于判断符合条件的 fact 是否存在。

示例:

rule "test exists"  
    when
        exists(Person(sex == "female", age > 18))
    then
        System.out.println("girls!");
end  

示例与

rule "test"  
    when
        Person(sex == "female", age > 18)
    then
        System.out.println("girl!");
end  

的区别在于:

当向会话中插入多个 Person(sex "female", age > 18) fact 时,前者仅执行一次,而或者会执行多次。

forall

forall 用于判断是否所有的 fact 都满足条件。

示例:

rule "test forall"  
    when
        exists(Person())
        forall(Person() Person(sex == "female", age > 18))
    then
        System.out.println("all girls!");
end  

注意:当会话中没有 Person(sex "female", age > 18) fact 时

rule "test forall"  
    when
        forall(Person() Person(sex == "female", age > 18))
    then
        System.out.println("all girls!");
end  

forall 的结果为真!!!

所以,需要在 forall 前添加 exists 先判断一下。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约