Donnerstag, 6. Juni 2013

New project: Optaplanner - Getting started

Today I have started to work with OptaPlanner. To say the truth, I already started reading the documentation a few days ago. But today I wanted to start implementing a first application.

So what is OptaPlanner and why do I want to test it. My first contact with OptaPlanner had been a few weeks ago, when looking for planning and optimization frameworks based on Java. Currently the system I am developing is using a Java implementation of FF planner - the implementation is pure and very hard to maintain. This is the reason I am looking for a better more flexible solution of a planning system. I am not sure if OptaPlanner fulfills the requirements. At the moment I am formulating my problems using PDDL (Planning Domain Definition Language). But I will talk about this later.

So now lets get started - or rather lets get the examples running. First we create a new eclipse maven project. Next we add the optaplanner core dependency:

    <dependency>
        <groupId>org.optaplanner</groupId>
        <artifactId>optaplanner-core</artifactId>
        <version>${optaplanner.version}</version>
    </dependency>


The version I am using is 6.0.0.Beta3. Next thing I wanted to do is to add the example sources of the optaplanner homepage. So after downloading and extracting the archive, we just copy the source code into the eclipse project... and get a lot of problems.

One problem is the missing XStreamScoreConverter class, which is part of the optaplanner-benchmark module. Hence, we add this one to the project, which solve some of our problems. But still we have some more errors. Some missing classes of the org.jdom package are the problem. So we have to add one more dependency (I got the version information from the example module of optaplanner).

    <dependency>
        <groupId>org.jdom</groupId>
        <artifactId>jdom</artifactId>
        <version>1.1</version>
    </dependency>

...and finally no more problems. Getting the examples to work require two more things. First you have to copy the resources into your eclipse resource folder. Second you have to copy the example data (the folder is named data as well) into your project. That's it. Now I could finally run the examples and inspect the source code. As you can see in the following picture everything works fine for me now.


So that's it for now... more to come... I will try to understand what is happing and how it works.

Mittwoch, 5. Juni 2013

Drools presentation

When I started writing about Drools in this blog, I wrote that I have to prepare a presentation. And this presentation is finally done. I uploaded it to slideshare. You can watch it here - beware it is in German.

If you like it, or you want to use some of the stuff inside, I would be very happy if you could inform me and also add a reference to my blog. If you want to have a PDF version just contact me.

Drools Part 4 - Decision Tables

When taking a look at the Drools documentation, the second chapter deals with decision tables. So I thought, it is time to take a look into this topic. Instead of using  the previously presented DRL files and the MVEL-syntax to formulate rules, Drools also supports so called decision tables. Refering to the documentation "Decision tables are a 'precise yet compact' (ref. Wikipedia) way of representing conditional logic, and are well suited to business level rules". If you want to get more detailled information just take a look at chapter 2.4 of the Drools documentation or go on with reading this post.

So I took a deeper look into this function. After a first look it seems to be a really interesting and compact way to formulate rules. In my sensor scenario I wanted to use this feature, to identify different "levels" of a measurement. Starting with a simple example it should be something like:
  • when measured value > 0 and <= 50 then set quality level to normal and debug
  • when measured value > 50 and <= 100 then set quality level to high and debug
  • when measured value > 101 then set quality level to critical and debug
Using a decision table first of all we have to set up the table structure. I decided to use Microsoft Excel. Because Drools does not support the new .xlsx format I have to save my work as .xls.

A decision table consists of a rule set that contains the definition of the used types, imports etc. and the rule tables. The rule set looks like shown in the picture:


It is as easy as it looks. The rule table is also very easy to set up. The following picture shows the solution for the above rule set.


That’s all what we have to do. Now let’s change our source code, so that the decision table is loaded instead. We have to start the same way as before by creating the KnowledgeBuilder.

   KnowledgeBuilder kbuilder = 
      KnowledgeBuilderFactory.newKnowledgeBuilder();

Next we have to add the resource. Because we are using a decision table, the resource path as well as the type of the resource has to be updated. The rest is similar to the initialization of a knowledge base.

   kbuilder.add(ResourceFactory.newClassPathResource(
      "/dt_example.xls", ResourceType.DTABLE, dtc);   
   KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); 
   kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

One important thing is the dtc variable. This one contains the so-called decision table configuration. It is created using the KnowledgeBuilderFactory.

   DecisionTableConfiguration dtc = 
      KnowledgeBuilderFactory.newDecisionTableConfiguration(); 
   dtc.setInputType(DecisionTableInputType.XLS);   
   dtc.setWorksheetName(“Sheet 1”);

That’s it. Have fun.