一、背景

在咱们编写drools的规定文件的时候,有些时候须要用到全局变量,那么这个该怎么实现呢?有哪些注意事项呢?

二、前置常识

1、语法结构

2、全局变量的特点

  1. 应用global来定义全局变量,它能够为规定提供数据和服务
  2. 全局变量并不会被写入到工作内存中,因而咱们不可放到规定的约束条件中,即when的前面,除非这个全局变量是常量
  3. 如果不同的包中存在雷同标识符的常量,那么咱们的常量必须是雷同的类型,以便能够援用到雷同的值。官网原文: If you declare global variables with the same identifier in multiple packages, then you must set all the packages with the same type so that they all reference the same global value.
  4. 尽量不要应用全局变量在规定之间传递参数,如果须要在规定之间传递参数,咱们应该向工作内存中插入Fact对象。

三、需要

规定文件中定义常量,而后java代码中设置常量的值。

四、实现


失去论断:
1、数据类型须要统一。
2、常量是KieSession级别隔离的。

五、代码实现

https://gitee.com/huan1993/spring-cloud-parent/tree/master/drools/drools-drl-global-variable

六、参考链接

https://docs.drools.org/7.69.0.Final/drools-docs/html_single/index.html#drl-globals-con_drl-rules