drools7 (一、最简单的例子)
生活随笔
收集整理的這篇文章主要介紹了
drools7 (一、最简单的例子)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
切記!!! 必須使用jdk1.8?
?
?
工程目錄
?
引入依賴包,pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>cn.xiaojf</groupId><artifactId>drools7-demo</artifactId><version>1.0-SNAPSHOT</version><properties><drools.version>7.5.0.Final</drools.version><commons-lang3.version>3.7</commons-lang3.version><junit.version>4.12</junit.version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency><!-- drools --><dependency><groupId>org.kie</groupId><artifactId>kie-api</artifactId><version>${drools.version}</version></dependency><dependency><groupId>org.drools</groupId><artifactId>drools-compiler</artifactId><version>${drools.version}</version></dependency><dependency><groupId>org.drools</groupId><artifactId>drools-core</artifactId><version>${drools.version}</version></dependency><!-- tools --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>${commons-lang3.version}</version></dependency></dependencies></project>?
META-INF/kmodule.xml , drools 最新api最大的變化之一
<?xml version="1.0" encoding="utf-8" ?> <kmodule xmlns="http://www.drools.org/xsd/kmodule"><kbase name="rules"><ksession name="all-rules"></ksession></kbase> </kmodule>?
定義規(guī)則文件?discount.drl
package cn.xiaojf.rulesimport cn.xiaojf.drools7.model.Car; rule "car-discount>30" when$car : Car(person.age > 30)then$car.setDiscount(80);endrule "car-discount<=30" when$car : Car(person.age <= 30)then$car.setDiscount(90);end?
兩個(gè)業(yè)務(wù)的實(shí)體類
package cn.xiaojf.drools7.model;public class Person {private String name;private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;} } package cn.xiaojf.drools7.model;public class Car {private int discount = 100;private Person person;public int getDiscount() {return discount;}public void setDiscount(int discount) {this.discount = discount;}public Person getPerson() {return person;}public void setPerson(Person person) {this.person = person;} }?
最后是測試類,新的api大量簡化了api的使用,主要看注釋
package day20180117;import cn.xiaojf.drools7.model.Car; import cn.xiaojf.drools7.model.Person; import org.junit.Test; import org.kie.api.KieServices; import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession;public class Demo {@Testpublic void test() {KieServices kieServices = KieServices.Factory.get();//默認(rèn)自動加載 META-INF/kmodule.xmlKieContainer kieContainer = kieServices.getKieClasspathContainer();//kmodule.xml 中定義的 ksession nameKieSession kieSession = kieContainer.newKieSession("all-rules");Person p1 = new Person();p1.setAge(20);Car car = new Car();car.setPerson(p1);kieSession.insert(car);int count = kieSession.fireAllRules();System.out.println(count);System.out.println(car.getDiscount());kieSession.dispose();} }?
結(jié)果
1 90?
轉(zhuǎn)載于:https://www.cnblogs.com/xiaojf/p/8306959.html
總結(jié)
以上是生活随笔為你收集整理的drools7 (一、最简单的例子)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux下curl的使用
- 下一篇: 第五节 -- 字典