Drools7 Hello Wrold 入门详细步骤--系列01课
一、什么叫规则引擎?
规则---》写在文档上
引擎---》在java代码上,引用这个文档上的规则
二、drools规则引擎有什么用?
简单来说就是将多变的规则,从业务代码中剥离出来
(当规则变了之后,不用修改代码,而是修改一份文本,这样就可以不用编译,打包,上线,这些过程就省略了,省了很多时间呀!!也省了很多重复步骤!!)
三、这个规则引擎的应用场景有哪一些?
风控系统(规则很多,而且容易变动,做互联网金融的同志深有体会)
活动营销系统(活动很多种,集积分送礼品,抽奖送礼品,竞争成功送礼品等等不同形式,这里可以变成不同的规则)
商品折扣系统(同一个商品,不同的用户,每个用户有不同的折扣优惠)
积分系统
个人总结:
有人会问,如果把规则,写在java代码上,同样也能实现功能呀,为什么还要单独弄一份文档出来,另外去读取它,这样操作呢?分离这些规则,很麻烦呀。当然,任何事情都有双面性。在架构选型的时候,要充分认识到drools的使用和各种因素,才能决定是否在实际项目中使用它。总结一句话,规则多变,则可以选用。(功能可以简单地理解成 java中的策略模式,switch功能)
==================================================================
四、将drools规则融入到项目中。本文主要介绍drools7的从0开始,目前有一个简单的需求:
某汽车举行活动,年龄超过60,打8折,其他年龄打9折,请用drools7来实现。
(本文是用IDEA来做,用eclipse的同学,请先百度一下,drools ecplies的运行环境安装,按步骤安装完就可以了)
1、添加maven依赖包
-
<dependency>
-
<groupId>org.drools</groupId>
-
<artifactId>drools-core</artifactId>
-
<version>7.7.0.Final</version>
-
</dependency>
-
<dependency>
-
<groupId>org.drools</groupId>
-
<artifactId>drools-compiler</artifactId>
-
<version>7.7.0.Final</version>
-
</dependency>
-
<dependency>
-
<groupId>org.drools</groupId>
-
<artifactId>drools-templates</artifactId>
-
<version>7.7.0.Final</version>
-
</dependency>
2、创建一个xml,注意一定要放在META-INF文件夹下,drools会自动解析它
-
<?xml version="1.0" encoding="UTF-8"?>
-
<kmodule xmlns="http://www.drools.org/xsd/kmodule">
-
<kbase name="rules">
-
<ksession name="all-rules"/>
-
</kbase>
-
</kmodule>
3、
-
@Test
-
public void testDrool7Api(){
-
KieServices kieServices = KieServices.Factory.get(); // 通过这个静态方法去获取一个实例
-
KieContainer kieContainer = kieServices.getKieClasspathContainer();// 默认去读取配置文件
-
KieSession kieSession = kieContainer.newKieSession("all-rules");// 根据这个名词去获取kieSession
-
-
Person p1 = new Person();
-
p1.setAge(30);
-
Car c1 = new Car();
-
c1.setPerson(p1);
-
-
Person p2 = new Person();
-
p1.setAge(70);
-
Car c2 = new Car();
-
c2.setPerson(p2);
-
-
kieSession.insert(c1); // 将c1实例放入到session中,
-
kieSession.insert(c2); //
-
-
int count = kieSession.fireAllRules();// 开始执行规则,并获取执行了多少条规则
-
kieSession.dispose();// 关闭session
-
System.out.println("Fire " + count + " rule(s)!");
-
System.out.println("The discount of c1 is " + c1.getDiscount() + "%");
-
System.out.println("The discount of c2 is " + c2.getDiscount() + "%");
-
}
4、注意路径
package com.rules
import com.drools.model.Car
import com.drools.model.Person
rule "test-drool7-older than 60"
when
$Car : Car( person.age > 60)
then
$Car.setDiscount(80);
System.out.println("test-drool7-older than 60"+$Car.getPerson().getAge());
end
rule "test-drool7-other"
when
$Car : Car( person.age<=60)
then
$Car.setDiscount(70);
System.out.println("test-drool7-other"+$Car.getPerson().getAge());
end
5、结果:
原文地址:https://blog.csdn.net/weixin_37662342/article/details/80773027
Drools7 Hello Wrold 入门详细步骤--系列01课的更多相关文章
- php从入门到放弃系列-01.php环境的搭建
php从入门到放弃系列-01.php环境的搭建 一.为什么要学习php 1.php语言适用于中小型网站的快速开发: 2.并且有非常成熟的开源框架,例如yii,thinkphp等: 3.几乎全部的CMS ...
- OSG入门即osgEarth建立一个地球的详细步骤
OSG入门即osgEarth建立一个地球的详细步骤 转:http://blog.csdn.net/xiaol_deng/article/details/9246291 最近在学习有关osg的知识,刚开 ...
- Git学习系列之Windows上安装Git详细步骤(图文详解)
前言 最初,Git是用于Linux下的内核代码管理.因为其非常好用,目前,已经被成功移植到Mac和Windows操作系统下. 鉴于大部分使用者使用的是Windows操作系统,故,这里详细讲解Windo ...
- [.NET MVC4 入门系列01]Helloworld MVC 4 第一个MVC4程序
[.NET MVC4 入门系列01]Helloworld MVC 4 第一个MVC4程序 一.练习项目: http://www.asp.net/mvc/tutorials/mvc-4/gettin ...
- Wireshark入门与进阶系列(二)
摘自http://blog.csdn.net/howeverpf/article/details/40743705 Wireshark入门与进阶系列(二) “君子生非异也,善假于物也”---荀子 本文 ...
- Wireshark入门与进阶系列(一)
摘自http://blog.csdn.net/howeverpf/article/details/40687049 Wireshark入门与进阶系列(一) “君子生非异也,善假于物也”---荀子 本文 ...
- MVC5 + EF6 入门完整教程(转载)--01
MVC5 + EF6 入门完整教程 第0课 从0开始 ASP.NET MVC开发模式和传统的WebForm开发模式相比,增加了很多"约定". 直接讲这些 "约定&qu ...
- [转帖]龙芯服务器部署WEB服务的体验和详细步骤
龙芯服务器部署WEB服务的体验和详细步骤 2019年01月02日 18:40:34 weixin_40065369 阅读数 1733 版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权 ...
- OPGL+GLFW+GLEW配置详细步骤
转载自:https://blog.csdn.net/weixin_40921421/article/details/80211813 本文设计的工具包: 链接:https://pan.baidu.co ...
随机推荐
- AttributeError: module 'pytest' has no attribute 'allure'
解决 pip3 uninstall pytest-allure-adaptor pip3 install allure-pytest 参考: https://www.cnblogs.com/lansa ...
- JS 将数字字符串数组转为 数字数组 (互换)
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; arr.map(String); //结果: ['1', '2', '3', '4', '5', '6', '7', '8 ...
- 主题模型TopicModel:主题模型LDA的应用
http://blog.csdn.net/pipisorry/article/details/45665779 主题模型LDA的应用 拿到这些topic后继续后面的这些应用怎么做呢:除了推断出这些主题 ...
- uniapp - 富文本编辑器editor(仅支持App和微信小程序)
uniapp - editor富文本编辑器用法示例 丢几个图,用心看下去(-.-) 这里使用了https://ext.dcloud.net.cn/plugin?id=412 插件,用于选择字体颜色.其 ...
- 高通平台sensor框架图【学习笔记】
- MLflow系列1:MLflow入门教程(Python)
英文链接:https://mlflow.org/docs/latest/tutorial.html 本文链接:https://www.cnblogs.com/CheeseZH/p/11943280.h ...
- Zookeeper:Unable to read additional data from client sessionid 0x00, likely client has closed socket
异常信息: 2018-03-20 23:34:01,887 [myid:99] - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerC ...
- js---省略花括号{}的几种表达式
在进行js的书写中,对于常见的if,for,while是可以简写,省略花括号{}的: var a = 10,b = 20; /** * if 简写 */ if(a > b) console.lo ...
- pycharm 生成requirements.txt
在命令行中输入 pip freeze>requirements.txt 1 安装requirements文件中的包 pip install -r requirements.txt
- 【SpringBoot】SpringBoot与Thymeleaf模版(六)
---恢复内容开始--- 模板引擎的思想 模板是为了将显示与数据分离,模板技术多种多样,但其本质都是将模板文件和数据通过模板引擎生成最终的HTML代码. Thymeleaf介绍 Thymeleaf是适 ...