import java.util.*; import java.util.zip.*; import java.util.List; import java.util.regex.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; import java.util.concurrent.locks.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import javax.swing.table.*; import java.io.*; import java.net.*; import java.lang.reflect.*; import java.lang.ref.*; import java.lang.management.*; import java.security.*; import java.security.spec.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import java.math.*; import java.text.*; import java.text.NumberFormat; import javax.net.ssl.*; import java.security.SecureRandom; import java.security.cert.X509Certificate; import java.text.SimpleDateFormat; import net.schmizz.sshj.xfer.InMemorySourceFile; import net.schmizz.sshj.xfer.scp.SCPFileTransfer; import oshi.SystemInfo; import oshi.hardware.*; import net.schmizz.sshj.SSHClient; import net.schmizz.sshj.common.IOUtils; import net.schmizz.sshj.connection.channel.direct.Session; import net.schmizz.sshj.transport.verification.PromiscuousVerifier; import javax.imageio.metadata.*; import javax.imageio.stream.*; import java.util.*; import java.io.*; import java.net.*; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.security.KeyStore; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.GZIPOutputStream; import java.nio.charset.*; import java.nio.*; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.DataFlavor; import java.awt.geom.AffineTransform; import java.awt.datatransfer.*; import java.awt.dnd.*; import java.awt.geom.*; import java.awt.font.GlyphVector; import javax.swing.event.AncestorListener; import javax.swing.event.AncestorEvent; import javax.swing.Timer; import javax.swing.undo.UndoManager; import java.awt.datatransfer.UnsupportedFlavorException; public class main { public static boolean greetCookies, theoryOn; public static boolean cloned; public static long clonedSince, cloningSince; public static String postStartUpMessageToChat; public static List thoughtBots = new ArrayList(); public static long started, indexMakingTime, bootTime; public static Class theoryModule; public static Lock theoryLock = lock(); public static String selectedWord; public static int inputNr = 1; public static int myPort = 4678; public static int ultimateAnswerLimit = 500000; public static double speculationTimeoutForHttp = 5.0; public static double speculationTimeoutForNewChatLine = 30.0; public static int autoComplete_timeOut = 1000; public static Set speculationQueue = synchroHashSet(); public static double spammerSleep = 10.0; public static Set blockedIPs = synchroHashSet(); public static String classesToShare = smartBot_sharedClasses(); public static Lock aiLock = lock(); public static ThreadLocal httpAuthed = new ThreadLocal(); public static Map> lastUserLines = syncCIMap(); public static ThreadLocal afterAnswering = new ThreadLocal(); public static ThreadLocal answering = new ThreadLocal(); public static List latestWebs = synchroList(new LatestList(1000)); public static TimedCache cachedNumberOfThreads = new TimedCache("numberOfAllThreads", 10.0); public static List tripleAnswerFunctions = ll("ai_tripleAnswer_regexpMatches"); public static class E { } public static void main(final String[] args) throws Exception { profilePoint_print = true; print("yo"); started = sysNow(); try { setGCFrequency(10 * 60.0); } catch (Throwable _e) { updateJavaXAndRestart(); } doEvery(10 * 60 * 1000, new Runnable() { public void run() { try { blockedIPs.clear(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "blockedIPs.clear()"; } }); ai_useThoughtSpaces(true); cachedNodeIndex_autoMake = false; releaseDBLockOnTimeoutEval(); ai_prepLoadFunctions(); db(); load("selectedWord"); load("inputNr"); Object webSocketFactory = "makeWebSocket"; if (canOccupyPort80()) serveHttpWithWebsockets_multiplePorts(webSocketFactory, myPort, 80); else serveHttpWithWebsockets(myPort, webSocketFactory); try { serveHttps_botCompany(); } catch (Throwable __e) { _handleException(__e); } bot("Smart Bot.", new F1() { public String get(String s) { try { String x = "Input line " + (inputNr++); print(x); save("inputNr"); ai_postTriple(x, "is", quote(s)); stefansChat_onLine_fullParams.set(litmap("auth", true)); try { return actualAnswer(s); } finally { stefansChat_onLine_fullParams.set(null); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "S x = \"Input line \" + (inputNr++);\r\n print(x);\r\n save(\"inputNr\");\r\n ..."; } }); loadPage_forcedTimeout = 70000; thoughtBots.add(mc()); indexMakingTime = sysNow(); ai_fillTripleIndex(); indexMakingTime = sysNow() - indexMakingTime; handleStefansChat(); handleSmartBotsChat(); if (greetCookies) runInNewThread("#1010793"); ai_onNewWeb("ai_spec_moveToMiddle"); ai_onNewWeb("addToLatestWebs"); ai_extendKnownVerbs(); addVirtualNodeIndex(hugeEnglishDictionary_virtualNodeIndex()); loadTheory(); { Thread _t_0 = new Thread("Speculator") { public void run() { try { speculatorLoop(); } catch (Throwable __e) { _handleException(__e); } } }; startThread(_t_0); } doneLoading(); bootTime = sysNow() - started; if (cloned) postToChatNamed(postStartUpMessageToChat, infoBoxAndReturn("Smart Bot cloned in " + formatSeconds(bootTime, 1) + "s (" + formatSeconds(sysNow() - clonedSince, 1) + "s full)")); else infoBox("Smart Bot booted in " + formatSeconds(bootTime, 1) + "s"); } public static String actualAnswer(String s) { return scanMultipleBots(thoughtBots, s); } public static void loadTheory() { Lock __0 = theoryLock; lock(__0); try { cleanUp(theoryModule); theoryModule = null; if (!theoryOn) return; theoryModule = run("#1010963"); setOpt(theoryModule, "onUpdate", new Runnable() { public void run() { try { call(theoryModule, "react"); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "call(theoryModule, \"react\")"; } }); call(theoryModule, "react"); print("Theory module loaded."); } finally { unlock(__0); } } public static int lineCount; public static boolean authed() { return isTrue(mapGet(stefansChat_onLine_fullParams.get(), "auth")) || webAuthed(); } public static boolean byBot() { return isTrue(mapGet(stefansChat_onLine_fullParams.get(), "botMark")); } public static String answer(String s) { final Matches m = new Matches(); try { if (byBot()) return null; selectWord(quote(s)); String a = answer_2(s); { String _a_19 = a; if (!empty(_a_19)) return _a_19; } } catch (Exception __e) { throw rethrow(__e); } return null; } public static String answer_2(String s) { while (licensed()) { try { return trim(answer_go(s)); } catch (Throwable e) { _handleException(e); String a = exceptionToStringShort(e); if (eq(a, "ConcurrentModificationException")) { sleep(50); continue; } return a; } } return null; } public static String answer_go(String s) { ++lineCount; boolean authed = authed(); Lock __1 = dbLock(); lock(__1); try { String std = ai_standardAnswer(s); if (std == null) std = ai_standardAnswer_dollarVars(s); if (!startsWith(s, "!")) s = ai_dropDroppableStuff(s); int safety = 0; replaceloop: while (safety++ < 10) { s = trim(s); { String _a_20 = answer_inner(s, lineCount); if (!empty(_a_20)) return _a_20; } for (WebNode node : indexedNodes(s)) { String x = web_operandText(node, "replace with"); if (x != null && neq(s, x)) { print("Replacing with " + x); s = x; continue replaceloop; } } break; } return std; } finally { unlock(__1); } } public static String chatName() { return or2((String) mapGet(stefansChat_onLine_fullParams.get(), "chatName"), "Unknown Chat"); } public static String chatLineSymbol() { return (String) mapGet(stefansChat_onLine_fullParams.get(), "Chat line symbol"); } public static String answer_inner(String s, int lineCount) { String chatName = chatName(); final Matches m = new Matches(); if (eqic(s, "!eval ps")) return backtick("ps"); if (swic_trim(s, "!eval ps ", m)) return backtick("ps " + noUnixTricks(m.rest())); if (authed()) { { if (eqic(s, "!restart")) { restart(); return "Yo"; } if (eqic(s, "!kill")) { cleanKillIn1Second(); return "Yo"; } if (eqic(s, "!soft-restart 1")) { softRestart_level1(); return "Hold on..."; } if (eqicOneOf(s, "!soft-restart", "!auto-restart")) { softRestart_level2(); return "Hold on..."; } if (eqic(s, "!theory")) { loadTheory(); return "OK"; } if (swic_trim(s, "!theory ", m)) return callAnswerFunction(theoryModule, m.rest()); if (eqic(s, "!peep")) { runAndCleanUp("#1010980"); return "OK, got " + fileLength(ai_chatAnalysis_peepHoleFile()) + " bytes"; } if (swic_trim(s, "!subst-web ", m)) { List tok = javaTokC(m.rest()); if (l(tok) == 2) return ai_subst_web_charBasedReplace(first(tok), selectWord(unquote(last(tok)))); else return ai_subst_web(m.rest()); } if (eqic(s, "!idle-patterns")) { long _startTime_9 = sysNow(); Either e = (Either) evalWithTimeout(60.0, "ai_idle_matchPatterns"); _startTime_9 = sysNow() - _startTime_9; saveTiming(_startTime_9); return e.isA() ? "Made " + nWebs(e.a()) + " in " + toSeconds(lastTiming(), 1) + "s" : "Timeout"; } if (swic_trim(s, "!experiment ", m)) { String code = m.rest(); if (!isSnippetID(code)) code = psI("#1013002") + " " + bashQuote(code); File outFile = directNohupJavax(code); return "OK, " + f2s(outFile); } if (swic_trim(s, "!experiment ", m)) { File outFile = directNohupJavax(m.rest()); return "OK, " + f2s(outFile); } if (eqic(s, "!rotate")) { rotateSoftwareMadeWebs(); return "OK"; } if (swic_trim(s, "!speculate-all ", m)) { long _startTime_10 = sysNow(); Either e = (Either) evalWithTimeout(10.0, new Runnable() { public void run() { try { ai_speculate_all(m.rest()); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ai_speculate_all(m.rest())"; } }); _startTime_10 = sysNow() - _startTime_10; saveTiming(_startTime_10); int n = 0; try { n = e.isA() ? e.a() : websMadeInThread(e.b()); } catch (Throwable __e) { _handleException(__e); } return "Made " + nWebs(n) + " in " + toSeconds(lastTiming(), 1) + "s"; } if (eqic(s, "!quick-transpile yourself")) return transpileMyself("quick"); if (eqic(s, "!medium-transpile yourself")) return transpileMyself("medium"); } if (match("unlearn * *", s, m)) return "OK, was: " + uniq(MatchLearner.class, "pattern", m.unq(0)).examples.put(m.unq(1), "rejected"); if (swic_trim(s, "!sf ", m)) return sfu(callAndMake_orDirect(m.rest())); if (swic_trim(s, "!specf ", m)) { String sf = m.rest(); print("specf " + sf); Class c = loadFunctionsWithInclude(ll(sf), "#1011841"); try { callOpt(c, "ai_spec_init"); return sfu(call(c, sf)); } finally { cleanUp(c); } } if (swicOneOf_trim(s, m, "!eval ", "!j ")) return smartBotEval(m.rest()); if (swic_trim(s, "!fresh ", m)) { dynClear(); return smartBotEval(m.rest()); } if (eqicOneOf(s, "!fresh", "!dyn-clear")) { dynClear(); return "OK, refreshed"; } if (swic_trim(s, "!spec ", m)) { Pair p = splitAtFirstSpace(m.rest()); String f = startsWith(p.a, "ai_") ? p.a : "ai_spec_" + p.a; return smartBotEval("ai_callSpeculationFunction(f " + f + ", " + p.b + ");"); } if (swic_trim(s, "!spec-all ", m)) { String f = m.rest(); f = startsWith(f, "ai_") ? f : "ai_spec_" + f; final String _f = f; return smartBotEval("ai_callSpeculationFunctionOnAllTerms(f " + f + ");"); } if (swic_trim(s, "!run ", m)) { runAndCleanUp(m.rest()); return "OK"; } if (swic_trim(s, "!match ", m)) { List tok = javaTokC(m.rest()); if (l(tok) != 2) return "need 2 parameters"; String pat = get(tok, 0), input = unquote(get(tok, 1)); Matches mm = ai_matchPattern(pat, input); return mm == null ? "No match" : sfu(toList(mm.m)); } if (eqic(s, "!compact")) { { Thread _t_1 = new Thread("Compacting") { public void run() { try { startedLoading(); try { { long _startTime_7 = sysNow(); try { ai_compactLiveDB(true); } finally { _startTime_7 = sysNow() - _startTime_7; saveTiming(_startTime_7); } } postToSmartBotsChat("Tripelized in " + toSeconds(lastTiming()) + " s"); } finally { doneLoading(); } } catch (Throwable __e) { _handleException(__e); } } }; startThread(_t_1); } return "OK, compacting"; } if (eqic(s, "!sort-lists")) { { long _startTime_8 = sysNow(); try { tripleIndex().sortAllLists(); } finally { _startTime_8 = sysNow() - _startTime_8; saveTiming(_startTime_8); } } return "OK [" + lastTiming() + " ms]"; } if (swic_trim(s, "!var ", m)) return sfu(get(mc(), m.rest())); if (eqic(s, "!mtm")) { ai_spec_moveToMiddle_all(); return "OK"; } if (eqic(s, "!gc")) return ai_gc_scan("The most important word"); if (eqic(s, "!num-invalid")) return lstr(ai_allInvalidWebs()); if (swic_trim(s, "!load-subspace ", m)) { AI_SubSpace ss = ai_swapInSubSpace(m.unq(0)); return "OK, size=" + l(ss); } if (eqic(s, "!assume")) { assumeSmartBotURL(); return "OK"; } if (match("done making that", s)) return ai_doneMakingThat(chatName); String c = "are making in " + chatName; if (flexMatchIC("let's make a *", s, m) || flexMatchIC("let's make an *", s, m)) { String id = ai_text_verified("We", c, "$X"); if (id != null) return "We're still making " + id; String aThing = a(m.rest()); id = selectWord(aGlobalID()); post("We", c, id); post(id, "is", aThing); String answer = "OK, ID for new " + m.rest() + ": " + id + "."; try { answer = appendWithSpaceIfNempty(answer, ai_makePartQuestion(chatName, id)); } catch (Throwable __e) { _handleException(__e); } return answer; } WebNode openQuestion = first(ai_search_dollarX_verified("Open question in " + chatName, "is", "$X")); if (openQuestion != null) { post(web_text(openQuestion), "is", s); ai_invalidateWeb(openQuestion); String answer = "OK, stored: " + web_text(openQuestion) + " is " + quoteUnlessWordsAndSpaces(s) + "."; String id = ai_text("We", c, "$X"); String q = ai_makePartQuestion(chatName, id); if (q == null) { ai_doneMakingThat(chatName); q = "I think we're done here. Made " + id + ", " + ai_renderThingDescriptionWithParts(id); } answer = appendWithSpaceIfNempty(answer, q); return answer; } for (String sf : ai_texts_verified("$X", "is a good function to call when user says", quote(s))) { sf = unquote(sf); if (endsWith(sf, "()", m)) if (isIdentifier(m.rest())) return sfu(makeAndCall(m.rest())); } } if (ai_isQuestion_1(s) && !byBot()) { selectWord(s); ai_postTriple(s, "should be", "answered in the chat"); } if (match("Are you online?", s)) return "Yes"; if (eqicOneOf(s, "!question", "!gac")) return random_gac36k(); if (swic_trim(s, "!word ", m)) { selectWord(m.rest()); return "OK " + unicode_rightPointingTriangle() + unicode_rightPointingTriangle(); } if (swic_trim(s, "!image ", m)) { String x = m.rest(); print("Googling " + x); BufferedImage img = quickVisualize(x); return "[IMAGE] " + uploadJPEGToImageServer(img, x); } if (match("memory stats", s) || match("full memory stats", s)) return nWeb(ai_allWebsCount()) + ". " + diskSpaceEtcBot_answer(s) + ". " + diskSpaceInfo(); { String _a_21 = diskSpaceEtcBot_answer(s); if (!empty(_a_21)) return _a_21; } if (swic_trim(s, "!search ", m)) { selectWord("[search] " + m.rest()); return "OK " + unicode_rightPointingTriangle() + unicode_rightPointingTriangle(); } if (swic_trim(s, "!solve ", m)) { String a; long _startTime_16 = sysNow(); a = ai_solveQuestionInSubSpace(m.rest()); done2_always("Solve in subspace", _startTime_16); return "[" + lastTiming() + " ms] " + (a != null ? a : "I don't know"); } if (eqic(s, "!loaded-subspaces")) return sfu(ai_subSpaceNames()); if (eqic(s, "!subspaces")) return sfu(ai_subSpaceNamesOnDisk()); if (eqic(s, "!index-time")) return toS(indexMakingTime, 1) + " s"; if (eqic(s, "!boot-time")) return toS(bootTime, 1) + " s (" + toS(bootTime + started - vmStarted(), 1) + "s full)"; if (swic_trim(s, "!bench-search", m)) { T3 t = ai_parseArrowTriple(m.rest()); long _startTime_11 = sysNow(); int n = l(ai_search_dollarX(t)); _startTime_11 = sysNow() - _startTime_11; saveTiming(_startTime_11); return n(n, "result") + ", " + lastTiming() + " ms"; } if (swic_trim(s, "!vary ", m)) return ifEq(ai_nlVary(m.rest()), m.rest(), "Sorry, no variation found"); if (swic_trim(s, "!google ", m)) return join(" - ", swapPair(first(quickGoogle(m.unq(0))))); if (swic_trim(s, "!google-texts", m)) return lines(uniquify(map("tripleC", quickGoogle2(m.unq(0))))); if (swic_trim(s, "!google-define", m)) return lines(uniquify(map(new F1, Object>() { public Object get(T3 t) { try { return t.b + " ~ " + t.c; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "t.b + \" ~ \" + t.c"; } }, quickGoogle2(m.unq(0))))); if (swic_trim(s, "!google-complete ", m)) return lines(googleAutoComplete(m.unq(0))); if (swic_trim(s, "!antonyms ", m)) return join(", ", antonymsFromSynonymDotCom(m.unq(0))); if (swic_trim(s, "!has-triple ", m)) { T3 t = ai_parseArrowTriple(m.rest()); return yn(ai_cache_hasTriple(t)); } if (swic_trim(s, "!triple ", m) || swic_trim(s, "!triples ", m)) { List elements = trimAll(splitAt(m.rest(), "->")); if (l(elements) != 3) return "3 parts please"; return "[IMAGE] " + "http://ai1.space/1007546?triples=" + urlencode(lines(elements)); } if (swic_trim(s, "!answer ", m)) { long _startTime_12 = sysNow(); String a = ai_specReturn_answerToExternalQuestion(10.0, m.rest()); _startTime_12 = sysNow() - _startTime_12; saveTiming(_startTime_12); return "[" + lastTiming() + " ms] " + (a != null ? a : "I don't know"); } if (swic_trim(s, "!web ", m)) { String id = m.unq(0); Web web = indexedWebWithGlobalID(id); if (web == null) return "Not found"; return "[IMAGE] " + smartBotURL() + "/diagram?id=" + id; } if (swic_trim(s, "!parse ", m)) return ai_renderTriple(ai_tripelize(m.rest())); if (eqic(s, "!worst-case")) return sfu(getOptMC("ai_hasTriple_vary_worstCase")); if (eqic(s, "!uptime")) return n(secondsSinceSysTime(started), "second"); if (eqic(s, "!typical-set")) return sfu(diagramServer_typicalSet()); if (eqic(s, "!num-unverified")) return lstr(ai_unverifiedWebs()); if (eqic(s, "!num-true")) return lstr(ai_trueWebs()); if (eqic(s, "!version")) return autoRestart_localMD5(); if (swic_trim(s, "!blob ", m)) { String a = postNodeFromInput(m.rest(), s); selectWord(m.rest()); return a; } if (swic_trim(s, "!store ", m)) return postTripleFromInput(ai_tripelize(m.rest()), s); if (swic_trim(s, "!store-triple ", m)) return postTripleFromInput(ai_parseArrowTriple(m.rest()), s); if (swic(s, "!nodes ", m)) { if (dropSuffixTrim("...", m.rest(), m)) return lstr(indexedNodesStartingWith(m.rest())); return lstr(indexedNodes(m.rest())); } if (eqic(s, "!webs")) return lstr(allIndexedWebs()); if (swic(s, "!webs ", m)) return joinWithSpace(collect(indexedWebs(m.unq(0)), "globalID")); if (swic(s, "!splitAtBaseFormVerb ", m)) return sfu(splitAtBaseFormVerb(m.unq(0))); if (swic(s, "!maxRunlengthOfRepeatingChars ", m)) return str(maxRunlengthOfRepeatingChars(m.unq(0))); if (swic(s, "!collapseWord ", m)) return collapseWord(m.unq(0)); if (swic(s, "!gac ", m)) return or2(first(scoredSearch(m.get(0), keys(gac36k()))), "-"); if (eqic(s, "!vms")) return computerVMs_text(); if (eqic(s, "!id")) return aGlobalID(); if (swic_trim(s, "!count-triple ", m)) { T3 t = ai_parseArrowTriple(m.rest()); print("Searching: " + sfu(t)); Pair p = ai_countBoth_dollarX(t); return eq(p.a, p.b) ? str(p.a) : p.a + " (+" + (p.b - p.a) + " unverified)"; } try { { String _a_22 = ai_sfCommands(s); if (!empty(_a_22)) return _a_22; } } catch (Throwable __e) { _handleException(__e); } if (swic_trim(s, "!macmillan ", m)) try { Pair> p = macmillanDefinitions3(m.rest()); return toUpper(p.a) + "\n" + lines(prependAll("-", p.b)); } catch (Throwable e) { printShortException(e); return "Macmillan is quiet today."; } { String _a_23 = ai_answerFromCache(s); if (!empty(_a_23)) return _a_23; } String _s = s; s = ai_dropLeadingAdditions(s); if (neq(_s, s)) { String _a_24 = ai_answerFromCache(s); if (!empty(_a_24)) return _a_24; } if (match("cache size", s)) return n2(indexedTerms(), "different term") + ", " + nWeb(tripleIndex().numWebs()); if (matchStartX("give me subtypes of ...", s, m)) return ai_renderList(ai_subtypesOf(m.rest())); if (match("give me a third person verb", s)) return random(thirdPersonVerbs()); if (learnedFlexMatch("give me some *", s, m)) { String query = singular(m.unq(0)); return ai_renderNodesList(concatLists(ai_index_search_dollarX("$X", "is a", query), ai_index_search_dollarX("$X", "is", a(query)))); } if (match("authed?", s)) return authed() ? "You are authed." : "You are not authed."; if (match("what is the singular of *", s, m)) return or2(getSingularFromWordHippo(m.unq(0)), singular(m.unq(0))); if (learnedFlexMatch("what is the relation between * and *", s, m)) { List l = ai_getRelation(m.unq(0), m.unq(1)); if (nempty(l)) return m.unq(0) + " " + first(l) + " " + m.unq(1); } if (containsAll(asCISet(javaTokC(s)), "list", "long")) try { { String _a_25 = callAnswerMethod(hotwireOnce("#1013113"), s); if (!empty(_a_25)) return _a_25; } } catch (Throwable __e) { _handleException(__e); } if (match("what unix day is it", s)) return str(unixDay()); if (matchStartX("show me the ...", s, m)) return "What " + m.unq(0) + "?"; if (learnedFlexMatch("What do * have?", s, m)) return ai_renderNodesList(ai_index_search_dollarX(m.unq(0), "have", "$X")); if (learnedFlexMatch("What does * do?", s, m)) return ai_renderList(map("web_nodePairToText", webs_search_dollarXY(webFromTriples(m.unq(0), "$X", "$Y"), indexedWebs(m.unq(0))))); if (learnedFlexMatch_multi(s, m, "What is *", "Who is *", "Was ist *", "Wer ist *")) { String _a_26 = ai_renderList(ai_whatIs(m.unq(0)), ""); if (!empty(_a_26)) return _a_26; } String sf = web_text(first(ai_search_dollarX_verified("$X", "implements", quote(s)))); if (startsWith(sf, "sf ", m)) return str(makeAndCall(m.unq(0))); if (learnedFlexMatch("* how many visitors *", s) || match("how many visitors", s)) return str(ai1_cookiesToday_int()); if (learnedFlexMatch("What is *", s, m)) { List extendedWebs = indexedWebsAfterVariableSubstitution(m.unq(0)); print("Have " + n(extendedWebs, "extended web") + " for " + quote(m.unq(0))); { String _a_27 = ai_renderNodesList(webs_search_dollarX(webFromTriples(m.unq(0), "is", "$X"), extendedWebs), null); if (!empty(_a_27)) return _a_27; } } { String _a_28 = ai_tryIfAndInput(s); if (!empty(_a_28)) return _a_28; } { String _a_29 = ai_autoApplyRules(s); if (!empty(_a_29)) return _a_29; } if (!ai_isQuestion_1(s) && learnedFlexMatch_multi(s, m, "* is *", "* ist *")) { String _a_30 = postTripleFromInput(triple(m.get(0), "is", m.get(1)), s); if (!empty(_a_30)) return _a_30; } { String _a_31 = ai_standardAnswer(s); if (!empty(_a_31)) return _a_31; } { String _a_32 = ai_standardAnswer_dollarVars(s); if (!empty(_a_32)) return _a_32; } { String _a_33 = ai_standardAnswerEval(s); if (!empty(_a_33)) return _a_33; } try { final T3 t = ai_tripelize(s); if (t != null) { { String _a_34 = evalWithTimeoutOrNull(20.0, new F0() { public String get() { try { for (Object f : tripleAnswerFunctions) { String _a_35 = (String) pcallF(f, t); if (!empty(_a_35)) return _a_35; } return null; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "for (O f : tripleAnswerFunctions)\r\n try answer (S) pcallF(f, t);\r\n ..."; } }); if (!empty(_a_34)) return _a_34; } { String _a_36 = ai_standardAnswerSF_triple_dollarVars(t); if (!empty(_a_36)) return _a_36; } } } catch (Throwable __e) { _handleException(__e); } String a = ai_specReturn_answerToExternalQuestion(10.0, s); if (nempty(a) && !swicOneOf(a, "[", "What is ")) return a; return null; } public static void loadBots(String... botIDs) { for (String id : botIDs) try { thoughtBots.add(runSubBot(id)); } catch (Throwable __e) { _handleException(__e); } } public static Object html(String uri, Map params) { profilePoint("html_1"); String ip = clientIP(); boolean bad = eq(uri, "/simulate-bad-client"); Pair pair = simpleSpamClientDetect2(ip, uri); if (pair.b) { if (eq(ip, pcall_myFirewallIP())) print("Not blocking myself."); else { print("Blocking IP " + ip); blockedIPs.add(ip); } } if (blockedIPs.contains(ip)) { bad = true; post_verified(print(t3("an HTTP request from a blocked IP", "came in at", dateWithTimeGMT()))); } if (bad) { NanoHTTPD.IHTTPSession httpSession = NanoHTTPD.currentSession.get(); if (httpSession == null) { print("No HTML session?"); sleepSeconds(spammerSleep); return "go away"; } httpSession.badClient(true); return null; } print(gmtWithSeconds() + ": Serving HTTP " + quote(uri) + " to " + ip + " (anti-spam count: " + pair.a + "/" + simpleSpamClientDetect2_spamMax + ")"); profilePoint("html_2"); String requestID = aGlobalID(); String symbol = "HTTP request " + requestID; if (http_isBot()) symbol = "Bot " + symbol; { AutoCloseable __3 = temp_ai_startBuffering(); try { post_verified(symbol, "came in at", dateWithTimeGMT()); post_verified(symbol, "was for path", uri); post_verified(symbol, "had user agent", serveHttp_userAgent()); post_verified(symbol, "came from IP", ip); } finally { _close(__3); } } profilePoint("html_3"); String cookie = serveHttp_cookieHandling(); String loginUrl = "/"; String pw = params.get("pw"); Pair p = standaloneWebAuth(loginUrl, cookie, pw); print("ip=" + ip + ", cookie=" + l(cookie) + ", authed: " + p.b); if (nempty(p.a)) return p.a; if (eq(uri, "/login")) return standaloneWebAuth_showForm(loginUrl, cookie); httpAuthed.set(p.b); try { long time = sysNow(); Object html = html_2(uri, params); done2(time, "Making HTML " + uri); return html; } finally { httpAuthed.set(false); } } public static Object html_2(String s, Map params) { Matches m = new Matches(); String domain = serveHttp_domainName(); if (ewic(domain, ".supersrv.de")) return "Super server!"; if (eqic(s, "/favicon.ico")) return serveFile(loadLibrary("#1013028"), "image/x-icon"); if (nempty(params.get("_pretendAuthed")) || authed()) html_pretendAuthed.set(true); if (eqic(s, "/number-of-threads")) return str(cachedNumberOfThreads.get()); if (eqic(s, "/realurl")) return hrefresh(smartBotDirectURL()); if (swic(domain, "pixelboys.") || eqic(domain, "www.botcompany.de")) return hrefresh("https://www.botcompany.de:8443/1016323/raw"); String input = params.get("input"); if (nempty(input)) { if (webAuthed()) return htmlencode(callAnswerMethodVerbose(input)); else return "Can't send input (not authed)"; } if (swic(s, "/matrix/", m)) { s = "/dyn/" + psI("#1013031"); if (nempty(m.rest())) params.put("topic", urldecode(m.rest())); } if (eqicOptSlash(s, "/rules")) s = "/dyn/1013529/ai_rulesTable"; if (eqicOptSlash(s, "/livecount")) s = "/dyn/1013377"; if (eqic(s, "/dyn/fresh")) { if (!webAuthed()) return "No"; dynClear(); return "OK"; } if (swic(s, "/dyn/", m)) { if (loading()) return "LOADING, please try again"; String u = m.rest(); int i = smartIndexOf(u, '/'); String id = fsI(takeFirst(u, i)); if (!has_verified(id, "is", "a safe web module")) return "Unsafe"; Object mod = hotwireCached(id); return callHtmlMethod2(mod, or2(substring(u, i), "/"), params); } if (swic(s, "/sf/", m)) return htmlencode2(struct(callSafeSF(m.rest()))); if (match("random-web", s)) s = str(randomIndexedTriple().globalID()); try { if (match("threads", s)) { long _startTime_13 = sysNow(); String text = renderRunnableThreadsWithStackTraces(); _startTime_13 = sysNow() - _startTime_13; saveTiming(_startTime_13); return hpre(text + "\n\n" + lastTiming() + " ms"); } if (eqic(s, "/livecheck")) return "Smart Bot"; if (eqic(s, "/load")) return formatDouble(systemLoad(), 2); if (eqic(s, "/1-sec-profile")) return html_profile(1); if (eqic(s, "/10-sec-profile")) return html_profile(10); if (eqic(s, "/deadlocks")) return htmlencode(solveDeadlocks()); if (eqic(s, "/source-stats")) return h1_title("Source Stats") + multiSetToUL(ai_sourceStats()); if (match("thoughts", s)) return serveHTML(html_thoughts()); if (match("diagram", s)) { String id = params.get("id"); Web web = indexedWebWithGlobalID(id); return serveJPEG(webToCAL(web).makeImage(600, 400)); } if (match("log", s)) return serveText_direct(printLog()); if (match("learners", s)) return serveText_direct(renderConcepts(list(MatchLearner.class))); if (match("unreached", s)) return h3_title("Unreached") + ul(map("html_encyclopediaTopic", ai_gc_unreachedWords())); s = dropPrefix("/", s); if (possibleGlobalID(s)) { List webs = ai_getWebs(s); if (nempty(webs)) { for (Web web : webs) ai_speculateOnWeb(web); Web web = first(webs); return h2_title("Web " + s + (ai_isInvalidWeb(web) ? " [INVALID]" : "")) + loadjquery() + pre(htmlencode(renderWeb_multiLine(web))) + p(himg(ai_webImage_link(s), "title", "Web " + s)) + (l(webs) > 1 ? p("Warning: Multiple webs") : "") + ai_html_wordThoughts("Web " + web.globalID); } return serveHTML("Unknown: " + s); } if (swic(s, "json/", m)) { s = m.rest(); if (swic(s, "relations/", m)) { List out = new ArrayList(); Map map = litorderedmap(); if (loading()) map.put("loading", true); int max = 1000; for (WebRelation r : indexedRelations(m.rest())) if (l(out) >= max) { map.put("more", true); break; } else { List l = tripleToList(web_relationToTriple(r)); l.add(r.web.globalID()); l.add(r.web.source); if (r.web.verified()) l.add("v"); out.add(l); } map.put("data", out); return serveText(jsonEncode(map)); } return "unknown"; } if (swic(s, "e/", m)) { String topic = urldecode(m.rest()); return html_topic(topic, eq("1", params.get("random"))); } if (swic(s, "autocomplete/", m)) { int max = min(100, parseInt(or2(params.get("n"), "10"))); List completed = ai_autoComplete(urldecode(m.rest()), max, autoComplete_timeOut); List out = new ArrayList(); for (String c : completed) { HashMap map = new HashMap(); if (startsWith(s, c)) map.put("follow", substring(c, l(s))); else map.put("whole", c); out.add(map); } return serveText(jsonEncode_breakAtLevel1(out)); } if (eqic(s, "alphabetical")) return html_alphabetical(params); if (eqic(s, "latest-webs")) { int n = toInt(params.get("n")); if (n == 0) n = 100; return html_latestWebs(min(1000, max(n, 10))); } if (eqic(s, "the-big-number")) return formatWithThousandsSeparator(ai_allWebsCount()); if (eqic(s, "random")) return hrefresh(smartBot_encyclopediaLink(ai_randomString()) + "?random=1"); if (eqic(s, "classes")) return html_classes(); if (eqic(s, "standard-relations")) return html_standardRelations(); if (eqic(s, "active")) return html_active(); if (eqic(s, "all-web-ids")) { List l = allToString(allWebIDs_cloned()); return serveText(l(l) + " ids follow.\n" + lines(l) + l(l) + " ids written."); } if (eqic(s, "internal-files")) return serveText(lines(map(new F1() { public String get(File f) { try { return quote(f.getName()) + " " + fileSize(f) + " " + fileModificationTime(f); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "quote(f.getName()) + \" \" + fileSize(f) + \" \" + fileModificationTime(f)"; } }, listFiles(programDir())))); if (swic(s, "internal-files/", m)) { String name = urldecode(m.rest()); if (!isProperFileName(name)) return serve404("No"); File f = getProgramFile(name); return f.isFile() ? serveFile(f) : serve404(); } if (eqic(s, "submit-triple")) { String a = params.get("a"), b = params.get("b"), c = params.get("c"); if (empty(a) || empty(b) || empty(c)) return "Empty field(s)"; return htmlencode(postTripleFromInput(t3(a, b, c), null)); } if (swic_trim(s, "texts/", m)) { return serveText((loading() ? "[LOADING]\n" : "\n") + lines(map("quoteBorderless2", ai_texts(ai_parseSlashTriple(urldecode(m.rest())))))); } T3 texts_big_t = null; if (swic_trim(s, "texts-big/", m)) texts_big_t = ai_parseSlashTriple(urldecode(m.rest())); if (eqic(s, "texts-big")) { texts_big_t = t3(params.get("a"), params.get("b"), params.get("c")); } if (swic_trim(s, "rev/", m)) texts_big_t = triple("$X", "is", urldecode(m.rest())); if (texts_big_t != null) { final boolean strip = eq("1", params.get("strip")); final String style = or2(params.get("style"), "font-size: 20px"); T3 t = texts_big_t; List> refs = ai_tripleRefs(t); return htitle_htmlencode(ai_renderTriple(t)) + (loading() ? "[LOADING]\n" : "\n") + joinMap(refs, new F1, String>() { public String get(TripleRef r) { try { TripleWeb w = (TripleWeb) (r.triple); String title = "Web " + w.globalID(); String source = w.source(); if (nempty(source)) title += ", source: " + source; String text = r.get(); if (strip) text = unquoteMultiline(text); return pre(htmlencode_noQuote(text), "style", style, "title", title); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "TripleWeb w = (TripleWeb) ( r.triple);\r\n S title = \"Web \" + w.globalID();\r\n ..."; } }); } String q = params.get("q"); if (nempty(q)) return html_searchResults(q, params.get("mode")); if (eqic(s, "popular")) return html_popular(params); String topic = nempty(s) ? s : params.get("topic"); if (nempty(topic)) return html_topic(urldecode(topic), false); return html_home(); } finally { html_pretendAuthed.set(null); } } public static NanoHTTPD.Response html_topic(String topic, boolean random) { long time; boolean timeout = false; print("User agent: " + serveHttp_userAgent()); if (http_isBot()) { time = -1; print("Bot request"); } else { long _startTime_14 = sysNow(); timeout = ai_speculateDeepWithActiveAndTimeout(topic, speculationTimeoutForHttp); _startTime_14 = sysNow() - _startTime_14; saveTiming(_startTime_14); time = lastTiming(); } List words = ll(topic); return serveHTMLNoCache(htitle(topic + " | Smart Bot") + h1(html_smallSmartBotLogo() + " " + htmlencode_noQuote("Topic: " + topic)) + loadjquery() + (random ? ahref("/random", "Another random term") + " | " : "") + "Search: " + html_searchField(topic) + "

" + ai_html_wordThoughts(words) + ai_html_enterTripleForm(first(words)) + p((time < 0 ? "" : "Speculated for " + time + " ms" + (timeout ? " (TIMED OUT)" : "") + " ") + ahref(smartBot_directTopicLink(topic), "[Direct link]"))); } public static String html_home() { AutoCloseable __4 = tempProfileBlock("html_home"); try { return tag("link", "", "rel", "search", "type", "application/opensearchdescription+xml", "title", "Smart Bot", "href", "http://tinybrain.de:8080/getraw.php?id=1013357") + hcenter3(hsnippetimg("#1101146", "width", 64, "height", 64) + h1_title("Smart Bot") + p("by " + ahref("http://BotCompany.de/", "BotCompany.de") + (webAuthed() ? " Authed." : "")) + ahref("/popular", "Most occurring") + " | " + ahref("/alphabetical", "Alphabetical") + " | " + ahref("/latest-webs", "Latest") + " | " + ahref("/random", "Random term") + " | " + html_searchField() + "

" + h3("Triple Search") + form(htextfield("a") + " -> " + htextfield("b") + " -> " + htextfield("c", "$X") + " " + hsubmit("Search triples"), "action", "texts-big")); } finally { _close(__4); } } public static String html_popular(Map params) { long _startTime_17 = sysNow(); List keys = multiSetByPopularity(tripleIndex().wordIndex); done2_always("Popularity search", _startTime_17); int step = 100, n = toInt(params.get("n")); int count = l(keys); List l = subList(keys, n, n + step); return hcenter3(hsnippetimg("#1011942")) + h1_title("Smart Bot's Encyclopedia (" + n_fancy(keys, "entry") + ")") + "Most occurring | " + ahref("/alphabetical", "Alphabetical") + " | " + ahref("/latest-webs", "Latest") + " | " + html_searchField() + "

" + pageNav2("/", count, n, step, "n") + ul(map(new F1() { public String get(String s) { try { return ahref(smartBot_encyclopediaLink(s), htmlencode(or2(s, "-"))) + " [" + ai_approximateIndexedNodesCount(s) + "]"; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ahref(smartBot_encyclopediaLink(s), htmlencode(or2(s, \"-\")))\r\n + \" [\" ..."; } }, l)); } public static String html_searchField() { return html_searchField(""); } public static String html_searchField(String q) { return html_searchField(q, ""); } public static String html_searchField(String q, String mode) { return hform(htextinput("q", "value", q, "autofocus", "autofocus") + " Mode: " + hselect_list(ll("starts with", "multiple words", "ends with", "contains"), mode, "name", "mode") + " " + hsubmit("Search"), "style", "display: inline", "action", smartBotOfficialURL()); } public static Object html_profile(int seconds) { if (poorMansProfiling_isOn()) return "Already on"; poorMansProfiling(100); sleepSeconds(seconds); return serveText(poorMansProfiling_stopAndRenderResults()); } public static String html_searchResults(String q, String mode) { int max = 1000; long _startTime_15 = sysNow(); List l; if (eqic(mode, "multiple words")) l = indexedTerms_scoredSearch(q, max + 1); else if (eqic(mode, "ends with")) l = asList(termsEndingWith(q)); else if (eqic(mode, "contains")) l = asList(indexedTermsContaining(q)); else l = asList(fullIndexedTermsStartingWith(q)); if (possibleGlobalID(q) && !eq(first(l), q)) l.add(0, q); _startTime_15 = sysNow() - _startTime_15; saveTiming(_startTime_15); long time = lastTiming(); boolean more = l(l) > max; l = takeFirst(max, l); sortInPlaceAlternativeCI(l); String directHit = ""; String trimmed = trim(q); String direct = ai_findIndexedTerm(trimmed); if (direct != null) directHit = p(unicode_blackRightArrow() + " " + b(html_encyclopediaTopic(direct))); else directHit = p("No direct hit. Create entry: " + b(html_encyclopediaTopic(trimmed))); return h1_title("Smart Bot Search: " + htmlencode(q) + " (" + n_fancy(l, "result") + (loading() ? ", LOADING" : "") + ")") + "Search again: " + html_searchField(q, mode) + "

" + directHit + p(l(l) > max ? max + "+ search results" : n(l, "search result") + " [" + time + " ms]") + ul(map("html_encyclopediaTopic", l)); } public static String html_alphabetical(Map params) { int step = 100, n = toInt(params.get("n")); Collection all = indexedTerms(); int count = l(all); List l = subListOfCollection(all, n, n + step); return h1_title("Smart Bot's Encyclopedia (Alphabetical) :)") + ahref("/popular", "Most occurring") + " | " + "Alphabetical" + " | " + ahref("/latest-webs", "Latest") + " | " + html_searchField() + "

" + pageNav2("/alphabetical", count, n, step, "n") + ul(map(new F1() { public String get(String s) { try { return ahref(smartBot_encyclopediaLink(s), htmlencode(or2(s, "-"))); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ahref(smartBot_encyclopediaLink(s), htmlencode(or2(s, \"-\")))"; } }, l)); } public static String html_classes() { List l = sortedIC(ai_texts_verified("$X", "is", "a standard class")); return h1_title("Smart Bot Classes") + ul(map(new F1() { public String get(String s) { try { return ahref(smartBot_encyclopediaLink(s), htmlencode(or2(s, "-"))) + " [" + ai_approximateIndexedNodesCount(s) + "]"; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ahref(smartBot_encyclopediaLink(s), htmlencode(or2(s, \"-\")))\r\n + \" [\" ..."; } }, l)); } public static String html_standardRelations() { List l = sortedIC(unquoteAll(ai_texts_verified("$X", "is", "a standard relation"))); return h1_title("Smart Bot Standard Relations") + ul(map(new F1() { public String get(String s) { try { return ahref(smartBot_encyclopediaLink(s), htmlencode(or2(s, "-"))) + " [" + ai_approximateIndexedNodesCount(s) + "]"; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ahref(smartBot_encyclopediaLink(s), htmlencode(or2(s, \"-\")))\r\n + \" [\" ..."; } }, l)); } public static String html_active() { List l = sortedIC(unquoteAll(ai_texts_verified("$X", "is", "an active topic"))); List lRules = sortedIC(unquoteAll(ai_texts_verified("$X", "is", "an active rule"))); return h1_title("Smart Bot's Active Topics") + ul(map(new F1() { public String get(String s) { try { return ahref(smartBot_encyclopediaLink(s), htmlencode(or2(s, "-"))) + " [" + ai_approximateIndexedNodesCount(s) + "]"; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ahref(smartBot_encyclopediaLink(s), htmlencode(or2(s, \"-\")))\r\n + \" [\" ..."; } }, l)) + h2("Active Rules") + ul(map(new F1() { public String get(String s) { try { return ahref(smartBot_encyclopediaLink(s), htmlencode(or2(s, "-"))) + " [" + ai_approximateIndexedNodesCount(s) + "]"; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ahref(smartBot_encyclopediaLink(s), htmlencode(or2(s, \"-\")))\r\n + \" [\" ..."; } }, lRules)); } public static String html_latestWebs(int n) { List l = takeLast(n, cloneList(latestWebs)); String bn = formatWithThousands(ai_theBigNumber()); return htitle("Latest Webs (" + bn + ") | Smart Bot") + h1(html_smallSmartBotLogo() + " - Latest Webs from " + bn) + ul(mapReversed(new F1() { public String get(GlobalID id) { try { TripleWeb w = ai_getTriple(id); if (w == null) return "[not found] Web " + id; return ai_html_linkedWeb(id) + htmlencode(" [" + renderGMTDate(w.created()) + "] " + w.source() + ": ") + ai_html_renderTriple(w); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "TripleWeb w = ai_getTriple(id);\r\n if (w == null) ret \"[not found] Web \" ..."; } }, l)); } public static String html_thoughts() { String html = hGoogleFontOswald() + loadjquery(); String status = ""; Pair, Set> p = evalWithTimeout_calculations(); if (nempty(p.a) || nempty(p.b)) status = ul(nempty(p.a) ? "In time calculations" + ul_htmlEncode(map("threadName", p.a)) : null, nempty(p.b) ? "Timed out calculations" + ul_htmlEncode(map("threadName", p.b)) : null); ai_html_wordThoughts_nodesMax.set(100); try { return html + wordThoughts() + status; } finally { ai_html_wordThoughts_nodesMax.set(null); } } public static String wordThoughts() { try { if (empty(selectedWord)) return ""; String html = ai_html_wordThoughts(selectedWord); html = html_addLinkToSpan(html, "title", encyclopediaLink(selectedWord)); return html_addTargetBlank(html); } catch (Throwable e) { printStackTrace(e); return "Erreur"; } } public static String postTripleFromInput(T3 triple, String input) { if (triple == null) return null; if (swic(input, "OK, stored")) return null; if (!ai_tripleAllowedToPost(triple)) return null; Matches m = new Matches(); if (!(swic(triple.a, "Web ", m) && possibleGlobalID(m.rest()))) selectWord(triple.a); if (ai_cache_hasTriple(triple)) return "I know"; else { Web web = webFromTriple(triple); web.unverified = !authed(); postSoftwareMadeWeb(web, "input", input); if (eqic(triple.b, "is") && eqic(triple.c, "invalid")) ai_invalidateWeb(triple.a); if (eqic(triple.b, "is") && eqic(triple.c, "wrong")) ai_wrongWeb(triple.a); ai_postTriple("Web " + web.globalID, "stored because of", chatLineSymbol()); return "OK, stored" + (web.unverified ? " (unverified)" : "") + ": " + ai_renderTriple(triple); } } public static String postNodeFromInput(String node, String input) { if (!ai_nodeNameAllowedToPost(node)) return null; if (hasIndexedNode(node)) return "I know"; else { Web web = oneNodeWeb(node); web.unverified = !authed(); postSoftwareMadeWeb(web, "input", input); return "OK, stored" + (web.unverified ? " (unverified)" : "") + ": " + node; } } public static void processSelectedWord() { final String word = selectedWord; if (empty(word)) return; ai_withMaker("processSelectedWord", new Runnable() { public void run() { try { try { ai_greetingRule1(word); if (ai_hasTriple(word, "should be", "answered by me") && ai_postTriple(word, "was", "answered by me") != null) { String text = firstQuoted(web_texts(ai_search_dollarX(word, "is", "$X"))); postToStefansChat((nempty(text) ? text + " << " : "") + "Greetings back to you!"); } } catch (Throwable __e) { _handleException(__e); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "//ai_speculate(word);\r\n pcall {\r\n ai_greetingRule1(word);\r\n //ai..."; } }); } public static void makeChatLinesUnrecent() { Matches m = new Matches(); for (WebNode node : ai_search_dollarX("$X", "is", "recent")) if (web_match("Chat line *", node, m)) try { int n = parseInt(m.unq(0)); if (n <= stefansChat_n_value - 100) { ai_invalidateWeb(node.web); } } catch (Throwable __e) { _handleException(__e); } else if (web_match("Smart Bot's Chat line *", node, m)) try { int n = parseInt(m.unq(0)); if (n <= smartBotsChat_n_value - 100) { ai_invalidateWeb(node.web); } } catch (Throwable __e) { _handleException(__e); } } public static void onHistoryRead() { Lock __2 = dbLock(); lock(__2); try { print("History read."); processSelectedWord(); try { makeChatLinesUnrecent(); } catch (Throwable __e) { _handleException(__e); } } finally { unlock(__2); } } public static String selectWord(String word) { if (nempty(word)) { selectedWord = word; save("selectedWord"); processSelectedWord(); } return word; } public static String transpileMyself(String mode) { postToStefansChat("Transpiling..."); Pair p = transpileOnServer(programID(), "medium"); return p.a ? "OK" : "Not OK"; } public static void speculatorLoop() { while (licensed()) { try { ai_speculateDeepAndActive(selectedWord); for (List words : cloneValues(lastUserLines)) for (String word : words) ai_speculateDeepAndActive(word); String s; while ((s = first_sync(speculationQueue)) != null) { speculationQueue.remove(s); long time = sysNow(); ai_speculateWithActive(s); done2_always(time, "Speculation Queue > " + s); } } catch (Throwable __e) { _handleException(__e); } sleepSeconds(5); } } public static ISpec ispec = ai_defaultISpec(); public static JavaXClassLoader hotwire_makeClassLoader(List files) { ClassLoader cl = myClassLoader(); if (cloned && cloningSince != 0) { ClassLoader parent = (ClassLoader) (getOpt(cl, "virtualParent")); print("Cloned class loader. " + parent); if (parent != null) cl = parent; } return new JavaXClassLoaderWithParent2(null, files, cl, parseClassesToShareList(classesToShare)); } public static Lock aiLock() { return aiLock; } public static boolean webAuthed() { return isTrue(httpAuthed.get()); } public static void handleStefansChat() { stefansChat_n_onChange(new Runnable() { public void run() { try { clearTransientWebs(); addTransientWeb(webFromTriple("Latest chat line", "is", "Chat line " + stefansChat_n_value)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "clearTransientWebs();\r\n addTransientWeb(webFromTriple(\"Latest chat line\", ..."; } }); stefansChat_onLine_lookback = 1; stefansChat_onLine_onHistoryRead.set("onHistoryRead"); stefansChat_onLine_safe(new VF1() { public void get(final String text) { try { try { int nr = toInt(mapGet(stefansChat_onLine_fullParams.get(), "nr")); print("> [" + nr + "] " + text); if (nr != 0) { String x = "Chat line " + nr; stefansChat_onLine_fullParams.get().put("chatName", "Stefan's Chat"); stefansChat_onLine_fullParams.get().put("Chat line symbol", x); post(x, "is", "a chat line"); post(x, "is", "recent"); postChatLineInfo(x, text); stefansChat_n_notify(nr); ai_speculateWithActiveAndTimeout(x, speculationTimeoutForNewChatLine); if (!byBot()) lastUserLines.put("Stefan's Chat", ll(text, quote(text), x)); } } catch (Throwable __e) { _handleException(__e); } AutoCloseable __5 = tempSetThreadLocal(answering, true); try { try { String a = actualAnswer(text); a = shortenAnswerForChatAndUploadFull(text, a); postToStefansChat(a); callAll(getAndClearThreadLocal(afterAnswering)); } catch (Throwable _e) { clearThreadLocal(afterAnswering); throw rethrow(_e); } } finally { _close(__5); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pcall {\r\n int nr = toInt(mapGet(stefansChat_onLine_fullParams!, \"nr\"));\r..."; } }); } public static void handleSmartBotsChat() { smartBotsChat_onLine_lookback = 1; smartBotsChat_onLine_onHistoryRead.set("onHistoryRead"); smartBotsChat_onLine_safe(new VF1() { public void get(final String text) { try { try { int nr = toInt(mapGet(stefansChat_onLine_fullParams.get(), "nr")); print("> [" + nr + "] " + text); if (nr != 0) { String x = "Smart Bot's Chat line " + nr; stefansChat_onLine_fullParams.get().put("chatName", "Smart Bot's Chat"); stefansChat_onLine_fullParams.get().put("Chat line symbol", x); post(x, "is", "a Smart Bot's chat line"); post(x, "is", "recent"); postChatLineInfo(x, text); smartBotsChat_n_notify(nr); ai_speculateWithActiveAndTimeout(x, speculationTimeoutForNewChatLine); if (!byBot()) lastUserLines.put("Smart Bot's Chat", ll(text, quote(text), x)); } } catch (Throwable __e) { _handleException(__e); } AutoCloseable __6 = tempSetThreadLocal(answering, true); try { try { String a = actualAnswer(text); a = shortenAnswerForChatAndUploadFull(text, a); postToSmartBotsChat(a); callAll(getAndClearThreadLocal(afterAnswering)); } catch (Throwable _e) { clearThreadLocal(afterAnswering); throw rethrow(_e); } } finally { _close(__6); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pcall {\r\n int nr = toInt(mapGet(stefansChat_onLine_fullParams!, \"nr\"));\r..."; } }); } public static String handleArchiveMsg(String msgSymbol) { String text = firstQuoted(ai_texts(msgSymbol, "is", "$X")); if (text == null) return "No text found for " + msgSymbol; text = unquote(text); Map oldParams = setThreadLocal(stefansChat_onLine_fullParams, lithashmap("Chat line symbol", msgSymbol)); try { return actualAnswer(text); } finally { stefansChat_onLine_fullParams.set(oldParams); } } public static String shortenAnswerForChatAndUploadFull(String text, String a) { a = shorten(a, ultimateAnswerLimit); if (l(a) > stefansChat_messageLimit()) { String title = "Smart Bot's answer to: " + text; String id = ntUpload("smart-bot-for-user", title, unSlackSnippet(a)); a = snippetURL(id) + " | " + a; } return a; } public static boolean ai_enabled() { return true; } public static boolean http_isBot() { return userAgentIsBot(serveHttp_userAgent()); } public static void softRestart_level1() { startedLoading(); cloningSince = sysNow(); classesToShare = "T3, TripleWeb, Symbol, GlobalID, MultiSet"; Class c = hotwire(programID()); set(c, "ai_fillTripleIndex_useThese", asList(ai_allTriples())); softRestart_phase2(c); } public static void softRestart_phase2(final Class c) { print("Cloning phase 2"); set(c, "cloned", true); set(c, "clonedSince", cloningSince); set(c, "postStartUpMessageToChat", chatName()); addAll((List) getOpt(c, "latestWebs"), latestWebs); removeFirstInjection(); afterAnswering(new Runnable() { public void run() { try { print("Cloning, new thread"); copyFields(mc(), c, "transpileRaw_trans"); transpileRaw_trans = null; cleanUp(mc()); System.out.println("Cloning: Cleaned up"); clearAllVariables(); System.out.println("Cloning: Running main"); runMain(c); System.out.println("Cloning: Done!"); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "// Create raw thread without any registering/pinging etc.\r\n //new Thread(\"..."; } }); } public static void softRestart_level2() { startedLoading(); cloningSince = sysNow(); classesToShare = smartBot_sharedClassesForRestart(); Class c = hotwire(programID()); set(c, "ai_fillTripleIndex_useTripleIndex", tripleIndex()); softRestart_phase2(c); } public static void afterAnswering(Object runnable) { if (isTrue(answering.get())) afterAnswering.set(addToOrCreateList(afterAnswering.get(), runnable)); else callF(runnable); } public static void addToLatestWebs(Web web) { latestWebs.add(web.globalIDObj()); } public static void dynClear() { veryQuickJava_refresh(); hotwireCached_clear(); clearAllCaches(); } public static void postChatLineInfo(String x, String text) { post(x, "has text", quote(text)); post(x, "was", byBot() ? "sent by bot" : "sent by human"); post(x, "was sent by IP", (String) lookupPossiblyIgnoreCase(stefansChat_onLine_fullParams.get(), "ip")); } public static WebSocket makeWebSocket(NanoHTTPD.IHTTPSession handshake) { print("New WebSocket."); return new WebSocket(handshake) { public AutoCloseable streamer; @Override public void onMessage(WebSocketFrame messageFrame) { String s = messageFrame.getTextPayload(); print("WebSocket msg: " + s); if (streamer == null && eq(s, "stream-big-number")) { streamer = timerAsAutocloseable(doEveryStartingNow(100, new Runnable() { public String lastS = null; public void run() { try { String s = formatWithThousands(ai_theBigNumber()); if (neq(s, lastS)) { lastS = s; send(s); } } catch (Exception __e) { throw rethrow(__e); } } })); println("Streaming!"); } } public void onClose(WebSocketFrame.CloseCode code, String reason, boolean initiatedByRemote) { print("WebSocket close"); _close(streamer); streamer = null; } }; } public static String diskSpaceInfo() { return toM(fileSize(programFile("triples.gz"))) + " MB (triples.gz) + " + toM(fileSize(programFile("webs-made.txt"))) + " MB (webs-made.txt). " + toM(userDir().getUsableSpace()) + " MB free disk space"; } public static List ai_possibleRuleNames() { return filter(ellipsisTerms("the ... rule"), new F1() { public Object get(String s) { try { return !swic(s, "the text of ") && !tok_containsSimpleArrow(s) && !has(quote(s), "is", "not a rule name"); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "!swic(s, \"the text of \") && !tok_containsSimpleArrow(s)\r\n && !has(quote(s)..."; } }); } public static int numberOfAllThreads() { return l(Thread.getAllStackTraces()); } public static String ai_tripleAnswer_regexpMatches(T3 t) { Matches m1 = new Matches(); Matches m2 = new Matches(); if (eqic(t.b, "matches?") && match("the regexp *", t.a, m1) && match("the string *", t.c, m2) && isQuoted(m1.get(0)) && isQuoted(m2.get(0))) return yn(safeRegexp(m1.unq(0), m2.unq(0)).matches()); return null; } public static void ai_spec_moveToMiddle(String term) { List mtmRels = ai_spec_moveToMiddle_mtmRels(); for (Web web : indexedWebs(term)) { ping(); ai_spec_moveToMiddle(web, mtmRels); } } public static void ai_spec_moveToMiddle(Web web) { ai_spec_moveToMiddle(web, ai_spec_moveToMiddle_mtmRels()); } public static void ai_spec_moveToMiddle(Web web, List mtmRels) { for (WebRelation r : web_relationObjects(web)) { String rel = web_text(r), b = web_text(r.b); for (WebRelation mtm : mtmRels) { List l = splitAtSlash_keepAll(web_text(mtm.a)); List lReplacement = splitAtSlash_keepAll(web_text(mtm.b)); { if (!(l(l) == 2 && l(lReplacement) == 2)) continue; } Matches m = new Matches(); String prefix = second(l) + " "; { if (!(eqic(first(l), rel) && swic(b, prefix, m))) continue; } ai_postTriple(web_text(r.a), first(lReplacement), second(lReplacement) + " " + m.rest()); } } } public static List ai_spec_moveToMiddle_mtmRels() { return indexedRelations_verified("move to middle"); } public static int ai_idle_matchPatterns() { String previousMaker = ai_setMaker("ai_idle_matchPatterns"); try { int websMade = 0; List patterns = ai_index_search_dollarX_quoted("$X", "is", "a pattern"); print("Have " + n_fancy(patterns, "pattern")); for (WebNode nPat : patterns) { ping(); String pattern = web_unquote(nPat); List tokPat = javaTok(pattern); List tokens = listWithout(codeTokens(tokPat), "*"); print("Processing: " + pattern); for (String sKey : allIndexedTerms()) { ping(); List l = uniquify(ai_expandShortenedKeys(sKey)); for (String sOrig : l) { ping(); String s = unquote(sOrig); if (eq(s, pattern)) continue; List tok = javaTok(s); if (tok.contains("*")) continue; if (containsAll(tok, tokens)) if (flexMatchIC2(tokPat, tok, null)) { if (ai_postTriple(web_text(nPat), "matches", sOrig) != null) ++websMade; } } } } return websMade; } finally { ai_setMaker(previousMaker); } } public static C tripleC(T3 t) { return t == null ? null : t.c; } public static String web_nodePairToText(Pair p) { return p == null ? "-" : web_text(p.a) + " " + web_text(p.b); } public static String html_encyclopediaTopic(String topic) { String text = or2(ai_html_shortenTextForDisplay(topic), ""); return ahref(encyclopediaLink(topic), htmlencode_noQuotes(text), "style", dottedStyleForLinks() + "; color: inherit"); } public static String quoteBorderless2(Object o) { return quoteBorderless_less(o); } public static String quoteBorderless2(String s) { return quoteBorderless_less(s); } public static String threadName(Thread t) { return t == null ? null : t.getName(); } public static void lock(Lock lock) { try { ping(); if (lock == null) return; try { lock.lockInterruptibly(); } catch (InterruptedException e) { print("Locking interrupted! I probably deadlocked, oops."); printStackTrace(e); rethrow(e); } ping(); } catch (Exception __e) { throw rethrow(__e); } } public static void lock(Lock lock, String msg) { print("Locking: " + msg); lock(lock); } public static void lock(Lock lock, String msg, long timeout) { print("Locking: " + msg); lockOrFail(lock, timeout); } public static ReentrantLock lock() { return fairLock(); } public static Set synchroHashSet() { return Collections.synchronizedSet(new HashSet()); } public static String smartBot_sharedClasses() { return smartBot_sharedClassesForRestart() + "\r\n ISpec, AI_SubSpace, GlobalID, VirtualNodeIndex, TripleIndex,\r\n Matches, TripleRef, TripleA, TripleB, TripleC,\r\n AbstractThoughtSpace, GlobalThoughtSpace, ActualThoughtSpace\r\n "; } public static Map syncCIMap() { return (Map) synchroMap(caseInsensitiveMap()); } public static List synchroList() { return Collections.synchronizedList(new ArrayList()); } public static List synchroList(List l) { return Collections.synchronizedList(l); } public static List ll(A... a) { ArrayList l = new ArrayList(a.length); for (A x : a) l.add(x); return l; } public static volatile StringBuffer local_log = new StringBuffer(); public static volatile StringBuffer print_log = local_log; public static volatile int print_log_max = 1024 * 1024; public static volatile int local_log_max = 100 * 1024; public static boolean print_silent; public static Object print_byThread_lock = new Object(); public static volatile ThreadLocal print_byThread; public static volatile Object print_allThreads; public static void print() { print(""); } public static A print(String s, A o) { print((endsWithLetterOrDigit(s) ? s + ": " : s) + o); return o; } public static A print(A o) { ping(); if (print_silent) return o; String s = String.valueOf(o) + "\n"; print_noNewLine(s); return o; } public static void print_noNewLine(String s) { Object f = print_byThread == null ? null : print_byThread.get(); if (f == null) f = print_allThreads; if (f != null) if (isFalse(f instanceof F1 ? ((F1) f).get(s) : callF(f, s))) return; print_raw(s); } public static void print_raw(String s) { s = fixNewLines(s); StringBuffer loc = local_log; StringBuffer buf = print_log; int loc_max = print_log_max; if (buf != loc && buf != null) { print_append(buf, s, print_log_max); loc_max = local_log_max; } if (loc != null) print_append(loc, s, loc_max); System.out.print(s); } public static long sysNow() { return System.nanoTime() / 1000000; } public static void setGCFrequency(double seconds) { if (!_inCore()) { call(javax(), "setGCFrequency", seconds); return; } Lock __73 = generalLock(); lock(__73); try { cancelTimer(regularGC_timer); regularGC_timer = null; regularGC_interval = regularGC_firstDelay = toInt(toMS(seconds)); regularGC(); } finally { unlock(__73); } } public static void updateJavaXAndRestart() { upgradeJavaXAndRestart(); } public static FixedRateTimer doEvery(long delay, final Object r) { return doEvery(delay, delay, r); } public static FixedRateTimer doEvery(long delay, long firstDelay, final Object r) { FixedRateTimer timer = new FixedRateTimer(); timer.scheduleAtFixedRate(smartTimerTask(r, timer, toInt(delay)), toInt(firstDelay), toInt(delay)); return timer; } public static FixedRateTimer doEvery(double initialSeconds, double delaySeconds, final Object r) { return doEvery(toMS(delaySeconds), toMS(initialSeconds), r); } public static FixedRateTimer doEvery(double delaySeconds, final Object r) { return doEvery(toMS(delaySeconds), r); } public static RuntimeException rethrow(Throwable t) { if (t instanceof Error) _handleError((Error) t); throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t); } public static boolean ai_useThoughtSpaces_value; public static boolean ai_useThoughtSpaces() { return ai_useThoughtSpaces_value; } public static void ai_useThoughtSpaces(boolean flag) { ai_useThoughtSpaces_value = flag; } public static void releaseDBLockOnTimeoutEval() { callOptMC("beforeDelegatingToThread_do", "releaseDBLockOnTimeoutEval_before"); callOptMC("afterDelegatingToThread_do", "releaseDBLockOnTimeoutEval_after"); } public static ThreadLocal releaseDBLockOnTimeoutEval_hadLock = new ThreadLocal(); public static void releaseDBLockOnTimeoutEval_before(Thread t) { releaseDBLockOnTimeoutEval_hadLock.set(unlockIfHolding(dbLock())); } public static void releaseDBLockOnTimeoutEval_after(Thread t) { if (isTrue(getAndClearThreadLocal(releaseDBLockOnTimeoutEval_hadLock))) lock(dbLock()); } public static void ai_prepLoadFunctions() { setOpt(mc(), "loadFunctions_veryQuickJava", "ai_prepLoadFunctions_veryQuickJava"); } public static Class ai_prepLoadFunctions_veryQuickJava(String code) { code = smartBotEval_prelude() + "\nplease include function ai_spec_init.\n" + code; Class c = veryQuickJava(code); ai_copyVarsToChild(c); call(c, "ai_spec_init"); return c; } public static void db() { conceptsAndBot(); } public static Object load(String varName) { readLocally(varName); return get(mc(), varName); } public static Object load(String progID, String varName) { readLocally(progID, varName); return get(mc(), varName); } public static boolean canOccupyPort80() { return (isWindows() || isLinux() && isRootUser()) && !isPortOpen(80); } public static List serveHttpWithWebsockets_multiplePorts_servers = synchroList(); public static void serveHttpWithWebsockets_multiplePorts(Object factory, int... ports) { try { for (int port : ports) { WebSocketHTTPD httpd = new WebSocketHTTPD(port, factory); serveHttpWithWebsockets_multiplePorts_servers.add(httpd); httpd.start(); } } catch (Exception __e) { throw rethrow(__e); } } public static void cleanMeUp_serveHttpWithWebsockets_multiplePorts() { for (WebSocketHTTPD httpd : cloneList(serveHttpWithWebsockets_multiplePorts_servers)) { try { httpd.stop(); } catch (Throwable __e) { _handleException(__e); } serveHttpWithWebsockets_multiplePorts_servers.remove(httpd); } } public static WebSocketHTTPD serveHttpWithWebsockets_server; public static int serveHttpWithWebsockets_port = 8888; public static TreeSet serveHttpWithWebsockets_peers = new TreeSet(); public static int serveHttpWithWebsockets(int port, Object factory) { try { serveHttpWithWebsockets_port = port; serveHttpWithWebsockets_server = new WebSocketHTTPD(port, factory); serveHttpWithWebsockets_server.start(); return serveHttpWithWebsockets_port(); } catch (Exception __e) { throw rethrow(__e); } } public static int serveHttpWithWebsockets(Object factory) { serveHttpWithWebsockets(0, factory); return serveHttpWithWebsockets_port(); } public static int serveHttpWithWebsockets_port() { return serveHttpWithWebsockets_server == null ? 0 : serveHttpWithWebsockets_server.getListeningPort(); } public static void cleanMeUp_serveHttpWithWebsockets() { if (serveHttpWithWebsockets_server != null) { serveHttpWithWebsockets_server.stop(); serveHttpWithWebsockets_server = null; } } public static void serveHttps_botCompany() { File keystore = secretProgramFile("#1002017", "keystore.jks"); String pass = loadSecretTextFileMandatory("#1002017", "keystore-pass").trim(); serveHttps(443, keystore, pass); } public static volatile Throwable _handleException_lastException; public static List _handleException_onException = synchroList(ll("printStackTrace2")); public static void _handleException(Throwable e) { _handleException_lastException = e; for (Object f : cloneList(_handleException_onException)) try { callF(f, e); } catch (Throwable e2) { printStackTrace2(e2); } } public static Android3 bot(String greeting) { return makeAndroid3(greeting); } public static Android3 bot(Android3 a) { return makeBot(a); } public static Android3 bot(String greeting, Object responder) { return makeBot(greeting, responder); } public static Android3 bot() { return makeBot(); } public static void save(String varName) { saveLocally(varName); } public static void save(String progID, String varName) { saveLocally(progID, varName); } public static ThreadLocal ai_postTriple_verified = new ThreadLocal(); public static ThreadLocal ai_postTriple_recursion = new ThreadLocal(); public static String ai_postTriple(T3 triple) { ping(); if (triple_anyPartNull(triple)) return null; triple = trimTriple(triple); if (!ai_tripleAllowedToPost(triple)) return null; if (has(triple)) return null; if (has(ai_tripleToInvalid(triple))) return null; boolean ver = !isFalse(ai_postTriple_verified.get()); AI_PostHandler handler = ai_postHandler(); T3 striple = toSymbolTriple(triple); if (handler != null && !isTrue(ai_postTriple_recursion.get())) { ai_postTriple_recursion.set(true); try { return strOrNull(handler.postTriple(striple, ver)); } finally { ai_postTriple_recursion.set(null); } } else { Web web = webFromTriple(striple); web.unverified = !ver; postSoftwareMadeWeb(web); return web.globalID(); } } public static String ai_postTriple(String a, String b, String c) { return ai_postTriple(t3(a, b, c)); } public static String quote(Object o) { if (o == null) return "null"; return quote(str(o)); } public static String quote(String s) { if (s == null) return "null"; StringBuilder out = new StringBuilder((int) (l(s) * 1.5 + 2)); quote_impl(s, out); return out.toString(); } public static void quote_impl(String s, StringBuilder out) { out.append('"'); int l = s.length(); for (int i = 0; i < l; i++) { char c = s.charAt(i); if (c == '\\' || c == '"') out.append('\\').append(c); else if (c == '\r') out.append("\\r"); else if (c == '\n') out.append("\\n"); else if (c == '\0') out.append("\\0"); else out.append(c); } out.append('"'); } public static HashMap litmap(Object... x) { HashMap map = new HashMap(); litmap_impl(map, x); return map; } public static void litmap_impl(Map map, Object... x) { for (int i = 0; i < x.length - 1; i += 2) if (x[i + 1] != null) map.put(x[i], x[i + 1]); } public static Class mc() { return main.class; } public static List ai_fillTripleIndex_useThese; public static Object ai_fillTripleIndex_useTripleIndex; public static void ai_fillTripleIndex() { if (ai_fillTripleIndex_useTripleIndex != null) { print("Copying triple index."); ai_reuseTripleIndex(ai_fillTripleIndex_useTripleIndex); ai_fillTripleIndex_useTripleIndex = null; ai_fillTripleIndex_useThese = null; return; } print("Getting triples."); long _startTime_119 = sysNow(); List triples = ai_fillTripleIndex_useThese != null ? ai_fillTripleIndex_useThese : localSoftwareMadeTriples(); ai_fillTripleIndex_useThese = null; _startTime_119 = sysNow() - _startTime_119; saveTiming(_startTime_119); long time = lastTiming(); long us = ratio(time * 1000, l(triples)); print("Got " + nTriples(triples) + " in " + us + " " + unicode_micro() + "s per triple. Making triple index."); long _startTime_120 = sysNow(); ai_replaceTripleIndexWithTriples(triples); ai_deleteInvalidTriples(); done2_always("Triple index made.", _startTime_120); } public static Thread runInNewThread(final String progID) { return startThread(progID, new Runnable() { public void run() { try { callMainAsChild(hotwire(progID)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callMainAsChild(hotwire(progID));"; } }); } public static void ai_onNewWeb(Object onNewWeb) { if (onNewWeb == null) return; setAdd(postSoftwareMadeWeb_onNewWeb, onNewWeb); onTransientWebAdded_do(onNewWeb); } public static void ai_extendKnownVerbs() { knownVerbs().addAll(ai_texts_multi("$X", "is", ll("a third person verb", "a verb"))); } public static A addVirtualNodeIndex(A i) { setAdd(virtualNodeIndices_list, i); return i; } public static VirtualNodeIndex hugeEnglishDictionary_virtualNodeIndex() { return new HugeEnglishDictionary_VirtualNodeIndex(); } public static class HugeEnglishDictionary_VirtualNodeIndex extends VirtualNodeIndex { public SortedCondensedStrings2 dict = hugeEnglishDictionary(); public HugeEnglishDictionary_VirtualNodeIndex() { addAll(implicitConcepts, "is", "an english word according to the huge dictionary"); } public int numWebs() { return l(dict); } public int numTerms() { return l(dict); } public Collection indexedTerms() { return dict; } public List get(CharSequence _query) { String query = str(_query); query = lower(query); if (!dict.contains(query)) return emptyList(); return ll(web_firstNode(webFromTriple(query, "is", "an english word according to the huge dictionary"))); } public boolean hasShortTerm(String s) { return dict.contains(lower(s)); } } public static Thread startThread(Object runnable) { return startThread(defaultThreadName(), runnable); } public static Thread startThread(String name, Object runnable) { runnable = wrapAsActivity(runnable); return startThread(newThread(toRunnable(runnable), name)); } public static Thread startThread(Thread t) { _registerThread(t); t.start(); return t; } public static void doneLoading() { loading_value = false; } public static void postToChatNamed(String chatName, String text) { if (empty(chatName)) return; if (eqic(chatName, "Stefan's Chat")) postToStefansChat(text); else if (eqic(chatName, "Smart Bot's Chat")) postToSmartBotsChat(text); else warn("Unknown chat name: " + chatName); } public static String infoBoxAndReturn(String s) { infoBox(s); return s; } public static String formatSeconds(long ms, int digits) { return formatDouble(toS(ms), digits); } public static JWindow infoBox(String text) { return infoMessage(text); } public static JWindow infoBox(String text, double seconds) { return infoMessage(text, seconds); } public static JWindow infoBox(Throwable e) { return infoMessage(e); } public static String scanMultipleBots(List bots, String s) { for (Object bot : unnull(bots)) try { { String _a_143 = callAnswerMethod(bot, s); if (!empty(_a_143)) return _a_143; } } catch (Throwable __e) { _handleException(__e); } return null; } public static boolean cleanUp_interruptThreads; public static void cleanUp(Object c) { if (c == null) return; if (c instanceof Collection) { cleanUp((Collection) c); return; } if (c instanceof Map) { for (Object o : keys((Map) c)) cleanUp(o); for (Object o : values((Map) c)) cleanUp(o); ((Map) c).clear(); return; } try { callOpt(c, "licensed_off"); setOpt(c, "cleaningUp_flag", true); setOpt(c, "ping_pauseAll", false); innerCleanUp(c); pcallOpt(c, "finalize"); List androids = (List) getOpt(c, "record_list"); for (Object android : unnull(androids)) pcallOpt(android, "dispose"); List classes = (List) (getOpt(c, "hotwire_classes")); if (classes != null) for (WeakReference cc : classes) try { cleanUp(cc.get()); } catch (Throwable __e) { _handleException(__e); } if (cleanUp_interruptThreads) { List threads = registeredThreads(c); if (nempty(threads)) { print("cleanUp: Interrupting " + n2(threads, "thread") + ": " + joinWithComma(allToString(threads))); interruptThreads(threads); } } } catch (Throwable __e) { _handleException(__e); } setOpt(c, "cleaningUp_flag", false); if (c instanceof Class && ((Class) c).getName().equals("main")) retireClassLoader(((Class) c).getClassLoader()); } public static void cleanUp(Collection l) { if (l == null) return; for (Object c : l) cleanUp(c); l.clear(); } public static Class run(String progID, String... args) { Class main = hotwire(progID); callMain(main, args); return main; } public static Field setOpt_findField(Class c, String field) { HashMap map; synchronized (getOpt_cache) { map = getOpt_cache.get(c); if (map == null) map = getOpt_makeCache(c); } return map.get(field); } public static void setOpt(Object o, String field, Object value) { try { if (o == null) return; if (o instanceof _SetField) { ((_SetField) o)._setField(field, value); return; } Class c = o.getClass(); HashMap map; synchronized (getOpt_cache) { map = getOpt_cache.get(c); if (map == null) map = getOpt_makeCache(c); } if (map == getOpt_special) { if (o instanceof Class) { setOpt((Class) o, field, value); return; } setOpt_raw(o, field, value); return; } Field f = map.get(field); if (f != null) smartSet(f, o, value); } catch (Exception __e) { throw rethrow(__e); } } public static void setOpt(Class c, String field, Object value) { if (c == null) return; try { Field f = setOpt_findStaticField(c, field); if (f != null) smartSet(f, null, value); } catch (Exception e) { throw new RuntimeException(e); } } public static Field setOpt_findStaticField(Class c, String field) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) { f.setAccessible(true); return f; } _c = _c.getSuperclass(); } while (_c != null); return null; } public static Object call(Object o) { return callFunction(o); } public static Object call(Object o, String method, String[] arg) { return call(o, method, new Object[] { arg }); } public static Object call(Object o, String method, Object... args) { try { if (o == null) return null; if (o instanceof Class) { Method m = call_findStaticMethod((Class) o, method, args, false); m.setAccessible(true); return invokeMethod(m, null, args); } else { Method m = call_findMethod(o, method, args, false); m.setAccessible(true); return invokeMethod(m, o, args); } } catch (Exception __e) { throw rethrow(__e); } } public static Method call_findStaticMethod(Class c, String method, Object[] args, boolean debug) { Class _c = c; while (c != null) { for (Method m : c.getDeclaredMethods()) { if (debug) System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters"); ; if (!m.getName().equals(method)) { if (debug) System.out.println("Method name mismatch: " + method); continue; } if ((m.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0 || !call_checkArgs(m, args, debug)) continue; return m; } c = c.getSuperclass(); } throw new RuntimeException("Method '" + method + "' (static) with " + args.length + " parameter(s) not found in " + _c.getName()); } public static Method call_findMethod(Object o, String method, Object[] args, boolean debug) { Class c = o.getClass(); while (c != null) { for (Method m : c.getDeclaredMethods()) { if (debug) System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters"); ; if (m.getName().equals(method) && call_checkArgs(m, args, debug)) return m; } c = c.getSuperclass(); } throw new RuntimeException("Method '" + method + "' (non-static) with " + args.length + " parameter(s) not found in " + o.getClass().getName()); } public static boolean call_checkArgs(Method m, Object[] args, boolean debug) { Class[] types = m.getParameterTypes(); if (types.length != args.length) { if (debug) System.out.println("Bad parameter length: " + args.length + " vs " + types.length); return false; } for (int i = 0; i < types.length; i++) if (!(args[i] == null || isInstanceX(types[i], args[i]))) { if (debug) System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]); return false; } return true; } public static void unlock(Lock lock, String msg) { if (lock == null) return; print("Unlocking: " + msg); lock.unlock(); } public static void unlock(Lock lock) { if (lock == null) return; lock.unlock(); } public static boolean isTrue(Object o) { if (o instanceof Boolean) return ((Boolean) o).booleanValue(); if (o == null) return false; if (o instanceof ThreadLocal) return isTrue(((ThreadLocal) o).get()); throw fail(getClassName(o)); } public static B mapGet(Map map, A a) { return map == null || a == null ? null : map.get(a); } public static boolean empty(Collection c) { return c == null || c.isEmpty(); } public static boolean empty(CharSequence s) { return s == null || s.length() == 0; } public static boolean empty(Map map) { return map == null || map.isEmpty(); } public static boolean empty(Object[] o) { return o == null || o.length == 0; } public static boolean empty(Object o) { if (o instanceof Collection) return empty((Collection) o); if (o instanceof String) return empty((String) o); if (o instanceof Map) return empty((Map) o); if (o instanceof Object[]) return empty((Object[]) o); if (o instanceof byte[]) return empty((byte[]) o); if (o == null) return true; throw fail("unknown type for 'empty': " + getType(o)); } public static boolean empty(float[] a) { return a == null || a.length == 0; } public static boolean empty(int[] a) { return a == null || a.length == 0; } public static boolean empty(long[] a) { return a == null || a.length == 0; } public static boolean empty(byte[] a) { return a == null || a.length == 0; } public static boolean empty(MultiSet ms) { return ms == null || ms.isEmpty(); } public static volatile boolean licensed_yes = true; public static boolean licensed() { ping(); return licensed_yes; } public static void licensed_off() { licensed_yes = false; } public static String trim(String s) { return s == null ? null : s.trim(); } public static String trim(StringBuilder buf) { return buf.toString().trim(); } public static String trim(StringBuffer buf) { return buf.toString().trim(); } public static String exceptionToStringShort(Throwable e) { lastException(e); e = getInnerException(e); String msg = hideCredentials(unnull(e.getMessage())); if (msg.indexOf("Error") < 0 && msg.indexOf("Exception") < 0) return baseClassName(e) + prependIfNempty(": ", msg); else return msg; } public static boolean eq(Object a, Object b) { return a == null ? b == null : a == b || a.equals(b); } public static volatile boolean sleep_noSleep; public static void sleep(long ms) { ping(); if (ms < 0) return; if (isAWTThread() && ms > 100) throw fail("Should not sleep on AWT thread"); try { Thread.sleep(ms); } catch (Exception e) { throw new RuntimeException(e); } } public static void sleep() { try { if (sleep_noSleep) throw fail("nosleep"); print("Sleeping."); sleepQuietly(); } catch (Exception __e) { throw rethrow(__e); } } public static int concepts_internStringsLongerThan = 10; public static ThreadLocal concepts_unlisted = new ThreadLocal(); public static interface Derefable { public Concept get(); } public static interface IConceptIndex { public void update(Concept c); public void remove(Concept c); } public static interface IFieldIndex { public List getAll(Val val); } public static class Concepts { public Map concepts = synchroTreeMap(); public HashMap perClassData = new HashMap(); public String programID; public long idCounter; public volatile long changes, changesWritten; public volatile java.util.Timer autoSaver; public volatile boolean savingConcepts, dontSave, noXFullGrab; public boolean initialSave = true; public int autoSaveInterval = -1000; public boolean useGZIP = true, quietSave; public ReentrantLock lock = new ReentrantLock(true); public ReentrantLock saverLock = new ReentrantLock(true); public long lastSaveTook, lastSaveWas; public float maxAutoSavePercentage = 10; public List conceptIndices; public Map, Map> fieldIndices; public List saveActions = synchroList(); public Concepts() { } public Concepts(String programID) { this.programID = programID; } public synchronized long internalID() { do { ++idCounter; } while (hasConcept(idCounter)); return idCounter; } public void initProgramID() { if (programID == null) programID = getDBProgramID(); } public Concepts load() { return load(false); } public Concepts safeLoad() { return load(true); } public Concepts load(boolean allDynamic) { initProgramID(); try { if (tryToGrab(allDynamic)) return this; } catch (Throwable e) { if (!exceptionMessageContains(e, "no xfullgrab")) printShortException(e); print("xfullgrab failed - loading DB of " + programID + " from disk"); } return loadFromDisk(allDynamic); } public Concepts loadFromDisk() { return loadFromDisk(false); } public Concepts loadFromDisk(boolean allDynamic) { if (nempty(concepts)) clearConcepts(); long time = now(); Map _concepts = concepts; readLocally2_allDynamic.set(allDynamic); readLocally2(this, programID, "concepts"); Map __concepts = concepts; concepts = _concepts; concepts.putAll(__concepts); int l = readLocally_stringLength; int tokrefs = unstructure_tokrefs; assignConceptsToUs(); done("Loaded " + n(l(concepts), "concepts"), time); if (fileSize(getProgramFile(programID, "idCounter.structure")) != 0) readLocally2(this, programID, "idCounter"); else calcIdCounter(); if (initialSave) allChanged(); return this; } public Concepts loadConcepts() { return load(); } public boolean tryToGrab(boolean allDynamic) { if (sameSnippetID(programID, getDBProgramID())) return false; RemoteDB db = connectToDBOpt(programID); try { if (db != null) { loadGrab(db.fullgrab(), allDynamic); return true; } } finally { if (db != null) db.close(); } return false; } public Concepts load(String grab) { return loadGrab(grab, false); } public Concepts safeLoad(String grab) { return loadGrab(grab, true); } public Concepts loadGrab(String grab, boolean allDynamic) { clearConcepts(); DynamicObject_loading.set(true); try { Map map = (Map) (allDynamic ? safeUnstructure(grab) : unstructure(grab)); concepts.putAll(map); assignConceptsToUs(); for (long l : map.keySet()) idCounter = max(idCounter, l); } finally { DynamicObject_loading.set(null); } allChanged(); return this; } public void assignConceptsToUs() { for (Concept c : values(concepts)) { c._concepts = this; callOpt_noArgs(c, "_doneLoading2"); } } public String progID() { return programID == null ? getDBProgramID() : programID; } public Concept getConcept(String id) { return empty(id) ? null : getConcept(parseLong(id)); } public Concept getConcept(long id) { return (Concept) concepts.get((long) id); } public Concept getConcept(RC ref) { return ref == null ? null : getConcept(ref.longID()); } public boolean hasConcept(long id) { return concepts.containsKey((long) id); } public void deleteConcept(long id) { Concept c = getConcept(id); if (c == null) print("Concept " + id + " not found"); else c.delete(); } public void calcIdCounter() { long id_ = 0; for (long id : keys(concepts)) id_ = max(id_, id); idCounter = id_ + 1; saveLocally2(this, programID, "idCounter"); } public void saveConceptsIfDirty() { saveConcepts(); } public void save() { saveConcepts(); } public void saveConcepts() { if (dontSave) return; initProgramID(); saverLock.lock(); savingConcepts = true; long start = now(), time; try { String s = null; long _changes = changes; if (_changes == changesWritten) return; File f = getProgramFile(programID, useGZIP ? "concepts.structure.gz" : "concepts.structure"); lock.lock(); long fullTime = now(); try { saveLocally2(this, programID, "idCounter"); if (useGZIP) { saveGZStructureToFile(f, cloneMap(concepts)); getProgramFile(programID, "concepts.structure").delete(); } else s = structure(cloneMap(concepts)); } finally { lock.unlock(); } while (nempty(saveActions)) pcallF(popFirst(saveActions)); changesWritten = _changes; if (!useGZIP) { time = now() - start; if (!quietSave) print("Saving " + toM(l(s)) + "M chars (" + time + " ms)"); start = now(); saveTextFile(f, javaTokWordWrap(s)); getProgramFile(programID, "concepts.structure.gz").delete(); } copyFile(f, getProgramFile(programID, "backups/concepts.structure" + (useGZIP ? ".gz" : "") + ".backup" + ymd() + "-" + formatInt(hours(), 2))); time = now() - start; if (!quietSave) print(programID + ": Saved " + toK(f.length()) + " K, " + n(concepts, "concepts") + " (" + time + " ms)"); lastSaveWas = fullTime; lastSaveTook = now() - fullTime; } finally { savingConcepts = false; saverLock.unlock(); } } public void _autoSaveConcepts() { if (autoSaveInterval < 0 && maxAutoSavePercentage != 0) { long pivotTime = Math.round(lastSaveWas + lastSaveTook * 100.0 / maxAutoSavePercentage); if (now() < pivotTime) { return; } } try { saveConcepts(); } catch (Throwable e) { print("Concept save failed, will try again: " + e); } } public void clearConcepts() { concepts.clear(); allChanged(); } public synchronized void allChanged() { ++changes; } public synchronized void autoSaveConcepts() { if (autoSaver == null) { if (isTransient()) throw fail("Can't persist transient database"); autoSaver = doEvery_daemon(abs(autoSaveInterval), new Runnable() { public void run() { try { _autoSaveConcepts(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "_autoSaveConcepts()"; } }); } } public void cleanMeUp() { boolean shouldSave = autoSaver != null; if (autoSaver != null) { autoSaver.cancel(); autoSaver = null; } while (savingConcepts) sleepInCleanUp(10); if (shouldSave) saveConceptsIfDirty(); } public Map getIDsAndNames() { Map map = new HashMap(); Map cloned = cloneMap(concepts); for (long id : keys(cloned)) map.put(id, cloned.get(id).className); return map; } public void deleteConcepts(List l) { for (Object o : l) if (o instanceof Long) concepts.remove((Long) o); else if (o instanceof Concept) ((Concept) o).delete(); else warn("Can't delete " + getClassName(o)); } public A conceptOfType(Class type) { return firstOfType(allConcepts(), type); } public List conceptsOfType(Class type) { return filterByType(allConcepts(), type); } public List listConcepts(Class type) { return conceptsOfType(type); } public List list(Class type) { return conceptsOfType(type); } public List list(String type) { return conceptsOfType(type); } public List conceptsOfType(String type) { return filterByDynamicType(allConcepts(), "main$" + type); } public boolean hasConceptOfType(Class type) { return hasType(allConcepts(), type); } public void persistConcepts() { loadConcepts(); autoSaveConcepts(); } public void conceptPersistence() { persistConcepts(); } public Concepts persist() { persistConcepts(); return this; } public void persist(int interval) { autoSaveInterval = interval; persist(); } public A ensureHas(Class c, Runnable r) { A a = conceptOfType(c); if (a == null) { r.run(); a = conceptOfType(c); if (a == null) throw fail("Concept not made by " + r + ": " + shortClassName(c)); } return a; } public void ensureHas(Class c1, Class c2, Object func) { for (Concept a : conceptsOfType(c1)) { Concept b = findBackRef(a, c2); if (b == null) { callF(func, a); b = findBackRef(a, c2); if (b == null) throw fail("Concept not made by " + func + ": " + shortClassName(c2)); } } } public void forEvery(Class type, Object func) { for (Concept c : conceptsOfType(type)) callF(func, c); } public int deleteAll(Class type) { List l = (List) conceptsOfType(type); for (Concept c : l) c.delete(); return l(l); } public Collection allConcepts() { synchronized (concepts) { return new ArrayList(values(concepts)); } } public int countConcepts(Class c, Object... params) { int n = 0; for (A x : list(c)) if (checkConceptFields(x, params)) ++n; return n; } public int countConcepts(String c, Object... params) { int n = 0; for (Concept x : list(c)) if (checkConceptFields(x, params)) ++n; return n; } public int countConcepts() { return l(concepts); } public synchronized void addConceptIndex(IConceptIndex index) { if (conceptIndices == null) conceptIndices = new ArrayList(); conceptIndices.add(index); } public synchronized void removeConceptIndex(IConceptIndex index) { if (conceptIndices == null) return; conceptIndices.remove(index); if (empty(conceptIndices)) conceptIndices = null; } public synchronized void addFieldIndex(Class c, String field, IFieldIndex index) { if (fieldIndices == null) fieldIndices = new HashMap(); Map map = fieldIndices.get(c); if (map == null) fieldIndices.put(c, map = new HashMap()); map.put(field, index); } public synchronized IFieldIndex getFieldIndex(Class c, String field) { if (fieldIndices == null) return null; Map map = fieldIndices.get(c); return map == null ? null : map.get(field); } public RC xnew(String name, Object... values) { return new RC(cnew(name, values)); } public void xset(long id, String field, Object value) { xset(new RC(id), field, value); } public void xset(RC c, String field, Object value) { if (value instanceof RC) value = getConcept((RC) value); cset(getConcept(c), field, value); } public Object xget(long id, String field) { return xget(new RC(id), field); } public Object xget(RC c, String field) { return xgetPost(cget(getConcept(c), field)); } public Object xgetPost(Object o) { o = deref(o); if (o instanceof Concept) return new RC((Concept) o); return o; } public void xdelete(long id) { xdelete(new RC(id)); } public void xdelete(RC c) { getConcept(c).delete(); } public void xdelete(List l) { for (RC c : l) xdelete(c); } public List xlist() { return map("toPassRef", allConcepts()); } public List xlist(String className) { return map("toPassRef", conceptsOfType(className)); } public boolean isTransient() { return eq(programID, "-"); } public String xfullgrab() { if (noXFullGrab) throw fail("no xfullgrab (DB too large)"); Lock __44 = lock(); lock(__44); try { if (changes == changesWritten && !isTransient()) return loadConceptsStructure(programID); return structure(cloneMap(concepts)); } finally { unlock(__44); } } public void xshutdown() { cleanKillVM(); } public long xchangeCount() { return changes; } public int xcount() { return countConcepts(); } public void register(Concept c) { if (c._concepts == this) return; if (c._concepts != null) throw fail("Can't re-register"); c._concepts = this; c.id = internalID(); c.created = now(); concepts.put((long) c.id, c); c.change(); } public void conceptChanged(Concept c) { allChanged(); if (conceptIndices != null) for (IConceptIndex index : conceptIndices) index.update(c); } } public static volatile Concepts mainConcepts = new Concepts(); public static class Concept extends DynamicObject { public transient Concepts _concepts; public long id; public long created; public Concept(String className) { super(className); _created(); } public Concept() { if (!_loading()) { _created(); } } public Concept(boolean unlisted) { if (!unlisted) _created(); } public List refs; public List backRefs; public static boolean loading() { return _loading(); } public static boolean _loading() { return isTrue(DynamicObject_loading.get()); } public void _created() { if (!isTrue(concepts_unlisted.get())) mainConcepts.register(this); } public class Ref { public A value; public Ref() { if (!isTrue(DynamicObject_loading.get())) refs = addDyn(refs, this); } public Ref(A value) { this.value = value; refs = addDyn(refs, this); index(); } public Concept concept() { return Concept.this; } public A get() { return value; } public boolean has() { return value != null; } public void set(A a) { if (a == value) return; unindex(); value = a; index(); } public void set(Ref ref) { set(ref.get()); } public void clear() { set((A) null); } public void index() { if (value != null) value.backRefs = addDyn(value.backRefs, this); change(); } public void unindex() { if (value != null) value.backRefs = removeDyn(value.backRefs, this); } public void change() { Concept.this.change(); } } public class RefL extends AbstractList { public List> l = new ArrayList(); public A set(int i, A o) { A prev = l.get(i).get(); l.get(i).set(o); return prev; } public void add(int i, A o) { l.add(i, new Ref(o)); } public A get(int i) { return l.get(i).get(); } public A remove(int i) { return l.remove(i).get(); } public int size() { return l.size(); } public boolean contains(Object o) { if (o instanceof Concept) for (Ref r : l) if (eq(r.get(), o)) return true; return super.contains(o); } } public void delete() { for (Ref r : unnull(refs)) r.unindex(); refs = null; for (Ref r : cloneList(backRefs)) r.set((Concept) null); backRefs = null; if (_concepts != null) { _concepts.concepts.remove((long) id); _concepts.allChanged(); if (_concepts.conceptIndices != null) for (IConceptIndex index : _concepts.conceptIndices) index.remove(this); _concepts = null; } id = 0; } public BaseXRef export() { return new BaseXRef(_concepts.progID(), id); } public void change() { if (_concepts != null) _concepts.conceptChanged(this); } public void _change() { change(); } public String _programID() { return _concepts == null ? getDBProgramID() : _concepts.progID(); } } public static class RC { public transient Object owner; public String id; public RC() { } public RC(long id) { this.id = str(id); } public RC(Object owner, long id) { this.id = str(id); this.owner = owner; } public RC(Concept c) { this(c.id); } public long longID() { return parseLong(id); } public String toString() { return id; } public transient RemoteDB db; public String getString(String field) { return db.xS(this, field); } public Object get(String field) { return db.xget(this, field); } public void set(String field, Object value) { db.xset(this, field, value); } } public static class BaseXRef { public String programID; public long id; public BaseXRef() { } public BaseXRef(String programID, long id) { this.id = id; this.programID = programID; } public boolean equals(Object o) { if (!(o instanceof BaseXRef)) return false; BaseXRef r = (BaseXRef) (o); return eq(programID, r.programID) && eq(id, r.id); } public int hashCode() { return programID.hashCode() + (int) id; } } public static class XRef extends Concept { public BaseXRef ref; public XRef() { } public XRef(BaseXRef ref) { this.ref = ref; _doneLoading2(); } public void _doneLoading2() { getIndex().put(ref, this); } public HashMap getIndex() { return getXRefIndex(_concepts); } } public static synchronized HashMap getXRefIndex(Concepts concepts) { HashMap cache = (HashMap) concepts.perClassData.get(XRef.class); if (cache == null) concepts.perClassData.put(XRef.class, cache = new HashMap()); return cache; } public static XRef lookupOrCreateXRef(BaseXRef ref) { XRef xref = getXRefIndex(mainConcepts).get(ref); if (xref == null) xref = new XRef(ref); return xref; } public static List list(Class type) { return mainConcepts.list(type); } public static List list(Concepts concepts, Class type) { return concepts.list(type); } public static List list(String type) { return mainConcepts.list(type); } public static List list(Concepts concepts, String type) { return concepts.list(type); } public static int csetAll(Concept c, Object... values) { return cset(c, values); } public static void cleanMeUp_concepts() { mainConcepts.cleanMeUp(); } public static void loadAndAutoSaveConcepts() { mainConcepts.persist(); } public static void loadAndAutoSaveConcepts(int interval) { mainConcepts.persist(interval); } public static void loadConceptsFrom(String progID) { mainConcepts.programID = progID; mainConcepts.load(); } public static List conceptsOfType(String type) { return mainConcepts.conceptsOfType(type); } public static long changeCount() { return mainConcepts.changes; } public static List exposedDBMethods = ll("xlist", "xnew", "xset", "xdelete", "xget", "xclass", "xfullgrab", "xshutdown", "xchangeCount", "xcount"); public static RC toPassRef(Concept c) { return new RC(c); } public static Lock dbLock() { return mainConcepts.lock; } public static String ai_standardAnswer(String s) { { String _a_171 = unquote(random(ai_texts("$X", "is a standard answer to", quote(s)))); if (!empty(_a_171)) return _a_171; } return unquote(random(ai_texts("$X", "is a standard answer to", s))); } public static String ai_standardAnswer_dollarVars(String s) { for (Pair p : ai_texts_xy_bothQuoted("$X", "is a standard answer to", "$Y")) { Map match = matchDollarVarsIC(p.b, s); if (match != null) return formatDollarVars(p.a, match); } return null; } public static boolean startsWith(String a, String b) { return a != null && a.startsWith(b); } public static boolean startsWith(String a, char c) { return nempty(a) && a.charAt(0) == c; } public static boolean startsWith(String a, String b, Matches m) { if (!startsWith(a, b)) return false; m.m = new String[] { substring(a, l(b)) }; return true; } public static boolean startsWith(List a, List b) { if (a == null || l(b) > l(a)) return false; for (int i = 0; i < l(b); i++) if (neq(a.get(i), b.get(i))) return false; return true; } public static String ai_dropDroppableStuff(String s) { String s2; while (neq(s, s2 = ai_dropDroppablePrefix(ai_dropDroppableTail(s)))) s = s2; return s; } public static List indexedNodes(String s) { return ai_withoutInvalidNodes(cloneList(indexedNodes_withInvalids(s))); } public static String web_operandText(WebNode node, String... rels) { return unquote(web_text(web_operand(node, rels))); } public static boolean neq(Object a, Object b) { return !eq(a, b); } public static String or2(String a, String b) { return nempty(a) ? a : b; } public static String or2(String a, String b, String c) { return or2(or2(a, b), c); } public static boolean eqic(String a, String b) { if ((a == null) != (b == null)) return false; if (a == null) return true; return a.equalsIgnoreCase(b); } public static boolean eqic(char a, char b) { if (a == b) return true; char u1 = Character.toUpperCase(a); char u2 = Character.toUpperCase(b); if (u1 == u2) return true; return Character.toLowerCase(u1) == Character.toLowerCase(u2); } public static int backtick_exitValue; public static boolean backtick_verbose, backtick_keepScript; public static ThreadLocal backtick_scriptFile = new ThreadLocal(); public static ThreadLocal backtick_uninterruptable = new ThreadLocal(); public static boolean backtick_win_cmd; public static String backtick(String cmd) { try { File outFile = File.createTempFile("_backtick", ""); backtickToFile(cmd, outFile); String result = loadTextFile(outFile.getPath(), ""); if (backtick_verbose) { print("[[\n" + result + "]]"); } outFile.delete(); return result; } catch (Exception __e) { throw rethrow(__e); } } public static Process backtickToFile(String cmd, File outFile) { try { try { Process process = backtickToFile_noWait(cmd, outFile); process.waitFor(); backtick_exitValue = process.exitValue(); if (backtick_verbose) System.out.println("Process return code: " + backtick_exitValue); return process; } finally { if (!backtick_keepScript) deleteFile(backtick_scriptFile.get()); backtick_scriptFile.set(null); } } catch (Exception __e) { throw rethrow(__e); } } public static Process backtickToFile_noWait(String cmd, File outFile) { try { File scriptFile; String ext = isWindows() ? ".bat" : ""; if (backtick_keepScript) scriptFile = makeFileNameUnique_withExtension(javaxCachesDir("Cmd Scripts/backtick"), ".bat"); else scriptFile = File.createTempFile("_backtick", ext); backtick_scriptFile.set(scriptFile); if (backtick_verbose) print("backtick: scriptFile " + f2s(scriptFile)); cmd = trim(cmd); if (numLines(cmd) > 1) throw fail("No multi-line commands allowed"); String command = cmd + " >" + bashQuote(outFile.getPath()) + " 2>&1"; if (!isTrue(backtick_uninterruptable.get()) && !isWindows()) command = fixNewLines("\r\ninterruptable() {\r\n\r\n # handle options\r\n local setsid=\"\"\r\n local debug=false\r\n while true; do\r\n case \"${1:-}\" in\r\n --killall) setsid=setsid; shift ;;\r\n --debug) debug=true; shift ;;\r\n --*) echo \"Invalid option: $1\" 1>&2; exit 1;;\r\n *) break;; # no more options\r\n esac\r\n done\r\n\r\n # start the specified command\r\n $setsid \"$@\" &\r\n local child_pid=$!\r\n\r\n # arrange to propagate a signal to the child process\r\n trap '\r\n exec 1>&2\r\n set +e\r\n trap \"\" SIGPIPE # ensure a possible sigpipe from the echo does not prevent the kill\r\n echo \"${BASH_SOURCE[0]} caught SIGTERM while executing $* (pid $child_pid), sending SIGTERM to it\"\r\n # (race) child may have exited in which case kill will report an error\r\n # if setsid is used then prefix the pid with a \"-\" to indicate that the signal\r\n # should be sent to the entire process group\r\n kill ${setsid:+-}$child_pid\r\n exit 143\r\n ' SIGTERM\r\n # ensure that the trap doesn't persist after we return\r\n trap 'trap - SIGTERM' RETURN\r\n\r\n $debug && echo \"interruptable wait (child $child_pid, self $$) for: $*\"\r\n\r\n # An error status from the child process will trigger an exception (via set -e)\r\n # here unless the caller is checking the return status\r\n wait $child_pid # last command, so status of waited for command is returned\r\n}\r\n\r\ninterruptable ") + command; if (backtick_verbose) { print("backtick: command " + command); print("backtick: saving to " + scriptFile.getPath()); } saveTextFile(scriptFile.getPath(), command); if (backtick_verbose) print("backtick: command length=" + l(command) + ", file length=" + scriptFile.length()); String[] command2; if (isWindows()) if (backtick_win_cmd) command2 = new String[] { "cmd", "/c", scriptFile.getPath() }; else command2 = new String[] { scriptFile.getPath() }; else command2 = new String[] { "/bin/bash", scriptFile.getPath() }; if (backtick_verbose) print("backtick: command2 " + structure(command2)); return Runtime.getRuntime().exec(command2); } catch (Exception __e) { throw rethrow(__e); } } public static boolean swic_trim(String a, String b, Matches m) { if (!swic(a, b)) return false; m.m = new String[] { trim(substring(a, l(b))) }; return true; } public static Pattern noUnixTricks_allowedCharacters = regexpIC("[a-z0-9\\- ]*"); public static String noUnixTricks(String s) { if (noUnixTricks_allowedCharacters.matcher(s).matches()) return s; throw fail("You bloody hound. " + quote(s)); } public static void restart() { Object j = getJavaX(); call(j, "cleanRestart", get(j, "fullArgs")); } public static void cleanKillIn1Second() { { Thread _t_0 = new Thread() { public void run() { try { sleepSeconds(1); cleanKill(); } catch (Throwable __e) { _handleException(__e); } } }; startThread(_t_0); } } public static boolean eqicOneOf(String s, String... l) { for (String x : l) if (eqic(s, x)) return true; return false; } public static String callAnswerFunction(Object responder, String s) { return callAnswerMethod(responder, s); } public static String callAnswerFunction(String s) { return callAnswerMethod(s); } public static String callAnswerFunction(Object responder, String s, List history) { return callAnswerMethod(responder, s, history); } public static void runAndCleanUp(String progID) { Class c = hotwire(progID); try { callMain(c); } finally { cleanUp(c); } } public static long fileLength(String path) { return getFileSize(path); } public static long fileLength(File f) { return getFileSize(f); } public static File ai_chatAnalysis_peepHoleFile() { return programFile("#1010980", "peephole-webs.structure.gz"); } public static List javaTokC(String s) { if (s == null) return null; int l = s.length(); ArrayList tok = new ArrayList(); int i = 0; while (i < l) { int j = i; char c, d; while (j < l) { c = s.charAt(j); d = j + 1 >= l ? '\0' : s.charAt(j + 1); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else if (c == '/' && d == '*') { do ++j; while (j < l && !s.substring(j, Math.min(j + 2, l)).equals("*/")); j = Math.min(j + 2, l); } else if (c == '/' && d == '/') { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else break; } i = j; if (i >= l) break; c = s.charAt(i); d = i + 1 >= l ? '\0' : s.charAt(i + 1); if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { if (s.charAt(j) == opener || s.charAt(j) == '\n') { ++j; break; } else if (s.charAt(j) == '\\' && j + 1 < l) j += 2; else ++j; } } else if (Character.isJavaIdentifierStart(c)) do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; } else if (c == '[' && d == '[') { do ++j; while (j + 1 < l && !s.substring(j, j + 2).equals("]]")); j = Math.min(j + 2, l); } else if (c == '[' && d == '=' && i + 2 < l && s.charAt(i + 2) == '[') { do ++j; while (j + 2 < l && !s.substring(j, j + 3).equals("]=]")); j = Math.min(j + 3, l); } else ++j; tok.add(javaTok_substringC(s, i, j)); i = j; } return tok; } public static int l(Object[] a) { return a == null ? 0 : a.length; } public static int l(boolean[] a) { return a == null ? 0 : a.length; } public static int l(byte[] a) { return a == null ? 0 : a.length; } public static int l(int[] a) { return a == null ? 0 : a.length; } public static int l(float[] a) { return a == null ? 0 : a.length; } public static int l(char[] a) { return a == null ? 0 : a.length; } public static int l(Collection c) { return c == null ? 0 : c.size(); } public static int l(Map m) { return m == null ? 0 : m.size(); } public static int l(CharSequence s) { return s == null ? 0 : s.length(); } public static long l(File f) { return f == null ? 0 : f.length(); } public static int l(Object o) { return o == null ? 0 : o instanceof String ? l((String) o) : o instanceof Map ? l((Map) o) : o instanceof Collection ? l((Collection) o) : (Integer) call(o, "size"); } public static int l(MultiSet ms) { return ms == null ? 0 : ms.size(); } public static int l(Lisp l) { return l == null ? 0 : l.size(); } public static String ai_subst_web_charBasedReplace(String webID, final String input) { int n = websMadeInThread(); Web web = assertNotNull("Web not found: " + webID, ai_getWeb(webID)); WebRelation rel = assertNotNull(web_findRelationNamed(web, "matches")); String pat = web_unquoted(rel.a), cmd = web_unquoted(rel.b); assertEquals("substitute * => * with *", pat); Matches m = new Matches(); assertTrue(flexMatchIC(pat, cmd, m)); String src = m.unq(0); final String var = m.unq(2); List terms = unquoteAll(trimAll(splitAtJavaToken(m.get(1), ","))); Object replacer = new F1() { public Object get(String s) { try { return replaceIC(s, var, input); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "replaceIC(s, var, input)"; } }; src = (String) callF(replacer, src); terms = map(replacer, terms); ai_postTriple_multi(src, "see:", terms); return nWebs(websMadeInThread() - n) + " made"; } public static Object first(Object list) { return empty((List) list) ? null : ((List) list).get(0); } public static A first(List list) { return empty(list) ? null : list.get(0); } public static A first(A[] bla) { return bla == null || bla.length == 0 ? null : bla[0]; } public static A first(Iterable i) { if (i == null) return null; Iterator it = i.iterator(); return it.hasNext() ? it.next() : null; } public static Character first(String s) { return empty(s) ? null : s.charAt(0); } public static A first(Pair p) { return p == null ? null : p.a; } public static A first(T3 t) { return t == null ? null : t.a; } public static String unquote(String s) { if (s == null) return null; if (startsWith(s, '[')) { int i = 1; while (i < s.length() && s.charAt(i) == '=') ++i; if (i < s.length() && s.charAt(i) == '[') { String m = s.substring(1, i); if (s.endsWith("]" + m + "]")) return s.substring(i + 1, s.length() - i - 1); } } if (s.length() > 1) { char c = s.charAt(0); if (c == '\"' || c == '\'') { int l = endsWith(s, c) ? s.length() - 1 : s.length(); StringBuilder sb = new StringBuilder(l - 1); for (int i = 1; i < l; i++) { char ch = s.charAt(i); if (ch == '\\') { char nextChar = (i == l - 1) ? '\\' : s.charAt(i + 1); if (nextChar >= '0' && nextChar <= '7') { String code = "" + nextChar; i++; if ((i < l - 1) && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '7') { code += s.charAt(i + 1); i++; if ((i < l - 1) && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '7') { code += s.charAt(i + 1); i++; } } sb.append((char) Integer.parseInt(code, 8)); continue; } switch(nextChar) { case '\"': ch = '\"'; break; case '\\': ch = '\\'; break; case 'b': ch = '\b'; break; case 'f': ch = '\f'; break; case 'n': ch = '\n'; break; case 'r': ch = '\r'; break; case 't': ch = '\t'; break; case '\'': ch = '\''; break; case 'u': if (i >= l - 5) { ch = 'u'; break; } int code = Integer.parseInt("" + s.charAt(i + 2) + s.charAt(i + 3) + s.charAt(i + 4) + s.charAt(i + 5), 16); sb.append(Character.toChars(code)); i += 5; continue; default: ch = nextChar; } i++; } sb.append(ch); } return sb.toString(); } } return s; } public static A last(List l) { return empty(l) ? null : l.get(l.size() - 1); } public static char last(String s) { return empty(s) ? '#' : s.charAt(l(s) - 1); } public static int last(int[] a) { return l(a) != 0 ? a[l(a) - 1] : 0; } public static A last(A[] a) { return l(a) != 0 ? a[l(a) - 1] : null; } public static A last(Iterator it) { A a = null; while (it.hasNext()) { ping(); a = it.next(); } return a; } public static String ai_subst_web(String webID) { int n = websMadeInThread(); Web web = assertNotNull("Web not found: " + webID, ai_getWeb(webID)); WebRelation rel = assertNotNull(web_findRelationNamed(web, "matches")); String pat = web_unquoted(rel.a), input = web_unquoted(rel.b); assertEquals("substitute * => * with *", pat); Matches m = new Matches(); assertTrue(flexMatchIC(pat, input, m)); String src = unquote(m.get(0)); List terms = unquoteAll(trimAll(splitAtJavaToken(m.get(1), ","))); ai_postTriple_multi(src, "see:", terms); return nWebs(websMadeInThread() - n) + " made"; } public static Set evalWithTimeout_inTime = synchroSet(); public static Set evalWithTimeout_allThreads = newWeakHashSet(); public static ThreadLocal evalWithTimeout_threadName = new ThreadLocal(); public static Either evalWithTimeout(int timeoutMS, final Object r) { final Flag done = new Flag(); final Flag doneWaiting = new Flag(); final Var var = new Var(); final Var error = new Var(); Thread t = newThread(getAndClearThreadLocal(evalWithTimeout_threadName), new Runnable() { public void run() { try { try { try { var.set(callF(r)); } finally { evalWithTimeout_allThreads.remove(currentThread()); } } catch (Throwable e) { error.set(e); if (doneWaiting.isUp()) printStackTrace(e); } finally { done.raise(); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "try {\r\n try {\r\n var.set(callF(r));\r\n } finally {\r\n ev..."; } }); beforeDelegatingToThread(t); try { startThread(t); evalWithTimeout_inTime.add(t); evalWithTimeout_allThreads.add(t); try { done.waitUntilUp(timeoutMS); doneWaiting.raise(); } finally { evalWithTimeout_inTime.remove(t); } if (!done.isUp()) { cancelAndInterruptThread(t); return either2(t); } if (error.get() != null) rethrow(error.get()); return either1(var.get()); } finally { afterDelegatingToThread(t); } } public static Either evalWithTimeout(double timeoutSeconds, final Object r) { return evalWithTimeout(toMS_int(timeoutSeconds), r); } public static ThreadLocal saveTiming_last = new ThreadLocal(); public static void saveTiming(long ms) { print(ms + " ms"); saveTiming_noPrint(ms); } public static void saveTiming_noPrint(long ms) { saveTiming_last.set(ms); } public static String nWebs(long i) { return n_fancy2(i, "web", "webs"); } public static String nWebs(Collection c) { return n_fancy2(c, "web", "webs"); } public static double toSeconds(long ms) { return ms / 1000.0; } public static String toSeconds(long ms, int digits) { return formatDouble(toSeconds(ms), digits); } public static long lastTiming() { return getLastTiming(); } public static boolean isSnippetID(String s) { try { parseSnippetID(s); return true; } catch (RuntimeException e) { return false; } } public static long psI(String snippetID) { return parseSnippetID(snippetID); } public static String bashQuote(String text) { if (text == null) return null; return "\"" + text.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n").replace("\r", "\\r") + "\""; } public static String bashQuote(File f) { return bashQuote(f.getAbsolutePath()); } public static boolean directNohupJava_loggingOn = true; public static File directNohupJavax(String javaxargs) { return directNohupJavax(javaxargs, javaxDefaultVMArgs()); } public static File directNohupJavax(final String _javaxargs, final String _vmArgs) { if (directNohupJava_loggingOn) appendToLocalMechLog("nohupJavax Log", sfu(dropSecondIfEmpty(str(parseFirstInt(_javaxargs)), _vmArgs))); String javaxargs = _javaxargs.trim(); if (javaxargs.startsWith("#")) javaxargs = javaxargs.substring(1); String snippetID = javaTok(javaxargs).get(1); int idx = javaxargs.indexOf(' '); String args = idx < 0 ? "" : javaxargs.substring(idx + 1).trim(); String vmArgs = trim(_vmArgs); if (empty(vmArgs) && usePreSpunVMs()) { String line; if (args.length() != 0) line = format3("please start program * with arguments *", snippetID, args); else line = format3("please start program *", snippetID); String answer = sendToLocalBotOpt("A pre-spun VM.", line); if (match3("ok", answer)) { print("OK, used pre-spun VM."); return null; } if (answer != null) print("> " + answer); print("Using standard nohup."); } return classicNohupJavax(javaxargs, vmArgs); } public static String f2s(File f) { return f == null ? null : f.getAbsolutePath(); } public static String f2s(java.nio.file.Path p) { return p == null ? null : f2s(p.toFile()); } public static void rotateSoftwareMadeWebs() { rotateLogFile(postSoftwareMadeWeb_file()); } public static int ai_speculate_all(String prefix) { int n = websMadeInThread(); for (String key : cloneList(allIndexedTermsStartingWith(prefix))) { ping(); for (String word : cloneList(ai_expandShortenedKeys(key))) { ping(); ai_speculate(word); } } return websMadeInThread() - n; } public static int ai_speculate_all() { return ai_speculate_all(""); } public static int websMadeInThread() { return toInt(postSoftwareMadeWeb_websMadeInThread.get()); } public static int websMadeInThread(Thread thread) { return toInt(postSoftwareMadeWeb_websMadeInThread.get(thread)); } public static boolean match(String pat, String s) { return match3(pat, s); } public static boolean match(String pat, String s, Matches matches) { return match3(pat, s, matches); } public static A uniq(Class c, Object... params) { return uniqueConcept(c, params); } public static A uniq(Concepts cc, Class c, Object... params) { return uniqueConcept(cc, c, params); } public static String sfu(Object o) { return structureForUser(o); } public static Object callAndMake_orDirect(String functionName, List args) { return callAndMake_orDirect(functionName, toObjectArray(args)); } public static Object callAndMake_orDirect(String functionName, Object... args) { if (functionName == null) return null; if (hasMethod(mc(), functionName, args)) return callMC(functionName, args); else return makeAndCall_cached(functionName, args); } public static Class loadFunctionsWithInclude(Collection functions, String include) { StringBuilder buf = new StringBuilder(); buf.append("!include once " + fsI(include) + "\n"); for (String s : asSet(functions)) buf.append("please include function " + assertIdentifier(s) + ".\n"); return veryQuickJava(buf); } public static Object callOpt(Object o) { if (o == null) return null; return callF(o); } public static Object callOpt(Object o, String method, Object... args) { try { if (o == null) return null; if (o instanceof Class) { Method m = callOpt_findStaticMethod((Class) o, method, args, false); if (m == null) return null; m.setAccessible(true); return invokeMethod(m, null, args); } else { Method m = callOpt_findMethod(o, method, args, false); if (m == null) return null; m.setAccessible(true); return invokeMethod(m, o, args); } } catch (Exception e) { throw rethrow(e); } } public static Method callOpt_findStaticMethod(Class c, String method, Object[] args, boolean debug) { Class _c = c; while (c != null) { for (Method m : c.getDeclaredMethods()) { if (debug) System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters"); ; if (!m.getName().equals(method)) { if (debug) System.out.println("Method name mismatch: " + method); continue; } if ((m.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0 || !callOpt_checkArgs(m, args, debug)) continue; return m; } c = c.getSuperclass(); } return null; } public static Method callOpt_findMethod(Object o, String method, Object[] args, boolean debug) { Class c = o.getClass(); while (c != null) { for (Method m : c.getDeclaredMethods()) { if (debug) System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters"); ; if (m.getName().equals(method) && callOpt_checkArgs(m, args, debug)) return m; } c = c.getSuperclass(); } return null; } public static boolean callOpt_checkArgs(Method m, Object[] args, boolean debug) { Class[] types = m.getParameterTypes(); if (types.length != args.length) { if (debug) System.out.println("Bad parameter length: " + args.length + " vs " + types.length); return false; } for (int i = 0; i < types.length; i++) if (!(args[i] == null || isInstanceX(types[i], args[i]))) { if (debug) System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]); return false; } return true; } public static boolean swicOneOf_trim(String s, Matches m, String... l) { for (String x : l) if (swic_trim(s, x, m)) return true; return false; } public static double smartBotEval_timeout = 120.0; public static String smartBotEval(final String expression) { long time = sysNow(); evalWithTimeout_threadName.set(shorten(expression, 100)); Either e = (Either) evalWithTimeout(smartBotEval_timeout, new F0() { public String get() { try { return smartBotEval_noTimeout(expression); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "smartBotEval_noTimeout(expression)"; } }); return e.isA() ? e.a() : "Timeout after " + iround(smartBotEval_timeout) + "s"; } public static Pair splitAtFirstSpace(String s) { int i = smartIndexOf(s, ' '); return pair(trim(substring(s, 0, i)), trim(substring(s, i + 1))); } public static A get(List l, int idx) { return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null; } public static A get(A[] l, int idx) { return idx >= 0 && idx < l(l) ? l[idx] : null; } public static boolean get(boolean[] l, int idx) { return idx >= 0 && idx < l(l) ? l[idx] : false; } public static Object get(Object o, String field) { try { if (o instanceof Class) return get((Class) o, field); if (o instanceof Map) return ((Map) o).get(field); Field f = getOpt_findField(o.getClass(), field); if (f != null) { f.setAccessible(true); return f.get(o); } if (o instanceof DynamicObject) return ((DynamicObject) o).fieldValues.get(field); } catch (Exception e) { throw asRuntimeException(e); } throw new RuntimeException("Field '" + field + "' not found in " + o.getClass().getName()); } public static Object get_raw(Object o, String field) { try { Field f = get_findField(o.getClass(), field); f.setAccessible(true); return f.get(o); } catch (Exception e) { throw new RuntimeException(e); } } public static Object get(Class c, String field) { try { Field f = get_findStaticField(c, field); f.setAccessible(true); return f.get(null); } catch (Exception e) { throw new RuntimeException(e); } } public static Field get_findStaticField(Class c, String field) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) return f; _c = _c.getSuperclass(); } while (_c != null); throw new RuntimeException("Static field '" + field + "' not found in " + c.getName()); } public static Field get_findField(Class c, String field) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field)) return f; _c = _c.getSuperclass(); } while (_c != null); throw new RuntimeException("Field '" + field + "' not found in " + c.getName()); } public static Matches ai_matchPattern(String pat, String input) { pat = quoteUnlessQuoted(pat); String function = ai_forward(pat, "can be matched with"); function = "get" + firstToUpper(function); if (!has_verified(function, "is", "a safe function to call")) throw fail("no mama: " + function); return (Matches) quickImport(callAndMake(function, unquote(pat), input)); } public static ArrayList toList(A[] a) { return asList(a); } public static ArrayList toList(int[] a) { return asList(a); } public static ArrayList toList(Set s) { return asList(s); } public static ArrayList toList(Iterable s) { return asList(s); } public static void startedLoading() { loading_value = true; } public static void ai_compactLiveDB(boolean withPhase2) { ai_compactLiveDB_Class c = new ai_compactLiveDB_Class(); c.phase1(); if (!withPhase2) return; print("PHASE 2 in 5 SECONDS"); sleepSeconds(5); c.phase2(); } public static class ai_compactLiveDB_Class { public File mainLog, fTriples, file, file2; public void phase1() { try { mainLog = programFile("#1010745", "webs-made.txt"); rotateLogFile(mainLog); File tripelizedDir = programDir("#1010745", "tripelized"); fTriples = programFile("#1010745", "triples.gz"); file = new File(tripelizedDir, "triples.new.gz"); OutputStream out = newGZIPOutputStream(file); int n = ai_writeTripleFile(ai_allTriples(), out); out.close(); file2 = new File(tripelizedDir, "webs-made.new.gz"); deleteFile(file2); print("Stored " + nTriple(n) + " in " + f2s(file) + ", nothing in " + f2s(file2)); } catch (Exception __e) { throw rethrow(__e); } } public void phase2() { print("PHASE 2."); File fBackups = programFile("#1010745", "backups"); for (File f : earlierPartsOfLogFile(mainLog)) { int n = 1; File p; while ((p = newFile(fBackups, dropSuffix(".gz", f.getName()) + ".part" + n + ".gz")).exists()) ++n; renameFile_assertTrue(f, p); } int n = 1; File p; while ((p = newFile(fBackups, dropSuffix(".gz", fTriples.getName()) + ".part" + n + ".gz")).exists()) ++n; renameFile_assertTrue(fTriples, p); renameFile_assertTrue(file, fTriples); if (file2.exists()) renameFile_assertTrue(file2, programFile("#1010745", "webs-made.txt.part1.gz")); print("DONE."); } } public static long postToSmartBotsChat_lastPost = -60000; public static boolean postToSmartBotsChat_printStackTrace; public static ThreadLocal postToSmartBotsChat_silently = new ThreadLocal(); public static boolean postToSmartBotsChat(String text) { boolean silently = optBoolParam(postToSmartBotsChat_silently); if (empty(text)) return false; if (postToSmartBotsChat_printStackTrace) printStackTrace(); text = shorten(text, stefansChat_messageLimit()); if (!silently) print("CHAT>> " + text); String url = "http://ai1.space/" + psI("#1011975") + "/raw/msg?botmark=1&message=" + urlencode(text); boolean ok = eq("OK", silently ? loadPageSilently(url) : loadPage(url)); postToSmartBotsChat_lastPost = sysNow(); return ok; } public static TripleIndex tripleIndex_instance; public static TripleIndex tripleIndex() { if (tripleIndex_instance == null) { tripleIndex_instance = addVirtualNodeIndex(new TripleIndex()); tripleIndex_instance.activate(); } return tripleIndex_instance; } public static void ai_spec_moveToMiddle_all() { List mtmRels = ai_spec_moveToMiddle_mtmRels(); for (Web web : allIndexedWebs()) try { ping(); ai_spec_moveToMiddle(web, mtmRels); } catch (Throwable __e) { _handleException(__e); } } public static double ai_gc_scan_maxDistance_reached; public static String ai_gc_scan(String root) { ai_gc_clear(); ai_startBuffering(); Map distances; try { long _startTime_282 = sysNow(); ai_postTriple(root, "has GC distance", "0"); distances = litcimap(root, 0.0); LinkedList queue = litLinkedList(root); String word; while ((word = queue.poll()) != null) { ping(); double d = distances.get(word); for (WebNode node : indexedNodes(word)) { for (WebRelation r : web_forwardRelations(node)) { double dist = ai_gc_distanceFunction(web_text(r), true); if (!isNaN(dist)) ai_gc_scan_update(distances, queue, r.b, d + dist); } for (WebRelation r : web_backwardRelations(node)) { double dist = ai_gc_distanceFunction(web_text(r), false); if (!isNaN(dist)) ai_gc_scan_update(distances, queue, r.a, d + dist); } } } Collection keys = keys(distances); if (ai_inTestMode()) keys = keysSortedByValueDesc(distances); ai_gc_scan_maxDistance_reached = 0; for (String key : keys) { double d = distances.get(key); if (d > ai_gc_scan_maxDistance_reached) ai_gc_scan_maxDistance_reached = d; ai_postTriple(key, "has GC distance", formatDouble(d, 1)); } done2_always("GC Scan", _startTime_282); } finally { ai_stopBuffering(); } return print(l(distances) + "/" + l(indexedTerms()) + " terms were reached in " + toS(lastTiming(), 1) + " s GC scan from " + quote(root) + ". Max distance: " + formatDouble(ai_gc_scan_maxDistance_reached, 1)); } public static void ai_gc_scan_update(Map distances, List queue, WebNode n, double d) { String text = web_text(n); Double od = distances.get(text); if (od == null || d < od) { distances.put(text, d); queue.add(text); } } public static String lstr(Map map) { return str(l(map)); } public static String lstr(Collection c) { return str(l(c)); } public static String lstr(String s) { return str(l(s)); } public static List ai_allInvalidWebs() { List webs = new ArrayList(); for (GlobalID id : ai_invalidatedWebs()) addIfNotNull(webs, ai_getWeb(id)); return webs; } public static AI_SubSpace ai_swapInSubSpace(String name) { AI_SubSpace ss = ai_loadSubSpace(ai_subSpaceFile(name)); ai_putSubSpace(name, ss); return ss; } public static void assumeSmartBotURL() { String url = smartBot_findMyExternalURL(); if (url == null) throw fail("Don't have external URL"); setSmartBotURL(url); tellSmartBotURLToChat(); } public static String ai_doneMakingThat(String chatName) { String c = "are making in " + chatName; WebNode node = first(ai_search_dollarX_verified("We", c, "$X")); if (node != null) { ai_invalidateWeb(node.web); return "OK: " + selectWord(web_text(node)); } return "Done making what?"; } public static boolean flexMatchIC(String pat, String s) { return flexMatchIC2(pat, s); } public static boolean flexMatchIC(String pat, String s, Matches m) { return flexMatchIC2(pat, s, m); } public static boolean flexMatchIC(String pat, String s, Matches m, boolean joinBrackets) { return flexMatchIC2(pat, s, m, joinBrackets); } public static String ai_text_verified(String a, String b, String c) { ai_search_indexedWebs_verified.set(true); try { return web_text(ai_index_search_dollarX_first(a, b, c)); } finally { ai_search_indexedWebs_verified.set(null); } } public static String ai_text_verified(T3 t) { return ai_text_verified(t.a, t.b, t.c); } public static String a(String noun) { if (eq(noun, "")) return "?"; return ("aeiou".indexOf(noun.charAt(0)) >= 0 ? "an " : "a ") + noun; } public static String a(String contents, Object... params) { return htag("a", contents, params); } public static String aGlobalID() { return randomID(16); } public static String post(T3 t) { return ai_postTriple(t); } public static String post(String a, String b, String c) { return ai_postTriple(a, b, c); } public static String post(String a, String b, int c) { return post(a, b, symbol(str(c))); } public static String appendWithSpaceIfNempty(String a, String b) { return empty(b) ? a : a + " " + b; } public static String ai_makePartQuestion(String chatName, String id) { if (empty(id)) return null; for (String aThing : ai_texts_verified(id, "is", "$X")) { List parts = ai_partsOf(aThing); for (String part : unnull(parts)) { String thePart = ai_spec_allWords(part + " of " + id); if (ai_text(thePart, "is", "$X") != null) continue; String q = "What is its " + drop_a_an_the(part) + "?"; post("Open question in " + chatName, "is", thePart); return q; } } return null; } public static List ai_search_dollarX_verified(String a, String b, String c) { String query = isDollarVar(a) ? c : a; return webs_search_dollarX(webFromTriples(a, b, c), indexedVerifiedWebs(query)); } public static List ai_search_dollarX_verified(T3 t) { return ai_search_dollarX_verified(t.a, t.b, t.c); } public static String web_text(WebNode node) { return node == null ? null : node.text(); } public static ThreadLocal ai_invalidateWeb_postAntiTriple = new ThreadLocal(); public static boolean ai_invalidateWeb_removeFromMemory = true; public static String ai_invalidateWeb(GlobalID webID) { return ai_invalidateWeb(ai_getWeb(webID)); } public static String ai_invalidateWeb(String webID) { return ai_invalidateWeb(ai_getWebFromTerm(webID)); } public static String ai_invalidateWeb(Web web) { if (web == null) return null; String id = post("Web " + web.globalID, "is", "invalid"); if (isTrue(ai_invalidateWeb_postAntiTriple.get())) { T3 t = webToTriple(web); if (t != null) post(ai_tripleToInvalid(t)); } if (ai_invalidateWeb_removeFromMemory) ai_removeWeb(web); return id; } public static void ai_invalidateWeb(WebNode node) { ai_invalidateWeb(webNodeToWeb(node)); } public static String quoteUnlessWordsAndSpaces(String s) { if (s == null) return null; return isWordsAndSpaces(s) ? s : quote(s); } public static String ai_text(String a, String b, String c) { return firstNotNull(ai_texts(a, b, c)); } public static String ai_text(T3 t) { return firstNotNull(ai_texts(t.a, t.b, t.c)); } public static String ai_renderThingDescriptionWithParts(String s) { String what = ai_lookup(s); if (what == null) return null; List parts = ai_partsOf(what); if (empty(parts)) return what; List l = new ArrayList(); for (String part : parts) { String value = ai_lookup(print(part + " of " + s)); String p = dropArticle(part); l.add(value != null ? p + "=" + value : "unknown " + p); } return what + " with " + textOut_and(l); } public static List ai_texts_verified(CharSequence _a, CharSequence _b, CharSequence _c) { String a = symbol(_a), b = symbol(_b), c = symbol(_c); int position = tripleIndexOf(a, b, c, symbol_dollarX()); if (position < 0) throw fail("No $X in " + ai_renderTriple(a, b, c)); if (ai_useThoughtSpaces()) return ai_texts_verified(a, b, c, thoughtSpace()); return allToSymbol(web_texts(ai_search_dollarX_verified(a, b, c))); } public static List ai_texts_verified(String a, String b, String c, AbstractThoughtSpace ts) { return ai_tripleRefsToTexts_verified(a, b, c, ai_thoughtSpace_query(a, b, c, ts)); } public static List ai_texts_verified(T3 t) { return t == null ? sharedEmptyList() : ai_texts_verified(t.a, t.b, t.c); } public static boolean endsWith(String a, String b) { return a != null && a.endsWith(b); } public static boolean endsWith(String a, char c) { return nempty(a) && lastChar(a) == c; } public static boolean endsWith(String a, String b, Matches m) { if (!endsWith(a, b)) return false; m.m = new String[] { dropLast(l(b), a) }; return true; } public static boolean isIdentifier(String s) { return isJavaIdentifier(s); } public static Object makeAndCall(String functionName, Object... args) { return makeAndCall_cached(functionName, args); } public static boolean ai_isQuestion_1(String s) { return ai_isYesNoQuestion_1(s) || ai_questionWord_1(firstWord(s)); } public static String random_gac36k() { return random(keys(gac36k())); } public static String unicode_rightPointingTriangle() { return charToString(0x25B8); } public static String quickVisualize_progID = "#1007145"; public static Lock quickVisualize_lock = lock(); public static boolean quickVisualize_hasCached(String query) { return quickVisualize_imageFile(query).length() != 0; } public static BufferedImage quickVisualize_fromCache(String query) { File f = quickVisualize_imageFile(query); if (f.length() != 0) try { return loadPNG(f); } catch (Throwable __e) { _handleException(__e); } return null; } public static String quickVisualize_preprocess(String query) { return toUpper(shorten(trim(query), 200)); } public static BufferedImage quickVisualize(String query) { query = quickVisualize_preprocess(query); if (empty(query)) return null; BufferedImage img = quickVisualize_fromCache(query); if (img != null) return img; File f = quickVisualize_imageFile(query); Lock __334 = quickVisualize_lock; lock(__334); try { img = googleImageSearchFirst(query); if (img == null) return null; savePNG(f, img); return img; } finally { unlock(__334); } } public static String quickVisualize_imagePath(String query) { query = quickVisualize_preprocess(query); return fsI(quickVisualize_progID) + "/" + urlencode(query) + ".png"; } public static File quickVisualize_imageFile(String query) { query = quickVisualize_preprocess(query); return prepareProgramFile(quickVisualize_progID, urlencode(query) + ".png"); } public static File quickVisualize_urlsFile(String query) { query = quickVisualize_preprocess(query); return prepareProgramFile(quickVisualize_progID, "urls-" + urlencode(query) + ".txt"); } public static String uploadJPEGToImageServer(BufferedImage img, String name) { byte[] jpeg = toJPEG(img); String md5 = md5OfRGBImage(new RGBImage(imageFromByteArray(jpeg))); long id = imageServerCheckMD5(md5); if (id == 0) return uploadToImageServer_new_jpg(jpeg, name); else return "http://ai1.space/images/raw/" + id; } public static String nWeb(long i) { return nWebs(i); } public static String nWeb(Collection c) { return nWebs(c); } public static long ai_allWebsCount() { return allWebsByID().keysSize() + ai_numberOfVirtualWebs(); } public static String diskSpaceEtcBot_answer(String s) { Matches m = new Matches(); if (match("disk space", s) || match("get disk space", s)) { File f = userDir(); long total = f.getTotalSpace(); long usable = f.getUsableSpace(); String a = "Server disk space: " + toM(usable) + " MB free of " + toM(total) + " MB."; if (!isWindows()) try { String[] inodes = toLines(backtick("df -hi " + bashQuote(f.getPath()))).get(1).split(" +"); a += " Free inodes: " + inodes[3] + "/" + inodes[1]; } catch (Throwable __e) { _handleException(__e); } return a; } if (match("uptime", s)) return backtick("uptime"); if (match("full memory stats", s)) { { long _startTime_353 = sysNow(); try { System.gc(); } finally { _startTime_353 = sysNow() - _startTime_353; saveTiming(_startTime_353); } } return "GC done in " + getLastTiming() + " ms. " + getMemoryInfoPlusSystemMemory(); } if (match("memory stats", s)) return getMemoryInfoPlusSystemMemory(); if (match("vm args", s)) return "This VM was invoked with: " + getVMCommand(); if (match("system properties", s)) return slackSnippet(structureLines(System.getProperties())); if (match("system load", s)) return formatDouble(systemLoad(), 2); return null; } public static double ai_solveQuestionInSubSpace_defautTimeOut = 60; public static String ai_solveQuestionInSubSpace(List> seeds, List> lookingFor) { return ai_solveQuestionInSubSpace(seeds, lookingFor, ai_solveQuestionInSubSpace_defautTimeOut); } public static String ai_solveQuestionInSubSpace(final List> seeds, final List> lookingFor, double maxSeconds) { return (String) eitherAOpt(evalWithTimeout(maxSeconds, new F0() { public Object get() { try { return ai_solveQuestionInSubSpace_noTimeOut(seeds, lookingFor); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ai_solveQuestionInSubSpace_noTimeOut(seeds, lookingFor)"; } })); } public static String ai_solveQuestionInSubSpace(String q) { Pair>, List>> p = ai_solveQuestion_seedAndLookingFor(q); return ai_solveQuestionInSubSpace(p.a, p.b); } public static long done2_always(long startTime, String desc) { long time = sysNow() - startTime; print(desc + " [" + time + " ms]"); return time; } public static long done2_always(String desc, long startTime) { return done2_always(startTime, desc); } public static long done2_always(long startTime) { return done2_always(startTime, ""); } public static List ai_subSpaceNames() { synchronized (ai_getSubSpace_map) { return cloneList(keys(ai_getSubSpace_map)); } } public static List ai_subSpaceNamesOnDisk() { return urldecodeFileNames(filesEndingWith(programFile("subspaces"), ".gz")); } public static double toS(long ms) { return toSeconds(ms); } public static String toS(long ms, int digits) { return toSeconds(ms, digits); } public static long vmStarted() { return vmStartTime_sys(); } public static T3 ai_parseArrowTriple(String s) { return ai_parseTriple(s); } public static List ai_search_dollarX(String a, String b, String c) { return ai_index_search_dollarX(a, b, c); } public static List ai_search_dollarX(T3 t) { return ai_search_dollarX(t.a, t.b, t.c); } public static String n(long l, String name) { return l + " " + trim(l == 1 ? singular(name) : getPlural(name)); } public static String n(Collection l, String name) { return n(l(l), name); } public static String n(Map m, String name) { return n(l(m), name); } public static String n(Object[] a, String name) { return n(l(a), name); } public static String n(MultiSet ms, String name) { return n(l(ms), name); } public static A ifEq(A a, A b, A c) { return eq(a, b) ? c : a; } public static String ai_nlVary(String s) { List> tok = ai_nlClassify1(s); for (int i = 1; i < l(tok); i += 2) { List l = tok.get(i); String word = ai_varyClassification(l); if (word != null) tok.set(i, ll(quoteUnlessQuoted(word))); } return join(map(new F1, Object>() { public Object get(List set) { try { return unquote(last(set)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "unquote(last(set))"; } }, tok)); } public static String join(String glue, Iterable strings) { if (strings == null) return ""; StringBuilder buf = new StringBuilder(); Iterator i = strings.iterator(); if (i.hasNext()) { buf.append(i.next()); while (i.hasNext()) buf.append(glue).append(i.next()); } return buf.toString(); } public static String join(String glue, String... strings) { return join(glue, Arrays.asList(strings)); } public static String join(Iterable strings) { return join("", strings); } public static String join(Iterable strings, String glue) { return join(glue, strings); } public static String join(String[] strings) { return join("", strings); } public static String join(String glue, Pair p) { return p == null ? "" : str(p.a) + glue + str(p.b); } public static Pair swapPair(Pair p) { return p == null ? null : pair(p.b, p.a); } public static List> quickGoogle(String query) { List> out = new ArrayList(); String userAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0"; String html = loadPageWithUserAgent("http://google.com/search?q=" + urlencode(query) + "&lr=lang_en&hl=en", userAgent); String url = first(loadPage_responseHeaders.get().get("Location")); if (url != null) html = loadPageWithUserAgent(url, userAgent); List> h3s = findContainerTagDeep(html, "h3"); for (List tok : h3s) { List linkTok = first(findContainerTag(tok, "a")); if (empty(linkTok)) continue; String link = tagGet(second(linkTok), "href"); { if (!(isAbsoluteURL(link))) continue; } String text = htmldecode(join(dropTags(contentsOfContainerTag(linkTok)))); out.add(pair(link, text)); } return out; } public static String lines(Collection lines) { return fromLines(lines); } public static List lines(String s) { return toLines(s); } public static List uniquify(Collection l) { return uniquifyList(l); } public static List map(Iterable l, Object f) { return map(f, l); } public static List map(Object f, Iterable l) { List x = emptyList(l); if (l != null) for (Object o : l) x.add(callF(f, o)); return x; } public static List map(F1 f, Iterable l) { List x = emptyList(l); if (l != null) for (Object o : l) x.add(callF(f, o)); return x; } public static List map(Object f, Object[] l) { return map(f, asList(l)); } public static List map(Object[] l, Object f) { return map(f, l); } public static List map(Object f, Map map) { return map(map, f); } public static List map(Map map, Object f) { List x = new ArrayList(); if (map != null) for (Object _e : map.entrySet()) { Map.Entry e = (Map.Entry) _e; x.add(callF(f, e.getKey(), e.getValue())); } return x; } public static List> quickGoogle2(String query) { return quickGoogle2(query, "lang_en"); } public static List> quickGoogle2(String query, String language) { query = trim(query); List> out = lookupPossiblyIgnoringCase(parseGoogleLog(), query); if (out != null) return out; out = new ArrayList(); String userAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0"; String html = loadPageWithUserAgent("https://www.google.com/search?q=" + urlencode(query) + "&lr=" + language + "&hl=en", userAgent); String url = first(loadPage_responseHeaders.get().get("Location")); if (url != null) html = loadPageWithUserAgent(url, userAgent); saveTextFile(javaxCachesDir("last-google-result-page.html"), html); List htmlTok = htmlTok(html); List> h3s = findContainerTagDeep(htmlTok, "h3"); for (List tok : h3s) { List linkTok = first(findContainerTag(tok, "a")); if (empty(linkTok)) continue; String link = tagGet(second(linkTok), "href"); { if (!(isAbsoluteURL(link))) continue; } String text = htmldecode(join(dropTags(contentsOfContainerTag(linkTok)))); List sub = subList(htmlTok, magicIndexOfSubList(htmlTok, tok) + l(tok) - 1); String desc = trim(htmldecode(dropTags(join(first(findContainerTagWithParams(sub, "span", "class", "st")))))); out.add(triple(link, text, desc)); } logStructure(googleLog(), litorderedmap("query", query, "language", language, "date", localDateWithSeconds(), "results", out)); return out; } public static List googleAutoComplete(String query) { String url = "http://suggestqueries.google.com/complete/search?client=firefox&q=" + urlencode(query); List data = (List) (jsonDecode(loadPage(url))); return (List) data.get(1); } public static List antonymsFromSynonymDotCom(String word) { String url = "http://www.synonym.com/synonyms/" + urlencode(lower(word)); String html = loadPageWithUserAgent(url, "Mac Safari"); List tok = htmlTok(html); return multiSetByPopularity(asMultiSet(splitAtSpace(joinWithSpace(map(new F1, String>() { public String get(List tok) { try { return dropPrefixTrim("Antonyms:", tok_html_noTags(tok)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "dropPrefixTrim(\"Antonyms:\", tok_html_noTags(tok))"; } }, findContainerTagWithParamsDeep(tok, "ul", "class", "antonyms")))))); } public static String yn(boolean b) { return yesno(b); } public static boolean ai_cache_hasTriple_debug; public static boolean ai_cache_hasTriple(String a, String b, String c) { if (a == null || b == null || c == null) return false; if (ai_useThoughtSpaces()) return thoughtSpace().hasTriple(symbol(a), symbol(b), symbol(c)); AI_CacheChecker checker = ai_cacheChecker(); if (checker != null && checker.hasTriple(symbol(a), symbol(b), symbol(c))) return true; List webs = ai_fewestIndexedWebs(a, b, c); boolean x = webs_search_noVar_bool(webFromTriple(a, b, c), webs); if (ai_cache_hasTriple_debug) print("hasTriple: " + sfuLL(a, b, c, x) + ", searched " + nWebs(webs)); return x; } public static boolean ai_cache_hasTriple(T3 t) { return ai_cache_hasTriple(t.a, t.b, t.c); } public static List trimAll(Collection l) { List l2 = new ArrayList(); if (l != null) for (String s : l) l2.add(trim(s)); return l2; } public static List splitAt(String s, String splitter) { List parts = new ArrayList(); int i = 0; if (s != null) while (i < l(s)) { int j = indexOf(s, splitter, i); if (j < 0) j = l(s); parts.add(substring(s, i, j)); i = j + l(splitter); } return parts; } public static String urlencode(String x) { try { return URLEncoder.encode(unnull(x), "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } public static String ai_specReturn_answerToExternalQuestion(String q, Var answer) { answer.set("[Calculating...]"); q = switcheroo(q); print("Switcheroo to: " + q); T3 t = ai_tripelize(q); print("Have triple: " + t); if (t == null) return "[Couldn't tripelize]"; T3 tFact = ai_tripleWithoutQuestionMark(t); if (has_verbose(tFact)) return "Yes"; T3 tNot = ai_negateTriple(t); if (has_verbose(tNot)) return "No"; { String _a_400 = ai_answerIsQuestion(t); if (!empty(_a_400)) return _a_400; } { String _a_402 = ai_answerWhatQuestion(t); if (!empty(_a_402)) return _a_402; } String unknown = ai_findUnknownThingInTriple(t); if (unknown != null) return "What is " + unknown + "?"; return "[No answer found for: " + ai_renderTriple(t) + "]"; } public static double ai_specReturn_answerToExternalQuestion_defaultTimeout = 10.0; public static String ai_specReturn_answerToExternalQuestion(final String q) { return ai_specReturn_answerToExternalQuestion(ai_specReturn_answerToExternalQuestion_defaultTimeout, q); } public static String ai_specReturn_answerToExternalQuestion(double timeout, final String q) { final Var answer = new Var(); evalWithTimeout(timeout, new Runnable() { public void run() { try { setVarUnlessNull(answer, ai_specReturn_answerToExternalQuestion(q, answer)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "setVarUnlessNull(answer, ai_specReturn_answerToExternalQuestion(q, answer))"; } }); return answer.get(); } public static Web indexedWebWithGlobalID(String globalID) { return ai_getWeb(globalID); } public static String smartBotURL_default = "http://smartbot.botcompany.de"; public static String smartBotURL() { return or2(trim(loadProgramTextFile("#1010745", "location")), smartBotURL_default); } public static String ai_renderTriple(T3 t) { return ai_tripleToString(t); } public static String ai_renderTriple(String a, String b, String c) { return ai_tripleToString(triple(a, b, c)); } public static T3 ai_tripelize(String s) { T3 t = ai_parseTriple(s); if (t != null) return t; String x = ""; if (endsWith(s, "?")) x = " (?)"; t = ai_tripelize_impl(dropCertainPunctuationAtEnd(s, ":;,?!.")); if (t == null) return null; ai_fixTriple(t); if (nempty(x) && t != null && !endsWith(t.a, "?") && !endsWith(t.b, "?") && !endsWith(t.c, "?")) t.b += x; if (endsWith(s, "!") && eq(t.b, ".")) t.b = "!"; return t; } public static T3 ai_tripelize_impl(String s) { List original = ai_tripelize_prepare(s); if (l(original) <= 1) return null; original = simpleSpaces(original); List tok = original; String a = get(tok, 1); T3 triple; { triple = ai_tripelize_dash(s, original); if (triple != null) return triple; } { triple = ai_tripelize_exclamCommand(s, original); if (triple != null) return triple; } { triple = ai_tripelize_howLongEtc(s, original); if (triple != null) return triple; } if (ai_questionWord_1(a)) { tok = subList(tok, 2); a = get(tok, 1); } String before = trimJoinSubList(original, 0, indexOfSubList(original, tok)); if (ai_yesNoQuestionWord_1(a) && neqicOneOf(a, "am", "are", "is")) { List tok2 = subList(tok, 2); int i = indexOfFirstVerb(tok2); String v = get(tok2, i); if (v != null) { String middle = trimJoinSubList(tok2, 0, i); String rest = trimJoinSubList(tok2, i + 1); v = ai_tripelize_helperVerb(a, v); return triple(joinWithSpace2(before, middle), v, rest); } } if (isVerb(a)) { if (ai_yesNoQuestionWord_1(a)) { tok = subList(tok, 2); int i = ai_splitTwoNounPhrases(tok); return triple(trimJoinSubList(tok, 0, i), a + "?", trimJoinSubList(tok, i)); } else { return triple(a, ".", join(subList(tok, 2))); } } int i = ai_endOfNounPhrase(tok); int j = indexOfFirstVerb(tok, i); if (j < 0) return triple(s, ".", "."); return triple(joinWithSpace2(before, trimJoinSubList(tok, 0, j)), tok.get(j), trimJoinSubList(tok, j + 2)); } public static Object getOptMC(String field) { return getOpt(mc(), field); } public static long secondsSinceSysTime(long time) { return (sysNow() - time) / 1000; } public static Map diagramServer_typicalSet() { return litorderedmap(toObjectArray(allToString((List) safeUnstructure(loadPage("http://ai1.space/1010474/raw/typical-set"))))); } public static List ai_unverifiedWebs() { return filterByField(allIndexedWebs(), "unverified", true); } public static DependentCache> ai_trueWebs_cache = new DependentCache("ai_trueWebs_calc", "cachedNodeIndex"); public static Set ai_trueWebs() { return keys(ai_trueWebs_cache.get()); } public static MultiMap ai_trueWebs_calc() { ai_onNewOrRemovedWeb("ai_trueWebs_onNewWeb", "ai_trueWebs_onRemovedWeb"); ai_trueWebs_cache.set(new MultiMap()); Collection webs = allWebsFromCachedNodeIndex_uncached(); for (Web web : webs) ai_trueWebs_onNewWeb(web); return ai_trueWebs_cache.get(); } public static void ai_trueWebs_onNewWeb(Web web) { if (web.unverified) return; Lock __421 = ai_trueWebs_cache.lock; lock(__421); try { Matches m = new Matches(); for (WebNode n : web_search_dollarX(webFromTriple("$X", "is", "true"), web)) if (web_match("Web *", n, m) && isGlobalID(m.unq(0))) ai_trueWebs_cache.get().put(asGlobalID(m.unq(0)), web.globalIDObj()); } finally { unlock(__421); } } public static void ai_trueWebs_onRemovedWeb(Web web) { if (web.unverified) return; Lock __422 = ai_trueWebs_cache.lock; lock(__422); try { Matches m = new Matches(); for (WebNode n : web_search_dollarX(webFromTriple("$X", "is", "true"), web)) if (web_match("Web *", n, m) && isGlobalID(m.unq(0))) ai_trueWebs_cache.get().remove(asGlobalID(m.unq(0)), web.globalIDObj()); } finally { unlock(__422); } } public static String autoRestart_localMD5; public static String autoRestart_localMD5() { if (autoRestart_localMD5 == null) autoRestart_localMD5 = md5(loadCachedTranspilation(programID())); return autoRestart_localMD5; } public static boolean swic(String a, String b) { return startsWithIgnoreCase(a, b); } public static boolean swic(String a, String b, Matches m) { if (!swic(a, b)) return false; m.m = new String[] { substring(a, l(b)) }; return true; } public static String dropSuffixTrim(String suffix, String s) { return dropSuffixAndTrim(suffix, s); } public static boolean dropSuffixTrim(String suffix, String s, Matches m) { if (!endsWith(s, suffix)) return false; m.m = new String[] { dropLast(l(suffix), s) }; return true; } public static List indexedNodesStartingWith(String s) { return allMultiMapValues(prefixSubMap((NavigableMap) cachedNodeIndex().data, s)); } public static Collection allIndexedWebs() { return values(allWebsByID()); } public static String joinWithSpace(Collection c) { return join(" ", c); } public static String joinWithSpace(String... c) { return join(" ", c); } public static List collect(Collection c, String field) { return collectField(c, field); } public static List collect(String field, Collection c) { return collectField(c, field); } public static List collect(Class c, String field) { return collect(list(c), field); } public static List indexedWebs(String s) { return ai_withoutInvalidWebs(nodesToWebs(indexedNodes_withInvalids(s))); } public static List indexedWebs(AI_SubSpace ss, String s) { return ai_withoutInvalidWebs(nodesToWebs(indexedNodes_withInvalids(ss, s))); } public static T3 splitAtBaseFormVerb(String s) { List tok = nlTok(s); int i = indexOfAny(allToLower(tok), baseFormVerbs()); if (i < 0) return null; return triple(trimJoinSubList(tok, 0, i), tok.get(i), trimJoinSubList(tok, i + 1)); } public static String str(Object o) { return o == null ? "null" : o.toString(); } public static String str(char[] c) { return new String(c); } public static int maxRunlengthOfRepeatingChars(String s) { if (empty(s)) return 0; int max = 0; for (int i = 0; i < l(s); ) { char c = s.charAt(i); int j = i + 1; while (j < l(s) && s.charAt(j) == c) ++j; int n = j - i; max = max(max, j - i); i = j; } return max; } public static String collapseWord(String s) { if (s == null) return ""; StringBuilder buf = new StringBuilder(); for (int i = 0; i < l(s); i++) if (i == 0 || !charactersEqualIC(s.charAt(i), s.charAt(i - 1))) buf.append(s.charAt(i)); return buf.toString(); } public static List scoredSearch(String query, Iterable data) { Map scores = new HashMap(); List prepared = scoredSearch_prepare(query); for (String s : data) { int score = scoredSearch_score(s, prepared); if (score != 0) scores.put(s, score); } return keysSortedByValuesDesc(scores); } public static Set keys(Map map) { return map == null ? new HashSet() : map.keySet(); } public static Set keys(Object map) { return keys((Map) map); } public static Set keys(MultiSet ms) { return ms.keySet(); } public static Set keys(MultiMap mm) { return mm.keySet(); } public static Set keys(SmallestListMultiMap mm) { return mm.keySet(); } public static boolean gac3k_verbose; public static Cache> gac36k_cache = new Cache("gac36k_load"); public static TreeMap gac36k() { return gac36k_cache.get(); } public static TreeMap gac36k_load() { TreeMap map = new TreeMap(); for (String s : tlft(loadSnippet("#1010778"))) try { int i = indexOfAny(s, 0, " \t"); float f = parseFloat(takeFirst(i, s)); Float previous = mapPutIfChange2(map, trim(substring(s, i)), f); if (gac3k_verbose && previous != null) print("Gac-36K: Duplicate question: " + s + " (was: " + previous + ")"); } catch (Throwable __e) { _handleException(__e); } print("GAC-36K: Have " + n(map, "question")); return map; } public static String computerVMs_text() { return numberedLines(map("renderMap_colonComma", values(computerVMs_shorter()))); } public static Pair ai_countBoth_dollarX(T3 t) { return pair(l(ai_search_dollarX_verified(t)), l(ai_search_dollarX(t))); } public static String ai_sfCommands(String s) { Matches m = new Matches(); if (swic_trim(s, "!sfargs ", m)) return joinWithComma(allToString(numbersOfStandardFunctionArguments(m.rest()))); if (swic_trim(s, "!callsf ", m)) { String name = m.rest(); assertIdentifier(name); return sfu(callAndMakeOrDirect_safe(name)); } return null; } public static Pair> macmillanDefinitions3(String word) { String url = "https://www.macmillandictionary.com/search/british/direct/?q=" + urlencode(word); String html = loadPageWithUserAgent(url, "Mac Safari"); List tok = htmlTok(html); return pair(trimJoin(dropTagsAndHTMLComments(unnull(first(findContainerTagWithParamsDeep(tok, "span", "class", "BASE"))))), (List) map(fg("trimJoin", "dropTagsAndHTMLComments"), findContainerTagWithParamsDeep(tok, "span", "class", "DEFINITION"))); } public static String toUpper(String s) { return s == null ? null : s.toUpperCase(); } public static List toUpper(Collection s) { return allToUpper(s); } public static List prependAll(final String s, Collection l) { return map(new F1() { public String get(String x) { try { return s + x; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "s + x"; } }, l); } public static void printShortException(Throwable e) { print(exceptionToStringShort(e)); } public static String ai_answerFromCache(String q) { for (int _repeat_472 = 0; _repeat_472 < 10; _repeat_472++) { { String _a_474 = ai_renderNodesList(ai_search_dollarX_verified("$X", "answers", q), null, " or "); if (!empty(_a_474)) return _a_474; } { String _a_475 = ai_standardAnswer(q); if (!empty(_a_475)) return _a_475; } WebNode n = first(ai_search_dollarX_verified(q, "see answer to:", "$X")); if (n != null) q = web_text(n); else break; } return null; } public static List ai_dropLeadingAdditions_list = ll("also", "tell me"); public static String ai_dropLeadingAdditions(String s) { s = dropLeadingPunctuation(s); Matches m = new Matches(); for (String a : ai_dropLeadingAdditions_list) { if (startsWithWords(s, a, m)) s = m.rest(); } return dropLeadingPunctuation(s); } public static String n2(long l) { return formatWithThousands(l); } public static String n2(long l, String singular, String plural) { return n_fancy2(l, singular, plural); } public static String n2(long l, String singular) { return n_fancy2(l, singular, singular + "s"); } public static String n2(Collection l, String singular) { return n2(l(l), singular); } public static String n2(Collection l, String singular, String plural) { return n_fancy2(l, singular, plural); } public static String n2(Map m, String singular, String plural) { return n_fancy2(m, singular, plural); } public static String n2(Object[] a, String singular, String plural) { return n_fancy2(a, singular, plural); } public static String n2(MultiSet ms, String singular, String plural) { return n_fancy2(ms, singular, plural); } public static Set indexedTerms() { return ai_mainIndexKeys(); } public static boolean matchStartX(String pat, String s, Matches matches) { return matchStart(dropSuffix("...", pat), s, matches); } public static String ai_renderList(List l) { return ai_renderList(l, "-"); } public static String ai_renderList(List l, String defaultValue) { if (empty(l)) return defaultValue; return joinWithComma(allToString(l)); } public static List ai_subtypesOf(String type) { return web_uniqueTexts(webs_search_dollarX(webFromTriples("$X", "is a type of", type), indexedWebs(type))); } public static Random random_random = new Random(); public static int random(int n) { return n <= 0 ? 0 : random_random.nextInt(n); } public static double random(double max) { return random() * max; } public static double random() { return random_random.nextInt(100001) / 100000.0; } public static double random(double min, double max) { return min + random() * (max - min); } public static int random(int min, int max) { return min + random(max - min); } public static A random(List l) { return oneOf(l); } public static A random(Collection c) { if (c instanceof List) return random((List) c); int i = random(l(c)); return collectionGet(c, i); } public static Cache> thirdPersonVerbs_cache = new Cache("thirdPersonVerbs_load"); public static Set thirdPersonVerbs() { return thirdPersonVerbs_cache.get(); } public static Set thirdPersonVerbs_load() { return new TreeSet(nthColumn(englishVerbQuintuples(), 3)); } public static boolean learnedFlexMatch(String pattern, String s) { return learnedFlexMatch(pattern, s, null); } public static boolean learnedFlexMatch(String pattern, String s, Matches m) { if (!flexMatchIC(dropPunctuationAtEnd(pattern), dropPunctuationAtEnd(s), m)) return false; MatchLearner l = uniq(MatchLearner.class, "pattern", pattern); String status = l.examples.get(s); if (eq(status, "rejected")) return false; if (empty(status)) l.examples.put(s, "assumed"); return true; } public static Map singular_specials = litmap("children", "child", "images", "image", "chess", "chess"); public static Set singular_specials2 = litset("time", "machine", "line"); public static String singular(String s) { if (s == null) return null; { String _a_490 = singular_specials.get(s); if (!empty(_a_490)) return _a_490; } if (singular_specials2.contains(dropSuffix("s", afterLastSpace(s)))) return dropSuffix("s", s); if (s.endsWith("ness")) return s; if (s.endsWith("ges")) return dropSuffix("s", s); if (endsWith(s, "bases")) return dropLast(s); s = dropSuffix("es", s); s = dropSuffix("s", s); return s; } public static String ai_renderNodesList(List l) { return ai_renderNodesList(l, "-"); } public static String ai_renderNodesList(List l, String defaultText) { return empty(l) ? defaultText : joinWithComma(map("web_text", l)); } public static String ai_renderNodesList(List l, String defaultText, String joiner) { return empty(l) ? defaultText : join(joiner, map("web_text", l)); } public static List concatLists(Collection... lists) { List l = new ArrayList(); for (Collection list : lists) if (list != null) l.addAll(list); return l; } public static List concatLists(Collection> lists) { List l = new ArrayList(); for (Collection list : lists) if (list != null) l.addAll(list); return l; } public static List ai_index_search_dollarX(String a, String b, String c) { String query = isDollarVar(a) ? c : a; return webs_tripleSearch_dollarX(a, b, c, ai_search_indexedWebs(query)); } public static List ai_index_search_dollarX(T3 t) { return ai_index_search_dollarX(t.a, t.b, t.c); } public static String getSingularFromWordHippo(String word) { word = tolower(word); Map hippoSingulars = pairsToMap((List>) scanStructureLog("#1011041", "singulars")); { String _a_503 = hippoSingulars.get(word); if (!empty(_a_503)) return _a_503; } String html = loadPage("https://www.wordhippo.com/what-is/the-singular-of/" + urlencode(word) + ".html"); String pat = "The singular form of " + word + " is"; int i = indexOfIC(html, pat); if (i < 0) { print("no singular: " + html); return null; } i += l(pat); int j = indexOf(html, "", i); if (j < 0) return null; String singular = trim(dropTags(substring(html, i, j))); logStructure("#1011041", "singulars", pair(word, singular)); return singular; } public static List ai_getRelation(String a, String b) { LinkedHashSet relations = new LinkedHashSet(); for (WebNode n : indexedNodes(a)) relations.addAll(web_texts(web_relationToNodeNamed(n, b))); return asList(relations); } public static boolean nempty(Collection c) { return !isEmpty(c); } public static boolean nempty(CharSequence s) { return !isEmpty(s); } public static boolean nempty(Object[] o) { return !isEmpty(o); } public static boolean nempty(byte[] o) { return !isEmpty(o); } public static boolean nempty(Map m) { return !isEmpty(m); } public static boolean nempty(Iterator i) { return i != null && i.hasNext(); } public static boolean nempty(Object o) { return !empty(o); } public static boolean containsAll(Collection a, Collection b) { for (A o : b) if (!a.contains(o)) return false; return true; } public static boolean containsAll(Collection a, A... b) { for (A o : b) if (!a.contains(o)) return false; return true; } public static TreeSet asCISet(Collection c) { return toCaseInsensitiveSet(c); } public static TreeSet asCISet(String... x) { return toCaseInsensitiveSet(x); } public static int callAnswerMethod_version; public static String callAnswerMethod(String s) { if (callAnswerMethod_version == 0) if (hasMethod(mc(), "answer", "", new ArrayList())) callAnswerMethod_version = 1; else if (hasMethod(mc(), "answer", "")) callAnswerMethod_version = 2; else callAnswerMethod_version = -1; if (callAnswerMethod_version == 1) return (String) callMC("answer", s, emptyList()); else if (callAnswerMethod_version == 2) return (String) callMC("answer", s); return null; } public static String callAnswerMethod(Object responder, String s) { return callAnswerMethod(responder, s, litlist(s)); } public static String callAnswerMethod(Object responder, String s, List history) { String answer = (String) callOpt(responder, "answer", s, history); if (answer == null) answer = (String) callOpt(responder, "answer", s); return emptyToNull(answer); } public static Class hotwireOnce(String programID) { return hotwireCached(programID, false); } public static long unixDay() { return unixDay(now()); } public static long unixDay(long timestamp) { return unixHour(timestamp) / 24; } public static List> webs_search_dollarXY(final Web searchWeb, Collection webs) { List> mappings = new ArrayList(); for (Web web : webs) addIfNotNull(mappings, web_matchAllPerms_x(searchWeb, web, "nodeMatch_dollarVars")); return map(mappings, new F1, Object>() { public Object get(Map map) { try { map = web_addRelationsToMapping(map); print("xy map: " + map); return pair(map.get(web_findNodeOrRelation(searchWeb, "$X")), map.get(web_findNodeOrRelation(searchWeb, "$Y"))); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "map = web_addRelationsToMapping(map);\r\n print(\"xy map: \" + map);\r\n ret ..."; } }); } public static Web webFromTriples(String... l) { Web web = simpleWeb(); for (int i = 0; i < l(l); i += 3) { String a = unnull(asString(get(l, i))), b = unnull(asString(get(l, i + 1))), c = unnull(asString(get(l, i + 2))); web_addRelation(web, a, c, b); } return web; } public static Web webFromTriples(List l) { return webFromTriples(toSymbolArray(l)); } public static boolean learnedFlexMatch_multi(String s, String... patterns) { return learnedFlexMatch_multi(s, null, patterns); } public static boolean learnedFlexMatch_multi(String s, Matches m, String... patterns) { for (String pattern : patterns) if (learnedFlexMatch(pattern, s, m)) return true; return false; } public static List ai_whatIs(String s) { return uniquify(concatLists(ai_texts(s, "is", "$X"), ai_texts("$X", "is", s))); } public static int ai1_cookiesToday_int() { return parseFirstInt(ai1_cookiesToday()); } public static List indexedWebsAfterVariableSubstitution(String s) { return nodesToWebs(indexedNodesAfterVariableSubstitution(s)); } public static List webs_search_dollarX(Web searchWeb, Collection webs) { List> mappings = new ArrayList(); for (Web web : webs) addIfNotNull(mappings, web_matchAllPerms_x(searchWeb, web, "nodeMatch_dollarVars")); return getFromAll(mappings, web_findNode(searchWeb, "$X")); } public static String ai_tryIfAndInput(String s) { for (String rule : fullIndexedTermsStartingWith("[[if ")) try { if (ai_isRuleSuitableForFunction(rule, "ai_ifAndInput")) try { { String _a_538 = ai_ifAndInput(rule, s); if (!empty(_a_538)) return _a_538; } } catch (Throwable __e) { _handleException(__e); } } catch (Throwable __e) { _handleException(__e); } return null; } public static String ai_autoApplyRules(String input) { try { AutoCloseable __539 = temp_ai_setVar("#input", input); try { List rules = ai_texts_verified("$X", "is", "an auto-apply rule"); ai_prepLoadFunctions(); Class c = loadFunctions_cached(rules); for (String ruleName : rules) { print("Calling rule function: " + ruleName); { String _a_542 = (String) pcallOpt(c, ruleName); if (!empty(_a_542)) return _a_542; } } return null; } finally { _close(__539); } } catch (Exception __e) { throw rethrow(__e); } } public static T3 triple(A a, B b, C c) { return new T3(a, b, c); } public static String ai_standardAnswerEval(String s) { String code = unquote(random(ai_texts_verified("$X", "is a good eval for user input", quote(s)))); if (code != null) return smartBotEval(code); return null; } public static Object evalWithTimeoutOrNull(final Object f, int timeoutMS) { return evalWithTimeoutOrNull(timeoutMS, f); } public static Object evalWithTimeoutOrNull(int timeoutMS, final Object f) { return eitherAOpt(evalWithTimeout(timeoutMS, f)); } public static Object evalWithTimeoutOrNull(double timeoutSeconds, final Object f) { return eitherAOpt(evalWithTimeout(timeoutSeconds, f)); } public static A evalWithTimeoutOrNull(int timeoutMS, F0 f) { return (A) eitherAOpt(evalWithTimeout(timeoutMS, f)); } public static A evalWithTimeoutOrNull(double timeoutSeconds, F0 f) { return (A) eitherAOpt(evalWithTimeout(timeoutSeconds, f)); } public static Object pcallF(Object f, Object... args) { return pcallFunction(f, args); } public static A pcallF(F0 f) { try { return f == null ? null : f.get(); } catch (Throwable __e) { return null; } } public static B pcallF(F1 f, A a) { try { return f == null ? null : f.get(a); } catch (Throwable __e) { return null; } } public static String ai_standardAnswerSF_triple_dollarVars(T3 t) { for (Pair p : ai_texts_verified_dollarXY("$X", "is a good standard function for user triple", "$Y")) { Map match = simpleMatchTriple_dollarVars(ai_parseTriple(p.b), t); if (match != null) return smartBotEval(assertIdentifier(p.a) + "(" + tripleToJava(t) + ")"); } return null; } public static boolean swicOneOf(String s, String... l) { for (String x : l) if (swic(s, x)) return true; return false; } public static Class runSubBot(String id) { Class c = hotwire(id); setOpt(c, "sleep_noSleep", true); setOpt(c, "readLine_noReadLine", true); try { callMain(c); } catch (Throwable e) { if (!eq(getInnerMessage(e), "nosleep")) printStackTrace(e); } return c; } public static boolean profilePoint_print; public static void profilePoint(String name) { if (profilePoint_print) print(localDateWithMilliseconds() + ": profile point " + currentThreadID() + " " + name); } public static String clientIP() { return getClientIP(); } public static int simpleSpamClientDetect2_interval = 60000; public static double simpleSpamClientDetect2_perSecondThreshold = 2.0; public static int simpleSpamClientDetect2_spamMax; public static Map, Pair> simpleSpamClientDetect2_ipsAndUris = synchroMap(); public static Pair simpleSpamClientDetect2(String clientIP, String uri) { Pair pair = pair(clientIP, uri); Pair value = simpleSpamClientDetect2_ipsAndUris.get(pair); if (value == null || now() > value.a + simpleSpamClientDetect2_interval) simpleSpamClientDetect2_ipsAndUris.put(pair, value = pair(now(), 1)); else value.b++; simpleSpamClientDetect2_spamMax = iround(simpleSpamClientDetect2_perSecondThreshold * simpleSpamClientDetect2_interval / 1000); return pair(value.b, value.b >= simpleSpamClientDetect2_spamMax); } public static String pcall_myFirewallIP() { try { return myFirewallIP(); } catch (Throwable __e) { return null; } } public static String post_verified(T3 t) { Boolean prev = setThreadLocal(ai_postTriple_verified, true); try { return ai_postTriple(t); } finally { setThreadLocal(ai_postTriple_verified, prev); } } public static String post_verified(String a, String b, String c) { return post_verified(t3(a, b, c)); } public static T3 t3(A a, B b, C c) { return new T3(a, b, c); } public static String dateWithTimeGMT(long time) { SimpleDateFormat format = simpleDateFormat_GMT("yyyy/MM/dd"); return format.format(time) + " " + formatGMT_24(time); } public static String dateWithTimeGMT() { return dateWithTimeGMT(now()); } public static void sleepSeconds(double s) { if (s > 0) sleep(round(s * 1000)); } public static String gmtWithSeconds() { return formatGMTWithSeconds_24(now()); } public static AutoCloseable temp_ai_startBuffering() { ai_startBuffering(); return new AutoCloseable() { public void close() { ai_stopBuffering(); } }; } public static String serveHttp_userAgent() { NanoHTTPD.IHTTPSession session = NanoHTTPD.currentSession.get(); if (session == null) return null; return session.getHeaders().get("user-agent"); } public static void _close(AutoCloseable c) { close(c); } public static int serveHttp_cookieHandling_defaultDays = 365; public static String serveHttp_cookieHandling_defaultCookieName = "cookie"; public static boolean serveHttp_cookieHandling_verbose; public static String serveHttp_cookieHandling() { NanoHTTPD.IHTTPSession session = NanoHTTPD.currentSession.get(); NanoHTTPD.CookieHandler cookies = session.getCookies(); String cookie = cookies.read("cookie"); if (cookie == null) { if (empty((String) mapGet(MyHTTPD_headers.get(), "x-no-cookies"))) { cookie = randomID(20); if (serveHttp_cookieHandling_verbose) print("New cookie."); } } if (serveHttp_cookieHandling_verbose) print("Cookie: " + cookie); if (nempty(cookie)) cookies.set("cookie", cookie, serveHttp_cookieHandling_defaultDays); return cookie; } public static List standaloneWebAuth_authorizedCookies; public static Lock standaloneWebAuth_lock = lock(); public static Pair standaloneWebAuth(String url, String cookie, String pw) { Lock __583 = standaloneWebAuth_lock; lock(__583); try { if (empty(cookie)) return pair(null, false); String realPW = standaloneWebAuth_realPW(); if (empty(realPW)) { print("No password set by admin - please put password in: " + standaloneWebAuth_realPWFile().getAbsolutePath()); return pair(null, false); } if (nempty(pw)) { if (eqic(pw, "logout")) { standaloneWebAuth_authorizedCookies().remove(cookie); return pair("OK, logged out.", false); } if (neq(pw, realPW)) { logQuotedWithDate("bad-login-attempts.txt", print("XXX - Alert, bad PW entered on web - XXX - IP: " + clientIP())); return pair("Bad PW, bugger", false); } standaloneWebAuth_authorizedCookies.add(cookie); save("standaloneWebAuth_authorizedCookies"); return pair(null, true); } boolean in = contains(standaloneWebAuth_authorizedCookies(), cookie); if (in) return pair(null, true); return pair(null, false); } finally { unlock(__583); } } public static String standaloneWebAuth_showForm(String url, String cookie) { boolean in = nempty(cookie) && contains(standaloneWebAuth_authorizedCookies(), cookie); String formContents = tag("p", "Enter deh pass word: " + htag("input", "", "type", "password", "name", "pw")); formContents += tag("input", "", "type", "submit", "value", "Submit!"); return p((in ? "You're in." : "You're not in.") + " " + (nempty(cookie) ? "Have cookie." : "No cookie.")) + tag("form", formContents, "method", "POST", "action", url) + tag("form", hhidden("pw", "logout") + hsubmit("Log out"), "method", "POST", "action", url); } public static File standaloneWebAuth_realPWFile() { return secretProgramFile("master-pw"); } public static String standaloneWebAuth_realPW() { return trim(loadTextFile(standaloneWebAuth_realPWFile())); } public static List standaloneWebAuth_authorizedCookies() { Lock __585 = standaloneWebAuth_lock; lock(__585); try { if (standaloneWebAuth_authorizedCookies == null) { standaloneWebAuth_authorizedCookies = new ArrayList(); load("standaloneWebAuth_authorizedCookies"); } return standaloneWebAuth_authorizedCookies; } finally { unlock(__585); } } public static int done2_minPrint = 10; public static long done2(long startTime, String desc) { return done2(startTime, desc, done2_minPrint); } public static long done2(long startTime, String desc, int minPrint) { long time = sysNow() - startTime; saveTiming_noPrint(time); if (time >= minPrint) print(desc + " [" + time + " ms]"); return time; } public static long done2(String desc, long startTime) { return done2(startTime, desc); } public static long done2(long startTime) { return done2(startTime, ""); } public static String serveHttp_domainName() { NanoHTTPD.IHTTPSession session = NanoHTTPD.currentSession.get(); if (session == null) return null; return session.getHeaders().get("host"); } public static boolean ewic(String a, String b) { return endsWithIgnoreCase(a, b); } public static boolean ewic(String a, String b, Matches m) { return endsWithIgnoreCase(a, b, m); } public static NanoHTTPD.Response serveFile(File file) { return serveFile(file, "application/octet-stream"); } public static NanoHTTPD.Response serveFile(File file, String mimeType) { try { return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Status.OK, mimeType, new FileInputStream(file), file.length()); } catch (Exception __e) { throw rethrow(__e); } } public static File loadLibrary(String snippetID) { return loadBinarySnippet(snippetID); } public static String hrefresh(String target) { return hrefresh(0, target); } public static String hrefresh(double seconds) { return hrefresh(seconds, ""); } public static String hrefresh(double seconds, String target) { return tag("meta", "", "http-equiv", "refresh", "content", iceil(seconds) + (nempty(target) ? "; url=" + target : "")); } public static String smartBotDirectURL() { return smartBotURL(); } public static String htmlencode(Object o) { return htmlencode(str(o)); } public static String htmlencode(String s) { if (s == null) return ""; StringBuilder out = new StringBuilder(Math.max(16, s.length())); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c > 127 || c == '"' || c == '<' || c == '>' || c == '&') out.append("&#").append((int) c).append(';'); else out.append(c); } return out.toString(); } public static String callAnswerMethodVerbose(String s) { return callAnswerFunctionVerbose(s); } public static String urldecode(String x) { try { return URLDecoder.decode(unnull(x), "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } public static boolean eqicOptSlash(String a, String b) { int n = lCommonPrefixIC(a, b); int la = l(a), lb = l(b); if (n == la && n == lb) return true; if (n == lb && n == la - 1 && lastChar(a) == '/') return true; if (n == la && n == lb - 1 && lastChar(b) == '/') return true; return false; } public static volatile boolean loading_value = true; public static boolean loading() { return loading_value; } public static int smartIndexOf(String s, String sub, int i) { if (s == null) return 0; i = s.indexOf(sub, min(i, l(s))); return i >= 0 ? i : l(s); } public static int smartIndexOf(String s, int i, char c) { return smartIndexOf(s, c, i); } public static int smartIndexOf(String s, char c, int i) { if (s == null) return 0; i = s.indexOf(c, min(i, l(s))); return i >= 0 ? i : l(s); } public static int smartIndexOf(String s, String sub) { return smartIndexOf(s, sub, 0); } public static int smartIndexOf(String s, char c) { return smartIndexOf(s, c, 0); } public static int smartIndexOf(List l, A sub) { return smartIndexOf(l, sub, 0); } public static int smartIndexOf(List l, int start, A sub) { return smartIndexOf(l, sub, start); } public static int smartIndexOf(List l, A sub, int start) { int i = indexOf(l, sub, start); return i < 0 ? l(l) : i; } public static String fsI(String id) { return formatSnippetID(id); } public static String fsI(long id) { return formatSnippetID(id); } public static List takeFirst(List l, int n) { return l(l) <= n ? l : newSubList(l, 0, n); } public static List takeFirst(int n, List l) { return takeFirst(l, n); } public static String takeFirst(int n, String s) { return substring(s, 0, n); } public static String takeFirst(String s, int n) { return substring(s, 0, n); } public static List takeFirst(int n, Iterable i) { List l = new ArrayList(); Iterator it = i.iterator(); for (int _repeat_612 = 0; _repeat_612 < n; _repeat_612++) { if (it.hasNext()) l.add(it.next()); else break; } return l; } public static boolean has_verified(String a, String b, String c) { return ai_cache_hasTriple_verified(a, b, c); } public static boolean has_verified(T3 t) { return ai_cache_hasTriple_verified(t); } public static TreeMap hotwireCached_cache = new TreeMap(); public static Lock hotwireCached_lock = lock(); public static Class hotwireCached(String programID) { return hotwireCached(programID, true); } public static Class hotwireCached(String programID, boolean runMain) { return hotwireCached(programID, runMain, false); } public static Class hotwireCached(String programID, boolean runMain, boolean dependent) { Lock __618 = hotwireCached_lock; lock(__618); try { programID = formatSnippetID(programID); Class c = hotwireCached_cache.get(programID); if (c == null) { c = hotwire(programID); if (dependent) makeDependent(c); if (runMain) callMain(c); hotwireCached_cache.put(programID, c); } return c; } finally { unlock(__618); } } public static Object callHtmlMethod2(Object o, String uri) { return callHtmlMethod2(o, uri, null); } public static Object callHtmlMethod2(Object o, String uri, Map params) { Object s = callOpt(o, "html", uri, params); if (s == null) s = callOpt(o, "html", uri); if (s == null) s = callOpt(o, "html"); return s; } public static String substring(String s, int x) { return substring(s, x, l(s)); } public static String substring(String s, int x, int y) { if (s == null) return null; if (x < 0) x = 0; if (x >= s.length()) return ""; if (y < x) y = x; if (y > s.length()) y = s.length(); return s.substring(x, y); } public static String htmlencode2(String s) { return htmlencode_noQuotes(s); } public static String struct(Object o) { return structure(o); } public static String struct(Object o, structure_Data data) { return structure(o, data); } public static Object callSafeSF(String functionName, Object... args) { return callAndMakeOrDirect_safe(functionName, asList(args)); } public static TripleWeb randomIndexedTriple() { return random(ai_allTriples()); } public static String renderRunnableThreadsWithStackTraces() { return renderUserThreadsWithStackTraces(runnableThreadsWithStackTraces(), "running thread"); } public static String hpre(Object contents, Object... params) { return tag("pre", contents, params); } public static String formatDouble(double d, int digits) { String format = "0." + rep(digits, '#'); return new java.text.DecimalFormat(format, new java.text.DecimalFormatSymbols(Locale.ENGLISH)).format(d); } public static double systemLoad() { return ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage(); } public static String solveDeadlocks() { return hijackPrint("printDeadlocksAndSolve"); } public static String h1_title(String s) { return htitle_h1(s); } public static String multiSetToUL(final MultiSet ms) { return ul_htmlencode(map(multiSetKeysByPopularity(ms), new F1() { public String get(String s) { try { return "[" + ms.get(s) + "] " + s; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "\"[\" + ms.get(s) + \"] \" + s"; } })); } public static MultiSet ai_sourceStats() { MultiSet ms = new MultiSet(); for (TripleWeb w : tripleIndex().allTriples()) ms.add(w.source()); return ms; } public static NanoHTTPD.Response serveHTML(String text) { return NanoHTTPD.newFixedLengthResponse(String.valueOf(text)); } public static NanoHTTPD.Response serveHTML(StringBuilder buf) { return NanoHTTPD.newFixedLengthResponse(buf.toString()); } public static NanoHTTPD.Response serveHTML(StringBuffer buf) { return NanoHTTPD.newFixedLengthResponse(buf.toString()); } public static NanoHTTPD.Response serveJPEG(BufferedImage img) { return serveByteArray(toJPG(img), "image/jpeg"); } public static NanoHTTPD.Response serveJPEG(File file) { return serveFile(file, "image/jpeg"); } public static class WebToCAL { public Web web; public BufferedImage img = newBufferedImage(20, 20, Color.white); public CirclesAndLines cal = new CirclesAndLines(); public Map map = new HashMap(); public String makeLabel(Lisp lisp) { return or(lispHead(lisp), "[null]"); } public void convert(final Web web) { this.web = web; cal.title = web.title; List nodes = web.nodes; nodes = listMinusSet(nodes, web_relationObjects(web)); for (WebNode node : nodes) { double x = node.x, y = node.y; if (x == 0 && y == 0) { x = cal_randomCircleCoord(); y = cal_randomCircleCoord(); } String text = makeLabel(first(node.labels())); String quickvis = null; if (node.visInfo() instanceof String) quickvis = (String) node.visInfo(); if (empty(quickvis)) quickvis = webToCAL_visMap.get(text); Circle c; if (nempty(quickvis)) c = cal.circle_autoVis(text, quickvis, x, y); else c = cal.circle(img, x, y, text); map.put(node, c); for (Lisp l : dropFirst(node.labels())) c.addTrait(makeLabel(l)); } for (Pair p : web_relations(web)) { WebNode rel = web.getRelation(p); if (empty(rel.labels())) continue; Circle a = map.get(p.a), b = map.get(p.b); if (a == null || b == null) { warn("webToCAL: Circle not found"); continue; } if (a == b) continue; Line line = cal.arrow(a, makeLabel(first(rel.labels())), b); for (Lisp l : dropFirst(rel.labels())) line.addTrait(makeLabel(l)); } cal.onUserMadeCircle = new VF1() { public void get(Circle c) { try { map.put(web.newNode(c.text), c); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "map.put(web.newNode(c.text), c);"; } }; cal.onDeleteCircle = new VF1() { public void get(Circle c) { try { web.removeNode(reverseLookup(map, c)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "web.removeNode(reverseLookup(map, c));"; } }; cal.onDeleteLine = new VF1() { public void get(Line l) { try { web.removeRelation(reverseLookup(map, l.a), reverseLookup(map, l.b)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "web.removeRelation(reverseLookup(map, l.a), reverseLookup(map, l.b));"; } }; cal.onUserMadeArrow = new VF1() { public void get(Arrow a) { try { web.getRelation(reverseLookup(map, a.a), reverseLookup(map, a.b)).addLabel(a.text); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "web.getRelation(reverseLookup(map, a.a), reverseLookup(map, a.b)).addLabel(a...."; } }; cal.onLayoutChange = new VF1() { public void get(Circle c) { try { WebNode node = reverseLookup(map, c); updateLayout(node); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "WebNode node = reverseLookup(map, c);\r\n //print(\"layout change: \" + c.te..."; } }; cal.onFullLayoutChange = new Runnable() { public void run() { try { for (WebNode web : keys(map)) updateLayout(web); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "//print(\"full layout change\");\r\n for (WebNode web : keys(map))\r\n ..."; } }; } public void updateLayout(WebNode node) { if (node == null) return; Circle c = map.get(node); if (c == null) return; node.x = c.x; node.y = c.y; } } public static CirclesAndLines webToCAL(WebNode node) { return webToCAL(node.web); } public static CirclesAndLines webToCAL(Web web) { WebToCAL wtc = new WebToCAL(); wtc.convert(web); return wtc.cal; } public static void webToCAL(Web web, CirclesAndLines cal, Canvas canvas) { copyCAL(webToCAL(web), cal); canvas.update(); } public static NanoHTTPD.Response serveText_direct(String s) { return serveByteArray(toUtf8(s), "text/plain; charset=utf8"); } public static String printLog() { return str(local_log); } public static String renderConcepts(Collection l) { return fromLines(map(l, new F1() { public String get(Concept c) { try { return renderConcept(c); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "renderConcept(c)"; } })); } public static String h3_title(String s) { return h3_htitle(s); } public static String ul(String... list) { return ul(asList(list)); } public static String ul(List list, Object... params) { StringBuilder buf = new StringBuilder(); int i = indexOf(params, null); if (i == -1) i = l(params); for (String s : withoutNulls(list)) buf.append(tag("li", s, subArray(params, i + 1))).append("\n"); return containerTag("ul", buf, subArray(params, 0, i)) + "\n"; } public static Collection ai_gc_unreachedWords() { Set unreached = caseInsensitiveSet(); Map distances = ciMap(); for (WebRelation r : indexedRelations("has GC distance")) try { distances.put(web_text(r.a), parseDouble(web_text(r.b))); } catch (Throwable __e) { _handleException(__e); } for (String text : allIndexedFullTerms()) { { if (unreached.contains(text)) continue; } if (!distances.containsKey(text)) unreached.add(text); } return unreached; } public static String dropPrefix(String prefix, String s) { return s == null ? null : s.startsWith(prefix) ? s.substring(l(prefix)) : s; } public static boolean possibleGlobalID(String s) { return l(s) == 16 && allLowerCaseCharacters(s); } public static List ai_getWebs(String id) { if (!possibleGlobalID(id)) return null; return ai_getWebs(asGlobalID(id)); } public static List ai_getWebs(GlobalID id) { if (id == null) return emptyList(); Web primary = ai_getWeb(id); if (primary == null) return emptyList(); List l = ll(primary); l.addAll(dropFirst(allWebsByID().get(id))); return l; } public static List ai_speculateOnWeb_functions = ll("ai_spec_include", "ai_spec_relRewrite", "ai_spec_priorityToNormal"); public static void ai_speculateOnWeb(Web web) { if (web == null) return; for (String f : ai_speculateOnWeb_functions) ai_callSpeculationFunctionOnWeb(f, web); } public static String h2_title(String s) { return htitle_h2(s); } public static boolean ai_isInvalidWeb(Web web) { return ai_invalidatedWebs().contains(web.globalID); } public static boolean ai_isInvalidWeb(String id) { return ai_invalidatedWebs().contains(id); } public static String loadjquery() { return loadJQuery2(); } public static String pre(Object contents, Object... params) { return tag("pre", contents, params); } public static String renderWeb_multiLine(final Web web) { return hijackPrint(new Runnable() { public void run() { try { printWeb(web); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "printWeb(web)"; } }); } public static String p(Object contents, Object... params) { return tag("p", contents, params) + "\n"; } public static String himg(String src, Object... params) { return tag("img", "", arrayPlus(params, "src", src)); } public static String ai_webImage_link(String globalID) { return smartBotOfficialURL() + "/diagram?id=" + urlencode(globalID); } public static boolean ai_html_wordThoughts_showMem = true; public static int ai_html_wordThoughts_searchMax = 100; public static int ai_html_wordThoughts_nodesMax_default = 1000; public static ThreadLocal ai_html_wordThoughts_nodesMax = new ThreadLocal(); public static String ai_html_wordThoughts(String selectedWord) { return ai_html_wordThoughts(ll(selectedWord)); } public static String ai_html_wordThoughts(List selectedWords) { Lock __697 = aiLock(); lock(__697); try { int shrt = 80; Integer oldShrt = setThreadLocal(ai_html_shortenTextForDisplay_length, shrt); try { String t = ai_html_triangle(); String selectedWord = first(selectedWords); String title = p(span(htmlencode(selectedWord), "id", "title", "style", "font-size: 25px") + " " + ahref(smartBot_matrixLink(selectedWord), "[Matrix]")); Matches m = new Matches(); if (swic_trim(selectedWord, "[search]", m)) { int max = ai_html_wordThoughts_searchMax; List l = indexedTerms_scoredSearch(m.rest(), max + 1); return title + p(l(l) > ai_html_wordThoughts_searchMax ? max + "+ search results" : n(l, "search result")) + ul(map("html_encyclopediaTopic", takeFirst(max, l))); } List nodes = new ArrayList(); int max = or(ai_html_wordThoughts_nodesMax.get(), ai_html_wordThoughts_nodesMax_default); Set ignoredWebs = ai_ignoredWebs(); for (String word : selectedWords) { nodes.addAll(web_nodesNotFromCertainWebs(ignoredWebs, indexedNodes(word))); if (l(nodes) >= max) break; } int realN = l(nodes); truncateList(nodes, max); List rel1 = web_dropConfirmedPossibles(web_collectBackwardRelations(nodes)); List rel2 = web_dropConfirmedPossibles(web_collectForwardRelations(nodes)); final MultiMap mm1 = orderedMultiMap(); final MultiMap mm2 = orderedMultiMap(); final MultiMap mm3 = orderedMultiMap(); for (boolean invalid : ll(false, true)) { for (WebRelation r : ai_sortRelationsByImportance(ai_filterInvalid(invalid, rel2))) mm1.put(t + " " + ai_textHTML(r) + t + ai_renderNodeHTML(r.b), r.web.globalID()); for (WebRelation r : ai_sortRelationsByImportance(ai_filterInvalid(invalid, rel1))) mm2.put(ai_renderNodeHTML(r.a) + t + ai_textHTML(r) + t + ai_textHTML(r.b), r.web.globalID()); if (!invalid) for (WebNode n : nodes) if (n instanceof WebRelation) mm3.put(ai_renderNodeHTML(((WebRelation) n).a) + t + ai_textHTML(n) + t + ai_textHTML(((WebRelation) n).b), n.web.globalID()); } List l1 = ll(); Web web = ai_getWebFromTerm(selectedWord); if (web != null) { String link = ai_html_webLink(web.globalID()); boolean inv = ai_isInvalidWeb(web); l1.add(inv ? "is an " + ahref(link, "invalid web") : "is a " + ahref(link, "web")); } List lists = new ArrayList(); if (nempty(l1)) lists.add(ul(l1)); for (final MultiMap mm : ll(mm1, mm2, mm3)) lists.add(ul(map("html_linkURLs", allToString(map(multiMapKeysByPopularity(mm), new F1() { public Object get(String s) { try { List webIDs = mm.get(s); return s + " [" + joinWithComma(map("ai_html_linkedWebWithThumbs", webIDs)) + "]"; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "L webIDs = mm.get(s);\r\n ret s + \" [\" + joinWithComma(map ai_htm..."; } }))))); long mem = ai_html_wordThoughts_showMem ? memoryUsedAfterLastGC() : 0; return title + (realN > max ? p("List truncated (" + n(realN, "total node") + ")") : "") + lines(lists) + p(n(nodes, "node") + " out of " + ahref(smartBotOfficialURL(), nWebs(ai_allWebsCount())) + (loading() ? " (LOADING)" : "") + (mem == 0 ? "" : ". " + toMB(mem) + " MB used. " + toM(freeSystemMemory()) + " MB free")); } finally { ai_html_shortenTextForDisplay_length.set(oldShrt); } } finally { unlock(__697); } } public static LinkedHashMap litorderedmap(Object... x) { LinkedHashMap map = new LinkedHashMap(); litmap_impl(map, x); return map; } public static List indexedRelations(String s) { return web_relationsOnly(indexedNodes(s)); } public static List tripleToList(T3 t) { return t == null ? null : ll(t.a, t.b, t.c); } public static T3 web_relationToTriple(WebRelation r) { return r == null ? null : t3(web_text(r.a), web_text(r), web_text(r.b)); } public static Object serveText(Object s) { if (getMainBot() == null) return serveText_direct(str(s)); return call(getMainBot(), "serveByteArray", toUtf8(str(s)), "text/plain; charset=utf8"); } public static String jsonEncode(Object o) { StringBuilder buf = new StringBuilder(); jsonEncode(o, buf); return str(buf); } public static void jsonEncode(Object o, StringBuilder buf) { if (o == null) buf.append("null"); else if (o instanceof String) buf.append(quote((String) o)); else if (o instanceof Number || o instanceof Boolean) buf.append(o); else if (o instanceof Map) { Map map = (Map) (o); buf.append("{"); boolean first = true; for (Object key : keys(map)) { if (first) first = false; else buf.append(","); buf.append(quote((String) key)); buf.append(":"); jsonEncode(map.get(key), buf); } buf.append("}"); } else if (o instanceof Collection) { Collection l = (Collection) (o); buf.append("["); boolean first = true; for (Object element : l) { if (first) first = false; else buf.append(","); jsonEncode(element, buf); } buf.append("]"); } else throw fail("Unknown object for JSON encoding: " + className(o)); } public static int min(int a, int b) { return Math.min(a, b); } public static long min(long a, long b) { return Math.min(a, b); } public static float min(float a, float b) { return Math.min(a, b); } public static float min(float a, float b, float c) { return min(min(a, b), c); } public static double min(double a, double b) { return Math.min(a, b); } public static double min(double[] c) { double x = Double.MAX_VALUE; for (double d : c) x = Math.min(x, d); return x; } public static float min(float[] c) { float x = Float.MAX_VALUE; for (float d : c) x = Math.min(x, d); return x; } public static byte min(byte[] c) { byte x = 127; for (byte d : c) if (d < x) x = d; return x; } public static short min(short[] c) { short x = 0x7FFF; for (short d : c) if (d < x) x = d; return x; } public static int min(int[] c) { int x = Integer.MAX_VALUE; for (int d : c) if (d < x) x = d; return x; } public static int parseInt(String s) { return empty(s) ? 0 : Integer.parseInt(s); } public static int parseInt(char c) { return Integer.parseInt(str(c)); } public static List ai_autoComplete(final String s, final int maxEntries, int timeout) { return (List) evalWithTimeoutOrNull(new F0() { public Object get() { try { return ai_autoComplete_impl(s, maxEntries); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ai_autoComplete_impl(s, maxEntries)"; } }, timeout); } public static List ai_autoComplete_impl(String s, int maxEntries) { return takeFirst(maxEntries, fullIndexedTermsStartingWith(s)); } public static String jsonEncode_breakAtLevel1(Object o) { StringBuilder buf = new StringBuilder(); jsonEncode_breakAtLevel1(o, buf, 1); return str(buf); } public static void jsonEncode_breakAtLevel1(Object o, StringBuilder buf, int level) { if (o == null) buf.append("null"); else if (o instanceof String) buf.append(quote((String) o)); else if (o instanceof Number || o instanceof Boolean) buf.append(o); else if (o instanceof Map) { Map map = (Map) (o); buf.append("{"); boolean first = true; for (Object key : keys(map)) { if (first) first = false; else buf.append(","); if (level == 1) buf.append("\n "); buf.append(quote((String) key)); buf.append(":"); jsonEncode_breakAtLevel1(map.get(key), buf, level + 1); } if (level == 1) buf.append("\n"); buf.append("}"); } else if (o instanceof Collection) { Collection l = (Collection) (o); buf.append("["); boolean first = true; for (Object element : l) { if (first) first = false; else buf.append(","); if (level == 1) buf.append("\n "); jsonEncode_breakAtLevel1(element, buf, level + 1); } if (level == 1) buf.append("\n"); buf.append("]"); } else throw fail("Unknown object for JSON encoding: " + className(o)); } public static int toInt(Object o) { if (o == null) return 0; if (o instanceof Number) return ((Number) o).intValue(); if (o instanceof String) return parseInt((String) o); throw fail("woot not int: " + getClassName(o)); } public static int toInt(long l) { if (l != (int) l) throw fail("Too large for int: " + l); return (int) l; } public static int max(int a, int b) { return Math.max(a, b); } public static int max(int a, int b, int c) { return max(max(a, b), c); } public static long max(int a, long b) { return Math.max((long) a, b); } public static long max(long a, long b) { return Math.max(a, b); } public static double max(int a, double b) { return Math.max((double) a, b); } public static float max(float a, float b) { return Math.max(a, b); } public static double max(double a, double b) { return Math.max(a, b); } public static int max(Collection c) { int x = Integer.MIN_VALUE; for (int i : c) x = max(x, i); return x; } public static double max(double[] c) { if (c.length == 0) return Double.MIN_VALUE; double x = c[0]; for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]); return x; } public static float max(float[] c) { if (c.length == 0) return Float.MAX_VALUE; float x = c[0]; for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]); return x; } public static byte max(byte[] c) { byte x = -128; for (byte d : c) if (d > x) x = d; return x; } public static short max(short[] c) { short x = -0x8000; for (short d : c) if (d > x) x = d; return x; } public static int max(int[] c) { int x = Integer.MIN_VALUE; for (int d : c) if (d > x) x = d; return x; } public static String formatWithThousandsSeparator(long l) { return NumberFormat.getInstance(new Locale("en_US")).format(l); } public static String smartBot_encyclopediaLink(String topic) { return smartBotOfficialURL() + "/e/" + urlencode(topic); } public static String ai_randomString() { return randomIndexedTerm(); } public static List allToString(Collection c) { List l = new ArrayList(); for (Object o : unnull(c)) l.add(str(o)); return l; } public static List allToString(Object[] c) { List l = new ArrayList(); for (Object o : unnull(c)) l.add(str(o)); return l; } public static List allWebIDs_cloned() { Lock __727 = aiLock(); lock(__727); try { return cloneList(keys(allWebsByID())); } finally { unlock(__727); } } public static long fileSize(String path) { return getFileSize(path); } public static long fileSize(File f) { return getFileSize(f); } public static long fileModificationTime(File f) { return f == null ? 0 : f.lastModified(); } public static File[] listFiles(File dir) { File[] files = dir.listFiles(); return files == null ? new File[0] : files; } public static File[] listFiles(String dir) { return listFiles(new File(dir)); } public static File programDir_mine; public static File programDir() { return programDir(getProgramID()); } public static File programDir(String snippetID) { boolean me = sameSnippetID(snippetID, programID()); if (programDir_mine != null && me) return programDir_mine; File dir = new File(javaxDataDir(), formatSnippetID(snippetID)); if (me) { String c = caseID(); if (nempty(c)) dir = newFile(dir, c); } return dir; } public static File programDir(String snippetID, String subPath) { return new File(programDir(snippetID), subPath); } public static boolean isProperFileName(String s) { return nempty(s) && !startsWith(s, ".") && regexpIC("[a-z0-9-._]+", s).matches(); } public static NanoHTTPD.Response serve404() { return serve404("Not Found"); } public static NanoHTTPD.Response serve404(String msg) { return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Status.NOT_FOUND, NanoHTTPD.MIME_PLAINTEXT, "Code 404 - " + msg); } public static File getProgramFile(String progID, String fileName) { if (new File(fileName).isAbsolute()) return new File(fileName); return new File(getProgramDir(progID), fileName); } public static File getProgramFile(String fileName) { return getProgramFile(getProgramID(), fileName); } public static List ai_texts(CharSequence _a, CharSequence _b, CharSequence _c) { String a = symbol(_a), b = symbol(_b), c = symbol(_c); int position = tripleIndexOf(a, b, c, symbol_dollarX()); if (position < 0) throw fail("No $X in " + ai_renderTriple(a, b, c)); if (ai_useThoughtSpaces()) return ai_texts(a, b, c, thoughtSpace()); return allToSymbol(web_texts(ai_search_dollarX(a, b, c))); } public static List ai_texts(String a, String b, String c, AbstractThoughtSpace ts) { return ai_tripleRefsToTexts(a, b, c, ai_thoughtSpace_query(a, b, c, ts)); } public static List ai_texts(T3 t) { return t == null ? sharedEmptyList() : ai_texts(t.a, t.b, t.c); } public static T3 ai_parseSlashTriple(String s) { List l = map("tok_unCurlyBracket", tok_splitAtSlash(tok_combineCurlyBrackets_keep(javaTok(s)))); if (l(l) == 2) l.add("."); return listToTriple(l); } public static List> ai_tripleRefs(T3 t) { return ai_tripleRefs(t.a, t.b, t.c); } public static List> ai_tripleRefs(String a, String b, String c) { return ai_tripleRefs(a, b, c, thoughtSpace()); } public static List> ai_tripleRefs(String a, String b, String c, AbstractThoughtSpace ts) { int position = tripleIndexOf(a, b, c, symbol_dollarX()); if (position < 0) throw fail("No $X in " + ai_renderTriple(a, b, c)); return ai_filterTripleRefs(position, a, b, c, ai_thoughtSpace_query(a, b, c, ts)); } public static String htitle_htmlencode(String title) { return htitle(title); } public static String joinMap(Object f, Iterable l) { return join(map(f, l)); } public static String joinMap(Iterable l, Object f) { return joinMap(f, l); } public static String unquoteMultiline(String s) { if (s == null) return null; if (startsWith(s, '[')) { int i = 1; while (i < s.length() && s.charAt(i) == '=') ++i; if (i < s.length() && s.charAt(i) == '[') { String m = s.substring(1, i); if (s.endsWith("]" + m + "]")) return s.substring(i + 1, s.length() - i - 1); } } return s; } public static String htmlencode_noQuote(String s) { return htmlencode_noQuotes(s); } public static boolean ai_speculateDeepWithActiveAndTimeout(final String word, double timeoutSeconds) { if (loading()) return false; return eitherIsB(evalWithTimeout(toMS(timeoutSeconds), new Runnable() { public void run() { try { ai_speculateDeepAndActive(word); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ai_speculateDeepAndActive(word)"; } })); } public static NanoHTTPD.Response serveHTMLNoCache(String html) { NanoHTTPD.Response response = serveHTML(html); response.addHeader("Cache-Control", "no-cache, must-revalidate, max-age=0"); return response; } public static String htitle(String title) { return tag("title", htmlencode_noQuotes(title)); } public static String h1(String s, Object... params) { return tag("h1", s, params); } public static String html_smallSmartBotLogo() { return ahref(smartBotOfficialURL(), "Smart Bot"); } public static String ahref(String link, Object contents, Object... params) { return href(link, contents, params); } public static String ai_html_enterTripleForm(String word) { return h3("Enter information") + hform(htextinput("a", "value", word) + " " + htextinput("b") + " " + htextinput("c") + " " + hsubmit("Submit"), "action", smartBotDirectURL() + "/submit-triple"); } public static String smartBot_directTopicLink(String topic) { return smartBotRealURL() + "/e/" + urlencode(topic); } public static AutoCloseable tempProfileBlock(final String name) { if (!profilePoint_print) return null; profilePoint(name); return new AutoCloseable() { public void close() { profilePoint("/" + name); } }; } public static String tag(String tag) { return htag(tag); } public static String tag(String tag, Object contents, Object... params) { return htag(tag, str(contents), params); } public static String tag(String tag, StringBuilder contents, Object... params) { return htag(tag, contents, params); } public static String tag(String tag, StringBuffer contents, Object... params) { return htag(tag, contents, params); } public static String hcenter3(Object contents) { return tag("div", contents, "style", "width: 100%; text-align: center"); } public static String hsnippetimg(String imageID, Object... params) { return himg(snippetImageLink(imageID), params); } public static String h3(String s, Object... params) { return tag("h3", s, params) + "\n"; } public static String form(Object contents, Object... params) { return hform(contents, params); } public static String htextfield(String name, Object... params) { return htextinput(name, params); } public static String hsubmit(String text, Object... params) { return tag("input", "", concatArrays(new Object[] { "type", "submit", "value", text }, params)); } public static String hsubmit() { return hsubmit("Submit"); } public static List multiSetByPopularity(MultiSet ms) { return ms == null ? null : ms.getSortedListDescending(); } public static List subList(List l, int startIndex) { return subList(l, startIndex, l(l)); } public static List subList(List l, int startIndex, int endIndex) { startIndex = Math.max(0, Math.min(l(l), startIndex)); endIndex = Math.max(0, Math.min(l(l), endIndex)); if (startIndex > endIndex) return litlist(); return l.subList(startIndex, endIndex); } public static String n_fancy(long l, String name) { return formatWithThousandsSeparator(l) + " " + trim(l == 1 ? singular(name) : getPlural(name)); } public static String n_fancy(Collection l, String name) { return n_fancy(l(l), name); } public static String n_fancy(Map m, String name) { return n_fancy(l(m), name); } public static String n_fancy(Object[] a, String name) { return n_fancy(l(a), name); } public static String n_fancy(MultiSet ms, String name) { return n_fancy(l(ms), name); } public static String pageNav2(String baseLink, int count, int value, int step, String nVar) { List l = new ArrayList(); baseLink += contains(baseLink, "?") ? "&" : "?" + urlencode(nVar) + "="; if (value > 0) l.add(ahref(baseLink + max(0, value - step), htmlencode(unicode_leftPointingTriangle()))); for (int i = 0; i < count; i += step) { int n = i / step + 1; if (pageNav2_showPage(value, i, step, count)) if (value == i) l.add(b(n)); else l.add(ahref(baseLink + i, n)); } if (value + step < count) l.add(ahref(baseLink + (value + step), htmlencode(unicode_rightPointingTriangle()))); return p("Pages: " + lines(l)); } public static boolean pageNav2_showPage(int actual, int i, int step, int count) { int diff = abs(actual - i) / step; return i == 0 || i >= (count - 1) / step * step || diff <= 10 || ((i / step) % 10) == 9; } public static int ai_approximateIndexedNodesCount(String s) { return l(ai_expandShortenedKeys(ai_shortenForIndex(s))); } public static String hform(Object contents, Object... params) { return htag("form", contents, params); } public static String htextinput(String name, Object... params) { Object value = ""; if (odd(l(params))) { value = params[0]; params = dropFirst(params); } return tag("input", "", concatArrays(new Object[] { "type", "text", "name", name, "value", value }, params)); } public static String htextinput(String name) { return htextinput(name, ""); } public static String hselect_list(Collection entries, Object... params) { StringBuilder buf = new StringBuilder(); String selected = null; if (odd(l(params))) { selected = str(first(params)); params = dropFirst(params); } int i = indexOf(params, "allowEmpty"); if (even(i)) { buf.append("\n"); params[i] = params[i + 1] = null; } if (nempty(entries)) for (String k : entries) { String value = k; buf.append(tag("option", htmlencode(str(or(value, ""))), "value", k, "selected", eq(selected, k) ? "selected" : null)).append("\n"); } return tag("select", buf, params) + "\n"; } public static String smartBotOfficialURL_url = "http://smartbot.botcompany.de"; public static String smartBotOfficialURL() { return smartBotOfficialURL_url; } public static boolean poorMansProfiling_isOn() { Lock __810 = poorMansProfiling_lock; lock(__810); try { return poorMansProfiling_timer != null; } finally { unlock(__810); } } public static MultiSet poorMansProfiling_results = new MultiSet(); public static int poorMansProfiling_samples; public static java.util.Timer poorMansProfiling_timer; public static Lock poorMansProfiling_lock = lock(); public static int poorMansProfiling_defaultInterval = 100; public static void poorMansProfiling() { poorMansProfiling(poorMansProfiling_defaultInterval); } public static void poorMansProfiling(int interval) { Lock __813 = poorMansProfiling_lock; lock(__813); try { poorMansProfiling_stop(); poorMansProfiling_clear(); poorMansProfiling_timer = doEvery_daemon(interval, new Runnable() { public void run() { try { Map map = runnableThreadsWithStackTraces(); Lock __814 = poorMansProfiling_lock; lock(__814); try { poorMansProfiling_samples++; for (Thread t : keys(map)) { { if (isSystemThread(t)) continue; } StringBuilder buf = new StringBuilder(); for (StackTraceElement e : map.get(t)) buf.append(e).append("\n"); poorMansProfiling_results.add(str(buf)); } } finally { unlock(__814); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Map map = runnableThreadsWithStackTraces();\r\n ..."; } }); } finally { unlock(__813); } } public static void poorMansProfiling_stop() { Lock __815 = poorMansProfiling_lock; lock(__815); try { if (poorMansProfiling_timer != null) { stopTimer(poorMansProfiling_timer); poorMansProfiling_timer = null; } } finally { unlock(__815); } } public static void poorMansProfiling_clear() { Lock __816 = poorMansProfiling_lock; lock(__816); try { poorMansProfiling_results.clear(); poorMansProfiling_samples = 0; } finally { unlock(__816); } } public static MultiSet poorMansProfiling_results() { return new MultiSet(poorMansProfiling_results); } public static MultiSet poorMansProfiling_stopAndGetResults() { Lock __817 = poorMansProfiling_lock; lock(__817); try { poorMansProfiling_stop(); return poorMansProfiling_results(); } finally { unlock(__817); } } public static String poorMansProfiling_stopAndRenderResults() { return poorMansProfiling_renderFullResults(poorMansProfiling_stopAndGetResults()); } public static List indexedTerms_scoredSearch(String query, int max) { return scoredSearch2(max, query, allIndexedTerms_iterator()); } public static ArrayList asList(A[] a) { return a == null ? new ArrayList() : new ArrayList(Arrays.asList(a)); } public static ArrayList asList(int[] a) { ArrayList l = new ArrayList(); for (int i : a) l.add(i); return l; } public static ArrayList asList(Iterable s) { if (s instanceof ArrayList) return (ArrayList) s; ArrayList l = new ArrayList(); if (s != null) for (A a : s) l.add(a); return l; } public static ArrayList asList(Producer p) { ArrayList l = new ArrayList(); A a; if (p != null) while ((a = p.next()) != null) l.add(a); return l; } public static ArrayList asList(Enumeration e) { ArrayList l = new ArrayList(); if (e != null) while (e.hasMoreElements()) l.add(e.nextElement()); return l; } public static List termsEndingWith(String s) { return endingWithIC(fullIndexedTerms(), s); } public static List indexedTermsContaining(String searchString) { return filterCic(ai_mainIndexKeysIterator(), searchString); } public static Collection fullIndexedTermsStartingWith(String prefix) { return ai_expandShortenedKeys_multi(concurrentlyIterateSet(indexedTermsStartingWith(prefix))); } public static void sortInPlaceAlternativeCI(List l) { sortInPlace(l, alternativeCIComparator()); } public static String ai_findIndexedTerm(String term) { String s = ai_shortenForIndex(term); String result = ai_navigableShortTerms().floor(s); if (result == null) return null; return findInListIC(ai_expandShortenedKeys(result), term); } public static String unicode_blackRightArrow() { return charToString(0x27a1); } public static String b(Object contents, Object... params) { return tag("b", contents, params); } public static List subListOfCollection(Collection c, int from, int to) { int n = l(c); to = min(to, n); if (from >= n || to <= from) return ll(); List l = new ArrayList(to - from); Iterator it = iterator(c); for (int _repeat_842 = 0; _repeat_842 < from; _repeat_842++) { it.next(); } while (from++ < to) l.add(it.next()); return l; } public static List sortedIC(Collection l) { return sortedIgnoreCase(l); } public static List unquoteAll(List l) { List t = cloneList(l); for (int i = 0; i < l(t); i++) if (isQuoted(t.get(i))) t.set(i, unquote(t.get(i))); return t; } public static String h2(String s, Object... params) { return tag("h2", s, params); } public static List takeLast(List l, int n) { return newSubList(l, l(l) - n); } public static List takeLast(int n, List l) { return takeLast(l, n); } public static String takeLast(int n, String s) { return substring(s, l(s) - n); } public static String takeLast(String s, int n) { return substring(s, l(s) - n); } public static ArrayList cloneList(Iterable l) { return l instanceof Collection ? cloneList((Collection) l) : asList(l); } public static ArrayList cloneList(Collection l) { if (l == null) return new ArrayList(); synchronized (collectionMutex(l)) { return new ArrayList(l); } } public static String formatWithThousands(long l) { return formatWithThousandsSeparator(l); } public static long ai_theBigNumber() { return ai_allWebsCount(); } public static List mapReversed(List l, Object f) { return mapReversed(f, l); } public static List mapReversed(Object f, List l) { List x = emptyList(l); for (int i = l(l) - 1; i >= 0; i--) x.add(callF(f, l.get(i))); return x; } public static List mapReversed(F1 f, List l) { List x = emptyList(l); for (int i = l(l) - 1; i >= 0; i--) x.add(callF(f, l.get(i))); return x; } public static TripleWeb ai_getTriple(String id) { if (!possibleGlobalID(id)) return null; return tripleIndex().getTriple(asGlobalID(id)); } public static TripleWeb ai_getTriple(GlobalID id) { if (id == null) return null; return tripleIndex().getTriple(id); } public static String ai_html_linkedWeb(GlobalID globalID) { return ai_html_linkedWeb(str(globalID)); } public static String ai_html_linkedWeb(String globalID) { return ahref(ai_html_webLink(globalID), htmlencode(globalID)); } public static String ai_html_linkedWeb(Web web) { return web == null ? "-" : ai_html_linkedWeb(web.globalID()); } public static String renderGMTDate(long time) { return formatGMTWithOptionalDate_24(time); } public static String ai_html_renderTriple(WebRelation r) { return r == null ? "" : ai_html_encyclopediaTopic(r.a) + ai_html_triangle() + ai_html_encyclopediaTopic(r) + ai_html_triangle() + ai_html_encyclopediaTopic(r.b); } public static String ai_html_renderTriple(T3 t) { return t == null ? "" : ai_html_encyclopediaTopic(t.a) + ai_html_triangle() + ai_html_encyclopediaTopic(t.b) + ai_html_triangle() + ai_html_encyclopediaTopic(t.c); } public static String hGoogleFontOswald() { return hcss("\r\n@import url(https://fonts.googleapis.com/css?family=Oswald:400,300);\r\n@import url(https://fonts.googleapis.com/css?family=Open+Sans);\r\nbody {\r\n font-family: 'Open Sans', sans-serif;\r\n}"); } public static Pair, Set> evalWithTimeout_calculations() { Set all = evalWithTimeout_allThreads; Set inTime = evalWithTimeout_inTime; return pair(inTime, setMinusSet(all, inTime)); } public static String ul_htmlEncode(String... list) { return ul_htmlEncode(asList(list)); } public static String ul_htmlEncode(Collection list, Object... params) { return ul(map("htmlEncode", list), params); } public static String html_addLinkToSpan(String html, String id, String link) { List tok = htmlTok(html); for (List tag : findContainerTagWithParams(tok, "span", "id", id)) { tag = subList(tag, 2, l(tag) - 2); replaceTokens(tag, ahref(link, join(tag))); } return join(tok); } public static String encyclopediaLink(String topic) { return smartBot_encyclopediaLink(topic); } public static String html_addTargetBlank(String html) { List tok = htmlTok(html); for (int i = 1; i < l(tok); i += 2) try { String t = tok.get(i); if (isTag(t, "a")) { Map map = htmlParams(t); if (map.containsKey("href")) { map.put("target", "_blank"); tok.set(i, hopeningTag("a", mapToParams(map))); } } } catch (Throwable __e) { _handleException(__e); } return join(tok); } public static Throwable printStackTrace(Throwable e) { print(getStackTrace(e)); return e; } public static void printStackTrace() { printStackTrace(new Throwable()); } public static void printStackTrace(String msg) { printStackTrace(new Throwable(msg)); } public static boolean ai_tripleAllowedToPost(T3 t) { return ai_nodeNameAllowedToPost(t.a) && ai_nodeNameAllowedToPost(t.b) && ai_nodeNameAllowedToPost(t.c); } public static Web webFromTriple(CharSequence a, CharSequence b, CharSequence c) { return webFromTriple(triple(symbol(a), symbol(b), symbol(c))); } public static Web webFromTriple(T3 t) { if (t == null) return null; return webFromTriple(t, 0.2, 0.2, 0.8, 0.7); } public static Web webFromTriple(T3 t, double x1, double y1, double x2, double y2) { if (t == null) return null; Web web = webWithoutIndexAndLock(); String a = unnull(str(t.a)), b = unnull(str(t.b)), c = unnull(str(t.c)); WebNode nodeA = web.newNode(a), nodeC = web.newNode(c); web_addRelation(nodeA, nodeC, b); web_setPosition(first(web.nodes), x1, y1); web_setPosition(second(web.nodes), x2, y2); if (t instanceof TripleWeb) { TripleWeb w = (TripleWeb) (t); web.globalID = w.globalID(); web.created = w.created(); web.source = w.source(); web.unverified = w.unverified(); } return web; } public static ThreadLocal postSoftwareMadeWeb_disabled = new ThreadLocal(); public static BetterThreadLocal postSoftwareMadeWeb_websMadeInThread = new BetterThreadLocal(); public static ThreadLocal postSoftwareMadeWeb_maker = new ThreadLocal(); public static ThreadLocal postSoftwareMadeWeb_programID = new ThreadLocal(); public static String postSoftwareMadeWeb_programID_allThreads; public static List postSoftwareMadeWeb_onNewWeb = synchroList(); public static List postSoftwareMadeWeb_onDisabledPost = synchroList(); public static void postSoftwareMadeWeb(Web web, Object... params) { Lock __884 = aiLock(); lock(__884); try { web_intern(web); incIntThreadLocal(postSoftwareMadeWeb_websMadeInThread); String progID = postSoftwareMadeWeb_programID(); String maker = postSoftwareMadeWeb_maker.get(); if (maker != null) if (empty(web.source)) web.source = maker; else params = paramsPlus(params, "maker", postSoftwareMadeWeb_maker.get()); if (isTrue(postSoftwareMadeWeb_disabled.get())) { print(" Would post: " + webToStringShort(web) + " [" + web.source + "]"); pcallFAll(postSoftwareMadeWeb_onDisabledPost, web); } else { logStructure(postSoftwareMadeWeb_file(), nu(SoftwareMadeWeb.class, "date", now(), "computerID", computerID(), "programID", progID, "web", web, "furtherInfo", empty(params) ? null : litorderedmap(params))); markWebsPosted_createMarker(progID); pcallFAll(postSoftwareMadeWeb_onNewWeb, web); } } finally { unlock(__884); } } public static String postSoftwareMadeWeb_programID() { return or2(postSoftwareMadeWeb_programID.get(), postSoftwareMadeWeb_programID_allThreads, programID()); } public static File postSoftwareMadeWeb_file() { return getProgramFile(postSoftwareMadeWeb_programID(), "webs-made.txt"); } public static void ai_wrongWeb(String webID) { ai_wrongWeb(ai_getWebFromTerm(webID)); } public static String ai_wrongWeb(Web web) { if (web == null) return null; Boolean old = setThreadLocal(ai_invalidateWeb_postAntiTriple, true); try { return ai_invalidateWeb(web); } finally { ai_invalidateWeb_postAntiTriple.set(old); } } public static boolean ai_nodeNameAllowedToPost(String s) { return !isDollarVar(s); } public static boolean hasIndexedNode(String s) { return cachedNodeIndex().containsKey(s); } public static Web oneNodeWeb(String text) { Web web = simpleWeb(); web.newNode(text); return web; } public static Object ai_withMaker(String maker, Object f) { String prev = ai_setMaker(maker); try { return callF(f); } finally { ai_setMaker(prev); } } public static boolean ai_greetingRule1(String a) { if (ai_is(a, "recent") && ai_is(a, "a greeting") && ai_is(a, "unanswered")) { ai_postTriple(a, "should be", "answered by me"); return true; } return false; } public static boolean ai_hasTriple(String a, String b, String c) { return ai_cache_hasTriple(a, b, c); } public static boolean ai_hasTriple(T3 t) { return ai_cache_hasTriple(t); } public static String firstQuoted(Collection l) { if (l != null) for (String s : l) if (isQuoted(s)) return s; return null; } public static List web_texts(WebNode node) { return node == null ? new ArrayList() : node.texts(); } public static List web_texts(Collection l) { List texts = new ArrayList(l(l)); if (l != null) for (WebNode n : l) texts.add(web_text(n)); return texts; } public static long postToStefansChat_lastPost = -60000; public static boolean postToStefansChat_printStackTrace; public static boolean postToStefansChat(String text) { if (empty(text)) return false; if (postToStefansChat_printStackTrace) printStackTrace(); text = shorten(text, stefansChat_messageLimit()); print("CHAT>> " + text); boolean ok = eq("OK", loadPage("http://ai1.space/" + psI("#1008998") + "/raw/msg?botmark=1&message=" + urlencode(text))); postToStefansChat_lastPost = sysNow(); return ok; } public static boolean web_match(String pat, WebNode node, Matches m) { for (String s : web_texts(node)) if (match(pat, s, m)) return true; return false; } public static Pair transpileOnServer(String snippetID) { return transpileOnServer(snippetID, "quick"); } public static Pair transpileOnServer(long snippetID) { return transpileOnServer(fsI(snippetID)); } public static Pair transpileOnServer(long snippetID, String mode) { return transpileOnServer(fsI(snippetID), mode); } public static Pair transpileOnServer(String snippetID, String mode) { try { print("Transpiling " + formatSnippetID(snippetID)); String url = "http://tinybrain.de:8080/tb/transpile.php"; String modeStr = "&quick=1"; if (eq(mode, "medium")) modeStr += "&medium=1"; if (eq(mode, "fresh")) modeStr += "&fresh=1"; String query = "id=" + parseSnippetID(snippetID) + modeStr + standardCredentials(); String result = loadPage(url + "?" + query); print(result); Matcher m = regexp("URL=/(dexcompile\\.php.*?)'", result); if (!m.find()) return pair(false, result); url = "http://tinybrain.de:8080/" + m.group(1) + standardCredentials(); result = loadPage(url); boolean ok = result.contains("SUCCESS"); return pair(ok, result); } catch (Exception __e) { throw rethrow(__e); } } public static String programID() { return getProgramID(); } public static void ai_speculateDeepAndActive(String word) { ai_speculateDeep(word); ai_activeSpec(word); } public static List cloneValues(Map map) { return cloneList(values(map)); } public static A first_sync(List list) { synchronized (list) { return empty(list) ? null : list.get(0); } } public static A first_sync(Iterable i) { if (i == null) return null; synchronized (i) { Iterator it = i.iterator(); return it.hasNext() ? it.next() : null; } } public static String ai_speculateWithActive(String word) { ai_speculate(word); ai_activeSpec(word); return word; } public static AI_DefaultISpec ai_defaultISpec_value; public static AI_DefaultISpec ai_defaultISpec() { if (ai_defaultISpec_value == null) ai_defaultISpec_value = new AI_DefaultISpec(); return ai_defaultISpec_value; } public static class AI_DefaultISpec implements ISpec { public Lock aiLock() { return main.aiLock(); } public boolean ai_cache_hasTriple(String a, String b, String c) { return main.ai_cache_hasTriple(a, b, c); } public String ai_postTriple(String a, String b, String c) { return main.ai_postTriple(a, b, c); } public String ai_setMaker(String maker) { return main.ai_setMaker(maker); } public List allIndexedFullTerms() { return main.allIndexedFullTerms(); } public List ai_texts_verified(String a, String b, String c) { return main.ai_texts_verified(a, b, c); } public String ai_text(String a, String b, String c) { return main.ai_text(a, b, c); } public Web ai_blob(String word, boolean verified) { return main.ai_blob(word, verified); } public List ai_index_search_dollarX(String a, String b, String c) { return main.ai_index_search_dollarX(a, b, c); } public List indexedNodes(String s) { return main.indexedNodes(s); } public List indexedWebs(String s) { return main.indexedWebs(s); } public Set ai_invalidatedWebs() { return main.ai_invalidatedWebs(); } public void ai_invalidatedWebs_cache_put(GlobalID a, GlobalID b) { main.ai_invalidatedWebs_cache_put(a, b); } public MultiMap cachedNodeIndex2() { return main.cachedNodeIndex2(); } public Web ai_getWeb(String id) { return main.ai_getWeb(id); } public void ai_setSubSpace(AI_SubSpace subSpace) { main.ai_setSubSpace(subSpace); } public AI_SubSpace ai_currentSubSpace() { return main.ai_currentSubSpace(); } public Set ai_mainIndexKeys() { return main.ai_mainIndexKeys(); } public TripleIndex tripleIndex() { return main.tripleIndex(); } public void ai_removeWeb(Web web) { main.ai_removeWeb(web); } public List virtualNodeIndices() { return main.virtualNodeIndices(); } public boolean ai_enabled() { return main.ai_enabled(); } public boolean ai_useThoughtSpaces() { return main.ai_useThoughtSpaces(); } public boolean ai_inTripleMode() { return main.ai_inTripleMode(); } public AbstractThoughtSpace currentThoughtSpace() { return main.currentThoughtSpace(); } public A thoughtSpace(A ts) { return main.thoughtSpace(ts); } public void ai_markMemoryDirty() { main.ai_markMemoryDirty(); } public boolean ai_isMemoryDirty() { return main.ai_isMemoryDirty(); } } public static ClassLoader myClassLoader() { return _getClass(mc()).getClassLoader(); } public static Object getOpt(Object o, String field) { return getOpt_cached(o, field); } public static Object getOpt_raw(Object o, String field) { try { Field f = getOpt_findField(o.getClass(), field); if (f == null) return null; f.setAccessible(true); return f.get(o); } catch (Exception e) { throw new RuntimeException(e); } } public static Object getOpt(Class c, String field) { try { if (c == null) return null; Field f = getOpt_findStaticField(c, field); if (f == null) return null; f.setAccessible(true); return f.get(null); } catch (Exception e) { throw new RuntimeException(e); } } public static Field getOpt_findStaticField(Class c, String field) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) return f; _c = _c.getSuperclass(); } while (_c != null); return null; } public static List parseClassesToShareList(String s) { return onlyClassNamesThatExist(prependAll("main$", identifiersOnly(javaTokC(s)))); } public static void clearTransientWebs() { List l = cloneAndClear(transientWebs_list); for (Web web : l) onTransientWebRemoved(web); } public static void addTransientWeb(Web web) { onTransientWebAdded(web); transientWebs_list.add(web); } public static double stefansChat_onLine_safe_delay = 1; public static void stefansChat_onLine_safe(final Object f) { stefansChat_onLine(new VF1() { public void get(String text) { try { if (sysNow() > postToStefansChat_lastPost + toMS(stefansChat_onLine_safe_delay)) callF(f, text); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (sysNow() > postToStefansChat_lastPost+toMS(stefansChat_onLine_safe_delay)..."; } }); } public static boolean ai_speculateWithActiveAndTimeout(final String word, double timeoutSeconds) { if (loading()) return false; return eitherIsB(evalWithTimeout(toMS(timeoutSeconds), new Runnable() { public void run() { try { ai_speculateWithActive(word); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ai_speculateWithActive(word)"; } })); } public static AutoCloseable tempSetThreadLocal(final ThreadLocal tl, A a) { final A prev = setThreadLocal(tl, a); return new AutoCloseable() { public void close() { tl.set(prev); } }; } public static List callAll(List l) { List out = emptyList(l); for (Object o : unnull(l)) { out.add(call(o)); } return out; } public static A getAndClearThreadLocal(ThreadLocal tl) { A a = tl.get(); tl.set(null); return a; } public static void clearThreadLocal(ThreadLocal tl) { if (tl != null) tl.set(null); } public static void smartBotsChat_onLine_safe(final Object f) { smartBotsChat_onLine(new VF1() { public void get(String text) { try { if (sysNow() > postToSmartBotsChat_lastPost + toMS(stefansChat_onLine_safe_delay)) callF(f, text); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (sysNow() > postToSmartBotsChat_lastPost+toMS(stefansChat_onLine_safe_dela..."; } }); } public static A setThreadLocal(ThreadLocal tl, A value) { A old = tl.get(); tl.set(value); return old; } public static HashMap lithashmap(Object... x) { HashMap map = new HashMap(); litmap_impl(map, x); return map; } public static int shorten_default = 100; public static String shorten(String s) { return shorten(s, shorten_default); } public static String shorten(String s, int max) { return shorten(s, max, "..."); } public static String shorten(String s, int max, String shortener) { if (s == null) return ""; if (max < 0) return s; return s.length() <= max ? s : substring(s, 0, min(s.length(), max - l(shortener))) + shortener; } public static String shorten(int max, String s) { return shorten(s, max); } public static int stefansChat_messageLimit() { return 1000; } public static String ntUpload(String title, String text) { return ntUpload(programID(), title, text); } public static String ntUpload(String gummipw, String title, String text) { return ntUpload(gummipw, title, text, ""); } public static String ntUpload(String gummipw, String title, String text, String comment) { try { if (gummipw == null || gummipw.length() == 0) throw new RuntimeException("Need gummi password."); System.out.println("Uploading:"); System.out.println(" " + gummipw + " (" + text.length() + " chars)"); System.out.println(" " + title); int displayLength = 40; System.out.println(" " + (text.length() > displayLength ? quote(text.substring(0, displayLength)) + " [...]" : quote(text))); URL url = new URL("http://tinybrain.de:8080/nt-int/add_snippet.php"); String postData = makePostData("gummipw", gummipw, "text", text, "title", title, "comment", comment); String contents = doPost(postData, url.openConnection(), url); if (isInteger(contents)) { long id = parseSnippetID(contents); System.out.println("=> #" + id); return "#" + id; } else throw new RuntimeException("Server error: " + contents); } catch (Exception __e) { throw rethrow(__e); } } public static String unSlackSnippet(String s) { if (s.startsWith("```") && s.endsWith("```") && l(s) >= 6) s = s.substring(3, l(s) - 3); return s; } public static String snippetURL(String id) { return snippetLink(id); } public static boolean userAgentIsBot(String userAgent) { return cic(userAgent, "bot") || cic(userAgent, "crawler"); } public static Class hotwire(String src) { assertFalse(_inCore()); Class j = getJavaX(); if (isAndroid()) { synchronized (j) { List libraries = new ArrayList(); File srcDir = (File) call(j, "transpileMain", src, libraries); if (srcDir == null) throw fail("transpileMain returned null (src=" + quote(src) + ")"); Object androidContext = get(j, "androidContext"); return (Class) call(j, "loadx2android", srcDir, src); } } else { return hotwire_overInternalBot(src); } } public static void set(Object o, String field, Object value) { if (o instanceof Class) set((Class) o, field, value); else try { Field f = set_findField(o.getClass(), field); f.setAccessible(true); smartSet(f, o, value); } catch (Exception e) { throw new RuntimeException(e); } } public static void set(Class c, String field, Object value) { try { Field f = set_findStaticField(c, field); f.setAccessible(true); smartSet(f, null, value); } catch (Exception e) { throw new RuntimeException(e); } } public static Field set_findStaticField(Class c, String field) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) return f; _c = _c.getSuperclass(); } while (_c != null); throw new RuntimeException("Static field '" + field + "' not found in " + c.getName()); } public static Field set_findField(Class c, String field) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field)) return f; _c = _c.getSuperclass(); } while (_c != null); throw new RuntimeException("Field '" + field + "' not found in " + c.getName()); } public static Collection ai_allTriples() { return tripleIndex().allTriples(); } public static void addAll(Collection c, Iterable b) { if (c != null && b != null) for (A a : b) c.add(a); } public static void addAll(Collection c, Collection b) { if (c != null && b != null) c.addAll(b); } public static void addAll(Collection c, A... b) { if (c != null) c.addAll(Arrays.asList(b)); } public static Object removeFirstInjection() { List injections = (List) call(getJavaX(), "getInjections"); synchronized (injections) { return popFirst(injections); } } public static A copyFields(Object x, A y, String... fields) { if (empty(fields)) { Map map = objectToMap(x); for (String field : map.keySet()) setOpt(y, field, map.get(field)); } else for (String field : fields) { Object o = getOpt(x, field); if (o != null) setOpt(y, field, o); } return y; } public static A copyFields(Object x, A y, Collection fields) { return copyFields(x, y, asStringArray(fields)); } public static void clearAllVariables() { try { for (Field f : allFieldObjects(mc())) if (!f.getType().isPrimitive() && (f.getModifiers() & Modifier.FINAL) == 0) f.set(null, null); } catch (Exception __e) { throw rethrow(__e); } } public static void runMain(Object c, String... args) { callMain(c, args); } public static void runMain() { callMain(); } public static String smartBot_sharedClassesForRestart() { return "\r\n Symbol, Web, WebNode, WebRelation, Lisp, MultiMap,\r\n Pair, T3, GlobalID,\r\n TripleWeb, TripleWebWithSource, TripleWebWithSourceAndResult,\r\n SyncListMultiMap, CompactHashSet,\r\n SynchronizedArrayList, MultiSet\r\n "; } public static List addToOrCreateList(List l, A a) { return addDyn(l, a); } public static WeakHashMap> callF_cache = new WeakHashMap(); public static A callF(F0 f) { return f == null ? null : f.get(); } public static B callF(F1 f, A a) { return f == null ? null : f.get(a); } public static C callF(F2 f, A a, B b) { return f == null ? null : f.get(a, b); } public static Object callF(Object f, Object... args) { try { if (f instanceof String) return callMC((String) f, args); if (f instanceof Runnable) { ((Runnable) f).run(); return null; } if (f == null) return null; Class c = f.getClass(); ArrayList methods; synchronized (callF_cache) { methods = callF_cache.get(c); if (methods == null) methods = callF_makeCache(c); } int n = l(methods); if (n == 0) { throw fail("No get method in " + getClassName(c)); } if (n == 1) return invokeMethod(methods.get(0), f, args); for (int i = 0; i < n; i++) { Method m = methods.get(i); if (call_checkArgs(m, args, false)) return invokeMethod(m, f, args); } throw fail("No matching get method in " + getClassName(c)); } catch (Exception __e) { throw rethrow(__e); } } public static ArrayList callF_makeCache(Class c) { ArrayList l = new ArrayList(); Class _c = c; do { for (Method m : _c.getDeclaredMethods()) if (m.getName().equals("get")) { m.setAccessible(true); l.add(m); } if (!l.isEmpty()) break; _c = _c.getSuperclass(); } while (_c != null); callF_cache.put(c, l); return l; } public static synchronized boolean veryQuickJava_refresh() { boolean change = false; try { for (File f : listFiles(new File(javaxCodeDir(), "Transpilations"))) if (ewic(f.getName(), ".java")) { change = true; f.delete(); } } catch (Throwable __e) { _handleException(__e); } Object trans = getOpt(mc(), "transpileRaw_trans"); if (trans != null) { cleanUp(trans); setOpt(mc(), "transpileRaw_trans", null); change = true; } callOpt(getOpt(mc(), "loadFunctions_cache"), "clear"); return change; } public static void hotwireCached_clear() { List classes; { Lock __982 = hotwireCached_lock; lock(__982); try { classes = cloneValues(hotwireCached_cache); hotwireCached_cache.clear(); } finally { unlock(__982); } } cleanUp(classes); } public static void clearAllCaches() { for (String field : allCacheVars()) pcallOpt(getOptMC(field), "clear"); } public static List lookupPossiblyIgnoreCase(MultiMap map, String key) { if (map == null) return null; List value = map.get(key); return !value.isEmpty() ? value : lookupIgnoreCase(map, key); } public static A lookupPossiblyIgnoreCase(Map map, String key) { if (map == null) return null; A value = map.get(key); return value != null ? value : lookupIgnoreCase(map, key); } public static AutoCloseable timerAsAutocloseable(final java.util.Timer timer) { return timer == null ? null : new AutoCloseable() { public void close() { timer.cancel(); } }; } public static java.util.Timer doEveryStartingNow(int delay, Object r) { return doEvery(delay, 0, r); } public static String send(String bot, String text, Object... args) { return sendToLocalBot(bot, text, args); } public static String send(int port, String text, Object... args) { return sendToLocalBot(port, text, args); } public static A println(A a) { return print(a); } public static long toM(long l) { return (l + 1024 * 1024 - 1) / (1024 * 1024); } public static String toM(long l, int digits) { return formatDouble(toM_double(l), digits); } public static File programFile(String name) { return prepareProgramFile(name); } public static File programFile(String progID, String name) { return prepareProgramFile(progID, name); } public static File userDir() { return new File(userHome()); } public static File userDir(String path) { return new File(userHome(), path); } public static Throwable printStackTrace2(Throwable e) { print(getStackTrace2(e)); return e; } public static void printStackTrace2() { printStackTrace2(new Throwable()); } public static void printStackTrace2(String msg) { printStackTrace2(new Throwable(msg)); } public static List virtualNodeIndices_list = synchroList(); public static List virtualNodeIndices() { return virtualNodeIndices_list; } public static class getOpt_Map extends WeakHashMap { public getOpt_Map() { if (getOpt_special == null) getOpt_special = new HashMap(); clear(); } public void clear() { super.clear(); put(Class.class, getOpt_special); put(String.class, getOpt_special); } } public static final Map> getOpt_cache = _registerDangerousWeakMap(synchroMap(new getOpt_Map())); public static HashMap getOpt_special; public static Object getOpt_cached(Object o, String field) { try { if (o == null) return null; Class c = o.getClass(); HashMap map; synchronized (getOpt_cache) { map = getOpt_cache.get(c); if (map == null) map = getOpt_makeCache(c); } if (map == getOpt_special) { if (o instanceof Class) return getOpt((Class) o, field); if (o instanceof Map) return ((Map) o).get(field); } Field f = map.get(field); if (f != null) return f.get(o); if (o instanceof DynamicObject) return ((DynamicObject) o).fieldValues.get(field); return null; } catch (Exception __e) { throw rethrow(__e); } } public static HashMap getOpt_makeCache(Class c) { HashMap map; if (isSubtypeOf(c, Map.class)) map = getOpt_special; else { map = new HashMap(); Class _c = c; do { for (Field f : _c.getDeclaredFields()) { f.setAccessible(true); String name = f.getName(); if (!map.containsKey(name)) map.put(name, f); } _c = _c.getSuperclass(); } while (_c != null); } getOpt_cache.put(c, map); return map; } public static Map ai_getSubSpace_map = synchroMap(); public static AI_SubSpace ai_getSubSpace(String name) { return ai_getSubSpace_map.get(name); } public static MultiMap cachedNodeIndex() { return cachedNodeIndex2(); } public static String renderMap_colonComma(Map map) { return joinWithComma(mapMapToList(new F2() { public Object get(Object a, Object b) { try { return str(a) + ": " + str(b); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "str(a) + \": \" + str(b)"; } }, map)); } public static String trimJoin(List s) { return trim(join(s)); } public static List dropTagsAndHTMLComments(List tok) { return dropHTMLComments(dropTags(tok)); } public static double nodeMatch_dollarVars(WebNode a, WebNode b) { if (isDollarVar(a.text())) return isDollarVar(b.text()) ? -1 : 0; for (String s : web_texts(b)) if (!web_hasLabel(a, s)) return -1; return 0; } public static double nodeMatch_dollarVars(CharSequence _text, WebNode b) { String text = str(_text); if (b == null) return -1; if (isDollarVar(text)) return isDollarVar(b.text()) ? -1 : 0; return cic(web_texts(b), text) ? 0 : -1; } public static List printDeadlocksAndSolve() { List l = printDeadlocks(); interruptDeadlockedThreads(l); return l; } public static Map webToCAL_visMap = caseInsensitiveSynchroMap(); public static void webToCAL_visStructures(String... structures) { for (String s : structures) try { webToCAL_visStructures(cal_unstructure(s)); } catch (Throwable __e) { _handleException(__e); } } public static void webToCAL_visStructures(CirclesAndLines cal) { if (cal != null) for (Circle c : cal.circles) if (nempty(c.quickvis) && nempty(c.text)) webToCAL_visMap.put(c.text, c.quickvis); } public static void ai_spec_include(Web web) { T3 t = webToTriple(web); ai_spec_include(t.a, t.b, t.c); } public static void ai_spec_include(String a, String b, String c) { { if (!(eqic(b, "include"))) return; } String x = a(singular(a)); for (String s : ai_parseList(c)) post(s, "is", x); } public static boolean ai_spec_relRewrite_debug; public static void ai_spec_relRewrite(Web web) { for (WebRelation r : web_relationObjects(web)) { String rel = web_text(r); List newRels = ai_texts_verified(rel, "[relation] rewrites to", "$X"); if (ai_spec_relRewrite_debug) print("ai_spec_relRewrite: " + sfu(newRels)); for (String newRel : newRels) postDerivedFromWeb(web, web_text(r.a), newRel, web_text(r.b)); } } public static void ai_spec_priorityToNormal(Web web) { Matches m = new Matches(); for (WebRelation r : web_relationObjects(web)) { String rel = web_text(r); if (swic(rel, "[priority] ", m)) postDerivedFromWeb(web, web_text(r.a), m.rest(), web_text(r.b)); } } public static String html_linkURLs(String html, String params) { List tok = htmlTok(html); for (int i = 0; i < l(tok); i += 2) tok.set(i, tok.get(i).replaceAll("(https?://[^\" ]+)", "$1")); return join(tok); } public static String html_linkURLs(String html) { return html_linkURLs(html, ""); } public static String ai_html_linkedWebWithThumbs(String webID) { boolean auth = html_pretendAuthed(); String html = ai_html_linkedWeb_icon(webID); if (auth) { T3 t = ai_webToTriple(ai_getWeb(webID)); Matches m = new Matches(); if (t != null && swic(t.b, "possibly ", m) && !ai_hasTriple(t.a, m.rest(), t.c)) html += " " + html_smartBotSendInput(smartBot_storeTripleCmd(t.a, m.rest(), t.c), ai_html_thumbUp()); else html += " " + ai_html_webThumbUpLink(webID); html += " " + ai_html_webThumbDownLink(webID); } return html; } public static String tok_unCurlyBracket(String s) { return isCurlyBraced(s) ? join(dropFirstThreeAndLastThree(javaTok(s))) : s; } public static String htmlEncode(String s) { return htmlencode(s); } public static List transientWebs_list = synchroList(); public static List transientWebs() { return transientWebs_list; } public static List filter(Iterable c, Object pred) { if (pred instanceof F1) return filter(c, (F1) pred); List x = new ArrayList(); if (c != null) for (Object o : c) if (isTrue(callF(pred, o))) x.add(o); return x; } public static List filter(Object pred, Iterable c) { return filter(c, pred); } public static List filter(Iterable c, F1 pred) { List x = new ArrayList(); if (c != null) for (B o : c) if (pred.get(o).booleanValue()) x.add(o); return x; } public static List filter(F1 pred, Iterable c) { return filter(c, pred); } public static List ellipsisTerms(String pat) { int i = indexOf(pat, "..."); if (i < 0) return ll(ai_getIndexedTerm(pat)); String prefix = substring(pat, 0, i), suffix = substring(pat, i + 3); return endingWithIC(suffix, fullTermsStartingWith(prefix)); } public static boolean tok_containsSimpleArrow(String s) { return tok_containsSimpleArrow(javaTok(s)); } public static boolean tok_containsSimpleArrow(List tok) { return containsSubList(tok, "-", "", ">"); } public static boolean has(String a, String b, String c) { if (ai_cache_hasTriple(a, b, c)) return true; return false; } public static boolean has(T3 t) { if (ai_cache_hasTriple(t)) return true; return false; } public static boolean isQuoted(String s) { if (isNormalQuoted(s)) return true; return isMultilineQuoted(s); } public static Matcher safeRegexp(String pat, String s) { return Pattern.compile(pat).matcher(new InterruptibleString(s)); } public static volatile boolean ping_pauseAll; public static int ping_sleep = 100; public static volatile boolean ping_anyActions; public static Map ping_actions = newWeakHashMap(); public static boolean ping() { if (ping_pauseAll || ping_anyActions) ping_impl(); return true; } public static boolean ping_impl() { try { if (ping_pauseAll && !isAWTThread()) { do Thread.sleep(ping_sleep); while (ping_pauseAll); return true; } if (ping_anyActions) { Object action; synchronized (ping_actions) { action = ping_actions.get(currentThread()); if (action instanceof Runnable) ping_actions.remove(currentThread()); if (ping_actions.isEmpty()) ping_anyActions = false; } if (action instanceof Runnable) ((Runnable) action).run(); else if (eq(action, "cancelled")) throw fail("Thread cancelled."); } return false; } catch (Exception __e) { throw rethrow(__e); } } public static Collection web_relationObjects(Web web) { List l = new ArrayList(); for (WebNode n : web.nodes) if (n instanceof WebRelation) l.add(((WebRelation) n)); return l; } public static List splitAtSlash_keepAll(String s) { int i = 0; List l = new ArrayList(); while (true) { int j = smartIndexOf(s, '/', i); l.add(substring(s, i, j)); if (j == l(s)) break; i = j + 1; } return l; } public static A second(List l) { return get(l, 1); } public static A second(A[] bla) { return bla == null || bla.length <= 1 ? null : bla[1]; } public static B second(Pair p) { return p == null ? null : p.b; } public static B second(T3 t) { return t == null ? null : t.b; } public static List indexedRelations_verified(String s) { return web_relationsOnly(web_nodesFromVerifiedWebs(indexedNodes(s))); } public static String ai_setMaker(String maker) { return setThreadLocal(postSoftwareMadeWeb_maker, maker); } public static List ai_index_search_dollarX_quoted(String a, String b, String c) { String query = isDollarVar(a) ? c : a; return web_quotedNodesOnly(webs_search_dollarX(webFromTriples(a, b, c), indexedWebs(query))); } public static String web_unquote(WebNode node) { return node == null ? null : unquote(web_text(node)); } public static int javaTok_n, javaTok_elements; public static boolean javaTok_opt; public static List javaTok(String s) { ++javaTok_n; ArrayList tok = new ArrayList(); int l = s == null ? 0 : s.length(); int i = 0, n = 0; while (i < l) { int j = i; char c, d; while (j < l) { c = s.charAt(j); d = j + 1 >= l ? '\0' : s.charAt(j + 1); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else if (c == '/' && d == '*') { do ++j; while (j < l && !s.substring(j, Math.min(j + 2, l)).equals("*/")); j = Math.min(j + 2, l); } else if (c == '/' && d == '/') { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else break; } tok.add(javaTok_substringN(s, i, j)); ++n; i = j; if (i >= l) break; c = s.charAt(i); d = i + 1 >= l ? '\0' : s.charAt(i + 1); if (c == '\'' && Character.isJavaIdentifierStart(d) && i + 2 < l && "'\\".indexOf(s.charAt(i + 2)) < 0) { j += 2; while (j < l && Character.isJavaIdentifierPart(s.charAt(j))) ++j; } else if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { int c2 = s.charAt(j); if (c2 == opener || c2 == '\n' && opener == '\'') { ++j; break; } else if (c2 == '\\' && j + 1 < l) j += 2; else ++j; } } else if (Character.isJavaIdentifierStart(c)) do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; } else if (c == '[' && d == '[') { do ++j; while (j + 1 < l && !s.substring(j, j + 2).equals("]]")); j = Math.min(j + 2, l); } else if (c == '[' && d == '=' && i + 2 < l && s.charAt(i + 2) == '[') { do ++j; while (j + 2 < l && !s.substring(j, j + 3).equals("]=]")); j = Math.min(j + 3, l); } else ++j; tok.add(javaTok_substringC(s, i, j)); ++n; i = j; } if ((tok.size() % 2) == 0) tok.add(""); javaTok_elements += tok.size(); return tok; } public static List javaTok(List tok) { return javaTokWithExisting(join(tok), tok); } public static List listWithout(List a, Object... b) { return minus(a, b); } public static List codeTokens(List tok) { return codeTokensOnly(tok); } public static Set allIndexedTerms() { return indexedTerms(); } public static Collection ai_expandShortenedKeys(String sKey) { if (!endsWith(sKey, "...")) return ll(sKey); LinkedHashSet set = new LinkedHashSet(); for (int position = 0; position < 3; position++) for (TripleRef r : unnull(tripleIndex().getTripleRefs(sKey, position))) if (r != null) set.add(r.get()); return set; } public static boolean flexMatchIC2_debug; public static boolean flexMatchIC2(String pat, String s) { return flexMatchIC2(pat, s, null); } public static boolean flexMatchIC2(String pat, String s, Matches m) { return flexMatchIC2(javaTok(pat), javaTok_cached(unnull(s)), m); } public static boolean flexMatchIC2(String pat, String s, Matches m, boolean joinBrackets) { return flexMatchIC2(javaTok(pat), javaTok_cached(unnull(s)), m, joinBrackets); } public static boolean flexMatchIC2(List tokpat, List tokfull, Matches m) { return flexMatchIC2(tokpat, tokfull, m, true); } public static boolean flexMatchIC2(List tokpat, List tokfull, Matches m, boolean joinBrackets) { tokpat = codeTokens(joinBrackets ? joinBrackets(tokpat) : tokpat); for (int i = 0; i < l(tokpat); i++) if (eq(tokpat.get(i), "*")) tokpat.add(i++, "!*"); if (joinBrackets) tokfull = joinBrackets(tokfull); List tok = codeTokens(tokfull); BitSet bla = new BitSet(); BitSet bla2 = new BitSet(); if (!flexMatchIC2_impl(tokpat, 0, tok, 0, bla, bla2)) return false; if (m != null) { List l = new ArrayList(); for (int i = 1; i < l(tokfull); i += 2) { if (bla.get(i / 2)) { int j = i; while (j < l(tokfull) && bla.get(j / 2)) j += 2; l.add(join(subList(tokfull, i, j - 1))); i = j - 2; } else if (bla2.get(i / 2)) l.add(tokfull.get(i)); } m.m = toStringArray(l); } return true; } public static boolean flexMatchIC2_impl(List pat, int ipat, List tok, int itok, BitSet bla, BitSet bla2) { if (flexMatchIC2_debug) print("flexMatchIC2 pat=" + structure(subList(pat, ipat)) + " tok=" + structure(subList(tok, itok)) + " " + structure(bla)); if (ipat >= l(pat)) return itok >= l(tok); String t = pat.get(ipat); if (eq(t, "*")) { if (flexMatchIC2_debug) print("Trying zero tokens"); if (flexMatchIC2_impl(pat, ipat + 1, tok, itok, bla, bla2)) { if (flexMatchIC2_debug) print("Success!"); return true; } bla.set(itok); if (itok < l(tok)) { if (flexMatchIC2_debug) print("Trying one or more tokens"); if (flexMatchIC2_impl(pat, ipat, tok, itok + 1, bla, bla2)) { if (flexMatchIC2_debug) print("Success!"); return true; } } if (flexMatchIC2_debug) print("Failed * matching"); bla.clear(itok); return false; } if (itok >= l(tok)) { if (flexMatchIC2_debug) print("too much pattern"); return false; } if (eq(t, "!*")) { bla.set(itok); if (flexMatchIC2_impl(pat, ipat + 1, tok, itok + 1, bla, bla2)) return true; bla.clear(itok); return false; } String realt = tok.get(itok); if (t.startsWith("(") && t.endsWith(")")) { if (flexMatchIC2_debug) print("flexMatchIC2 precheck " + t + " " + realt); if (!containsIgnoreCase(t, realt)) return false; List list = splitAt(dropFirstAndLast(t), "|"); if (flexMatchIC2_debug) print("flexMatchIC2 real check " + struct(list)); if (!containsIgnoreCase(list, realt)) return false; bla2.set(itok); } else if (neqic(realt, t)) { if (flexMatchIC2_debug) print("mismatch"); return false; } if (flexMatchIC2_impl(pat, ipat + 1, tok, itok + 1, bla, bla2)) return true; else { bla2.clear(itok); return false; } } public static ThreadLocal ai_html_shortenTextForDisplay_length = new ThreadLocal(); public static String ai_html_shortenTextForDisplay(String s) { Integer max = ai_html_shortenTextForDisplay_length.get(); return max == null ? s : shorten(s, max); } public static String htmlencode_noQuotes(String s) { if (s == null) return ""; StringBuilder out = new StringBuilder(Math.max(16, s.length())); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == '<') out.append("<"); else if (c == '>') out.append(">"); else if (c > 127 || c == '&') { out.append("&#"); out.append((int) c); out.append(';'); } else out.append(c); } return out.toString(); } public static String dottedStyleForLinks() { return dottedStyle() + "; text-decoration: none"; } public static String quoteBorderless_less(Object o) { if (o == null) return "null"; return quoteBorderless_less(str(o)); } public static String quoteBorderless_less(String s) { if (s == null) return "null"; StringBuilder out = new StringBuilder((int) (l(s) * 1.5)); quoteBorderless_less_impl(s, out); return out.toString(); } public static void quoteBorderless_less_impl(String s, StringBuilder out) { int l = s.length(); for (int i = 0; i < l; i++) { char c = s.charAt(i); if (c == '\\') out.append('\\').append(c); else if (c == '\r') out.append("\\r"); else if (c == '\n') out.append("\\n"); else out.append(c); } } public static void lockOrFail(Lock lock, long timeout) { try { ping(); if (!lock.tryLock(timeout, TimeUnit.MILLISECONDS)) { String s = "Couldn't acquire lock after " + timeout + " ms."; if (lock instanceof ReentrantLock) { ReentrantLock l = (ReentrantLock) (lock); s += " Hold count: " + l.getHoldCount() + ", owner: " + call(l, "getOwner"); } throw fail(s); } ping(); } catch (Exception __e) { throw rethrow(__e); } } public static ReentrantLock fairLock() { return new ReentrantLock(true); } public static Map synchroMap() { return synchroHashMap(); } public static Map synchroMap(Map map) { return Collections.synchronizedMap(map); } public static TreeMap caseInsensitiveMap() { return new TreeMap(caseInsensitiveComparator()); } public static boolean endsWithLetterOrDigit(String s) { return s != null && s.length() > 0 && Character.isLetterOrDigit(s.charAt(s.length() - 1)); } public static boolean isFalse(Object o) { return eq(false, o); } public static String fixNewLines(String s) { int i = indexOf(s, '\r'); if (i < 0) return s; int l = s.length(); StringBuilder out = new StringBuilder(l); out.append(s, 0, i); for (; i < l; i++) { char c = s.charAt(i); if (c != '\r') out.append(c); else { out.append('\n'); if (i + 1 < l && s.charAt(i + 1) == '\n') ++i; } } return out.toString(); } public static void print_append(StringBuffer buf, String s, int max) { synchronized (buf) { buf.append(s); max /= 2; if (buf.length() > max) try { int newLength = max / 2; int ofs = buf.length() - newLength; String newString = buf.substring(ofs); buf.setLength(0); buf.append("[...] ").append(newString); } catch (Exception e) { buf.setLength(0); } } } public static boolean _inCore() { return false; } public static Class javax() { return getJavaX(); } public static Lock generalLock_instance = lock(); public static Lock generalLock() { return generalLock_instance; } public static void cancelTimer(javax.swing.Timer timer) { if (timer != null) timer.stop(); } public static void cancelTimer(java.util.Timer timer) { if (timer != null) timer.cancel(); } public static void cancelTimer(Object o) { if (o instanceof java.util.Timer) cancelTimer((java.util.Timer) o); else if (o instanceof javax.swing.Timer) cancelTimer((javax.swing.Timer) o); else if (o instanceof AutoCloseable) try { ((AutoCloseable) o).close(); } catch (Throwable __e) { _handleException(__e); } } public static long toMS(double seconds) { return (long) (seconds * 1000); } public static int regularGC_interval = 60000; public static int regularGC_firstDelay = 20000; public static volatile java.util.Timer regularGC_timer; public static int regularGC_countdown = -1; public static synchronized void regularGC() { if (!_inCore()) return; if (regularGC_timer == null) regularGC_timer = doEvery_daemon(regularGC_interval, regularGC_firstDelay, "regularGC_doIt"); } public static void regularGC_doIt() { if (regularGC_countdown == 0) return; else if (regularGC_countdown > 0) --regularGC_countdown; timedGCPlusDeadlockSolving(); } public static void upgradeJavaXAndRestart() { run("#1001639"); restart(); sleep(); } public static TimerTask smartTimerTask(Object r, java.util.Timer timer, long delay) { return new SmartTimerTask(r, timer, delay); } public static class SmartTimerTask extends TimerTask { public Object r; public java.util.Timer timer; public long delay; public SmartTimerTask() { } public SmartTimerTask(Object r, java.util.Timer timer, long delay) { this.delay = delay; this.timer = timer; this.r = r; } public String toString() { return "SmartTimerTask(" + r + ", " + timer + ", " + delay + ")"; } public long lastRun; public void run() { if (!licensed()) timer.cancel(); else { AutoCloseable __1122 = tempActivity(r); try { lastRun = fixTimestamp(lastRun); long now = now(); if (now >= lastRun + delay * 0.9) { lastRun = now; if (eq(false, pcallF(r))) timer.cancel(); } } finally { _close(__1122); } } } } public static void _handleError(Error e) { call(javax(), "_handleError", e); } public static Object callOptMC(String method, Object... args) { return callOpt(mc(), method, args); } public static boolean unlockIfHolding(Lock lock) { if (lock == null || !hasLock(lock)) return false; unlock(lock); return true; } public static String smartBotEval_prelude() { return "!include once " + fsI("#1011841") + "\n"; } public static Class veryQuickJava(CharSequence mainJava) { return veryQuickJava3(str(mainJava)); } public static void ai_copyVarsToChild(Class child) { setOpt(child, "ai_getVar_map", ai_getVar_map); } public static volatile boolean conceptsAndBot_running; public static void conceptsAndBot() { conceptsAndBot(null); } public static void conceptsAndBot(Integer autoSaveInterval) { if (conceptsAndBot_running) return; conceptsAndBot_running = true; try { ensureDBNotRunning(dbBotStandardName()); } catch (Throwable _e) { mainConcepts.dontSave = true; throw rethrow(_e); } if (autoSaveInterval != null) loadAndAutoSaveConcepts(autoSaveInterval); else loadAndAutoSaveConcepts(); dbBot(); } public static void readLocally(String progID, String varNames) { readLocally2(mc(), progID, varNames); } public static void readLocally(String varNames) { readLocally2(mc(), programID(), varNames); } public static void readLocally2(Object obj, String varNames) { readLocally2(obj, programID(), varNames); } public static int readLocally_stringLength; public static ThreadLocal readLocally2_allDynamic = new ThreadLocal(); public static void readLocally2(Object obj, String progID, String varNames) { try { boolean allDynamic = isTrue(getAndClearThreadLocal(readLocally2_allDynamic)); for (String variableName : javaTokC(varNames)) { File textFile = new File(programDir(progID), variableName + ".text"); String value = loadTextFile(textFile); if (value != null) set(main.class, variableName, value); else { File structureFile = new File(programDir(progID), variableName + ".structure"); value = loadTextFile(structureFile); if (value == null) { File structureGZFile = new File(programDir(progID), variableName + ".structure.gz"); if (!structureGZFile.isFile()) return; InputStream fis = new FileInputStream(structureGZFile); try { GZIPInputStream gis = newGZIPInputStream(fis); InputStreamReader reader = new InputStreamReader(gis, "UTF-8"); BufferedReader bufferedReader = new BufferedReader(reader); Object o = unstructure_tok(javaTokC_noMLS_onReader(bufferedReader), allDynamic, null); readLocally_set(obj, variableName, o); } finally { fis.close(); } return; } readLocally_stringLength = l(value); if (nempty(value)) readLocally_set(obj, variableName, allDynamic ? safeUnstructure(value) : unstructure(value)); } } } catch (Exception __e) { throw rethrow(__e); } } public static void readLocally_set(Object c, String varName, Object value) { Object oldValue = get(c, varName); if (oldValue instanceof List && !(oldValue instanceof ArrayList) && value != null) { value = synchroList((List) value); } set(c, varName, value); } public static boolean isWindows() { return System.getProperty("os.name").contains("Windows"); } public static boolean isLinux() { return !isWindows() && !isMac() && !isAndroid(); } public static boolean isRootUser() { return eq(System.getProperty("user.name"), "root"); } public static int isPortOpen_timeout = 500; public static boolean isPortOpen(int port) { try { Socket socket = new Socket(); socket.connect(new InetSocketAddress("127.0.0.1", port), isPortOpen_timeout); socket.close(); return true; } catch (Exception ex) { return false; } } public static File secretProgramFile(String progID, String fileName) { return new File(getSecretProgramDir(progID), fileName); } public static File secretProgramFile(String fileName) { return secretProgramFile(getProgramID(), fileName); } public static String loadSecretTextFileMandatory(String name) { return loadTextFileMandatory(new File(getSecretProgramDir(), name)); } public static String loadSecretTextFileMandatory(String progID, String name) { return loadTextFileMandatory(new File(getSecretProgramDir(progID), name)); } public static MyHTTPD serveHttps_server; public static int serveHttps_port = 8889; public static void serveHttps(int port, File keystore, String pass) { try { serveHttps_port = port; serveHttps_server = new MyHTTPD(port); serveHttps_server.makeSecure(serveHttps_makeSSLSocketFactory(keystore, pass)); serveHttps_server.start(); print("HTTPS server started (listening on port " + port + "!)"); } catch (Exception __e) { throw rethrow(__e); } } public static SSLServerSocketFactory serveHttps_makeSSLSocketFactory(File keystoreFile, String passphrase) { try { KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); InputStream keystoreStream = new FileInputStream(keystoreFile); try { keystore.load(keystoreStream, passphrase.toCharArray()); } finally { keystoreStream.close(); } TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(keystore); KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(keystore, passphrase.toCharArray()); SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null); return ctx.getServerSocketFactory(); } catch (Exception __e) { throw rethrow(__e); } } public static void cleanMeUp_serveHttps() { if (serveHttps_server != null) { serveHttps_server.stop(); serveHttps_server = null; } } public static boolean makeAndroid3_disable; public static class Android3 { public String greeting; public boolean publicOverride; public int startPort = 5000; public Responder responder; public boolean console = true; public boolean quiet; public boolean daemon = false; public boolean incomingSilent = false; public int incomingPrintLimit = 200; public boolean useMultiPort = true; public boolean recordHistory; public boolean verbose; public int answerPrintLimit = 500; public boolean newLineAboveAnswer, newLineBelowAnswer; public int port; public long vport; public DialogHandler handler; public ServerSocket server; public Android3(String greeting) { this.greeting = greeting; } public Android3() { } public synchronized void dispose() { if (server != null) { try { server.close(); } catch (IOException e) { print("[internal] " + e); } server = null; } if (vport != 0) try { print("Disposing " + this); removeFromMultiPort(vport); vport = 0; } catch (Throwable __e) { _handleException(__e); } } public String toString() { return "Bot: " + greeting + " [vport " + vport + "]"; } } public abstract static class Responder { public abstract String answer(String s, List history); } public static Android3 makeAndroid3(final String greeting) { return makeAndroid3(new Android3(greeting)); } public static Android3 makeAndroid3(final String greeting, Responder responder) { Android3 android = new Android3(greeting); android.responder = responder; return makeAndroid3(android); } public static Android3 makeAndroid3(final Android3 a) { if (makeAndroid3_disable) return a; if (a.responder == null) a.responder = new Responder() { public String answer(String s, List history) { return callStaticAnswerMethod(s, history); } }; if (!a.quiet) print("[bot] " + a.greeting); if (a.console && (readLine_noReadLine || makeAndroid3_consoleInUse())) a.console = false; record(a); if (a.useMultiPort) a.vport = addToMultiPort(a.greeting, makeAndroid3_verboseResponder(a)); if (a.console) makeAndroid3_handleConsole(a); if (a.useMultiPort) return a; a.handler = makeAndroid3_makeDialogHandler(a); if (a.quiet) startDialogServer_quiet.set(true); try { a.port = a.daemon ? startDialogServerOnPortAboveDaemon(a.startPort, a.handler) : startDialogServerOnPortAbove(a.startPort, a.handler); } finally { startDialogServer_quiet.set(null); } a.server = startDialogServer_serverSocket; return a; } public static void makeAndroid3_handleConsole(final Android3 a) { if (!a.quiet) print("You may also type on this console."); { Thread _t_0 = new Thread() { public void run() { try { List history = new ArrayList(); while (licensed()) { String line; try { line = readLine(); } catch (Throwable e) { print(getInnerMessage(e)); break; } if (line == null) break; { history.add(line); history.add(makeAndroid3_getAnswer(line, history, a)); } } } catch (Throwable __e) { _handleException(__e); } } }; startThread(_t_0); } } public static DialogHandler makeAndroid3_makeDialogHandler(final Android3 a) { return new DialogHandler() { public void run(final DialogIO io) { if (!a.publicOverride && !(publicCommOn() || io.isLocalConnection())) { io.sendLine("Sorry, not allowed"); return; } String dialogID = randomID(8); io.sendLine(a.greeting + " / Your ID: " + dialogID); List history = new ArrayList(); while (io.isStillConnected()) { if (io.waitForLine()) { final String line = io.readLineNoBlock(); String s = dialogID + " at " + now() + ": " + quote(line); if (!a.incomingSilent) print(shorten(s, a.incomingPrintLimit)); if (eq(line, "bye")) { io.sendLine("bye stranger"); return; } Matches m = new Matches(); if (a.recordHistory) history.add(line); String answer; if (match3("this is a continuation of talk *", s, m) || match3("hello bot! this is a continuation of talk *", s, m)) { dialogID = unquote(m.m[0]); answer = "ok"; } else try { makeAndroid3_io.set(io); answer = makeAndroid3_getAnswer(line, history, a); } finally { makeAndroid3_io.set(null); } if (a.recordHistory) history.add(answer); io.sendLine(answer); } } } }; } public static String makeAndroid3_getAnswer(String line, List history, Android3 a) { String answer, originalAnswer; try { originalAnswer = a.responder.answer(line, history); answer = makeAndroid3_fallback(line, history, originalAnswer); } catch (Throwable e) { e = getInnerException(e); printStackTrace(e); originalAnswer = answer = e.toString(); } if (!a.incomingSilent) { if (originalAnswer == null) originalAnswer = "?"; if (a.newLineAboveAnswer) print(); print(">" + dropFirst(indentx(2, shorten(rtrim(originalAnswer), a.answerPrintLimit)))); if (a.newLineBelowAnswer) print(); } return answer; } public static String makeAndroid3_fallback(String s, List history, String answer) { if (answer == null && match3("what is your pid", s)) return getPID(); if (answer == null && match3("what is your program id", s)) return getProgramID(); if (match3("get injection id", s)) return getInjectionID(); if (answer == null) answer = "?"; if (answer.indexOf('\n') >= 0 || answer.indexOf('\r') >= 0) answer = quote(answer); return answer; } public static boolean makeAndroid3_consoleInUse() { for (Object o : record_list) if (o instanceof Android3 && ((Android3) o).console) return true; return false; } public static Responder makeAndroid3_verboseResponder(final Android3 a) { return new Responder() { public String answer(String s, List history) { if (a.verbose) print("> " + shorten(s, a.incomingPrintLimit)); String answer = a.responder.answer(s, history); if (a.verbose) print("< " + shorten(answer, a.incomingPrintLimit)); return answer; } }; } public static ThreadLocal makeAndroid3_io = new ThreadLocal(); public static Android3 makeAndroid3() { return makeAndroid3(getProgramTitle() + "."); } public static int makeBot(String greeting) { return makeAndroid3(greeting).port; } public static Android3 makeBot(Android3 a) { makeAndroid3(a); return a; } public static Android3 makeBot(String greeting, Object responder) { Android3 a = new Android3(greeting); a.responder = makeResponder(responder); makeBot(a); return a; } public static Android3 makeBot() { return makeAndroid3(defaultBotName()); } public static void saveLocally(String variableName) { saveLocally(programID(), variableName); } public static void saveLocally(String progID, String variableName) { saveLocally2(mc(), progID, variableName); } public static void saveLocally2(Object obj, String variableName) { saveLocally2(obj, programID(), variableName); } public static void saveLocally2(Object obj, String progID, String variableName) { Lock __1156 = saveLock(); lock(__1156); try { File textFile = new File(programDir(progID), variableName + ".text"); File structureFile = new File(programDir(progID), variableName + ".structure"); Object x = get(obj, variableName); if (x == null) { textFile.delete(); structureFile.delete(); } else if (x instanceof String) { saveTextFile(textFile, (String) x); structureFile.delete(); } else { saveTextFile(structureFile, javaTokWordWrap(structure(x))); textFile.delete(); } } finally { unlock(__1156); } } public static boolean triple_anyPartNull(T3 t) { return t == null || t.a == null || t.b == null || t.c == null; } public static T3 trimTriple(T3 t) { return t == null ? null : triple(trim(t.a), trim(t.b), trim(t.c)); } public static T3 ai_tripleToInvalid(T3 t) { return triple(t.a, "[invalid] " + t.b, t.c); } public static ThreadLocal ai_postHandler = new ThreadLocal(); public static AI_PostHandler ai_postHandler() { if (ai_useThoughtSpaces()) return currentThoughtSpace(); return ai_postHandler.get(); } public static T3 toSymbolTriple(T3 t) { return t3(symbol(t.a), symbol(t.b), symbol(t.c)); } public static String strOrNull(Object o) { return o == null ? null : str(o); } public static void ai_reuseTripleIndex(Object index) { copyAllFields(index, tripleIndex()); } public static List localSoftwareMadeTriples() { List triples = new ArrayList(); for (String progID : localSoftwareMadeWebs_programsToRead()) try { triples.addAll(websMadeByProgramAsTriples(progID)); } catch (Throwable __e) { _handleException(__e); } return triples; } public static long ratio(long x, long y) { return y == 0 ? 0 : x / y; } public static String nTriples(long i) { return n_fancy2(i, "triple", "triples"); } public static String nTriples(Collection c) { return n_fancy2(c, "triple", "triples"); } public static String unicode_micro() { return "µ"; } public static void ai_replaceTripleIndexWithTriples(List l) { TripleIndex i = tripleIndex(); i.clear(); for (TripleWeb w : l) i.addTriple(w); i.trimToSize(); } public static int ai_deleteInvalidTriples() { Matches m = new Matches(); int count = 0; for (TripleWeb w : ai_triples("$X", "is", "invalid")) if (swic_trim(w.a, "Web ", m) && isGlobalID(m.unq(0))) { String id = m.unq(0); if (eq(id, "rjncddmdgpisijjq")) print("rjncddmdgpisijjq!"); TripleWeb ww = tripleIndex().getTriple(asGlobalID(id)); if (ww != null) ++count; tripleIndex().removeTriple(ww); ai_invalidatedWebs_cache_put(asGlobalID(id), w.globalID()); } if (count > 0) print("Deleted " + n2(count, "invalid triple")); return count; } public static void callMainAsChild(Object child, String... args) { moveThisThreadToChild(child); callMain(child, args); } public static boolean setAdd(Collection c, A a) { if (c == null || c.contains(a)) return false; c.add(a); return true; } public static List onTransientWebAdded_list = synchroList(); public static void onTransientWebAdded_do(Object f) { setAdd(onTransientWebAdded_list, f); } public static void onTransientWebAdded(Web web) { pcallFAll(onTransientWebAdded_list, web); } public static Cache> knownVerbs_cache = new Cache("knownVerbs_load"); public static Set knownVerbs() { return knownVerbs_cache.get(); } public static Set knownVerbs_load() { return caseInsensitiveSet(tlft(loadSnippet("#1009502"))); } public static List ai_texts_multi(List a, List b, List c) { LinkedHashSet l = new LinkedHashSet(); if (!empty(a) && !empty(b) && !empty(c)) for (String _a : a) for (String _b : b) for (String _c : c) l.addAll(ai_texts(_a, _b, _c)); return cloneList(l); } public static List ai_texts_multi(String a, List b, String c) { return ai_texts_multi(ll(a), b, ll(c)); } public static List ai_texts_multi(List a, List b, String c) { return ai_texts_multi(a, b, ll(c)); } public static List ai_texts_multi(String a, String b, List c) { return ai_texts_multi(ll(a), ll(b), c); } public static SortedCondensedStrings2 hugeEnglishDictionary() { File zip = loadLibrary("#1002028"); List lines = lines(fromUTF8(loadBinaryFromZip(zip, "words.txt"))); return new SortedCondensedStrings2(lines); } public static String lower(String s) { return s == null ? null : s.toLowerCase(); } public static char lower(char c) { return Character.toLowerCase(c); } public static List emptyList() { return new ArrayList(); } public static List emptyList(int capacity) { return new ArrayList(capacity); } public static List emptyList(Iterable l) { return l instanceof Collection ? emptyList(((Collection) l).size()) : emptyList(); } public static WebNode web_firstNode(Web web) { return web == null ? null : first(web.nodes); } public static String defaultThreadName_name; public static String defaultThreadName() { if (defaultThreadName_name == null) defaultThreadName_name = "A thread by " + programID(); return defaultThreadName_name; } public static Runnable wrapAsActivity(Object r) { return toRunnable(r); } public static Thread newThread(Object runnable) { return new Thread(_topLevelErrorHandling(toRunnable(runnable))); } public static Thread newThread(Object runnable, String name) { if (name == null) name = defaultThreadName(); return new Thread(_topLevelErrorHandling(toRunnable(runnable)), name); } public static Thread newThread(String name, Object runnable) { return newThread(runnable, name); } public static Runnable toRunnable(final Object o) { if (o instanceof Runnable) return (Runnable) o; return new Runnable() { public void run() { try { callF(o); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callF(o)"; } }; } public static Map _registerThread_threads; public static Object _onRegisterThread; public static Thread _registerThread(Thread t) { if (_registerThread_threads == null) _registerThread_threads = newWeakHashMap(); _registerThread_threads.put(t, true); callF(_onRegisterThread, t); return t; } public static void _registerThread() { _registerThread(Thread.currentThread()); } public static boolean warn_on = true; public static void warn(String s) { if (warn_on) print("Warning: " + s); } public static void warn(String s, List warnings) { warn(s); if (warnings != null) warnings.add(s); } public static boolean infoMessage_alwaysOnTop = true; public static double infoMessage_defaultTime = 5.0; public static JWindow infoMessage(String text) { return infoMessage(text, infoMessage_defaultTime); } public static JWindow infoMessage(final String text, final double seconds) { printHidingCredentials(text); return infoMessage_noprint(text, seconds); } public static JWindow infoMessage_noprint(String text) { return infoMessage_noprint(text, infoMessage_defaultTime); } public static JWindow infoMessage_noprint(final String _text, final double seconds) { final String text = hideCredentials(_text); if (empty(text)) return null; logQuotedWithDate(infoBoxesLogFile(), text); if (isHeadless()) return null; return (JWindow) swingAndWait(new F0() { public Object get() { try { final JWindow window = showWindow(infoMessage_makePanel(text)); window.setSize(300, 150); moveToTopRightCorner(window); if (infoMessage_alwaysOnTop) window.setAlwaysOnTop(true); window.setVisible(true); disposeWindowAfter(iround(seconds * 1000), window); return window; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "final JWindow window = showWindow(infoMessage_makePanel(text));\r\n window.s..."; } }); } public static JWindow infoMessage(Throwable e) { showConsole(); printStackTrace(e); return infoMessage(exceptionToStringShort(e)); } public static String unnull(String s) { return s == null ? "" : s; } public static List unnull(List l) { return l == null ? emptyList() : l; } public static Map unnull(Map l) { return l == null ? emptyMap() : l; } public static Iterable unnull(Iterable i) { return i == null ? emptyList() : i; } public static A[] unnull(A[] a) { return a == null ? (A[]) new Object[0] : a; } public static BitSet unnull(BitSet b) { return b == null ? new BitSet() : b; } public static Pt unnull(Pt p) { return p == null ? new Pt() : p; } public static Collection values(Map map) { return map == null ? emptyList() : map.values(); } public static Collection values(MultiMap mm) { return mm == null ? emptyList() : concatLists(values(mm.data)); } public static Collection values(SmallestListMultiMap mm) { return mm == null ? emptyList() : concatLists(values(mm.asMap())); } public static void innerCleanUp(Object c) { if (!isFalse(pcallOpt(c, "cleanMeUp"))) for (String name : sorted(methodsStartingWith(c, "cleanMeUp_"))) pcallOpt(c, name); } public static void innerCleanUp() { innerCleanUp(mc()); } public static Object pcallOpt(Object o, String method, Object... args) { try { return callOpt(o, method, args); } catch (Throwable __e) { _handleException(__e); } return null; } public static List registeredThreads(Object o) { Map map = (Map) (getOpt(o, "_registerThread_threads")); if (map == null) return ll(); map.size(); synchronized (map) { return asList(keys(map)); } } public static List registeredThreads() { _registerThread_threads.size(); return asList(keys(_registerThread_threads)); } public static String joinWithComma(Collection c) { return join(", ", c); } public static String joinWithComma(String... c) { return join(", ", c); } public static void interruptThreads(List threads) { for (Thread t : unnull(threads)) t.interrupt(); } public static void interruptThreads(Class mainClass) { interruptThreads(registeredThreads(mainClass)); } public static void retireClassLoader(ClassLoader cl) { if (isJavaXClassLoader(cl)) setOpt(cl, "retired", true); } public static A callMain(A c, String... args) { callOpt(c, "main", new Object[] { args }); return c; } public static void callMain() { callMain(mc()); } public static void setOpt_raw(Object o, String field, Object value) { try { if (o == null) return; if (o instanceof Class) setOpt_raw((Class) o, field, value); else { Field f = setOpt_raw_findField(o.getClass(), field); if (f != null) { f.setAccessible(true); smartSet(f, o, value); } } } catch (Exception __e) { throw rethrow(__e); } } public static void setOpt_raw(Class c, String field, Object value) { try { if (c == null) return; Field f = setOpt_raw_findStaticField(c, field); if (f != null) { f.setAccessible(true); smartSet(f, null, value); } } catch (Exception __e) { throw rethrow(__e); } } public static Field setOpt_raw_findStaticField(Class c, String field) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) return f; _c = _c.getSuperclass(); } while (_c != null); return null; } public static Field setOpt_raw_findField(Class c, String field) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field)) return f; _c = _c.getSuperclass(); } while (_c != null); return null; } public static void smartSet(Field f, Object o, Object value) throws Exception { try { f.set(o, value); } catch (Exception e) { Class type = f.getType(); if (type == int.class && value instanceof Long) value = ((Long) value).intValue(); if (type == LinkedHashMap.class && value instanceof Map) { f.set(o, asLinkedHashMap((Map) value)); return; } try { if (f.getType() == Concept.Ref.class) { f.set(o, ((Concept) o).new Ref((Concept) value)); return; } if (o instanceof Concept.Ref) { f.set(o, ((Concept.Ref) o).get()); return; } } catch (Throwable _e) { } throw e; } } public static Object callFunction(Object f, Object... args) { return callF(f, args); } public static Object invokeMethod(Method m, Object o, Object... args) { try { try { return m.invoke(o, args); } catch (InvocationTargetException e) { throw rethrow(getExceptionCause(e)); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(e.getMessage() + " - was calling: " + m + ", args: " + joinWithSpace(classNames(args))); } } catch (Exception __e) { throw rethrow(__e); } } public static boolean isInstanceX(Class type, Object arg) { if (type == boolean.class) return arg instanceof Boolean; if (type == int.class) return arg instanceof Integer; if (type == long.class) return arg instanceof Long; if (type == float.class) return arg instanceof Float; if (type == short.class) return arg instanceof Short; if (type == char.class) return arg instanceof Character; if (type == byte.class) return arg instanceof Byte; if (type == double.class) return arg instanceof Double; return type.isInstance(arg); } public static RuntimeException fail() { throw new RuntimeException("fail"); } public static RuntimeException fail(Throwable e) { throw asRuntimeException(e); } public static RuntimeException fail(Object msg) { throw new RuntimeException(String.valueOf(msg)); } public static RuntimeException fail(String msg) { throw new RuntimeException(msg == null ? "" : msg); } public static RuntimeException fail(String msg, Throwable innerException) { throw new RuntimeException(msg, innerException); } public static String getClassName(Object o) { return o == null ? "null" : o instanceof Class ? ((Class) o).getName() : o.getClass().getName(); } public static String getType(Object o) { return getClassName(o); } public static volatile PersistableThrowable lastException_lastException; public static PersistableThrowable lastException() { return lastException_lastException; } public static void lastException(Throwable e) { lastException_lastException = persistableThrowable(e); } public static Throwable getInnerException(Throwable e) { while (e.getCause() != null) e = e.getCause(); return e; } public static String hideCredentials(URL url) { return url == null ? null : hideCredentials(str(url)); } public static String hideCredentials(String url) { return url.replaceAll("([&?])(_pass|key)=[^&\\s\"]*", "$1$2="); } public static String hideCredentials(Object o) { return hideCredentials(str(o)); } public static String baseClassName(String className) { return substring(className, className.lastIndexOf('.') + 1); } public static String baseClassName(Object o) { return baseClassName(getClassName(o)); } public static String prependIfNempty(String prefix, String s) { return empty(s) ? s : prefix + s; } public static boolean isAWTThread() { if (isAndroid()) return false; if (isHeadless()) return false; return isAWTThread_awt(); } public static boolean isAWTThread_awt() { return SwingUtilities.isEventDispatchThread(); } public static Object sleepQuietly_monitor = new Object(); public static void sleepQuietly() { try { assertFalse(isAWTThread()); synchronized (sleepQuietly_monitor) { sleepQuietly_monitor.wait(); } } catch (Exception __e) { throw rethrow(__e); } } public static List> ai_texts_xy_bothQuoted(String a, String b, String c) { List> l = new ArrayList(); for (Pair p : ai_texts_dollarXY(a, b, c)) if (isProperlyQuoted2(p.a) && isProperlyQuoted2(p.b)) l.add(pair(unquote(p.a), unquote(p.b))); return l; } public static Map matchDollarVarsIC(String pat, String s) { if (pat == null || s == null) return null; List vars = new ArrayList(); String pat2 = dollarVarsToStars(pat, vars); Matches m = new Matches(); if (!match(pat2, s, m)) return null; Map map = new HashMap(); for (int i = 0; i < l(m.m); i++) { String var = vars.get(i); String value = m.get(i); String oldValue = map.get(var); if (oldValue == null) map.put(var, value); else if (neqic(oldValue, value)) return null; } return map; } public static String formatDollarVars(String pat, Map vars) { List tok = javaTok(pat); for (int i = 1; i < l(tok); i += 2) { String t = tok.get(i); String value = vars.get(t); if (value != null) tok.set(i, value); } return join(tok); } public static String ai_dropDroppablePrefix(String s) { for (WebNode node : ai_search_dollarX_verified("$X", "is", "a droppable prefix")) { if (isQuoted(web_text(node))) { String tail = web_unquote(node); if (nempty(tail) && swic(s, tail)) { print("Dropping prefix: " + quote(tail)); return trim(substring(s, l(tail))); } } } return s; } public static String ai_dropDroppableTail(String s) { for (WebNode node : ai_search_dollarX_verified("$X", "is", "a droppable tail")) { if (isQuoted(web_text(node))) { String tail = web_unquote(node); if (nempty(tail) && ewic(s, tail)) { print("Dropping tail: " + quote(tail)); return dropSuffixTrimIC(tail, s); } } } return s; } public static Collection ai_withoutInvalidNodes(Collection l) { if (l == null) return null; for (WebNode n : l) if (ai_isInvalidWeb(n.web)) { Iterator it = iterator(l); WebNode node; List x = new ArrayList(); while ((node = it.next()) != n) x.add(node); while (it.hasNext()) { node = it.next(); if (!ai_isInvalidWeb(node.web)) x.add(node); } return x; } return l; } public static List ai_withoutInvalidNodes(List l) { return (List) ai_withoutInvalidNodes((Collection) l); } public static List indexedNodes_withInvalids(CharSequence _s) { String s = str(_s); List l = indexedNodes_rawLookup(s); for (VirtualNodeIndex i : concurrentlyIterateList(virtualNodeIndices())) { List x = i.get(s); if (nempty(x)) { if (empty(l)) l = x; else { if (!(l instanceof CombinedList)) l = combinedList_ll(l); ((CombinedList) l).addList(x); } } } return assertNotNull(l); } public static List indexedNodes_withInvalids(AI_SubSpace ss, CharSequence s) { return ss.indexedNodes_rawLookup(symbol(s)); } public static WebNode web_operand(WebNode node, String... rels) { return web_findForwardRelated_multiRel(node, rels); } public static String asString(Object o) { return o == null ? null : o.toString(); } public static String loadTextFile(String fileName) { return loadTextFile(fileName, null); } public static String loadTextFile(File f, String defaultContents) { try { checkFileNotTooBigToRead(f); if (f == null || !f.exists()) return defaultContents; FileInputStream fileInputStream = new FileInputStream(f); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); return loadTextFile(inputStreamReader); } catch (Exception __e) { throw rethrow(__e); } } public static String loadTextFile(File fileName) { return loadTextFile(fileName, null); } public static String loadTextFile(String fileName, String defaultContents) { return fileName == null ? defaultContents : loadTextFile(newFile(fileName), defaultContents); } public static String loadTextFile(Reader reader) throws IOException { StringBuilder builder = new StringBuilder(); try { char[] buffer = new char[1024]; int n; while (-1 != (n = reader.read(buffer))) builder.append(buffer, 0, n); } finally { reader.close(); } return str(builder); } public static boolean deleteFile(File file) { return file != null && file.delete(); } public static File makeFileNameUnique_withExtension(File f, String ext) { File orig = f; int n = 0; ext = addPrefixIfNempty(".", dropPrefix(".", ext)); String name = dropSuffixIC(ext, orig.getName()); f = new File(dirOfFile(orig), name + ext); while (f.exists()) f = new File(dirOfFile(orig), name + "." + (++n) + ext); return f; } public static File javaxCachesDir_dir; public static File javaxCachesDir() { return javaxCachesDir_dir != null ? javaxCachesDir_dir : new File(userHome(), "JavaX-Caches"); } public static File javaxCachesDir(String sub) { return newFile(javaxCachesDir(), sub); } public static int numLines(String s) { return countLines(s); } public static File saveTextFile(String fileName, String contents) throws IOException { CriticalAction action = beginCriticalAction("Saving file " + fileName + " (" + l(contents) + " chars)"); try { File file = new File(fileName); mkdirsForFile(file); String tempFileName = fileName + "_temp"; File tempFile = new File(tempFileName); if (contents != null) { if (tempFile.exists()) try { String saveName = tempFileName + ".saved." + now(); copyFile(tempFile, new File(saveName)); } catch (Throwable e) { printStackTrace(e); } FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath()); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8"); PrintWriter printWriter = new PrintWriter(outputStreamWriter); printWriter.print(contents); printWriter.close(); } if (file.exists() && !file.delete()) throw new IOException("Can't delete " + fileName); if (contents != null) if (!tempFile.renameTo(file)) throw new IOException("Can't rename " + tempFile + " to " + file); return file; } finally { action.done(); } } public static File saveTextFile(File fileName, String contents) { try { saveTextFile(fileName.getPath(), contents); return fileName; } catch (Exception __e) { throw rethrow(__e); } } public static boolean structure_showTiming, structure_checkTokenCount; public static String structure(Object o) { return structure(o, new structure_Data()); } public static String structure(Object o, structure_Data d) { StringWriter sw = new StringWriter(); d.out = new PrintWriter(sw); structure_go(o, d); String s = str(sw); if (structure_checkTokenCount) { print("token count=" + d.n); assertEquals("token count", l(javaTokC(s)), d.n); } return s; } public static void structure_go(Object o, structure_Data d) { structure_1(o, d); while (nempty(d.stack)) popLast(d.stack).run(); } public static void structureToPrintWriter(Object o, PrintWriter out) { structure_Data d = new structure_Data(); d.out = out; structure_go(o, d); } public static boolean structure_allowShortening = false; public static class structure_Data { public PrintWriter out; public int stringSizeLimit; public int shareStringsLongerThan = 20; public boolean noStringSharing; public IdentityHashMap seen = new IdentityHashMap(); public HashMap strings = new HashMap(); public HashSet concepts = new HashSet(); public HashMap> fieldsByClass = new HashMap(); public int n; public List stack = new ArrayList(); public structure_Data append(String token) { out.print(token); ++n; return this; } public structure_Data append(int i) { out.print(i); ++n; return this; } public structure_Data append(String token, int tokCount) { out.print(token); n += tokCount; return this; } public structure_Data app(String token) { out.print(token); return this; } public structure_Data app(int i) { out.print(i); return this; } } public static void structure_1(final Object o, final structure_Data d) { if (o == null) { d.append("null"); return; } Class c = o.getClass(); boolean concept = false; concept = o instanceof Concept; List lFields = d.fieldsByClass.get(c); if (lFields == null) { if (o instanceof Number) { PrintWriter out = d.out; if (o instanceof Integer) { int i = ((Integer) o).intValue(); out.print(i); d.n += i < 0 ? 2 : 1; return; } if (o instanceof Long) { long l = ((Long) o).longValue(); out.print(l); out.print("L"); d.n += l < 0 ? 2 : 1; return; } if (o instanceof Short) { short s = ((Short) o).shortValue(); d.append("sh ", 2); out.print(s); d.n += s < 0 ? 2 : 1; return; } if (o instanceof Float) { d.append("fl ", 2); quoteToPrintWriter(str(o), out); return; } if (o instanceof Double) { d.append("d(", 3); quoteToPrintWriter(str(o), out); d.append(")"); return; } if (o instanceof BigInteger) { out.print("bigint("); out.print(o); out.print(")"); d.n += ((BigInteger) o).signum() < 0 ? 5 : 4; return; } } if (o instanceof Boolean) { d.append(((Boolean) o).booleanValue() ? "t" : "f"); return; } if (o instanceof Character) { d.append(quoteCharacter((Character) o)); return; } if (o instanceof File) { d.append("File ").append(quote(((File) o).getPath())); return; } Integer ref = d.seen.get(o); if (o instanceof String && ref == null) ref = d.strings.get((String) o); if (ref != null) { d.append("t").app(ref); return; } if (!(o instanceof String)) d.seen.put(o, d.n); else { String s = d.stringSizeLimit != 0 ? shorten((String) o, d.stringSizeLimit) : (String) o; if (!d.noStringSharing) { if (d.shareStringsLongerThan == Integer.MAX_VALUE) d.seen.put(o, d.n); if (l(s) >= d.shareStringsLongerThan) d.strings.put(s, d.n); } quoteToPrintWriter(s, d.out); d.n++; return; } if (o instanceof HashSet) { d.append("hashset "); structure_1(new ArrayList((Set) o), d); return; } if (o instanceof TreeSet) { d.append("treeset "); structure_1(new ArrayList((Set) o), d); return; } String name = c.getName(); if (o instanceof Collection && !startsWith(name, "main$")) { d.append("["); final int l = d.n; final Iterator it = ((Collection) o).iterator(); d.stack.add(new Runnable() { public void run() { try { if (!it.hasNext()) d.append("]"); else { d.stack.add(this); if (d.n != l) d.append(", "); structure_1(it.next(), d); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (!it.hasNext())\r\n d.append(\"]\");\r\n else {\r\n d.sta..."; } }); return; } if (o instanceof Map && !startsWith(name, "main$")) { if (o instanceof LinkedHashMap) d.append("lhm"); else if (o instanceof HashMap) d.append("hm"); else if (name.equals("java.util.Collections$SynchronizedMap")) d.append("sync"); else if (name.equals("java.util.Collections$SynchronizedSortedMap")) { d.append("sync tm", 2); } d.append("{"); final int l = d.n; final Iterator it = ((Map) o).entrySet().iterator(); d.stack.add(new Runnable() { public boolean v; public Map.Entry e; public void run() { if (v) { d.append("="); v = false; d.stack.add(this); structure_1(e.getValue(), d); } else { if (!it.hasNext()) d.append("}"); else { e = (Map.Entry) it.next(); v = true; d.stack.add(this); if (d.n != l) d.append(", "); structure_1(e.getKey(), d); } } } }); return; } if (c.isArray()) { if (o instanceof byte[]) { d.append("ba ").append(quote(bytesToHex((byte[]) o))); return; } final int n = Array.getLength(o); if (o instanceof boolean[]) { String hex = boolArrayToHex((boolean[]) o); int i = l(hex); while (i > 0 && hex.charAt(i - 1) == '0' && hex.charAt(i - 2) == '0') i -= 2; d.append("boolarray ").append(n).app(" ").append(quote(substring(hex, 0, i))); return; } String atype = "array", sep = ", "; if (o instanceof int[]) { atype = "intarray"; sep = " "; } d.append(atype).append("{"); d.stack.add(new Runnable() { public int i; public void run() { if (i >= n) d.append("}"); else { d.stack.add(this); if (i > 0) d.append(", "); structure_1(Array.get(o, i++), d); } } }); return; } if (o instanceof Class) { d.append("class(", 2).append(quote(((Class) o).getName())).append(")"); return; } if (o instanceof Throwable) { d.append("exception(", 2).append(quote(((Throwable) o).getMessage())).append(")"); return; } if (o instanceof BitSet) { BitSet bs = (BitSet) o; d.append("bitset{", 2); int l = d.n; for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) { if (d.n != l) d.append(", "); d.append(i); } d.append("}"); return; } if (name.startsWith("java.") || name.startsWith("javax.")) { d.append("j ").append(quote(str(o))); return; } if (o instanceof Lisp) { d.append("l(", 2); final Lisp lisp = (Lisp) (o); structure_1(lisp.head, d); final Iterator it = lisp.args == null ? emptyIterator() : lisp.args.iterator(); d.stack.add(new Runnable() { public void run() { try { if (!it.hasNext()) d.append(")"); else { d.stack.add(this); d.append(", "); structure_1(it.next(), d); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (!it.hasNext())\r\n d.append(\")\");\r\n else {\r\n ..."; } }); return; } String dynName = shortDynamicClassName(o); if (concept && !d.concepts.contains(dynName)) { d.concepts.add(dynName); d.append("c "); } TreeSet fields = new TreeSet(new Comparator() { public int compare(Field a, Field b) { return stdcompare(a.getName(), b.getName()); } }); Class cc = c; while (cc != Object.class) { for (Field field : getDeclaredFields_cached(cc)) { if ((field.getModifiers() & (java.lang.reflect.Modifier.STATIC | java.lang.reflect.Modifier.TRANSIENT)) != 0) continue; String fieldName = field.getName(); fields.add(field); } cc = cc.getSuperclass(); } lFields = asList(fields); for (int i = 0; i < l(lFields); i++) { Field f = lFields.get(i); if (f.getName().equals("this$1")) { lFields.remove(i); lFields.add(0, f); break; } } d.fieldsByClass.put(c, lFields); } else { Integer ref = d.seen.get(o); if (ref != null) { d.append("t").app(ref); return; } d.seen.put(o, d.n); } LinkedHashMap fv = new LinkedHashMap(); for (Field f : lFields) { Object value; try { value = f.get(o); } catch (Exception e) { value = "?"; } if (value != null) fv.put(f.getName(), value); } String name = c.getName(); String shortName = dropPrefix("main$", name); if (concept && eq(fv.get("className"), shortName)) fv.remove("className"); if (o instanceof DynamicObject) { fv.putAll((Map) fv.get("fieldValues")); fv.remove("fieldValues"); shortName = shortDynamicClassName(o); fv.remove("className"); } String singleField = fv.size() == 1 ? first(fv.keySet()) : null; d.append(shortName); final int l = d.n; final Iterator it = fv.entrySet().iterator(); d.stack.add(new Runnable() { public void run() { try { if (!it.hasNext()) { if (d.n != l) d.append(")"); } else { Map.Entry e = (Map.Entry) it.next(); d.append(d.n == l ? "(" : ", "); d.append((String) e.getKey()).append("="); d.stack.add(this); structure_1(e.getValue(), d); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (!it.hasNext()) {\r\n if (d.n != l)\r\n d.append(\")\");\r\n } else..."; } }); } public static Matcher regexpIC(String pat, String s) { return Pattern.compile(pat, Pattern.CASE_INSENSITIVE).matcher(s); } public static Pattern regexpIC(String pat) { return Pattern.compile(pat, Pattern.CASE_INSENSITIVE); } public static Class __javax; public static Class getJavaX() { try { return __javax; } catch (Exception __e) { throw rethrow(__e); } } public static void cleanKill() { cleanKillVM(); } public static long getFileSize(String path) { return path == null ? 0 : new File(path).length(); } public static long getFileSize(File f) { return f == null ? 0 : f.length(); } public static String javaTok_substringC(String s, int i, int j) { return s.substring(i, j); } public static A assertNotNull(A a) { assertTrue(a != null); return a; } public static A assertNotNull(String msg, A a) { assertTrue(msg, a != null); return a; } public static Web ai_getWeb(String id) { if (!possibleGlobalID(id)) return null; return ai_getWeb(asGlobalID(id)); } public static Web ai_getWeb(GlobalID id) { if (id == null) return null; Web web = allWebsByID().getFirst(id); if (web != null) return web; for (VirtualNodeIndex i : virtualNodeIndices()) { web = i.getWeb(id); if (web != null) return web; } return null; } public static WebRelation web_findRelationNamed(Web web, String s) { return web_findRelationWithLabel(web, s); } public static String web_unquoted(WebNode node) { return web_unquote(node); } public static A assertEquals(Object x, A y) { return assertEquals(null, x, y); } public static A assertEquals(String msg, Object x, A y) { if (!(x == null ? y == null : x.equals(y))) throw fail((msg != null ? msg + ": " : "") + y + " != " + x); return y; } public static void assertTrue(Object o) { if (!(eq(o, true))) throw fail(str(o)); } public static boolean assertTrue(String msg, boolean b) { if (!b) throw fail(msg); return b; } public static boolean assertTrue(boolean b) { if (!b) throw fail("oops"); return b; } public static List splitAtJavaToken(String s, String splitToken) { return splitByJavaToken(s, splitToken); } public static List replaceIC(List l, String a, String b) { List l2 = new ArrayList(); for (int i = 0; i < l(l); i++) { String s = l.get(i); l2.add(eqic(s, a) ? b : s); } return l2; } public static String replaceIC(String s, String a, String b) { return s.replaceAll("(?i)" + patternQuote(a), quoteReplacement(b)); } public static List ai_postTriple_multi(String a, String b, Collection c) { List webIDs = new ArrayList(); for (String s : unnull(c)) addIfNotNull(webIDs, ai_postTriple(a, b, s)); return webIDs; } public static List ai_postTriple_multi(Collection a, String b, String c) { List webIDs = new ArrayList(); for (String s : unnull(a)) addIfNotNull(webIDs, ai_postTriple(s, b, c)); return webIDs; } public static Set synchroSet() { return synchroHashSet(); } public static Set synchroSet(Set set) { return Collections.synchronizedSet(set); } public static Set newWeakHashSet() { return synchroWeakHashSet(); } public static Thread currentThread() { return Thread.currentThread(); } public static List beforeDelegatingToThread_operations = synchroList(); public static void beforeDelegatingToThread(Thread t) { for (Object op : cloneList(beforeDelegatingToThread_operations)) pcallF(op, t); } public static void beforeDelegatingToThread_do(Object f) { setAdd(beforeDelegatingToThread_operations, f); } public static void cancelAndInterruptThread(Thread t) { if (t == null) return; cancelThread(t); t.interrupt(); } public static Either either2(B b) { return new Either(2, b); } public static Either either1(A a) { return new Either(1, a); } public static List afterDelegatingToThread_operations = synchroList(); public static void afterDelegatingToThread(Thread t) { for (Object op : cloneList(afterDelegatingToThread_operations)) pcallF(op, t); } public static void afterDelegatingToThread_do(Object f) { setAdd(afterDelegatingToThread_operations, f); } public static int toMS_int(double seconds) { return toInt_checked((long) (seconds * 1000)); } public static String n_fancy2(long l, String singular, String plural) { return formatWithThousandsSeparator(l) + " " + trim(l == 1 ? singular : plural); } public static String n_fancy2(Collection l, String singular, String plural) { return n_fancy2(l(l), singular, plural); } public static String n_fancy2(Map m, String singular, String plural) { return n_fancy2(l(m), singular, plural); } public static String n_fancy2(Object[] a, String singular, String plural) { return n_fancy2(l(a), singular, plural); } public static String n_fancy2(MultiSet ms, String singular, String plural) { return n_fancy2(l(ms), singular, plural); } public static long getLastTiming() { ThreadLocal tl = (ThreadLocal) (getOpt(getMainClass(), "saveTiming_last")); if (tl == null) return -1; Long l = tl.get(); return l == null ? -1 : l; } public static long parseSnippetID(String snippetID) { long id = Long.parseLong(shortenSnippetID(snippetID)); if (id == 0) throw fail("0 is not a snippet ID"); return id; } public static String javaxDefaultVMArgs() { return trim(loadProgramTextFile("#1005850", "default-vm-args", "")); } public static void appendToLocalMechLog(String listName, String text) { logMechListAppendLocally(listName, text); } public static List dropSecondIfEmpty(String a, String b) { return empty(b) ? ll(a) : ll(a, b); } public static int parseFirstInt(String s) { return parseInt(jextract("", s)); } public static boolean usePreSpunVMs() { return eq("1", trim(loadProgramTextFile("#1005457", "enable-prespun-vms"))); } public static void usePreSpunVMs(boolean flag) { saveProgramTextFile("#1005457", "enable-prespun-vms", flag ? "1" : null); } public static String format3(String pat, Object... args) { if (args.length == 0) return pat; List tok = javaTokPlusPeriod(pat); int argidx = 0; for (int i = 1; i < tok.size(); i += 2) if (tok.get(i).equals("*")) tok.set(i, format3_formatArg(argidx < args.length ? args[argidx++] : "null")); return join(tok); } public static String format3_formatArg(Object arg) { if (arg == null) return "null"; if (arg instanceof String) { String s = (String) arg; return isIdentifier(s) || isNonNegativeInteger(s) ? s : quote(s); } if (arg instanceof Integer || arg instanceof Long) return String.valueOf(arg); return quote(structure(arg)); } public static String sendToLocalBotOpt(String bot, String text, Object... args) { if (bot == null) return null; text = format(text, args); DialogIO channel = findBot(bot); if (channel == null) { print(quote(bot) + " not found, skipping send: " + quote(text)); return null; } try { channel.readLine(); print(shorten(bot + "> " + text, 200)); channel.sendLine(text); String s = channel.readLine(); print(shorten(bot + "< " + s, 200)); return s; } catch (Throwable e) { e.printStackTrace(); return null; } finally { channel.close(); } } public static boolean match3(String pat, String s) { return match3(pat, s, null); } public static boolean match3(String pat, String s, Matches matches) { if (pat == null || s == null) return false; return match3(pat, parse3_cached(s), matches); } public static boolean match3(String pat, List toks, Matches matches) { List tokpat = parse3(pat); return match3(tokpat, toks, matches); } public static boolean match3(List tokpat, List toks, Matches matches) { String[] m = match2(tokpat, toks); if (m == null) return false; if (matches != null) matches.m = m; return true; } public static File classicNohupJavax(String javaxargs) { return classicNohupJavax(javaxargs, ""); } public static File classicNohupJavax(String javaxargs, String vmArgs) { try { File jar = pathToJavaxJar(); if (javaxargs.startsWith("#")) javaxargs = javaxargs.substring(1); String tempPrefix = takeFirst(30, nohup_sanitize(ymd_minus_hms() + "_" + javaxargs + "_")); File outFile; try { outFile = File.createTempFile(tempPrefix, ""); } catch (Throwable e) { _handleException(e); print("Temp file creation failed for " + tempPrefix + ", trying again."); outFile = File.createTempFile("", ""); } String cmd = joinWithSpace(uniquifyList(platformParseArgs(_javaCommand() + " " + vmArgs))) + " -jar " + (isWindows() ? winQuote(jar) : bashQuote(jar)) + " " + javaxargs; nohup(cmd, outFile, false); return outFile; } catch (Exception __e) { throw rethrow(__e); } } public static void rotateLogFile(File f) { Lock __1367 = appendToFile_lock; lock(__1367); try { if (!f.exists()) return; int n = 1; File p; while ((p = newFile(f.getParentFile(), f.getName() + ".part" + n + ".gz")).exists()) ++n; gzipFile(f, p); assertTrue(f.delete()); } finally { unlock(__1367); } } public static NavigableSet allIndexedTermsStartingWith(String prefix) { prefix = takeFirst(ai_indexShorteningLength(), prefix); return prefixSubSet(ai_navigableShortTerms(), prefix); } public static List ai_speculate_functions = ll("ai_spec_webIsTriple", "ai_spec_singularPlural", "ai_spec_giveMe", "ai_spec_parseAnd", "ai_spec_tripelize", "ai_spec_parseArrowTriple", "ai_spec_webContents", "ai_spec_addQuestionsToChatLine", "ai_spec_simplify", "ai_spec_containsThePhrase", "ai_spec_executeBestSplit", "ai_spec_parseIf", "ai_spec_subclass", "ai_spec_isAQuestion", "ai_spec_substWebs", "ai_spec_apostrophS", "ai_spec_allWords", "ai_spec_numbered", "ai_spec_howMany", "ai_spec_analyzeLitList", "ai_spec_doYouThink", "ai_spec_unquote", "ai_spec_quoteUnlessQuoted", "ai_spec_checkFact", "ai_spec_switcheroo", "ai_spec_groupingPrefix", "ai_spec_moveToMiddle", "ai_spec_compareNumbers", "ai_spec_compareNumbers_triple", "ai_spec_answerYesNoTriple", "ai_spec_moveToMiddle_triple", "ai_spec_searchForAnswer", "ai_specTemp_length", "ai_spec_whatIsXInY", "ai_spec_whatIsTheLength"); public static String ai_speculate(final String word) { if (empty(word) || loading()) return word; ai_withMaker("ai_speculate", new Runnable() { public void run() { try { ai_blob(word, false); if (isAdjective(word)) ai_postTriple(word, "is", "an adjective"); for (String f : ai_speculate_functions) ai_callSpeculationFunction(f, word); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ai_blob(word, false);\r\n if (isAdjective(word)) ai_postTriple(word, \"is\", \"..."; } }); ai_speculateOnWeb(ai_getWebFromTerm(word)); return word; } public static A uniqueConcept(Class c, Object... params) { return uniqueConcept(mainConcepts, c, params); } public static A uniqueConcept(Concepts cc, Class c, Object... params) { params = expandParams(c, params); A x = findConceptWhere(cc, c, params); if (x == null) { x = unlisted(c); csetAll(x, params); cc.register(x); } return x; } public static String structureForUser(Object o) { return beautifyStructure(struct_noStringSharing(o)); } public static Object[] toObjectArray(Collection c) { List l = asList(c); return l.toArray(new Object[l.size()]); } public static boolean hasMethod(Object o, String method, Object... args) { return findMethod(o, method, args) != null; } public static HashMap> callMC_cache = new HashMap(); public static String callMC_key; public static Method callMC_value; public static Object callMC(String method, String[] arg) { return callMC(method, new Object[] { arg }); } public static Object callMC(String method, Object... args) { try { Method me; if (callMC_cache == null) callMC_cache = new HashMap(); synchronized (callMC_cache) { me = method == callMC_key ? callMC_value : null; } if (me != null) try { return invokeMethod(me, null, args); } catch (IllegalArgumentException e) { throw new RuntimeException("Can't call " + me + " with arguments " + classNames(args), e); } List m; synchronized (callMC_cache) { m = callMC_cache.get(method); } if (m == null) { if (callMC_cache.isEmpty()) { callMC_makeCache(); m = callMC_cache.get(method); } if (m == null) throw fail("Method named " + method + " not found in main"); } int n = m.size(); if (n == 1) { me = m.get(0); synchronized (callMC_cache) { callMC_key = method; callMC_value = me; } try { return invokeMethod(me, null, args); } catch (IllegalArgumentException e) { throw new RuntimeException("Can't call " + me + " with arguments " + classNames(args), e); } } for (int i = 0; i < n; i++) { me = m.get(i); if (call_checkArgs(me, args, false)) return invokeMethod(me, null, args); } throw fail("No method called " + method + " with matching arguments found in main"); } catch (Exception __e) { throw rethrow(__e); } } public static void callMC_makeCache() { synchronized (callMC_cache) { callMC_cache.clear(); Class _c = (Class) mc(), c = _c; while (c != null) { for (Method m : c.getDeclaredMethods()) if ((m.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) { m.setAccessible(true); multiMapPut(callMC_cache, m.getName(), m); } c = c.getSuperclass(); } } } public static ThreadLocal makeAndCall_initChild = new ThreadLocal(); public static Object makeAndCall_cached(String functionName, Object... args) { Class c = loadFunctions_cached(functionName); callF(makeAndCall_initChild.get(), c); return preciseCall(c, functionName, args); } public static Set asSet(Object[] array) { HashSet set = new HashSet(); for (Object o : array) if (o != null) set.add(o); return set; } public static Set asSet(String[] array) { TreeSet set = new TreeSet(); for (String o : array) if (o != null) set.add(o); return set; } public static Set asSet(Collection l) { if (l instanceof Set) return (Set) l; HashSet set = new HashSet(); for (A o : l) if (o != null) set.add(o); return set; } public static String assertIdentifier(String s) { return assertIsIdentifier(s); } public static String smartBotEval_noTimeout(String expression) { long _startTime_1408 = sysNow(); final String main = smartBotEval_prelude() + evalJava_prep(expression, "calc", "ai_spec_init();\n"); print("--\n" + main + "\n--"); final Var o = new Var(); final Var error = new Var(); String printed = hijackPrint_tee(new Runnable() { public void run() { try { try { o.set(evalJava_myProgramID_main(main)); } catch (Throwable e) { _handleException(e); error.set(e); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "try {\r\n o.set(evalJava_myProgramID_main(main));\r\n } catch (Throwa..."; } }); smartBot_savePrintLog(expression, printed); _startTime_1408 = sysNow() - _startTime_1408; saveTiming(_startTime_1408); if (error.has()) throw rethrow(error.get()); String s = sfuOrText(o.get()); return "[" + lastTiming() + " ms]" + (containsNewLine(trim(s)) ? "\n\n" + s : " " + s); } public static int iround(double d) { return (int) Math.round(d); } public static Pair pair(A a, B b) { return new Pair(a, b); } public static Pair pair(A a) { return new Pair(a, a); } public static Field getOpt_findField(Class c, String field) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field)) return f; _c = _c.getSuperclass(); } while (_c != null); return null; } public static RuntimeException asRuntimeException(Throwable t) { if (t instanceof Error) _handleError((Error) t); return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t); } public static String quoteUnlessQuoted(String s) { return s == null ? null : isQuoted(s) ? s : quote(s); } public static String ai_forward(String a, String b) { return ai_text(a, b, "$X"); } public static String firstToUpper(String s) { if (empty(s)) return s; return Character.toUpperCase(s.charAt(0)) + s.substring(1); } public static Object quickImport(Object o) { return quickExport(o, mc()); } public static Object callAndMake(String functionName, Object... args) { return callAndMake_orDirect(functionName, args); } public static GZIPOutputStream newGZIPOutputStream(File f) { try { return new GZIPOutputStream(newFileOutputStream(f)); } catch (Exception __e) { throw rethrow(__e); } } public static int ai_writeTripleFile(Collection triples, OutputStream out) { try { print("Writing triple file"); List names = new ArrayList(); Map index = new HashMap(); ai_writeTripleFile_precollectNames(names, index, triples); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintWriter writer = utf8printWriter(baos); int n = 0; for (TripleWeb w : triples) { ++n; String title = null; String moreInfo = quote(str(w.globalID())) + " " + quote(unnull(title)) + " " + quote(unnull(w.source())) + " " + (w.unverified() ? "u" : "v") + " " + w.created(); Object result = w.result(); if (result != null) moreInfo += " (" + sfu(result) + ")"; writer.write(ai_writeTripleFile_indexGet(names, index, w.a) + " " + ai_writeTripleFile_indexGet(names, index, w.b) + " " + ai_writeTripleFile_indexGet(names, index, w.c) + " " + moreInfo + "\n"); } writer.flush(); print("Have " + n2(names, "name") + ", " + toM(baos.size()) + "M buffered bytes"); PrintWriter writer2 = utf8printWriter(out); for (String s : names) writer2.println(quoteUnlessIdentifierOrInteger(s)); writer2.println(); writer2.flush(); baos.writeTo(out); return n; } catch (Exception __e) { throw rethrow(__e); } } public static int ai_writeTripleFile_indexGet(List names, Map index, String s) { Integer i = index.get(s); if (i == null) { i = l(names); names.add(s); index.put(s, i); } return i; } public static void ai_writeTripleFile_precollectNames(List names, Map index, Collection triples) { for (TripleWeb w : triples) { ai_writeTripleFile_indexGet(names, index, w.a); ai_writeTripleFile_indexGet(names, index, w.b); ai_writeTripleFile_indexGet(names, index, w.c); } sortInPlaceIC(names); index.clear(); for (int i = 0; i < l(names); i++) index.put(names.get(i), i); } public static String nTriple(long i) { return n_fancy2(i, "triple", "triples"); } public static String nTriple(Collection c) { return n_fancy2(c, "triple", "triples"); } public static List earlierPartsOfLogFile(File file) { String name = file.getName() + ".part"; try { Matches m = new Matches(); TreeMap map = new TreeMap(); for (File p : listFiles(file.getParent())) try { String n = p.getName(); if (startsWith(n, name, m)) map.put(parseFirstInt(m.rest()), p); } catch (Throwable __e) { _handleException(__e); } return valuesList(map); } catch (Throwable e) { _handleException(e); return ll(); } } public static File newFile(File base, String... names) { for (String name : names) base = new File(base, name); return base; } public static File newFile(String name) { return name == null ? null : new File(name); } public static String dropSuffix(String suffix, String s) { return s.endsWith(suffix) ? s.substring(0, l(s) - l(suffix)) : s; } public static void renameFile_assertTrue(File a, File b) { try { if (!a.exists()) throw fail("Source file not found: " + f2s(a)); if (b.exists()) throw fail("Target file exists: " + f2s(b)); mkdirsForFile(b); if (!a.renameTo(b)) throw fail("Can't rename " + f2s(a) + " to " + f2s(b)); } catch (Exception __e) { throw rethrow(__e); } } public static boolean optBoolParam(ThreadLocal tl) { return boolOptParam(tl); } public static int loadPage_defaultTimeout = 60000; public static ThreadLocal loadPage_charset = new ThreadLocal(); public static boolean loadPage_allowGzip = true, loadPage_debug; public static boolean loadPage_anonymous; public static int loadPage_verboseness = 100000; public static int loadPage_retries = 1; public static ThreadLocal loadPage_silent = new ThreadLocal(); public static volatile int loadPage_forcedTimeout; public static ThreadLocal loadPage_forcedTimeout_byThread = new ThreadLocal(); public static ThreadLocal>> loadPage_responseHeaders = new ThreadLocal(); public static ThreadLocal> loadPage_extraHeaders = new ThreadLocal(); public static String loadPageSilently(String url) { try { return loadPageSilently(new URL(loadPage_preprocess(url))); } catch (Exception __e) { throw rethrow(__e); } } public static String loadPageSilently(URL url) { try { if (url.getProtocol().equals("https")) disableCertificateValidation(); IOException e = null; for (int tries = 0; tries < loadPage_retries; tries++) try { URLConnection con = loadPage_openConnection(url); return loadPage(con, url); } catch (IOException _e) { e = _e; if (loadPageThroughProxy_enabled) { print("Trying proxy because of: " + e); try { return loadPageThroughProxy(str(url)); } catch (Throwable e2) { print(" " + exceptionToStringShort(e2)); } } else if (loadPage_debug) print(exceptionToStringShort(e)); if (tries < loadPage_retries - 1) sleepSeconds(1); } throw e; } catch (Exception __e) { throw rethrow(__e); } } public static String loadPage_preprocess(String url) { if (url.startsWith("tb/")) url = tb_mainServer() + "/" + url; if (url.indexOf("://") < 0) url = "http://" + url; return url; } public static String loadPage(String url) { try { url = loadPage_preprocess(url); if (!isTrue(loadPage_silent.get())) printWithTime("Loading: " + hideCredentials(url)); return loadPageSilently(new URL(url)); } catch (Exception __e) { throw rethrow(__e); } } public static String loadPage(URL url) { return loadPage(url.toExternalForm()); } public static String loadPage(URLConnection con, URL url) throws IOException { return loadPage(con, url, true); } public static String loadPage(URLConnection con, URL url, boolean addHeaders) throws IOException { Map extraHeaders = getAndClearThreadLocal(loadPage_extraHeaders); if (addHeaders) try { if (!loadPage_anonymous) setHeaders(con); if (loadPage_allowGzip) con.setRequestProperty("Accept-Encoding", "gzip"); con.setRequestProperty("X-No-Cookies", "1"); for (String key : keys(extraHeaders)) con.setRequestProperty(key, extraHeaders.get(key)); } catch (Throwable e) { } vm_generalSubMap("URLConnection per thread").put(currentThread(), con); loadPage_responseHeaders.set(con.getHeaderFields()); InputStream in = null; try { in = con.getInputStream(); if (loadPage_debug) print("Put stream in map: " + currentThread()); String contentType = con.getContentType(); if (contentType == null) { throw new IOException("Page could not be read: " + hideCredentials(url)); } String charset = loadPage_charset == null ? null : loadPage_charset.get(); if (charset == null) charset = loadPage_guessCharset(contentType); if ("gzip".equals(con.getContentEncoding())) { if (loadPage_debug) print("loadPage: Using gzip."); in = newGZIPInputStream(in); } Reader r = new InputStreamReader(in, charset); StringBuilder buf = new StringBuilder(); int n = 0; while (true) { int ch = r.read(); if (ch < 0) break; buf.append((char) ch); ++n; if ((n % loadPage_verboseness) == 0) print(" " + n + " chars read"); } return buf.toString(); } finally { if (loadPage_debug) print("loadPage done"); vm_generalSubMap("URLConnection per thread").remove(currentThread()); if (in != null) in.close(); } } public static String loadPage_guessCharset(String contentType) { Pattern p = Pattern.compile("text/[a-z]+;\\s*charset=([^\\s]+)\\s*"); Matcher m = p.matcher(contentType); String match = m.matches() ? m.group(1) : null; if (loadPage_debug) print("loadPage: contentType=" + contentType + ", match: " + match); return or(match, "UTF-8"); } public static URLConnection loadPage_openConnection(URL url) { URLConnection con = openConnection(url); int timeout = toInt(loadPage_forcedTimeout_byThread.get()); if (timeout == 0) timeout = loadPage_forcedTimeout; if (timeout != 0) setURLConnectionTimeouts(con, loadPage_forcedTimeout); else setURLConnectionDefaultTimeouts(con, loadPage_defaultTimeout); return con; } public static void ai_gc_clear() { int n = 0; for (WebRelation rel : indexedRelations("has GC distance")) { ping(); if (ai_isTripleWeb(rel.web)) { ai_invalidateWeb(rel.web); ++n; } } if (n != 0) print("Cleared " + n(n, "GC entry")); } public static void ai_startBuffering() { appendToFile_keepOpen(true); } public static TreeMap litcimap(Object... x) { return litCIMap(x); } public static LinkedList litLinkedList(A... a) { return new LinkedList(Arrays.asList(a)); } public static List web_forwardRelations(WebNode node) { if (node == null) return emptyList(); List l = new ArrayList(); for (WebRelation r : web_relationObjects(node.web)) if (r.a == node) l.add(r); return l; } public static Map ai_gc_distanceFunction_backwardValues = litcimap("is", 1, "is \"a\"", 0.1, "unquotes to", 0.1, "probably is", 1); public static double ai_gc_distanceFunction(String rel, boolean forward) { if (forward) return 1; if (!forward) { rel = dropSuffix(":", rel); Number n = ai_gc_distanceFunction_backwardValues.get(rel); if (n != null) return toDouble(n); } return nan(); } public static boolean isNaN(double d) { return Double.isNaN(d); } public static List web_backwardRelations(WebNode node) { if (node == null) return emptyList(); List l = new ArrayList(); for (WebRelation r : web_relationObjects(node.web)) if (r.b == node) l.add(r); return l; } public static boolean ai_inTestMode() { return isTrue(postSoftwareMadeWeb_disabled.get()); } public static List keysSortedByValueDesc(final Map map) { return keysSortedByValuesDesc(map); } public static void ai_stopBuffering() { appendToFile_keepOpen(false); } public static DependentCache> ai_invalidatedWebs_cache = new DependentCache("ai_invalidatedWebs_calc", "cachedNodeIndex"); public static Set ai_invalidatedWebs() { return keys(ai_invalidatedWebs_cache.get()); } public static MultiMap ai_invalidatedWebs_calc() { ai_onNewOrRemovedWeb("ai_invalidatedWebs_onNewWeb", "ai_invalidatedWebs_onRemovedWeb"); ai_invalidatedWebs_cache.set(new MultiMap()); Matches m = new Matches(); for (WebNode n : indexedNodes_withInvalids("invalid")) for (WebRelation r : web_backwardRelations(n)) if (web_hasLabelIC(r, "is") && web_match("Web *", r.a, m) && isGlobalID(m.unq(0))) ai_invalidatedWebs_cache.get().put(asGlobalID(m.unq(0)), r.web.globalIDObj()); print("ai_invalidatedWebs calc done with " + n2(ai_invalidatedWebs_cache.get().keysSize(), "invalidated web", "invalidated webs")); return ai_invalidatedWebs_cache.get(); } public static void ai_invalidatedWebs_onNewWeb(Web web) { if (web.unverified) return; Lock __1450 = ai_invalidatedWebs_cache.lock; lock(__1450); try { Matches m = new Matches(); for (WebNode n : web_search_dollarX(webFromTriple("$X", "is", "invalid"), web)) if (web_match("Web *", n, m) && isGlobalID(m.unq(0))) ai_invalidatedWebs_cache.get().put(asGlobalID(m.unq(0)), web.globalIDObj()); } finally { unlock(__1450); } } public static void ai_invalidatedWebs_onRemovedWeb(Web web) { if (web.unverified) return; Lock __1451 = ai_invalidatedWebs_cache.lock; lock(__1451); try { Matches m = new Matches(); for (WebNode n : web_search_dollarX(webFromTriple("$X", "is", "invalid"), web)) if (web_match("Web *", n, m) && isGlobalID(m.unq(0))) ai_invalidatedWebs_cache.get().remove(asGlobalID(m.unq(0)), web.globalIDObj()); } finally { unlock(__1451); } } public static void ai_invalidatedWebs_cache_put(GlobalID a, GlobalID b) { ai_invalidatedWebs_cache.get().put(a, b); } public static void addIfNotNull(Collection l, A a) { if (a != null) l.add(a); } public static void addIfNotNull(MultiSet ms, A a) { if (a != null) ms.add(a); } public static AI_SubSpace ai_loadSubSpace(File f) { AI_SubSpace ss = new AI_SubSpace(); List webs = (List) loadGZStructure(f); for (Web web : webs) ss.addWeb(web); ss.changed = false; return ss; } public static File ai_subSpaceFile(String name) { return getProgramFile("subspaces/" + urlencode(name) + ".gz"); } public static void ai_putSubSpace(String name, AI_SubSpace ss) { ai_getSubSpace_map.put(name, ss); } public static String smartBot_findMyExternalURL() { String ip = myFirewallIP(); for (int port = 4678; port <= 4681; port++) { String url = "http://" + ip + ":" + port; print("Trying " + url); try { if (swic(loadPageWithTimeout(url + "/livecheck", 10000), "Smart Bot")) return url; } catch (Throwable __e) { print(exceptionToStringShort(__e)); } } return null; } public static void setSmartBotURL(String url) { saveProgramTextFile("#1010745", "location", addPrefixIfNotEmpty2("http://", url)); } public static void tellSmartBotURLToChat() { scpUploadFile(programFile("#1010745", "location"), "root", "botcompany.de", "/root/JavaX-Data/#1010745/location"); scpUploadFile(programFile("#1010745", "location"), "root", "tinybrain.de", "/root/JavaX-Data/#1010745/location"); } public static WebNode ai_index_search_dollarX_first(String a, String b, String c) { String query = isDollarVar(a) ? c : a; return webs_tripleSearch_dollarX_first(a, b, c, ai_search_indexedWebs(query)); } public static String htag(String tag) { return htag(tag, ""); } public static String htag(String tag, Object contents, Object... params) { String openingTag = hopeningTag(tag, params); String s = str(contents); if (empty(s) && neqic(tag, "script")) return dropLast(openingTag) + "/>"; return openingTag + s + ""; } public static int randomID_defaultLength = 12; public static String randomID(int length) { return makeRandomID(length); } public static String randomID() { return randomID(randomID_defaultLength); } public static String symbol(String s) { return s; } public static String symbol(CharSequence s) { if (s == null) return null; return str(s); } public static String symbol(Object o) { return symbol((CharSequence) o); } public static List ai_partsOf(String whole) { Set cluster = ai_makeCluster(whole); cluster = ai_cluster_forward(cluster, "consists of"); cluster.addAll(ai_cluster_forward(cluster, "parses to")); for (String s : cluster) { List l = ai_parseAndList(s); if (l(l) >= 2) return l; } return null; } public static String ai_spec_allWords(String _s) { String s = unquote(_s); List l = javaTokC(s); if (l(l) > 1) for (String t : l) if (l(t) > 1) ai_postTriple(_s, "possibly related to:", t); return _s; } public static String drop_a_an_the(String s) { return dropArticle(s); } public static boolean isDollarVar(String s) { return startsWith(s, '$') && isJavaIdentifierAfter(s, 1); } public static List indexedVerifiedWebs(String s) { return web_verifiedWebsOnly(indexedWebs(s)); } public static Web ai_getWebFromTerm(String s) { if (possibleGlobalID(s)) { Web web = ai_getWeb(s); if (web != null) return web; } Matches m = new Matches(); if (swic(s, "Web ", m)) return ai_getWeb(m.rest()); return null; } public static T3 webToTriple(Web web) { return ai_webToTriple(web); } public static void ai_removeWeb(Web web) { onTransientWebRemoved(web); } public static Web webNodeToWeb(WebNode n) { return n == null ? null : n.web; } public static boolean isWordsAndSpaces(String s) { for (int i = 0; i < l(s); i++) { char c = s.charAt(i); if (c != ' ' && !isLetterOrDigitOrApostrophe(c)) return false; } return true; } public static A firstNotNull(Iterable c) { Iterator it = c.iterator(); while (it.hasNext()) { A a = it.next(); if (a != null) return a; } return null; } public static String ai_lookup(String s) { { String _a_1498 = ai_text(s, "is", "$X"); if (!empty(_a_1498)) return _a_1498; } return ai_text("$X", "is", s); } public static String dropArticle(String text) { String s = dropPrefixICTrim("a ", text); if (neq(s, text)) return s; s = dropPrefixICTrim("an ", text); if (neq(s, text)) return s; return dropPrefixICTrim("the ", text); } public static String textOut_and(List l) { return textOut_and(l, "nothing"); } public static String textOut_and(List l, String emptyText) { if (empty(l)) return emptyText; List separators = rep(", ", l(l) - 2); separators.add(" and "); return join_flex(separators, l); } public static int tripleIndexOf(T3 t, A a) { if (t == null) return -1; if (eq(t.a, a)) return 0; if (eq(t.b, a)) return 1; if (eq(t.c, a)) return 2; return -1; } public static int tripleIndexOf(A a, A b, A c, A x) { if (eq(a, x)) return 0; if (eq(b, x)) return 1; if (eq(c, x)) return 2; return -1; } public static String symbol_dollarX_value; public static String symbol_dollarX() { if (symbol_dollarX_value == null) symbol_dollarX_value = symbol("$X"); return symbol_dollarX_value; } public static AbstractThoughtSpace thoughtSpace() { return currentThoughtSpace(); } public static A thoughtSpace(A ts) { currentThoughtSpace_value.set(ts); return ts; } public static List allToSymbol(Collection l) { return allToString(l); } public static List ai_tripleRefsToTexts_verified(String a, String b, String c, List> l) { if (empty(l)) return sharedEmptyList(); List out = new ArrayList(); for (TripleRef ref : l) if (ref != null) { TripleWeb t = (TripleWeb) (ref.triple); { if (!(t.verified())) continue; } Map match = simpleMatchTriple_dollarVars(a, b, c, t); if (match != null) out.add(match.get(symbol_dollarX())); } return out; } public static List> ai_thoughtSpace_query(String a, String b, String c, AbstractThoughtSpace ts) { List> l = null; if (!isDollarVar(a)) { if (!isDollarVar(b)) return ai_triplesToCRefs(ts.getOneTwo(a, b)); l = shortestListUnlessNull2(l, ts.get(a, 0)); if (l(l) <= 1) return l; } if (!isDollarVar(b)) { l = shortestListUnlessNull2(l, ts.get(b, 1)); if (l(l) <= 1) return l; } if (!isDollarVar(c)) l = shortestListUnlessNull2(l, ts.get(c, 2)); return l; } public static List sharedEmptyList() { return Collections.emptyList(); } public static char lastChar(String s) { return empty(s) ? '\0' : s.charAt(l(s) - 1); } public static String[] dropLast(String[] a, int n) { n = Math.min(n, a.length); String[] b = new String[a.length - n]; System.arraycopy(a, 0, b, 0, b.length); return b; } public static List dropLast(List l) { return subList(l, 0, l(l) - 1); } public static List dropLast(int n, List l) { return subList(l, 0, l(l) - n); } public static List dropLast(Iterable l) { return dropLast(asList(l)); } public static String dropLast(String s) { return substring(s, 0, l(s) - 1); } public static String dropLast(String s, int n) { return substring(s, 0, l(s) - n); } public static String dropLast(int n, String s) { return dropLast(s, n); } public static boolean isJavaIdentifier(String s) { if (empty(s) || !Character.isJavaIdentifierStart(s.charAt(0))) return false; for (int i = 1; i < s.length(); i++) if (!Character.isJavaIdentifierPart(s.charAt(i))) return false; return true; } public static boolean ai_isYesNoQuestion_1(String s) { return ai_yesNoQuestionWord_1(firstWord(s)); } public static Set ai_questionWord_1_list = litciset("what", "who", "why", "when", "where", "how"); public static boolean ai_questionWord_1(String s) { return containsNotNull(ai_questionWord_1_list, s); } public static String firstWord(String s) { return firstJavaToken(s); } public static String charToString(char c) { return String.valueOf(c); } public static String charToString(int c) { return String.valueOf((char) c); } public static BufferedImage loadPNG(File file) { return loadBufferedImage(file); } public static String googleImageSearch(String q) { return (String) call(hotwireOnce("#1010230"), "googleImageSearch", q); } public static List googleImageSearch_multi(String q) { return (List) call(hotwireOnce("#1010230"), "googleImageSearch_multi", q); } public static BufferedImage googleImageSearchFirst(String q) { return (BufferedImage) call(hotwireOnce("#1010230"), "googleImageSearchFirst", q); } public static void savePNG(BufferedImage img, File file) { try { File tempFile = new File(file.getPath() + "_temp"); CriticalAction ca = beginCriticalAction("Save " + f2s(file)); try { ImageIO.write(img, "png", mkdirsFor(tempFile)); file.delete(); tempFile.renameTo(file); } finally { ca.done(); } } catch (Exception __e) { throw rethrow(__e); } } public static void savePNG(File file, BufferedImage img) { savePNG(img, file); } public static void savePNG(File file, RGBImage img) { savePNG(file, img.getBufferedImage()); } public static File prepareProgramFile(String name) { return mkdirsForFile(getProgramFile(name)); } public static File prepareProgramFile(String progID, String name) { return mkdirsForFile(getProgramFile(progID, name)); } public static byte[] toJPEG(BufferedImage img) { try { ByteArrayOutputStream stream = new ByteArrayOutputStream(); ImageIO.write(img, "jpeg", stream); return stream.toByteArray(); } catch (Exception __e) { throw rethrow(__e); } } public static byte[] toJPEG(File f) { return toJPEG(loadImage2(f)); } public static String md5OfRGBImage(RGBImage img) { try { MessageDigest m = MessageDigest.getInstance("MD5"); m.update(intToBytes(img.getWidth())); int[] pixels = img.getPixels(); for (int i = 0; i < l(pixels); i++) m.update(intToBytes(pixels[i])); return bytesToHex(m.digest()); } catch (Exception __e) { throw rethrow(__e); } } public static BufferedImage imageFromByteArray(byte[] data) { try { return ImageIO.read(new ByteArrayInputStream(data)); } catch (Exception __e) { throw rethrow(__e); } } public static long imageServerCheckMD5(String md5) { assertTrue(isMD5(md5)); String s = loadPage("http://ai1.space/images/raw/checkmd5/" + md5); return parseLongOpt(s); } public static String uploadToImageServer_new_jpg(BufferedImage img, String name) { return uploadToImageServer_new_jpg(toJPEG(img), name); } public static String uploadToImageServer_new_jpg(byte[] imgData, String name) { String page = postPage("http://ai1.space/images/raw/upload", "data", bytesToHex(imgData), "name", name); print(page); Matcher m = Pattern.compile("/raw/([0-9]+)").matcher(page); if (!m.find()) throw fail("Image server said: " + page); return "http://ai1.space/images/raw/" + m.group(1); } public static DependentCache> allWebsByID_cache = new DependentCache("allWebsByID_calc", "cachedNodeIndex"); public static SmallestListMultiMap allWebsByID() { return allWebsByID_cache.get(); } public static SmallestListMultiMap allWebsByID_calc() { setAdd(postSoftwareMadeWeb_onNewWeb, "allWebsByID_onNewWeb"); onTransientWebAdded_do("allWebsByID_onNewWeb"); onTransientWebRemoved_do("allWebsByID_onRemovedWeb"); return smallestListMultiMapIndexByMethod(allWebsFromCachedNodeIndex_uncached(), "globalIDObj"); } public static void allWebsByID_onNewWeb(Web web, Object[] params) { allWebsByID_onNewWeb(web); } public static void allWebsByID_onNewWeb(Web web) { Lock __1550 = allWebsByID_cache.lock; lock(__1550); try { if (allWebsByID_cache.get() != null) allWebsByID_cache.get().put(web.globalIDObj(), web); } finally { unlock(__1550); } } public static void allWebsByID_onRemovedWeb(Web web) { Lock __1552 = allWebsByID_cache.lock; lock(__1552); try { if (allWebsByID_cache.get() != null) allWebsByID_cache.get().remove(web.globalIDObj()); } finally { unlock(__1552); } } public static long ai_numberOfVirtualWebs() { long n = 0; try { for (VirtualNodeIndex i : virtualNodeIndices()) try { n += i.numWebs(); } catch (Throwable __e) { _handleException(__e); } } catch (Throwable __e) { _handleException(__e); } return n; } public static IterableIterator toLines(File f) { return linesFromFile(f); } public static List toLines(String s) { List lines = new ArrayList(); if (s == null) return lines; int start = 0; while (true) { int i = toLines_nextLineBreak(s, start); if (i < 0) { if (s.length() > start) lines.add(s.substring(start)); break; } lines.add(s.substring(start, i)); if (s.charAt(i) == '\r' && i + 1 < s.length() && s.charAt(i + 1) == '\n') i += 2; else ++i; start = i; } return lines; } public static int toLines_nextLineBreak(String s, int start) { for (int i = start; i < s.length(); i++) { char c = s.charAt(i); if (c == '\r' || c == '\n') return i; } return -1; } public static String getMemoryInfoPlusSystemMemory() { return getMemoryInfo() + ". " + toM(freeSystemMemory()) + " MB free"; } public static String getVMCommand() { if (isWindows()) throw fail("Windows!"); String bla = backtick("ps -u -p " + getProcessID()); List lines = toLines(bla); String header = lines.get(0); String body = lines.get(1); int idx = header.indexOf("COMMAND"); return body.substring(idx).trim(); } public static String slackSnippet(Object contents) { if (contents instanceof List) contents = join("\n", (List) contents); String s = str(contents); return "```\n" + s + "```"; } public static String structureLines(Object data) { if (data instanceof Object[]) data = asList((Object[]) data); if (data instanceof Collection) { List l = new ArrayList(); for (Object x : (Collection) data) l.add(structureForUser(x)); return fromLines(l); } if (data instanceof MultiSet) data = multiSetToMap(((MultiSet) data)); if (data instanceof Map) { List l = new ArrayList(); for (Object key : ((Map) data).keySet()) { Object value = ((Map) data).get(key); l.add(structureForUser(key) + " = " + structureForUser(value)); } return fromLines(l); } return structureForUser(data); } public static A eitherAOpt(Either e) { return e != null && e.isA() ? e.a() : null; } public static int ai_solveQuestionInSubSpace_limit = 10000; public static boolean ai_solveQuestionInSubSpace_saveIfSuccessful = true; public static String ai_solveQuestionInSubSpace_noTimeOut(List> seeds, List> lookingFor) { return pairB(ai_solveQuestionInSubSpace_noTimeOut2(seeds, lookingFor)); } public static Pair ai_solveQuestionInSubSpace_noTimeOut2(List> seeds, List> lookingFor) { return ai_solveQuestionInSubSpace_noTimeOut2(seeds, lookingFor, "ai_solveQuestionInSubSpace_defaultStep"); } public static Pair ai_solveQuestionInSubSpace_noTimeOut2(List> seeds, List> lookingFor, Object stepFunction) { AI_SubSpace subSpace = new AI_SubSpace(ai_solveQuestionInSubSpace_limit); ai_setSubSpace(subSpace); try { for (T3 seed : seeds) subSpace.postTriple(seed); for (T3 t : lookingFor) subSpace.postTriple(t3("_goal", "is", ai_renderTriple(t))); int step = 0; while (!subSpace.limitReached()) { ++step; int n = l(subSpace.webs); print("Solving question: " + allToString(lookingFor) + " (step " + step + ", " + n + "/" + subSpace.limit + ")"); try { callF(stepFunction); } catch (Throwable __e) { print(exceptionToStringShort(__e)); } List newWebs = cloneSubList(subSpace.webs, n); ai_spec_possiblyToActually(newWebs); for (T3 t : lookingFor) { String a = ai_text(t); if (a != null) { if (ai_solveQuestionInSubSpace_saveIfSuccessful) saveGZStructure(countTillNewFile(programFile("logged-subspaces/subspace"), ".gz"), subSpace); return pair(subSpace, a); } } if (l(subSpace.webs) == n) { print("Got " + nWeb(n) + ". No new webs made"); break; } } if (subSpace.limitReached()) print("Subspace limit reached"); return pair(subSpace, null); } finally { ai_setSubSpace(null); } } public static Pair ai_solveQuestionInSubSpace_noTimeOut2(String q) { Pair>, List>> p = ai_solveQuestion_seedAndLookingFor(q); return ai_solveQuestionInSubSpace_noTimeOut2(p.a, p.b); } public static void ai_solveQuestionInSubSpace_defaultStep() { AI_SubSpace subSpace = ai_currentSubSpace(); for (String term : concatLists(keys(subSpace.index), allToString(keys(subSpace.websByID)))) ai_speculate(term); ai_spec_answerPattern(); ai_spec_daysOld(); } public static Pair>, List>> ai_solveQuestion_seedAndLookingFor(String q) { return pair(ll(triple(q, "should be", "answered")), ll(triple("$X", "answers", q), triple("Answer", "is", "$X"))); } public static List urldecodeFileNames(List l) { List out = new ArrayList(); for (File f : l) out.add(urldecode(f.getName())); return out; } public static List filesEndingWith(File dir, String suffix) { return listFilesWithSuffix(dir, suffix); } public static long vmStartTime_sys() { try { return toLong(get(javax(), "vmStarted_sysTime")); } catch (Throwable e) { _handleException(e); upgradeJavaXAndRestart(); return 0; } } public static Map> ai_parseTriple_cache = synchronizedMRUCache(1000); public static T3 ai_parseTriple(String s) { T3 t = ai_parseTriple_cache.get(s); if (t != null) return t; List l = map("tok_unCurlyBracket", splitAtSimpleArrow(tok_combineCurlyBrackets_keep(javaTok(s)))); if (l(l) == 2) l.add("."); t = listToTriple(l); ai_parseTriple_cache.put(s, t); return t; } public static List getPlural_specials = ll("sheep", "fish"); public static String getPlural(String s) { if (containsIgnoreCase(getPlural_specials, s)) return s; if (ewic(s, "y")) return dropSuffixIgnoreCase("y", s) + "ies"; if (ewic(s, "ss")) return s + "es"; if (ewic(s, "s")) return s; return s + "s"; } public static List> ai_nlClassify1(String s) { return (List) tok_classifyEnglishWordsWithOriginal(nlTok5(s)); } public static String ai_varyClassification(List l) { if (l(l) <= 1) return null; String original = unquote(last(l)); for (String category : dropLast(l)) { { String _a_1585 = random(listWithoutIC(ai_texts("$X", "is a subclass of", category), original)); if (!empty(_a_1585)) return _a_1585; } { String _a_1586 = random(listWithoutIC(ai_texts("$X", "is a", category), original)); if (!empty(_a_1586)) return _a_1586; } } return null; } public static String loadPageWithUserAgent(String url, String userAgent) { try { URL tURL = new URL(url); URLConnection conn = tURL.openConnection(); conn.setRequestProperty("User-Agent", userAgent); loadPage_anonymous = true; print("Loading as " + userAgent + ": " + hideCredentials(url)); return loadPage(conn, tURL); } catch (Exception __e) { throw rethrow(__e); } } public static List> findContainerTagDeep(List tok, String tag) { List> l = new ArrayList(); for (int i = 1; i < l(tok); i += 2) if (isOpeningTag(tok.get(i), tag)) { int j, level = 1; for (j = i + 2; j < tok.size(); j += 2) if (isOpeningTag(tok.get(j), tag)) ++level; else if (isTag(tok.get(j), "/" + tag)) { --level; if (level == 0) { l.add(tok.subList(i - 1, j + 2)); break; } } } return l; } public static List> findContainerTagDeep(String html, String tag) { return findContainerTagDeep(htmlTok(html), tag); } public static List> findContainerTag(List tok, String tag) { List> l = new ArrayList(); for (int i = 1; i < l(tok); i += 2) if (isOpeningTag(tok.get(i), tag)) { int j, level = 1; for (j = i + 2; j < tok.size(); j += 2) if (isOpeningTag(tok.get(j), tag)) ++level; else if (isTag(tok.get(j), "/" + tag)) { --level; if (level == 0) { l.add(tok.subList(i - 1, j + 2)); break; } } i = j; } return l; } public static List> findContainerTag(String html, String tag) { return findContainerTag(htmlTok(html), tag); } public static String tagGet(String tag, String param) { return mapGet(htmlgetparams(tag), param); } public static boolean isAbsoluteURL(String s) { return contains(s, "://"); } public static String htmldecode(final String input) { if (input == null) return null; final int MIN_ESCAPE = 2; final int MAX_ESCAPE = 6; StringWriter writer = null; int len = input.length(); int i = 1; int st = 0; while (true) { while (i < len && input.charAt(i - 1) != '&') i++; if (i >= len) break; int j = i; while (j < len && j < i + MAX_ESCAPE + 1 && input.charAt(j) != ';') j++; if (j == len || j < i + MIN_ESCAPE || j == i + MAX_ESCAPE + 1) { i++; continue; } if (input.charAt(i) == '#') { int k = i + 1; int radix = 10; final char firstChar = input.charAt(k); if (firstChar == 'x' || firstChar == 'X') { k++; radix = 16; } try { int entityValue = Integer.parseInt(input.substring(k, j), radix); if (writer == null) writer = new StringWriter(input.length()); writer.append(input.substring(st, i - 1)); if (entityValue > 0xFFFF) { final char[] chrs = Character.toChars(entityValue); writer.write(chrs[0]); writer.write(chrs[1]); } else { writer.write(entityValue); } } catch (NumberFormatException ex) { i++; continue; } } else { CharSequence value = htmldecode_lookupMap.get(input.substring(i, j)); if (value == null) { i++; continue; } if (writer == null) writer = new StringWriter(input.length()); writer.append(input.substring(st, i - 1)); writer.append(value); } st = j + 1; i = st; } if (writer != null) { writer.append(input.substring(st, len)); return writer.toString(); } return input; } public static final String[][] htmldecode_ESCAPES = { { "\"", "quot" }, { "&", "amp" }, { "<", "lt" }, { ">", "gt" }, { " ", "nbsp" }, { "¡", "iexcl" }, { "¢", "cent" }, { "£", "pound" }, { "¤", "curren" }, { "¥", "yen" }, { "¦", "brvbar" }, { "§", "sect" }, { "¨", "uml" }, { "©", "copy" }, { "ª", "ordf" }, { "«", "laquo" }, { "¬", "not" }, { "­", "shy" }, { "®", "reg" }, { "¯", "macr" }, { "°", "deg" }, { "±", "plusmn" }, { "²", "sup2" }, { "³", "sup3" }, { "´", "acute" }, { "µ", "micro" }, { "¶", "para" }, { "·", "middot" }, { "¸", "cedil" }, { "¹", "sup1" }, { "º", "ordm" }, { "»", "raquo" }, { "¼", "frac14" }, { "½", "frac12" }, { "¾", "frac34" }, { "¿", "iquest" }, { "À", "Agrave" }, { "Á", "Aacute" }, { "Â", "Acirc" }, { "Ã", "Atilde" }, { "Ä", "Auml" }, { "Å", "Aring" }, { "Æ", "AElig" }, { "Ç", "Ccedil" }, { "È", "Egrave" }, { "É", "Eacute" }, { "Ê", "Ecirc" }, { "Ë", "Euml" }, { "Ì", "Igrave" }, { "Í", "Iacute" }, { "Î", "Icirc" }, { "Ï", "Iuml" }, { "Ð", "ETH" }, { "Ñ", "Ntilde" }, { "Ò", "Ograve" }, { "Ó", "Oacute" }, { "Ô", "Ocirc" }, { "Õ", "Otilde" }, { "Ö", "Ouml" }, { "×", "times" }, { "Ø", "Oslash" }, { "Ù", "Ugrave" }, { "Ú", "Uacute" }, { "Û", "Ucirc" }, { "Ü", "Uuml" }, { "Ý", "Yacute" }, { "Þ", "THORN" }, { "ß", "szlig" }, { "à", "agrave" }, { "á", "aacute" }, { "â", "acirc" }, { "ã", "atilde" }, { "ä", "auml" }, { "å", "aring" }, { "æ", "aelig" }, { "ç", "ccedil" }, { "è", "egrave" }, { "é", "eacute" }, { "ê", "ecirc" }, { "ë", "euml" }, { "ì", "igrave" }, { "í", "iacute" }, { "î", "icirc" }, { "ï", "iuml" }, { "ð", "eth" }, { "ñ", "ntilde" }, { "ò", "ograve" }, { "ó", "oacute" }, { "ô", "ocirc" }, { "õ", "otilde" }, { "ö", "ouml" }, { "÷", "divide" }, { "ø", "oslash" }, { "ù", "ugrave" }, { "ú", "uacute" }, { "û", "ucirc" }, { "ü", "uuml" }, { "ý", "yacute" }, { "þ", "thorn" }, { "ÿ", "yuml" }, { "–", "ndash" }, { "‘", "lsquo" }, { "’", "rsquo" }, { "”", "rdquo" }, { "“", "ldquo" }, { "—", "mdash" }, { "'", "apos" } }; public static final HashMap htmldecode_lookupMap; static { htmldecode_lookupMap = new HashMap(); for (final CharSequence[] seq : htmldecode_ESCAPES) htmldecode_lookupMap.put(seq[1].toString(), seq[0]); } public static String dropTags(String html) { return dropAllTags(html); } public static List dropTags(List tok) { return dropAllTags(tok); } public static List contentsOfContainerTag(List tok) { return tok == null ? null : subList(tok, 2, l(tok) - 2); } public static List contentsOfContainerTag(List tok, String tagName) { return contentsOfContainerTag(first(findContainerTag(tok, tagName))); } public static String fromLines(Collection lines) { StringBuilder buf = new StringBuilder(); if (lines != null) for (Object line : lines) buf.append(str(line)).append('\n'); return buf.toString(); } public static String fromLines(String... lines) { return fromLines(asList(lines)); } public static List uniquifyList(Collection l) { if (l(l) < 2) return asList(l); HashSet set = new HashSet(); List out = new ArrayList(); for (A a : unnull(l)) if (set.add(a)) out.add(a); return out; } public static List lookupPossiblyIgnoringCase(MultiMap map, String key) { return lookupPossiblyIgnoreCase(map, key); } public static A lookupPossiblyIgnoringCase(Map map, String key) { return lookupPossiblyIgnoreCase(map, key); } public static Map>> parseGoogleLog() { Map>> out = new HashMap(); for (Map map : scanMapLog(googleLog())) out.put((String) map.get("query"), (List) map.get("results")); return out; } public static List htmlTok(String s) { return htmlcoarsetok(s); } public static int magicIndexOfSubList(List list, List sublist) { Integer o1 = (Integer) (getOpt(list, "offset")); int o2 = (Integer) get(sublist, "offset"); return o2 - (o1 != null ? o1 : 0); } public static List> findContainerTagWithParams(List tok, String tag, Object... params) { List> l = new ArrayList(); for (List t : findContainerTag(tok, tag)) if (htmlCheckParams(second(t), params)) l.add(t); return l; } public static List> findContainerTagWithParams(String html, String tag, Object... params) { return findContainerTagWithParams(htmlTok(html), tag, params); } public static void logStructure(File logFile, Object o) { logQuoted(logFile, structure(o)); } public static void logStructure(String fileName, Object o) { logStructure(getProgramFile(fileName), o); } public static void logStructure(String progID, String fileName, Object o) { logStructure(getProgramFile(progID, fileName), o); } public static File googleLog() { return javaxDataDir("google-log.txt"); } public static String localDateWithSeconds(long time) { SimpleDateFormat format = simpleDateFormat_local("yyyy/MM/dd HH:mm:ss"); return format.format(time); } public static String localDateWithSeconds() { return localDateWithSeconds(now()); } public static boolean jsonDecode_useOrderedMaps = true; public static Object jsonDecode(final String text) { final List tok = jsonTok(text); class Y { public int i = 1; public Object parse() { String t = tok.get(i); if (t.startsWith("\"")) { String s = unquote(tok.get(i)); i += 2; return s; } if (t.equals("{")) return parseMap(); if (t.equals("[")) return this.parseList(); if (t.equals("null")) { i += 2; return null; } if (t.equals("false")) { i += 2; return false; } if (t.equals("true")) { i += 2; return true; } boolean minus = false; if (t.equals("-")) { minus = true; i += 2; t = get(tok, i); } if (isInteger(t)) { i += 2; if (eq(get(tok, i), ".")) { String x = t + "." + get(tok, i + 2); i += 4; double d = parseDouble(x); if (minus) d = -d; return d; } else { long l = parseLong(t); if (minus) l = -l; return l != (int) l ? new Long(l) : new Integer((int) l); } } throw new RuntimeException("Unknown token " + (i + 1) + ": " + t + ": " + text); } public Object parseList() { consume("["); List list = new ArrayList(); while (!tok.get(i).equals("]")) { list.add(parse()); if (tok.get(i).equals(",")) i += 2; } consume("]"); return list; } public Object parseMap() { consume("{"); Map map = jsonDecode_useOrderedMaps ? new LinkedHashMap() : new TreeMap(); while (!tok.get(i).equals("}")) { String key = unquote(tok.get(i)); i += 2; consume(":"); Object value = parse(); map.put(key, value); if (tok.get(i).equals(",")) i += 2; } consume("}"); return map; } public void consume(String s) { if (!tok.get(i).equals(s)) { String prevToken = i - 2 >= 0 ? tok.get(i - 2) : ""; String nextTokens = join(tok.subList(i, Math.min(i + 4, tok.size()))); throw fail(quote(s) + " expected: " + prevToken + " " + nextTokens + " (" + i + "/" + tok.size() + ")"); } i += 2; } } return new Y().parse(); } public static MultiSet asMultiSet(Iterable l) { return new MultiSet(l); } public static List splitAtSpace(String s) { return asList(s.split("\\s+")); } public static String dropPrefixTrim(String prefix, String s) { return trim(dropPrefix(prefix, s)); } public static String tok_html_noTags(List tok) { return trimJoin(dropTagsAndHTMLComments(tok)); } public static List> findContainerTagWithParamsDeep(List tok, String tag, Object... params) { List> l = new ArrayList(); for (List t : findContainerTagDeep(tok, tag)) if (htmlCheckParams(second(t), params)) l.add(t); return l; } public static List> findContainerTagWithParamsDeep(String html, String tag, Object... params) { return findContainerTagWithParamsDeep(htmlTok(html), tag, params); } public static String yesno(boolean b) { return yesno((Boolean) b); } public static String yesno(Boolean b) { return eq(b, true) ? "Yes. " : eq(b, false) ? "No. " : "Unknown. "; } public static ThreadLocal ai_cacheChecker = new ThreadLocal(); public static AI_CacheChecker ai_cacheChecker() { return ai_cacheChecker.get(); } public static List ai_fewestIndexedWebs(String a, String b, String c) { return ai_withoutInvalidWebs(nodesToWebs(shortestList3(indexedNodes_withInvalids(a), indexedNodes_withInvalids(b), indexedNodes_withInvalids(c)))); } public static boolean webs_search_noVar_bool(Web searchWeb, Collection webs) { return webs_search_noVar(searchWeb, webs) != null; } public static String sfuLL(Object... x) { return sfu(ll(x)); } public static int indexOf(List l, A a, int startIndex) { if (l == null) return -1; for (int i = startIndex; i < l(l); i++) if (eq(l.get(i), a)) return i; return -1; } public static int indexOf(List l, int startIndex, A a) { return indexOf(l, a, startIndex); } public static int indexOf(List l, A a) { if (l == null) return -1; return l.indexOf(a); } public static int indexOf(String a, String b) { return a == null || b == null ? -1 : a.indexOf(b); } public static int indexOf(String a, String b, int i) { return a == null || b == null ? -1 : a.indexOf(b, i); } public static int indexOf(String a, char b) { return a == null ? -1 : a.indexOf(b); } public static int indexOf(String a, int i, char b) { return indexOf(a, b, i); } public static int indexOf(String a, char b, int i) { return a == null ? -1 : a.indexOf(b, i); } public static int indexOf(String a, int i, String b) { return a == null || b == null ? -1 : a.indexOf(b, i); } public static int indexOf(A[] x, A a) { if (x == null) return -1; for (int i = 0; i < l(x); i++) if (eq(x[i], a)) return i; return -1; } public static List switcheroo_themap = ll("mine", "yours", "my", "your", "you are", "i am", "are you", "am i", "you're", "i'm", "you've", "i've", "me", "you", "i", "you"); public static String switcheroo(String s) { List tok = nlTok5(s); for (int i = 1; i < l(tok); i += 2) { for (int j = 0; j < l(switcheroo_themap); j += 2) { String x = switcheroo_themap.get(j), y = switcheroo_themap.get(j + 1); List tokx = nlTok5(x), toky = nlTok5(y); switcheroo_matchReplace(tok, i, tokx, toky); switcheroo_matchReplace(tok, i, toky, tokx); } } return join(map("switcheroo_dropAsterisk", tok)); } public static String switcheroo_dropAsterisk(String t) { return startsWith(t, "*") && l(t) > 1 ? substring(t, 1) : t; } public static void switcheroo_matchReplace(List tok, int i, List tokx, List toky) { if (switcheroo_tokRegionMatch(tok, i, tokx)) { for (int j = 0; j * 2 + 1 < l(tokx); j++) tok.set(i + j * 2, "*" + toky.get(1 + j * 2)); } } public static boolean switcheroo_tokRegionMatch(List tok, int i, List tokx) { for (int j = 0; j * 2 + 1 < l(tokx); j++) if (!eqic(get(tok, i + j * 2), get(tokx, 1 + j * 2))) return false; return true; } public static T3 ai_tripleWithoutQuestionMark(T3 t) { return t3(t.a, dropSuffix("?", dropSuffix(" (?)", t.b)), t.c); } public static boolean has_verbose(String a, String b, String c) { boolean has = ai_cache_hasTriple(a, b, c); print((has ? "Has: " : "Doesn't have: ") + ai_renderTriple(a, b, c)); return has; } public static boolean has_verbose(T3 t) { return has_verbose(t.a, t.b, t.c); } public static T3 ai_negateTriple(T3 t) { return t3(t.a, ai_negateVerbPhrase(t.b), t.c); } public static String ai_answerIsQuestion(T3 t) { if (!eqicOneOf(t.b, "am?", "are?", "is?")) return null; List is_l = ai_superclasses(t.a, dropQuestionMark(t.b)); printStruct("is_l", is_l); List askedFor_l = ai_superclasses(t.c); printStruct("askedFor_l", askedFor_l); for (String askedFor : askedFor_l) for (String is : is_l) if (has(is, "is not", askedFor)) return "No - " + is + " is not " + askedFor; return null; } public static String ai_answerWhatQuestion(T3 t) { if (!eqicOneOf(t.c, "what?")) return null; return ai_mergeSolutions(ai_texts(t.a, t.b, "$X"), null); } public static String ai_findUnknownThingInTriple(T3 t) { if (!ai_isKnownThing(t.a)) return t.a; if (!ai_isKnownThing(t.c)) return t.c; if (!ai_isKnownThing(t.b)) return t.b; return null; } public static void setVarUnlessNull(Var v, A a) { if (a != null && v != null) v.set(a); } public static String loadProgramTextFile(String name) { return loadTextFile(getProgramFile(name)); } public static String loadProgramTextFile(String progID, String name) { return loadTextFile(getProgramFile(progID, name)); } public static String loadProgramTextFile(String progID, String name, String defaultText) { return loadTextFile(getProgramFile(progID, name), defaultText); } public static String ai_tripleToString(T3 t) { return t == null ? "" : curlyBraceIfContainsArrow(t.a) + " -> " + curlyBraceIfContainsArrow(t.b) + " -> " + curlyBraceIfContainsArrow(t.c); } public static List dropCertainPunctuationAtEnd(List tok, String charsToDrop) { tok = cloneList(tok); for (int i = tok.size() - 2; i > 0; i -= 2) { String t = tok.get(i); if (t.length() == 1 && charsToDrop.contains(t)) { tok.remove(i + 1); tok.remove(i); } else break; } return tok; } public static String dropCertainPunctuationAtEnd(String s, String chars) { return join(dropCertainPunctuationAtEnd(nlTok(s), chars)); } public static T3 ai_fixTriple(T3 t) { if (t == null) return t; t.a = ai_fix(t.a); t.b = ai_fix(t.b); t.c = ai_fix(t.c); List tok = javaTokC(t.a); String s = last(tok); if (l(tok) > 1 && (isAdverb(s) || eqicOneOf(s, "can", "should", "rather", "will"))) { tok = javaTokC(t.a = trim(dropLastJavaToken(t.a))); t.b = s + " " + t.b; } if (l(tok) > 1 && eqicOneOf(first(tok), "why", "today")) { t.a = trim(dropFirstJavaToken(t.a)); t.b = first(tok) + " " + t.b; } tok = javaTokC(t.c); s = last(tok); if (l(tok) > 1 && (isAdverb(s) || eqicOneOf(s, "today"))) { t.c = trim(dropLastJavaToken(t.c)); t.b = s + " " + t.b; } if (empty(t.c)) t.c = "."; return t; } public static List ai_tripelize_prepare(String s) { return ai_tok_combinePhrases(tok_combineCurlyBrackets_keep(nlTok(s))); } public static List simpleSpaces(List tok) { tok = cloneList(tok); for (int i = 0; i < l(tok); i += 2) tok.set(i, tok.get(i).equals("") ? "" : " "); return tok; } public static String simpleSpaces(String s) { return join(simpleSpaces(nlTok(s))); } public static T3 ai_tripelize_dash(String s, List tok) { int i = indexOfLL(tok, " ", "-", " "); if (i < 0) return null; return triple(trimJoinSubList(tok, 0, i), "-", trimJoinSubList(tok, i + 3)); } public static T3 ai_tripelize_exclamCommand(String s, List tok) { if (!eqGet(tok, 1, "!")) return null; if (!isIdentifier(get(tok, 3))) return null; int i = tok_smartIndexOfFirstSpace(tok, 1); String arg = trimJoinSubList(tok, i + 1); return t3(trimJoinSubList(tok, 0, i), empty(arg) ? "." : ",", or2(arg, ".")); } public static T3 ai_tripelize_howLongEtc(String s) { return ai_tripelize_howLongEtc(s, ai_tripelize_prepare(s)); } public static T3 ai_tripelize_howLongEtc(String s, List tok) { if (eqicGetOneOf(tok, 1, "how", "what")) { int verb = indexOfFirstVerb(tok); String v = get(tok, verb); if (eqicOneOf(v, "does", "do")) { int verb2 = indexOfFirstVerb(tok, verb + 2); if (verb2 >= 0) { String a = v; v = get(tok, verb2); print("verb: " + v); String middle = trimJoinSubList(tok, verb + 2, verb2); String rest = trimJoinSubList(tok, verb2 + 1); v = ai_tripelize_helperVerb(a, v); return t3(middle, dropSuffix("?", v), trim(trimJoinSubList(tok, 0, verb) + " " + rest) + "?"); } } return t3(trimJoinSubList(tok, verb + 1), v, trimJoinSubList(tok, 0, verb) + "?"); } return null; } public static String trimJoinSubList(List l, int i, int j) { return trim(join(subList(l, i, j))); } public static String trimJoinSubList(List l, int i) { return trim(join(subList(l, i))); } public static int indexOfSubList(List x, List y) { return indexOfSubList(x, y, 0); } public static int indexOfSubList(List x, List y, int i) { outer: for (; i + l(y) <= l(x); i++) { for (int j = 0; j < l(y); j++) if (neq(x.get(i + j), y.get(j))) continue outer; return i; } return -1; } public static int indexOfSubList(List x, A[] y, int i) { outer: for (; i + l(y) <= l(x); i++) { for (int j = 0; j < l(y); j++) if (neq(x.get(i + j), y[j])) continue outer; return i; } return -1; } public static Set ai_yesNoQuestionWord_1_list = litciset("am", "are", "is", "can", "does", "do", "did", "would", "might", "will", "must", "should", "has", "have"); public static boolean ai_yesNoQuestionWord_1(String s) { return containsNotNull(ai_yesNoQuestionWord_1_list, s); } public static boolean neqicOneOf(String s, String... l) { return !eqicOneOf(s, l); } public static int indexOfFirstVerb(List tok, int i) { return indexOfAny(tok, i, knownVerbs()); } public static int indexOfFirstVerb(List tok) { return indexOfFirstVerb(tok, 0); } public static String ai_tripelize_helperVerb(String a, String v) { if (eqic(a, "does")) v = verbToThirdPerson(v) + "?"; else if (eqic(a, "do")) v = v + "?"; else if (eqic(a, "did")) v = verbToPast(v) + "?"; else v = a + " " + v + "?"; return v; } public static String joinWithSpace2(Collection c) { return joinNempties(" ", c); } public static String joinWithSpace2(String... c) { return joinNempties(" ", c); } public static boolean isVerb(String s) { return containsNotNull(knownVerbs(), lower(s)); } public static int ai_splitTwoNounPhrases(List tok1) { return ai_splitTwoNounPhrasesC(codeTokens(tok1)) * 2; } public static int ai_splitTwoNounPhrasesC(List tok) { String a = first(tok), b = second(tok); boolean a1 = ai_is_myYourEtc(a); if ((a1 || ai_is_myYourEtc_maybe(a)) && isNoun(b)) return 2; if (eqicOneOf(get(tok, 2), "a", "an", "the", "my", "your", "their", "our", "his", "her")) return 2; int i = 0; if (a1) ++i; ai_splitTwoNounPhrases_assumeNoun(get(tok, i)); return i + 1; } public static void ai_splitTwoNounPhrases_assumeNoun(String s) { } public static int ai_endOfNounPhrase(List tok) { int i = 1; while (i < l(tok)) { String t = get(tok, i); if (i + 2 < l(tok) && isKnownPhrase(t + " " + tok.get(i + 2))) i += 4; else if (i == 1 && eqicOneOf(t, "a", "the")) i += 4; else if (nounPhraseContinuers().contains(lower(t)) || isAdjective(t) || isNoun(t)) i += 2; else break; } return i; } public static Object safeUnstructure(String s) { return unstructure(s, true); } public static List filterByField(Collection c, String field, Object value) { List l = new ArrayList(); for (A x : c) if (eq(getOpt(x, field), value)) l.add(x); return l; } public static void ai_onNewOrRemovedWeb(Object onNewWeb, Object onRemovedWeb) { ai_onNewWeb(onNewWeb); if (onRemovedWeb != null) onTransientWebRemoved_do(onRemovedWeb); } public static Collection allWebsFromCachedNodeIndex_uncached() { return nodesToWebs(values(cachedNodeIndex())); } public static List web_search_dollarX(Web searchWeb, Web web) { return webs_search_dollarX(searchWeb, ll(web)); } public static boolean isGlobalID(String s) { return possibleGlobalID(s); } public static GlobalID asGlobalID(String id) { return id == null ? null : new GlobalID(id); } public static String md5(String text) { try { if (text == null) return "-"; return bytesToHex(md5_impl(text.getBytes("UTF-8"))); } catch (Exception __e) { throw rethrow(__e); } } public static String md5(byte[] data) { if (data == null) return "-"; return bytesToHex(md5_impl(data)); } public static MessageDigest md5_md; public static byte[] md5_impl(byte[] data) { try { return MessageDigest.getInstance("MD5").digest(data); } catch (Exception __e) { throw rethrow(__e); } } public static String md5(File file) { return md5OfFile(file); } public static String loadCachedTranspilation(String id) { return loadTextFilePossiblyGZipped(new File(getCodeProgramDir(id), "Transpilation")); } public static boolean startsWithIgnoreCase(String a, String b) { return regionMatchesIC(a, 0, b, 0, b.length()); } public static String dropSuffixAndTrim(String suffix, String s) { return trim(dropSuffix(suffix, s)); } public static List allMultiMapValues(MultiMap mm) { return mm == null ? emptyList() : concatLists(values(mm.data)); } public static List allMultiMapValues(Map> mm) { return concatLists(values(mm)); } public static NavigableMap prefixSubMap(NavigableMap map, String prefix) { return map.subMap(prefix, true, prefix + Character.MAX_VALUE, true); } public static List collectField(Collection c, String field) { List l = new ArrayList(); for (Object a : c) l.add(getOpt(a, field)); return l; } public static List collectField(String field, Collection c) { return collectField(c, field); } public static Collection ai_withoutInvalidWebs(Collection l) { if (l == null) return null; for (Web n : l) if (ai_isInvalidWeb(n)) { Iterator it = iterator(l); Web node; List x = new ArrayList(); while ((node = it.next()) != n) x.add(node); while (it.hasNext()) { node = it.next(); if (!ai_isInvalidWeb(node)) x.add(node); } return x; } return l; } public static List ai_withoutInvalidWebs(List l) { return (List) ai_withoutInvalidWebs((Collection) l); } public static List nodesToWebs(Collection nodes) { Set webs = litorderedset(); if (nodes != null) for (WebNode node : nodes) if (node != null) addIfNotNull(webs, node.web); return asList(webs); } public static List nlTok(String s) { return javaTokPlusPeriod(s); } public static int indexOfAny(List l, int i, A... x) { while (i < l(l)) if (eqOneOf(l.get(i), x)) return i; else ++i; return -1; } public static int indexOfAny(List l, Collection x) { return indexOfAny(l, 0, x); } public static int indexOfAny(List l, int i, Collection x) { if (nempty(x)) while (i < l(l)) if (x.contains(l.get(i))) return i; else ++i; return -1; } public static int indexOfAny(String s, int i, String chars) { for (; i < l(s); i++) if (chars.indexOf(s.charAt(i)) >= 0) return i; return -1; } public static List allToLower(Collection l) { return map("lower", l); } public static Cache> baseFormVerbs_cache = new Cache("baseFormVerbs_load"); public static Set baseFormVerbs() { return baseFormVerbs_cache.get(); } public static Set baseFormVerbs_load() { return new TreeSet(nthColumn(englishVerbQuintuples(), 0)); } public static boolean charactersEqualIC(char c1, char c2) { if (c1 == c2) return true; char u1 = Character.toUpperCase(c1); char u2 = Character.toUpperCase(c2); if (u1 == u2) return true; return Character.toLowerCase(u1) == Character.toLowerCase(u2); } public static int scoredSearch_score(String s, List words) { int score = 0; for (String word : words) score += scoredSearch_score_single(s, word); return score; } public static int scoredSearch_score(String s, String query) { return scoredSearch_score(s, scoredSearch_prepare(query)); } public static List scoredSearch_prepare(String query) { return splitAtSpace(query); } public static List keysSortedByValuesDesc(final Map map) { List l = new ArrayList(map.keySet()); sort(l, mapComparatorDesc(map)); return l; } public static List tlft(String s) { return toLinesFullTrim(s); } public static List tlft(File f) { return toLinesFullTrim(f); } public static boolean preferCached = false; public static boolean loadSnippet_debug = false; public static ThreadLocal loadSnippet_silent = new ThreadLocal(); public static int loadSnippet_timeout = 30000; public static String loadSnippet(String snippetID) { try { if (snippetID == null) return null; return loadSnippet(parseSnippetID(snippetID), preferCached); } catch (Exception __e) { throw rethrow(__e); } } public static String loadSnippet(String snippetID, boolean preferCached) throws IOException { return loadSnippet(parseSnippetID(snippetID), preferCached); } public static String loadSnippet(long snippetID) { try { return loadSnippet(snippetID, preferCached); } catch (Exception __e) { throw rethrow(__e); } } public static String loadSnippet(long snippetID, boolean preferCached) throws IOException { String text; initSnippetCache(); text = DiskSnippetCache_get(snippetID); if (preferCached && text != null) return text; try { if (loadSnippet_debug && text != null) System.err.println("md5: " + md5(text)); String url = tb_mainServer() + "/getraw.php?id=" + snippetID + "&utf8=1"; if (nempty(text)) url += "&md5=" + md5(text); url += standardCredentials(); String text2 = loadSnippet_loadFromServer(url); boolean same = eq(text2, "==*#*=="); if (loadSnippet_debug) print("loadSnippet: same=" + same); if (!same) text = text2; } catch (RuntimeException e) { e.printStackTrace(); throw new IOException("Snippet #" + snippetID + " not found or not public"); } try { initSnippetCache(); DiskSnippetCache_put(snippetID, text); } catch (IOException e) { System.err.println("Minor warning: Couldn't save snippet to cache (" + DiskSnippetCache_getDir() + ")"); } return text; } public static File DiskSnippetCache_dir; public static void initDiskSnippetCache(File dir) { DiskSnippetCache_dir = dir; dir.mkdirs(); } public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException { return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null); } public static File DiskSnippetCache_getFile(long snippetID) { return new File(DiskSnippetCache_dir, "" + snippetID); } public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException { saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet); } public static File DiskSnippetCache_getDir() { return DiskSnippetCache_dir; } public static void initSnippetCache() { if (DiskSnippetCache_dir == null) initDiskSnippetCache(getGlobalCache()); } public static String loadSnippet_loadFromServer(String url) { Integer oldTimeout = setThreadLocal(loadPage_forcedTimeout_byThread, loadSnippet_timeout); try { return isTrue(loadSnippet_silent.get()) ? loadPageSilently(url) : loadPage(url); } finally { loadPage_forcedTimeout_byThread.set(oldTimeout); } } public static float parseFloat(String s) { return Float.parseFloat(s); } public static B mapPutIfChange2(Map map, A key, B value) { if (map != null && key != null && value != null && neq(map.get(key), value)) return map.put(key, value); return null; } public static String numberedLines(Collection l) { int i = 0; List lines = new ArrayList(); if (l != null) for (A a : l) lines.add(((++i) + ". " + str(a))); return lines(lines); } public static Map computerVMs_shorter() { Map vms = computerVMs(); for (Map map : values(vms)) mapRemoveAll(map, "More info", "Injection ID"); return vms; } public static TreeSet numbersOfStandardFunctionArguments(String sfName) { return numbersOfMethodArguments(loadFunctions_cached(sfName), sfName); } public static Object callAndMakeOrDirect_safe(String functionName, List args) { return callAndMake_orDirect(ai_verifySafeToCall(functionName), args); } public static Object callAndMakeOrDirect_safe(String functionName) { return callAndMakeOrDirect_safe(functionName, new ArrayList()); } public static class FG extends F1 { public F1 f, g; public FG() { } public FG(F1 f, F1 g) { this.g = g; this.f = f; } public Object get(Object o) { return f.get(g.get(o)); } } public static Object fg(final Object f, final Object g) { return new FG(f1(f), f1(g)); } public static Object fg(final Object f, final Object g, final Object h) { return fg(f, fg(g, h)); } public static List allToUpper(Collection l) { List x = new ArrayList(l(l)); if (l != null) for (String s : l) x.add(upper(s)); return x; } public static List dropLeadingPunctuation_keep = ll("*", "<", ">", "(", "{", "["); public static List dropLeadingPunctuation(List tok) { tok = new ArrayList(tok); for (int i = 1; i < l(tok); ) { String t = tok.get(i); if (eq(t, "...") || t.length() == 1 && !Character.isLetterOrDigit(t.charAt(0)) && !dropLeadingPunctuation_keep.contains(t)) { tok.remove(i); tok.remove(i); } else break; } return tok; } public static String dropLeadingPunctuation(String s) { return join(dropLeadingPunctuation(nlTok(s))); } public static boolean startsWithWords(String a, String b, Matches m) { return startsWithWord(a, b, m); } public static boolean startsWithWords(String a, String b) { return startsWithWord(a, b); } public static NavigableSet ai_mainIndexKeys() { return cachedNodeIndex_autoMake ? navigableKeys(cachedNodeIndex()) : tripleIndex().navigableMainKeys(); } public static boolean matchStart(String pat, String s) { return matchStart(pat, s, null); } public static boolean matchStart(String pat, String s, Matches matches) { if (s == null) return false; List tokpat = parse3(pat), toks = parse3(s); if (toks.size() < tokpat.size()) return false; String[] m = match2(tokpat, toks.subList(0, tokpat.size())); if (m == null) return false; else { if (matches != null) { matches.m = new String[m.length + 1]; arraycopy(m, matches.m); matches.m[m.length] = join(toks.subList(tokpat.size(), toks.size())); } return true; } } public static List web_uniqueTexts(List l) { return asList(new LinkedHashSet(map("web_text", l))); } public static A oneOf(List l) { return l.isEmpty() ? null : l.get(new Random().nextInt(l.size())); } public static char oneOf(String s) { return empty(s) ? '?' : s.charAt(random(l(s))); } public static String oneOf(String... l) { return oneOf(asList(l)); } public static A collectionGet(Collection c, int idx) { if (c == null || idx < 0 || idx >= l(c)) return null; if (c instanceof List) return listGet((List) c, idx); Iterator it = c.iterator(); for (int i = 0; i < idx; i++) if (it.hasNext()) it.next(); else return null; return it.hasNext() ? it.next() : null; } public static List nthColumn(List> matrix, int idx) { List l = new ArrayList(); for (List row : matrix) l.add(get(row, idx)); return l; } public static List> englishVerbQuintuples() { return english1000VerbQuintuples(); } public static List dropPunctuationAtEnd_keep = litlist("*", "<", ">", "]", ")", "}"); public static List dropPunctuationAtEnd(List tok) { tok = new ArrayList(tok); for (int i = tok.size() - 2; i > 0; i -= 2) { String t = tok.get(i); if (t.length() == 1 && !Character.isLetter(t.charAt(0)) && !Character.isDigit(t.charAt(0)) && !dropPunctuationAtEnd_keep.contains(t)) { tok.remove(i); tok.remove(i); } else break; } return tok; } public static String dropPunctuationAtEnd(String s) { return join(dropPunctuationAtEnd(nlTok(s))); } public static HashSet litset(A... items) { return lithashset(items); } public static String afterLastSpace(String s) { return s == null ? null : substring(s, s.lastIndexOf(' ') + 1); } public static List webs_tripleSearch_dollarX(String a, String b, String c, Collection webs) { List l = new ArrayList(); boolean isA = eq(a, "$X"); for (Web web : webs) { Pair p = web_matchTriple_dollarVars(a, b, c, web); if (p != null) l.add(isA ? p.a : p.b); } return l; } public static List webs_tripleSearch_dollarX(Collection webs, T3 t) { return webs_tripleSearch_dollarX(t.a, t.b, t.c, webs); } public static ThreadLocal ai_search_indexedWebs_verified = new ThreadLocal(); public static List ai_search_indexedWebs(String query) { return isTrue(ai_search_indexedWebs_verified) ? indexedVerifiedWebs(query) : indexedWebs(query); } public static String tolower(String s) { return s == null ? null : s.toLowerCase(); } public static char tolower(char c) { return Character.toLowerCase(c); } public static HashMap pairsToMap(Collection> l) { HashMap map = new HashMap(); if (l != null) for (Pair p : l) map.put(p.a, p.b); return map; } public static List scanStructureLog(String progID, String fileName) { return scanStructureLog(getProgramFile(progID, fileName)); } public static List scanStructureLog(String fileName) { return scanStructureLog(getProgramFile(fileName)); } public static List scanStructureLog(File file) { List l = new ArrayList(); for (String s : scanLog(file)) try { l.add(unstructure(s)); } catch (Throwable __e) { _handleException(__e); } return l; } public static int indexOfIC(List a, String b) { return indexOfIgnoreCase(a, b); } public static int indexOfIC(List a, String b, int i) { return indexOfIgnoreCase(a, b, i); } public static int indexOfIC(String a, String b) { return indexOfIgnoreCase(a, b); } public static WebRelation web_relationToNodeNamed(WebNode a, String b) { for (WebNode n : web_forwardRelated(a)) if (web_hasText(n, b)) return web_getRelation(a, n); return null; } public static boolean isEmpty(Collection c) { return c == null || c.isEmpty(); } public static boolean isEmpty(CharSequence s) { return s == null || s.length() == 0; } public static boolean isEmpty(Object[] a) { return a == null || a.length == 0; } public static boolean isEmpty(byte[] a) { return a == null || a.length == 0; } public static boolean isEmpty(Map map) { return map == null || map.isEmpty(); } public static TreeSet toCaseInsensitiveSet(Collection c) { TreeSet set = caseInsensitiveSet(); if (c != null) set.addAll(c); return set; } public static TreeSet toCaseInsensitiveSet(String... x) { TreeSet set = caseInsensitiveSet(); addAll(set, x); return set; } public static ArrayList litlist(A... a) { ArrayList l = new ArrayList(a.length); for (A x : a) l.add(x); return l; } public static String emptyToNull(String s) { return eq(s, "") ? null : s; } public static long now_virtualTime; public static long now() { return now_virtualTime != 0 ? now_virtualTime : System.currentTimeMillis(); } public static long unixHour() { return unixHour(now()); } public static long unixHour(long timestamp) { return unixTime(timestamp) / (60 * 60); } public static Map web_matchAllPerms_x(Web patternWeb, Web inputWeb, Object nodeMatcher) { List patternNodes = web_nodes(patternWeb); List inputNodes = web_nodes(inputWeb); if (l(patternNodes) != l(inputNodes)) return null; List> perms = allPermutations(inputNodes); Best> best = new Best(); for (List perm : perms) best.put(perm, web_matchNodeLists_x(patternNodes, perm, nodeMatcher)); List l = best.getIfScoreAbove(0); return l == null ? null : twoListsToOrderedMap(patternNodes, l); } public static Map web_addRelationsToMapping(Map map) { Web web = firstKey(map).web; Map map2 = cloneMap(map); for (WebRelation rel : web_relationObjects(web)) { WebNode a2 = map.get(rel.a), b2 = map.get(rel.b); if (a2 != null && b2 != null) mapPut(map2, rel, web_findRelationObject(a2, b2)); } return map2; } public static WebNode web_findNodeOrRelation(Web web, String name) { WebNode n = web.findNode(name); if (n != null) return n; return web_findRelationWithLabel(web, name); } public static Web simpleWeb() { Web web = new Web(); web.useCLParse = false; return web; } public static void web_addRelation(WebNode a, WebNode b, String rel) { if (a != null && b != null && rel != null) a.web.getRelation(a, b).addLabel(rel); } public static void web_addRelation(Web web, String a, String b, String rel) { web_addRelation(web.node(a), web.node(b), rel); } public static void web_addRelation(WebNode a, String rel, WebNode b) { web_addRelation(a, b, rel); } public static String[] toSymbolArray(Collection c) { String[] a = new String[l(c)]; Iterator it = c.iterator(); for (int i = 0; i < l(a); i++) a[i] = it.next(); return a; } public static String ai1_cookiesToday() { String info = ai1_visitorsInfo(); return or2(jextract(" cookie", info), jextract(" cookies", info)); } public static List indexedNodesAfterVariableSubstitution(String s) { List nodes = indexedNodesContainingDollarVars(); List l = new ArrayList(); for (WebNode node : nodes) { Map map = matchDollarVarsIC(web_text(node), s); if (map == null) continue; WebNode n = cloneWeb_handlingNode(node); web_replaceDollarVars(n.web, map); l.add(n); } return l; } public static List getFromAll(Collection> maps, A key) { List l = new ArrayList(); if (maps != null) for (Map map : maps) addIfNotNull(l, map.get(key)); return l; } public static WebNode web_findNode(Web web, String name) { return web.findNode(name); } public static Map, Boolean> ai_isRuleSuitableForFunction_cache = synchroHashMap(); public static boolean ai_isRuleSuitableForFunction(String rule, String f) { Pair p = pair(rule, f); Boolean b = ai_isRuleSuitableForFunction_cache.get(p); if (b != null) return b; try { callAndMake(f, rule, null); ai_isRuleSuitableForFunction_cache.put(p, true); return true; } catch (Throwable e) { if (shortClassNameIs(e, "UnsuitableRuleException")) { ai_isRuleSuitableForFunction_cache.put(p, false); return false; } else { ai_isRuleSuitableForFunction_cache.put(p, false); printException(e); throw new FunctionIsBroken(f); } } } public static String ai_ifAndInput(String rule, String input) { rule = unquote(rule); if (containsJavaToken(rule, "Y")) return ai_ifAndInput_v2(rule, input); Matches m = new Matches(); if (!simpleStarMatchOnCTokensIC("if * and input * then say *", javaTokDroppingBracketsC(rule), m)) unsuitableRule(); if (!match(m.unq(1), input)) return null; T3 t = ai_tripelize(ai_bigXToDollarX(m.unq(0))); printStruct("t", t); String x = random(ai_texts(t)); if (x == null) return null; return ai_replaceBigX(m.unq(2), x); } public static AutoCloseable temp_ai_setVar(final String var, Object value) { final Map prev = ai_getOrCreateVars(); final Object oldValue = prev.put(var, value); return new AutoCloseable() { public void close() { mapPut(prev, var, oldValue); ai_setVars(prev); } }; } public static HashMap, Class> loadFunctions_cache = new HashMap(); public static Lock loadFunctions_cached_lock = lock(); public static boolean loadFunctions_useDiskCache; public static Class loadFunctions_cached(String... functions) { return loadFunctions_cached(asList(functions)); } public static Class loadFunctions_cached(Collection functions) { TreeSet functionSet = new TreeSet(functions); Lock __1851 = loadFunctions_cached_lock; lock(__1851); try { Class c = loadFunctions_cache.get(functionSet); if (c != null) return c; String transpilation = loadFunctions_useDiskCache ? getTranspiledStandardFunctionsIfCached(functionSet) : null; if (transpilation != null) { c = veryQuickJava_finish(transpilation, emptyList()); } if (c == null) c = loadFunctions(functionSet); loadFunctions_cache.put(functionSet, c); assertNotNull(loadFunctions_cache.get(functionSet)); return c; } finally { unlock(__1851); } } public static Object pcallFunction(Object f, Object... args) { try { return callFunction(f, args); } catch (Throwable __e) { _handleException(__e); } return null; } public static List> ai_texts_verified_dollarXY(String a, String b, String c) { if (isDollarVar(b)) throw todo(); List> l = new ArrayList(); if (eq(a, "$X") && eq(c, "$Y")) { for (WebRelation r : indexedRelations_verified(b)) l.add(pair(web_text(r.a), web_text(r.b))); return l; } if (eq(a, "$Y") && eq(c, "$X")) { for (WebRelation r : indexedRelations_verified(b)) l.add(pair(web_text(r.b), web_text(r.a))); return l; } throw fail("improper arguments: " + a + "/" + b + "/" + c); } public static Map simpleMatchTriple_dollarVars(T3 pat, T3 t) { if (pat == null || t == null) return null; return simpleMatchTriple_dollarVars(pat.a, pat.b, pat.c, t); } public static Map simpleMatchTriple_dollarVars(String patA, String patB, String patC, T3 t) { if (t == null) return null; Map match = new HashMap(); return matchDollarVarIC(match, patA, t.a) && matchDollarVarIC(match, patB, t.b) && matchDollarVarIC(match, patC, t.c) ? match : null; } public static String tripleToJava(T3 t) { return "triple(" + quote(t.a) + ", " + quote(t.b) + ", " + quote(t.c) + ")"; } public static String getInnerMessage(Throwable e) { if (e == null) return null; return getInnerException(e).getMessage(); } public static String localDateWithMilliseconds(long time) { SimpleDateFormat format = simpleDateFormat_local("yyyy/MM/dd HH:mm:ss''SSSS"); return format.format(time); } public static String localDateWithMilliseconds() { return localDateWithMilliseconds(now()); } public static long currentThreadID() { return currentThread().getId(); } public static String getClientIP() { { Map headers = MyHTTPD_headers.get(); if (headers != null) return getClientIPFromHeaders(headers); } { Map headers = WebSocketHTTPD_headers.get(); if (headers != null) return getClientIPFromHeaders(headers); } return getClientIP_subBot(); } public static String myFirewallIP() { return assertIPv4(loadPage("http://tinybrain.de:8080/tb-int/my-ip.php")); } public static SimpleDateFormat simpleDateFormat_GMT(String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); sdf.setTimeZone(TimeZone.getTimeZone("GMT")); return sdf; } public static String formatGMT_24(long time) { SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); sdf.setTimeZone(TimeZone.getTimeZone("GMT")); return sdf.format(time) + " GMT"; } public static long round(double d) { return Math.round(d); } public static String formatGMTWithSeconds_24(long time) { SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); sdf.setTimeZone(TimeZone.getTimeZone("GMT")); return sdf.format(time) + " GMT"; } public static void close(AutoCloseable c) { try { if (c != null) c.close(); } catch (Exception __e) { throw rethrow(__e); } } public static void logQuotedWithDate(String s) { logQuotedWithTime(s); } public static void logQuotedWithDate(String logFile, String s) { logQuotedWithTime(logFile, s); } public static void logQuotedWithDate(File logFile, String s) { logQuotedWithTime(logFile, s); } public static boolean contains(Collection c, Object o) { return c != null && c.contains(o); } public static boolean contains(Object[] x, Object o) { if (x != null) for (Object a : x) if (eq(a, o)) return true; return false; } public static boolean contains(String s, char c) { return s != null && s.indexOf(c) >= 0; } public static boolean contains(String s, String b) { return s != null && s.indexOf(b) >= 0; } public static boolean contains(BitSet bs, int i) { return bs != null && bs.get(i); } public static String hhidden(String name, Object value, Object... params) { return tag("input", "", concatArrays(new Object[] { "type", "hidden", "name", name, "value", value }, params)); } public static boolean endsWithIgnoreCase(String a, String b) { int la = l(a), lb = l(b); return la >= lb && regionMatchesIC(a, la - lb, b, 0, lb); } public static boolean endsWithIgnoreCase(String a, String b, Matches m) { if (!endsWithIgnoreCase(a, b)) return false; m.m = new String[] { substring(a, 0, l(a) - l(b)) }; return true; } public static File loadBinarySnippet(String snippetID) { try { long id = parseSnippetID(snippetID); File f = DiskSnippetCache_getLibrary(id); if (fileSize(f) == 0) f = loadDataSnippetToFile(snippetID); return f; } catch (Exception __e) { throw rethrow(__e); } } public static int iceil(double d) { return (int) Math.ceil(d); } public static String callAnswerFunctionVerbose(String s) { print("> " + s); String answer = callStaticAnswerMethod(mc(), s); print("< " + answer); return answer; } public static int lCommonPrefixIC(String a, String b) { int i = 0; while (i < a.length() && i < b.length() && eqic(a.charAt(i), b.charAt(i))) ++i; return i; } public static String formatSnippetID(String id) { return "#" + parseSnippetID(id); } public static String formatSnippetID(long id) { return "#" + id; } public static List newSubList(List l, int startIndex, int endIndex) { return cloneList(subList(l, startIndex, endIndex)); } public static List newSubList(List l, int startIndex) { return cloneList(subList(l, startIndex)); } public static boolean ai_cache_hasTriple_verified(String a, String b, String c) { if (a == null || b == null || c == null) return false; if (ai_useThoughtSpaces()) return thoughtSpace().hasTriple_verified(symbol(a), symbol(b), symbol(c)); AI_CacheChecker checker = ai_cacheChecker(); if (checker != null && checker.hasTriple(symbol(a), symbol(b), symbol(c))) return true; List webs = ai_fewestIndexedWebs_verified(a, b, c); boolean x = webs_search_noVar_bool(webFromTriple(a, b, c), webs); return x; } public static boolean ai_cache_hasTriple_verified(T3 t) { return ai_cache_hasTriple_verified(t.a, t.b, t.c); } public static void makeDependent(Object c) { if (c == null) return; assertTrue("Not a class", c instanceof Class); hotwire_classes.add(new WeakReference(c)); Object local_log = getOpt(mc(), "local_log"); if (local_log != null) setOpt(c, "local_log", local_log); Object print_byThread = getOpt(mc(), "print_byThread"); if (print_byThread != null) setOpt(c, "print_byThread", print_byThread); } public static String renderUserThreadsWithStackTraces() { return renderUserThreadsWithStackTraces(Thread.getAllStackTraces()); } public static String renderUserThreadsWithStackTraces(Map threadMap) { return renderUserThreadsWithStackTraces(threadMap, "user thread"); } public static String renderUserThreadsWithStackTraces(Map threadMap, String threadDescription) { StringBuilder buf = new StringBuilder(); int n = 0; for (Thread t : threadMap.keySet()) { ThreadGroup g = t.getThreadGroup(); if (g != null && g.getName().equals("system")) continue; ++n; printTo(buf, t); for (StackTraceElement e : threadMap.get(t)) { printTo(buf, " " + e); } printTo(buf); } printTo(buf, n(n, threadDescription) + "."); return asciiHeading2(n(n, threadDescription) + " (+" + (l(threadMap) - n) + ")") + "\n" + buf; } public static Map runnableThreadsWithStackTraces() { Map map = filterMap("isThreadRunnable_x", Thread.getAllStackTraces()); map.remove(currentThread()); return map; } public static String rep(int n, char c) { return repeat(c, n); } public static String rep(char c, int n) { return repeat(c, n); } public static List rep(A a, int n) { return repeat(a, n); } public static List rep(int n, A a) { return repeat(n, a); } public static String hijackPrint(Object r) { final StringBuilder buf = new StringBuilder(); Object old = interceptPrintInThisThread(new F1() { public Boolean get(String s) { buf.append(s); return false; } }); try { callF(r); return str(buf); } finally { interceptPrintInThisThread(old); } } public static String htitle_h1(String s) { return htitle_noEncode(dropTags(s)) + h1(s); } public static String ul_htmlencode(String... list) { return ul_htmlEncode(list); } public static String ul_htmlencode(Collection list, Object... params) { return ul_htmlEncode(list, params); } public static List multiSetKeysByPopularity(MultiSet ms) { return multiSetByPopularity(ms); } public static NanoHTTPD.Response serveByteArray(byte[] data, String mimeType) { try { return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Status.OK, mimeType, new ByteArrayInputStream(data), l(data)); } catch (Exception __e) { throw rethrow(__e); } } public static byte[] toJPG(BufferedImage img) { return toJPEG(img); } public static byte[] toJPG(File f) { return toJPEG(f); } public static BufferedImage newBufferedImage(int w, int h) { return new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); } public static BufferedImage newBufferedImage(int w, int h, Color color) { BufferedImage img = newBufferedImage(w, h); Graphics2D g = img.createGraphics(); g.setColor(color); g.fillRect(0, 0, w, h); return img; } public static BufferedImage newBufferedImage(Pt p, Color color) { return newBufferedImage(p.x, p.y, color); } public static A or(A a, A b) { return a != null ? a : b; } public static String lispHead(Lisp l) { return l == null ? null : l.head; } public static List listMinusSet(Collection l, Collection stuff) { if (empty(stuff)) return asList(l); Set set = asSet(stuff); List l2 = new ArrayList(); for (A a : l) if (!set.contains(a)) l2.add(a); return l2; } public static double cal_randomCircleCoord() { return random(0.2, 0.8); } public static String[] dropFirst(int n, String[] a) { return drop(n, a); } public static String[] dropFirst(String[] a) { return drop(1, a); } public static Object[] dropFirst(Object[] a) { return drop(1, a); } public static List dropFirst(List l) { return dropFirst(1, l); } public static List dropFirst(Iterable i) { return dropFirst(toList(i)); } public static List dropFirst(int n, List l) { return n <= 0 ? l : new ArrayList(l.subList(Math.min(n, l.size()), l.size())); } public static List dropFirst(List l, int n) { return dropFirst(n, l); } public static String dropFirst(int n, String s) { return substring(s, n); } public static String dropFirst(String s) { return substring(s, 1); } public static Set> web_relations(Web web) { LinkedHashSet> l = new LinkedHashSet(); for (WebNode n : web.nodes) if (n instanceof WebRelation) l.add(pair(((WebRelation) n).a, ((WebRelation) n).b)); return l; } public static A reverseLookup(B value, Map map) { return reverseLookup(map, value); } public static A reverseLookup(Map map, B value) { if (map != null && value != null) for (A key : map.keySet()) if (eq(map.get(key), value)) return key; return null; } public static void copyCAL(CirclesAndLines cal1, CirclesAndLines cal2) { if (cal1 == cal2) return; copyList(cal1.circles, cal2.circles); copyList(cal1.lines, cal2.lines); copyFields(cal1, cal2, "defaultImageID", "arrowClass", "circleClass", "imgZoom", "imageForUserMadeNodes", "title", "globalID"); copyListeners(cal1, cal2); } public static byte[] toUtf8(String s) { try { return s.getBytes("UTF-8"); } catch (Exception __e) { throw rethrow(__e); } } public static String renderConcept(Concept.Ref ref) { return renderConcept(ref.get()); } public static String renderConcept(Concept c) { if (c == null) return "null"; StringBuilder buf = new StringBuilder(); buf.append(c.id + " " + shortDynamicClassName(c)); for (String field : conceptFields(c)) { Object val = cget(c, field); if (val != null) { if (eq(val, Boolean.FALSE) && isPrimitiveBoolField(c, field)) continue; buf.append(" " + field + "="); buf.append(renderConcept_short(c, val)); } } return str(buf); } public static String renderConcept_short(Concept c, Object val) { if (val instanceof Concept) return shortDynamicClassName(val) + ((Concept) val).id; else if (val instanceof Concept.RefL) return "[" + join(", ", map((List) val, "renderConcept_short")) + "]"; else { structure_Data data = new structure_Data(); data.seen.put(c, 0); return struct(val, data); } } public static String h3_htitle(String s) { return htitle(s) + h3(s); } public static List withoutNulls(List l) { if (!containsNulls(l)) return l; List l2 = new ArrayList(); for (A a : l) if (a != null) l2.add(a); return l2; } public static Map withoutNulls(Map map) { Map map2 = similarEmptyMap(map); for (A a : keys(map)) if (a != null) { B b = map.get(a); if (b != null) map2.put(a, b); } return map2; } public static int[] subArray(int[] b, int start, int end) { int[] x = new int[end - start]; System.arraycopy(b, start, x, 0, end - start); return x; } public static byte[] subArray(byte[] b, int start, int end) { start = max(start, 0); end = min(end, l(b)); if (start >= end) return new byte[0]; byte[] x = new byte[end - start]; System.arraycopy(b, start, x, 0, end - start); return x; } public static short[] subArray(short[] b, int start, int end) { short[] x = new short[end - start]; System.arraycopy(b, start, x, 0, end - start); return x; } public static float[] subArray(float[] b, int start, int end) { float[] x = new float[end - start]; System.arraycopy(b, start, x, 0, end - start); return x; } public static Object[] subArray(Object[] b, int start) { return subArray(b, start, l(b)); } public static Object[] subArray(Object[] b, int start, int end) { start = max(start, 0); end = min(end, l(b)); if (start >= end) return new Object[0]; Object[] x = new Object[end - start]; System.arraycopy(b, start, x, 0, end - start); return x; } public static String containerTag(String tag) { return containerTag(tag, ""); } public static String containerTag(String tag, Object contents, Object... params) { String openingTag = hopeningTag(tag, params); String s = str(contents); return openingTag + s + ""; } public static TreeSet caseInsensitiveSet() { return new TreeSet(caseInsensitiveComparator()); } public static TreeSet caseInsensitiveSet(Collection c) { return toCaseInsensitiveSet(c); } public static TreeMap ciMap() { return caseInsensitiveMap(); } public static double parseDouble(String s) { return Double.parseDouble(s); } public static List allIndexedFullTerms() { return concatMap("ai_expandShortenedKeys", cloneList(allIndexedTerms())); } public static boolean allLowerCaseCharacters(String s) { for (int i = 0; i < l(s); i++) if (Character.getType(s.charAt(i)) != Character.LOWERCASE_LETTER) return false; return true; } public static void ai_callSpeculationFunctionOnWeb(final String f, String webID) { ai_callSpeculationFunctionOnWeb(f, ai_getWebFromTerm(webID)); } public static void ai_callSpeculationFunctionOnWeb(final String f, final Web web) { final T3 t = webToTriple(web); { if (t == null) return; } try { ping(); ai_withMaker(f, new Runnable() { public void run() { try { callMC(f, web); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callMC(f, web)"; } }); } catch (Throwable __e) { _handleException(__e); } } public static String htitle_h2(String s) { return htitle_noEncode(dropTags(s)) + h2(s); } public static String loadJQuery2() { return ""; } public static void printWeb(Web web) { if (web == null) { print("-"); return; } List> relations = web_nonEmptyRelations(web); print((web.unverified ? "UNVERIFIED " : "VERIFIED ") + "Web " + web.globalID + " from " + web.source + (web.created == 0 ? "" : ", created " + dateGMT(web.created)) + ": " + n(l(web.nodes) - l(relations), "node") + ", " + n(relations, "relation")); print(); List nodes = web_nonRelationNodes(web); if (!(l(nodes) == 2 && l(relations) == 1)) { pnlIndent(collectMethod(nodes, "text")); print(); } web_printRelations(web); } public static Object[] arrayPlus(Object[] a1, Object... a2) { return concatArrays(a1, a2); } public static String ai_html_triangle; public static String ai_html_triangle() { if (ai_html_triangle == null) ai_html_triangle = " " + htmlencode(unicode_rightPointingTriangle()) + " "; return ai_html_triangle; } public static String span(Object contents, Object... params) { return tag("span", contents, params); } public static String smartBot_matrixLink(String topic) { return smartBotRealURL() + "/matrix/" + urlencode(topic); } public static Set ai_ignoredWebs() { Matches m = new Matches(); Set ignoredWebs = new HashSet(); for (String s : ai_texts("$X", "can be", "ignored")) if (swic(s, "web ", m)) ignoredWebs.add(m.unq(0)); return ignoredWebs; } public static List web_nodesNotFromCertainWebs(Set ignoredWebIDs, List nodes) { List l = new ArrayList(); for (WebNode n : unnull(nodes)) if (!containsNotNull(ignoredWebIDs, n.web.globalID)) l.add(n); return l; } public static void truncateList(List l, int n) { if (l(l) < n) return; removeSubList(l, n, l(l)); } public static List web_dropConfirmedPossibles(List l) { if (empty(l)) return l; HashSet> index = new HashSet(); for (WebRelation r : l) index.add(tripleToLower(web_relationToTriple(r))); Matches m = new Matches(); List out = emptyListWithCapacity(l); for (WebRelation r : l) if (!(swic(web_text(r), "possibly ", m) && index.contains(tripleToLower(t3(web_text(r.a), m.rest(), web_text(r.b)))))) out.add(r); return out; } public static List web_collectBackwardRelations(Collection nodes) { List l = new ArrayList(); for (WebNode n : unnull(nodes)) l.addAll(web_backwardRelations(n)); return l; } public static List web_collectForwardRelations(Collection nodes) { List l = new ArrayList(); for (WebNode n : unnull(nodes)) l.addAll(web_forwardRelations(n)); return l; } public static MultiMap orderedMultiMap() { MultiMap mm = new MultiMap(); mm.data = new LinkedHashMap(); return mm; } public static List ai_sortRelationsByImportance(List rel) { return sortByCalculatedFieldDesc_f1(rel, new F1() { public Integer get(WebRelation r) { try { return swic(web_text(r), "possibly ") ? 0 : 1; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "swic(web_text(r), \"possibly \") ? 0 : 1"; } }); } public static List ai_filterInvalid(boolean invalid, List rel) { List out = new ArrayList(); for (WebRelation r : unnull(rel)) if (invalid == swic(web_text(r), "[invalid] ")) out.add(r); return out; } public static String ai_textHTML(WebNode n) { String text = web_text(n); return n == null ? "-" : html_encyclopediaTopic(text); } public static String ai_renderNodeHTML(WebNode n) { return ai_renderNodeHTML(n, "-"); } public static String ai_renderNodeHTML(WebNode n, String defaultText) { if (n == null || !web_hasLabels(n)) return defaultText; return joinWithComma(map("html_encyclopediaTopic", web_texts(n))); } public static String ai_html_webLink(String globalID) { return "http://botcompany.de/" + globalID; } public static List multiMapKeysByPopularity(final MultiMap mm) { return sortedDescAccordingTo(keys(mm), new F1() { public Object get(A key) { try { return l(mm.get(key)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "l(mm.get(key))"; } }); } public static long memoryUsedAfterLastGC() { try { return toLong(get(javax(), "gc_memoryUsedAfterGC")); } catch (Throwable e) { _handleException(e); upgradeJavaXAndRestart(); return 0; } } public static long toMB(long l) { return toM(l); } public static String toMB(long l, int digits) { return toM(l, digits); } public static long freeSystemMemory() { return oshi_systemMemory().getAvailable(); } public static List web_relationsOnly(Collection c) { List l = new ArrayList(); for (WebNode n : unnull(c)) if (n instanceof WebRelation) l.add(((WebRelation) n)); return l; } public static Object mainBot; public static Object getMainBot() { return mainBot; } public static String className(Object o) { return getClassName(o); } public static String randomIndexedTerm() { return random(ai_expandShortenedKeys(randomFromIterator(ai_indexSize(), indexedTerms_iterator()))); } public static String programID; public static String getProgramID() { return nempty(programID) ? formatSnippetIDOpt(programID) : "?"; } public static String getProgramID(Class c) { String id = (String) getOpt(c, "programID"); if (nempty(id)) return formatSnippetID(id); return "?"; } public static String getProgramID(Object o) { return getProgramID(getMainClass(o)); } public static boolean sameSnippetID(String a, String b) { if (!isSnippetID(a) || !isSnippetID(b)) return false; return parseSnippetID(a) == parseSnippetID(b); } public static File javaxDataDir_dir; public static File javaxDataDir() { return javaxDataDir_dir != null ? javaxDataDir_dir : new File(userHome(), "JavaX-Data"); } public static File javaxDataDir(String sub) { return newFile(javaxDataDir(), sub); } public static volatile String caseID_caseID; public static String caseID() { return caseID_caseID; } public static void caseID(String id) { caseID_caseID = id; } public static File getProgramDir() { return programDir(); } public static File getProgramDir(String snippetID) { return programDir(snippetID); } public static List ai_tripleRefsToTexts(String a, String b, String c, List> l) { if (empty(l)) return sharedEmptyList(); List out = new ArrayList(); for (TripleRef ref : l) if (ref != null) { T3 t = ref.triple; Map match = simpleMatchTriple_dollarVars(a, b, c, t); if (match != null) out.add(match.get(symbol_dollarX())); } return out; } public static List tok_splitAtSlash(String s) { return tok_splitAtSlash(javaTok(s)); } public static List tok_splitAtSlash(List tok) { List l = new ArrayList(); int i = 0; while (i < l(tok)) { if (i >= l(tok)) break; int j = indexOf(tok, "/", i); if (j < 0) j = l(tok); l.add(trimJoin(tok.subList(i, j))); i = j + 1; } return l; } public static List tok_combineCurlyBrackets_keep(List tok) { List l = new ArrayList(); for (int i = 0; i < l(tok); i++) { String t = tok.get(i); if (odd(i) && eq(t, "{")) { int j = findEndOfCurlyBracketPart(tok, i); l.add(joinSubList(tok, i, j)); i = j - 1; } else l.add(t); } return l; } public static T3 listToTriple(List l) { return l(l) != 3 ? null : triple(get(l, 0), get(l, 1), get(l, 2)); } public static List> ai_filterTripleRefs(int position, String a, String b, String c, List> l) { List> out = new ArrayList(); for (TripleRef r : l) if (r != null && canSimpleMatchTriple_dollarVars(a, b, c, r.triple)) out.add(modifyTripleRef(r, position)); return out; } public static boolean eitherIsB(Either e) { return e != null && e.isB(); } public static String href(String link, Object contents, Object... params) { if (link == null) return str(contents); return tag("a", contents, arrayPlus(params, "href", link)); } public static String smartBotRealURL() { return smartBotURL(); } public static String snippetImageLink(String snippetID) { return snippetImageURL(snippetID); } public static Object[] concatArrays(Object[]... arrays) { int l = 0; for (Object[] a : arrays) l += l(a); Object[] x = new Object[l]; int i = 0; for (Object[] a : arrays) if (a != null) { System.arraycopy(a, 0, x, i, l(a)); i += l(a); } return x; } public static String unicode_leftPointingTriangle() { return "?"; } public static float abs(float f) { return Math.abs(f); } public static int abs(int i) { return Math.abs(i); } public static double abs(double d) { return Math.abs(d); } public static int ai_shortenForIndex_maxLength = 100; public static String ai_shortenForIndex(String text) { return shorten(text, ai_shortenForIndex_maxLength); } public static boolean odd(int i) { return (i & 1) != 0; } public static boolean odd(long i) { return (i & 1) != 0; } public static boolean even(int i) { return (i & 1) == 0; } public static boolean even(long i) { return (i & 1) == 0; } public static FixedRateTimer doEvery_daemon(long delay, final Object r) { return doEvery_daemon(delay, delay, r); } public static FixedRateTimer doEvery_daemon(long delay, long firstDelay, final Object r) { FixedRateTimer timer = new FixedRateTimer(true); timer.scheduleAtFixedRate(smartTimerTask(r, timer, delay), firstDelay, delay); return timer; } public static FixedRateTimer doEvery_daemon(double delaySeconds, final Object r) { return doEvery_daemon(toMS(delaySeconds), r); } public static boolean isSystemThread(Thread t) { ThreadGroup g = t.getThreadGroup(); return g != null && g.getName().equals("system"); } public static void stopTimer(java.util.Timer timer) { if (timer != null) timer.cancel(); } public static ThreadLocal poorMansProfiling_renderFullResults_backwards = new ThreadLocal(); public static String poorMansProfiling_renderFullResults(final MultiSet traces) { int samples = poorMansProfiling_samples; boolean backwards = isTrue(getAndClearThreadLocal(poorMansProfiling_renderFullResults_backwards)); final int n = traces.size(); int percent = ratioToIntPercent(l(traces), samples); return (samples == 0 ? "Nothing sampled" : percent + "% core activity [" + n2(samples, "sample") + " taken]") + "\n\n" + joinMap(backwards ? traces.lowestFirst() : traces.highestFirst(), new F1() { public Object get(String trace) { try { return traces.get(trace) + "/" + n + "\n" + trace + "\n\n"; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "traces.get(trace) + \"/\" + n + \"\\n\" + trace + \"\\n\\n\""; } }); } public static List scoredSearch2(int maxResults, String query, Iterable data) { MultiMap map = new MultiMap(); List prepared = scoredSearch_prepare(query); for (String s : data) { int score = scoredSearch_score(s, prepared); if (score == 0) continue; map.put(score, s); } List out = new ArrayList(); for (int score : keysDesc(map)) { if (l(out) >= maxResults) break; int need = maxResults - l(out); out.addAll(takeFirst(need, map.get(score))); } return out; } public static IterableIterator allIndexedTerms_iterator() { return indexedTerms_iterator(); } public static List endingWithIC(Collection l, final String suffix) { List out = new ArrayList(); for (String s : unnull(l)) if (ewic(s, suffix)) out.add(s); return out; } public static List endingWithIC(String suffix, Collection l) { return endingWithIC(l, suffix); } public static List fullIndexedTerms() { return allIndexedFullTerms(); } public static List filterCic(Iterable l, String s) { List out = new ArrayList(); if (l != null) for (String x : l) if (cic(x, s)) out.add(x); return out; } public static IterableIterator ai_mainIndexKeysIterator() { return concurrentlyIterateSet(ai_mainIndexKeys()); } public static Collection ai_expandShortenedKeys_multi(Iterable l) { LinkedHashSet set = new LinkedHashSet(); if (l != null) for (String sKey : l) if (sKey != null) { if (!ai_possiblyShortenedKey(sKey)) set.add(sKey); else for (int position = 0; position < 3; position++) for (TripleRef r : unnull(tripleIndex().getTripleRefs(sKey, position))) if (r != null) set.add(r.get()); } return set; } public static IterableIterator concurrentlyIterateSet(final NavigableSet set) { final Object mutex = collectionMutex(set); class ConcurrentlyIterateSet extends F0 { public Iterator it = set.iterator(); public A key; public A get() { synchronized (mutex) { try { if (!it.hasNext()) return null; return key = it.next(); } catch (ConcurrentModificationException e) { print("Re-iterating"); it = set.tailSet(key, false).iterator(); if (!it.hasNext()) return null; return key = it.next(); } } } } return iteratorFromFunction(new ConcurrentlyIterateSet()); } public static NavigableSet indexedTermsStartingWith(String prefix) { return allIndexedTermsStartingWith(prefix); } public static List sortInPlace(List l, final Object comparator) { return sortedInPlace(l, comparator); } public static List sortInPlace(List l) { return sortedInPlace(l); } public static Comparator alternativeCIComparator_instance = new Comparator() { public int compare(String a, String b) { return compareIgnoreCase_keepSeparate(a, b); } }; public static Comparator alternativeCIComparator() { return alternativeCIComparator_instance; } public static NavigableSet ai_navigableShortTerms() { return ai_inTripleMode() ? tripleIndex().navigableMainKeys() : navigableKeys(cachedNodeIndex()); } public static String findInListIC(Collection l, String s) { for (String x : unnull(l)) if (eqic(x, s)) return x; return null; } public static Iterator iterator(Iterable c) { return c == null ? emptyIterator() : c.iterator(); } public static List sortedIgnoreCase(Collection c) { List l = cloneList(c); Collections.sort(l, caseInsensitiveComparator()); return l; } public static Object collectionMutex(Object o) { String c = className(o); if (eq(c, "java.util.TreeMap$KeySet")) c = className(o = getOpt(o, "m")); else if (eq(c, "java.util.HashMap$KeySet")) c = className(o = get_raw(o, "this$0")); if (eqOneOf(c, "java.util.TreeMap$AscendingSubMap", "java.util.TreeMap$DescendingSubMap")) c = className(o = get_raw(o, "m")); return o; } public static String formatGMTWithOptionalDate_24(long time) { SimpleDateFormat format = simpleDateFormat_GMT("yyyy/MM/dd"); String date = format.format(time); boolean needDate = neq(date, format.format(now())); return (needDate ? date + " " : "") + formatGMT_24(time); } public static String ai_html_encyclopediaTopic(WebNode n) { return ai_html_encyclopediaTopic(web_text(n)); } public static String ai_html_encyclopediaTopic(String topic) { return topic == null ? "" : html_encyclopediaTopic(topic); } public static String hcss(Object contents) { return htag("style", contents); } public static Set setMinusSet(Set l, Collection stuff) { if (empty(stuff)) return l; Set set = asSet(stuff); Set l2 = similarEmptySet(l); for (A a : l) if (!set.contains(a)) l2.add(a); return l2; } public static void replaceTokens(List tok, int i, int j, String s) { clearAllTokens(subList(tok, i, j)); tok.set(i, s); } public static void replaceTokens(List tok, String s) { clearAllTokens(tok); tok.set(0, s); } public static boolean isTag(String token, String tag) { return token.regionMatches(true, 0, "<" + tag + " ", 0, tag.length() + 2) || token.regionMatches(true, 0, "<" + tag + ">", 0, tag.length() + 2); } public static Map htmlParams(String tag) { return getHtmlTagParameters(tag); } public static String hopeningTag(String tag, Map params) { return hopeningTag(tag, mapToParams(params)); } public static String hopeningTag(String tag, Object... params) { StringBuilder buf = new StringBuilder(); buf.append("<" + tag); for (int i = 0; i < l(params); i += 2) { String name = (String) get(params, i); Object val = get(params, i + 1); if (nempty(name) && val != null) { String s = str(val); if (!empty(s)) buf.append(" " + name + "=" + htmlQuote(s)); } } buf.append(">"); return str(buf); } public static Object[] mapToParams(Map map) { return mapToObjectArray(map); } public static String getStackTrace(Throwable throwable) { lastException(throwable); return getStackTrace_noRecord(throwable); } public static String getStackTrace_noRecord(Throwable throwable) { StringWriter writer = new StringWriter(); throwable.printStackTrace(new PrintWriter(writer)); return hideCredentials(writer.toString()); } public static String getStackTrace() { return getStackTrace_noRecord(new Throwable()); } public static Web webWithoutIndexAndLock() { Web web = simpleWeb(); return web; } public static WebNode web_setPosition(WebNode node, double x, double y) { node.x = x; node.y = y; return node; } public static Web web_intern(Web web) { if (web == null) return null; for (WebNode n : web_nodesAndRelations(web)) web_setLabels_forced(n, map_lisp_intern(n.labels())); web.source = intern(web.source); web.title = intern(web.title); trimToSize(web.nodes); for (WebNode n : web.nodes) if (n.labels instanceof List) trimToSize((List) n.labels); if (web.globalID instanceof String) web.globalID = new GlobalID((String) web.globalID); return web; } public static void incIntThreadLocal(ThreadLocal tl) { tl.set(toInt(tl.get()) + 1); } public static void incIntThreadLocal(BetterThreadLocal tl) { tl.set(toInt(tl.get()) + 1); } public static Object[] paramsPlus(Object[] a1, Object... a2) { if (l(a2) == 2 && a2[0] == null) return a1; return concatArrays(assertEvenLength(a1), assertEvenLength(a2)); } public static String webToStringShort(Web web) { try { if (web == null) return "-"; int n = l(web_nodes(web)); if (n == 0) return "Empty web"; WebNode a = web_firstNode(web); if (n == 1) return join(" = ", web_texts(a)); if (n == 2) { WebNode b = web_secondNode(web); return ai_renderTriple(web_text(a), web_text(web_getRelation(a, b)), web_text(b)); } return "Includes " + joinWithComma(map("web_text", web_nodes(web))); } catch (Throwable e) { _handleException(e); return "Error"; } } public static void pcallFAll(Collection l, Object... args) { if (l != null) for (Object f : cloneList(l)) pcallF(f, args); } public static A nu(Class c, Object... values) { A a = nuObject(c); setAll(a, values); return a; } public static String _computerID; public static String computerID() { Lock __2133 = programLock(); lock(__2133); try { if (_computerID == null) { File file = computerIDFile(); _computerID = loadTextFile(file.getPath()); if (_computerID == null) { _computerID = loadTextFile(userDir(".tinybrain/computer-id")); if (_computerID == null) _computerID = makeRandomID(12); saveTextFile(file, _computerID); } } return _computerID; } finally { unlock(__2133); } } public static void markWebsPosted_createMarker() { markWebsPosted_createMarker(programID()); } public static void markWebsPosted_createMarker(String progID) { createMarkerFile("#1007609", "webs.posted.at." + psI(progID)); } public static boolean ai_is(String a, String b) { return ai_hasTriple(a, "is", b); } public static String standardCredentials() { String user = standardCredentialsUser(); String pass = standardCredentialsPass(); if (nempty(user) && nempty(pass)) return "&_user=" + urlencode(user) + "&_pass=" + urlencode(pass); return ""; } public static Matcher regexp(String pat, String s) { return Pattern.compile(pat).matcher(s); } public static void ai_speculateDeep(final String word) { ai_clearTriggeredSpeculations(); ai_speculate(word); for (String s : ai_triggeredSpeculations()) { print("deep spec " + word + " -> " + s); ai_speculate(s); } } public static String ai_activeSpec(final String word) { if (empty(word)) return word; ai_withMaker("ai_activeSpec", new Runnable() { public void run() { try { try { ping(); ai_activeSpec_answerOnChat(word); } catch (Throwable __e) { _handleException(__e); } try { ping(); ai_activeSpec_answerChatLine(word); } catch (Throwable __e) { _handleException(__e); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pcall ping { ai_activeSpec_answerOnChat(word); }\r\n pcall ping { ai_activeS..."; } }); return word; } public static Web ai_blob(String word, boolean verified) { return null; } public static Cache> cachedNodeIndex2_cache = new Cache("cachedNodeIndex2_make"); public static boolean cachedNodeIndex_autoMake = true; public static boolean cachedNodeIndex2_monitorCircleEditor; public static boolean cachedNodeIndex2_first = true; public static FileStatus cachedNodeIndex2_status; public static MultiMap cachedNodeIndex2() { if (cachedNodeIndex2_first) { cachedNodeIndex2_first = false; onWebsChanged("cachedNodeIndex2_clear"); onTransientWebAdded_do("cachedNodeIndex2_onNewWeb"); onTransientWebRemoved_do("cachedNodeIndex2_onWebRemoved"); setAdd(postSoftwareMadeWeb_onNewWeb, "cachedNodeIndex2_onNewWeb"); } if (cachedNodeIndex2_monitorCircleEditor) { FileStatus status = conceptsFileStatus(circlesEditorDBID()); if (neq(cachedNodeIndex2_status, status)) { cachedNodeIndex2_status = status; cachedNodeIndex2_cache.clear(); } } return cachedNodeIndex2_cache.get(); } public static void cachedNodeIndex2_clear() { cachedNodeIndex2_cache.clear(); } public static boolean cachedNodeIndex2_onNewWeb(Web web, Object[] params) { return cachedNodeIndex2_onNewWeb(web); } public static boolean cachedNodeIndex2_onNewWeb(Web web) { if (!cachedNodeIndex_autoMake) return false; Lock __2152 = cachedNodeIndex2_cache.lock; lock(__2152); try { ai_addWebToIndex(cachedNodeIndex2_cache.get(), web); cachedNodeIndex2_cache.changeCount++; return false; } finally { unlock(__2152); } } public static void cachedNodeIndex2_onWebRemoved(Web web) { if (!cachedNodeIndex_autoMake) return; Lock __2153 = cachedNodeIndex2_cache.lock; lock(__2153); try { print("Incremental removal"); ai_removeWebFromIndex(cachedNodeIndex2_cache.get(), web); cachedNodeIndex2_cache.changeCount++; } finally { unlock(__2153); } } public static long cachedNodeIndex_changeCount() { return cachedNodeIndex2_cache.changeCount; } public static MultiMap cachedNodeIndex2_make() { if (cachedNodeIndex_autoMake) { print("Making index"); return dwlw_fullNodeIndex_ci(); } else { return ai_emptyWebIndex(); } } public static void ai_setSubSpace(AI_SubSpace subSpace) { ai_currentSubSpace.set(subSpace); ai_setCacheChecker(subSpace); ai_setPostHandler(subSpace); } public static ThreadLocal ai_currentSubSpace = new ThreadLocal(); public static AI_SubSpace ai_currentSubSpace() { return ai_currentSubSpace.get(); } public static boolean ai_inTripleMode() { return !cachedNodeIndex_autoMake; } public static ThreadLocal currentThoughtSpace_value = new ThreadLocal() { public AbstractThoughtSpace initialValue() { return new GlobalThoughtSpace(); } }; public static AbstractThoughtSpace currentThoughtSpace() { return currentThoughtSpace_value.get(); } public static volatile boolean ai_markMemoryDirty_mark; public static void ai_markMemoryDirty() { ai_markMemoryDirty_mark = true; } public static boolean ai_isMemoryDirty() { return ai_markMemoryDirty_mark; } public static Class _getClass(String name) { try { return Class.forName(name); } catch (ClassNotFoundException e) { return null; } } public static Class _getClass(Object o) { return o == null ? null : o instanceof Class ? (Class) o : o.getClass(); } public static Class _getClass(Object realm, String name) { try { return getClass(realm).getClassLoader().loadClass(classNameToVM(name)); } catch (Exception __e) { throw rethrow(__e); } } public static List onlyClassNamesThatExist(List l) { List out = new ArrayList(); for (String s : l) if (hasClass(s)) out.add(s); return out; } public static List identifiersOnly(Collection l) { List out = new ArrayList(); for (String a : unnull(l)) if (isIdentifier(a)) out.add(a); return out; } public static List cloneAndClear(List l) { return cloneAndClearList(l); } public static List onTransientWebRemoved_list = synchroList(); public static void onTransientWebRemoved_do(Object f) { setAdd(onTransientWebRemoved_list, f); } public static void onTransientWebRemoved(Web web) { pcallFAll(onTransientWebRemoved_list, web); } public static double stefansChat_onLine_safetyDelay = 1; public static double stefansChat_onLine_shortSafetyDelay = 0.1; public static double stefansChat_onLine_errorDelay = 10; public static int stefansChat_onLine_lookback = 0; public static int stefansChat_onLine_firstRead = -1; public static ThreadLocal stefansChat_onLine_fullParams = new ThreadLocal(); public static ThreadLocal stefansChat_onLine_onHistoryRead = new ThreadLocal(); public static void stefansChat_onLine(int lookback, Object f) { stefansChat_onLine_lookback = lookback; stefansChat_onLine(f); } public static void stefansChat_onLine(final Object f) { final Object hr = getAndClearThreadLocal(stefansChat_onLine_onHistoryRead); { Thread _t_0 = new Thread("Stefan's Chat") { public void run() { try { int a = stefansChat_onLine_firstRead = max(0, stefansChat_n() - stefansChat_onLine_lookback); boolean first = true; while (licensed()) { try { String s = loadPageWithTimeout("http://ai1.space/1008998/raw/incremental?json=1&a=" + a, 120 * 1000); if (empty(trim(s))) { sleepSeconds(stefansChat_onLine_safetyDelay); continue; } try { Map map = safeUnstructMap(s); int n = getInt(map, "n"); List msgs = (List) (get(map, "msgs")); for (Object msg : msgs) try { stefansChat_onLine_fullParams.set(mapPlus((Map) msg, "chatName", "Stefan's Chat")); pcallF(f, getString(msg, "text")); } finally { stefansChat_onLine_fullParams.set(null); } sleepSeconds(stefansChat_onLine_shortSafetyDelay); a = n; } catch (Throwable e) { printException(e); sleepSeconds(stefansChat_onLine_errorDelay); } if (first) { pcallF(hr); first = false; } } catch (Throwable __e) { _handleException(__e); } } } catch (Throwable __e) { _handleException(__e); } } }; startThread(_t_0); } } public static double smartBotsChat_onLine_safetyDelay = 1; public static double smartBotsChat_onLine_shortSafetyDelay = 0.1; public static double smartBotsChat_onLine_errorDelay = 10; public static int smartBotsChat_onLine_lookback = 0; public static int smartBotsChat_onLine_firstRead = -1; public static ThreadLocal smartBotsChat_onLine_onHistoryRead = new ThreadLocal(); public static void smartBotsChat_onLine(int lookback, Object f) { smartBotsChat_onLine_lookback = lookback; smartBotsChat_onLine(f); } public static void smartBotsChat_onLine(final Object f) { final Object hr = getAndClearThreadLocal(smartBotsChat_onLine_onHistoryRead); { Thread _t_0 = new Thread("Smart Bot's Chat") { public void run() { try { int a = smartBotsChat_onLine_firstRead = max(0, smartBotsChat_n() - smartBotsChat_onLine_lookback); boolean first = true; while (licensed()) { try { String s = loadPageWithTimeout("http://ai1.space/1011975/raw/incremental?json=1&a=" + a, 120 * 1000); if (empty(trim(s))) { sleepSeconds(smartBotsChat_onLine_safetyDelay); continue; } try { Map map = safeUnstructMap(s); int n = getInt(map, "n"); List msgs = (List) (get(map, "msgs")); for (Object msg : msgs) try { stefansChat_onLine_fullParams.set(mapPlus((Map) msg, "chatName", "Smart Bot's Chat")); pcallF(f, getString(msg, "text")); } finally { stefansChat_onLine_fullParams.set(null); } sleepSeconds(smartBotsChat_onLine_shortSafetyDelay); a = n; } catch (Throwable e) { printException(e); sleepSeconds(smartBotsChat_onLine_errorDelay); } if (first) { pcallF(hr); first = false; } } catch (Throwable __e) { _handleException(__e); } } } catch (Throwable __e) { _handleException(__e); } } }; startThread(_t_0); } } public static String makePostData(Map map) { List l = new ArrayList(); for (Map.Entry e : map.entrySet()) { String key = (String) (e.getKey()); Object val = e.getValue(); if (val != null) { String value = str(val); l.add(urlencode(key) + "=" + urlencode((value))); } } return join("&", l); } public static String makePostData(Object... params) { return makePostData(litorderedmap(params)); } public static ThreadLocal doPost_silently = new ThreadLocal(); public static ThreadLocal doPost_timeout = new ThreadLocal(); public static String doPost(Map urlParameters, String url) { return doPost(makePostData(urlParameters), url); } public static String doPost(String urlParameters, String url) { try { URL _url = new URL(url); ping(); return doPost(urlParameters, _url.openConnection(), _url); } catch (Exception __e) { throw rethrow(__e); } } public static String doPost(String urlParameters, URLConnection conn, URL url) { try { boolean silently = isTrue(optParam(doPost_silently)); Long timeout = optParam(doPost_timeout); setHeaders(conn); int l = lUtf8(urlParameters); if (!silently) print("Sending POST request: " + hideCredentials(url) + " (" + l + " bytes)"); if (timeout != null) setURLConnectionTimeouts(conn, timeout); ((HttpURLConnection) conn).setRequestMethod("POST"); conn.setDoOutput(true); conn.setRequestProperty("Content-Length", str(l)); OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); writer.write(urlParameters); writer.flush(); loadPage_charset.set("UTF-8"); String contents = loadPage(conn, url, false); writer.close(); return contents; } catch (Exception __e) { throw rethrow(__e); } } public static boolean isInteger(String s) { if (s == null) return false; int n = l(s); if (n == 0) return false; int i = 0; if (s.charAt(0) == '-') if (++i >= n) return false; while (i < n) { char c = s.charAt(i); if (c < '0' || c > '9') return false; ++i; } return true; } public static String snippetLink(String id) { return "http://tinybrain.de/" + parseSnippetID(id); } public static boolean cic(Collection l, String s) { return containsIgnoreCase(l, s); } public static boolean cic(String[] l, String s) { return containsIgnoreCase(l, s); } public static boolean cic(String s, char c) { return containsIgnoreCase(s, c); } public static boolean cic(String a, String b) { return containsIgnoreCase(a, b); } public static void assertFalse(Object o) { if (!(eq(o, false))) throw fail(str(o)); } public static boolean assertFalse(boolean b) { if (b) throw fail("oops"); return b; } public static boolean assertFalse(String msg, boolean b) { if (b) throw fail(msg); return b; } public static int isAndroid_flag; public static boolean isAndroid() { if (isAndroid_flag == 0) isAndroid_flag = System.getProperty("java.vendor").toLowerCase().indexOf("android") >= 0 ? 1 : -1; return isAndroid_flag > 0; } public static Object hotwire_onCompile; public static boolean hotwire_serially; public static Lock hotwire_overInternalBot_lock = lock(); public static Class hotwire_overInternalBot(String progID) { try { Pair p = CompilerBot.compileSnippet2(progID); Lock __2194 = hotwire_serially ? hotwire_overInternalBot_lock : null; lock(__2194); try { callF(hotwire_onCompile, p); File jar = p.a; assertTrue(jar.getAbsolutePath(), jar.isFile()); List files = ll(jar); String dehlibs; try { dehlibs = unnull(loadTextFileFromZip(jar, "libraries")); } catch (Throwable e) { jar.delete(); throw rethrow(e); } Matcher matcher = Pattern.compile("\\d+").matcher(dehlibs); while (matcher.find()) { String libID = matcher.group(); files.add(loadLibrary(libID)); } JavaXClassLoader classLoader = hotwire_makeClassLoader(files); classLoader.progID = progID; return hotwire_finish(classLoader, progID, p.b); } finally { unlock(__2194); } } catch (Exception __e) { throw rethrow(__e); } } public static A popFirst(List l) { if (empty(l)) return null; A a = first(l); l.remove(0); return a; } public static Map objectToMap(Object o) { try { if (o instanceof Map) return (Map) o; TreeMap map = new TreeMap(); Class c = o.getClass(); while (c != Object.class) { Field[] fields = c.getDeclaredFields(); for (final Field field : fields) { if ((field.getModifiers() & Modifier.STATIC) != 0) continue; field.setAccessible(true); final Object value = field.get(o); if (value != null) map.put(field.getName(), value); } c = c.getSuperclass(); } if (o instanceof DynamicObject) map.putAll(((DynamicObject) o).fieldValues); return map; } catch (Exception __e) { throw rethrow(__e); } } public static List> objectToMap(Collection l) { List x = new ArrayList(); for (Object o : l) x.add(objectToMap(o)); return x; } public static String[] asStringArray(Collection c) { return toStringArray(c); } public static String[] asStringArray(Object o) { return toStringArray(o); } public static List allFieldObjects(Object o) { List fields = new ArrayList(); Class _c = _getClass(o); do { for (Field f : _c.getDeclaredFields()) { f.setAccessible(true); fields.add(f); } _c = _c.getSuperclass(); } while (_c != null); return fields; } public static List addDyn(List l, A a) { if (l == null) l = new ArrayList(); l.add(a); return l; } public static File javaxCodeDir_dir; public static File javaxCodeDir() { return javaxCodeDir_dir != null ? javaxCodeDir_dir : new File(userHome(), "JavaX-Code"); } public static List allCacheVars() { return allCacheVars(mc()); } public static List allCacheVars(Object program) { return endingWith("_cache", allFields(program)); } public static A lookupIgnoreCase(Map map, String key) { for (String s : map.keySet()) if (eqic(s, key)) return map.get(s); return null; } public static List lookupIgnoreCase(MultiMap map, String key) { for (String s : map.keySet()) if (eqic(s, key)) return map.get(s); return litlist(); } public static String sendToLocalBot(String bot, String text, Object... args) { text = format3(text, args); DialogIO channel = findBot(bot); if (channel == null) throw fail(quote(bot) + " not found"); try { channel.readLine(); print(bot + "> " + shorten(text, 80)); channel.sendLine(text); String s = channel.readLine(); print(bot + "< " + shorten(s, 80)); return s; } catch (Throwable e) { e.printStackTrace(); return null; } finally { channel.close(); } } public static String sendToLocalBot(int port, String text, Object... args) { text = format3(text, args); DialogIO channel = talkTo(port); try { channel.readLine(); print(port + "> " + shorten(text, 80)); channel.sendLine(text); String s = channel.readLine(); print(port + "< " + shorten(s, 80)); return s; } catch (Throwable e) { e.printStackTrace(); return null; } finally { if (channel != null) channel.close(); } } public static double toM_double(long l) { return l / (1024 * 1024.0); } public static String _userHome; public static String userHome() { if (_userHome == null) { if (isAndroid()) _userHome = "/storage/sdcard0/"; else _userHome = System.getProperty("user.home"); } return _userHome; } public static File userHome(String path) { return new File(userDir(), path); } public static ThreadLocal> ai_getVar_map = new ThreadLocal(); public static Object ai_getVar(String name) { return mapGet(ai_getVar_map.get(), name); } public static String myJavaSource_code; public static String myJavaSource() { return myJavaSource_code; } public static Lock appendToFile_lock = lock(); public static boolean appendToFile_keepOpen; public static HashMap appendToFile_writers = new HashMap(); public static void appendToFile(String path, String s) { try { Lock __2224 = appendToFile_lock; lock(__2224); try { mkdirsForFile(new File(path)); path = getCanonicalPath(path); Writer writer = appendToFile_writers.get(path); if (writer == null) { writer = new BufferedWriter(new OutputStreamWriter(newFileOutputStream(path, true), "UTF-8")); if (appendToFile_keepOpen) appendToFile_writers.put(path, writer); } writer.write(s); if (!appendToFile_keepOpen) writer.close(); } finally { unlock(__2224); } } catch (Exception __e) { throw rethrow(__e); } } public static void appendToFile(File path, String s) { if (path != null) appendToFile(path.getPath(), s); } public static void cleanMeUp_appendToFile() { Lock __2225 = appendToFile_lock; lock(__2225); try { closeAllWriters(values(appendToFile_writers)); appendToFile_writers.clear(); } finally { unlock(__2225); } } public static void ai_spec_webIsTriple(String globalID) { ai_spec_webIsTriple(ai_getWebFromTerm(globalID)); } public static void ai_spec_webIsTriple(Web web) { if (web == null) return; ai_postTriple(web.globalID(), "is a", "web"); ai_postTriple(web.globalID(), "see:", "Web " + web.globalID); T3 t = webToTriple(web); if (t != null) ai_postTriple("Web " + web.globalID, "is triple", ai_renderTriple(t)); else ai_postTriple("Web " + web.globalID, "is not a", "triple"); } public static void ai_spec_singularPlural(String s) { if (is(s, "singular") || is(s, "plural")) return; if (!endsWithLetter(s)) return; if (ewic(s, "s")) { post(s, "possibly is", "plural"); post(s, "possibly is plural of", dropLast(s)); } else post(s, "possibly is", "singular"); } public static void ai_spec_giveMe(String chatLine) { String s = ai_textOfChatLine(chatLine); { if (s == null) return; } { if (ai_hasAny("$X", "answers", chatLine)) return; } Matches m = new Matches(); if (swic(s, "give me ", m) && swicOneOf(m.rest(), "a ", "an ")) post(random(ai_texts("$X", "is", m.rest())), "answers", chatLine); } public static List ai_spec_parseAnd_words = ll("and", "of", "or", "for"); public static void ai_spec_parseAnd(String s) { List tok = tok_combineCurlyBrackets_keep(javaTok(s)); for (String word : ai_spec_parseAnd_words) { int i = indexOfIC(tok, word); if (i < 3 || i >= l(tok) - 2) continue; T3 t = tok_tripleSplitAtToken(tok, i); ai_postTriple(s, "possibly parses as", optionalCurlyBrace(dropSuffix(",", t.a)) + " " + t.b + " " + optionalCurlyBrace(t.c)); } } public static void ai_spec_tripelize(String s) { if (tok_containsSimpleArrow(tok_combineCurlyBrackets(javaTok(s)))) { return; } Matches m = new Matches(); T3 t = ai_tripelize(s); if (!ai_trivialTriple(t)) ai_postTriple(s, "possibly parses to triple", ai_renderTriple(t)); } public static void ai_spec_parseArrowTriple(String s) { Matches m = new Matches(); T3 t = ai_parseArrowTriple(s); if (t == null) return; ai_postTriple(s, "is a triple with left part", t.a); ai_postTriple(s, "is a triple with middle part", t.b); ai_postTriple(s, "is a triple with right part", t.c); } public static void ai_spec_webContents(String s) { Matches m = new Matches(); if (!swic_trim(s, "Web ", m)) return; Web web = ai_getWeb(m.rest()); if (web == null) return; ai_postTriple(s, "is", "a web"); T3 t = ai_webToTriple(web); if (t != null) ai_postTriple(s, "is triple", ai_renderTriple(t)); else for (WebNode node : web_nodesAndRelations(web)) ai_postTriple(s, node instanceof WebRelation ? "contains relation" : "contains node", web_text(node)); } public static void ai_spec_addQuestionsToChatLine(String line) { String text = ai_textOfChatLine(line); if (empty(text)) return; if (containsWord(text, "it")) { ai_postTriple(line, "begs the question", quote("What is \"it\"?")); ai_postTriple(line, "see:", "The \"it\" in " + line); } } public static void ai_spec_simplify(String s) { String x = ai_simplifyNP(s); if (nempty(x) && neqic(s, x)) post(s, "possibly simplifies to", x); } public static void ai_spec_containsThePhrase(String s) { for (String phrase : allCurlyBracketPhrases(unquote(s))) ai_postTriple(s, "contains the phrase", phrase); } public static void ai_spec_executeBestSplit(String s) { if (!isNormalQuoted(s)) return; int idx = ai_intOrMinus1(s, "is best split at index", "$X"); if (idx < 0) return; post(s, "best-splits to", sfu(splitStringAtIndices(unquote(s), ll(idx)))); } public static void ai_spec_parseIf(String s) { Matches m = new Matches(); if (swic_trim(s, "if ", m) && l(javaTokC(m.rest())) > 1 && !isCurlyBraced(m.rest())) ai_postTriple(s, "possibly parses as", takeFirst(3, s) + curlyBrace(m.rest())); } public static void ai_spec_subclass(String s) { Matches m = new Matches(); if (ai_isAOrAn(s, m)) { String x = m.rest(); for (String type : ai_texts(s, "is a", "$X")) post(x, "is a subclass of", type); } } public static void ai_spec_isAQuestion(String _s) { String s = unquote(_s); if (ai_isQuestion_1(s) || endsWith(s, "?")) ai_postTriple(_s, "is", "a question"); } public static void ai_spec_substWebs(String word) { Matches m = new Matches(); if (swic_trim(word, "a ", m)) ai_subst_web_charBasedReplace("pypazhgrxubdmsqw", m.rest()); } public static void ai_spec_apostrophS(String s) { int i = indexOf(s, "'s"); if (i < 1) return; if (i + 2 >= l(s) || !isLetter(s.charAt(i + 2))) ai_postTriple(s, "possibly related to:", takeFirst(i, s)); } public static void ai_spec_numbered(String s) { List l = javaTokC(s); if (l(l) > 1 && isInteger(last(l))) { String type = a(dropLastJavaToken(s)); if (!has(s, "is", type)) post(s, "probably is", type); } } public static void ai_spec_howMany(String question) { String bla = web_text(first(ai_search_dollarX(question, "counts instances of", "$X"))); if (bla == null) return; print("bla=" + quote(bla)); int count = l(ai_search_dollarX("$X", "is", bla)) + l(ai_search_dollarX("$X", "probably is", bla)); ai_postTriple(str(count), "answers", question); } public static void ai_spec_analyzeLitList(String s) { if (!startsWithAndEndsWith(s, "[", "]")) return; List l = (List) (safeUnstructure_nullAndPrintShortOnError(s)); if (l == null) return; post(s, "is a list with length", l(l)); for (int i = 0; i < l(l); i++) post(structure(l.get(i)), "is element " + (i + 1) + " in", s); } public static void ai_spec_doYouThink(String s) { Matches m = new Matches(); if (learnedFlexMatch("do you think *", s, m) || learnedFlexMatch("Is it true that *", s, m)) ai_postTriple(s, "asks about fact:", dropPunctuationAtEnd(m.rest())); } public static void ai_spec_unquote(String s) { if (isProperlyQuoted(s)) ai_postTriple(s, "unquotes to:", unquote(s)); } public static void ai_spec_quoteUnlessQuoted(String s) { if (!isQuoted(s)) ai_postTriple(s, "quotes to:", quote(s)); } public static void ai_spec_checkFact(String question) { for (String fact : ai_texts(question, "asks about fact:", "$X")) { if (ai_hasTriple_vary(fact, "is", "never a certain fact")) ai_postTriple("You can never be sure about that", "answers", question); if (ai_hasTriple_vary(fact, "is", "probably not true")) ai_postTriple("Probably not", "answers", question); } } public static void ai_spec_switcheroo(String s) { boolean q = isQuoted(s); String x = switcheroo(unquote(s)); if (q) x = quote(x); if (neq(x, s)) ai_postTriple(s, "switcheroo", x); } public static void ai_spec_groupingPrefix(String s) { ai_spec_groupingPrefix(s, ai_groupingPrefixes()); } public static void ai_spec_groupingPrefix(String s, Set prefixes) { Matches m = new Matches(); for (String prefix : prefixes) if (swic(s, prefix + " ", m)) ai_postTriple(s, "groups to", prefix + " " + curlyBraces(m.rest())); } public static void ai_spec_compareNumbers(String term) { for (Web web : indexedWebs(term)) { ping(); ai_spec_compareNumbers(web); } } public static void ai_spec_compareNumbers(Web web) { for (WebRelation r : web_relationsNamed(web, "is bigger than?")) { String a = web_text(r.a), b = web_text(r.b); if (isInteger(a) && isInteger(b)) { int cmp = bigint(a).compareTo(bigint(b)); String rel = cmp > 0 ? "is bigger than" : "is not bigger than"; ai_postTriple(a, rel, b); } } } public static void ai_spec_compareNumbers_triple(String s) { T3 t = ai_parseTriple(s); if (t == null || !eqic(t.b, "is bigger than?")) return; String a = t.a, b = t.c; if (isInteger(a) && isInteger(b)) { int cmp = bigint(a).compareTo(bigint(b)); String rel = cmp > 0 ? "is bigger than" : "is not bigger than"; ai_postTriple(a, rel, b); } } public static void ai_spec_answerYesNoTriple(String s) { T3 t = ai_parseTriple(s); { if (!(t != null && endsWith(t.b, "?"))) return; } String rel = dropLastTrim(t.b); if (ai_hasTriple(t.a, rel, t.c)) { ai_postTriple("Yes", "answers triple", s); return; } String neg = ai_text("$X", "is negated relation of", rel); if (ai_hasTriple(t.a, neg, t.c)) { ai_postTriple("No", "answers triple", s); return; } } public static void ai_spec_moveToMiddle_triple(String term) { T3 t = ai_parseTriple(term); if (t != null) ai_spec_moveToMiddle_triple(term, t, ai_spec_moveToMiddle_mtmRels()); } public static void ai_spec_moveToMiddle_triple(String term, T3 t, List mtmRels) { if (t == null) return; String rel = t.b, b = t.c; for (WebRelation mtm : mtmRels) { List l = splitAtSlash_keepAll(web_text(mtm.a)); List lReplacement = splitAtSlash_keepAll(web_text(mtm.b)); { if (!(l(l) == 2 && l(lReplacement) == 2)) continue; } Matches m = new Matches(); String prefix = second(l) + " "; { if (!(eqic(first(l), rel) && swic(b, prefix, m))) continue; } ai_postTriple(term, "is identical to", ai_renderTriple(t.a, first(lReplacement), second(lReplacement) + " " + m.rest())); } } public static void ai_spec_searchForAnswer(String s) { Set cluster = litciset(s); cluster = ai_cluster_forward(cluster, "parses to triple"); if (empty(cluster)) return; ai_expandCluster(cluster); cluster = ai_cluster_backward(cluster, "answers triple"); if (empty(cluster)) return; ai_postTriple_multi(cluster, "answers", s); } public static void ai_specTemp_length(String s) { post(s, "has length", ai_text(s, "is a list with length", "$X")); } public static void ai_spec_whatIsXInY(String s) { Matches m = new Matches(); if (!flexMatchIC("What is * (in|of) *", unquote(s), m)) return; String elem = m.get(0), thing = m.get(2); post(s, "possibly related to:", elem); post(s, "possibly related to:", thing); ai_triggerSpeculationOn(thing); for (String text : takeFirst(ai_spec_maxAnswersToReturn(), ai_texts("$X", "is " + elem + " in", thing))) post(quote(text), "answers", s); } public static void ai_spec_whatIsTheLength(String s) { Matches m = new Matches(); if (!swic(unquote(s), "how long is ", m)) return; String postUnder = optQuote(s); String thing = m.get(0); print("ai_spec_whatIsTheLength thing: " + thing); post(postUnder, "possibly related to:", thing); ai_triggerSpeculationOn(thing); post(ai_forward(thing, "has length"), "answers", postUnder); } public static List> hotwire_classes = synchroList(); public static Class hotwireDependent(String src) { Class c = hotwire(src); makeDependent(c); return c; } public static boolean isThreadRunnable_x(Thread t, StackTraceElement[] trace) { if (t.getState() != Thread.State.RUNNABLE) return false; StackTraceElement e = first(trace); if (e == null) return false; String s = e.getClassName() + "." + e.getMethodName(); return !eqOneOf(s, "java.net.Inet6AddressImpl.lookupAllHostAddr", "java.io.FileInputStream.readBytes", "jdk.internal.misc.Unsafe.park", "sun.misc.Unsafe.park", "java.net.SocketInputStream.socketRead0", "java.net.PlainSocketImpl.socketConnect", "java.net.PlainSocketImpl.socketAccept", "sun.awt.X11.XToolkit.waitForEvents", "java.net.DualStackPlainSocketImpl.accept0"); } public static String getStackTrace2(Throwable e) { return hideCredentials(getStackTrace(unwrapTrivialExceptionWraps(e)) + replacePrefix("java.lang.RuntimeException: ", "FAIL: ", hideCredentials(str(getInnerException(e)))) + "\n"); } public static void clear(Collection c) { if (c != null) c.clear(); } public static void put(Map map, A a, B b) { if (map != null) map.put(a, b); } public static List _registerDangerousWeakMap_preList; public static A _registerDangerousWeakMap(A map) { return _registerDangerousWeakMap(map, null); } public static A _registerDangerousWeakMap(A map, Object init) { callF(init, map); if (init instanceof String) { final String f = (String) (init); init = new VF1() { public void get(Map map) { try { callMC(f, map); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callMC(f, map)"; } }; } if (javax() == null) { if (_registerDangerousWeakMap_preList == null) _registerDangerousWeakMap_preList = synchroList(); _registerDangerousWeakMap_preList.add(pair(map, init)); return map; } call(javax(), "_registerDangerousWeakMap", map, init); return map; } public static void _onLoad_registerDangerousWeakMap() { assertNotNull(javax()); if (_registerDangerousWeakMap_preList == null) return; for (Pair p : _registerDangerousWeakMap_preList) _registerDangerousWeakMap(p.a, p.b); _registerDangerousWeakMap_preList = null; } public static boolean isSubtypeOf(Class a, Class b) { return b.isAssignableFrom(a); } public static List mapMapToList(Map map, Object f) { return map(map, f); } public static List mapMapToList(Object f, Map map) { return map(f, map); } public static List dropHTMLComments(List tok) { List l = new ArrayList(); for (int idx = 0; idx < l(tok); idx++) { String t = tok.get(idx); if (even(idx)) { int i; while ((i = t.indexOf("", i + 4); t = substring(t, 0, i) + substring(t, j + 3); } } l.add(t); } return l; } public static boolean web_hasLabel(WebNode node, String text) { return web_hasLabelIC(node, text); } public static boolean web_hasLabel(Pair p, String text) { return web_hasLabelIC(p, text); } public static boolean web_hasLabel(WebNode a, WebNode b, String text) { return web_hasLabelIC(a, b, text); } public static volatile List printDeadlocks_lastDeadlock; public static List printDeadlocks() { ThreadMXBean bean = ManagementFactory.getThreadMXBean(); long[] ids = bean.findDeadlockedThreads(); if (empty(ids)) return emptyList(); printAsciiHeading("DEADLOCKED THREADS!"); ThreadInfo[] threadInfo = bean.getThreadInfo(ids); print(deadlockedThreadsToString(asList(threadInfo))); return printDeadlocks_lastDeadlock = asList(threadInfo); } public static void interruptDeadlockedThreads(List lockingThreads) { if (empty(lockingThreads)) return; List threads = threadInfosToThreads(lockingThreads); if (empty(threads)) return; Thread t = random(threads); print("Interrupting deadlocked thread " + quote(t.getName()) + " (out of " + l(lockingThreads) + ")"); t.interrupt(); } public static Map caseInsensitiveSynchroMap() { return (Map) synchroMap(caseInsensitiveMap()); } public static CirclesAndLines cal_unstructure(String s) { return (CirclesAndLines) unstructure(s); } public static List ai_parseList(String s) { return splitAtJavaToken(s, ","); } public static String postDerivedFromWeb(Web web, String a, String b, String c) { try { AutoCloseable __2324 = temp_ai_setMaker(web.source); try { AutoCloseable __2325 = tempSetThreadLocal(ai_postTriple_verified, web.verified()); try { return post(a, b, c); } finally { _close(__2325); } } finally { _close(__2324); } } catch (Exception __e) { throw rethrow(__e); } } public static ThreadLocal html_pretendAuthed = new ThreadLocal(); public static boolean html_pretendAuthed() { return isTrue(html_pretendAuthed.get()) || webAuthed(); } public static String ai_html_linkedWeb_icon(String globalID) { return ahref(ai_html_webLink(globalID), hsnippetimg("#1011882")); } public static T3 ai_webToTriple(Web web) { if (web == null) return null; Collection l = web_relationObjects(web); if (l(l) != 1) return null; WebRelation r = first(l); if (!web_tripelizableNode(r.a) || !web_tripelizableNode(r.b) || !web_tripelizableNode(r)) return null; return t3(web_sym(r.a), web_sym(r), web_sym(r.b)); } public static String html_smartBotSendInput(String msg, Object contents, Object... params) { return webAuthed() ? html_smartBotsChatPostLink(msg, contents, params) : ahref(smartBot_sendInputLink(msg), contents, params); } public static String smartBot_storeTripleCmd(String a, String b, String c) { return "!store-triple " + ai_renderTriple(a, b, c); } public static String ai_html_thumbUp() { return hsnippetimg("#1000223"); } public static String ai_html_webThumbUpLink(String webID) { String imgUp = ai_html_thumbUp(); String textUp = "Web " + webID + " is correct"; return html_smartBotSendInput(textUp, imgUp); } public static String ai_html_webThumbDownLink(String webID) { String imgDown = hsnippetimg("#1000224"); String textDown = "Web " + webID + " is wrong"; return html_smartBotSendInput(textDown, imgDown); } public static boolean isCurlyBraced(String s) { List tok = tok_combineCurlyBrackets_keep(javaTok(s)); return l(tok) == 3 && startsWithAndEndsWith(tok.get(1), "{", "}"); } public static List dropFirstThreeAndLastThree(List l) { return dropFirstAndLast(3, l); } public static String ai_getIndexedTerm(String s) { String key = first(indexedTermsStartingWith(s)); if (key == null) return null; for (String term : ai_expandShortenedKey(key)) if (eqic(term, s)) return term; return null; } public static Collection fullTermsStartingWith(String prefix) { return fullIndexedTermsStartingWith(prefix); } public static boolean containsSubList(List x, List y) { return indexOfSubList(x, y) >= 0; } public static boolean containsSubList(List x, A... y) { return indexOfSubList(x, y, 0) >= 0; } public static boolean isNormalQuoted(String s) { int l = l(s); if (!(l >= 2 && s.charAt(0) == '"' && lastChar(s) == '"')) return false; int j = 1; while (j < l) if (s.charAt(j) == '"') return j == l - 1; else if (s.charAt(j) == '\\' && j + 1 < l) j += 2; else ++j; return false; } public static boolean isMultilineQuoted(String s) { if (!startsWith(s, "[")) return false; int i = 1; while (i < s.length() && s.charAt(i) == '=') ++i; return i < s.length() && s.charAt(i) == '['; } public static Map newWeakHashMap() { return _registerWeakMap(synchroMap(new WeakHashMap())); } public static List web_nodesFromVerifiedWebs(Collection nodes) { List l = new ArrayList(); for (WebNode n : unnull(nodes)) if (web_isVerified(n)) l.add(n); return l; } public static List web_quotedNodesOnly(Collection l) { return filter(l, "web_isQuotedNode"); } public static String javaTok_substringN(String s, int i, int j) { if (i == j) return ""; if (j == i + 1 && s.charAt(i) == ' ') return " "; return s.substring(i, j); } public static List javaTokWithExisting(String s, List existing) { ++javaTok_n; int nExisting = javaTok_opt && existing != null ? existing.size() : 0; ArrayList tok = existing != null ? new ArrayList(nExisting) : new ArrayList(); int l = s.length(); int i = 0, n = 0; while (i < l) { int j = i; char c, d; while (j < l) { c = s.charAt(j); d = j + 1 >= l ? '\0' : s.charAt(j + 1); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else if (c == '/' && d == '*') { do ++j; while (j < l && !s.substring(j, Math.min(j + 2, l)).equals("*/")); j = Math.min(j + 2, l); } else if (c == '/' && d == '/') { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else break; } if (n < nExisting && javaTokWithExisting_isCopyable(existing.get(n), s, i, j)) tok.add(existing.get(n)); else tok.add(javaTok_substringN(s, i, j)); ++n; i = j; if (i >= l) break; c = s.charAt(i); d = i + 1 >= l ? '\0' : s.charAt(i + 1); if (c == '\'' && Character.isJavaIdentifierStart(d) && i + 2 < l && "'\\".indexOf(s.charAt(i + 2)) < 0) { j += 2; while (j < l && Character.isJavaIdentifierPart(s.charAt(j))) ++j; } else if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { if (s.charAt(j) == opener) { ++j; break; } else if (s.charAt(j) == '\\' && j + 1 < l) j += 2; else ++j; } } else if (Character.isJavaIdentifierStart(c)) do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; } else if (c == '[' && d == '[') { do ++j; while (j + 1 < l && !s.substring(j, j + 2).equals("]]")); j = Math.min(j + 2, l); } else if (c == '[' && d == '=' && i + 2 < l && s.charAt(i + 2) == '[') { do ++j; while (j + 2 < l && !s.substring(j, j + 3).equals("]=]")); j = Math.min(j + 3, l); } else ++j; if (n < nExisting && javaTokWithExisting_isCopyable(existing.get(n), s, i, j)) tok.add(existing.get(n)); else tok.add(javaTok_substringC(s, i, j)); ++n; i = j; } if ((tok.size() % 2) == 0) tok.add(""); javaTok_elements += tok.size(); return tok; } public static boolean javaTokWithExisting_isCopyable(String t, String s, int i, int j) { return t.length() == j - i && s.regionMatches(i, t, 0, j - i); } public static List minus(List a, Object... b) { Set set = asSet(b); List l = new ArrayList(); for (Object s : a) if (!set.contains(s)) l.add(s); return l; } public static BigInteger minus(BigInteger a, BigInteger b) { return a.subtract(b); } public static List codeTokensOnly(List tok) { List l = new ArrayList(); for (int i = 1; i < tok.size(); i += 2) l.add(tok.get(i)); return l; } public static String javaTok_cached_s; public static List javaTok_cached_l; public static synchronized List javaTok_cached(String s) { if (neq(s, javaTok_cached_s)) javaTok_cached_l = javaTok(javaTok_cached_s = s); return javaTok_cached_l; } public static List joinBrackets(List tok) { List t = new ArrayList(); Map map = getBracketMap(tok); for (int i = 0; i < l(tok); i++) { Integer dest = map.get(i); if (dest != null) { t.add(join(subList(tok, i, dest + 1))); i = dest; } else t.add(tok.get(i)); } return t; } public static String[] toStringArray(Collection c) { String[] a = new String[l(c)]; Iterator it = c.iterator(); for (int i = 0; i < l(a); i++) a[i] = it.next(); return a; } public static String[] toStringArray(Object o) { if (o instanceof String[]) return (String[]) o; else if (o instanceof Collection) return toStringArray((Collection) o); else throw fail("Not a collection or array: " + getClassName(o)); } public static boolean containsIgnoreCase(Collection l, String s) { for (String x : l) if (eqic(x, s)) return true; return false; } public static boolean containsIgnoreCase(String[] l, String s) { for (String x : l) if (eqic(x, s)) return true; return false; } public static boolean containsIgnoreCase(String s, char c) { return indexOfIgnoreCase(s, String.valueOf(c)) >= 0; } public static boolean containsIgnoreCase(String a, String b) { return indexOfIgnoreCase(a, b) >= 0; } public static List dropFirstAndLast(int n, List l) { return new ArrayList(subList(l, n, l(l) - n)); } public static List dropFirstAndLast(List l) { return dropFirstAndLast(1, l); } public static String dropFirstAndLast(String s) { return substring(s, 1, l(s) - 1); } public static boolean neqic(String a, String b) { return !eqic(a, b); } public static String dottedStyle() { return "border-bottom: dotted 1px"; } public static Map synchroHashMap() { return Collections.synchronizedMap(new HashMap()); } public static Comparator caseInsensitiveComparator() { return betterCIComparator(); } public static void timedGCPlusDeadlockSolving() { timedGC(); printDeadlocksAndSolve(); } public static AutoCloseable tempActivity(Object r) { return null; } public static long fixTimestamp(long timestamp) { return timestamp > now() ? 0 : timestamp; } public static boolean hasLock(Lock lock) { return ((ReentrantLock) lock).isHeldByCurrentThread(); } public static boolean veryQuickJava_silent = true; public static boolean veryQuickJava_useCompilerBot = true; public static ThreadLocal veryQuickJava_transpiled = new ThreadLocal(); public static Class veryQuickJava3(String mainJava) { return veryQuickJava3(mainJava, emptyList()); } public static Class veryQuickJava3(String mainJava, List libs) { transpileRaw_silent = veryQuickJava_silent; String src = transpileRaw(mainJava); if (veryQuickJava_transpiled.get() != null) veryQuickJava_transpiled.set(src); return veryQuickJava_finish(src, libs); } public static void ensureDBNotRunning(String name) { if (hasBot(name)) { try { String id = fsI(dropAfterSpace(name)); String framesBot = id + " Frames"; print("Trying to activate frames of running DB: " + id); if (isOK(sendOpt(framesBot, "activate frames")) && isMainProgram()) cleanKill(); } catch (Throwable __e) { _handleException(__e); } throw fail("Already running: " + name); } } public static void ensureDBNotRunning() { ensureDBNotRunning(dbBotStandardName()); } public static String dbBotStandardName() { String home = userHome(); String name = dbBotName(getDBProgramID()); if (neq(home, actualUserHome())) name += " " + quote(home); return name + "."; } public static volatile Android3 dbBot_instance; public static Android3 dbBot() { return dbBot(dbBotStandardName()); } public static Android3 dbBot(String name) { ensureDBNotRunning(name); assertNotNull(mainConcepts); return dbBot_instance = methodsBot2(name, mainConcepts, exposedDBMethods, mainConcepts.lock); } public static GZIPInputStream newGZIPInputStream(File f) { return gzInputStream(f); } public static GZIPInputStream newGZIPInputStream(InputStream in) { return gzInputStream(in); } public static Object unstructure(String text) { return unstructure(text, false); } public static Object unstructure(String text, final boolean allDynamic) { return unstructure(text, allDynamic, null); } public static int structure_internStringsLongerThan = 50; public static int unstructure_unquoteBufSize = 100; public static int unstructure_tokrefs; public abstract static class unstructure_Receiver { public abstract void set(Object o); } public static Object unstructure(String text, boolean allDynamic, Object classFinder) { if (text == null) return null; return unstructure_tok(javaTokC_noMLS_iterator(text), allDynamic, classFinder); } public static Object unstructure_reader(BufferedReader reader) { return unstructure_tok(javaTokC_noMLS_onReader(reader), false, null); } public static Object unstructure_tok(final Producer tok, final boolean allDynamic, final Object classFinder) { final boolean debug = unstructure_debug; final class X { public int i = -1; public HashMap refs = new HashMap(); public HashMap tokrefs = new HashMap(); public HashSet concepts = new HashSet(); public HashMap classesMap = new HashMap(); public List stack = new ArrayList(); public String curT; public char[] unquoteBuf = new char[unstructure_unquoteBufSize]; public String unquote(String s) { return unquoteUsingCharArray(s, unquoteBuf); } public String t() { return curT; } public String tpp() { String t = curT; consume(); return t; } public void parse(final unstructure_Receiver out) { String t = t(); int refID = 0; if (structure_isMarker(t, 0, l(t))) { refID = parseInt(t.substring(1)); consume(); } final int _refID = refID; final int tokIndex = i; parse_inner(refID, tokIndex, new unstructure_Receiver() { public void set(Object o) { if (_refID != 0) refs.put(_refID, o); if (o != null) tokrefs.put(tokIndex, o); out.set(o); } }); } public void parse_inner(int refID, int tokIndex, final unstructure_Receiver out) { String t = t(); Class c = classesMap.get(t); if (c == null) { if (t.startsWith("\"")) { String s = internIfLongerThan(unquote(tpp()), structure_internStringsLongerThan); out.set(s); return; } if (t.startsWith("'")) { out.set(unquoteCharacter(tpp())); return; } if (t.equals("bigint")) { out.set(parseBigInt()); return; } if (t.equals("d")) { out.set(parseDouble()); return; } if (t.equals("fl")) { out.set(parseFloat()); return; } if (t.equals("sh")) { consume(); t = tpp(); if (t.equals("-")) { t = tpp(); out.set((short) (-parseInt(t))); return; } out.set((short) parseInt(t)); return; } if (t.equals("-")) { consume(); t = tpp(); out.set(isLongConstant(t) ? (Object) (-parseLong(t)) : (Object) (-parseInt(t))); return; } if (isInteger(t) || isLongConstant(t)) { consume(); if (isLongConstant(t)) { out.set(parseLong(t)); return; } long l = parseLong(t); boolean isInt = l == (int) l; if (debug) print("l=" + l + ", isInt: " + isInt); out.set(isInt ? (Object) new Integer((int) l) : (Object) new Long(l)); return; } if (t.equals("false") || t.equals("f")) { consume(); out.set(false); return; } if (t.equals("true") || t.equals("t")) { consume(); out.set(true); return; } if (t.equals("-")) { consume(); t = tpp(); out.set(isLongConstant(t) ? (Object) (-parseLong(t)) : (Object) (-parseInt(t))); return; } if (isInteger(t) || isLongConstant(t)) { consume(); if (isLongConstant(t)) { out.set(parseLong(t)); return; } long l = parseLong(t); boolean isInt = l == (int) l; if (debug) print("l=" + l + ", isInt: " + isInt); out.set(isInt ? (Object) new Integer((int) l) : (Object) new Long(l)); return; } if (t.equals("File")) { consume(); File f = new File(unquote(tpp())); out.set(f); return; } if (t.startsWith("r") && isInteger(t.substring(1))) { consume(); int ref = Integer.parseInt(t.substring(1)); Object o = refs.get(ref); if (o == null) print("Warning: unsatisfied back reference " + ref); out.set(o); return; } if (t.startsWith("t") && isInteger(t.substring(1))) { consume(); int ref = Integer.parseInt(t.substring(1)); Object o = tokrefs.get(ref); if (o == null) print("Warning: unsatisfied token reference " + ref); out.set(o); return; } if (t.equals("hashset")) { parseHashSet(out); return; } if (t.equals("treeset")) { parseTreeSet(out); return; } if (eqOneOf(t, "hashmap", "hm")) { consume(); parseMap(new HashMap(), out); return; } if (t.equals("lhm")) { consume(); parseMap(new LinkedHashMap(), out); return; } if (t.equals("sync")) { consume(); if (t().equals("tm")) { consume(); { parseMap(synchronizedTreeMap(), out); return; } } { parseMap(synchronizedMap(), out); return; } } if (t.equals("{")) { parseMap(out); return; } if (t.equals("[")) { this.parseList(out); return; } if (t.equals("bitset")) { parseBitSet(out); return; } if (t.equals("array") || t.equals("intarray")) { parseArray(out); return; } if (t.equals("ba")) { consume(); String hex = unquote(tpp()); out.set(hexToBytes(hex)); return; } if (t.equals("boolarray")) { consume(); int n = parseInt(tpp()); String hex = unquote(tpp()); out.set(boolArrayFromBytes(hexToBytes(hex), n)); return; } if (t.equals("class")) { out.set(parseClass()); return; } if (t.equals("l")) { parseLisp(out); return; } if (t.equals("null")) { consume(); out.set(null); return; } if (eq(t, "c")) { consume("c"); t = t(); assertTrue(isJavaIdentifier(t)); concepts.add(t); } } if (eq(t, "j")) { consume("j"); out.set(parseJava()); return; } if (c == null && !isJavaIdentifier(t)) throw new RuntimeException("Unknown token " + (i + 1) + ": " + t); if (c == null) { if (allDynamic) c = null; else c = classFinder != null ? (Class) callF(classFinder, t) : findClass(t); if (c != null) classesMap.put(t, c); } consume(); boolean hasBracket = eq(t(), "("); if (hasBracket) consume(); boolean hasOuter = hasBracket && eq(t(), "this$1"); DynamicObject dO = null; Object o = null; final String thingName = t; if (c != null) { o = hasOuter ? nuStubInnerObject(c) : nuEmptyObject(c); if (o instanceof DynamicObject) dO = (DynamicObject) o; } else { if (concepts.contains(t) && (c = findClass("Concept")) != null) o = dO = (DynamicObject) nuEmptyObject(c); else dO = new DynamicObject(); dO.className = t; if (debug) print("Made dynamic object " + t + " " + shortClassName(dO)); } if (refID != 0) refs.put(refID, o != null ? o : dO); tokrefs.put(tokIndex, o != null ? o : dO); final LinkedHashMap fields = new LinkedHashMap(); final Object _o = o; final DynamicObject _dO = dO; if (hasBracket) { stack.add(new Runnable() { public void run() { try { if (eq(t(), ")")) { consume(")"); objRead(_o, _dO, fields); out.set(_o != null ? _o : _dO); } else { final String key = unquote(tpp()); if (!eq(tpp(), "=")) throw fail("= expected, got " + t() + " after " + quote(key) + " in object " + thingName); stack.add(this); parse(new unstructure_Receiver() { public void set(Object value) { fields.put(key, value); if (eq(t(), ",")) consume(); } }); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (eq(t(), \")\")) {\r\n consume(\")\");\r\n objRead(_o, _dO, ..."; } }); } else { objRead(o, dO, fields); out.set(o != null ? o : dO); } } public void objRead(Object o, DynamicObject dO, Map fields) { if (o != null) if (dO != null) { if (debug) printStructure("setOptAllDyn", fields); setOptAllDyn(dO, fields); } else { setOptAll_pcall(o, fields); } else for (String field : keys(fields)) dO.fieldValues.put(intern(field), fields.get(field)); if (o != null) pcallOpt_noArgs(o, "_doneLoading"); } public void parseSet(final Set set, final unstructure_Receiver out) { this.parseList(new unstructure_Receiver() { public void set(Object o) { set.addAll((List) o); out.set(set); } }); } public void parseLisp(final unstructure_Receiver out) { consume("l"); consume("("); final ArrayList list = new ArrayList(); stack.add(new Runnable() { public void run() { try { if (eq(t(), ")")) { consume(")"); out.set(new Lisp((String) list.get(0), subList(list, 1))); } else { stack.add(this); parse(new unstructure_Receiver() { public void set(Object o) { list.add(o); if (eq(t(), ",")) consume(); } }); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (eq(t(), \")\")) {\r\n consume(\")\");\r\n out.set(Lisp((Str..."; } }); if (false) throw fail("class Lisp not included"); } public void parseBitSet(final unstructure_Receiver out) { consume("bitset"); consume("{"); final BitSet bs = new BitSet(); stack.add(new Runnable() { public void run() { try { if (eq(t(), "}")) { consume("}"); out.set(bs); } else { stack.add(this); parse(new unstructure_Receiver() { public void set(Object o) { bs.set((Integer) o); if (eq(t(), ",")) consume(); } }); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (eq(t(), \"}\")) {\r\n consume(\"}\");\r\n out.set(bs);\r\n ..."; } }); } public void parseList(final unstructure_Receiver out) { consume("["); final ArrayList list = new ArrayList(); stack.add(new Runnable() { public void run() { try { if (eq(t(), "]")) { consume("]"); out.set(list); } else { stack.add(this); parse(new unstructure_Receiver() { public void set(Object o) { list.add(o); if (eq(t(), ",")) consume(); } }); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (eq(t(), \"]\")) {\r\n consume(\"]\");\r\n out.set(list);\r\n ..."; } }); } public void parseArray(final unstructure_Receiver out) { final String type = tpp(); consume("{"); final List list = new ArrayList(); stack.add(new Runnable() { public void run() { try { if (eq(t(), "}")) { consume("}"); out.set(type.equals("intarray") ? toIntArray(list) : list.toArray()); } else { stack.add(this); parse(new unstructure_Receiver() { public void set(Object o) { list.add(o); if (eq(t(), ",")) consume(); } }); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (eq(t(), \"}\")) {\r\n consume(\"}\");\r\n out.set(type.equals(\"..."; } }); } public Object parseClass() { consume("class"); consume("("); String name = unquote(tpp()); consume(")"); name = dropPrefix("main$", name); Class c = allDynamic ? null : classFinder != null ? (Class) callF(classFinder, name) : findClass(name); if (c != null) return c; DynamicObject dO = new DynamicObject(); dO.className = "java.lang.Class"; dO.fieldValues.put("name", name); return dO; } public Object parseBigInt() { consume("bigint"); consume("("); String val = tpp(); if (eq(val, "-")) val = "-" + tpp(); consume(")"); return new BigInteger(val); } public Object parseDouble() { consume("d"); consume("("); String val = unquote(tpp()); consume(")"); return Double.parseDouble(val); } public Object parseFloat() { consume("fl"); String val; if (eq(t(), "(")) { consume("("); val = unquote(tpp()); consume(")"); } else { val = unquote(tpp()); } return Float.parseFloat(val); } public void parseHashSet(unstructure_Receiver out) { consume("hashset"); parseSet(new HashSet(), out); } public void parseTreeSet(unstructure_Receiver out) { consume("treeset"); parseSet(new TreeSet(), out); } public void parseMap(unstructure_Receiver out) { parseMap(new TreeMap(), out); } public Object parseJava() { String j = unquote(tpp()); Matches m = new Matches(); if (jmatch("java.awt.Color[r=*,g=*,b=*]", j, m)) return nuObject("java.awt.Color", parseInt(m.unq(0)), parseInt(m.unq(1)), parseInt(m.unq(2))); else { warn("Unknown Java object: " + j); return null; } } public void parseMap(final Map map, final unstructure_Receiver out) { consume("{"); stack.add(new Runnable() { public boolean v; public Object key; public void run() { if (v) { v = false; stack.add(this); if (!eq(tpp(), "=")) throw fail("= expected, got " + t() + " in map of size " + l(map)); parse(new unstructure_Receiver() { public void set(Object value) { map.put(key, value); if (debug) print("parseMap: Got value " + getClassName(value) + ", next token: " + quote(t())); if (eq(t(), ",")) consume(); } }); } else { if (eq(t(), "}")) { consume("}"); out.set(map); } else { v = true; stack.add(this); parse(new unstructure_Receiver() { public void set(Object o) { key = o; } }); } } } }); } public void consume() { curT = tok.next(); ++i; } public void consume(String s) { if (!eq(t(), s)) { throw fail(quote(s) + " expected, got " + quote(t())); } consume(); } public void parse_x(unstructure_Receiver out) { consume(); parse(out); while (nempty(stack)) popLast(stack).run(); } } Boolean b = DynamicObject_loading.get(); DynamicObject_loading.set(true); try { final Var v = new Var(); X x = new X(); x.parse_x(new unstructure_Receiver() { public void set(Object o) { v.set(o); } }); unstructure_tokrefs = x.tokrefs.size(); return v.get(); } finally { DynamicObject_loading.set(b); } } public static boolean unstructure_debug; public static Producer javaTokC_noMLS_onReader(final BufferedReader r) { final class X implements Producer { public StringBuilder buf = new StringBuilder(); public char c, d, e = 'x'; public X() { nc(); nc(); nc(); } public void nc() { try { c = d; d = e; if (e == '\0') return; int i = r.read(); e = i < 0 ? '\0' : i == '\0' ? '_' : (char) i; } catch (Exception __e) { throw rethrow(__e); } } public void ncSave() { if (c != '\0') { buf.append(c); nc(); } } public String next() { while (c != '\0') { if (c == ' ' || c == '\t' || c == '\r' || c == '\n') nc(); else if (c == '/' && d == '*') { do nc(); while (c != '\0' && !(c == '*' && d == '/')); nc(); nc(); } else if (c == '/' && d == '/') { do nc(); while (c != '\0' && "\r\n".indexOf(c) < 0); } else break; } if (c == '\0') return null; if (c == '\'' || c == '"') { char opener = c; ncSave(); while (c != '\0') { if (c == opener || c == '\n') { ncSave(); break; } else if (c == '\\') { ncSave(); ncSave(); } else ncSave(); } } else if (Character.isJavaIdentifierStart(c)) do ncSave(); while (Character.isJavaIdentifierPart(c) || c == '\''); else if (Character.isDigit(c)) { do ncSave(); while (Character.isDigit(c)); if (c == 'L') ncSave(); } else ncSave(); String t = buf.toString(); buf.setLength(0); return t; } } return new X(); } public static boolean isMac() { return System.getProperty("os.name").toLowerCase().contains("mac"); } public static File getSecretProgramDir() { return getSecretProgramDir(getProgramID()); } public static File getSecretProgramDir(String snippetID) { if (empty(snippetID)) return javaxSecretDir(); return newFile(javaxSecretDir(), formatSnippetID(snippetID)); } public static String loadTextFileMandatory(File file) { String contents = loadTextFile(file); if (contents == null) throw fail("File not found: " + file.getPath()); return contents; } public static void removeFromMultiPort(long vport) { if (vport == 0) return; for (Object port : getMultiPorts()) call(port, "removePort", vport); } public static String callStaticAnswerMethod(List bots, String s) { for (Object c : bots) try { String answer = callStaticAnswerMethod(c, s); if (!empty(answer)) return answer; } catch (Throwable e) { print("Error calling " + getProgramID(c)); e.printStackTrace(); } return null; } public static String callStaticAnswerMethod(Object c, String s) { String answer = (String) callOpt(c, "answer", s, litlist(s)); if (answer == null) answer = (String) callOpt(c, "answer", s); return emptyToNull(answer); } public static String callStaticAnswerMethod(String s) { return callStaticAnswerMethod(mc(), s); } public static String callStaticAnswerMethod(String s, List history) { return callStaticAnswerMethod(mc(), s, history); } public static String callStaticAnswerMethod(Object c, String s, List history) { String answer = (String) callOpt(c, "answer", s, history); if (answer == null) answer = (String) callOpt(c, "answer", s); return emptyToNull(answer); } public static List record_list = synchroList(); public static void record(Object o) { record_list.add(o); } public static Object addToMultiPort_responder; public static long addToMultiPort(final String botName) { return addToMultiPort(botName, new Object() { public String answer(String s, List history) { String answer = (String) (callOpt(getMainClass(), "answer", s, history)); if (answer != null) return answer; answer = (String) callOpt(getMainClass(), "answer", s); if (answer != null) return answer; if (match3("get injection id", s)) return getInjectionID(); return null; } }); } public static long addToMultiPort(final String botName, final Object responder) { addToMultiPort_responder = responder; startMultiPort(); List ports = getMultiPorts(); if (ports == null) return 0; if (ports.isEmpty()) throw fail("No multiports!"); if (ports.size() > 1) print("Multiple multi-ports. Using last one."); Object port = last(ports); Object responder2 = new Object() { public String answer(String s, List history) { if (match3("get injection id", s)) return getInjectionID(); if (match3("your name", s)) return botName; return (String) call(responder, "answer", s, history); } }; record(responder2); return (Long) call(port, "addResponder", botName, responder2); } public static AtomicInteger dialogServer_clients = new AtomicInteger(); public static boolean dialogServer_printConnects; public static ThreadLocal startDialogServer_quiet = new ThreadLocal(); public static Set dialogServer_knownClients = synchroTreeSet(); public static int startDialogServerOnPortAbove(int port, DialogHandler handler) { while (!forbiddenPort(port) && !startDialogServerIfPortAvailable(port, handler)) ++port; return port; } public static int startDialogServerOnPortAboveDaemon(int port, DialogHandler handler) { while (!forbiddenPort(port) && !startDialogServerIfPortAvailable(port, handler, true)) ++port; return port; } public static void startDialogServer(int port, DialogHandler handler) { if (!startDialogServerIfPortAvailable(port, handler)) throw fail("Can't start dialog server on port " + port); } public static boolean startDialogServerIfPortAvailable(int port, final DialogHandler handler) { return startDialogServerIfPortAvailable(port, handler, false); } public static ServerSocket startDialogServer_serverSocket; public static boolean startDialogServerIfPortAvailable(int port, final DialogHandler handler, boolean daemon) { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(port); } catch (IOException e) { return false; } final ServerSocket _serverSocket = serverSocket; startDialogServer_serverSocket = serverSocket; Thread thread = new Thread("Socket accept port " + port) { public void run() { try { while (true) { try { final Socket s = _serverSocket.accept(); String client = s.getInetAddress().toString(); if (!dialogServer_knownClients.contains(client) && neq(client, "/127.0.0.1")) { print("connect from " + client + " - clients: " + dialogServer_clients.incrementAndGet()); dialogServer_knownClients.add(client); } String threadName = "Handling client " + s.getInetAddress(); Thread t2 = new Thread(threadName) { public void run() { try { final Writer w = new OutputStreamWriter(s.getOutputStream(), "UTF-8"); final BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8")); DialogIO io = new DialogIO() { public boolean isLocalConnection() { return s.getInetAddress().isLoopbackAddress(); } public boolean isStillConnected() { return !(eos || s.isClosed()); } public void sendLine(String line) { try { w.write(line + "\n"); w.flush(); } catch (Exception __e) { throw rethrow(__e); } } public String readLineImpl() { try { return in.readLine(); } catch (Exception __e) { throw rethrow(__e); } } public void close() { try { s.close(); } catch (IOException e) { } } public Socket getSocket() { return s; } }; try { handler.run(io); } finally { if (!io.noClose) s.close(); } } catch (IOException e) { print("[internal] " + e); } finally { } } }; t2.setDaemon(true); t2.start(); } catch (SocketTimeoutException e) { } } } catch (IOException e) { print("[internal] " + e); } } }; if (daemon) thread.setDaemon(true); thread.start(); if (!isTrue(getAndClearThreadLocal(startDialogServer_quiet))) print("Dialog server on port " + port + " started."); return true; } public static volatile boolean readLine_noReadLine; public static String readLine_lastInput; public static String readLine_prefix = "[] "; public static String readLine() { if (readLine_noReadLine) return null; String s = readLineHidden(); if (s != null) { readLine_lastInput = s; print(readLine_prefix + s); } return s; } public static boolean publicCommOn() { return "1".equals(loadTextFile(new File(userHome(), ".javax/public-communication"))); } public static String indentx(String s) { return indentx(indent_default, s); } public static String indentx(int n, String s) { return dropSuffix(repeat(' ', n), indent(n, s)); } public static String indentx(String indent, String s) { return dropSuffix(indent, indent(indent, s)); } public static String rtrim(String s) { if (s == null) return null; int i = s.length(); while (i > 0 && " \t\r\n".indexOf(s.charAt(i - 1)) >= 0) --i; return i < s.length() ? s.substring(0, i) : s; } public static String processID_cached; public static String getPID() { if (processID_cached == null) { String name = ManagementFactory.getRuntimeMXBean().getName(); processID_cached = name.replaceAll("@.*", ""); } return processID_cached; } public static String getInjectionID() { return (String) call(getJavaX(), "getInjectionID", getMainClass()); } public static String getProgramTitle() { return getProgramName(); } public static String makeResponder_callAnswerMethod(Object bot, String s, List history) { String answer = (String) callOpt(bot, "answer", s, history); if (answer == null) answer = (String) callOpt(bot, "answer", s); return answer; } public static Responder makeResponder(final Object bot) { if (bot instanceof Responder) return (Responder) bot; if (bot instanceof String) { String f = (String) (bot); return new Responder() { public String answer(String s, List history) { String answer = (String) callOptMC((String) bot, s, history); if (answer == null) answer = (String) callOptMC((String) bot, s); return answer; } }; } return new Responder() { public String answer(String s, List history) { return makeResponder_callAnswerMethod(bot, s, history); } }; } public static String defaultBotName() { return getProgramTitle() + "."; } public static Lock saveLock_lock = fairLock(); public static Lock saveLock() { return saveLock_lock; } public static String javaTokWordWrap(String s) { return javaTokWordWrap(120, s); } public static String javaTokWordWrap(int cols, String s) { int col = 0; List tok = javaTok(s); for (int i = 0; i < l(tok); i++) { String t = tok.get(i); if (odd(i) && col >= cols && !containsNewLine(t)) tok.set(i, t = rtrimSpaces(t) + "\n"); int idx = t.lastIndexOf('\n'); if (idx >= 0) col = l(t) - (idx + 1); else col += l(t); } return join(tok); } public static void copyAllFields(Object x, Object y) { copyFields(x, y); } public static List localSoftwareMadeWebs_programsToRead_list = ll("#1010745"); public static List localSoftwareMadeWebs_programsToRead() { return localSoftwareMadeWebs_programsToRead_list; } public static List websMadeByProgramAsTriples(String progID) { return websMadeByProgramAsTriples(progID, null); } public static List websMadeByProgramAsTriples(String progID, Map furtherInfoMap) { List triples = ai_triplesMadeByProgramAsTriples(progID); List webs = new ArrayList(); ai_loadWebsFromFile(programFile(progID, "webs-made.txt"), progID, webs, furtherInfoMap); triples.addAll(websToTripleWebs(webs)); return triples; } public static List ai_triples(String a, String b, String c) { return ai_filterTriples(a, b, c, ai_thoughtSpace_queryTriples(a, b, c, thoughtSpace())); } public static List ai_triples(String a, String b, String c, AbstractThoughtSpace ts) { return ai_filterTriples(a, b, c, ai_thoughtSpace_queryTriples(a, b, c, ts)); } public static void moveThisThreadToChild(Object child) { Thread t = currentThread(); callOpt(child, "_registerThread", t); _unregisterThread(t); } public static String fromUTF8(byte[] bytes) { return fromUtf8(bytes); } public static byte[] loadBinaryFromZip(File inZip, String fileName) { try { ZipFile zip = new ZipFile(inZip); try { ZipEntry entry = zip.getEntry(fileName); if (entry == null) return null; InputStream fin = zip.getInputStream(entry); ByteArrayOutputStream baos = new ByteArrayOutputStream(); copyStream(fin, baos); fin.close(); return baos.toByteArray(); } finally { zip.close(); } } catch (Exception __e) { throw rethrow(__e); } } public static Runnable _topLevelErrorHandling(final Runnable runnable) { return new Runnable() { public void run() { try { try { runnable.run(); } catch (Throwable __e) { _handleException(__e); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pcall { runnable.run(); }"; } }; } public static A printHidingCredentials(A o) { print(hideCredentials(str(o))); return o; } public static File infoBoxesLogFile() { return new File(javaxDataDir(), "Logs/infoBoxes.txt"); } public static Boolean isHeadless_cache; public static boolean isHeadless() { if (isHeadless_cache != null) return isHeadless_cache; if (isAndroid()) return isHeadless_cache = true; if (GraphicsEnvironment.isHeadless()) return isHeadless_cache = true; try { SwingUtilities.isEventDispatchThread(); return isHeadless_cache = false; } catch (Throwable e) { return isHeadless_cache = true; } } public static void swingAndWait(Runnable r) { try { if (isAWTThread()) r.run(); else EventQueue.invokeAndWait(r); } catch (Exception __e) { throw rethrow(__e); } } public static Object swingAndWait(final Object f) { if (isAWTThread()) return callF(f); else { final Var result = new Var(); swingAndWait(new Runnable() { public void run() { try { result.set(callF(f)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "result.set(callF(f));"; } }); return result.get(); } } public static JWindow showWindow(Component c) { JWindow w = new JWindow(); w.add(wrap(c)); return w; } public static JPanel infoMessage_makePanel(String text) { final JTextArea ta = wrappedTextArea(text); onClick(ta, new Runnable() { public void run() { try { disposeWindow(ta); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "disposeWindow(ta)"; } }); int size = 14; if (l(text) <= 50) size *= 2; else if (l(text) < 100) size = iround(size * 1.5); ta.setFont(typeWriterFont(size)); JScrollPane sp = jscroll(ta); return withMargin(sp); } public static int moveToTopRightCorner_inset = 20; public static A moveToTopRightCorner(A a) { return moveToTopRightCorner(moveToTopRightCorner_inset, moveToTopRightCorner_inset, a); } public static A moveToTopRightCorner(int insetX, int insetY, A a) { Window w = getWindow(a); if (w != null) w.setLocation(getScreenSize().width - w.getWidth() - insetX, insetY); return a; } public static A disposeWindowAfter(int delay, final A w) { if (w != null) swingLater(delay, new Runnable() { public void run() { try { w.dispose(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "w.dispose();"; } }); return w; } public static A disposeWindowAfter(A w, double seconds) { return disposeWindowAfter(toMS_int(seconds), w); } public static void showConsole() { callOpt(get(javax(), "console"), "showConsole"); } public static Map emptyMap() { return new HashMap(); } public static List sorted(Collection c, final Object comparator) { List l = cloneList(c); sort(l, makeComparator(comparator)); return l; } public static List sorted(Collection c) { List l = cloneList(c); sort(l); return l; } public static List methodsStartingWith(Object o, final String prefix) { return filter(allMethodNames(o), new F1() { public Object get(String s) { try { return startsWith(s, prefix); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "startsWith(s, prefix)"; } }); } public static boolean isJavaXClassLoader(ClassLoader cl) { return startsWithOneOf(className(cl), "main$JavaXClassLoader", "x30$JavaXClassLoader"); } public static LinkedHashMap asLinkedHashMap(Map map) { if (map instanceof LinkedHashMap) return (LinkedHashMap) map; LinkedHashMap m = new LinkedHashMap(); if (map != null) synchronized (collectionMutex(map)) { m.putAll(map); } return m; } public static Throwable getExceptionCause(Throwable e) { Throwable c = e.getCause(); return c != null ? c : e; } public static List classNames(Collection l) { return getClassNames(l); } public static List classNames(Object[] l) { return getClassNames(Arrays.asList(l)); } public static PersistableThrowable persistableThrowable(Throwable e) { return e == null ? null : new PersistableThrowable(e); } public static List> ai_texts_dollarXY(String a, String b, String c) { if (isDollarVar(b)) throw todo(); List> l = new ArrayList(); if (eq(a, "$X") && eq(c, "$Y")) { for (WebRelation r : indexedRelations(b)) l.add(pair(web_text(r.a), web_text(r.b))); return l; } if (eq(a, "$Y") && eq(c, "$X")) { for (WebRelation r : indexedRelations(b)) l.add(pair(web_text(r.b), web_text(r.a))); return l; } throw fail("improper arguments: " + a + "/" + b + "/" + c); } public static boolean isProperlyQuoted2(String s) { return isNormalQuoted(s); } public static String dollarVarsToStars(String s) { return dollarVarsToStars(s, null); } public static String dollarVarsToStars(String s, List varNames_out) { List tok = javaTok(s); for (int i = 1; i < l(tok); i += 2) { String t = tok.get(i); if (isDollarVar(t)) { listAdd(varNames_out, t); tok.set(i, "*"); } else if (eq(t, "*")) listAdd(varNames_out, "*"); } return join(tok); } public static String dropSuffixTrimIC(String suffix, String s) { return dropSuffixICTrim(suffix, s); } public static List indexedNodes_rawLookup(String s) { List l = cachedNodeIndex().get(ai_shortenForIndex(s)); AI_SubSpace ss = ai_currentSubSpace(); if (ss != null) { List l2 = ss.indexedNodes_rawLookup(symbol(s)); return concatLists_conservative(l2, l); } return l; } public static IterableIterator concurrentlyIterateList(final List l) { return iteratorFromFunction_withEndMarker_f0(new F0() { public int i; public A get() { int _i = i++; synchronized (l) { if (_i < l(l)) { A a = l.get(_i); if (a == iteratorFromFunction_endMarker) throw fail("no"); return a; } return (A) iteratorFromFunction_endMarker; } } }); } public static CombinedList combinedList_ll(List l) { CombinedList cl = new CombinedList(); cl.addList(l); return cl; } public static WebNode web_findForwardRelated_multiRel(WebNode node, String... relations) { if (node == null) return null; if (empty(relations)) relations = litstringarray(""); Web web = node.web; List labels = web_parseLabels(web, relations); for (Pair p : web_relations(web)) if (p.a == node) { WebNode rel = web.getRelationOpt(p); if (web_hasOneOfTheLabels(rel, labels)) return p.b; } return null; } public static ThreadLocal> checkFileNotTooBigToRead_tl = new ThreadLocal(); public static void checkFileNotTooBigToRead(File f) { callF(checkFileNotTooBigToRead_tl.get(), f); } public static String addPrefixIfNempty(String prefix, String s) { return addPrefixIfNotEmpty(prefix, s); } public static String dropSuffixIC(String suffix, String s) { return s == null ? null : ewic(s, suffix) ? s.substring(0, l(s) - l(suffix)) : s; } public static File dirOfFile(File f) { return f == null ? null : f.getParentFile(); } public static int countLines(String s) { return l(toLines(s)); } public static List beginCriticalAction_inFlight = synchroList(); public static class CriticalAction { public String description; public CriticalAction() { } public CriticalAction(String description) { this.description = description; } public void done() { beginCriticalAction_inFlight.remove(this); } } public static CriticalAction beginCriticalAction(String description) { ping(); CriticalAction c = new CriticalAction(description); beginCriticalAction_inFlight.add(c); return c; } public static void cleanMeUp_beginCriticalAction() { int n = 0; while (nempty(beginCriticalAction_inFlight)) { int m = l(beginCriticalAction_inFlight); if (m != n) { n = m; try { print("Waiting for " + n2(n, "critical actions") + ": " + join(", ", collect(beginCriticalAction_inFlight, "description"))); } catch (Throwable __e) { _handleException(__e); } } sleepInCleanUp(10); } } public static File mkdirsForFile(File file) { File dir = file.getParentFile(); if (dir != null) { dir.mkdirs(); if (!dir.isDirectory()) if (dir.isFile()) throw fail("Please delete the file " + f2s(dir) + " - it is supposed to be a directory!"); else throw fail("Unknown IO exception during mkdirs of " + f2s(file)); } return file; } public static String mkdirsForFile(String path) { mkdirsForFile(new File(path)); return path; } public static void copyFile(File src, File dest) { try { FileInputStream inputStream = new FileInputStream(src.getPath()); FileOutputStream outputStream = newFileOutputStream(dest.getPath()); try { copyStream(inputStream, outputStream); inputStream.close(); } finally { outputStream.close(); } } catch (Exception __e) { throw rethrow(__e); } } public static FileOutputStream newFileOutputStream(File path) throws IOException { return newFileOutputStream(path.getPath()); } public static FileOutputStream newFileOutputStream(String path) throws IOException { return newFileOutputStream(path, false); } public static FileOutputStream newFileOutputStream(String path, boolean append) throws IOException { mkdirsForFile(path); FileOutputStream f = new FileOutputStream(path, append); callJavaX("registerIO", f, path, true); return f; } public static A popLast(List l) { return liftLast(l); } public static void quoteToPrintWriter(String s, PrintWriter out) { if (s == null) { out.print("null"); return; } out.print('"'); int l = s.length(); for (int i = 0; i < l; i++) { char c = s.charAt(i); if (c == '\\' || c == '"') { out.print('\\'); out.print(c); } else if (c == '\r') out.print("\\r"); else if (c == '\n') out.print("\\n"); else if (c == '\0') out.print("\\0"); else out.print(c); } out.print('"'); } public static String quoteCharacter(char c) { if (c == '\'') return "'\\''"; if (c == '\\') return "'\\\\'"; if (c == '\r') return "'\\r'"; if (c == '\n') return "'\\n'"; if (c == '\t') return "'\\t'"; return "'" + c + "'"; } public static String bytesToHex(byte[] bytes) { return bytesToHex(bytes, 0, bytes.length); } public static String bytesToHex(byte[] bytes, int ofs, int len) { StringBuilder stringBuilder = new StringBuilder(len * 2); for (int i = 0; i < len; i++) { String s = "0" + Integer.toHexString(bytes[ofs + i]); stringBuilder.append(s.substring(s.length() - 2, s.length())); } return stringBuilder.toString(); } public static String boolArrayToHex(boolean[] a) { return bytesToHex(boolArrayToBytes(a)); } public static Iterator emptyIterator() { return Collections.emptyIterator(); } public static String shortDynamicClassName(Object o) { if (o instanceof DynamicObject && ((DynamicObject) o).className != null) return ((DynamicObject) o).className; return shortClassName(o); } public static int stdcompare(Number a, Number b) { return cmp(a, b); } public static int stdcompare(String a, String b) { return cmp(a, b); } public static int stdcompare(long a, long b) { return a < b ? -1 : a > b ? 1 : 0; } public static int stdcompare(Object a, Object b) { return cmp(a, b); } public static Map getDeclaredFields_cache = newDangerousWeakHashMap(); public static Field[] getDeclaredFields_cached(Class c) { Field[] fields; synchronized (getDeclaredFields_cache) { fields = getDeclaredFields_cache.get(c); if (fields == null) { getDeclaredFields_cache.put(c, fields = c.getDeclaredFields()); for (Field f : fields) f.setAccessible(true); } } return fields; } public static void cleanKillVM() { try { ping(); assertNotOnAWTThread(); cleanKillVM_noSleep(); Object o = new Object(); synchronized (o) { o.wait(); } } catch (Exception __e) { throw rethrow(__e); } } public static void cleanKillVM_noSleep() { call(getJavaX(), "cleanKill"); } public static WebRelation web_findRelationWithLabel(Web web, String s) { for (WebRelation r : web_relationObjects(web)) if (web_matchNodeName(r, s)) return r; return null; } public static List splitByJavaToken(String s, String splitToken) { List tok = javaTok(s); List l = new ArrayList(); int i = 1; while (i < l(tok)) { int j = smartIndexOf(tok, splitToken, i); l.add(join(subList(tok, i, j - 1))); i = j + 2; } return l; } public static String patternQuote(String s) { return s.length() == 0 ? "" : Pattern.quote(s); } public static String quoteReplacement(String s) { return Matcher.quoteReplacement(s); } public static Set synchroWeakHashSet() { return Collections.newSetFromMap((Map) newWeakHashMap()); } public static void cancelThread(Thread t) { if (t == null) return; ping(); synchronized (mc()) { ping_actions.put(t, "cancelled"); ping_anyActions = true; } } public static int toInt_checked(long l) { if (l != (int) l) throw fail("Too large for int: " + l); return (int) l; } public static Class getMainClass() { return mc(); } public static Class getMainClass(Object o) { try { if (o == null) return null; return (o instanceof Class ? (Class) o : o.getClass()).getClassLoader().loadClass("main"); } catch (Exception __e) { throw rethrow(__e); } } public static String shortenSnippetID(String snippetID) { if (snippetID.startsWith("#")) snippetID = snippetID.substring(1); String httpBlaBla = "http://tinybrain.de/"; if (snippetID.startsWith(httpBlaBla)) snippetID = snippetID.substring(httpBlaBla.length()); return "" + parseLong(snippetID); } public static void logMechListAppendLocally(String listName, String text) { logStructureWithDate(mechListAppendLogFile(listName), litorderedmap("list", listName, "text", text)); } public static String jextract(String pat, String s) { return jextract(pat, javaTok(s)); } public static String jextract(String pat, List tok) { List tokpat = javaTok(pat); jfind_preprocess(tokpat); int i = jfind(tok, tokpat); if (i < 0) return null; int j = i + l(tokpat) - 2; return join(subList(tok, i, j)); } public static File saveProgramTextFile(String name, String contents) { return saveTextFile(getProgramFile(name), contents); } public static File saveProgramTextFile(String progID, String name, String contents) { return saveTextFile(getProgramFile(progID, name), contents); } public static List javaTokPlusPeriod(String s) { List tok = new ArrayList(); int l = s.length(); int i = 0; while (i < l) { int j = i; char c; String cc; while (j < l) { c = s.charAt(j); cc = s.substring(j, Math.min(j + 2, l)); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else if (cc.equals("/*")) { do ++j; while (j < l && !s.substring(j, Math.min(j + 2, l)).equals("*/")); j = Math.min(j + 2, l); } else if (cc.equals("//")) { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else break; } tok.add(s.substring(i, j)); i = j; if (i >= l) break; c = s.charAt(i); cc = s.substring(i, Math.min(i + 2, l)); if (c == (char) 0x201C || c == (char) 0x201D) c = '"'; if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { char _c = s.charAt(j); if (_c == (char) 0x201C || _c == (char) 0x201D) _c = '"'; if (_c == opener) { ++j; break; } else if (s.charAt(j) == '\\' && j + 1 < l) j += 2; else ++j; } if (j - 1 >= i + 1) { tok.add(opener + s.substring(i + 1, j - 1) + opener); i = j; continue; } } else if (Character.isJavaIdentifierStart(c)) do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || s.charAt(j) == '\'')); else if (Character.isDigit(c)) do ++j; while (j < l && Character.isDigit(s.charAt(j))); else if (cc.equals("[[")) { do ++j; while (j + 1 < l && !s.substring(j, j + 2).equals("]]")); j = Math.min(j + 2, l); } else if (cc.equals("[=") && i + 2 < l && s.charAt(i + 2) == '[') { do ++j; while (j + 2 < l && !s.substring(j, j + 3).equals("]=]")); j = Math.min(j + 3, l); } else if (s.substring(j, Math.min(j + 3, l)).equals("...")) j += 3; else if (c == '$' || c == '#') do ++j; while (j < l && Character.isDigit(s.charAt(j))); else ++j; tok.add(s.substring(i, j)); i = j; } if ((tok.size() % 2) == 0) tok.add(""); return tok; } public static boolean isNonNegativeInteger(String s) { return s != null && Pattern.matches("\\d+", s); } public static String format(String pat, Object... args) { return format3(pat, args); } public static Map findBot_cache = synchroHashMap(); public static int findBot_timeout = 5000; public static DialogIO findBot(String searchPattern) { String subBot = null; int i = searchPattern.indexOf('/'); if (i >= 0 && (isJavaIdentifier(searchPattern.substring(0, i)) || isInteger(searchPattern.substring(0, i)))) { subBot = searchPattern.substring(i + 1); searchPattern = searchPattern.substring(0, i); if (!isInteger(searchPattern)) searchPattern = "Multi-Port at " + searchPattern + "."; } if (isInteger(searchPattern)) return talkToSubBot(subBot, talkTo(parseInt(searchPattern))); if (eq(searchPattern, "remote")) return talkToSubBot(subBot, talkTo("second.tinybrain.de", 4999)); Integer port = findBot_cache.get(searchPattern); if (port != null) try { DialogIO io = talkTo("localhost", port); io.waitForLine(); String line = io.readLineNoBlock(); if (indexOfIgnoreCase(line, searchPattern) == 0) { call(io, "pushback", line); return talkToSubBot(subBot, io); } } catch (Exception e) { e.printStackTrace(); } List bots = quickBotScan(); for (ProgramScan.Program p : bots) { if (indexOfIgnoreCase(p.helloString, searchPattern) == 0) { findBot_cache.put(searchPattern, p.port); return talkToSubBot(subBot, talkTo("localhost", p.port)); } } for (ProgramScan.Program p : bots) { String botName = firstPartOfHelloString(p.helloString); boolean isVM = startsWithIgnoreCase(p.helloString, "This is a JavaX VM."); boolean shouldRecurse = startsWithIgnoreCase(botName, "Multi-Port") || isVM; if (shouldRecurse) try { Map subBots = (Map) unstructure(sendToLocalBotQuietly(p.port, "list bots")); for (Number vport : subBots.keySet()) { String name = subBots.get(vport); if (startsWithIgnoreCase(name, searchPattern)) return talkToSubBot(vport.longValue(), talkTo("localhost", p.port)); } } catch (Throwable __e) { print(exceptionToStringShort(__e)); } } return null; } public static String parse3_cached_s; public static List parse3_cached_l; public static synchronized List parse3_cached(String s) { if (neq(s, parse3_cached_s)) parse3_cached_l = parse3(parse3_cached_s = s); return parse3_cached_l; } public static List parse3(String s) { return dropPunctuation(javaTokPlusPeriod(s)); } public static String[] match2(List pat, List tok) { int i = pat.indexOf("..."); if (i < 0) return match2_match(pat, tok); pat = new ArrayList(pat); pat.set(i, "*"); while (pat.size() < tok.size()) { pat.add(i, "*"); pat.add(i + 1, ""); } return match2_match(pat, tok); } public static String[] match2_match(List pat, List tok) { List result = new ArrayList(); if (pat.size() != tok.size()) { return null; } for (int i = 1; i < pat.size(); i += 2) { String p = pat.get(i), t = tok.get(i); if (eq(p, "*")) result.add(t); else if (!equalsIgnoreCase(unquote(p), unquote(t))) return null; } return result.toArray(new String[result.size()]); } public static File pathToJavaxJar() { try { int x = latestInstalledJavaX(); File xfile = new File(userHome(), ".javax/x" + Math.max(x, 30) + ".jar"); if (!xfile.isFile()) { print("Saving " + f2s(xfile)); String url = "http://tinybrain.de/x30.jar"; byte[] data = loadBinaryPage(url); if (data.length < 1000000) throw fail("Could not load " + url); saveBinaryFile(xfile.getPath(), data); } return xfile; } catch (Exception __e) { throw rethrow(__e); } } public static String nohup_sanitize(String s) { return takeFirst(50, s.replaceAll("[^a-zA-Z0-9\\-_]", "")); } public static String ymd_minus_hms() { return ymd() + "-" + hms(); } public static List platformParseArgs(String s) { List out = new ArrayList(); List tok = javaTok(trim(s)); for (int i = 1; i < l(tok); i += 2) { if (isQuoted(tok.get(i))) { out.add(tok.get(i)); continue; } int j = i; while (j + 2 < l(tok) && empty(tok.get(j + 1))) j += 2; out.add(joinSubList(tok, i, j + 1)); i = j; } return out; } public static String _javaCommand() { return or2(trim(loadTextFile(javaxDataDir("java-command.txt"))), "java"); } public static String winQuote(String text) { if (text == null) return null; return "\"" + text.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n").replace("\r", "\\r") + "\""; } public static String winQuote(File f) { return winQuote(f.getAbsolutePath()); } public static boolean nohup_debug; public static boolean nohup_noSlashB; public static ThreadLocal nohup_exitValue = new ThreadLocal(); public static File nohup(String cmd) { try { File outFile = File.createTempFile("nohup_" + nohup_sanitize(cmd) + "_", ".out"); nohup(cmd, outFile, false); return outFile; } catch (Exception __e) { throw rethrow(__e); } } public static void nohup(String cmd, File outFile, boolean append) { try { String command = nohup_makeNohupCommand(cmd, outFile, append); File scriptFile = File.createTempFile("_realnohup", isWindows() ? ".bat" : ""); System.out.println("[Nohup] " + command); try { if (nohup_debug) print("[nohup] Script file: " + scriptFile.getPath()); saveTextFile(scriptFile.getPath(), command); String[] command2; if (isWindows()) if (nohup_noSlashB) command2 = new String[] { "cmd", "/c", "start", scriptFile.getPath() }; else command2 = new String[] { "cmd", "/c", "start", "/b", scriptFile.getPath() }; else command2 = new String[] { "/bin/bash", scriptFile.getPath() }; printStruct("[nohup] ", command2); Process process = Runtime.getRuntime().exec(command2); try { process.waitFor(); } catch (InterruptedException e) { throw new RuntimeException(e); } int value = process.exitValue(); nohup_exitValue.set(value); if (value != 0) warn("nohup exit value != 0: " + value); } finally { if (!isWindows()) scriptFile.delete(); } } catch (Exception __e) { throw rethrow(__e); } } public static String nohup_makeNohupCommand(String cmd, File outFile, boolean append) { mkdirsForFile(outFile); String command; if (isWindows()) command = cmd + (append ? " >>" : " >") + winQuote(outFile.getPath()) + " 2>&1"; else command = "nohup " + cmd + (append ? " >>" : " >") + bashQuote(outFile.getPath()) + " 2>&1 &"; return command; } public static void gzipFile(File in, File out) { try { saveStreamToGZFile(newFileInputStream(in), out); } catch (Exception __e) { throw rethrow(__e); } } public static int ai_indexShorteningLength() { return ai_shortenForIndex_maxLength; } public static NavigableSet prefixSubSet(NavigableSet set, String prefix) { return set.subSet(prefix, true, prefix + Character.MAX_VALUE, true); } public static boolean isAdjective(String s) { return contains(knownAdjectives(), lower(s)); } public static ThreadLocal> ai_callSpeculationFunction_functionsCalled = new ThreadLocal(); public static void ai_callSpeculationFunction(final String f, final String word) { try { ping(); collectionAdd(ai_callSpeculationFunction_functionsCalled.get(), f); ai_withMaker(f, new Runnable() { public void run() { try { callMC(f, word); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callMC(f, word)"; } }); } catch (Throwable __e) { _handleException(__e); } } public static Object[] expandParams(Class c, Object[] params) { if (l(params) == 1) params = new Object[] { singleFieldName(c), params[0] }; else warnIfOddCount(params); return params; } public static A findConceptWhere(Class c, Object... params) { return findConceptWhere(mainConcepts, c, params); } public static A findConceptWhere(Concepts concepts, Class c, Object... params) { params = expandParams(c, params); if (concepts.fieldIndices != null) for (int i = 0; i < l(params); i += 2) { IFieldIndex index = concepts.getFieldIndex(c, (String) params[i]); if (index != null) { for (A x : index.getAll(params[i + 1])) if (checkConceptFields(x, params)) return x; return null; } } for (A x : concepts.list(c)) if (checkConceptFields(x, params)) return x; return null; } public static Concept findConceptWhere(Concepts concepts, String c, Object... params) { for (Concept x : concepts.list(c)) if (checkConceptFields(x, params)) return x; return null; } public static A unlisted(Class c, Object... args) { concepts_unlisted.set(true); try { return nuObject(c, args); } finally { concepts_unlisted.set(null); } } public static String beautifyStructure(String s) { return structure_addTokenMarkers(s); } public static String struct_noStringSharing(Object o) { structure_Data d = new structure_Data(); d.noStringSharing = true; return structure(o, d); } public static Method findMethod(Object o, String method, Object... args) { try { if (o == null) return null; if (o instanceof Class) { Method m = findMethod_static((Class) o, method, args, false); if (m == null) return null; m.setAccessible(true); return m; } else { Method m = findMethod_instance(o, method, args, false); if (m == null) return null; m.setAccessible(true); return m; } } catch (Exception e) { throw new RuntimeException(e); } } public static Method findMethod_static(Class c, String method, Object[] args, boolean debug) { Class _c = c; while (c != null) { for (Method m : c.getDeclaredMethods()) { if (debug) System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters"); ; if (!m.getName().equals(method)) { if (debug) System.out.println("Method name mismatch: " + method); continue; } if ((m.getModifiers() & Modifier.STATIC) == 0 || !findMethod_checkArgs(m, args, debug)) continue; return m; } c = c.getSuperclass(); } return null; } public static Method findMethod_instance(Object o, String method, Object[] args, boolean debug) { Class c = o.getClass(); while (c != null) { for (Method m : c.getDeclaredMethods()) { if (debug) System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters"); ; if (m.getName().equals(method) && findMethod_checkArgs(m, args, debug)) return m; } c = c.getSuperclass(); } return null; } public static boolean findMethod_checkArgs(Method m, Object[] args, boolean debug) { Class[] types = m.getParameterTypes(); if (types.length != args.length) { if (debug) System.out.println("Bad parameter length: " + args.length + " vs " + types.length); return false; } for (int i = 0; i < types.length; i++) if (!(args[i] == null || isInstanceX(types[i], args[i]))) { if (debug) System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]); return false; } return true; } public static void multiMapPut(Map> map, A a, B b) { List l = map.get(a); if (l == null) map.put(a, l = new ArrayList()); l.add(b); } public static boolean preciseCall_debug; public static Object preciseCall(Object o, String method, Object... args) { try { if (o instanceof Class) { List methods = findMethodsNamed2((Class) o, method, true); Lowest best = new Lowest(); for (Method m : methods) { int score = methodApplicabilityScore(m, args); if (score < Integer.MAX_VALUE) { if (preciseCall_debug) print("Method score: " + m + " " + score); best.put(m, score); } } Method m = best.get(); if (m == null) throw fail("No suitable method found: " + methodSignature(method, args)); return invokeMethod(m, null, args); } else { throw todo(); } } catch (Exception __e) { throw rethrow(__e); } } public static String assertIsIdentifier(String s) { if (!isIdentifier(s)) throw fail("Not an identifier: " + quote(s)); return s; } public static String evalJava_prep(String code) { return evalJava_prep(code, "calc"); } public static String evalJava_prep(String code, String mainName) { return evalJava_prep(code, mainName, ""); } public static String evalJava_prep(String code, String mainName, String preCode) { code = tok_addReturn(trim(code)); String returnType = containsReturnWithArgument(code) ? "O" : "void"; String main = "static " + returnType + " " + mainName + "() throws Exception { " + preCode + code + "\n" + "}"; return main; } public static String hijackPrint_tee(Runnable r) { final StringBuilder buf = new StringBuilder(); Object old = interceptPrintInThisThread(new F1() { public Boolean get(String s) { buf.append(s); return true; } }); try { callF(r); return str(buf); } finally { interceptPrintInThisThread(old); } } public static Object evalJava_myProgramID_main(String main) { Object obj = veryQuickJava(main); setOpt(obj, "programID", programID()); makeDependent(obj); return callCalc(obj); } public static void smartBot_savePrintLog(String expression, String printed) { File f = countTillNewFile(prepareProgramFile("printlogs/printlog"), ".txt"); saveTextFile(f, dateWithTimeGMT() + "\n\n" + expression + "\n\n----\n\n" + printed); print("Saved print log: " + f2s(f)); } public static String sfuOrText(Object o) { return o instanceof String ? (String) o : sfu(o); } public static boolean containsNewLine(String s) { return contains(s, '\n'); } public static Object quickExport(Object o, Object dest) { return quickExport_impl(o, dest, new IdentityHashMap(), new HashMap()); } public static Object quickExport(Object o, Object dest, int expectedNumberOfObjects) { return quickExport_impl(o, dest, new IdentityHashMap(expectedNumberOfObjects), new HashMap()); } public static Object quickExport_impl(Object o, Object dest, IdentityHashMap seen, HashMap classMap) { try { if (o == null || o instanceof String || o instanceof Number) return o; Object oo = seen.get(o); if (oo != null) return oo; if (o instanceof Object[]) { Object[] l = (Object[]) o; Object[] destO = newObjectArrayOfSameType(l, l.length); seen.put(o, destO); for (int i = 0; i < l.length; i++) destO[i] = quickExport_impl(l[i], dest, seen, classMap); return destO; } if (o instanceof List) { List l = (List) (o); List destO = new ArrayList(l.size()); seen.put(o, destO); for (int i = 0; i < l.size(); i++) destO.add(quickExport_impl(l.get(i), dest, seen, classMap)); return destO; } if (o instanceof Map) { Map m = (Map) (o); Map destO = new HashMap(); seen.put(o, destO); for (Object e : ((Map) o).entrySet()) destO.put(quickExport_impl(((Map.Entry) e).getKey(), dest, seen, classMap), quickExport_impl(((Map.Entry) e).getValue(), dest, seen, classMap)); return destO; } Class c = o.getClass(); String className = c.getName(); if (className.startsWith("main$")) { Class destClass = classMap.get(c); if (destClass == null) { if (!classMap.containsKey(c)) { if (!isAnonymousClassName(className)) destClass = getClass_vmName(dest, className); classMap.put(c, destClass); } if (destClass == null) return o; } if (c == destClass) return o; Object destO = nuObjectWithoutArguments(destClass); seen.put(o, destO); while (c != Object.class) { Field[] fields = c.getDeclaredFields(); for (Field field : fields) { if ((field.getModifiers() & Modifier.STATIC) != 0) continue; field.setAccessible(true); Object value = field.get(o); setOpt(destO, field.getName(), quickExport_impl(value, dest, seen, classMap)); } c = c.getSuperclass(); } return destO; } return o; } catch (Exception __e) { throw rethrow(__e); } } public static PrintWriter utf8printWriter(OutputStream out) { return new PrintWriter(outputStreamToWriter(out)); } public static String quoteUnlessIdentifierOrInteger(String s) { return quoteIfNotIdentifierOrInteger(s); } public static void sortInPlaceIC(List l) { sortInPlace(l, caseInsensitiveComparator()); } public static List valuesList(Map map) { return cloneListSynchronizingOn(values(map), map); } public static boolean boolOptParam(ThreadLocal tl) { return isTrue(optPar(tl)); } public static volatile boolean disableCertificateValidation_attempted; public static void disableCertificateValidation() { try { if (disableCertificateValidation_attempted) return; disableCertificateValidation_attempted = true; try { TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; HostnameVerifier hv = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); HttpsURLConnection.setDefaultHostnameVerifier(hv); } catch (Throwable __e) { _handleException(__e); } } catch (Exception __e) { throw rethrow(__e); } } public static final boolean loadPageThroughProxy_enabled = false; public static String loadPageThroughProxy(String url) { return null; } public static String tb_mainServer_default = "http://tinybrain.de:8080"; public static Object tb_mainServer_override; public static String tb_mainServer() { if (tb_mainServer_override != null) return (String) callF(tb_mainServer_override); return trim(loadTextFile(tb_mainServer_file(), tb_mainServer_default)); } public static File tb_mainServer_file() { return getProgramFile("#1001638", "mainserver.txt"); } public static boolean tb_mainServer_isDefault() { return eq(tb_mainServer(), tb_mainServer_default); } public static void printWithTime(String s) { print(hmsWithColons() + ": " + s); } public static void setHeaders(URLConnection con) throws IOException { String computerID = getComputerID_quick(); if (computerID != null) try { con.setRequestProperty("X-ComputerID", computerID); con.setRequestProperty("X-OS", System.getProperty("os.name") + " " + System.getProperty("os.version")); } catch (Throwable e) { } } public static Map vm_generalSubMap(Object name) { synchronized (get(javax(), "generalMap")) { Map map = (Map) (vm_generalMap_get(name)); if (map == null) vm_generalMap_put(name, map = synchroMap()); return map; } } public static URLConnection openConnection(URL url) { try { ping(); callOpt(javax(), "recordOpenURLConnection", str(url)); return url.openConnection(); } catch (Exception __e) { throw rethrow(__e); } } public static URLConnection setURLConnectionTimeouts(URLConnection con, long timeout) { con.setConnectTimeout(toInt(timeout)); con.setReadTimeout(toInt(timeout)); if (con.getConnectTimeout() != timeout || con.getReadTimeout() != timeout) print("Warning: Timeouts not set by JDK."); return con; } public static URLConnection setURLConnectionDefaultTimeouts(URLConnection con, long timeout) { if (con.getConnectTimeout() == 0) { con.setConnectTimeout(toInt(timeout)); if (con.getConnectTimeout() != timeout) print("Warning: URL connect timeout not set by JDK."); } if (con.getReadTimeout() == 0) { con.setReadTimeout(toInt(timeout)); if (con.getReadTimeout() != timeout) print("Warning: URL read timeout not set by JDK."); } return con; } public static boolean ai_isTripleWeb(Web web) { return ai_webToTriple(web) != null; } public static void appendToFile_keepOpen(boolean b) { Lock __2673 = appendToFile_lock; lock(__2673); try { appendToFile_keepOpen = b; if (!b) cleanMeUp_appendToFile(); } finally { unlock(__2673); } } public static TreeMap litCIMap(Object... x) { TreeMap map = caseInsensitiveMap(); litmap_impl(map, x); return map; } public static double toDouble(Object o) { if (o instanceof Number) return ((Number) o).doubleValue(); if (o instanceof BigInteger) return ((BigInteger) o).doubleValue(); if (o == null) return 0.0; throw fail(o); } public static double nan() { return Double.NaN; } public static boolean web_hasLabelIC(WebNode node, String text) { if (node != null) for (Lisp l : node.labels()) if (eqic(text, node.web.unparseLabel(l))) return true; return false; } public static boolean web_hasLabelIC(Pair p, String text) { return web_hasLabelIC(p.a.web.relation(p), text); } public static boolean web_hasLabelIC(WebNode a, WebNode b, String text) { return web_hasLabelIC(a.web.relation(a, b), text); } public static Object loadGZStructure(File f) { return unstructureGZFile(f); } public static String loadPageWithTimeout(long timeout, String url) { return loadPageWithTimeout(url, timeout); } public static String loadPageWithTimeout(String url, long timeout) { try { url = loadPage_preprocess(url); print("Loading with timeout: " + url); URL _url = new URL(url); return loadPage(setURLConnectionTimeouts(_url.openConnection(), timeout), _url); } catch (Exception __e) { throw rethrow(__e); } } public static String loadPageWithTimeout(String url, double timeoutSeconds) { return loadPageWithTimeout(url, toMS(timeoutSeconds)); } public static String addPrefixIfNotEmpty2(String prefix, String s) { return empty(s) ? "" : addPrefix(prefix, s); } public static void scpUploadFile(File localFile, String user, String host, final String remotePath) { try { print("Uploading file: " + f2s(localFile)); scpUploadWithMkdirs(user, host, remotePath, fileSize(localFile), newFileInputStream(localFile)); } catch (Exception __e) { throw rethrow(__e); } } public static WebNode webs_tripleSearch_dollarX_first(String a, String b, String c, Collection webs) { boolean isA = eq(a, "$X"); for (Web web : webs) { Pair p = web_matchTriple_dollarVars(a, b, c, web); if (p != null) return isA ? p.a : p.b; } return null; } public static WebNode webs_tripleSearch_dollarX_first(Collection webs, T3 t) { return webs_tripleSearch_dollarX_first(t.a, t.b, t.c, webs); } public static String makeRandomID(int length) { Random random = new Random(); char[] id = new char[length]; for (int i = 0; i < id.length; i++) id[i] = (char) ((int) 'a' + random.nextInt(26)); return new String(id); } public static Set ai_makeCluster(String entry) { return litciset(entry); } public static Set ai_cluster_forward(Set cluster, String rel) { Set set = litciset(); for (String s : cluster) set.addAll(ai_texts(s, rel, "$X")); return set; } public static List ai_parseAndList(String s) { return splitAtJavaTokens(s, ",", "and"); } public static boolean isJavaIdentifierAfter(String s, int i) { int n = l(s); if (i >= n || !Character.isJavaIdentifierStart(s.charAt(i))) return false; for (i++; i < n; i++) if (!Character.isJavaIdentifierPart(s.charAt(i))) return false; return true; } public static List web_verifiedWebsOnly(Collection l) { List x = new ArrayList(); if (l != null) for (Web web : l) if (!web.unverified) x.add(web); return x; } public static boolean isLetterOrDigitOrApostrophe(char c) { return Character.isLetterOrDigit(c) || c == '\''; } public static String dropPrefixICTrim(String prefix, String s) { return trim(swic(s, prefix) ? s.substring(l(prefix)) : s); } public static String join_flex(List glues, Iterable strings) { if (strings == null) return ""; StringBuilder buf = new StringBuilder(); Iterator i = strings.iterator(); Iterator iGlues = glues.iterator(); if (i.hasNext()) { buf.append(i.next()); while (i.hasNext()) buf.append(iGlues.next()).append(i.next()); } return buf.toString(); } public static List> ai_triplesToCRefs(final List l) { if (l == null) return null; class TTCR extends LazyList> { public final int n = l(l); public int size() { return n; } public TripleRef get(int i) { return new TripleC(syncGet(l, i)); } } return new TTCR(); } public static List shortestListUnlessNull2(List a, List b) { if (a == null) return b; if (b == null) return a; return l(a) < l(b) ? a : b; } public static TreeSet litciset(String... items) { TreeSet set = caseInsensitiveSet(); for (String a : items) set.add(a); return set; } public static boolean containsNotNull(Collection c, Object o) { return o != null && contains(c, o); } public static boolean containsNotNull(Object[] x, Object o) { return o != null && contains(x, o); } public static String firstJavaToken(String s) { if (s == null) return null; int l = s.length(); int j = 0; char c, d; while (j < l) { c = s.charAt(j); d = j + 1 >= l ? '\0' : s.charAt(j + 1); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else if (c == '/' && d == '*') { do ++j; while (j < l && !s.substring(j, Math.min(j + 2, l)).equals("*/")); j = Math.min(j + 2, l); } else if (c == '/' && d == '/') { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else break; } if (j >= l) return null; int i = j; c = s.charAt(i); d = i + 1 >= l ? '\0' : s.charAt(i + 1); if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { if (s.charAt(j) == opener || s.charAt(j) == '\n') { ++j; break; } else if (s.charAt(j) == '\\' && j + 1 < l) j += 2; else ++j; } } else if (Character.isJavaIdentifierStart(c)) do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; } else if (c == '[' && d == '[') { do ++j; while (j + 1 < l && !s.substring(j, j + 2).equals("]]")); j = Math.min(j + 2, l); } else if (c == '[' && d == '=' && i + 2 < l && s.charAt(i + 2) == '[') { do ++j; while (j + 2 < l && !s.substring(j, j + 3).equals("]=]")); j = Math.min(j + 3, l); } else ++j; return quickSubstring(s, i, j); } public static boolean loadBufferedImage_useImageCache = true; public static BufferedImage loadBufferedImage(String snippetIDOrURL) { try { if (snippetIDOrURL == null) return null; if (isURL(snippetIDOrURL)) return imageIO_readURL(snippetIDOrURL); if (!isSnippetID(snippetIDOrURL)) throw fail("Not a URL or snippet ID: " + snippetIDOrURL); String snippetID = "" + parseSnippetID(snippetIDOrURL); File dir = getCacheProgramDir("Image-Snippets"); if (loadBufferedImage_useImageCache) { dir.mkdirs(); File file = new File(dir, snippetID + ".png"); if (file.exists() && file.length() != 0) try { return ImageIO.read(file); } catch (Throwable e) { e.printStackTrace(); } } String imageURL = snippetImageURL(snippetID); print("Loading image: " + imageURL); BufferedImage image = imageIO_readURL(imageURL); if (loadBufferedImage_useImageCache) { File tempFile = new File(dir, snippetID + ".tmp." + System.currentTimeMillis()); ImageIO.write(image, "png", tempFile); tempFile.renameTo(new File(dir, snippetID + ".png")); } return image; } catch (Exception __e) { throw rethrow(__e); } } public static BufferedImage loadBufferedImage(File file) { try { return file.isFile() ? ImageIO.read(file) : null; } catch (Exception __e) { throw rethrow(__e); } } public static File mkdirsFor(File file) { return mkdirsForFile(file); } public static BufferedImage loadImage2(String snippetIDOrURL) { return loadBufferedImage(snippetIDOrURL); } public static BufferedImage loadImage2(File file) { return loadBufferedImage(file); } public static byte[] intToBytes(int i) { return new byte[] { (byte) (i >>> 24), (byte) (i >>> 16), (byte) (i >>> 8), (byte) i }; } public static boolean isMD5(String s) { return l(s) == 32 && isLowerHexString(s); } public static long parseLongOpt(String s) { return isInteger(s) ? parseLong(s) : 0; } public static String postPage(String url, Object... params) { return doPost(litmap(params), url); } public static SmallestListMultiMap smallestListMultiMapIndexByMethod(Collection c, String method, Object... args) { SmallestListMultiMap map = new SmallestListMultiMap(); for (Object a : c) { Object val = callOpt(a, method, args); if (val != null) map.put(val, a); } return map; } public static IterableIterator linesFromFile(File f) { try { if (!f.exists()) return emptyIterableIterator(); if (ewic(f.getName(), ".gz")) return linesFromReader(utf8bufferedReader(newGZIPInputStream(f))); return linesFromReader(utf8bufferedReader(f)); } catch (Exception __e) { throw rethrow(__e); } } public static String getMemoryInfo() { long total = Runtime.getRuntime().totalMemory(); long avail = Runtime.getRuntime().freeMemory(); return "Heap: " + iceil(toM_double(total - avail)) + "/" + iceil(toM_double(total)) + " MB"; } public static String getProcessID() { return getPID(); } public static Map multiSetToMap(MultiSet ms) { return ms == null ? null : ms.asMap(); } public static B pairB(Pair p) { return p == null ? null : p.b; } public static List cloneSubList(List l, int startIndex, int endIndex) { return newSubList(l, startIndex, endIndex); } public static List cloneSubList(List l, int startIndex) { return newSubList(l, startIndex); } public static void ai_spec_possiblyToActually(Collection webs) { for (Web web : webs) ai_spec_possiblyToActually(web); } public static void ai_spec_possiblyToActually(Web web) { T3 t = webToTriple(web); if (t == null) return; Matches m = new Matches(); if (swic(t.b, "possibly ", m)) ai_postTriple(t.a, m.rest(), t.c); } public static void saveGZStructure(String file, Object o) { saveGZStructureToFile(file, o); } public static void saveGZStructure(File file, Object o) { saveGZStructureToFile(file, o); } public static File countTillNewFile(File base) { return countTillNewFile(base, ""); } public static File countTillNewFile(File base, String suffix) { File f; long i = 1; while ((f = newFile(f2s(base) + i + suffix)).exists()) ++i; return f; } public static void ai_spec_answerPattern() { if (!ai_postingInSubSpace()) return; for (String q : ai_texts_unquote("$X", "should be", "answered")) { for (Pair p : ai_texts_xy_unquoteBoth("$X", "is an answer pattern for", "$Y")) { Map match = matchDollarVarsIC(p.b, q); if (match != null) post(".", "Look for answer pattern", quote(formatDollarVarsIC(p.a, match))); else print("no match: " + unquote(p.b) + " / " + q); } } } public static void ai_spec_daysOld() { for (String pat : ai_texts_unquote(".", "Look for answer pattern", "$X")) { Map match = matchDollarVarsIC("$X is $Y days old", pat); { if (!(match != null)) continue; } String who = match.get("$X"); for (String dayBorn : filterMatchDollarX_IC("born on day $X", ai_texts(who, "was", "$Y"))) { String today = matchDollarX_IC("day $X", ai_text("it", "today is", "$X")); if (isInteger(today) && isInteger(dayBorn)) { long age = parseLong(today) - parseLong(dayBorn); post("Answer", "is", who + " is " + age + " days old"); } } } } public static List listFilesWithSuffix(File dir, String suffix) { List l = new ArrayList(); for (File f : listFiles(dir)) if (!f.isDirectory() && (empty(suffix) || endsWithIgnoreCase(f.getName(), suffix))) l.add(f); return l; } public static long toLong(Object o) { if (o instanceof Number) return ((Number) o).longValue(); if (o instanceof String) return parseLong((String) o); return 0; } public static Map synchronizedMRUCache(int maxSize) { return synchroMap(new MRUCache(maxSize)); } public static List splitAtSimpleArrow(String s) { return splitAtSimpleArrow(javaTok(s)); } public static List splitAtSimpleArrow(List tok) { List l = new ArrayList(); int i = 0; while (i < l(tok)) { int j = indexOfSubList(tok, ll("-", "", ">"), i); if (i >= l(tok)) break; if (j < 0) j = l(tok); l.add(trimJoin(tok.subList(i, j))); i = j + 3; } return l; } public static String dropSuffixIgnoreCase(String suffix, String s) { return ewic(s, suffix) ? s.substring(0, l(s) - l(suffix)) : s; } public static List> tok_classifyEnglishWordsWithOriginal(List l) { return map(new F1>() { public List get(String s) { try { List set = tok_classifyEnglishWord(s); set.add(quote(s)); return set; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "L set = tok_classifyEnglishWord(s);\r\n set.add(quote(s));\r\n ret set;"; } }, l); } public static List nlTok5(String s) { List tok = new ArrayList(); int l = s.length(); int i = 0; while (i < l) { int j = i; char c; String cc; while (j < l) { c = s.charAt(j); cc = s.substring(j, Math.min(j + 2, l)); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else if (cc.equals("/*")) { do ++j; while (j < l && !s.substring(j, Math.min(j + 2, l)).equals("*/")); j = Math.min(j + 2, l); } else if (cc.equals("//")) { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else break; } tok.add(s.substring(i, j)); i = j; if (i >= l) break; c = s.charAt(i); cc = s.substring(i, Math.min(i + 2, l)); if (c == '“' || c == '”') c = '"'; if (c == '"') { char opener = c; ++j; while (j < l) { char _c = s.charAt(j); if (_c == '“' || _c == '”') _c = '"'; if (_c == opener) { ++j; break; } else if (s.charAt(j) == '\\' && j + 1 < l) j += 2; else ++j; } if (j - 1 >= i + 1) { tok.add(opener + s.substring(i + 1, j - 1) + opener); i = j; continue; } } else if (Character.isJavaIdentifierStart(c)) do ++j; while (j < l && Character.isJavaIdentifierPart(s.charAt(j))); else if (Character.isDigit(c)) do ++j; while (j < l && Character.isDigit(s.charAt(j))); else if (cc.equals("[[")) { do ++j; while (j + 1 < l && !s.substring(j, j + 2).equals("]]")); j = Math.min(j + 2, l); } else if (cc.equals("[=") && i + 2 < l && s.charAt(i + 2) == '[') { do ++j; while (j + 2 < l && !s.substring(j, j + 3).equals("]=]")); j = Math.min(j + 3, l); } else if (s.substring(j, Math.min(j + 3, l)).equals("...")) j += 3; else if (c == '$' || c == '#') do ++j; while (j < l && Character.isDigit(s.charAt(j))); else ++j; tok.add(s.substring(i, j)); i = j; } if ((tok.size() % 2) == 0) tok.add(""); return tok; } public static List listWithoutIC(List a, String... b) { Set set = litCISet(b); List l = new ArrayList(); for (String s : a) if (!set.contains(s)) l.add(s); return l; } public static boolean isOpeningTag(String token, String tag) { return isTag(token, tag) && !token.endsWith("/>"); } public static boolean isOpeningTag(String token) { return token.startsWith("<") && token.endsWith(">") && !token.endsWith("/>") && isLetter(token.charAt(1)); } public static Map htmlgetparams(String tag) { return getHtmlTagParameters(tag); } public static List dropAllTags(List tok) { List list = new ArrayList(); for (int i = 0; i < l(tok); i++) { String t = tok.get(i); if (odd(i) && t.startsWith("<")) { list.set(list.size() - 1, list.get(list.size() - 1) + tok.get(i + 1)); ++i; } else list.add(t); } return list; } public static String dropAllTags(String html) { return join(dropAllTags(htmlcoarsetok(html))); } public static List scanMapLog(String progID, String fileName) { return scanMapLog(getProgramFile(progID, fileName)); } public static List scanMapLog(String fileName) { return scanMapLog(getProgramFile(fileName)); } public static List scanMapLog(File file) { List l = new ArrayList(); for (String s : scanLog(file)) try { l.add(unstructureMap(s)); } catch (Throwable __e) { _handleException(__e); } return l; } public static List htmlcoarsetok(String s) { List tok = new ArrayList(); int l = s.length(); int i = 0; while (i < l) { int j = i; char c; while (j < l) { if (s.charAt(j) != '<') ++j; else if (s.substring(j, Math.min(j + 4, l)).equals("")); j = Math.min(j + 3, l); } else break; } tok.add(s.substring(i, j)); i = j; if (i >= l) break; c = s.charAt(i); if (c == '<') { ++j; while (j < l && s.charAt(j) != '>') ++j; if (j < l) ++j; } tok.add(s.substring(i, j)); i = j; } if ((tok.size() % 2) == 0) tok.add(""); return tok; } public static boolean htmlCheckParams(String tag, Object... params) { return checkFields(htmlgetparams(tag), params); } public static void logQuoted(String logFile, String line) { logQuoted(getProgramFile(logFile), line); } public static void logQuoted(File logFile, String line) { appendToFile(logFile, quote(line) + "\n"); } public static SimpleDateFormat simpleDateFormat_local(String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); sdf.setTimeZone(localTimeZone()); return sdf; } public static List jsonTok(String s) { List tok = new ArrayList(); int l = s.length(); int i = 0; while (i < l) { int j = i; char c; String cc; while (j < l) { c = s.charAt(j); cc = s.substring(j, Math.min(j + 2, l)); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else if (cc.equals("/*")) { do ++j; while (j < l && !s.substring(j, Math.min(j + 2, l)).equals("*/")); j = Math.min(j + 2, l); } else if (cc.equals("//")) { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else break; } tok.add(s.substring(i, j)); i = j; if (i >= l) break; c = s.charAt(i); if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { if (s.charAt(j) == opener) { ++j; break; } else if (s.charAt(j) == '\\' && j + 1 < l) j += 2; else ++j; } } else if (Character.isLetter(c)) do ++j; while (j < l && Character.isLetter(s.charAt(j))); else if (Character.isDigit(c)) do ++j; while (j < l && Character.isDigit(s.charAt(j))); else ++j; tok.add(s.substring(i, j)); i = j; } if ((tok.size() % 2) == 0) tok.add(""); return tok; } public static long parseLong(String s) { if (s == null) return 0; return Long.parseLong(dropSuffix("L", s)); } public static long parseLong(Object s) { return Long.parseLong((String) s); } public static List shortestList3(List a, List b, List c) { int la = l(a), lb = l(b); if (la < lb) return l(c) < la ? c : a; else return l(c) < lb ? c : b; } public static Web webs_search_noVar(Web searchWeb, Collection webs) { for (Web web : webs) if (web_matchAllPerms(searchWeb, web) != null) return web; return null; } public static Map ai_negateVerbPhrase_map = litcimap("am", "am not", "are", "are not", "is", "is not", "has", "doesn't have", "have", "don't have"); public static String ai_negateVerbPhrase(String s) { { String _a_2800 = ai_negateVerbPhrase_map.get(s); if (!empty(_a_2800)) return _a_2800; } return isThirdPersonVerb(s) ? "doesn't " + s : "don't " + s; } public static List ai_superclasses(String s) { return ai_texts(s, "is", "$X"); } public static List ai_superclasses(String s, String rel) { return ai_texts(s, rel, "$X"); } public static String dropQuestionMark(String s) { return dropSuffixTrim("?", s); } public static A printStruct(String prefix, A a) { printStructure(prefix, a); return a; } public static A printStruct(A a) { printStructure(a); return a; } public static String ai_mergeSolutions(List l) { return ai_renderAndList(l); } public static String ai_mergeSolutions(List l, String emptyText) { return ai_renderAndList(l, emptyText); } public static boolean ai_isKnownThing(String s) { return ai_text(s, "is", "$X") != null; } public static String curlyBraceIfContainsArrow(String s) { return tok_containsSimpleArrow(s) ? optionalCurlyBrace(s) : s; } public static String ai_fix(String s) { s = trim(s); if (startsWith(s, "{") && endsWith(s, "}")) s = trim(dropFirstAndLast(s)); return s; } public static boolean isAdverb(String s) { return containsNotNull(knownAdverbs(), s); } public static String dropLastJavaToken(String s) { return join(dropLast(2, javaTok(s))); } public static String dropFirstJavaToken(String s) { return join(dropFirst(2, javaTok(s))); } public static int ai_tok_combinePhrases_maxPhraseLength = 3; public static List ai_tok_combinePhrases(List tok) { List l = new ArrayList(); int n = l(tok); bigloop: for (int i = 0; i < l(tok); i++) { String t = tok.get(i); if (odd(i)) { String phrase = t; int j = i + 2; while (j < n && j < i + ai_tok_combinePhrases_maxPhraseLength * 2) { phrase += " " + tok.get(j); if (isKnownPhrase(phrase)) { l.add(joinSubList(tok, i, j + 1)); i = j; continue bigloop; } j += 2; } } l.add(t); } return l; } public static int indexOfLL(List x, A... y) { outer: for (int i = 0; i + l(y) <= l(x); i++) { for (int j = 0; j < l(y); j++) if (neq(x.get(i + j), y[j])) continue outer; return i; } return -1; } public static boolean eqGet(List l, int i, Object o) { return eq(get(l, i), o); } public static int tok_smartIndexOfFirstSpace(List tok, int i) { for (; i < l(tok); i++) if (even(i) && nempty(tok.get(i))) return i; return i; } public static boolean eqicGetOneOf(List l, int i, String... options) { return eqicOneOf(get(l, i), options); } public static String verbToThirdPerson(String s) { { String _a_2832 = verbToThirdPersonMap().get(lower(s)); if (!empty(_a_2832)) return _a_2832; } return s + "s"; } public static String verbToPast(String s) { { String _a_2837 = verbToPastMap().get(lower(s)); if (!empty(_a_2837)) return _a_2837; } return s + "s"; } public static String joinNempties(String sep, String... strings) { return joinStrings(sep, strings); } public static String joinNempties(String sep, Collection strings) { return joinStrings(sep, strings); } public static Set ai_is_myYourEtc = litciset("a", "my", "your", "their", "his", "our", "the"); public static boolean ai_is_myYourEtc(String s) { return containsNotNull(ai_is_myYourEtc, s); } public static Set ai_is_myYourEtc_maybe = litciset("this", "that", "all", "some"); public static boolean ai_is_myYourEtc_maybe(String s) { return containsNotNull(ai_is_myYourEtc_maybe, s); } public static boolean isNoun(String s) { if (ai_enabled() && has(s, "is", "a noun")) return true; return isSingularNoun(s) || isPluralNoun(s); } public static boolean isKnownPhrase(String s) { return containsNotNull(knownPhrases(), s); } public static Cache> nounPhraseContinuers_cache = new Cache("nounPhraseContinuers_load"); public static Set nounPhraseContinuers() { return nounPhraseContinuers_cache.get(); } public static Set nounPhraseContinuers_load() { return new TreeSet(tlft(loadSnippet("#1011080"))); } public static String md5OfFile(File f) { try { if (!f.exists()) return "-"; MessageDigest md5 = MessageDigest.getInstance("MD5"); FileInputStream in = new FileInputStream(f); try { byte[] buf = new byte[65536]; int l; while (true) { l = in.read(buf); if (l <= 0) break; md5.update(buf, 0, l); } return bytesToHex(md5.digest()); } finally { _close(in); } } catch (Exception __e) { throw rethrow(__e); } } public static String loadTextFilePossiblyGZipped(String fileName) { return loadTextFilePossiblyGZipped(fileName, null); } public static String loadTextFilePossiblyGZipped(String fileName, String defaultContents) { File gz = new File(fileName + ".gz"); return gz.exists() ? loadGZTextFile(gz) : loadTextFile(fileName, defaultContents); } public static String loadTextFilePossiblyGZipped(File fileName) { return loadTextFilePossiblyGZipped(fileName, null); } public static String loadTextFilePossiblyGZipped(File fileName, String defaultContents) { return loadTextFilePossiblyGZipped(fileName.getPath(), defaultContents); } public static File getCodeProgramDir() { return getCodeProgramDir(getProgramID()); } public static File getCodeProgramDir(String snippetID) { return new File(javaxCodeDir(), formatSnippetID(snippetID)); } public static File getCodeProgramDir(long snippetID) { return getCodeProgramDir(formatSnippetID(snippetID)); } public static boolean regionMatchesIC(String a, int offsetA, String b, int offsetB, int len) { return a != null && a.regionMatches(true, offsetA, b, offsetB, len); } public static LinkedHashSet litorderedset(A... items) { LinkedHashSet set = new LinkedHashSet(); for (A a : items) set.add(a); return set; } public static boolean eqOneOf(Object o, Object... l) { for (Object x : l) if (eq(o, x)) return true; return false; } public static int scoredSearch_score_single(String s, String query) { int i = indexOfIC_underscore(s, query); if (i < 0) return 0; if (i > 0) return 1; return l(s) == l(query) ? 3 : 2; } public static void sort(T[] a, Comparator c) { Arrays.sort(a, c); } public static void sort(T[] a) { Arrays.sort(a); } public static void sort(List a, Comparator c) { Collections.sort(a, c); } public static void sort(List a) { Collections.sort(a); } public static Comparator mapComparatorDesc(final Map map) { return new Comparator() { public int compare(A a, A b) { return cmp(map.get(b), map.get(a)); } }; } public static List toLinesFullTrim(String s) { List l = new ArrayList(); for (String line : toLines(s)) if (nempty(line = trim(line))) l.add(line); return l; } public static List toLinesFullTrim(File f) { List l = new ArrayList(); for (String line : linesFromFile(f)) if (nempty(line = trim(line))) l.add(line); return l; } public static File getGlobalCache() { File file = new File(javaxCachesDir(), "Binary Snippets"); file.mkdirs(); return file; } public static Map computerVMs() { LinkedHashMap vms = new LinkedHashMap(); for (Map inj : computerInjectionsData()) putIfNotExisting(vms, getInt(inj, "VM"), inj); return vms; } public static void mapRemoveAll(Map map, A... keys) { if (map != null) for (A a : keys) map.remove(a); } public static TreeSet numbersOfMethodArguments(Object o, String method) { Class c; boolean mustBeStatic = false; if (o instanceof Class) { c = (Class) o; mustBeStatic = true; } else c = o.getClass(); TreeSet numbers = new TreeSet(); while (c != null) { for (Method m : c.getDeclaredMethods()) { if (!m.getName().equals(method)) continue; if (mustBeStatic && !methodIsStatic(m)) continue; numbers.add(l(m.getParameterTypes())); } c = c.getSuperclass(); } return numbers; } public static String ai_verifySafeToCall(String sfName) { if (!ai_isSafeToCall(sfName)) throw fail("Not safe to call: " + sfName); return sfName; } public static F1 f1(Object f) { return functionToF1(f); } public static String upper(String s) { return s == null ? null : s.toUpperCase(); } public static boolean startsWithWord(String a, String b, Matches m) { if (!endsWithLetterOrDigit(b)) b += " "; if (!startsWithIC(a, b)) return false; if (m != null) m.m = new String[] { trim(substring(a, l(b))) }; return true; } public static boolean startsWithWord(String a, String b) { return startsWithWord(a, b, null); } public static NavigableSet navigableKeys(NavigableMap map) { return map == null ? new TreeSet() : map.navigableKeySet(); } public static NavigableSet navigableKeys(MultiSet ms) { return ((NavigableMap) ms.map).navigableKeySet(); } public static NavigableSet navigableKeys(MultiMap mm) { return ((NavigableMap) mm.data).navigableKeySet(); } public static void arraycopy(Object[] a, Object[] b) { int n = min(a.length, b.length); for (int i = 0; i < n; i++) b[i] = a[i]; } public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int n) { System.arraycopy(src, srcPos, dest, destPos, n); } public static A listGet(List l, int idx) { return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null; } public static Cache>> english1000VerbQuintuples_cache = new Cache("english1000VerbQuintuples_load"); public static List> english1000VerbQuintuples() { return english1000VerbQuintuples_cache.get(); } public static List> english1000VerbQuintuples_load() { List> out = new ArrayList(); for (String s : tlft_j(loadSnippet("#1010402"))) { List l = splitAtSpace(s); if (l(l) != 6) { } else { l = dropFirst(l); out.add(l); } } return out; } public static HashSet lithashset(A... items) { HashSet set = new HashSet(); for (A a : items) set.add(a); return set; } public static Pair web_matchTriple_dollarVars(String a, String b, String c, Web web) { List nodes = web_nodes(web); if (l(nodes) == 1) { WebNode n = first(nodes); if (nodeMatch_dollarVars(b, web_getRelationOpt(n, n)) == 0 && nodeMatch_dollarVars(a, n) == 0 && nodeMatch_dollarVars(c, n) == 0) return pair(n); } else if (l(nodes) == 2) { WebNode n1 = first(nodes), n2 = second(nodes); if (nodeMatch_dollarVars(b, web_getRelationOpt(n1, n2)) == 0 && nodeMatch_dollarVars(a, n1) == 0 && nodeMatch_dollarVars(c, n2) == 0) return pair(n1, n2); if (nodeMatch_dollarVars(b, web_getRelationOpt(n2, n1)) == 0 && nodeMatch_dollarVars(a, n2) == 0 && nodeMatch_dollarVars(c, n1) == 0) return pair(n2, n1); } return null; } public static List scanLog(String progID, String fileName) { return scanLog(getProgramFile(progID, fileName)); } public static List scanLog(String fileName) { return scanLog(getProgramFile(fileName)); } public static List scanLog(File file) { List l = new ArrayList(); for (File f : concatLists(earlierPartsOfLogFile(file), ll(file))) for (String s : toLines(file)) if (isProperlyQuoted(s)) l.add(unquote(s)); return l; } public static int indexOfIgnoreCase(List a, String b) { return indexOfIgnoreCase(a, b, 0); } public static int indexOfIgnoreCase(List a, String b, int i) { int n = l(a); for (; i < n; i++) if (eqic(a.get(i), b)) return i; return -1; } public static int indexOfIgnoreCase(String a, String b) { return indexOfIgnoreCase_manual(a, b); } public static List web_forwardRelated(WebNode node) { return web_allForwardRelated(node); } public static boolean web_hasText(WebNode node, String text) { return web_hasLabelIC(node, text); } public static boolean web_hasText(Pair p, String text) { return web_hasLabelIC(p, text); } public static boolean web_hasText(WebNode a, WebNode b, String text) { return web_hasLabelIC(a, b, text); } public static WebRelation web_getRelation(WebNode a, WebNode b) { return a.web.getRelation(a, b); } public static long unixTime() { return unixTime(now()); } public static long unixTime(long timestamp) { return timestamp / 1000; } public static List web_nodes(Web web) { return web_nonRelationNodes(web); } public static List> allPermutations(List l) { List> out = new ArrayList(); allPermutations_impl(cloneList(l), l(l), out); return out; } public static void allPermutations_impl(List l, int n, List> out) { if (n <= 1) { ping(); out.add(cloneList(l)); } else for (int i = 0; i < n; i++) { allPermutations_impl(l, n - 1, out); swapElements(l, n % 2 == 0 ? i : 0, n - 1); } } public static boolean web_matchNodeLists_x_debug; public static int web_matchNodeLists_x(List l1, List l2, Object nodeMatcher) { if (l(l1) != l(l2)) return -1000; int score = 0; if (empty(l1)) return score; HashMap map = twoListsToHashMap(l1, l2); Web web = first(l1).web, web2 = first(l2).web; for (int i = 0; i < l(l1); i++) { WebNode a = l1.get(i), b = l2.get(i); score += toDouble(callF(nodeMatcher, a, b)); } for (WebRelation rel : web_relationObjects(web)) { WebNode aa = map.get(rel.a), bb = map.get(rel.b); if (aa == null || bb == null) continue; WebRelation rel2 = web2.getRelationOpt(aa, bb); if (web_matchNodeLists_x_debug) print("a=" + rel.a + ", b=" + rel.b + ", aa=" + aa + ", bb=" + bb + ", rel2 labels=" + (rel2 == null ? "-" : str(rel2.labels)) + ", labels=" + rel.labels); if (rel2 == null) --score; else score += toDouble(callF(nodeMatcher, rel, rel2)); } return score; } public static LinkedHashMap twoListsToOrderedMap(List l1, List l2) { if (l(l1) != l(l2)) throw fail("Differing list size: " + l(l1) + "/" + l(l2)); LinkedHashMap map = new LinkedHashMap(); int n = l(l1); for (int i = 0; i < n; i++) map.put(l1.get(i), l2.get(i)); return map; } public static A firstKey(Map map) { return first(keys(map)); } public static Map cloneMap(Map map) { if (map == null) return litmap(); synchronized (map) { return map instanceof TreeMap ? new TreeMap((TreeMap) map) : map instanceof LinkedHashMap ? new LinkedHashMap(map) : new HashMap(map); } } public static void mapPut(Map map, A key, B value) { if (map != null && key != null && value != null) map.put(key, value); } public static WebRelation web_findRelationObject(WebNode a, WebNode b) { return a.web.getRelationOpt(a, b); } public static String ai1_visitorsInfo() { return loadPageSilently("http://www.botcompany.de:8080/1002157/raw"); } public static List indexedNodesContainingDollarVars() { List l = new ArrayList(); MultiMap index = cachedNodeIndex(); for (String s : keys(index)) if (containsDollarVars(s)) l.addAll(index.get(s)); return l; } public static WebNode cloneWeb_handlingNode(WebNode node) { return cloneWebWithMap(node.web).b.get(node); } public static Web web_replaceDollarVars(Web web, final Map map) { for (WebNode node : web.nodes) web_modifyTexts(node, new F1() { public Object get(String s) { try { return formatDollarVars(s, map); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "formatDollarVars(s, map)"; } }); return web; } public static boolean shortClassNameIs(Object o, String name) { return eq(shortClassName(o), name); } public static void printException(Throwable e) { printStackTrace(e); } public static boolean containsJavaToken(String s, String t) { return contains(javaTokC(s), t); } public static String ai_ifAndInput_v2(String rule, String input) { print("ai_ifAndInput_v2"); Matches m = new Matches(); if (!simpleStarMatchOnCTokensIC("if * and input * then say *", javaTokDroppingBracketsC_unquote(rule), m)) unsuitableRule(); String d2 = m.unq(1); printStructs("d2", d2, "input", input); String x = matchBigX(d2, input); printStructs("x", x); if (x == null) return null; String pat = formatVars(m.unq(0), litmap("X", x, "Y", "$X")); printStruct("pat", pat); T3 t = ai_tripelize(pat); printStruct("t", t); String y = random(ai_texts(t)); if (y == null) return null; return ai_replaceBigY(m.unq(2), y); } public static boolean simpleStarMatchOnCTokensIC(String pat, List tok, Matches m) { List tokPat = javaTokC(pat); if (l(tokPat) != l(tok)) return false; List matches = new ArrayList(); for (int i = 0; i < l(tokPat); i++) { String p = tokPat.get(i), t = tok.get(i); if (eq(p, "*")) { matches.add(t); continue; } if (!eqic(p, t)) return false; } m.m = toStringArray(matches); return true; } public static List javaTokDroppingBracketsC(String s) { return codeTokens(javaTokDroppingBrackets(s)); } public static UnsuitableRuleException unsuitableRule() { throw new UnsuitableRuleException(); } public static String ai_bigXToDollarX(String s) { return join(jreplace_return(javaTok(s), "X", "$X")); } public static String ai_replaceBigX(String s, String value) { return join(jreplace_return(javaTok(s), "X", value)); } public static Map ai_getOrCreateVars() { Map map = ai_getVars(); if (map == null) ai_setVars(map = new HashMap()); return map; } public static void ai_setVars(Map map) { ai_getVar_map.set(map); } public static void ai_setVars(Object... values) { ai_setVars(paramsToMap(values)); } public static String getTranspiledStandardFunctionsIfCached(TreeSet functionSet) { try { File f = transpiledStandardFunctions_file(functionSet); printFileStatus(f); return loadTextFile(f); } catch (Throwable __e) { return null; } } public static Class veryQuickJava_finish(String src, List libs) { libs = cloneList(libs); src = findTranslators2(src, libs); String dehlibs = join(" ", libs); File bytecode = null; bytecode = javaCompile_overInternalBot(src, dehlibs); return hotwireCore(concatLists(ll(bytecode), loadLibraries(libs))); } public static boolean loadFunctions_debug; public static Object loadFunctions_veryQuickJava; public static Class loadFunctions(String... functions) { return loadFunctions(asList(functions)); } public static Class loadFunctions(Collection functions) { TreeSet functionSet = new TreeSet(functions); StringBuilder buf = new StringBuilder(); for (String s : functionSet) buf.append("please include function " + assertIdentifier(s) + ".\n"); long time = sysNow(); Class c; if (loadFunctions_veryQuickJava != null) c = (Class) callF(loadFunctions_veryQuickJava, str(buf)); else { veryQuickJava_transpiled.set(""); c = veryQuickJava(str(buf)); saveTranspiledStandardFunctions(functionSet, getAndClearThreadLocal(veryQuickJava_transpiled)); } if (loadFunctions_debug) done2_always(time, "loadFunctions: " + join(" ", functions)); return c; } public static RuntimeException todo() { throw new RuntimeException("TODO"); } public static RuntimeException todo(String msg) { throw new RuntimeException("TODO: " + msg); } public static boolean matchDollarVarIC(Map match, String pat, String s) { if (isDollarVar(pat)) return strictPutIC(match, pat, s); else return eqic(pat, s); } public static String getClientIPFromHeaders(Map headers) { String remoteAddr = (String) (headers.get("remote-addr")); String client = (String) (headers.get("x-forwarded-for")); if (nempty(client)) remoteAddr += "," + client; return remoteAddr; } public static String getClientIP_subBot() { Object session = call(getMainBot(), "getSession"); Map headers = (Map) (call(session, "getHeaders")); return getClientIPFromHeaders(headers); } public static String assertIPv4(String s) { assertTrue(s, isIPv4(s)); return s; } public static void logQuotedWithTime(String s) { logQuotedWithTime(standardLogFile(), s); } public static void logQuotedWithTime(File logFile, String s) { logQuoted(logFile, logQuotedWithTime_format(s)); } public static void logQuotedWithTime(String logFile, String s) { logQuoted(logFile, logQuotedWithTime_format(s)); } public static String logQuotedWithTime_format(String s) { return formatGMTWithDate_24(now()) + " " + s; } public static File DiskSnippetCache_file(long snippetID) { return new File(getGlobalCache(), "data_" + snippetID + ".jar"); } public static File DiskSnippetCache_getLibrary(long snippetID) throws IOException { File file = DiskSnippetCache_file(snippetID); return file.exists() ? file : null; } public static void DiskSnippetCache_putLibrary(long snippetID, byte[] data) throws IOException { saveBinaryFile(DiskSnippetCache_file(snippetID), data); } public static byte[] loadDataSnippetImpl(String snippetID) throws IOException { byte[] data; try { URL url = new URL(dataSnippetLink(snippetID)); print("Loading library: " + hideCredentials(url)); try { data = loadBinaryPage(url.openConnection()); } catch (RuntimeException e) { data = null; } if (data == null || data.length == 0) { url = new URL("http://data.tinybrain.de/blobs/" + parseSnippetID(snippetID)); print("Loading library: " + hideCredentials(url)); data = loadBinaryPage(url.openConnection()); } print("Bytes loaded: " + data.length); } catch (FileNotFoundException e) { throw new IOException("Binary snippet #" + snippetID + " not found or not public"); } return data; } public static File loadDataSnippetToFile(String snippetID) { try { snippetID = fsI(snippetID); File f = DiskSnippetCache_file(parseSnippetID(snippetID)); List urlsTried = new ArrayList(); List errors = new ArrayList(); try { URL url = addAndReturn(urlsTried, new URL(dataSnippetLink(snippetID))); print("Loading library: " + hideCredentials(url)); try { loadBinaryPageToFile(openConnection(url), f); if (fileSize(f) == 0) throw fail(); } catch (Throwable e) { errors.add(e); url = addAndReturn(urlsTried, new URL("http://data.tinybrain.de/blobs/" + psI(snippetID))); print("Trying other server: " + hideCredentials(url)); loadBinaryPageToFile(openConnection(url), f); print("Got bytes: " + fileSize(f)); } if (fileSize(f) == 0) throw fail(); System.err.println("Bytes loaded: " + fileSize(f)); } catch (Throwable e) { printStackTrace(e); errors.add(e); throw fail("Binary snippet " + snippetID + " not found or not public. URLs tried: " + allToString(urlsTried) + ", errors: " + allToString(errors)); } return f; } catch (Exception __e) { throw rethrow(__e); } } public static List ai_fewestIndexedWebs_verified(String a, String b, String c) { return web_verifiedWebsOnly(ai_fewestIndexedWebs(a, b, c)); } public static void printTo(StringBuilder buf, Object o) { buf.append(str(o)).append("\n"); } public static void printTo(StringBuilder buf) { printTo(buf, ""); } public static String asciiHeading2(String title) { return asciiHeading(title) + "\n"; } public static Map filterMap(Map map, Object f) { Map m2 = similarEmptyMap(map); for (A a : keys(map)) { B b = map.get(a); if (isTrue(callF(f, a, b))) m2.put(a, b); } return m2; } public static Map filterMap(Object f, Map map) { return filterMap(map, f); } public static String repeat(char c, int n) { n = Math.max(n, 0); char[] chars = new char[n]; for (int i = 0; i < n; i++) chars[i] = c; return new String(chars); } public static List repeat(A a, int n) { n = Math.max(n, 0); List l = new ArrayList(n); for (int i = 0; i < n; i++) l.add(a); return l; } public static List repeat(int n, A a) { return repeat(a, n); } public static Object interceptPrintInThisThread(Object f) { Object old = print_byThread().get(); print_byThread().set(f); return old; } public static String htitle_noEncode(String title) { return tag("title", title); } public static String[] drop(int n, String[] a) { n = Math.min(n, a.length); String[] b = new String[a.length - n]; System.arraycopy(a, n, b, 0, b.length); return b; } public static Object[] drop(int n, Object[] a) { n = Math.min(n, a.length); Object[] b = new Object[a.length - n]; System.arraycopy(a, n, b, 0, b.length); return b; } public static void copyList(Collection a, Collection b) { if (a == null || b == null) return; b.clear(); b.addAll(a); } public static void copyListeners(Object a, Object b) { for (String f : fieldNames(a)) if (l(f) > 2 && startsWith(f, "on") && isUpperCase(f.charAt(2))) setOpt(b, f, getOpt(a, f)); } public static String[] conceptFields_drop = { "className", "fieldValues", "id", "created", "refs", "backRefs", "_concepts" }; public static Set conceptFields(Concept c) { return setMinus(mergeSets(allNonStaticNonTransientFields(c), keys(c.fieldValues)), conceptFields_drop); } public static Set conceptFields(Class c) { return setMinus(allNonStaticNonTransientFields(c), conceptFields_drop); } public static Object cget(Object c, String field) { Object o = getOpt(c, field); if (o instanceof Concept.Ref) return ((Concept.Ref) o).get(); return o; } public static boolean isPrimitiveBoolField(Object o, String field) { Field f = findField2(o, field); return f != null && eq(f.getType(), boolean.class); } public static boolean containsNulls(Collection c) { return contains(c, null); } public static Map similarEmptyMap(Map m) { if (m instanceof TreeMap) return new TreeMap(((TreeMap) m).comparator()); if (m instanceof LinkedHashMap) return new LinkedHashMap(); return new HashMap(); } public static List concatMap(Object f, Iterable l) { return concatLists(map(f, l)); } public static List concatMap(Iterable l, Object f) { return concatMap(f, l); } public static List concatMap(Object f, Object[] l) { return concatLists(map(f, l)); } public static List> web_nonEmptyRelations(Web web) { List> l = new ArrayList(); for (Pair r : web_relations(web)) if (nempty(web.getRelation(r).labels())) l.add(r); return l; } public static String dateGMT() { return formatGMTWithOptionalDate_24(now()); } public static String dateGMT(long time) { return formatGMTWithOptionalDate_24(time); } public static List web_nonRelationNodes(Web web) { List l = new ArrayList(); for (WebNode n : web.nodes) if (!(n instanceof WebRelation)) l.add(n); return l; } public static void pnlIndent(Collection l) { print_noNewLine(indentx(numberedLines(l))); } public static List collectMethod(Collection c, String methodName) { return mapMethod(c, methodName); } public static List collectMethod(String methodName, Collection c) { return mapMethod(c, methodName); } public static void web_printRelations(Web web) { List> relations = web_nonEmptyRelations(web); if (nempty(relations)) { for (Pair p : relations) { WebNode rel = web.getRelation(p); String lbl = rel.text(); if (l(rel.labels) > 1) lbl += "+"; printIndent(p.a.text() + " -> " + lbl + " -> " + p.b.text()); } print(); } } public static void removeSubList(List l, int from, int to) { subList(l, from, to).clear(); } public static void removeSubList(List l, int from) { subList(l, from).clear(); } public static T3 tripleToLower(T3 t) { return t == null ? null : t3(lower(t.a), lower(t.b), lower(t.c)); } public static List emptyListWithCapacity(Collection c) { return new ArrayList(l(c)); } public static List emptyListWithCapacity(int n) { return new ArrayList(n); } public static List sortByCalculatedFieldDesc_f1(Collection c, final F1 f) { List l = cloneList(c); sort(l, new Comparator() { public int compare(A a, A b) { return stdcompare(callF(f, b), callF(f, a)); } }); return l; } public static boolean web_hasLabels(WebNode n) { return n.hasLabels(); } public static List sortedDescAccordingTo(Collection c, final Object scoreF) { List l = cloneList(c); sort(l, new Comparator() { public int compare(A a, A b) { return stdcompare(toDouble(callF(scoreF, b)), toDouble(callF(scoreF, a))); } }); return l; } public static GlobalMemory oshi_systemMemory() { return oshi_hardware().getMemory(); } public static A randomFromIterator(int n, Iterator it) { n = random(n); while (n-- > 0) if (it.hasNext()) it.next(); else return null; return it.hasNext() ? it.next() : null; } public static int ai_indexSize() { return l(ai_mainIndexKeys()); } public static IterableIterator indexedTerms_iterator() { return concurrentlyIterateSet(ai_navigableShortTerms()); } public static String formatSnippetIDOpt(String s) { return isSnippetID(s) ? formatSnippetID(s) : s; } public static int findEndOfCurlyBracketPart(List cnc, int i) { int j = i + 2, level = 1; while (j < cnc.size()) { if (eq(cnc.get(j), "{")) ++level; else if (eq(cnc.get(j), "}")) --level; if (level == 0) return j + 1; ++j; } return cnc.size(); } public static String joinSubList(List l, int i, int j) { return join(subList(l, i, j)); } public static String joinSubList(List l, int i) { return join(subList(l, i)); } public static boolean canSimpleMatchTriple_dollarVars(T3 pat, T3 t) { if (pat == null || t == null) return false; return simpleMatchTriple_dollarVars(pat.a, pat.b, pat.c, t) != null; } public static boolean canSimpleMatchTriple_dollarVars(String patA, String patB, String patC, T3 t) { return simpleMatchTriple_dollarVars(patA, patB, patC, t) != null; } public static TripleRef modifyTripleRef(TripleRef r, int position) { return r.position() == position ? r : tripleRef(r.triple, position); } public static String snippetImageURL(String snippetID) { return snippetImageURL(snippetID, "png"); } public static String snippetImageURL(String snippetID, String contentType) { long id = parseSnippetID(snippetID); String url; if (id == 1000010 || id == 1000012) url = "http://tinybrain.de:8080/tb/show-blobimage.php?id=" + id; else if (isImageServerSnippet(id)) url = imageServerLink(id); else url = "https://www.botcompany.de:8443/img/" + id; return url; } public static String shortenSymbol(String s) { return shortenSymbol(s, shorten_default); } public static String shortenSymbol(String s, int max) { return shortenSymbol(s, max, "..."); } public static String shortenSymbol(String s, int max, String shortener) { if (s == null) return emptySymbol(); if (max < 0) return s; return s.length() <= max ? s : symbol(substring(str(s), 0, min(s.length(), max - l(shortener))) + shortener); } public static String shortenSymbol(int max, String s) { return shortenSymbol(s, max); } public static int ratioToIntPercent(double x, double y) { return roundToInt(x * 100 / y); } public static List keysDesc(Map map) { return sortedDesc(keys(map)); } public static List keysDesc(MultiMap mm) { return sortedDesc(keys(mm)); } public static boolean ai_possiblyShortenedKey(String s) { return l(s) == ai_indexShorteningLength() && endsWith(s, "..."); } public static IterableIterator iteratorFromFunction(final Object f) { class IFF extends IterableIterator { public A a; public boolean done; public boolean hasNext() { getNext(); return !done; } public A next() { getNext(); if (done) throw fail(); A _a = a; a = null; return _a; } public void getNext() { if (done || a != null) return; a = (A) callF(f); done = a == null; } } ; return new IFF(); } public static IterableIterator iteratorFromFunction(final F0 f) { return iteratorFromFunction_f0(f); } public static List sortedInPlace(List l, final Object comparator) { sort(l, makeComparator(comparator)); return l; } public static List sortedInPlace(List l) { sort(l); return l; } public static int compareIgnoreCase_keepSeparate(String a, String b) { if (a == null) return b == null ? 0 : -1; if (b == null) return 1; int c = a.compareToIgnoreCase(b); if (c != 0) return c; return a.compareTo(b); } public static Set similarEmptySet(Set m) { if (m instanceof TreeSet) return new TreeSet(((TreeSet) m).comparator()); if (m instanceof LinkedHashSet) return new LinkedHashSet(); return new HashSet(); } public static void clearAllTokens(List tok) { for (int i = 0; i < tok.size(); i++) tok.set(i, ""); } public static void clearAllTokens(List tok, int i, int j) { for (; i < j; i++) tok.set(i, ""); } public static boolean getHtmlTagParameters_debug; public static Map getHtmlTagParameters(String tag) { if (empty(tag)) return null; List tok = codeTokens(tok_joinMinusIdentifiers(htmlFineTok(tag))); if (getHtmlTagParameters_debug) printStruct(tok); assertEquals("<", tok.get(0)); int i = 1; if (eq(tok.get(1), "/")) ++i; String name = tok.get(i++); if (!isMinusIdentifier(name)) throw fail(tag + " (" + name + ")"); Map map = new HashMap(); while (i < l(tok)) { String t = tok.get(i); if (eqOneOf(t, "/", ">")) break; if (!isMinusIdentifier(t)) throw fail(tag + " (" + t + ")"); ++i; String value = "1"; if (eq(tok.get(i), "=")) { ++i; value = htmlunquote(tok.get(i++)); } map.put(t, value); } return map; } public static String htmlQuote(String s) { return "\"" + htmlencode_forParams(s) + "\""; } public static Object[] mapToObjectArray(Map map) { List l = new ArrayList(); for (Object o : keys(map)) { l.add(o); l.add(map.get(o)); } return toObjectArray(l); } public static List web_nodesAndRelations(Web web) { return web.nodes; } public static void web_setLabels_forced(WebNode node, List labels) { web_deindex(node); node.labels = cloneList(labels); web_index(node); } public static List map_lisp_intern(Collection l) { List out = new ArrayList(l(l)); if (l != null) for (Lisp x : l) out.add(lisp_intern(x)); return out; } public static String intern(String s) { return fastIntern(s); } public static List trimToSize(List l) { synchronized (l) { if (l instanceof ArrayList) ((ArrayList) l).trimToSize(); else if (l instanceof SynchronizedArrayList) ((SynchronizedArrayList) l).trimToSize(); } return l; } public static Object[] assertEvenLength(Object[] a) { assertTrue(even(l(a))); return a; } public static WebNode web_secondNode(Web web) { return web == null ? null : second(web.nodes); } public static Object nuObject(String className, Object... args) { try { return nuObject(classForName(className), args); } catch (Exception __e) { throw rethrow(__e); } } public static A nuObject(Class c, Object... args) { try { if (args.length == 0) return nuObjectWithoutArguments(c); Constructor m = nuObject_findConstructor(c, args); m.setAccessible(true); return (A) m.newInstance(args); } catch (Exception __e) { throw rethrow(__e); } } public static Constructor nuObject_findConstructor(Class c, Object... args) { for (Constructor m : c.getDeclaredConstructors()) { if (!nuObject_checkArgs(m.getParameterTypes(), args, false)) continue; return m; } throw fail("Constructor " + c.getName() + getClasses(args) + " not found" + (args.length == 0 && (c.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0 ? " - hint: it's a non-static class!" : "")); } public static boolean nuObject_checkArgs(Class[] types, Object[] args, boolean debug) { if (types.length != args.length) { if (debug) System.out.println("Bad parameter length: " + args.length + " vs " + types.length); return false; } for (int i = 0; i < types.length; i++) if (!(args[i] == null || isInstanceX(types[i], args[i]))) { if (debug) System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]); return false; } return true; } public static void setAll(Object o, Map fields) { if (fields == null) return; for (String field : keys(fields)) set(o, field, fields.get(field)); } public static void setAll(Object o, Object... values) { failIfOddCount(values); for (int i = 0; i + 1 < l(values); i += 2) { String field = (String) values[i]; Object value = values[i + 1]; set(o, field, value); } } public static Lock programLock() { return mainLock(); } public static File computerIDFile() { return javaxDataDir("Basic Info/computer-id.txt"); } public static void createMarkerFile(String progID, String name) { File f = getProgramFile(progID, name); if (fileLength(f) == 0) saveTextFile(f, "1"); } public static String standardCredentialsUser() { return trim(loadTextFile(new File(userHome(), ".tinybrain/username"))); } public static String standardCredentialsPass() { return trim(loadTextFile(new File(userHome(), ".tinybrain/userpass"))); } public static void ai_clearTriggeredSpeculations() { ai_triggerSpeculationOn_set.get().clear(); } public static List ai_triggeredSpeculations() { return cloneList(ai_triggerSpeculationOn_set.get()); } public static void ai_activeSpec_answerOnChat(String s) { String response = ""; { Lock __3105 = ai_activeSpecLock(); lock(__3105); try { if (has(s, "should be", "answered in the chat") && !has(s, "was", "answered in the chat")) { List answers = ai_texts("$X", "answers external question", s); if (empty(answers)) answers = unquoteAll(ai_texts_multi("$X", "answers", ll(s, quote(s)))); if (nempty(answers)) { ai_invalidateTriple(s, "should be", "answered in the chat"); response = s + "\n=> " + joinWithComma(answers); } } } finally { unlock(__3105); } } if (nempty(response) && postToStefansChat(response)) post(s, "was", "answered in the chat"); } public static void ai_activeSpec_answerChatLine(String line) { { if (ai_textOfChatLine(line) == null) return; } { if (!(is(line, "recent"))) return; } { if (has(line, "was", "answered in the chat")) return; } String a = ai_text("$X", "answers", line); { if (empty(a)) return; } if (swic(line, "smart bot's chat") ? postToSmartBotsChat(a) : postToStefansChat(a)) post(line, "was", "answered in the chat"); } public static List onWebsChanged_listeners = synchroList(); public static void triggerWebsChanged() { pcallF_all(onWebsChanged_listeners); } public static void onWebsChanged(Object f) { setAdd(onWebsChanged_listeners, f); } public static FileStatus conceptsFileStatus(String progID) { return fileStatus(conceptsFile(progID)); } public static String circlesEditorDBID() { return "#1007609"; } public static void ai_addWebToIndex(MultiMap index, Web web) { if (web == null) return; for (WebNode node : web_nodesAndRelations(web)) for (String text : asSet(node.texts())) { String key = ai_shortenForIndex(text); boolean newText; synchronized (index) { if (key == text) newText = true; else newText = !web_nodesContainText(index.get(key), text); index.put(key, node); } if (newText) ai_onNewIndexedText(text); } } public static void ai_removeWebFromIndex(MultiMap index, Web web) { if (web != null) for (WebNode node : web_nodesAndRelations(web)) for (String text : asSet(node.texts())) index.remove(ai_shortenForIndex(text), node); } public static MultiMap dwlw_fullNodeIndex_ci() { print("Getting webs."); long _startTime_3122 = sysNow(); List webs = ai_filterBadWebs(allWebs()); _startTime_3122 = sysNow() - _startTime_3122; saveTiming(_startTime_3122); print("Got webs in " + lastTiming() + " ms. Making index of " + nWebs(webs) + "."); long _startTime_3123 = sysNow(); MultiMap index = ai_indexWebs(webs); done2_always("Index made.", _startTime_3123); return index; } public static MultiMap ai_emptyWebIndex() { MultiMap index = caseInsensitiveSyncListMultiMap(); makeMultiMapsInnerNavigableMapSynchronized(index); return index; } public static void ai_setCacheChecker(AI_CacheChecker cacheChecker) { ai_cacheChecker.set(cacheChecker); } public static void ai_setPostHandler(AI_PostHandler handler) { ai_postHandler.set(handler); } public static Class getClass(String name) { try { return Class.forName(name); } catch (ClassNotFoundException e) { return null; } } public static Class getClass(Object o) { return o instanceof Class ? (Class) o : o.getClass(); } public static Class getClass(Object realm, String name) { try { try { return getClass(realm).getClassLoader().loadClass(classNameToVM(name)); } catch (ClassNotFoundException e) { return null; } } catch (Exception __e) { throw rethrow(__e); } } public static String classNameToVM(String name) { return name.replace(".", "$"); } public static boolean hasClass(String name) { try { Class.forName(name); return true; } catch (Throwable _e) { return false; } } public static List cloneAndClearList(List l) { List l2 = cloneList(l); l.clear(); return l2; } public static volatile int stefansChat_n_value; public static Lock stefansChat_n_lock = lock(); public static List stefansChat_n_onChange = synchroList(); public static int stefansChat_n() { return stefansChat_n_notify(toInt(loadPage("http://botcompany.de/1008998/raw/n"))); } public static int stefansChat_n_notify(int n) { boolean changed = false; if (n > stefansChat_n_value) { Lock __3139 = stefansChat_n_lock; lock(__3139); try { if (n > stefansChat_n_value) { stefansChat_n_value = n; changed = true; } } finally { unlock(__3139); } } if (changed) pcallF_all(stefansChat_n_onChange); return n; } public static void stefansChat_n_onChange(Object r) { if (stefansChat_n_value == 0) stefansChat_n(); setAdd(stefansChat_n_onChange, r); } public static Map safeUnstructMap(String s) { return safeUnstructureMap(s); } public static int getInt(List c, int i) { return toInt(get(c, i)); } public static int getInt(Map map, Object key) { return toInt(mapGet(map, key)); } public static int getInt(Object o, Object field) { return toInt(getOpt(o, (String) field)); } public static Map mapPlus(Map m, Object... data) { m = cloneMap(m); litmap_impl(m, data); return m; } public static String getString(Map map, Object key) { return map == null ? null : (String) map.get(key); } public static String getString(List l, int idx) { return (String) get(l, idx); } public static String getString(Object o, Object key) { if (o instanceof Map) return getString((Map) o, key); if (key instanceof String) return (String) getOpt(o, (String) key); throw fail("Not a string key: " + getClassName(key)); } public static volatile int smartBotsChat_n_value; public static Lock smartBotsChat_n_lock = lock(); public static List smartBotsChat_n_onChange = synchroList(); public static int smartBotsChat_n() { return smartBotsChat_n_notify(toInt(loadPage("http://botcompany.de/1011975/raw/n"))); } public static int smartBotsChat_n_notify(int n) { boolean changed = false; if (n > smartBotsChat_n_value) { Lock __3148 = smartBotsChat_n_lock; lock(__3148); try { if (n > smartBotsChat_n_value) { smartBotsChat_n_value = n; changed = true; } } finally { unlock(__3148); } } if (changed) pcallF_all(smartBotsChat_n_onChange); return n; } public static void smartBotsChat_n_onChange(Object r) { if (smartBotsChat_n_value == 0) smartBotsChat_n(); setAdd(smartBotsChat_n_onChange, r); } public static A optParam(ThreadLocal tl, A defaultValue) { return optPar(tl, defaultValue); } public static A optParam(ThreadLocal tl) { return optPar(tl); } public static A optParam(Object[] opt, String name, A defaultValue) { if (!even(l(opt))) throw fail("Odd parameter length"); for (int i = 0; i < l(opt); i += 2) if (eq(opt[i], name)) return (A) opt[i + 1]; return defaultValue; } public static A optParam(Object[] opt, String name) { return optParam(opt, name, null); } public static int lUtf8(String s) { return l(utf8(s)); } public static String loadTextFileFromZip(File inZip, String fileName) { return loadTextFileFromZipFile(inZip, fileName); } public static Class hotwire_finish(ClassLoader classLoader, String progID, String javaSource) { try { Class theClass = classLoader.loadClass("main"); Class j = getJavaX(); setOpt(theClass, "myJavaSource_code", javaSource); synchronized (j) { call(j, "setVars", theClass, progID); callOpt(j, "addInstance", progID, theClass); } hotwire_copyOver(theClass); return theClass; } catch (Exception __e) { throw rethrow(__e); } } public static List endingWith(Collection l, final String suffix) { return filter(unnull(l), new F1() { public Object get(String s) { try { return endsWith(s, suffix); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "endsWith(s, suffix)"; } }); } public static List endingWith(String suffix, Collection l) { return endingWith(l, suffix); } public static Set allFields(Object o) { TreeSet fields = new TreeSet(); Class _c = _getClass(o); do { for (Field f : _c.getDeclaredFields()) fields.add(f.getName()); _c = _c.getSuperclass(); } while (_c != null); return fields; } public static DialogIO talkTo(int port) { return talkTo("localhost", port); } public static int talkTo_defaultTimeout = 10000; public static int talkTo_timeoutForReads = 0; public static ThreadLocal> talkTo_byThread = new ThreadLocal(); public static DialogIO talkTo(String ip, int port) { try { String full = ip + ":" + port; Map map = talkTo_byThread.get(); if (map != null && map.containsKey(full)) return map.get(full); if (isLocalhost(ip) && port == vmPort()) return talkToThisVM(); return new talkTo_IO(ip, port); } catch (Exception __e) { throw rethrow(__e); } } public static class talkTo_IO extends DialogIO { public String ip; public int port; public Socket s; public Writer w; public BufferedReader in; public talkTo_IO(String ip, int port) { this.port = port; this.ip = ip; try { s = new Socket(); try { if (talkTo_timeoutForReads != 0) s.setSoTimeout(talkTo_timeoutForReads); s.connect(new InetSocketAddress(ip, port), talkTo_defaultTimeout); } catch (Throwable e) { throw fail("Tried talking to " + ip + ":" + port, e); } w = new OutputStreamWriter(s.getOutputStream(), "UTF-8"); in = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8")); } catch (Exception __e) { throw rethrow(__e); } } public boolean isLocalConnection() { return s.getInetAddress().isLoopbackAddress(); } public boolean isStillConnected() { return !(eos || s.isClosed()); } public void sendLine(String line) { try { Lock __3165 = lock; lock(__3165); try { w.write(line + "\n"); w.flush(); } finally { unlock(__3165); } } catch (Exception __e) { throw rethrow(__e); } } public String readLineImpl() { try { return in.readLine(); } catch (Exception __e) { throw rethrow(__e); } } public void close() { try { if (!noClose) s.close(); } catch (IOException e) { } } public Socket getSocket() { return s; } } public static boolean web_isQuotedNode(WebNode node) { return isQuoted(web_text(node)); } public static ThreadLocal> ai_triggerSpeculationOn_set = initializedThreadLocal(LinkedHashSet.class); public static void ai_triggerSpeculationOn(String s) { ai_triggerSpeculationOn_set.get().add(s); } public static String getCanonicalPath(String path) { try { return new File(path).getCanonicalPath(); } catch (Exception __e) { throw rethrow(__e); } } public static void closeAllWriters(Collection l) { for (Writer w : unnull(l)) try { w.close(); } catch (Throwable __e) { _handleException(__e); } } public static boolean is(String a, String b) { return ai_hasTriple(a, "is", b); } public static boolean endsWithLetter(String s) { return nempty(s) && isLetter(last(s)); } public static String ai_textOfChatLine(String line) { if (!swicOneOf(line, "chat line ", "Smart Bot's Chat line ")) return null; String a = firstQuoted(ai_texts_verified(line, "has text", "$X")); if (a != null) return unquote(a); return unquote(firstQuoted(ai_texts_verified(line, "has text", "$X"))); } public static boolean ai_hasAny(String a, String b, String c) { return ai_text(a, b, c) != null; } public static boolean ai_hasAny(T3 t) { return ai_hasAny(t.a, t.b, t.c); } public static T3 tok_tripleSplitAtToken(List tok, int i) { return triple(trimJoinSubList(tok, 0, i), tok.get(i), trimJoinSubList(tok, i + 1)); } public static String optionalCurlyBrace(String s) { return isCurlyBraced(s) ? s : curlyBrace(s); } public static List tok_combineCurlyBrackets(List tok) { List l = new ArrayList(); for (int i = 0; i < l(tok); i++) { String t = tok.get(i); if (odd(i) && eq(t, "{")) { int j = findEndOfCurlyBracketPart(tok, i); l.add(joinSubList(tok, i + 1, j - 1)); i = j - 1; } else l.add(t); } return l; } public static boolean ai_trivialTriple(T3 t) { if (t == null) return true; return eq(t.b, "?") && eq(t.c, "?") || eq(t.b, ".") && eq(t.c, "."); } public static boolean containsWord(String s, String word) { return cic(nlTok3(s), word); } public static String ai_simplifyNP(String s) { { String _a_3196 = dropAfterSubString_orNull(s, " in order to "); if (!empty(_a_3196)) return _a_3196; } { String _a_3197 = dropAfterSubString_orNull(s, " you "); if (!empty(_a_3197)) return _a_3197; } s = ai_dropLeadingAdverbs(s); List tok = nlTok(s); int i = indexOfPred(tok, "isPreposition"); if (i >= 0) return joinTakeFirst(tok, i - 1); return s; } public static List allCurlyBracketPhrases(String s) { List tok = javaTok(s); Map map = curlyBracketMap(tok); List phrases = new ArrayList(); for (int i : keys(map)) { int j = map.get(i); phrases.add(trimJoinSubList(tok, i + 1, j - 1)); } return phrases; } public static int ai_intOrMinus1(String a, String b, String c) { return ai_text_int(a, b, c); } public static int ai_intOrMinus1(T3 t) { return ai_text_int(t); } public static List splitStringAtIndices(String s, List indices) { Iterator it = indices.iterator(); List parts = new ArrayList(); int i = 0; while (true) { if (!it.hasNext()) { parts.add(substring(s, i)); break; } int j = it.next(); parts.add(substring(s, i, j)); i = j; } return parts; } public static String curlyBrace(String s) { return "{" + s + "}"; } public static boolean ai_isAOrAn(String s) { return swicOneOf(s, "a ", "an "); } public static boolean ai_isAOrAn(String s, Matches m) { return swicOneOf_trim(s, m, "a ", "an "); } public static boolean isLetter(char c) { return Character.isLetter(c); } public static boolean startsWithAndEndsWith(String s, String prefix, String suffix) { return startsWith(s, prefix) && endsWith(s, suffix); } public static Object safeUnstructure_nullAndPrintShortOnError(String s) { try { return safeUnstructure(s); } catch (Throwable e) { printShortException(e); return null; } } public static boolean isProperlyQuoted(String s) { return s.length() >= 2 && s.startsWith("\"") && s.endsWith("\"") && (!s.endsWith("\\\"") || s.endsWith("\\\\\"")); } public static long ai_hasTriple_vary_worstCase; public static boolean ai_hasTriple_vary(String a, String b, String c) { List x = ai_variationsOf(a); List y = ai_variationsOf(b); List z = ai_variationsOf(c); int n = l(x) * l(y) * l(z); ai_hasTriple_vary_worstCase = max(ai_hasTriple_vary_worstCase, n); for (String xx : x) for (String yy : y) for (String zz : z) if (ai_hasTriple(xx, yy, zz)) return true; return false; } public static Set ai_groupingPrefixes() { return new TreeSet(ai_texts_verified_unquote("$X", "is a", "grouping prefix")); } public static String curlyBraces(String s) { return curlyBrace(s); } public static List web_relationsNamed(Web web, String label) { return web_allRelationsWithLabel(web, label); } public static BigInteger bigint(String s) { return new BigInteger(s); } public static BigInteger bigint(long l) { return BigInteger.valueOf(l); } public static String dropLastTrim(String s) { return trim(dropLast(s)); } public static void ai_expandCluster(Set cluster) { for (String s : cloneList(cluster)) { cluster.addAll(ai_texts(s, "is identical to", "$X")); cluster.addAll(ai_texts("$X", "is identical to", s)); } } public static Set ai_cluster_backward(Set cluster, String rel) { Set set = litciset(); for (String s : cluster) set.addAll(ai_texts("$X", rel, s)); return set; } public static int ai_spec_maxAnswersToReturn() { return 10; } public static String optQuote(String s) { return quoteUnlessQuoted(s); } public static Throwable unwrapTrivialExceptionWraps(Throwable e) { if (e == null) return e; while (e.getClass() == RuntimeException.class && e.getCause() != null && eq(e.getMessage(), str(e.getCause()))) e = e.getCause(); return e; } public static String replacePrefix(String prefix, String replacement, String s) { if (!startsWith(s, prefix)) return s; return replacement + substring(s, l(prefix)); } public static void printAsciiHeading(String text) { print(asciiHeading2(text)); } public static String deadlockedThreadsToString(List threadInfo) { List out = new ArrayList(); int i = 0; Map map = threadInfosToThreads_map(threadInfo); for (ThreadInfo threadInfo1 : threadInfo) { out.add("Thread " + (++i) + "/" + l(threadInfo) + ": ID=" + threadInfo1.getThreadId() + ", name=" + quote(threadInfo1.getThreadName())); out.add(" Trying to lock: " + threadInfo1.getLockName() + " which is owned by thread " + threadInfo1.getLockOwnerId() + " (" + quote(threadInfo1.getLockOwnerName()) + ")"); Thread t = map.get(threadInfo1); if (t != null) { StackTraceElement[] st = t.getStackTrace(); out.add("Stack trace (" + l(st) + "): "); out.add(indentx(stackTraceToString(st))); } } return lines(out); } public static List threadInfosToThreads(List l) { HashMap map = new HashMap(); for (Thread t : keys(Thread.getAllStackTraces())) map.put(t.getId(), t); List out = new ArrayList(); for (ThreadInfo t : l) addIfNotNull(out, map.get(t.getThreadId())); return out; } public static AutoCloseable temp_ai_setMaker(String maker) { return tempSetThreadLocal(postSoftwareMadeWeb_maker, maker); } public static boolean web_tripelizableNode(WebNode n) { return web_countLabels(n) == 1 && (n.visInfo() == null || eq(n.visInfo(), "")); } public static String web_sym(WebNode node) { return node == null ? null : symbol(node.text()); } public static String html_smartBotsChatPostLink(String msg, Object contents, Object... params) { return ahref("javascript:void(0);", contents, paramsPlus(params, "onClick", js_smartBotsChatPostCode(msg))); } public static String smartBot_sendInputLink(String input) { return smartBotDirectURL() + "/?input=" + urlencode(input); } public static Collection ai_expandShortenedKey(String s) { return ai_expandShortenedKeys(s); } public static List _registerWeakMap_preList; public static A _registerWeakMap(A map) { if (javax() == null) { if (_registerWeakMap_preList == null) _registerWeakMap_preList = synchroList(); _registerWeakMap_preList.add(map); return map; } try { call(javax(), "_registerWeakMap", map); } catch (Throwable e) { printException(e); print("Upgrade JavaX!!"); } return map; } public static void _onLoad_registerWeakMap() { assertNotNull(javax()); if (_registerWeakMap_preList == null) return; for (Object o : _registerWeakMap_preList) _registerWeakMap(o); _registerWeakMap_preList = null; } public static boolean web_isVerified(WebNode n) { return n != null && n.web.verified(); } public static Map getBracketMap(List tok) { TreeMap map = new TreeMap(); List stack = new ArrayList(); for (int i = 1; i < l(tok); i += 2) { if (getBracketMap_opening.contains(tok.get(i))) stack.add(i); else if (getBracketMap_closing.contains(tok.get(i))) { if (!empty(stack)) map.put(liftLast(stack), i); } } return map; } public static List getBracketMap_opening = ll("{", "("); public static List getBracketMap_closing = ll("}", ")"); public static betterCIComparator_C betterCIComparator_instance; public static betterCIComparator_C betterCIComparator() { if (betterCIComparator_instance == null) betterCIComparator_instance = new betterCIComparator_C(); return betterCIComparator_instance; } public static class betterCIComparator_C implements Comparator { public int compare(String s1, String s2) { if (s1 == null) return s2 == null ? 0 : -1; if (s2 == null) return 1; int n1 = s1.length(); int n2 = s2.length(); int min = Math.min(n1, n2); for (int i = 0; i < min; i++) { char c1 = s1.charAt(i); char c2 = s2.charAt(i); if (c1 != c2) { c1 = Character.toUpperCase(c1); c2 = Character.toUpperCase(c2); if (c1 != c2) { c1 = Character.toLowerCase(c1); c2 = Character.toLowerCase(c2); if (c1 != c2) { return c1 - c2; } } } } return n1 - n2; } } public static int timedGC_minPrint = 5000; public static void timedGC() { long time = sysNow(), start = now(); gc_notTooOften(); done2_systemErr_minPrint = timedGC_minPrint; done2_systemErr(time, "GC " + hmsWithColons(start)); } public static Class transpileRaw_trans; public static Object transpileRaw_silent = true; public static boolean transpileRaw_useDiskCache; public static Lock transpileRaw_lock = lock(); public static String transpileRaw(String mainJava) { return transpileRaw(mainJava, false); } public static String transpileRaw(String mainJava, boolean fragment) { mainJava = dropTranslators(mainJava); transpileRaw_copyFromCreator(); Lock __3267 = transpileRaw_lock; lock(__3267); try { { JWindow _loading_window = showLoadingAnimation("Transpiling..."); try { File cacheFile = null; if (transpileRaw_useDiskCache) { String md5 = md5(mainJava); cacheFile = new File(javaxCodeDir(), "Transpilations/" + md5 + ".java"); { String _a_3278 = loadTextFile(cacheFile); if (!empty(_a_3278)) return _a_3278; } } transpileRaw_translator(); if (fragment) setOpt(transpileRaw_trans, "localStuffOnly", true); set(transpileRaw_trans, "mainJava", mainJava); print("Running translator " + getOpt(transpileRaw_trans, "programID")); callMain(transpileRaw_trans); String main = (String) (get(transpileRaw_trans, "mainJava")); if (transpileRaw_useDiskCache) saveTextFile(cacheFile, main); return main; } finally { disposeWindow(_loading_window); } } } finally { unlock(__3267); } } public static Class transpileRaw_translator() { if (transpileRaw_trans == null) { print("Loading translator."); transpileRaw_trans = hotwireSharingLibraries("#759"); } setOpt(transpileRaw_trans, "print_silent", transpileRaw_silent); return transpileRaw_trans; } public static void transpileRaw_copyFromCreator() { Lock __3270 = transpileRaw_lock; lock(__3270); try { if (transpileRaw_trans != null) return; Object c = creator(); if (c == null) return; Class trans = (Class) (getOpt(c, "transpileRaw_trans")); Lock lock = (Lock) (getOpt(c, "transpileRaw_lock")); if (trans != null && lock != null) { print("Using creator's transpiler: " + getProgramID(c) + " => " + programID()); transpileRaw_lock = lock; transpileRaw_trans = trans; } } finally { unlock(__3270); } } public static boolean hasBot(String searchPattern) { DialogIO io = findBot(searchPattern); if (io != null) { io.close(); return true; } else return false; } public static String dropAfterSpace(String s) { return s == null ? null : substring(s, 0, smartIndexOf(s, ' ')); } public static boolean isOK(String s) { s = trim(s); return swic(s, "ok ") || eqic(s, "ok") || matchStart("ok", s); } public static String sendOpt(String bot, String text, Object... args) { return sendToLocalBotOpt(bot, text, args); } public static boolean isMainProgram() { return creator() == null; } public static String dbBotName(String progID) { return fsI(progID) + " Concepts"; } public static String getDBProgramID_id; public static String getDBProgramID() { return nempty(getDBProgramID_id) ? getDBProgramID_id : programIDWithCase(); } public static String actualUserHome_value; public static String actualUserHome() { if (actualUserHome_value == null) { if (isAndroid()) actualUserHome_value = "/storage/sdcard0/"; else actualUserHome_value = System.getProperty("user.home"); } return actualUserHome_value; } public static File actualUserHome(String sub) { return newFile(new File(actualUserHome()), sub); } public static Android3 methodsBot2(String name, final Object receiver, final List exposedMethods) { return methodsBot2(name, receiver, exposedMethods, null); } public static Android3 methodsBot2(String name, final Object receiver, final List exposedMethods, final Lock lock) { Android3 android = new Android3(); android.greeting = name; android.console = false; android.responder = new Responder() { public String answer(String s, List history) { return exposeMethods2(receiver, s, exposedMethods, lock); } }; return makeBot(android); } public static int gzInputStream_defaultBufferSize = 65536; public static GZIPInputStream gzInputStream(File f) { try { return gzInputStream(new FileInputStream(f)); } catch (Exception __e) { throw rethrow(__e); } } public static GZIPInputStream gzInputStream(File f, int bufferSize) { try { return new GZIPInputStream(new FileInputStream(f), bufferSize); } catch (Exception __e) { throw rethrow(__e); } } public static GZIPInputStream gzInputStream(InputStream in) { try { return new GZIPInputStream(in, gzInputStream_defaultBufferSize); } catch (Exception __e) { throw rethrow(__e); } } public static Producer javaTokC_noMLS_iterator(final String s) { return javaTokC_noMLS_iterator(s, 0); } public static Producer javaTokC_noMLS_iterator(final String s, final int startIndex) { return new Producer() { public final int l = s.length(); public int i = startIndex; public String next() { if (i >= l) return null; int j = i; char c, d; while (j < l) { c = s.charAt(j); d = j + 1 >= l ? '\0' : s.charAt(j + 1); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else if (c == '/' && d == '*') { do ++j; while (j < l && !s.substring(j, Math.min(j + 2, l)).equals("*/")); j = Math.min(j + 2, l); } else if (c == '/' && d == '/') { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else break; } i = j; if (i >= l) return null; c = s.charAt(i); d = i + 1 >= l ? '\0' : s.charAt(i + 1); if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { if (s.charAt(j) == opener || s.charAt(j) == '\n') { ++j; break; } else if (s.charAt(j) == '\\' && j + 1 < l) j += 2; else ++j; } } else if (Character.isJavaIdentifierStart(c)) do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; } else ++j; String t = quickSubstring(s, i, j); i = j; return t; } }; } public static String unquoteUsingCharArray(String s, char[] buf) { if (s == null) return null; if (startsWith(s, '[')) { int i = 1; while (i < s.length() && s.charAt(i) == '=') ++i; if (i < s.length() && s.charAt(i) == '[') { String m = s.substring(1, i); if (s.endsWith("]" + m + "]")) return s.substring(i + 1, s.length() - i - 1); } } if (s.length() > 1) { char c = s.charAt(0); if (c == '\"' || c == '\'') { int l = endsWith(s, c) ? s.length() - 1 : s.length(); if (l > buf.length) return unquote(s); int n = 0; for (int i = 1; i < l; i++) { char ch = s.charAt(i); if (ch == '\\') { char nextChar = (i == l - 1) ? '\\' : s.charAt(i + 1); if (nextChar >= '0' && nextChar <= '7') { String code = "" + nextChar; i++; if ((i < l - 1) && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '7') { code += s.charAt(i + 1); i++; if ((i < l - 1) && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '7') { code += s.charAt(i + 1); i++; } } buf[n++] = (char) Integer.parseInt(code, 8); continue; } switch(nextChar) { case '\"': ch = '\"'; break; case '\\': ch = '\\'; break; case 'b': ch = '\b'; break; case 'f': ch = '\f'; break; case 'n': ch = '\n'; break; case 'r': ch = '\r'; break; case 't': ch = '\t'; break; case '\'': ch = '\''; break; case 'u': if (i >= l - 5) { ch = 'u'; break; } int code = Integer.parseInt("" + s.charAt(i + 2) + s.charAt(i + 3) + s.charAt(i + 4) + s.charAt(i + 5), 16); char[] x = Character.toChars(code); int lx = x.length; for (int j = 0; j < lx; j++) buf[n++] = x[j]; i += 5; continue; default: ch = nextChar; } i++; } buf[n++] = ch; } return new String(buf, 0, n); } } return s; } public static boolean structure_isMarker(String s, int i, int j) { if (i >= j) return false; if (s.charAt(i) != 'm') return false; ++i; while (i < j) { char c = s.charAt(i); if (c < '0' || c > '9') return false; ++i; } return true; } public static String internIfLongerThan(String s, int l) { return s == null ? null : l(s) >= l ? intern(s) : s; } public static char unquoteCharacter(String s) { assertTrue(s.startsWith("'") && s.length() > 1); return unquote("\"" + s.substring(1, s.endsWith("'") ? s.length() - 1 : s.length()) + "\"").charAt(0); } public static boolean isLongConstant(String s) { if (!s.endsWith("L")) return false; s = s.substring(0, l(s) - 1); return isInteger(s); } public static SortedMap synchronizedTreeMap() { return synchroTreeMap(); } public static Map synchronizedMap() { return synchroMap(); } public static byte[] hexToBytes(String s) { if (odd(l(s))) throw fail("Hex string has odd length: " + quote(shorten(10, s))); int n = l(s) / 2; byte[] bytes = new byte[n]; for (int i = 0; i < n; i++) { int a = parseHexChar(s.charAt(i * 2)); int b = parseHexChar(s.charAt(i * 2 + 1)); if (a < 0 || b < 0) throw fail("Bad hex byte: " + quote(substring(s, i * 2, i * 2 + 2)) + " at " + i * 2 + "/" + l(s)); bytes[i] = (byte) ((a << 4) | b); } return bytes; } public static boolean[] boolArrayFromBytes(byte[] a, int n) { boolean[] b = new boolean[n]; int m = min(n, l(a) * 8); for (int i = 0; i < m; i++) b[i] = (a[i / 8] & 1 << (i & 7)) != 0; return b; } public static HashMap findClass_cache = new HashMap(); public static Class findClass(String name) { synchronized (findClass_cache) { if (findClass_cache.containsKey(name)) return findClass_cache.get(name); if (!isJavaIdentifier(name)) return null; Class c; try { c = Class.forName("main$" + name); } catch (ClassNotFoundException e) { c = null; } findClass_cache.put(name, c); return c; } } public static A nuStubInnerObject(Class c) { try { Class outerType = getOuterClass(c); Constructor m = c.getDeclaredConstructor(outerType); m.setAccessible(true); return (A) m.newInstance(new Object[] { null }); } catch (Exception __e) { throw rethrow(__e); } } public static Map nuEmptyObject_cache = newDangerousWeakHashMap(); public static A nuEmptyObject(Class c) { try { Constructor ctr; synchronized (nuEmptyObject_cache) { ctr = nuEmptyObject_cache.get(c); if (ctr == null) { nuEmptyObject_cache.put(c, ctr = nuEmptyObject_findConstructor(c)); ctr.setAccessible(true); } } try { return (A) ctr.newInstance(); } catch (InstantiationException e) { if (empty(e.getMessage())) if ((c.getModifiers() & Modifier.ABSTRACT) != 0) throw fail("Can't instantiate abstract class " + className(c), e); else throw fail("Can't instantiate " + className(c), e); else throw rethrow(e); } } catch (Exception __e) { throw rethrow(__e); } } public static Constructor nuEmptyObject_findConstructor(Class c) { for (Constructor m : c.getDeclaredConstructors()) if (m.getParameterTypes().length == 0) return m; throw fail("No default constructor declared in " + c.getName()); } public static String shortClassName(Object o) { if (o == null) return null; Class c = o instanceof Class ? (Class) o : o.getClass(); String name = c.getName(); return shortenClassName(name); } public static void printStructure(String prefix, Object o) { if (endsWithLetter(prefix)) prefix += ": "; print(prefix + structureForUser(o)); } public static void printStructure(Object o) { print(structureForUser(o)); } public static boolean setOptAllDyn_debug; public static void setOptAllDyn(DynamicObject o, Map fields) { if (fields == null) return; HashMap fieldMap = instanceFieldsMap(o); for (Map.Entry e : fields.entrySet()) { String field = e.getKey(); Object val = e.getValue(); boolean has = fieldMap.containsKey(field); if (has) setOpt(o, field, val); else { o.fieldValues.put(intern(field), val); if (setOptAllDyn_debug) print("setOptAllDyn added dyn " + field + " to " + o + " [value: " + val + ", fieldValues = " + systemHashCode(o.fieldValues) + ", " + struct(keys(o.fieldValues)) + "]"); } } } public static void setOptAll_pcall(Object o, Map fields) { if (fields == null) return; for (String field : keys(fields)) try { setOpt(o, field, fields.get(field)); } catch (Throwable __e) { print(exceptionToStringShort(__e)); } } public static void setOptAll_pcall(Object o, Object... values) { warnIfOddCount(values); for (int i = 0; i + 1 < l(values); i += 2) { String field = (String) values[i]; Object value = values[i + 1]; try { setOpt(o, field, value); } catch (Throwable __e) { print(exceptionToStringShort(__e)); } } } public static void pcallOpt_noArgs(Object o, String method) { try { callOpt_noArgs(o, method); } catch (Throwable __e) { _handleException(__e); } } public static int[] toIntArray(List l) { int[] a = new int[l(l)]; for (int i = 0; i < a.length; i++) a[i] = l.get(i); return a; } public static boolean jmatch(String pat, String s) { return jmatch(pat, s, null); } public static boolean jmatch(String pat, String s, Matches matches) { if (s == null) return false; return jmatch(pat, javaTok(s), matches); } public static boolean jmatch(String pat, List toks) { return jmatch(pat, toks, null); } public static boolean jmatch(String pat, List toks, Matches matches) { List tokpat = javaTok(pat); String[] m = match2(tokpat, toks); if (m == null) return false; else { if (matches != null) matches.m = m; return true; } } public static File javaxSecretDir_dir; public static File javaxSecretDir() { return javaxSecretDir_dir != null ? javaxSecretDir_dir : new File(userHome(), "JavaX-Secret"); } public static File javaxSecretDir(String sub) { return newFile(javaxSecretDir(), sub); } public static List getMultiPorts() { return (List) callOpt(getJavaX(), "getMultiPorts"); } public static void startMultiPort() { List mp = getMultiPorts(); if (mp != null && mp.isEmpty()) { nohupJavax("#1001639"); throw fail("Upgrading JavaX, please restart this program afterwards."); } } public static Set synchroTreeSet() { return Collections.synchronizedSet(new TreeSet()); } public static boolean forbiddenPort(int port) { return port == 5037; } public static String readLineHidden() { try { if (get(javax(), "readLine_reader") == null) set(javax(), "readLine_reader", new BufferedReader(new InputStreamReader(System.in, "UTF-8"))); try { return ((BufferedReader) get(javax(), "readLine_reader")).readLine(); } finally { consoleClearInput(); } } catch (Exception __e) { throw rethrow(__e); } } public static int indent_default = 2; public static String indent(int indent) { return repeat(' ', indent); } public static String indent(int indent, String s) { return indent(repeat(' ', indent), s); } public static String indent(String indent, String s) { return indent + s.replace("\n", "\n" + indent); } public static String indent(String s) { return indent(indent_default, s); } public static List indent(String indent, List lines) { List l = new ArrayList(); for (String s : lines) l.add(indent + s); return l; } public static String getProgramName_cache; public static String getProgramName() { Lock __3346 = downloadLock(); lock(__3346); try { if (getProgramName_cache == null) getProgramName_cache = getSnippetTitleOpt(programID()); return getProgramName_cache; } finally { unlock(__3346); } } public static void _onLoad_getProgramName() { { Thread _t_0 = new Thread() { public void run() { try { getProgramName(); } catch (Throwable __e) { _handleException(__e); } } }; startThread(_t_0); } } public static String rtrimSpaces(String s) { if (s == null) return null; int i = s.length(); while (i > 0 && " \t".indexOf(s.charAt(i - 1)) >= 0) --i; return i < s.length() ? s.substring(0, i) : s; } public static List ai_triplesMadeByProgramAsTriples(String progID) { return webs_readTripleFileAsTriples(getProgramFile(progID, "triples.gz")); } public static List ai_loadWebsFromFile(File f, String src) { List webs = new ArrayList(); ai_loadWebsFromFile(f, src, webs); return webs; } public static void ai_loadWebsFromFile(File f, String src, List websOut) { ai_loadWebsFromFile(f, src, websOut, null); } public static void ai_loadWebsFromFile(File f, String src, List websOut, Map furtherInfoMap) { IterableIterator l = scanLog_unstructure_iterator(f); for (SoftwareMadeWeb o : l) try { Web web = o.web; if (web == null) web = web_unstructure(o.structure); if (web != null) { websOut.add(web_intern(web_setSourceIfEmpty(web, src))); mapPut(furtherInfoMap, web, o); } } catch (Throwable __e) { _handleException(__e); } } public static List websToTripleWebs(Collection l) { return map("ai_webToTripleWeb", l); } public static List ai_filterTriples(String a, String b, String c, List l) { List out = new ArrayList(); for (TripleWeb w : l) if (canSimpleMatchTriple_dollarVars(a, b, c, w)) out.add(w); return out; } public static List ai_thoughtSpace_queryTriples(String a, String b, String c, AbstractThoughtSpace ts) { List l = null; if (!isDollarVar(a)) { if (!isDollarVar(b)) return ts.getOneTwo(a, b); l = shortestListUnlessNull2(l, ts.getTriples(a, 0)); if (l(l) <= 1) return l; } if (!isDollarVar(b)) { l = shortestListUnlessNull2(l, ts.getTriples(b, 1)); if (l(l) <= 1) return l; } if (!isDollarVar(c)) l = shortestListUnlessNull2(l, ts.getTriples(c, 2)); return l; } public static Thread _unregisterThread(Thread t) { _registerThread_threads.remove(t); return t; } public static void _unregisterThread() { _unregisterThread(currentThread()); } public static String fromUtf8(byte[] bytes) { try { return new String(bytes, "UTF-8"); } catch (Exception __e) { throw rethrow(__e); } } public static void copyStream(InputStream in, OutputStream out) { try { byte[] buf = new byte[65536]; while (true) { int n = in.read(buf); if (n <= 0) return; out.write(buf, 0, n); } } catch (Exception __e) { throw rethrow(__e); } } public static JComponent wrap(Object swingable) { if (swingable == null) return null; JComponent c; if (swingable instanceof JComponent) c = (JComponent) swingable; else c = (JComponent) callOpt(swingable, "swing"); if (c instanceof JTable || c instanceof JList || c instanceof JTextArea || c instanceof JEditorPane || c instanceof JTextPane || c instanceof JTree) return jscroll(c); return c == null ? jlabel(str(swingable)) : c; } public static JTextArea wrappedTextArea(final JTextArea ta) { { swing(new Runnable() { public void run() { try { ta.setLineWrap(true); ta.setWrapStyleWord(true); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ta.setLineWrap(true);\r\n ta.setWrapStyleWord(true);"; } }); } return ta; } public static JTextArea wrappedTextArea() { return wrappedTextArea(jtextarea()); } public static JTextArea wrappedTextArea(String text) { JTextArea ta = wrappedTextArea(); ta.setText(text); return ta; } public static void onClick(JComponent c, final Object runnable) { c.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { callF(runnable, e); } }); } public static void onClick(JButton btn, final Object runnable) { onEnter(btn, runnable); } public static void disposeWindow(final Window window) { if (window != null) { swing(new Runnable() { public void run() { try { window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING)); myFrames_list.remove(window); window.dispose(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING)); //..."; } }); } } public static void disposeWindow(final Component c) { disposeWindow(getWindow(c)); } public static void disposeWindow(Object o) { if (o != null) disposeWindow(((Component) o)); } public static void disposeWindow() { disposeWindow(heldInstance(Component.class)); } public static Font typeWriterFont() { return typeWriterFont(14); } public static Font typeWriterFont(int size) { return new Font("Courier", Font.PLAIN, size); } public static JScrollPane jscroll(final Component c) { return swing(new F0() { public JScrollPane get() { try { return new JScrollPane(c); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "new JScrollPane(c)"; } }); } public static int withMargin_defaultWidth = 6; public static JPanel withMargin(Component c) { return withMargin(withMargin_defaultWidth, c); } public static JPanel withMargin(int w, Component c) { return withMargin(w, w, c); } public static JPanel withMargin(int w, int h, Component c) { return withMargin(w, h, w, h, c); } public static JPanel withMargin(final int top, final int left, final int bottom, final int right, final Component c) { return swing(new F0() { public JPanel get() { try { JPanel p = new JPanel(new BorderLayout()); p.setBorder(BorderFactory.createEmptyBorder(top, left, bottom, right)); p.add(c); return p; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JPanel p = new JPanel(new BorderLayout);\r\n p.setBorder(BorderFactory.creat..."; } }); } public static Window getWindow(Object o) { if (!(o instanceof Component)) return null; Component c = (Component) o; while (c != null) { if (c instanceof Window) return (Window) c; c = c.getParent(); } return null; } public static Dimension getScreenSize() { return Toolkit.getDefaultToolkit().getScreenSize(); } public static void swingLater(long delay, final Object r) { javax.swing.Timer timer = new javax.swing.Timer(toInt(delay), actionListener(wrapAsActivity(r))); timer.setRepeats(false); timer.start(); } public static void swingLater(Object r) { SwingUtilities.invokeLater(toRunnable(r)); } public static Comparator makeComparator(final Object f) { if (f instanceof Comparator) return (Comparator) f; return new Comparator() { public int compare(Object a, Object b) { return (Integer) callF(f, a, b); } }; } public static List allMethodNames(Object o) { Class c = _getClass(o); TreeSet names = new TreeSet(); while (c != null) { for (Method m : c.getDeclaredMethods()) names.add(m.getName()); c = c.getSuperclass(); } return asList(names); } public static boolean startsWithOneOf(String s, String... l) { for (String x : l) if (startsWith(s, x)) return true; return false; } public static List getClassNames(Collection l) { List out = new ArrayList(); if (l != null) for (Object o : l) out.add(o == null ? null : getClassName(o)); return out; } public static void listAdd(Collection c, A a) { if (c != null) c.add(a); } public static String dropSuffixICTrim(String suffix, String s) { return trim(dropSuffixIC(suffix, s)); } public static List concatLists_conservative(List a, List b) { if (empty(a)) return b; if (empty(b)) return a; return concatLists(a, b); } public static IterableIterator iteratorFromFunction_withEndMarker_f0(final F0 f) { class IFF2 extends IterableIterator { public A a; public boolean have, done; public boolean hasNext() { getNext(); return !done; } public A next() { getNext(); if (done) throw fail(); A _a = a; a = null; have = false; return _a; } public void getNext() { if (done || have) return; a = f.get(); have = true; if (a == iteratorFromFunction_endMarker) done = true; } } ; return new IFF2(); } public static String[] litstringarray(String... data) { return data; } public static List web_parseLabels(final Web web, String... labels) { return map(new F1() { public Object get(String s) { try { return web.parseLabel(s); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "web.parseLabel(s)"; } }, labels); } public static boolean web_hasOneOfTheLabels(WebNode node, List labels) { for (Lisp l : labels) if (node.hasLabel(l)) return true; return false; } public static String addPrefixIfNotEmpty(String prefix, String s) { return empty(s) ? "" : prefix + s; } public static void sleepInCleanUp(long ms) { try { if (ms < 0) return; Thread.sleep(ms); } catch (Exception __e) { throw rethrow(__e); } } public static A callJavaX(String method, Object... args) { return (A) callOpt(getJavaX(), method, args); } public static A liftLast(List l) { if (l.isEmpty()) return null; int i = l(l) - 1; A a = l.get(i); l.remove(i); return a; } public static byte[] boolArrayToBytes(boolean[] a) { byte[] b = new byte[(l(a) + 7) / 8]; for (int i = 0; i < l(a); i++) if (a[i]) b[i / 8] |= 1 << (i & 7); return b; } public static int cmp(Number a, Number b) { return a == null ? b == null ? 0 : -1 : cmp(a.doubleValue(), b.doubleValue()); } public static int cmp(double a, double b) { return a < b ? -1 : a == b ? 0 : 1; } public static int cmp(String a, String b) { return a == null ? b == null ? 0 : -1 : a.compareTo(b); } public static int cmp(Object a, Object b) { if (a == null) return b == null ? 0 : -1; if (b == null) return 1; return ((Comparable) a).compareTo(b); } public static Map newDangerousWeakHashMap() { return _registerDangerousWeakMap(synchroMap(new WeakHashMap())); } public static Map newDangerousWeakHashMap(Object initFunction) { return _registerDangerousWeakMap(synchroMap(new WeakHashMap()), initFunction); } public static void assertNotOnAWTThread() { assertFalse("Can't do this in AWT thread", isAWTThread()); } public static boolean web_matchNodeName(WebNode node, String pat) { return eq(pat, "*") || eqic(pat, web_text(node)); } public static A logStructureWithDate(File logFile, A o) { logQuoted(logFile, now() + " " + structure(o)); return o; } public static A logStructureWithDate(String fileName, A o) { return logStructureWithDate(getProgramFile(fileName), o); } public static File mechListAppendLogFile(String listName) { return javaxDataDir("Mech List Appends/" + uniqueFileNameUsingMD5_80(listName)); } public static int jfind(String s, String in) { return jfind(javaTok(s), in); } public static int jfind(List tok, String in) { return jfind(tok, 1, in); } public static int jfind(List tok, int startIdx, String in) { return jfind(tok, startIdx, in, null); } public static int jfind(List tok, String in, Object condition) { return jfind(tok, 1, in, condition); } public static int jfind(List tok, int startIdx, String in, Object condition) { List tokin = javaTok(in); jfind_preprocess(tokin); return jfind(tok, startIdx, tokin, condition); } public static int jfind(List tok, List tokin) { return jfind(tok, 1, tokin); } public static int jfind(List tok, int startIdx, List tokin) { return jfind(tok, startIdx, tokin, null); } public static int jfind(List tok, int startIdx, List tokin, Object condition) { return findCodeTokens(tok, startIdx, false, toStringArray(codeTokensOnly(tokin)), condition); } public static List jfind_preprocess(List tok) { for (String type : litlist("quoted", "id", "int")) replaceSublist(tok, ll("<", "", type, "", ">"), ll("<" + type + ">")); replaceSublist(tok, ll("\\", "", "*"), ll("\\*")); return tok; } public static DialogIO talkToSubBot(final long vport, final DialogIO io) { return talkToSubBot(String.valueOf(vport), io); } public static DialogIO talkToSubBot(final String subBot, final DialogIO io) { if (subBot == null) return io; return new talkToSubBot_IO(subBot, io); } public static class talkToSubBot_IO extends DialogIO { public String subBot; public DialogIO io; public talkToSubBot_IO(String subBot, DialogIO io) { this.io = io; this.subBot = subBot; } public boolean isStillConnected() { return io.isStillConnected(); } public String readLineImpl() { return io.readLineImpl(); } public boolean isLocalConnection() { return io.isLocalConnection(); } public Socket getSocket() { return io.getSocket(); } public void close() { io.close(); } public void sendLine(String line) { io.sendLine(format3("please forward to bot *: *", subBot, line)); } } public static List quickBotScan() { return ProgramScan.quickBotScan(); } public static List quickBotScan(int[] preferredPorts) { return ProgramScan.quickBotScan(preferredPorts); } public static List quickBotScan(String searchPattern) { List l = new ArrayList(); for (ProgramScan.Program p : ProgramScan.quickBotScan()) if (indexOfIgnoreCase(p.helloString, searchPattern) == 0) l.add(p); return l; } public static String firstPartOfHelloString(String s) { int i = s.lastIndexOf('/'); return i < 0 ? s : rtrim(s.substring(0, i)); } public static String sendToLocalBotQuietly(String bot, String text, Object... args) { text = format3(text, args); DialogIO channel = newFindBot2(bot); if (channel == null) throw fail(quote(bot) + " not found"); try { channel.readLine(); channel.sendLine(text); String s = channel.readLine(); return s; } catch (Throwable e) { e.printStackTrace(); return null; } finally { channel.close(); } } public static String sendToLocalBotQuietly(int port, String text, Object... args) { text = format3(text, args); DialogIO channel = talkTo(port); try { channel.readLine(); channel.sendLine(text); String s = channel.readLine(); return s; } catch (Throwable e) { e.printStackTrace(); return null; } finally { if (channel != null) channel.close(); } } public static List dropPunctuation_keep = litlist("*", "<", ">"); public static List dropPunctuation(List tok) { tok = new ArrayList(tok); for (int i = 1; i < tok.size(); i += 2) { String t = tok.get(i); if (t.length() == 1 && !Character.isLetter(t.charAt(0)) && !Character.isDigit(t.charAt(0)) && !dropPunctuation_keep.contains(t)) { tok.set(i - 1, tok.get(i - 1) + tok.get(i + 1)); tok.remove(i); tok.remove(i); i -= 2; } } return tok; } public static String dropPunctuation(String s) { return join(dropPunctuation(nlTok(s))); } public static boolean equalsIgnoreCase(String a, String b) { return eqic(a, b); } public static boolean equalsIgnoreCase(char a, char b) { return eqic(a, b); } public static int latestInstalledJavaX() { File[] files = new File(userHome(), ".javax").listFiles(); int v = 0; if (files != null) for (File f : files) { Matcher m = Pattern.compile("x(\\d\\d\\d?)\\.jar").matcher(f.getName()); if (m.matches()) v = Math.max(v, Integer.parseInt(m.group(1))); } return v; } public static ThreadLocal>> loadBinaryPage_responseHeaders = new ThreadLocal(); public static ThreadLocal> loadBinaryPage_extraHeaders = new ThreadLocal(); public static byte[] loadBinaryPage(String url) { try { print("Loading " + url); return loadBinaryPage(loadPage_openConnection(new URL(url))); } catch (Exception __e) { throw rethrow(__e); } } public static byte[] loadBinaryPage(URLConnection con) { try { Map extraHeaders = getAndClearThreadLocal(loadBinaryPage_extraHeaders); setHeaders(con); for (String key : keys(extraHeaders)) con.setRequestProperty(key, extraHeaders.get(key)); return loadBinaryPage_noHeaders(con); } catch (Exception __e) { throw rethrow(__e); } } public static byte[] loadBinaryPage_noHeaders(URLConnection con) { try { ByteArrayOutputStream buf = new ByteArrayOutputStream(); InputStream inputStream = con.getInputStream(); loadBinaryPage_responseHeaders.set(con.getHeaderFields()); long len = 0; try { len = con.getContentLength(); } catch (Throwable e) { printStackTrace(e); } int n = 0; while (true) { int ch = inputStream.read(); if (ch < 0) break; buf.write(ch); if (++n % 100000 == 0) println(" " + n + (len != 0 ? "/" + len : "") + " bytes loaded."); } inputStream.close(); return buf.toByteArray(); } catch (Exception __e) { throw rethrow(__e); } } public static void saveBinaryFile(String fileName, byte[] contents) throws IOException { File file = new File(fileName); File parentFile = file.getParentFile(); if (parentFile != null) parentFile.mkdirs(); String tempFileName = fileName + "_temp"; FileOutputStream fileOutputStream = newFileOutputStream(tempFileName); fileOutputStream.write(contents); fileOutputStream.close(); if (file.exists() && !file.delete()) throw new IOException("Can't delete " + fileName); if (!new File(tempFileName).renameTo(file)) throw new IOException("Can't rename " + tempFileName + " to " + fileName); } public static void saveBinaryFile(File fileName, byte[] contents) { try { saveBinaryFile(fileName.getPath(), contents); } catch (IOException e) { throw new RuntimeException(e); } } public static String ymd() { return year() + formatInt(month(), 2) + formatInt(dayOfMonth(), 2); } public static String hms() { return hms(now()); } public static String hms(long time) { return new SimpleDateFormat("HHmmss").format(time); } public static void saveStreamToGZFile(InputStream in, File file) { try { File parentFile = file.getParentFile(); if (parentFile != null) parentFile.mkdirs(); String tempFileName = file.getPath() + "_temp"; File tempFile = new File(tempFileName); if (tempFile.exists()) try { String saveName = tempFileName + ".saved." + now(); copyFile(tempFile, new File(saveName)); } catch (Throwable e) { printStackTrace(e); } FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath()); GZIPOutputStream gos = new GZIPOutputStream(fileOutputStream); copyStreamAndCloseAll(in, gos); if (file.exists() && !file.delete()) throw fail("Can't delete " + file.getPath()); if (!tempFile.renameTo(file)) throw fail("Can't rename " + tempFile + " to " + file); } catch (Exception __e) { throw rethrow(__e); } } public static FileInputStream newFileInputStream(File path) throws IOException { return newFileInputStream(path.getPath()); } public static FileInputStream newFileInputStream(String path) throws IOException { FileInputStream f = new FileInputStream(path); return f; } public static Cache> knownAdjectives_cache = new Cache("knownAdjectives_load"); public static Set knownAdjectives() { return knownAdjectives_cache.get(); } public static Set knownAdjectives_load() { return new TreeSet(tlft(loadSnippet("#1011077"))); } public static void collectionAdd(Collection c, A a) { if (c != null && a != null) c.add(a); } public static String singleFieldName(Class c) { Set l = listFields(c); if (l(l) != 1) throw fail("No single field found in " + c + " (have " + n(l(l), "fields") + ")"); return first(l); } public static void warnIfOddCount(Object... list) { if (odd(l(list))) printStackTrace("Odd list size: " + list); } public static boolean checkConceptFields(Concept x, Object... data) { for (int i = 0; i < l(data); i += 2) if (neq(cget(x, (String) data[i]), deref(data[i + 1]))) return false; return true; } public static String structure_addTokenMarkers(String s) { List tok = javaTok(s); TreeSet refs = new TreeSet(); for (int i = 1; i < l(tok); i += 2) { String t = tok.get(i); if (t.startsWith("t") && isInteger(t.substring(1))) refs.add(parseInt(t.substring(1))); } if (empty(refs)) return s; for (int i : refs) { int idx = i * 2 + 1; String t = ""; if (endsWithLetterOrDigit(tok.get(idx - 1))) t = " "; tok.set(idx, t + "m" + i + " " + tok.get(idx)); } return join(tok); } public static List findMethodsNamed2(Object obj, String method, boolean staticOnly) { if (obj == null) return null; return findMethodsNamed2(_getClass(obj), method, staticOnly); } public static List findMethodsNamed2(Class c, String method, boolean staticOnly) { List l = new ArrayList(); while (c != null) { for (Method m : c.getDeclaredMethods()) if (m.getName().equals(method) && (!staticOnly || (m.getModifiers() & Modifier.STATIC) != 0)) { m.setAccessible(true); l.add(m); } c = c.getSuperclass(); } return l; } public static int methodApplicabilityScore(Method m, Object[] args) { Class[] types = m.getParameterTypes(); if (types.length != args.length) return Integer.MAX_VALUE; int score = 0; for (int i = 0; i < types.length; i++) { Object a = args[i]; Class c = types[i]; if (a == null) ++score; else if (c == a.getClass()) { } else if (isInstanceX(c, a)) ++score; else return Integer.MAX_VALUE; } return score; } public static String methodSignature(String name, Object[] args) { StringBuilder buf = new StringBuilder(name + "("); for (int i = 0; i < l(args); i++) { if (i != 0) buf.append(", "); buf.append(getClassName(args[i])); } buf.append(")"); return str(buf); } public static String tok_addReturn(List tok) { String lastToken = get(tok, l(tok) - 2); if (eq(lastToken, "}") || eq(lastToken, ";")) return join(tok); return "ret " + join(tok) + ";"; } public static String tok_addReturn(String s) { return tok_addReturn(javaTok(s)); } public static boolean containsReturnWithArgument(String code) { List tok = javaTok(code); for (int i = 1; i + 2 < l(tok); i += 2) if (eqOneOf(tok.get(i), "ret", "return") && neq(tok.get(i + 2), ";")) return true; return false; } public static Object callCalc(Object o) { return call(o, "calc"); } public static Object[] newObjectArrayOfSameType(Object[] a, int n) { return (Object[]) Array.newInstance(a.getClass().getComponentType(), n); } public static boolean isAnonymousClassName(String s) { for (int i = 0; i < l(s); i++) if (s.charAt(i) == '$' && Character.isDigit(s.charAt(i + 1))) return true; return false; } public static Class getClass_vmName(Object realm, String name) { try { try { return getClass(realm).getClassLoader().loadClass(name); } catch (ClassNotFoundException e) { return null; } } catch (Exception __e) { throw rethrow(__e); } } public static Map nuObjectWithoutArguments_cache = newDangerousWeakHashMap(); public static Object nuObjectWithoutArguments(String className) { try { return nuObjectWithoutArguments(classForName(className)); } catch (Exception __e) { throw rethrow(__e); } } public static A nuObjectWithoutArguments(Class c) { try { Constructor m; m = nuObjectWithoutArguments_cache.get(c); if (m == null) nuObjectWithoutArguments_cache.put(c, m = nuObjectWithoutArguments_findConstructor(c)); return (A) m.newInstance(); } catch (Exception __e) { throw rethrow(__e); } } public static Constructor nuObjectWithoutArguments_findConstructor(Class c) { for (Constructor m : c.getDeclaredConstructors()) if (empty(m.getParameterTypes())) { m.setAccessible(true); return m; } throw fail("No default constructor found in " + c.getName()); } public static Writer outputStreamToWriter(OutputStream out) { try { return new OutputStreamWriter(out, "UTF-8"); } catch (Exception __e) { throw rethrow(__e); } } public static String quoteIfNotIdentifierOrInteger(String s) { if (s == null) return null; return isJavaIdentifier(s) || isInteger(s) ? s : quote(s); } public static ArrayList cloneListSynchronizingOn(Collection l, Object mutex) { if (l == null) return new ArrayList(); synchronized (mutex) { return new ArrayList(l); } } public static A optPar(ThreadLocal tl, A defaultValue) { A a = tl.get(); if (a != null) { tl.set(null); return a; } return defaultValue; } public static A optPar(ThreadLocal tl) { return optPar(tl, null); } public static String hmsWithColons() { return hmsWithColons(now()); } public static String hmsWithColons(long time) { return new SimpleDateFormat("HH:mm:ss").format(time); } public static String getComputerID_quick() { return computerID(); } public static A vm_generalMap_get(Object key) { return (A) vm_generalMap().get(key); } public static Object vm_generalMap_put(Object key, Object value) { return mapPutOrRemove(vm_generalMap(), key, value); } public static Object unstructureGZFile(File f) { try { BufferedReader reader = utf8BufferedReader(gzInputStream(f)); return unstructure_tok(javaTokC_noMLS_onReader(reader), false, null); } catch (Exception __e) { throw rethrow(__e); } } public static String addPrefix(String prefix, String s) { return s.startsWith(prefix) ? s : prefix + s; } public static void scpUploadWithMkdirs(String user, String host, final String remotePath, final long length, final InputStream in) { try { final SSHClient client = sshLogin(user, host); try { String remoteDir = dropAfterLastSlash(remotePath); if (nempty(remoteDir)) client.newSFTPClient().mkdirs(remoteDir); print("Uploading to on " + user + "@" + host + ": " + remotePath); SCPFileTransfer fileTransfer = client.newSCPFileTransfer(); fileTransfer.upload(new InMemorySourceFile() { public String getName() { return "dummy"; } public long getLength() { return length; } public InputStream getInputStream() { return in; } }, remotePath); } finally { sshDisconnect(client); } } catch (Exception __e) { throw rethrow(__e); } } public static List splitAtJavaTokens(String s, String... splitTokens) { List tok = javaTok(s); List l = new ArrayList(); int i = 1; while (i < l(tok)) { int j = smartIndexOfAny(tok, i, splitTokens); l.add(join(subList(tok, i, j - 1))); i = j + 2; } return l; } public static A syncGet(List l, int idx) { if (l == null || idx < 0) return null; synchronized (l) { return idx < l(l) ? l.get(idx) : null; } } public static TreeSet treeSet() { return new TreeSet(); } public static String quickSubstring(String s, int i, int j) { if (i == j) return ""; return s.substring(i, j); } public static boolean isURL(String s) { return s.startsWith("http://") || s.startsWith("https://"); } public static BufferedImage imageIO_readURL(String url) { try { if (startsWith(url, "https:")) disableCertificateValidation(); return ImageIO.read(new URL(url)); } catch (Exception __e) { throw rethrow(__e); } } public static File getCacheProgramDir() { return getCacheProgramDir(getProgramID()); } public static File getCacheProgramDir(String snippetID) { return new File(userHome(), "JavaX-Caches/" + formatSnippetIDOpt(snippetID)); } public static boolean isLowerHexString(String s) { for (int i = 0; i < l(s); i++) { char c = s.charAt(i); if (c >= '0' && c <= '9' || c >= 'a' && c <= 'f') { } else return false; } return true; } public static IterableIterator emptyIterableIterator_instance = new IterableIterator() { public Object next() { throw fail(); } public boolean hasNext() { return false; } }; public static IterableIterator emptyIterableIterator() { return emptyIterableIterator_instance; } public static IterableIterator linesFromReader(Reader r) { final BufferedReader br = bufferedReader(r); return iteratorFromFunction_f0(new F0() { public String get() { try { return readLineFromReaderWithClose(br); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "readLineFromReaderWithClose(br)"; } }); } public static BufferedReader utf8bufferedReader(InputStream in) { try { return new BufferedReader(new InputStreamReader(in, "UTF-8")); } catch (Exception __e) { throw rethrow(__e); } } public static BufferedReader utf8bufferedReader(File f) { try { return utf8bufferedReader(newFileInputStream(f)); } catch (Exception __e) { throw rethrow(__e); } } public static void saveGZStructureToFile(String file, Object o) { saveGZStructureToFile(getProgramFile(file), o); } public static void saveGZStructureToFile(File file, Object o) { try { File parentFile = file.getParentFile(); if (parentFile != null) parentFile.mkdirs(); File tempFile = tempFileFor(file); if (tempFile.exists()) try { String saveName = tempFile.getPath() + ".saved." + now(); copyFile(tempFile, new File(saveName)); } catch (Throwable e) { printStackTrace(e); } FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath()); try { GZIPOutputStream gos = new GZIPOutputStream(fileOutputStream); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(gos, "UTF-8"); PrintWriter printWriter = new PrintWriter(outputStreamWriter); structureToPrintWriter(o, printWriter); printWriter.close(); gos.close(); fileOutputStream.close(); } catch (Throwable e) { fileOutputStream.close(); tempFile.delete(); throw rethrow(e); } if (file.exists() && !file.delete()) throw new IOException("Can't delete " + file.getPath()); if (!tempFile.renameTo(file)) throw new IOException("Can't rename " + tempFile + " to " + file); } catch (Exception __e) { throw rethrow(__e); } } public static boolean ai_postingInSubSpace() { return ai_postHandler() != null || ai_webPostingDisabled(); } public static List ai_texts_unquote(String a, String b, String c) { return unquoteAll(ai_texts(a, b, c)); } public static List> ai_texts_xy_unquoteBoth(String a, String b, String c) { List> l = new ArrayList(); for (Pair p : ai_texts_dollarXY(a, b, c)) l.add(pair(unquote(p.a), unquote(p.b))); return l; } public static String formatDollarVarsIC(String pat, Map vars) { return formatDollarVars(pat, asCIMap(vars)); } public static List filterMatchDollarX_IC(String pat, Collection l) { List out = new ArrayList(); for (String s : unnull(l)) { Map match = matchDollarVarsIC(pat, s); if (match != null) out.add(match.get("$X")); } return out; } public static String matchDollarX_IC(String pat, String s) { Map match = matchDollarVarsIC(pat, s); return match != null ? match.get("$X") : null; } public static List tok_classifyEnglishWord(String t) { HashSet l = new HashSet(); if (isNoun(t)) l.add("noun"); if (isAdjective(t)) l.add("adjective"); l.addAll(ai_isAWhat(t)); return ai_sortCategoriesBySize(l); } public static TreeSet litCISet(String... items) { return litciset(items); } public static Map unstructureMap(String s) { return (Map) unstructure(s); } public static boolean checkFields(Object x, Object... data) { for (int i = 0; i < l(data); i += 2) if (neq(getOpt(x, (String) data[i]), data[i + 1])) return false; return true; } public static TimeZone localTimeZone() { return getTimeZone(standardTimeZone()); } public static Map web_matchAllPerms(Web patternWeb, Web inputWeb) { List patternNodes = web_nodes(patternWeb); List inputNodes = web_nodes(inputWeb); if (l(patternNodes) != l(inputNodes)) return null; List> perms = allPermutations(inputNodes); Best> best = new Best(); for (List perm : perms) best.put(perm, web_matchNodeLists(patternNodes, perm)); List l = best.getIfScoreAbove(0); return l == null ? null : twoListsToOrderedMap(patternNodes, l); } public static boolean isThirdPersonVerb(String s) { return containsNotNull(thirdPersonVerbs(), s); } public static String ai_renderAndList(List l) { return textOut_and(l); } public static String ai_renderAndList(List l, String emptyText) { return textOut_and(l, emptyText); } public static Cache> knownAdverbs_cache = new Cache("knownAdverbs_load"); public static Set knownAdverbs() { return knownAdverbs_cache.get(); } public static Set knownAdverbs_load() { return toCaseInsensitiveSet(concatLists(map("adjectiveToAdverb", knownAdjectives()), tlft(loadSnippet("#1011128")))); } public static Cache> verbToThirdPersonMap_cache = new Cache("verbToThirdPersonMap_load"); public static Map verbToThirdPersonMap() { return verbToThirdPersonMap_cache.get(); } public static Map verbToThirdPersonMap_load() { HashMap map = new HashMap(); for (List l : englishVerbQuintuples()) map.put(get(l, 0), get(l, 3)); return map; } public static Cache> verbToPastMap_cache = new Cache("verbToPastMap_load"); public static Map verbToPastMap() { return verbToPastMap_cache.get(); } public static Map verbToPastMap_load() { HashMap map = new HashMap(); for (List l : englishVerbQuintuples()) map.put(get(l, 0), get(l, 1)); return map; } public static String joinStrings(String sep, String... strings) { return joinStrings(sep, Arrays.asList(strings)); } public static String joinStrings(String sep, Collection strings) { StringBuilder buf = new StringBuilder(); for (String s : unnull(strings)) if (nempty(s)) { if (nempty(buf)) buf.append(sep); buf.append(s); } return str(buf); } public static boolean isSingularNoun(String s) { return contains(knownNouns(), lower(s)); } public static boolean isPluralNoun(String s) { return contains(knownPluralNouns(), lower(s)); } public static Cache> knownPhrases_cache = new Cache("knownPhrases_load"); public static Set knownPhrases() { return knownPhrases_cache.get(); } public static Set knownPhrases_load() { return toCaseInsensitiveSet(tlft(loadSnippet("#1011207"))); } public static String loadGZTextFile(File file) { try { if (!file.isFile()) return null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream fis = new FileInputStream(file); GZIPInputStream gis = newGZIPInputStream(fis); try { byte[] buffer = new byte[1024]; int len; while ((len = gis.read(buffer)) != -1) { baos.write(buffer, 0, len); } } finally { fis.close(); } baos.close(); return fromUtf8(baos.toByteArray()); } catch (Exception __e) { throw rethrow(__e); } } public static int indexOfIC_underscore(String a, String b) { int la = l(a), lb = l(b); if (la < lb) return -1; int n = la - lb; elsewhere: for (int i = 0; i <= n; i++) { for (int j = 0; j < lb; j++) { char c2 = b.charAt(j); if (c2 == '_' || eqic(c2, a.charAt(i + j))) { } else continue elsewhere; } return i; } return -1; } public static List computerInjectionsData() { long time = sysNow(); List l = new ArrayList(); for (DialogIO vm : talkToAllVMs()) { try { int vmPort = vm.getPort(); String x = vm.ask("get injections"); String mem = dropUntilColon(vm.ask("how much memory are you consuming")); List injections = (List) (safeUnstructure(x)); for (Object inj : injections) try { String progID = formatSnippetID((String) get(inj, "progID")); l.add(litorderedmap("Program name", getSnippetTitle(progID), "Program ID", progID, "VM Size (Java)", mem, "VM", vmPort, "More info", isTrue(get(inj, "mainDone")) ? "main done" : "main running", "Injection ID", get(inj, "injectionID"))); } catch (Throwable __e) { _handleException(__e); } } catch (Throwable __e) { _handleException(__e); } vm.close(); } done2(time, "Scanned " + n(l, "VMs")); return l; } public static void putIfNotExisting(Map map, A key, B value) { putIfNotThere(map, key, value); } public static boolean methodIsStatic(Method m) { return (m.getModifiers() & Modifier.STATIC) != 0; } public static boolean ai_isSafeToCall(String sfName) { return has_verified_multi(ll("Standard function " + quote(sfName), "Standard function " + sfName), "is", "safe to call") || has_verified(sfName, "is", "a safe standard function"); } public static F1 functionToF1(final Object f) { if (isString(f)) return mainFunctionToF1((String) f); if (f instanceof F1) return (F1) f; return new F1() { public Object get(Object a) { return callF(f, a); } }; } public static boolean startsWithIC(String a, String b) { return startsWithIgnoreCase(a, b); } public static List tlft_j(String text) { return toLinesFullTrim_java(text); } public static WebRelation web_getRelationOpt(WebNode a, WebNode b) { return a.web.getRelationOpt(a, b); } public static int indexOfIgnoreCase_manual(String a, String b) { int la = l(a), lb = l(b); if (la < lb) return -1; int n = la - lb; loop: for (int i = 0; i <= n; i++) { for (int j = 0; j < lb; j++) { char c1 = a.charAt(i + j), c2 = b.charAt(j); if (!eqic(c1, c2)) continue loop; } return i; } return -1; } public static List web_allForwardRelated(WebNode node, String relationLabel) { return web_allForwardRelated(node.web, node, relationLabel); } public static List web_allForwardRelated(Web web, WebNode node, String relationLabel) { return web_allForwardRelated(web, node, web.parseLabel(relationLabel)); } public static List web_allForwardRelated(Web web, WebNode node, Lisp relationLabel) { List l = new ArrayList(); for (Pair p : web_relations(web)) if (p.a == node) { WebNode rel = web.getRelationOpt(p); if (rel.hasLabel(relationLabel)) l.add(p.b); } return l; } public static List web_allForwardRelated(WebNode node) { return pairList_forwardLookup(web_relations(node.web), node); } public static List web_allForwardRelated(Web web, String a, String relationLabel) { Lisp name = web.parseLabel(a); Lisp rel = web.parseLabel(relationLabel); HashSet l = new HashSet(); for (WebNode node : web_allNodesNamed(web, name)) l.addAll(web_allForwardRelated(web, node, rel)); return asList(l); } public static void swapElements(List l, int i, int j) { if (i == j) return; Object o = l.get(i); l.set(i, l.get(j)); l.set(j, o); } public static HashMap twoListsToHashMap(List l1, List l2) { if (l(l1) != l(l2)) throw fail("Differing list size: " + l(l1) + "/" + l(l2)); HashMap map = new HashMap(); int n = l(l1); for (int i = 0; i < n; i++) map.put(l1.get(i), l2.get(i)); return map; } public static HashMap twoListsToHashMap(A[] l1, A[] l2) { return twoListsToHashMap(Arrays.asList(l1), Arrays.asList(l2)); } public static boolean containsDollarVars(String s) { List tok = javaTok(s); for (int i = 1; i < l(tok); i += 2) if (isDollarVar(tok.get(i))) return true; return false; } public static Pair> cloneWebWithMap(Web web) { Web w = web_similarEmptyWeb(web); return pair(w, (Map) web_addWebToWeb(web, w)); } public static void web_modifyTexts(WebNode node, Object f) { List labels = node.labels(); for (int i = 0; i < l(node.labels); i++) { String s = node.web.unparseLabel(labels.get(i)); String s2 = (String) callF(f, s); if (neq(s, s2)) node.setLabel(i, node.web.parseLabel(s2)); } } public static List javaTokDroppingBracketsC_unquote(String s) { return javaTokDroppingBracketsC(unquote(s)); } public static void printStructs(Object... params) { print(renderStructs(params)); } public static String matchBigX(String pat, String s) { List tokPat = javaTokC(pat), tok = javaTokC(s); if (l(tokPat) != l(tok)) return null; Var x = new Var(); for (int i = 0; i < l(tokPat); i++) { String p = tokPat.get(i), t = tok.get(i); if (eq(p, "X")) { if (!strictSetIC(x, t)) return null; } else if (neqic(p, t)) return null; } return unnull(x.get()); } public static String formatVars(String pat, Map vars) { return formatDollarVars(pat, vars); } public static String ai_replaceBigY(String s, String value) { return join(jreplace_return(javaTok(s), "Y", value)); } public static List javaTokDroppingBrackets(String s) { return tok_combineRoundOrCurlyBrackets(javaTok(s)); } public static List jreplace_return(List tok, String in, String out) { jreplace(tok, in, out); return tok; } public static Map ai_getVars() { return ai_getVar_map.get(); } public static LinkedHashMap paramsToMap(Object... params) { LinkedHashMap map = new LinkedHashMap(); for (int i = 0; i + 1 < l(params); i += 2) mapPut(map, params[i], params[i + 1]); return map; } public static File transpiledStandardFunctions_file(TreeSet functionSet) { String name = first(functionSet); if (isWindows()) name = escapeUpperCaseWithCharacter(name, ' '); if (l(functionSet) > 1) name += "-" + md5(joinWithSpace(dropFirst(functionSet))); name += ".java"; return newFile(javaxCachesDir(), "Transpiled Standard Functions", name); } public static File printFileStatus(File f) { return printFileInfo(f); } public static String findTranslators2(String src, List libsOut) { List tok = javaTok(src); int i; while ((i = jfind(tok, "!")) >= 0) { setAdd(libsOut, tok.get(i + 2)); clearTokens(tok, i, i + 3); } return join(tok); } public static File javaCompile_overInternalBot(String src) { return javaCompile_overInternalBot(src, ""); } public static synchronized File javaCompile_overInternalBot(String src, String dehlibs) { return CompilerBot.compile(src, dehlibs); } public static Class hotwireCore(List files) { try { JavaXClassLoader classLoader = hotwire_makeClassLoader(files); Class theClass = classLoader.loadClass("main"); setOpt(theClass, "__javax", getJavaX()); if (getOpt(theClass, "programID") == null) setOpt(theClass, "programID", "#3999999"); if (!_inCore()) hotwire_copyOver(theClass); return theClass; } catch (Exception __e) { throw rethrow(__e); } } public static List loadLibraries(List snippetIDs) { return map("loadLibrary", snippetIDs); } public static void saveTranspiledStandardFunctions(TreeSet functionSet, String transpiledSrc) { try { saveTextFile(transpiledStandardFunctions_file(functionSet), transpiledSrc); } catch (Throwable __e) { _handleException(__e); } } public static boolean strictPutIC(Map map, String key, String value) { if (!(map != null && key != null && value != null)) return true; String old = map.get(key); if (old == null) { map.put(key, value); return true; } return eqic(old, value); } public static boolean isIPv4(String s) { return s != null && l(javaTokC(s)) == 7 && jfind(javaTok(s), "...") == 1; } public static File standardLogFile() { return getProgramFile("log"); } public static String formatGMTWithDate_24(long time) { return simpleDateFormat_GMT("yyyy/MM/dd").format(time) + " " + formatGMT_24(time); } public static String dataSnippetLink(String snippetID) { long id = parseSnippetID(snippetID); if (id >= 1100000 && id < 1200000) return "http://ai1.space/images/raw/" + id; if (id >= 1400000 && id < 1500000) return "http://butter.botcompany.de:8080/files/" + id + "?_pass=" + muricaPassword(); if (id >= 1200000 && id < 1300000) { String pw = muricaPassword(); if (empty(pw)) throw fail("Please set 'murica password by running #1008829"); return "http://butter.botcompany.de:8080/1008823/raw/" + id + "?_pass=" + pw; } else return "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + id + "&contentType=application/binary"; } public static A addAndReturn(Collection c, A a) { if (c != null) c.add(a); return a; } public static void loadBinaryPageToFile(String url, File file) { try { print("Loading " + url); loadBinaryPageToFile(openConnection(new URL(url)), file); } catch (Exception __e) { throw rethrow(__e); } } public static void loadBinaryPageToFile(URLConnection con, File file) { try { setHeaders(con); loadBinaryPageToFile_noHeaders(con, file); } catch (Exception __e) { throw rethrow(__e); } } public static void loadBinaryPageToFile_noHeaders(URLConnection con, File file) { try { File ftemp = new File(f2s(file) + "_temp"); FileOutputStream buf = newFileOutputStream(mkdirsFor(ftemp)); try { InputStream inputStream = con.getInputStream(); long len = 0; try { len = con.getContentLength(); } catch (Throwable e) { printStackTrace(e); } String pat = " {*}" + (len != 0 ? "/" + len : "") + " bytes loaded."; copyStreamWithPrints(inputStream, buf, pat); inputStream.close(); buf.close(); file.delete(); renameFile_assertTrue(ftemp, file); } finally { if (buf != null) buf.close(); } } catch (Exception __e) { throw rethrow(__e); } } public static String asciiHeading(String title) { return "\n" + title + "\n" + rep(l(title), '-'); } public static ThreadLocal print_byThread() { synchronized (print_byThread_lock) { if (print_byThread == null) print_byThread = new ThreadLocal(); } return print_byThread; } public static Set fieldNames(Object o) { return allFields(o); } public static boolean isUpperCase(char c) { return Character.isUpperCase(c); } public static Set setMinus(Set set, Object... stuff) { Set s2 = cloneSet(set); for (Object o : stuff) s2.remove(o); return s2; } public static Set mergeSets(Set... l) { return joinSets(l); } public static Set allNonStaticNonTransientFields(Object o) { TreeSet fields = new TreeSet(); Class _c = _getClass(o); do { for (Field f : _c.getDeclaredFields()) if ((f.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) == 0) fields.add(f.getName()); _c = _c.getSuperclass(); } while (_c != null); return fields; } public static Field findField2(Object o, String field) { Class c = o.getClass(); HashMap map; synchronized (getOpt_cache) { map = getOpt_cache.get(c); if (map == null) map = getOpt_makeCache(c); } if (map == getOpt_special) { if (o instanceof Class) return findField2_findStaticField((Class) o, field); } return map.get(field); } public static Field findField2_findStaticField(Class c, String field) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) return f; _c = _c.getSuperclass(); } while (_c != null); return null; } public static List mapMethod(Iterable c, final String methodName) { return map(c, new F1() { public Object get(Object o) { try { return callOpt(o, methodName); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callOpt(o, methodName)"; } }); } public static List mapMethod(String methodName, Iterable c) { return mapMethod(c, methodName); } public static List mapMethod(String methodName, Enumeration c) { return mapMethod(methodName, enumerationToIterator(c)); } public static A printIndent(A o) { print(indentx(str(o))); return o; } public static void printIndent(String indent, Object o) { print(indentx(indent, str(o))); } public static void printIndent(int indent, Object o) { print(indentx(indent, str(o))); } public static Cache oshi_hardware_cache = new Cache("oshi_hardware_load"); public static HardwareAbstractionLayer oshi_hardware() { return oshi_hardware_cache.get(); } public static HardwareAbstractionLayer oshi_hardware_load() { return new SystemInfo().getHardware(); } public static TripleRef tripleRef(T3 t, int position) { if (position == 0) return new TripleA(t); if (position == 1) return new TripleB(t); if (position == 2) return new TripleC(t); throw fail("crazy position"); } public static boolean isImageServerSnippet(long id) { return id >= 1100000 && id < 1200000; } public static String imageServerLink(String md5OrID) { if (possibleMD5(md5OrID)) return "https://botcompany.de/images/md5/" + md5OrID; return imageServerLink(parseSnippetID(md5OrID)); } public static String imageServerLink(long id) { return "https://botcompany.de/images/" + id; } public static String emptySymbol_value; public static String emptySymbol() { if (emptySymbol_value == null) emptySymbol_value = symbol(""); return emptySymbol_value; } public static int roundToInt(double d) { return (int) Math.round(d); } public static List sortedDesc(Collection c, final Object comparator) { List l = cloneList(c); sort(l, makeReversedComparator(comparator)); return l; } public static List sortedDesc(Collection c) { List l = cloneList(c); sort(l); Collections.reverse(l); return l; } public static IterableIterator iteratorFromFunction_f0(final F0 f) { class IFF2 extends IterableIterator { public A a; public boolean done; public boolean hasNext() { getNext(); return !done; } public A next() { getNext(); if (done) throw fail(); A _a = a; a = null; return _a; } public void getNext() { if (done || a != null) return; a = f.get(); done = a == null; } } ; return new IFF2(); } public static List tok_joinMinusIdentifiers(List tok) { for (int i = 1; i + 4 < l(tok); i += 2) if (isMinusIdentifier(get(tok, i)) && eq(get(tok, i + 2), "-") && isIdentifier(get(tok, i + 4))) { replaceSublist(tok, i, i + 5, ll(join(subList(tok, i, i + 5)))); i -= 2; } return tok; } public static List htmlFineTok(String s) { List tok = new ArrayList(); int l = s.length(); int i = 0, n = 0; while (i < l) { int j = i; char c, d; while (j < l) { c = s.charAt(j); d = j + 1 >= l ? '\0' : s.charAt(j + 1); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else break; } tok.add(quickSubstring(s, i, j)); ++n; i = j; if (i >= l) break; c = s.charAt(i); d = i + 1 >= l ? '\0' : s.charAt(i + 1); if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { if (s.charAt(j) == opener) { ++j; break; } else if (s.charAt(j) == '\\' && j + 1 < l) j += 2; else ++j; } } else if (Character.isJavaIdentifierStart(c)) do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; } else ++j; tok.add(quickSubstring(s, i, j)); ++n; i = j; } if ((tok.size() % 2) == 0) tok.add(""); return tok; } public static boolean isMinusIdentifier(String s) { if (empty(s)) return false; if (!Character.isJavaIdentifierStart(s.charAt(0)) && !s.startsWith("-")) return false; for (int i = 1; i < s.length(); i++) if (!Character.isJavaIdentifierPart(s.charAt(i)) && s.charAt(i) != '-') return false; return true; } public static String htmlunquote(String s) { if (s.startsWith("'") && s.endsWith("'") && s.length() >= 2 || s.startsWith("\"") && s.endsWith("\"") && s.length() >= 2) s = s.substring(1, s.length() - 1); return htmldecode(s); } public static String htmlencode_forParams(String s) { if (s == null) return ""; StringBuilder out = new StringBuilder(Math.max(16, s.length())); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c > 127 || c == '"' || c == '<' || c == '>') { out.append("&#"); out.append((int) c); out.append(';'); } else out.append(c); } return out.toString(); } public static void web_deindex(WebNode n) { MultiMap index = n.web.index; if (index != null) for (Lisp label : n.labels()) index.remove(label, n); } public static void web_index(WebNode n) { MultiMap index = n.web.index; if (index != null) for (Lisp label : n.labels()) index.setPut(label, n); } public static Lisp lisp_intern(Lisp l) { if (l == null) return null; return lisp(intern(l.head), map_lisp_intern(l.args)); } public static Method fastIntern_method; public static String fastIntern(String s) { try { if (s == null) return null; if (fastIntern_method == null) { fastIntern_method = findMethodNamed(javax(), "internPerProgram"); if (fastIntern_method == null) upgradeJavaXAndRestart(); } return (String) fastIntern_method.invoke(null, s); } catch (Exception __e) { throw rethrow(__e); } } public static Map classForName_cache = synchroHashMap(); public static Class classForName(String name) { try { Class c = classForName_cache.get(name); if (c == null) classForName_cache.put(name, c = Class.forName(name)); return c; } catch (Exception __e) { throw rethrow(__e); } } public static List getClasses(Object[] array) { List l = new ArrayList(); for (Object o : array) l.add(_getClass(o)); return l; } public static void failIfOddCount(Object... list) { if (odd(l(list))) throw fail("Odd list size: " + list); } public static Lock mainLock_lock = fairLock(); public static Lock mainLock() { return mainLock_lock; } public static Lock ai_activeSpecLock = lock(); public static Lock ai_activeSpecLock() { return ai_activeSpecLock; } public static void ai_invalidateTriple(String a, String b, String c) { ai_invalidateWebs(ai_cache_findTriple(a, b, c)); } public static TripleWeb ai_invalidateTriple(TripleWeb t) { return t == null ? null : ai_getTriple(ai_invalidateWeb(t.globalID())); } public static void pcallF_all(Collection l, Object... args) { if (l != null) for (Object f : cloneList(l)) pcallF(f, args); } public static class FileStatus { public long time, size; public boolean isDir; public boolean equals(Object o) { return stdEq2(this, o); } public int hashCode() { return stdHash2(this); } } public static FileStatus fileStatus(String path) { return fileStatus(newFile(path)); } public static FileStatus fileStatus(File f) { return nu(FileStatus.class, "time", f.lastModified(), "size", fileLength(f), "isDir", f.isDirectory()); } public static File conceptsFile(String progID) { return getProgramFile(progID, "concepts.structure.gz"); } public static File conceptsFile() { return conceptsFile(dbProgramID()); } public static boolean web_nodesContainText(Collection l, String text) { if (l != null) for (WebNode n : l) if (web_hasText(n, text)) return true; return false; } public static List ai_onNewIndexedText_list = synchroList(); public static void ai_onNewIndexedText_do(Object f) { setAdd(ai_onNewIndexedText_list, f); } public static void ai_onNewIndexedText(String text) { pcallFAll(ai_onNewIndexedText_list, text); } public static List ai_filterBadWebs(List _webs) { List webs = emptyListWithCapacity(_webs); int dropped = 0, dropped2 = 0; for (Web web : _webs) if (web_nodesTooLong(web)) dropped++; else if (!web_tripelizable(web)) dropped2++; else webs.add(web); if (dropped != 0) print("Dropped " + nWeb(dropped) + " with too long nodes"); if (dropped2 != 0) print("Dropped " + n(dropped2, "non-triple")); return webs; } public static List allWebs() { return concatWebLists(allWebs_fullList()); } public static MultiMap ai_indexWebs(Collection webs) { if (hasMultiCore() && l(webs) >= 2000) return ai_indexWebs_parallel2(webs); return ai_indexWebs_serial(webs); } public static MultiMap ai_indexWebs_serial(Collection webs) { MultiMap index = ai_emptyWebIndex(); for (Web web : unnull(webs)) ai_addWebToIndex(index, web); return index; } public static SyncListMultiMap caseInsensitiveSyncListMultiMap() { SyncListMultiMap mm = new SyncListMultiMap(); mm.data = caseInsensitiveMap(); return mm; } public static boolean makeMultiMapsInnerNavigableMapSynchronized(MultiMap mm) { if (!startsWith(className(mm.data), "java.util.Collections$")) { mm.data = synchroNavigableMap((NavigableMap) mm.data); return true; } return false; } public static Map safeUnstructureMap(String s) { return (Map) safeUnstructure(s); } public static byte[] utf8(String s) { return toUtf8(s); } public static String loadTextFileFromZipFile(File inZip, String fileName) { try { if (!fileExists(inZip)) return null; ZipFile zip = new ZipFile(inZip); try { return loadTextFileFromZipFile(zip, fileName); } finally { zip.close(); } } catch (Exception __e) { throw rethrow(__e); } } public static String loadTextFileFromZipFile(ZipFile zip, String fileName) { try { ZipEntry entry = zip.getEntry(fileName); if (entry == null) return null; InputStream fin = zip.getInputStream(entry); ByteArrayOutputStream baos = new ByteArrayOutputStream(); copyStream(fin, baos); fin.close(); return fromUTF8(baos.toByteArray()); } catch (Exception __e) { throw rethrow(__e); } } public static void hotwire_copyOver(Class c) { synchronized (StringBuffer.class) { for (String field : litlist("print_log", "print_silent", "androidContext", "_userHome")) { Object o = getOpt(mc(), field); if (o != null) setOpt(c, field, o); } Object mainBot = getMainBot(); if (mainBot != null) setOpt(c, "mainBot", mainBot); setOpt(c, "creator_class", new WeakReference(mc())); } } public static boolean isLocalhost(String ip) { return isLoopbackIP(ip) || eqic(ip, "localhost"); } public static int vmPort() { return myVMPort(); } public static DialogIO talkToThisVM() { return new talkToThisVM_IO(); } public static class talkToThisVM_IO extends DialogIO { public List answers = ll(thisVMGreeting()); public boolean isLocalConnection() { return true; } public boolean isStillConnected() { return true; } public int getPort() { return vmPort(); } public void sendLine(String line) { answers.add(or2(sendToThisVM_newThread(line), "?")); } public String readLineImpl() { try { return popFirst(answers); } catch (Exception __e) { throw rethrow(__e); } } public void close() { } public Socket getSocket() { return null; } } public static boolean isPreposition(String s) { return containsNotNull(knownPrepositions(), s); } public static TripleWeb ai_webToTripleWeb(Web web) { T3 t = ai_webToTriple(web); if (t == null) return null; TripleWeb w = newTripleWebWithSource(web.source); w.a = t.a; w.b = t.b; w.c = t.c; w.globalID(web.globalIDObj()); w.created(web.created); w.flags = web.unverified ? w.UNVERIFIED : 0; return w; } public static Map myFrames_list = weakHashMap(); public static List myFrames() { return swing(new F0>() { public List get() { try { return keysList(myFrames_list); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "keysList(myFrames_list)"; } }); } public static Object iteratorFromFunction_endMarker = new Object(); public static IterableIterator iteratorFromFunction_withEndMarker(final Object f) { class IFF extends IterableIterator { public A a; public boolean done; public boolean hasNext() { getNext(); return !done; } public A next() { getNext(); if (done) throw fail(); A _a = a; a = null; return _a; } public void getNext() { if (done || a != null) return; a = (A) callF(f); if (a == iteratorFromFunction_endMarker) done = true; } } ; return new IFF(); } public static IterableIterator iteratorFromFunction_withEndMarker(final F0 f) { return iteratorFromFunction_withEndMarker_f0(f); } public static String adjectiveToAdverb(String s) { s = replaceSuffixIC("y", "i", s); s = replaceSuffixIC("c", "cal", s); s = dropSuffixIC("le", s); return s + "ly"; } public static ThreadLocal initializedThreadLocal(final Class c) { return new ThreadLocal() { public A initialValue() { return nuInstance(c); } }; } public static List nlTok3(String s) { List tok = new ArrayList(); int l = s.length(); int i = 0; while (i < l) { int j = i; char c; String cc; while (j < l) { c = s.charAt(j); cc = s.substring(j, Math.min(j + 2, l)); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else if (cc.equals("/*")) { do ++j; while (j < l && !s.substring(j, Math.min(j + 2, l)).equals("*/")); j = Math.min(j + 2, l); } else if (cc.equals("//")) { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else break; } tok.add(s.substring(i, j)); i = j; if (i >= l) break; c = s.charAt(i); cc = s.substring(i, Math.min(i + 2, l)); if (c == '“' || c == '”') c = '"'; if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { char _c = s.charAt(j); if (_c == '“' || _c == '”') _c = '"'; if (_c == opener) { ++j; break; } else if (s.charAt(j) == '\\' && j + 1 < l) j += 2; else ++j; } if (j - 1 >= i + 1) { tok.add(opener + s.substring(i + 1, j - 1) + opener); i = j; continue; } } else if (Character.isJavaIdentifierStart(c)) do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)))); else if (Character.isDigit(c)) do ++j; while (j < l && Character.isDigit(s.charAt(j))); else if (cc.equals("[[")) { do ++j; while (j + 1 < l && !s.substring(j, j + 2).equals("]]")); j = Math.min(j + 2, l); } else if (cc.equals("[=") && i + 2 < l && s.charAt(i + 2) == '[') { do ++j; while (j + 2 < l && !s.substring(j, j + 3).equals("]=]")); j = Math.min(j + 3, l); } else if (s.substring(j, Math.min(j + 3, l)).equals("...")) j += 3; else if (c == '$' || c == '#') do ++j; while (j < l && Character.isDigit(s.charAt(j))); else ++j; tok.add(s.substring(i, j)); i = j; } if ((tok.size() % 2) == 0) tok.add(""); return tok; } public static String dropAfterSubString_orNull(String s, String b) { int i = indexOf(s, b); if (i < 0) return null; return substring(s, 0, i); } public static String ai_dropLeadingAdverbs(String s) { List tok = nlTok(s); int i = indexOfNotPredC(tok, "isAdverb"); return trimJoinSubList(tok, i - 1); } public static int indexOfPred(List l, Object pred) { for (int i = 0; i < l(l); i++) if (checkCondition(pred, l.get(i))) return i; return -1; } public static String joinTakeFirst(List l, int n) { return join(takeFirst(n, l)); } public static Map curlyBracketMap(List tok) { TreeMap map = new TreeMap(); List stack = new ArrayList(); int n = l(tok); for (int i = 1; i < n; i += 2) { String t = tok.get(i); if (eq(t, "{")) stack.add(i); else if (eq(t, "}") && nempty(stack)) map.put(liftLast(stack), i); } return map; } public static int ai_text_int(String a, String b, String c) { return parseIntOrMinus1(firstIntegerString(ai_texts(a, b, c))); } public static int ai_text_int(T3 t) { return ai_text_int(t.a, t.b, t.c); } public static List ai_variationsOf(String s) { return uniquify(concatLists(ll(s), ai_texts_bidirectionalOperands(s, "unquotes to:"))); } public static List ai_texts_verified_unquote(String a, String b, String c) { return unquoteAll(ai_texts_verified(a, b, c)); } public static List web_allRelationsWithLabel(Web web, String label) { Lisp l = web.parseLabel(label); List out = new ArrayList(); for (WebRelation r : web_relationObjects(web)) if (r.hasLabel(l)) out.add(r); return out; } public static Map threadInfosToThreads_map(List l) { HashMap map = new HashMap(); for (Thread t : keys(Thread.getAllStackTraces())) map.put(t.getId(), t); HashMap out = new HashMap(); for (ThreadInfo t : l) mapPut(out, t, map.get(t.getThreadId())); return out; } public static String stackTraceToString(StackTraceElement[] st) { return lines(allToString(asList(st))); } public static int web_countLabels(WebNode n) { return web_numberOfLabels(n); } public static String js_smartBotsChatPostCode(String msg) { return "$.get('http://botcompany.de/1011975/raw/msg?message=" + urlencode(msg) + "'); return false;"; } public static volatile long gc_notTooOften_last; public static volatile long gc_notTooOften_interval = 1000; public static void gc_notTooOften() { gc_notTooOften_last = fixTimestamp(gc_notTooOften_last); if (now() >= gc_notTooOften_last + gc_notTooOften_interval) { gc(); gc_notTooOften_last = now(); } } public static int done2_systemErr_minPrint = 10; public static long done2_systemErr(long startTime, String desc) { long time = sysNow() - startTime; if (time >= done2_systemErr_minPrint) System.err.println(desc + " [" + time + " ms]"); return time; } public static long done2_systemErr(String desc, long startTime) { return done2_systemErr(startTime, desc); } public static long done2_systemErr(long startTime) { return done2_systemErr(startTime, ""); } public static String dropTranslators(String src) { return findTranslators2(src, null); } public static JWindow showLoadingAnimation() { return showLoadingAnimation("Hold on user..."); } public static JWindow showLoadingAnimation(String text) { try { return showAnimationInTopRightCorner("#1003543", text); } catch (Throwable __e) { return null; } } public static Class hotwireSharingLibraries(String progID) { try { Pair p = CompilerBot.compileSnippet2(progID); File jar = p.a; assertTrue(f2s(jar), jar.isFile()); List files = ll(jar); String dehlibs = unnull(loadTextFileFromZip(jar, "libraries")); List myLibraries = myLibraryFiles(); Matcher matcher = Pattern.compile("\\d+").matcher(dehlibs); while (matcher.find()) { String libID = matcher.group(); File lib = loadLibrary(libID); if (myLibraries.contains(lib)) { } else { files.add(lib); } } JavaXClassLoaderWithParent classLoader = new JavaXClassLoaderWithParent(progID, files, myClassLoader()); return hotwire_finish(classLoader, progID, p.b); } catch (Exception __e) { throw rethrow(__e); } } public static WeakReference creator_class; public static Object creator() { return creator_class == null ? null : creator_class.get(); } public static String programIDWithCase() { return nempty(caseID()) ? programID() + "/" + quoteUnlessIdentifierOrInteger(caseID()) : programID(); } public static boolean exposeMethods2_debug; public static String exposeMethods2(Object receiver, String s, List methodNames) { return exposeMethods2(receiver, s, methodNames, null); } public static String exposeMethods2(Object receiver, String s, List methodNames, Lock lock) { Matches m = new Matches(); if (exposeMethods2_debug) print("Received: " + s); if (match("call *", s, m)) { List l; if (isIdentifier(m.unq(0))) l = ll(m.unq(0)); else l = (List) unstructure(m.unq(0)); String method = getString(l, 0); if (!contains(methodNames, method)) throw fail("Method not allowed: " + method); if (lock != null) lock.lock(); try { if (exposeMethods2_debug) print("Calling: " + method); Object o = call(receiver, method, asObjectArray(subList(l, 1))); if (exposeMethods2_debug) print("Got: " + getClassName(o)); return ok2(structure(o)); } finally { if (lock != null) lock.unlock(); } } if (match("list methods", s)) return ok2(structure(methodNames)); return null; } public static SortedMap synchroTreeMap() { return Collections.synchronizedSortedMap(new TreeMap()); } public static int parseHexChar(char c) { if (c >= '0' && c <= '9') return charDiff(c, '0'); if (c >= 'a' && c <= 'f') return charDiff(c, 'a') + 10; if (c >= 'A' && c <= 'F') return charDiff(c, 'A') + 10; return -1; } public static Class getOuterClass(Class c) { try { String s = c.getName(); int i = s.lastIndexOf('$'); return Class.forName(substring(s, 0, i)); } catch (Exception __e) { throw rethrow(__e); } } public static String shortenClassName(String name) { if (name == null) return null; int i = lastIndexOf(name, "$"); if (i < 0) i = lastIndexOf(name, "."); return i < 0 ? name : substring(name, i + 1); } public static HashMap instanceFieldsMap(Object o) { Class c = o.getClass(); HashMap map; synchronized (getOpt_cache) { map = getOpt_cache.get(c); if (map == null) map = getOpt_makeCache(c); } return map; } public static int systemHashCode(Object o) { return identityHashCode(o); } public static Map> callOpt_noArgs_cache = newDangerousWeakHashMap(); public static Object callOpt_noArgs(Object o, String method) { try { if (o == null) return null; if (o instanceof Class) return callOpt(o, method); Class c = o.getClass(); HashMap map; synchronized (callOpt_noArgs_cache) { map = callOpt_noArgs_cache.get(c); if (map == null) map = callOpt_noArgs_makeCache(c); } Method m = map.get(method); return m != null ? m.invoke(o) : null; } catch (Exception __e) { throw rethrow(__e); } } public static HashMap callOpt_noArgs_makeCache(Class c) { HashMap map = new HashMap(); Class _c = c; do { for (Method m : c.getDeclaredMethods()) if (m.getParameterTypes().length == 0) { m.setAccessible(true); String name = m.getName(); if (!map.containsKey(name)) map.put(name, m); } _c = _c.getSuperclass(); } while (_c != null); callOpt_noArgs_cache.put(c, map); return map; } public static void nohupJavax(final String javaxargs) { { Thread _t_0 = new Thread() { public void run() { try { call(hotwireOnce("#1008562"), "nohupJavax", javaxargs); } catch (Throwable __e) { _handleException(__e); } } }; startThread(_t_0); } } public static void nohupJavax(final String javaxargs, final String vmArgs) { { Thread _t_1 = new Thread() { public void run() { try { call(hotwireOnce("#1008562"), "nohupJavax", javaxargs, vmArgs); } catch (Throwable __e) { _handleException(__e); } } }; startThread(_t_1); } } public static void consoleClearInput() { consoleSetInput(""); } public static Lock downloadLock_lock = fairLock(); public static Lock downloadLock() { return downloadLock_lock; } public static String getSnippetTitleOpt(String s) { return isSnippetID(s) ? getSnippetTitle(s) : s; } public static boolean webs_readTripleFileAsTriples_debug; public static List webs_readTripleFileAsTriples(File f) { if (hasMultipleCores()) return webs_readTripleFileAsTriples_parallel2(f); if (!f.exists()) return ll(); Iterator it = linesFromFile(f); List names = new ArrayList(); while (it.hasNext()) { String s = trim(it.next()); if (empty(s)) break; names.add(symbol(unquote(s))); } if (webs_readTripleFileAsTriples_debug) print("Have " + l(names) + " names"); List triples = new ArrayList(); while (it.hasNext()) { String s = it.next(); try { addIfNotNull(triples, webs_readTripleFileAsTriples_line(s, names)); } catch (Throwable __e) { _handleException(__e); } } return triples; } public static TripleWeb webs_readTripleFileAsTriples_line(String s, List names) { List l = javaTokC(s); if (l(l) >= 8) { String source = intern(unquote(l.get(5))); Object result = null; try { if (eq(get(l, 8), "(")) { int i = indexOf(l, ")", 9); if (i >= 0) result = safeUnstructure(joinWithSpace(subList(l, 9, i))); } } catch (Throwable __e) { _handleException(__e); } TripleWeb t = newTripleWebWithSourceAndResult(source, result); t.a = names.get(parseInt(l.get(0))); t.b = names.get(parseInt(l.get(1))); t.c = names.get(parseInt(l.get(2))); t.globalID(unquote(l.get(3))); t.flags = eq(l.get(6), "v") ? 0 : TripleWeb.UNVERIFIED; t.created(parseLong(l.get(7))); return t; } else if (webs_readTripleFileAsTriples_debug) print("Wrong number of tokens in line: " + l(l) + " - " + sfu(l)); return null; } public static IterableIterator scanLog_unstructure_iterator(String progID, String fileName) { return scanLog_unstructure_iterator(getProgramFile(progID, fileName)); } public static IterableIterator scanLog_unstructure_iterator(String fileName) { return scanLog_unstructure_iterator(getProgramFile(fileName)); } public static IterableIterator scanLog_unstructure_iterator(File file) { final Iterator it = scanLog_iterator(file); return iteratorFromFunction(new F0() { public Object get() { try { while (it.hasNext()) try { return unstructure(it.next()); } catch (Throwable __e) { _handleException(__e); } return null; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "while (it.hasNext()) pcall {\r\n ret unstructure(it.next());\r\n }\r\n n..."; } }); } public static Web web_unstructure(String s) { return (Web) unstructure(s); } public static Web web_setSourceIfEmpty(Web web, String src) { if (web == null) return null; if (empty(web.source)) web.source = src; return web; } public static JLabel jlabel(final String text) { return swingConstruct(BetterLabel.class, text); } public static JLabel jlabel() { return jlabel(" "); } public static Object swing(Object f) { return swingAndWait(f); } public static A swing(F0 f) { return (A) swingAndWait(f); } public static JTextArea jtextarea() { return jTextArea(); } public static JTextArea jtextarea(String text) { return jTextArea(text); } public static JTextField onEnter(final JTextField tf, final Object action) { if (action == null || tf == null) return tf; tf.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent _evt) { try { tf.selectAll(); callF(action); } catch (Throwable __e) { messageBox(__e); } } }); return tf; } public static JButton onEnter(JButton btn, final Object action) { if (action == null || btn == null) return btn; btn.addActionListener(actionListener(action)); return btn; } public static JList onEnter(JList list, Object action) { list.addKeyListener(enterKeyListener(action)); return list; } public static JComboBox onEnter(final JComboBox cb, final Object action) { JTextField text = (JTextField) cb.getEditor().getEditorComponent(); onEnter(text, action); return cb; } public static JTable onEnter(final JTable table, final Object action) { table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter"); table.getActionMap().put("Enter", new AbstractAction() { public void actionPerformed(ActionEvent e) { callF(action, table.getSelectedRow()); } }); return table; } public static A heldInstance(Class c) { List l = holdInstance_l.get(); for (int i = l(l) - 1; i >= 0; i--) { Object o = l.get(i); if (isInstanceOf(o, c)) return (A) o; } throw fail("No instance of " + className(c) + " held"); } public static ActionListener actionListener(final Object runnable) { return actionListener(runnable, null); } public static ActionListener actionListener(final Object runnable, final Object instanceToHold) { if (runnable instanceof ActionListener) return (ActionListener) runnable; return new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent _evt) { try { AutoCloseable __3849 = holdInstance(instanceToHold); try { callF(runnable); } finally { _close(__3849); } } catch (Throwable __e) { messageBox(__e); } } }; } public static String uniqueFileNameUsingMD5_80(String fullName) { return md5(fullName) + " " + takeFirst(80 - 33, fileNameEncode(fullName)); } public static int findCodeTokens(List tok, String... tokens) { return findCodeTokens(tok, 1, false, tokens); } public static int findCodeTokens(List tok, boolean ignoreCase, String... tokens) { return findCodeTokens(tok, 1, ignoreCase, tokens); } public static int findCodeTokens(List tok, int startIdx, boolean ignoreCase, String... tokens) { return findCodeTokens(tok, startIdx, ignoreCase, tokens, null); } public static List findCodeTokens_specials = litlist("*", "", "", "", "\\*"); public static boolean findCodeTokens_debug; public static int findCodeTokens_indexed, findCodeTokens_unindexed; public static int findCodeTokens_bails, findCodeTokens_nonbails; public static int findCodeTokens(List tok, int startIdx, boolean ignoreCase, String[] tokens, Object condition) { if (findCodeTokens_debug) { if (eq(getClassName(tok), "main$IndexedList2")) findCodeTokens_indexed++; else findCodeTokens_unindexed++; } if (!findCodeTokens_specials.contains(tokens[0]) && !tok.contains(tokens[0])) { ++findCodeTokens_bails; return -1; } ++findCodeTokens_nonbails; outer: for (int i = startIdx | 1; i + tokens.length * 2 - 2 < tok.size(); i += 2) { for (int j = 0; j < tokens.length; j++) { String p = tokens[j], t = tok.get(i + j * 2); boolean match; if (eq(p, "*")) match = true; else if (eq(p, "")) match = isQuoted(t); else if (eq(p, "")) match = isIdentifier(t); else if (eq(p, "")) match = isInteger(t); else if (eq(p, "\\*")) match = eq("*", t); else match = ignoreCase ? eqic(p, t) : eq(p, t); if (!match) continue outer; } if (condition == null || checkTokCondition(condition, tok, i - 1)) return i; } return -1; } public static List replaceSublist(List l, List x, List y) { if (x == null) return l; int i = 0; while (true) { i = indexOfSubList(l, x, i); if (i < 0) break; for (int j = 0; j < l(x); j++) l.remove(i); l.addAll(i, y); i += l(y); } return l; } public static List replaceSublist(List l, int fromIndex, int toIndex, List y) { while (toIndex > fromIndex) l.remove(--toIndex); l.addAll(fromIndex, y); return l; } public static Map newFindBot2_cache = synchroHashMap(); public static boolean newFindBot2_verbose; public static DialogIO newFindBot2(String name) { Integer port = newFindBot2_cache.get(name); if (port != null) { if (newFindBot2_verbose) print("newFindBot2: testing " + name + " => " + port); DialogIO io = talkTo(port); String q = format("has bot *", name); String s = io.ask(q); if (match("yes", s)) { io = talkToSubBot(name, io); call(io, "pushback", "?"); return io; } newFindBot2_cache.remove(name); if (newFindBot2_verbose) print("newFindBot2: dropping " + name + " => " + port); } DialogIO io = findBot(name); if (io != null) { newFindBot2_cache.put(name, io.getPort()); if (newFindBot2_verbose) print("newFindBot2: remembering " + name + " => " + port); } return io; } public static int year() { return Calendar.getInstance().get(Calendar.YEAR); } public static String formatInt(int i, int digits) { return padLeft(str(i), '0', digits); } public static String formatInt(long l, int digits) { return padLeft(str(l), '0', digits); } public static int month() { return Calendar.getInstance().get(Calendar.MONTH) + 1; } public static int dayOfMonth() { return days(); } public static void copyStreamAndCloseAll(InputStream in, OutputStream out) { try { try { copyStreamAndCloseInput(in, out); } finally { out.close(); } } catch (Exception __e) { throw rethrow(__e); } } public static Set listFields(Object c) { TreeSet fields = new TreeSet(); for (Field f : _getClass(c).getDeclaredFields()) fields.add(f.getName()); return fields; } public static Object deref(Object o) { if (o instanceof Derefable) o = ((Derefable) o).get(); return o; } public static Map vm_generalMap_map; public static Map vm_generalMap() { if (vm_generalMap_map == null) vm_generalMap_map = (Map) get(javax(), "generalMap"); return vm_generalMap_map; } public static B mapPutOrRemove(Map map, A key, B value) { if (map != null && key != null) if (value != null) return map.put(key, value); else return map.remove(key); return null; } public static BufferedReader utf8BufferedReader(InputStream in) { return utf8bufferedReader(in); } public static BufferedReader utf8BufferedReader(File f) { return utf8bufferedReader(f); } public static boolean sshLogin_keepOpen; public static String sshLogin_openUser; public static String sshLogin_openHost; public static SSHClient sshLogin_client; public static Lock sshLogin_lock = lock(); public static SSHClient sshLogin(String user, String host) { try { { Lock __3886 = sshLogin_lock; lock(__3886); try { if (sshLogin_client != null && eq(sshLogin_openUser, user) && eq(sshLogin_openHost, host)) return sshLogin_client; } finally { unlock(__3886); } } String pass = findSSHPassword(user, host); final SSHClient client = new SSHClient(); client.addHostKeyVerifier(new PromiscuousVerifier()); client.connect(host); try { client.authPassword(user, pass); } catch (Throwable e) { client.disconnect(); rethrow(e); } Lock __3887 = sshLogin_lock; lock(__3887); try { if (sshLogin_keepOpen && sshLogin_client == null) { sshLogin_openUser = user; sshLogin_openHost = host; sshLogin_client = client; } return client; } finally { unlock(__3887); } } catch (Exception __e) { throw rethrow(__e); } } public static void cleanMeUp_sshLogin() { Lock __3889 = sshLogin_lock; lock(__3889); try { if (sshLogin_client != null) { try { sshLogin_client.disconnect(); } catch (Throwable __e) { _handleException(__e); } sshLogin_client = null; } } finally { unlock(__3889); } } public static String dropAfterLastSlash(String s) { if (s == null) return null; int i = s.lastIndexOf('/'); return i < 0 ? s : rtrim(substring(s, 0, i)); } public static void sshDisconnect(SSHClient client) { try { if (client == null) return; { Lock __3885 = sshLogin_lock; lock(__3885); try { if (client == sshLogin_client) return; } finally { unlock(__3885); } } client.disconnect(); } catch (Exception __e) { throw rethrow(__e); } } public static int smartIndexOfAny(List l, int i, A... x) { while (i < l(l)) if (eqOneOf(l.get(i), x)) return i; else ++i; return l(l); } public static BufferedReader bufferedReader(Reader r) { return r instanceof BufferedReader ? (BufferedReader) r : new BufferedReader(r); } public static String readLineFromReaderWithClose(BufferedReader r) { try { String s = r.readLine(); if (s == null) r.close(); return s; } catch (Exception __e) { throw rethrow(__e); } } public static File tempFileFor(File f) { return new File(f.getPath() + "_temp"); } public static boolean ai_webPostingDisabled() { return isTrue(postSoftwareMadeWeb_disabled); } public static TreeMap asCIMap(Map map) { return asCaseInsensitiveMap(map); } public static List ai_isAWhat(String s) { return ai_texts_multi(ll(quoteUnlessQuoted(s), unquote(s)), ll("is a", "is a subclass of"), "$X"); } public static List ai_sortCategoriesBySize(Collection l) { return sorted(l, ai_smallerCategoryComparator()); } public static TimeZone getTimeZone(String name) { return TimeZone.getTimeZone(name); } public static String standardTimeZone_name = "Europe/Berlin"; public static String standardTimeZone() { return standardTimeZone_name; } public static boolean web_matchNodeLists_debug; public static int web_matchNodeLists(List l1, List l2) { if (l(l1) != l(l2)) return -1000; int score = 0; if (empty(l1)) return score; HashMap map = twoListsToHashMap(l1, l2); Web web = first(l1).web, web2 = first(l2).web; for (int i = 0; i < l(l1); i++) { WebNode a = l1.get(i), b = l2.get(i); if (web_isAnythingVariable(a)) continue; if (!containsAllIC_lisp(web_labels(b), web_labels(a))) --score; } for (WebRelation rel : web_relationObjects(web)) { WebNode aa = map.get(rel.a), bb = map.get(rel.b); if (aa == null || bb == null) continue; WebRelation rel2 = web2.getRelationOpt(aa, bb); if (web_matchNodeLists_debug) print("a=" + rel.a + ", b=" + rel.b + ", aa=" + aa + ", bb=" + bb + ", rel2 labels=" + (rel2 == null ? "-" : str(web_labels(rel2)) + ", labels=" + rel.labels)); if (rel2 == null || !containsAllIC_lisp(web_labels(rel2), web_labels(rel))) --score; } return score; } public static Cache> knownNouns_cache = new Cache("knownNouns_load"); public static Set knownNouns() { return knownNouns_cache.get(); } public static Set knownNouns_load() { return new TreeSet(tlft(loadSnippet("#1011082"))); } public static Cache> knownPluralNouns_cache = new Cache("knownPluralNouns_load"); public static Set knownPluralNouns() { return knownPluralNouns_cache.get(); } public static Set knownPluralNouns_load() { return new TreeSet(map("plural", knownNouns())); } public static Iterable talkToAllVMs() { return talkToAll("This is a JavaX VM."); } public static String dropUntilColon(String s) { if (s == null) return ""; int i = s.indexOf(':'); return trim(i >= 0 ? substring(s, i + 1) : s); } public static String getSnippetTitle(String id) { try { if (id == null) return null; if (!isSnippetID(id)) return "?"; long parsedID = parseSnippetID(id); String url; if (isImageServerSnippet(parsedID)) url = "http://ai1.space/images/raw/title/" + parsedID + muricaCredentialsQuery(); else if (isGeneralFileServerSnippet(parsedID)) url = "http://butter.botcompany.de:8080/files/name/" + parsedID; else url = tb_mainServer() + "/tb-int/getfield.php?id=" + parsedID + "&field=title" + standardCredentials(); return trim(loadPageSilently(url)); } catch (Exception __e) { throw rethrow(__e); } } public static String getSnippetTitle(long id) { return getSnippetTitle(fsI(id)); } public static void putIfNotThere(Map map, A key, B value) { if (map != null && key != null && value != null && !map.containsKey(key)) map.put(key, value); } public static boolean has_verified_multi(List a, List b, List c) { for (String _a : a) for (String _b : b) for (String _c : c) if (has_verified(_a, _b, _c)) return true; return false; } public static boolean has_verified_multi(String a, List b, String c) { return has_verified_multi(ll(a), b, ll(c)); } public static boolean has_verified_multi(List a, List b, String c) { return has_verified_multi(a, b, ll(c)); } public static boolean has_verified_multi(List a, String b, String c) { return has_verified_multi(a, ll(b), ll(c)); } public static boolean has_verified_multi(List a, String b, List c) { return has_verified_multi(a, ll(b), c); } public static boolean isString(Object o) { return o instanceof String; } public static F1 mainFunctionToF1(final String fname) { return new F1() { public Object get(Object a) { return callMC(fname, a); } }; } public static List toLinesFullTrim_java(String text) { return toLinesFullTrim(joinLines(map("javaDropComments", toLinesFullTrim(text)))); } public static List pairList_forwardLookup(Collection> l, A a) { List out = new ArrayList(); for (Pair p : unnull(l)) if (eq(p.a, a)) out.add(p.b); return out; } public static List web_allNodesNamed(Web web, String name) { return web.index.get(web.parseLabel(name)); } public static List web_allNodesNamed(Web web, Lisp name) { return web.index.get(name); } public static List web_allNodesNamed(Web web, Collection names) { LinkedHashSet l = new LinkedHashSet(); for (String name : names) l.addAll(web.index.get(web.parseLabel(name))); return asList(l); } public static Web web_similarEmptyWeb(Web web) { Web w = new Web(); w.useCLParse = web.useCLParse; w.labelsToUpper = web.labelsToUpper; return w; } public static HashMap web_addWebToWeb(Web web, Web w) { HashMap map = new HashMap(); for (WebNode node : web.nodes) if (!(node instanceof WebRelation)) map.put(node, cloneNodeToWeb(node, w)); for (Pair p : web_relations(web)) w.getRelation(assertNotNull(map.get(p.a)), assertNotNull(map.get(p.b))).addLabels(web.getRelation(p).labels()); return map; } public static String renderStructs(Object... params) { List l = new ArrayList(); for (int i = 0; i + 1 < l(params); i += 2) l.add(params[i] + "=" + sfu(params[i + 1])); return joinWithSpace(l); } public static boolean strictSetIC(IVar v, String value) { String old = v.get(); if (old == null) { v.set(value); return true; } return eqic(old, value); } public static List tok_combineRoundOrCurlyBrackets(List tok) { List l = new ArrayList(); for (int i = 0; i < l(tok); i++) { String t = tok.get(i); if (odd(i) && eqOneOf(t, "{", "(")) { int j = findEndOfBracketPart(tok, i); l.add(joinSubList(tok, i + 1, j - 1)); i = j - 1; } else l.add(t); } return l; } public static String jreplace(String s, String in, String out) { return jreplace(s, in, out, null); } public static String jreplace(String s, String in, String out, Object condition) { List tok = javaTok(s); return jreplace(tok, in, out, condition) ? join(tok) : s; } public static boolean jreplace(List tok, String in, String out) { return jreplace(tok, in, out, false, true, null); } public static boolean jreplace(List tok, String in, String out, Object condition) { return jreplace(tok, in, out, false, true, condition); } public static boolean jreplace(List tok, String in, String out, boolean ignoreCase, boolean reTok, Object condition) { List tokin = javaTok(in); jfind_preprocess(tokin); String[] toks = toStringArray(codeTokensOnly(tokin)); boolean anyChange = false; for (int n = 0; n < 10000; n++) { int i = findCodeTokens(tok, 1, ignoreCase, toks, condition); if (i < 0) return anyChange; List subList = tok.subList(i - 1, i + l(tokin) - 1); String expansion = jreplaceExpandRefs(out, subList); int end = i + l(tokin) - 2; clearAllTokens(tok, i, end); tok.set(i, expansion); if (reTok) reTok(tok, i, end); anyChange = true; } throw fail("woot? 10000! " + quote(in) + " => " + quote(out)); } public static boolean jreplace_debug; public static String escapeUpperCaseWithCharacter(String s, char specialChar) { StringBuilder buf = new StringBuilder(); for (int i = 0; i < l(s); i++) { char c = s.charAt(i); if (isUpperCase(c)) buf.append(specialChar); buf.append(c); } return str(buf); } public static File printFileInfo(File f) { print(renderFileInfo(f)); return f; } public static void clearTokens(List tok) { clearAllTokens(tok); } public static void clearTokens(List tok, int i, int j) { clearAllTokens(tok, i, j); } public static volatile boolean muricaPassword_pretendNotAuthed; public static String muricaPassword() { if (muricaPassword_pretendNotAuthed) return null; return trim(loadTextFile(muricaPasswordFile())); } public static void copyStreamWithPrints(InputStream in, OutputStream out, String pat) { try { byte[] buf = new byte[65536]; int total = 0; while (true) { int n = in.read(buf); if (n <= 0) return; out.write(buf, 0, n); if ((total + n) / 100000 > total / 100000) print(pat.replace("{*}", str(roundDownTo(total, 100000)))); total += n; } } catch (Exception __e) { throw rethrow(__e); } } public static Set cloneSet(Set set) { synchronized (set) { return set instanceof TreeSet ? new TreeSet(set) : new HashSet(set); } } public static Set joinSets(Set... l) { TreeSet set = new TreeSet(); for (Set o : l) if (o != null) set.addAll(o); return set; } public static IterableIterator enumerationToIterator(final Enumeration e) { return e == null ? null : new IterableIterator() { public boolean hasNext() { return e.hasMoreElements(); } public A next() { return e.nextElement(); } }; } public static boolean possibleMD5(String s) { return isMD5(s); } public static Comparator makeReversedComparator(final Object f) { return new Comparator() { public int compare(Object a, Object b) { return (int) callF(f, b, a); } }; } public static Lisp lisp(String head, Object... args) { Lisp l = new Lisp(head); for (Object o : args) l.add(o); return l; } public static Lisp lisp(String head, Collection args) { return new Lisp(head, args); } public static Method findMethodNamed(Object obj, String method) { if (obj == null) return null; if (obj instanceof Class) return findMethodNamed((Class) obj, method); return findMethodNamed(obj.getClass(), method); } public static Method findMethodNamed(Class c, String method) { while (c != null) { for (Method m : c.getDeclaredMethods()) if (m.getName().equals(method)) { m.setAccessible(true); return m; } c = c.getSuperclass(); } return null; } public static void ai_invalidateWebs(Collection webs) { for (Web web : unnull(webs)) ai_invalidateWeb(web); } public static List ai_cache_findTriple(String a, String b, String c) { List webs = ai_fewestIndexedWebs(a, b, c); return webs_search_noVar_all(webFromTriple(a, b, c), webs); } public static List ai_cache_findTriple(T3 t) { return ai_cache_findTriple(t.a, t.b, t.c); } public static boolean stdEq2(Object a, Object b) { if (a == null) return b == null; if (b == null) return false; if (a.getClass() != b.getClass()) return false; for (String field : allFields(a)) if (neq(getOpt(a, field), getOpt(b, field))) return false; return true; } public static int stdHash2(Object a) { if (a == null) return 0; return stdHash(a, toStringArray(allFields(a))); } public static String dbProgramID() { return getDBProgramID(); } public static boolean web_nodesTooLong(Web web) { if (web == null) return false; int max = ai_maxNodeTextLength(); for (WebNode n : web_nodesAndRelations(web)) for (String s : web_texts(n)) if (l(s) > max) return true; return false; } public static boolean web_tripelizable(Web web) { return ai_webToTriple(web) != null; } public static List concatWebLists(Collection... lists) { return uniqueByField_favorLast(concatLists(lists), "globalID"); } public static boolean allWebs_transientOnly; public static List allWebs_fullList() { if (allWebs_transientOnly) return transientWebs(); return concatLists(transientWebs(), downloadedWebs(), localSoftwareMadeWebs(), localUserWebEdits(), localWebs()); } public static boolean hasMultiCore() { return isMultiCore(); } public static MultiMap ai_indexWebs_parallel2(Collection webs) { try { print("Making index in parallel."); long _startTime_3999 = sysNow(); List> indexes = parallelMap(chunksOf1000(webs), "ai_indexWebs_serial"); done2_always("Parallel indexing", _startTime_3999); long _startTime_4000 = sysNow(); MultiMap index = caseInsensitiveSyncListMultiMap(); for (MultiMap i : indexes) index.putAll(i); done2_always("Tying up", _startTime_4000); return index; } catch (Exception __e) { throw rethrow(__e); } } public static NavigableMap synchroNavigableMap(NavigableMap map) { return (NavigableMap) call(Collections.class, "synchronizedNavigableMap", map); } public static boolean fileExists(String path) { return path != null && new File(path).exists(); } public static boolean fileExists(File f) { return f != null && f.exists(); } public static boolean isLoopbackIP(String ip) { return eq(ip, "127.0.0.1"); } public static int myVMPort() { List records = (List) (get(getJavaX(), "record_list")); Object android = records.get(records.size() - 1); return (Integer) get(android, "port"); } public static String thisVMGreeting() { List record_list = (List) (get(getJavaX(), "record_list")); Object android = first(record_list); return getString(android, "greeting"); } public static String sendToThisVM_newThread(String s, Object... args) { final String _s = format(s, args); try { return (String) evalInNewThread(new F0() { public Object get() { try { return callStaticAnswerMethod(getJavaX(), _s); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callStaticAnswerMethod(getJavaX(), _s)"; } }); } catch (Throwable e) { e = getInnerException(e); printStackTrace(e); return str(e); } } public static ThreadLocal> holdInstance_l = new ThreadLocal(); public static AutoCloseable holdInstance(Object o) { if (o == null) return null; listThreadLocalAdd(holdInstance_l, o); return new AutoCloseable() { public void close() { listThreadLocalPopLast(holdInstance_l); } }; } public static String plural(String s) { return getPlural(s); } public static String javaDropComments(String s) { return javaDropAllComments(s); } public static Cache> knownPrepositions_cache = new Cache("knownPrepositions_load"); public static Set knownPrepositions() { return knownPrepositions_cache.get(); } public static Set knownPrepositions_load() { return toCISet(tlft(loadSnippet("#1011199"))); } public static TripleWeb newTripleWebWithSource(String source) { return empty(source) ? new TripleWeb() : new TripleWebWithSource(source); } public static Map weakHashMap() { return newWeakHashMap(); } public static List keysList(Map map) { return cloneListSynchronizingOn(keys(map), map); } public static String replaceSuffixIC(String a, String b, String s) { if (endsWithIC(s, a)) return dropLast(s, l(a)) + b; return s; } public static A nuInstance(Class c) { return nuEmptyObject(c); } public static int indexOfNotPredC(List l, Object pred) { for (int i = 1; i < l(l); i += 2) if (!checkCondition(pred, l.get(i))) return i; return -1; } public static boolean checkCondition(Object condition, Object... args) { return isTrue(callF(condition, args)); } public static int parseIntOrMinus1(String s) { return empty(s) ? -1 : Integer.parseInt(s); } public static String firstIntegerString(Iterable c) { Iterator it = c.iterator(); while (it.hasNext()) { String s = it.next(); if (isInteger(s)) return s; } return null; } public static List ai_texts_bidirectionalOperands(String s, String rel) { return concatLists(ai_texts(s, rel, "$X"), ai_texts("$X", rel, s)); } public static int web_numberOfLabels(WebNode n) { return n == null ? 0 : l(n.labels); } public static volatile long gc_memoryUsedAfterGC; public static void gc() { callOpt(javax(), "cleanWeakMaps"); System.gc(); gc_memoryUsedAfterGC = usedMemory(); } public static boolean showAnimationInTopRightCorner_alwaysOnTop = true; public static JWindow showAnimationInTopRightCorner(String imageID, String text) { if (isHeadless()) return null; return showAnimationInTopRightCorner(imageIcon(imageID), text); } public static JWindow showAnimationInTopRightCorner(final BufferedImage image, final String text) { if (isHeadless()) return null; return showAnimationInTopRightCorner(imageIcon(image), text); } public static JWindow showAnimationInTopRightCorner(final ImageIcon imageIcon, final String text) { if (isHeadless()) return null; return (JWindow) swingAndWait(new F0() { public Object get() { try { JLabel label = new JLabel(imageIcon); if (nempty(text)) { label.setText(text); label.setVerticalTextPosition(SwingConstants.BOTTOM); label.setHorizontalTextPosition(SwingConstants.CENTER); } final JWindow window = showInTopRightCorner(label); onClick(label, new Runnable() { public void run() { try { window.dispose(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "window.dispose()"; } }); if (showAnimationInTopRightCorner_alwaysOnTop) window.setAlwaysOnTop(true); return window; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JLabel label = new JLabel(imageIcon);\r\n if (nempty(text)) {\r\n label.s..."; } }); } public static JWindow showAnimationInTopRightCorner(final String imageID) { return showAnimationInTopRightCorner(imageID, ""); } public static JWindow showAnimationInTopRightCorner(String imageID, double seconds) { return showAnimationInTopRightCorner(imageID, "", seconds); } public static JWindow showAnimationInTopRightCorner(String imageID, String text, double seconds) { if (isHeadless()) return null; return disposeWindowAfter(iround(seconds * 1000), showAnimationInTopRightCorner(imageID, text)); } public static JWindow showAnimationInTopRightCorner(BufferedImage img, String text, double seconds) { return disposeWindowAfter(iround(seconds * 1000), showAnimationInTopRightCorner(img, text)); } public static List myLibraryFiles() { return (List) get(myClassLoader(), "files"); } public static Object[] asObjectArray(List l) { return toObjectArray(l); } public static String ok2(String s) { return "ok " + s; } public static int charDiff(char a, char b) { return (int) a - (int) b; } public static int charDiff(String a, char b) { return charDiff(stringToChar(a), b); } public static int lastIndexOf(String a, String b) { return a == null || b == null ? -1 : a.lastIndexOf(b); } public static int lastIndexOf(String a, char b) { return a == null ? -1 : a.lastIndexOf(b); } public static int identityHashCode(Object o) { return System.identityHashCode(o); } public static void consoleSetInput(final String text) { if (headless()) return; setTextAndSelectAll(consoleInputField(), text); focusConsole(); } public static boolean hasMultipleCores() { return isMultiCore(); } public static List webs_readTripleFileAsTriples_parallel2(File f) { try { if (!f.exists()) return ll(); long _startTime_4063 = sysNow(); Iterator it = linesFromFile(f); final List names = new ArrayList(); while (it.hasNext()) { String s = trim(it.next()); if (empty(s)) break; names.add(symbol(unquote(s))); } done2_always("Getting names", _startTime_4063); if (webs_readTripleFileAsTriples_debug) print("Have " + l(names) + " names"); final List, Integer>> triples = new ArrayList(); int poolSize = numberOfCores(), queueSize = 10000; int blockSize = 1000; NotifyingBlockingThreadPoolExecutor e = new NotifyingBlockingThreadPoolExecutor(poolSize, queueSize, 15, TimeUnit.SECONDS); print("Using " + poolSize + " cores, block size: " + blockSize + "."); try { int i = 0; List block = emptyListWithCapacity(blockSize); while (licensed()) { ++i; final Integer _i = i; boolean hasNext = it.hasNext(); if ((!hasNext || l(block) >= blockSize) && nempty(block)) { final List _block = cloneList(block); e.execute(new Runnable() { public void run() { try { try { List l = emptyListWithCapacity(_block); for (String s : _block) { TripleWeb w = webs_readTripleFileAsTriples_line(s, names); if (w != null) l.add(w); } synchronized (triples) { triples.add(pair(l, _i)); } } catch (Throwable __e) { _handleException(__e); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pcall {\r\n List l = emptyListWithCapacity(_block);\r\n ..."; } }); block.clear(); } if (hasNext) block.add(it.next()); else break; } e.shutdown(); e.awaitTermination(1, TimeUnit.DAYS); } finally { e.shutdown(); } sortBySecondOfPairs_inPlace(triples); int n = 0; for (Pair, Integer> p : triples) n += l(p.a); List l = emptyListWithCapacity(n); for (Pair, Integer> p : triples) l.addAll(p.a); return l; } catch (Exception __e) { throw rethrow(__e); } } public static TripleWeb newTripleWebWithSourceAndResult(String source, Object result) { if (result == null) return newTripleWebWithSource(source); return new TripleWebWithSourceAndResult(source, result); } public static IterableIterator scanLog_iterator(String progID, String fileName) { return scanLog_iterator(getProgramFile(progID, fileName)); } public static IterableIterator scanLog_iterator(String fileName) { return scanLog_iterator(getProgramFile(fileName)); } public static IterableIterator scanLog_iterator(File file) { final Iterator it = chainIterators(map("linesFromFile", concatLists(earlierPartsOfLogFile(file), ll(file)))); return iteratorFromFunction(new F0() { public String get() { try { while (it.hasNext()) { String s = it.next(); if (isProperlyQuoted(s)) return unquote(s); } return null; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "while (it.hasNext()) {\r\n S s = it.next();\r\n if (isProperlyQuoted(s)..."; } }); } public static A swingConstruct(final Class c, final Object... args) { return swing(new F0() { public A get() { try { return nuObject(c, args); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "nuObject(c, args)"; } }); } public static JTextArea jTextArea() { return jTextArea(""); } public static JTextArea jTextArea(final String text) { return swing(new F0() { public JTextArea get() { try { return new JTextArea(text); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "new JTextArea(text)"; } }); } public static void messageBox(final String msg) { if (headless()) print(msg); else { swing(new Runnable() { public void run() { try { JOptionPane.showMessageDialog(null, msg, "JavaX", JOptionPane.INFORMATION_MESSAGE); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JOptionPane.showMessageDialog(null, msg, \"JavaX\", JOptionPane.INFORMATION_MES..."; } }); } } public static void messageBox(Throwable e) { printStackTrace(e); messageBox(hideCredentials(innerException(e))); } public static KeyListener enterKeyListener(final Object action) { return new KeyAdapter() { public void keyReleased(KeyEvent ke) { if (ke.getKeyCode() == KeyEvent.VK_ENTER) pcallF(action); } }; } public static boolean isInstanceOf(Object o, Class type) { return type.isInstance(o); } public static String fileNameEncode_safeChars = " ()[]#,!"; public static String fileNameEncode(String s) { StringBuilder buf = new StringBuilder(); int n = l(s); for (int i = 0; i < n; i++) { char c = s.charAt(i); if (contains(fileNameEncode_safeChars, c)) buf.append(c); else buf.append(urlencode(str(c))); } return str(buf); } public static boolean checkTokCondition(Object condition, List tok, int i) { if (condition instanceof TokCondition) return ((TokCondition) condition).get(tok, i); return checkCondition(condition, tok, i); } public static String padLeft(String s, char c, int n) { return rep(c, n - l(s)) + s; } public static String padLeft(String s, int n) { return padLeft(s, ' ', n); } public static int days() { return Calendar.getInstance().get(Calendar.DAY_OF_MONTH); } public static void copyStreamAndCloseInput(InputStream in, OutputStream out) { try { try { copyStream(in, out); } finally { in.close(); } } catch (Exception __e) { throw rethrow(__e); } } public static String findSSHPassword(String user, String server) { return trim(loadTextFile(orFail("SSH password for " + user + "@" + server + " not found on this machine", findSSHPasswordFile(user, server)))); } public static TreeMap asCaseInsensitiveMap(Map map) { if (isCIMap(map)) return (TreeMap) map; TreeMap m = ciMap(); m.putAll(map); return m; } public static TimedCache> ai_smallerCategoryComparator_cache = new TimedCache(10.0); public static Comparator ai_smallerCategoryComparator() { return ai_smallerCategoryComparator_cache.get("ai_smallerCategoryComparator_uncached"); } public static Comparator ai_smallerCategoryComparator_uncached() { final Set> set = new HashSet(pairsToLower(ai_texts_unquoteBoth("is a smaller category than"))); return new Comparator() { public int compare(String a, String b) { a = lower(a); b = lower(b); if (set.contains(pair(a, b))) return -1; if (set.contains(pair(b, a))) return 1; return 0; } }; } public static boolean web_isAnythingVariable(WebNode n) { return endsWithIC(web_text(n), " [Anything]"); } public static boolean containsAllIC_lisp(Collection a, Collection b) { for (Lisp o : b) if (!containsLispIC(a, o)) return false; return true; } public static List web_labels(WebNode n) { return n == null ? null : n.labels(); } public static Iterable talkToAll(String searchPattern) { final List list = quickBotScan(searchPattern); return mapI(list, new F1() { public Object get(ProgramScan.Program p) { try { return talkTo(p.port); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "talkTo(p.port)"; } }); } public static Iterable talkToAll() { return talkToAll(""); } public static String muricaCredentialsQuery() { return htmlQuery(muricaCredentials()); } public static boolean isGeneralFileServerSnippet(long id) { return id >= 1400000 && id < 1500000; } public static String joinLines(List lines) { return fromLines(lines); } public static String joinLines(String glue, String text) { return join(glue, toLines(text)); } public static WebNode cloneNodeToWeb(WebNode node, Web w) { WebNode n = w.newNode(); n.addLabels(web_labels(node)); copyFields(node, n, "x", "y"); return n; } public static int findEndOfBracketPart(List cnc, int i) { int j = i + 2, level = 1; while (j < cnc.size()) { if (eqOneOf(cnc.get(j), "{", "(")) ++level; else if (eqOneOf(cnc.get(j), "}", ")")) --level; if (level == 0) return j + 1; ++j; } return cnc.size(); } public static String jreplaceExpandRefs(String s, List tokref) { List tok = javaTok(s); for (int i = 1; i < l(tok); i += 2) { if (tok.get(i).startsWith("$") && isInteger(tok.get(i).substring(1))) { String x = tokref.get(-1 + parseInt(tok.get(i).substring(1)) * 2); tok.set(i, x); } } return join(tok); } public static List reTok(List tok) { replaceCollection(tok, javaTok(tok)); return tok; } public static List reTok(List tok, int i) { return reTok(tok, i, i + 1); } public static List reTok(List tok, int i, int j) { i = i & ~1; j = j | 1; List t = javaTok(join(subList(tok, i, j))); replaceListPart(tok, i, j, t); return tok; } public static String renderFileInfo(File f) { return f == null ? "-" : f2s(f) + " " + (f.isFile() ? "(file, " + fileSize(f) + " bytes)" : f.isDirectory() ? "(dir)" : "(not found)"); } public static File muricaPasswordFile() { return new File(javaxSecretDir(), "murica/muricaPasswordFile"); } public static int roundDownTo(int x, int n) { return x / n * n; } public static long roundDownTo(long x, long n) { return x / n * n; } public static List webs_search_noVar_all(Web searchWeb, Collection webs) { List l = new ArrayList(); for (Web web : webs) if (web_matchAllPerms(searchWeb, web) != null) l.add(web); return l; } public static int stdHash(Object a, String... fields) { if (a == null) return 0; int hash = getClassName(a).hashCode(); for (String field : fields) hash = hash * 2 + hashCode(getOpt(a, field)); return hash; } public static int ai_maxNodeTextLength = 65536; public static int ai_maxNodeTextLength() { return ai_maxNodeTextLength; } public static List uniqueByField_favorLast(Collection l, String field) { LinkedHashMap map = new LinkedHashMap(); if (l != null) for (A a : l) map.put(getOpt(a, field), a); return asList(values(map)); } public static List downloadedWebs() { return downloadedTypicalWebs(); } public static List localSoftwareMadeWebs() { List webs = new ArrayList(); for (String progID : localSoftwareMadeWebs_programsToRead()) try { webs.addAll(websMadeByProgram(progID)); } catch (Throwable __e) { _handleException(__e); } return webs; } public static List localUserWebEdits() { List webs = new ArrayList(); for (String progID : websPostedIn()) try { if (neq(progID, "#1009961")) for (String s : scanLog(progID, "user-web-edits")) try { webs.add(parseWeb(trim(afterSpace(s)))); } catch (Throwable __e) { _handleException(__e); } } catch (Throwable __e) { _handleException(__e); } return webs; } public static List localWebs() { return allWebsFromCircleEditor(); } public static boolean isMultiCore() { return numberOfCores() > 1; } public static > List parallelMap(A l, final Object f) { try { return parallelMap(iterator((Collection) l), f); } catch (Exception __e) { throw rethrow(__e); } } public static List parallelMap(Iterator it, final Object f) { try { final List> out = new ArrayList(); int poolSize = coresToUse(), queueSize = 10; NotifyingBlockingThreadPoolExecutor e = new NotifyingBlockingThreadPoolExecutor(poolSize, queueSize, 15, TimeUnit.SECONDS); try { int i = 0; for (final Object o : iterable(it)) { ++i; final int _i = i; e.execute(new Runnable() { public void run() { try { try { Object x = callF(f, o); synchronized (out) { out.add(pair(x, _i)); } } catch (Throwable __e) { _handleException(__e); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pcall {\r\n Object x = callF(f, o);\r\n synchronized(out) {\r\n ..."; } }); } e.shutdown(); e.awaitTermination(1, TimeUnit.DAYS); } finally { e.shutdown(); } return firstOfPairs(sortBySecondOfPairs_inPlace(out)); } catch (Exception __e) { throw rethrow(__e); } } public static List> chunksOf1000(Collection c) { List> out = new ArrayList(); Iterator it = iterator(c); List l = new ArrayList(1000); while (it.hasNext()) { l.add(it.next()); if (l(l) >= 1000) { out.add(l); l = new ArrayList(1000); } } if (nempty(l)) out.add(l); return out; } public static Object evalInNewThread(final Object f) { final Flag flag = new Flag(); final Var var = new Var(); final Var exception = new Var(); { Thread _t_0 = new Thread() { public void run() { try { try { var.set(callF(f)); } catch (Throwable e) { exception.set(e); } flag.raise(); } catch (Throwable __e) { _handleException(__e); } } }; startThread(_t_0); } flag.waitUntilUp(); if (exception.has()) throw rethrow(exception.get()); return var.get(); } public static void listThreadLocalAdd(ThreadLocal> tl, A a) { List l = tl.get(); if (l == null) tl.set(l = new ArrayList()); l.add(a); } public static A listThreadLocalPopLast(ThreadLocal> tl) { List l = tl.get(); if (l == null) return null; A a = popLast(l); if (empty(l)) tl.set(null); return a; } public static String javaDropAllComments(String s) { List tok = javaTok(s); for (int i = 0; i < l(tok); i += 2) tok.set(i, tok_javaDropCommentsFromWhitespace(tok.get(i))); return join(tok); } public static TreeSet toCISet(Collection c) { return toCaseInsensitiveSet(c); } public static boolean endsWithIC(String a, String b) { return endsWithIgnoreCase(a, b); } public static long usedMemory() { return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); } public static int imageIcon_cacheSize = 10; public static boolean imageIcon_verbose; public static Map imageIcon_cache; public static Lock imageIcon_lock = lock(); public static ThreadLocal imageIcon_fixGIF = new ThreadLocal(); public static ImageIcon imageIcon(String imageID) { try { Lock __4164 = imageIcon_lock; lock(__4164); try { if (imageIcon_cache == null) imageIcon_cache = new MRUCache(imageIcon_cacheSize); imageID = fsI(imageID); ImageIcon ii = imageIcon_cache.get(imageID); if (ii == null) { if (imageIcon_verbose) print("Loading image icon: " + imageID); File f = loadBinarySnippet(imageID); Boolean b = imageIcon_fixGIF.get(); if (!isFalse(b)) ii = new ImageIcon(loadBufferedImageFixingGIFs(f)); else ii = new ImageIcon(f.toURI().toURL()); } else imageIcon_cache.remove(imageID); imageIcon_cache.put(imageID, ii); return ii; } finally { unlock(__4164); } } catch (Exception __e) { throw rethrow(__e); } } public static ImageIcon imageIcon(Image img) { return new ImageIcon(img); } public static ImageIcon imageIcon(RGBImage img) { return imageIcon(img.getBufferedImage()); } public static JWindow showInTopRightCorner(Component c) { JWindow w = new JWindow(); w.add(c); w.pack(); moveToTopRightCorner(w); w.setVisible(true); return w; } public static char stringToChar(String s) { if (l(s) != 1) throw fail("bad stringToChar: " + s); return firstChar(s); } public static boolean headless() { return isHeadless(); } public static JTextField setTextAndSelectAll(final JTextField tf, final String text) { { swing(new Runnable() { public void run() { try { tf.setText(text); tf.selectAll(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "tf.setText(text);\r\n tf.selectAll();"; } }); } return tf; } public static JTextField consoleInputField() { Object console = get(getJavaX(), "console"); return (JTextField) getOpt(console, "tfInput"); } public static void focusConsole(String s) { setConsoleInput(s); focusConsole(); } public static void focusConsole() { JComponent tf = consoleInputFieldOrComboBox(); if (tf != null) { tf.requestFocus(); } } public static volatile int numberOfCores_value; public static int numberOfCores() { if (numberOfCores_value == 0) numberOfCores_value = Runtime.getRuntime().availableProcessors(); return numberOfCores_value; } public static List> sortBySecondOfPairs_inPlace(List> l) { sort(l, new Comparator>() { public int compare(Pair a, Pair b) { return stdcompare(a.b, b.b); } }); return l; } public static Iterator chainIterators(Collection> l) { final List> _l = new LinkedList(l); return iteratorFromFunction(new F0() { public A get() { try { while (nempty(_l)) { if (first(_l).hasNext()) return first(_l).next(); _l.remove(0); } return null; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "while (nempty(_l)) {\r\n if (first(_l).hasNext())\r\n ret first(_l).n..."; } }); } public static Throwable innerException(Throwable e) { return getInnerException(e); } public static A orFail(String failMsg, A a) { return assertNotNull(failMsg, a); } public static File findSSHPasswordFile(String user, String server) { return findSecretFile(user + "@" + server + ".ssh.pass"); } public static boolean isCIMap(Map m) { return m instanceof TreeMap && ((TreeMap) m).comparator() == caseInsensitiveComparator(); } public static List> pairsToLower(Collection> l) { return map("pairToLower", l); } public static List> ai_texts_unquoteBoth(String b) { return ai_texts_xy_unquoteBoth("$X", b, "$Y"); } public static boolean containsLispIC(Collection l, Lisp s) { for (Lisp x : l) if (lispEqic(x, s)) return true; return false; } public static IterableIterator mapI(final Object f, final Iterator i) { return new IterableIterator() { public boolean hasNext() { return i.hasNext(); } public Object next() { return callF(f, i.next()); } }; } public static IterableIterator mapI(Iterator i, Object f) { return mapI(f, i); } public static IterableIterator mapI(Iterable i, Object f) { return mapI(f, i.iterator()); } public static String htmlQuery(Map params) { return params.isEmpty() ? "" : "?" + makePostData(params); } public static String htmlQuery(Object... data) { return htmlQuery(litmap(data)); } public static Object[] muricaCredentials() { String pass = muricaPassword(); return nempty(pass) ? new Object[] { "_pass", pass } : new Object[0]; } public static void replaceCollection(Collection dest, Collection src) { dest.clear(); dest.addAll(src); } public static void replaceListPart(List l, int i, int j, List l2) { l.subList(i, j).clear(); l.addAll(i, l2); } public static int hashCode(Object a) { return a == null ? 0 : a.hashCode(); } public static List downloadedTypicalWebs() { List webs = new ArrayList(); long _startTime_4210 = sysNow(); for (String struct : downloadedTypicalDiagramStructures()) try { Web web = web_flexUnstructure(struct); webs.add(web_setSource("downloaded typical", web)); } catch (Throwable __e) { print(exceptionToStringShort(__e)); } done2_always("Unpacking webs", _startTime_4210); return webs; } public static List websMadeByProgram(String progID) { return websMadeByProgram(progID, null); } public static List websMadeByProgram(String progID, Map furtherInfoMap) { if (hasMultipleCores()) return websMadeByProgram_parallel(progID, furtherInfoMap); List webs = ai_triplesMadeByProgram(progID); ai_loadWebsFromFile(programFile(progID, "webs-made.txt"), progID, webs, furtherInfoMap); return webs; } public static List websPostedIn() { Matches m = new Matches(); List programsToScan = new ArrayList(); for (String s : listFileNames(getProgramDir(circlesEditorDBID()))) try { if (swic(s, "webs.posted.at.", m)) programsToScan.add(fsI(m.unq(0))); } catch (Throwable __e) { _handleException(__e); } return programsToScan; } public static Web parseWeb(String s) { Object o = unstructure(s); if (o instanceof CirclesAndLines) return calToWeb((CirclesAndLines) o); return (Web) o; } public static String afterSpace(String s) { return dropUntilSpace(s); } public static List allWebsFromCircleEditor() { return map_pcall(new F1() { public Object get(Drawing d) { try { return web_setSource("local", calToWeb(d.cal())); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "web_setSource(\"local\", calToWeb(d.cal()))"; } }, reversed(list(new Concepts(circlesEditorDBID()).load(), Drawing.class))); } public static int coresToUse() { return max(1, numberOfCores() - 1); } public static Iterable iterable(final Iterator i) { return new Iterable() { public Iterator iterator() { return i; } }; } public static List firstOfPairs(Collection> l) { List out = new ArrayList(); for (Pair p : unnull(l)) out.add(firstOfPair(p)); return out; } public static Pair pairToLower(Pair p) { return p == null ? null : pair(lower(p.a), lower(p.b)); } public static String tok_javaDropCommentsFromWhitespace(String s) { int l = l(s), j = 0; StringBuilder buf = new StringBuilder(); while (j < l) { char c = s.charAt(j); char d = j + 1 >= l ? '\0' : s.charAt(j + 1); if (c == '/' && d == '*') { do ++j; while (j < l && !s.substring(j, Math.min(j + 2, l)).equals("*/")); j = Math.min(j + 2, l); } else if (c == '/' && d == '/') { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else { buf.append(c); ++j; } } return str(buf); } public static boolean loadBufferedImageFixingGIFs_debug; public static ThreadLocal> loadBufferedImageFixingGIFs_output = new ThreadLocal(); public static Image loadBufferedImageFixingGIFs(File file) { try { if (!file.exists()) return null; if (!isGIF(file)) return ImageIO.read(file); if (loadBufferedImageFixingGIFs_debug) print("loadBufferedImageFixingGIFs" + ": checking gif"); ImageReader reader = ImageIO.getImageReadersByFormatName("gif").next(); reader.setInput(ImageIO.createImageInputStream(file)); int numImages = reader.getNumImages(true); IIOMetadata imageMetaData = reader.getImageMetadata(0); String metaFormatName = imageMetaData.getNativeMetadataFormatName(); boolean foundBug = false; for (int i = 0; i < numImages && !foundBug; i++) { IIOMetadataNode root = (IIOMetadataNode) reader.getImageMetadata(i).getAsTree(metaFormatName); int nNodes = root.getLength(); for (int j = 0; j < nNodes; j++) { org.w3c.dom.Node node = root.item(j); if (node.getNodeName().equalsIgnoreCase("GraphicControlExtension")) { String delay = ((IIOMetadataNode) node).getAttribute("delayTime"); if (Integer.parseInt(delay) == 0) { foundBug = true; } break; } } } if (loadBufferedImageFixingGIFs_debug) print("loadBufferedImageFixingGIFs" + ": " + f2s(file) + " foundBug=" + foundBug); Image image; if (!foundBug) { image = Toolkit.getDefaultToolkit().createImage(f2s(file)); } else { ByteArrayOutputStream baoStream = new ByteArrayOutputStream(); { ImageOutputStream ios = ImageIO.createImageOutputStream(baoStream); try { ImageWriter writer = ImageIO.getImageWriter(reader); writer.setOutput(ios); writer.prepareWriteSequence(null); for (int i = 0; i < numImages; i++) { BufferedImage frameIn = reader.read(i); IIOMetadataNode root = (IIOMetadataNode) reader.getImageMetadata(i).getAsTree(metaFormatName); int nNodes = root.getLength(); for (int j = 0; j < nNodes; j++) { org.w3c.dom.Node node = root.item(j); if (node.getNodeName().equalsIgnoreCase("GraphicControlExtension")) { String delay = ((IIOMetadataNode) node).getAttribute("delayTime"); if (Integer.parseInt(delay) == 0) { ((IIOMetadataNode) node).setAttribute("delayTime", "10"); } break; } } IIOMetadata metadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(frameIn), null); metadata.setFromTree(metadata.getNativeMetadataFormatName(), root); IIOImage frameOut = new IIOImage(frameIn, null, metadata); writer.writeToSequence(frameOut, writer.getDefaultWriteParam()); } writer.endWriteSequence(); } finally { _close(ios); } } byte[] data = baoStream.toByteArray(); setVar(loadBufferedImageFixingGIFs_output.get(), data); if (loadBufferedImageFixingGIFs_debug) print("Data size: " + l(data)); image = Toolkit.getDefaultToolkit().createImage(data); } return image; } catch (Exception __e) { throw rethrow(__e); } } public static char firstChar(String s) { return s.charAt(0); } public static void setConsoleInput(String text) { consoleSetInput(text); } public static JComponent consoleInputFieldOrComboBox() { Object console = get(getJavaX(), "console"); JComboBox cb = (JComboBox) (getOpt(console, "cbInput")); if (cb != null) return cb; return (JTextField) getOpt(console, "tfInput"); } public static File findSecretFile(String name) { List l = new ArrayList(); for (File dir : allSecretDirs()) { File f = new File(dir, name); if (f.exists()) return f; } return null; } public static boolean lispEqic(Lisp a, Lisp b) { if (a == null) return b == null; if (neqic(a.head, b.head)) return false; int n = l(a.args); if (n != l(b.args)) return false; for (int i = 0; i < n; i++) if (!lispEqic(a.args.get(i), b.args.get(i))) return false; return true; } public static boolean lispEqic(Lisp a, String b) { if (a == null) return b == null; return nempty(a.args) && eqic(a.head, b); } public static List downloadedTypicalDiagramStructures() { Map map = safeUnstructureMap(loadTextFile(getProgramFile("#1010484", "typical.structure"))); if (map == null) return ll(); return downloadedDiagramStructures(map(values(map), new F1() { public Object get(String md5) { try { return getProgramFile("#1010484", md5); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "getProgramFile(#1010484, md5)"; } })); } public static Web web_flexUnstructure(String struct) { if (eq(firstJavaToken(struct), "SoftwareMadeWeb")) { struct = getString(safeUnstructure(struct), "structure"); return (Web) unstructure(struct); } else return calToWeb(cal_unstructure(struct)); } public static Web web_setSource(String source, Web web) { web.source = source; return web; } public static List websMadeByProgram_parallel(String progID) { return websMadeByProgram_parallel(progID, null); } public static List websMadeByProgram_parallel(String progID, final Map furtherInfoMap) { try { IterableIterator l = scanLog_iterator(progID, "webs-made.txt"); final String src = progID; final List> webs = new ArrayList(); int poolSize = numberOfCores(), queueSize = 20; NotifyingBlockingThreadPoolExecutor e = new NotifyingBlockingThreadPoolExecutor(poolSize, queueSize, 15, TimeUnit.SECONDS); try { int i = 0; for (final String s : l) { ++i; final int _i = i; e.execute(new Runnable() { public void run() { try { try { SoftwareMadeWeb o = (SoftwareMadeWeb) (unstructure(s)); Web web = o.web; if (web == null) web = (Web) unstructure(o.structure); if (web != null) synchronized (webs) { webs.add(pair(web_intern(web_setSourceIfEmpty(web, src)), _i)); mapPut(furtherInfoMap, web, o); } } catch (Throwable __e) { _handleException(__e); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pcall {\r\n SoftwareMadeWeb o = (SoftwareMadeWeb) ( unstructure(s));\r\n ..."; } }); } e.shutdown(); e.awaitTermination(1, TimeUnit.DAYS); } finally { e.shutdown(); } long _startTime_4259 = sysNow(); sortBySecondOfPairs_inPlace(webs); done2_always("Sort", _startTime_4259); return concatLists(ai_triplesMadeByProgram(progID), firstOfPairs(webs)); } catch (Exception __e) { throw rethrow(__e); } } public static List ai_triplesMadeByProgram(String progID) { return webs_readTripleFile(getProgramFile(progID, "triples.gz")); } public static List listFileNames(File dir) { return asList(dir.list()); } public static List listFileNames(String dir) { return listFileNames(new File(dir)); } public static Web calToWeb(CirclesAndLines cal) { Web web = new Web(); web.globalID = cal.globalID; web_useCLParse(web, false); web.title = cal.title; web.created = cal.created; HashMap map = new HashMap(); for (Circle c : cal.circles) { WebNode node = web.newNode(c.text); copyFields(c, node, "x", "y"); node.visInfo(c.quickvis); map.put(c, node); } for (Line l : cal.lines) { WebNode rel = web.getRelation(assertNotNull(map.get(l.a)), assertNotNull(map.get(l.b))); rel.addLabel(l.text); } return web; } public static String dropUntilSpace(String s) { if (s == null) return ""; int i = s.indexOf(' ') + 1; while (i < l(s) && s.charAt(i) == ' ') ++i; return s.substring(i); } public static List map_pcall(Iterable l, Object f) { return map_pcall(f, l); } public static List map_pcall(Object f, Iterable l) { List x = new ArrayList(); for (Object o : unnull(l)) try { x.add(callF(f, o)); } catch (Throwable __e) { _handleException(__e); } return x; } public static List reversed(Collection l) { return reversedList(l); } public static List reversed(A[] l) { return reversedList(asList(l)); } public static String reversed(String s) { return reversedString(s); } public static A firstOfPair(Pair p) { return p == null ? null : p.a; } public static byte[] isGIF_magic = bytesFromHex("47494638"); public static boolean isGIF(byte[] data) { return byteArrayStartsWith(data, isGIF_magic); } public static boolean isGIF(File f) { return isGIF(loadBeginningOfBinaryFile(f, l(isGIF_magic))); } public static void setVar(Var v, A value) { if (v != null) v.set(value); } public static List allSecretDirs() { List l = new ArrayList(); for (File dir : listDirs(addAndReturn(l, javaxSecretDir()))) l.add(dir); return l; } public static List downloadedDiagramStructures() { return downloadedDiagramStructures(listFilesOfProgram_notDirs("#1010484")); } public static List downloadedDiagramStructures(List files) { List diagrams = new ArrayList(); for (File f : files) { if (!isPossibleMD5(f.getName()) || !f.isFile()) continue; print(f); if (fileStartsWith(f, toUtf8("CirclesAndLines"))) { print("Diagram!"); diagrams.add(loadTextFile(f)); } else if (isGZ(f)) { String text = loadGZippedTextFile(f); if (isQuoted(firstJavaToken(text))) diagrams.addAll(scanQuotedLogLines(text)); else { Concepts c = new Concepts().loadGrab(text, true); List structures = getFieldOfAllConceptClasses(c, "calStructure"); print("Got " + n(structures, "diagram")); diagrams.addAll(structures); } } else print("Huh?"); } return diagrams; } public static List webs_readTripleFile(File f) { if (!f.exists()) return ll(); Iterator it = linesFromFile(f); List names = new ArrayList(); while (it.hasNext()) { String s = trim(it.next()); if (empty(s)) break; names.add(unquote(s)); } List webs = new ArrayList(); while (it.hasNext()) { String s = it.next(); try { addIfNotNull(webs, webs_readTripleFile_line(s, names)); } catch (Throwable __e) { _handleException(__e); } } return webs; } public static Web webs_readTripleFile_line(String s, List names) { List l = javaTokC(s); if (l(l) == 8) { Web web = webFromTriple(names.get(parseInt(first(l))), names.get(parseInt(second(l))), names.get(parseInt(third(l)))); web.globalID = unquote(l.get(3)); web.title = unquote(l.get(4)); web.source = unquote(l.get(5)); web.unverified = !eq(l.get(6), "v"); web.created = parseLong(l.get(7)); return web_intern(web); } return null; } public static Web web_useCLParse(Web web, boolean b) { web.useCLParse = b; return web; } public static List reversedList(Collection l) { List x = cloneList(l); Collections.reverse(x); return x; } public static String reversedString(String s) { return reverseString(s); } public static byte[] bytesFromHex(String s) { return hexToBytes(s); } public static boolean byteArrayStartsWith(byte[] a, byte[] b) { if (a == null || b == null) return false; if (a.length < b.length) return false; for (int i = 0; i < b.length; i++) if (a[i] != b[i]) return false; return true; } public static byte[] loadBeginningOfBinaryFile(File file, int maxBytes) { return loadBinaryFilePart(file, 0, maxBytes); } public static List listDirs(File dir) { File[] files = dir.listFiles(); List l = new ArrayList(); if (files != null) for (File f : files) if (f.isDirectory()) l.add(f); return l; } public static List listFilesOfProgram_notDirs(String programID) { return asList(listFilesOnly(programDir(programID))); } public static boolean isPossibleMD5(String s) { return isMD5(s); } public static boolean fileStartsWith(File f, byte[] data) { return byteArrayEquals(loadBeginningOfBinaryFile(f, l(data)), data); } public static byte[] isGZ_magic = bytesFromHex("1f8b"); public static boolean isGZ(byte[] data) { return byteArrayStartsWith(data, isGZ_magic); } public static boolean isGZ(File f) { return fileStartsWith(f, isGZ_magic); } public static String loadGZippedTextFile(File file) { return loadGZTextFile(file); } public static List scanQuotedLogLines(String text) { List l = new ArrayList(); for (String s : toLines(text)) if (isProperlyQuoted(s)) l.add(unquote(s)); return l; } public static List getFieldOfAllConceptClasses(Concepts cc, String field) { return notNullOnly(c_collect(values(cc.concepts), field)); } public static A third(List l) { return _get(l, 2); } public static A third(A[] bla) { return bla == null || bla.length <= 2 ? null : bla[2]; } public static C third(T3 t) { return t == null ? null : t.c; } public static String reverseString(String s) { return new StringBuilder(s).reverse().toString(); } public static byte[] loadBinaryFilePart(File file, long start, long end) { try { RandomAccessFile raf = new RandomAccessFile(file, "r"); int n = toInt(min(raf.length(), end - start)); byte[] buffer = new byte[n]; try { raf.seek(start); raf.readFully(buffer, 0, n); return buffer; } finally { raf.close(); } } catch (Exception __e) { throw rethrow(__e); } } public static List listFilesOnly(String dir) { return listFilesOnly(new File(dir)); } public static List listFilesOnly(File... dirs) { return concatMap(rcurry("listFilesWithSuffix", ""), dirs); } public static boolean byteArrayEquals(byte[] a, byte[] b) { if (a == null || b == null) return false; if (a.length != b.length) return false; for (int i = 0; i < b.length; i++) if (a[i] != b[i]) return false; return true; } public static List notNullOnly(List l) { List out = new ArrayList(); for (A a : unnull(l)) if (a != null) out.add(a); return out; } public static List notNullOnly(A... l) { return notNullOnly(asList(l)); } public static List c_collect(Collection l, String field) { List out = new ArrayList(); if (l != null) for (Object o : l) out.add(cget(o, field)); return out; } public static A _get(List l, int idx) { return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null; } public static Object _get(Object o, String field) { return get(o, field); } public static A _get(A[] l, int idx) { return idx >= 0 && idx < l(l) ? l[idx] : null; } public static Object rcurry(final Object f, final Object arg) { int n = numberOfFunctionArguments(f); if (n == 0) throw fail("function takes no arguments"); if (n == 1) return new F0() { public Object get() { return callF(f, arg); } }; if (n == 2) return new F1() { public Object get(Object a) { return callF(f, a, arg); } }; throw todo("currying a function with " + n + "arguments"); } public static int numberOfFunctionArguments(Object f) { if (f instanceof F1) return 1; if (f instanceof String) return numberOfMethodArguments(mc(), (String) f); return numberOfMethodArguments(f, "get"); } public static int numberOfMethodArguments(Object o, String method) { Class c; boolean mustBeStatic = false; if (o instanceof Class) { c = (Class) o; mustBeStatic = true; } else c = o.getClass(); Class _c = c; int n = -1; while (c != null) { for (Method m : c.getDeclaredMethods()) { if (!m.getName().equals(method)) continue; if (mustBeStatic && !methodIsStatic(m)) continue; int nn = l(m.getParameterTypes()); if (n == -1) n = nn; else if (n != nn) throw fail("Variable number of method arguments: " + _c + "." + method); } c = c.getSuperclass(); } if (n == -1) throw fail("Method not found: " + _c + "." + method); return n; } public abstract static class VF1 { public abstract void get(A a); } public static class MatchLearner extends Concept { public static String _fieldOrder = "pattern examples"; public String pattern; public Map examples = mapInConcept(this, litorderedmap()); } public static class CombinedList extends RandomAccessAbstractList { public int size; public List> lists = new ArrayList(); public CombinedList() { } public void addList(List l) { lists.add(l); size += l(l); } public int size() { return size; } public A get(int index) { int idx = 0; for (List l : lists) { int j = idx + l(l); if (index < j) return l.get(index - idx); idx = j; } throw new NoSuchElementException(); } } public static class Var implements IVar { public A v; public Var() { } public Var(A v) { this.v = v; } public synchronized void set(A a) { if (v != a) { v = a; notifyAll(); } } public synchronized A get() { return v; } public synchronized boolean has() { return v != null; } public synchronized void clear() { v = null; } public String toString() { return str(get()); } } public static class GlobalThoughtSpace extends AbstractThoughtSpace { public GlobalID postTriple(T3 t, boolean verified) { Boolean prev = setThreadLocal(ai_postTriple_verified, verified); try { return asGlobalID(post(str(t.a), str(t.b), str(t.c))); } finally { setThreadLocal(ai_postTriple_verified, prev); } } public GlobalID postTriple(T3 t) { return asGlobalID(post(str(t.a), str(t.b), str(t.c))); } public List> get(String s) { return tripleIndex().getTripleRefs(s); } public List> get(String s, int position) { return tripleIndex().getTripleRefs(s, position); } public List getTriples(String s) { return tripleIndex().getTriples(s); } public List getTriples(String s, int position) { return tripleIndex().getTriples(s, position); } public List getOneTwo(String a, String b) { return tripleIndex().getOneTwo(a, b); } public int size() { return tripleIndex().numWebs(); } } public static interface ISpec { public Lock aiLock(); public boolean ai_cache_hasTriple(String a, String b, String c); public String ai_postTriple(String a, String b, String c); public String ai_setMaker(String maker); public List allIndexedFullTerms(); public List ai_texts_verified(String a, String b, String c); public String ai_text(String a, String b, String c); public Web ai_blob(String word, boolean verified); public List ai_index_search_dollarX(String a, String b, String c); public List indexedNodes(String s); public List indexedWebs(String s); public Set ai_invalidatedWebs(); public void ai_invalidatedWebs_cache_put(GlobalID a, GlobalID b); public MultiMap cachedNodeIndex2(); public Web ai_getWeb(String id); public void ai_setSubSpace(AI_SubSpace subSpace); public AI_SubSpace ai_currentSubSpace(); public Set ai_mainIndexKeys(); public TripleIndex tripleIndex(); public void ai_removeWeb(Web web); public List virtualNodeIndices(); public boolean ai_enabled(); public boolean ai_useThoughtSpaces(); public boolean ai_inTripleMode(); public AbstractThoughtSpace currentThoughtSpace(); public A thoughtSpace(A ts); public void ai_markMemoryDirty(); public boolean ai_isMemoryDirty(); } public abstract static class TokCondition { public abstract boolean get(List tok, int i); } public static class NotifyingBlockingThreadPoolExecutor extends ThreadPoolExecutor { public AtomicInteger tasksInProcess = new AtomicInteger(); public Synchronizer synchronizer = new Synchronizer(); public NotifyingBlockingThreadPoolExecutor(int poolSize, int queueSize, long keepAliveTime, TimeUnit keepAliveTimeUnit, long maxBlockingTime, TimeUnit maxBlockingTimeUnit, Callable blockingTimeCallback) { super(poolSize, poolSize, keepAliveTime, keepAliveTimeUnit, new ArrayBlockingQueue(Math.max(poolSize, queueSize)), new BlockThenRunPolicy(maxBlockingTime, maxBlockingTimeUnit, blockingTimeCallback)); super.allowCoreThreadTimeOut(true); } public NotifyingBlockingThreadPoolExecutor(int poolSize, int queueSize, long keepAliveTime, TimeUnit unit) { super(poolSize, poolSize, keepAliveTime, unit, new ArrayBlockingQueue(Math.max(poolSize, queueSize)), new BlockThenRunPolicy()); super.allowCoreThreadTimeOut(true); } @Override public void execute(Runnable task) { tasksInProcess.incrementAndGet(); try { super.execute(task); } catch (RuntimeException e) { tasksInProcess.decrementAndGet(); throw e; } catch (Error e) { tasksInProcess.decrementAndGet(); throw e; } } @Override public void afterExecute(Runnable r, Throwable t) { super.afterExecute(r, t); synchronized (this) { tasksInProcess.decrementAndGet(); if (tasksInProcess.intValue() == 0) { synchronizer.signalAll(); } } } @Override public void setCorePoolSize(int corePoolSize) { super.setCorePoolSize(corePoolSize); super.setMaximumPoolSize(corePoolSize); } @Override public void setMaximumPoolSize(int maximumPoolSize) { throw new UnsupportedOperationException("setMaximumPoolSize is not supported."); } public void setRejectedExecutionHandler(RejectedExecutionHandler handler) { throw new UnsupportedOperationException("setRejectedExecutionHandler is not allowed on this class."); } public void await() throws InterruptedException { synchronizer.await(); } public boolean await(long timeout, TimeUnit timeUnit) throws InterruptedException { return synchronizer.await(timeout, timeUnit); } public class Synchronizer { public final Lock lock = new ReentrantLock(); public final Condition done = lock.newCondition(); public boolean isDone = false; public final void signalAll() { lock.lock(); try { isDone = true; done.signalAll(); } finally { lock.unlock(); } } public void await() throws InterruptedException { lock.lock(); try { while (!isDone) { done.await(); } } finally { isDone = false; lock.unlock(); } } public boolean await(long timeout, TimeUnit timeUnit) throws InterruptedException { boolean await_result = false; lock.lock(); boolean localIsDone; try { await_result = done.await(timeout, timeUnit); } finally { localIsDone = isDone; isDone = false; lock.unlock(); } return await_result && localIsDone; } } public static class BlockThenRunPolicy implements RejectedExecutionHandler { public long maxBlockingTime; public TimeUnit maxBlockingTimeUnit; public Callable blockingTimeCallback; public BlockThenRunPolicy(long maxBlockingTime, TimeUnit maxBlockingTimeUnit, Callable blockingTimeCallback) { this.maxBlockingTime = maxBlockingTime; this.maxBlockingTimeUnit = maxBlockingTimeUnit; this.blockingTimeCallback = blockingTimeCallback; } public BlockThenRunPolicy() { } @Override public void rejectedExecution(Runnable task, ThreadPoolExecutor executor) { BlockingQueue workQueue = executor.getQueue(); boolean taskSent = false; while (!taskSent) { if (executor.isShutdown()) { throw new RejectedExecutionException("ThreadPoolExecutor has shutdown while attempting to offer a new task."); } try { if (blockingTimeCallback != null) { if (workQueue.offer(task, maxBlockingTime, maxBlockingTimeUnit)) { taskSent = true; } else { Boolean result = null; try { result = blockingTimeCallback.call(); } catch (Exception e) { throw new RejectedExecutionException(e); } if (result == false) { throw new RejectedExecutionException("User decided to stop waiting for task insertion"); } else { continue; } } } else { workQueue.put(task); taskSent = true; } } catch (InterruptedException e) { } } } } } public static class GlobalID { public long a; public int b; public GlobalID() { } public GlobalID(String id) { assertGlobalID(id); BigInteger value = bigint(0); for (int i = 0; i < l(id); i++) value = plus(mul(value, 26), charDiff(id.charAt(i), 'a')); a = value.longValue(); value = value.shiftRight(64); b = value.shortValue(); } public String toString() { BigInteger value = bigint(b); value = value.shiftLeft(32); value = plus(value, (a >> 32) & 0xFFFFFFFFL); value = value.shiftLeft(32); value = plus(value, a & 0xFFFFFFFFL); char[] buf = new char[16]; for (int i = 16 - 1; i >= 0; i--) { buf[i] = charPlus('a', mod(value, 26).intValue()); value = div(value, 26); } return str(buf); } public boolean equals(Object o) { if (!(o instanceof GlobalID)) return false; return ((GlobalID) o).a == a && ((GlobalID) o).b == b; } public int hashCode() { return (int) a; } } public static class DependentCache extends Cache { public Object f, lastDep; public DependentCache() { } public DependentCache(Object maker, Object f) { this.f = f; this.maker = maker; } public A get() { if (hasLock(lock)) return value; { Lock __4357 = lock; lock(__4357); try { Object dep; if (f instanceof Cache) dep = ((Cache) f).changeCount; else { dep = callF(f); if (dep instanceof Cache) dep = ((Cache) dep).changeCount; } if (dep != lastDep) { clear(); lastDep = dep; } } finally { unlock(__4357); } } return super.get(); } } public static interface AI_CacheChecker { public boolean hasTriple(String a, String b, String c); } public static class TripleA extends TripleRef { public TripleA() { } public TripleA(T3 triple) { this.triple = triple; } public int position() { return 0; } } public static class TripleB extends TripleRef { public TripleB() { } public TripleB(T3 triple) { this.triple = triple; } public int position() { return 1; } } public static class TripleC extends TripleRef { public TripleC() { } public TripleC(T3 triple) { this.triple = triple; } public int position() { return 2; } } public static class Best { public A best; public double score; public transient Object onChange; public synchronized boolean isNewBest(double score) { return best == null || !isNaN(score) && score > this.score; } public synchronized double bestScore() { return best == null ? Double.NaN : score; } public double score() { return bestScore(); } public double getScore() { return bestScore(); } public boolean put(A a, double score) { boolean change = false; synchronized (this) { if (a != null && isNewBest(score)) { best = a; this.score = score; change = true; } change = true; } if (change) pcallF(onChange); return change; } public synchronized A get() { return best; } public synchronized boolean has() { return best != null; } public synchronized Pair pair() { return main.pair(best, bestScore()); } public synchronized A getIfScoreAbove(double x) { return score() >= x ? best : null; } } public static class FixedRateTimer extends java.util.Timer { public FixedRateTimer() { this(false); } public FixedRateTimer(boolean daemon) { super(daemon); _registerTimer(this); } public List entries = synchroList(); public static class Entry { public TimerTask task; public long firstTime; public long period; public Entry() { } public Entry(TimerTask task, long firstTime, long period) { this.period = period; this.firstTime = firstTime; this.task = task; } public String toString() { return "Entry(" + task + ", " + firstTime + ", " + period + ")"; } } public void scheduleAtFixedRate(TimerTask task, long delay, long period) { entries.add(new Entry(task, now() + delay, period)); super.scheduleAtFixedRate(task, delay, period); } public void cancel() { entries.clear(); super.cancel(); } public int purge() { entries.clear(); return super.purge(); } } public static class TripleWeb extends T3 { public long globalID1; public short globalID2; public byte flags; public long created; public String source() { return null; } public Object result() { return null; } public static final byte UNVERIFIED = 1; public final GlobalID globalID() { return globalIDFromParts(globalID1, globalID2); } public final void globalID(GlobalID id) { globalID1 = globalIDPart1(id); globalID2 = globalIDPart2(id); } public final void globalID(String id) { globalID(asGlobalID(id)); } public final int hashCode() { return (int) globalID1; } public final boolean equals(Object o) { return o instanceof TripleWeb && globalID1 == ((TripleWeb) o).globalID1 && globalID2 == ((TripleWeb) o).globalID2; } public final boolean unverified() { return (flags & UNVERIFIED) != 0; } public final boolean verified() { return !unverified(); } public final void verified(boolean v) { flags = v ? 0 : UNVERIFIED; } public String toString() { return "[" + globalID() + "] " + super.toString(); } public final long created() { return created; } public final void created(long created) { this.created = created; } } public static class Lisp implements Iterable { public String head; public List args; public Lisp() { } public Lisp(String head) { this.head = _compactString(head); } public Lisp(String head, Lisp... args) { this.head = head; argsForEdit().addAll(asList(args)); } public Lisp(String head, Collection args) { this.head = _compactString(head); for (Object arg : args) add(arg); } public List argsForEdit() { return args == null ? (args = new ArrayList()) : args; } public String toString() { if (empty()) return quoteIfNotIdentifierOrInteger(head); List bla = new ArrayList(); for (Lisp a : args) bla.add(a.toString()); String inner = join(", ", bla); if (head.equals("")) return "{" + inner + "}"; else return quoteIfNotIdentifier(head) + "(" + inner + ")"; } public String raw() { if (!isEmpty()) throw fail("not raw: " + this); return head; } public Lisp add(Lisp l) { argsForEdit().add(l); return this; } public Lisp add(String s) { argsForEdit().add(new Lisp(s)); return this; } public Lisp add(Object o) { if (o instanceof Lisp) add((Lisp) o); else if (o instanceof String) add((String) o); else throw fail("Bad argument type: " + structure(o)); return this; } public int size() { return l(args); } public boolean empty() { return main.empty(args); } public boolean isEmpty() { return main.empty(args); } public boolean isLeaf() { return main.empty(args); } public Lisp get(int i) { return main.get(args, i); } public String getString(int i) { Lisp a = get(i); return a == null ? null : a.head; } public String s(int i) { return getString(i); } public String rawOrNull(int i) { Lisp a = get(i); return a != null && a.isLeaf() ? a.head : null; } public String raw(int i) { return assertNotNull(rawOrNull(i)); } public boolean isLeaf(int i) { return rawOrNull(i) != null; } public boolean isA(String head) { return eq(head, this.head); } public boolean is(String head, int size) { return isA(head) && size() == size; } public boolean is(String head) { return isA(head); } public boolean headIs(String head) { return isA(head); } public boolean is(String... heads) { return asList(heads).contains(head); } public boolean isic(String... heads) { return containsIgnoreCase(heads, head); } public Iterator iterator() { return main.iterator(args); } public Lisp subList(int fromIndex, int toIndex) { Lisp l = new Lisp(head); l.argsForEdit().addAll(args.subList(fromIndex, toIndex)); return l; } public boolean equals(Object o) { if (o == null || o.getClass() != Lisp.class) return false; Lisp l = (Lisp) (o); return eq(head, l.head) && (isLeaf() ? l.isLeaf() : l.args != null && eq(args, l.args)); } public int hashCode() { return head.hashCode() + main.hashCode(args); } public Lisp addAll(List args) { for (Object arg : args) add(arg); return this; } public String unquoted() { return unquote(raw()); } public String unq() { return unquoted(); } public String unq(int i) { return get(i).unq(); } public List heads() { return collect(args, "head"); } } public static class TripleWebWithSourceAndResult extends TripleWebWithSource { public Object result; public TripleWebWithSourceAndResult() { } public TripleWebWithSourceAndResult(String source, Object result) { this.result = result; this.source = source; } public Object result() { return result; } } public static class ProgramScan { public static int threads = isWindows() ? 500 : 10; public static int timeout = 5000; public static String ip = "127.0.0.1"; public static int quickScanFrom = 10000, quickScanTo = 10999; public static int maxNumberOfVMs_android = 4; public static int maxNumberOfVMs_nonAndroid = 50; public static int maxNumberOfVMs; public static boolean verbose; public static class Program { public int port; public String helloString; public Program(int port, String helloString) { this.helloString = helloString; this.port = port; } } public static List scan() { try { return scan(1, 65535); } catch (Exception __e) { throw rethrow(__e); } } public static List scan(int fromPort, int toPort) { return scan(fromPort, toPort, new int[0]); } public static List scan(int fromPort, int toPort, int[] preferredPorts) { try { Set preferredPortsSet = new HashSet(asList(preferredPorts)); int scanSize = toPort - fromPort + 1; String name = toPort < 10000 ? "bot" : "program"; int threads = isWindows() ? min(500, scanSize) : min(scanSize, 10); final ExecutorService es = Executors.newFixedThreadPool(threads); if (verbose) print(firstToUpper(name) + "-scanning " + ip + " with timeout " + timeout + " ms in " + threads + " threads."); startTiming(); List> futures = new ArrayList(); List ports = new ArrayList(); for (int port : preferredPorts) { futures.add(checkPort(es, ip, port, timeout)); ports.add(port); } for (int port = fromPort; port <= toPort; port++) if (!preferredPortsSet.contains(port) && !forbiddenPort(port)) { futures.add(checkPort(es, ip, port, timeout)); ports.add(port); } es.shutdown(); List programs = new ArrayList(); long time = now(); int i = 0; for (final Future f : futures) { if (verbose) print("Waiting for port " + get(ports, i++) + " at time " + (now() - time)); Program p = f.get(); if (p != null) programs.add(p); } if (verbose) print("Found " + programs.size() + " " + name + "(s) on " + ip); return programs; } catch (Exception __e) { throw rethrow(__e); } } public static Future checkPort(final ExecutorService es, final String ip, final int port, final int timeout) { return es.submit(new Callable() { @Override public Program call() { try { Socket socket = new Socket(); try { socket.setSoTimeout(timeout); socket.connect(new InetSocketAddress(ip, port), timeout); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8")); String hello = or(in.readLine(), "?"); return new Program(port, hello); } finally { socket.close(); } } catch (Exception ex) { return null; } } }); } public static List quickScan() { return scan(quickScanFrom, quickScanTo); } public static List quickBotScan() { return quickBotScan(new int[0]); } public static List quickBotScan(int[] preferredPorts) { if (maxNumberOfVMs == 0) maxNumberOfVMs = isAndroid() ? maxNumberOfVMs_android : maxNumberOfVMs_nonAndroid; return scan(4999, 5000 + maxNumberOfVMs - 1, preferredPorts); } } public static ThreadLocal> MyHTTPD_headers = new ThreadLocal(); public static ThreadLocal MyHTTPD_current = new ThreadLocal(); public static class MyHTTPD extends NanoHTTPD { public MyHTTPD(int port) { super(port); } public Object onServe, onEndServe; public volatile long requests; public static class Serving { public String uri; public Map header, parms, files; } public List currentlyServing = synchroList(); public Response serve(String uri, Method method, Map header, Map parms, Map files) { try { ++requests; AutoCloseable __4381 = tempSetThreadLocal(MyHTTPD_current, this); try { Serving serving = nu(Serving.class, "uri", uri, "header", header, "parms", parms, "files", files); currentlyServing.add(serving); pcallF(onServe, serving); try { String remoteAddr = getClientIPFromHeaders(header); print(formatDateAndTime() + " Serving URI: " + quote(uri) + " to: " + remoteAddr); try { Response response = (Response) (callOpt(getMainClass(), "serve", uri, method, header, parms, files)); if (response != null) return response; Object html; MyHTTPD_headers.set(header); try { html = callHtmlMethod2(getMainClass(), uri, parms); } finally { MyHTTPD_headers.set(null); } if (html != null) return html instanceof String ? serveHTML((String) html) : (NanoHTTPD.Response) html; return serve404(); } catch (Throwable e) { printStackTrace(e); return serveHTML("ERROR."); } } finally { pcallF(onEndServe, serving); currentlyServing.remove(serving); } } finally { _close(__4381); } } catch (Exception __e) { throw rethrow(__e); } } } public static class SyncListMultiMap extends MultiMap { public SyncListMultiMap() { } public SyncListMultiMap(boolean useTreeMap) { super(useTreeMap); } public SyncListMultiMap(Map> data) { this.data = data; } public List _makeEmptyList() { return new SynchronizedArrayList(); } } public abstract static class F0 { public abstract A get(); } public abstract static class F1 { public abstract B get(A a); } public abstract static class IterableIterator implements Iterator, Iterable { public Iterator iterator() { return this; } public void remove() { unsupportedOperation(); } } public abstract static class F2 { public abstract C get(A a, B b); } public static class RemoteDB { public DialogIO db; public String name; public RemoteDB(String s) { this(s, false); } public RemoteDB(String s, boolean autoStart) { name = s; if (isSnippetID(s)) name = dbBotName(s); db = findBot(name); if (db == null) if (autoStart) { nohupJavax(fsI(s)); waitForBotStartUp(name); assertNotNull("Weird problem", db = findBot(s)); } else throw fail("DB " + s + " not running"); } public boolean functional() { return db != null; } public List list() { return adopt((List) rpc(db, "xlist")); } public List list(String className) { return adopt((List) rpc(db, "xlist", className)); } public List xlist() { return list(); } public List xlist(String className) { return list(className); } public List adopt(List l) { if (l != null) for (RC rc : l) adopt(rc); return l; } public RC adopt(RC rc) { if (rc != null) rc.db = this; return rc; } public Object adopt(Object o) { if (o instanceof RC) return adopt((RC) o); return o; } public String xclass(RC o) { return (String) rpc(db, "xclass", o); } public Object xget(RC o, String field) { return adopt(rpc(db, "xget", o, field)); } public String xS(RC o, String field) { return (String) xget(o, field); } public RC xgetref(RC o, String field) { return adopt((RC) xget(o, field)); } public void xset(RC o, String field, Object value) { rpc(db, "xset", o, field, value); } public RC uniq(String className) { RC ref = first(list(className)); if (ref == null) ref = xnew(className); return ref; } public RC xuniq(String className) { return uniq(className); } public RC xnew(String className, Object... values) { return adopt((RC) rpc(db, "xnew", className, values)); } public void xdelete(RC o) { rpc(db, "xdelete", o); } public void xdelete(List l) { rpc(db, "xdelete", l); } public void close() { if (db != null) db.close(); } public String fullgrab() { return (String) rpc(db, "xfullgrab"); } public String xfullgrab() { return fullgrab(); } public void xshutdown() { rpc(db, "xshutdown"); } public long xchangeCount() { return (long) rpc(db, "xchangeCount"); } public int xcount() { return (int) rpc(db, "xcount"); } public void reconnect() { close(); db = findBot(name); } public RC rc(long id) { return new RC(this, id); } } public static class Flag { public boolean up; public synchronized boolean raise() { if (!up) { up = true; notifyAll(); return true; } else return false; } public synchronized void waitUntilUp() { while (!up) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void waitUntilUp(long timeout) { if (!up) { try { wait(timeout); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized boolean isUp() { return up; } public String toString() { return isUp() ? "up" : "down"; } public void waitForThisOr(Flag otherFlag) { try { while (!isUp() && !otherFlag.isUp()) Thread.sleep(50); } catch (Exception __e) { throw rethrow(__e); } } } public static class TripleWebWithSource extends TripleWeb { public String source; public TripleWebWithSource() { } public TripleWebWithSource(String source) { this.source = source; } public String source() { return source; } } public static interface Producer { public A next(); } public static class WebRelation extends WebNode { public WebNode a, b; public void _setField(String f, Object x) { if (f.equals("a")) a = (WebNode) x; else if (f.equals("b")) b = (WebNode) x; else super._setField(f, x); } public WebRelation() { } public WebRelation(Web web, WebNode a, WebNode b) { super(web); this.a = a; this.b = b; } } public static class Web { public List nodes = new ArrayList(); public static final MultiMap index = null; public boolean useCLParse = true; public boolean labelsToUpper; public String title; public Object globalID = aGlobalIDObj(); public String source; public boolean unverified; public long created = nowUnlessLoading(); public static List onNewNode = new ArrayList(); public static List onNewLabel = new ArrayList(); public static int F_useCLParse = 1; public static int F_labelsToupper = 2; public static int F_unverified = 3; public void relation(WebNode a, String arrow, WebNode b) { getRelation(a, b).addLabel(arrow); } public Pair relation(String a, String arrow, String b) { return relation(lisp(arrow, a, b)); } public Pair relation(Lisp l) { if (l(l) == 1) { findNode(l.get(0)).addLabel(lisp("wvuyakuvuelmxpwp", l.head)); return null; } else if (l(l) == 2) { String a = lisp2label(l.get(0)), b = lisp2label(l.get(1)); if (l.is("fgvvrzypbkqomktd")) { findNode(a).addLabel(b); findNode(b).addLabel(a); } WebNode na = findNode(a), nb = findNode(b); getRelation(na, nb).addLabel(l.head); return pair(na, nb); } return null; } public void relations(List l) { for (Lisp li : l) relation(li); } public WebRelation getRelation(String a, String b) { return getRelation(findNode(a), findNode(b)); } public WebRelation getRelation(WebNode a, WebNode b) { return getRelation(pair(a, b)); } public WebRelation relation(WebNode a, WebNode b) { return getRelation(a, b); } public WebRelation relation(Pair p) { return getRelation(p); } public WebRelation getRelationOpt(Pair p) { return getRelationOpt(p.a, p.b); } public WebRelation getRelationOpt(WebNode a, WebNode b) { for (WebNode n : nodes) if (n instanceof WebRelation) { WebRelation r = ((WebRelation) n); if (r.a == a && r.b == b) return r; } return null; } public WebRelation getRelation(Pair p) { WebRelation r = getRelationOpt(p.a, p.b); if (r == null) { r = _newRelation(p.a, p.b); } return r; } public WebRelation _newRelation(WebNode a, WebNode b) { WebRelation r = new WebRelation(this, a, b); nodes.add(r); return r; } public WebNode newNode() { WebNode node = new WebNode(this); nodes.add(node); return node; } public WebNode newNode(String s) { WebNode node = newNode(); node.addLabel(parseLabel(s)); return node; } public WebNode node(String s) { return findNode(s); } public WebNode node(Lisp l) { return findNode(l); } public WebNode findNode(String s) { return findNode(parseLabel(s)); } public WebNode findNode(Lisp l) { WebNode n = findNodeOpt(l); return n != null ? n : newNode(l); } public WebNode findNodeOpt(Lisp l) { if (index != null) return first(index.get(l)); for (WebNode n : nodes) if (n.hasLabel(l)) return n; return null; } public WebNode newNode(String... labels) { WebNode n = newNode(); for (String label : labels) n.addLabel(label); return n; } public WebNode newNode(Lisp... labels) { WebNode n = newNode(); for (Lisp label : labels) n.addLabel(label); return n; } public String toString() { return webToString(this); } public void index(Lisp label, WebNode n) { if (index != null) index.put(label, n); fireNewLabel(n, label); } public void clear() { clearAll(nodes, index); } public Lisp parseLabel(String s) { return lisp(labelsToUpper ? upper(s) : s); } public String unparseLabel(Lisp l) { return lispHead(l); } public List parseLabels(List l) { List x = new ArrayList(l(l)); for (String s : l) x.add(parseLabel(s)); return x; } public List unparseLabels(List l) { List x = new ArrayList(l(l)); for (Lisp lbl : l) x.add(unparseLabel(lbl)); return x; } public void fireNewNode(WebNode node) { for (Object f : onNewNode) pcallF(f, node); } public void fireNewLabel(WebNode node, Lisp label) { for (Object f : onNewLabel) pcallF(f, node, label); } public void removeNode(WebNode n) { if (n == null || !nodes.contains(n)) return; n.web = null; if (index != null) for (Lisp label : n.labels()) index.remove(label, n); nodes.remove(n); } public void removeRelation(WebNode a, WebNode b) { Pair p = pair(a, b); WebNode r = getRelationOpt(p); if (r == null) return; removeNode(r); } public boolean verified() { return !unverified; } public String globalID() { return strOrNull(globalID); } public GlobalID globalIDObj() { return globalID instanceof String ? new GlobalID((String) globalID) : (GlobalID) globalID; } public void setGlobalID(String id) { globalID = asGlobalID(id); } public int hashCode() { return globalIDObj().hashCode(); } public boolean equals(Object o) { return o instanceof Web && eq(globalIDObj(), ((Web) o).globalIDObj()); } } public abstract static class VirtualNodeIndex { public TreeSet implicitConcepts = new TreeSet(); public abstract int numWebs(); public abstract int numTerms(); public abstract List get(CharSequence query); public boolean hasShortTerm(String s) { return false; } public Web getWeb(GlobalID id) { return null; } public Collection indexedTerms() { return null; } public Collection allTriples() { return null; } public Set implicitConcepts() { return implicitConcepts; } } public static class LatestList extends LinkedList { public int limit; public LatestList(int limit) { this.limit = limit; } public boolean add(A o) { if (limit == 0) return true; while (size() >= limit) super.remove(); return super.add(o); } } public static interface IVar { public void set(A a); public A get(); public boolean has(); public void clear(); } public static class Cache { public Object maker; public A value; public long loaded; public static boolean debug; public long changeCount; public Lock lock = lock(); public Cache() { } public Cache(Object maker) { this.maker = maker; } public A get() { if (hasLock(lock)) return value; Lock __4406 = lock; lock(__4406); try { if (loaded == 0) { value = (A) callF(maker); changeCount++; loaded = sysNow(); } return value; } finally { unlock(__4406); } } public void clear() { Lock __4407 = lock; lock(__4407); try { if (debug && loaded != 0) print("Clearing cache"); value = null; changeCount++; loaded = 0; } finally { unlock(__4407); } } public void clear(double seconds) { Lock __4408 = lock; lock(__4408); try { if (seconds != 0 && loaded != 0 && sysNow() >= loaded + seconds * 1000) clear(); } finally { unlock(__4408); } } public void set(A a) { Lock __4409 = lock; lock(__4409); try { value = a; ++changeCount; loaded = sysNow(); } finally { unlock(__4409); } } } public abstract static class LazyList extends RandomAccessAbstractList { public Iterator iterator() { return concurrentlyIterateList(this); } } public static class Drawing extends Concept { public static String _fieldOrder = "globalID name calStructure"; public String globalID = aGlobalID(); public String name; public String calStructure; public CirclesAndLines cal() { return (CirclesAndLines) unstructure(calStructure); } } public static class Matches { public String[] m; public Matches() { } public Matches(String... m) { this.m = m; } public String get(int i) { return i < m.length ? m[i] : null; } public String unq(int i) { return unquote(get(i)); } public String fsi(int i) { return formatSnippetID(unq(i)); } public String fsi() { return fsi(0); } public String tlc(int i) { return unq(i).toLowerCase(); } public boolean bool(int i) { return "true".equals(unq(i)); } public String rest() { return m[m.length - 1]; } public int psi(int i) { return Integer.parseInt(unq(i)); } } public static class BaseBase { public String globalID = aGlobalID(); public String text; public String textForRender() { return text; } } public static boolean traits_multiLine = true; public static class Base extends BaseBase { public List traits = new ArrayList(); public boolean hasTrait(String t) { return containsIC(traits(), t); } public List traits() { if (nempty(text) && neq(first(traits), text)) traits.add(0, text); return traits; } public void addTraits(List l) { setAddAll(traits(), l); } public void addTrait(String t) { if (nempty(t)) setAdd(traits(), t); } public String textForRender() { List traits = traits(); if (traits_multiLine) return lines(traits); if (l(traits) <= 1) return first(traits); return first(traits) + " [" + join(", ", dropFirst(traits)) + "]"; } public void setText(String text) { this.text = text; traits = ll(text); } } public static class CirclesAndLines { public List circles = new ArrayList(); public List lines = new ArrayList(); public Class arrowClass = Arrow.class; public Class circleClass = Circle.class; public String title; public String globalID = aGlobalID(); public long created = nowUnlessLoading(); public transient Lock lock = fairLock(); public transient String defaultImageID = "#1007372"; public transient double imgZoom = 1; public transient Pt translate; public Circle hoverCircle; public transient Object onUserMadeArrow, onUserMadeCircle, onLayoutChange; public transient Object onFullLayoutChange, onDeleteCircle, onDeleteLine; public transient Object onRenameCircle, onRenameLine, onStructureChange; public transient BufferedImage imageForUserMadeNodes; public static int maxDistanceToLine = 20; public transient String backgroundImageID = defaultBackgroundImageID; public static String defaultBackgroundImageID = "#1007195"; public static Color defaultLineColor = Color.white; public static boolean debugRender; public static Object staticPopupExtender; public transient double scale = 1; public transient boolean recordHistory = true; public List history; public Circle circle_autoVis(String text, String visualizationText, double x, double y) { return addAndReturn(circles, nu(circleClass, "x", x, "y", y, "text", text, "quickvis", visualizationText, "img", processImage(quickVisualizeOr(visualizationText, defaultImageID)))); } public String makeVisualizationText(String text) { return possibleGlobalID(text) ? "" : text; } public Circle circle_autoVis(String text, double x, double y) { return circle_autoVis(text, makeVisualizationText(text), x, y); } public Circle circle(BufferedImage img, double x, double y, String text) { return addAndReturn(circles, nu(circleClass, "x", x, "y", y, "text", text, "img", processImage(img))); } public Circle circle(String text, BufferedImage img, double x, double y) { return circle(img, x, y, text); } public Circle circle(String text, double x, double y) { return addAndReturn(circles, nu(circleClass, "x", x, "y", y, "text", text, "img", processImage(imageForUserMadeNodes()))); } public Circle addCircle(String imageID, double x, double y) { return addCircle(imageID, x, y, ""); } public Circle addCircle(String imageID, double x, double y, String text) { return addAndReturn(circles, nu(circleClass, "x", x, "y", y, "text", text, "img", processImage(loadImage2(imageID)))); } public Arrow findArrow(Circle a, Circle b) { for (Line l : getWhere(lines, "a", a, "b", b)) if (l instanceof Arrow) return (Arrow) l; return null; } public Line addLine(Circle a, Circle b) { Line line = findWhere(lines, "a", a, "b", b); if (line == null) lines.add(line = nu(Line.class, "a", a, "b", b)); return line; } public Arrow arrow(Circle a, String text, Circle b) { return addArrow(a, b, text); } public Arrow addArrow(Circle a, Circle b) { return addArrow(a, b, ""); } public Arrow addArrow(Circle a, Circle b, String text) { return addAndReturn(lines, nu(arrowClass, "a", a, "b", b, "text", text)); } public BufferedImage makeImage(int w, int h) { BufferedImage bg = renderTiledBackground(backgroundImageID, w, h, ptX(translate), ptY(translate)); if (!lock.tryLock()) return null; try { if (scale != 1) createGraphics_modulate(bg, new VF1() { public void get(Graphics2D g) { try { g.scale(scale, scale); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "g.scale(scale, scale);"; } }); if (debugRender) print("Have " + n(lines, "line")); HashMap, Line> hasLine = new HashMap(); HashMap flipMap = new HashMap(); for (Line l : lines) { hasLine.put(pair(l.a, l.b), l); Line x = hasLine.get(pair(l.b, l.a)); if (x != null) { if (debugRender) print("flipMap " + l.a.text + " / " + l.b.text); flipMap.put(x, false); flipMap.put(l, false); } } for (Line l : lines) { DoublePt a = translateDoublePt(translate, l.a.doublePt(w, h, this)); DoublePt b = translateDoublePt(translate, l.b.doublePt(w, h, this)); if (debugRender) print("Line " + a + " " + b); if (l instanceof Arrow) drawThoughtArrow(bg, l.a.img(this), iround(a.x), iround(a.y), l.b.img(this), iround(b.x), iround(b.y), l.color); else drawThoughtLine(bg, l.a.img(this), iround(a.x), iround(a.y), l.b.img(this), iround(b.x), iround(b.y), l.color); String text = l.textForRender(); if (nempty(text)) { drawOutlineTextAlongLine_flip.set(flipMap.get(l)); drawThoughtLineText_multiLine(bg, l.a.img(this), iround(a.x), iround(a.y), l.b.img(this), iround(b.x), iround(b.y), text, Color.white); } } for (Circle c : circles) { DoublePt p = translateDoublePt(translate, c.doublePt(w, h, this)); drawThoughtCircle(bg, c.img(this), p.x, p.y); } for (Circle c : circles) { DoublePt p = translateDoublePt(translate, c.doublePt(w, h, this)); String text = c.textForRender(); if (nempty(text)) drawThoughtCircleText(bg, c.img(this), p, text); if (c == hoverCircle) drawThoughtCirclePlus(bg, c.img(this), p.x, p.y); } } finally { lock.unlock(); createGraphics_modulate(bg, null); } return bg; } public Canvas showAsFrame(int w, int h) { Canvas canvas = showAsFrame(); frameInnerSize(canvas, w, h); centerFrame(getFrame(canvas)); return canvas; } public Canvas showAsFrame() { return (Canvas) swing(new F0() { public Object get() { try { Canvas canvas = makeCanvas(); showCenterFrame(canvas); return canvas; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Canvas canvas = makeCanvas();\r\n showCenterFrame(canvas);\r\n ret canvas;"; } }); } public Canvas makeCanvas() { final Object makeImg = new F2() { public Object get(Integer w, Integer h) { try { return makeImage(w, h); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "makeImage(w, h)"; } }; final Canvas canvas = jcanvas(makeImg); disableImageSurfaceSelector(canvas); new CircleDragger(this, canvas, new Runnable() { public void run() { try { updateCanvas(canvas, makeImg); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "updateCanvas(canvas, makeImg)"; } }); componentPopupMenu(canvas, new VF1() { public void get(JPopupMenu menu) { try { Pt p = pointFromEvent(canvas, componentPopupMenu_mouseEvent.get()); JMenu imageMenu = jmenu("Image"); moveAllMenuItems(menu, imageMenu); addMenuItem(menu, "New Circle...", new Runnable() { public void run() { try { newCircle(canvas); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "newCircle(canvas)"; } }); final Line l = findLine(canvas, p); if (l != null) { addMenuItem(menu, "Rename Relation...", new Runnable() { public void run() { try { renameLine(canvas, l); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "renameLine(canvas, l)"; } }); addMenuItem(menu, "Delete Relation", new Runnable() { public void run() { try { deleteLine(l); canvas.update(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "deleteLine(l);\r\n canvas.update();"; } }); } final Circle c = findCircle(canvas, p); if (c != null) { addMenuItem(menu, "Rename Circle...", new Runnable() { public void run() { try { renameCircle(canvas, c); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "renameCircle(canvas, c)"; } }); addMenuItem(menu, "Delete Circle", new Runnable() { public void run() { try { deleteCircle(c); canvas.update(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "deleteCircle(c);\r\n canvas.update();"; } }); if (c.img != null || c.quickvis != null) addMenuItem(menu, "Delete Image", new Runnable() { public void run() { try { c.img = null; c.quickvis = null; canvas.update(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "c.img = null;\r\n c.quickvis = null;\r\n canvas.update();"; } }); if (neqic(c.text, c.quickvis)) addMenuItem(menu, "Visualize", new Runnable() { public void run() { try { { Thread _t_0 = new Thread("Visualizing") { public void run() { try { quickVisualize(c.text); print("Quickvis done"); { swing(new Runnable() { public void run() { try { c.img = null; c.quickvis = c.text; canvas.update(); pcallF(onRenameCircle, c); schange(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "c.img = null;\r\n c.quickvis = c.text;\r\n canvas.u..."; } }); } } catch (Throwable __e) { _handleException(__e); } } }; startThread(_t_0); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "thread \"Visualizing\" {\r\n quickVisualize(c.text);\r\n ..."; } }); } addMenuItem(menu, "Copy structure to clipboard", new Runnable() { public void run() { try { copyTextToClipboard(cal_simplifiedStructure(CirclesAndLines.this)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "copyTextToClipboard(cal_simplifiedStructure(CirclesAndLines.this))"; } }); addMenuItem(menu, "Paste structure", new Runnable() { public void run() { try { String text = getTextFromClipboard(); if (nempty(text)) { copyCAL(cal_unstructure(text), CirclesAndLines.this); canvas.update(); schange(); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "String text = getTextFromClipboard();\r\n if (nempty(text)) {\r\n ..."; } }); pcallF(staticPopupExtender, CirclesAndLines.this, canvas, menu); addMenuItem(menu, imageMenu); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "// POPUP MENU START\r\n Pt p = pointFromEvent(canvas, componentPopupMenu_m..."; } }); return canvas; } public void newCircle(final Canvas canvas) { final JTextField text = jtextfield(); showFormTitled("New Circle", "Text", text, runnableThread(new Runnable() { public void run() { try { { JWindow _loading_window = showLoadingAnimation(); try { String theText = getTextTrim(text); makeCircle(theText); canvas.update(); } finally { disposeWindow(_loading_window); } } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "{ JWindow _loading_window = showLoadingAnimation(); try {\r\n String theTe..."; } })); } public Canvas show() { return showAsFrame(); } public Canvas show(int w, int h) { return showAsFrame(w, h); } public Circle findCircle(String text) { for (Circle c : circles) if (eq(c.text, text)) return c; for (Circle c : circles) if (eqic(c.text, text)) return c; return null; } public void renameCircle(final Canvas canvas, final Circle c) { final JTextField tf = jtextfield(c.text); showFormTitled("Rename circle", "Old name", jlabel(c.text), "New name", tf, new Runnable() { public void run() { try { c.setText(getTextTrim(tf)); canvas.update(); pcallF(onRenameCircle, c); schange(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "c.setText(getTextTrim(tf));\r\n canvas.update();\r\n pcallF(onRenam..."; } }); } public void renameLine(final Canvas canvas, final Line l) { final JTextField tf = jtextfield(l.text); showFormTitled("Rename relation", "Old name", jlabel(l.text), "New name", tf, new Runnable() { public void run() { try { l.setText(getTextTrim(tf)); canvas.update(); pcallF(onRenameLine, l); schange(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "l.setText(getTextTrim(tf));\r\n canvas.update();\r\n pcallF(onRenam..."; } }); } public void clear() { clearAll(circles, lines); } public Circle findCircle(ImageSurface canvas, Pt p) { p = untranslatePt(translate, p); Lowest best = new Lowest(); for (Circle c : circles) if (c.contains(this, canvas, p)) best.put(c, pointDistance(p, c.pt2(this, canvas))); return best.get(); } public Circle findNearestCircle(ImageSurface canvas, Pt p) { Lowest best = new Lowest(); for (Circle c : circles) if (c.contains(this, canvas, p)) best.put(c, pointDistance(p, c.pt2(this, canvas))); return best.get(); } public BufferedImage processImage(BufferedImage img) { return scaleImage(img, imgZoom); } public void deleteCircle(Circle c) { for (Line l : cloneList(lines)) if (l.a == c || l.b == c) deleteLine(l); circles.remove(c); pcallF(onDeleteCircle, c); schange(); } public void deleteLine(Line l) { lines.remove(l); pcallF(onDeleteLine, l); schange(); } public void openPlusDialog(final Circle c, final ImageSurface canvas) { if (c == null) return; final JTextField tfFrom = jtextfield(c.text); final JTextField tfRel = jtextfield(web_defaultRelationName()); final JComboBox tfTo = autoComboBox(collect(circles, "text")); showFormTitled("Add connection", "From node", tfFrom, "Connection name", tfRel, "To node", tfTo, new F0() { public Object get() { try { String sA = getTextTrim(tfFrom); Circle a = eq(sA, c.text) ? c : findOrMakeCircle(sA); if (a == null) { messageBox("Not found: " + getTextTrim(tfFrom)); return false; } Circle b = findOrMakeCircle(getTextTrim(tfTo)); if (b == null) { messageBox("Not found: " + getTextTrim(tfTo)); return false; } if (a == b) { infoBox("Can't connect circle to itself for now"); return false; } Arrow arrow = arrow(a, getTextTrim(tfRel), b); ((Canvas) canvas).update(); pcallF(onUserMadeArrow, arrow); schange(); return null; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "S sA = getTextTrim(tfFrom);\r\n Circle a = eq(sA, c.text) ? c : findOrMa..."; } }); awtLater(tfRel, 100, new Runnable() { public void run() { try { requestFocus(tfRel); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "requestFocus(tfRel)"; } }); } public void schange() { pcallF(onStructureChange); logQuoted("user-web-edits", now() + " " + cal_structure(this)); markWebsPosted(); } public Circle findOrMakeCircle(String text) { Circle c = findCircle(text); if (c != null) return c; return makeCircle(text); } public Circle makeCircle(String text) { Circle c = circle(imageForUserMadeNodes(), random(0.1, 0.9), random(0.1, 0.9), text); pcallF(onUserMadeCircle, c); schange(); historyLog(lisp("Made circle", text)); return c; } public BufferedImage imageForUserMadeNodes() { if (imageForUserMadeNodes == null) imageForUserMadeNodes = whiteImage(20, 20); return imageForUserMadeNodes; } public Line findLine(Canvas is, Pt p) { p = untranslatePt(translate, p); Lowest best = new Lowest(); for (Line line : lines) { double d = distancePointToLineSegment(line.a.pt(this, is), line.b.pt(this, is), p); if (d <= maxDistanceToLine) best.put(line, d); } return best.get(); } public Pt pointFromEvent(ImageSurface canvas, MouseEvent e) { return scalePt(canvas.pointFromEvent(e), 1 / scale); } public void historyLog(Object o) { if (!recordHistory) return; if (history == null) history = new ArrayList(); history.add(o); } } public static class Circle extends Base { public transient BufferedImage img; public double x, y; public String quickvis; public BufferedImage img(CirclesAndLines cal) { if (img != null) return img; if (nempty(quickvis)) img = quickVisualize(quickvis); return cal.imageForUserMadeNodes(); } public Pt pt2(CirclesAndLines cal, ImageSurface is) { return pt2(is.getWidth(), is.getHeight(), cal); } public Pt pt(CirclesAndLines cal, ImageSurface is) { return pt(is.getWidth(), is.getHeight(), cal); } public Pt pt(int w, int h, CirclesAndLines cal) { return new Pt(iround(x * w / cal.scale), iround(y * h / cal.scale)); } public Pt pt2(int w, int h, CirclesAndLines cal) { return new Pt(iround(x * w), iround(y * h)); } public DoublePt doublePt(int w, int h, CirclesAndLines cal) { return new DoublePt(x * w / cal.scale, y * h / cal.scale); } public boolean contains(CirclesAndLines cal, ImageSurface is, Pt p) { return pointDistance(p, pt2(cal, is)) <= iround(thoughtCircleSize(img(cal)) * cal.scale) / 2 + 1; } } public static class Line extends Base { public Circle a, b; public transient Color color = CirclesAndLines.defaultLineColor; public Line setColor(Color color) { this.color = color; return this; } } public static class Arrow extends Line { } public static class CircleDragger extends MouseAdapter { public CirclesAndLines cal; public ImageSurface is; public Object update; public int dx, dy; public Circle circle; public Pt startPoint; public CircleDragger(CirclesAndLines cal, ImageSurface is, Object update) { this.update = update; this.is = is; this.cal = cal; if (containsInstance(is.tools, CircleDragger.class)) return; is.tools.add(this); is.addMouseListener(this); is.addMouseMotionListener(this); } public void mouseMoved(MouseEvent e) { Pt p = is.pointFromEvent(e); Circle c = cal.findCircle(is, p); if (c != cal.hoverCircle) { cal.hoverCircle = c; callF(update); } } public void mousePressed(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON1) { Pt p = is.pointFromEvent(e); startPoint = p; circle = cal.findCircle(is, p); if (circle != null) { dx = p.x - iround(circle.x * is.getWidth()); dy = p.y - iround(circle.y * is.getHeight()); } else { Pt t = unnull(cal.translate); dx = p.x - t.x; dy = p.y - t.y; } } } public void mouseDragged(MouseEvent e) { if (startPoint == null) return; Pt p = is.pointFromEvent(e); if (circle != null) { circle.x = (p.x - dx) / (double) is.getWidth(); circle.y = (p.y - dy) / (double) is.getHeight(); pcallF(cal.onLayoutChange, circle); callF(update); } else { cal.translate = new Pt(p.x - dx, p.y - dy); callF(update); } } public void mouseReleased(MouseEvent e) { mouseDragged(e); if (eq(is.pointFromEvent(e), startPoint)) cal.openPlusDialog(circle, is); circle = null; startPoint = null; } } public static class TimedCache { public long timeout; public A value; public Object function; public long set; public Lock lock = lock(); public int stores, fails, hits; public TimedCache(Object function, double timeoutSeconds) { this(timeoutSeconds); this.function = function; } public TimedCache(double timeoutSeconds) { timeout = toMS(timeoutSeconds); } public A set(A a) { Lock __4421 = lock; lock(__4421); try { ++stores; value = a; set = now(); return a; } finally { unlock(__4421); } } public boolean has() { Lock __4422 = lock; lock(__4422); try { clean(); if (set != 0) { ++hits; return true; } ++fails; return false; } finally { unlock(__4422); } } public A get() { Lock __4423 = lock; lock(__4423); try { if (function != null) return get(function); clean(); if (set != 0) ++hits; else ++fails; return value; } finally { unlock(__4423); } } public A get(Object makerFunction) { Lock __4424 = lock; lock(__4424); try { if (this.has()) return getNoClean(); A a = (A) (callF(makerFunction)); return set(a); } finally { unlock(__4424); } } public A getNoClean() { Lock __4425 = lock; lock(__4425); try { return value; } finally { unlock(__4425); } } public void clean() { Lock __4426 = lock; lock(__4426); try { if (timeout > 0 && now() > set + timeout) clear(); } finally { unlock(__4426); } } public void clear() { Lock __4427 = lock; lock(__4427); try { set = 0; value = null; } finally { unlock(__4427); } } public String stats() { return "Stores: " + stores + ", hits: " + hits + ", fails: " + fails; } } public static boolean NanoHTTPD_debug; public abstract static class NanoHTTPD { public boolean decodePercentInURI; public static ThreadLocal currentSession = new ThreadLocal(); public static List badClients = synchroList(); public interface AsyncRunner { public void closeAll(); public void closed(ClientHandler clientHandler); public void exec(ClientHandler code); } public class ClientHandler implements Runnable { public final InputStream inputStream; public final Socket acceptSocket; public ClientHandler(InputStream inputStream, Socket acceptSocket) { this.inputStream = inputStream; this.acceptSocket = acceptSocket; } public void close() { safeClose(this.inputStream); safeClose(this.acceptSocket); } @Override public void run() { OutputStream outputStream = null; try { outputStream = this.acceptSocket.getOutputStream(); TempFileManager tempFileManager = NanoHTTPD.this.tempFileManagerFactory.create(); HTTPSession session = new HTTPSession(tempFileManager, this.inputStream, outputStream, this.acceptSocket.getInetAddress()); while (!this.acceptSocket.isClosed() && !session.badClient) { session.execute(); } } catch (Exception e) { if (!(e instanceof SocketException && "NanoHttpd Shutdown".equals(e.getMessage())) && !(e instanceof SocketTimeoutException)) { NanoHTTPD.LOG.log(Level.FINE, "Communication with the client broken", e); } } finally { safeClose(outputStream); safeClose(this.inputStream); safeClose(this.acceptSocket); NanoHTTPD.this.asyncRunner.closed(this); } } } public static class Cookie { public static String getHTTPTime(int days) { Calendar calendar = Calendar.getInstance(); SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); calendar.add(Calendar.DAY_OF_MONTH, days); return dateFormat.format(calendar.getTime()); } public final String n, v, e; public Cookie(String name, String value) { this(name, value, 30); } public Cookie(String name, String value, int numDays) { this.n = name; this.v = value; this.e = getHTTPTime(numDays); } public Cookie(String name, String value, String expires) { this.n = name; this.v = value; this.e = expires; } public String getHTTPHeader() { String fmt = "%s=%s; expires=%s"; return String.format(fmt, this.n, this.v, this.e); } } public class CookieHandler implements Iterable { public final HashMap cookies = new HashMap(); public final ArrayList queue = new ArrayList(); public CookieHandler(Map httpHeaders) { String raw = httpHeaders.get("cookie"); if (raw != null) { String[] tokens = raw.split(";"); for (String token : tokens) { String[] data = token.trim().split("="); if (data.length == 2) { this.cookies.put(data[0], data[1]); } } } } public void delete(String name) { set(name, "-delete-", -30); } @Override public Iterator iterator() { return this.cookies.keySet().iterator(); } public String read(String name) { return this.cookies.get(name); } public void set(Cookie cookie) { this.queue.add(cookie); } public void set(String name, String value, int expires) { this.queue.add(new Cookie(name, value, Cookie.getHTTPTime(expires))); } public void unloadQueue(Response response) { for (Cookie cookie : this.queue) { response.addHeader("Set-Cookie", cookie.getHTTPHeader()); } } } public static class DefaultAsyncRunner implements AsyncRunner { public long requestCount; public final List running = Collections.synchronizedList(new ArrayList()); public List getRunning() { return running; } @Override public void closeAll() { for (ClientHandler clientHandler : new ArrayList(this.running)) { clientHandler.close(); } } @Override public void closed(ClientHandler clientHandler) { this.running.remove(clientHandler); } @Override public void exec(ClientHandler clientHandler) { ++this.requestCount; Thread t = new Thread(clientHandler); String clientIP = "?"; try { clientIP = clientHandler.acceptSocket.getInetAddress().getHostAddress().toString(); } catch (Throwable __e) { _handleException(__e); } t.setName("NanoHttpd serving request #" + this.requestCount + " to " + clientIP); this.running.add(clientHandler); t.start(); } } public static class DefaultTempFile implements TempFile { public final File file; public final OutputStream fstream; public DefaultTempFile(String tempdir) throws IOException { this.file = File.createTempFile("NanoHTTPD-", "", new File(tempdir)); this.fstream = new FileOutputStream(this.file); System.err.println("Temp file created: " + file); } @Override public void delete() throws Exception { safeClose(this.fstream); System.err.println("Temp file deleted: " + file); if (!this.file.delete()) { throw new Exception("could not delete temporary file"); } } @Override public String getName() { return this.file.getAbsolutePath(); } @Override public OutputStream open() throws Exception { return this.fstream; } } public static class DefaultTempFileManager implements TempFileManager { public final String tmpdir; public final List tempFiles; public DefaultTempFileManager() { this.tmpdir = tempDir().getPath(); this.tempFiles = new ArrayList(); } @Override public void clear() { for (TempFile file : this.tempFiles) { try { file.delete(); } catch (Exception ignored) { NanoHTTPD.LOG.log(Level.WARNING, "could not delete file ", ignored); } } this.tempFiles.clear(); } @Override public TempFile createTempFile() throws Exception { DefaultTempFile tempFile = new DefaultTempFile(this.tmpdir); this.tempFiles.add(tempFile); return tempFile; } } public class DefaultTempFileManagerFactory implements TempFileManagerFactory { @Override public TempFileManager create() { return new DefaultTempFileManager(); } } public static final String CONTENT_DISPOSITION_REGEX = "([ |\t]*Content-Disposition[ |\t]*:)(.*)"; public static final Pattern CONTENT_DISPOSITION_PATTERN = Pattern.compile(CONTENT_DISPOSITION_REGEX, Pattern.CASE_INSENSITIVE); public static final String CONTENT_TYPE_REGEX = "([ |\t]*content-type[ |\t]*:)(.*)"; public static final Pattern CONTENT_TYPE_PATTERN = Pattern.compile(CONTENT_TYPE_REGEX, Pattern.CASE_INSENSITIVE); public static final String CONTENT_DISPOSITION_ATTRIBUTE_REGEX = "[ |\t]*([a-zA-Z]*)[ |\t]*=[ |\t]*['|\"]([^\"^']*)['|\"]"; public static final Pattern CONTENT_DISPOSITION_ATTRIBUTE_PATTERN = Pattern.compile(CONTENT_DISPOSITION_ATTRIBUTE_REGEX); public class HTTPSession implements IHTTPSession { public boolean badClient; public static final int BUFSIZE = 8192; public final TempFileManager tempFileManager; public final OutputStream outputStream; public final PushbackInputStream inputStream; public int splitbyte; public int rlen; public String uri; public Method method; public Map parms; public Map headers; public CookieHandler cookies; public String queryParameterString; public String remoteIp; public String protocolVersion; public HTTPSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream) { this.tempFileManager = tempFileManager; this.inputStream = new PushbackInputStream(inputStream, HTTPSession.BUFSIZE); this.outputStream = outputStream; } public HTTPSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream, InetAddress inetAddress) { this.tempFileManager = tempFileManager; this.inputStream = new PushbackInputStream(inputStream, HTTPSession.BUFSIZE); this.outputStream = outputStream; this.remoteIp = inetAddress.isLoopbackAddress() || inetAddress.isAnyLocalAddress() ? "127.0.0.1" : inetAddress.getHostAddress().toString(); this.headers = new LinkedHashMap(); } public void badClient(boolean b) { badClient = b; } public final void decodeHeader(BufferedReader in, Map pre, Map parms, Map headers) throws ResponseException { try { String inLine = in.readLine(); if (inLine == null) { return; } StringTokenizer st = new StringTokenizer(inLine); if (!st.hasMoreTokens()) { throw new ResponseException(Status.BAD_REQUEST, "BAD REQUEST: Syntax error. Usage: GET /example/file.html"); } pre.put("method", st.nextToken()); if (!st.hasMoreTokens()) { throw new ResponseException(Status.BAD_REQUEST, "BAD REQUEST: Missing URI. Usage: GET /example/file.html"); } String uri = st.nextToken(); int qmi = uri.indexOf('?'); if (qmi >= 0) { decodeParms(uri.substring(qmi + 1), parms); uri = uri.substring(0, qmi); } if (decodePercentInURI) uri = decodePercent(uri); if (st.hasMoreTokens()) { protocolVersion = st.nextToken(); } else { protocolVersion = "HTTP/1.1"; NanoHTTPD.LOG.log(Level.FINE, "no protocol version specified, strange. Assuming HTTP/1.1."); } String line = in.readLine(); while (line != null && line.trim().length() > 0) { int p = line.indexOf(':'); if (p >= 0) { headers.put(line.substring(0, p).trim().toLowerCase(Locale.US), line.substring(p + 1).trim()); } line = in.readLine(); } pre.put("uri", uri); } catch (IOException ioe) { throw new ResponseException(Status.INTERNAL_ERROR, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage(), ioe); } } public final void decodeMultipartFormData(String boundary, ByteBuffer fbuf, Map parms, Map files) throws ResponseException { try { int[] boundary_idxs = getBoundaryPositions(fbuf, boundary.getBytes()); if (boundary_idxs.length < 2) { throw new ResponseException(Status.BAD_REQUEST, "BAD REQUEST: Content type is multipart/form-data but contains less than two boundary strings."); } final int MAX_HEADER_SIZE = 1024; byte[] part_header_buff = new byte[MAX_HEADER_SIZE]; for (int bi = 0; bi < boundary_idxs.length - 1; bi++) { fbuf.position(boundary_idxs[bi]); int len = (fbuf.remaining() < MAX_HEADER_SIZE) ? fbuf.remaining() : MAX_HEADER_SIZE; fbuf.get(part_header_buff, 0, len); ByteArrayInputStream bais = new ByteArrayInputStream(part_header_buff, 0, len); BufferedReader in = new BufferedReader(new InputStreamReader(bais, Charset.forName("US-ASCII"))); String mpline = in.readLine(); if (!mpline.contains(boundary)) { throw new ResponseException(Status.BAD_REQUEST, "BAD REQUEST: Content type is multipart/form-data but chunk does not start with boundary."); } String part_name = null, file_name = null, content_type = null; mpline = in.readLine(); while (mpline != null && mpline.trim().length() > 0) { Matcher matcher = CONTENT_DISPOSITION_PATTERN.matcher(mpline); if (matcher.matches()) { String attributeString = matcher.group(2); matcher = CONTENT_DISPOSITION_ATTRIBUTE_PATTERN.matcher(attributeString); while (matcher.find()) { String key = matcher.group(1); if (key.equalsIgnoreCase("name")) { part_name = matcher.group(2); } else if (key.equalsIgnoreCase("filename")) { file_name = matcher.group(2); } } } matcher = CONTENT_TYPE_PATTERN.matcher(mpline); if (matcher.matches()) { content_type = matcher.group(2).trim(); } mpline = in.readLine(); } int part_header_len = len - (int) in.skip(MAX_HEADER_SIZE); if (part_header_len >= len - 4) { throw new ResponseException(Status.INTERNAL_ERROR, "Multipart header size exceeds MAX_HEADER_SIZE."); } int part_data_start = boundary_idxs[bi] + part_header_len; int part_data_end = boundary_idxs[bi + 1] - 4; fbuf.position(part_data_start); if (content_type == null) { byte[] data_bytes = new byte[part_data_end - part_data_start]; fbuf.get(data_bytes); parms.put(part_name, new String(data_bytes)); } else { String path = saveTmpFile(fbuf, part_data_start, part_data_end - part_data_start); if (!files.containsKey(part_name)) { files.put(part_name, path); } else { int count = 2; while (files.containsKey(part_name + count)) { count++; } files.put(part_name + count, path); } parms.put(part_name, file_name); } } } catch (ResponseException re) { throw re; } catch (Exception e) { throw new ResponseException(Status.INTERNAL_ERROR, e.toString()); } } public final void decodeParms(String parms, Map p) { if (parms == null) { this.queryParameterString = ""; return; } this.queryParameterString = parms; StringTokenizer st = new StringTokenizer(parms, "&"); while (st.hasMoreTokens()) { String e = st.nextToken(); int sep = e.indexOf('='); if (sep >= 0) { p.put(decodePercent(e.substring(0, sep)).trim(), decodePercent(e.substring(sep + 1))); } else { p.put(decodePercent(e).trim(), ""); } } } @Override public void execute() throws IOException { Response r = null; try { byte[] buf = new byte[HTTPSession.BUFSIZE]; this.splitbyte = 0; this.rlen = 0; int read = -1; try { read = this.inputStream.read(buf, 0, HTTPSession.BUFSIZE); } catch (Exception e) { safeClose(this.inputStream); safeClose(this.outputStream); throw new SocketException("NanoHttpd Shutdown"); } if (read == -1) { safeClose(this.inputStream); safeClose(this.outputStream); throw new SocketException("NanoHttpd Shutdown"); } while (read > 0) { this.rlen += read; if (rlen >= buf.length) throw fail("Header too big (" + rlen + " bytes)"); this.splitbyte = findHeaderEnd(buf, this.rlen); if (this.splitbyte > 0) { break; } read = this.inputStream.read(buf, this.rlen, HTTPSession.BUFSIZE - this.rlen); } if (this.splitbyte < this.rlen) { this.inputStream.unread(buf, this.splitbyte, this.rlen - this.splitbyte); } this.parms = new HashMap(); if (null == this.headers) { this.headers = new HashMap(); } else { this.headers.clear(); } if (null != this.remoteIp) { this.headers.put("remote-addr", this.remoteIp); this.headers.put("http-client-ip", this.remoteIp); } BufferedReader hin = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(buf, 0, this.rlen))); Map pre = new LinkedHashMap(); decodeHeader(hin, pre, this.parms, this.headers); this.method = Method.lookup(pre.get("method")); if (this.method == null) { throw new ResponseException(Status.BAD_REQUEST, "BAD REQUEST: Syntax error."); } this.uri = pre.get("uri"); this.cookies = new CookieHandler(this.headers); String connection = this.headers.get("connection"); boolean keepAlive = protocolVersion.equals("HTTP/1.1") && (connection == null || !connection.matches("(?i).*close.*")); r = serve(this); if (badClient) return; if (r == null) { throw new ResponseException(Status.INTERNAL_ERROR, "SERVER INTERNAL ERROR: Serve() returned a null response."); } else { String acceptEncoding = this.headers.get("accept-encoding"); this.cookies.unloadQueue(r); r.setRequestMethod(this.method); r.setGzipEncoding(useGzipWhenAccepted(r) && acceptEncoding != null && acceptEncoding.contains("gzip")); r.setKeepAlive(keepAlive); r.send(this.outputStream); } if (!keepAlive || "close".equalsIgnoreCase(r.getHeader("connection"))) { throw new SocketException("NanoHttpd Shutdown"); } } catch (SocketException e) { throw e; } catch (SocketTimeoutException ste) { throw ste; } catch (IOException ioe) { Response resp = newFixedLengthResponse(Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); resp.send(this.outputStream); safeClose(this.outputStream); } catch (ResponseException re) { Response resp = newFixedLengthResponse(re.getStatus(), NanoHTTPD.MIME_PLAINTEXT, re.getMessage()); resp.send(this.outputStream); safeClose(this.outputStream); } finally { if (badClient) badClients.add(this); else safeClose(r); this.tempFileManager.clear(); } } public final int findHeaderEnd(final byte[] buf, int rlen) { int splitbyte = 0; while (splitbyte + 3 < rlen) { if (buf[splitbyte] == '\r' && buf[splitbyte + 1] == '\n' && buf[splitbyte + 2] == '\r' && buf[splitbyte + 3] == '\n') { return splitbyte + 4; } splitbyte++; } return 0; } public final int[] getBoundaryPositions(ByteBuffer b, byte[] boundary) { int[] res = new int[0]; if (b.remaining() < boundary.length) { return res; } int search_window_pos = 0; byte[] search_window = new byte[4 * 1024 + boundary.length]; int first_fill = (b.remaining() < search_window.length) ? b.remaining() : search_window.length; b.get(search_window, 0, first_fill); int new_bytes = first_fill - boundary.length; do { for (int j = 0; j < new_bytes; j++) { for (int i = 0; i < boundary.length; i++) { if (search_window[j + i] != boundary[i]) break; if (i == boundary.length - 1) { int[] new_res = new int[res.length + 1]; System.arraycopy(res, 0, new_res, 0, res.length); new_res[res.length] = search_window_pos + j; res = new_res; } } } search_window_pos += new_bytes; System.arraycopy(search_window, search_window.length - boundary.length, search_window, 0, boundary.length); new_bytes = search_window.length - boundary.length; new_bytes = (b.remaining() < new_bytes) ? b.remaining() : new_bytes; b.get(search_window, boundary.length, new_bytes); } while (new_bytes > 0); return res; } @Override public CookieHandler getCookies() { return this.cookies; } @Override public final Map getHeaders() { return this.headers; } @Override public final InputStream getInputStream() { return this.inputStream; } @Override public final Method getMethod() { return this.method; } @Override public final Map getParms() { return this.parms; } @Override public String getQueryParameterString() { return this.queryParameterString; } public final RandomAccessFile getTmpBucket() { try { TempFile tempFile = this.tempFileManager.createTempFile(); return new RandomAccessFile(tempFile.getName(), "rw"); } catch (Exception e) { throw new Error(e); } } @Override public final String getUri() { return this.uri; } @Override public void parseBody(Map files) throws IOException, ResponseException { final int REQUEST_BUFFER_LEN = 512; final int MEMORY_STORE_LIMIT = 1024; RandomAccessFile randomAccessFile = null; try { long size; if (this.headers.containsKey("content-length")) { size = Integer.parseInt(this.headers.get("content-length")); } else if (this.splitbyte < this.rlen) { size = this.rlen - this.splitbyte; } else { size = 0; } ByteArrayOutputStream baos = null; DataOutput request_data_output = null; if (size < MEMORY_STORE_LIMIT) { baos = new ByteArrayOutputStream(); request_data_output = new DataOutputStream(baos); } else { randomAccessFile = getTmpBucket(); request_data_output = randomAccessFile; } byte[] buf = new byte[REQUEST_BUFFER_LEN]; while (this.rlen >= 0 && size > 0) { this.rlen = this.inputStream.read(buf, 0, (int) Math.min(size, REQUEST_BUFFER_LEN)); size -= this.rlen; if (this.rlen > 0) { request_data_output.write(buf, 0, this.rlen); } } ByteBuffer fbuf = null; if (baos != null) { fbuf = ByteBuffer.wrap(baos.toByteArray(), 0, baos.size()); } else { fbuf = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, randomAccessFile.length()); randomAccessFile.seek(0); } if (Method.POST.equals(this.method)) { String contentType = ""; String contentTypeHeader = this.headers.get("content-type"); StringTokenizer st = null; if (contentTypeHeader != null) { st = new StringTokenizer(contentTypeHeader, ",; "); if (st.hasMoreTokens()) { contentType = st.nextToken(); } } if ("multipart/form-data".equalsIgnoreCase(contentType)) { if (!st.hasMoreTokens()) { throw new ResponseException(Status.BAD_REQUEST, "BAD REQUEST: Content type is multipart/form-data but boundary missing. Usage: GET /example/file.html"); } String boundaryStartString = "boundary="; int boundaryContentStart = contentTypeHeader.indexOf(boundaryStartString) + boundaryStartString.length(); String boundary = contentTypeHeader.substring(boundaryContentStart, contentTypeHeader.length()); if (boundary.startsWith("\"") && boundary.endsWith("\"")) { boundary = boundary.substring(1, boundary.length() - 1); } decodeMultipartFormData(boundary, fbuf, this.parms, files); } else { byte[] postBytes = new byte[fbuf.remaining()]; if (NanoHTTPD_debug) print("NanoHTTPD: Handling POST data (" + l(postBytes) + " bytes)"); fbuf.get(postBytes); String postLine = new String(postBytes).trim(); if ("application/x-www-form-urlencoded".equalsIgnoreCase(contentType)) { decodeParms(postLine, this.parms); } else if (postLine.length() != 0) { files.put("postData", postLine); } } } else if (Method.PUT.equals(this.method)) { files.put("content", saveTmpFile(fbuf, 0, fbuf.limit())); } } finally { safeClose(randomAccessFile); } } public final String saveTmpFile(ByteBuffer b, int offset, int len) { String path = ""; if (len > 0) { FileOutputStream fileOutputStream = null; try { TempFile tempFile = this.tempFileManager.createTempFile(); ByteBuffer src = b.duplicate(); fileOutputStream = new FileOutputStream(tempFile.getName()); FileChannel dest = fileOutputStream.getChannel(); src.position(offset).limit(offset + len); dest.write(src.slice()); path = tempFile.getName(); } catch (Exception e) { throw new Error(e); } finally { safeClose(fileOutputStream); } } return path; } } public interface IHTTPSession { public void badClient(boolean b); public void execute() throws IOException; public CookieHandler getCookies(); public Map getHeaders(); public InputStream getInputStream(); public Method getMethod(); public Map getParms(); public String getQueryParameterString(); public String getUri(); public void parseBody(Map files) throws IOException, ResponseException; } public enum Method { GET, PUT, POST, DELETE, HEAD, OPTIONS, TRACE, CONNECT, PATCH; public static Method lookup(String method) { for (Method m : Method.values()) { if (m.toString().equalsIgnoreCase(method)) { return m; } } return null; } } public static class Response implements Closeable { public static class ChunkedOutputStream extends FilterOutputStream { public ChunkedOutputStream(OutputStream out) { super(out); } @Override public void write(int b) throws IOException { byte[] data = { (byte) b }; write(data, 0, 1); } @Override public void write(byte[] b) throws IOException { write(b, 0, b.length); } @Override public void write(byte[] b, int off, int len) throws IOException { if (len == 0) return; out.write(String.format("%x\r\n", len).getBytes()); out.write(b, off, len); out.write("\r\n".getBytes()); } public void finish() throws IOException { out.write("0\r\n\r\n".getBytes()); } } public IStatus status; public String mimeType; public InputStream data; public long contentLength; public final Map header = lithashmap("X-Powered-By", "JavaX"); public Method requestMethod; public boolean chunkedTransfer; public boolean encodeAsGzip; public boolean keepAlive; public Response(IStatus status, String mimeType, InputStream data, long totalBytes) { this.status = status; this.mimeType = mimeType; if (data == null) { this.data = new ByteArrayInputStream(new byte[0]); this.contentLength = 0L; } else { this.data = data; this.contentLength = totalBytes; } this.chunkedTransfer = this.contentLength < 0; keepAlive = true; } @Override public void close() throws IOException { if (this.data != null) { this.data.close(); } } public void addHeader(String name, String value) { this.header.put(name, value); } public InputStream getData() { return this.data; } public String getHeader(String name) { for (String headerName : header.keySet()) { if (headerName.equalsIgnoreCase(name)) { return header.get(headerName); } } return null; } public String getMimeType() { return this.mimeType; } public Method getRequestMethod() { return this.requestMethod; } public IStatus getStatus() { return this.status; } public void setGzipEncoding(boolean encodeAsGzip) { this.encodeAsGzip = encodeAsGzip; } public void setKeepAlive(boolean useKeepAlive) { this.keepAlive = useKeepAlive; } public final boolean headerAlreadySent(Map header, String name) { boolean alreadySent = false; for (String headerName : header.keySet()) { alreadySent |= headerName.equalsIgnoreCase(name); } return alreadySent; } public void send(OutputStream outputStream) { String mime = this.mimeType; SimpleDateFormat gmtFrmt = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss 'GMT'", Locale.US); gmtFrmt.setTimeZone(TimeZone.getTimeZone("GMT")); try { if (this.status == null) { throw new Error("sendResponse(): Status can't be null."); } PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")), false); pw.print("HTTP/1.1 " + this.status.getDescription() + " \r\n"); if (mime != null) { pw.print("Content-Type: " + mime + "\r\n"); } if (this.header == null || this.header.get("Date") == null) { pw.print("Date: " + gmtFrmt.format(new Date()) + "\r\n"); } if (this.header != null) { for (String key : this.header.keySet()) { String value = this.header.get(key); pw.print(key + ": " + value + "\r\n"); } } if (!headerAlreadySent(header, "connection")) { pw.print("Connection: " + (this.keepAlive ? "keep-alive" : "close") + "\r\n"); } if (headerAlreadySent(this.header, "content-length")) { encodeAsGzip = false; } if (encodeAsGzip) { pw.print("Content-Encoding: gzip\r\n"); setChunkedTransfer(true); } long pending = this.data != null ? this.contentLength : 0; if (this.requestMethod != Method.HEAD && this.chunkedTransfer) { pw.print("Transfer-Encoding: chunked\r\n"); } else if (!encodeAsGzip) { pending = sendContentLengthHeaderIfNotAlreadyPresent(pw, this.header, pending); } pw.print("\r\n"); pw.flush(); sendBodyWithCorrectTransferAndEncoding(outputStream, pending); outputStream.flush(); safeClose(this.data); } catch (IOException ioe) { NanoHTTPD.LOG.log(Level.SEVERE, "Could not send response to the client", ioe); } } public final void sendBodyWithCorrectTransferAndEncoding(OutputStream outputStream, long pending) throws IOException { if (this.requestMethod != Method.HEAD && this.chunkedTransfer) { ChunkedOutputStream chunkedOutputStream = new ChunkedOutputStream(outputStream); sendBodyWithCorrectEncoding(chunkedOutputStream, -1); chunkedOutputStream.finish(); } else { sendBodyWithCorrectEncoding(outputStream, pending); } } public final void sendBodyWithCorrectEncoding(OutputStream outputStream, long pending) throws IOException { if (encodeAsGzip) { GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream); sendBody(gzipOutputStream, -1); gzipOutputStream.finish(); } else { sendBody(outputStream, pending); } } public final void sendBody(OutputStream outputStream, long pending) throws IOException { long BUFFER_SIZE = 16 * 1024; byte[] buff = new byte[(int) BUFFER_SIZE]; boolean sendEverything = pending == -1; while (pending > 0 || sendEverything) { long bytesToRead = sendEverything ? BUFFER_SIZE : Math.min(pending, BUFFER_SIZE); int read = this.data.read(buff, 0, (int) bytesToRead); if (read <= 0) { break; } outputStream.write(buff, 0, read); if (!sendEverything) { pending -= read; } } } public long sendContentLengthHeaderIfNotAlreadyPresent(PrintWriter pw, Map header, long size) { for (String headerName : header.keySet()) { if (headerName.equalsIgnoreCase("content-length")) { try { return Long.parseLong(header.get(headerName)); } catch (NumberFormatException ex) { return size; } } } pw.print("Content-Length: " + size + "\r\n"); return size; } public void setChunkedTransfer(boolean chunkedTransfer) { this.chunkedTransfer = chunkedTransfer; } public void setData(InputStream data) { this.data = data; } public void setMimeType(String mimeType) { this.mimeType = mimeType; } public void setRequestMethod(Method requestMethod) { this.requestMethod = requestMethod; } public void setStatus(IStatus status) { this.status = status; } } public static final class ResponseException extends Exception { public static final long serialVersionUID = 6569838532917408380L; public final Status status; public ResponseException(Status status, String message) { super(message); this.status = status; } public ResponseException(Status status, String message, Exception e) { super(message, e); this.status = status; } public Status getStatus() { return this.status; } } public class ServerRunnable implements Runnable { public final int timeout; public IOException bindException; public boolean hasBinded = false; public ServerRunnable(int timeout) { this.timeout = timeout; } @Override public void run() { try { myServerSocket.bind(hostname != null ? new InetSocketAddress(hostname, myPort) : new InetSocketAddress(myPort)); hasBinded = true; } catch (IOException e) { this.bindException = e; return; } do { try { final Socket finalAccept = NanoHTTPD.this.myServerSocket.accept(); if (NanoHTTPD_debug) print("NanoHTTPD: New socket."); if (this.timeout > 0) { finalAccept.setSoTimeout(this.timeout); } final InputStream inputStream = wrapStuff("SocketInputStream", finalAccept.getInputStream(), finalAccept, NanoHTTPD.this.myServerSocket); NanoHTTPD.this.asyncRunner.exec(createClientHandler(finalAccept, inputStream)); } catch (IOException e) { NanoHTTPD.LOG.log(Level.FINE, "Communication with the client broken", e); } } while (!NanoHTTPD.this.myServerSocket.isClosed()); } } public interface TempFile { public void delete() throws Exception; public String getName(); public OutputStream open() throws Exception; } public interface TempFileManager { public void clear(); public TempFile createTempFile() throws Exception; } public interface TempFileManagerFactory { public TempFileManager create(); } public static int SOCKET_READ_TIMEOUT = 24 * 3600 * 1000; public static final String MIME_PLAINTEXT = "text/plain; charset=utf-8"; public static final String MIME_HTML = "text/html; charset=utf-8"; public static final String QUERY_STRING_PARAMETER = "NanoHttpd.QUERY_STRING"; public static final Logger LOG = Logger.getLogger(NanoHTTPD.class.getName()); public static SSLServerSocketFactory makeSSLSocketFactory(KeyStore loadedKeyStore, KeyManager[] keyManagers) throws IOException { SSLServerSocketFactory res = null; try { TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(loadedKeyStore); SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(keyManagers, trustManagerFactory.getTrustManagers(), null); res = ctx.getServerSocketFactory(); } catch (Exception e) { throw new IOException(e.getMessage()); } return res; } public static SSLServerSocketFactory makeSSLSocketFactory(KeyStore loadedKeyStore, KeyManagerFactory loadedKeyFactory) throws IOException { SSLServerSocketFactory res = null; try { TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(loadedKeyStore); SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(loadedKeyFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null); res = ctx.getServerSocketFactory(); } catch (Exception e) { throw new IOException(e.getMessage()); } return res; } public static SSLServerSocketFactory makeSSLSocketFactory(String keyAndTrustStoreClasspathPath, char[] passphrase) throws IOException { SSLServerSocketFactory res = null; try { KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); InputStream keystoreStream = NanoHTTPD.class.getResourceAsStream(keyAndTrustStoreClasspathPath); keystore.load(keystoreStream, passphrase); TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(keystore); KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(keystore, passphrase); SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null); res = ctx.getServerSocketFactory(); } catch (Exception e) { throw new IOException(e.getMessage()); } return res; } public static final void safeClose(Object closeable) { try { if (closeable != null) { if (closeable instanceof Closeable) { ((Closeable) closeable).close(); } else if (closeable instanceof Socket) { ((Socket) closeable).close(); } else if (closeable instanceof ServerSocket) { ((ServerSocket) closeable).close(); } else { throw new IllegalArgumentException("Unknown object to close"); } } } catch (IOException e) { NanoHTTPD.LOG.log(Level.SEVERE, "Could not close", e); } } public final String hostname; public final int myPort; public ServerSocket myServerSocket; public SSLServerSocketFactory sslServerSocketFactory; public Thread myThread; public AsyncRunner asyncRunner; public TempFileManagerFactory tempFileManagerFactory; public NanoHTTPD(int port) { this(null, port); } public NanoHTTPD(String hostname, int port) { this.hostname = hostname; this.myPort = port; setTempFileManagerFactory(new DefaultTempFileManagerFactory()); setAsyncRunner(new DefaultAsyncRunner()); } public synchronized void closeAllConnections() { stop(); } public ClientHandler createClientHandler(final Socket finalAccept, final InputStream inputStream) { return new ClientHandler(inputStream, finalAccept); } public ServerRunnable createServerRunnable(final int timeout) { return new ServerRunnable(timeout); } public Map> decodeParameters(Map parms) { return this.decodeParameters(parms.get(NanoHTTPD.QUERY_STRING_PARAMETER)); } public Map> decodeParameters(String queryString) { Map> parms = new HashMap>(); if (queryString != null) { StringTokenizer st = new StringTokenizer(queryString, "&"); while (st.hasMoreTokens()) { String e = st.nextToken(); int sep = e.indexOf('='); String propertyName = sep >= 0 ? decodePercent(e.substring(0, sep)).trim() : decodePercent(e).trim(); if (!parms.containsKey(propertyName)) { parms.put(propertyName, new ArrayList()); } String propertyValue = sep >= 0 ? decodePercent(e.substring(sep + 1)) : null; if (propertyValue != null) { parms.get(propertyName).add(propertyValue); } } } return parms; } public String decodePercent(String str) { String decoded = null; try { decoded = URLDecoder.decode(str, "UTF8"); } catch (UnsupportedEncodingException ignored) { NanoHTTPD.LOG.log(Level.WARNING, "Encoding not supported, ignored", ignored); } return decoded; } public boolean useGzipWhenAccepted(Response r) { return r.getMimeType() != null && r.getMimeType().toLowerCase().contains("text/"); } public final int getListeningPort() { return this.myServerSocket == null ? -1 : this.myServerSocket.getLocalPort(); } public final boolean isAlive() { return wasStarted() && !this.myServerSocket.isClosed() && this.myThread.isAlive(); } public void join() throws InterruptedException { myThread.join(); } public void makeSecure(SSLServerSocketFactory sslServerSocketFactory) { this.sslServerSocketFactory = sslServerSocketFactory; } public Response newChunkedResponse(IStatus status, String mimeType, InputStream data) { return new Response(status, mimeType, data, -1); } public static Response newFixedLengthResponse(IStatus status, String mimeType, InputStream data, long totalBytes) { return new Response(status, mimeType, data, totalBytes); } public static Response newFixedLengthResponse(IStatus status, String mimeType, String txt) { if (txt == null) { return newFixedLengthResponse(status, mimeType, new ByteArrayInputStream(new byte[0]), 0); } else { byte[] bytes; try { bytes = txt.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { NanoHTTPD.LOG.log(Level.SEVERE, "encoding problem, responding nothing", e); bytes = new byte[0]; } return newFixedLengthResponse(status, mimeType, new ByteArrayInputStream(bytes), bytes.length); } } public static Response newFixedLengthResponse(String msg) { return newFixedLengthResponse(Status.OK, NanoHTTPD.MIME_HTML, msg); } public Response serve(IHTTPSession session) { currentSession.set(session); Map files = new HashMap(); Method method = session.getMethod(); if (Method.PUT.equals(method) || Method.POST.equals(method)) { try { session.parseBody(files); } catch (IOException ioe) { return newFixedLengthResponse(Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); } catch (ResponseException re) { return newFixedLengthResponse(re.getStatus(), NanoHTTPD.MIME_PLAINTEXT, re.getMessage()); } } Map parms = session.getParms(); parms.put(NanoHTTPD.QUERY_STRING_PARAMETER, session.getQueryParameterString()); return serve(session.getUri(), method, session.getHeaders(), parms, files); } @Deprecated public Response serve(String uri, Method method, Map headers, Map parms, Map files) { return newFixedLengthResponse(Status.NOT_FOUND, NanoHTTPD.MIME_PLAINTEXT, "Not Found"); } public void setAsyncRunner(AsyncRunner asyncRunner) { this.asyncRunner = asyncRunner; } public void setTempFileManagerFactory(TempFileManagerFactory tempFileManagerFactory) { this.tempFileManagerFactory = tempFileManagerFactory; } public void start() throws IOException { start(NanoHTTPD.SOCKET_READ_TIMEOUT); } public void start(final int timeout) throws IOException { boolean ssl = this.sslServerSocketFactory != null; if (ssl) { SSLServerSocket ss = (SSLServerSocket) this.sslServerSocketFactory.createServerSocket(); ss.setNeedClientAuth(false); this.myServerSocket = ss; } else { this.myServerSocket = new ServerSocket(); } this.myServerSocket.setReuseAddress(true); ServerRunnable serverRunnable = createServerRunnable(timeout); this.myThread = new Thread(serverRunnable); this.myThread.setName("NanoHttpd Main Listener"); this.myThread.start(); while (!serverRunnable.hasBinded && serverRunnable.bindException == null) { try { Thread.sleep(10L); } catch (Throwable e) { } } if (serverRunnable.bindException != null) { throw serverRunnable.bindException; } System.out.println("HTTP" + (ssl ? "S" : "") + " server started (listening on port " + getListeningPort() + "!)"); printMyIPs(); } public void stop() { try { safeClose(this.myServerSocket); this.asyncRunner.closeAll(); if (this.myThread != null) { this.myThread.join(); } } catch (Exception e) { NanoHTTPD.LOG.log(Level.SEVERE, "Could not stop all connections", e); } } public final boolean wasStarted() { return this.myServerSocket != null && this.myThread != null; } public static void printMyIPs() { String ip; try { Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); if (iface.isLoopback() || !iface.isUp()) continue; Enumeration addresses = iface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); ip = addr.getHostAddress(); if (ip.startsWith("127.")) continue; boolean local = addr.isSiteLocalAddress() || ip.startsWith("fe"); System.out.println(iface.getDisplayName() + " " + ip + " " + (local ? "(private address)" : "(public address)")); } } } catch (Throwable e) { e.printStackTrace(); } } public static interface IStatus { public String getDescription(); public int getRequestStatus(); } static enum Status implements IStatus { SWITCH_PROTOCOL(101, "Switching Protocols"), OK(200, "OK"), CREATED(201, "Created"), ACCEPTED(202, "Accepted"), NO_CONTENT(204, "No Content"), PARTIAL_CONTENT(206, "Partial Content"), REDIRECT(301, "Moved Permanently"), NOT_MODIFIED(304, "Not Modified"), BAD_REQUEST(400, "Bad Request"), UNAUTHORIZED(401, "Unauthorized"), FORBIDDEN(403, "Forbidden"), NOT_FOUND(404, "Not Found"), METHOD_NOT_ALLOWED(405, "Method Not Allowed"), REQUEST_TIMEOUT(408, "Request Timeout"), RANGE_NOT_SATISFIABLE(416, "Requested Range Not Satisfiable"), INTERNAL_ERROR(500, "Internal Server Error"), UNSUPPORTED_HTTP_VERSION(505, "HTTP Version Not Supported"); public final int requestStatus; public final String description; Status(int requestStatus, String description) { this.requestStatus = requestStatus; this.description = description; } @Override public String getDescription() { return "" + this.requestStatus + " " + this.description; } @Override public int getRequestStatus() { return this.requestStatus; } } } public static class UnsuitableRuleException extends RuntimeException { } public static class MultiMap { public Map> data = new HashMap>(); public MultiMap() { } public MultiMap(boolean useTreeMap) { if (useTreeMap) data = new TreeMap(); } public MultiMap(MultiMap map) { putAll(map); } public MultiMap(Map> data) { this.data = data; } public void put(A key, B value) { synchronized (data) { List list = data.get(key); if (list == null) data.put(key, list = _makeEmptyList()); list.add(value); } } public void addAll(A key, Collection values) { synchronized (data) { putAll(key, values); } } public void addAllIfNotThere(A key, Collection values) { synchronized (data) { for (B value : values) setPut(key, value); } } public void setPut(A key, B value) { synchronized (data) { if (!containsPair(key, value)) put(key, value); } } public boolean containsPair(A key, B value) { synchronized (data) { return get(key).contains(value); } } public void putAll(A key, Collection values) { synchronized (data) { for (B value : values) put(key, value); } } public void removeAll(A key, Collection values) { synchronized (data) { for (B value : values) remove(key, value); } } public List get(A key) { synchronized (data) { List list = data.get(key); return list == null ? Collections.emptyList() : list; } } public List getActual(A key) { synchronized (data) { List list = data.get(key); if (list == null) data.put(key, list = _makeEmptyList()); return list; } } public void clean(A key) { synchronized (data) { List list = data.get(key); if (list != null && list.isEmpty()) data.remove(key); } } public Set keySet() { synchronized (data) { return data.keySet(); } } public Set keys() { synchronized (data) { return data.keySet(); } } public void remove(A key) { synchronized (data) { data.remove(key); } } public void remove(A key, B value) { synchronized (data) { List list = data.get(key); if (list != null) { list.remove(value); if (list.isEmpty()) data.remove(key); } } } public void clear() { synchronized (data) { data.clear(); } } public boolean containsKey(A key) { synchronized (data) { return data.containsKey(key); } } public B getFirst(A key) { synchronized (data) { List list = get(key); return list.isEmpty() ? null : list.get(0); } } public void putAll(MultiMap map) { synchronized (data) { for (A key : map.keySet()) putAll(key, map.get(key)); } } public int keysSize() { synchronized (data) { return l(data); } } public int size() { synchronized (data) { int n = 0; for (List l : data.values()) n += l(l); return n; } } public List reverseGet(B b) { synchronized (data) { List l = new ArrayList(); for (A key : data.keySet()) if (data.get(key).contains(b)) l.add(key); return l; } } public Map> asMap() { synchronized (data) { return cloneMap(data); } } public boolean isEmpty() { synchronized (data) { return data.isEmpty(); } } public List _makeEmptyList() { return new ArrayList(); } public Collection> allLists() { synchronized (data) { return new ArrayList(data.values()); } } } public static class SoftwareMadeWeb { public long date; public String computerID, programID; public String structure; public Web web; public Map furtherInfo; } public static class BetterThreadLocal { public Map map = newWeakHashMap(); public A get() { return map.get(currentThread()); } public A get(Thread thread) { return map.get(thread); } public void set(A a) { mapPutOrRemove(map, currentThread(), a); } } public static class Lowest { public A best; public double score; public transient Object onChange; public boolean isNewBest(double score) { return best == null || score < this.score; } public double bestScore() { return best == null ? Double.NaN : score; } public float floatScore() { return best == null ? Float.NaN : (float) score; } public float floatScoreOr(float defaultValue) { return best == null ? defaultValue : (float) score; } public boolean put(A a, double score) { if (a != null && isNewBest(score)) { best = a; this.score = score; pcallF(onChange); return true; } return false; } public A get() { return best; } public boolean has() { return best != null; } } public static class InterruptibleString implements CharSequence { public String inner; public InterruptibleString() { } public InterruptibleString(String inner) { this.inner = inner; } @Override public char charAt(int index) { ping(); return inner.charAt(index); } @Override public int length() { return inner.length(); } @Override public CharSequence subSequence(int start, int end) { return new InterruptibleString(inner.substring(start, end)); } @Override public String toString() { return inner.toString(); } } public static class Either { public byte which; public Object value; public Either() { } public Either(int which, Object value) { this.value = value; this.which = (byte) which; } public boolean isA() { return which == 1; } public boolean isB() { return which == 2; } public A a() { if (which != 1) _failMe(); return (A) value; } public B b() { if (which != 2) _failMe(); return (B) value; } public void _failMe() { throw fail("Either object is of wrong type: " + shortClassName(value)); } } public static class MultiSet { public Map map = new HashMap(); public MultiSet(boolean useTreeMap) { if (useTreeMap) map = new TreeMap(); } public MultiSet() { } public MultiSet(Iterable c) { addAll(c); } public MultiSet(MultiSet ms) { synchronized (ms) { for (A a : ms.keySet()) add(a, ms.get(a)); } } public synchronized void add(A key) { add(key, 1); } public synchronized void addAll(Iterable c) { if (c != null) for (A a : c) add(a); } public synchronized void addAll(MultiSet ms) { for (A a : ms.keySet()) add(a, ms.get(a)); } public synchronized void add(A key, int count) { if (map.containsKey(key)) map.put(key, map.get(key) + count); else map.put(key, count); } public synchronized int get(A key) { Integer i = map.get(key); return i != null ? i : 0; } public synchronized boolean contains(A key) { return map.containsKey(key); } public synchronized void remove(A key) { Integer i = map.get(key); if (i != null && i > 1) map.put(key, i - 1); else map.remove(key); } public synchronized List topTen() { return getTopTen(); } public synchronized List getTopTen() { return getTopTen(10); } public synchronized List getTopTen(int maxSize) { List list = getSortedListDescending(); return list.size() > maxSize ? list.subList(0, maxSize) : list; } public synchronized List highestFirst() { return getSortedListDescending(); } public synchronized List lowestFirst() { return reversedList(getSortedListDescending()); } public synchronized List getSortedListDescending() { List list = new ArrayList(map.keySet()); Collections.sort(list, new Comparator() { public int compare(A a, A b) { return map.get(b).compareTo(map.get(a)); } }); return list; } public synchronized int getNumberOfUniqueElements() { return map.size(); } public synchronized int uniqueSize() { return map.size(); } public synchronized Set asSet() { return map.keySet(); } public synchronized NavigableSet navigableSet() { return navigableKeys((NavigableMap) map); } public synchronized Set keySet() { return map.keySet(); } public synchronized A getMostPopularEntry() { int max = 0; A a = null; for (Map.Entry entry : map.entrySet()) { if (entry.getValue() > max) { max = entry.getValue(); a = entry.getKey(); } } return a; } public synchronized void removeAll(A key) { map.remove(key); } public synchronized int size() { int size = 0; for (int i : map.values()) size += i; return size; } public synchronized MultiSet mergeWith(MultiSet set) { MultiSet result = new MultiSet(); for (A a : set.asSet()) { result.add(a, set.get(a)); } return result; } public synchronized boolean isEmpty() { return map.isEmpty(); } public synchronized String toString() { return str(map); } public synchronized void clear() { map.clear(); } public synchronized Map asMap() { return cloneMap(map); } } public static class FunctionIsBroken extends RuntimeException { public String fname; public FunctionIsBroken() { } public FunctionIsBroken(String fname) { this.fname = fname; } public String toString() { return "FunctionIsBroken: " + fname; } } public abstract static class TripleRef { public T3 triple; public TripleRef() { } public TripleRef(T3 triple) { this.triple = triple; } public A get() { return tripleAtPosition(triple(), position()); } public T3 triple() { return triple; } public abstract int position(); public String toString() { return position() + "@" + triple; } } public static class AI_SubSpace implements AI_PostHandler, AI_CacheChecker { public int limit; public MultiMap index = new MultiMap(); public HashMap websByID = new HashMap(); public List webs = new ArrayList(); public boolean changed; public AI_SubSpace() { } public AI_SubSpace(int limit) { this.limit = limit; } public GlobalID postTriple(T3 t) { if (limitReached()) throw fail("Subspace limit reached"); Web web = webFromTriple(t); web.source = ai_currentMaker(); addWeb(web); return web.globalIDObj(); } public GlobalID postTriple(T3 t, boolean verified) { return postTriple(t); } public void addWeb(Web web) { websByID.put(web.globalIDObj(), web); webs.add(web); changed = true; for (WebNode node : web_nodesAndRelations(web)) for (String text : asSet(node.texts())) index.put(symbol(ai_shortenForIndex(text)), node); } public boolean hasTriple(String a, String b, String c) { List webs = ai_withoutInvalidWebs(nodesToWebs(shortestList3(index.get(a), index.get(b), index.get(c)))); return webs_search_noVar_bool(webFromTriple(a, b, c), webs); } public boolean limitReached() { return limit > 0 && l(websByID) >= limit; } public List indexedNodes_rawLookup(String s) { return index.get(ai_shortenForIndex(s)); } public int numWebs() { return l(websByID); } } public static ThreadLocal> WebSocketHTTPD_headers = new ThreadLocal(); public static ThreadLocal WebSocketHTTPD_current = new ThreadLocal(); public static class WebSocketHTTPD extends NanoWebSocketServer { public WebSocketHTTPD(int port, Object factory) { super(port, webSocketFactory(factory)); nanohttpd_socketTimeoutFix(); } public Object onServe, onEndServe; public volatile long requests; public boolean printServes = true; public static class Serving { public String uri; public Map header, parms, files; } public List currentlyServing = synchroList(); public Response serve(String uri, Method method, Map header, Map parms, Map files) { try { ++requests; AutoCloseable __4454 = tempSetThreadLocal(WebSocketHTTPD_current, this); try { Serving serving = nu(Serving.class, "uri", uri, "header", header, "parms", parms, "files", files); currentlyServing.add(serving); pcallF(onServe, serving); try { String remoteAddr = getClientIPFromHeaders(header); if (printServes) print(formatDateAndTime() + " Serving URI: " + quote(header.get("host") + uri) + " to: " + remoteAddr); try { Response response = (Response) (callOpt(getMainClass(), "serve", uri, method, header, parms, files)); if (response != null) return response; Object html; WebSocketHTTPD_headers.set(header); try { html = callHtmlMethod2(getMainClass(), uri, parms); } finally { WebSocketHTTPD_headers.set(null); } if (html != null) return html instanceof String ? serveHTML((String) html) : (NanoHTTPD.Response) html; return serve404(); } catch (Throwable e) { printStackTrace(e); return serveHTML("ERROR."); } } finally { pcallF(onEndServe, serving); currentlyServing.remove(serving); } } finally { _close(__4454); } } catch (Exception __e) { throw rethrow(__e); } } } public static class CompilerBot { public static boolean verbose; public static File compileSnippet(String snippetID) { return compileSnippet(snippetID, ""); } public static Pair compileSnippet2(String snippetID) { return compileSnippet2(snippetID, ""); } public static File compileSnippet(String snippetID, String javaTarget) { return compileSnippet2(snippetID, javaTarget).a; } public static Pair compileSnippet2(String snippetID, String javaTarget) { String transpiledSrc = getServerTranspiled2(snippetID); int i = transpiledSrc.indexOf('\n'); String libs = transpiledSrc.substring(0, Math.max(0, i)); if (verbose) print("Compiling snippet: " + snippetID + ". Libs: " + libs); transpiledSrc = transpiledSrc.substring(i + 1); return pair(compile(transpiledSrc, libs, javaTarget, snippetID), transpiledSrc); } public static File compile(String src) { return compile(src, ""); } public static File compile(String src, String libs) { return compile(src, libs, null); } public static File compile(String src, String dehlibs, String javaTarget) { return compile(src, dehlibs, javaTarget, null); } public static File compile(String src, String dehlibs, String javaTarget, String progID) { if (verbose) print("Compiling " + l(src) + " chars"); String md5 = md5(dehlibs + "\n" + src + "\n" + progID); File jar = getJarFile(md5); if (jar == null || jar.length() <= 22) { boolean canRename = useDummyMainClasses() && isSnippetID(progID) && !tok_classHasModifier(findMainClass(javaTok(src)), "public"); if (verbose) print("useRenaming: " + useDummyMainClasses() + ", canRename: " + canRename + ", progID: " + progID); javaCompileToJar_optionalRename(src, dehlibs, jar, canRename ? progID : null); } else { if (verbose) print("Getting classes from cache (" + jar.getAbsolutePath() + ", " + jar.length() + " bytes)"); touchFile(jar); } return jar; } public static File getJarFile(String md5) { assertTrue(isMD5(md5)); return new File(getCacheProgramDir("#1002203"), md5 + ".jar"); } } public static class T3 { public A a; public B b; public C c; public T3() { } public T3(A a, B b, C c) { this.c = c; this.b = b; this.a = a; } public T3(T3 t) { a = t.a; b = t.b; c = t.c; } public int hashCode() { return _hashCode(a) + 2 * _hashCode(b) - 4 * _hashCode(c); } public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof T3)) return false; T3 t = (T3) o; return eq(a, t.a) && eq(b, t.b) && eq(c, t.c); } public String toString() { return "(" + quoteBorderless(a) + ", " + quoteBorderless(b) + ", " + quoteBorderless(c) + ")"; } } public static class Pair implements Comparable> { public A a; public B b; public Pair() { } public Pair(A a, B b) { this.b = b; this.a = a; } public int hashCode() { return hashCodeFor(a) + 2 * hashCodeFor(b); } public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof Pair)) return false; Pair t = (Pair) o; return eq(a, t.a) && eq(b, t.b); } public String toString() { return "<" + a + ", " + b + ">"; } public int compareTo(Pair p) { if (p == null) return 1; int i = ((Comparable) a).compareTo(p.a); if (i != 0) return i; return ((Comparable) b).compareTo(p.b); } } public static interface AI_PostHandler { public GlobalID postTriple(T3 triple); public GlobalID postTriple(T3 triple, boolean verified); } public static class JavaXClassLoader extends URLClassLoader { public String progID; public List files; public Set loadedClasses = synchroSet(); public boolean retired; public JavaXClassLoader(String progID, List files) { this(progID, files, getSystemClassLoader()); } public JavaXClassLoader(String progID, List files, ClassLoader parent) { super(new URL[0], parent); this.progID = progID; this.files = files; { try { for (File f : files) addURL(f.toURI().toURL()); } catch (Exception __e) { throw rethrow(__e); } } } public Class findClass(String name) throws ClassNotFoundException { try { Class c = super.findClass(name); loadedClasses.add(c); if (eq(name, "main")) callOpt(javax(), "registerAMainClass", c); return c; } catch (ClassNotFoundException e) { throw new ClassNotFoundException("Class " + name + " not found in " + joinWithComma(map("f2s", files)) + " (progID=" + progID + ")"); } } public String toString() { return "JavaXClassLoader[" + systemHashCodeHex(this) + "] - " + progID; } } public abstract static class DialogIO { public String line; public boolean eos, loud, noClose; public Lock lock = lock(); public abstract String readLineImpl(); public abstract boolean isStillConnected(); public abstract void sendLine(String line); public abstract boolean isLocalConnection(); public abstract Socket getSocket(); public abstract void close(); public int getPort() { Socket s = getSocket(); return s == null ? 0 : s.getPort(); } public boolean helloRead; public int shortenOutputTo = 500; public String readLineNoBlock() { String l = line; line = null; return l; } public boolean waitForLine() { try { ping(); if (line != null) return true; line = readLineImpl(); if (line == null) eos = true; return line != null; } catch (Exception __e) { throw rethrow(__e); } } public String readLine() { waitForLine(); helloRead = true; return readLineNoBlock(); } public String ask(String s, Object... args) { if (loud) return askLoudly(s, args); if (!helloRead) readLine(); if (args.length != 0) s = format3(s, args); sendLine(s); return readLine(); } public String askLoudly(String s, Object... args) { if (!helloRead) readLine(); if (args.length != 0) s = format3(s, args); print("> " + shorten(s, shortenOutputTo)); sendLine(s); String answer = readLine(); print("< " + shorten(answer, shortenOutputTo)); return answer; } public void pushback(String l) { if (line != null) throw fail(); line = l; helloRead = false; } } public abstract static class DialogHandler { public abstract void run(DialogIO io); } public static class JavaXClassLoaderWithParent extends JavaXClassLoader { public ClassLoader virtualParent; public JavaXClassLoaderWithParent(String progID, List files, ClassLoader virtualParent) { super(progID, files); this.virtualParent = virtualParent; } public Class findClass(String name) throws ClassNotFoundException { if (!eq(name, "main") && !name.startsWith("main$")) { try { return virtualParent.loadClass(name); } catch (ClassNotFoundException e) { } } return super.findClass(name); } } public static final class SmallestListMultiMap { public Map data = new HashMap(); public SmallestListMultiMap() { } public SmallestListMultiMap(boolean useTreeMap) { if (useTreeMap) data = new TreeMap(); } public SmallestListMultiMap(MultiMap map) { putAll(map); } public void put(A key, B value) { synchronized (data) { Object list = data.get(key); data.put(key, smallestList_add(list, value)); } } public void addAll(A key, Collection values) { synchronized (data) { putAll(key, values); } } public void addAllIfNotThere(A key, Collection values) { synchronized (data) { for (B value : values) setPut(key, value); } } public void setPut(A key, B value) { synchronized (data) { if (!containsPair(key, value)) put(key, value); } } public boolean containsPair(A key, B value) { synchronized (data) { return smallestList_contains(get(key), value); } } public void putAll(A key, Collection values) { synchronized (data) { for (B value : values) put(key, value); } } public void removeAll(A key, Collection values) { synchronized (data) { for (B value : values) remove(key, value); } } public List get(A key) { synchronized (data) { return smallestList_toList(data.get(key)); } } public Set keySet() { synchronized (data) { return data.keySet(); } } public Set keys() { synchronized (data) { return data.keySet(); } } public void remove(A key) { synchronized (data) { data.remove(key); } } public void remove(A key, B value) { synchronized (data) { Object list = data.get(key); if (list != null) { Object list2 = smallestList_remove(list, value); if (list2 != list) if (list2 == null) data.remove(key); else data.put(key, list2); } } } public void clear() { synchronized (data) { data.clear(); } } public boolean containsKey(A key) { synchronized (data) { return data.containsKey(key); } } public B getFirst(A key) { synchronized (data) { return (B) smallestList_get(data.get(key), 0); } } public void putAll(MultiMap map) { synchronized (data) { for (A key : map.keySet()) putAll(key, map.get(key)); } } public int keysSize() { synchronized (data) { return l(data); } } public int size() { synchronized (data) { int n = 0; for (Object l : data.values()) n += smallestList_l(l); return n; } } public List reverseGet(B b) { synchronized (data) { List l = new ArrayList(); for (A key : data.keySet()) if (smallestList_contains(data.get(key), b)) l.add(key); return l; } } public Map> asMap() { synchronized (data) { HashMap> map = new HashMap(); for (A key : data.keySet()) map.put(key, smallestList_toList(data.get(key))); return map; } } public boolean isEmpty() { synchronized (data) { return data.isEmpty(); } } } public static class BetterLabel extends JLabel { public boolean autoToolTip = true; public BetterLabel() { final WeakReference me = new WeakReference(this); componentPopupMenu(this, BetterLabel_menuItems(me)); } public BetterLabel(String text) { this(); setText(text); } public void setText(String text) { super.setText(text); if (autoToolTip) if (!swic(text, "")) setToolTipText(nullIfEmpty(text)); } } public static VF1 BetterLabel_menuItems(final WeakReference me) { return new VF1() { public void get(JPopupMenu menu) { try { addMenuItem(menu, "Copy text to clipboard", new Runnable() { public void run() { try { copyTextToClipboard(me.get().getText()); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "copyTextToClipboard(me.get().getText());"; } }); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "addMenuItem(menu, \"Copy text to clipboard\", r {\r\n copyTextToClipboard(me..."; } }; } public static class MRUCache extends LinkedHashMap { public int maxSize = 10; public MRUCache() { } public MRUCache(int maxSize) { this.maxSize = maxSize; } public boolean removeEldestEntry(Map.Entry eldest) { return size() > maxSize; } } public static class TripleIndex extends VirtualNodeIndex { public static boolean useExactIndex; public boolean activated; public SyncListMultiMap index; public MultiSet wordIndex = caseInsensitiveMultiSet(); public SyncListMultiMap[] positionalIndices = new SyncListMultiMap[] { symbolSyncListMultiMap(), symbolSyncListMultiMap(), symbolSyncListMultiMap() }; public ExactTripleIndex exactIndex = useExactIndex ? exactTripleIndex() : null; public CompactHashSet websByID = new CompactHashSet(); public Map> oneTwoIndex; public SyncListMultiMap, TripleWeb> oneTwoIndex2 = new SyncListMultiMap(new CompactPairKeyHashMap()); public int size() { return numWebs(); } public int numWebs() { return l(websByID); } public int numTerms() { return index != null ? index.keysSize() : wordIndex.uniqueSize(); } public Set mainKeys() { return index != null ? keys(index) : wordIndex.asSet(); } public NavigableSet navigableMainKeys() { return index != null ? navigableKeys(index) : wordIndex.navigableSet(); } public Map mainIndexMap() { return index != null ? index.data : wordIndex.map; } public Collection indexedTerms() { return mainKeys(); } public List get(CharSequence _query) { String query = symbol(_query); List triples = index != null ? index.get(query) : combineLists3(getTriples(query, 0), getTriples(query, 1), getTriples(query, 2)); return ai_triplesToWebNodes_lazyList(query, triples); } public List> getTripleRefs(String query) { if (index == null) throw fail("no index"); return ai_triplesToTripleRefs_lazyList(query, index.get(query)); } public List> getTripleRefs(String query, int position) { return ai_triplesToTripleRefs_lazyList(query, getTriples(query, position)); } public List getTriples(String query) { if (index == null) throw fail("no index"); return index.get(query); } public List getTriples(String query, int position) { return positionalIndices[position].get(query); } public boolean hasShortTerm(String s) { return index != null ? index.containsKey(s) : wordIndex.contains(s); } public Web getWeb(GlobalID id) { return webFromTriple(getTriple(id)); } public TripleWeb getTriple(GlobalID id) { return websByID.find(dummyTripleWebWithGlobalID(id)); } public void addWeb(Web web) { if (web == null) return; TripleWeb w = ai_webToTripleWeb(web); if (w == null) throw fail("Skipping non-tripelizable web: " + webToStringShort(web)); addTriple(w); } public void addTriple(TripleWeb w) { if (w == null) return; if (index != null) for (String s : sortedInPlace(ll(w.a, w.b, w.c))) index.put(s, w); if (wordIndex != null) { wordIndex.add(w.a); wordIndex.add(w.b); wordIndex.add(w.c); } positionalIndices[0].put(w.a, w); positionalIndices[1].put(w.b, w); positionalIndices[2].put(w.c, w); if (exactIndex != null) exactIndex.add(w); if (oneTwoIndex != null) synchronized (oneTwoIndex) { SyncListMultiMap map = oneTwoIndex.get(w.a); if (map == null) oneTwoIndex.put(w.a, map = symbolSyncListMultiMap()); map.put(w.b, w); } if (oneTwoIndex2 != null) oneTwoIndex2.put(pair(w.a, w.b), w); websByID.add(w); if (activated) ai_fireNewTriple(w); } public void removeWeb(Web web) { if (web != null) removeTriple(ai_webToTripleWeb(web)); } public void removeTriples(Collection l) { MultiMap mm = new MultiMap(); for (TripleWeb w : l) { mm.put(ai_shortenForIndex(w.a), w); mm.put(ai_shortenForIndex(w.b), w); mm.put(ai_shortenForIndex(w.c), w); removeFromSets(w); } for (String term : keys(mm)) { HashSet webs = asHashSet(mm.get(term)); if (index != null) indexRemoveMulti(index, term, webs); for (int i = 0; i < 3; i++) indexRemoveMulti(positionalIndices[i], term, webs); } if (activated) for (TripleWeb w : l) ai_fireForgottenTriple(w); } public void removeFromSets(TripleWeb w) { websByID.remove(w); if (exactIndex != null) exactIndex.remove(w); if (oneTwoIndex != null) synchronized (oneTwoIndex) { SyncListMultiMap map = oneTwoIndex.get(w.a); if (map != null) { map.remove(w.a, w); if (empty(map)) oneTwoIndex.remove(w.a); } } if (oneTwoIndex2 != null) oneTwoIndex2.remove(pair(w.a, w.b), w); if (wordIndex != null) { wordIndex.remove(w.a); wordIndex.remove(w.b); wordIndex.remove(w.c); } } public void removeTriple(TripleWeb w) { if (w == null) return; removeFromSets(w); GlobalID id = w.globalID(); indexRemove(0, ai_shortenForIndex(w.a), id); indexRemove(1, ai_shortenForIndex(w.b), id); indexRemove(2, ai_shortenForIndex(w.c), id); if (activated) ai_fireForgottenTriple(w); } public void indexRemove(int position, String term, GlobalID globalID) { if (index != null) indexRemove2(index, term, globalID); indexRemove2(positionalIndices[position], term, globalID); } public void indexRemove2(MultiMap mm, String term, GlobalID globalID) { List l = mm.get(term); for (int i = 0; i < l(l); i++) if (eq(l.get(i).globalID(), globalID)) { mm.remove(term, l.get(i)); return; } } public void indexRemoveMulti(MultiMap mm, String term, Set set) { List l = mm.get(term); synchronized (l) { removeSetFromListQuickly(l, set); } } public void clear() { if (index != null) index.clear(); if (wordIndex != null) wordIndex.clear(); websByID.clear(); if (exactIndex != null) exactIndex.clear(); if (oneTwoIndex != null) oneTwoIndex.clear(); if (oneTwoIndex2 != null) oneTwoIndex2.clear(); } public void activate() { if (!activated) { activated = true; ai_onNewOrRemovedWeb(new F1() { public Object get(Web web) { try { addWeb(web); return false; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "addWeb(web);\r\n false;"; } }, new F1() { public Object get(Web web) { try { removeWeb(web); return false; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "removeWeb(web);\r\n false;"; } }); } } public Collection allTriples() { return websByID; } public void trimToSize() { if (index != null) trimToSizeAll(index.allLists()); for (int i = 0; i < 3; i++) trimToSizeAll(positionalIndices[i].allLists()); for (SyncListMultiMap map : values(oneTwoIndex)) trimToSizeAll(map.allLists()); } public void sortAllLists() { if (index != null) sortLists(index.allLists()); for (int i = 0; i < 3; i++) sortLists(positionalIndices[i].allLists()); for (SyncListMultiMap map : values(oneTwoIndex)) sortLists(map.allLists()); } public void sortLists(Collection> ll) { for (List l : ll) ai_sortTriplesListByPriority_inPlace(l); } public List getOneTwo(String a, String b) { if (oneTwoIndex2 != null) return oneTwoIndex2.get(pair(a, b)); return shortestList2(getTriples(a), getTriples(b)); } } public static class JavaXClassLoaderWithParent2 extends JavaXClassLoader { public ClassLoader virtualParent; public List classesToSkip; public JavaXClassLoaderWithParent2(String progID, List files, ClassLoader virtualParent, List classesToSkip) { super(progID, files); this.virtualParent = virtualParent; this.classesToSkip = classesToSkip; } public Class findClass(String name) throws ClassNotFoundException { if (shouldDelegate(name)) { Class c = virtualParent.loadClass(name); if (c != null) return c; } return super.findClass(name); } public boolean shouldDelegate(String name) { for (String s : classesToSkip) if (eq(name, s) || startsWith(name, s + "$")) return true; return false; } } public static ThreadLocal DynamicObject_loading = new ThreadLocal(); public static class DynamicObject { public String className; public LinkedHashMap fieldValues = new LinkedHashMap(); public DynamicObject() { } public DynamicObject(String className) { this.className = className; } } public abstract static class AbstractThoughtSpace implements AI_PostHandler { public AbstractThoughtSpace parent; public AbstractThoughtSpace() { } public AbstractThoughtSpace(AbstractThoughtSpace parent) { this.parent = parent; } public abstract List> get(String query); public abstract List> get(String query, int position); public abstract List getTriples(String query); public abstract List getTriples(String query, int position); public abstract List getOneTwo(String a, String b); public abstract GlobalID postTriple(T3 t); public abstract int size(); public boolean hasTriple(String a, String b, String c) { return findTriple(a, b, c) != null; } public TripleWeb findTriple(String a, String b, String c) { List l = shortestList3(getTriples(a, 0), getTriples(b, 1), getTriples(c, 2)); if (l != null) for (TripleWeb w : l) if (w != null && tripleEqic(w, a, b, c)) return w; return null; } public boolean hasTriple_verified(String a, String b, String c) { List l = shortestList3(getTriples(a, 0), getTriples(b, 1), getTriples(c, 2)); if (l != null) for (TripleWeb w : l) if (w != null && w.verified() && tripleEqic(w, a, b, c)) return true; return false; } } public static class WebNode implements _SetField { public Web web; public List labels = new ArrayList(); public Object visInfo; public void _setField(String f, Object x) { if (f.equals("web")) web = (Web) x; else if (f.equals("labels")) labels = (List) x; else if (f.equals("visInfo")) visInfo = x; } public WebNode() { } public WebNode(Web web) { this.web = web; web.fireNewNode(this); } public void addLabel(String label) { addLabel(web.parseLabel(label)); } public void addLabel(Lisp label) { if (setAdd(labels, label) && web != null) web.index(label, this); } public void addLabels(Collection l) { for (Lisp lbl : l) addLabel(lbl); } public void addStrings(Collection l) { for (String lbl : l) addLabel(lbl); } public boolean hasLabel(Lisp label) { return labels.contains(label); } public boolean hasLabel(String label) { return labels.contains(web.parseLabel(label)); } public String toString() { return l(labels) == 1 ? str(first(labels)) : str(labels); } public int count() { return 1 + l(labels); } public String text() { return web.unparseLabel(first(labels)); } public List texts() { return web.unparseLabels(labels); } public void relation(String arrow, String b) { web.getRelation(this, web.node(b)).addLabel(arrow); } public Lisp parseLabel(String l) { return web.parseLabel(l); } public String unparseLabel(Lisp l) { return web.unparseLabel(l); } public List labels() { return labels; } public boolean hasLabels() { return nempty(labels); } public void setLabel(int i, Lisp l) { labels.set(i, l); } public Object visInfo() { return visInfo; } public void visInfo(Object o) { visInfo = o; } public double x, y; } public static class PersistableThrowable { public String className; public String msg; public String stacktrace; public PersistableThrowable() { } public PersistableThrowable(Throwable e) { if (e == null) className = "Crazy Null Error"; else { className = getClassName(e).replace('/', '.'); msg = e.getMessage(); stacktrace = getStackTrace_noRecord(e); } } public String toString() { return nempty(msg) ? className + ": " + msg : className; } } public static class SortedCondensedStrings2 extends CondensedStrings2 { public SortedCondensedStrings2() { } public SortedCondensedStrings2(List l) { super(sorted(l)); } public int indexOf(Object o) { if (!(o instanceof String)) return -1; return max(-1, Collections.binarySearch(this, (String) o)); } public boolean contains(Object o) { return indexOf(o) >= 0; } } public static class RGBImage implements MakesBufferedImage { public transient BufferedImage bufferedImage; public File file; public int width, height; public int[] pixels; public RGBImage() { } public RGBImage(BufferedImage image) { this(image, null); } public RGBImage(BufferedImage image, File file) { this.file = file; bufferedImage = image; width = image.getWidth(); height = image.getHeight(); pixels = new int[width * height]; PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width); try { if (!pixelGrabber.grabPixels()) throw new RuntimeException("Could not grab pixels"); cleanPixels(); } catch (InterruptedException e) { throw new RuntimeException(e); } } public RGBImage(String file) throws IOException { this(new File(file)); } public RGBImage(Dimension size, Color color) { this(size.width, size.height, color); } public RGBImage(Dimension size, RGB color) { this(size.width, size.height, color); } public final void cleanPixels() { for (int i = 0; i < pixels.length; i++) pixels[i] &= 0xFFFFFF; } public RGBImage(int width, int height, int[] pixels) { this.width = width; this.height = height; this.pixels = pixels; } public RGBImage(int w, int h, RGB[] pixels) { this.width = w; this.height = h; this.pixels = asInts(pixels); } public static int[] asInts(RGB[] pixels) { int[] ints = new int[pixels.length]; for (int i = 0; i < pixels.length; i++) ints[i] = pixels[i] == null ? 0 : pixels[i].getColor().getRGB(); return ints; } public RGBImage(int w, int h) { this(w, h, Color.black); } public RGBImage(int w, int h, RGB rgb) { this.width = w; this.height = h; this.pixels = new int[w * h]; int col = rgb.asInt(); if (col != 0) for (int i = 0; i < pixels.length; i++) pixels[i] = col; } public RGBImage(RGBImage image) { this(image.width, image.height, copyPixels(image.pixels)); } public RGBImage(int width, int height, Color color) { this(width, height, new RGB(color)); } public RGBImage(File file) throws IOException { this(javax.imageio.ImageIO.read(file)); } public static int[] copyPixels(int[] pixels) { int[] copy = new int[pixels.length]; System.arraycopy(pixels, 0, copy, 0, pixels.length); return copy; } public int getIntPixel(int x, int y) { if (inRange(x, y)) return pixels[y * width + x]; else return 0xFFFFFF; } public static RGB asRGB(int packed) { int r = (packed >> 16) & 0xFF; int g = (packed >> 8) & 0xFF; int b = packed & 0xFF; return new RGB(r / 255f, g / 255f, b / 255f); } public RGB getRGB(int x, int y) { if (inRange(x, y)) return asRGB(pixels[y * width + x]); else return new RGB(0xFFFFFF); } public RGB getPixel(int x, int y) { return getRGB(x, y); } public RGB getPixel(Pt p) { return getPixel(p.x, p.y); } public int getWidth() { return width; } public int getHeight() { return height; } public int w() { return width; } public int h() { return height; } public BufferedImage getBufferedImage() { if (bufferedImage == null) { bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int y = 0; y < height; y++) for (int x = 0; x < width; x++) bufferedImage.setRGB(x, y, pixels[y * width + x]); } return bufferedImage; } public RGBImage clip(Rect r) { return r == null ? null : clip(r.getRectangle()); } public RGBImage clip(Rectangle r) { r = fixClipRect(r); int[] newPixels; try { newPixels = new int[r.width * r.height]; } catch (RuntimeException e) { System.out.println(r); throw e; } for (int y = 0; y < r.height; y++) { System.arraycopy(pixels, (y + r.y) * width + r.x, newPixels, y * r.width, r.width); } return new RGBImage(r.width, r.height, newPixels); } public final Rectangle fixClipRect(Rectangle r) { r = r.intersection(new Rectangle(0, 0, width, height)); if (r.isEmpty()) r = new Rectangle(r.x, r.y, 0, 0); return r; } public File getFile() { return file; } public static RGBImage load(String fileName) { return load(new File(fileName)); } public static RGBImage load(File file) { try { BufferedImage bufferedImage = javax.imageio.ImageIO.read(file); return new RGBImage(bufferedImage); } catch (IOException e) { throw new RuntimeException(e); } } public int getInt(int x, int y) { return pixels[y * width + x]; } public void save(File file) throws IOException { String name = file.getName().toLowerCase(); String type; if (name.endsWith(".png")) type = "png"; else if (name.endsWith(".jpg") || name.endsWith(".jpeg")) type = "jpeg"; else throw new IOException("Unknown image extension: " + name); javax.imageio.ImageIO.write(getBufferedImage(), type, file); } public static RGBImage dummyImage() { return new RGBImage(1, 1, new int[] { 0xFFFFFF }); } public int[] getPixels() { return pixels; } public void setPixel(int x, int y, RGB rgb) { if (x >= 0 && y >= 0 && x < width && y < height) pixels[y * width + x] = rgb.asInt(); } public void setPixel(int x, int y, Color color) { setPixel(x, y, new RGB(color)); } public void setPixel(int x, int y, int rgb) { if (x >= 0 && y >= 0 && x < width && y < height) pixels[y * width + x] = rgb; } public void setPixel(Pt p, RGB rgb) { setPixel(p.x, p.y, rgb); } public void setPixel(Pt p, Color color) { setPixel(p.x, p.y, color); } public RGBImage copy() { return new RGBImage(this); } public boolean inRange(int x, int y) { return x >= 0 && y >= 0 && x < width && y < height; } public Dimension getSize() { return new Dimension(width, height); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; RGBImage rgbImage = (RGBImage) o; if (height != rgbImage.height) return false; if (width != rgbImage.width) return false; if (!Arrays.equals(pixels, rgbImage.pixels)) return false; return true; } @Override public int hashCode() { int result = width; result = 31 * result + height; result = 31 * result + Arrays.hashCode(pixels); return result; } public String getHex(int x, int y) { return getPixel(x, y).getHexString(); } public RGBImage clip(int x, int y, int width, int height) { return clip(new Rectangle(x, y, width, height)); } public RGBImage clipLine(int y) { return clip(0, y, width, 1); } public int numPixels() { return width * height; } } public static class Rect { public int x, y, w, h; public Rect() { } public Rect(Rectangle r) { x = r.x; y = r.y; w = r.width; h = r.height; } public Rect(int x, int y, int w, int h) { this.h = h; this.w = w; this.y = y; this.x = x; } public Rect(Pt p, int w, int h) { this.h = h; this.w = w; x = p.x; y = p.y; } public Rectangle getRectangle() { return new Rectangle(x, y, w, h); } public boolean equals(Object o) { return stdEq2(this, o); } public int hashCode() { return stdHash2(this); } public String toString() { return x + "," + y + " / " + w + "," + h; } public int x2() { return x + w; } public int y2() { return y + h; } public boolean contains(Pt p) { return contains(p.x, p.y); } public boolean contains(int _x, int _y) { return _x >= x && _y >= y && _x < x + w && _y < y + h; } public boolean empty() { return w <= 0 || h <= 0; } } public static class Pt { public int x, y; public Pt() { } public Pt(Point p) { x = p.x; y = p.y; } public Pt(int x, int y) { this.y = y; this.x = x; } public Point getPoint() { return new Point(x, y); } public boolean equals(Object o) { return stdEq2(this, o); } public int hashCode() { return stdHash2(this); } public String toString() { return x + ", " + y; } } public static class CompactPairKeyHashMap extends AbstractMap, V> { public static final int INITIAL_SIZE = 3; public static final double LOAD_FACTOR = 0.6; public static final Object nullObject = new Object(); public static final Object deletedObject = new Object(); public int elements; public int freecells; public K[] keys1; public K[] keys2; public V[] values; public int modCount; public CompactPairKeyHashMap() { this(INITIAL_SIZE); } public CompactPairKeyHashMap(int size) { keys1 = (K[]) new Object[(size == 0 ? 1 : size)]; keys2 = (K[]) new Object[(size == 0 ? 1 : size)]; values = (V[]) new Object[(size == 0 ? 1 : size)]; elements = 0; freecells = keys1.length; modCount = 0; } public synchronized int size() { return elements; } public synchronized boolean isEmpty() { return elements == 0; } public synchronized void clear() { elements = 0; for (int ix = 0; ix < keys1.length; ix++) { keys1[ix] = keys2[ix] = null; values[ix] = null; } freecells = values.length; modCount++; } public synchronized boolean containsKey(Object k) { return keys1[findKeyIndex(k)] != null; } public synchronized boolean containsValue(Object v) { if (v == null) v = (V) nullObject; for (int ix = 0; ix < values.length; ix++) if (values[ix] != null && values[ix].equals(v)) return true; return false; } public synchronized Set, V>> entrySet() { throw new UnsupportedOperationException(); } public synchronized V remove(Object k) { int index = findKeyIndex(k); if (keys1[index] != null) { V v = values[index]; keys1[index] = keys2[index] = (K) deletedObject; values[index] = (V) deletedObject; modCount++; elements--; return v; } else return null; } public synchronized V put(Pair k, V v) { if (k.a == null) k = pair((K) nullObject, k.b); if (k.b == null) k = pair(k.a, (K) nullObject); int hash = k.hashCode(); int hash1 = k.a.hashCode(); int hash2 = k.b.hashCode(); int index = (hash & 0x7FFFFFFF) % keys1.length; int offset = 1; int deletedix = -1; while (keys1[index] != null && !(keys1[index].hashCode() == hash1 && keys2[index].hashCode() == hash2 && keys1[index].equals(k.a) && keys2[index].equals(k.b))) { if (keys1[index] == deletedObject) deletedix = index; index = ((index + offset) & 0x7FFFFFFF) % keys1.length; offset = offset * 2 + 1; if (offset == -1) offset = 2; } if (keys1[index] == null) { if (deletedix != -1) index = deletedix; else freecells--; modCount++; elements++; keys1[index] = k.a; keys2[index] = k.b; values[index] = (V) v; if (1 - (freecells / (double) keys1.length) > LOAD_FACTOR) rehash(keys1.length * 2 + 1); return null; } else { modCount++; V oldv = values[index]; values[index] = (V) v; return oldv; } } public void rehash(int newCapacity) { int oldCapacity = keys1.length; K[] newKeys1 = (K[]) new Object[newCapacity]; K[] newKeys2 = (K[]) new Object[newCapacity]; V[] newValues = (V[]) new Object[newCapacity]; for (int ix = 0; ix < oldCapacity; ix++) { Object k1 = keys1[ix]; if (k1 == null || k1 == deletedObject) continue; Object k2 = keys2[ix]; int hash = pair(k1, k2).hashCode(); int index = (hash & 0x7FFFFFFF) % newCapacity; int offset = 1; while (newKeys1[index] != null) { index = ((index + offset) & 0x7FFFFFFF) % newCapacity; offset = offset * 2 + 1; if (offset == -1) offset = 2; } newKeys1[index] = (K) k1; newKeys2[index] = (K) k2; newValues[index] = values[ix]; } keys1 = newKeys1; keys2 = newKeys2; values = newValues; freecells = keys1.length - elements; } public synchronized V get(Object k) { return values[findKeyIndex(k)]; } public synchronized Collection values() { return new ValueCollection(); } public synchronized Set> keySet() { return new KeySet(); } public final int findKeyIndex(Object _k) { Pair k = (Pair) (_k); if (k.a == null) k = pair((K) nullObject, k.b); if (k.b == null) k = pair(k.a, (K) nullObject); int hash = k.hashCode(); int hash1 = k.a.hashCode(); int hash2 = k.b.hashCode(); int index = (hash & 0x7FFFFFFF) % keys1.length; int offset = 1; int deletedix = -1; while (keys1[index] != null && !(keys1[index].hashCode() == hash1 && keys2[index].hashCode() == hash2 && keys1[index].equals(k.a) && keys2[index].equals(k.b))) { index = ((index + offset) & 0x7FFFFFFF) % keys1.length; offset = offset * 2 + 1; if (offset == -1) offset = 2; } return index; } public class KeySet extends AbstractSet> { public synchronized int size() { return elements; } public synchronized boolean contains(Object k) { return containsKey(k); } public synchronized Iterator> iterator() { return new KeyIterator(); } } public class KeyIterator implements Iterator> { public int ix; public KeyIterator() { for (; ix < keys1.length; ix++) if (values[ix] != null && keys1[ix] != deletedObject) break; } public synchronized boolean hasNext() { return ix < keys1.length; } public synchronized void remove() { throw new UnsupportedOperationException("Collection is read-only"); } public synchronized Pair next() { if (ix >= keys1.length) throw new NoSuchElementException(); K key1 = (K) keys1[ix]; K key2 = (K) keys2[ix]; ix++; for (; ix < keys1.length; ix++) if (keys1[ix] != null && keys1[ix] != deletedObject) break; return pair(key1, key2); } } public class ValueCollection extends AbstractCollection { public synchronized int size() { return elements; } public synchronized Iterator iterator() { return new ValueIterator(); } public synchronized boolean contains(Object v) { return containsValue(v); } } public class ValueIterator implements Iterator { public int ix; public ValueIterator() { for (; ix < values.length; ix++) if (values[ix] != null && values[ix] != deletedObject) break; } public synchronized boolean hasNext() { return ix < values.length; } public synchronized void remove() { throw new UnsupportedOperationException("Collection is read-only"); } public synchronized V next() { if (ix >= values.length) throw new NoSuchElementException(); V value = (V) values[ix++]; for (; ix < values.length; ix++) if (values[ix] != null && values[ix] != deletedObject) break; return value; } } } public abstract static class RandomAccessAbstractList extends AbstractList implements RandomAccess { } public static class ImageSurface extends Surface { public BufferedImage image; public double zoomX = 1, zoomY = 1, zoomFactor = 1.5; public Rectangle selection; public List tools = new ArrayList(); public Object overlay; public Runnable onSelectionChange; public static boolean verbose; public boolean noMinimumSize = true; public String titleForUpload; public Object onZoom; public boolean specialPurposed; public boolean zoomable = true; public ImageSurface() { this(dummyImage()); } public static BufferedImage dummyImage() { return new RGBImage(1, 1, new int[] { 0xFFFFFF }).getBufferedImage(); } public ImageSurface(RGBImage image) { this(image != null ? image.getBufferedImage() : dummyImage()); } public ImageSurface(BufferedImage image) { setImage(image); clearSurface = false; componentPopupMenu2(this, ImageSurface_popupMenuMaker()); new ImageSurfaceSelector(this); jHandleFileDrop(this, new VF1() { public void get(File f) { try { setImage(loadBufferedImage(f)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "setImage(loadBufferedImage(f))"; } }); } public ImageSurface(RGBImage image, double zoom) { this(image); setZoom(zoom); } public void fillPopupMenu(JPopupMenu menu, final Point point) { if (zoomable) { JMenuItem miZoomReset = new JMenuItem("Zoom 100%"); miZoomReset.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { setZoom(1.0); centerPoint(point); } }); menu.add(miZoomReset); JMenuItem miZoomIn = new JMenuItem("Zoom in"); miZoomIn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { zoomIn(zoomFactor); centerPoint(point); } }); menu.add(miZoomIn); JMenuItem miZoomOut = new JMenuItem("Zoom out"); miZoomOut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { zoomOut(zoomFactor); centerPoint(point); } }); menu.add(miZoomOut); JMenuItem miZoomToWindow = new JMenuItem("Zoom to window"); miZoomToWindow.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { zoomToDisplaySize(); } }); menu.add(miZoomToWindow); addMenuItem(menu, "Show full screen", new Runnable() { public void run() { try { showFullScreen(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "showFullScreen()"; } }); addMenuItem(menu, "Point: " + point.x + "," + point.y + " (image: " + image.getWidth() + "*" + image.getHeight() + ")", null); menu.addSeparator(); } addMenuItem(menu, "Save image...", new Runnable() { public void run() { try { saveImage(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "saveImage()"; } }); addMenuItem(menu, "Upload image...", new Runnable() { public void run() { try { uploadTheImage(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "uploadTheImage()"; } }); addMenuItem(menu, "Copy image to clipboard", new Runnable() { public void run() { try { copyImageToClipboard(getImage()); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "copyImageToClipboard(getImage())"; } }); if (!specialPurposed) addMenuItem(menu, "Paste image from clipboard", new Runnable() { public void run() { try { loadFromClipboard(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "loadFromClipboard()"; } }); if (selection != null) addMenuItem(menu, "Crop", new Runnable() { public void run() { try { crop(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "crop()"; } }); if (!specialPurposed) addMenuItem(menu, "No image", new Runnable() { public void run() { try { noImage(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "noImage()"; } }); } public void noImage() { setImage((BufferedImage) null); } public void crop() { if (selection == null) return; BufferedImage img = clipBufferedImage(getImage(), selection); selection = null; setImage(img); } public void loadFromClipboard() { BufferedImage img = getImageFromClipboard(); if (img != null) setImage(img); } public void saveImage() { RGBImage image = new RGBImage(getImage(), null); JFileChooser fileChooser = new JFileChooser(getProgramDir()); if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { try { image.save(fileChooser.getSelectedFile()); } catch (IOException e) { popup(e); } } } public void render(int w, int h, Graphics2D g) { if (verbose) main.print("render"); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.setColor(Color.white); if (image == null) g.fillRect(0, 0, w, h); else { int iw = getZoomedWidth(), ih = getZoomedHeight(); boolean alpha = hasTransparency(image); if (alpha) g.fillRect(0, 0, w, h); g.drawImage(image, 0, 0, iw, ih, null); if (!alpha) { g.fillRect(iw, 0, w - iw, h); g.fillRect(0, ih, iw, h - ih); } } if (verbose) main.print("render overlay"); if (overlay != null) pcallF(overlay, g); if (verbose) main.print("render selection"); if (selection != null) { drawSelectionRect(g, selection, Color.green, Color.white); } } public void drawSelectionRect(Graphics2D g, Rectangle selection, Color green, Color white) { drawSelectionRect(g, selection, green, white, zoomX, zoomY); } public void drawSelectionRect(Graphics2D g, Rectangle selection, Color green, Color white, double zoomX, double zoomY) { g.setColor(green); int top = (int) (selection.y * zoomY); int bottom = (int) ((selection.y + selection.height) * zoomY); int left = (int) (selection.x * zoomX); int right = (int) ((selection.x + selection.width) * zoomX); g.drawRect(left - 1, top - 1, right - left + 1, bottom - top + 1); g.setColor(white); g.drawRect(left - 2, top - 2, right - left + 3, bottom - top + 3); } public void setZoom(double zoom) { setZoom(zoom, zoom); } public void setZoom(double zoomX, double zoomY) { this.zoomX = zoomX; this.zoomY = zoomY; revalidate(); repaint(); centerPoint(new Point(getImage().getWidth() / 2, getImage().getHeight() / 2)); pcallF(onZoom); } public Dimension getMinimumSize() { if (noMinimumSize) return new Dimension(1, 1); int w = getZoomedWidth(); int h = getZoomedHeight(); Dimension min = super.getMinimumSize(); return new Dimension(Math.max(w, min.width), Math.max(h, min.height)); } public final int getZoomedHeight() { return (int) (image.getHeight() * zoomY); } public final int getZoomedWidth() { return (int) (image.getWidth() * zoomX); } public void setImage(RGBImage image) { setImage(image.getBufferedImage()); } public void setImage(BufferedImage image) { this.image = image != null ? image : dummyImage(); revalidate(); repaint(); } public BufferedImage getImage() { return image; } public double getZoomX() { return zoomX; } public double getZoomY() { return zoomY; } public Dimension getPreferredSize() { return new Dimension(getZoomedWidth(), getZoomedHeight()); } public JScrollPane makeScrollPane() { JScrollPane scrollPane = new JScrollPane(this); scrollPane.getViewport().setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE); return scrollPane; } public void zoomToWindow() { zoomToDisplaySize(); } public void zoomToDisplaySize() { if (image == null) return; Dimension display = getDisplaySize(); double xRatio = display.width / (double) image.getWidth(); double yRatio = display.height / (double) image.getHeight(); setZoom(Math.min(xRatio, yRatio)); revalidate(); } public final Dimension getDisplaySize() { Container c = getParent(); while (c != null) { if (c instanceof JScrollPane) return c.getSize(); c = c.getParent(); } return getSize(); } public void setSelection(Rectangle r) { if (neq(selection, r)) { selection = r; pcallF(onSelectionChange); repaint(); } } public Rectangle getSelection() { return selection; } public RGBImage getRGBImage() { return new RGBImage(getImage()); } public void centerPoint(Point p) { JScrollPane sp = enclosingScrollPane(this); if (sp == null) return; p = new Point((int) (p.x * getZoomX()), (int) (p.y * getZoomY())); final JViewport viewport = sp.getViewport(); Dimension viewSize = viewport.getExtentSize(); int x = max(0, p.x - viewSize.width / 2); int y = max(0, p.y - viewSize.height / 2); p = new Point(x, y); final Point _p = p; awtLater(new Runnable() { public void run() { try { viewport.setViewPosition(_p); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "viewport.setViewPosition(_p);"; } }); } public Pt pointFromEvent(MouseEvent e) { return pointFromComponentCoordinates(new Pt(e.getX(), e.getY())); } public Pt pointFromComponentCoordinates(Pt p) { return new Pt((int) (p.x / zoomX), (int) (p.y / zoomY)); } public void uploadTheImage() { call(hotwire("#1016427"), "go", getImage(), titleForUpload); } public void showFullScreen() { showFullScreenImageSurface(getImage()); } public void zoomIn(double f) { setZoom(getZoomX() * f, getZoomY() * f); } public void zoomOut(double f) { setZoom(getZoomX() / f, getZoomY() / f); } } public static VF2 ImageSurface_popupMenuMaker() { return new VF2() { public void get(ImageSurface is, JPopupMenu menu) { try { Point p = is.pointFromEvent(componentPopupMenu_mouseEvent.get()).getPoint(); is.fillPopupMenu(menu, p); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Point p = is.pointFromEvent(componentPopupMenu_mouseEvent.get()).getPoint();\r..."; } }; } public static class RGB { public float r, g, b; public RGB() { } public RGB(float r, float g, float b) { this.r = r; this.g = g; this.b = b; } public RGB(double r, double g, double b) { this.r = (float) r; this.g = (float) g; this.b = (float) b; } public RGB(int rgb) { this(new Color(rgb)); } public RGB(double brightness) { this.r = this.g = this.b = max(0f, min(1f, (float) brightness)); } public RGB(Color color) { this.r = color.getRed() / 255f; this.g = color.getGreen() / 255f; this.b = color.getBlue() / 255f; } public RGB(String hex) { int i = l(hex) - 6; r = Integer.parseInt(hex.substring(i, i + 2), 16) / 255f; g = Integer.parseInt(hex.substring(i + 2, i + 4), 16) / 255f; b = Integer.parseInt(hex.substring(i + 4, i + 6), 16) / 255f; } public float getComponent(int i) { return i == 0 ? r : i == 1 ? g : b; } public Color getColor() { return new Color(r, g, b); } public static RGB newSafe(float r, float g, float b) { return new RGB(Math.max(0, Math.min(1, r)), Math.max(0, Math.min(1, g)), Math.max(0, Math.min(1, b))); } public int asInt() { return getColor().getRGB() & 0xFFFFFF; } public int getInt() { return getColor().getRGB() & 0xFFFFFF; } public float getBrightness() { return (r + g + b) / 3.0f; } public String getHexString() { return Integer.toHexString(asInt() | 0xFF000000).substring(2).toUpperCase(); } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof RGB)) return false; RGB rgb = (RGB) o; if (Float.compare(rgb.b, b) != 0) return false; if (Float.compare(rgb.g, g) != 0) return false; if (Float.compare(rgb.r, r) != 0) return false; return true; } @Override public int hashCode() { int result = (r != +0.0f ? Float.floatToIntBits(r) : 0); result = 31 * result + (g != +0.0f ? Float.floatToIntBits(g) : 0); result = 31 * result + (b != +0.0f ? Float.floatToIntBits(b) : 0); return result; } public boolean isBlack() { return r == 0f && g == 0f && b == 0f; } public boolean isWhite() { return r == 1f && g == 1f && b == 1f; } public String toString() { return getHexString(); } } public static interface MakesBufferedImage { public BufferedImage getBufferedImage(); } public static class SynchronizedArrayList extends SynchronizedArrayList_Base implements RandomAccess, Cloneable { public static final int DEFAULT_CAPACITY = 10; public static final Object[] EMPTY_ELEMENTDATA = {}; public static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; public transient Object[] elementData; public int size; public SynchronizedArrayList(int initialCapacity) { if (initialCapacity > 0) { this.elementData = new Object[initialCapacity]; } else if (initialCapacity == 0) { this.elementData = EMPTY_ELEMENTDATA; } else { throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); } } public SynchronizedArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; } public SynchronizedArrayList(Collection c) { elementData = c.toArray(); if ((size = elementData.length) != 0) { if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, size, Object[].class); } else { this.elementData = EMPTY_ELEMENTDATA; } } public synchronized void trimToSize() { modCount++; if (size < elementData.length) { elementData = (size == 0) ? EMPTY_ELEMENTDATA : Arrays.copyOf(elementData, size); } } public synchronized void ensureCapacity(int minCapacity) { int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA) ? 0 : DEFAULT_CAPACITY; if (minCapacity > minExpand) { ensureExplicitCapacity(minCapacity); } } public final void ensureCapacityInternal(int minCapacity) { if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); } ensureExplicitCapacity(minCapacity); } public final void ensureExplicitCapacity(int minCapacity) { modCount++; if (minCapacity - elementData.length > 0) grow(minCapacity); } public static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; public final void grow(int minCapacity) { int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); elementData = Arrays.copyOf(elementData, newCapacity); } public static int hugeCapacity(int minCapacity) { if (minCapacity < 0) throw new OutOfMemoryError(); return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; } public synchronized int size() { return size; } public synchronized boolean isEmpty() { return size == 0; } public boolean contains(Object o) { return indexOf(o) >= 0; } public synchronized int indexOf(Object o) { if (o == null) { for (int i = 0; i < size; i++) if (elementData[i] == null) return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1; } public synchronized int lastIndexOf(Object o) { if (o == null) { for (int i = size - 1; i >= 0; i--) if (elementData[i] == null) return i; } else { for (int i = size - 1; i >= 0; i--) if (o.equals(elementData[i])) return i; } return -1; } public synchronized Object clone() { return new SynchronizedArrayList(this); } public synchronized Object[] toArray() { return Arrays.copyOf(elementData, size); } @SuppressWarnings("unchecked") public synchronized T[] toArray(T[] a) { if (a.length < size) return (T[]) Arrays.copyOf(elementData, size, a.getClass()); System.arraycopy(elementData, 0, a, 0, size); if (a.length > size) a[size] = null; return a; } @SuppressWarnings("unchecked") public A elementData(int index) { return (A) elementData[index]; } public synchronized A get(int index) { rangeCheck(index); return elementData(index); } public synchronized A set(int index, A element) { rangeCheck(index); A oldValue = elementData(index); elementData[index] = element; return oldValue; } public synchronized boolean add(A e) { ensureCapacityInternal(size + 1); elementData[size++] = e; return true; } public synchronized void add(int index, A element) { rangeCheckForAdd(index); ensureCapacityInternal(size + 1); System.arraycopy(elementData, index, elementData, index + 1, size - index); elementData[index] = element; size++; } public synchronized A remove(int index) { rangeCheck(index); modCount++; A oldValue = elementData(index); int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index + 1, elementData, index, numMoved); elementData[--size] = null; return oldValue; } public synchronized boolean remove(Object o) { if (o == null) { for (int index = 0; index < size; index++) if (elementData[index] == null) { fastRemove(index); return true; } } else { for (int index = 0; index < size; index++) if (o.equals(elementData[index])) { fastRemove(index); return true; } } return false; } public final void fastRemove(int index) { modCount++; int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index + 1, elementData, index, numMoved); elementData[--size] = null; } public synchronized void clear() { modCount++; for (int i = 0; i < size; i++) elementData[i] = null; size = 0; } public synchronized boolean addAll(Collection c) { Object[] a = c.toArray(); int numNew = a.length; ensureCapacityInternal(size + numNew); System.arraycopy(a, 0, elementData, size, numNew); size += numNew; return numNew != 0; } public synchronized boolean addAll(int index, Collection c) { rangeCheckForAdd(index); Object[] a = c.toArray(); int numNew = a.length; ensureCapacityInternal(size + numNew); int numMoved = size - index; if (numMoved > 0) System.arraycopy(elementData, index, elementData, index + numNew, numMoved); System.arraycopy(a, 0, elementData, index, numNew); size += numNew; return numNew != 0; } public synchronized void removeRange(int fromIndex, int toIndex) { modCount++; int numMoved = size - toIndex; System.arraycopy(elementData, toIndex, elementData, fromIndex, numMoved); int newSize = size - (toIndex - fromIndex); for (int i = newSize; i < size; i++) { elementData[i] = null; } size = newSize; } public final void rangeCheck(int index) { if (index >= size) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } public final void rangeCheckForAdd(int index) { if (index > size || index < 0) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } public final String outOfBoundsMsg(int index) { return "Index: " + index + ", Size: " + size; } public synchronized boolean removeAll(Collection c) { Objects.requireNonNull(c); return batchRemove(c, false); } public synchronized boolean retainAll(Collection c) { Objects.requireNonNull(c); return batchRemove(c, true); } public final boolean batchRemove(Collection c, boolean complement) { final Object[] elementData = this.elementData; int r = 0, w = 0; boolean modified = false; try { for (; r < size; r++) if (c.contains(elementData[r]) == complement) elementData[w++] = elementData[r]; } finally { if (r != size) { System.arraycopy(elementData, r, elementData, w, size - r); w += size - r; } if (w != size) { for (int i = w; i < size; i++) elementData[i] = null; modCount += size - w; size = w; modified = true; } } return modified; } public final void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { int expectedModCount = modCount; s.defaultWriteObject(); s.writeInt(size); for (int i = 0; i < size; i++) { s.writeObject(elementData[i]); } if (modCount != expectedModCount) { throw new ConcurrentModificationException(); } } public final void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { elementData = EMPTY_ELEMENTDATA; s.defaultReadObject(); s.readInt(); if (size > 0) { ensureCapacityInternal(size); Object[] a = elementData; for (int i = 0; i < size; i++) { a[i] = s.readObject(); } } } public synchronized ListIterator listIterator(int index) { if (index < 0 || index > size) throw new IndexOutOfBoundsException("Index: " + index); return new ListItr(index); } public ListIterator listIterator() { return new ListItr(0); } public Iterator iterator() { return concurrentlyIterateList(this); } public class Itr implements Iterator { public int cursor; public int lastRet = -1; public int expectedModCount = modCount; public boolean hasNext() { return cursor != size; } @SuppressWarnings("unchecked") public A next() { checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException(); Object[] elementData = SynchronizedArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (A) elementData[lastRet = i]; } public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { SynchronizedArrayList.this.remove(lastRet); cursor = lastRet; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } public final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } } public class ListItr extends Itr implements ListIterator { public ListItr(int index) { super(); cursor = index; } public boolean hasPrevious() { return cursor != 0; } public int nextIndex() { return cursor; } public int previousIndex() { return cursor - 1; } @SuppressWarnings("unchecked") public A previous() { checkForComodification(); int i = cursor - 1; if (i < 0) throw new NoSuchElementException(); Object[] elementData = SynchronizedArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i; return (A) elementData[lastRet = i]; } public void set(A e) { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { SynchronizedArrayList.this.set(lastRet, e); } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } public void add(A e) { checkForComodification(); try { int i = cursor; SynchronizedArrayList.this.add(i, e); cursor = i + 1; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } } public List subList(int fromIndex, int toIndex) { subListRangeCheck(fromIndex, toIndex, size); return new SubList(this, 0, fromIndex, toIndex); } public static void subListRangeCheck(int fromIndex, int toIndex, int size) { if (fromIndex < 0) throw new IndexOutOfBoundsException("fromIndex = " + fromIndex); if (toIndex > size) throw new IndexOutOfBoundsException("toIndex = " + toIndex); if (fromIndex > toIndex) throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")"); } public class SubList extends SynchronizedArrayList_Base implements RandomAccess { public final SynchronizedArrayList_Base parent; public final int parentOffset; public final int offset; public int size; public SubList(SynchronizedArrayList_Base parent, int offset, int fromIndex, int toIndex) { this.parent = parent; this.parentOffset = fromIndex; this.offset = offset + fromIndex; this.size = toIndex - fromIndex; this.modCount = SynchronizedArrayList.this.modCount; } public A set(int index, A e) { synchronized (SynchronizedArrayList.this) { rangeCheck(index); checkForComodification(); A oldValue = SynchronizedArrayList.this.elementData(offset + index); SynchronizedArrayList.this.elementData[offset + index] = e; return oldValue; } } public A get(int index) { synchronized (SynchronizedArrayList.this) { rangeCheck(index); checkForComodification(); return SynchronizedArrayList.this.elementData(offset + index); } } public int size() { synchronized (SynchronizedArrayList.this) { checkForComodification(); return this.size; } } public void add(int index, A e) { synchronized (SynchronizedArrayList.this) { rangeCheckForAdd(index); checkForComodification(); parent.add(parentOffset + index, e); this.modCount = parent.modCount(); this.size++; } } public A remove(int index) { synchronized (SynchronizedArrayList.this) { rangeCheck(index); checkForComodification(); A result = parent.remove(parentOffset + index); this.modCount = parent.modCount(); this.size--; return result; } } public void removeRange(int fromIndex, int toIndex) { synchronized (SynchronizedArrayList.this) { checkForComodification(); parent.removeRange(parentOffset + fromIndex, parentOffset + toIndex); this.modCount = parent.modCount(); this.size -= toIndex - fromIndex; } } public boolean addAll(Collection c) { return addAll(this.size, c); } public boolean addAll(int index, Collection c) { synchronized (SynchronizedArrayList.this) { rangeCheckForAdd(index); int cSize = c.size(); if (cSize == 0) return false; checkForComodification(); parent.addAll(parentOffset + index, c); this.modCount = parent.modCount(); this.size += cSize; return true; } } public Iterator iterator() { return listIterator(); } public ListIterator listIterator(final int index) { synchronized (SynchronizedArrayList.this) { checkForComodification(); rangeCheckForAdd(index); final int offset = this.offset; return new ListIterator() { public int cursor = index; public int lastRet = -1; public int expectedModCount = SynchronizedArrayList.this.modCount; public boolean hasNext() { return cursor != SubList.this.size; } @SuppressWarnings("unchecked") public A next() { checkForComodification(); int i = cursor; if (i >= SubList.this.size) throw new NoSuchElementException(); Object[] elementData = SynchronizedArrayList.this.elementData; if (offset + i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (A) elementData[offset + (lastRet = i)]; } public boolean hasPrevious() { return cursor != 0; } @SuppressWarnings("unchecked") public A previous() { checkForComodification(); int i = cursor - 1; if (i < 0) throw new NoSuchElementException(); Object[] elementData = SynchronizedArrayList.this.elementData; if (offset + i >= elementData.length) throw new ConcurrentModificationException(); cursor = i; return (A) elementData[offset + (lastRet = i)]; } public int nextIndex() { return cursor; } public int previousIndex() { return cursor - 1; } public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { SubList.this.remove(lastRet); cursor = lastRet; lastRet = -1; expectedModCount = SynchronizedArrayList.this.modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } public void set(A e) { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { SynchronizedArrayList.this.set(offset + lastRet, e); } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } public void add(A e) { checkForComodification(); try { int i = cursor; SubList.this.add(i, e); cursor = i + 1; lastRet = -1; expectedModCount = SynchronizedArrayList.this.modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } public final void checkForComodification() { if (expectedModCount != SynchronizedArrayList.this.modCount) throw new ConcurrentModificationException(); } }; } } public List subList(int fromIndex, int toIndex) { synchronized (SynchronizedArrayList.this) { subListRangeCheck(fromIndex, toIndex, size); return new SubList(parent, offset, fromIndex, toIndex); } } public final void rangeCheck(int index) { if (index < 0 || index >= this.size) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } public final void rangeCheckForAdd(int index) { if (index < 0 || index > this.size) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } public final String outOfBoundsMsg(int index) { return "Index: " + index + ", Size: " + this.size; } public final void checkForComodification() { if (SynchronizedArrayList.this.modCount != this.modCount) throw new ConcurrentModificationException(); } } @Override @SuppressWarnings("unchecked") public synchronized void sort(Comparator c) { final int expectedModCount = modCount; Arrays.sort((A[]) elementData, 0, size, c); if (modCount != expectedModCount) { throw new ConcurrentModificationException(); } modCount++; } } public static class WebSocketException extends IOException { public WebSocketFrame.CloseCode code; public String reason; public WebSocketException(Exception cause) { this(WebSocketFrame.CloseCode.InternalServerError, cause.toString(), cause); } public WebSocketException(WebSocketFrame.CloseCode code, String reason) { this(code, reason, null); } public WebSocketException(WebSocketFrame.CloseCode code, String reason, Exception cause) { super(code + ": " + reason, cause); this.code = code; this.reason = reason; } public WebSocketFrame.CloseCode getCode() { return code; } public String getReason() { return reason; } } public static class WebSocketFrame { public OpCode opCode; public boolean fin; public byte[] maskingKey; public byte[] payload; public transient int _payloadLength; public transient String _payloadString; public WebSocketFrame(OpCode opCode, boolean fin) { setOpCode(opCode); setFin(fin); } public WebSocketFrame(OpCode opCode, boolean fin, byte[] payload, byte[] maskingKey) { this(opCode, fin); setMaskingKey(maskingKey); setBinaryPayload(payload); } public WebSocketFrame(OpCode opCode, boolean fin, byte[] payload) { this(opCode, fin, payload, null); } public WebSocketFrame(OpCode opCode, boolean fin, String payload, byte[] maskingKey) throws CharacterCodingException { this(opCode, fin); setMaskingKey(maskingKey); setTextPayload(payload); } public WebSocketFrame(OpCode opCode, boolean fin, String payload) throws CharacterCodingException { this(opCode, fin, payload, null); } public WebSocketFrame(WebSocketFrame clone) { setOpCode(clone.getOpCode()); setFin(clone.isFin()); setBinaryPayload(clone.getBinaryPayload()); setMaskingKey(clone.getMaskingKey()); } public WebSocketFrame(OpCode opCode, List fragments) throws WebSocketException { setOpCode(opCode); setFin(true); long _payloadLength = 0; for (WebSocketFrame inter : fragments) { _payloadLength += inter.getBinaryPayload().length; } if (_payloadLength < 0 || _payloadLength > Integer.MAX_VALUE) { throw new WebSocketException(WebSocketFrame.CloseCode.MessageTooBig, "Max frame length has been exceeded."); } this._payloadLength = (int) _payloadLength; byte[] payload = new byte[this._payloadLength]; int offset = 0; for (WebSocketFrame inter : fragments) { System.arraycopy(inter.getBinaryPayload(), 0, payload, offset, inter.getBinaryPayload().length); offset += inter.getBinaryPayload().length; } setBinaryPayload(payload); } public OpCode getOpCode() { return opCode; } public void setOpCode(OpCode opcode) { this.opCode = opcode; } public boolean isFin() { return fin; } public void setFin(boolean fin) { this.fin = fin; } public boolean isMasked() { return maskingKey != null && maskingKey.length == 4; } public byte[] getMaskingKey() { return maskingKey; } public void setMaskingKey(byte[] maskingKey) { if (maskingKey != null && maskingKey.length != 4) { throw new IllegalArgumentException("MaskingKey " + Arrays.toString(maskingKey) + " hasn't length 4"); } this.maskingKey = maskingKey; } public void setUnmasked() { setMaskingKey(null); } public byte[] getBinaryPayload() { return payload; } public void setBinaryPayload(byte[] payload) { this.payload = payload; this._payloadLength = payload.length; this._payloadString = null; } public String getTextPayload() { if (_payloadString == null) { try { _payloadString = binary2Text(getBinaryPayload()); } catch (CharacterCodingException e) { throw new RuntimeException("Undetected CharacterCodingException", e); } } return _payloadString; } public void setTextPayload(String payload) throws CharacterCodingException { this.payload = text2Binary(payload); this._payloadLength = payload.length(); this._payloadString = payload; } public static WebSocketFrame read(InputStream in) throws IOException { byte head = (byte) checkedRead(in.read()); boolean fin = ((head & 0x80) != 0); OpCode opCode = OpCode.find((byte) (head & 0x0F)); if ((head & 0x70) != 0) { throw new WebSocketException(WebSocketFrame.CloseCode.ProtocolError, "The reserved bits (" + Integer.toBinaryString(head & 0x70) + ") must be 0."); } if (opCode == null) { throw new WebSocketException(WebSocketFrame.CloseCode.ProtocolError, "Received frame with reserved/unknown opcode " + (head & 0x0F) + "."); } else if (opCode.isControlFrame() && !fin) { throw new WebSocketException(WebSocketFrame.CloseCode.ProtocolError, "Fragmented control frame."); } WebSocketFrame frame = new WebSocketFrame(opCode, fin); frame.readPayloadInfo(in); frame.readPayload(in); if (frame.getOpCode() == WebSocketFrame.OpCode.Close) { return new WebSocketFrame.CloseFrame(frame); } else { return frame; } } public static int checkedRead(int read) throws IOException { if (read < 0) { throw new EOFException(); } return read; } public final void readPayloadInfo(InputStream in) throws IOException { byte b = (byte) checkedRead(in.read()); boolean masked = ((b & 0x80) != 0); _payloadLength = (byte) (0x7F & b); if (_payloadLength == 126) { _payloadLength = (checkedRead(in.read()) << 8 | checkedRead(in.read())) & 0xFFFF; if (_payloadLength < 126) { throw new WebSocketException(WebSocketFrame.CloseCode.ProtocolError, "Invalid data frame 2byte length. (not using minimal length encoding)"); } } else if (_payloadLength == 127) { long _payloadLength = ((long) checkedRead(in.read())) << 56 | ((long) checkedRead(in.read())) << 48 | ((long) checkedRead(in.read())) << 40 | ((long) checkedRead(in.read())) << 32 | checkedRead(in.read()) << 24 | checkedRead(in.read()) << 16 | checkedRead(in.read()) << 8 | checkedRead(in.read()); if (_payloadLength < 65536) { throw new WebSocketException(WebSocketFrame.CloseCode.ProtocolError, "Invalid data frame 4byte length. (not using minimal length encoding)"); } if (_payloadLength < 0 || _payloadLength > Integer.MAX_VALUE) { throw new WebSocketException(WebSocketFrame.CloseCode.MessageTooBig, "Max frame length has been exceeded."); } this._payloadLength = (int) _payloadLength; } if (opCode.isControlFrame()) { if (_payloadLength > 125) { throw new WebSocketException(WebSocketFrame.CloseCode.ProtocolError, "Control frame with payload length > 125 bytes."); } if (opCode == OpCode.Close && _payloadLength == 1) { throw new WebSocketException(WebSocketFrame.CloseCode.ProtocolError, "Received close frame with payload len 1."); } } if (masked) { maskingKey = new byte[4]; int read = 0; while (read < maskingKey.length) { read += checkedRead(in.read(maskingKey, read, maskingKey.length - read)); } } } public final void readPayload(InputStream in) throws IOException { payload = new byte[_payloadLength]; int read = 0; while (read < _payloadLength) { read += checkedRead(in.read(payload, read, _payloadLength - read)); } if (isMasked()) { for (int i = 0; i < payload.length; i++) { payload[i] ^= maskingKey[i % 4]; } } if (getOpCode() == WebSocketFrame.OpCode.Text) { _payloadString = binary2Text(getBinaryPayload()); } } public void write(OutputStream out) throws IOException { byte header = 0; if (fin) { header |= 0x80; } header |= opCode.getValue() & 0x0F; out.write(header); _payloadLength = getBinaryPayload().length; if (_payloadLength <= 125) { out.write(isMasked() ? 0x80 | (byte) _payloadLength : (byte) _payloadLength); } else if (_payloadLength <= 0xFFFF) { out.write(isMasked() ? 0xFE : 126); out.write(_payloadLength >>> 8); out.write(_payloadLength); } else { out.write(isMasked() ? 0xFF : 127); out.write(_payloadLength >>> 56 & 0); out.write(_payloadLength >>> 48 & 0); out.write(_payloadLength >>> 40 & 0); out.write(_payloadLength >>> 32 & 0); out.write(_payloadLength >>> 24); out.write(_payloadLength >>> 16); out.write(_payloadLength >>> 8); out.write(_payloadLength); } if (isMasked()) { out.write(maskingKey); for (int i = 0; i < _payloadLength; i++) { out.write(getBinaryPayload()[i] ^ maskingKey[i % 4]); } } else { out.write(getBinaryPayload()); } out.flush(); } public static final Charset TEXT_CHARSET = Charset.forName("UTF-8"); public static final CharsetDecoder TEXT_DECODER = TEXT_CHARSET.newDecoder(); public static final CharsetEncoder TEXT_ENCODER = TEXT_CHARSET.newEncoder(); public static String binary2Text(byte[] payload) throws CharacterCodingException { return TEXT_DECODER.decode(ByteBuffer.wrap(payload)).toString(); } public static String binary2Text(byte[] payload, int offset, int length) throws CharacterCodingException { return TEXT_DECODER.decode(ByteBuffer.wrap(payload, offset, length)).toString(); } public static byte[] text2Binary(String payload) throws CharacterCodingException { return TEXT_ENCODER.encode(CharBuffer.wrap(payload)).array(); } @Override public String toString() { final StringBuilder sb = new StringBuilder("WS["); sb.append(getOpCode()); sb.append(", ").append(isFin() ? "fin" : "inter"); sb.append(", ").append(isMasked() ? "masked" : "unmasked"); sb.append(", ").append(payloadToString()); sb.append(']'); return sb.toString(); } public String payloadToString() { if (payload == null) return "null"; else { final StringBuilder sb = new StringBuilder(); sb.append('[').append(payload.length).append("b] "); if (getOpCode() == WebSocketFrame.OpCode.Text) { String text = getTextPayload(); if (text.length() > 100) sb.append(text.substring(0, 100)).append("..."); else sb.append(text); } else { sb.append("0x"); for (int i = 0; i < Math.min(payload.length, 50); ++i) sb.append(Integer.toHexString((int) payload[i] & 0xFF)); if (payload.length > 50) sb.append("..."); } return sb.toString(); } } public static enum OpCode { Continuation(0), Text(1), Binary(2), Close(8), Ping(9), Pong(10); public final byte code; private OpCode(int code) { this.code = (byte) code; } public byte getValue() { return code; } public boolean isControlFrame() { return this == Close || this == Ping || this == Pong; } public static OpCode find(byte value) { for (OpCode opcode : values()) { if (opcode.getValue() == value) { return opcode; } } return null; } } public static enum CloseCode { NormalClosure(1000), GoingAway(1001), ProtocolError(1002), UnsupportedData(1003), NoStatusRcvd(1005), AbnormalClosure(1006), InvalidFramePayloadData(1007), PolicyViolation(1008), MessageTooBig(1009), MandatoryExt(1010), InternalServerError(1011), TLSHandshake(1015); public final int code; private CloseCode(int code) { this.code = code; } public int getValue() { return code; } public static WebSocketFrame.CloseCode find(int value) { for (WebSocketFrame.CloseCode code : values()) { if (code.getValue() == value) { return code; } } return null; } } public static class CloseFrame extends WebSocketFrame { public CloseCode _closeCode; public String _closeReason; public CloseFrame(WebSocketFrame wrap) throws CharacterCodingException { super(wrap); assert wrap.getOpCode() == OpCode.Close; if (wrap.getBinaryPayload().length >= 2) { _closeCode = CloseCode.find((wrap.getBinaryPayload()[0] & 0xFF) << 8 | (wrap.getBinaryPayload()[1] & 0xFF)); _closeReason = binary2Text(getBinaryPayload(), 2, getBinaryPayload().length - 2); } } public CloseFrame(CloseCode code, String closeReason) throws CharacterCodingException { super(OpCode.Close, true, generatePayload(code, closeReason)); } public static byte[] generatePayload(CloseCode code, String closeReason) throws CharacterCodingException { if (code != null) { byte[] reasonBytes = text2Binary(closeReason); byte[] payload = new byte[reasonBytes.length + 2]; payload[0] = (byte) ((code.getValue() >> 8) & 0xFF); payload[1] = (byte) ((code.getValue()) & 0xFF); System.arraycopy(reasonBytes, 0, payload, 2, reasonBytes.length); return payload; } else { return new byte[0]; } } public String payloadToString() { return (_closeCode != null ? _closeCode : "UnknownCloseCode[" + _closeCode + "]") + (_closeReason != null && !_closeReason.isEmpty() ? ": " + _closeReason : ""); } public CloseCode getCloseCode() { return _closeCode; } public String getCloseReason() { return _closeReason; } } } public static class WebSocket { public final InputStream in; public OutputStream out; public WebSocketFrame.OpCode continuousOpCode = null; public List continuousFrames = new LinkedList(); public State state = State.UNCONNECTED; public static enum State { UNCONNECTED, CONNECTING, OPEN, CLOSING, CLOSED } public final NanoHTTPD.IHTTPSession handshakeRequest; public final NanoHTTPD.Response handshakeResponse = new NanoHTTPD.Response(NanoHTTPD.Status.SWITCH_PROTOCOL, null, (InputStream) null, -1) { @Override public void send(OutputStream out) { WebSocket.this.out = out; state = State.CONNECTING; super.send(out); state = State.OPEN; readWebsocket(); } }; public WebSocket(NanoHTTPD.IHTTPSession handshakeRequest) { this.handshakeRequest = handshakeRequest; this.in = handshakeRequest.getInputStream(); handshakeResponse.addHeader(NanoWebSocketServer.HEADER_UPGRADE, NanoWebSocketServer.HEADER_UPGRADE_VALUE); handshakeResponse.addHeader(NanoWebSocketServer.HEADER_CONNECTION, NanoWebSocketServer.HEADER_CONNECTION_VALUE); } public void readWebsocket() { try { while (state == State.OPEN) { handleWebsocketFrame(WebSocketFrame.read(in)); } } catch (CharacterCodingException e) { onException(e); doClose(WebSocketFrame.CloseCode.InvalidFramePayloadData, e.toString(), false); } catch (IOException e) { onException(e); if (e instanceof WebSocketException) { doClose(((WebSocketException) e).getCode(), ((WebSocketException) e).getReason(), false); } } finally { doClose(WebSocketFrame.CloseCode.InternalServerError, "Handler terminated without closing the connection.", false); } } public void handleWebsocketFrame(WebSocketFrame frame) throws IOException { if (frame.getOpCode() == WebSocketFrame.OpCode.Close) { handleCloseFrame(frame); } else if (frame.getOpCode() == WebSocketFrame.OpCode.Ping) { sendFrame(new WebSocketFrame(WebSocketFrame.OpCode.Pong, true, frame.getBinaryPayload())); } else if (frame.getOpCode() == WebSocketFrame.OpCode.Pong) { onPong(frame); } else if (!frame.isFin() || frame.getOpCode() == WebSocketFrame.OpCode.Continuation) { handleFrameFragment(frame); } else if (continuousOpCode != null) { throw new WebSocketException(WebSocketFrame.CloseCode.ProtocolError, "Continuous frame sequence not completed."); } else if (frame.getOpCode() == WebSocketFrame.OpCode.Text || frame.getOpCode() == WebSocketFrame.OpCode.Binary) { onMessage(frame); } else { throw new WebSocketException(WebSocketFrame.CloseCode.ProtocolError, "Non control or continuous frame expected."); } } public void handleCloseFrame(WebSocketFrame frame) throws IOException { WebSocketFrame.CloseCode code = WebSocketFrame.CloseCode.NormalClosure; String reason = ""; if (frame instanceof WebSocketFrame.CloseFrame) { code = ((WebSocketFrame.CloseFrame) frame).getCloseCode(); reason = ((WebSocketFrame.CloseFrame) frame).getCloseReason(); } if (state == State.CLOSING) { doClose(code, reason, false); } else { State oldState = state; state = State.CLOSING; if (oldState == State.OPEN) try { sendFrame(new WebSocketFrame.CloseFrame(code, reason)); } catch (Throwable _e) { } doClose(code, reason, true); } } public void handleFrameFragment(WebSocketFrame frame) throws IOException { if (frame.getOpCode() != WebSocketFrame.OpCode.Continuation) { if (continuousOpCode != null) { throw new WebSocketException(WebSocketFrame.CloseCode.ProtocolError, "Previous continuous frame sequence not completed."); } continuousOpCode = frame.getOpCode(); continuousFrames.clear(); continuousFrames.add(frame); } else if (frame.isFin()) { if (continuousOpCode == null) { throw new WebSocketException(WebSocketFrame.CloseCode.ProtocolError, "Continuous frame sequence was not started."); } onMessage(new WebSocketFrame(continuousOpCode, continuousFrames)); continuousOpCode = null; continuousFrames.clear(); } else if (continuousOpCode == null) { throw new WebSocketException(WebSocketFrame.CloseCode.ProtocolError, "Continuous frame sequence was not started."); } else { continuousFrames.add(frame); } } public synchronized void sendFrame(WebSocketFrame frame) throws IOException { frame.write(out); } public void doClose(WebSocketFrame.CloseCode code, String reason, boolean initiatedByRemote) { if (state == State.CLOSED) { return; } if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } state = State.CLOSED; onClose(code, reason, initiatedByRemote); } public void onPong(WebSocketFrame pongFrame) { print("WebSocket pong"); } public void onMessage(WebSocketFrame messageFrame) { print("WebSocket msg: " + messageFrame.getTextPayload()); } public void onClose(WebSocketFrame.CloseCode code, String reason, boolean initiatedByRemote) { print("WebSocket close"); } public void onException(IOException e) { printStackTrace(e); } public void ping(byte[] payload) throws IOException { sendFrame(new WebSocketFrame(WebSocketFrame.OpCode.Ping, true, payload)); } public void send(byte[] payload) throws IOException { sendFrame(new WebSocketFrame(WebSocketFrame.OpCode.Binary, true, payload)); } public void send(String payload) throws IOException { sendFrame(new WebSocketFrame(WebSocketFrame.OpCode.Text, true, payload)); } public void close(WebSocketFrame.CloseCode code, String reason) throws IOException { State oldState = state; state = State.CLOSING; if (oldState == State.OPEN) { sendFrame(new WebSocketFrame.CloseFrame(code, reason)); } else { doClose(code, reason, false); } } public NanoHTTPD.IHTTPSession getHandshakeRequest() { return handshakeRequest; } public NanoHTTPD.Response getHandshakeResponse() { return handshakeResponse; } } public static interface WebSocketFactory { public WebSocket openWebSocket(NanoHTTPD.IHTTPSession handshake); } public static class NanoWebSocketServer extends NanoHTTPD implements WebSocketFactory { public static final String HEADER_UPGRADE = "upgrade"; public static final String HEADER_UPGRADE_VALUE = "websocket"; public static final String HEADER_CONNECTION = "connection"; public static final String HEADER_CONNECTION_VALUE = "Upgrade"; public static final String HEADER_WEBSOCKET_VERSION = "sec-websocket-version"; public static final String HEADER_WEBSOCKET_VERSION_VALUE = "13"; public static final String HEADER_WEBSOCKET_KEY = "sec-websocket-key"; public static final String HEADER_WEBSOCKET_ACCEPT = "sec-websocket-accept"; public static final String HEADER_WEBSOCKET_PROTOCOL = "sec-websocket-protocol"; public static final String WEBSOCKET_KEY_MAGIC = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; public WebSocketFactory webSocketFactory; public NanoWebSocketServer(int port) { super(port); webSocketFactory = null; } public NanoWebSocketServer(String hostname, int port) { super(hostname, port); webSocketFactory = null; } public NanoWebSocketServer(int port, WebSocketFactory webSocketFactory) { super(port); this.webSocketFactory = webSocketFactory; } public NanoWebSocketServer(String hostname, int port, WebSocketFactory webSocketFactory) { super(hostname, port); this.webSocketFactory = webSocketFactory; } @Override public Response serve(final IHTTPSession session) { Map headers = session.getHeaders(); if (isWebsocketRequested(session)) { if (!HEADER_UPGRADE_VALUE.equalsIgnoreCase(headers.get(HEADER_UPGRADE)) || !isWebSocketConnectionHeader(session.getHeaders())) { return newFixedLengthResponse(Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT, "Invalid Websocket handshake"); } if (!HEADER_WEBSOCKET_VERSION_VALUE.equalsIgnoreCase(headers.get(HEADER_WEBSOCKET_VERSION))) { return newFixedLengthResponse(Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT, "Invalid Websocket-Version " + headers.get(HEADER_WEBSOCKET_VERSION)); } if (!headers.containsKey(HEADER_WEBSOCKET_KEY)) { return newFixedLengthResponse(Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT, "Missing Websocket-Key"); } WebSocket webSocket = openWebSocket(session); try { webSocket.getHandshakeResponse().addHeader(HEADER_WEBSOCKET_ACCEPT, makeAcceptKey(headers.get(HEADER_WEBSOCKET_KEY))); } catch (NoSuchAlgorithmException e) { return newFixedLengthResponse(Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "The SHA-1 Algorithm required for websockets is not available on the server."); } if (headers.containsKey(HEADER_WEBSOCKET_PROTOCOL)) { webSocket.getHandshakeResponse().addHeader(HEADER_WEBSOCKET_PROTOCOL, headers.get(HEADER_WEBSOCKET_PROTOCOL).split(",")[0]); } return webSocket.getHandshakeResponse(); } else { return super.serve(session); } } public WebSocket openWebSocket(IHTTPSession handshake) { if (webSocketFactory == null) { throw new Error("You must either override this method or supply a WebSocketFactory in the constructor"); } return webSocketFactory.openWebSocket(handshake); } public boolean isWebsocketRequested(IHTTPSession session) { Map headers = session.getHeaders(); String upgrade = headers.get(HEADER_UPGRADE); boolean isCorrectConnection = isWebSocketConnectionHeader(headers); boolean isUpgrade = HEADER_UPGRADE_VALUE.equalsIgnoreCase(upgrade); return (isUpgrade && isCorrectConnection); } public final boolean isWebSocketConnectionHeader(Map headers) { String connection = headers.get(HEADER_CONNECTION); return (connection != null && connection.toLowerCase().contains(HEADER_CONNECTION_VALUE.toLowerCase())); } public static String makeAcceptKey(String key) throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance("SHA-1"); String text = key + WEBSOCKET_KEY_MAGIC; md.update(text.getBytes(), 0, text.length()); byte[] sha1hash = md.digest(); return base64encode(sha1hash); } } public static interface _SetField { public void _setField(String name, Object value); } public static class DoublePt { public double x, y; public DoublePt() { } public DoublePt(Point p) { x = p.x; y = p.y; } public DoublePt(double x, double y) { this.y = y; this.x = x; } public boolean equals(Object o) { return stdEq2(this, o); } public int hashCode() { return stdHash2(this); } public String toString() { return x + ", " + y; } } public static class CondensedStrings2 extends AbstractList implements RandomAccess { public byte[] data; public int[] indices; public CondensedStrings2() { } public CondensedStrings2(List l) { long totalLength = 0; for (String s : l) totalLength += l(toUtf8(s)); int len = (int) totalLength; if (len != totalLength) throw fail("TOO BIG!!!!!! " + totalLength); data = new byte[len]; int n = l(l); indices = new int[n]; int idx = 0; for (int i = 0; i < n; i++) { String s = l.get(i); try { byte[] x = toUtf8(s); System.arraycopy(x, 0, data, idx, l(x)); indices[i] = idx; idx += l(x); } catch (RuntimeException e) { print("idx=" + idx + ", s=" + s + ", i=" + i + ", n=" + n + ", len=" + len); throw e; } } } public String get(int i) { try { int idx1 = indices[i]; int idx2 = i + 1 < indices.length ? indices[i + 1] : data.length; return fastu_fromUtf8(data, idx1, idx2 - idx1); } catch (Exception __e) { throw rethrow(__e); } } public int size() { return indices.length; } public int totalSize() { return data.length; } } public static class CompactHashSet extends java.util.AbstractSet { public static final int INITIAL_SIZE = 3; public static final double LOAD_FACTOR = 0.75; public static final Object nullObject = new Object(); public static final Object deletedObject = new Object(); public int elements; public int freecells; public A[] objects; public int modCount; public CompactHashSet() { this(INITIAL_SIZE); } public CompactHashSet(int size) { objects = (A[]) new Object[(size == 0 ? 1 : size)]; elements = 0; freecells = objects.length; modCount = 0; } public CompactHashSet(Collection c) { this(c.size()); addAll(c); } @Override public Iterator iterator() { return new CompactHashIterator(); } @Override public int size() { return elements; } @Override public boolean isEmpty() { return elements == 0; } @Override public boolean contains(Object o) { return find(o) != null; } public synchronized A find(Object o) { if (o == null) o = nullObject; int hash = o.hashCode(); int index = (hash & 0x7FFFFFFF) % objects.length; int offset = 1; while (objects[index] != null && !(objects[index].hashCode() == hash && objects[index].equals(o))) { index = ((index + offset) & 0x7FFFFFFF) % objects.length; offset = offset * 2 + 1; if (offset == -1) offset = 2; } return objects[index]; } public boolean removeIfSame(Object o) { A value = find(o); if (value == o) { remove(value); return true; } return false; } @Override public synchronized boolean add(Object o) { if (o == null) o = nullObject; int hash = o.hashCode(); int index = (hash & 0x7FFFFFFF) % objects.length; int offset = 1; int deletedix = -1; while (objects[index] != null && !(objects[index].hashCode() == hash && objects[index].equals(o))) { if (objects[index] == deletedObject) deletedix = index; index = ((index + offset) & 0x7FFFFFFF) % objects.length; offset = offset * 2 + 1; if (offset == -1) offset = 2; } if (objects[index] == null) { if (deletedix != -1) index = deletedix; else freecells--; modCount++; elements++; objects[index] = (A) o; if (1 - (freecells / (double) objects.length) > LOAD_FACTOR) rehash(); return true; } else return false; } @Override public synchronized boolean remove(Object o) { if (o == null) o = nullObject; int hash = o.hashCode(); int index = (hash & 0x7FFFFFFF) % objects.length; int offset = 1; while (objects[index] != null && !(objects[index].hashCode() == hash && objects[index].equals(o))) { index = ((index + offset) & 0x7FFFFFFF) % objects.length; offset = offset * 2 + 1; if (offset == -1) offset = 2; } if (objects[index] != null) { objects[index] = (A) deletedObject; modCount++; elements--; return true; } else return false; } @Override public synchronized void clear() { elements = 0; for (int ix = 0; ix < objects.length; ix++) objects[ix] = null; freecells = objects.length; modCount++; } @Override public synchronized Object[] toArray() { Object[] result = new Object[elements]; Object[] objects = this.objects; int pos = 0; for (int i = 0; i < objects.length; i++) if (objects[i] != null && objects[i] != deletedObject) { if (objects[i] == nullObject) result[pos++] = null; else result[pos++] = objects[i]; } return result; } @Override public synchronized T[] toArray(T[] a) { int size = elements; if (a.length < size) a = (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size); A[] objects = this.objects; int pos = 0; for (int i = 0; i < objects.length; i++) if (objects[i] != null && objects[i] != deletedObject) { if (objects[i] == nullObject) a[pos++] = null; else a[pos++] = (T) objects[i]; } return a; } public void rehash() { int gargagecells = objects.length - (elements + freecells); if (gargagecells / (double) objects.length > 0.05) rehash(objects.length); else rehash(objects.length * 2 + 1); } public void rehash(int newCapacity) { int oldCapacity = objects.length; @SuppressWarnings("unchecked") A[] newObjects = (A[]) new Object[newCapacity]; for (int ix = 0; ix < oldCapacity; ix++) { Object o = objects[ix]; if (o == null || o == deletedObject) continue; int hash = o.hashCode(); int index = (hash & 0x7FFFFFFF) % newCapacity; int offset = 1; while (newObjects[index] != null) { index = ((index + offset) & 0x7FFFFFFF) % newCapacity; offset = offset * 2 + 1; if (offset == -1) offset = 2; } newObjects[index] = (A) o; } objects = newObjects; freecells = objects.length - elements; } public class CompactHashIterator implements Iterator { public int index; public int lastReturned = -1; public int expectedModCount; @SuppressWarnings("empty-statement") public CompactHashIterator() { synchronized (CompactHashSet.this) { for (index = 0; index < objects.length && (objects[index] == null || objects[index] == deletedObject); index++) ; expectedModCount = modCount; } } @Override public boolean hasNext() { synchronized (CompactHashSet.this) { return index < objects.length; } } @SuppressWarnings("empty-statement") @Override public T next() { synchronized (CompactHashSet.this) { int length = objects.length; if (index >= length) { lastReturned = -2; throw new NoSuchElementException(); } lastReturned = index; for (index += 1; index < length && (objects[index] == null || objects[index] == deletedObject); index++) ; if (objects[lastReturned] == nullObject) return null; else return (T) objects[lastReturned]; } } @Override public void remove() { synchronized (CompactHashSet.this) { if (modCount != expectedModCount) throw new ConcurrentModificationException(); if (lastReturned == -1 || lastReturned == -2) throw new IllegalStateException(); if (objects[lastReturned] != null && objects[lastReturned] != deletedObject) { objects[lastReturned] = (A) deletedObject; elements--; modCount++; expectedModCount = modCount; } } } } } public abstract static class VF2 { public abstract void get(A a, B b); } public abstract static class SynchronizedArrayList_Base extends AbstractList { public final int modCount() { return modCount; } public void removeRange(int i, int j) { super.removeRange(i, j); } } public static class ImageSurfaceSelector extends MouseAdapter { public ImageSurface is; public Point startingPoint; public boolean enabled = true; public static boolean verbose = false; public ImageSurfaceSelector(ImageSurface is) { this.is = is; if (containsInstance(is.tools, ImageSurfaceSelector.class)) return; is.tools.add(this); is.addMouseListener(this); is.addMouseMotionListener(this); } public void mousePressed(MouseEvent evt) { if (verbose) print("mousePressed"); if (evt.getButton() != MouseEvent.BUTTON1) return; if (enabled) startingPoint = getPoint(evt); } public void mouseDragged(MouseEvent e) { if (verbose) print("mouseDragged"); if (startingPoint != null) { Point endPoint = getPoint(e); Rectangle r = new Rectangle(startingPoint, new Dimension(endPoint.x - startingPoint.x + 1, endPoint.y - startingPoint.y + 1)); normalize(r); r.width = min(r.width, is.getImage().getWidth() - r.x); r.height = min(r.height, is.getImage().getHeight() - r.y); is.setSelection(r); } if (verbose) print("mouseDragged done"); } public static void normalize(Rectangle r) { if (r.width < 0) { r.x += r.width; r.width = -r.width; } if (r.height < 0) { r.y += r.height; r.height = -r.height; } } public void mouseReleased(MouseEvent e) { if (verbose) print("mouseReleased"); mouseDragged(e); if (getPoint(e).equals(startingPoint)) is.setSelection(null); startingPoint = null; } public Point getPoint(MouseEvent e) { return new Point((int) (e.getX() / is.getZoomX()), (int) (e.getY() / is.getZoomY())); } } public abstract static class Surface extends JPanel { public boolean clearSurface = true; public boolean clearOnce; public Surface() { setDoubleBuffered(false); } public Graphics2D createGraphics2D(int width, int height, Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setBackground(getBackground()); if (clearSurface || clearOnce) { g2.clearRect(0, 0, width, height); clearOnce = false; } return g2; } public abstract void render(int w, int h, Graphics2D g); public void paintImmediately(int x, int y, int w, int h) { RepaintManager repaintManager = null; boolean save = true; if (!isDoubleBuffered()) { repaintManager = RepaintManager.currentManager(this); save = repaintManager.isDoubleBufferingEnabled(); repaintManager.setDoubleBufferingEnabled(false); } super.paintImmediately(x, y, w, h); if (repaintManager != null) repaintManager.setDoubleBufferingEnabled(save); } public void paint(Graphics g) { Dimension d = getSize(); Graphics2D g2 = createGraphics2D(d.width, d.height, g); render(d.width, d.height, g2); g2.dispose(); } } public static boolean bareDBMode_on; public static void bareDBMode() { bareDBMode(null); } public static void bareDBMode(Integer autoSaveInterval) { bareDBMode_on = true; conceptsAndBot(autoSaveInterval); } public static List getAll(Map map, Collection l) { return lookupAllOpt(map, l); } public static List getAll(Collection l, Map map) { return lookupAllOpt(map, l); } public static boolean hasConcept(Class c, Object... params) { return findConceptWhere(c, params) != null; } public static boolean exceptionMessageContains(Throwable e, String s) { return cic(getInnerMessage(e), s); } public static void clearConcepts() { mainConcepts.clearConcepts(); } public static void clearConcepts(Concepts concepts) { concepts.clearConcepts(); } public static int done_minPrint = 10; public static long done(long startTime, String desc) { long time = now() - startTime; if (time >= done_minPrint) print(desc + " [" + time + " ms]"); return time; } public static long done(String desc, long startTime) { return done(startTime, desc); } public static long done(long startTime) { return done(startTime, ""); } public static RemoteDB connectToDBOpt(String dbNameOrID) { try { return new RemoteDB(dbNameOrID); } catch (Throwable __e) { return null; } } public static Concept getConcept(long id) { return mainConcepts.getConcept(id); } public static A getConcept(Class cc, long id) { return getConcept(mainConcepts, cc, id); } public static A getConcept(Concepts concepts, Class cc, long id) { Concept c = concepts.getConcept(id); if (c == null) return null; if (!isInstance(cc, c)) throw fail("Can't convert concept: " + getClassName(c) + " -> " + getClassName(cc) + " (" + id + ")"); return (A) c; } public static int hours() { return hours(Calendar.getInstance()); } public static int hours(Calendar c) { return c.get(Calendar.HOUR_OF_DAY); } public static long toK(long l) { return (l + 1023) / 1024; } public static A firstOfType(Collection c, Class type) { for (Object x : c) if (isInstanceX(type, x)) return (A) x; return null; } public static Collection allConcepts() { return mainConcepts.allConcepts(); } public static Collection allConcepts(Concepts concepts) { return concepts.allConcepts(); } public static List filterByType(Collection c, Class type) { List l = new ArrayList(); for (Object x : c) if (isInstanceX(type, x)) l.add((A) x); return l; } public static List filterByType(Object[] c, Class type) { return filterByType(asList(c), type); } public static List filterByDynamicType(Collection c, String type) { List l = new ArrayList(); for (A x : c) if (eq(dynamicClassName(x), type)) l.add(x); return l; } public static boolean hasType(Collection c, Class type) { for (Object x : c) if (isInstanceX(type, x)) return true; return false; } public static A findBackRef(Concept c, Class type) { for (Concept.Ref r : c.backRefs) if (instanceOf(r.concept(), type)) return (A) r.concept(); return null; } public static A findBackRef(Class type, Concept c) { return findBackRef(c, type); } public static Concept cnew(String name, Object... values) { Class cc = findClass(name); Concept c = cc != null ? nuObject(cc) : new Concept(name); csetAll(c, values); return c; } public static Concept cnew(Concepts concepts, String name, Object... values) { Class cc = findClass(name); concepts_unlisted.set(true); Concept c; try { c = cc != null ? nuObject(cc) : new Concept(name); } finally { concepts_unlisted.set(null); } concepts.register(c); csetAll(c, values); return c; } public static A cnew(Class cc, Object... values) { A c = nuObject(cc); csetAll(c, values); return c; } public static A cnew(Concepts concepts, Class cc, Object... values) { concepts_unlisted.set(true); A c; try { c = nuObject(cc); } finally { concepts_unlisted.set(null); } concepts.register(c); csetAll(c, values); return c; } public static int cset(Concept c, Object... values) { try { if (c == null) return 0; int changes = 0; values = expandParams(c.getClass(), values); warnIfOddCount(values); for (int i = 0; i + 1 < l(values); i += 2) { String field = (String) values[i]; Object value = values[i + 1]; Field f = setOpt_findField(c.getClass(), field); if (value instanceof RC) value = c._concepts.getConcept((RC) value); value = deref(value); if (value instanceof String && l((String) value) >= concepts_internStringsLongerThan) value = intern((String) value); if (f == null) { mapPut2(c.fieldValues, field, value instanceof Concept ? c.new Ref((Concept) value) : value); c.change(); } else if (isSubtypeOf(f.getType(), Concept.Ref.class)) { ((Concept.Ref) f.get(c)).set((Concept) derefRef(value)); c.change(); ++changes; } else { Object old = f.get(c); if (neq(value, old)) { f.set(c, value); if ((f.getModifiers() & java.lang.reflect.Modifier.TRANSIENT) == 0) c.change(); ++changes; } } } return changes; } catch (Exception __e) { throw rethrow(__e); } } public static String loadConceptsStructure(String progID) { return loadTextFilePossiblyGZipped(getProgramFile(progID, "concepts.structure")); } public static String loadConceptsStructure() { return loadConceptsStructure(dbProgramID()); } public static int countConcepts(Class c, Object... params) { return mainConcepts.countConcepts(c, params); } public static int countConcepts() { return mainConcepts.countConcepts(); } public static int countConcepts(String className) { return mainConcepts.countConcepts(className); } public static int countConcepts(Concepts concepts, String className) { return concepts.countConcepts(className); } public static Str concept(String name) { for (Str s : list(Str.class)) if (eqic(s.name, name) || containsIgnoreCase(s.otherNames, name)) return s; return new Str(name); } public static void change() { callOpt(getOptMC("mainConcepts"), "allChanged"); } public static List removeDyn(List l, A a) { if (l == null) return null; l.remove(a); return empty(l) ? null : l; } public static Map mapInConcept(Concept c, Map m) { return new MapInConcept(m, c); } public static String assertGlobalID(String s) { return assertPossibleGlobalID(s); } public static BigInteger plus(BigInteger a, BigInteger b) { return a.add(b); } public static BigInteger plus(BigInteger a, long b) { return a.add(bigint(b)); } public static BigInteger mul(BigInteger a, BigInteger b) { return a.multiply(b); } public static BigInteger mul(BigInteger a, long b) { return a.multiply(bigint(b)); } public static char charPlus(char a, int b) { return (char) (((int) a) + b); } public static int mod(int n, int m) { return (n % m + m) % m; } public static long mod(long n, long m) { return (n % m + m) % m; } public static BigInteger mod(BigInteger n, int m) { return n.mod(bigint(m)); } public static double mod(double n, double m) { return (n % m + m) % m; } public static String div(Object contents, Object... params) { return hfulltag("div", contents, params); } public static BigInteger div(BigInteger a, BigInteger b) { return a.divide(b); } public static BigInteger div(BigInteger a, int b) { return a.divide(bigint(b)); } public static float getScore(Scored s) { return s == null ? 0 : s.score; } public static Set _registerTimer_list = newWeakHashSet(); public static void _registerTimer(java.util.Timer timer) { _registerTimer_list.add(timer); } public static void cleanMeUp__registerTimer() { cancelTimers(getAndClearList(_registerTimer_list)); } public static GlobalID globalIDFromParts(long a, short b) { GlobalID id = new GlobalID(); id.a = a; id.b = b; return id; } public static long globalIDPart1(GlobalID id) { return id == null ? 0 : id.a; } public static short globalIDPart2(GlobalID id) { return id == null ? 0 : (short) id.b; } public static String _compactString(String s) { return s; } public static void add(BitSet bs, int i) { bs.set(i); } public static boolean add(Collection c, A a) { return c != null && c.add(a); } public static String quoteIfNotIdentifier(String s) { if (s == null) return null; return isJavaIdentifier(s) ? s : quote(s); } public static long stopTiming_defaultMin = 10; public static long startTiming_startTime; public static void startTiming() { startTiming_startTime = now(); } public static void stopTiming() { stopTiming(null); } public static void stopTiming(String text) { stopTiming(text, stopTiming_defaultMin); } public static void stopTiming(String text, long minToPrint) { long time = now() - startTiming_startTime; if (time >= minToPrint) { text = or2(text, "Time: "); print(text + time + " ms"); } } public static String formatDateAndTime(long timestamp) { return formatDate(timestamp); } public static String formatDateAndTime() { return formatDate(); } public static UnsupportedOperationException unsupportedOperation() { throw new UnsupportedOperationException(); } public static long waitForBotStartUp_timeoutSeconds = 60; public static String waitForBotStartUp(String botName) { for (int i = 0; i < waitForBotStartUp_timeoutSeconds; i++) { sleepSeconds(i == 0 ? 0 : 1); String addr = getBotAddress(botName); if (addr != null) return addr; } throw fail("Bot not found: " + quote(botName)); } public static Object rpc(String botName, String method, Object... args) { return unstructure_matchOK2OrFail(sendToLocalBot(botName, rpc_makeCall(method, args))); } public static Object rpc(DialogIO bot, String method, Object... args) { return unstructure_matchOK2OrFail(bot.ask(rpc_makeCall(method, args))); } public static String rpc_makeCall(String method, Object... args) { if (empty(args)) return "call " + method; return format("call *", concatLists((List) ll(method), asList(args))); } public static GlobalID aGlobalIDObj() { return asGlobalID(randomID(16)); } public static long nowUnlessLoading() { return now(); } public static String lisp2label(Lisp l) { return l.isLeaf() ? l.head : clUnparse(l); } public static String webToString(Web web) { List out = new ArrayList(); Map index = new HashMap(); for (int i = 0; i < l(web.nodes); i++) { out.add("Node " + (i + 1) + ": " + web.nodes.get(i)); index.put(web.nodes.get(i), i + 1); } for (Pair p : web_relations(web)) out.add(index.get(p.a) + " -> " + index.get(p.b) + " = " + web.getRelation(p)); return fromLines(out); } public static void clearAll(Object... l) { for (Object o : l) callOpt(o, "clear"); } public static String fsi(String id) { return formatSnippetID(id); } public static boolean containsIC(Collection l, String s) { return containsIgnoreCase(l, s); } public static boolean containsIC(String[] l, String s) { return containsIgnoreCase(l, s); } public static boolean containsIC(String s, char c) { return containsIgnoreCase(s, c); } public static boolean containsIC(String a, String b) { return containsIgnoreCase(a, b); } public static void setAddAll(List a, Collection b) { for (A x : b) setAdd(a, x); } public static BufferedImage quickVisualizeOr(String query, String defaultImageID) { BufferedImage img = quickVisualize(query); return img != null ? img : loadImage2(defaultImageID); } public static List getWhere(Collection c, Object... data) { List l = new ArrayList(); for (A x : c) if (checkFields(x, data)) l.add(x); return l; } public static A findWhere(Collection c, Object... data) { for (A x : c) if (checkFields(x, data)) return x; return null; } public static BufferedImage renderTiledBackground(String tileImageID, int w, int h) { return renderTiledBackground(tileImageID, w, h, 0, 0); } public static BufferedImage renderTiledBackground(String tileImageID, int w, int h, int shiftX, int shiftY) { BufferedImage tileImage = loadImage2(tileImageID); BufferedImage img = newBufferedImage(w, h, Color.black); Graphics2D g = img.createGraphics(); int tw = tileImage.getWidth(), th = tileImage.getHeight(); for (int x = mod(shiftX - 1, tw) - tw + 1; x < w; x += tw) for (int y = mod(shiftY - 1, th) - th + 1; y < h; y += th) g.drawImage(tileImage, x, y, null); g.dispose(); return img; } public static int ptX(Pt p) { return p == null ? 0 : p.x; } public static int ptY(Pt p) { return p == null ? 0 : p.y; } public static Map createGraphics_modulators = synchroIdentityHashMap(); public static Graphics2D createGraphics(BufferedImage img) { Graphics2D g = img.createGraphics(); Object mod = createGraphics_modulators.get(img); if (mod != null) callF(mod, g); return g; } public static void createGraphics_modulate(BufferedImage img, Object mod) { mapPut2(createGraphics_modulators, img, mod); } public static DoublePt translateDoublePt(Pt a, DoublePt b) { return a == null ? b : b == null ? new DoublePt(a.x, a.y) : new DoublePt(a.x + b.x, a.y + b.y); } public static int drawThoughtArrow_size = 15; public static void drawThoughtArrow(BufferedImage bg, BufferedImage img1, double x1, double y1, BufferedImage img2, double x2, double y2, Color color) { double cs = thoughtCircleSize(img2) / 2 - 1; double dist = pointDistance(x1, y1, x2, y2); double arrowLen = drawThoughtArrow_size * drawArrowHead_length - 1; DoublePt v = blendDoublePts(new DoublePt(x2, y2), new DoublePt(x1, y1), (cs + arrowLen) / dist); DoublePt p = blendDoublePts(new DoublePt(x2, y2), new DoublePt(x1, y1), cs / dist); Graphics2D g = imageGraphics(bg); g.setColor(color); g.setStroke(new BasicStroke(drawThoughtLine_width)); g.draw(new Line2D.Double(x1, y1, v.x, v.y)); drawArrowHead(g, x1, y1, p.x, p.y, drawThoughtArrow_size); g.dispose(); } public static int drawThoughtLine_width = 10; public static void drawThoughtLine(BufferedImage bg, BufferedImage img1, int x1, int y1, BufferedImage img2, int x2, int y2, Color color) { Graphics2D g = imageGraphics(bg); g.setColor(color); g.setStroke(new BasicStroke(drawThoughtLine_width)); g.drawLine(x1, y1, x2, y2); g.dispose(); } public static int drawThoughtLineText_shift = 10; public static void drawThoughtLineText_multiLine(BufferedImage bg, BufferedImage img1, int x1, int y1, BufferedImage img2, int x2, int y2, String text, Color color) { Graphics2D g = imageGraphics(bg); g.setColor(color); g.setFont(sansSerif(20)); int w1 = img_minOfWidthAndHeight(img1); int w2 = img_minOfWidthAndHeight(img2); int sideShift = (w1 - w2) / 4; int len = vectorLength(x2 - x1, y2 - y1); int dx = iround(sideShift * (x2 - x1) / len), dy = iround(sideShift * (y2 - y1) / len); x1 += dx; x2 += dx; y1 += dy; y2 += dy; FontMetrics fm = g.getFontMetrics(); int lineHeight = fm.getHeight(); float yshift = 0; for (String line : reversed(lines(text))) { drawOutlineTextAlongLine(g, line, x1, y1, x2, y2, drawThoughtLine_width / 2 + drawThoughtLineText_shift, yshift, color, Color.black); yshift -= lineHeight; } g.dispose(); } public static Color drawThoughtCircle_defaultColor = Color.white; public static void drawThoughtCircle(BufferedImage bg, BufferedImage img, double x, double y) { BufferedImage circle = cutImageToCircle(img_addBorder(cutImageToCircle(img), drawThoughtCircle_defaultColor, 10)); x -= circle.getWidth() / 2; y -= circle.getHeight() / 2; drawImageOnImage(circle, bg, iround(x), iround(y)); } public static int drawThoughtCircleText_margin = 5; public static Color drawThoughtCircleText_color = Color.yellow; public static void drawThoughtCircleText(BufferedImage bg, BufferedImage img, DoublePt p, String text) { Graphics2D g = imageGraphics(bg); g.setFont(sansSerifBold(20)); FontMetrics fm = g.getFontMetrics(); int h = fm.getHeight(); double y = p.y + thoughtCircleSize(img) / 2 + drawThoughtCircleText_margin; for (String s : lines(text)) { drawTextWithOutline(g, s, (float) (p.x - fm.stringWidth(s) / 2), (float) (y + fm.getLeading() + fm.getMaxAscent()), drawThoughtCircleText_color, Color.black); y += h; } g.dispose(); } public static BufferedImage drawThoughtCirclePlus_img; public static int drawThoughtCirclePlus_size = 24; public static void drawThoughtCirclePlus(BufferedImage bg, BufferedImage img, double x, double y) { if (drawThoughtCirclePlus_img == null) drawThoughtCirclePlus_img = resizeImage(loadImage2("#1009864"), drawThoughtCirclePlus_size); x -= drawThoughtCirclePlus_img.getWidth() / 2; y -= drawThoughtCirclePlus_img.getHeight() / 2; drawImageOnImage(drawThoughtCirclePlus_img, bg, iround(x), iround(y)); } public static JFrame frameInnerSize(final Component c, final double w, final double h) { final JFrame frame = getFrame(c); if (frame != null) { swing(new Runnable() { public void run() { try { Container cp = frame.getContentPane(); Dimension oldSize = cp.getPreferredSize(); cp.setPreferredSize(new Dimension(iround(w), iround(h))); frame.pack(); cp.setPreferredSize(oldSize); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Container cp = frame.getContentPane();\r\n Dimension oldSize = cp.getPreferr..."; } }); } return frame; } public static void frameInnerSize(JFrame frame, Dimension d) { frameInnerSize(frame, d.width, d.height); } public static JFrame frameInnerSize(Pt p, JFrame frame) { frameInnerSize(frame, p.x, p.y); return frame; } public static A centerFrame(A c) { Window w = getWindow(c); if (w != null) w.setLocationRelativeTo(null); return c; } public static A centerFrame(int w, int h, A c) { return centerFrame(setFrameSize(w, h, c)); } public static JFrame getFrame(final Object _o) { return swing(new F0() { public JFrame get() { try { Object o = _o; if (o instanceof ButtonGroup) o = first(buttonsInGroup((ButtonGroup) o)); if (!(o instanceof Component)) return null; Component c = (Component) o; while (c != null) { if (c instanceof JFrame) return (JFrame) c; c = c.getParent(); } return null; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "O o = _o;\r\n if (o instanceof ButtonGroup) o = first(buttonsInGroup((Button..."; } }); } public static JFrame showCenterFrame(String title, int w, int h) { return showCenterFrame(title, w, h, null); } public static JFrame showCenterFrame(int w, int h, Component content) { return showCenterFrame(defaultFrameTitle(), w, h, content); } public static JFrame showCenterFrame(String title, int w, int h, Component content) { JFrame frame = makeFrame(title, content); frame.setSize(w, h); return centerFrame(frame); } public static JFrame showCenterFrame(String title, Component content) { return centerFrame(makeFrame(title, content)); } public static JFrame showCenterFrame(Component content) { return centerFrame(makeFrame(content)); } public static class Canvas extends ImageSurface { public Object makeImg; public boolean updating; public Canvas() { zoomable = false; } public Canvas(Object makeImg) { this(); this.makeImg = makeImg; } public void update() { updateCanvas(this, makeImg); } } public static Canvas jcanvas() { return jcanvas(null, 0); } public static Canvas jcanvas(Object f) { return jcanvas(f, 0); } public static Canvas jcanvas(final Object f, final int updateDelay) { return (Canvas) swing(new F0() { public Object get() { try { final Canvas is = new Canvas(f); is.specialPurposed = true; final Runnable update = new Runnable() { public boolean first = true; public void run() { BufferedImage img = is.getImage(); int w = is.getWidth(), h = is.getHeight(); if (first || img.getWidth() != w || img.getHeight() != h) { updateCanvas(is, f); first = false; } } }; onResize(is, new Runnable() { public void run() { try { awtLater(is, updateDelay, update); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "awtLater(is, updateDelay, update)"; } }); bindToComponent(is, update); return is; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "final Canvas is = new Canvas(f);\r\n is.specialPurposed = true;\r\n final R..."; } }); } public static void disableImageSurfaceSelector(ImageSurface is) { ImageSurfaceSelector s = firstInstance(is.tools, ImageSurfaceSelector.class); if (s == null) return; is.removeMouseListener(s); is.removeMouseMotionListener(s); is.tools.add(s); } public static int updateCanvas_retryInterval = 50; public static void updateCanvas(final Canvas canvas, final Object makeImg) { swingNowOrLater(new Runnable() { public void run() { try { if (canvas.updating || canvas.getWidth() == 0) return; canvas.updating = true; try { BufferedImage img = asBufferedImage(callF(makeImg, canvas.getWidth(), canvas.getHeight())); if (img != null) { canvas.setImage(img); canvas.updating = false; } else awtLater(updateCanvas_retryInterval, new Runnable() { public void run() { try { canvas.updating = false; updateCanvas(canvas, makeImg); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "canvas.updating = false;\r\n updateCanvas(canvas, makeImg);"; } }); } catch (Throwable e) { canvas.updating = false; throw rethrow(e); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (canvas.updating || canvas.getWidth() == 0) return;\r\n canvas.updating =..."; } }); } public static void updateCanvas(final Canvas canvas) { if (canvas != null) canvas.update(); } public static Map componentPopupMenu_map; public static ThreadLocal componentPopupMenu_mouseEvent; public static void componentPopupMenu_init() { { swing(new Runnable() { public void run() { try { if (componentPopupMenu_map == null) componentPopupMenu_map = or((Map) getOpt(creator(), "componentPopupMenu_map"), (Map) (Map) newWeakHashMap()); if (componentPopupMenu_mouseEvent == null) componentPopupMenu_mouseEvent = or((ThreadLocal) getOpt(creator(), "componentPopupMenu_mouseEvent"), new ThreadLocal()); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (componentPopupMenu_map == null)\r\n componentPopupMenu_map = or((Map() { public JMenu get() { try { JMenu menu = new JMenu(title); fillJMenu(menu, items); return menu; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JMenu menu = new(title);\r\n fillJMenu(menu, items);\r\n ret menu;"; } }); } public static void moveAllMenuItems(JPopupMenu src, JMenu dest) { Component[] l = src.getComponents(); src.removeAll(); for (Component c : l) dest.add(c); } public static void addMenuItem(JPopupMenu menu, String text, Object action) { menu.add(jmenuItem(text, action)); } public static void addMenuItem(JPopupMenu menu, JMenuItem menuItem) { menu.add(menuItem); } public static void addMenuItem(JMenu menu, String text, Object action) { menu.add(jmenuItem(text, action)); } public static void addMenuItem(Menu menu, String text, Object action) { menu.add(menuItem(text, action)); } public static void addMenuItem(JMenu menu, JMenuItem menuItem) { menu.add(menuItem); } public static String copyTextToClipboard(String text) { StringSelection selection = new StringSelection(text); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, selection); return text; } public static String cal_simplifiedStructure(CirclesAndLines cal) { return cal_simplifiedStructure(structure(cal)); } public static String cal_simplifiedStructure(String structure) { CirclesAndLines cal = (CirclesAndLines) (unstructure(structure)); for (Circle c : cal.circles) { c.x = roundToOneHundredth(c.x); c.y = roundToOneHundredth(c.y); } return cal_structure_impl(cal); } public static String getTextFromClipboard() { try { Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) return (String) transferable.getTransferData(DataFlavor.stringFlavor); return null; } catch (Exception __e) { throw rethrow(__e); } } public static JTextField jtextfield() { return jTextField(); } public static JTextField jtextfield(String text) { return jTextField(text); } public static JTextField jtextfield(Object o) { return jTextField(o); } public static int showForm_defaultGap = 4; public static int showForm_gapBetweenColumns = 10; public static JPanel showFormTitled(final String title, final Object... _parts) { JDesktopPane desktop = mainDesktopPane(); if (desktop != null) return showInternalFrameFormTitled(desktop, title, _parts); return swing(new F0() { public JPanel get() { try { final Var frame = new Var(); JPanel panel = showForm_makePanel(false, _parts); frame.set(showForm_makeFrame(title, panel)); return panel; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "final new Var frame;\r\n JPanel panel = showForm_makePanel(false, _p..."; } }); } public static JPanel showForm_makePanel(Boolean internalFrame, Object... _parts) { List out = showForm_arrange1(showForm_makeComponents(internalFrame, _parts)); return vstackWithSpacing(out, showForm_defaultGap); } public static Runnable runnableThread(final Runnable r) { return new Runnable() { public void run() { try { new Thread(r).start(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "new Thread(r).start()"; } }; } public static String getTextTrim(JTextComponent c) { return trim(getText(c)); } public static String getTextTrim(JComboBox cb) { return trim(getText(cb)); } public static String getTextTrim(JComponent c) { if (c instanceof JLabel) return trim(((JLabel) c).getText()); if (c instanceof JComboBox) return getTextTrim((JComboBox) c); return getTextTrim((JTextComponent) c); } public static Pt untranslatePt(Pt a, Pt b) { if (a == null) return b; return new Pt(b.x - a.x, b.y - a.y); } public static double pointDistance(Pt a, Pt b) { return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y)); } public static double pointDistance(double x1, double y1, double x2, double y2) { return sqrt(sqr(x1 - x2) + sqr(y1 - y2)); } public static BufferedImage scaleImage(BufferedImage before, double scale) { if (scale == 1) return before; int w = before.getWidth(); int h = before.getHeight(); int neww = max(1, iround(w * scale)), newh = max(1, iround(h * scale)); BufferedImage after = new BufferedImage(neww, newh, BufferedImage.TYPE_INT_ARGB); AffineTransform at = new AffineTransform(); at.scale(scale, scale); AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR); return scaleOp.filter(before, after); } public static String web_defaultRelationName = ""; public static String web_defaultRelationName() { return web_defaultRelationName; } public static AutoComboBox autoComboBox() { return autoComboBox(new ArrayList()); } public static AutoComboBox autoComboBox(final Collection items) { return swing(new F0() { public AutoComboBox get() { try { AutoComboBox cb = new AutoComboBox(); cb.setKeyWord(items); return cb; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "new AutoComboBox cb;\r\n cb.setKeyWord(items);\r\n ret cb;"; } }); } public static AutoComboBox autoComboBox(String value, Collection items) { return setText(autoComboBox(items), value); } public static void awtLater(int delay, final Object r) { swingLater(delay, r); } public static void awtLater(Object r) { swingLater(r); } public static void awtLater(JComponent component, int delay, Object r) { installTimer(component, r, delay, delay, false); } public static void awtLater(JFrame frame, int delay, Object r) { awtLater(frame.getRootPane(), delay, r); } public static void requestFocus(final JComponent c) { focus(c); } public static String cal_structure(CirclesAndLines cal) { return cal_structure_impl(restructure(cal)); } public static String cal_structure_impl(CirclesAndLines cal) { if (cal.arrowClass == Arrow.class) cal.arrowClass = null; if (cal.circleClass == Circle.class) cal.circleClass = null; for (Circle c : cal.circles) cal_structure_simplifyElement(c); for (Line l : cal.lines) cal_structure_simplifyElement(l); return structure(cal); } public static void cal_structure_simplifyElement(Base b) { if (eq(b.traits, ll(b.text))) b.traits = null; } public static void markWebsPosted() { markWebsPosted_createMarker(); triggerWebsChanged(); } public static BufferedImage whiteImage(int w, int h) { return newBufferedImage(w, h, Color.white); } public static double distancePointToLineSegment(Pt a, Pt b, Pt p) { int x1 = a.x, y1 = a.y, x2 = b.x, y2 = b.y, x3 = p.x, y3 = p.y; float px = x2 - x1; float py = y2 - y1; float temp = (px * px) + (py * py); float u = ((x3 - x1) * px + (y3 - y1) * py) / (temp); if (u > 1) u = 1; else if (u < 0) u = 0; float x = x1 + u * px; float y = y1 + u * py; float dx = x - x3; float dy = y - y3; return sqrt(dx * dx + dy * dy); } public static Pt scalePt(Pt p, double f) { return new Pt(iround(p.x * f), iround(p.y * f)); } public static Pt pt(int x, int y) { return new Pt(x, y); } public static int thoughtCircleSize(BufferedImage img) { return min(img.getWidth(), img.getHeight()) + 20; } public static boolean containsInstance(Iterable i, Class c) { if (i != null) for (Object o : i) if (isInstanceX(c, o)) return true; return false; } public static File tempDir() { return makeTempDir(); } public static File createTempFile(String prefix, String suffix) { try { return File.createTempFile(takeFirst(10, pad(nohup_sanitize(prefix), 3, '-')), nohup_sanitize(suffix)); } catch (Exception __e) { throw rethrow(__e); } } public static A wrapStuff(String name, A object, Object... args) { return or((A) callOpt(mc(), "_wrap", object, asList(args)), object); } public static void printMyIPs() { String ip; try { Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); if (iface.isLoopback() || !iface.isUp()) continue; Enumeration addresses = iface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); ip = addr.getHostAddress(); if (ip.startsWith("127.")) continue; boolean local = addr.isSiteLocalAddress() || ip.startsWith("fe"); System.out.println(iface.getDisplayName() + " " + ip + " " + (local ? "(private address)" : "(public address)")); } } } catch (Throwable e) { e.printStackTrace(); } } public static Map putAll(Map a, Map b) { if (a != null) a.putAll(b); return a; } public static void remove(List l, int i) { if (l != null && i >= 0 && i < l(l)) l.remove(i); } public static void remove(Collection l, A a) { if (l != null) l.remove(a); } public static Set keySet(Map map) { return map == null ? new HashSet() : map.keySet(); } public static Set keySet(Object map) { return keys((Map) map); } public static Set keySet(MultiSet ms) { return ms.keySet(); } public static Set keySet(MultiMap mm) { return mm.keySet(); } public static Set keySet(SmallestListMultiMap mm) { return mm.keySet(); } public static char charAt(String s, int i) { return s != null && i >= 0 && i < s.length() ? s.charAt(i) : '\0'; } public static A tripleAtPosition(T3 t, int i) { if (t == null) return null; if (i == 0) return t.a; if (i == 1) return t.b; if (i == 2) return t.c; return null; } public static String ai_currentMaker() { return postSoftwareMadeWeb_maker.get(); } public static WebSocketFactory webSocketFactory(final Object factory) { if (factory instanceof WebSocketFactory) return ((WebSocketFactory) factory); if (factory == null) return null; return new WebSocketFactory() { public WebSocket openWebSocket(NanoHTTPD.IHTTPSession handshake) { return (WebSocket) callF(factory, handshake); } }; } public static void nanohttpd_socketTimeoutFix() { NanoHTTPD.SOCKET_READ_TIMEOUT = 24 * 3600 * 1000; } public static String getServerTranspiled2(String id) { String transpiled = loadCachedTranspilation(id); String md5 = null; if (isOfflineMode()) return transpiled; if (transpiled != null) md5 = md5(transpiled); String transpiledSrc = getServerTranspiled(formatSnippetID(id), md5); if (eq(transpiledSrc, "SAME")) { if (!isTrue(loadPage_silent.get())) print("SAME"); return transpiled; } return transpiledSrc; } public static boolean useDummyMainClasses() { return true; } public static boolean tok_classHasModifier(List classDecl, String modifier) { int i = classDecl.indexOf("class"); return subList(classDecl, 0, i).contains(modifier); } public static List findMainClass(List tok) { for (List c : reversedList(allClasses(tok))) { String name = getClassDeclarationName(c); if (eq(name, "main") || name.startsWith("x")) return c; } return findBlock("m {", tok); } public static boolean javaCompileToJar_useRAMDisk; public static File javaCompileToJar_optionalRename(String src, File destJar, String progID) { return javaCompileToJar_optionalRename(src, "", destJar, progID); } public static synchronized File javaCompileToJar_optionalRename(String src, String dehlibs, File destJar, String progID) { String javaTarget = null; String dummyClass = "main"; if (progID != null) { dummyClass = dummyMainClassName(progID); src += "\nclass " + dummyClass + "{}"; } String md5 = md5(src); File jar = destJar; Class j = getJavaX(); if (javaTarget != null) setOpt(j, "javaTarget", javaTarget); File srcDir = tempDir(); String className = "main"; String fileName = dummyClass + ".java"; File mainJava = new File(srcDir, fileName); saveTextFile(mainJava, src); File classesDir = javaCompileToJar_useRAMDisk ? tempDirPossiblyInRAMDisk() : tempDir(); try { List libraries = new ArrayList(); Matcher m = Pattern.compile("\\d+").matcher(dehlibs); while (m.find()) { String libID = m.group(); assertTrue(isSnippetID(libID)); libraries.add(loadLibrary(libID)); } String compilerOutput; try { compilerOutput = (String) call(j, "compileJava", srcDir, libraries, classesDir); } catch (Throwable e) { compilerOutput = (String) get(getJavaX(), "javaCompilerOutput"); throw fail("Compile Error. " + compilerOutput + " " + e); } if (nempty(compilerOutput)) { print("Compiler said: " + compilerOutput); } if (!new File(classesDir, className + ".class").exists()) throw fail("No class generated (" + className + ")"); saveTextFile(new File(classesDir, "main.java"), src); if (nempty(dehlibs)) saveTextFile(new File(classesDir, "libraries"), dehlibs); dir2zip_recurse_verbose = false; int n = dir2zip_recurse(classesDir, jar); return jar; } finally { if (isInRAMDisk(classesDir)) deleteDirectory(classesDir); } } public static void touchFile(File file) { try { closeRandomAccessFile(newRandomAccessFile(mkdirsForFile(file), "rw")); } catch (Exception __e) { throw rethrow(__e); } } public static int _hashCode(Object a) { return a == null ? 0 : a.hashCode(); } public static String quoteBorderless(Object o) { if (o == null) return "null"; return quoteBorderless(str(o)); } public static String quoteBorderless(String s) { if (s == null) return "null"; StringBuilder out = new StringBuilder((int) (l(s) * 1.5)); quoteBorderless_impl(s, out); return out.toString(); } public static void quoteBorderless_impl(String s, StringBuilder out) { int l = s.length(); for (int i = 0; i < l; i++) { char c = s.charAt(i); if (c == '\\' || c == '"') out.append('\\').append(c); else if (c == '\r') out.append("\\r"); else if (c == '\n') out.append("\\n"); else out.append(c); } } public static int hashCodeFor(Object a) { return a == null ? 0 : a.hashCode(); } public static String systemHashCodeHex(Object o) { return intToHex(identityHashCode(o)); } public static Object smallestList_add(Object l, Object o) { if (l == null) return o; if (l instanceof Pair) return triple(((Pair) l).a, ((Pair) l).b, o); if (l instanceof T3) return new Object[] { ((T3) l).a, ((T3) l).b, ((T3) l).c, o }; if (l instanceof Object[]) return addToObjectArray((Object[]) l, o); return pair(l, o); } public static boolean smallestList_contains(Object l, Object o) { if (l == null) return false; if (l instanceof Pair) return pairContains(((Pair) l), o); if (l instanceof T3) return tripleContains(((T3) l), o); if (l instanceof Object[]) return contains((Object[]) l, o); return l == o; } public static List smallestList_toList(Object l) { return smallestList_asList(l); } public static Object smallestList_remove(Object l, Object o) { int i = smallestList_indexOf(l, o); if (i < 0) return l; List list = smallestList_toMutableList(l); list.remove(i); return smallestList_fromList(list); } public static Object smallestList_get(Object l, int i) { if (l == null) return null; if (l instanceof Pair) return i == 0 ? ((Pair) l).a : i == 1 ? ((Pair) l).b : null; if (l instanceof T3) return i == 0 ? ((T3) l).a : i == 1 ? ((T3) l).b : i == 2 ? ((T3) l).c : null; if (l instanceof Object[]) return get((Object[]) l, i); return i == 0 ? l : null; } public static int smallestList_l(Object l) { if (l == null) return 0; if (l instanceof Pair) return 2; if (l instanceof T3) return 3; if (l instanceof Object[]) return l((Object[]) l); return 1; } public static boolean setText_opt = true; public static A setText(A c, Object text) { setText((Object) c, text); return c; } public static A setText(final A c, Object text) { final String s = strUnnull(text); { swing(new Runnable() { public void run() { try { c.getEditor().setItem(s); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "c.getEditor().setItem(s);"; } }); } return c; } public static void setText(JLabel c, Object text) { setText((Object) c, text); } public static JButton setText(JButton c, Object text) { setText((Object) c, jlabel_textAsHTML_center_ifNeeded(strUnnull(text))); return c; } public static A setText(final A c, Object text) { if (c == null) return null; final String s = strUnnull(text); swingAndWait(new Runnable() { public void run() { try { if (!setText_opt || neq(callOpt(c, "getText"), s)) call(c, "setText", s); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (!setText_opt || neq(callOpt(c, \"getText\"), s))\r\n call(c, \"setText\", s);"; } }); return c; } public static A setToolTipText(final A c, final Object toolTip) { if (c == null) return null; { swing(new Runnable() { public void run() { try { String s = nullIfEmpty(str(toolTip)); if (neq(s, c.getToolTipText())) c.setToolTipText(s); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "String s = nullIfEmpty(str(toolTip));\r\n if (neq(s, c.getToolTipText()))\r\n ..."; } }); } return c; } public static A setToolTipText(Object toolTip, A c) { return setToolTipText(c, toolTip); } public static String nullIfEmpty(String s) { return isEmpty(s) ? null : s; } public static Map nullIfEmpty(Map map) { return isEmpty(map) ? null : map; } public static MultiSet caseInsensitiveMultiSet() { MultiSet ms = new MultiSet(); ms.map = caseInsensitiveMap(); return ms; } public static SyncListMultiMap symbolSyncListMultiMap() { return caseInsensitiveSyncListMultiMap(); } public static ExactTripleIndex exactTripleIndex() { return new ExactTripleIndex(); } public static class ExactTripleHasher implements Hasher { public int hashCode(TripleWeb w) { return tripleHashCodeByContentsIC(w); } public boolean equals(TripleWeb a, TripleWeb b) { return tripleEqualsByContentsIC(a, b); } } public static class ExactTripleIndex extends CustomCompactHashSet { public ExactTripleIndex() { super(new ExactTripleHasher()); } } public static List combineLists3(List a, List b, List c) { return combineLists(a, combineLists(b, c)); } public static List ai_triplesToWebNodes_lazyList(final String searchTerm, final List l) { return new LazyList() { public final int n = l(l); public int size() { return n; } public WebNode get(int i) { TripleWeb w = syncGet(l, i); if (w == null) return null; int runLength = 0; while (i - runLength > 0 && l.get(i - runLength - 1) == w) ++runLength; return ai_tripleToWebNode(l.get(i), searchTerm, runLength); } }; } public static boolean ai_triplesToTripleRefs_lazyList_debug; public static List> ai_triplesToTripleRefs_lazyList(final String searchTerm, final List l) { if (l == null) return null; class TTTRList extends LazyList> { public final int n = l(l); public int size() { return n; } public TripleRef get(int i) { if (ai_triplesToTripleRefs_lazyList_debug) print("tttrlist.get " + i + "/" + l(l)); TripleWeb w = syncGet(l, i); if (w == null) return null; int runLength = 0; while (i - runLength > 0 && l.get(i - runLength - 1) == w) ++runLength; return ai_tripleToTripleRef(l.get(i), searchTerm, runLength); } } return new TTTRList(); } public static TripleWeb dummyTripleWebWithGlobalID(GlobalID id) { TripleWeb w = new TripleWeb(); w.globalID(id); return w; } public static void ai_fireNewTriple(TripleWeb w) { for (TripleListener l : ai_tripleListeners()) try { l.onNewTriple(w); } catch (Throwable __e) { _handleException(__e); } } public static HashSet asHashSet(Collection c) { synchronized (collectionMutex(c)) { return new HashSet(c); } } public static HashSet asHashSet(A[] a) { return a == null ? null : new HashSet(Arrays.asList(a)); } public static void ai_fireForgottenTriple(TripleWeb w) { for (TripleListener l : ai_tripleListeners()) try { l.onForgottenTriple(w); } catch (Throwable __e) { _handleException(__e); } } public static void removeSetFromListQuickly(List l, Collection _set) { Set set = asSet(_set); if (l(set) < 10) { ListIterator it = l.listIterator(); while (it.hasNext()) if (set.contains(it.next())) it.remove(); } else { List l2 = emptyListWithCapacity(l); for (A a : l) if (!set.contains(a)) l2.add(a); copyList(l2, l); } } public static void trimToSizeAll(Collection> l) { if (l != null) for (List ll : l) trimToSize(ll); } public static void ai_sortTriplesListByPriority_inPlace(List l) { sortInPlaceByCalculatedFieldDesc_f1(l, new F1() { public Boolean get(TripleWeb w) { try { return ai_isPriorityTriple(w); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ai_isPriorityTriple(w)"; } }); } public static List shortestList2(List a, List b) { return l(a) < l(b) ? a : b; } public static boolean tripleEqic(T3 a, T3 b) { if (a == null) return b == null; else if (b == null) return false; return eqic(a.a, b.a) && eqic(a.b, b.b) && eqic(a.c, b.c); } public static boolean tripleEqic(T3 a, String ba, String bb, String bc) { if (a == null) return false; return eqic(a.a, ba) && eqic(a.b, bb) && eqic(a.c, bc); } public static boolean inRange(int x, int n) { return x >= 0 && x < n; } public static boolean containsKey(Map map, A key) { return map != null && map.containsKey(key); } public static void componentPopupMenu2(A component, final VF2 menuMaker) { final WeakReference ref = new WeakReference(component); componentPopupMenu(component, new VF1() { public void get(JPopupMenu menu) { try { callF(menuMaker, ref.get(), menu); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callF(menuMaker, ref!, menu);"; } }); } public static void jHandleFileDrop(JComponent c, final Object onDrop) { new DropTarget(c, new DropTargetAdapter() { public void drop(DropTargetDropEvent e) { try { Transferable tr = e.getTransferable(); DataFlavor[] flavors = tr.getTransferDataFlavors(); for (DataFlavor flavor : flavors) { if (flavor.isFlavorJavaFileListType()) { e.acceptDrop(e.getDropAction()); File file = first((List) tr.getTransferData(flavor)); if (file != null && !isFalse(callF(onDrop, file))) e.dropComplete(true); return; } } } catch (Throwable __e) { _handleException(__e); } e.rejectDrop(); } }); } public static JFrame showFullScreen(JComponent c) { return showFullScreen(defaultFrameTitle(), c); } public static JFrame showFullScreen(final String title, final JComponent c) { return (JFrame) swingAndWait(new F0() { public Object get() { try { GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); if (!gd.isFullScreenSupported()) throw fail("No full-screen mode supported!"); boolean dec = JFrame.isDefaultLookAndFeelDecorated(); if (dec) JFrame.setDefaultLookAndFeelDecorated(false); final JFrame window = new JFrame(title); window.setUndecorated(true); if (dec) JFrame.setDefaultLookAndFeelDecorated(true); registerEscape(window, new Runnable() { public void run() { try { disposeWindow(window); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "disposeWindow(window)"; } }); window.add(wrap(c)); gd.setFullScreenWindow(window); for (int i = 100; i <= 1000; i += 100) awtLater(i, new Runnable() { public void run() { try { window.toFront(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "window.toFront()"; } }); return window; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()\r\n ..."; } }); } public static void copyImageToClipboard(Image img) { TransferableImage trans = new TransferableImage(img); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trans, null); print("Copied image to clipboard (" + img.getWidth(null) + "*" + img.getHeight(null) + " px)"); } public static BufferedImage clipBufferedImage(BufferedImage src, Rectangle clip) { return clipBufferedImage(src, new Rect(clip)); } public static BufferedImage clipBufferedImage(BufferedImage src, Rect r) { if (src == null) return null; r = intersectRects(r, new Rect(0, 0, src.getWidth(), src.getHeight())); if (rectEmpty(r)) return null; return src.getSubimage(r.x, r.y, r.w, r.h); } public static BufferedImage clipBufferedImage(BufferedImage src, int x, int y, int w, int h) { return clipBufferedImage(src, new Rect(x, y, w, h)); } public static BufferedImage getImageFromClipboard() { try { Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.imageFlavor)) return (BufferedImage) transferable.getTransferData(DataFlavor.imageFlavor); return imageFromDataURL(getTextFromClipboard()); } catch (Exception __e) { throw rethrow(__e); } } public static void popup(final Throwable throwable) { popupError(throwable); } public static void popup(final String msg) { print(msg); SwingUtilities.invokeLater(new Runnable() { public void run() { JOptionPane.showMessageDialog(null, msg); } }); } public static boolean hasTransparency(BufferedImage img) { return img.getColorModel().hasAlpha(); } public static void revalidate(final Component c) { if (c == null || !c.isShowing()) return; { swing(new Runnable() { public void run() { try { c.revalidate(); c.repaint(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "// magic combo to actually relayout and repaint\r\n c.revalidate();\r\n c.r..."; } }); } } public static void revalidate(RootPaneContainer c) { revalidate((Component) c); } public static void revalidate(JFrame f) { revalidate((Component) f); } public static void revalidate(JInternalFrame f) { revalidate((Component) f); } public static A repaint(A c) { if (c != null) c.repaint(); return c; } public static Dimension getMinimumSize(final Component c) { return c == null ? null : swing(new F0() { public Dimension get() { try { return c.getMinimumSize(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "c.getMinimumSize()"; } }); } public static Container getParent(final Component c) { return c == null ? null : swing(new F0() { public Container get() { try { return c.getParent(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "c.getParent()"; } }); } public static JScrollPane enclosingScrollPane(Component c) { while (c.getParent() != null && !(c.getParent() instanceof JViewport) && c.getParent().getComponentCount() == 1) c = c.getParent(); if (!(c.getParent() instanceof JViewport)) return null; c = c.getParent().getParent(); return c instanceof JScrollPane ? (JScrollPane) c : null; } public static void showFullScreenImageSurface(BufferedImage img) { showFullScreen(jscroll_centered(disposeFrameOnClick(new ImageSurface(img)))); } public static int asInt(Object o) { return toInt(o); } public static Object[] toArray(Collection c) { return toObjectArray(c); } public static A[] toArray(Collection c, Class type) { A[] a = arrayOfType(l(c), type); if (a.length == 0) return a; asList(c).toArray(a); return a; } public static String base64encode(byte[] a) { int aLen = a.length; int numFullGroups = aLen / 3; int numBytesInPartialGroup = aLen - 3 * numFullGroups; int resultLen = 4 * ((aLen + 2) / 3); StringBuffer result = new StringBuffer(resultLen); char[] intToAlpha = intToBase64; int inCursor = 0; for (int i = 0; i < numFullGroups; i++) { int byte0 = a[inCursor++] & 0xff; int byte1 = a[inCursor++] & 0xff; int byte2 = a[inCursor++] & 0xff; result.append(intToAlpha[byte0 >> 2]); result.append(intToAlpha[(byte0 << 4) & 0x3f | (byte1 >> 4)]); result.append(intToAlpha[(byte1 << 2) & 0x3f | (byte2 >> 6)]); result.append(intToAlpha[byte2 & 0x3f]); } if (numBytesInPartialGroup != 0) { int byte0 = a[inCursor++] & 0xff; result.append(intToAlpha[byte0 >> 2]); if (numBytesInPartialGroup == 1) { result.append(intToAlpha[(byte0 << 4) & 0x3f]); result.append("=="); } else { int byte1 = a[inCursor++] & 0xff; result.append(intToAlpha[(byte0 << 4) & 0x3f | (byte1 >> 4)]); result.append(intToAlpha[(byte1 << 2) & 0x3f]); result.append('='); } } return result.toString(); } public static final char[] intToBase64 = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; public static String fastu_fromUtf8(byte[] data) { return fastu_fromUtf8(data, 0, data.length); } public static String fastu_fromUtf8(byte[] data, int i, int n) { char[] chars = new char[n]; int len = 0, offset = i; n += i; while (offset < n) { if (data[offset] >= 0) { chars[len] = (char) data[offset]; len++; offset++; } else { int uc = 0; if ((data[offset] & 0xE0) == 0xC0) { uc = (int) (data[offset] & 0x1F); offset++; uc <<= 6; uc |= (int) (data[offset] & 0x3F); offset++; } else if ((data[offset] & 0xF0) == 0xE0) { uc = (int) (data[offset] & 0x0F); offset++; uc <<= 6; uc |= (int) (data[offset] & 0x3F); offset++; uc <<= 6; uc |= (int) (data[offset] & 0x3F); offset++; } else if ((data[offset] & 0xF8) == 0xF0) { uc = (int) (data[offset] & 0x07); offset++; uc <<= 6; uc |= (int) (data[offset] & 0x3F); offset++; uc <<= 6; uc |= (int) (data[offset] & 0x3F); offset++; uc <<= 6; uc |= (int) (data[offset] & 0x3F); offset++; } else if ((data[offset] & 0xFC) == 0xF8) { uc = (int) (data[offset] & 0x03); offset++; uc <<= 6; uc |= (int) (data[offset] & 0x3F); offset++; uc <<= 6; uc |= (int) (data[offset] & 0x3F); offset++; uc <<= 6; uc |= (int) (data[offset] & 0x3F); offset++; uc <<= 6; uc |= (int) (data[offset] & 0x3F); offset++; } else if ((data[offset] & 0xFE) == 0xFC) { uc = (int) (data[offset] & 0x01); offset++; uc <<= 6; uc |= (int) (data[offset] & 0x3F); offset++; uc <<= 6; uc |= (int) (data[offset] & 0x3F); offset++; uc <<= 6; uc |= (int) (data[offset] & 0x3F); offset++; uc <<= 6; uc |= (int) (data[offset] & 0x3F); offset++; uc <<= 6; uc |= (int) (data[offset] & 0x3F); offset++; } len = fastu_fromUtf8_toChars(uc, chars, len); } } return new String(chars, 0, len); } public static int fastu_fromUtf8_toChars(int codePoint, char[] dst, int index) { if (codePoint < 0 || codePoint > Character.MAX_CODE_POINT) throw new IllegalArgumentException(); if (codePoint < Character.MIN_SUPPLEMENTARY_CODE_POINT) { dst[index] = (char) codePoint; return ++index; } int offset = codePoint - Character.MIN_SUPPLEMENTARY_CODE_POINT; dst[index + 1] = (char) ((offset & 0x3ff) + Character.MIN_LOW_SURROGATE); dst[index] = (char) ((offset >>> 10) + Character.MIN_HIGH_SURROGATE); return index + 2; } public static Color getBackground(final Component c) { return c == null ? null : swing(new F0() { public Color get() { try { return c.getBackground(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "c.getBackground()"; } }); } public static List lookupAllOpt(Map map, Collection l) { List out = new ArrayList(); if (l != null) for (A a : l) addIfNotNull(out, map.get(a)); return out; } public static List lookupAllOpt(Collection l, Map map) { return lookupAllOpt(map, l); } public static boolean isInstance(Class type, Object arg) { return type.isInstance(arg); } public static String dynamicClassName(Object o) { if (o instanceof DynamicObject && ((DynamicObject) o).className != null) return "main$" + ((DynamicObject) o).className; return className(o); } public static boolean instanceOf(Object o, String className) { if (o == null) return false; String c = o.getClass().getName(); return eq(c, className) || eq(c, "main$" + className); } public static boolean instanceOf(Object o, Class c) { if (c == null) return false; return c.isInstance(o); } public static void mapPut2(Map map, A key, B value) { if (map != null && key != null) if (value != null) map.put(key, value); else map.remove(key); } public static Object derefRef(Object o) { if (o instanceof Concept.Ref) o = ((Concept.Ref) o).get(); return o; } public static String assertPossibleGlobalID(String s) { if (!possibleGlobalID(s)) throw fail("Not an acceptable global ID: " + s); return s; } public static String hfulltag(String tag) { return hfulltag(tag, ""); } public static String hfulltag(String tag, Object contents, Object... params) { return hopeningTag(tag, params) + str(contents) + ""; } public static void cancelTimers(Collection timers) { for (Object timer : timers) cancelTimer(timer); } public static List getAndClearList(Collection l) { if (l == null) return emptyList(); synchronized (collectionMutex(l)) { List out = cloneList(l); l.clear(); return out; } } public static String formatDate() { return formatDate(now()); } public static String formatDate(long timestamp) { return timestamp == 0 ? "-" : str(new Date(timestamp)); } public static String getBotAddress(String bot) { List l = fullBotScan(bot); return empty(l) ? null : first(l).address; } public static Object unstructure_matchOK2OrFail(String s) { if (swic(s, "ok ")) return unstructure_startingAtIndex(s, 3); else throw fail(s); } public static String clUnparse(Lisp l) { if (l == null) return ""; return join(" ", (List) concatLists(ll(conceptQuote(l.head)), map("clUnparse_sub", l))); } public static String clUnparse_sub(Lisp l) { return l.empty() ? clUnparse(l) : "(" + clUnparse(l) + ")"; } public static List clUnparse(List l) { return map("clUnparse", l); } public static Map synchroIdentityHashMap() { return synchroMap(new IdentityHashMap()); } public static DoublePt blendDoublePts(DoublePt x, DoublePt y, double yish) { double xish = 1 - yish; return new DoublePt(x.x * xish + y.x * yish, x.y * xish + y.y * yish); } public static ThreadLocal imageGraphics_antiAlias = new ThreadLocal(); public static Graphics2D imageGraphics(BufferedImage img) { return !isFalse(imageGraphics_antiAlias.get()) ? antiAliasGraphics(img) : createGraphics(img); } public static float drawArrowHead_length = 2f; public static void drawArrowHead(Graphics2D g, double x1, double y1, double x2, double y2, double size) { if (y2 == y1 && x2 == x1) return; Path2D.Double arrowHead = new Path2D.Double(); arrowHead.moveTo(0, 0); double l = drawArrowHead_length * size; arrowHead.lineTo(-size, -l); arrowHead.lineTo(size, -l); arrowHead.closePath(); AffineTransform tx = new AffineTransform(); double angle = Math.atan2(y2 - y1, x2 - x1); tx.translate(x2, y2); tx.rotate(angle - Math.PI / 2d); AffineTransform old = g.getTransform(); g.transform(tx); g.fill(arrowHead); g.setTransform(old); } public static Font sansSerif(int fontSize) { return new Font(Font.SANS_SERIF, Font.PLAIN, fontSize); } public static int img_minOfWidthAndHeight(BufferedImage image) { return image == null ? 0 : min(image.getWidth(), image.getHeight()); } public static int vectorLength(int x, int y) { return isqrt(sqr((long) x) + sqr((long) y)); } public static ThreadLocal drawOutlineTextAlongLine_flip = new ThreadLocal(); public static void drawOutlineTextAlongLine(Graphics2D g, String text, int x1, int y1, int x2, int y2, int shift, Color fillColor, Color outlineColor) { drawOutlineTextAlongLine(g, text, x1, y1, x2, y2, shift, 0, fillColor, outlineColor); } public static void drawOutlineTextAlongLine(Graphics2D g, String text, int x1, int y1, int x2, int y2, int shift, float yshift, Color fillColor, Color outlineColor) { Boolean flip = optPar(drawOutlineTextAlongLine_flip); if (y2 == y1 && x2 == x1) ++x2; AffineTransform tx = new AffineTransform(); double angle = Math.atan2(y2 - y1, x2 - x1); if (flip == null) flip = abs(angle) > pi() / 2; if (flip) angle -= pi(); tx.translate((x1 + x2) / 2.0, (y1 + y2) / 2.0); tx.rotate(angle); FontMetrics fm = g.getFontMetrics(); int y = -shift - fm.getMaxDescent(); AffineTransform old = g.getTransform(); g.transform(tx); drawTextWithOutline(g, text, -fm.stringWidth(text) / 2.0f, y + yshift, fillColor, outlineColor); g.setTransform(old); } public static BufferedImage cutImageToCircle(String imageID) { return cutImageToCircle(loadImage2(imageID)); } public static BufferedImage cutImageToCircle(BufferedImage image) { int w = min(image.getWidth(), image.getHeight()); return cutImageToCircle(image, w); } public static BufferedImage cutImageToCircle(BufferedImage image, int w) { int h = w; image = img_getCenterPortion(image, w, w); BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = output.createGraphics(); g2.setComposite(AlphaComposite.Src); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(Color.WHITE); g2.fill(new Ellipse2D.Float(0, 0, w, h)); g2.setComposite(AlphaComposite.SrcAtop); g2.drawImage(image, 0, 0, null); g2.dispose(); return output; } public static BufferedImage img_addBorder(BufferedImage img, Color color, int border) { int top = border, bottom = border, left = border, right = border; int w = img.getWidth(), h = img.getHeight(); BufferedImage img2 = createBufferedImage(left + w + right, top + h + bottom, color); Graphics2D g = img2.createGraphics(); g.drawImage(img, left, top, null); g.dispose(); return img2; } public static BufferedImage drawImageOnImage(BufferedImage img, BufferedImage canvas, int x, int y) { createGraphics(canvas).drawImage(img, x, y, null); return canvas; } public static Font sansSerifBold(int fontSize) { return new Font(Font.SANS_SERIF, Font.BOLD, fontSize); } public static void drawTextWithOutline(Graphics2D g2, String text, float x, float y, Color fillColor, Color outlineColor) { BasicStroke outlineStroke = new BasicStroke(2.0f); g2.translate(x, y); Stroke originalStroke = g2.getStroke(); RenderingHints originalHints = g2.getRenderingHints(); GlyphVector glyphVector = g2.getFont().createGlyphVector(g2.getFontRenderContext(), text); Shape textShape = glyphVector.getOutline(); g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2.setColor(outlineColor); g2.setStroke(outlineStroke); g2.draw(textShape); g2.setColor(fillColor); g2.fill(textShape); g2.setStroke(originalStroke); g2.setRenderingHints(originalHints); g2.translate(-x, -y); } public static BufferedImage resizeImage(BufferedImage img, int newW, int newH) { return resizeImage(img, newW, newH, Image.SCALE_SMOOTH); } public static BufferedImage resizeImage(BufferedImage img, int newW, int newH, int scaleType) { Image tmp = img.getScaledInstance(newW, newH, scaleType); BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = dimg.createGraphics(); g2d.drawImage(tmp, 0, 0, null); g2d.dispose(); return dimg; } public static BufferedImage resizeImage(BufferedImage img, int newW) { int newH = iround(img.getHeight() * (double) newW / img.getWidth()); return resizeImage(img, newW, newH); } public static A setFrameSize(A c, int w, int h) { JFrame f = getFrame(c); if (f != null) f.setSize(w, h); return c; } public static A setFrameSize(int w, int h, A c) { return setFrameSize(c, w, h); } public static List buttonsInGroup(ButtonGroup g) { if (g == null) return ll(); return asList(g.getElements()); } public static String defaultFrameTitle() { return autoFrameTitle(); } public static void defaultFrameTitle(String title) { autoFrameTitle_value = title; } public static String makeFrame_defaultIcon; public static boolean makeFrame_hideConsole; public static ThreadLocal> makeFrame_post = new ThreadLocal(); public static JFrame makeFrame() { return makeFrame((Component) null); } public static JFrame makeFrame(Object content) { return makeFrame(programTitle(), content); } public static JFrame makeFrame(String title) { return makeFrame(title, null); } public static JFrame makeFrame(String title, Object content) { return makeFrame(title, content, true); } public static JFrame makeFrame(final String title, final Object content, final boolean showIt) { final VF1 post = optParam(makeFrame_post); return swing(new F0() { public JFrame get() { try { if (getFrame(content) != null) return getFrame(setFrameTitle((Component) content, title)); final JFrame frame = new JFrame(title); if (makeFrame_defaultIcon != null) setFrameIconLater(frame, makeFrame_defaultIcon); _initFrame(frame); JComponent wrapped = wrap(content); if (wrapped != null) frame.getContentPane().add(wrapped); frame.setBounds(defaultNewFrameBounds()); callF(post, frame); if (showIt) frame.setVisible(true); if (showIt && makeFrame_hideConsole) { hideConsole(); makeFrame_hideConsole = false; } return frame; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (getFrame(content) != null)\r\n ret getFrame(setFrameTitle((Component) ..."; } }); } public static void onResize(Component c, final Object r) { c.addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent e) { pcallF(r); } }); } public static A bindToComponent(A component, final Runnable onShow, final Runnable onUnShow) { component.addAncestorListener(new AncestorListener() { public void ancestorAdded(AncestorEvent event) { pcallF(onShow); } public void ancestorRemoved(AncestorEvent event) { pcallF(onUnShow); } public void ancestorMoved(AncestorEvent event) { } }); return component; } public static A bindToComponent(A component, Runnable onShow) { return bindToComponent(component, onShow, null); } public static A firstInstance(Collection c, Class type) { return firstOfType(c, type); } public static void swingNowOrLater(Runnable r) { if (isAWTThread()) r.run(); else swingLater(r); } public static BufferedImage asBufferedImage(Object o) { BufferedImage bi = toBufferedImageOpt(o); if (bi == null) throw fail(getClassName(o)); return bi; } public static void fillJMenu(JMenu m, Object... x) { if (x == null) return; for (int i = 0; i < l(x); i++) { Object o = x[i], y = get(x, i + 1); if (o instanceof List) fillJMenu(m, asArray((List) o)); else if (eqOneOf(o, "***", "---", "===", "")) m.addSeparator(); else if (o instanceof LiveValue && ((LiveValue) o).getType() == String.class && isRunnableX(y)) { final LiveValue lv = (LiveValue) (o); final JMenuItem mi = jmenuItem("", y); bindLiveValueListenerToComponent(mi, lv, new Runnable() { public void run() { try { String s = lv.get(); if (isCurlyBracketed(s)) { setEnabled(mi, false); s = unCurlyBracket(s); } else setEnabled(mi, true); setText(mi, s); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "String s = lv.get();\r\n if (isCurlyBracketed(s)) {\r\n setEnable..."; } }); m.add(mi); } else if (o instanceof String && isRunnableX(y)) { m.add(jmenuItem((String) o, y)); ++i; } else if (o instanceof JMenuItem) m.add((JMenuItem) o); else if (o instanceof String || o instanceof Action || o instanceof Component) call(m, "add", o); else print("Unknown menu item: " + o); } } public static boolean jmenuItem_newThreads; public static JMenuItem jmenuItem(final String text, final Object r) { return swing(new F0() { public JMenuItem get() { try { Pair p = jmenu_autoMnemonic(text); JMenuItem mi = new JMenuItem(p.a); if (p.b != 0) mi.setMnemonic(p.b); mi.addActionListener(jmenuItem_newThreads ? actionListenerInNewThread(r) : actionListener(r)); return mi; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Pair p = jmenu_autoMnemonic(text);\r\n JMenuItem mi = new JMenuItem(..."; } }); } public static MenuItem menuItem(String text, final Object r) { MenuItem mi = new MenuItem(text); mi.addActionListener(actionListener(r)); return mi; } public static double roundToOneHundredth(double d) { return new BigDecimal(d).setScale(2, RoundingMode.HALF_UP).doubleValue(); } public static JTextField jTextField() { return jTextField(""); } public static JTextField jTextField(final String text) { return swing(new F0() { public JTextField get() { try { JTextField tf = new JTextField(unnull(text)); jenableUndoRedo(tf); tf.selectAll(); return tf; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JTextField tf = new JTextField(unnull(text));\r\n jenableUndoRedo(tf);\r\n ..."; } }); } public static JTextField jTextField(Object o) { return jTextField(strOrEmpty(o)); } public static JDesktopPane mainDesktopPane_value; public static JDesktopPane mainDesktopPane() { return mainDesktopPane_value; } public static JPanel showInternalFrameFormTitled(final JDesktopPane desktop, final String title, final Object... _parts) { JPanel panel = showForm_makePanel(true, _parts); showForm_makeInternalFrame(desktop, title, panel); return panel; } public static JFrame showForm_makeFrame(String title, JPanel panel) { return handleEscapeKey(minFrameWidth(showPackedFrame(title, withMargin(panel)), 400)); } public static List showForm_arrange1(List> l) { int minW = showForm_leftWidth(l); List out = new ArrayList(); for (List row : l) out.add(westAndCenter(withRightMargin(showForm_gapBetweenColumns, jMinWidth(minW, first(row))), second(row))); return out; } public static List> showForm_makeComponents(final Boolean internalFrame, Object... _parts) { List> l = new ArrayList(); List parts = asList(_parts); JButton submitButton = null; for (int i = 0; i < l(parts); i++) { final Object o = parts.get(i), next = get(parts, i + 1); if (o instanceof String && next instanceof Component) setComponentID((Component) next, (String) o); if (o instanceof Component || o instanceof String || next instanceof Component) { l.add(mapLL("wrapForSmartAdd", o == null ? new JPanel() : o instanceof String ? humanizeFormLabel((String) o) : o, next)); if (next instanceof JButton && submitButton == null) submitButton = (JButton) next; i++; } else if (isRunnable(o)) l.add(mapLL("wrapForSmartAdd", null, submitButton = jbutton(showFormSubmitButtonName(), new Runnable() { public void run() { try { Object result = call(o); if (neq(Boolean.FALSE, result)) if (isTrue(internalFrame)) disposeInternalFrame(heldInstance(JButton.class)); else if (isFalse(internalFrame)) disposeFrame(heldInstance(JButton.class)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Object result = call(o);\r\n if (neq(Boolean.FALSE, result))\r\n ..."; } }))); else print("showForm: Unknown element type: " + getClassName(o)); } if (submitButton != null) { final JButton _submitButton = submitButton; onEnterInAllTextFields(concatLists(l), new Runnable() { public void run() { try { clickButton(_submitButton); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "clickButton(_submitButton)"; } }); } for (List row : l) { JComponent left = first(row); if (left instanceof JLabel) makeBold((JLabel) left).setVerticalAlignment(JLabel.TOP); } return l; } public static int vstackWithSpacing_default = 10; public static JPanel vstackWithSpacing(final List parts, final int spacing) { return swing(new F0() { public JPanel get() { try { JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.weightx = 1; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.insets = new Insets(spacing / 2, 0, spacing / 2, 0); smartAddWithLayout(panel, gbc, toObjectArray(parts)); gbc.weighty = 1; gbc.insets = new Insets(0, 0, 0, 0); panel.add(jrigid(), gbc); return panel; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JPanel panel = new JPanel(new GridBagLayout);\r\n new GridBagConstraints gbc..."; } }); } public static JPanel vstackWithSpacing(Component... parts) { return vstackWithSpacing(asList(parts), vstackWithSpacing_default); } public static String getText(final AbstractButton c) { return c == null ? "" : (String) swingAndWait(new F0() { public Object get() { try { return c.getText(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "c.getText()"; } }); } public static String getText(final JTextComponent c) { return c == null ? "" : (String) swingAndWait(new F0() { public Object get() { try { return c.getText(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "c.getText()"; } }); } public static String getText(final JLabel l) { return l == null ? "" : (String) swingAndWait(new F0() { public Object get() { try { return l.getText(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "l.getText()"; } }); } public static String getText(final JComboBox cb) { if (cb.isEditable()) return unnull((String) cb.getEditor().getItem()); else return str(cb.getSelectedItem()); } public static double sqrt(double x) { return Math.sqrt(x); } public static long sqr(long l) { return l * l; } public static double sqr(double d) { return d * d; } public static Timer installTimer(JComponent component, Object r, long delay) { return installTimer(component, r, delay, delay); } public static Timer installTimer(RootPaneContainer frame, long delay, Object r) { return installTimer(frame.getRootPane(), r, delay, delay); } public static Timer installTimer(JComponent component, long delay, Object r) { return installTimer(component, r, delay, delay); } public static Timer installTimer(JComponent component, long delay, long firstDelay, Object r) { return installTimer(component, r, delay, firstDelay); } public static Timer installTimer(final JComponent component, final Object r, final long delay, final long firstDelay) { return installTimer(component, r, delay, firstDelay, true); } public static Timer installTimer(final JComponent component, final Object r, final long delay, final long firstDelay, final boolean repeats) { return (Timer) swingAndWait(new F0() { public Object get() { try { final Var timer = new Var(); timer.set(new Timer(toInt(delay), new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent _evt) { try { AutoCloseable __4956 = tempActivity(r); try { try { if (!allPaused()) if (isFalse(callF(r))) cancelTimer(timer.get()); } catch (Throwable __e) { _handleException(__e); } } finally { _close(__4956); } } catch (Throwable __e) { messageBox(__e); } } })); timer.get().setInitialDelay(toInt(firstDelay)); timer.get().setRepeats(repeats); bindTimerToComponent(timer.get(), component); return timer.get(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "final new Var timer;\r\n timer.set(new Timer(toInt(delay), actionList..."; } }); } public static Timer installTimer(RootPaneContainer frame, long delay, long firstDelay, Object r) { return installTimer(frame.getRootPane(), delay, firstDelay, r); } public static A focus(final A a) { if (a != null) swingLater(new Runnable() { public void run() { try { a.requestFocus(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "a.requestFocus();"; } }); return a; } public static A restructure(A a) { return (A) unstructure(structure(a)); } public static File makeTempDir() { return (File) call(getJavaX(), "TempDirMaker_make"); } public static String pad(String s, int l) { return pad(s, l, ' '); } public static String pad(String s, int l, char c) { if (l(s) >= l) return s; return rep(c, l - l(s)) + s; } public static boolean isOfflineMode() { return eq("1", trim(loadProgramTextFile("#1005806", "offline-mode"))); } public static String getServerTranspiled(String snippetID) { return getServerTranspiled(snippetID, null); } public static String getServerTranspiled(String snippetID, String expectedMD5) { try { long id = parseSnippetID(snippetID); String text = loadPage_utf8(tb_mainServer() + "/tb-int/get-transpiled.php?raw=1&withlibs=1&id=" + id + "&utf8=1" + (l(expectedMD5) > 1 ? "&md5=" + urlencode(expectedMD5) : "") + standardCredentials()); if (nempty(text) && neq(text, "SAME")) saveTranspiledCode(snippetID, text); return text; } catch (Exception __e) { throw rethrow(__e); } } public static HashSet allClasses_keywords = lithashset("class", "interface", "enum", "sclass"); public static List> allClasses(List tok) { List> l = new ArrayList(); for (int i = 1; i < tok.size(); i += 2) { if (eq(tok.get(i), "{")) i = findEndOfBlock(tok, i) - 1; else if (allClasses_keywords.contains(tok.get(i)) && (i == 1 || !tok.get(i - 2).equals("."))) { int j = i; while (j < tok.size() && !tok.get(j).equals("{")) j += 2; j = findEndOfBlock(tok, j) + 1; i = leftScanModifiers(tok, i); l.add(tok.subList(i - 1, Math.min(tok.size(), j))); i = j - 2; } } return l; } public static List> allClasses(String text) { return allClasses(javaTok(text)); } public static String getClassDeclarationName(List c) { if (c != null) for (int i = 1; i + 2 < c.size(); i += 2) if (allClasses_keywords.contains(c.get(i))) return c.get(i + 2); return null; } public static List findBlock(String pat, List tok) { List tokpat = javaTok(pat); int i = findCodeTokens(tok, toStringArray(codeTokensOnly(tokpat))); if (i < 0) return null; int bracketIdx = i + tokpat.size() - 3; assertEquals("{", tok.get(bracketIdx)); int endIdx = findEndOfBlock(tok, bracketIdx); return subList(tok, i - 1, endIdx + 1); } public static String dummyMainClassName(String progID) { return "m" + psI(progID); } public static File tempDirPossiblyInRAMDisk() { File f = linux_fileInRamDisk(aGlobalID()); if (f != null) { f.mkdirs(); return f; } return makeTempDir(); } public static boolean dir2zip_recurse_verbose; public static int dir2zip_recurse(File inDir, File zip) { return dir2zip_recurse(inDir, zip, ""); } public static int dir2zip_recurse(File inDir, File zip, String outPrefix) { try { mkdirsForFile(zip); FileOutputStream fout = newFileOutputStream(zip); ZipOutputStream outZip = new ZipOutputStream(fout); try { return dir2zip_recurse(inDir, outZip, outPrefix, 0); } finally { outZip.close(); } } catch (Exception __e) { throw rethrow(__e); } } public static int dir2zip_recurse(File inDir, ZipOutputStream outZip, String outPrefix, int level) { try { if (++level >= 20) throw fail("woot? 20 levels in zip?"); List files = new ArrayList(); for (File f : listFiles(inDir)) files.add(f); int n = 0; sortFilesByName(files); for (File f : files) { if (f.isDirectory()) { print("dir2zip_recurse: Scanning " + f.getAbsolutePath()); n += dir2zip_recurse(f, outZip, outPrefix + f.getName() + "/", level); } else { if (dir2zip_recurse_verbose) print("Copying " + f.getName()); outZip.putNextEntry(new ZipEntry(outPrefix + f.getName())); InputStream fin = new FileInputStream(f); copyStream(fin, outZip); fin.close(); ++n; } } return n; } catch (Exception __e) { throw rethrow(__e); } } public static boolean isInRAMDisk(File f) { return startsWith(f2s(f), "/dev/shm"); } public static int deleteDirectory_minPathLength = 10; public static void deleteDirectory(File dir) { deleteDirectory(dir, false, false); } public static void deleteDirectory(File dir, boolean verbose, boolean testRun) { dir = getCanonicalFile(dir); assertTrue(f2s(dir), l(f2s(dir)) >= deleteDirectory_minPathLength); File[] files = dir.listFiles(); if (files == null) return; for (File f : files) { if (f.isDirectory()) deleteDirectory(f, verbose, testRun); else { if (verbose) print((testRun ? "Would delete " : "Deleting ") + f.getAbsolutePath()); if (!testRun) f.delete(); } } if (verbose) print((testRun ? "Would delete " : "Deleting ") + dir.getAbsolutePath()); if (!testRun) dir.delete(); } public static void closeRandomAccessFile(RandomAccessFile f) { if (f != null) try { f.close(); callJavaX("dropIO", f); } catch (Throwable e) { printStackTrace(e); } } public static RandomAccessFile newRandomAccessFile(File path, String mode) throws IOException { RandomAccessFile f = new RandomAccessFile(path, mode); callJavaX("registerIO", f, path, mode.indexOf('w') >= 0); return f; } public static String intToHex(int i) { return bytesToHex(intToBytes(i)); } public static Object[] addToObjectArray(Object[] a, Object o) { Object[] x = new Object[l(a) + 1]; arraycopy(a, 0, x, 0, l(a)); x[l(a)] = o; return x; } public static boolean pairContains(Pair p, Object o) { return p != null && (p.a == o || p.b == o); } public static boolean tripleContains(T3 t, Object o) { return t != null && (t.a == o || t.b == o || t.c == o); } public static List smallestList_asList(Object l) { if (l == null) return ll(); if (l instanceof Pair) return ll(((Pair) l).a, ((Pair) l).b); if (l instanceof T3) return ll(((T3) l).a, ((T3) l).b, ((T3) l).c); if (l instanceof Object[]) return Arrays.asList((Object[]) l); return ll(l); } public static int smallestList_indexOf(Object l, Object o) { if (l == null) return -1; if (l instanceof Pair) return eq(((Pair) l).a, o) ? 0 : eq(((Pair) l).b, o) ? 1 : -1; if (l instanceof T3) return eq(((T3) l).a, o) ? 0 : eq(((T3) l).b, o) ? 1 : eq(((T3) l).c, o) ? 2 : -1; if (l instanceof Object[]) return indexOf((Object[]) l, o); return eq(l, o) ? 0 : -1; } public static List smallestList_toMutableList(Object l) { if (l == null) return ll(); if (l instanceof Pair) return ll(((Pair) l).a, ((Pair) l).b); if (l instanceof T3) return ll(((T3) l).a, ((T3) l).b, ((T3) l).c); if (l instanceof Object[]) return new ArrayList(Arrays.asList((Object[]) l)); return ll(l); } public static Object smallestList_fromList(List e) { int n = l(e); smallestList_checkElements(e); switch(n) { case 0: return null; case 1: return e.get(0); case 2: return pair(e.get(0), e.get(1)); case 3: return triple(e.get(0), e.get(1), e.get(2)); default: return toObjectArray(e); } } public static String strUnnull(Object o) { return o == null ? "" : str(o); } public static String jlabel_textAsHTML_center_ifNeeded(String text) { if (swic(text, "") && ewic(text, "")) return text; if (!containsNewLines(text)) return text; return jlabel_textAsHTML_center(text); } public static int tripleHashCodeByContentsIC(T3 t) { return t == null ? 0 : lowerHashCode(t.a) + 2 * lowerHashCode(t.b) - 4 * lowerHashCode(t.c); } public static boolean tripleEqualsByContentsIC(T3 a, T3 b) { return a == null ? b == null : eqic(a.a, b.a) && eqic(a.b, b.b) && eqic(a.c, b.c); } public static List combineLists(List a, List b) { if (empty(a)) return b; if (empty(b)) return a; if (!(a instanceof CombinedList)) a = combinedList_ll(a); ((CombinedList) a).addList(b); return a; } public static WebNode ai_tripleToWebNode(TripleWeb w, String searchTerm, int occurrence) { if (w == null) return null; Web web = webFromTripleWeb(w); try { for (int i = 0; i < l(web.nodes); i++) if (eqic(web_sym(web.nodes.get(i)), searchTerm)) { if (occurrence-- == 0) return web.nodes.get(i); } return null; } catch (Throwable e) { print("ai_tripleToWebNode: " + e + " " + sfu(w) + " => " + webToStringShort(web)); throw rethrow(e); } } public static boolean ai_tripleToTripleRef_debug; public static TripleRef ai_tripleToTripleRef(TripleWeb w, String searchTerm, int occurrence) { if (ai_tripleToTripleRef_debug) { print("ai_tripleToTripleRef: " + w + " " + searchTerm + " " + occurrence); print("eq: " + eqic(w.a, searchTerm) + " " + eqic(w.b, searchTerm) + " " + eqic(w.c, searchTerm)); } if (w == null) return null; if (occurrence < 3) for (int i = 0; i < 3; i++) if (eqic(tripleGet(w, i), searchTerm)) { if (occurrence-- == 0) { if (ai_tripleToTripleRef_debug) print("ai_tripleToTripleRef returning: " + i); return tripleRef(w, i); } } return null; } public static List ai_tripleListeners() { return cloneList(ai_addTripleListener_l); } public static List sortInPlaceByCalculatedFieldDesc_f1(List l, final F1 f) { sort(l, new Comparator() { public int compare(A a, A b) { return stdcompare(f.get(b), f.get(a)); } }); return l; } public static boolean ai_isPriorityTriple(TripleWeb w) { return swic(w.b, "[priority] "); } public static void registerEscape(JFrame frame, final Runnable r) { registerEscape_rootPane(frame.getRootPane(), r); } public static Rect intersectRects(Rect a, Rect b) { int x = max(a.x, b.x), y = max(a.y, b.y); int x2 = min(a.x + a.w, b.x + b.w), y2 = min(a.y + a.h, b.y + b.h); return new Rect(x, y, x2 - x, y2 - y); } public static boolean rectEmpty(Rect r) { return r == null || r.w <= 0 || r.h <= 0; } public static BufferedImage imageFromDataURL(String url) { String pref = "base64,"; int i = indexOf(url, pref); if (i < 0) return null; return decodeImage(base64decode(substring(url, i + l(pref)))); } public static void popupError(final Throwable throwable) { throwable.printStackTrace(); SwingUtilities.invokeLater(new Runnable() { public void run() { String text = throwable.toString(); JOptionPane.showMessageDialog(null, text); } }); } public static JScrollPane jscroll_centered(Component c) { return new JScrollPane(jFullCenter(c)); } public static A disposeFrameOnClick(final A c) { onClick(c, new Runnable() { public void run() { try { disposeFrame(c); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "disposeFrame(c)"; } }); return c; } public static A[] arrayOfType(Class type, int n) { return makeArray(type, n); } public static A[] arrayOfType(int n, Class type) { return arrayOfType(type, n); } public static JComponent wrapForSmartAdd(Object o) { if (o == null) return jpanel(); if (o instanceof String) return jlabel((String) o); return wrap(o); } public static Set ai_addTripleListener_l = synchroHashSet(); public static void ai_addTripleListener(TripleListener l) { ai_addTripleListener_l.add(l); } public static class ScannedBot { public String helloString; public String address; public ScannedBot(String helloString, String address) { this.address = address; this.helloString = helloString; } public ScannedBot() { } } public static List fullBotScan() { return fullBotScan(""); } public static List fullBotScan(String searchPattern) { List bots = new ArrayList(); for (ProgramScan.Program p : quickBotScan()) { String botName = firstPartOfHelloString(p.helloString); boolean isVM = startsWithIgnoreCase(p.helloString, "This is a JavaX VM."); boolean shouldRecurse = swic(botName, "Multi-Port") || isVM; if (swic(botName, searchPattern)) bots.add(new ScannedBot(botName, "" + p.port)); if (shouldRecurse) try { Map subBots = (Map) unstructure(sendToLocalBotQuietly(p.port, "list bots")); for (Number vport : subBots.keySet()) { botName = subBots.get(vport); if (swic(botName, searchPattern)) bots.add(new ScannedBot(botName, p.port + "/" + vport)); } } catch (Exception e) { e.printStackTrace(); } } return bots; } public static Object unstructure_startingAtIndex(String s, int i) { return unstructure_tok(javaTokC_noMLS_iterator(s, i), false, null); } public static String conceptQuote(String s) { if (isIdentifier(s) || isInteger(s) || isProperlyQuoted(s)) return s; return quote(s); } public static Graphics2D antiAliasGraphics(BufferedImage img) { return antiAliasOn(createGraphics(img)); } public static int isqrt(double x) { return iround(Math.sqrt(x)); } public static double pi() { return Math.PI; } public static BufferedImage img_getCenterPortion(BufferedImage img, int w, int h) { int iw = img.getWidth(), ih = img.getHeight(); if (iw < w || ih < h) throw fail("Too small"); int x = (iw - w) / 2, y = (ih - h) / 2; return clipBufferedImage(img, x, y, w, h); } public static BufferedImage createBufferedImage(int w, int h, Color color) { return newBufferedImage(w, h, color); } public static String autoFrameTitle_value; public static String autoFrameTitle() { return autoFrameTitle_value != null ? autoFrameTitle_value : getProgramTitle(); } public static void autoFrameTitle(Component c) { setFrameTitle(getFrame(c), autoFrameTitle()); } public static String programTitle() { return getProgramName(); } public static A setFrameTitle(A c, final String title) { final Frame f = getAWTFrame(c); if (f != null) { swing(new Runnable() { public void run() { try { f.setTitle(title); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "f.setTitle(title);"; } }); } return c; } public static A setFrameTitle(String title, A c) { return setFrameTitle(c, title); } public static JFrame setFrameTitle(String title) { Object f = getOpt(mc(), "frame"); if (f instanceof JFrame) return setFrameTitle((JFrame) f, title); return null; } public static JFrame setFrameIconLater(Component c, final String imageID) { final JFrame frame = getFrame(c); if (frame != null) { Thread _t_0 = new Thread("Loading Icon") { public void run() { try { final Image i = imageIcon(or2(imageID, "#1005557")).getImage(); swingLater(new Runnable() { public void run() { try { frame.setIconImage(i); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "frame.setIconImage(i);"; } }); } catch (Throwable __e) { _handleException(__e); } } }; startThread(_t_0); } return frame; } public static void _initFrame(JFrame f) { myFrames_list.put(f, Boolean.TRUE); standardTitlePopupMenu(f); } public static Rectangle defaultNewFrameBounds_r = new Rectangle(300, 100, 500, 400); public static Rectangle defaultNewFrameBounds() { return swing(new F0() { public Rectangle get() { try { defaultNewFrameBounds_r.translate(60, 20); if (!screenRectangle().contains(defaultNewFrameBounds_r)) defaultNewFrameBounds_r.setLocation(30 + random(30), 20 + random(20)); return new Rectangle(defaultNewFrameBounds_r); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "defaultNewFrameBounds_r.translate(60, 20);\r\n if (!screenRectangle().contai..."; } }); } public static void hideConsole() { final JFrame frame = consoleFrame(); if (frame != null) { autoVMExit(); swingLater(new Runnable() { public void run() { try { frame.setVisible(false); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "frame.setVisible(false);"; } }); } } public static BufferedImage toBufferedImageOpt(Object o) { if (o instanceof BufferedImage) return (BufferedImage) o; if (o instanceof MakesBufferedImage) return ((MakesBufferedImage) o).getBufferedImage(); String c = getClassName(o); if (eqOneOf(c, "main$BWImage", "main$RGBImage")) return (BufferedImage) call(o, "getBufferedImage"); if (eq(c, "main$PNGFile")) return (BufferedImage) call(o, "getImage"); return null; } public static Object[] asArray(List l) { return toObjectArray(l); } public static A[] asArray(Class type, List l) { return (A[]) l.toArray((Object[]) Array.newInstance(type, l.size())); } public static boolean isRunnableX(Object o) { if (o == null) return false; if (o instanceof String) return hasMethod(mc(), (String) o); return o instanceof Runnable || hasMethod(o, "get"); } public static A bindLiveValueListenerToComponent(A component, final LiveValue lv, final Runnable listener) { if (lv != null) bindToComponent(component, new Runnable() { public void run() { try { lv.onChangeAndNow(listener); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "lv.onChangeAndNow(listener)"; } }, new Runnable() { public void run() { try { lv.removeOnChangeListener(listener); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "lv.removeOnChangeListener(listener)"; } }); return component; } public static boolean isCurlyBracketed(String s) { return isCurlyBraced(s); } public static void setEnabled(final JComponent c, final boolean enable) { if (c != null) { swing(new Runnable() { public void run() { try { c.setEnabled(enable); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "c.setEnabled(enable);"; } }); } } public static String unCurlyBracket(String s) { return tok_unCurlyBracket(s); } public static Pair jmenu_autoMnemonic(String s) { int i = indexOf(s, '&'); if (i >= 0 && i < l(s) && isLetterOrDigit(s.charAt(i + 1))) return pair(substring(s, 0, i) + substring(s, i + 1), (int) s.charAt(i + 1)); return pair(s, 0); } public static ActionListener actionListenerInNewThread(final Object runnable) { return actionListenerInNewThread(runnable, null); } public static ActionListener actionListenerInNewThread(final Object runnable, final Object instanceToHold) { if (runnable instanceof ActionListener) return (ActionListener) runnable; return new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent _evt) { try { { Thread _t_0 = new Thread("Action Listener") { public void run() { try { AutoCloseable __5089 = holdInstance(instanceToHold); try { callF(runnable); } finally { _close(__5089); } } catch (Throwable __e) { _handleException(__e); } } }; startThread(_t_0); } } catch (Throwable __e) { messageBox(__e); } } }; } public static A jenableUndoRedo(A textcomp) { final UndoManager undo = new UndoManager(); textcomp.getDocument().addUndoableEditListener(new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); } }); textcomp.getActionMap().put("Undo", abstractAction("Undo", new Runnable() { public void run() { try { if (undo.canUndo()) undo.undo(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (undo.canUndo()) undo.undo()"; } })); textcomp.getActionMap().put("Redo", abstractAction("Redo", new Runnable() { public void run() { try { if (undo.canRedo()) undo.redo(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (undo.canRedo()) undo.redo()"; } })); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo"); return textcomp; } public static String strOrEmpty(Object o) { return o == null ? "" : str(o); } public static JInternalFrame showForm_makeInternalFrame(JDesktopPane desktop, String title, JPanel panel) { JInternalFrame f = addInternalFrame(desktop, title, withMargin(panel)); minInternalFrameWidth(f, 400); packInternalFrameVertically(f); centerInternalFrame(f); return f; } public static JFrame handleEscapeKey(final JFrame frame) { KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); frame.getRootPane().registerKeyboardAction(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { frame.dispose(); } }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW); return frame; } public static JFrame minFrameWidth(JFrame frame, int w) { if (frame != null && frame.getWidth() < w) frame.setSize(w, frame.getHeight()); return frame; } public static JFrame minFrameWidth(int w, JFrame frame) { return minFrameWidth(frame, w); } public static JFrame showPackedFrame(String title, Component contents) { return packFrame(showFrame(title, contents)); } public static JFrame showPackedFrame(Component contents) { return packFrame(showFrame(contents)); } public static int showForm_leftWidth(List> l) { int minW = 0; for (List row : l) minW = max(minW, getMinimumSize(first(row)).width); return minW; } public static JPanel westAndCenter(final Component w, final Component c) { return swing(new F0() { public JPanel get() { try { JPanel panel = new JPanel(new BorderLayout()); panel.add(BorderLayout.WEST, wrap(w)); panel.add(BorderLayout.CENTER, wrap(c)); return panel; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JPanel panel = new JPanel(new BorderLayout);\r\n panel.add(BorderLayout.WEST..."; } }); } public static int withRightMargin_defaultWidth = 6; public static JPanel withRightMargin(Component c) { return withRightMargin(withRightMargin_defaultWidth, c); } public static JPanel withRightMargin(final int w, final Component c) { return swing(new F0() { public JPanel get() { try { JPanel p = new JPanel(new BorderLayout()); p.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, w)); p.add(c); return p; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JPanel p = new JPanel(new BorderLayout);\r\n p.setBorder(BorderFactory.creat..."; } }); } public static A jMinWidth(int w, A c) { Dimension size = c.getMinimumSize(); c.setMinimumSize(new Dimension(w, size.height)); return jPreferWidth(w, c); } public static void setComponentID(Component c, String id) { if (c != null) componentID_map.put(c, id); } public static List mapLL(Object f, Object... data) { return map(f, ll(data)); } public static Map humanizeFormLabel_replacements = litmap("id", "ID", "md5", "MD5"); public static String humanizeFormLabel(String s) { return firstToUpper(joinWithSpace(replaceElementsUsingMap(splitCamelCase(s), humanizeFormLabel_replacements)).replace("I D", "ID")); } public static boolean isRunnable(Object o) { return o instanceof Runnable || hasMethod(o, "get"); } public static JButton jbutton(String text, Object action) { return newButton(text, action); } public static JButton jbutton(String text) { return newButton(text, null); } public static JButton jbutton(Action action) { return swingNu(JButton.class, action); } public static String showFormSubmitButtonName() { return "Submit"; } public static void disposeInternalFrame(Component c) { final JInternalFrame f = getInternalFrame(c); if (f != null) { swing(new Runnable() { public void run() { try { f.dispose(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "f.dispose();"; } }); } } public static void disposeFrame(final Component c) { disposeWindow(c); } public static void onEnterInAllTextFields(JComponent c, Object action) { if (action == null) return; for (Component tf : allChildren(c)) onEnterIfTextField(tf, action); } public static void onEnterInAllTextFields(List c, Object action) { for (Object o : unnull(c)) if (o instanceof JComponent) onEnterInAllTextFields((JComponent) o, action); } public static void clickButton(JButton b) { if (b != null && b.isEnabled()) b.doClick(); } public static A makeBold(final A c) { if (c != null) { swing(new Runnable() { public void run() { try { c.setFont(c.getFont().deriveFont(Font.BOLD)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "c.setFont(c.getFont().deriveFont(Font.BOLD));"; } }); } return c; } public static JPanel smartAddWithLayout(JPanel panel, Object layout, List parts) { for (Object o : parts) panel.add(wrapForSmartAdd(o), layout); return panel; } public static JPanel smartAddWithLayout(JPanel panel, Object layout, Object... parts) { return smartAddWithLayout(panel, layout, asList(parts)); } public static Component jrigid() { return javax.swing.Box.createRigidArea(new Dimension(0, 0)); } public static boolean allPaused() { return ping_pauseAll; } public static void bindTimerToComponent(final Timer timer, JFrame f) { bindTimerToComponent(timer, f.getRootPane()); } public static void bindTimerToComponent(final Timer timer, JComponent c) { if (c.isShowing()) timer.start(); c.addAncestorListener(new AncestorListener() { public void ancestorAdded(AncestorEvent event) { timer.start(); } public void ancestorRemoved(AncestorEvent event) { timer.stop(); } public void ancestorMoved(AncestorEvent event) { } }); } public static String loadPage_utf8(URL url) { return loadPage_utf8(url.toString()); } public static String loadPage_utf8(String url) { loadPage_charset.set("UTF-8"); try { return loadPage(url); } finally { loadPage_charset.set(null); } } public static void saveTranspiledCode(String progID, String code) { File dir = getCodeProgramDir(progID); new File(dir, "Transpilation").delete(); saveGZTextFile(new File(dir, "Transpilation.gz"), code); } public static int findEndOfBlock(List cnc, int i) { int j = i + 2, level = 1; while (j < cnc.size()) { if (cnc.get(j).equals("{")) ++level; else if (cnc.get(j).equals("}")) --level; if (level == 0) return j + 1; ++j; } return cnc.size(); } public static int leftScanModifiers(List tok, int i) { List mod = getJavaModifiers(); while (i > 1 && mod.contains(tok.get(i - 2))) i -= 2; return i; } public static File linux_fileInRamDisk(String name) { if (!isLinux()) return null; File dir = newFile("/dev/shm"); if (dir.isDirectory()) return newFile(dir, name); return null; } public static void sortFilesByName(List l) { sort(l, new Comparator() { public int compare(File a, File b) { return stdcompare(a.getName(), b.getName()); } }); } public static File getCanonicalFile(File f) { try { return f == null ? null : f.getCanonicalFile(); } catch (Exception __e) { throw rethrow(__e); } } public static void smallestList_checkElements(Object... e) { for (Object o : e) if (o instanceof Pair || o instanceof T3 || o instanceof Object[]) throw fail("Can't add this to a smallestList: " + shortToString(o)); } public static boolean containsNewLines(String s) { return containsNewLine(s); } public static String jlabel_textAsHTML_center(String text) { return "
" + replace(htmlencode(text), "\n", "
") + "
"; } public static int lowerHashCode(String s) { return s == null ? 0 : lower(s).hashCode(); } public static Web webFromTripleWeb(TripleWeb w) { return webFromTriple(w); } public static
A tripleGet(T3 t, int i) { if (t == null) return null; if (i == 0) return t.a; if (i == 1) return t.b; if (i == 2) return t.c; return null; } public static void registerEscape_rootPane(JComponent rootPane, final Runnable r) { String name = "Escape"; Action action = abstractAction(name, r); KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); rootPane.getActionMap().put(name, action); rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, name); } public static BufferedImage decodeImage(byte[] data) { try { return ImageIO.read(new ByteArrayInputStream(data)); } catch (Exception __e) { throw rethrow(__e); } } public static byte[] base64decode(String s) { byte[] alphaToInt = base64decode_base64toint; int sLen = s.length(); int numGroups = sLen / 4; if (4 * numGroups != sLen) throw new IllegalArgumentException("String length must be a multiple of four."); int missingBytesInLastGroup = 0; int numFullGroups = numGroups; if (sLen != 0) { if (s.charAt(sLen - 1) == '=') { missingBytesInLastGroup++; numFullGroups--; } if (s.charAt(sLen - 2) == '=') missingBytesInLastGroup++; } byte[] result = new byte[3 * numGroups - missingBytesInLastGroup]; int inCursor = 0, outCursor = 0; for (int i = 0; i < numFullGroups; i++) { int ch0 = base64decode_base64toint(s.charAt(inCursor++), alphaToInt); int ch1 = base64decode_base64toint(s.charAt(inCursor++), alphaToInt); int ch2 = base64decode_base64toint(s.charAt(inCursor++), alphaToInt); int ch3 = base64decode_base64toint(s.charAt(inCursor++), alphaToInt); result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4)); result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2)); result[outCursor++] = (byte) ((ch2 << 6) | ch3); } if (missingBytesInLastGroup != 0) { int ch0 = base64decode_base64toint(s.charAt(inCursor++), alphaToInt); int ch1 = base64decode_base64toint(s.charAt(inCursor++), alphaToInt); result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4)); if (missingBytesInLastGroup == 1) { int ch2 = base64decode_base64toint(s.charAt(inCursor++), alphaToInt); result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2)); } } return result; } public static int base64decode_base64toint(char c, byte[] alphaToInt) { int result = alphaToInt[c]; if (result < 0) throw new IllegalArgumentException("Illegal character " + c); return result; } public static final byte[] base64decode_base64toint = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 }; public static JPanel jFullCenter(final Component c) { return swing(new F0() { public JPanel get() { try { JPanel panel = new JPanel(new GridBagLayout()); panel.add(c); return panel; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JPanel panel = new JPanel(new GridBagLayout);\r\n panel.add(c);\r\n ret panel;"; } }); } public static A[] makeArray(Class type, int n) { return (A[]) Array.newInstance(type, n); } public static Map componentID_map = weakHashMap(); public static String componentID(Component c) { return c == null ? null : componentID_map.get(c); } public static JPanel jpanel(LayoutManager layout) { return swingNu(JPanel.class, layout); } public static JPanel jpanel() { return swingNu(JPanel.class); } public static Graphics2D antiAliasOn(Graphics2D g) { g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); return g; } public static Frame getAWTFrame(final Object _o) { return swing(new F0() { public Frame get() { try { Object o = _o; if (o instanceof ButtonGroup) o = first(buttonsInGroup((ButtonGroup) o)); if (!(o instanceof Component)) return null; Component c = (Component) o; while (c != null) { if (c instanceof Frame) return (Frame) c; c = c.getParent(); } return null; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "O o = _o;\r\n /*\r\n ifdef HaveProcessing\r\n if (o instanceof PApplet) ..."; } }); } public static void standardTitlePopupMenu(final JFrame frame) { if (isSubstanceLAF()) titlePopupMenu(frame, new VF1() { public void get(JPopupMenu menu) { try { boolean alwaysOnTop = frame.isAlwaysOnTop(); menu.add(jmenuItem("Restart Program", "restart")); menu.add(jmenuItem("Duplicate Program", "duplicateThisProgram")); menu.add(jmenuItem("Show Console", "showConsole")); menu.add(jCheckBoxMenuItem("Always On Top", alwaysOnTop, new Runnable() { public void run() { try { toggleAlwaysOnTop(frame); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "toggleAlwaysOnTop(frame)"; } })); menu.add(jMenuItem("Shoot Window", new Runnable() { public void run() { try { shootWindowGUI_external(frame, 500); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "shootWindowGUI_external(frame, 500)"; } })); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "bool alwaysOnTop = frame.isAlwaysOnTop();\r\n menu.add(jmenuItem(\"Restar..."; } }); } public static Rectangle screenRectangle() { return new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); } public static JFrame consoleFrame() { return (JFrame) getOpt(get(getJavaX(), "console"), "frame"); } public static void autoVMExit() { call(getJavaX(), "autoVMExit"); } public static boolean isLetterOrDigit(char c) { return Character.isLetterOrDigit(c); } public static AbstractAction abstractAction(String name, final Object runnable) { return new AbstractAction(name) { public void actionPerformed(ActionEvent evt) { pcallF(runnable); } }; } public static ThreadLocal addInternalFrame_dontSelect = new ThreadLocal(); public static ThreadLocal addInternalFrame_layer = new ThreadLocal(); public static JInternalFrame addInternalFrame(final JDesktopPane desktop, final String title, final int x, final int y, final int w, final int h) { return addInternalFrame(desktop, title, x, y, w, h, null); } public static JInternalFrame addInternalFrame(final JDesktopPane desktop, final String title, final int x, final int y, final int w, final int h, final Component contents) { return addInternalFrame(desktop, title, rect(x, y, w, h), contents); } public static JInternalFrame addInternalFrame(final JDesktopPane desktop, final String title, final Component contents) { return addInternalFrame(desktop, title, null, contents); } public static JInternalFrame addInternalFrame(final JDesktopPane desktop, final String title, final Rect r, final Component contents) { final boolean dontSelect = isTrue(optParam(addInternalFrame_dontSelect)); final Integer layer = optParam(addInternalFrame_layer); return swing(new F0() { public JInternalFrame get() { try { JInternalFrame frame; if (contents instanceof JInternalFrame) frame = (JInternalFrame) contents; else { frame = jInternalFrame(title); setInternalFrameContents(frame, contents); } frame.setVisible(true); desktop.add(frame, layer); if (r != null) setBounds(frame, r); else internalFrameDefaultPosition(frame); if (dontSelect) frame.toFront(); else frame.setSelected(true); return fixInternalFrame(frame); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JInternalFrame frame;\r\n if (contents instanceof JInternalFrame)\r\n fra..."; } }); } public static JInternalFrame addInternalFrame(JDesktopPane desktop, String title) { return addInternalFrame(desktop, title, jpanel()); } public static JInternalFrame minInternalFrameWidth(final JInternalFrame frame, final int w) { { swing(new Runnable() { public void run() { try { if (frame != null && frame.getWidth() < w) frame.setSize(w, frame.getHeight()); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (frame != null && frame.getWidth() < w)\r\n frame.setSize(w, frame.getH..."; } }); } return frame; } public static JInternalFrame minInternalFrameWidth(int w, JInternalFrame frame) { return minInternalFrameWidth(frame, w); } public static A packInternalFrameVertically(A c) { return packInternalFrameVertically(-1, c); } public static A packInternalFrameVertically(int width, A c) { final JInternalFrame win = getInternalFrame(c); if (win == null) return c; final int w = width < 0 ? win.getWidth() : width; { swing(new Runnable() { public void run() { try { win.pack(); win.setSize(w, win.getHeight()); fixInternalFrame(win); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "win.pack();\r\n win.setSize(w, win.getHeight());\r\n fixInternalFrame(win);"; } }); } return c; } public static JInternalFrame centerInternalFrame(final JInternalFrame f) { { swing(new Runnable() { public void run() { try { Container c = f.getParent(); if (c != null) { f.setLocation((c.getWidth() - f.getWidth()) / 2, (c.getHeight() - f.getHeight()) / 2); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Container c = f.getParent();\r\n if (c != null) {\r\n //print(\"Container ..."; } }); } return f; } public static JInternalFrame centerInternalFrame(final int w, final int h, final JInternalFrame f) { { swing(new Runnable() { public void run() { try { f.setSize(w, h); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "f.setSize(w, h);"; } }); } return centerInternalFrame(f); } public static int packFrame_minw = 150, packFrame_minh = 50; public static A packFrame(final A c) { { swing(new Runnable() { public void run() { try { Window w = getWindow(c); if (w != null) { w.pack(); int maxW = getScreenWidth() - 50, maxH = getScreenHeight() - 50; w.setSize(min(maxW, max(w.getWidth(), packFrame_minw)), min(maxH, max(w.getHeight(), packFrame_minh))); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Window w = getWindow(c);\r\n if (w != null) {\r\n w.pack();\r\n int ma..."; } }); } return c; } public static JFrame packFrame(ButtonGroup g) { return packFrame(getFrame(g)); } public static JFrame showFrame() { return makeFrame(); } public static JFrame showFrame(Object content) { return makeFrame(content); } public static JFrame showFrame(String title) { return makeFrame(title); } public static JFrame showFrame(String title, Object content) { return makeFrame(title, content); } public static JFrame showFrame(final JFrame f) { if (f != null) { swing(new Runnable() { public void run() { try { if (frameTooSmall(f)) frameStandardSize(f); if (!f.isVisible()) f.setVisible(true); if (f.getState() == Frame.ICONIFIED) f.setState(Frame.NORMAL); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (frameTooSmall(f)) frameStandardSize(f);\r\n if (!f.isVisible()) f.setVis..."; } }); } return f; } public static JFrame showFrame(String title, Object content, JFrame frame) { if (frame == null) return showFrame(title, content); else { frame.setTitle(title); setFrameContents(frame, content); return frame; } } public static A jPreferWidth(int w, A c) { Dimension size = c.getPreferredSize(); c.setPreferredSize(new Dimension(w, size.height)); return c; } public static List replaceElementsUsingMap(Iterable l, final Map map) { return map(l, new F1() { public Object get(A a) { try { return getOrKeep(map, a); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "getOrKeep(map, a)"; } }); } public static List splitCamelCase(String s) { return ai_splitCamelCase(s); } public static boolean newButton_autoToolTip = true; public static JButton newButton(final String text, final Object action) { return swing(new F0() { public JButton get() { try { final JButton btn = new JButton(text); if (newButton_autoToolTip) { btn.setToolTipText(btn.getText()); } if (action != null) btn.addActionListener(actionListener(action, btn)); return btn; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "final JButton btn = new JButton(text);\r\n if (newButton_autoToolTip) {\r\n ..."; } }); } public static A swingNu(final Class c, final Object... args) { return swingConstruct(c, args); } public static JInternalFrame getInternalFrame(final Object _o) { return swing(new F0() { public JInternalFrame get() { try { Object o = _o; if (o instanceof ButtonGroup) o = first(buttonsInGroup((ButtonGroup) o)); if (!(o instanceof Component)) return null; Component c = (Component) o; while (c != null) { if (c instanceof JInternalFrame) return (JInternalFrame) c; c = c.getParent(); } return null; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "O o = _o;\r\n if (o instanceof ButtonGroup) o = first(buttonsInGroup((Button..."; } }); } public static List allChildren(Component c) { return childrenOfType(c, Component.class); } public static void onEnterIfTextField(Component c, Object action) { if (action == null) return; if (c instanceof JTextField) onEnter((JTextField) c, action); else if (c instanceof JComboBox) onEnter((JComboBox) c, action); } public static void saveGZTextFile(File file, String contents) { try { File parentFile = file.getParentFile(); if (parentFile != null) parentFile.mkdirs(); String tempFileName = file.getPath() + "_temp"; File tempFile = new File(tempFileName); if (contents != null) { if (tempFile.exists()) try { String saveName = tempFileName + ".saved." + now(); copyFile(tempFile, new File(saveName)); } catch (Throwable e) { printStackTrace(e); } FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath()); GZIPOutputStream gos = new GZIPOutputStream(fileOutputStream); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(gos, "UTF-8"); PrintWriter printWriter = new PrintWriter(outputStreamWriter); printWriter.print(contents); printWriter.close(); gos.close(); fileOutputStream.close(); } if (file.exists() && !file.delete()) throw new IOException("Can't delete " + file.getPath()); if (contents != null) if (!tempFile.renameTo(file)) throw new IOException("Can't rename " + tempFile + " to " + file); } catch (Exception __e) { throw rethrow(__e); } } public static List getJavaModifiers_list = litlist("static", "abstract", "public", "private", "protected", "final", "native", "volatile", "synchronized", "transient"); public static List getJavaModifiers() { return getJavaModifiers_list; } public static String shortToString(Object o) { return shorten(str(o), 100); } public static List replace(List l, A a, A b) { for (int i = 0; i < l(l); i++) if (eq(l.get(i), a)) l.set(i, b); return l; } public static String replace(String s, String a, String b) { return s == null ? null : a == null || b == null ? s : s.replace(a, b); } public static void duplicateThisProgram() { nohupJavax(trim(programID() + " " + smartJoin((String[]) get(getJavaX(), "fullArgs")))); } public static boolean isSubstanceLAF() { return substanceLookAndFeelEnabled(); } public static void titlePopupMenu(final Component c, final Object menuMaker) { swingNowOrLater(new Runnable() { public void run() { try { if (!isSubstanceLAF()) print("Can't add title right click!"); else { JComponent titleBar = getTitlePaneComponent(getPossiblyInternalFrame(c)); componentPopupMenu(titleBar, menuMaker); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (!isSubstanceLAF())\r\n print(\"Can't add title right click!\");\r\n els..."; } }); } public static JCheckBoxMenuItem jCheckBoxMenuItem(String text, boolean checked, final Object r) { JCheckBoxMenuItem mi = new JCheckBoxMenuItem(text, checked); mi.addActionListener(actionListener(r)); return mi; } public static void toggleAlwaysOnTop(JFrame frame) { frame.setAlwaysOnTop(!frame.isAlwaysOnTop()); } public static JMenuItem jMenuItem(String text, Object r) { return jmenuItem(text, r); } public static void shootWindowGUI_external(JFrame frame) { call(hotwireOnce("#1007178"), "shootWindowGUI", frame); } public static void shootWindowGUI_external(final JFrame frame, int delay) { call(hotwireOnce("#1007178"), "shootWindowGUI", frame, delay); } public static Rect rect(int x, int y, int w, int h) { return new Rect(x, y, w, h); } public static boolean jInternalFrame_iconifiable = true; public static JInternalFrame jInternalFrame() { return jInternalFrame(""); } public static JInternalFrame jInternalFrame(final String title) { return swing(new F0() { public JInternalFrame get() { try { JInternalFrame f = new JInternalFrame(title, true, true, true, jInternalFrame_iconifiable); f.setVisible(true); return f; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JInternalFrame f = new JInternalFrame(title, true, true, true, jInternalFrame..."; } }); } public static void setInternalFrameContents(final Component c, final Object contents) { { swing(new Runnable() { public void run() { try { JInternalFrame frame = getInternalFrame(c); if (frame == null) return; frame.getContentPane().removeAll(); frame.getContentPane().setLayout(new BorderLayout()); if (contents != null) frame.getContentPane().add(wrap(contents)); revalidate(frame); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JInternalFrame frame = getInternalFrame(c);\r\n if (frame == null) return;\r\n..."; } }); } } public static A setBounds(int x, int y, int w, int h, A a) { if (a != null) a.setBounds(x, y, w, h); return a; } public static A setBounds(A a, Rect r) { if (a != null) a.setBounds(toRectangle(r)); return a; } public static A setBounds(A a, int x, int y, int w, int h) { return setBounds(x, y, w, h, a); } public static void internalFrameDefaultPosition(JInternalFrame f) { f.setSize(500, 300); centerInternalFrame(f); } public static int fixInternalFrame_borderTopLeft = 0; public static int fixInternalFrame_borderBottomRight = 40; public static JInternalFrame fixInternalFrame(final JInternalFrame f) { return swing(new F0() { public JInternalFrame get() { try { Container c = f.getParent(); if (c == null) return f; Rect r = toRect(f.getBounds()); int a = fixInternalFrame_borderTopLeft, b = fixInternalFrame_borderBottomRight; Rect outer = new Rect(a, a, c.getWidth() - b, c.getHeight() - b); if (!rectContains(outer, r)) f.setLocation(max(b, min(r.x, outer.x2())), max(b, min(r.y, outer.y2()))); if (r.w > c.getWidth() || r.h > c.getHeight()) f.setSize(c.getWidth() - a - b, c.getHeight() - a - b); return f; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Container c = f.getParent();\r\n if (c == null) ret f;\r\n Rect r = toRect(..."; } }); } public static int getScreenWidth() { return getScreenSize().width; } public static int getScreenHeight() { return getScreenSize().height; } public static boolean frameTooSmall(JFrame frame) { return frame.getWidth() < 100 || frame.getHeight() < 50; } public static void frameStandardSize(JFrame frame) { frame.setBounds(300, 100, 500, 400); } public static void setFrameContents(final Component c, final Object contents) { { swing(new Runnable() { public void run() { try { JFrame frame = getFrame(c); frame.getContentPane().removeAll(); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(wrap(contents)); revalidate(frame); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JFrame frame = getFrame(c);\r\n frame.getContentPane().removeAll();\r\n fra..."; } }); } } public static A getOrKeep(Map map, A a) { A v = map.get(a); return v != null ? v : a; } public static List ai_splitCamelCase(String s) { int j = 0; List l = new ArrayList(); for (int i = 0; i < l(s); i++) if (isUpperCaseLetter(s.charAt(i)) && i > j) { l.add(substring(s, j, i)); j = i; } if (j < l(s)) l.add(substring(s, j)); return l; } public static List childrenOfType(Component c, Class theClass) { List l = new ArrayList(); scanForComponents(c, theClass, l); return l; } public static List childrenOfType(Class theClass, Component c) { return childrenOfType(c, theClass); } public static String smartJoin(String[] args) { if (args.length == 1) return args[0]; String[] a = new String[args.length]; for (int i = 0; i < a.length; i++) a[i] = !isJavaIdentifier(args[i]) && !isQuoted(args[i]) ? quote(args[i]) : args[i]; return join(" ", a); } public static String smartJoin(List args) { return smartJoin(toStringArray(args)); } public static boolean substanceLookAndFeelEnabled() { return startsWith(getLookAndFeel(), "org.pushingpixels."); } public static JComponent getTitlePaneComponent(RootPaneContainer window) { if (!substanceLookAndFeelEnabled() || window == null) return null; if (window instanceof JInternalFrame) return getInternalFrameTitlePaneComponent((JInternalFrame) window); JRootPane rootPane = window.getRootPane(); if (rootPane != null) { Object ui = rootPane.getUI(); return (JComponent) call(ui, "getTitlePane"); } return null; } public static RootPaneContainer getPossiblyInternalFrame(Component c) { JInternalFrame f = getInternalFrame(c); if (f != null) return f; return optCast(getWindow(c), RootPaneContainer.class); } public static Rectangle toRectangle(Rect r) { return r == null ? null : r.getRectangle(); } public static Rect toRect(Rectangle r) { return r == null ? null : new Rect(r); } public static boolean rectContains(int x1, int y1, int w, int h, Pt p) { return p.x >= x1 && p.y >= y1 && p.x < x1 + w && p.y < y1 + h; } public static boolean rectContains(Rect a, Rect b) { return b.x >= a.x && b.y >= a.y && b.x2() <= a.x2() && b.y2() <= a.y2(); } public static boolean isUpperCaseLetter(char c) { return Character.isUpperCase(c); } public static void scanForComponents(Component c, Class theClass, List l) { if (theClass.isInstance(c)) l.add((A) c); if (c instanceof Container) for (Component comp : ((Container) c).getComponents()) scanForComponents(comp, theClass, l); } public static String getLookAndFeel() { return getClassName(UIManager.getLookAndFeel()); } public static JComponent getInternalFrameTitlePaneComponent(JInternalFrame f) { return (JComponent) childWithClassNameEndingWith(f, "InternalFrameTitlePane"); } public static A optCast(Object o, Class c) { return isInstance(c, o) ? (A) o : null; } public static Component childWithClassNameEndingWith(Component c, String suffix) { if (endsWith(className(c), suffix)) return c; Component x; for (Component comp : getComponents(c)) if ((x = childWithClassNameEndingWith(comp, suffix)) != null) return x; return null; } public static List getComponents(final Component c) { return !(c instanceof Container) ? emptyList() : asList(swing(new F0() { public Component[] get() { try { return ((Container) c).getComponents(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "((Container) c).getComponents()"; } })); } public static class CustomCompactHashSet extends AbstractSet { public static final int INITIAL_SIZE = 3; public static final double LOAD_FACTOR = 0.75; public static final Object nullObject = new Object(); public static final Object deletedObject = new Object(); public int elements; public int freecells; public A[] objects; public int modCount; public Hasher hasher; public CustomCompactHashSet() { this(INITIAL_SIZE); } public CustomCompactHashSet(int size) { objects = (A[]) new Object[(size == 0 ? 1 : size)]; elements = 0; freecells = objects.length; modCount = 0; } public CustomCompactHashSet(Collection c) { this(c.size()); addAll(c); } public CustomCompactHashSet(Hasher hasher) { this(); this.hasher = hasher; } @Override public Iterator iterator() { return new CompactHashIterator(); } @Override public int size() { return elements; } @Override public boolean isEmpty() { return elements == 0; } @Override public boolean contains(Object o) { return find(o) != null; } public synchronized A find(Object o) { if (o == null) o = nullObject; int hash = elementHashCode(o); int index = (hash & 0x7FFFFFFF) % objects.length; int offset = 1; while (objects[index] != null && !(elementHashCode(objects[index]) == hash && elementEquals(objects[index], o))) { index = ((index + offset) & 0x7FFFFFFF) % objects.length; offset = offset * 2 + 1; if (offset == -1) offset = 2; } return objects[index]; } public boolean removeIfSame(Object o) { A value = find(o); if (value == o) { remove(value); return true; } return false; } @Override public synchronized boolean add(Object o) { if (o == null) o = nullObject; int hash = elementHashCode(o); int index = (hash & 0x7FFFFFFF) % objects.length; int offset = 1; int deletedix = -1; while (objects[index] != null && !(elementHashCode(objects[index]) == hash && elementEquals(objects[index], o))) { if (objects[index] == deletedObject) deletedix = index; index = ((index + offset) & 0x7FFFFFFF) % objects.length; offset = offset * 2 + 1; if (offset == -1) offset = 2; } if (objects[index] == null) { if (deletedix != -1) index = deletedix; else freecells--; modCount++; elements++; objects[index] = (A) o; if (1 - (freecells / (double) objects.length) > LOAD_FACTOR) rehash(); return true; } else return false; } @Override public synchronized boolean remove(Object o) { if (o == null) o = nullObject; int hash = elementHashCode(o); int index = (hash & 0x7FFFFFFF) % objects.length; int offset = 1; while (objects[index] != null && !(elementHashCode(objects[index]) == hash && elementEquals(objects[index], o))) { index = ((index + offset) & 0x7FFFFFFF) % objects.length; offset = offset * 2 + 1; if (offset == -1) offset = 2; } if (objects[index] != null) { objects[index] = (A) deletedObject; modCount++; elements--; return true; } else return false; } @Override public synchronized void clear() { elements = 0; for (int ix = 0; ix < objects.length; ix++) objects[ix] = null; freecells = objects.length; modCount++; } @Override public synchronized Object[] toArray() { Object[] result = new Object[elements]; Object[] objects = this.objects; int pos = 0; for (int i = 0; i < objects.length; i++) if (objects[i] != null && objects[i] != deletedObject) { if (objects[i] == nullObject) result[pos++] = null; else result[pos++] = objects[i]; } return result; } @Override public synchronized T[] toArray(T[] a) { int size = elements; if (a.length < size) a = (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size); A[] objects = this.objects; int pos = 0; for (int i = 0; i < objects.length; i++) if (objects[i] != null && objects[i] != deletedObject) { if (objects[i] == nullObject) a[pos++] = null; else a[pos++] = (T) objects[i]; } return a; } public void rehash() { int gargagecells = objects.length - (elements + freecells); if (gargagecells / (double) objects.length > 0.05) rehash(objects.length); else rehash(objects.length * 2 + 1); } public void rehash(int newCapacity) { int oldCapacity = objects.length; @SuppressWarnings("unchecked") A[] newObjects = (A[]) new Object[newCapacity]; for (int ix = 0; ix < oldCapacity; ix++) { Object o = objects[ix]; if (o == null || o == deletedObject) continue; int hash = elementHashCode(o); int index = (hash & 0x7FFFFFFF) % newCapacity; int offset = 1; while (newObjects[index] != null) { index = ((index + offset) & 0x7FFFFFFF) % newCapacity; offset = offset * 2 + 1; if (offset == -1) offset = 2; } newObjects[index] = (A) o; } objects = newObjects; freecells = objects.length - elements; } public class CompactHashIterator implements Iterator { public int index; public int lastReturned = -1; public int expectedModCount; @SuppressWarnings("empty-statement") public CompactHashIterator() { synchronized (CustomCompactHashSet.this) { for (index = 0; index < objects.length && (objects[index] == null || objects[index] == deletedObject); index++) ; expectedModCount = modCount; } } @Override public boolean hasNext() { synchronized (CustomCompactHashSet.this) { return index < objects.length; } } @SuppressWarnings("empty-statement") @Override public T next() { synchronized (CustomCompactHashSet.this) { int length = objects.length; if (index >= length) { lastReturned = -2; throw new NoSuchElementException(); } lastReturned = index; for (index += 1; index < length && (objects[index] == null || objects[index] == deletedObject); index++) ; if (objects[lastReturned] == nullObject) return null; else return (T) objects[lastReturned]; } } @Override public void remove() { synchronized (CustomCompactHashSet.this) { if (modCount != expectedModCount) throw new ConcurrentModificationException(); if (lastReturned == -1 || lastReturned == -2) throw new IllegalStateException(); if (objects[lastReturned] != null && objects[lastReturned] != deletedObject) { objects[lastReturned] = (A) deletedObject; elements--; modCount++; expectedModCount = modCount; } } } } public int elementHashCode(Object o) { if (hasher == null || o == nullObject || o == deletedObject) return _hashCode(o); return hasher.hashCode((A) o); } public boolean elementEquals(Object a, Object b) { if (hasher == null) return eq(a, b); if (a == nullObject || a == deletedObject || b == nullObject || b == deletedObject) return a == b; return hasher.equals((A) a, (A) b); } } public static class Str extends Concept { public String name; public List otherNames = new ArrayList(); public Str() { } public Str(String name) { this.name = name; } public String toString() { return name; } } public static class TransferableImage implements Transferable { public Image i; public TransferableImage(Image i) { this.i = i; } public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (flavor.equals(DataFlavor.imageFlavor) && i != null) { return i; } else { throw new UnsupportedFlavorException(flavor); } } public DataFlavor[] getTransferDataFlavors() { DataFlavor[] flavors = new DataFlavor[1]; flavors[0] = DataFlavor.imageFlavor; return flavors; } public boolean isDataFlavorSupported(DataFlavor flavor) { DataFlavor[] flavors = getTransferDataFlavors(); for (int i = 0; i < flavors.length; i++) { if (flavor.equals(flavors[i])) { return true; } } return false; } } public static interface Hasher { public int hashCode(A a); public boolean equals(A a, A b); } public static class MapInConcept implements Map { public Map m; public Concept cc; public MapInConcept() { } public MapInConcept(Map m, Concept cc) { this.cc = cc; this.m = m; } public int size() { synchronized (this) { return m.size(); } } public boolean isEmpty() { synchronized (this) { return m.isEmpty(); } } public boolean containsKey(Object key) { synchronized (this) { return m.containsKey(key); } } public boolean containsValue(Object value) { synchronized (this) { return m.containsValue(value); } } public V get(Object key) { synchronized (this) { return m.get(key); } } public V put(K key, V value) { try { synchronized (this) { return m.put(key, value); } } finally { cc.change(); } } public V remove(Object key) { try { synchronized (this) { return m.remove(key); } } finally { cc.change(); } } public void putAll(Map map) { synchronized (this) { m.putAll(map); } cc.change(); } public void clear() { synchronized (this) { m.clear(); } cc.change(); } public transient Set keySet; public transient Set> entrySet; public transient Collection values; public Set keySet() { synchronized (this) { if (keySet == null) keySet = new SynchronizedSet(m.keySet(), this); return keySet; } } public Set> entrySet() { synchronized (this) { if (entrySet == null) entrySet = new SynchronizedSet(m.entrySet(), this); return entrySet; } } public Collection values() { synchronized (this) { if (values == null) values = new SynchronizedCollection(m.values(), this); return values; } } public boolean equals(Object o) { if (this == o) return true; synchronized (this) { return m.equals(o); } } public int hashCode() { synchronized (this) { return m.hashCode(); } } public String toString() { synchronized (this) { return m.toString(); } } public Map unwrap() { return m; } } public static class AutoComboBox extends JComboBox { public String[] keyWord = { "item1", "item2", "item3" }; public Vector myVector = new Vector(); public AutoComboBox() { setModel(new DefaultComboBoxModel(myVector)); setSelectedIndex(-1); setEditable(true); JTextField text = (JTextField) this.getEditor().getEditorComponent(); text.setFocusable(true); text.setText(""); text.addKeyListener(new ComboListener(this, myVector)); text.setFocusTraversalKeysEnabled(false); setMyVector(); } public void setKeyWord(String[] keyWord) { this.keyWord = keyWord; setMyVector(); } public void setKeyWord(Collection keyWord) { setKeyWord(toStringArray(keyWord)); } public final void setMyVector() { copyArrayToVector(keyWord, myVector); } } public static class ComboListener extends KeyAdapter { public JComboBox cb; public Vector vector; public ComboListener(JComboBox cb, Vector vector) { this.vector = vector; this.cb = cb; } public void keyPressed(KeyEvent key) { if (key.getKeyCode() == KeyEvent.VK_ENTER) return; if (key.getKeyCode() == KeyEvent.VK_ESCAPE) { cb.hidePopup(); return; } if (key.getKeyCode() == KeyEvent.VK_TAB) { print("Have tab event (modifiers=" + key.getModifiers() + ")"); if ((key.getModifiers() & ActionEvent.SHIFT_MASK) == 0 && cb.isPopupVisible()) { cb.setSelectedIndex(0); cb.hidePopup(); } else swing_standardTabBehavior(key); return; } JTextField tf = (JTextField) (cb.getEditor().getEditorComponent()); if (tf.getCaretPosition() != l(tf.getText())) return; String text = ((JTextField) key.getSource()).getText(); Vector list = getFilteredList(text); if (nempty(list)) { cb.setModel(new DefaultComboBoxModel(list)); cb.setSelectedIndex(-1); tf.setText(text); cb.showPopup(); } else cb.hidePopup(); } public Vector getFilteredList(String text) { return new Vector(scoredSearch(text, vector)); } } public abstract static class LiveValue { public abstract Class getType(); public abstract A get(); public abstract void onChange(Runnable l); public abstract void removeOnChangeListener(Runnable l); public void onChangeAndNow(Runnable l) { onChange(l); callF(l); } } public static class Scored extends Var { public float score; public Scored() { } public Scored(A a, float score) { super(a); this.score = score; } public float score() { return score; } public String toString() { return toIntPercent(score) + "%: " + str(get()); } } public abstract static class TripleListener { public void onNewTriple(TripleWeb w) { } public void onForgottenTriple(TripleWeb w) { } } public static class SynchronizedSet extends SynchronizedCollection implements Set { public SynchronizedSet() { } public SynchronizedSet(Set set) { super(set); } public SynchronizedSet(Set set, Object mutex) { super(set, mutex); } public boolean equals(Object o) { if (this == o) return true; synchronized (this) { return c.equals(o); } } public int hashCode() { synchronized (this) { return c.hashCode(); } } } public static class SynchronizedCollection implements Collection { public Collection c; public Object mutex; public SynchronizedCollection() { } public SynchronizedCollection(Collection c) { this.c = Objects.requireNonNull(c); mutex = this; } public SynchronizedCollection(Collection c, Object mutex) { this.c = Objects.requireNonNull(c); this.mutex = Objects.requireNonNull(mutex); } public int size() { synchronized (mutex) { return c.size(); } } public boolean isEmpty() { synchronized (mutex) { return c.isEmpty(); } } public boolean contains(Object o) { synchronized (mutex) { return c.contains(o); } } public Object[] toArray() { synchronized (mutex) { return c.toArray(); } } public T[] toArray(T[] a) { synchronized (mutex) { return c.toArray(a); } } public Iterator iterator() { return c.iterator(); } public boolean add(A A) { synchronized (mutex) { return c.add(A); } } public boolean remove(Object o) { synchronized (mutex) { return c.remove(o); } } public boolean containsAll(Collection coll) { synchronized (mutex) { return c.containsAll(coll); } } public boolean addAll(Collection coll) { synchronized (mutex) { return c.addAll(coll); } } public boolean removeAll(Collection coll) { synchronized (mutex) { return c.removeAll(coll); } } public boolean retainAll(Collection coll) { synchronized (mutex) { return c.retainAll(coll); } } public void clear() { synchronized (mutex) { c.clear(); } } public String toString() { synchronized (mutex) { return c.toString(); } } @Override public Spliterator spliterator() { return c.spliterator(); } } public static JComponent unwrap(JComponent c) { return c instanceof JScrollPane ? unwrapScrollPane((JScrollPane) c) : c; } public static void setSelectedIndex(final JList l, final int i) { { swing(new Runnable() { public void run() { try { l.setSelectedIndex(i); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "l.setSelectedIndex(i);"; } }); } } public static void copyArrayToVector(Object[] array, Vector v) { v.clear(); v.addAll(toList(array)); } public static void swing_standardTabBehavior(KeyEvent key) { if ((key.getModifiers() & ActionEvent.SHIFT_MASK) != 0) KeyboardFocusManager.getCurrentKeyboardFocusManager().focusPreviousComponent(); else KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent(); } public static void onChange(JSpinner spinner, Object r) { spinner.addChangeListener(changeListener(r)); } public static A onChange(A b, Object r) { b.addItemListener(itemListener(r)); return b; } public static void onChange(JTextComponent tc, Object r) { onUpdate(tc, r); } public static void onChange(final JSlider slider, final Object r) { { swing(new Runnable() { public void run() { try { slider.addChangeListener(changeListener(r)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "slider.addChangeListener(changeListener(r));"; } }); } } public static void onChange(JComboBox cb, Object r) { onChange(textFieldFromComboBox(cb), r); } public static int toIntPercent(double ratio) { return roundToInt(ratio * 100); } public static JComponent unwrapScrollPane(JScrollPane sp) { return sp == null ? null : (JComponent) sp.getViewport().getView(); } public static ChangeListener changeListener(final Object r) { return new ChangeListener() { public void stateChanged(ChangeEvent e) { pcallF(r); } }; } public static ItemListener itemListener(final Object r) { return new ItemListener() { public void itemStateChanged(ItemEvent e) { pcallF(r); } }; } public static void onUpdate(JComponent c, final Object r) { if (c instanceof JTextComponent) ((JTextComponent) c).getDocument().addDocumentListener(new DocumentListener() { public void insertUpdate(DocumentEvent e) { call(r); } public void removeUpdate(DocumentEvent e) { call(r); } public void changedUpdate(DocumentEvent e) { call(r); } }); else if (c instanceof ItemSelectable) ((ItemSelectable) c).addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { call(r); } }); else print("Warning: onUpdate doesn't know " + getClassName(c)); } public static void onUpdate(List l, Object r) { for (JComponent c : l) onUpdate(c, r); } public static JTextField textFieldFromComboBox(JComboBox cb) { return (JTextField) cb.getEditor().getEditorComponent(); } }