Libraryless. Click here for Pure Java version (4368L/28K/90K).
1 | !759 |
2 | |
3 | static S codeSnippetID = "#1002745"; |
4 | static Lisp code; |
5 | static new L<Lisp> memory; |
6 | static LogicEngine engine; |
7 | |
8 | p { |
9 | code = snlToTree(loadSnippet(codeSnippetID)); |
10 | print(getProgramID() + ": parse result = " + code); |
11 | load("memory"); |
12 | } |
13 | |
14 | static class LogicEngine implements IEngine { |
15 | Lisp code; |
16 | new L<Lisp> memory; |
17 | new L<S> log; |
18 | new L<Lisp> stm; |
19 | boolean fireOneRuleOnly = true; |
20 | |
21 | *() {} |
22 | *(Lisp *code, L<Lisp> *memory) {} |
23 | *(S codeText, L<Lisp> *memory) { |
24 | code = snlToTree(codeText); |
25 | } |
26 | |
27 | void stmAdd(Lisp statement) { |
28 | stm.add(statement); |
29 | } |
30 | |
31 | Lisp snlMatchFirst(Lisp pat, L<Lisp> list, SNLMatches m) { |
32 | for (Lisp l : list) |
33 | if (snlMatch2(pat, l, m)) |
34 | ret l; |
35 | ret null; |
36 | } |
37 | |
38 | Lisp scanMem(Lisp pat, SNLMatches m) { |
39 | ret snlMatchFirst(pat, memory, m); |
40 | } |
41 | |
42 | Lisp scanMem(S pat, SNLMatches m) { |
43 | ret snlMatchFirst(snlToTree_cached(pat), memory, m); |
44 | } |
45 | |
46 | public void memorize(Lisp l) { |
47 | if (!memory.contains(l)) { |
48 | memory.add(l); |
49 | saveMemory(); |
50 | } |
51 | } |
52 | |
53 | void saveMemory() {} // override this for persistence |
54 | |
55 | public boolean yo(Lisp snl, SNLMatches m) { |
56 | if (scanMem(snl, m) != null) |
57 | ret true; |
58 | |
59 | if (snlMatch2("i < don't < know < X", snl, m)) |
60 | ret scanMem("* < said < X < is < *", m) == null; // TODO: vars probably don't work |
61 | |
62 | ret false; |
63 | } |
64 | |
65 | L<Lisp> runSingle(Lisp code) { |
66 | log.add("runSingle " + code); |
67 | LThread thread = new LThread(this, code); |
68 | thread.log = log; |
69 | thread.statements.addAll(stm); |
70 | //print("Statements: " + structure(thread.statements)); |
71 | ret thread.run() ? thread.output : null; |
72 | } |
73 | |
74 | // run multiple rules |
75 | |
76 | L<Lisp> runMulti(Lisp code) { |
77 | assertTrue(isJuxta(code)); |
78 | new L<Lisp> out; |
79 | for (Lisp sub : code) { |
80 | L<Lisp> l = runSingle(sub); |
81 | if (l != null) { // success |
82 | out.addAll(l); |
83 | if (fireOneRuleOnly) |
84 | break; |
85 | } |
86 | } |
87 | ret out; |
88 | } |
89 | |
90 | L<Lisp> run() { |
91 | ret isJuxta(code) |
92 | ? runMulti(code) |
93 | : runSingle(code); |
94 | } |
95 | } |
96 | |
97 | !include #1002752 // newest LThread |
98 | |
99 | static S allToText(L<Lisp> trees) { |
100 | new L<S> l; |
101 | for (Lisp tree : trees) |
102 | l.add(snlToText(tree)); |
103 | ret joinLines(l); |
104 | } |
105 | |
106 | synchronized answer { |
107 | if (!attn()) ret null; |
108 | |
109 | if (match("log " + parseSnippetID(programID()), s)) |
110 | ret engine == null ? "nullie haven't run yet": slackSnippet(fromLines(engine.log)); |
111 | |
112 | if (match("memory " + parseSnippetID(programID()), s)) |
113 | ret slackSnippet(allToText(memory)); |
114 | |
115 | // convert to SNL first |
116 | s = or((S) callOpt/*Safe*/(getBot ("#1002700"), "naiveSNL", s), s); |
117 | Lisp in = snlToTree(s); |
118 | |
119 | Lisp statement = snlToTree("user < says < [" + snlFromTree(in) + "]"); |
120 | engine = new LogicEngine(code, memory) { |
121 | void saveMemory() { |
122 | save("memory"); |
123 | } |
124 | }; |
125 | engine.stmAdd(statement); |
126 | |
127 | L<Lisp> out = engine.run(); |
128 | if (nempty(out)) |
129 | ret "[SNL: " + snlFromTree(in) + "])\n" + allToText(out); |
130 | } |
131 | |
132 | static Lisp quickrun(L<Lisp> memory, S userInput) { |
133 | engine = new LogicEngine(code, memory); |
134 | Lisp statement = snlToTree("user < says < [" + userInput + "]"); |
135 | engine.stmAdd(statement); |
136 | ret snlMakeJuxta(engine.run()); |
137 | } |
Began life as a copy of #1002746
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1002751 |
Snippet name: | SNL Logic Engine 4, multiple returns, as class |
Eternal ID of this version: | #1002751/1 |
Text MD5: | 82db206ce219bb2da015fc0fcb39d043 |
Transpilation MD5: | 260b6191efb645079ed2e140640e3ea3 |
Author: | stefan |
Category: | eleu / nl |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-02-20 04:19:03 |
Source code size: | 3309 bytes / 137 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 698 / 1174 |
Referenced in: | [show references] |