Variables

In RuLa, variables that bind to case instances β€” such as p is Person β€” are called declarations. This page is about the other kind: value variables, which let you capture any intermediate result and give it a name inside the assignment part of a rule.

What Are Value Variables?

A value variable holds the result of any expression: a number, string, list, dictionary, date, and so on. You define one with :=, giving it a plain name on the left and the expression on the right.

p is Person
---
full_name := CONCAT(p.first_name, ' ', p.last_name)
p.display_name := full_name

full_name is a value variable. It is computed once and then reused on the next line. Without it, you would have to write the CONCAT(...) expression twice.

circle-info

A value variable is local to the rule it is defined in. It is available on all lines that follow its definition, but it does not persist between rules.

Syntax

left_side := right_side

Left side β€” determines what receives the value:

  • A plain name (e.g. net_worth) creates a local variable available on subsequent lines in the same rule.

  • A field path (e.g. p.net_worth) writes the value into that information field on the instance.

Right side β€” any expression that produces a value: a calculation, a string operation, a conditional expression, a list, a dictionary, and so on.


Examples

Storing a calculated value to reuse it

net_worth is computed once and referenced twice β€” once to store the figure and once to derive the risk score from it.


Cleaning up a string before assignment

The raw value of first_name is trimmed of whitespace and uppercased. The cleaned result is stored in a variable and then written to a separate field.


Building a list incrementally

base_docs holds the default document list. full_docs extends it conditionally. The final list is then assigned to the field.


Using a variable across multiple assignments

annual_fee is derived from the contract and then used in an if-then-else to enforce a fee floor before being written to the person.


Intermediate dictionary for a structured assignment

A dictionary is assembled from several fields into address, which is then formatted and stored.


Naming Variables

Variable names:

  • Must start with a letter.

  • Can contain letters, digits, and underscores.

  • Are case-sensitive (NetWorth and net_worth are two different variables).

  • Should be short but descriptive β€” the goal is to make the rule easier to read, not harder.

Last updated

Was this helpful?