当前位置:网站首页>drools执行String规则或执行某个规则文件
drools执行String规则或执行某个规则文件
2022-07-02 09:42:00 【huan_1993】
1、背景
此处主要记录一下2个小的知识点,防止以后忘记。
1、如何在drools中执行某个drl文件。
2、如果我们的规则是一个String类型的字符串,那么该如何执行。
2、实现
2.1 执行指定的drl文件
KieHelper kieHelper = new KieHelper();
// 获取指定的drl文件
Resource resource = ResourceFactory.newClassPathResource("rules/alway-rule.drl", "UTF-8");
kieHelper.addResource(resource,ResourceType.DRL);
KieBase kieBase = kieHelper.build();
KieSession kieSession = kieBase.newKieSession();
kieSession.fireAllRules();
kieSession.dispose();
2.2 执行String规则
String drl = "package rules\n" +
"\n" +
"rule \"rule-01\"\n" +
" when\n" +
" $i: Integer()\n" +
" then \n" +
" System.out.println(\"规则:[\"+drools.getRule().getName()+\"]执行,规则内存中存在的值是:\"+$i);\n" +
"end";
KieHelper kieHelper = new KieHelper();
kieHelper.addContent(drl, ResourceType.DRL);
// 创建KieBase是一个成本很大的
KieBase kieBase = kieHelper.build(EqualityBehaviorOption.IDENTITY);
System.out.println(kieBase);
// 创建KieSession成本小
KieSession kieSession = kieBase.newKieSession();
kieSession.insert(123);
kieSession.fireAllRules();
kieBase.removeRule("rules", "rule-01");
kieSession.insert(456);
kieSession.fireAllRules();
kieSession.dispose();
需要注意的是KieBase的创建成本是很大的,因此如果真的要这样用必要的缓存是需要的。
3、完整代码
边栏推荐
- Dynamic memory (advanced 4)
- 5g era, learning audio and video development, a super hot audio and video advanced development and learning classic
- Some problems encountered in introducing lvgl into esp32 Arduino
- Leetcode122 the best time to buy and sell stocks II
- How to Create a Beautiful Plots in R with Summary Statistics Labels
- How to Visualize Missing Data in R using a Heatmap
- lombok常用注解
- jenkins 凭证管理
- Differences between nodes and sharding in ES cluster
- How to Add P-Values onto Horizontal GGPLOTS
猜你喜欢

How to Create a Beautiful Plots in R with Summary Statistics Labels

Take you ten days to easily finish the finale of go micro services (distributed transactions)

SVO2系列之深度滤波DepthFilter

How to Visualize Missing Data in R using a Heatmap

Larvel modify table fields

How to Add P-Values onto Horizontal GGPLOTS

深入理解PyTorch中的nn.Embedding

From scratch, develop a web office suite (3): mouse events

Test shift left and right

Data analysis - Matplotlib sample code
随机推荐
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
二分刷题记录(洛谷题单)区间的甄别
Depth filter of SvO2 series
Leetcode209 长度最小的子数组
(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?
The blink code based on Arduino and esp8266 runs successfully (including error analysis)
自然语言处理系列(三)——LSTM
Gaode map test case
排序---
GGPlot Examples Best Reference
Codeforces 771-div2 C (trouble, permutation is not very good)
自然语言处理系列(二)——使用RNN搭建字符级语言模型
Filtre de profondeur de la série svo2
基于Arduino和ESP8266的连接手机热点实验(成功)
b格高且好看的代码片段分享图片生成
【2022 ACTF-wp】
Natural language processing series (II) -- building character level language model using RNN
史上最易懂的f-string教程,收藏这一篇就够了
Leetcode122 the best time to buy and sell stocks II
mysql索引和事务