Drools Getting Started-----------environment building, analyzing HelloWorld Drools5.0 xls file to DRL file promotion parsing efficiency using BRMS Tomcat6.0 configuration

Source: Internet
Author: User
Tags getmessage static class throwable zip jboss knowledge base tomcat jbpm

http://justsee.iteye.com/blog/1198259

Drools Official website: http://www.jboss.org/drools

Drools and JBPM consist out of several projects: (Drools package provides several parts of the functionality)

Drools Guvnor (Business Rules manager) (rule set manager)

Drools Expert (Rule engine)

JBPM 5 (Process/workflow) (workflow)
Drools Fusion (Event processing/temporal reasoning) (rule set engine search and planning)

Drools Planner (Automated planning) (decision table)

----------------------------------------------------------------------------------------------------------- ------------

Eclipse3.5 Installing the Drools5.2.0.final plugin

to drools download page (now http://www.jboss.org/drools/downloads.html)

-Download and unzip the Drools:drools-distribution-5.2.0.final.zip (unzip the catalogue at random)

-Download and unzip the plugin: Droolsjbpm-tools-distribution-5.2.0.final.zip (unzip the directory arbitrarily)

-Open Eclipse.

-click "Help"-"Install new software ..."

-click "Add ..."

-Enter "Droolslocaluupdatesite" in name (the name is optional)

-click on "Local ..." and select ".../binaries/org.drools.updatesite" (This directory is in the plugin just unzipped)

-Click OK

-Select So plugin.

-Here is the click Next,finish

After the plug-in is installed, you need to add the drools environment in eclipse:

-click "Windows"-"preferencess"

-Select "Drools"-"Installed Drools runtimes" in the tree menu of the window

-In the empty Drools runtime list on the right, click "Add"

-In the pop-up window, name input droolsruntime (a random name), select ".../drools-distribution-5.2.0.final\" in path. Drools-distribution-5.2.0.final\binaries "(This directory is in the drools just unzipped),

-click "OK"

-click "OK"

----------------------------------------------------------------------------------------------------------- ------------

HelloWorld Example

-"New"-"project"-"Drools Project"-Enter project name-"Next"

-Here you can choose the type of sample file to be generated (3 types of 6 sample files: Rule files and Java test classes for the Rule Engine sample, decision table files for decision table development and Java test classes; workflow process files and Java test classes). Tick the Rule Engine sample rule file and Java test class, click "Next"

-Enter the selection interface to the Drools Runtime library. The default runtime library is already set up in the environment and is loaded here. Then select the version of the rule engine used to compile the code, select "Drools 5.1 or above" and click "Finish"

SAMPLE.DRL: Sample file for the rule engine, which is written according to the Drools rule language syntax

Java code package com.sample import com.sample.DroolsTest.Message; rule ' Hello world ' when//LHS block//In working memory, if present A message object satisfies his status property equal to the Message.hello condition,//The message instance that satisfies the condition is represented by the variable m, and his message property is expressed in Mymessage for use in RHS. M:message (Status = = Message.hello, mymessage:message) then//RHS block//LHS block is run if the condition is met in RHS. The variable m,mymessage System.out.println (mymessage) was used in the LHS; M.setmessage ("Goodbye Cruel World"); M.setstatus (Message.goodbye); Update (m);//indicates that this Message instance is updated in working memory. This triggers the rule with the name "GoodBye", and the end rule "GoodBye" when message (status = = message. GOODBYE, Mymessage:message) then System.out.println (mymessage); End

Package com.sample
 
import com.sample.DroolsTest.Message;
 
Rule "Hello World"
       when//LHS block
       //In working memory, if there is a message object satisfies his status attribute equals Message.hello condition,
       / /The message instance satisfying the condition is represented by the variable m, and his message property is expressed in Mymessage for use in RHS.
        m:message (Status = = Message.hello, mymessage:message
    )
       then//RHS block
       //LHS block is run if the condition is met in RHS. The variable m,mymessage
        System.out.println (mymessage) was used in the LHS;
        M.setmessage ("Goodbye Cruel World");
        M.setstatus (Message.goodbye);
        Update (m);//indicates that this message instance is updated in working memory. This triggers the rule with the name "GoodBye", and the 
end

rule "GoodBye" when
        Message (Status = = Message.goodbye, mymessage:message) then
        System.out.println (mymessage);
End

The DroolsTest.java:java test class is used to read the SAMPLE.DRL and run, which is written using the API of the Drools rule engine

Java code package com.sample; Import Org.drools.KnowledgeBase; Import Org.drools.KnowledgeBaseFactory; Import Org.drools.builder.KnowledgeBuilder; Import Org.drools.builder.KnowledgeBuilderError; Import org.drools.builder.KnowledgeBuilderErrors; Import Org.drools.builder.KnowledgeBuilderFactory; Import Org.drools.builder.ResourceType; Import Org.drools.io.ResourceFactory; Import Org.drools.logger.KnowledgeRuntimeLogger; Import Org.drools.logger.KnowledgeRuntimeLoggerFactory; Import org.drools.runtime.StatefulKnowledgeSession; /** * This was a sample class to launch a rule. */public class Droolstest {public staticfinal void Main (string[] args) {try {//load up the Knowledge Base Knowledgeba Se kbase = readknowledgebase (); Statefulknowledgesession ksession = Kbase.newstatefulknowledgesession ();//Create session Knowledgeruntimelogger Logger = Knowledgeruntimeloggerfactory.newfilelogger (ksession, "test"); Go! Message message = new Message (); Message.setmessage ("Hello World"); Message.setstatus (MeSsage. HELLO); Ksession.insert (message);//Insert Ksession.fireallrules ();//Execute Rule logger.close ();//close} catch (Throwable t) { T.printstacktrace (); }} private static Knowledgebase Readknowledgebase () throws Exception {Knowledgebuilder Kbuilder = Knowledgebuilderfactor Y.newknowledgebuilder ();//Create Rule Builder Kbuilder.add (Resourcefactory.newclasspathresource ("SAMPLE.DRL"), RESOURCETYPE.DRL)///Load Rule file and add to builder knowledgebuildererrors errors = Kbuilder.geterrors (); if (errors.size () > 0) {//] rules are found during the compile rule for errors in the for (Knowledgebuildererror error:errors) {System.out.println ("There are errors in the rules, The error message is as follows: "); SYSTEM.ERR.PRINTLN (Error); } throw new IllegalArgumentException ("Could not parse knowledge."); } Knowledgebase Kbase = Knowledgebasefactory.newknowledgebase ();//Create a Rule build Library kbase.addknowledgepackages ( Kbuilder.getknowledgepackages ());//The resource file package loaded by the builder is placed in the build library return kbase; Public Staticclass Message {public staticfinal int ' HELLO =0; public staticfinal 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;}} }

Package com.sample;
Import Org.drools.KnowledgeBase;
Import Org.drools.KnowledgeBaseFactory;
Import Org.drools.builder.KnowledgeBuilder;
Import Org.drools.builder.KnowledgeBuilderError;
Import org.drools.builder.KnowledgeBuilderErrors;
Import Org.drools.builder.KnowledgeBuilderFactory;
Import Org.drools.builder.ResourceType;
Import Org.drools.io.ResourceFactory;
Import Org.drools.logger.KnowledgeRuntimeLogger;
Import Org.drools.logger.KnowledgeRuntimeLoggerFactory;

Import org.drools.runtime.StatefulKnowledgeSession;
 /** * This was a sample class to launch a rule. */public class Droolstest {public static final void main (string[] args) {try {//load up the
            Knowledge Base Knowledgebase Kbase = Readknowledgebase ();  Statefulknowledgesession ksession = Kbase.newstatefulknowledgesession ();//Create session Knowledgeruntimelogger Logger =
            Knowledgeruntimeloggerfactory.newfilelogger (ksession, "test");
            Go! MessaGE message = new message ();
            Message.setmessage ("Hello World");
            Message.setstatus (Message.hello); Ksession.insert (message);//Insert Ksession.fireallrules ();//Execute Rule logger.close ();//close} catch (
        Throwable t) {t.printstacktrace (); }} private static Knowledgebase Readknowledgebase () throws Exception {Knowledgebuilder Kbuilder = Knowl Edgebuilderfactory.newknowledgebuilder ();//Create Rule Builder Kbuilder.add (Resourcefactory.newclasspathresource ("
        Sample.drl "), RESOURCETYPE.DRL);//Load Rule file and add to builder knowledgebuildererrors errors = Kbuilder.geterrors (); if (errors.size () > 0) {//] rule was found during the compile rule error for (Knowledgebuildererror error:errors) {System.out.println ("
                There is an error in the error message as follows: ");
            SYSTEM.ERR.PRINTLN (Error);
        } throw new IllegalArgumentException ("Could not parse knowledge."); } Knowledgebase Kbase = Knowledgebasefactory.neWknowledgebase ();//Create Rule Build Library Kbase.addknowledgepackages (Kbuilder.getknowledgepackages ());//The Builder loads the resource package into the build library
    return kbase;
        public static 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;
        The public void Setmessage (String message) {this.message = message;
        } public int GetStatus () {return this.status;
        } public void SetStatus (int status) {this.status = status;
 }

    }

}

Droolstest.rar (5.8 KB)

http://sunxboy.iteye.com/blog/1464919 drools, common file types supported are DRL (drools rule language), xls,cvs,rf (rule stream file), DRT (drools rule Template), in simple applications, generally only use DRL and XLS files, in order to facilitate the business staff and maintenance personnel can configure the rules file, the general rules files are made by the programmer execl file, so, add the relevant configuration is very silly. But doing so affects the efficiency of the program, and we do a test.

Rule file: Base.xls contains Rule.xls and Simple.xls, Credit.xls is included in Rule.xls, in order to read the Rule object in Credit.xls, you need to Base.xls, Rule.xls,credit.xls three execl file parsing, reading rule object results total time consuming:

Read Rule file —————— –13484 milliseconds

In drools5.0, Org.drools.decisiontable.SpreadsheetCompiler This class can convert the XLS file into DrL string object, FileWriter to write DrL file, the relevant conversion code is:
public string drlstring (string file,string todir) throws exception{
Spreadsheetcompiler sc = new Spreadsheetcompiler ();
File F = new file (file);
String fileName = F.getname ();
String name = filename.substring (0,filename.lastindexof ("."));
FileInputStream Xlsstream = new FileInputStream (f);
String drlfiletmp = Sc.compile (Xlsstream, Inputtype.xls);
Drlfiletmp.replaceall (". xls", ". DrL");
File Drlfile = new file (Todir + "/" + name + ". DrL");
FileWriter writer = new FileWriter (drlfile);
Writer.write (DRLFILETMP);
Writer.close ();
return drlfiletmp;
}
After parsing the execl into a DRL file, it takes a total time to read the result of the Rule object:

——————--7579 milliseconds to read a rule file

In terms of data, almost half of the time overhead is saved. Of course, 7.6 seconds is still not an acceptable number and needs to be optimized. We need to convert the XLS rule file to the DRL rule file when the system hits the version, and then we cache the rule object in memory by Oscache when the system starts. If no object is re-read from the rule file, and some words are read directly from memory, the read Rule object consumes almost 0 of the time. Here the XLS is converted to DRL file just to speed up the system startup.

http://xiaoyanzi1205.iteye.com/blog/611936

Our company server is WebSphere, so I decided to test it with Tomcat6.0 for later deployment to the server.
Tomcat6.0 Requirements:
1.jdk5.0 above, and must be sun (IBM's JDK will be issued with an error);
2. Download the required jar package, URL: http://download.jboss.org/drools/dependencies/jboss_jsf_libs.zip, unzip it and put it under Tomcat_home/lib.
3. Remove the El-api.jar from the Drools-guvnor.war Lib (as the Tomcat6.0 has been brought in, other versions of Tomcat are unclear).

Then throw the Drools-guvnor.war directly under Tomcat's WebApps and start Tomcat.
Visit brms:http://localhost:8080/drools-guvnor/org.drools.guvnor.guvnor/guvnor.html

Username:admin
Password:null
Over

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.