Netbeans and JBoss 7

As much I like to use Glassfish, customers might have different taste or policy. We are challenged now to run our EJB/Web-application on JBoss as well. Netbeans supports JBoss up to version 6 but not the latest version 7 which is fully EE6 web profile compatible. Version 7 is not (yet) supported due to major revamp of the management API of JBoss. I found one plugin provided by Oleg Kulikov on github, a working prototype that can control Jboss 7 server from Netbeans, but cannot deply an application to it or debug it. I wish I know more about Netbeans RCP development, but it is not an easy task to create this kind of highly integrated NB plugin.
Discussion as Netbeans issue here.

JBoss Plugin

JBoss Plugin

Gettings started with JBoss jBPM5

In my journey through all the modules of Drools (Expert, Fusion, Planner), I also visit jBPM (fomerly known as Drools Flow). In this tutorial we just get started with Netbeans and a simple hello world project and focusing on the basics, such as required libraries and running within the Netbeans IDE.
Netbeans is not as close to the BPM community as Eclipse, there is no plugin available, the SOA project is abandoned (I guess). But using Netbeans as our IDE, I will not move over to Eclipse, but see how I can run and debug with Netbeans but still use some Eclipse features.

About BPMN:

The JBoss engine execute BPMN files, we can create this files manually or using one of the visual tools. For the basic understanding you should create a simple BPMN by hand to learn a bit about the notation.

Lets create the most simple BPM flow possible:

Pre-Requisites:

Continue reading

Getting started with Drools Fusion

JBoss Drools is more than Expert and Guvnor only, the suite of products also offers Fusion, Planner and Flow (now called jBPM). After getting my hands with dirty with Expert, I want to look at the Fusion part, which covers the event handling. Based on CEP (Complex Event Processing) as an event-driven architecture which is an own science by itself, whole books written about nothing else than events (link).

There is not much information on the web about Fusion other than the JBoss documentation, some chapters in Books (Packt Publishing: Drools 5 Developer Guide, Drools Cookbook) and a handful of blog entries (link), practically no working samples or end-to-end tutorials.

In this tutorial we will apply the necessary changes to our HelloDrools project from the previous tutorial:

  • The good thing: We dont need any other libraries than the ones already used.
  • Copy the previous project and give it a new name ‘HelloDroolsFusion’
  • In the last tutorial we had a simple message class and inserted a message as fact. A rule was triggered when the message type is equal to ‘Hello’
  • As refresher: The first rule
    import hellodrools.Message
    rule "Hello World"
    when
        message:Message (type=="Hello")
    then
        System.out.println("Hello World, Drools!");
    end
    

    Continue reading

Debugging Drools Rules

At the ‘hello world’ level we cant see much need to debug our rules, but with growing complexity we will be challenged quickly. We need to see which rules was fired, what parameter, what object, etc. Eclipse IDE users have the advantage of the plugin which even visualizes the RETE tree, for the rest-of-us, aka Netbeans user, we need to rely on the debugging output available. We have 4 options that give us access to almost all information of interest.

To try the below debugging options, use the previous HelloDrools Tutorial

1. Default Debug Listener

Out-of-the-box we can use 2 debug event listener:

        ...
        ksession = kbase.newStatefulKnowledgeSession();

        ksession.addEventListener( new DebugAgendaEventListener() );
        ksession.addEventListener( new DebugWorkingMemoryEventListener() );
        ...

Continue reading

Netbeans + Visual Paradigm = EJB Tutorial (Part 4)

Based on the previous parts of this tutorial (1, 2, 3) we will modify the application and Glassfish to connect to PostgreSQL or Oracle as DB.

  • Preparing Glassfish libraries
    Download the drivers for Oracle and PostgreSQL and add them to your {GLASSFISHHOME}/domains/{YOURDOMAIN}/lib
    Restart Glassfish.

    Glassfish lib folder

    Continue reading

Drools Expert read rules from String and Database

All samples read their rules from a drl textfile, in a real production rather unlikely to happen. You would rather read the rules from a DB and add it to the Knowledgebase. The add method syntax is public void add(Resource rsrc, ResourceType rt) and does not support a String as a parameter. So here the workaround to convert a String (that you would read from DB) to a resource.

        // read rule from String
        String myRule = "import hellodrools.Message rule \"Hello World 2\" when message:Message (type==\"Test\") then System.out.println(\"Test, Drools!\"); end";
        Resource myResource = ResourceFactory.newReaderResource((Reader) new StringReader(myRule));
        kbuilder.add(myResource, ResourceType.DRL);

Please note: Drools rules dont need a carriage return, only for simplicity I dropped them here.

Up to you where you retrieve the String from, read from DB, XML, user input,…

I have noticed the previous tutorial I was using some kind of legacy packages. It works but is not in sync with the current Drools Documentation and samples. So here the updated HelloDrools code.


package hellodrools;

import java.io.Reader;
import java.io.StringReader;
import java.util.Collection;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.definition.KnowledgePackage;
import org.drools.io.Resource;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;

public class HelloDroolsNew {

    private static KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    private static Collection<KnowledgePackage> pkgs;
    private static KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    private static StatefulKnowledgeSession ksession;

    public static void main(final String[] args) {

        initDrools();
        initMessageObject();
        fireRules();

    }

    private static void initDrools(){

        // this will parse and compile in one step
        // read from file
        kbuilder.add( ResourceFactory.newClassPathResource( "/hellodrools/testrules.drl",HelloDroolsNew.class),ResourceType.DRL );

        // read second rule from String
        String myRule = "import hellodrools.Message rule \"Hello World 2\" when message:Message (type==\"Test\") then System.out.println(\"Test, Drools!\"); end";
        Resource myResource = ResourceFactory.newReaderResource((Reader) new StringReader(myRule));
        kbuilder.add(myResource, ResourceType.DRL);

        // Check the builder for errors
        if ( kbuilder.hasErrors() ) {
            System.out.println( kbuilder.getErrors().toString() );
            throw new RuntimeException( "Unable to compile drl\"." );
        }

        // get the compiled packages (which are serializable)
        pkgs = kbuilder.getKnowledgePackages();

        // add the packages to a knowledgebase (deploy the knowledge packages).
        kbase.addKnowledgePackages( pkgs );

        ksession = kbase.newStatefulKnowledgeSession();
    }

    private static void fireRules(){
        ksession.fireAllRules();
    }

    private static void initMessageObject() {
        Message msg = new Message();
        msg.setType("Test");
        ksession.insert(msg);
    }
}

Netbeans running the Drools samples

Drools is closer to Eclipse because there is a plugin that supports Drools projects with flows, rules, etc. I still prefer Netbeans because it is our corporate IDE of choice and we can live without the visualization or the syntax highlighting of drl files.
The Drools distribution file (download from here)  comes with a bunch of samples that helps you get going with your own projects, usually easier to get hands-on code than to read AI style documentation. Samples and tutorials are for the rest of us.

Netbeans is smart enough to open the examples folder as maven project.

Open Maven Project

When you open the project the first time it will open as “unloadable” because of the missing dependencies. Just right-click on the project “resolve problems” and “prime” the project which will download all the required libraries.

Then you can execute the individual samples (Run File). Voila !

Running the examples

Getting started with Drools and Netbeans

Rule engines are a rather complex topic with potentially a steep learning curve. I am looking at a few options, Drools being one of them. As usual one can read all the theoretical papers, but rather I have a something to look at and play with fast. Ultimately I need to integrate the rule engine into my application, so I dont want to play with eclipse editors for rules but seeing a rule engine ticking inside my Netbeans project. I found one tutorial from Vishal Akhouri which I updated to the latest version of Drools and some minor fixes. Most of the sample source code is from Vishal.

Pre-Requirements:

Tutorial:

Netbeans + Visual Paradigm = EJB Tutorial (Part 3)

In part 1 of this tutorial we created an EJB using Netbeans and Visual Paradigm, in part 2 a little ZK application to read data using the EJB. In part 3 we will move away from the Derby DB to PostgreSQL and Oracle DB and challenge ourselves with identifier more than 30 characters, which is an issue for Oracle (yes, it is 2011). We will add columns with more than 30 characters and play with a few different column types (the ones showing up in a normal DB layout).

  • Add new fields to the ERD
    this_is_a_very_long_remark_field
    floatcol
    numbercol

    Updated ERD

  • Continue reading