本文主要是介绍[bigdata-092] 一个基于eclipse的drools插件的java的drools应用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. 文档来源
从drools官方demo改编而来
2. eclipse要先安装drools的第三方开发工具
3. 用eclipse-->new-->project..->drools--> drools project创建一个新的空项目
3. 源码目录结构
├── pom.xml
├── src
│ └── main
│ ├── java
│ │ └── com
│ │ └── ttz
│ │ └── demo
│ │ ├── Main.java
│ │ └── Message.java
│ └── resources
│ ├── dtables
│ ├── META-INF
│ │ ├── kmodule.xml
│ │ └── maven
│ │ └── pom.properties
│ ├── process
│ └── rules
│ ├── Sample2.drl
│ └── Sample.drl
4. 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>com.ttz</groupId><artifactId>mydroolsexample</artifactId><version>1.0.0-SNAPSHOT</version><name>Drools :: Sample Maven Project</name><description>A sample Drools Maven project</description><properties><runtime.version>6.4.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></dependencies>
</project>
5. Main.jav内容如下:
package com.ttz.demo;import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;import com.ttz.demo.Message;public class Main {public static void main(String[] args) {KieServices ks = KieServices.Factory.get();KieContainer kContainer = ks.getKieClasspathContainer();KieSession kSession = kContainer.newKieSession("ksession-rules");Message message = new Message();message.setMessage("Hello World 2");message.setStatus(Message.HELLO);kSession.insert(message);kSession.fireAllRules();}}
6. Message.java内容如下
package com.ttz.demo;public class Message {public static final int HELLO = 0;public static final int GOODBYE = 1;private String message;private int status;public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public int getStatus() {return status;}public void setStatus(int status) {this.status = status;}}
7. Sample.drl文件内容如下
package com.ttz.demoimport com.ttz.demo.Message;rule "Hello World"whenm : Message( status == Message.HELLO, myMessage : message )then//System.out.println( myMessage );//m.setMessage( "THis is my hello world" );//m.setStatus( Message.GOODBYE );//update( m );System.out.print("hello wrold\n");
endrule "GoodBye"whenMessage( status == Message.GOODBYE, myMessage : message )thenSystem.out.println( myMessage );
end
8. Sample2.drl文件内容如下
package com.ttz.demoimport com.ttz.demo.Message;rule "Hello World 2"whenm : Message( status == Message.HELLO, myMessage : message )thenSystem.out.println("hello world 2\n");//System.out.println( myMessage );//m.setMessage( "THis is my hello world 2" );//m.setStatus( Message.GOODBYE );//update( m );
endrule "GoodBye 2"whenMessage( status == Message.GOODBYE, myMessage : message )thenSystem.out.println( myMessage );
end
10. 运行,则输出结果是
hello wrold
hello world 2
表明对同一个Fact,两条规则都生效了。
同理,其实也可以设置更多的Fact,更多的规则。
这篇关于[bigdata-092] 一个基于eclipse的drools插件的java的drools应用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!