Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

262
LINES

< > BotCompany Repo | #1003582 // "Random" v1

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (2597L/17K/63K).

1  
!759
2  
3  
static JList list;
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  
15  
sclass Gen {
16  
  S name;
17  
  O func;
18  
  
19  
  *(S *name, O *func) {}
20  
  
21  
  public S toString() {
22  
    ret name;
23  
  }
24  
}
25  
26  
p-awt {
27  
  loadLog();
28  
29  
  final L<S> _log = cloneList(log);
30  
  thread "Scoring!" {
31  
    print("Yo: " + callF(oneAssoc(dropLast(_log))));
32  
    scoreGenerator(_log, "1 assoc");
33  
    scoreGenerators(_log);
34  
  }
35  
  
36  
  list = new JList;
37  
  chat = autoScroll(wordWrapTextArea());
38  
  chat.setText(joinLines(log));
39  
  input = new JTextField;
40  
  JFrame frame = showFrame(vgrid(centerAndSouth(chat, input), list));
41  
  //setFrameIconLater(frame, "#1003593");
42  
  
43  
  onEnter(input, r {
44  
    post();
45  
  });
46  
  
47  
  onDoubleClick(list, voidfunc(S s) {
48  
    if (empty(s)) ret;
49  
    input.setText(s);
50  
    post();
51  
  });
52  
  
53  
  fillList();
54  
  
55  
  input.requestFocus();
56  
}
57  
58  
static S getInput() {
59  
  ret joinLines(" # ", input.getText().trim());
60  
}
61  
62  
static void post() {
63  
  S s = getInput() + "\n";
64  
  chat.append(s);
65  
  appendToFile(logFile, "[" + chatTime() + "] " + s);
66  
  input.selectAll();
67  
}
68  
69  
static void fillList() {
70  
  bool t = shouldUpdateList();
71  
  if (neq(t, thinking)) {
72  
    thinking = t;
73  
    setFrameIcon(list, t ? "#1003603" : "#1003593");
74  
  }
75  
  
76  
  if (!t)
77  
    againl8r();
78  
  else thread "Fill List" {
79  
    final new L<S> data;
80  
    makeListData(data);
81  
    
82  
    awt {
83  
      fillListWithStrings(list, data);
84  
      againl8r();
85  
    }
86  
  }
87  
}
88  
89  
static void againl8r() {
90  
  swingAfter(list, listDelay, r { fillList(); });
91  
}
92  
93  
static bool shouldUpdateList() {
94  
  //print("List bounds: " + boundsOnScreen(list));
95  
  ret getFrame(list).isFocused()
96  
    && !mouseInComponent(list);
97  
}
98  
99  
// also called from outside
100  
static L<S> loadLog() {
101  
  if (logFile == null)
102  
    logFile = getProgramFile("log.txt");
103  
  for (S s : toLines(loadTextFile(logFile))) pcall {
104  
    log.add(substring(s, s.indexOf(']')+1).trim());
105  
  }
106  
  ret log;
107  
}
108  
109  
// also called from outside
110  
static void recommendSolver(S solverID) {
111  
  if (!isRecommendedSolver(solverID = fsi(solverID))) {
112  
    print("Adding recommended solver: " + solverID);
113  
    logQuoted("recommendations.txt", solverID);
114  
  } else
115  
    print("Solver already recommended: " + solverID);
116  
}
117  
118  
static bool isRecommendedSolver(S solverID) {
119  
  ret contains(scanLog("recommendations.txt"), fsI(solverID));
120  
}
121  
122  
static void makeListData(L<S> l) {
123  
  try {
124  
    new L<Gen> gens;
125  
    makeGenerators(gens, log);
126  
    if (empty(gens)) {
127  
      l.add("No generators");
128  
      ret;
129  
    }
130  
    
131  
    long timeout = now() + listMakingTimeout;
132  
    int i = -1;
133  
    new HashSet<S> seen;
134  
    while (now() < timeout && l(l) < maxListLength && nempty(gens)) {
135  
      i = (i+1) % l(gens);
136  
      Gen gen = gens.get(i);
137  
      try {
138  
        S s = cast callF(gen.func);
139  
        if (empty(s) || seen.contains(s))
140  
          gens.remove(i);
141  
        else {
142  
          seen.add(s);
143  
          l.add(s);
144  
        }
145  
      } catch {
146  
        gens.remove(i);
147  
      }
148  
    }
149  
  } catch e {
150  
    printStackTrace(e);
151  
    l.add(e.toString());
152  
  }
153  
}
154  
155  
// VERIFICATION PART
156  
157  
static void scoreGenerators(L<S> log) {
158  
  new MultiSet<S> scores;
159  
  for (int i = 1; i <= l(log); i++)
160  
    scoreGenerators1(subList(log, 0, i), scores);
161  
  print(asciiHeading2("SCORES"));
162  
  for (S name : scores.getTopTen())
163  
    print("  [" + scores.get(name) + "] " + name);
164  
  print();
165  
}
166  
  
167  
static void scoreGenerators1(L<S> log, MultiSet<S> scores) {
168  
  if (empty(log)) ret;
169  
  S line = last(log);
170  
  log = dropLast(log);
171  
  
172  
  new L<Gen> gens;
173  
  makeGenerators(gens, log);
174  
  
175  
  for (Gen gen : gens) {
176  
    try {
177  
      if (eq(callF(gen.func), line))
178  
        scores.add(gen.name);
179  
    } catch {}
180  
  }
181  
}
182  
183  
static S callSingle(L<S> log, O genName) {
184  
  new L<Gen> gens;
185  
  makeGenerators(gens, log);
186  
  Gen gen = findByField(gens, "name", genName);
187  
  if (gen == null) null;
188  
  
189  
  ret (S) callF(gen.func);
190  
}
191  
192  
static bool verifySingle(L<S> log, O genName) {
193  
  if (empty(log)) false;
194  
  S line = last(log);
195  
  log = dropLast(log);
196  
  
197  
  new L<Gen> gens;
198  
  makeGenerators(gens, log);
199  
  Gen gen = findByField(gens, "name", genName);
200  
  if (gen == null) false;
201  
  
202  
  try {
203  
    if (eq(callF(gen.func), line))
204  
      true;
205  
  } catch {}
206  
  
207  
  false;
208  
}
209  
210  
static void scoreGenerator(L<S> log, S genName) {
211  
  for (int i = 1; i < l(log); i++) {
212  
    S expect = log.get(i);
213  
    S s = callSingle(subList(log, 0, i), genName);
214  
    bool ok = eq(s, expect);
215  
    if (ok)
216  
      print(genName + " OK: " + expect);
217  
    else
218  
      print(genName + " NO [" + s + "]: " + expect);
219  
  }
220  
}
221  
222  
// CREATIVE PART!
223  
224  
// put func {}'s returning a string in there
225  
static void makeGenerators(L<Gen> l, final L<S> log) {
226  
  gen(l, "hello random", func { "Hello " + randomID(10) });
227  
  gen(l, quine(func { last(log) }));
228  
  gen(l, quine(func { oneOf(log) }));
229  
  gen(l, "1 assoc", oneAssoc(log));
230  
  gen(l, "most popular", mostPopular(log));
231  
  addLoadedSolvers(l);
232  
}
233  
234  
svoid addLoadedSolvers(L<Gen> l) {
235  
  if (recommendations == null) { 
236  
    recommendations = new L;
237  
    for (S s : scanLog("recommendations.txt"))
238  
      if (isSnippetID(s))
239  
        recommendations.add(s);
240  
  }
241  
    
242  
  for (final S solverID : recommendations)
243  
    gen(l, solverID, func {
244  
      O c = hotwireCached(solverID);
245  
      ret call(c, "calc", log);
246  
    });
247  
}
248  
249  
static O oneAssoc(final L<S> log) {
250  
  ret func {
251  
    for (int i = l(log)-2; i >= 0; i--)
252  
      if (eqic(log.get(i), last(log)))
253  
        ret log.get(i+1);
254  
    null;
255  
  };
256  
}
257  
258  
static O mostPopular(final L<S> log) {
259  
  ret func {
260  
    ret new MultiHashSet<S>(log).getMostPopularEntry();
261  
  };
262  
}

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: #1003582
Snippet name: "Random" v1
Eternal ID of this version: #1003582/1
Text MD5: ff6c7ea610b4094f85c99b769209c15d
Transpilation MD5: af17dd5c265fc62f8fb9bcd7252dd512
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-24 20:40:11
Source code size: 5853 bytes / 262 lines
Pitched / IR pitched: No / No
Views / Downloads: 794 / 1222
Referenced in: [show references]