当前位置:网站首页>Quick Start to Drools Rule Engine (1)
Quick Start to Drools Rule Engine (1)
2022-08-05 06:44:00 【User Nickname 23】
目录
二、droolsThe specific steps for the development and implementation of the rule engine are as follows:
第一步:创建maven项目droolsSpring,在创建好的项目pom.xml文件中添加drools依赖包
第二步:根据drools要求在maven项目目录下创建?resources/META-INF/kmodule.xml
第四步:创建规则文件resources/rules/score-rules.drl,规则文件的内容如下:
一、Drools环境集成
Integrated use in the projectdrools时,Can be used independently or integratedspring使用,If it is used alone, it is directly importedmaven依赖包:如下
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-spring</artifactId>
<version>7.68.0.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>7.68.0.Final</version>
</dependency>
If the development tools used are IDEA,那么在file->setting目录下
点击pluginsSearch and install in the plugin marketdrools插件 ,如图
二、droolsThe specific steps for the development and implementation of the rule engine are as follows:
三、drools快速入门
业务场景描述
The rules of bonus points corresponding to the amount consumed by the user are as follows:
//消费100元以下 不加分
//消费100元-500元 加100分
//消费500元-1000元 加500分
//消费1000元以上 加1000分
四、开发实现
第一步:创建maven项目droolsSpring,在创建好的项目pom.xml文件中添加drools依赖包
第二步:根据drools要求在maven项目目录下创建resources/META-INF/kmodule.xml
There needs to be a configuration file that tells the code the rules filedrl在哪里,在drools中这个文件就是kmodule.xml
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
<!--
name:指定kbase的名称,可以任意,但是必须唯一
packages:指定规则文件的目录,需要根据实际情况填写,否则无法加载到规则文件
default:指定当前kbase是否为默认
-->
<kbase name="SimpleRuleKBase" packages="rules">
<!--
name:指定ksession的名称,可以任意,但是必须唯一
default:指定当前ksession是否为默认
-->
<ksession name="simpleRuleKSession"/>
</kbase>
</kmodule>
注意:The names and locations in the configuration file are fixed,不能更改
Kmodule中可以包含一个到多个kbase,分别对应drl的规则文件.
kbase需要一个唯一的name,可以取任意字符串.
packages为drl文件所在resource目录下的路径.注意分区drl文件中的package与此处的package不一定相同.多个包用逗号分隔,默认情况下会扫描resources目录下的所有(包含子目录)规则文件.
kbase的default属性,标示当前KieBase是不是默认的,If it is the default, it can be found directly without the nameKieBase,但每个moduleThere can be at most one defaultKieBase.
kbase下面可以有一个或多个ksession,ksession的name属性必须设置,且必须唯一.
第三步:创建订单实体类Order
package com.hx.rules.entity;
import lombok.Data;
@Data
public class Order {
/**
* 订单金额
*/
private int amount;
/**
* 积分
*/
private int score;
@Override
public String toString() {
return "Order{" +
"amount=" + amount +
", score=" + score +
'}';
}
}
第四步:创建规则文件resources/rules/score-rules.drl,规则文件的内容如下:
package rules;
import com.hx.rules.entity.Order;
//100No points will be added for the following
rule "score_1" //The name needs to be unique
when
$order:Order(amount < 100);
then
$order.setScore(0);
System.out.println("触发规则,100No points will be added for the following");
end
//100元-500元 加100分
rule "score_2"
when
$order:Order(100 < amount && amount< 500)
then
$order.setScore(100);
System.out.println("触发规则,100元-500元 加100分");
end
//500元-1000元 加500分
rule "score_3"
when
$order:Order(500 < amount && amount < 1000)
then
$order.setScore(500);
System.out.println("触发规则,500元-1000元 加500分");
end
//1000元以上 加1000分
rule "score_4"
when
$order:Order(1000 < amount)
then
$order.setScore(1000);
System.out.println("触发规则,500元-1000元 加500分");
end
第五步:编写测试类TestDrools.
情况一:module文件中kbaseSpecify a specific name,并且default设置为true,测试类的代码如下:
package com.hx.rules;
import com.hx.rules.entity.Order;
import org.junit.Test;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
public class TestDrools {
@Test
public void test() {
//第一步 获取KieServices
KieServices kieServices = KieServices.Factory.get();
//第二步获取kieContainer
KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer();
//第三步获取kieSession
KieSession simpleRuleKSession = kieClasspathContainer.newKieSession();
//Create a new fact object
Order order = new Order();
order.setAmount(235);
//第四步 Insert a fact object into session中
simpleRuleKSession.insert(order);
//第五步 执行规则引擎
simpleRuleKSession.fireAllRules();
//第六步 Shut down the rules engine
simpleRuleKSession.dispose();
System.out.println("规则执行完成,关闭规则");
}
}
注意:
KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer(); //第三步获取kieSession KieSession simpleRuleKSession = kieClasspathContainer.newKieSession();
in both ends of the codecontainer和kiesessionWireless designationname也能查找到KieBase
情况二:module文件中kbaseSpecify a specific name,default未设置或者设置为false,测试类的代码如下:
package com.hx.rules;
import com.hx.rules.entity.Order;
import org.junit.Test;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
public class TestDrools {
@Test
public void test() {
//第一步 获取KieServices
KieServices kieServices = KieServices.Factory.get();
//第二步获取kieContainer
KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer("SimpleRuleKBase");
//第三步获取kieSession
KieSession simpleRuleKSession = kieClasspathContainer.newKieSession("simpleRuleKSession");
//Create a new fact object
Order order = new Order();
order.setAmount(235);
//第四步 Insert a fact object into session中
simpleRuleKSession.insert(order);
//第五步 执行规则引擎
simpleRuleKSession.fireAllRules();
//第六步 Shut down the rules engine
simpleRuleKSession.dispose();
System.out.println("规则执行完成,关闭规则");
}
}
The test code is written one by one according to the steps described in the annotations,Executing the code yields the following result:
Seeing the corresponding rule output indicates that the case test is successful
DroolsGet started quickly with the rules engine(二)_的博客-CSDN博客
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在.深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小.自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前.因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担.添加下方名片,即可获取全套学习资料哦
边栏推荐
猜你喜欢
Introduction to Network Layer Protocols
el-autocomplete use
设置文本向两边居中展示
单片机原理与应用复习
BIO,NIO,AIO实践学习笔记(便于理解理论)
Some basic method records of commonly used languages in LeetCode
LeetCode practice and self-comprehension record (1)
教您简单几步实现工业树莓派正确安装RS232转USB驱动
[issue resolved] - jenkins pipeline checkout timeout
图像处理、分析与机器视觉一书纠错笔记
随机推荐
无法导入torchvision.io.read_image
[issue resolved] - jenkins pipeline checkout timeout
带你深入了解Cookie
BIO,NIO,AIO实践学习笔记(便于理解理论)
网络排错基础-学习笔记
D39_ coordinate transformation
云计算基础-学习笔记
VLAN is introduced with the experiment
Does flink cdc currently support Gauss database sources?
数组&的运算
DevOps - Understanding Learning
Transformer详细解读与预测实例记录
[ingress]-ingress使用tcp端口暴露服务
Come, come, let you understand how Cocos Creator reads and writes JSON files
Some basic method records of commonly used languages in LeetCode
深入分析若依数据权限@datascope (注解+AOP+动态sql拼接) 【循序渐进,附分析过程】
LeetCode刷题记录(2)
DisabledDate date picker datePicker
flink cdc 目前支持Gauss数据库源吗
媒体查询、rem移动端适配