标签:抽奖 lru count() 规则引擎 汽车 disco lan service maven
商品折扣系统(同一个商品,不同的用户,每个用户有不同的折扣优惠)
积分系统
某汽车举行活动,年龄超过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、结果:
Drools7 Hello Wrold 入门详细步骤--系列01课
标签:抽奖 lru count() 规则引擎 汽车 disco lan service maven
原文地址:https://www.cnblogs.com/jpfss/p/12015760.html