当前位置:网站首页>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、完整代码
边栏推荐
- How to Add P-Values onto Horizontal GGPLOTS
- B high and beautiful code snippet sharing image generation
- conda常用命令汇总
- YYGH-BUG-04
- Depth filter of SvO2 series
- b格高且好看的代码片段分享图片生成
- PyTorch中repeat、tile与repeat_interleave的区别
- ESP32 Arduino 引入LVGL 碰到的一些问题
- mysql索引和事务
- HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
猜你喜欢
排序---
How does Premiere (PR) import the preset mogrt template?
Natural language processing series (I) -- RNN Foundation
Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
HOW TO ADD P-VALUES TO GGPLOT FACETS
YYGH-BUG-04
数据分析 - matplotlib示例代码
(C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
conda常用命令汇总
随机推荐
How to Create a Beautiful Plots in R with Summary Statistics Labels
Applet link generation
Log4j2
CONDA common command summary
The blink code based on Arduino and esp8266 runs successfully (including error analysis)
SVO2系列之深度濾波DepthFilter
基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)
SVO2系列之深度滤波DepthFilter
Input a three digit number and output its single digit, ten digit and hundred digit.
How to Visualize Missing Data in R using a Heatmap
自然语言处理系列(一)——RNN基础
【C语言】杨辉三角,自定义三角的行数
Find the factorial of a positive integer within 16, that is, the class of n (0= < n < =16). Enter 1111 to exit.
(C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
Analyse de l'industrie
XSS labs master shooting range environment construction and 1-6 problem solving ideas
Jenkins用户权限管理
测试左移和右移
How does Premiere (PR) import the preset mogrt template?
mysql索引和事务