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).

!759

!include #1003606 // GenTesting

static JList list;
static JTextArea chat;
static JTextField input;
static File logFile;
static new L<S> log;
static L<S> recommendations;

static int listDelay = 2000, listMakingTimeout = 2000;
static int maxListLength = 100;

static Bool thinking;

p-awt {
  //substanceLAF("EmeraldDusk"); // Too dark!
  substanceLAF("ChallengerDeep");
  loadLog();

  final L<S> _log = cloneList(log);
  
  thread "Scoring!" {
    GenTesting gt = new GenTesting(voidfunc(L<Gen> gens, L<S> log) { makeGenerators(gens, log); });
    print("Yo: " + callF(oneAssoc(dropLast(_log))));
    gt.scoreGenerator(_log, "1 assoc");
    gt.scoreGenerators(_log);
  }
  
  list = new JList;
  chat = autoScroll(wordWrapTextArea());
  chat.setText(joinLines(log));
  input = new JTextField;
  JFrame frame = showFrame(vgrid(centerAndSouth(chat, input), list));
  //setFrameIconLater(frame, "#1003593");
  
  onEnter(input, r {
    post();
  });
  
  onDoubleClick(list, voidfunc(S s) {
    if (empty(s)) ret;
    input.setText(s);
    post();
  });
  
  fillList();
  
  input.requestFocus();
}

static S getInput() {
  ret joinLines(" # ", input.getText().trim());
}

static void post() {
  S i = getInput();
  S s = i + "\n";
  chat.append(s);
  appendToFile(logFile, "[" + chatTime() + "] " + s);
  log.add(i);
  input.selectAll();
}

static void fillList() {
  bool t = shouldUpdateList();
  if (neq(t, thinking)) {
    thinking = t;
    setFrameIcon(list, t ? "#1003603" : "#1003593");
  }
  
  if (!t)
    againl8r();
  else thread "Fill List" {
    final new L<S> data;
    makeListData(data);
    
    awt {
      fillListWithStrings(list, data);
      againl8r();
    }
  }
}

static void againl8r() {
  swingAfter(list, listDelay, r { fillList(); });
}

static bool shouldUpdateList() {
  //print("List bounds: " + boundsOnScreen(list));
  ret getFrame(list).isFocused()
    && !mouseInComponent(list);
}

// also called from outside
static L<S> loadLog() {
  if (logFile == null)
    logFile = getProgramFile("log.txt");
  for (S s : toLines(loadTextFile(logFile))) pcall {
    log.add(substring(s, s.indexOf(']')+1).trim());
  }
  ret log;
}

// also called from outside
static void recommendSolver(S solverID) {
  if (!isRecommendedSolver(solverID = fsi(solverID))) {
    print("Adding recommended solver: " + solverID);
    logQuoted("recommendations.txt", solverID);
  } else
    print("Solver already recommended: " + solverID);
}

static bool isRecommendedSolver(S solverID) {
  ret contains(scanLog("recommendations.txt"), fsI(solverID));
}

static void makeListData(L<S> l) {
  try {
    new L<Gen> gens;
    makeGenerators(gens, log);
    if (empty(gens)) {
      l.add("No generators");
      ret;
    }
    
    long timeout = now() + listMakingTimeout;
    int i = -1;
    new HashSet<S> seen;
    while (now() < timeout && l(l) < maxListLength && nempty(gens)) {
      i = (i+1) % l(gens);
      Gen gen = gens.get(i);
      try {
        S s = cast callF(gen.func);
        if (empty(s) || seen.contains(s))
          gens.remove(i);
        else {
          seen.add(s);
          l.add(s);
        }
      } catch {
        gens.remove(i);
      }
    }
  } catch e {
    printStackTrace(e);
    l.add(e.toString());
  }
}

// CREATIVE PART!

// put func {}'s returning a string in there
static void makeGenerators(L<Gen> l, final L<S> log) {
  gen(l, "hello random", func { "Hello " + randomID(10) });
  gen(l, quine(func { last(log) }));
  gen(l, quine(func { oneOf(log) }));
  gen(l, "1 assoc", oneAssoc(log));
  gen(l, "most popular", mostPopular(log));
  addLoadedSolvers(l);
}

svoid addLoadedSolvers(L<Gen> l) {
  if (recommendations == null) { 
    recommendations = new L;
    for (S s : scanLog("recommendations.txt"))
      if (isSnippetID(s))
        recommendations.add(s);
  }
    
  for (final S solverID : recommendations)
    gen(l, solverID, func {
      O c = hotwireCached(solverID);
      ret call(c, "calc", log);
    });
}

static O oneAssoc(final L<S> log) {
  ret func {
    for (int i = l(log)-2; i >= 0; i--)
      if (eqic(log.get(i), last(log)))
        ret log.get(i+1);
    null;
  };
}

static O mostPopular(final L<S> log) {
  ret func {
    ret new MultiHashSet<S>(log).getMostPopularEntry();
  };
}

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: 479 / 547
Referenced in: [show references]