小工具知识网

当前位置: 首页 > 行业知识 > Java-springboot集成+drools规则引擎集成,动态化编程

行业知识

Java-springboot集成+drools规则引擎集成,动态化编程

2026-10-25 20:38:43 来源:互联网转载

drools+springboot集成

添加drools相关pom依赖
// 第一种:kie-spring 整体引入            org.kie            kie-spring            ${kie.version}                            org.drools            drools-core            ${kie.version}                        // 第二种:单独引入                         7.24.0.Final                                     org.drools            drools-core            ${drools.version}                            org.drools            drools-compiler            ${drools.version}                                    org.drools            drools-decisiontables            ${drools.version}                                    org.drools            drools-templates            ${drools.version}                            org.kie            kie-api            ${drools.version}        
创建Kie相关 Bean 使其被spring管理
@Configurationpublic class KiaSessionConfig {    private static final String RULES_PATH = "rules/";    @Bean    public KieFileSystem kieFileSystem() throws IOException {        KieFileSystem kieFileSystem = getKieServices().newKieFileSystem();        for (Resource file : getRuleFiles()) {            kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_PATH + file.getFilename(), "UTF-8"));        }        return kieFileSystem;    }    private Resource[] getRuleFiles() throws IOException {        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();        final Resource[] resources = resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "**/*.*");        return resources;    }    @Bean    public KieContainer kieContainer() throws IOException {        final KieRepository kieRepository = getKieServices().getRepository();        kieRepository.addKieModule(new KieModule() {            public ReleaseId getReleaseId() {                return kieRepository.getDefaultReleaseId();            }        });        KieBuilder kieBuilder = getKieServices().newKieBuilder(kieFileSystem());        kieBuilder.buildAll();        return getKieServices().newKieContainer(kieRepository.getDefaultReleaseId());    }    private KieServices getKieServices() {        return KieServices.Factory.get();    }    @Bean    public KieBase kieBase() throws IOException {        return kieContainer().getKieBase();    }    @Bean    public KieSession kieSession() throws IOException {        return kieContainer().newKieSession();    }}
创建实体对象People
@Datapublic class People {    private int sex;    private String name;    private String drlType;    public People(int sex, String name, String drlType) {        this.sex = sex;        this.name = name;        this.drlType = drlType;    }    public People() {    }}
创建规则文件
package com.lvyuanj.micro.updatdialect "java"import com.lvyuanj.micro.entity.Peoplerule 'update1'    when       $p : People(drlType == "update" && sex == 0)    then        System.out.println("update1执行====" + $p);        $p.setSex(1);        update($p)    endrule 'update2'    when       $p : People(drlType == "update" && sex == 1)    then        System.out.println("update2执行====" + $p);    endrule 'modify'    when      $p : People(drlType == "update" && sex == 1)    then       System.out.println("update3执行====" + $p);       modify($p){          setSex(-1)       }    end
创建测试方法
@SpringBootTest@RunWith(SpringRunner.class)class DroolsApplicationTests {    @Autowired    private KieSession session;        @Autowired    private KieBase kieBase;    @Test    public void people() {            People people = new People();        people.setName("sun");        people.setSex(1);        people.setDrlType("people");        session.insert(people);//插入        session.fireAllRules();//执行规则    }    @AfterEach    public void runDispose() {        session.dispose();//释放资源    }}
drl文件结构
  1. package 包充当每组规则的唯一名称空间。一个规则库可以包含多个程序包。

    ()

    通常,将包的所有规则与包声明存储在同一文件中,以便包是独立的。但是,

    也可以在规则中使用从其他的包中导入的对象

    ()
  2. imports 与Java中的import语句类似,用来标识在规则中使用的任何对象的标准路径和类型名称

  3. factions 函数代码块如:

    function String hello(String applicantName) {    return "Hello " + applicantName + "!";}
  4. queries 在Drools引擎的工作内存中搜索与DRL文件中的规则相关的事实

    query "people under the age of 21"    $person : Person( age  
    5. global 为规则提供数据或服务
2.1 rules 规则
属性描述
salience定义规则优先级的整数,数值越大,优先级越高
enabled规则启用开关
date-effective包含日期和时间定义的字符串。仅当当前日期和时间在date-effective属性之后时,才能激活该规则。
date-expires如果当前日期和时间在date-expires属性之后,则无法激活该规则。
no-loop选择该选项后,如果规则的结果重新触发了先前满足的条件,则无法重新激活(循环)规则。如果未选择条件,则在这些情况下可以循环规则。
agenda-group标识要向其分配规则的议程组
activation-group激活组,在激活组中,只能激活一个规则。触发的第一个规则将取消激活组中所有规则的所有未决激活。
duration定义了如果仍满足规则条件,则可以激活规则的持续时间(以毫秒为单位)。
timercron定时表达式
calendar时钟
auto-focus仅适用于议程组中的规则。选择该选项后,下次激活该规则时,将自动将焦点分配给分配了该规则的议程组。
lock-on-activeno-loop属性的更强版
ruleflow-group标识规则流组的字符串
dialect用于标识规则中的代码表达式JAVA或MVEL将其用作语言
2.2 匹配模式

2.2.1 没有约束的匹配模式

实事不需要满足任何条件,若类型相同,则触发该规则,如:

package com.ytooo.beanimport com.ytooo.bean.Peopledialect  "java"rule "girl"    when        People()    then        System.out.println("girl规则执行");end

2.2.2 有条件约束的匹配模式

实事类型相同,且满足条件,则触发该规则,如:

package com.ytooo.beanimport com.ytooo.bean.Peopledialect  "java"rule "girl"    when        People(sex == 0 && drlType == "people")    then        System.out.println("girl规则执行");end

2.2.3 匹配并绑定属性以及实事

实事类型相同,且满足条件,则触发该规则,并绑定数据,如:

package com.ytooo.beanimport com.ytooo.bean.Peopledialect  "java"rule "girl"    when         $p:People(sex == 0,$sex : sex && drlType == "people")    then        System.out.println("girl规则执行");        System.out.println($p);        System.out.println($sex);end

2.3 条件

and,or 等结合规则条件的多个模式,没有定义关键字连词,默认是and:

package com.ytooo.beanimport com.ytooo.bean.Peopleimport com.ytooo.bean.Catdialect  "java"rule "girl"    when        People(sex == 0) and        Cat(sex == 0)    then        System.out.println("girl规则执行");end

2.4 约束

标准Java运算符优先级适用于DRL中的约束运算符,而drl运算符除==和!=运算符外均遵循标准Java语义。

在drl中 Person( firstName != “John” )类似于 !java.util.Objects.equals(person.getFirstName(), “John”)

约束描述
!.使用此运算符可以以空安全的方式取消引用属性。!.运算符左侧的值不能为null(解释为!= null)
[]按List索引访问值或Map按键访问值
=在具有自然顺序的属性上使用这些运算符
==, !=在约束中使用这些运算符作为equals()和!equals()方法
&&,||组合关系条件
matches,not matches使用这些运算符可以指示字段与指定的Java正则表达式匹配或不匹配
contains,not contains使用这些运算符可以验证Array或字段是否包含或不包含指定值
memberOf,not memberOf使用这些运算符可以验证字段是否为定义为变量Array的成员
soundslike使用英语发音来验证单词是否具有与给定值几乎相同的声音(类似于该matches运算符)
in,notin使用这些运算符可以指定一个以上的可能值来匹配约束(复合值限制)

2.4.1 约束

matches, not matches

Person( country matches "(USA)?\S*UK" )Person( country not matches "(USA)?\S*UK" )

matches, not matches

FamilyTree(countries contains "UK" )Person( fullName not contains "Jr" )FamilyTree(countries contains $var)Person( fullName not contains $var )

matches, not matches

FamilyTree( person memberOf $europeanDescendants )FamilyTree( person not memberOf $europeanDescendants )

2.5 集合

package com.ytooo.bean;import lombok.Data;import java.util.List;/** *  */@Datapublic class Animal {    private Integer level;    private List peoples;}

2.5.1 from 取集合中的元素

/** *  */@Datapublic class Animal {    private Integer level;    private List peoples;}

新建from.drl

package com.lvyuanj.micro.frmdialect "java"import com.lvyuanj.micro.entity.Peopleimport com.lvyuanj.micro.entity.Animalrule "from"when    $an : Animal()    $p : People(sex != 3 && drlType == "from") from $an.peoples    then        System.out.println($p);    end

新建测试方法

@Testpublic void from() {    People p1 = new People(1,"达","from");    People p2 = new People(0,"秋","from");    People p3 = new People(3,"金","from");    Animal animal = new Animal();    animal.setPeoples(new ArrayList());    animal.getPeoples().add(p1);    animal.getPeoples().add(p2);    animal.getPeoples().add(p3);    session.insert(animal);//插入    session.fireAllRules();//执行规则}

2.5.2 collect

从指定来源或从Drools引擎的工作内存中获取集合,可以使用Java集合(例如List,LinkedList和HashSet)

上一篇:三免三减半网上备案流程怎么写

下一篇:板钣金服理财详解,收益与风险剖析,投资价值揭秘!