Build drools development environment

Source: Internet
Author: User

The drools version used in the space is 6.3.0.Final, and the 5.x version of the API has a big difference, please note the development tool settings

Development tools for IntelliJ idea, I've been on the internet for a long time and haven't found a description of using idea to develop drools
Only one of the idea's settings files was found, and the new. DrL file after importing the file will have smart hints for drools rule syntax
Download address development process maven

Idea can create drools types of projects directly, but not with Maven well integration, it is recommended to build a new MAVEN project directly for development, POM files need to add the following dependencies:

<dependencies> <dependency> <groupId>org.kie</groupId> <artifactid>kie-a
        pi</artifactid> <version>6.3.0.Final</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-core</artifactId> <versi on>6.3.0.final</version> </dependency> <dependency> <groupid>org.drools</gro
    Upid> <artifactId>drools-compiler</artifactId> <version>6.3.0.Final</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId> drools-decisiontables</artifactid> <version>6.3.0.Final</version> </dependency> &L T;dependency> <groupId>org.drools</groupId> <artifactid>drools-templates</artifact Id> <versiOn>6.3.0.final</version> </dependency> </dependencies> 
Project Structure Specification

The rules files and kmodule.xml of the Drools project must be created in the Main->resources folder, where Kmodule.xml needs to be located in the Main->resources->meta-inf directory
Otherwise, when the resource file cannot be found, the exception that throws the null pointer occurs
Resources directory, the rules files can be categorized in different directories, in the same directory as the package name referenced in the Kmodule.xml file , as

Main
=resources
==meta-inf
===kmodule.xml
==rules1
===rule.drl
==rules2
===rule.drl

Then the definition in the Kmodule.xml file should be as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <kmodule
xmlns= "Http://jboss.org/kie/6.0.0/kmodule" >
    <kbase name= "Rules" packages= "Rules1" >
        <ksession name= "Ksession-rules"/>
    </kbase>
    <kbase name= "Dtables" packages= "Dtables" >
        <ksession name= "Ksession-dtables"/>
    </kbase>
</kmodule>

Kbase in the packages is the resources folder under the corresponding rules of the directory, kmodule.xml the specific content in the following practice to explain the rules of the file writing

For a description of the syntax of the drools rule file, refer to:
DROOLS5 Rule Engine Development tutorial

This is a very detailed tutorial, for the drools5.x version, where the code part of the API in 6.x is not compatible , but the rules of the writing instructions are general, you can refer to the reference

Here is an example of a simple rule file:

Package Info.xiaohei.www.rules

//Import Custom entity class import
info.xiaohei.www.Message

//Rule name
"Hello World" "
    //Conditional statement
    when
        //if the entity's Status property is Hello, assign the entity to M, the entity's Message property is assigned to Printmsg
        m:message (Status = = Message.hello, Printmsg:message)
    //What to do after satisfying the condition then/
        /System output and reset entity properties
        System.out.println ( PRINTMSG);
        M.setmessage ("goodbye~");
        M.setstatus (Message.goodbye);
        Updating the entity will re-trigger the rule
        update (m);
End

//second rule
"GoodBye" when
        Message (Status = = Message.goodbye, printmsg:message)
    Then
        System.out.println (printmsg);
End

The entity class is defined as follows:

Package info.xiaohei.www;

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 this.message;
    }

    public void Setmessage (String message) {
        this.message = message;
    }

    public int getStatus () {
        return this.status;
    }

    public void setStatus (int status) {
        this.status = status;
    }
}
Java API

The concept of a kie is introduced in the 6.x release, which is described in more detail in the following example code

public static void Main (string[] args) {
    //Get Kieservices instance from factory
    kieservices kieservices = KieServices.Factory.get ();
    Get the Kiecontainer instance from Kieservices, which will load the Kmodule.xml file and load the rule file
    kiecontainer Kiecontainer = Kieservices.getkieclasspathcontainer ();
    Establish the communication pipeline kiesession to the rule file
    kiesession ksession = kiecontainer.newkiesession ("Ksession=rules");
    Message message = new Message ();
    Message.setmessage ("Hello World");
    Message.setstatus (Message.hello);
    Inserts an entity class into the execution rule
    ksession.insert (message);
    Ksession.fireallrules ();
}

Author: @ Little Black

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.