Thursday, May 21, 2009

Can You Pack Shampoo In Checked Luggage

JBoss Drools - operating the application in 10 minutes

decided to create a minimal application that uses JavaSE JBoss Drools rules engine and decision tables stored in the form of Excel spreadsheets. Here I present my results. The most important guideline when creating this application was simple to see what the minimum steps necessary to run Drools. Therefore, the application is devoid of error checking, validation parameters, etc - not what is meant here.

Well this is explained - Now to work:)

Start by creating a board decision:
basic notion of decision-making in the subject arrays is that - for our purposes we can define this concept as an object Javowy placed in working memory (working memory) of the engine rules during its startup, or created "inside the engine" as a result of the actions of either rule. In our example, there are only facts contained in the working memory "from the outside." Facts have attributes, which in our case the characteristics (properties ) Javowych objects. Drools allows you to create the conditions for a specific field value of the object - of course we have to stick to the JavaBeans conventions.

table definition starts with the text in the cell marked RuleTable . The next line, type the headings indicating the role of the column in the table - condition (condition) or action (consequence, action). The next two lines are used to define the details of the condition or action. The condition may be, for example the existence of a fact (cell B9) or a specific attribute value of fact (cell C8 and C9 - here we refer to the attribute value name fact type Activity and the subsequent rows are potential values \u200b\u200bof this field). The table contains two rules (lines 11 and 12):
  1. there and there is a Student Activity called fun
  2. there and there is a Student Activity called learning
For both rules, there is joint action, defined as a fragment of Java code - add a string in the list. $ Param is the value of the selected cell as the intersection of the current column (shares) and the line is running normally. So if the rules engine is powered by the fact Student Activity and the fact Attribute name equal learning - then the shares will be launched first rule. If further appears that Activity Attribute name = fun , shares will also be running a second rule. Some facts contained in working memory (working memory) rules engine may run multiple rules. More detailed information can be found in the documentation . Noteworthy section yet Variables, which allows you to define variables. This allows for very simple actions returning results rules - rules engine running, you can use the method setGlobal pass an object reference Javowego to represent a given variable is defined in the rules. In our example, to return the list as a result of the design rules apply:
 ... Letter 
rulesEvaluationResult = new ArrayList ();
statelessDroolsSession.setGlobal (resultList "rulesEvaluationResult);
Uruchom_reguĊ‚y

After its execution rulesEvaluationResult contains the output of the rules or what the array is put into the decision-making variable resultList .

you already have a ready decision table, we can begin to create applications. Start by creating a project using Maven'a:
 mvn archetype: create-DgroupId = pl.kadamczyk.droolssample-DartifactId = DroolsSample 

Edit your pom.xml so that they are in the following relationships:
 \u0026lt;Project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 
xsi: schemaLocation = "http:// maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd ">
\u0026lt;modelVersion> 4.0.0 \u0026lt;/ modelVersion>
\u0026lt;groupId> pl.kadamczyk.droolssample \u0026lt;/ groupId>
\u0026lt;artifactId> DroolsSample \u0026lt;/ artifactId>
\u0026lt;packaging> jar \u0026lt;/ packaging>
\u0026lt;version> 1.0-snapshot \u0026lt;/ version>
\u0026lt;name> ; DroolsSample \u0026lt;/ name>
\u0026lt;url> http://maven.apache.org \u0026lt;/ url>
\u0026lt;dependencies>
\u0026lt;dependency>
\u0026lt;groupId> Junit \u0026lt;/ groupId>
\u0026lt;artifactId> Junit \u0026lt; ; / artifactId>
\u0026lt;version> 3.8.1 \u0026lt;/ version>
\u0026lt;scope> test \u0026lt;/ scope>
\u0026lt;/ dependency>
\u0026lt;dependency>
\u0026lt;groupId> org.drools \u0026lt;/ groupId>
\u0026lt;artifactId> drools-core \u0026lt;/ artifactId>
\u0026lt;version> 4.0.7 \u0026lt;/ version>
\u0026lt;/ dependency>
\u0026lt;dependency>
\u0026lt;groupId> org.drools \u0026lt;/ groupId>
\u0026lt;artifactId> drools-compiler \u0026lt; ; / artifactId>
\u0026lt;version> 4.0.7 \u0026lt;/ version>
\u0026lt;/ dependency>
\u0026lt;dependency>
\u0026lt;groupId> org.drools \u0026lt;/ groupId>
\u0026lt;artifactId>-decisiontables drools \u0026lt;/ artifactId>
\u0026lt; version> 4.0.7 \u0026lt;/ version>
\u0026lt;/ dependency>
\u0026lt;/ dependencies>
\u0026lt;/ project>


Just
 mvn install mvn 
and
 eclipse: eclipse and import 
project to Eclipse.

Add to a project class, whose task is to create a rule base (RuleBase) on the basis delivered to the XLS file (name):
 pl.kadamczyk.droolssample package; 

import java.io.IOException;
import java.io. InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.Properties;

org.drools.RuleBase import, import
org.drools.RuleBaseFactory;
import org.drools.compiler . DroolsParserException;
org.drools.compiler.PackageBuilder import, import
org.drools.compiler.PackageBuilderConfiguration;
org.drools.decisiontable.InputType import, import
org.drools.decisiontable.SpreadsheetCompiler;
import org.drools.rule.Package;

public class DecisionTableXlsCompiler {

public RuleBase compileToRuleBase(final String decisionTablesXlsFile) throws Exception {
RuleBase result = RuleBaseFactory.newRuleBase();

final InputStream resourceAsStream = DecisionTableXlsCompiler.class.getResourceAsStream(decisionTablesXlsFile);
final String drl = compileXlsToDlr(resourceAsStream);
Package rulePackage = this.buildPackageFromDrl(drl);
result.addPackage(rulePackage);
return result;
}

private Package buildPackageFromDrl(final String drlString) throws DroolsParserException, IOException {
Properties properties = new Properties();
properties.setProperty("drools.dialect.java.compiler", "JANINO");
properties.setProperty("drools.dialect.java.lngLevel", "1.5");

final PackageBuilderConfiguration pkgBuilderCfg = new PackageBuilderConfiguration(properties);
final PackageBuilder builder = new PackageBuilder(pkgBuilderCfg);

Reader drlReader = new StringReader(drlString);
builder.addPackageFromDrl(drlReader);

Package result = builder.getPackage();

return result;
}

private String compileXlsToDlr (final InputStream xlsStream) {
final SpreadsheetCompiler SpreadsheetCompiler compiler = new ();
final String drl = compiler.compile (xlsStream, InputType.XLS) drl
return;}


}

And a class that starts the motor Rules of the pre-compiled rules base, supplying them with facts :
 pl.kadamczyk.droolssample package; 

import java.util.List;

org.drools.RuleBase import, import
org.drools.StatelessSession;

public class DroolsEvaluator {

\u0026lt;T> public void run (RuleBase ruleBase, final String outParamName, T outParamRef, final List facts \u0026lt;object>) {
final StatelessSession ruleBase.newStatelessSession statelessDroolsSession = ();
statelessDroolsSession.setGlobal (outParamName, outParamRef);
statelessDroolsSession.execute (facts);
}}


I already can run this application:
 pl.kadamczyk.droolssample package; 

import java.util.ArrayList;
import java.util.List;

import org.drools.RuleBase;

pl.kadamczyk import. droolssample.model.Activity;
import pl.kadamczyk.droolssample.model.Student;

public class Runner {

public static void main(String[] args) throws Exception {
DecisionTableXlsCompiler compiler = new DecisionTableXlsCompiler();
DroolsEvaluator evaluator = new DroolsEvaluator();

List<Object> rulesResult = new ArrayList<Object>();

RuleBase ruleBase = compiler.compileToRuleBase("decisiontable.xls");
evaluator.run(ruleBase, "resultList", rulesResult, prepareFacts());

for (Object obj : rulesResult) {
System.out.println(obj);
}
}

private static List \u0026lt;object> prepareFacts () {List \u0026lt;OBJECT>
facts \u0026lt;object> = new ArrayList ();
Activity learningActivity = new Activity ();
learningActivity.setName ("learning");
facts.add (learningActivity) ;
Activity funActivity = new Activity ();
funActivity.setName ("fun");
facts.add (funActivity);
facts.add (new Student ());

return facts;}

}

Running the example above will write the string on the console:
 student is having fun 
Student learning is


which is in line with expectations, as communicated to the rule engine facts (objects created in the method prepareFacts ()) satisfy both rules.

0 comments:

Post a Comment