Skip to main content

Running Your Decision Table

This guide shows you how to load and execute a decision table in a Java application.

Creating the Java Application

Let’s create a simple Java application that loads our shipping charges decision table and executes it with test data.

Main Application Class

Create a class called DecisionTableExample.java:

Understanding the Code

Let’s break down the important parts of this example:

Setup and Build the Knowledge Base

This code:
  1. Gets a KIE services instance
  2. Creates a virtual file system to hold the rules
  3. Loads the decision table from the classpath
  4. Builds the knowledge base
  5. Checks for any build errors
  6. Creates a container that holds the compiled rules

Testing with Different Scenarios

This runs four different test cases with varying:
  • Number of items
  • Order total
  • Delivery days

The Test Method

For each test case, this method:
  1. Creates a new KIE session (a working memory for rules)
  2. Creates an Order object with the test parameters
  3. Inserts the order into the session
  4. Fires all the rules to calculate the shipping charge
  5. Collects any Charge objects that were created
  6. Displays the results
  7. Disposes of the session to free resources

Running the Application

You can run this application by executing the main method in your IDE or by building and running the JAR file.

Expected Output

When you run the application, you should see output similar to this:

Alternative Loading Method

For simpler applications, you can also use the SpreadsheetCompiler directly:
This method:
  1. Creates a SpreadsheetCompiler instance
  2. Compiles the decision table directly to DRL (Drools Rule Language)
  3. Creates a KieHelper to add the compiled DRL
  4. Gets a KieContainer from the helper

Next Steps

Now that you know how to create and run a decision table, you can:
  1. Modify the existing rules to change the shipping charges
  2. Add more conditions like customer type or destination region
  3. Create more complex decision tables for other business scenarios
In the next section, we’ll explore the RuleSet definitions in more detail.