Execute an Action in a Decision Tree
This example conditionally executes Java methods based on the outcome of executing a decision tree. It provides a flexible and programmable framework to define conditions and corresponding Java functions to be executed when those conditions are met in the decision tree.
For a detailed explanation of creating decision trees, see Evaluate a Decision Tree example where we also associate outcomes with certain conditions.
Real-life use cases for this library can be found in various domains where decision-making based on conditional logic is required. For example, in financial applications, the library can be used to automate loan approval processes. The decision tree 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.decisiontree;
import com.northconcepts.datapipeline.core.Functions;
import com.northconcepts.datapipeline.core.Record;
import com.northconcepts.datapipeline.foundations.decisiontree.DecisionTree;
import com.northconcepts.datapipeline.foundations.decisiontree.DecisionTreeNode;
import com.northconcepts.datapipeline.foundations.decisiontree.DecisionTreeResult;
import com.northconcepts.datapipeline.internal.expression.DefaultExpressionContext;
public class ExecuteAnActionInADecisionTree {
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.decisiontree.ExecuteAnActionInADecisionTree.action2");
DefaultExpressionContext input = new DefaultExpressionContext();
input.setValue("Age", 49);
input.setValue("houseOwned", true);
input.setValue("Income", 1000.0);
DecisionTree tree = new DecisionTree().setRootNode(new DecisionTreeNode()
.addNode(new DecisionTreeNode("Age >= 40")
.addNode(new DecisionTreeNode("houseOwned == true").addOutcome("Eligible", "true")
.addOutcome("Action1", "com.northconcepts.datapipeline.foundations.examples.decisiontree.ExecuteAnActionInADecisionTree.action1()")
.addOutcome("Action2", "action2(Age, Income)"))
.addNode(new DecisionTreeNode("houseOwned == false")
.addNode(new DecisionTreeNode("Income >= 2000")
.addOutcome("Eligible", "true")
.addOutcome("Action1", "com.northconcepts.datapipeline.foundations.examples.decisiontree.ExecuteAnActionInADecisionTree.action1()")
.addOutcome("Action2", "action2(Age, Income)"))
.addNode(new DecisionTreeNode("Income < 2000")
.addOutcome("Eligible", "false")
.addOutcome("Action1", "com.northconcepts.datapipeline.foundations.examples.decisiontree.ExecuteAnActionInADecisionTree.action1()")
.addOutcome("Action2", "action2(Age, Income)"))))
.addNode(new DecisionTreeNode("Age < 40")
.addNode(new DecisionTreeNode("Income >= 3000")
.addOutcome("Eligible", "true")
.addOutcome("Action1", "com.northconcepts.datapipeline.foundations.examples.decisiontree.ExecuteAnActionInADecisionTree.action1()")
.addOutcome("Action2", "action2(Age, Income)"))
.addNode(new DecisionTreeNode("Income < 3000")
.addOutcome("Eligible", "false")
.addOutcome("Action1", "com.northconcepts.datapipeline.foundations.examples.decisiontree.ExecuteAnActionInADecisionTree.action1()")
.addOutcome("Action2", "action2(Age, Income)"))));
DecisionTreeResult result = tree.evaluate(input);
Record outcome = result.getOutcome();
System.out.println("outcome = " + outcome);
}
}
Code Walkthrough
- A
DefaultExpressionContextis defined as the input of the decision tree where we set properties such asAge,houseOwnedandIncome. - Then, a
DecisionTreeis initialized with a root node and a variety of child nodes having conditions on propertiesAge,houseOwnedandIncome. - In order to attach an outcome with a node,
addOutcome()method is invoked. addOutcome()can also execute Java functions as actions; therefore some outcomes will invokeaction1()andaction2()methods respectively.- The
inputis then evaluated and stored in aDecisionTreeResultinstance. - 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
}
