当前位置:网站首页>Use of rule engine drools
Use of rule engine drools
2022-07-26 04:05:00 【Grab】
introduce 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>Tool class encapsulation
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 Architecture rule tool
*
* @author Xiang Zhenhua
* @date 2022/06/30 17:29
*/
public class KieUtils {
/**
* Execute all rules
*
* @param entity Entity classes in rule files
* @param globals Global variables
* @param drl Rules file
*/
public static void fireAllRules(Object entity, Map<String, Object> globals, String drl) {
// Conversation object , Used to interact with the rule engine
KieSession kieSession = getKieSession(entity, globals, drl);
// Activate rule engine , If the rule match is successful, execute the rule
kieSession.fireAllRules();
// Close session
kieSession.dispose();
}
/**
* Execute all rules
*
* @param kSession
*/
public static void fireAllRules(KieSession kSession) {
// Activate rule engine , If the rule match is successful, execute the rule
kSession.fireAllRules();
// Close session
kSession.dispose();
}
/**
* obtain KieSession
*
* @param entity Entity classes in rule files
* @param globals Global variables
* @param drl Rules file
* @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;
}
/**
* obtain KieSession
*
* @param drls Rules file
* @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);
// establish KieSession
return kieBase.newKieSession();
}
/**
* Check rule file
*
* @param drl Rules file
*/
public static void verify(String drl) {
KieHelper kieHelper = new KieHelper();
kieHelper.addContent(drl, ResourceType.DRL);
verify(kieHelper);
}
/**
* check
*
* @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(" Rule file error ");
for (Message message : messages) {
error.append(message.getText()).append(";");
}
throw new RuntimeException(error.toString());
}
}
}Definition entity
/**
* @author Xiang Zhenhua
* @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 + '\'' +
'}';
}
}Create a rule file demo.drl
package demo
import com.xzh.drools.entity.DemoEntity
rule "demo"
when
$demo:DemoEntity(name contains " Xiao Ming ")
then
$demo.setRemark(" hello " + $demo.getName());
System.out.println(" Rule enforcement ---> " + $demo.getRemark());
endtest
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class DemoTest {
@Test
public void test1() throws IOException {
DemoEntity entity = new DemoEntity(" Xiao Ming ");
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);
}
}result
Rule enforcement ---> Hello, Xiaoming 边栏推荐
- Educational Codeforces Round 132 (Rated for Div. 2) E. XOR Tree
- 如何构建面向海量数据、高实时要求的企业级OLAP数据引擎?
- `Oi problem solving ` ` leetcode '2040. The k-th minor product of two ordered arrays
- LeetCode:1184. 公交站间的距离————简单
- Connect external MySQL databases in istio Service Grid
- Lua and go mixed call debugging records support cross platform (implemented through C and luajit)
- [Reading Notes - > data analysis] Introduction to BDA textbook data analysis
- Overview of wavelet packet transform methods
- 2021 CIKM |GF-VAE: A Flow-based Variational Autoencoder for Molecule Generation
- 智装时代已来,智哪儿邀您一同羊城论剑,8月4日,光亚展恭候
猜你喜欢

(translation) timing of website flow chart and user flow chart

《opencv学习笔记》-- 边缘检测和canny算子、sobel算子、LapIacian 算子、scharr滤波器

基于移位寄存器的同步FIFO

Verilog implementation of key dithering elimination

PHP method to find the location of session storage file

CPU and GPU are out of date, and the era of NPU and APU begins

Apple removed the last Intel chip from its products

Helloworld案例分析

Basic line chart: the most intuitive presentation of data trends and changes

firewall 命令简单操作
随机推荐
5年1.4W倍,NFT OG 的封神之路|Web3专栏
Linear basis property function code to achieve 3000 words detailed explanation, with examples
深度学习之SuperViT
In PHP, you can use the abs() function to turn negative numbers into positive numbers
【云原生之kubernetes】kubernetes集群下ConfigMap使用方法
Trust sums two numbers
Go plus security: an indispensable security ecological infrastructure for build Web3
[unity3d shader] character projection and reflection
[in depth study of 4g/5g/6g topic-42]: urllc-13 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -7-low delay technology-1-subcarrier spacing expansio
Opencv learning notes -- Hough transform
Luoda development - audio stream processing - AAC / loopbacktest as an example
php中可以使用取绝对值函数 abs() 将负数转成正数
`Oi problem solving ` ` leetcode '2040. The k-th minor product of two ordered arrays
【云原生】谈谈老牌消息中间件ActiveMQ的理解
按键消抖的Verilog实现
php 实现从1累加到100的算法
JS upload avatar (you can understand it after reading it, trust me)
How to build an enterprise level OLAP data engine for massive data and high real-time requirements?
基于移位寄存器的同步FIFO
1311_ Hardware design_ Summary of ICT concept, application, advantages and disadvantages