项目 drool-6.5的自学demo
先丢代码地址
https://gitee.com/a247292980/drools
再丢pom.xml
<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> <groupId>com.yy</groupId> <artifactId>drools</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>drools Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <runtime.version>6.5.0.Final</runtime.version> </properties> <repositories> <repository> <id>jboss-public-repository-group</id> <name>JBoss Public Repository Group</name> <url>http://repository.jboss.org/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> <updatePolicy>never</updatePolicy> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>daily</updatePolicy> </snapshots> </repository> </repositories> <dependencies> <dependency> <groupId>org.kie</groupId> <artifactId>kie-api</artifactId> <version>${runtime.version}</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-core</artifactId> <version>${runtime.version}</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-decisiontables</artifactId> <version>${runtime.version}</version> </dependency> <dependency> <groupId>org.jbpm</groupId> <artifactId>jbpm-test</artifactId> <version>${runtime.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>
最后丢目录结构
老规矩要说的基本都在注释里面,想看的去看源码就好了,只说几个重要的
Message
pojo类
DecisionTableTest
drools使用xxx.xls的例子
public class DecisionTableTest { public static final void main(String[] args) { try { // load up the knowledge base KieServices ks = KieServices.Factory.get(); KieContainer kContainer = ks.getKieClasspathContainer(); // 写名字则取相应的ksession下的东西 // KieSession kSession = kContainer.newKieSession("ksession-dtables"); // 不写名字则全取了 KieSession kSession = kContainer.newKieSession(); // go ! Message message = new Message(); message.setMessage("Hello World"); message.setStatus(Message.HELLO); kSession.insert(message); kSession.fireAllRules(); } catch (Throwable t) { t.printStackTrace(); } } }
DroolsTest
drools操作xxxx.drl的例子
public class DroolsTest { public static final void main(String[] args) { try { // load up the knowledge base KieServices ks = KieServices.Factory.get(); KieContainer kContainer = ks.getKieClasspathContainer(); KieSession kSession = kContainer.newKieSession("ksession-rules"); // go ! Message message = new Message(); message.setMessage("Hello World"); message.setStatus(Message.HELLO); kSession.insert(message); kSession.fireAllRules(); } catch (Throwable t) { t.printStackTrace(); } } }
ProcessTest
drools操作xxxx.bpmn的例子
public class ProcessTest { public static final void main(String[] args) { try { // load up the knowledge base KieServices ks = KieServices.Factory.get(); KieContainer kContainer = ks.getKieClasspathContainer(); KieSession kSession = kContainer.newKieSession("ksession-process"); // start a new process instance kSession.startProcess("com.sample.bpmn.hello"); } catch (Throwable t) { t.printStackTrace(); } } }
StatelessTest
用来区分StatelessKieSession和KieSession
public class StatelessTest { protected KieServices kieServices; protected KieContainer kieContainer; protected KieSession kieSession; protected StatelessKieSession statelessKieSession; @Before public void init() { //从Factory得到Services kieServices = KieServices.Factory.get(); // 在类路径中找到的所有DRL文件 kieContainer = kieServices.getKieClasspathContainer(); //得到kmodule的ksession kieSession = kieContainer.newKieSession("ksession-rules"); //stateless必须注释kmodule里全部的kbase // statelessKieSession=kieContainer.newStatelessKieSession(); } //kieSession和statelessKieSession的调用是不一样的 @Test public void testKieSession() { Message message = new Message(); message.setMessage("Hello World"); message.setStatus(Message.HELLO); kieSession.insert(message); kieSession.fireAllRules(); } @Test public void testStatelessKieSession() { Message message = new Message(); message.setMessage("Hello World"); message.setStatus(Message.HELLO); statelessKieSession.execute(message); } }
kmodule.xml
<?xml version="1.0" encoding="UTF-8"?> <kmodule xmlns="http://www.drools.org/xsd/kmodule"> <!--<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">--> <!--name kbase的唯一标志 packages 路径必须正确可少写可多写可不写 不写的话放于根目录(不建议) ksession 的唯一标记--> <!--<kbase name="rules" packages="Drools.rules">--> <kbase name="rules" packages="rules"> <!--<kbase name="rules" packages="">--> <!--<kbase name="rules" packages="myrules">--> <!--默认用最后一个--> <!--<kbase name="rules" packages="rules,myrules">--> <ksession name="ksession-rules"/> </kbase> <kbase name="dtables" packages="dtables"> <ksession name="ksession-dtables"/> </kbase> <kbase name="process" packages="process"> <ksession name="ksession-process"/> </kbase> </kmodule>