Evaluate a Decision Table with Lookup

This example shows how to combine the evaluation of a decision table 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 table.


Java Code Listing

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

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

public class EvaluateADecisionTableWithLookup {

    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")
        DecisionTable table = new DecisionTable()
                .setValue("currencyLookup", currencyLookup)
                .addRule(new DecisionTableRule()
                        .addCondition("Price", "? == null || ? < 20")
                        .addOutcome("Shipping", "0.00")
                        .addOutcome("Currency", "lookup(0, currencyLookup, toUpperCase(${Currency Code}))")
                .addRule(new DecisionTableRule()
                        .addCondition("Price", "? < 50")
                        .addOutcome("Shipping", "5.00")
                        .addOutcome("Currency", "lookup(0, currencyLookup, toUpperCase(${Currency Code}))")
                .addRule(new DecisionTableRule()
                        .addCondition("Price", "? < 100")
                        .addOutcome("Shipping", "7.00")
                        .addOutcome("Currency", "lookup(0, currencyLookup, toUpperCase(${Currency Code}))")
                .addRule(new DecisionTableRule()
                        .addCondition("Price", "? >= 100")
                        .addOutcome("Shipping", "${Price} * 0.10")
                        .addOutcome("Currency", "lookup(0, currencyLookup, toUpperCase(${Currency Code}))")
        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 tree where we set properties such as PriceCurrency Code.
  2. BasicLookup is created to specify additional information about Currency. A new field Currency Name is added with five rows.
  3. Then, a DecisionTable is initialized and currencyLookup specified in the previous step is applied.
  4. Rules are 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 rule.
  7. The input is then evaluated and stored in a DecisionTableResult instance.
  8. Finally, getOutcome() is invoked to display the results of the evaluation on the console.


Console Output

outcome = Record (MODIFIED) {
    1:[Currency]:STRING=[Canadian Dollars]:String
Mobile Analytics