Expressions
Build predicate expressions with the Expression API
This section covers generating WebAssembly modules for constraint predicates.
Constraint predicates (filter conditions, joiners, weighers) are compiled to WebAssembly. The WASM module is sent to the solver service along with your domain model and constraints.
use solverforge_core::wasm::{
Expr, FieldAccessExt, WasmModuleBuilder, PredicateDefinition,
HostFunctionRegistry,
};
// Build predicate: employee assigned but missing required skill
let predicate = {
let shift = Expr::param(0);
let employee = shift.clone().get("Shift", "employee");
Expr::and(
Expr::is_not_null(employee.clone()),
Expr::not(Expr::list_contains(
employee.get("Employee", "skills"),
shift.get("Shift", "requiredSkill"),
))
)
};
// Build WASM module
let wasm = WasmModuleBuilder::new()
.with_domain_model(model)
.with_host_functions(HostFunctionRegistry::with_standard_functions())
.add_predicate(PredicateDefinition::from_expression("skillMismatch", 1, predicate))
.build()?;
WebAssembly provides:
| Component | Purpose |
|---|---|
Expression | AST for predicate logic |
Expr | Fluent builder for expressions |
PredicateDefinition | Named predicate with arity |
WasmModuleBuilder | Compiles expressions to WASM |
HostFunctionRegistry | Registers host function imports |
Build predicate expressions with the Expression API
Generate WASM modules with WasmModuleBuilder
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.