Blog  Post

Blog Post

The Expression Language: ctx.price * ctx.qty, Anywhere a Value Goes

Jul 18, 2026   

Sometimes the value you need isn’t a field or a constant — it’s a small calculation. Line total is price times quantity. A fallback is “the customer’s region, or US if we don’t have one.” AI Rule Engine now has a built-in expression language for exactly these, and it shows up everywhere a value can go.

Two places you’ll use it

Wherever a value input appears, Expression is now one of the sources you can pick — alongside a context key, a variable, or a constant. That covers condition compare values, derived fact values, Add to Context entries, and action inputs. Anywhere the engine reads a value, it can now compute one.

Expressions also work on the left side of a condition. A clause used to test a single context key; now you can flip it to Expression and test the result of a calculation. “Is the line total over the limit?” becomes one clause — no helper key required.

Reading your data

Read a context value with ctx.key (the canonical form) or context.key. For keys that aren’t simple identifiers — anything with spaces or punctuation — use the bracket form: ctx.[customer region]. Variables are written $name. There are no bare identifiers: every value is explicitly ctx., context., or $, so an expression is never ambiguous about what it’s reading. If that convention feels familiar, it’s the same one the decision-table cell shorthand uses.

A condition editor in expression mode evaluating ctx.debt divided by ctx.income against a threshold.

Operators

The operator set is what you’d expect:

  • Arithmetic + - * / % — and + also concatenates strings.
  • Comparisons == != < <= > >=.
  • Logic and, or, not — with the aliases &&, ||, !.
  • Parentheses for grouping, and a ternary ?: for inline choices.

Numeric comparisons follow the same coercion rules the engine already uses in conditions, so an expression compares values the way the rest of your ruleset does.

The function library

There are 32 built-in functions, grouped by what they work on:

Group Functions
String len, lower, upper, trim, contains, startsWith, endsWith, replace, substring, concat
Math abs, round, floor, ceil, min, max
Conversion & null-safety number, string, bool, date, coalesce
Date dateAdd, dateDiff, year, month, day
Array count, sum, avg, first, last, join

coalesce is the escape hatch for missing data: it returns the first non-null argument, so coalesce(ctx.[customer region], "US") reads the region when it’s there and falls back cleanly when it isn’t. That matters because null propagates through operators — an arithmetic or comparison on a missing value yields null rather than a wrong answer, and coalesce is how you decide what to do about it. Results are typed — strings, numbers, booleans, dates, and array element types are all preserved — so a computed value behaves exactly like a value of that type everywhere downstream.

Validated as you type

Expressions are checked live while you write them. A parse error is reported inline with the character position where it went wrong, so you’re not guessing which parenthesis is unbalanced. The same check runs at save time, so a broken expression can’t slip into a saved ruleset.

The editor nudges you along, too — the hint reads “Read context with ctx.key, variables with $name,” and the placeholder shows a worked example, e.g. ctx.price * ctx.qty.

A few worked examples

ctx.price * ctx.qty

coalesce(ctx.[customer region], "US")

len(ctx.name) > 30 and $ENV == "prod"

ctx.total > 100 ? "bulk" : "standard"

The first computes a line total. The second supplies a default. The third combines a string function, a comparison, and a variable into one boolean condition. The fourth uses the ternary to produce a label inline.

One deliberate omission: there is no now() and no random(). The expression language is deterministic on purpose — the same inputs always produce the same result. That’s what makes runs repeatable, re-fire behavior stable under inference, and what-if comparisons trustworthy. It’s a design choice worth its own post.

Pick Expression the next time you’re about to reach for a helper key or a throwaway action — and let the value compute itself.

Visit RuleEngine.ai to try it.

The AI Rule Engine Team