!include once #1027630 // Msg replace LogicRule with BasicLogicRule. cmodule NewEngineSpike > DynPrintLog { L msgs; bool verbose = true, printNonMatches = true; transient new Map facts; // facts to trail transient AllOnAllWithOperation rulesAndFacts = new(lambda2 applyLogicRuleToFact); bool step() { ret rulesAndFacts.step(); } void addLogicRule(LogicRule rule) { if (verbose) print("New rule: " + rule); rulesAndFacts.newA(rule); } void addFact(O fact) { addFact(fact, "external"); } void addFact(O fact, O trail) { if (trail == null) trail = "unknown"; if (facts.put(fact, trail) != null) ret; if (verbose) print("New fact: " + fact); rulesAndFacts.newB(fact); } L factsInOrderOfDefinition() { ret rulesAndFacts.cloneBList(); } start-thread { if (empty(msgs)) setField(msgs := mainCruddieLog()); print("Have " + nMessages(msgs)); pnlStruct(takeFirst(2, msgs)); L msgs2 = whereFieldIsTrue fromUser(msgs); for (Msg msg : takeFirst(5, msgs2)) addFact(ll(msg, Fragment("was understood correctly"))); addFact(ll(get(msgs2, 5), Fragment("is a wrong recognition"))); addLogicRule(new LogicRule( ll("$x", Fragment("is a wrong recognition")), ll("$x", Fragment("should be corrected")))); addLogicRule(new LogicRule( ll("$x", Fragment("should be corrected")), ll("$x", Fragment("is a wrong recognition")))); stepAll(lambda0 step); print("done. " + nFacts(rulesAndFacts.bList) + ", " + nRules(rulesAndFacts.aList)); } // define what a variable is bool isVar(O o) { ret o instanceof S && isDollarVar(o/S); } void applyLogicRuleToFact(LogicRule rule, O fact) { new LogicRuleApplier ap; ap.rule = rule; ap.fact = fact; ap.verbose = verbose; ap.printNonMatches = printNonMatches; ap.addRewrittenRHS = lambda2 addRewrittenRHS; ap.tools.isVar = lambda1 isVar; ap.run(); } void addRewrittenRHS(O o, O trail) { if (o cast LogicRule) { o.trail = trail; addLogicRule(o); } else if o is And(O a, O b) { addRewrittenRHS(a, o); addRewrittenRHS(b, o); } else if (o != null) addFact(o, trail); } }