WASM Generation

Generate WASM modules for constraint predicates

This section covers generating WebAssembly modules for constraint predicates.

In This Section

  • Expressions - Build predicate expressions with the Expression API
  • Module Builder - Generate WASM modules with WasmModuleBuilder

Overview

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()?;

Why WASM?

WebAssembly provides:

  • Portability: Same predicates work across platforms
  • Safety: Sandboxed execution
  • Performance: Near-native speed
  • No JNI: Clean HTTP interface instead of complex native bindings

Components

ComponentPurpose
ExpressionAST for predicate logic
ExprFluent builder for expressions
PredicateDefinitionNamed predicate with arity
WasmModuleBuilderCompiles expressions to WASM
HostFunctionRegistryRegisters host function imports

Expressions

Build predicate expressions with the Expression API

Module Builder

Generate WASM modules with WasmModuleBuilder