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 calledDecisionTableExample.java:
Understanding the Code
Let’s break down the important parts of this example:Setup and Build the Knowledge Base
- Gets a KIE services instance
- Creates a virtual file system to hold the rules
- Loads the decision table from the classpath
- Builds the knowledge base
- Checks for any build errors
- Creates a container that holds the compiled rules
Testing with Different Scenarios
- Number of items
- Order total
- Delivery days
The Test Method
- Creates a new KIE session (a working memory for rules)
- Creates an Order object with the test parameters
- Inserts the order into the session
- Fires all the rules to calculate the shipping charge
- Collects any Charge objects that were created
- Displays the results
- 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:- Creates a SpreadsheetCompiler instance
- Compiles the decision table directly to DRL (Drools Rule Language)
- Creates a KieHelper to add the compiled DRL
- Gets a KieContainer from the helper
Next Steps
Now that you know how to create and run a decision table, you can:- Modify the existing rules to change the shipping charges
- Add more conditions like customer type or destination region
- Create more complex decision tables for other business scenarios