Drools 6.5 Final getting started, droolsfinal

Source: Internet
Author: User
Tags jbpm

Drools 6.5 Final getting started, droolsfinal
Drools 6.5 Final Study Notes

Recently, the project involves using rules to operate data. It is too difficult to think of a complete and flexible rule system that can meet business needs, I want to know if there is any open-source rule engine that can be used. Later, I found that Drools, an open-source engine, is more powerful than I expected.

What is Drools? It is an open-source rule engine system developed by the famous JBoss company. It can be downloaded and used for free. It has been developed to version 6.5 and can be downloaded and used by RC. It can be integrated and used in the JavaEE project. It is developed as follows: https://www.drools.org/download/download.html. After downloading the Drools Engine and Drools and jBPM tools, you can configure them in Eclipse.

Build the Eclipse development environment, decompress the Drools and JBPM tools Package, and decompress the directory \ binaries \ org. drools. copy the plagins and features sub-directories in the updatesite directory to the Eclipse directories. The content in these two sub-directories will be merged with the content in the corresponding sub-directories in Eclipse, and then start Eclipse, open Preferences under the Window menu, and you will find that the Drools item is added, and then configure the Drools, such:

Then select Drools Runtime to add the jar file extracted from the Drools Engine package, for example:

Click OK and select the added Runtime, for example:

After completing these configurations, restart Eclipse. Create a new Project and you will find that the Drools type is added to the Project type. Select "Drools Project" and "Next.

Select the second item to create a sample project.

After entering the project, the project structure is as follows:

Com. sample is a test class for three rule files. The three rule definition files under the resources Directory are Excel, drl, bpmn, and kmodule. description of kbase and kession stored in xml:

<? 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>

<Kbase name ="Dtables"Packages ="Dtables">

<Ksession name ="Ksession-dtables"/>

</Kbase>

<Kbase name ="Process"Packages ="Process">

<Ksession name ="Ksession-process"/>

</Kbase>

</Kmodule>

The packages of kbase must be consistent with the project registration. The ksession name will be used in the Code. Let's take the drl rule file as an example to see how it works, the test code corresponding to the drl rule file is DroolsTest. Its rule file is defined as follows:

PackageCom. sample

 

ImportCom. sample. Message;

 

Rule"Hello World"

When

M: Message (status = Message. HELLO, myMessage: message)

Then

System. out. println (myMessage );

M. setMessage ("Goodbye cruel world ");

M. setStatus (Message. GOODBYE );

Update(M );

End

 

Rule"GoodBye"

When

Message (status = Message. GOODBYE, myMessage: message)

Then

System. out. println (myMessage );

End

The package can be different from the code in a custom text segment. Use import to introduce the object type that requires rule verification. The code generated by Eclipse is a static class, actually, it can be a common class. Here we use the object type com. sample. message, which defines two rules: Hello World and GoodBye, which use when and then as the expression of if then (that is, what if so). In rule "Hello World, the when phrase defines a Message object m and the myMessage variable for obtaining the Message attribute message. The then statement always updates the attributes of the m object using methods in the Message class, the update (m) method is used to update back to the Java code. The entire when means to judge the input Message object m as follows if m. status = Message. HELLO, the operation in then is executed.

DroolsTest. the java code is as follows. The first three lines in the main method are used to construct the Kie object and obtain the kmodule. the session named ksession-rules in xml is passed through kSession. insert (msg) is used to pass in the object that requires rule verification, through kSession. fireAllRules () execution rules.

PackageCom. sample;

 

ImportJava. util. ArrayList;

ImportJava. util. List;

 

ImportOrg. kie. api. KieServices;

ImportOrg. kie. api. runtime. KieContainer;

ImportOrg. kie. api. runtime. KieSession;

 

Public ClassDroolsTest {

 

Public Static Final VoidMain (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 =NewMessage ();

Message. setMessage ("Hello World ");

Message. setStatus (Message.HELLO);

System.Out. Println (String.Format("The message object changes before the rule is executed as follows: message. message = % s, message. status = % d", message. getMessage (), message. getStatus ()));

Message message2 =NewMessage ();

Message2.setMessage ("liuyinghui ");

Message2.setStatus (Message.HELLO);

List <Message> listMsg =NewArrayList <Message> ();

ListMsg. add (message2 );

ListMsg. add (message );

For(Message msg: listMsg ){

KSession. insert (msg );

KSession. fireAllRules ();

}

System.Out. Println (String.Format("The message object changes after the rule is executed as follows: message. message = % s, message. status = % d", message. getMessage (), message. getStatus ()));

 

}Catch(Throwable t ){

T. printStackTrace ();

}

}

}

Message. java is defined as follows:

PackageCom. sample;

 

Public ClassMessage {

 

Public Static Final Int HELLO= 0;

Public Static Final Int GOODBYE= 1;

 

PrivateString message;

 

Private IntStatus;

 

PublicString getMessage (){

Return This. Message;

}

 

Public VoidSetMessage (String message ){

This. Message = message;

}

 

Public IntGetStatus (){

Return This. Status;

}

 

Public VoidSetStatus (IntStatus ){

This. Status = status;

}

 

}

 

As shown in the running result, you can see the changes in the message object before and after the processing by the rule engine, and some attribute values output during execution of the intermediate test rule file, so far, a simple program using the Drools rule engine has been developed.

 

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.