Drools

Drools规则文件——常用的语法属性

2017-09-30  本文已影响339人  天堂的码头

Drools规则文件——语法属性

  1. salience
    功能:设置规制执行的优先级
    值:数字(数字越大执行优先级越高)

    示例:

    rule "rule1"
      salience 1
      when
        eval(true)
      then  
        System.out.println("rule1");
    end
    
  2. no-loop
    功能:控制已经执行的规则条件再次满足是否再次执行
    值:true/false

    示例:

    rule "rule1"
      no-loop true
      when
        $customer:Customer(name=="张三")
      then  
        update($customer);
        System.out.println("customer name:"+$customer.getName());
    end
    
  3. date-effective
    功能:当系统时间>=date-effective后才会触发,即设置规则生效时间。
    值:日期默认格式为dd-MMM-yyyy,可以设置其它时间格式如yyyy-MM-dd,需在代码设置系统时间格式System.setProperty("drools.dateformat", "yyyy-MM-dd");

    示例:

    rule "rule1"
      date-effective "2009-09-25"
      when
        eval(true);
      then  
        System.out.println("rule1 is execution!");
    end
    
  4. date-expires
    功能:当系统时间<=date-expires后才会触发,即设置规则过期时间。
    值:日期默认格式为dd-MMM-yyyy可以设置其它时间格式如yyyy-MM-dd,需在代码设置系统时间格式System.setProperty("drools.dateformat", "yyyy-MM-dd");

    示例:

    rule "rule1"
      date-expires "2009-09-27"
      when
        eval(true);
      then  
        System.out.println("rule1 is execution!");  
    end
    
  5. enabled
    功能:设置规制是否可用
    值:true/false

    示例:

    rule "rule1"
      enabled false
      when
        eval(true);
      then  
        System.out.println("rule1 is execution!");
    end
    
  6. dialect
    功能:规则当中要使用的语言类型
    值:Java/mevl(默认为java)

    示例:

    rule "rule3"
      dialect "mvel"
      when
        $app:Applicant(age == 24);
      then
        System.out.println("rule3----" + $app.name);
    end
    
  7. duration
    功能:设置DRL文件开始执行之后延迟多长时间开始执行这条规则
    值:一个长整型,单位是毫秒

    示例:

    rule "rule1"
      duration 3000
      when
        eval(true)
      then  
        System.out.println("rule thread id:"+Thread.currentThread().getId());
    end
    
  8. activation-group
    功能:若干个规则划分成一个组
    值:分组名称

    示例:

    rule "rule2"
      activation-group "test"
      salience 10
      when
        eval(true)
      then
        System.out.println("rule2 execute");
    end
    
    rule "rule1"
      activation-group "test"
      salience 9
      when
        eval(true)
      then
        System.out.println("rule1 execute");
    end
    

    note: 如果同一组规则,谁的salience高就执行谁,没有则按顺序执行最后同组最后那个规则

  9. agenda-group
    功能:Agenda Group 是用来在 Agenda的基础之上,对现在的规则进行再次分组.Agenda Group 得到 Focus(焦点),这样位于该 Agenda Group当中的规则才会
    触发执行,否则将不执行。
    值:一个字符串

    示例:

    rule "rule1"
      agenda-group "001"
      when
        eval(true)
      then
        System.out.println("rule1 execute");
    end
    
    rule "rule2"
      agenda-group "002"
      when
        eval(true)
      then
        System.out.println("rule2 execute");
    end
    
  10. auto-focus
    功能:跟agenda-group一起使用,设置该规则是否可以自动独取 Focus,如果该属性设置为 true,那么在引擎执行时,就不需要
    显示的为某个Agenda Group 设置 Focus,否则需要。
    值:true/false

    示例:

    rule "rule1"
      agenda-group "001"
      auto-focus true
      when
        eval(true)
      then
        System.out.println("rule1 execute");
    end
    
    rule "rule2"
      agenda-group "002"
      auto-focus true
      when
        eval(true)
      then
        System.out.println("rule2 execute");
    end
    

上一篇下一篇

猜你喜欢

热点阅读