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

206
LINES

< > BotCompany Repo | #1003669 // "Random" v3 (extracting Thinker)

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

Libraryless. Click here for Pure Java version (2954L/20K/72K).

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  
static new Thinker thinker;
15  
16  
p-awt {
17  
  //substanceLAF("EmeraldDusk"); // Too dark!
18  
  substanceLAF("ChallengerDeep");
19  
  loadLog();
20  
21  
  thinker.startUp(log);
22  
  
23  
  list = new JList;
24  
  chat = autoScroll(wordWrapTextArea());
25  
  chat.setText(joinLines(log));
26  
  input = new JTextField;
27  
  JFrame frame = showFrame(vgrid(centerAndSouth(chat, input), list));
28  
  //setFrameIconLater(frame, "#1003593");
29  
  
30  
  onEnter(input, r {
31  
    post();
32  
  });
33  
  
34  
  onDoubleClick(list, voidfunc(S s) {
35  
    if (empty(s)) ret;
36  
    input.setText(s);
37  
    post();
38  
  });
39  
  
40  
  fillList();
41  
  
42  
  input.requestFocus();
43  
}
44  
45  
static S getInput() {
46  
  ret joinLines(" # ", input.getText().trim());
47  
}
48  
49  
static void post() {
50  
  S i = getInput();
51  
  S s = i + "\n";
52  
  chat.append(s);
53  
  appendToFile(logFile, "[" + chatTime() + "] " + s);
54  
  log.add(i);
55  
  input.selectAll();
56  
}
57  
58  
static void fillList() {
59  
  bool t = shouldUpdateList();
60  
  if (neq(t, thinking)) {
61  
    thinking = t;
62  
    setFrameIcon(list, t ? "#1003603" : "#1003593");
63  
  }
64  
  
65  
  if (!t)
66  
    againl8r();
67  
  else thread "Fill List" {
68  
    final new L<S> data;
69  
    thinker.makeListData(data);
70  
    
71  
    awt {
72  
      fillListWithStrings(list, data);
73  
      againl8r();
74  
    }
75  
  }
76  
}
77  
78  
static void againl8r() {
79  
  swingAfter(list, listDelay, r { fillList(); });
80  
}
81  
82  
static bool shouldUpdateList() {
83  
  //print("List bounds: " + boundsOnScreen(list));
84  
  ret getFrame(list).isFocused()
85  
    && !mouseInComponent(list);
86  
}
87  
88  
// also called from outside
89  
static L<S> loadLog() {
90  
  if (logFile == null)
91  
    logFile = getProgramFile("log.txt");
92  
  for (S s : toLines(loadTextFile(logFile))) pcall {
93  
    log.add(substring(s, s.indexOf(']')+1).trim());
94  
  }
95  
  ret log;
96  
}
97  
98  
!include #1003606 // GenTesting
99  
100  
sclass Thinker {
101  
  new MultiSet<S> scores;
102  
  
103  
  void startUp(L<S> log) {
104  
    final L<S> _log = cloneList(log); // Clone to be safe
105  
    
106  
    thread "Scoring!" {
107  
      GenTesting gt = new GenTesting(voidfunc(L<Gen> gens, L<S> log) { makeGenerators(gens, log); });
108  
      scores = gt.scoreGenerators(_log);
109  
    }
110  
  }
111  
112  
  // also called from outside
113  
  void recommendSolver(S solverID) {
114  
    if (!isRecommendedSolver(solverID = fsi(solverID))) {
115  
      print("Adding recommended solver: " + solverID);
116  
      logQuoted("recommendations.txt", solverID);
117  
    } else
118  
      print("Solver already recommended: " + solverID);
119  
  }
120  
  
121  
  bool isRecommendedSolver(S solverID) {
122  
    ret contains(scanLog("recommendations.txt"), fsI(solverID));
123  
  }
124  
  
125  
  L<Gen> sortGenerators(L<Gen> gens, final MultiSet<S> scores) {
126  
    ret sortedList(gens, func(Gen a, Gen b) {
127  
      scores.get(b.name)-scores.get(a.name)
128  
    });
129  
  }
130  
131  
  void makeListData(L<S> l) {
132  
    try {
133  
      new L<Gen> gens;
134  
      makeGenerators(gens, log);
135  
      gens = sortGenerators(gens, scores);
136  
      if (empty(gens)) {
137  
        l.add("No generators");
138  
        ret;
139  
      }
140  
      
141  
      long timeout = now() + listMakingTimeout;
142  
      int i = -1;
143  
      new HashSet<S> seen;
144  
      while (now() < timeout && l(l) < maxListLength && nempty(gens)) {
145  
        i = (i+1) % l(gens);
146  
        Gen gen = gens.get(i);
147  
        try {
148  
          S s = cast callF(gen.func);
149  
          if (empty(s) || seen.contains(s))
150  
            gens.remove(i);
151  
          else {
152  
            seen.add(s);
153  
            l.add(s);
154  
          }
155  
        } catch {
156  
          gens.remove(i);
157  
        }
158  
      }
159  
    } catch e {
160  
      printStackTrace(e);
161  
      l.add(e.toString());
162  
    }
163  
  }
164  
}
165  
166  
// CREATIVE PART!
167  
168  
// put func {}'s returning a string in there
169  
static void makeGenerators(L<Gen> l, final L<S> log) {
170  
  gen(l, "hello random", func { "Hello " + randomID(10) });
171  
  gen(l, quine(func { last(log) }));
172  
  gen(l, quine(func { oneOf(log) }));
173  
  gen(l, "1 assoc", oneAssoc(log));
174  
  gen(l, "most popular", mostPopular(log));
175  
  addLoadedSolvers(l);
176  
}
177  
178  
svoid addLoadedSolvers(L<Gen> l) {
179  
  if (recommendations == null) { 
180  
    recommendations = new L;
181  
    for (S s : scanLog("recommendations.txt"))
182  
      if (isSnippetID(s))
183  
        recommendations.add(s);
184  
  }
185  
    
186  
  for (final S solverID : recommendations)
187  
    gen(l, solverID, func {
188  
      O c = hotwireCached(solverID);
189  
      ret call(c, "calc", log);
190  
    });
191  
}
192  
193  
static O oneAssoc(final L<S> log) {
194  
  ret func {
195  
    for (int i = l(log)-2; i >= 0; i--)
196  
      if (eqic(log.get(i), last(log)))
197  
        ret log.get(i+1);
198  
    null;
199  
  };
200  
}
201  
202  
static O mostPopular(final L<S> log) {
203  
  ret func {
204  
    ret new MultiHashSet<S>(log).getMostPopularEntry();
205  
  };
206  
}

Author comment

Began life as a copy of #1003665

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: #1003669
Snippet name: "Random" v3 (extracting Thinker)
Eternal ID of this version: #1003669/1
Text MD5: 7f769ba7fcc3de359c8fefb9987a3cb4
Transpilation MD5: 5fab904a144603fe3cecaf21944ea948
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:14:06
Source code size: 4843 bytes / 206 lines
Pitched / IR pitched: No / No
Views / Downloads: 668 / 733
Referenced in: [show references]