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)

sclass Thinker {
  L<S> ranking = synchroList();
  int listMakingTimeout = 2000;
  int maxListLength = 100;
  bool showExceptions, debug;
  volatile int load;
  O env; // must supply "makeGenerators"
  
  void startUp(O env, L<S> log) {
    this.env = env;
    readLocally2(this, "ranking");
    print("Ranking: " + structure(ranking));
  }
  
  // also called from outside
  void recommendSolver(S solverID) {
    if (!isRecommendedSolver(solverID = fsi(solverID))) {
      print("Adding recommended solver: " + solverID);
      logQuoted("recommendations.txt", solverID);
    } else
      print("Solver already recommended: " + solverID);
  }
  
  bool isRecommendedSolver(S solverID) {
    ret contains(scanLog("recommendations.txt"), fsI(solverID));
  }
  
  // log = what's in the chat
  // input = what user is typing
  void makeListData(L<S> thelog, S input, L<ImmL<S>> otherLogs, L l) {
    long started = now();
    try {
      long timeout = started + listMakingTimeout;
      new HashMap<S, Map> seen; // maps to the line
      
      // extended log including what user is typing
      L<S> xlog = listPlus(thelog, input);
      
      // Make generators for both modes
      
      new L<Gen> gens;
      for (bool completing : ll(false, true)) {
        new L<Gen> gens_;
        try {
          genLog_set(completing ? xlog : thelog);
          gOtherLogs_set(otherLogs);
          gCompleting_set(completing);
          call(env, "makeGenerators", gens_);
          for (Gen g : gens_)
            gens.add(new Gen(g.name + gMode(), g.func));
        } finally {
          genLog_clear();
          gOtherLogs_clear();
          gCompleting_set(null);
        }
      }
      
      // Rank all generators
      
      gens = rankGenerators(gens);

      // Produce list
          
      int i = -1;
      while (now() < timeout && l(l) < maxListLength && nempty(gens)) {
        i = (i+1) % l(gens);
        Gen gen = gens.get(i);
        bool completing = gen.name.endsWith("/i");
        
        try {
          genLog_set(completing ? xlog : thelog);
          gOtherLogs_set(otherLogs);
          gCompleting_set(completing);
          bool remove = false;
          if (debug)
            print("Trying generator " + gen.name);
          try {
            S s = callGen(gen);
            if (empty(s) /*|| eq(input, s)*/)
              remove = true;
            else if (seen.containsKey(s)) {
              Map line = seen.get(s);
              setAdd((L) line.get("Suggesters"), gen.name);
              remove = true;
            } else {
              Map line = litorderedmap("Suggestion", s, "Suggesters", ll(gen.name));
              l.add(line);
              seen.put(s, line);
            }
          } catch e {
            if (showExceptions)
              l.add(litorderedmap("Suggestion", "[error] " + exceptionToStringShort(e), "Suggesters", ll(gen.name)));
            remove = true;
          }
          if (remove)
            gens.remove(i--);
        } finally {
          genLog_clear();
          gOtherLogs_clear();
          gCompleting_set(null);
        }
      }
    } catch e {
      printStackTrace(e);
      l.add(e.toString());
    } finally {
      load = (int) ((now()-started)*100/listMakingTimeout);
    }
  }
  
  L<Gen> rankGenerators(L<Gen> gens) {
    Map<S, Gen> index = indexByField(gens, "name");
    new L<Gen> l;
    L<S> rank = cloneList(ranking);
    for (S name : rank) {
      Gen g = index.get(name);
      if (g != null) {
        l.add(g);
        index.remove(name);
      }
    }
    l.addAll(values(index)); // add rest in unspecified order
    //print("Using ranking: " + struct(rank));
    //print("Ranked generators: " + struct(l));
    ret l;
  }
  
  void rankToTop(S name) {
    if (empty(name)) ret;
    if (eq(first(ranking), name)) ret;
    ranking.remove(name);
    ranking.add(0, name);
    saveLocally2(this, "ranking");
    print("New ranking: " + structure(ranking));
  }
}

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: 699 / 1324
Referenced in: #1004309 - Eleutheria ["Random" v10]