整合drools5.4所引用的jar 通过maven的pom.xml管理
什么是有状态的,什么是无状态的 请参考:http://blog.csdn.net/u013115157/article/details/52003592
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>drools-spring-samples</artifactId>
<name>Test Drools Spring</name>
<groupId>com.hcl.drools.spring.samples</groupId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>20040616</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>20030203.000129</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>5.4.0.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-spring</artifactId>
<version>5.4.0.Final</version>
</dependency>
<dependency>
<groupId>drools</groupId>
<artifactId>drools-all-jdk5</artifactId>
<version>2.5-final</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>drools</groupId>
<artifactId>drools-spring-examples</artifactId>
<version>2.5-beta-1</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-jsr94</artifactId>
<version>5.4.0.Final</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>transaction-api</artifactId>
<version>1.1-rev-1</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.167</version>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>3.1</version>
</dependency>
</dependencies>
</project>
spring的配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:drools-spring="http://drools.org/schema/drools-spring"
xsi:schemaLocation="http://drools.org/schema/drools-spring http://drools.org/schema/drools-spring.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<drools-spring:kbase id="kbase">
<drools-spring:resources><!-- type 表示 规则引擎是的引入类型,source 表示 被引入规则引擎的文件所在位置 -->
<drools-spring:resource type="DRL" source="classpath:rules/org/drools/tutorials/banking/person.drl" />
</drools-spring:resources>
</drools-spring:kbase>
<drools-spring:ksession id="ksession" type="stateful" kbase="kbase" />
<!-- 通过Spring 注入PersonDrools -->
<bean id="personDrools" class="com.goSmarter.drools.banking.PersonDrools">
<!-- 通过PersonDrools 将kession kbase 注入 -->
<property name="ksession" ref="ksession" />
<property name="kbase" ref="kbase" />
</bean>
</beans>
Drl文件的使用
规则文件:
package com.goSmarter.drools.banking //是必添项,但名称不做要求,为统一方便管理,与其位置路径一致。
import com.goSmarter.drools.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
<!--这里是javaBean的定义 -->
private String name;
private int age;
private String desc;
get set方法 省略 实体
调用类入口
实现类具体功能
package com.goSmarter.drools.banking;
import org.drools.KnowledgeBase;
import org.drools.runtime.StatefulKnowledgeSession;
import com.goSmarter.drools.Person;
public class PersonDrools {
private StatefulKnowledgeSession ksession;
private KnowledgeBase kbase;
public KnowledgeBase getKbase() {
return kbase;
}
public void setKbase(KnowledgeBase kbase) {
this.kbase = kbase;
}
public StatefulKnowledgeSession getKsession() {
return ksession;
}
public void setKsession(StatefulKnowledgeSession ksession) {
this.ksession = ksession;
}
public PersonDrools() {}
public void runRules() throws Exception {
Person p1 = new Person("白展堂", 68);
Person p2 = new Person("李大嘴", 32);
Person p3 = new Person("佟湘玉", 18);
Person p4 = new Person("郭芙蓉", 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);
//分别将对象插入到drl中
ksession.insert(p1);
ksession.insert(p2);
ksession.insert(p3);
ksession.insert(p4);
int count = ksession.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);
ksession.dispose(); //清空kiesession 注:无状态的不需要这样的操作
}
}
什么是有状态的,什么是无状态的 请参考:http://blog.csdn.net/u013115157/article/details/52003592
下面是小编的微信转帐二维码,小编再次谢谢读者的支持,小编会更努力的
----请看下方↓↓↓↓↓↓↓
百度搜索 Drools从入门到精通:可下载开源全套Drools教程
深度Drools教程不段更新中:
更多Drools实战陆续发布中………
扫描下方二维码关注公众号 ↓↓↓↓↓↓↓↓↓↓