Evaluate a Decision Tree with Lookup

This example combines the evaluation of a decision tree with lookup functionality, offering you a powerful tool for data processing and decision-making. By incorporating lookup capabilities, you can retrieve additional information or make data-driven decisions based on external data sources, enhancing the decision-making process within the evaluation of the decision tree.

Decision Tree is a hierarchical structure that represents a series of conditions and outcomes. It is used to evaluate input data by following the branches of the tree, checking conditions at each node, and ultimately reaching a final outcome based on the satisfied conditions.

 

Java Code Listing

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

import com.northconcepts.datapipeline.core.FieldList;
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;
import com.northconcepts.datapipeline.transform.lookup.BasicLookup;
import com.northconcepts.datapipeline.transform.lookup.Lookup;

public class EvaluateADecisionTreeWithLookup {

    public static void main(String[] args) {
        DefaultExpressionContext input = new DefaultExpressionContext();
        input.setValue("Price", 159);
        input.setValue("Currency Code", "cad");

        Lookup currencyLookup = new BasicLookup(new FieldList("Currency Name"))
            .add("CAD", "Canadian Dollars")
            .add("USD", "American Dollars")
            .add("EUR", "Euros")
            .add("GBP", "British Pounds")
            .add("MXN", "Mexican Pesos");

        DecisionTree tree = new DecisionTree()

            .setValue("currencyLookup", currencyLookup)
            
            .setRootNode(new DecisionTreeNode()
                
                .addNode(new DecisionTreeNode("Price == null || Price < 20")
                    .addOutcome("Shipping", "0.00")
                    .addOutcome("Currency", "lookup(0, currencyLookup, toUpperCase(${Currency Code}))"))
    
                .addNode(new DecisionTreeNode("Price  < 50")
                    .addOutcome("Shipping", "5.00")
                    .addOutcome("Currency", "lookup(0, currencyLookup, toUpperCase(${Currency Code}))"))
    
                .addNode(new DecisionTreeNode("Price  < 100")
                    .addOutcome("Shipping", "7.00")
                    .addOutcome("Currency", "lookup(0, currencyLookup, toUpperCase(${Currency Code}))"))
    
                .addNode(new DecisionTreeNode("Price  >= 100")
                    .addOutcome("Shipping", "${Price} * 0.10")
                    .addOutcome("Currency", "lookup(0, currencyLookup, toUpperCase(${Currency Code}))")));

        DecisionTreeResult result = tree.evaluate(input);
        Record outcome = result.getOutcome();

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

 

Code Walkthrough

  1. A DefaultExpressionContext is defined as the input of the decision tree where we set properties such as Price, Currency Code.
  2. BasicLookup is created to specify additional information about Currency. A new field Currency Name is added with five rows.
  3. Then, a DecisionTree is initialized and currencyLookup specified in the previous step is applied.
  4. A root node and a variety of child nodes having conditions on properties are then added.
  5. In order to attach an outcome with a node, addOutcome() method is invoked.
  6. The input is then evaluated and stored in a DecisionTreeResult instance.
  7. Finally, getOutcome() is invoked to display the results of the evaluation on the console.

 

Console Output

outcome = Record (MODIFIED) {
    0:[Shipping]:DOUBLE=[15.9]:Double
    1:[Currency]:STRING=[Canadian Dollars]:String
}
Mobile Analytics