惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
V
V2EX
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
P
Privacy International News Feed
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
Kaspersky official blog
S
Secure Thoughts
T
Tenable Blog
Security Latest
Security Latest
The Cloudflare Blog
S
Security @ Cisco Blogs
H
Heimdal Security Blog
aimingoo的专栏
aimingoo的专栏
TaoSecurity Blog
TaoSecurity Blog
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
IT之家
IT之家
Latest news
Latest news
The Hacker News
The Hacker News
C
Check Point Blog
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
N
News | PayPal Newsroom
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
S
Security Affairs
S
Securelist
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
A
About on SuperTechFans

博客园 - lonecloud

联通云主机安装openclaw 可食用教程 win10 出现你的设备有工作或学校账户方面的问题,请再次登录以访问组织资源 解决MAC升级系统后,GIT报错missing xcrun Git 在Mac下的中文乱码问题 记一个使用fyne-cross编译的坑 windows gcc 遇到的问题解决 Git 提交界面中文乱码解决 WARNING: IPv4 forwarding is disabled. Networking will not work. postgresql数据类型 Mybatis源码-详解Mybatis日志设计 JDBC 规范中文版 4.2- 第五章 类和接口 JDBC 规范中文版 4.2 -第四章 概览 JDBC 规范中文版 4.2 -第三章 新特性 JDBC 规范中文版 4.2 -第二章 目标 JDBC 规范中文版 4.2 -第一章 简介 基础回顾-线程的几种状态 一文读懂BeanFactory和FactoryBean区别 IDEA技巧-自动导包(Auto import)以及自动优化包 Drools学习教程 - 2. 规则文件语法
Drools学习教程 - 3. 规则属性
lonecloud · 2020-02-23 · via 博客园 - lonecloud

在drools中会有很多属性可供我们选择,有防止死循环的,优先级等

Drools文件中的相关配置属性

属性 作用 默认值
no-loop 防止死循环 false
ruleFlow-group 规则类型 rule:规则 flow:流程 group:分组
lock-on-active 锁定活跃(no-loop相似,但是是其升级版本) false
salience 执行优先级 0
enabled 是否执行 true
dialect 该执行语言类型 java/mvel(由包指定)
date-effective 规则生效时间(dd-MMM-yyyy格式)07-August-2018 N/A
date-expires 规则失效时间(dd-MMM-yyyy格式)07-August-2018 N/A
duration 定时器(废弃)代替->time属性 N/A
activation-group 激活分组,具有相同组名称的规则体有且只有一个规则被激活(选择哪个按照优先级) N/A
agenda-group 议程分组,可以在代码中控制执行哪个规则 无,基于代码设置
auto-focus 自动获取焦点,当前规则是否激活
timer 控制规则执行时间

no-loop配置

用于防止死循环,当规则调用了update操作的时候,会导致规则的对象再次激活,则可能再次激活对象

  • 默认值:false
  • 类型:boolean
rule "dead2"
  no-loop true
  when
    $p:Person(age==20)
  then
    System.out.println($p);
    $p.setAge(20);
    update($p)
  end

死循环实例

package chp3
import cn.lonecloud.drools.bean.Person;
rule "dead"
  no-loop true
  when
    $p:Person(age==20)
  then
    System.out.println($p);
    $p.setAge(20);
    update($p)
  end

rule "dead2"
  no-loop true
  when
    $p:Person(age==20)
  then
    System.out.println($p);
    $p.setAge(20);
    update($p)
  end

ruleFlow-group

用于表示规则流

  • 默认值:N/A
  • 可选值:
    • rule:规则
    • flow:流程
    • group:分组

lock-on-active

锁定活跃,也就是防止规则出现多次调用,当前规则只能执行一次。保证当前规则在同一个kession 是只会执行一次的。

  • 默认值:false

以下例子就不会出现死循环情况了

package chp3
import cn.lonecloud.drools.bean.Person;
rule "dead"
  no-loop true
  lock-on-active true
  when
    $p:Person(age==20)
  then
    System.out.println($p);
    $p.setAge(20);
    update($p)
  end

rule "dead2"
  no-loop true
  lock-on-active true
  when
    $p:Person(age==20)
  then
    System.out.println($p);
    $p.setAge(20);
    update($p)
  end

salience

优先级,默认是按照从上而下执行规则,该值越大则其优先级越高

  • 默认值:0
  • 类型:integer
  • 规范:可为负数,值越大则其优先级越高
package chp3
rule "salience40"

  salience 40
  when
    eval(true)
  then
    System.out.println("salience40");
  end

rule "salience2"
  no-loop true
  lock-on-active true
  salience 60
  when eval(true)
  then
    System.out.println("salience60");
  end

运行结果:

salience60
salience40

date-effective&&date-expires

  • 作用:用于指定规则的生效和失效时间
  • 默认值:无
  • 默认格式:dd-MMM-yyyy
  • 自定义时间格式:System.setProperty("drools.dateformat","yyyy-MM-dd HH:mm:ss")`

agenda-group 议程分组

Java代码可控的分组执行

drl文件定义

package chp3
rule "agenda"
  agenda-group "demo1"
  salience 40
  when
    eval(true)
  then
    System.out.println("agenda-group");
  end

rule "agenda2"
  agenda-group "demo2"
  salience 60
  when eval(true)
  then
    System.out.println("agenda-group2");
  end

代码执行


        KieSession salience = kieClasspathContainer.newKieSession("salience");
        //设置激活
				salience.getAgenda().getAgendaGroup("demo1").setFocus();
        int salienceCount = salience.fireAllRules();
        System.out.println("一共运行了"+salienceCount+"条规则");

结果

agenda-group

timer属性

用于定时器,支持cron表达式

rule "timer1"
  timer (int: 3s)
  when eval(true)
  then
    System.out.println("3s执行一次");
  end
rule "timer2"
  timer (cron: 0/15 * * * * ?)
  when eval(true)
  then
    System.out.println("15s执行一次");
  end

java

        //定时任务
        KieSessionConfiguration kieBaseConfiguration = kieServices.newKieSessionConfiguration();
        kieBaseConfiguration.setOption(TimedRuleExecutionOption.YES);
        KieSession kieSession = kieClasspathContainer.newKieBase("chp3",KieServices.Factory.get().newKieBaseConfiguration()).newKieSession(
                kieBaseConfiguration, null
        );
        int i1 = kieSession.fireAllRules();

执行结果

3s执行一次
15s执行一次
15s执行一次
15s执行一次
15s执行一次
15s执行一次
15s执行一次