Save and Load DecisionTable to XML
Updated: Jul 7, 2023
This example shows how you can use Data Pipeline to save decision tables, represented as tabular structures, to XML format. It allows users to store decision tables in a structured and portable format, capturing the conditions and outcomes for each rule of the table. Similarly, the library supports loading decision tables from XML files, enabling the reuse and sharing of decision table models across different systems or applications.
Java Code Listing
package com.northconcepts.datapipeline.foundations.examples.decisiontable; import java.io.FileInputStream; import java.io.FileWriter; import java.util.ArrayList; import java.util.List; import com.northconcepts.datapipeline.foundations.decisiontable.DecisionTable; import com.northconcepts.datapipeline.foundations.decisiontable.DecisionTableOutcome; import com.northconcepts.datapipeline.foundations.decisiontable.DecisionTableRule; import com.northconcepts.datapipeline.foundations.expression.CalculatedField; public class SaveAndLoadDecisionTableToXML { public static void main(String[] args) throws Throwable { Listoutcomes = new ArrayList<>(); outcomes.add(new DecisionTableOutcome("Total", "${Variant Price} + Shipping")); outcomes.add(new DecisionTableOutcome("Product Type", "Type")); DecisionTable table = new DecisionTable() .addField(new CalculatedField("Variant Price", "toBigDecimal(${Variant Price})")) .addRule(new DecisionTableRule() .addCondition("Variant Price", "? == null || ? < 20") .addOutcome("Shipping", "0.00") .addOutcome(outcomes) ) .addRule(new DecisionTableRule() .addCondition("Variant Price", "? < 50") .addOutcome("Shipping", "5.00") .addOutcome(outcomes) ) .addRule(new DecisionTableRule() .addCondition("Variant Price", "? < 100") .addOutcome("Shipping", "7.00") .addOutcome(outcomes) ) .addRule(new DecisionTableRule() .addCondition("Variant Price", "? >= 100") .addOutcome(new DecisionTableOutcome("Shipping", "${Variant Price} * 0.10")) .addOutcome(outcomes) ) ; //Save this DecisionTable to XML File. table.toXml(new FileWriter("example/data/output/decision-tree.xml"), true); DecisionTable decisionTable = new DecisionTable() .fromXml(new FileInputStream("example/data/output/decision-tree.xml")); System.out.println("DecisionTable loaded from XML:-\n" + decisionTable); } }
Code Walkthrough
- The outcomes of a decision tree are saved as a separate list of
DecisionTableOutcome
instances. DecisionTable
instance is created.- A calculated field is added to hold "Variant Price" variable which is parsed into BigDecimal data type.
- 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 decision table is saved/exported to a new output file
decision-tree.xml
viatoXml
method of DecisionTable and FileWriter class. - The decision table is then loaded from the file created in the previous step with the FileInputStream object.
- Data is printed in the console with
System.out.println()
method.
Console Output
DecisionTable loaded from XML:- { "fields" : [ { "expressionSource" : "toBigDecimal(${Variant Price})", "includeInOutcome" : false, "variable" : "Variant Price" } ], "rootExpressionContext" : { "parent" : { } }, "rownum" : 0, "rules" : [ { "allowNoConditions" : false, "allowNoOutcomes" : false, "conditions" : [ { "expressionSource" : ["${Variant Price} == null || ${Variant Price} < 20", "variable" : "Variant Price" } ], "outcomes" : [ { "expressionSource" : "0.00", "variable" : "Shipping" }, { "expressionSource" : "${Variant Price} + Shipping", "variable" : "Total" }, { "expressionSource" : "Type", "variable" : "Product Type" } ] }, { "allowNoConditions" : false, "allowNoOutcomes" : false, "conditions" : [ { "expressionSource" : "${Variant Price} < 50", "variable" : "Variant Price" } ], "outcomes" : [ { "expressionSource" : "5.00", "variable" : "Shipping" }, { "expressionSource" : "${Variant Price} + Shipping", "variable" : "Total" }, { "expressionSource" : "Type", "variable" : "Product Type" } ] }, { "allowNoConditions" : false, "allowNoOutcomes" : false, "conditions" : [ { "expressionSource" : "${Variant Price} < 100", "variable" : "Variant Price" } ], "outcomes" : [ { "expressionSource" : "7.00", "variable" : "Shipping" }, { "expressionSource" : "${Variant Price} + Shipping", "variable" : "Total" }, { "expressionSource" : "Type", "variable" : "Product Type" } ] }, { "allowNoConditions" : false, "allowNoOutcomes" : false, "conditions" : [ { "expressionSource" : "${Variant Price} >= 100", "variable" : "Variant Price" } ], "outcomes" : [ { "expressionSource" : "${Variant Price} * 0.10", "variable" : "Shipping" }, { "expressionSource" : "${Variant Price} + Shipping", "variable" : "Total" }, { "expressionSource" : "Type", "variable" : "Product Type" } ] } ] }