sclass RuleEngine { new LL<S> facts; // stores code token parses Map<S, O> phrasesWordTree = wordTree(); Clusters<S> clusters = ciClusters(); new Prolog prolog; *(S program) { this("", program); } *(S program, bool showStuff) { if (showStuff) showStuff(); runProgram(program); } *(S inspiration, S program) { runProgram(inspiration, program); } void runProgram(S program) { runProgram("", program); } void runProgram(S inspiration, S program) { for (S s : tlftj(inspiration)) addFact("inspired " + quote(s)); L<S> programLines = tlftj(program); //pnl(program); //print(); for (S s : programLines) { Pair<S, L<S>> p = parseFunctionCall(s); if (p != null) { //print("Function call: " + sfu(p)); S f = p.a; L<S> args = p.b; if (eqic(f, "assert")) assertFact(getSingleton(p.b)); else if (swic(f, "assert_")) { if (eqic(f, "assert_answer")) assertAnswer(unquote(first(args)), unquote(second(args))); else if (eqic(f, "assert_parse")) assertParse(args); else fail("Unknown assertion function: " + f); } else if (eqic(f, "fact_question_answer_upperCaseVars")) prolog.code( dontPrint("if [" + toProlog(unquote(first(args))) + "] and [" + toProlog(unquote(second(args))) + "] then [answer is [" + toProlog(unquote(third(args))) + "]]")); continue; } L<S> tok = javaTok(s); if (eqic(second(tok), "assert")) { assertFact(join(dropFirst(3, tok))); continue; } int i = indexOfSubListLL(tok, "-", "", ">"); if (i >= 0) { L<S> left = subList(tok, 1, i-1); int j = indexOf(left, "+"); if (j >= 0) { prolog.code( print("if [" + toProlog(joinSubList(left, 0, j-1)) + "] and [" + toProlog(joinSubList(left, j+2)) + "] then [" + toProlog(joinSubList(tok, i+4)) + "]")); continue; } } tok = codeTokens(tok); if (eqic(getSingletonOpt(tok), "rewrite")) continue with addFactsFromProlog(prolog.rewriteAndSave()); if (l(tok) == 2 && isIdentifier(first(tok)) && isQuoted(second(tok))) if (eq(first(tok), "phrase")) { addPhrase(unquote(second(tok))); continue; } if (l(tok) == 3 && eq(second(tok), "=")) { ciClusters_add(clusters, addPhrase(unquote(first(tok))), addPhrase(unquote(third(tok)))); continue; } addFact(s); } } void printStuff { printAsciiHeading("Facts"); pnlStruct(facts); print(); print("Clusters: " + sfu(clusters)); print("Phrases: " + sfu(phrasesWordTree)); } L<S> parseFact(S s) { L<S> tok = map toLowerIfIdentifier(javaTokWithAngleBracketsC(/*dropPunctuation*/(s))); ret nempties(translateUsingWordTreeC(tok, phrasesWordTree)); } void assertFact(S fact) { if (contains(facts, parseFact(fact))) print("OK fact: " + fact); else print("FAILED fact assert: " + fact); } void assertAnswer(S q, S a) { Prolog p = new(prolog); //p.showStuff = true; p.code(dontPrint("to prolog: ", squareBracket(toProlog(q)))); L<Lisp> out = p.rewrite(); //print("Got " + l(out) + " statement(s)"); L<S> lines = map nlUnparse(out); L<S> answers = startingWithEndingWith_drop(lines, "answer is [", "]"); if (l(answers) == 1 && eqic(first(answers), a)) print("OK answer: " + q + " -> " + a); else { pnl(map nlUnparse(out)); print("FAIL answer: " + q + " -> " + first(answers) + " - expected: " + a); } } S addPhrase(S s) { wordTreeAdd(phrasesWordTree, javaTokC(s), s); ret s; } void addFact(S s) { L<S> tok = parseFact(s); facts.add(tok); prolog.code(dontPrint("to prolog: ", squareBracket(factToProlog(tok)))); } void assertParse(L<S> args) { args = unquoteAll(args); S s = first(args); L<S> parsed = parseFact(s); L<S> expected = dropFirst(args); if (eq(parsed, expected)) print("OK parse: " + s + " -> " + sfu(parsed)); else print("FAILED parse: " + s + " -> " + sfu(parsed) + " - expected: " + sfu(expected)); } S toProlog(S s) { L<S> tok = parseFact(upperCaseVarsToDollarVars(s)); ret factToProlog(tok); } S factToProlog(L<S> tok) { ret joinWithSpace(map ruleEngine_tokenToProlog(tok)); } void addFactsFromProlog(L<Lisp> l) { addFacts(printAll(map nlUnparse(l))); } void addFacts(L<S> l) { for (S s : unnull(l)) addFact(s); } RuleEngine showStuff() { prolog.showStuff = true; this; } }
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1014771 |
Snippet name: | Rule Engine [Include] |
Eternal ID of this version: | #1014771/13 |
Text MD5: | 950578db2ebe0cbc4096479bdb940fc9 |
Author: | stefan |
Category: | javax / a.i. |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2018-05-02 09:58:03 |
Source code size: | 5016 bytes / 176 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 365 / 943 |
Version history: | 12 change(s) |
Referenced in: | #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674) |