Save and Load DecisionTable to XML

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 {
        List outcomes = 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

  1. The outcomes of a decision tree are saved as a separate list of DecisionTableOutcome instances.
  2. DecisionTable instance is created.
  3. A calculated field is added to hold "Variant Price" variable which is parsed into BigDecimal data type.
  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 decision table is saved/exported to a new output file decision-tree.xml via toXml method of DecisionTable and FileWriter class.
  7. The decision table is then loaded from the file created in the previous step with the FileInputStream object.
  8. 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"
    } ]
  } ]
}
Mobile Analytics