当前位置:网站首页>Drools(8): WorkBench uses
Drools(8): WorkBench uses
2022-08-02 12:08:00 【Phoenix. Alexander. Wolf cub】
1 创建项目
进⼊主⻚,点击 Design 中的 projects 链接,进⼊项⽬管理界⾯
点击【Add Project】进⾏添加项⽬
填写项⽬名称及 Maven Related completions⽬创建
2 包创建
Click on the created item⽬,进⼊主界⾯,点击右上⻆的【Create New Asset】,选择【软件包】,Create data pairs Like storage bags com.chenj.order_rules.fact
All resources are passed【Create New Asset】进⾏创建,后⾯将不再赘述.
3 Data object creation
选择【数据对象】进⾏ Fact 的创建,⾸先创建 Order 对象
点击【添加字段】Add fields to the current object,Set the field type and description
添加字段amout;
添加字段score;
字段添加完成后,可以点击【源代码】查看⽣Whether the completed object conforms⾃⼰的要求,如果符合,点击
【保存】即可.
4 创建DRL规则⽂件
After the object definition is complete,我们来创建⼀个简单规则,同样创建 Asset,选择【DRL⽂件】
创建score-rules.drl
上⾯Click the OK button to complete the creationDRL⽂件后,⻚⾯Jumps to editDRL⽂件⻚⾯
可以看到DRL规则⽂件⻚⾯分为两个部分:Items on the left⽬浏览视图、右侧为编辑区域,It should be noted that the default display on the left is not an item⽬浏览视图,Need to click on⾯设置按钮,选择“资料库视图”和“显示为⽂件夹”,如下图所示:
在编辑DRL⽂件⻚⾯Enter the area on the right⾏DRL⽂件的编写,点击右上⻆Save button to complete the save operation,Click Check Test button to enter⾏规则⽂file syntax check
4 场景测试
After the rules are written,It needs to pass the test before it can be released⽣产,在 workbench 中,Testing can also be done through configuration.
点击创建 Asset,选择【测试场景】
In the pop-up Create Test Scenario window⼝middle record⼊测试场景的名称,点击确定完成操作
Add dependent data objects,And configure the test conditions and expected results
Because of the rules we write⽂The file needs to be from⼯for in-memory acquisitionOrder对象进⾏规则匹配,So it is required in test scenarios 要准备Order对象给⼯作内存,点击“GIVEN”button to pop up a new data record⼊窗⼝,选择Order类,输⼊框 中输⼊Fact name(名称任意),如下图
我们给⼯provided as memoryOrder对象还需要设置amout属性的值,点击“添加字段”button popup⼝,选择amout属性
点击amout属性后⾯的编辑按钮,The field value window pops up⼝
在弹出的窗⼝Click on the word⾯值按钮
amout后⾯Lost appears⼊框,可以为amout属性设置值
设置好amout属性的值后点击保存按钮保存测试场景
点击右上⻆“运⾏测试场景”按钮进⾏测试
5 规则发布
before the rules are published,需要先配置 KieBase 与 KieSession,也就是配置 kmodule.xml ⽂件,在 workbench ⾥⾯,kmodule.xml ⽂Pieces are also through the bounds⾯配置,然后⾃动⽣成的.
点击项⽬主⻚(Create New Asset 所在⻚⾯)右上⻆的【Settings】按钮,再选择【Knowledge Base and Sessions】
点击右上⻆“Compile”Buttons can be paired to items⽬进⾏编译,点击“Bulid&Deploy”按钮进⾏构建和部署.
部署成功后可以在本地mavenThe current item is seen in the repository⽬has been markedjar包:
6 Use deployed rules in your project
第⼀步:在IDEA中创建⼀个maven项⽬并在pom.xml⽂件中导⼊相关坐标
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>7.10.0.Final</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
第⼆步:在项目中创建⼀个数据对象Order,需要和WorkBench中创建的Order包名、The class names are exactly the same 同,属性也需要对应,我这边是com.xiong.order_rules.fact
package com.xiong.order_rules.fact;
public class Order {
private Integer amout;//订单原价⾦额
private Integer score;//积分
@Override
public String toString() {
return "Order{" +
"amout=" + amout +
", score=" + score +
'}';
}
public Integer getAmout() {
return amout;
}
public void setAmout(Integer amout) {
this.amout = amout;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
}
第三步:编写单元测试,远程加载maven仓库中的jarThe package finally completes the rule call⽤
package com.xiongpeng.test;
import com.xiong.order_rules.fact.Order;
import org.drools.core.io.impl.UrlResource;
import org.junit.Test;
import org.kie.api.KieServices;
import org.kie.api.builder.KieModule;
import org.kie.api.builder.KieRepository;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import java.io.InputStream;
public class TestWorkBench {
@Test
public void test1() throws Exception {
//通过此URL可以访问到maven仓库中的jar包
// URL地址构成:http://ip地址:Tomcat端⼝号/WorkBench⼯程名/maven2/坐标/版 本号/xxx.jar
String url = "http://localhost:8080/kie-drools-wb/maven2/com/xiong/order-rules/1.0.0/order-rules-1.0.0.jar";
KieServices kieServices = KieServices.Factory.get();
// 通过Resource资源对象加载jar包
UrlResource resource = (UrlResource) kieServices.getResources().newUrlResource(url);
// 通过Workbench提供的服务来访问maven仓库中的jar包资源,需要先进⾏Workbench 的认证
resource.setUsername("kie");
resource.setPassword("kie");
resource.setBasicAuthentication("enabled");
//Convert resources to losses⼊流,Lose through this⼊流可以读取jar包数据
InputStream inputStream = resource.getInputStream();
//创建仓库对象,仓库对象中保存Drools的规则信息
KieRepository repository = kieServices.getRepository();
//通过输⼊流读取maven仓库中的jar包数据,包装成KieModule模块添加到仓库中
KieModule kieModule = repository.addKieModule(kieServices.getResources().newInputStreamResource(inputStream));
//基于KieModule模块创建容器对象,从容器中可以获取session会话
KieContainer kieContainer = kieServices.newKieContainer(kieModule.getReleaseId());
KieSession session = kieContainer.newKieSession();
Order order = new Order();
order.setAmout(500);
session.insert(order);
session.fireAllRules();
session.dispose();
}
}
执⾏Unit tests can find that the console has output relevant content.通过WorkBench修改规则输出内容并发布,再次执⾏Unit tests can also find the content of console output⽣了变化.
通过上⾯cases can be found,我们在IEDAitem developed in ⽬There are no rules written in it⽂件,规则⽂Pieces are we passWorkBench开发并安装部署到maven仓库中,我们⾃⼰开发的项⽬Just need to load remotelymaven仓库中的jar The package can complete the adjustment of the rules⽤.这种开发⽅The benefit of the formula is our response⽤It can be completely separated from business rules,同时通过WorkBenchOur response after modifying the rules⽤It can be loaded into the latest rule slave without any modification⽽Implement dynamic changes to rules.
边栏推荐
- 【kali-信息收集】(1.9)Metasploit+搜索引擎工具Shodan
- Failure Analysis | A SELECT statement crashes MySQL, what happened?
- Likou 209 - String with the Minimum Length - Sliding Window Method
- 受邀出席Rust开发者大会|Rust如何助力量化高频交易?
- 从幻核疑似裁撤看如何保证NFT的安全
- Transfer files between servers
- 面积曲线AUC(area under curve)
- 阿苹的思考
- SQL函数 TRIM
- 看我如何用多线程,帮助运营小姐姐解决数据校对系统变慢!
猜你喜欢
当POC遇见RPA:RPA项目顺利实施的关键
- [email protected] This version of tar is no longer supported, and will not receive"/>
npm WARN deprecated [email protected] This version of tar is no longer supported, and will not receive
WebUI自动化测试框架搭建从0到1(完整源码)更新完毕
力扣58-左旋转字符串
主流跨端技术一览
看我如何用多线程,帮助运营小姐姐解决数据校对系统变慢!
Thymeleaf
使用mosquitto过程中的问题解决
数字化转型中的低代码
After Effects 教程,如何在 After Effects 中对蒙版进行动画绘制?
随机推荐
分布式限流利器,手撕&redisson实现
看我如何用多线程,帮助运营小姐姐解决数据校对系统变慢!
运行yum报错Error: Cannot retrieve metalink for reposit
【云驻共创】数据工坊平台,0代码开发数据处理业务“快”人一步
Likou 704 - binary search
打破千篇一律,DIY属于自己独一无二的商城
使用kubesphere图形界面创建一个devops的CI/CD流程
The ex-boyfriend bought chili water and threatened to rob his daughter. Can the woman apply for a personal safety protection order?
手撸架构,网络 面试36问
Crack detection technology based on deep learning
【kali-信息收集】(1.9)Metasploit+搜索引擎工具Shodan
当POC遇见RPA:RPA项目顺利实施的关键
MySQL主从复制几个重要的启动选项
Create your own app applet ecosystem with applet containers
力扣977-有序数组的平方——暴力法&双指针法
今日睡眠质量记录85分
How to connect TDengine through DBeaver?
力扣209-长度最小的字符串——滑动窗口法
数字化转型中的低代码
Pod调度策略:亲和性、污点与污点容忍