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.

Sunday, May 10, 2009

Headaches With Fish Oil

GeeCON 2009

Last week was marked by GeeCON conference - time to summarize two days spent in Krakow.

Organizers chose the venue cinema (Multikino Krakow), which had its pluses, positive and negative;) Best result is an enormous screen behind the speaker, which slides are displayed - and it is a step in the right direction. The problem is, however, the right lighting - to the darkened room cinema speaker was visible, and the light does not illuminate the screen. Unfortunately, not completely managed to do during the first lecture Simon Ritter "JavaFX: The Platform for Rich Internet Applications." From the place where I sat some of the slides were practically invisible. He mentioned this to the same speaker, who often have to describe what should be visible on the screen.

From GeeCON

lighting problems were partially solved and then it was much better. An interesting and successful gentlemen gave a lecture to SpringSource: Alef Arendsen and Arjen Poutsma . They talked about the news in Spring 3.0, and Arjen has also started the second day of the conference an interesting lecture on Spring and RESTful Web Services.

From GeeCON

Next came time for the lecture Waldek Kota about virtualization - interesting, but probably a little too detailed, and somewhat stremowanego Jack Laskowski who presented on OpenEJB. Jacek went to the lecture probably a bit conservative and does not give any example at the beginning of "live". It seems that some jitters got to him associated with the occurrence of English. The concerns were unnecessary, because the language was good - but could use some more interesting topic, in which Jace could further spread my wings;)

On the first day I attended a great lecture yet Vaclav Pecha "Practical Groovy" - a way of presentation, showing examples , contact with the audience - all at six. Vaclav showed how Groovy helps to resolve some problems and how to easily integrate it with Java.

remember the second day mainly due to two very good lectures Antonio Goncalves 's, and a great run in a funny lecture by Bruno Bossoli. Antonio asked to substitute one of the speakers presented the GlassFish application server and the second (planned), the latest in a lecture devoted JavaEE6.
Bossola Bruno, a member of the JUG Torino, discussed the principles of software design in a very charismatic, seasoned with a large dose of humor way. Every once in a while, the slides containing found fragments of the funniest comments in the code:

From GeeCON

very important elements of the conference was "additional attractions, among which the most interesting was the Beer Certification Path, which is round of the pubs in Krakow on Thursday evening. In each of the four pubs to pick up Sun's was a beer:) and the stamp - one of the letters of the word JAVA:

From GeeCON

Great idea: D By the way, we have another argument in the superiority of Java over C # - Java has more letters;)

short - a few interesting lectures, afternoon beer in Krakow - is made generally think that the conference a success. Certainly the success of brilliance eclipsed technical problems at the beginning of the light, then connecting the laptop, especially "poppies" for the projectors, but we all learn from our mistakes and I'm sure that GeeCON 2010 will be even better - and the more such events, the better for us :)