Expression Language

The Expression Language (EL) is DTRules' domain-specific language for writing conditions and actions. It's designed to be readable by policy experts while remaining powerful enough for complex business logic.

Design Philosophy

EL prioritizes:

  • Readability - Looks like natural language
  • Validation - Compile-time error checking
  • Expressiveness - Handles complex business logic
  • Determinism - Same inputs always produce same outputs

Basic Syntax

Literals

// Numbers
42          // integer
3.14        // decimal

// Strings
"hello"     // string literal

// Booleans
true
false

// Dates
date("2024-01-15")

Attribute Access

// Simple
age
name
income

// Entity attribute
person.age
application.status

// Nested
application.household.head.name

// Array element
members[0]
household.members[2].name

Operators

Comparison

age = 18          // equals
age != 18         // not equals
age > 18          // greater than
age >= 18         // greater than or equal
age < 18          // less than
age <= 18         // less than or equal

Arithmetic

a + b             // addition
a - b             // subtraction
a * b             // multiplication
a / b             // division
a % b             // modulo (remainder)

Boolean

a and b           // logical AND
a or b            // logical OR
not a             // logical NOT

// Alternative syntax
a & b             // AND
a | b             // OR
!a                // NOT

String

a + b                   // concatenation
length of name          // string length
substring of name from 0 to 5   // substring
uppercase of name       // convert to uppercase
lowercase of name       // convert to lowercase
trim of name            // remove whitespace

Variables and Assignment

// Set attribute value
set age = 25
set person.name = "John"
set eligible = true

// Set with calculation
set total = subtotal * tax_rate
set monthly_income = annual_income / 12

Conditional Logic

If-Then-Else

// Simple if
if age >= 18 then set adult = true

// If-else
if income < threshold then
    set eligible = true
else
    set eligible = false

// Nested
if age >= 65 then
    set category = "senior"
else if age >= 18 then
    set category = "adult"
else
    set category = "minor"

Conditional Expression

// Inline conditional (like ternary operator)
set status = if eligible then "approved" else "denied"

Array Operations

Basic Operations

// Count elements
count of dependents

// Check if empty
is empty dependents
is not empty dependents

// Add element
add person to household.members

// Remove element
remove person from household.members

// Clear array
clear household.members

Iteration

// For each loop
for each member in household.members do
    set member.processed = true
done

// With index
for each member in household.members using index do
    set member.order = index
done

Predicates

// Any (exists)
any member in members satisfies (member.age >= 18)

// All
all members in members satisfy (member.citizen = true)

// None
no member in members satisfies (member.income < 0)

// Count matching
count of members where (member.age >= 18)

Filtering and Mapping

// First matching
first member in members where (member.age >= 18)

// All matching (creates new array)
all members in members where (member.age >= 18)

Date Operations

// Create date
date("2024-01-15")

// Current date
today

// Date arithmetic
birth_date + 18 years
due_date - 30 days

// Date comparison
birth_date < today
application_date >= start_of_period

// Date parts
year of birth_date
month of birth_date
day of birth_date

Decision Table Calls

// Execute another decision table
perform Calculate_Benefits

// Execute for each item
for each dependent in dependents do
    perform Check_Dependent_Eligibility
done

Entity Operations

// Create new entity
new Person

// Create and assign
set applicant = new Person
set applicant.name = "John"
set applicant.age = 30

// Copy entity
set backup = copy of original

// Check type
type of person = "Person"

Null Handling

// Check for null
value is null
value is not null

// Null-safe access (returns null if any part is null)
person?.address?.city

// Default value
value otherwise default_value

Built-in Functions

Math Functions

abs of value          // absolute value
round of value        // round to nearest integer
floor of value        // round down
ceiling of value      // round up
min of (a, b)         // minimum
max of (a, b)         // maximum

String Functions

length of text
uppercase of text
lowercase of text
trim of text
contains text1 text2   // text1 contains text2
starts with text1 text2
ends with text1 text2

Type Conversion

integer of value      // to integer
decimal of value      // to decimal
string of value       // to string
boolean of value      // to boolean

Operator Reference

DTRules includes 179+ operators. Here are the main categories:

Category Examples
Arithmetic +, -, *, /, %, abs, round, floor, ceiling
Comparison =, !=, <, <=, >, >=
Boolean and, or, not, xor
String +, length, substring, uppercase, lowercase, trim, contains
Array count, add, remove, first, any, all, for each
Date date, today, year, month, day, +/- days/months/years
Control if, perform, for each, while
Entity new, copy, type

Best Practices

1. Keep Expressions Simple

Break complex expressions into multiple lines or use intermediate variables. This improves readability and debugging.

2. Use Meaningful Names

Entity and attribute names should be self-documenting. Avoid abbreviations unless they're universally understood.

3. Handle Edge Cases

Check for null values and empty arrays before operations. Use default values where appropriate.

4. Avoid Side Effects in Conditions

Conditions should be pure boolean expressions. Save modifications for actions.

Next Steps