标签:ice alien path roo art new test port efault
Drools官网地址为:https://www.drools.org/
maven环境
入门程序例子如下:
项目结构截图:
一、导入pom文件
<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.drools</groupId> <artifactId>drools-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.drools</groupId> <artifactId>drools-core</artifactId> <version>6.2.0.Final</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-compiler</artifactId> <version>6.2.0.Final</version> </dependency> </dependencies> </project>
二、准备JavaBean
package com.test.bean; public class Person { private String name; private int age; private String desc; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public String toString() { return "[name="+name+",age="+age+",desc="+desc+"]"; } }
三、在resource下建立rule文件夹编写drl文件
package com.person; import com.test.bean.Person; rule "boy" salience 1 when $p : Person(age > 0); then $p.setDesc("少年"); retract($p); end rule "youth" salience 2 when $p : Person(age > 12); then $p.setDesc("青年"); retract($p); end rule "midlife" salience 3 when $p : Person(age > 24); then $p.setDesc("中年"); retract($p); end rule "old" salience 4 when $p : Person(age > 60); then $p.setDesc("老年"); retract($p); end
四、编写kmodule.xml文件
<?xml version="1.0" encoding="UTF-8"?> <kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule"> <kbase name="SimpleRuleKBase" packages="rule"> <ksession name="simpleRuleKSession" default="true"/> </kbase> </kmodule>
五、编写测试类
package com.test.bean; import org.kie.api.KieServices; import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; public class Test { static KieSession getSession() { KieServices ks = KieServices.Factory.get(); KieContainer kc = ks.getKieClasspathContainer(); return kc.newKieSession("simpleRuleKSession"); } public static void main(String[] args) { KieSession ks = getSession(); Person p1 = new Person("A", 68); Person p2 = new Person("B", 32); Person p3 = new Person("C", 18); Person p4 = new Person("D", 8); System.out.println("before p1 : " + p1); System.out.println("before p2 : " + p2); System.out.println("before p3 : " + p3); System.out.println("before p4 : " + p4); ks.insert(p1); ks.insert(p2); ks.insert(p3); ks.insert(p4); int count = ks.fireAllRules(); System.out.println("总执行了"+count+"条规则"); System.out.println("after p1 : " + p1); System.out.println("after p2 : " + p2); System.out.println("after p3 : " + p3); System.out.println("after p4 : " + p4); ks.dispose(); } }
标签:ice alien path roo art new test port efault
原文地址:https://www.cnblogs.com/youcong/p/9088680.html