Drools Fusion Samples

I want to share some more tips and more realistic data and rules scenarios.

  • Netbeans: If you run the java file (Run File)with the sample code instead of running the whole application, the resources and NOT rebuild, means any changes made to the rules would not be reflected. So rither Cleand and Build or run the application then resources are rebuild automatically.
  • Threads: In the first sample we did not use a separate thread to fire the rules. If you update or insert facts after the initial ksession.fireAllRules() you will not see any output from the engine, only if you call it again. I recommend the thread approach with ksession.fireUntilHalt()as shown in the last tutorial.
  • Container: A simple java application might be good enough for the first steps, but more realistic is the execution in a web application running in a container. I will post a separate tutorial how to run the rule engine inside a singleton EJB on Glassfish.

Rule samples and code snippets:  Managing Flights and Times
The previous samples with message instances are not close to anything realistic, so I create a little airport scenario. It wont be able to run an airport’s operations but gives an idea. I skip the basic initialization etc. We will use the pseudo clock.

  • We operate an airport with arriving and departing flights.
  • Our simplified data model
    Flights with attributes: Flightnumber, airlinecode, aircrafttype, origin, destination, number of pax.  + internal primary key
    Flight times with attributes:   Timestamp, type. + foreign key to Flights
  • Arriving flights usually have this or similar sequence of events: Scheduled Time (STOA) – Estimated (ETOA) – Landed/Touchdown (LAND) – OnBlock Time (ONBL)
    (in real operations you will have more events, also from different sources)
  • Rule 1: Alert me if the ETOA (often coming from automated interfaces like ATC or IATA Telex messages from the origin airport of the flight) is more than 30min after scheduled arrival time  STOA (maybe to inform public at the airport)
  • Rule 2: Alert me if the time between LAND and ONBL is more than 5min.
  • The rule does not make much operational sense, because you want to warn someone that the aircraft is still not at the position after minutes on the ground, means it is still taxiing and you want to inform the ground crew about it
    Rule 2b: Alert me if there is no ONBL 5min after the LAND event.
  • The Flight class
    public class Flight {
    
        private String flightKey;
        private String flightNumber;
        private String airline2lc;
        private String origin;
        private String destination;
        ... more attributes, getter-setter etc.
    }
    
  • The Flight Time class
    public class FlightTime {
    
        private Date flighttime;
        private long flighttimestamp;
        private String type;
        private String flightkey;
        ... constructor,getter-setter etc.
    }
    
  • For the simple comparison of times (Rule 1 and 2) we can ignore the session clock and use this simple rule
    rule "RULE 5"
    when
        time1:FlightTime(key:flightkey,type=='STOA') from entry-point entryone
        time2:FlightTime(flightkey == key, type=='ETOA', this after[ 30m ] time1) from entry-point entryone
    then
        System.out.println("More than 30min between STOA and ETOA: " + time1.getFlightkey() );
    end
    

    Insert facts (flight times), rules get fired by the separate thread (see previous sample)

    ..
    entrypoint1.insert(new FlightTime(new SimpleDateFormat("MM/dd/yy HH:mm").parse("03/08/12 12:00"), "STOA", "1"));
    entrypoint1.insert(new FlightTime(new SimpleDateFormat("MM/dd/yy HH:mm").parse("03/08/12 12:40"), "ETOA", "1"));
    ..
    
  • For Rule 2b we need to use the session clock
    rule "RULE 6"
    when
        time1:FlightTime(key:flightkey,type=='LAND') from entry-point entryone
        not (
            FlightTime(flightkey == key, type=='ONBL', this after[ 0,5m ] time1) from entry-point entryone
            )
    then
        System.out.println("Missing ONBL after 5min LAND: " + time1.getFlightkey() );
        System.out.println("Session Time at rule trigger: " + (drools.getWorkingMemory().getSessionClock().getCurrentTime())/60000);
    end
    

    Note: We cant use the separate thread like previously, but we must fire ksession.fireAllRules(); everytime we advance the clock !

         SessionPseudoClock clock = ksession.getSessionClock();
    
         clock.advanceTime(2, TimeUnit.MINUTES);
         entryPoint1.insert(new FlightTime((clock.getCurrentTime()), "LAND", "1"));
    
         clock.advanceTime(10, TimeUnit.MINUTES);
    
         ksession.fireAllRules();
         ..
    
  • Run with Rule 2b

    Missing ONBL after 5min

  • Challenge for now: The engine looks at the timestamp of fact insertion, so I cant backdate my timestamps, which would be necessary if you get delayed updates from an interface.
    Eg. at 02:00 you get LAND with timestamp 01:00 you would expect the rule to be triggered at 06:00 (01:00 + 5min) but it gets triggered at 07:00
    Will explore Fusion further to cover this situation.

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

MULE ESB Config Snippet: Most Simple Flow

For the first sample I create, I show all the details, subsequently I will only quote the configuration file.

  1. Create a new Mule Project
    (without sample code)

    New Mule Project

  2. Create a new configuration
    (add only standard I/O)

    New Configuration

    New Configuration

  3. This is an empty configuration file. You will always start from here.
    <?xml version="1.0" encoding="UTF-8"?>
    <mule xmlns="http://www.mulesoft.org/schema/mule/core"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:stdio="http://www.mulesoft.org/schema/mule/stdio"
    
          xsi:schemaLocation="
              http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.1/mule.xsd
              http://www.mulesoft.org/schema/mule/stdio http://www.mulesoft.org/schema/mule/stdio/3.1/mule-stdio.xsd">
    
    </mule
    

    This does basically nothing.

  4. The most basic concept you need to understand:
    • A flow describes the sequence of activities that happen to a piece of information (message)
    • You define one or more connectors
    • A message comes from somewhere (inbound-endpoint)
    • You do something to it (or not)
    • The message goes somewhere (outbound-endpoint)
  5. The most simple flow I could think of: You enter a value at the console and you get it back at the console !
    Refer to the stdio document for parameters.

    <?xml version="1.0" encoding="UTF-8"?>
    <mule xmlns="http://www.mulesoft.org/schema/mule/core"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:stdio="http://www.mulesoft.org/schema/mule/stdio"
    
          xsi:schemaLocation="
              http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.1/mule.xsd
              http://www.mulesoft.org/schema/mule/stdio http://www.mulesoft.org/schema/mule/stdio/3.1/mule-stdio.xsd">
    
    	<stdio:connector name="stdioConnector" messageDelayTime="1000" promptMessage="IN: " outputMessage="OUT:  "  />
    
    	<flow name="SimpleFlow">
    		<stdio:inbound-endpoint system="IN"  exchange-pattern="one-way" connector-ref="stdioConnector"/>
    		<stdio:outbound-endpoint system="OUT" exchange-pattern="one-way" connector-ref="stdioConnector"/>
    	</flow>
    </mule>
    

     

  6. Run (better Debug) the configuraion. Run as Mule Server
    If you debug, you can terminate the Mule server from eclipse, if you run you wont be able to stop the server (due to some plugin bug I guess)

    Run Mule Server

    Mule Server Running