Simple use of Rule Engine drools

Source: Internet
Author: User

The rule engine is applicable to complex and variable rules, such as product full reduction, bonus points, and attendance rules.

1. Introduce Maven dependency

<dependency>    <groupId>org.drools</groupId>    <artifactId>drools-core</artifactId>    <version>7.13.0.Final</version></dependency><dependency>    <groupId>org.drools</groupId>    <artifactId>drools-compiler</artifactId>    <version>7.13.0.Final</version></dependency>

  

2. drools can be executed in two ways. The first is based on the kmodule. xml and DRL configuration files, and the second is the dynamic read Rule.

Kmodule-based

1) create a fact. The fact in drools is a common bean in Java and is used to pass parameters.

package com.beyond.odc.entity;/** * Fact java bean * @author Hejinsheng */public class Message {    private String message;    private String result;    private int status;    public String getMessage() {        return this.message;    }    public void setMessage(String message) {        this.message = message;    }    public int getStatus() {        return this.status;    }    public void setStatus(int status) {        this.status = status;    }    public String getResult() {        return result;    }    public void setResult(String result) {        this.result = result;    }}

  

2) Create the kmodule. xml file under resources/META-INF with the following content:

<?xml version="1.0" encoding="UTF-8"?><kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">    <kbase name="rules" packages="rules">        <ksession name="ksession-rules"/>    </kbase></kmodule>

  

3) Create the rule description file rules. DRL in the resources/rules folder. The content is as follows:

Package rulesimport COM. beyond. ODC. entity. message; rule "hello" no-loop true lock-on-active true when $ message: Message (status = 0) Then system. out. println ("message:" + $ message. getmessage (); $ message. setresult ("Hello Response Message"); Update ($ message) endrule "Byebye" no-loop true lock-on-active true when $ message: Message (status = 1) then system. out. println ("message:" + $ message. getmessage (); $ message. setresult ("Byebye Response Message"); Update ($ message) End

  

4) Compile the test file

Public static void main (string [] ARGs) {try {kiesession ksession = kiecontainerfactory. getkiecontainer (). newkiesession ("ksession-Rules"); message = new message (); message. setmessage ("hello"); // corresponds to the rule name in the DRL file, indicating the rule message to be executed. setstatus (0); // obtain a field in the message, and determine whether to execute the code block ksession in the user rule file. insert (Message); // pass the message as the passed parameter to drools to execute ksession. fireallrules (); // load the rule system. out. println ("received messages from drools:" + message. getresult (); // If response parameters exist in drools, you can use the field to get} catch (throwable t) {T. printstacktrace ();}}

  

// Kiecontainerfactory code snippet public class kiecontainerfactory {Private Static kiecontainer; public static kiecontainer getkiecontainer () {If (kiecontainer = NULL) {// load up the knowledge base kieservices Ks = kieservices. factory. get (); kiecontainer kcontainer = Ks. getkieclasspathcontainer (); kiecontainer = kcontainer;} return kiecontainer ;}}

  

5) The execution result indicates that the code has been executed to the drools file. The specific business logic can be implemented in the code block after then.

Incoming, message: Hello received from drools: Hello Response Message

  

Iii. Dynamic execution

Generally, it refers to the description of a rule obtained from a database or remotely for rule execution. In the dynamic mode, kmodule. xml and DRL files are not required, making execution easier.

Public static void execfromdb () {kiesession ksession = NULL; try {// check the rules from the database according to the code according to the actual situation, here, the string rule = "package rules \ n" + "\ n" + "Import COM. beyond. ODC. entity. message; \ n "+" \ n "+" rule \ "Hello \" \ n "+" no-loop true \ n "+" lock-on-active true \ n "+ "When \ n" + "$ message: message (status = 0) \ n "+" then \ n "+" system. out. println (\ "dynamic execution, message: \" + $ message. getmessage (); \ n "+" $ message. setresult (\ "H Hello, the Response Message \ "); \ n" + "Update ($ message) \ n "+" End \ n "+" \ n "+" rule \ "Byebye \" \ n "+" no-loop true \ n "+" lock-on- active true \ n "+" When \ n "+" $ message: message (status = 1) \ n "+" then \ n "+" system. out. println (\ "dynamic execution, message: \" + $ message. getmessage (); \ n "+" $ message. setresult (\ "Byebye Response Message \"); \ n "+" Update ($ message) \ n "+" end "; knowledgebuilder kb = knowledgebuilderfactory. newknowledgebuilder (); kb. Add (resourcefactory. newbytearrayresource (rule. getbytes ("UTF-8"), resourcetype. DRL); // check rule correctness knowledgebuildererrors errors = kb. geterrors (); For (knowledgebuildererror error: errors) {system. out. println ("rule file correctness error: {}" + error);} internalknowledgebase kbase = knowledgebasefactory. newknowledgebase (); kbase. addpackages (kb. getknowledgepackages (); // executes the rule ksession = kbase. newkiesession (); // fac T message fact = new message (); fact. setmessage ("Byebye"); fact. setstatus (1); ksession. insert (fact); ksession. fireallrules (); system. out. println ("messages received from drools rules:" + fact. getresult ();} catch (exception e) {system. out. println ("rule execution exception: {}" + E) ;}finally {If (null! = Ksession) ksession. Dispose ();}}

The execution result is as follows:

Message: the message received by Byebye from the drools rule: the message returned by Byebye.

  

 

Simple use of Rule Engine drools

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.