Drools规则引擎详解-常用的drl实例

package droolsDemo
//说明:每个 drl 都必须声明一个包名,这个包名与 Java 里面的不同,它不需要与文件夹的层次结构一致,
//主要用于可以根据kmodule.xml中不同的package属性来指定加载哪个.drl文件,
//packages="org.some.pkg"表示ksession2只会加载org.some.pkg下的规则文件,如果没写,则默认加载所有的规则文件。

//导入实体类
import com.qianxin.rulesengine.drools.User
import com.qianxin.rulesengine.drools.Pet
import com.qianxin.rulesengine.drools.Dog
import com.qianxin.rulesengine.drools.BoolTest

rule "multiple condition"
// salience值越大越先匹配
salience 1
    when
        //多个条件测试:user中age在15-30 60-90之间并且 根据user的pet属性的color来处理不同的逻辑,
        $user : User((age>15 && age<30)||(age>60 && age<90))
        $pet : Pet() from $user.pet
        if($pet.getColor()=="black") do[black]
        // do 关键结束后,后续逻辑继续,break 关键字结束后结束程序
        if($pet.getColor()=="white") break[white]
    then
        System.out.print("测试结束");
    then [black]
        System.out.println("pet color is balck");
    then [white]
        System.out.print("pet color is white");
     end

rule "BigInteger test"
     salience 20
      when
      // 可以用BigInteger来解决大数(超过64位)的比较,支持直接用>=  ==  =< 符号直接比较,十分方便
      // 开发中可以用来解决IPv6 的比较
         $dog : Dog(age=="21262780079976241822035969236715638783")
      then
         System.out.println("我等于21262780079976241822035969236715638783");
       end
 rule "string compare"
    salience -20
     when
     /*字符串的数字也是可以比较的*/
        $user : User((age>"15" && age<"40")||(age>"60"))
     then
         System.out.println("我的优先级是 -20");
         //此规则匹配后不再匹配其他规则
         drools.halt();
      end
rule "test bool "
          salience 100
           when
           //即使value为boolean类型,也可以这样比较
              $bool : BoolTest(value=="true")
           then
               System.out.println("I am true");
            end
rule "test contains and not contains"
          salience 200
           when
           //contains 以及not contains 可以测试集合中内容
              $user : User(set contains "360")
           then
               System.out.println("测试表明包含 360");
            end

      
rule "test not in"
     salience 100
           when
           //测试not in 年龄不在15-30 60-90 之间的符合
              $user : User(!((age>15 && age<30)||(age>60 && age<90)))
           then
               System.out.print("age不在 15-30,60-90之间");
            end


rule "number test"                                  
    when
        $user : User(age>15 && age<60)     
    then
        System.out.println("年龄符合条件");
end

rule "String test"                                    
    when
        $pet : Pet(name=="cat")
    then
        System.out.println("宠物名称符合条件");

end
更详细的语法请参考:http://www.1994july.club/seo/

猜你喜欢

转载自www.cnblogs.com/1994july/p/12037509.html
今日推荐