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

148
LINES

< > BotCompany Repo | #1004209 // Random v9 Thinker (Other Logs)

JavaX fragment (include)

1  
!include #1003606 // GenTesting
2  
3  
sclass Thinker {
4  
  L<S> ranking = synchroList();
5  
  int listMakingTimeout = 2000;
6  
  int maxListLength = 100;
7  
  bool showExceptions, debug;
8  
  volatile int load;
9  
  
10  
  void startUp(L<S> log) {
11  
    readLocally2(this, "ranking");
12  
    print("Ranking: " + structure(ranking));
13  
  }
14  
  
15  
  MultiSet<S> scores(L<S> log) {
16  
    ret makeGT().scoreGenerators(log);
17  
  }
18  
  
19  
  MultiSet<S> scores(L<S> log, BitSet interestingLines) {
20  
    ret makeGT().scoreGenerators(log, interestingLines);
21  
  }
22  
  
23  
  GenTesting makeGT() {
24  
    ret new GenTesting(voidfunc(L<Gen> gens, L<S> log) { makeGenerators(gens); });
25  
  }
26  
  
27  
  // also called from outside
28  
  void recommendSolver(S solverID) {
29  
    if (!isRecommendedSolver(solverID = fsi(solverID))) {
30  
      print("Adding recommended solver: " + solverID);
31  
      logQuoted("recommendations.txt", solverID);
32  
    } else
33  
      print("Solver already recommended: " + solverID);
34  
  }
35  
  
36  
  bool isRecommendedSolver(S solverID) {
37  
    ret contains(scanLog("recommendations.txt"), fsI(solverID));
38  
  }
39  
  
40  
  // log = what's in the chat
41  
  // input = what user is typing
42  
  void makeListData(L<S> thelog, S input, L<ImmL<S>> otherLogs, L l) {
43  
    long started = now();
44  
    try {
45  
      long timeout = started + listMakingTimeout;
46  
      new HashMap<S, Map> seen; // maps to the line
47  
      
48  
      // extended log including what user is typing
49  
      L<S> xlog = listPlus(thelog, input);
50  
      
51  
      // Make generators for both modes
52  
      
53  
      new L<Gen> gens;
54  
      for (bool completing : ll(false, true)) {
55  
        new L<Gen> gens_;
56  
        try {
57  
          genLog_set(completing ? xlog : log);
58  
          gOtherLogs_set(otherLogs);
59  
          gCompleting_set(completing);
60  
          makeGenerators(gens_);
61  
          for (Gen g : gens_)
62  
            gens.add(new Gen(g.name + gMode(), g.func));
63  
        } finally {
64  
          genLog_clear();
65  
          gOtherLogs_clear();
66  
          gCompleting_set(null);
67  
        }
68  
      }
69  
      
70  
      // Rank all generators
71  
      
72  
      gens = rankGenerators(gens);
73  
74  
      // Produce list
75  
          
76  
      int i = -1;
77  
      while (now() < timeout && l(l) < maxListLength && nempty(gens)) {
78  
        i = (i+1) % l(gens);
79  
        Gen gen = gens.get(i);
80  
        bool completing = gen.name.endsWith("/i");
81  
        
82  
        try {
83  
          genLog_set(completing ? xlog : log);
84  
          gOtherLogs_set(otherLogs);
85  
          gCompleting_set(completing);
86  
          bool remove = false;
87  
          if (debug)
88  
            print("Trying generator " + gen.name);
89  
          try {
90  
            S s = callGen(gen);
91  
            if (empty(s) /*|| eq(input, s)*/)
92  
              remove = true;
93  
            else if (seen.containsKey(s)) {
94  
              Map line = seen.get(s);
95  
              setAdd((L) line.get("Suggesters"), gen.name);
96  
              remove = true;
97  
            } else {
98  
              Map line = litorderedmap("Suggestion", s, "Suggesters", ll(gen.name));
99  
              l.add(line);
100  
              seen.put(s, line);
101  
            }
102  
          } catch e {
103  
            if (showExceptions)
104  
              l.add(litorderedmap("Suggestion", "[error] " + exceptionToStringShort(e), "Suggesters", ll(gen.name)));
105  
            remove = true;
106  
          }
107  
          if (remove)
108  
            gens.remove(i--);
109  
        } finally {
110  
          genLog_clear();
111  
          gOtherLogs_clear();
112  
          gCompleting_set(null);
113  
        }
114  
      }
115  
    } catch e {
116  
      printStackTrace(e);
117  
      l.add(e.toString());
118  
    } finally {
119  
      load = (int) ((now()-started)*100/listMakingTimeout);
120  
    }
121  
  }
122  
  
123  
  L<Gen> rankGenerators(L<Gen> gens) {
124  
    Map<S, Gen> index = indexByField(gens, "name");
125  
    new L<Gen> l;
126  
    L<S> rank = cloneList(ranking);
127  
    for (S name : rank) {
128  
      Gen g = index.get(name);
129  
      if (g != null) {
130  
        l.add(g);
131  
        index.remove(name);
132  
      }
133  
    }
134  
    l.addAll(values(index)); // add rest in unspecified order
135  
    //print("Using ranking: " + struct(rank));
136  
    //print("Ranked generators: " + struct(l));
137  
    ret l;
138  
  }
139  
  
140  
  void rankToTop(S name) {
141  
    if (empty(name)) ret;
142  
    if (eq(first(ranking), name)) ret;
143  
    ranking.remove(name);
144  
    ranking.add(0, name);
145  
    saveLocally2(this, "ranking");
146  
    print("New ranking: " + structure(ranking));
147  
  }
148  
}

Author comment

Began life as a copy of #1003937

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1004209
Snippet name: Random v9 Thinker (Other Logs)
Eternal ID of this version: #1004209/1
Text MD5: 8cbb095d9c98ed38568793f351f3d630
Author: stefan
Category: javax / talking robots
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-10 16:58:58
Source code size: 4374 bytes / 148 lines
Pitched / IR pitched: No / No
Views / Downloads: 503 / 789
Referenced in: [show references]