Evaluate a Decision Table
Updated: Jul 7, 2023
This example evaluates decision tables, which are structured representations of rules and conditions that determine corresponding outcomes. By processing decision tables, users can automate decision-making processes based on complex sets of conditions and streamline the evaluation of multiple rules to arrive at the appropriate outcome.
Decision tables can be utilized to automate workflows by defining conditions that specify the next steps or actions to be taken. By evaluating decision tables within workflow systems, users can streamline and automate processes, improving efficiency and consistency in decision-making.
Java Code Listing
package com.northconcepts.datapipeline.foundations.examples.decisiontable; import com.northconcepts.datapipeline.core.Record; import com.northconcepts.datapipeline.foundations.decisiontable.DecisionTable; import com.northconcepts.datapipeline.foundations.decisiontable.DecisionTableResult; import com.northconcepts.datapipeline.foundations.decisiontable.DecisionTableRule; import com.northconcepts.datapipeline.internal.expression.DefaultExpressionContext; public class EvaluateADecisionTable { public static void main(String[] args) { DefaultExpressionContext input = new DefaultExpressionContext(); input.setValue("Age", 49); input.setValue("House Owned", true); input.setValue("Income", 1000.0); DecisionTable table = new DecisionTable() .addRule(new DecisionTableRule() .addCondition("Age", "? >= 40") .addCondition("House Owned", "? == true") .addOutcome("Eligible", "true") ) .addRule(new DecisionTableRule() .addCondition("Age", "? >= 40") .addCondition("House Owned", "? == false") .addCondition("Income", "? >= 2000") .addOutcome("Eligible", "true") ) .addRule(new DecisionTableRule() .addCondition("Age", "? >= 40") .addCondition("House Owned", "? == false") .addCondition("Income", "? < 2000") .addOutcome("Eligible", "false") ) .addRule(new DecisionTableRule() .addCondition("Age", "? < 40") .addCondition("Income", "? >= 3000") .addOutcome("Eligible", "true") ) .addRule(new DecisionTableRule() .addCondition("Age", "? < 40") .addCondition("Income", "? < 3000") .addOutcome("Eligible", "false") ) ; DecisionTableResult result = table.evaluate(input); Record outcome = result.getOutcome(); System.out.println("outcome = " + outcome); } }
Code Walkthrough
- A
DefaultExpressionContext
is defined as the input of the decision table where we set properties such asAge
,House Owned
andIncome
. - Then, a
DecisionTable
is initialized with rules defined in DecisionTableRule objects. - In order to add a condition to a rule,
addCondition()
method is called. As in the example, more than one condition can be applied to a single rule. addOutcome()
method is invoked to attach an outcome with a rule.- The
input
is then evaluated and stored in aDecisionTableResult
instance. - Finally,
getOutcome()
is invoked to display the results of the evaluation on the console.
Console Output
outcome = Record (MODIFIED) {
0:[Eligible]:BOOLEAN=[true]:Boolean
}