Execute an Action in a Decision Table

This example shows how to conditionally execute Java methods based on the outcome of executing a decision table. It provides a framework to define rules and corresponding Java functions to be executed when those rules are met in the decision table.

In financial applications, this example can be used to automate loan approval processes. The decision table can incorporate various factors such as credit score, income, and employment status, and based on these conditions, specific Java functions can be executed to either approve or reject the loan application.

 

Java Code Listing

package com.northconcepts.datapipeline.foundations.examples.decisiontable;

import com.northconcepts.datapipeline.core.Functions;
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 ExecuteAnActionInADecisionTable {
	
	public static void action1() {
		System.out.println("Action 1");
	}

	public static double action2(int age, double income) {
		System.out.println("Age: " + age + ";  Income: " + income);
		return age * income;
	}

    public static void main(String[] args) {
    	Functions.add("action2", "com.northconcepts.datapipeline.foundations.examples.decisiontable.ExecuteAnActionInADecisionTable.action2");
    	
        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")
                        .addOutcome("Action1", "com.northconcepts.datapipeline.foundations.examples.decisiontable.ExecuteAnActionInADecisionTable.action1()")
                        .addOutcome("Action2", "action2(Age, Income)")
                        )
                .addRule(new DecisionTableRule()
                        .addCondition("Age", "? >= 40")
                        .addCondition("House Owned", "? == false")
                        .addCondition("Income", "? >= 2000")
                        .addOutcome("Eligible", "true")
                        .addOutcome("Action1", "com.northconcepts.datapipeline.foundations.examples.decisiontable.ExecuteAnActionInADecisionTable.action1()")
                        .addOutcome("Action2", "action2(Age, Income)")
                        )
                .addRule(new DecisionTableRule()
                        .addCondition("Age", "? >= 40")
                        .addCondition("House Owned", "? == false")
                        .addCondition("Income", "? < 2000") 
                        .addOutcome("Eligible", "false")
                        .addOutcome("Action1", "com.northconcepts.datapipeline.foundations.examples.decisiontable.ExecuteAnActionInADecisionTable.action1()")
                        .addOutcome("Action2", "action2(Age, Income)")
                        )
                .addRule(new DecisionTableRule()
                        .addCondition("Age", "? < 40")
                        .addCondition("Income", "? >= 3000")
                        .addOutcome("Eligible", "true")
                        .addOutcome("Action1", "com.northconcepts.datapipeline.foundations.examples.decisiontable.ExecuteAnActionInADecisionTable.action1()")
                        .addOutcome("Action2", "action2(Age, Income)")
                        )
                .addRule(new DecisionTableRule()
                        .addCondition("Age", "? < 40")
                        .addCondition("Income", "? < 3000")
                        .addOutcome("Eligible", "false")
                        .addOutcome("Action1", "com.northconcepts.datapipeline.foundations.examples.decisiontable.ExecuteAnActionInADecisionTable.action1()")
                        .addOutcome("Action2", "action2(Age, Income)")
                        )
                ;
        
        DecisionTableResult result = table.evaluate(input);
        Record outcome = result.getOutcome();

        System.out.println("outcome = " + outcome);
    }

}

 

Code Walkthrough

  1. Two static methods action1() and action2() are declared in the class.
  2. Functions is used to hold functions as variables. In the given example, the method signature/location is saved as "action2". 
  3. DefaultExpressionContext is defined as the input of the decision table where we set properties such as Age, House Owned and Income.
  4. Then, a DecisionTable is initialized with rules defined in DecisionTableRule objects.
  5. 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. 
  6. addOutcome() method is invoked to attach an outcome with a node.
  7. addOutcome() can also execute Java functions as actions; therefore some outcomes will invoke action1() and action2() methods respectively.
  8. The input is then evaluated and stored in a DecisionTableResult instance.
  9. Finally, getOutcome() can be invoked to display the results of the evaluation on the console.

 

Console Output

Action 1
Age: 49;  Income: 1000.0
outcome = Record (MODIFIED) {
    0:[Eligible]:BOOLEAN=[true]:Boolean
    1:[Action1]:UNDEFINED=[null]
    2:[Action2]:DOUBLE=[49000.0]:Double
}

 

Mobile Analytics