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.
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.
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 â 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¶
p is Person
---
net_worth := p.total_assets - p.total_liabilities
p.net_worth := net_worth
p.risk_score := if net_worth < 0 then high_risk else low_risk end
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¶
p is Person
---
base_docs := ['id_copy', 'proof_of_address']
full_docs := if p.nationalities contains us then APPEND(base_docs, 'w8_form') else base_docs end
p.required_documents := full_docs
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¶
c is Contract
p is Person
---
annual_fee := c.aum * 0.0075
p.expected_fee := if annual_fee > 10000 then annual_fee else 10000 end
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¶
p is Person
---
address := {"street": p.street, "city": p.city, "country": p.domicile}
p.formatted_address := PRETTY_PRINT(address)
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 (
NetWorthandnet_worthare two different variables). - Should be short but descriptive â the goal is to make the rule easier to read, not harder.