Libraryless. Click here for Pure Java version (7210L/48K).
1 | //!include once #1022817 // Attractor Classes |
2 | |
3 | abstract sclass AttractorBot extends AbstractSayAndInputBot implements Runnable { |
4 | transient LS inputQueue = synchroLinkedList(); |
5 | transient Set<Thread> activeThreads = synchroSet(); |
6 | |
7 | // functions taking a string and returning a string or collection of strings |
8 | transient L preprocessors = synchroList(); |
9 | |
10 | transient F1<S, Bool> isYes = f<S, Bool> isYes_1; |
11 | new LinkedHashSet<S> facts; |
12 | new LinkedHashSet<S> assumptions; |
13 | |
14 | transient L<Attractor> standardAttractors = synchroList(); // permanent |
15 | transient L<Attractor> standardLowPrioAttractors = synchroList(); // permanent |
16 | transient L<Attractor> activeAttractors = synchroList(); // temporary |
17 | transient L<Attractor> lowPrioAttractors = synchroList(); // temporary |
18 | |
19 | S line; |
20 | |
21 | *() { |
22 | input = f<S> getInput; |
23 | standardPreprocessors(); |
24 | } |
25 | |
26 | void remember(S fact) { |
27 | if (facts.add(fact)) |
28 | print("[Bot learned:] " + fact); |
29 | } |
30 | |
31 | void assume(S fact) { |
32 | if (assumptions.add(fact)) |
33 | print("[Bot assumes:] " + fact); |
34 | } |
35 | |
36 | S getInput() { |
37 | while (empty(inputQueue)) |
38 | sleep(10); |
39 | ret trim(popFirst(inputQueue)); |
40 | } |
41 | |
42 | bool yes() { |
43 | ret callF(isYes, input()); |
44 | } |
45 | |
46 | // call this instead of run() so thread is marked |
47 | void run_public() { |
48 | temp tempAddToCollection(activeThreads, currentThread()); |
49 | run(); |
50 | } |
51 | |
52 | void addInput(S s) { |
53 | inputQueue.add(s); |
54 | line = s; |
55 | if (empty(activeThreads)) |
56 | attractorBattle(); |
57 | } |
58 | |
59 | void attractorBattle { |
60 | if (empty(inputQueue)) ret; |
61 | S input = getInput(); |
62 | print("Input: " + input); |
63 | |
64 | // high prio active, then standing attractors, then low prio active |
65 | L<Attractor> attractors = getAndClearList(activeAttractors); |
66 | addAll(attractors, standardAttractors); |
67 | addAll(attractors, getAndClearList(lowPrioAttractors)); |
68 | addAll(attractors, standardLowPrioAttractors); |
69 | |
70 | if (empty(attractors)) ret with print("No attractors, ignoring input"); |
71 | new L<Attractor> matchingAttractors; |
72 | for (Attractor a : attractors) pcall { |
73 | if (a.matches_public(input)) { |
74 | matchingAttractors.add(a); |
75 | } |
76 | } |
77 | if (empty(matchingAttractors)) |
78 | ret with print("No matching attractors, ignoring input"); |
79 | print("Got " + n2(matchingAttractors, "matching attractor") + ": " + matchingAttractors); |
80 | Attractor winner = first(matchingAttractors); |
81 | if (l(matchingAttractors) > 1) print("Choosing first attractor: " + winner); |
82 | runAttractor(winner); |
83 | } |
84 | |
85 | // attractors can use AttractorBot.this.say() [old] or emitAnswer() [preferred] |
86 | void runAttractor(Attractor a) { |
87 | say(getVars(getEmittedAnswers(a))); |
88 | } |
89 | |
90 | void addAttractor(Attractor a) { addAttractors(a); } |
91 | void tempAttractor(Attractor a) { addAttractors(a); } |
92 | void addAttractors(Attractor... attractors) { |
93 | addAll(activeAttractors, attractors); |
94 | } |
95 | |
96 | void addLowPrioAttractor(Attractor a) { add(lowPrioAttractors, a); } |
97 | void lowPrioAttractor(Attractor a) { addLowPrioAttractor(a); } |
98 | void defaultAttractor(Attractor a) { addLowPrioAttractor(a); } |
99 | |
100 | void addStandardAttractor(Attractor a) { add(standardAttractors, a); } |
101 | void standardAttractor(Attractor a) { addStandardAttractor(a); } |
102 | |
103 | void standardAttractors(Attractor... attractors) { |
104 | addAll(standardAttractors, attractors); |
105 | } |
106 | |
107 | void standardLowPrioAttractor(Attractor a) { add(standardLowPrioAttractors, a); } |
108 | |
109 | void addPreprocessors(O... l) { |
110 | addAll(preprocessors, l); |
111 | } |
112 | |
113 | void standardPreprocessors() { |
114 | addPreprocessors(f ai_dropLeadingAndTrailingAppellations_sortedByLength); |
115 | } |
116 | |
117 | // override me for start-up behavior |
118 | run {} |
119 | } |
Began life as a copy of #1022801
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1022804 |
Snippet name: | AttractorBot - trying to make persistable (attractors and preprocessors are transient) |
Eternal ID of this version: | #1022804/44 |
Text MD5: | d61f21ddd511af25aaa02aaf984dea58 |
Transpilation MD5: | 0c602e70ad9c5a9af1ddb45443f96379 |
Author: | stefan |
Category: | javax / a.i. |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2019-06-24 17:15:48 |
Source code size: | 3795 bytes / 119 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 438 / 1011 |
Version history: | 43 change(s) |
Referenced in: | [show references] |