// applies a logic rule to a single fact sclass LogicRuleApplier { // essential BasicLogicRule rule; O fact; // where does your output go swappable void addRewrittenRHS(O rhs, O trail) { print("Derived: " + rhs); } // optional new DeepZipTools tools; bool verbose, printNonMatches; run { // now we match the fact to our condition Map map = tools.deepZip(rule.lhs, fact); // Discard unless all keys are variables if (map != null && !all(keys(map), s -> tools.isVar(s))) map = null; if (map == null) ret with if (printNonMatches) print("Non-match: " + quote(rule.lhs) + " / " + quote(fact)); // Now we have a proper mapping with the keys being variables! if (verbose) print("Match: " + quote(rule.lhs) + " / " + quote(fact)); // Apply mapping to right hand side O rhs_replaced = tools.replaceVars(rule.rhs, map); if (verbose) print(+rhs_replaced); Map trail = litorderedmap(op := "applyLogicRuleToFact", +rule, +fact, +rhs_replaced); addRewrittenRHS(rhs_replaced, trail); } }