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

136
LINES

< > BotCompany Repo | #1004310 // Random v10 Thinker

JavaX fragment (include)

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

Author comment

Began life as a copy of #1004209

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1004310
Snippet name: Random v10 Thinker
Eternal ID of this version: #1004310/1
Text MD5: 18362ea8d54d74e365333e3ebe5f58ff
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-13 13:45:14
Source code size: 4101 bytes / 136 lines
Pitched / IR pitched: No / No
Views / Downloads: 640 / 1259
Referenced in: [show references]