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:
table definition starts with the text in the cell marked RuleTable
- there and there is a Student Activity called fun
- there and there is a Student Activity called learning
... 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 mvnand
eclipse: eclipse and importproject 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