Evaluate a Decision Table with Lookup
Updated: Aug 24, 2023
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
- A
DefaultExpressionContextis defined as the input of the decision tree where we set properties such asPrice,Currency Code. - BasicLookup is created to specify additional information about Currency. A new field
Currency Nameis added with five rows. - Then, a
DecisionTableis initialized andcurrencyLookupspecified in the previous step is applied. - Rules are defined in DecisionTableRule objects.
- 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. addOutcome()method is invoked to attach an outcome with a rule.- The
inputis then evaluated and stored in aDecisionTableResultinstance. - 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
}
