static class LogicEngine implements IEngine { Lisp code; new L memory; new L log; new L stm; boolean fireOneRuleOnly = true; *() {} *(Lisp *code, L *memory) {} *(S codeText, L *memory) { code = snlToTree(codeText); } void stmAdd(Lisp statement) { stm.add(statement); } Lisp snlMatchFirst(Lisp pat, L list, SNLMatches m) { for (Lisp l : list) if (snlMatch2(pat, l, m)) ret l; ret null; } Lisp scanMem(Lisp pat, SNLMatches m) { ret snlMatchFirst(pat, memory, m); } Lisp scanMem(S pat, SNLMatches m) { ret snlMatchFirst(snlToTree_cached(pat), memory, m); } public void memorize(Lisp l) { if (!memory.contains(l)) { memory.add(l); saveMemory(); } } public void forget(Lisp pat) { int l = l(memory); for (int i = 0; i < l(memory); i++) if (snlMatch(pat, memory.get(i), new HashMap)) memory.remove(i--); if (l(memory) != l) saveMemory(); } void saveMemory() {} // override this for persistence public boolean yo(Lisp snl, SNLMatches m) { if (scanMem(snl, m) != null) ret true; if (snlMatch2("i < don't < know < X", snl, m)) ret scanMem("* < said < X < is < *", m) == null; // TODO: vars probably don't work ret false; } boolean runSingle(Lisp code, L out) { log.add("runSingle " + code); LThread thread = new LThread(this, code); thread.log = log; thread.statements.addAll(stm); //print("Statements: " + structure(thread.statements)); boolean success = thread.run(); out.addAll(thread.output); ret success; } // run multiple rules boolean runMulti(Lisp code, L out) { assertTrue(isJuxta(code)); boolean success = false; for (Lisp sub : code) { if (runSingle(sub, out)) { // success success = true; if (fireOneRuleOnly) break; } } ret success; } L run() { new L out; if (isJuxta(code)) runMulti(code, out); else runSingle(code, out); ret out; } } !include #1002752 // newest LThread