drools规则引擎使用文档

drools规则引擎使用文档

一、规则文件结构

package

import

function  // Optional

query  // Optional

declare   // Optional

global   // Optional

rule "rule name"
    // Attributes
    when
        // Conditions
    then
        // Actions
end

rule "rule2 name"

...

package

  • package:可选,默认值defaultpkg(行尾;可以省略)

    若当前kbase指定路径下所有drl均未指定package,则为defaultpkg;

    每个drl文件中仅可出现一次,且必须置于其它代码之前。

    注意:package namespace建议与kbase packages目录保持一致,否则在最新版本中无法通过kbase获取package对象。

  • import:可选,自动导入package同名包及java.lang.*下的类;

  • global:可选,一般用于Service服务类的导入,例如emailServicesmsService,甚至是Spring 容器上下文对象applicationContext

  • 依据上图importdeclareglobalfunctionqueryrule位置是可以互换的。

  • 注释部分 ///*...*/,可出现在代码的任何地方,会被编译器忽略。

    示例:

    示例

二、规则体结构

rule "rule_name"
    // Attribute
    // Attribute
    when
        // Conditions
    then
        // Actions
end

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QvYy4GXW-1630918204682)(https://docs.jboss.org/drools/release/7.54.0.Final/drools-docs/html_single/LanguageReference/rule.png)]

1.Attribute

rule attributes

  • ruleflow-groupagenda-group概念已统一,ruleflow-group讲覆盖agenda-group属性。

Since version 6.x the two concepts have been unified, the ruleflow-group name will override the agenda-group.

  • Attribute之间可以使用分割,而非;,可省略)
  • 依据上图salienceenableddate-effectivedate-expiresno-loopagenda-groupactivation-groupdurationtimercalendarauto-focuslock-on-activeruleflow-groupdialect、位置是可以互换的。

示例:

package com.example.drools;

dialect  "mvel"

rule "rule1"
    salience 10
    enabled true
    date-effective "4-Sep-2018"
    date-expires "4-Sep-2022"
    no-loop true
    agenda-group "agenda-group-1"
    activation-group "activation-group-1"
    duration 1000
    timer (cron:0/10 * * * * ? )
//    calendar "* * 0-7,18-23 ? * *"
    auto-focus false
    lock-on-active false
    // 覆盖agenda-group属性值
//    ruleflow-group "ruleflow-group-1"
    // 覆盖包级别方言
    dialect "java"
    when
    then
    System.out.println("rule1");
end

2.Conditions

Pattern

条件部分由0个或多个patterns组成,多个pattern之间使用andornot组成,默认值and;

pattern括号内部由0个或多个constraints约束部分组成,多个constrain可用,分割(,语法上等同于&&,但优先级小于&&和||);

  • from和entry-point

from和entry-point一起使用,实现eval时载入数据;

rule "Authorize withdrawal"
  when
    WithdrawRequest( $ai : accountId, $am : amount ) from entry-point "ATM Stream"
    CheckingAccount( accountId == $ai, balance > $am )
  then
    // Authorize withdrawal.
end
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.EntryPoint;

// Create your KIE base and KIE session as usual:
KieSession session = ...

// Create a reference to the entry point:
EntryPoint atmStream = session.getEntryPoint("ATM Stream");

// Start inserting your facts into the entry point:
atmStream.insert(aWithdrawRequest);
  • accumulate

自定义聚合函数导入

import accumulate AverageAccumulateFunction.AverageData average

rule "Average profit"
  when
    $order : Order()
    accumulate( OrderItem( order == $order, $cost : cost, $price : price );
                $avgProfit : average( 1 - $cost / $price ) )
  then
    // Average profit for `$order` is `$avgProfit`.
end

3.Actions

  • 数据操作类

actions包括setmodifyupdateinsertLogicaldelete

  • drools内部变量调用,droolskcontext
rule "rule2"
    when
    then
    System.out.println(drools.getRule().getName());
    System.out.println(kcontext.getKieRuntime().getEnvironment());
end

三、Query语法

![query](https://img-blog.csdnimg.cn/img_convert/7cdf82c54ac3ffbbcc3f528b13a8341c.png)

说明:Query支持可选参数集合,每一个参数都有可选的类型。如果没有指定类型,则默认为Object类型。,支持Query间嵌套调用;LHSRule中的LHS语法一致代表Pattern;Query为了更加紧凑的代码风格,增加了对positional syntax位置语法的支持。

1.基本查询

  • 无参
query "people under the age of 21"
    $person : Person( age < 21 )
end
  • 有参
query contains(String $s, String $c)
    $s := String( this.contains( $c ) )
end

说明: :=pattern with unification; 虽然drools不允许重复声明"绑定",但是支持跨多个属性的参数统一。

示例:

declare PersonFact
    firstName : String @position( 1 )
    lastName : String @position( 0 )
    age : int @position( 2 )
    id : int @position( 3 )
    occupation: String
end

rule "rule2"
when
    // 约束1 限定firstName和lastName;约束2 限定age范围;二者使用id统一参数声明内连接
    PersonFact("cube", $b:"jack"; $a: age, $id:= id)
    PersonFact(age<10, $id:= id)
then
 System.out.println($a);
end

pattern with unification

2.位置语法

positional syntax位置语法用于简化LHS部分代码;可混合使用常规语法命名语法,两者使用;分割,但是位置语法必须放于前;

Location类型为例:

declare Location
    thing : String
    location : String
end
  • Pattern示例-正确
Location(thing == x, location == y)
Location(x; location == y)
Location(x, y;) // x-代表position为0的字段即thing y-代表position为1的字段即location
Location(x; $p:location == y) // $p-代表patternBinding参数
Location(x; $p:location) // 同上
Location(x, $p;) // 同上
  • Pattern示例-错误
Location(thing == x, y;)
Location(thing == x; y;)

四、类型定义与注解

类型声明

事实类型元数据可描述在类级别和字段级别,语法格式为 @key(value),例如@position(0)

1.类型定义

  • 无元数据描述信息
import java.util.Date

declare Person
    name : String
    dateOfBirth : Date
    address : Address
end
  • 有元数据描述信息
import java.util.Date

declare Person
    @author( Bob )
    @dateOfCreation( 01-Feb-2009 )

    name : String @key @maxLength( 30 )
    dateOfBirth : Date
    address : Address
end

2.预定义的元数据标签

元数据作用域可选值默认值描述
@roleclassfact,eventfact该标记决定在复杂事件处理期间,给定的事实类型是作为常规事实对象处理还是作为Drools引擎中的事件对象处理。
@timestampclasssession clock 或自定义时间戳属性(可为attribute)session clock支持参数:会话时钟时间或自定义时间戳属性
@durationclass时间间隔或时间点(可为attribute)Null (zero)该标记决定Drools引擎中事件的持续时间
@expiresclass[#d][#h][#m][#s][[ms]]Null (默认时间不在匹配和激活时过期)此标记决定事件在Drools引擎的工作内存中过期之前的时间。默认情况下,当事件不再匹配和激活任何当前规则时,该事件将过期。您可以定义事件过期的时间。这个标记定义还覆盖了根据时间约束和KIE基中的滑动窗口计算的隐式过期偏移量。只有当Drools引擎以流模式运行时,此标记才可用。
@typesafeclasstrue,falsetrue该标记决定给定的事实类型是否使用类型安全进行编译。
@serialVersionUIDclassintegerNull序列化ID,考虑到兼容性建议在相关类或DRL文件类型声明过程中显式指定serialVersionUID(若未指定则根据类各方面特征自动计算)。
@keyattribute--参与计算equals()hashCode()的属性;同时Drools引擎会隐式地定义3个构造函数,分别为无参构造器、全参构造器、附带@key属性的构造器。
@positionattributeintegerNone优先级原则:子类>父类;显式指定>未指定;声明顺序
@classReactiveclass--全局开关处于开启状态(默认 ALWAYS),代表属性响应性生效;可以通过该标记禁用指定类的属性响应性,实现微调;ALLOWED对应,每次触发规则时重新评估事实的所有事实模式
@propertyReactiveclass--全局开关处于可选状态(ALLOWED)时,代表属性响应性停用;可以通过该标记启用指定类的属性响应性,实现微调;ALWAYS对应,仅对给定模式内受约束或绑定的修改属性做出反应。DISABLED状态时,所有属性更改侦听器都将被忽略(即不重新评估)。
@watchfactPatternProperty name, * (all), ! (not), !* (no properties)None全局开关处于开启状态(ALWAYS)或处于可选状态(ALLOWED)且类被标记为@propertyReactive时,总之,当前类型处于属性响应性状态时,你可以使用通配符或非操作过滤属性;处于非属性响应性,使用@watch或出现冲突例如@watch( firstName, ! firstName ))时编译错误
@propertyChangeSupportclass--增加该标记,可使drools可以监听javabean的属性变动。

下面以典型的电信领域语音呼叫为例

public class VoiceCall {
  private String  originNumber;
  private String  destinationNumber;
  private Date    callDateTime;
  private long    callDuration;  // in milliseconds

  // Constructors, getters, and setters
}
  • 类级别元数据
declare VoiceCall
  @role( event )
  @timestamp( callDateTime )
  @duration( callDuration )
  @expires( 1h35m )
  @typesafe( false )
  @serialVersionUID( 42 )
end
  • @key
declare Person
    firstName : String @key
    lastName : String @key
    age : int
end
  • @position
declare Person
    firstName : String @position( 1 )
    lastName : String @position( 0 )
    age : int @position( 2 )
    occupation: String
end

declare Student extends Person
    degree : String @position( 1 )
    school : String @position( 0 )
    graduationDate : Date
end
  • @watch
// Listens for changes in both `firstName` (inferred) and `lastName`:
Person(firstName == $expectedFirstName) @watch( lastName )

// Listens for changes in all properties of the `Person` fact:
Person(firstName == $expectedFirstName) @watch( * )

// Listens for changes in `lastName` and explicitly excludes changes in `firstName`:
Person(firstName == $expectedFirstName) @watch( lastName, !firstName )

// Listens for changes in all properties of the `Person` fact except `age`:
Person(firstName == $expectedFirstName) @watch( *, !age )

// Excludes changes in all properties of the `Person` fact (equivalent to using `@classReactivity` tag):
Person(firstName == $expectedFirstName) @watch( !* )
  • @propertyChangeSupport
declare Person
    @propertyChangeSupport
end

3.java中使用声明的FactType

import java.util.Date;

import org.kie.api.definition.type.FactType;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;

...

// Get a reference to a KIE base with the declared type:
KieBase kbase = ...

// Get the declared fact type:
FactType personType = kbase.getFactType("org.drools.examples", "Person");

// Create instances:
Object bob = personType.newInstance();

// Set attribute values:
personType.set(bob, "name", "Bob" );
personType.set(bob, "dateOfBirth", new Date());
personType.set(bob, "address", new Address("King's Road","London","404"));

// Insert the fact into a KIE session:
KieSession ksession = ...
ksession.insert(bob);
ksession.fireAllRules();

// Read attributes:
String name = (String) personType.get(bob, "name");
Date date = (Date) personType.get(bob, "dateOfBirth");

Tips:

a.实际使用过程中,可使用Map简化操作

org.kie.api.definition.type.FactType#setFromMap

org.kie.api.definition.type.FactType#getAsMap

b.尽管API的行为类似于Java反射,但API并不使用反射,而是依赖于使用生成的字节码实现的性能更好的访问器

参考:

drools官方文档

未完待续……

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Drools是一个开源的规则引擎,它提供了一种强大的方式来管理和执行业务规则。Drools中文文档是一份关于Drools规则引擎的中文说明文档Drools中文文档内容丰富全面,涵盖了Drools的基本概念、核心功能以及使用方法等方面的知识。文档开头介绍了Drools的发展背景和优势,让用户对规则引擎有一个整体的了解。 接下来,文档详细介绍了Drools的安装和配置方法,包括如何下载和安装Drools的各种版本,以及如何配置开发环境和运行时环境等。 文档还详细介绍了Drools的核心概念,如规则、事实、条件、动作等,以及它们在Drools中的表示和使用方式。特别是对于Drools规则的编写和调试,文档提供了大量的示例代码和实践经验,帮助用户更好地理解和应用规则引擎。 此外,文档还介绍了Drools的高级功能,如规则的冲突解决、规则的动态修改和重载、规则的优化等。它还说明了如何在实际项目中使用Drools来实现复杂的业务逻辑和规则。 总的来说,Drools中文文档提供了完整而易懂的Drools规则引擎的相关知识,适合初学者和有一定经验的用户阅读和学习。无论是想了解规则引擎的基本概念,还是希望实际应用Drools构建规则引擎,该文档都是一个很好的参考资料。它的中文说明和丰富的示例代码可以帮助用户更好地理解和应用Drools规则引擎,提高系统的可维护性和扩展性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值