Drools Operator Sample

This topic describes how to run the Drools sample application, which illustrates how to use the TIBCO StreamBase® Operator for Drools to integrate a business rule interpreted by the Drools Core rules engine. For more information about this sample, including information about optionally integrating the Drools Core plug-in with StreamBase Studio, see Using the Drools Operator.

Note

The Java code in this sample is delivered only in source code form. To build the code, you must load the sample into StreamBase Studio, which automatically builds the Java code.

Sample Overview

This sample implements a simplified Order Routing-like application, one whose sole logic is to determine whether or not an order can be routed by a downstream Smart Order Routing system, depending on the volume of the order being managed. The implementation of the routing decision is embedded in the Drools rules file, routable-order.drl.

Note

When a StreamBase field has mixed case (such as MyField), you must refer to that field in your rules file with the first letter in lowercase (myField). Otherwise, you may see errors such as "Unable to create Field Extractor for "MyField".

This Sample's Files

This sample has the following files:

  • The sample EventFlow application, rulesuser.sbapp.

  • A Drools rule file, routable-order.drl. It contains a single rule that modifies the Routable boolean field to false if the tuple contains a field Volume whose value is greater than 1,000.

  • In the java-src directory, a Java interface file, IOrder.java, that describes the relevant portion of tuples entering the Drools operator.

Running This Sample in StreamBase Studio

The steps to run this sample in Studio are as follows:

  1. In the Package Explorer, double-click to open the rulesuser.sbapp application.

  2. Make sure the application is the currently active tab in the EventFlow Editor, then click the Run button. This opens the SB Test/Debug perspective and starts the application.

  3. In the Manual Input view, select the ParentOrder input stream.

  4. Enter any Symbol and Price, and enter a Volume that is less than 1,000.

    Click Send Data, and observe that the output stream that received the data is the RoutableParentOrder stream, with a value of true for the Routable field.

  5. Modify the Volume field to a value that is greater than 1,000.

    Click Send Data, and observe that the output stream that received a new tuple is the NonRoutableParentOrder stream, with a value of false for the Routable field.

  6. When done, press F9 or click the Stop Running Application button.

Running This Sample in Terminal Windows

This section describes how to run this sample in UNIX terminal windows or Windows command prompt windows. On Windows, be sure to use the StreamBase Command Prompt from the Start menu as described in the Test/Debug Guide, not the default command prompt.

  1. In StreamBase Studio, create a bundle file for this sample's application:

    1. In the Package Explorer, right-click and select Export from the context menu.

    2. In the Export dialog, select StreamBaseStreamBase Application Bundle and click Next.

    3. On the next panel, open the sample_drools project folder and select rulesuser.sbapp.

    4. On the same panel, specify a path and file name for the bundle file, then click Finish. For example, create a bundle file with the following path name: c:\tmp\rulesuser.sbbundle or /home/sbuser/tmp/rulesuser.sbbundle.

  2. Open three terminal windows on UNIX, or three StreamBase Command Prompts on Windows.

  3. In window 1, navigate to the directory location where you saved your bundle file, then start StreamBase Server with the bundle file as its argument. For example:

    Windows:   
       cd /d c:\tmp
       sbd rulesuser.sbbundle  
    
    Linux:
       cd /home/sbuser/tmp
       sbd ./rulesuser.sbbundle
    
  4. In window 2, type:

    sbc dequeue

    This window will display tuples dequeued from the application's output ports.

  5. In window 3, type:

    sbc enq ParentOrder

    This window is now ready to accept tuples typed with CSV syntax.

  6. In window 3, type:

    IBM, 400, 176.48

  7. Observe in window 2 one tuple is emitted on the RoutableParentOrder stream, and its Routable field is marked true:

    RoutableParentOrder,IBM,400,176.48,true

  8. In window 3, type:

    DELL, 1500, 17.15

  9. Observe in window 2 that one tuple is emitted on the NotRoutableParentOrder stream, and its Routable field is marked false:

    NotRoutableParentOrder,DELL,1500,17.15,false

  10. In window 2, type Ctrl+C to terminate the dequeuer. Then type the following command to shut down the server:

    sbadmin shutdown

Importing This Sample into StreamBase Studio

In StreamBase Studio, import this sample with the following steps:

  • From the top menu, select FileLoad StreamBase Sample.

  • Select drools from the Extending StreamBase category.

  • Click OK.

StreamBase Studio creates a project for the Drools sample in your current Studio workspace.

Sample Location

When you load the sample into StreamBase Studio, Studio copies the sample project's files to your Studio workspace, which is normally part of your home directory, with full access rights.

Important

Load this sample in StreamBase Studio, and thereafter use the Studio workspace copy of the sample to run and test it, even when running from the command prompt.

Using the workspace copy of the sample avoids the permission problems that can occur when trying to work with the initially installed location of the sample. The default workspace location for this sample is:

studio-workspace/sample_drools

See Default Installation Directories for the location of studio-workspace on your system.

In the default TIBCO StreamBase installation, this sample's files are initially installed in:

streambase-install-dir/sample/drools

See Default Installation Directories for the location of streambase-install-dir on your system. This location may require administrator privileges for write access, depending on your platform.