1 | static class LogicEngine implements IEngine { |
2 | Lisp code; |
3 | new L<Lisp> memory; |
4 | new L<S> log; |
5 | new L<Lisp> stm; |
6 | boolean fireOneRuleOnly = true; |
7 | |
8 | *() {} |
9 | *(Lisp *code, L<Lisp> *memory) {} |
10 | *(S codeText, L<Lisp> *memory) { |
11 | code = snlToTree(codeText); |
12 | } |
13 | |
14 | void stmAdd(Lisp statement) { |
15 | stm.add(statement); |
16 | } |
17 | |
18 | Lisp snlMatchFirst(Lisp pat, L<Lisp> list, SNLMatches m) { |
19 | for (Lisp l : list) |
20 | if (snlMatch2(pat, l, m)) |
21 | ret l; |
22 | ret null; |
23 | } |
24 | |
25 | Lisp scanMem(Lisp pat, SNLMatches m) { |
26 | ret snlMatchFirst(pat, memory, m); |
27 | } |
28 | |
29 | Lisp scanMem(S pat, SNLMatches m) { |
30 | ret snlMatchFirst(snlToTree_cached(pat), memory, m); |
31 | } |
32 | |
33 | public void memorize(Lisp l) { |
34 | if (!memory.contains(l)) { |
35 | memory.add(l); |
36 | saveMemory(); |
37 | } |
38 | } |
39 | |
40 | public void forget(Lisp pat) { |
41 | int l = l(memory); |
42 | for (int i = 0; i < l(memory); i++) |
43 | if (snlMatch(pat, memory.get(i), new HashMap)) |
44 | memory.remove(i--); |
45 | if (l(memory) != l) |
46 | saveMemory(); |
47 | } |
48 | |
49 | void saveMemory() {} // override this for persistence |
50 | |
51 | public boolean yo(Lisp snl, SNLMatches m) { |
52 | if (scanMem(snl, m) != null) |
53 | ret true; |
54 | |
55 | if (snlMatch2("i < don't < know < X", snl, m)) |
56 | ret scanMem("* < said < X < is < *", m) == null; // TODO: vars probably don't work |
57 | |
58 | ret false; |
59 | } |
60 | |
61 | boolean runSingle(Lisp code, L<Lisp> out) { |
62 | log.add("runSingle " + code); |
63 | LThread thread = new LThread(this, code); |
64 | thread.log = log; |
65 | thread.statements.addAll(stm); |
66 | //print("Statements: " + structure(thread.statements)); |
67 | boolean success = thread.run(); |
68 | out.addAll(thread.output); |
69 | ret success; |
70 | } |
71 | |
72 | // run multiple rules |
73 | |
74 | boolean runMulti(Lisp code, L<Lisp> out) { |
75 | assertTrue(isJuxta(code)); |
76 | boolean success = false; |
77 | for (Lisp sub : code) { |
78 | if (runSingle(sub, out)) { // success |
79 | success = true; |
80 | if (fireOneRuleOnly) |
81 | break; |
82 | } |
83 | } |
84 | ret success; |
85 | } |
86 | |
87 | L<Lisp> run() { |
88 | new L<Lisp> out; |
89 | if (isJuxta(code)) |
90 | runMulti(code, out); |
91 | else |
92 | runSingle(code, out); |
93 | ret out; |
94 | } |
95 | } |
96 | |
97 | !include #1002752 // newest LThread |
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: | #1002759 |
Snippet name: | class LogicEngine (LIVE!) |
Eternal ID of this version: | #1002759/1 |
Text MD5: | 8ceae4f0c4fae0ecc3794ddf6ec30a8a |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-02-20 21:44:36 |
Source code size: | 2268 bytes / 97 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 592 / 768 |
Referenced in: | [show references] |