Skip to content

Binding Matching

When a rule executes, its parameters (conditions, actions) need to be resolved from the available bindings. The BindingMatchingStrategy determines how rule parameters are matched to bindings in the container.

Each rule parameter declares a name and type. When the rule runs, the matching strategy looks through the available bindings to find the best match for each parameter:

// Rule with parameters "age" (Integer) and "name" (String)
Rule rule = Rule.builder()
.given(condition((Integer age, String name)
-> age > 18 && name != null))
.build();
// Bindings with matching names and compatible types
rule.run(age -> 25, name -> "Alice");
// "age" matches to age parameter, "name" matches to name parameter

The BindingMatchingStrategy interface defines how bindings are resolved for rule parameters:

public interface BindingMatchingStrategy {
<T> List<BindingMatch<T>> match(Bindings bindings, String name,
Type type, boolean containsGenericInfo);
}

A strategy returns every binding that fits the parameter. No match, one match and several matches are all possible, and each is handled as described below.

This strategy matches bindings by name. The parameter name in your condition or action must match the binding name exactly:

// Parameter named "total" will match binding named "total"
Rule rule = Rule.builder()
.given(condition((BigDecimal total) -> total.compareTo(BigDecimal.ZERO) > 0))
.build();
rule.run(total -> BigDecimal.valueOf(99.99)); // Matched by name "total"

When a name match is not found, the strategy can fall back to matching by type. If there is exactly one binding of the required type, it will be used:

// If only one BigDecimal binding exists, it matches regardless of name
Bindings bindings = Bindings.builder().standard();
bindings.bind("orderTotal", BigDecimal.valueOf(150));
// Parameter "total" can still match "orderTotal" by type

If more than one binding has the required type, the match is ambiguous and the run fails with a BindingException (wrapped in an UnrulyException), unless one of the candidates is marked as primary:

bindings.bind(Binding.builder()
.with("orderTotal")
.type(BigDecimal.class)
.value(BigDecimal.valueOf(150))
.primary(true) // wins when several bindings match
.build());
bindings.bind("tax", BigDecimal.valueOf(15));
// Parameter "total" matches "orderTotal"

The default strategy is a combination of the above, firstly it matches bindings by name and if there are no matches it will attempt to match by Type

You can set the matching strategy for a whole run on the RuleContext, or for a single parameter with @Param:

// For everything that runs in this context: match by name only
RuleContext ctx = RuleContext.builder()
.with(bindings)
.matchUsing(BindingMatchingStrategy.builder().matchByName())
.build();
// For one parameter of a rule class
@Given
public boolean isValid(@Param(matchUsing = MatchByNameMatchingStrategy.class) BigDecimal total) {
return total != null && total.compareTo(BigDecimal.ZERO) > 0;
}

BindingMatchingStrategy.builder() also offers matchByType(), matchByNameAndType(), matchByNameThenByType() (the default) and matchByNameAndTypeThenByJustType().

A parameter that matches no binding does not stop the rule. It receives a default, and the rule runs:

Parameter Receives
An object type (BigDecimal, String, Binding<T>, …) null
A primitive (int, boolean, …) 0, false
Optional<T> Optional.empty()
@Param(defaultValue = "5") int count 5; the text is converted to the parameter’s type
@Given
public boolean isValid(Optional<BigDecimal> total,
@Param(defaultValue = "5") int threshold) {
return total.isPresent() && total.get().intValue() > threshold;
}