Add Calculated Fields to a Decision Table

This example extends the functionality of decision tables by providing the capability to add calculated fields. It allows you to define custom calculations or transformations based on existing fields within the decision tables. By incorporating calculated fields, you can enhance the decision-making capabilities of the table by introducing additional features or derived values.

In e-commerce, this example can be used to evaluate customer behavior and preferences to determine personalized product recommendations. The decision table can consider factors such as past purchase history, browsing patterns, and demographic information to generate tailored recommendations for each customer.

 

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 AddCalculatedFieldsToADecisionTable {

    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()
        		
                .addField("ageThreshold", "40")
                .addField("overAgeThreshold", "Age >= ageThreshold")
                
                .addRule(new DecisionTableRule()
                        .addCondition("overAgeThreshold", "? == true")
                        .addCondition("House Owned", "? == true")
                        .addOutcome("Eligible", "true")
                        )
                .addRule(new DecisionTableRule()
                        .addCondition("overAgeThreshold", "? == true")
                        .addCondition("House Owned", "? == false")
                        .addCondition("Income", "? >= 2000")
                        .addOutcome("Eligible", "true")
                        )
                .addRule(new DecisionTableRule()
                        .addCondition("overAgeThreshold", "? == true")
                        .addCondition("House Owned", "? == false")
                        .addCondition("Income", "? < 2000")
                        .addOutcome("Eligible", "false")
                        )
                .addRule(new DecisionTableRule()
                        .addCondition("overAgeThreshold", "? == false")
                        .addCondition("Income", "? >= 3000")
                        .addOutcome("Eligible", "true")
                        )
                .addRule(new DecisionTableRule()
                        .addCondition("overAgeThreshold", "? == false")
                        .addCondition("Income", "? < 3000")
                        .addOutcome("Eligible", "false")
                        )
                ;
        
        DecisionTableResult result = table.evaluate(input);
        Record outcome = result.getOutcome();

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

}

 

Code Walkthrough

  1. DefaultExpressionContext is defined as the input of the decision table where we set properties such as Age, House Owned and Income.
  2. Then, a DecisionTable is initialized with two calculated fields: ageThreshold and overAgeThreshold.
  3. Rules are defined in DecisionTableRule objects.
  4. 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. 
  5. addOutcome() method is invoked to attach an outcome with a rule.
  6. The input is then evaluated and stored in a DecisionTableResult instance.
  7. Finally, getOutcome() is invoked to display the results of the evaluation on the console.

 

Console Output

outcome = Record (MODIFIED) {
    0:[Eligible]:BOOLEAN=[true]:Boolean
}
Mobile Analytics