Chapter 3. Rule Flows


Figure 3.1. A Rule Flow

A rule flow is a flow chart that describes the order in which a series of steps need to be undertaken. It consists of a collection of nodes that are linked to each other by connections. Each of the nodes represents one step in the overall process while the connections specify how to transition from one node to the other. A large selection of predefined node types have been supplied. Read the rest of this chapter to learn how to define and use rule flows in your application.

3.1. Creating a Rule Flow Process

Create rule flows in one of these three ways:
  1. By using the graphical Rule Flow Editor (part of the JBDS' JBoss Rules plug-in.)
  2. By writing an XML file, according to the XML process format as defined in the XML Schema definition for JBoss Rules processes.
  3. By directly creating a process using the Process API.

3.1.1. Using the Graphical Rule Flow Editor

The Rule Flow Editor is a graphical tool that allows you to create a process by dragging and dropping different nodes onto a canvas. It then allows you to edit the properties of these nodes.
Once you have set up a JBoss Rules project in the JBDS, you can start adding processes: When in a project, launch the New wizard by using the Ctrl+N shortcut or by right-clicking on the directory in which you would like to put your rule flow and selecting New, then Other....
Choose the section on JBoss Rules and then pick Rule Flow file. A new .rf file is created.
The Rule Flow Editor now appears.
Switch to the JBoss Rules Perspective. This will tweak the user interface so that it is optimal for rules. Then,
Next, ensure that you can see the Properties View (at the bottom of the JBDS window). If you cannot see the properties view, open it by going to the Window menu, clicking Show View and then Other....
Next, under the General directory, select the Properties View.
The Rule Flow Editor consists of a palette, a canvas and an Outline View. To add new elements to the canvas, select the element you would like to create and add it by clicking on your preferred location. For example, click on the RuleFlowGroup icon in the Components palette of the GUI and then draw a few rule flow groups.
Clicking on an element in your rule flow allows you to set its properties. You can connect the nodes (as long as it is permitted by the different types of nodes) by using Connection Creation from the Components palette.
Keep adding nodes and connections to your process until it represents the business logic that you want to specify.
Finally, check the process for any missing information (by pressing the green Check icon in the IDE menu bar) before using it in your application.

3.1.2. Defining Processes Using XML

You can also specify processes by writing the underlying XML by hand. The syntax of these XML processes is defined by a schema definition. For example, the following XML fragment shows a simple process made up of a Start node, an Action node that prints "Hello World" to the console, and an End node:
<?xml version="1.0" encoding="UTF-8"?>
<process xmlns="http://drools.org/drools-5.0/process"
         xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
         xs:schemaLocation="http://drools.org/drools-5.0/process drools-processes-5.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>
Copy to Clipboard Toggle word wrap
The process XML file must contain only one <process> element. This element contains parameters related to the process (its type, name, ID. and package name), and consists of three subsections: a <header> (where process-level information like variables, globals, imports and swimlanes are defined), a <nodes> section that defines each of the nodes in the process, and a <connections> section that contains the connections between all the nodes in the process.
In the nodes section, there is a specific element for each node. Use these to define the various parameters and sub-elements for that node type.

3.1.3. Defining Processes Using the Process API

Warning

Red Hat does not recommend using the APIs directly. You should always use the Graphical Editor or hand-code XML. This section is only included for the sake of completeness.
It is possible to define a rule flow directly via the Process API. The most important process elements are defined in the org.drools.workflow.core and org.drools.workflow.core.node packages.
The fluent API allows you to construct processes in a readable manner using factories. At the end, you can validate the process that you were constructing manually.

3.1.3.1. Example One

This is a simple example of a basic process that has a rule set node only:
RuleFlowProcessFactory factory =
    RuleFlowProcessFactory.createProcess("org.drools.HelloWorldRuleSet");
factory
    // Header
    .name("HelloWorldRuleSet")
    .version("1.0")
    .packageName("org.drools")
    // Nodes
    .startNode(1).name("Start").done()
    .ruleSetNode(2)
        .name("RuleSet")
        .ruleFlowGroup("someGroup").done()
    .endNode(3).name("End").done()
    // Connections
    .connection(1, 2)
    .connection(2, 3);
RuleFlowProcess process = factory.validate().getProcess();
Copy to Clipboard Toggle word wrap
Note from the above that you start by calling the static createProcess() method from the RuleFlowProcessFactory class. This method creates a new process with the given ID.
A typical process consists of three parts:
The header part is made up of global elements like the name of the process, imports, variables and so on.
The nodes section contains all the different nodes that make up the process.
The connections section finally links these nodes to each other to create a flow chart.
In the example above, the header contains the name and the version of the process. It also contains the package name. Following on from that, you can start adding nodes to the current process. If you are using auto-completion you can see that you different methods are available to you to create each of the supported node types at your disposal.
To start adding nodes to the process in this example, call the startNode(), ruleSetNode() and endNode() methods.
You will see that these methods return a specific NodeFactory, that allows you to set their properties.
Once you have finished configuring a specific node, call the done() method to return to the current RuleFlowProcessFactory so you can add more nodes if necessary.
When you have finished adding all the nodes, connect them by calling the connection method.
Finally, call the validate() method to check your work. This will also retrieve the RuleFlowProcess object you created.

3.1.3.2. Example Two

This example shows you how to use Split and Join nodes:
RuleFlowProcessFactory factory =
    RuleFlowProcessFactory.createProcess("org.drools.HelloWorldJoinSplit");
factory
    // Header
    .name("HelloWorldJoinSplit")
    .version("1.0")
    .packageName("org.drools")
    // Nodes
    .startNode(1).name("Start").done()
    .splitNode(2).name("Split").type(Split.TYPE_AND).done()
    .actionNode(3).name("Action 1")
        .action("mvel", "System.out.println(\"Inside Action 1\")").done()
    .actionNode(4).name("Action 2")
        .action("mvel", "System.out.println(\"Inside Action 2\")").done()
    .joinNode(5).type(Join.TYPE_AND).done()
    .endNode(6).name("End").done()
    // Connections
    .connection(1, 2)
    .connection(2, 3)
    .connection(2, 4)
    .connection(3, 5)
    .connection(4, 5)
    .connection(5, 6);
RuleFlowProcess process = factory.validate().getProcess();
Copy to Clipboard Toggle word wrap
Note from the above that a Split node can have multiple outgoing connections, and a Join node multiple incoming connections.

3.1.3.3. Example Three

This more complex example demonstrates the use of a ForEach node and nested action nodes:
RuleFlowProcessFactory factory =
    RuleFlowProcessFactory.createProcess("org.drools.HelloWorldForeach");
factory
    // Header
    .name("HelloWorldForeach")
    .version("1.0")
    .packageName("org.drools")
    // Nodes
    .startNode(1).name("Start").done()
    .forEachNode(2)
        // Properties
        .linkIncomingConnections(3)
        .linkOutgoingConnections(4)
        .collectionExpression("persons")
        .variable("child", new ObjectDataType("org.drools.Person"))
        // Nodes
        .actionNode(3)
            .action("mvel", "System.out.println(\"inside action1\")").done()
        .actionNode(4)
            .action("mvel", "System.out.println(\"inside action2\")").done()
        // Connections
        .connection(3, 4)
        .done()
    .endNode(5).name("End").done()
    // Connections
    .connection(1, 2)
    .connection(2, 5);
RuleFlowProcess process = factory.validate().getProcess();
Copy to Clipboard Toggle word wrap
Note how the linkIncomingConnections() and linkOutgoingConnections() methods that are called to link the ForEach node with the internal action node. These methods are used to specify the first and last nodes inside the ForEach composite node.
Back to top
Red Hat logoGithubredditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust. Explore our recent updates.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

Theme

© 2026 Red Hat