当前位置:网站首页>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、完整代码
边栏推荐
- H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
- YYGH-BUG-05
- Take you ten days to easily finish the finale of go micro services (distributed transactions)
- 多文件程序X32dbg动态调试
- Some problems encountered in introducing lvgl into esp32 Arduino
- to_bytes与from_bytes简单示例
- PgSQL string is converted to array and associated with other tables, which are displayed in the original order after matching and splicing
- mysql索引和事务
- Map和Set
- QT meter custom control
猜你喜欢

Differences between nodes and sharding in ES cluster

Map和Set

Some problems encountered in introducing lvgl into esp32 Arduino

机械臂速成小指南(七):机械臂位姿的描述方法
![[C language] convert decimal numbers to binary numbers](/img/9b/1848b68b95d98389ed985c83f2e856.png)
[C language] convert decimal numbers to binary numbers
![[visual studio 2019] create MFC desktop program (install MFC development components | create MFC application | edit MFC application window | add click event for button | Modify button text | open appl](/img/6a/111da81436659c7502648907ec1367.jpg)
[visual studio 2019] create MFC desktop program (install MFC development components | create MFC application | edit MFC application window | add click event for button | Modify button text | open appl

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

Natural language processing series (II) -- building character level language model using RNN

YYGH-BUG-04

Dynamic debugging of multi file program x32dbg
随机推荐
还不会安装WSL 2?看这一篇文章就够了
Filtre de profondeur de la série svo2
GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
全链路压测
Maximum profit of jz63 shares
lombok常用注解
Map和Set
倍增 LCA(最近公共祖先)
Industry analysis
多文件程序X32dbg动态调试
Full link voltage measurement
[geek challenge 2019] upload
基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
Mish-撼动深度学习ReLU激活函数的新继任者
Easyexcel and Lombok annotations and commonly used swagger annotations
YYGH-10-微信支付
深入理解P-R曲线、ROC与AUC
H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开
Those logs in MySQL