Creating Projects

This guide walks through creating a new DTRules project. We'll build a simple discount calculation system to demonstrate the process.

Project Setup

1. Copy the Template

Start with the TestProject template:

cp -r sampleprojects/TestProject sampleprojects/MyProject
cd sampleprojects/MyProject

2. Update Configuration

Edit DTRules.xml with your project details:

<DTRules>
  <compiler>EL</compiler>

  <RuleSet name="MyProject" source="file">
    <RuleSetFilePath>/xml</RuleSetFilePath>
    <WorkingDirectory>/temp</WorkingDirectory>

    <DTExcelFolder>/DecisionTables/</DTExcelFolder>
    <EDDExcelFolder>/edd/</EDDExcelFolder>

    <Entities        name="MyProject_edd.xml" />
    <Decisiontables  name="MyProject_dt.xml"  />
    <Map             name="MyProject_map.xml" />
  </RuleSet>
</DTRules>

3. Update pom.xml

<groupId>com.mycompany</groupId>
<artifactId>myproject</artifactId>
<version>1.0-SNAPSHOT</version>
<name>MyProject</name>

Define Your Data Model

Create the EDD

Create edd/MyProject_edd.xls with your entities:

Entity Attribute Type Subtype Default Comment
Order Customer order
customer Customer Who placed the order
items array LineItem Order line items
subtotal decimal 0 Pre-discount total
discount decimal 0 Applied discount
total decimal 0 Final total
Customer Customer info
name string
vip boolean false VIP status
member_since date Membership date
LineItem Order line
product string
quantity integer 1
unit_price decimal 0
line_total decimal 0
Context Global config
today date Current date
vip_discount decimal 0.15 VIP discount rate
bulk_threshold decimal 100 Bulk order threshold
bulk_discount decimal 0.10 Bulk discount rate

Write Decision Tables

Create the Decision Tables

Create DecisionTables/MyProject_dt.xls:

Table: Main

Name: Main
Type: ALL
Description: Main entry point for order processing

Actions:
  - perform Calculate_Line_Totals
  - perform Calculate_Subtotal
  - perform Apply_Discounts
  - perform Calculate_Total

Table: Calculate_Line_Totals

Name: Calculate_Line_Totals
Type: ALL
Description: Calculate each line item total

Actions:
  for each item in order.items do
    set item.line_total = item.quantity * item.unit_price
  done

Table: Calculate_Subtotal

Name: Calculate_Subtotal
Type: ALL
Description: Sum line totals to get subtotal

Actions:
  set order.subtotal = 0
  for each item in order.items do
    set order.subtotal = order.subtotal + item.line_total
  done

Table: Apply_Discounts

Name: Apply_Discounts
Type: FIRST
Description: Apply the best applicable discount

Conditions:
  1. customer.vip = true
  2. order.subtotal >= bulk_threshold

| Condition    | R1 | R2 | R3 | R4 |
|--------------|----|----|----|----|
| VIP          | Y  | Y  | N  | N  |
| Bulk Order   | Y  | N  | Y  | N  |
|--------------|----|----|----|----|
| VIP Discount | X  | X  | -  | -  |
| Bulk Discount| -  | -  | X  | -  |
| No Discount  | -  | -  | -  | X  |

Actions:
  - set order.discount = order.subtotal * vip_discount
  - set order.discount = order.subtotal * bulk_discount
  - set order.discount = 0

Table: Calculate_Total

Name: Calculate_Total
Type: ALL
Description: Apply discount to get final total

Actions:
  set order.total = order.subtotal - order.discount

Compile the Rules

Create the Compiler

Create src/main/java/com/mycompany/CompileMyProject.java:

package com.mycompany;

import com.dtrules.compiler.Excel2XML;

public class CompileMyProject {
    public static void main(String[] args) throws Exception {
        String path = System.getProperty("user.dir");
        Excel2XML.compile(path, "DTRules.xml", "MyProject");
        System.out.println("Compilation complete!");
    }
}

Run the Compiler

mvn exec:java -Dexec.mainClass="com.mycompany.CompileMyProject"

This generates XML files in the xml/ directory.

Test the Rules

Create Test Cases

Create test XML files in testfiles/:

<!-- testfiles/test1.xml -->
<data>
  <order>
    <customer>
      <name>John Doe</name>
      <vip>true</vip>
    </customer>
    <items>
      <item>
        <product>Widget</product>
        <quantity>5</quantity>
        <unit_price>10.00</unit_price>
      </item>
    </items>
  </order>
</data>

Run with Go CLI

cd /path/to/DTRules/go
go build -o dtrules ./cmd/dtrules

./dtrules -rules ../sampleprojects/MyProject/xml -entry Main -trace

Run with Java

Create a test runner class and execute with Maven.

Using the Visual UI

Once your project is compiled to XML, you can use the visual UI:

  1. Start the backend: cd go && go run ./cmd/api
  2. Start the UI: cd ui && npm run dev
  3. Click "Open Custom Project"
  4. Enter the path to your xml/ directory
  5. Explore and edit your rules visually

Best Practices

  • Start small - Begin with a minimal model and expand
  • Test early - Write tests as you develop rules
  • Use meaningful names - Entities and tables should be self-documenting
  • Keep tables focused - One decision per table
  • Document business logic - Use descriptions to explain the "why"

Next Steps