sclass NLLogicChecker_v2 { S input; new L facts; StringMatcher matcher = new NLStringMatcher_noVars; new L posted; new ThreadLocal checkingRule; sclass Matching { new LinkedHashMap matches; // var -> match new L output; bool verbose; } bool checkRule(IfThen rule, Matching m) { if (rule == null) false; temp tempSetThreadLocal(checkingRule, rule); if (!checkExpression(rule.in, m)) false; print("Rewriting output: " + sfu(rule.out)); m.output.add(apply(rule.out, m)); true; } Exp apply(Exp e, Matching m) { if (e == null) null; if (e cast And) ret And(apply(e.a, m), apply(e.b, m)); if (e cast Func) { S s = nlLogic_text(e.arg); ret Func(e.name, Sentence(javaTok(matcher.apply(s, m.matches)))); } ret e; } bool checkExpression(Exp e, Matching m) { if (e cast And) ret checkExpression(e.a, m) && checkExpression(e.b, m); if (e cast Func) { if (eq(e.name, "input")) { S pat = nlLogic_text(e.arg); bool result = matcher.match(pat, input, m.matches); if (m.verbose) print("matched " + sfu(e) + " with " + quote(input) + " & " + sfu(m.matches) + " => " + result); ret result; } else if (eq(e.name, "fact")) { S pat = nlLogic_text(e.arg); for (S fact : facts) if (matcher.match(pat, fact, m.matches)) true; } } false; } }