Drools入门demo

Posted load6685

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Drools入门demo相关的知识,希望对你有一定的参考价值。

第一步POM加入依赖包

<dependency>
  <groupId>org.kie</groupId>
  <artifactId>kie-api</artifactId>
  <version>7.1.0.Final</version>
</dependency>
<dependency>
  <groupId>org.kie</groupId>
  <artifactId>kie-ci</artifactId>
  <version>7.1.0.Final</version>
</dependency>

第二步 src/main/resources目录新建META-INF文件夹,并在META-INF文件夹创建新文件kmodule.xml

<?xml version="1.0" encoding="UTF-8" ?>
<kmodule xmlns="http://www.drools.org/xsd/kmodule">
    <kbase name="DroolsBase">
        <ksession name="droolssession"/>
    </kbase>
</kmodule>

第三步 src/main/resources目录下新建drools.drl文件(文件名随意)

package test

import test.Message
import test.Message2
import test.MqttMessage
import test.Run
import test.Run2

rule "alarm"
    when
        message : Message(productId=="101",status==1)
    then
        new Run().run();
end

rule "temperature"
    when
        message2 : Message2(productId=="123",status==1)
        run2:Run2()
    then
        message2.setProductId(12345L);
        run2.run(message2);
end

rule "mqttTest"
    when
        message2:Message2()
        mqttMessage:MqttMessage(message2.productId==9999)
        run2:Run2()
    then
        run2.run(message2);
end

第四步新建Test类

package test;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

public class Test {
    public static void main(String[] args) {
        KieServices kieServices = KieServices.Factory.get();
        KieContainer kieContainer = kieServices.getKieClasspathContainer();// kmodule.xml
        //配置文件new对象
        KieSession kieSession = kieContainer.newKieSession("droolssession");
        Message message = new Message();
        message.setProductId(101L);
        message.setStatus(1);
        kieSession.insert(message);
        int i = kieSession.fireAllRules();
        System.out.println("========" + i);
        kieSession.dispose();
        
        //传输对象 及对象传参
        kieSession = kieContainer.newKieSession("droolssession");
        Message2 message2 = new Message2();
        message2.setProductId(123L);
        message2.setStatus(1);
        Run2 run2 = new Run2();
        
        kieSession.insert(message2);
        kieSession.insert(run2);
        
        int count = kieSession.fireAllRules();
        System.out.println("========" +count);
        kieSession.dispose();
        
        
        //对象中有子对象
        kieSession = kieContainer.newKieSession("droolssession");
        MqttMessage mqttMessage = new MqttMessage();
        Message2 message22 = new Message2();
        message22.setProductId(9999L);
        mqttMessage.setMessage2(message22);
        Run2 run22 = new Run2();
        
        kieSession.insert(message22);
        kieSession.insert(mqttMessage);
        kieSession.insert(run22);
        
        int count22 = kieSession.fireAllRules();
        System.out.println("========" +count22);
        kieSession.dispose();
    }
}

Message类

public class Message {
    private Long productId;
    private Integer status;
    

    public Long getProductId() {
        return productId;
    }

    public void setProductId(Long productId) {
        this.productId = productId;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

}

Message2类

package test;

public class Message2 {
    private Long productId;
    private Integer status;
    

    public Long getProductId() {
        return productId;
    }

    public void setProductId(Long productId) {
        this.productId = productId;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

}

MqttMessage类

package test;

public class MqttMessage {
    private Long id;
    
    private Message2 message2;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Message2 getMessage2() {
        return message2;
    }

    public void setMessage2(Message2 message2) {
        this.message2 = message2;
    }

    
}

以上是关于Drools入门demo的主要内容,如果未能解决你的问题,请参考以下文章

Drools笔记:初识与入门

Drools规则引擎入门指南

Drools7 Hello Wrold 入门详细步骤--系列01课

Java规则引擎Drools急速入门

[Drools]JAVA规则引擎 -- Drools

JAVA规则引擎 -- Drools