JBoss.orgCommunity Documentation

Chapter 2. Getting Started

2.1. Installation
2.2. Creating your first process
2.3. Executing your first process

This section describes how to get started with Drools Flow. It will guide you to create and exectue your first Drools Flow process.

The best way to get started is to use the Drools Eclipse IDE. This is a plugin for the Eclipse developement environment that allows users to create, execute and debug Drools processes and rules.

To get started, you need an Eclipse 3.3.x, as well as the Eclipse Graphical Editing Framework (GEF) plugin installed. Eclipse can be downloaded from http://www.eclipse.org/downloads/ (choose either the Eclipse IDE for Java Developers or the Eclipse Classic). Eclipse GEF can also be downloaded from http://www.eclipse.org/gef/downloads/ (choose the corresponding version) or by using an update site.

Download the Drools Eclipse IDE plugin from http://www.jboss.org/auth/drools/downloads.html (the latest snapshot build can also be downloaded from https://hudson.jboss.org/hudson/job/drools/lastSuccessfulBuild/artifact/trunk/target/), unzip it in your eclipse folder and (re)start Eclipse. If installation was successful, the Drools menu action should appear in the top menu bar.

You should switch to the Drools perspective within Eclipse first, as this will open all the relevant views for you. You can do this by clicking on the Open Perspective button (top right of your screen) and selecting Other ... -> Drools.

A new project wizard can be used to setup an executable project to start using processes immediately. This will setup a basic structure, classpath, sample process and execution code to get you started. To create a new Drools Project, select File -> New -> Project ... and in the Drools folder, select Drools Project. This should open the following dialog:

Give your project a name and click Next. In the following dialog you can select which elements are added to your project by default. Since we are creating a new process, deselect the first two checkboxes and select the last two. This will generate a sample process and a class to execute this process in your project.

The end result should look like this and contains:

The RuleFlow editor contains a graphical representation of your process definition. It consists of nodes that are connected. The editor shows the overall control flow, while the details of each of the elements can be viewed (and edited) in the Properties View at the bottom. The editor contains a palette at the left that can be used to drag-and-drop new nodes, and an outline view at the right.

This process is a simple sequence of three nodes. The start node defines the start of the process. It is connected to an action node (called 'Hello' that simply prints out 'Hello World' to the standard output. You can see this by clicking on the Hello node and checking the action property in the properties view below. This node is then connected to an end node, signalling the end of the process.

While it is probably easier to edit processes using the graphical editor, user can also modify the underlying XML directly. The XML for our sample process is shown below (note that we did not include the graphical information here for simplicity). The process element contains parameters like the name and id of the process, and consists of three main subsections: a header (where information like variables, globals and imports can be defined), the nodes and the connections.

<?xml version="1.0" encoding="UTF-8"?>
<process xmlns="http://drools.org/drools-4.0/process"
         xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
         xs:schemaLocation="http://drools.org/drools-4.0/process drools-processes-4.0.xsd"
         type="RuleFlow" name="ruleflow" id="com.sample.ruleflow" package-name="com.sample" >

  <header>
  </header>

  <nodes>
    <start id="1" name="Start" x="16" y="16" />
    <actionNode id="2" name="Hello" x="128" y="16" >
      <action type="expression" dialect="mvel" >System.out.println("Hello World");</action>
    </actionNode>
    <end id="3" name="End" x="240" y="16" />
  </nodes>

  <connections>
    <connection from="1" to="2" />
    <connection from="2" to="3" />
  </connections>

</process>

To execute this process, right-click on RuleFlowTest.java and select Run As - Java Application. When the process in executed, the following output should appear on the console:

Hello World

If you look at the RuleFlowTest code (see below), you will see that executing a process requires a few steps:

package com.sample;

import java.io.InputStreamReader;
import java.io.Reader;

import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.StatefulSession;
import org.drools.compiler.PackageBuilder;
import org.drools.rule.Package;

/**
 * This is a sample file to launch a ruleflow.
 */
public class RuleFlowTest {

  public static final void main(String[] args) {
    try {
      //load the process
      RuleBase ruleBase = createKnowledgeBase();
      // create a new session
      StatefulSession session = ruleBase.newStatefulSession();
      // start a new process instance
      session.startProcess("com.sample.ruleflow");
    } catch (Throwable t) {
      t.printStackTrace();
    }
  }

  /**
   * Creates the knowledge base by loading the process definition.
   */
  private static RuleBase createKnowledgeBase() throws Exception {
    // create a builder
    PackageBuilder builder = new PackageBuilder();
    // load the process
    Reader source = new InputStreamReader(
      RuleFlowTest.class.getResourceAsStream("/ruleflow.rf"));
    builder.addProcessFromXml(source);
    // create the knowledge base 
    Package pkg = builder.getPackage();
    RuleBase ruleBase = RuleBaseFactory.newRuleBase();
    ruleBase.addPackage(pkg);
    return ruleBase;
  }

}

Congratulations, you have successfully executed your first process! You can now start experimenting and designing your own process by modifying our example. Note that you can validate your process by clicking on the green check box action in the upper toolbar. Processes will also be validated upon save and errors will be shown in the error view. Or you can continue reading our documentation to learn about our more advanced features.