规则引擎Drools的使用
Posted 抓手
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了规则引擎Drools的使用相关的知识,希望对你有一定的参考价值。
引入maven
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-templates</artifactId>
<version>7.14.0.Final</version>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-spring</artifactId>
<version>7.14.0.Final</version>
</dependency>
工具类封装
import org.kie.api.KieBase;
import org.kie.api.KieBaseConfiguration;
import org.kie.api.builder.Message;
import org.kie.api.builder.Results;
import org.kie.api.conf.EventProcessingOption;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieSession;
import org.kie.internal.utils.KieHelper;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
/**
* drools架构规则工具
*
* @author 向振华
* @date 2022/06/30 17:29
*/
public class KieUtils
/**
* 执行全部规则
*
* @param entity 规则文件中的实体类
* @param globals 全局变量
* @param drl 规则文件
*/
public static void fireAllRules(Object entity, Map<String, Object> globals, String drl)
//会话对象,用于和规则引擎交互
KieSession kieSession = getKieSession(entity, globals, drl);
//激活规则引擎,如果规则匹配成功则执行规则
kieSession.fireAllRules();
//关闭会话
kieSession.dispose();
/**
* 执行全部规则
*
* @param kSession
*/
public static void fireAllRules(KieSession kSession)
//激活规则引擎,如果规则匹配成功则执行规则
kSession.fireAllRules();
//关闭会话
kSession.dispose();
/**
* 获取KieSession
*
* @param entity 规则文件中的实体类
* @param globals 全局变量
* @param drl 规则文件
* @return
*/
public static KieSession getKieSession(Object entity, Map<String, Object> globals, String drl)
KieSession kieSession = getKieSession(drl);
kieSession.insert(entity);
if (!CollectionUtils.isEmpty(globals))
for (String key : globals.keySet())
kieSession.setGlobal(key, globals.get(key));
return kieSession;
/**
* 获取KieSession
*
* @param drls 规则文件
* @return
*/
public static KieSession getKieSession(String... drls)
KieHelper kieHelper = new KieHelper();
for (String drl : drls)
kieHelper.addContent(drl, ResourceType.DRL);
verify(kieHelper);
KieBaseConfiguration config = kieHelper.ks.newKieBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
KieBase kieBase = kieHelper.build(config);
//创建KieSession
return kieBase.newKieSession();
/**
* 校验规则文件
*
* @param drl 规则文件
*/
public static void verify(String drl)
KieHelper kieHelper = new KieHelper();
kieHelper.addContent(drl, ResourceType.DRL);
verify(kieHelper);
/**
* 校验
*
* @param kieHelper
*/
private static void verify(KieHelper kieHelper)
Results results = kieHelper.verify();
List<Message> messages = results.getMessages(Message.Level.WARNING, Message.Level.ERROR);
if (!CollectionUtils.isEmpty(messages))
StringBuilder error = new StringBuilder("规则文件错误 ");
for (Message message : messages)
error.append(message.getText()).append(";");
throw new RuntimeException(error.toString());
定义entity
/**
* @author 向振华
* @date 2022/06/30 16:11
*/
public class DemoEntity
private String name;
private String remark;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getRemark()
return remark;
public void setRemark(String remark)
this.remark = remark;
public DemoEntity()
public DemoEntity(String name)
this.name = name;
@Override
public String toString()
return "DemoEntity" +
"name='" + name + '\\'' +
", remark='" + remark + '\\'' +
'';
创建规则文件demo.drl
package demo
import com.xzh.drools.entity.DemoEntity
rule "demo"
when
$demo:DemoEntity(name contains "小明")
then
$demo.setRemark("哈喽" + $demo.getName());
System.out.println("规则执行 ---> " + $demo.getRemark());
end
测试
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class DemoTest
@Test
public void test1() throws IOException
DemoEntity entity = new DemoEntity("小明");
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
Resource resource = resourcePatternResolver.getResource("classpath:rules/demo.drl");
byte[] bytes = IOUtils.toByteArray(resource.getInputStream());
String drl = new String(bytes);
KieUtils.fireAllRules(entity, null, drl);
结果
规则执行 ---> 哈喽小明
以上是关于规则引擎Drools的使用的主要内容,如果未能解决你的问题,请参考以下文章
规则引擎Drools介绍使用及SpringBoot整合Drools