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

192
LINES

< > BotCompany Repo | #1003665 // "Random" v2

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

Libraryless. Click here for Pure Java version (2908L/19K/70K).

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

Author comment

Began life as a copy of #1003582

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1003665
Snippet name: "Random" v2
Eternal ID of this version: #1003665/1
Text MD5: 1590975cf186f9ca572014dbd32ec656
Transpilation MD5: 5493544d785f41a66eada05332d642e5
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 14:44:08
Source code size: 4461 bytes / 192 lines
Pitched / IR pitched: No / No
Views / Downloads: 543 / 633
Referenced in: [show references]