Libraryless. Click here for Pure Java version (4119L/29K/99K).
1 | !759 |
2 | |
3 | static JTable table; |
4 | static JTextArea chat; |
5 | static JTextField input; |
6 | static File logFile; |
7 | static new L<S> log; |
8 | static L<S> recommendations; |
9 | |
10 | static int listDelay = 2000, listMakingTimeout = 2000; |
11 | static int maxListLength = 100; |
12 | |
13 | static Bool thinking; |
14 | static bool updateOnce = true; |
15 | static new Thinker thinker; |
16 | |
17 | static S systemPrefix = "[system]"; |
18 | |
19 | p-awt { |
20 | //substanceLAF("EmeraldDusk"); // Too dark! |
21 | substanceLAF("ChallengerDeep"); |
22 | loadLog(); |
23 | |
24 | thinker.startUp(log); |
25 | |
26 | table = new JTable; |
27 | chat = autoScroll(wordWrapTextArea()); |
28 | chat.setText(joinLines(log)); |
29 | input = new JTextField; |
30 | JFrame frame = showFrame(vgrid(centerAndSouth(chat, input), table)); |
31 | //setFrameIconLater(frame, "#1003593"); |
32 | |
33 | onEnter(input, r { |
34 | post(); |
35 | }); |
36 | |
37 | onDoubleClick(table, voidfunc(int row) { |
38 | S s = getSelectedLine(table).get(0); |
39 | if (empty(s)) ret; |
40 | input.setText(s); |
41 | post(); |
42 | }); |
43 | |
44 | fillList(); |
45 | |
46 | input.requestFocus(); |
47 | } |
48 | |
49 | static S getInput() { |
50 | ret joinLines(" # ", input.getText().trim()); |
51 | } |
52 | |
53 | static void post() { |
54 | S i = getInput(); |
55 | if (inputAllowedByUser(i)) |
56 | post(i); |
57 | } |
58 | |
59 | static bool inputAllowedByUser(S i) { |
60 | ret !swic(i, systemPrefix); |
61 | } |
62 | |
63 | static void postSystemMessage(S msg) { |
64 | post(systemPrefix + " " + msg); |
65 | } |
66 | |
67 | static void post(S i) { |
68 | S s = i + "\n"; |
69 | chat.append(s); |
70 | appendToFile(logFile, "[" + chatTime() + "] " + s); |
71 | log.add(i); |
72 | input.selectAll(); |
73 | updateOnce = true; |
74 | action(i); |
75 | } |
76 | |
77 | static void action(S s) { |
78 | if (!s.startsWith("!")) ret; |
79 | s = dropPrefix("!", s); |
80 | new Matches m; |
81 | if "start program *" { |
82 | S progID = m.fsi(0); |
83 | S title = getSnippetTitle(progID); |
84 | // TODO: Show author! |
85 | S msg = "Run program " + progID + " - " + title + "?"; |
86 | if (confirmOKCancel(chat, msg)) { |
87 | postSystemMessage("Starting program " + progID + " - " + quote(title)); |
88 | nohupJavax(progID); |
89 | } else |
90 | postSystemMessage("Program start cancelled by user (was: " + progID + ")"); |
91 | } |
92 | } |
93 | |
94 | static void fillList() { |
95 | bool t = shouldUpdateList() || updateOnce; |
96 | updateOnce = false; |
97 | if (neq(t, thinking)) { |
98 | thinking = t; |
99 | setFrameIcon(table, t ? "#1003603" : "#1003593"); |
100 | } |
101 | |
102 | if (!t) |
103 | againl8r(); |
104 | else thread "Fill List" { |
105 | final new L<S> data; |
106 | thinker.makeListData(data); |
107 | |
108 | dataToTable_uneditable(table, data); |
109 | againl8r(); |
110 | } |
111 | } |
112 | |
113 | static void againl8r() { |
114 | swingAfter(table, listDelay, r { fillList(); }); |
115 | } |
116 | |
117 | static bool shouldUpdateList() { |
118 | ret getFrame(table).isFocused() |
119 | && !mouseInComponent(table); |
120 | } |
121 | |
122 | // also called from outside |
123 | static L<S> loadLog() { |
124 | if (logFile == null) |
125 | logFile = getProgramFile("log.txt"); |
126 | for (S s : toLines(loadTextFile(logFile))) pcall { |
127 | log.add(substring(s, s.indexOf(']')+1).trim()); |
128 | } |
129 | ret log; |
130 | } |
131 | |
132 | !include #1003606 // GenTesting |
133 | |
134 | sclass Thinker { |
135 | new MultiSet<S> scores; |
136 | |
137 | void startUp(L<S> log) { |
138 | final L<S> _log = cloneList(log); // Clone to be safe |
139 | |
140 | thread "Scoring!" { |
141 | GenTesting gt = new GenTesting(voidfunc(L<Gen> gens, L<S> log) { makeGenerators(gens, log); }); |
142 | scores = gt.scoreGenerators(_log); |
143 | } |
144 | } |
145 | |
146 | // also called from outside |
147 | void recommendSolver(S solverID) { |
148 | if (!isRecommendedSolver(solverID = fsi(solverID))) { |
149 | print("Adding recommended solver: " + solverID); |
150 | logQuoted("recommendations.txt", solverID); |
151 | } else |
152 | print("Solver already recommended: " + solverID); |
153 | } |
154 | |
155 | bool isRecommendedSolver(S solverID) { |
156 | ret contains(scanLog("recommendations.txt"), fsI(solverID)); |
157 | } |
158 | |
159 | L<Gen> sortGenerators(L<Gen> gens, final MultiSet<S> scores) { |
160 | ret sortedList(gens, func(Gen a, Gen b) { |
161 | scores.get(b.name)-scores.get(a.name) |
162 | }); |
163 | } |
164 | |
165 | void makeListData(L l) { |
166 | try { |
167 | new L<Gen> gens; |
168 | makeGenerators(gens, log); |
169 | gens = sortGenerators(gens, scores); |
170 | if (empty(gens)) { |
171 | l.add(ll("No generators")); |
172 | ret; |
173 | } |
174 | |
175 | long timeout = now() + listMakingTimeout; |
176 | int i = -1; |
177 | new HashSet<S> seen; |
178 | while (now() < timeout && l(l) < maxListLength && nempty(gens)) { |
179 | i = (i+1) % l(gens); |
180 | Gen gen = gens.get(i); |
181 | try { |
182 | S s = cast callF(gen.func); |
183 | if (empty(s) || seen.contains(s)) |
184 | gens.remove(i); |
185 | else { |
186 | seen.add(s); |
187 | l.add(litorderedmap("Suggestion", s, "Suggester", gen.name)); |
188 | } |
189 | } catch { |
190 | gens.remove(i); |
191 | } |
192 | } |
193 | } catch e { |
194 | printStackTrace(e); |
195 | l.add(e.toString()); |
196 | } |
197 | } |
198 | } |
199 | |
200 | // CREATIVE PART! |
201 | |
202 | // put func {}'s returning a string in there |
203 | static void makeGenerators(L<Gen> l, final L<S> log) { |
204 | gen(l, "hello random", func { "Hello " + randomID(10) }); |
205 | gen(l, quine(func { last(log) })); |
206 | gen(l, quine(func { oneOf(log) })); |
207 | gen(l, "1 assoc", oneAssoc(log)); |
208 | gen(l, "most popular", mostPopular(log)); |
209 | addLoadedSolvers(l); |
210 | } |
211 | |
212 | svoid addLoadedSolvers(L<Gen> l) { |
213 | if (recommendations == null) { |
214 | recommendations = new L; |
215 | for (S s : scanLog("recommendations.txt")) |
216 | if (isSnippetID(s)) |
217 | recommendations.add(s); |
218 | } |
219 | |
220 | for (final S solverID : recommendations) |
221 | gen(l, solverID, func { |
222 | O c = hotwireCached(solverID); |
223 | ret call(c, "calc", log); |
224 | }); |
225 | } |
226 | |
227 | static O oneAssoc(final L<S> log) { |
228 | ret func { |
229 | for (int i = l(log)-2; i >= 0; i--) |
230 | if (eqic(log.get(i), last(log))) |
231 | ret log.get(i+1); |
232 | null; |
233 | }; |
234 | } |
235 | |
236 | static O mostPopular(final L<S> log) { |
237 | ret func { |
238 | ret new MultiHashSet<S>(log).getMostPopularEntry(); |
239 | }; |
240 | } |
Began life as a copy of #1003669
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1003678 |
Snippet name: | "Random" v4 (with table instead of list & actions) |
Eternal ID of this version: | #1003678/1 |
Text MD5: | 870354b43e48434af2b19e7e8c8e6e21 |
Transpilation MD5: | ee0966e090a74d20a6319fe620a235b5 |
Author: | stefan |
Category: | javax / talking robots |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-07-25 17:54:00 |
Source code size: | 5788 bytes / 240 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 705 / 981 |
Referenced in: | [show references] |