Decision Table for Camera Placement Scoring

Building a JBoss BPMS Rules Application without Writing Code

This article assumes some basic knowledge of the JBoss BPM Suite including using Business Central.

JBoss BPM Suite (BPMS) and JBoss Business Rule Management System (BRMS) 6.1 introduced a new component called the Real-time Decision Server (RTDS). Rule projects built using BPMS can be deployed directly onto the Real-time Decision Server via Business Central. Applications can instantiate and execute rules on the RTDS using either a REST or JMS interface.

In this article, I am going to show you how you can build a rule-based application without writing even a single line of Java code. The application aims to rate locations for placement of mobile speed cameras. It is an example application I made up and is not being used by any Police Departments. My simplified speed camera placement rating criteria are based on:

  • Number of speeding incidents from the past month in that location
  • Is the location near a school ?
  • Is the location having roadwork in progress?

To build and deploy this speed camera application involves the following steps:

  1. Create a data model
  2. Create a Guided Decision Table for placement scoring locations
  3. Create a Domain Specific Language (DSL) Definition
  4. Create a Guided Rule using the DSL
  5. Create test scenario to test the rules
  6. Build and Deploy rule project on RTDS
  7. Execute your rules using the RESTinterface via SoapUI

The following sections outline how this is done pictorially.

Creating a Data Model

Use Business Central’s Data Modeler to create 2 data model: Location and SchoolHolidays, as shown.

Data Model for Location
Data Model for Location
Data Model for SchoolHolidays
Data Model for SchoolHolidays

Creating a Guided Decision Table

Create a Guided Decision Table like that shown. The Decision table is like a truth table which provides a placement score to a location depending on the range of speeding incidents in the area and whether it is near a school or having roadwork in progress.

Decision Table for Camera Placement Scoring
Decision Table for Camera Placement Scoring

Creating a Domain Specific Language Definition

Domain Specific Languages (DSLs) are a way of creating a rule language that is dedicated to your problem domain. I created the following DSL to help make the job of creating rules by a non-developer easier. You will see the result of using a DSL in the next section. Here is the DSL for the speed camera project.

[condition][location]There is a Location=$location : Location()
[condition][location]- near a school=nearSchool == "yes"
[condition][location]- not near a school=nearSchool == "no"
[condition][location]- near a roadwork site=roadworkSite == "yes"
[condition][location]- not near a roadwork site=roadworkSite == "no"
[condition][location]- has had  {number} number of speeding Indents=numberOfSpeedingIncidents == {number}
[condition][location]- with a placement score=score : placementScore
[condition][location]- has less than or equal to {number} number of tickets=numberOfTickets <= {number}
[condition][policy]It is School Holidays=exists SchoolHolidays()
[consequence][location]Remove Lcation=retract location
[consequence][location]Reduce Score by {amount} to Location= $location.setPlacementScore($location.getPlacementScore() - 10);

Creating a Guided Rule using DSL

The decision table always takes into account whether the location under evaluation is near a school. However, during school holidays, whether near a school or not does not make any difference as there are no students coming to school. Hence, I have to create a rule that should be applied during school holidays. If it is school holidays, reduce the placement score by a certain amount.

School Holidays Adjustment Rule
School Holidays Adjustment Rule

Creating Test Scenarios

Many test scenarios are required to thoroughly test out all rows in the decision table. I only created a couple as examples.

Test Scenario  #1
Test Scenario #1
Test Scenario #2
Test Scenario #2

Building and Deploying the Rule Project on RTDS

Use Build-> Build and Deploy from the Project Editor and then use Deploy-> Rule Deployment to deploy the application on RTDS.

The diagram that follows depicts the registration of a Decision Server.

Server Registration
Server Registration

Once a Decision server has been registered, we can proceed to create a container to deploy the rule project as below:

Create a Container
Create a Container

Executing Your rules

Once deployed, we can use SoapUI to try out the rules. We configure Soap to point to the URL to send a REST request. here is a screen shot.

SoapUI Screeshot
SoapUI Screeshot

The first request sends to RTDS 3 locations for placement scoring. The request and response are shown below:

<batch-execution lookup="defaultStatelessKieSession">
   <insert out-identifier="location1">
      <au.gov.police.Location>
         <name>Airport Link tunnel in Brisbane</name>
         <numberofSpeedingIncidents>5</numberofSpeedingIncidents>
         <nearSchool>no</nearSchool>
         <roadworkSite>no</roadworkSite>
      </au.gov.police.Location>
   </insert>
   <insert out-identifier="location2">
      <au.gov.police.Location>
         <name>Gold Coast Highway at Labrador</name>
         <numberofSpeedingIncidents>7</numberofSpeedingIncidents>
         <nearSchool>yes</nearSchool>
         <roadworkSite>no</roadworkSite>
      </au.gov.police.Location>
   </insert>
   <insert out-identifier="location3">
      <au.gov.police.Location>
         <name>Warrego Highway at Redwood</name>
         <numberofSpeedingIncidents>17</numberofSpeedingIncidents>
         <nearSchool>no</nearSchool>
         <roadworkSite>yes</roadworkSite>
      </au.gov.police.Location>
   </insert>
   <fire-all-rules/>
</batch-execution>
<response type="SUCCESS" msg="Container speedcamera successfully called.">
  <results><![CDATA[<execution-results>
  <result identifier="location1">
    <au.gov.police.Location>
      <name>Airport Link tunnel in Brisbane</name>
      <numberOfAccidents>0</numberOfAccidents>
      <nearSchool>no</nearSchool>
      <placementScore>5</placementScore>
      <numberofSpeedingIncidents>5</numberofSpeedingIncidents>
      <roadworkSite>no</roadworkSite>
    </au.gov.police.Location>
  </result>
  <result identifier="location2">
    <au.gov.police.Location>
      <name>Gold Coast Highway at Labrador</name>
      <numberOfAccidents>0</numberOfAccidents>
      <nearSchool>yes</nearSchool>
      <placementScore>35</placementScore>
      <numberofSpeedingIncidents>7</numberofSpeedingIncidents>
      <roadworkSite>no</roadworkSite>
    </au.gov.police.Location>
  </result>
  <result identifier="location3">
    <au.gov.police.Location>
      <name>Warrego Highway at Redwood</name>
      <numberOfAccidents>0</numberOfAccidents>
      <nearSchool>no</nearSchool>
      <placementScore>70</placementScore>
      <numberofSpeedingIncidents>17</numberofSpeedingIncidents>
      <roadworkSite>yes</roadworkSite>
    </au.gov.police.Location>
  </result>
  <fact-handle identifier="location1" external-form="0:1:17303633:17303633:1:DEFAULT:NON_TRAIT"/>
  <fact-handle identifier="location2" external-form="0:2:19513646:19513646:2:DEFAULT:NON_TRAIT"/>
  <fact-handle identifier="location3" external-form="0:3:6471721:6471721:3:DEFAULT:NON_TRAIT"/>
</execution-results>]]></results>
</response>

The second request sends to RTDS 3 locations during school holidays for placement scoring. The request and response are shown below:

<batch-execution lookup="defaultStatelessKieSession">
  <insert out-identifier="holiday">
      <au.gov.police.SchoolHolidays>
        <name>Easter</name>
        </au.gov.police.SchoolHolidays>
  </insert>
  <insert out-identifier="location1">
    <au.gov.police.Location>
        <name>Airport Link tunnel in Brisbane</name>
        <numberofSpeedingIncidents>5</numberofSpeedingIncidents>
        <nearSchool>no</nearSchool>
        <roadworkSite>no</roadworkSite>
    </au.gov.police.Location>
  </insert>
  <insert out-identifier="location2">
      <au.gov.police.Location>
        <name>Gold Coast Highway at Labrador</name>
        <numberofSpeedingIncidents>7</numberofSpeedingIncidents>
        <nearSchool>yes</nearSchool>
        <roadworkSite>no</roadworkSite>
    </au.gov.police.Location>
  </insert>
  <insert out-identifier="location3">
    <au.gov.police.Location>
        <name>Warrego Highway at Redwood</name>
        <numberofSpeedingIncidents>17</numberofSpeedingIncidents>
        <nearSchool>no</nearSchool>
        <roadworkSite>yes</roadworkSite>
    </au.gov.police.Location>
  </insert>

  <fire-all-rules />
</batch-execution>
<response type="SUCCESS" msg="Container speedcamera successfully called.">
  <results><![CDATA[<execution-results>
  <result identifier="location1">
    <au.gov.police.Location>
      <name>Airport Link tunnel in Brisbane</name>
      <numberOfAccidents>0</numberOfAccidents>
      <nearSchool>no</nearSchool>
      <placementScore>5</placementScore>
      <numberofSpeedingIncidents>5</numberofSpeedingIncidents>
      <roadworkSite>no</roadworkSite>
    </au.gov.police.Location>
  </result>
  <result identifier="location2">
    <au.gov.police.Location>
      <name>Gold Coast Highway at Labrador</name>
      <numberOfAccidents>0</numberOfAccidents>
      <nearSchool>yes</nearSchool>
      <placementScore>25</placementScore>
      <numberofSpeedingIncidents>7</numberofSpeedingIncidents>
      <roadworkSite>no</roadworkSite>
    </au.gov.police.Location>
  </result>
  <result identifier="location3">
    <au.gov.police.Location>
      <name>Warrego Highway at Redwood</name>
      <numberOfAccidents>0</numberOfAccidents>
      <nearSchool>no</nearSchool>
      <placementScore>70</placementScore>
      <numberofSpeedingIncidents>17</numberofSpeedingIncidents>
      <roadworkSite>yes</roadworkSite>
    </au.gov.police.Location>
  </result>
  <result identifier="holiday">
    <au.gov.police.SchoolHolidays>
      <name>Easter</name>
    </au.gov.police.SchoolHolidays>
  </result>
  <fact-handle identifier="location1" external-form="0:2:973722:973722:2:DEFAULT:NON_TRAIT"/>
  <fact-handle identifier="location2" external-form="0:3:22520772:22520772:3:DEFAULT:NON_TRAIT"/>
  <fact-handle identifier="location3" external-form="0:4:32594343:32594343:4:DEFAULT:NON_TRAIT"/>
  <fact-handle identifier="holiday" external-form="0:1:18465461:18465461:1:DEFAULT:NON_TRAIT"/>
  </execution-results>]]></results>
</response>

You will notice the placement score for location “Gold Coast Highway at Labrador” changes between request 1 and request 2. The difference is caused by the SchoolHolidayAdjustment rule.

To access the RTDS hosted sample application on this server, use the following parameters:

URL http://mrdreambot.ddns.net:8080/kie-server/services/rest/server/containers/speedcamera
User Name rtdsUser
Password rtdsPassword|23

 

Conclusion

As promised, I have shown you how to build and deploy a BPMS 6.1 rule project (application) without writing even a single line of Java code. XML is used in the REST requests but XML is not the only option. RTDS also supports JSON and JAXB. The rule project has been deployed on this server. This means that you can point your SoapUI to this server as described in the previous section to try out the RTDS yourself. Enjoy!