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.SimpleDateFormat; import java.awt.dnd.*; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.DataFlavor; import java.security.KeyStore; import java.nio.ByteBuffer; import java.util.regex.Pattern; import java.awt.datatransfer.DataFlavor; import javax.swing.event.AncestorEvent; import java.awt.datatransfer.*; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.nio.channels.FileChannel; import java.util.logging.Logger; import java.io.*; import java.nio.charset.Charset; import java.util.logging.Logger; import javax.net.ssl.*; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.awt.geom.AffineTransform; import java.awt.geom.*; import javax.swing.event.AncestorListener; import javax.swing.Timer; import java.awt.geom.*; import java.awt.geom.*; import java.awt.font.GlyphVector; import javax.swing.Timer; import javax.swing.undo.UndoManager; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.util.logging.Level; import javax.net.ssl.*; import java.text.*; import java.text.NumberFormat; import java.text.*; import java.text.*; import java.text.SimpleDateFormat; import java.util.*; import java.net.*; import java.nio.channels.FileChannel; import java.security.KeyStore; import java.util.logging.Level; import java.util.regex.Matcher; import java.util.zip.GZIPOutputStream; class main { static class WebNode { Web web; Object labels; // SmallestList Object visInfo; // visualisation info WebNode() {} WebNode(Web web) { this.web = web;} void addLabel(String label) { addLabel(web.parseLabel(label)); } void addLabel(Lisp label) { if (!smallestList_contains(labels, label)) { labels = smallestList_add(labels, label); if (web != null) web.index(label, this); } } void addLabels(Collection l) { for (Lisp lbl : l) addLabel(lbl); } void addStrings(Collection l) { for (String lbl : l) addLabel(lbl); } boolean hasLabel(Lisp label) { return smallestList_contains(labels, label); } boolean hasLabel(String label) { return smallestList_contains(labels, web.parseLabel(label)); } public String toString() { return smallestList_l(labels) == 1 ? str(smallestList_first(labels)) : str(smallestList_toList(labels)); } int count() { return 1 + l(labels); } String text() { return web.unparseLabel((Lisp) smallestList_first(labels)); } List texts() { return web.unparseLabels(smallestList_toList(labels)); } void relation(String arrow, String b) { web.getRelation(this, web.node(b)).addLabel(arrow); } Lisp parseLabel(String l) { return web.parseLabel(l); } String unparseLabel(Lisp l) { return web.unparseLabel(l); } List labels() { return smallestList_toList(labels); } boolean hasLabels() { return labels != null; } void setLabel(int i, Lisp l) { labels = smallestList_set(labels, i, l); } double x, y; // placement on screen } static class WebRelation extends WebNode { WebNode a, b; WebRelation() {} WebRelation(Web web, WebNode a, WebNode b) { super(web); this.a = a; this.b = b; } } static class Web { List nodes = new ArrayList(); // Relations are also nodes Map, WebRelation> relations = new HashMap(); MultiMap index = new MultiMap(); // label -> node //Map> pots = new Map; //int flags; // TODO boolean labelsToUpper; String title, globalID = aGlobalID(); String source; boolean unverified; long created = nowUnlessLoading(); static int F_useCLParse = 1; static int F_labelsToupper = 2; static int F_unverified = 3; /*bool potNotEmpty(S pot) { ret nempty(getPot(pot)); } L clearPot(S pot) { L l = getPot(pot); L l2 = cloneList(l); l.clear(); ret l2; }*/ /*L getPot(S pot) { L l = pots.get(pot); if (l == null) pots.put(pot, l = cloneList(nodes)); ret l; }*/ void relation(WebNode a, String arrow, WebNode b) { getRelation(a, b).addLabel(arrow); } Pair relation(String a, String arrow, String b) { return relation(lisp(arrow, a, b)); } 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")) { // X is Y. 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; } void relations(List l) { for (Lisp li : l) relation(li); } WebRelation getRelation(String a, String b) { return getRelation(findNode(a), findNode(b)); } WebRelation getRelation(WebNode a, WebNode b) { return getRelation(pair(a, b)); } WebRelation relation(WebNode a, WebNode b) { return getRelation(a, b); } WebRelation relation(Pair p) { return getRelation(p); } WebRelation getRelationOpt(WebNode a, WebNode b) { return relations.get(pair(a, b)); } WebRelation getRelation(Pair p) { WebRelation r = relations.get(p); if (r == null) relations.put(p, r = _newRelation(p.a, p.b)); return r; } WebRelation _newRelation(WebNode a, WebNode b) { WebRelation r = new WebRelation(this, a, b); nodes.add(r); //for (L l : values(pots)) l.add(r); return r; } WebNode newNode() { WebNode node = new WebNode(this); nodes.add(node); //for (L l : values(pots)) l.add(node); return node; } WebNode newNode(String s) { WebNode node = newNode(); node.addLabel(parseLabel(s)); return node; } WebNode node(String s) { return findNode(s); } WebNode node(Lisp l) { return findNode(l); } WebNode findNode(String s) { return findNode(parseLabel(s)); } WebNode findNode(Lisp l) { WebNode n = findNodeOpt(l); return n != null ? n : newNode(l); } WebNode findNodeOpt(Lisp l) { return first(index.get(l)); } WebNode newNode(String... labels) { WebNode n = newNode(); for (String label : labels) n.addLabel(label); return n; } WebNode newNode(Lisp... labels) { WebNode n = newNode(); for (Lisp label : labels) n.addLabel(label); return n; } public String toString() { return webToString(this); } void index(Lisp label, WebNode n) { index.put(label, n); } void clear() { clearAll(nodes, relations, index/*, pots*/); } Lisp parseLabel(String s) { return lisp(labelsToUpper ? upper(s) : s); } String unparseLabel(Lisp l) { return lispHead(l); } List parseLabels(List l) { return map(new Object() { Object get(String s) { try { return parseLabel(s) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "parseLabel(s)"; }}, l); } List unparseLabels(List l) { return map(new Object() { Object get(Lisp l) { try { return unparseLabel(l) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "unparseLabel(l)"; }}, l); } void removeNode(WebNode n) { if (n == null || !nodes.contains(n)) return; n.web = null; List> relationsToDelete = pairList_lookupAnySide(keys(relations), n); for (Lisp label : web_labels(n)) index.remove(label, n); nodes.remove(n); for (Pair p : relationsToDelete) removeRelation(p.a, p.b); } void removeRelation(WebNode a, WebNode b) { Pair p = pair(a, b); WebNode r = relations.get(p); if (r == null) return; relations.remove(p); removeNode(r); } } static boolean greetCookies, theoryOn; static List thoughtBots = new ArrayList(); static long started; static Class theoryModule; static Lock theoryLock = lock(); static double delay = 0; static String selectedWord; static int myPort = 4678; static double speculationTimeoutForHttp = 5.0; // 5 seconds static Queue speculationQueue = new ConcurrentLinkedQueue(); // save space static class E {} public static void main(final String[] args) throws Exception { started = sysNow(); db(); load("selectedWord"); autoRestart(5); typicalDownload(); serveHttp(myPort); bot("Smart Bot."); loadPage_forcedTimeout = 70000; thoughtBots.add(mc()); //loadBots(#1010753, #1010821, #1010825, #1001951); loadBots("#1010825", "#1001951"); 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();\n addTransientWeb(webFromTriple(\"Latest chat line\", \"..."; }}); stefansChat_onLine_lookback = 2; stefansChat_onLine_onHistoryRead.set("onHistoryRead"); stefansChat_onLine_safe(new Object() { void get(String text) { try { sleepSeconds(delay); try { int nr = toInt(mapGet(stefansChat_onLine_fullParams.get(), "nr")); print("> [" + nr + "] " + text); if (nr != 0) { String x = "Chat line " + nr; ai_postTriple(x, "is", quote(text)); ai_postTriple(x, "is", "recent"); stefansChat_n_notify(nr); ai_speculate(x); } } catch (Throwable __e) { printStackTrace2(__e); } String answer = scanMultipleBots(thoughtBots, text); if (nempty(answer)) postToStefansChat(answer); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "sleepSeconds(delay);\n \n pcall {\n int nr = toInt(mapGet(stefansChat..."; }}); //stefansChat_post("Smart Bot Upgraded! Boot took: " + formatDouble(fromMS(sysNow()-started), 1) + " s"); if (greetCookies) runInNewThread("#1010793"); // Post on UAIP // Do the slow stuff addVirtualNodeIndex(hugeEnglishDictionary_virtualNodeIndex()); loadTheory(); cachedNodeIndex(); // make index ai_onNewIndexedText_do(new Object() { Object get(String s) { try { return speculationQueue.add(s) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "speculationQueue.add(s)"; }}); { Thread _t_0 = new Thread("Speculator") { public void run() { try { speculatorLoop(); } catch (Throwable __e) { printStackTrace2(__e); } } }; startThread(_t_0); } } static void loadTheory() { Lock _lock_2 = theoryLock; lock(_lock_2); 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(_lock_2); } } static int lineCount; static boolean authed() { return isTrue(mapGet(stefansChat_onLine_fullParams.get(), "auth")); } static boolean byBot() { return isTrue(mapGet(stefansChat_onLine_fullParams.get(), "botMark")); } static String answer(String s) { final Matches m = new Matches(); try { ++lineCount; boolean authed = authed(); Lock _lock_3 = dbLock(); lock(_lock_3); try { s = ai_dropDroppableStuff(s); int safety = 0; replaceloop: while (safety++ < 10) { s = trim(s); { String _a_11 = answer_inner(s, lineCount); if (!empty(_a_11)) return _a_11; } 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; } //if (ai_isQuestion_1(s)) ret "No idea"; } finally { unlock(_lock_3); } } catch (Throwable e) { printException(e); return exceptionToStringShort(e); } return null; } static String answer_inner(String s, int lineCount) { final Matches m = new Matches(); if (authed()) { if (lineCount >= stefansChat_onLine_lookback) { if (eqic(s, "!restart")) { restart(); return "Yo"; } 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_6 = sysNow(); Either e = (Either) evalWithTimeoutReleasingDBLock(60.0, "ai_idle_matchPatterns"); _startTime_6 = sysNow()-_startTime_6; saveTiming(_startTime_6); return e.isA() ? "Made " + nWebs(e.a()) + " in " + toSeconds(lastTiming(), 1) + "s" : "Timeout"; } 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_7 = sysNow(); Either e = (Either) evalWithTimeoutReleasingDBLock(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_7 = sysNow()-_startTime_7; saveTiming(_startTime_7); int n = 0; try { n = e.isA() ? e.a() : websMadeInThread(e.b()); } catch (Throwable __e) { printStackTrace2(__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 (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, "!search ", m)) { selectWord("[search] " + m.rest()); return "OK " + unicode_rightPointingTriangle() + unicode_rightPointingTriangle(); } if (swic_trim(s, "!bench-search", m)) { T3 t = ai_parseArrowTriple(m.rest()); long _startTime_8 = sysNow(); int n = l(ai_search_dollarX(t)); _startTime_8 = sysNow()-_startTime_8; saveTiming(_startTime_8); return n(n, "result") + ", " + lastTiming() + " ms"; } if (swic_trim(s, "!google ", m)) return join(" - ", swapPair(first(quickGoogle(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.lol/1007546?triples=" + urlencode(lines(elements)); } if (swic_trim(s, "!web ", m)) { String id = m.unq(0); Web web = indexedWebWithGlobalID(id); if (web == null) return "Not found"; return "[IMAGE] http://ai1.lol:" + myPort + "/diagram?id=" + id; } if (swic_trim(s, "!parse ", m)) return ai_renderTriple(ai_tripelize(m.rest())); 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 (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_12 = ai_sfCommands(s); if (!empty(_a_12)) return _a_12; } } catch (Throwable __e) { printStackTrace2(__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_13 = ai_answerFromCache(s); if (!empty(_a_13)) return _a_13; } String _s = s; s = ai_dropLeadingAdditions(s); if (neq(_s, s)) { String _a_14 = ai_answerFromCache(s); if (!empty(_a_14)) return _a_14; } if (match("cache size", s)) return n(keys(cachedNodeIndex()), "different term") + ", " + n(web_countNodes(allWebs_cached()), "node") + ", " + n(allWebs_cached(), "web"); 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)); //ret ai_renderList(l); if (nempty(l)) return m.unq(0) + " " + first(l) + " " + m.unq(1); } 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_15 = ai_renderNodesList(ai_whatIs(selectWord(m.unq(0))), ""); if (!empty(_a_15)) return _a_15; } 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()); // Once more with generated webs (replacing $ vars) if (learnedFlexMatch("What is *", s, m)) { List extendedWebs = indexedWebsAfterVariableSubstitution(m.unq(0)); print("Have " + n(extendedWebs, "extended web") + " for " + quote(m.unq(0))); return ai_renderNodesList(webs_search_dollarX(webFromTriples(m.unq(0), "is", "$X"), extendedWebs), "I don't know"); } if (!ai_isQuestion_1(s) && learnedFlexMatch_multi(s, m, "* is *", "* ist *")) { String _a_16 = postTripleFromInput(triple(m.get(0), "is", m.get(1)), s); if (!empty(_a_16)) return _a_16; } return null; } static void loadBots(String... botIDs) { for (String id : botIDs) try { thoughtBots.add(runSubBot(id)); } catch (Throwable __e) { printStackTrace2(__e); } } static Object html(String uri, Map params) { { long _startTime_5 = sysNow(); try { return html_2(uri, params); } finally { _startTime_5 = sysNow()-_startTime_5; saveTiming(_startTime_5); } } } static Object html_2(String s, Map params) { if (eqic(s, "/favicon.ico")) return serve404(); 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))); // Serve Web With Global ID s = dropPrefix("/", s); if (possibleGlobalID(s)) { List webs = allWebsByID().get(s); if (nempty(webs)) { return h2_title("Web " + s) + pre(htmlencode(renderWeb_multiLine(first(webs)))) + p(himg(ai_webImage_link(s), "title" , "Web " + s)) + (l(webs) > 1 ? p("Warning: Multiple webs") : ""); } /*if (theoryOn && theoryModule == null) ret serveHTML("Loading theory module"); Map map = (Map) getOpt(theoryModule, 'theoryForLineMap); if (map.containsKey(toLower(s))) ret serveHTML("A line in the chat.");*/ return serveHTML("Unknown: " + s); } Matches m = new Matches(); if (swic(s, "e/", m)) { String topic = urldecode(m.rest()); long _startTime_9 = sysNow(); boolean timeout = ai_speculateWithTimeout(topic, speculationTimeoutForHttp); _startTime_9 = sysNow()-_startTime_9; saveTiming(_startTime_9); long time = lastTiming(); return h1_title(htmlencode_noQuote("Topic: " + topic)) //+ p("Have " + n(indexedWebs(topic), "web"))) + ai_html_wordThoughts(topic) + p("Speculated for " + time + " ms" + (timeout ? " (TIMED OUT)" : "")); } if (eqic(s, "alphabetical")) return html_alphabetical(); if (eqic(s, "latest-webs")) { int n = toInt(params.get("n")); return html_latestWebs(min(1000, max(n, 10))); } // Home Page Collection keys = multiMapKeysByPopularity(cachedNodeIndex()); return h1_title("Smart Bot's Encyclopedia (" + n_fancy(keys, "entry") + ")") + p("Most occurring | " + ahref("/alphabetical", "Alphabetical") + " | " + ahref("/latest-webs", "Latest")) + ul(map(new Object() { Object 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, \"-\")))\n + \" [\" +..."; }}, keys)); } static String html_alphabetical() { return h1_title("Smart Bot's Encyclopedia :)") + ul(map(new Object() { Object 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, \"-\")))"; }}, keys(cachedNodeIndex()))); } static String html_latestWebs(int n) { return h1_title("Smart Bot's Encyclopedia - Latest Webs") + ul(map(new Object() { Object get(Web web) { try { return ai_html_linkedWeb(web) + htmlencode(" [" + web.created + " - " + renderGMTDate(web.created) + "] " + web.source + ": ") + ai_html_renderWebShort(web) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ai_html_linkedWeb(web) + htmlencode(\" [\" + web.created + \" - \" + renderGMTDat..."; }}, takeFirst(n, sortByFieldDesc("created", allWebsFromCachedNodeIndex())))); } static String html_thoughts() { String html = /*hrefresh(5) +*/ hGoogleFontOswald(); String status = ""; Pair p = evalWithTimeout_numberOfCalculations(); if (p.a > 0) status = p(n(p.a, "calculation") + (p.b == 0 ? "" : ", " + p.b + " timed out")); return html + wordThoughts() + status; /* if (theoryModule == null) ret html + "Loading theory module..."; S thoughts = (S) call(theoryModule, 'html_thoughts); if (nempty(selectedWord)) ret html + tag('table, tr(td_top(thoughts, style := "background: #CCC") + td_top(wordThoughts(), style := "padding-left: 20px"))); ret html + thoughts; */ } static String wordThoughts() { try { if (empty(selectedWord)) return ""; return html_addTargetBlank(ai_html_wordThoughts(selectedWord)); } catch (Throwable e) { printStackTrace(e); return "Erreur"; } } static String postTripleFromInput(T3 triple, String input) { if (swic(input, "OK, stored")) return null; if (!ai_tripleAllowedToPost(triple)) return null; selectWord(triple.a); if (ai_cache_hasTriple(triple)) return "I know"; else { Web web = webFromTriple(triple); web.unverified = !authed(); postSoftwareMadeWeb(web, "input", input); return "OK, stored" + (web.unverified ? " (unverified)" : "") + ": " + ai_renderTriple(triple); } } 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; } } static void processSelectedWord() { final String word = selectedWord; if (empty(word)) return; ai_withMaker("processSelectedWord", new Runnable() { public void run() { try { //ai_speculate(word); 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) { printStackTrace2(__e); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "//ai_speculate(word);\n pcall {\n ai_greetingRule1(word);\n if (ai_..."; }}); } 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) { print("Unrecenting " + n); ai_invalidateWeb(node.web); } } catch (Throwable __e) { printStackTrace2(__e); } } static void onHistoryRead() { Lock _lock_4 = dbLock(); lock(_lock_4); try { print("History read."); processSelectedWord(); try { makeChatLinesUnrecent(); } catch (Throwable __e) { printStackTrace2(__e); } } finally { unlock(_lock_4); } } static String selectWord(String word) { if (nempty(word)) { selectedWord = word; save("selectedWord"); processSelectedWord(); } return word; } static String transpileMyself(String mode) { postToStefansChat("Transpiling..."); Pair p = transpileOnServer(programID(), "medium"); return p.a ? "OK" : "Not OK"; } static void speculatorLoop() { while (licensed()) { try { ai_speculate(selectedWord); ai_speculate(randomIndexedTerm()); ai_activeSpec(selectedWord); String s; while ((s = speculationQueue.poll()) != null) { long time = sysNow(); ai_speculateWithActive(s); done2_always(time, "Speculation Queue > " + s); } } catch (Throwable __e) { printStackTrace2(__e); } sleepSeconds(1); } } static String aGlobalID() { return randomID(16); } static String upper(String s) { return s == null ? null : s.toUpperCase(); } static long done2_always(long startTime, String desc) { long time = sysNow()-startTime; print(desc + " [" + time + " ms]"); return time; } static long done2_always(String desc, long startTime) { return done2_always(startTime, desc); } static long done2_always(long startTime) { return done2_always(startTime, ""); } static Pair swapPair(Pair p) { return p == null ? null : pair(p.b, p.a); } static String ai_webImage_link(String globalID) { return smartBotURL() + "/diagram?id=" + urlencode(globalID); } static long postToStefansChat_lastPost = -60000; static boolean postToStefansChat(String text) { if (empty(text)) return false; print("CHAT>> " + text); boolean ok = eq("OK", loadPage("http://ai1.lol/" + psI("#1008998") + "/raw/msg?botmark=1&message=" + urlencode(text))); postToStefansChat_lastPost = sysNow(); return ok; } static String ai_speculateWithActive(String word) { ai_speculate(word); ai_activeSpec(word); return word; } static String quote(Object o) { if (o == null) return "null"; return quote(str(o)); } 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(); } 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 out.append(c); } out.append('"'); } static long secondsSinceSysTime(long time) { return (sysNow()-time)/1000; } static String trim(String s) { return s == null ? null : s.trim(); } static String trim(StringBuilder buf) { return buf.toString().trim(); } static String trim(StringBuffer buf) { return buf.toString().trim(); } static String web_text(WebNode node) { return node == null ? null : node.text(); } static long unixDay() { return unixHour()/24; } static class MyHTTPD extends NanoHTTPD { MyHTTPD(int port) { super(port); } Object onServe, onEndServe; volatile long requests; static class Serving { String uri; Map header, parms, files; } List currentlyServing = synchroList(); public Response serve(String uri, Method method, Map header, Map parms, Map files) { ++requests; Serving serving = nu(Serving.class, "uri", uri, "header", header, "parms", parms, "files", files); currentlyServing.add(serving); pcallF(onServe, serving); try { String remoteAddr = header.get("remote-addr"); print(formatDateAndTime() + " Serving URI: " + quote(uri) + " to: " + remoteAddr); /*if (nempty(remoteAddr) && isTrue(callOpt(mc(), "isBlockedIP", remoteAddr))) { print("BLOCKED IP."); ret serve404(); }*/ try { Response response = (Response) ( callOpt(getMainClass(), "serve", uri, method, header, parms, files)); if (response != null) return response; Object html = callHtmlMethod2(getMainClass(), uri, parms); 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); } } } static MyHTTPD serveHttp_server; static int serveHttp_port = 8888; static TreeSet serveHttp_peers = new TreeSet(); static int serveHttp(int port) { try { serveHttp_port = port; serveHttp_server = new MyHTTPD(port); serveHttp_server.start(); return serveHttp_port(); } catch (Exception __e) { throw rethrow(__e); } } // serve on random port, return port number used static int serveHttp() { serveHttp(0); return serveHttp_port(); } static int serveHttp_port() { return serveHttp_server == null ? 0 : serveHttp_server.getListeningPort(); } static void cleanMeUp_serveHttp() { if (serveHttp_server != null) { serveHttp_server.stop(); serveHttp_server = null; } } 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)) { //print("match."); if (ai_postTriple(web_text(nPat), "matches", sOrig) != null) ++websMade; } } } } return websMade; } finally { ai_setMaker(previousMaker); } } static List ai_onNewIndexedText_list = synchroList(); // f: voidfunc(S) static void ai_onNewIndexedText_do(Object f) { setAdd(ai_onNewIndexedText_list, f); } static void ai_onNewIndexedText(String text) { pcallFAll(ai_onNewIndexedText_list, text); } static NanoHTTPD.Response serveText_direct(String s) { return serveByteArray(toUtf8(s), "text/plain"); } static Throwable printStackTrace(Throwable e) { // we go to system.out now - system.err is nonsense print(getStackTrace(e)); return e; } static void printStackTrace() { printStackTrace(new Throwable()); } static void printStackTrace(String msg) { printStackTrace(new Throwable(msg)); } /*static void printStackTrace(S indent, Throwable e) { if (endsWithLetter(indent)) indent += " "; printIndent(indent, getStackTrace(e)); }*/ static List ai_search_dollarX(String a, String b, String c) { return ai_index_search_dollarX(a, b, c); } static List ai_search_dollarX(T3 t) { return ai_search_dollarX(t.a, t.b, t.c); } 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));"; }}); } static String str(Object o) { return o == null ? "null" : o.toString(); } static String str(char[] c) { return new String(c); } static T3 triple(A a, B b, C c) { return new T3(a, b, c); } static long lastTiming() { return getLastTiming(); } static Map singular_specials = litmap( "children", "child", "images", "image", "chess", "chess"); static Set singular_specials2 = litset("time", "machine"); static String singular(String s) { if (s == null) return null; { String _a_65 = singular_specials.get(s); if (!empty(_a_65)) return _a_65; } { String _a_66 = hippoSingulars().get(lower(s)); if (!empty(_a_66)) return _a_66; } if (singular_specials2.contains(dropSuffix("s", 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; } 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; } static int ai_speculate_all() { return ai_speculate_all(""); } static DependentCache> allWebsByID_cache = new DependentCache("allWebsByID_calc", "cachedNodeIndex" /*_changeCount*/); static MultiMap allWebsByID() { return allWebsByID_cache.get(); } static MultiMap allWebsByID_calc() { setAdd(postSoftwareMadeWeb_onNewWeb, "allWebsByID_onNewWeb"); onTransientWebAdded_do("allWebsByID_onNewWeb"); onTransientWebRemoved_do("allWebsByID_onRemovedWeb"); return multiMapIndexByField(allWebsFromCachedNodeIndex_uncached(), "globalID"); } static void allWebsByID_onNewWeb(Web web, Object[] params) { allWebsByID_onNewWeb(web); } static void allWebsByID_onNewWeb(Web web) { Lock _lock_71 = allWebsByID_cache.lock; lock(_lock_71); try { if (allWebsByID_cache != null) allWebsByID_cache.get().put(web.globalID, web); } finally { unlock(_lock_71); } } static void allWebsByID_onRemovedWeb(Web web) { Lock _lock_72 = allWebsByID_cache.lock; lock(_lock_72); try { if (allWebsByID_cache != null) allWebsByID_cache.get().remove(web.globalID); } finally { unlock(_lock_72); } } static String ai_renderList(List l) { if (empty(l)) return "-"; return joinWithComma(allToString(l)); } 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; } // TODO: returns empty first, but not empty last 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; } static String ai_html_renderWebShort(Web web) { try { int n = l(web_nodes(web)); if (n == 0) return "Empty web"; WebNode a = web_firstNode(web); if (n == 1) return join(" = ", map("html_encyclopediaTopic",web_texts(a))); if (n == 2) { WebNode b = web_secondNode(web); WebRelation r= web_getRelationOpt(a, b); if (r != null) return ai_html_renderTriple(r); } return "Includes " + joinWithComma(map("html_encyclopediaTopic",map("web_text",web_nodes(web)))); } catch (Throwable e) { printException(e); return "Error"; } } static boolean swic(String a, String b) { return startsWithIgnoreCase(a, b); } 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; } static MultiMap cachedNodeIndex() { return cachedNodeIndex2(); } static List web_texts(WebNode node) { return node == null ? new ArrayList() : node.texts(); } static List web_texts(Collection l) { return map("web_text",l); } static List webs_search_dollarX(Web searchWeb, Collection webs) { List < Map > mappings = new ArrayList(); for (Web web : webs) addIfNotNull(mappings, web_matchAllPerms_x(searchWeb, web, "nodeMatch_dollarVars")); return getFromAll(mappings, web_findNode(searchWeb, "$X")); } 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; } static String ai_answerFromCache(String q) { for (int _repeat_91 = 0; _repeat_91 < 10; _repeat_91++) { { String _a_93 = ai_renderNodesList(ai_search_dollarX_verified("$X", "answers", q), null, " or "); if (!empty(_a_93)) return _a_93; } WebNode n = first(ai_search_dollarX_verified(q, "see answer to:", "$X")); if (n != null) q = web_text(n); else break; } return null; } static int min(int a, int b) { return Math.min(a, b); } static long min(long a, long b) { return Math.min(a, b); } static float min(float a, float b) { return Math.min(a, b); } static float min(float a, float b, float c) { return min(min(a, b), c); } static double min(double a, double b) { return Math.min(a, b); } static double min(double[] c) { double x = Double.MAX_VALUE; for (double d : c) x = Math.min(x, d); return x; } static float min(float[] c) { float x = Float.MAX_VALUE; for (float d : c) x = Math.min(x, d); return x; } static byte min(byte[] c) { byte x = 127; for (byte d : c) if (d < x) x = d; return x; } static short min(short[] c) { short x = 0x7FFF; for (short d : c) if (d < x) x = d; return x; } 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; } static boolean hasIndexedNode(String s) { return cachedNodeIndex().containsKey(s); } static boolean matchStartX(String pat, String s, Matches matches) { return matchStart(dropSuffix("...", pat), s, matches); } static String scanMultipleBots(List bots, String s) { for (Object bot : unnull(bots)) try { // just print exceptions, carry on { String _a_105 = callAnswerMethod(bot, s); if (!empty(_a_105)) return _a_105; } } catch (Throwable __e) { printStackTrace2(__e); } return null; } static String a(String noun) { if (eq(noun, "")) return "?"; return ("aeiou".indexOf(noun.charAt(0)) >= 0 ? "an " : "a ") + noun; } static String a(String contents, Object... params) { return htag("a", contents, params); } static void ai_invalidateWeb(Web web) { if (web == null) return; ai_postTriple("Web " + web.globalID, "is", "invalid"); } static volatile boolean licensed_yes = true; static boolean licensed() { ping(); return licensed_yes; } static void licensed_off() { licensed_yes = false; } static List indexedWebsAfterVariableSubstitution(String s) { return nodesToWebs(indexedNodesAfterVariableSubstitution(s)); } static Map _registerThread_threads = Collections.synchronizedMap(new WeakHashMap()); static Thread _registerThread(Thread t) { _registerThread_threads.put(t, true); return t; } static void _registerThread() { _registerThread(Thread.currentThread()); } static String himg(String src, Object... params) { return tag("img", "", arrayPlus(params, "src", src)); } static int l(Object[] a) { return a == null ? 0 : a.length; } static int l(boolean[] a) { return a == null ? 0 : a.length; } static int l(byte[] a) { return a == null ? 0 : a.length; } static int l(int[] a) { return a == null ? 0 : a.length; } static int l(float[] a) { return a == null ? 0 : a.length; } static int l(char[] a) { return a == null ? 0 : a.length; } static int l(Collection c) { return c == null ? 0 : c.size(); } static int l(Map m) { return m == null ? 0 : m.size(); } static int l(CharSequence s) { return s == null ? 0 : s.length(); } static long l(File f) { return f == null ? 0 : f.length(); } static int l(Object o) { return o instanceof String ? l((String) o) : o instanceof Map ? l((Map) o) : l((Collection) o); // incomplete } static int l(Lisp l) { return l == null ? 0 : l.size(); } static String exceptionToStringShort(Throwable e) { lastException(e); e = getInnerException(e); String msg = unnull(e.getMessage()); if (msg.indexOf("Error") < 0 && msg.indexOf("Exception") < 0) return baseClassName(e) + prependIfNempty(": ", msg); else return msg; } static String renderConcepts(Collection l) { return fromLines(map(l, new Object() { Object get(Concept c) { try { return renderConcept(c) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "renderConcept(c)"; }})); } 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) { printStackTrace2(__e); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pcall ping { ai_activeSpec_answerOnChat(word); }"; }}); return word; } static String lispHead(Lisp l) { return l == null ? null : l.head; } static String n(long l, String name) { return l + " " + trim(l == 1 ? singular(name) : getPlural(name)); } static String n(Collection l, String name) { return n(l(l), name); } static String n(Map m, String name) { return n(l(m), name); } static String n(Object[] a, String name) { return n(l(a), name); } static Object call(Object o) { return callFunction(o); } // varargs assignment fixer for a single string array argument static Object call(Object o, String method, String[] arg) { return call(o, method, new Object[] {arg}); } static Object call(Object o, String method, Object... args) { try { if (o instanceof Class) { Method m = call_findStaticMethod((Class) o, method, args, false); m.setAccessible(true); return m.invoke(null, args); } else { Method m = call_findMethod(o, method, args, false); m.setAccessible(true); return m.invoke(o, args); } } catch (Exception e) { throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e); } } 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()); } 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()); } private 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; } static String p(Object contents, Object... params) { return tag("p", contents, params) + "\n"; } static boolean isTrue(Object o) { if (o instanceof Boolean) return ((Boolean) o).booleanValue(); if (o == null) return false; throw fail(getClassName(o)); } static double stefansChat_onLine_safe_delay = 1; // prohibits answering your own questions - TODO: improve static void stefansChat_onLine_safe(final Object f) { stefansChat_onLine(new Object() { 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)..."; }}); } static String smartBot_encyclopediaLink(String topic) { return smartBotURL() + "/e/" + urlencode(topic); } static String lstr(Map map) { return str(l(map)); } static String lstr(Collection c) { return str(l(c)); } static String lstr(String s) { return str(l(s)); } static Object makeAndCall(String functionName, Object... args) { return makeAndCall_cached(functionName, args); } static String toUpper(String s) { return s == null ? null : s.toUpperCase(); } 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; } static String lisp2label(Lisp l) { return l.isLeaf() ? l.head : clUnparse(l); } static void db() { conceptsAndBot(); } static String sfu(Object o) { return structureForUser(o); } static int ai1_cookiesToday_int() { return parseFirstInt(ai1_cookiesToday()); } static ThreadLocal postSoftwareMadeWeb_disabled = new ThreadLocal(); static BetterThreadLocal postSoftwareMadeWeb_websMadeInThread = new BetterThreadLocal(); static ThreadLocal postSoftwareMadeWeb_maker = new ThreadLocal(); // L // function can also return false to stop triggerWebsChanged static List postSoftwareMadeWeb_onNewWeb = synchroList(); // L static List postSoftwareMadeWeb_onDisabledPost = synchroList(); static void postSoftwareMadeWeb(Web web, Object... params) { Lock _lock_157 = aiLock(); lock(_lock_157); try { web_intern(web); incIntThreadLocal(postSoftwareMadeWeb_websMadeInThread); 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)); pcallFAll(postSoftwareMadeWeb_onDisabledPost, web); } else { logStructure(postSoftwareMadeWeb_file(), nu(SoftwareMadeWeb.class, "date" , now(), "computerID" , computerID(), "programID" , programID(), "structure" , struct(web), "furtherInfo" , empty(params) ? null : litorderedmap(params))); markWebsPosted_createMarker(); if (!pcallF_anyFalse(postSoftwareMadeWeb_onNewWeb, web)) triggerWebsChanged(); } } finally { unlock(_lock_157); } } static File postSoftwareMadeWeb_file() { return getProgramFile("webs-made.txt"); } static Class run(String progID, String... args) { Class main = hotwire(progID); callMain(main, args); return main; } 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 : keys(web.relations)) out.add(index.get(p.a) + " -> " + index.get(p.b) + " = " + web.relations.get(p)); return fromLines(out); } static String ahref(String link, Object contents, Object... params) { return href(link, contents, params); } static String callAnswerFunction(Object responder, String s) { return callAnswerMethod(responder, s); } static String callAnswerFunction(Object responder, String s, List history) { return callAnswerMethod(responder, s, history); } static boolean empty(Collection c) { return c == null || c.isEmpty(); } static boolean empty(String s) { return s == null || s.length() == 0; } static boolean empty(Map map) { return map == null || map.isEmpty(); } static boolean empty(Object[] o) { return o == null || o.length == 0; } 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); throw fail("unknown type for 'empty': " + getType(o)); } static boolean empty(float[] a) { return a == null || a.length == 0; } static boolean empty(int[] a) { return a == null || a.length == 0; } static boolean empty(long[] a) { return a == null || a.length == 0; } static Cache> thirdPersonVerbs_cache = new Cache("thirdPersonVerbs_load"); static Set thirdPersonVerbs() { return thirdPersonVerbs_cache.get(); } static Set thirdPersonVerbs_load() { return new TreeSet(nthColumn(englishVerbQuintuples(), 3)); } static boolean ai_isQuestion_1(String s) { return ai_isYesNoQuestion_1(s) || ai_questionWord_1(firstWord(s)); } // rels can be empty static String web_operandText(WebNode node, String... rels) { return unquote(web_text(web_operand(node, rels))); } static String ul(String... list) { return ul(asList(list)); } static String ul(List list, Object... params) { StringBuilder buf = new StringBuilder(); int i = indexOf(params, null); // null separates params for ul from params for li 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"; } static String web_nodePairToText(Pair p) { return p == null ? "-" : web_text(p.a) + " " + web_text(p.b); } static DependentCache> ai_trueWebs_cache = new DependentCache("ai_trueWebs_calc", "cachedNodeIndex" /*_changeCount*/); static Set ai_trueWebs() { return keys(ai_trueWebs_cache.get()); } 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(); } static void ai_trueWebs_onNewWeb(Web web) { if (web.unverified) return; Lock _lock_181 = ai_trueWebs_cache.lock; lock(_lock_181); 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(m.unq(0), web.globalID); } finally { unlock(_lock_181); } } static void ai_trueWebs_onRemovedWeb(Web web) { if (web.unverified) return; Lock _lock_182 = ai_trueWebs_cache.lock; lock(_lock_182); 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(m.unq(0), web.globalID); } finally { unlock(_lock_182); } } static String pre(Object contents) { return tag("pre", contents); } static void unlock(Lock lock, String msg) { print("Unlocking: " + msg); lock.unlock(); } static void unlock(Lock lock) { lock.unlock(); } static A last(List l) { return empty(l) ? null : l.get(l.size()-1); } static char last(String s) { return empty(s) ? '#' : s.charAt(l(s)-1); } static int last(int[] a) { return l(a) != 0 ? a[l(a)-1] : 0; } static Object last(Object[] a) { return l(a) != 0 ? a[l(a)-1] : null; } // make a lisp form static Lisp lisp(String head, Object... args) { Lisp l = new Lisp(head); for (Object o : args) l.add(o); return l; } static Lisp lisp(String head, Collection args) { return new Lisp(head, args); } static long web_countNodes(Collection webs) { long n = 0; if (webs != null) for (Web web : webs) n += l(web_nodesWithoutRelations(web)); return n; } static String htmlencode_noQuote(String s) { return htmlencode_noQuotes(s); } static void printException(Throwable e) { printStackTrace(e); } static boolean eq(Object a, Object b) { return a == null ? b == null : a == b || a.equals(b); } static String or2(String a, String b) { return nempty(a) ? a : b; } static List indexedNodesStartingWith(String s) { return allMultiMapValues(prefixSubMap((NavigableMap) cachedNodeIndex().data, s)); } static List ai_whatIs(String s) { return ai_index_search_dollarX(s, "is", "$X"); } static List> webs_search_dollarXY(final Web searchWeb, Collection webs) { List < Map > mappings = new ArrayList(); for (Web web : webs) addIfNotNull(mappings, web_matchAllPerms_x(searchWeb, web, "nodeMatch_dollarVars")); return map(mappings, new Object() { 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 ..."; }}); } static int ai_approximateIndexedNodesCount(String s) { return l(indexedNodes_rawLookup(s)); } static List> pairList_lookupAnySide(Collection> l, A a) { List < Pair > out = new ArrayList(); for (Pair p : unnull(l)) if (eq(p.a, a) || eq(p.b, a)) out.add(p); return out; } static void runAndCleanUp(String progID) { Class c = hotwire(progID); try { callMain(c); } finally { cleanUp(c); } } static String ai_renderNodesList(List l) { return ai_renderNodesList(l, "-"); } static String ai_renderNodesList(List l, String defaultText) { return empty(l) ? defaultText : joinWithComma(map("web_text",l)); } static String ai_renderNodesList(List l, String defaultText, String joiner) { return empty(l) ? defaultText : join(joiner, map("web_text",l)); } static void rotateSoftwareMadeWebs() { rotateLogFile(postSoftwareMadeWeb_file()); } static File ai_chatAnalysis_peepHoleFile() { return programFile("#1010980", "peephole-webs.structure.gz"); } static List trimAll(List l) { List l2 = new ArrayList(); for (String s : l) l2.add(trim(s)); return l2; } static String ai_renderTriple(T3 t) { return ai_tripleToString(t); } static String ai_renderTriple(String a, String b, String c) { return ai_tripleToString(triple(a, b, c)); } static List ai_index_search_dollarX(String a, String b, String c) { String query = isDollarVar(a) ? c : a; return webs_search_dollarX(webFromTriples(a, b, c), indexedWebs(query)); } static class WebToCAL { Web web; BufferedImage img = newBufferedImage(20, 20, Color.white); CirclesAndLines cal = new CirclesAndLines(); Map map = new HashMap(); //new Map, Line> lines; String makeLabel(Lisp lisp) { return or(lispHead(lisp), "[null]"); } void convert(final Web web) { this.web = web; cal.title = web.title; List nodes = web.nodes; nodes = listMinusSet(nodes, values(web.relations)); for (WebNode node : nodes) { double x = node.x, y = node.y; if (x == 0 && y == 0) { x = cal_randomCircleCoord(); y = cal_randomCircleCoord(); } //print("Circle " + node.text() + " pos: " + x + "/" + y); 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 : keys(web.relations)) { WebNode rel = web.relations.get(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; // TODO /*Line line = lines.get(p); if (line == null) lines.put(p,*/ Line line = cal.arrow(a, makeLabel(first(rel.labels())), b); for (Lisp l : dropFirst(rel.labels())) line.addTrait(makeLabel(l)); } cal.onUserMadeCircle = new Object() { 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 Object() { 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 Object() { 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 Object() { 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 Object() { void get(Circle c) { try { WebNode node = reverseLookup(map, c); //print("layout change: " + c.text + " " + (node != null)); 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 { //print("full layout change"); 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 ..."; }}; } 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; // TODO: persist? } } static CirclesAndLines webToCAL(WebNode node) { return webToCAL(node.web); } static CirclesAndLines webToCAL(Web web) { WebToCAL wtc = new WebToCAL(); wtc.convert(web); return wtc.cal; } static void webToCAL(Web web, CirclesAndLines cal, Canvas canvas) { copyCAL(webToCAL(web), cal); canvas.update(); } static boolean ai_html_wordThoughts_showMem; static int ai_html_wordThoughts_searchMax = 100; static String ai_html_wordThoughts(String selectedWord) { String t = ai_html_triangle(); String title = h3(htmlencode(selectedWord)); Matches m = new Matches(); if (swic_trim(selectedWord, "[search]", m)) { int max = ai_html_wordThoughts_searchMax; List l = indexedTerms_scoredSearch(m.rest(), max); return title + p(l(l) > ai_html_wordThoughts_searchMax ? max + "+ search results" : n(l, "search result")) + ul(map("html_encyclopediaTopic",takeFirst(max, l))); } Set ignoredWebs = ai_ignoredWebs(); List nodes = web_nodesNotFromCertainWebs(ignoredWebs, indexedNodes(selectedWord)); List rel1 = web_collectBackwardRelations(nodes); List rel2 = web_collectForwardRelations(nodes); //L is = ai_whatIs(selectedWord); final MultiMap mm = orderedMultiMap(); for (WebRelation r : rel2) mm.put(ai_textHTML(r) + t + ai_renderNodeHTML(r.b), r.web.globalID); for (WebRelation r : rel1) mm.put(ai_renderNodeHTML(r.a) + t + ai_textHTML(r) + t + ai_textHTML(r.b), r.web.globalID); for (WebNode n : nodes) if (n instanceof WebRelation) mm.put(ai_renderNodeHTML(((WebRelation) n).a) + t + ai_textHTML(n) + t + ai_textHTML(((WebRelation) n).b), n.web.globalID); long nVirtual = ai_numberOfVirtualWebs(); long mem = ai_html_wordThoughts_showMem ? memoryUsedAfterLastGC() : 0; return title + ul(map("html_linkURLs_targetBlank", allToString( map(multiMapKeysByPopularity(mm), new Object() { Object get(String s) { try { return s + " [" + joinWithComma(map("ai_html_linkedWeb",mm.get(s))) + "]" ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "s + \" [\" + joinWithComma(map(f ai_html_linkedWeb,mm.get(s))) + \"]\""; }}) ))) + p(n(nodes, "node") + " out of " + ahref(smartBotURL(), nWebs(keys(allWebsByID()))) + (nVirtual == 0 ? "" : " + " + formatWithThousandsSeparator(nVirtual) + " virtual") + (mem == 0 ? "" : ". " + toMB(mem) + " MB used")); } static Random random_random = new Random(); static int random(int n) { return n <= 0 ? 0 : random_random.nextInt(n); } static double random(double max) { return random()*max; } static double random() { return random_random.nextInt(100001)/100000.0; } static double random(double min, double max) { return min+random()*(max-min); } // min <= value < max static int random(int min, int max) { return min+random(max-min); } static A random(List l) { return oneOf(l); } static A random(Collection c) { if (c instanceof List) return random((List) c); int i = random(l(c)); return collectionGet(c, i); } static Object load(String varName) { readLocally(varName); return get(mc(), varName); } static Object load(String progID, String varName) { readLocally(progID, varName); return get(mc(), varName); } static List> quickGoogle(String query) { List < Pair > 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 = join(dropTags(contentsOfContainerTag(linkTok))); out.add(pair(link, text)); } return out; } static Thread startThread(Object runnable) { return startThread(defaultThreadName(), runnable); } static Thread startThread(String name, Object runnable) { return startThread(newThread(toRunnable(runnable), name)); } static Thread startThread(Thread t) { _registerThread(t); t.start(); return t; } static String lines(Collection lines) { return fromLines(lines); } static List lines(String s) { return toLines(s); } static String unquote(String s) { if (s == null) return null; if (s.startsWith("[")) { 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.startsWith("\"") || s.startsWith("\'")) && s.length() > 1) { int l = s.endsWith(substring(s, 0, 1)) ? 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); // Octal escape? 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 '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 '\'': ch = '\''; break; // Hex Unicode: u???? 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; // added by Stefan } i++; } sb.append(ch); } return sb.toString(); } return s; // not quoted - return original } static List multiMapKeysByPopularity(final MultiMap mm) { return sortedDescAccordingTo(keys(mm), new Object() { Object get(A key) { try { return l(mm.get(key)) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "l(mm.get(key))"; }}); } static String programID() { return getProgramID(); } 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); if (!ai_hasTriple_verified("Standard function " + quote(name), "is", "safe to call")) return "Not safe to call"; return sfu(callAndMake(name)); } return null; } // A concept should be an object, not just a string. // Functions that should always be there for child processes: static int concepts_internStringsLongerThan = 10; static ThreadLocal concepts_unlisted = new ThreadLocal(); static interface Derefable { Concept get(); } static interface IConceptIndex { void update(Concept c); // also for adding void remove(Concept c); } static interface IFieldIndex { List getAll(Val val); } static class Concepts { Map concepts = synchroTreeMap(); HashMap perClassData = new HashMap(); String programID; // set to "-" for non-persistent (possibly not implemented) long idCounter; volatile long changes, changesWritten; volatile java.util.Timer autoSaver; volatile boolean savingConcepts, dontSave; int autoSaveInterval = -1000; // 1 second + wait logic boolean useGZIP = true, quietSave; ReentrantLock lock = new ReentrantLock(true); ReentrantLock saverLock = new ReentrantLock(true); long lastSaveTook, lastSaveWas; float maxAutoSavePercentage = 10; List conceptIndices; Map, Map> fieldIndices; List saveActions = synchroList(); Concepts() {} Concepts(String programID) { this.programID = programID;} synchronized long internalID() { do { ++idCounter; } while (hasConcept(idCounter)); return idCounter; } void initProgramID() { if (programID == null) programID = getDBProgramID(); } // Now tries to load from bot first, then go to disk. Concepts load() { return load(false); } Concepts safeLoad() { return load(true); } Concepts load(boolean allDynamic) { initProgramID(); if (tryToGrab(allDynamic)) return this; return loadFromDisk(allDynamic); } Concepts loadFromDisk() { return loadFromDisk(false); } Concepts loadFromDisk(boolean allDynamic) { clearConcepts(); //DynamicObject_loading.set(true); // now done in unstructure() //try { long time = now(); Map _concepts = concepts; // empty map 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(); /*} finally { DynamicObject_loading.set(null); }*/ allChanged(); return this; } Concepts loadConcepts() { return load(); } 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; } Concepts load(String grab) { return loadGrab(grab, false); } Concepts safeLoad(String grab) { return loadGrab(grab, true); } 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; } void assignConceptsToUs() { for (Concept c : values(concepts)) { c._concepts = this; callOpt_noArgs(c, "_doneLoading2"); } } String progID() { return programID == null ? getDBProgramID() : programID; } Concept getConcept(String id) { return empty(id) ? null : getConcept(parseLong(id)); } Concept getConcept(long id) { return (Concept) concepts.get((long) id); } Concept getConcept(RC ref) { return ref == null ? null : getConcept(ref.longID()); } boolean hasConcept(long id) { return concepts.containsKey((long) id); } void deleteConcept(long id) { Concept c = getConcept(id); if (c == null) print("Concept " + id + " not found"); else c.delete(); } void calcIdCounter() { long id_ = 0; for (long id : keys(concepts)) id_ = max(id_, id); idCounter = id_+1; saveLocally2(this, programID, "idCounter"); } void saveConceptsIfDirty() { saveConcepts(); } void save() { saveConcepts(); } void saveConcepts() { if (dontSave) return; initProgramID(); saverLock.lock(); savingConcepts = true; long start = now(), time; try { String s = null; //synchronized(main.class) { 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; // only update when structure didn't fail if (!useGZIP) { time = now()-start; if (!quietSave) print("Saving " + toM(l(s)) + "M chars (" /*+ changesWritten + ", "*/ + 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(); } } void _autoSaveConcepts() { if (autoSaveInterval < 0 && maxAutoSavePercentage != 0) { long pivotTime = Math.round(lastSaveWas+lastSaveTook*100.0/maxAutoSavePercentage); if (now() < pivotTime) { //print("Skipping auto-save (last save took " + lastSaveTook + ")"); return; } } try { saveConcepts(); } catch (Throwable e) { print("Concept save failed, will try again: " + e); } } void clearConcepts() { concepts.clear(); allChanged(); } synchronized void allChanged() { ++changes; } // auto-save every second if dirty 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()"; }}); // print("Installed auto-saver (" + autoSaveInterval + " ms, " + progID() + ")"); } } void cleanMeUp() { if (autoSaver != null) { autoSaver.cancel(); autoSaver = null; } while (savingConcepts) sleepInCleanUp(10); saveConceptsIfDirty(); } Map getIDsAndNames() { Map map = new HashMap(); Map cloned = cloneMap(concepts); for (long id : keys(cloned)) map.put(id, cloned.get(id).className); return map; } 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)); } A conceptOfType(Class type) { return firstOfType(allConcepts(), type); } List conceptsOfType(Class type) { return filterByType(allConcepts(), type); } List listConcepts(Class type) { return conceptsOfType(type); } List list(Class type) { return conceptsOfType(type); } List list(String type) { return conceptsOfType(type); } List conceptsOfType(String type) { return filterByDynamicType(allConcepts(), "main$" + type); } boolean hasConceptOfType(Class type) { return hasType(allConcepts(), type); } void persistConcepts() { loadConcepts(); autoSaveConcepts(); } // We love synonyms void conceptPersistence() { persistConcepts(); } Concepts persist() { persistConcepts(); return this; } void persist(int interval) { autoSaveInterval = interval; persist(); } // Runs r if there is no concept of that type 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; } // Ensures that every concept of type c1 is ref'd by a concept of // type c2. // Type of func: voidfunc(concept) 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)); } } } // Type of func: voidfunc(concept) void forEvery(Class type, Object func) { for (Concept c : conceptsOfType(type)) callF(func, c); } int deleteAll(Class type) { List l = (List) conceptsOfType(type); for (Concept c : l) c.delete(); return l(l); } Collection allConcepts() { synchronized(concepts) { return new ArrayList(values(concepts)); } } int countConcepts(Class c, Object... params) { int n = 0; for (A x : list(c)) if (checkConceptFields(x, params)) ++n; return n; } int countConcepts(String c, Object... params) { int n = 0; for (Concept x : list(c)) if (checkConceptFields(x, params)) ++n; return n; } int countConcepts() { return l(concepts); } synchronized void addConceptIndex(IConceptIndex index) { if (conceptIndices == null) conceptIndices = new ArrayList(); conceptIndices.add(index); } 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); } synchronized IFieldIndex getFieldIndex(Class c, String field) { if (fieldIndices == null) return null; Map map = fieldIndices.get(c); return map == null ? null : map.get(field); } // inter-process methods RC xnew(String name, Object... values) { return new RC(cnew(name, values)); } void xset(long id, String field, Object value) { xset(new RC(id), field, value); } void xset(RC c, String field, Object value) { if (value instanceof RC) value = getConcept((RC) value); cset(getConcept(c), field, value); } Object xget(long id, String field) { return xget(new RC(id), field); } Object xget(RC c, String field) { return xgetPost(cget(getConcept(c), field)); } Object xgetPost(Object o) { o = deref(o); if (o instanceof Concept) return new RC((Concept) o); return o; } void xdelete(long id) { xdelete(new RC(id)); } void xdelete(RC c) { getConcept(c).delete(); } void xdelete(List l) { for (RC c : l) xdelete(c); } List xlist() { return map("toPassRef", allConcepts()); } List xlist(String className) { return map("toPassRef", conceptsOfType(className)); } boolean isTransient() { return eq(programID, "-"); } String xfullgrab() { lock.lock(); try { if (changes == changesWritten && !isTransient()) return loadConceptsStructure(programID); return structure(cloneMap(concepts)); } finally { lock.unlock(); } } void xshutdown() { // Killing whole VM if someone wants this DB to shut down cleanKillVM(); } long xchangeCount() { return changes; } int xcount() { return countConcepts(); } 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(); } } // class Concepts static volatile Concepts mainConcepts = new Concepts(); // Where we create new concepts static class Concept extends DynamicObject { transient Concepts _concepts; // Where we belong long id; //O madeBy; //double energy; //bool defunct; long created; // used only internally (cnew) Concept(String className) { super(className); _created(); } Concept() { if (!_loading()) { //className = shortClassName(this); // XXX - necessary? //print("New concept of type " + className); _created(); } } Concept(boolean unlisted) { if (!unlisted) _created(); } List refs; List backRefs; static boolean loading() { return _loading(); } static boolean _loading() { return isTrue(DynamicObject_loading.get()); } void _created() { if (!isTrue(concepts_unlisted.get())) mainConcepts.register(this); } /*void put(S field, O value) { fieldValues.put(field, value); change(); } O get(S field) { ret fieldValues.get(field); }*/ class Ref { A value; Ref() { if (!isTrue(DynamicObject_loading.get())) refs = addDyn(refs, this); } Ref(A value) { this.value = value; refs = addDyn(refs, this); index(); } // get owning concept (source) Concept concept() { return Concept.this; } // get target A get() { return value; } boolean has() { return value != null; } void set(A a) { if (a == value) return; unindex(); value = a; index(); } void set(Ref ref) { set(ref.get()); } void clear() { set((A) null); } void index() { if (value != null) value.backRefs = addDyn(value.backRefs, this); change(); } void unindex() { if (value != null) value.backRefs = removeDyn(value.backRefs, this); } void change() { Concept.this.change(); } } class RefL extends AbstractList { List < Ref < A > > 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); } } void delete() { //name = "[defunct " + name + "]"; //defunct = true; //energy = 0; 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; } BaseXRef export() { return new BaseXRef(_concepts.progID(), id); } // notice system of a change in this object void change() { if (_concepts != null) { _concepts.allChanged(); if (_concepts.conceptIndices != null) for (IConceptIndex index : _concepts.conceptIndices) index.update(this); } } void _change() { change(); } String _programID() { return _concepts == null ? getDBProgramID() : _concepts.progID(); } } // class Concept // remote reference (for inter-process communication or // external databases). Formerly "PassRef". // prepared for string ids if we do them later static class RC { transient Object owner; String id; RC() {} // make serialisation happy RC(long id) { this.id = str(id); } RC(Object owner, long id) { this.id = str(id); this.owner = owner; } RC(Concept c) { this(c.id); } long longID() { return parseLong(id); } public String toString() { return id; } transient RemoteDB db; String getString(String field) { return db.xS(this, field); } Object get(String field) { return db.xget(this, field); } void set(String field, Object value) { db.xset(this, field, value); } } // Reference to a concept in another program static class BaseXRef { String programID; long id; BaseXRef() {} 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; } } // BaseXRef as a concept static class XRef extends Concept { BaseXRef ref; XRef() {} XRef(BaseXRef ref) { this.ref = ref; _doneLoading2(); } // after we have been added to concepts void _doneLoading2() { getIndex().put(ref, this); } HashMap getIndex() { return getXRefIndex(_concepts); } } 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; } // uses mainConcepts static XRef lookupOrCreateXRef(BaseXRef ref) { XRef xref = getXRefIndex(mainConcepts).get(ref); if (xref == null) xref = new XRef(ref); return xref; } // define standard concept functions to use main concepts static List list(Class type) { return mainConcepts.list(type); } static List list(Concepts concepts, Class type) { return concepts.list(type); } static List list(String type) { return mainConcepts.list(type); } static List list(Concepts concepts, String type) { return concepts.list(type); } static int csetAll(Concept c, Object... values) { return cset(c, values); } static void cleanMeUp_concepts() { mainConcepts.cleanMeUp(); } static void loadAndAutoSaveConcepts() { mainConcepts.persist(); } static void loadAndAutoSaveConcepts(int interval) { mainConcepts.persist(interval); } static void loadConceptsFrom(String progID) { mainConcepts.programID = progID; mainConcepts.load(); } static List conceptsOfType(String type) { return mainConcepts.conceptsOfType(type); } static long changeCount() { return mainConcepts.changes; } static List exposedDBMethods = ll("xlist", "xnew", "xset", "xdelete", "xget", "xclass", "xfullgrab", "xshutdown", "xchangeCount", "xcount"); static RC toPassRef(Concept c) { return new RC(c); } // so we can instantiate the program to run as a bare DB bot static Lock dbLock() { return mainConcepts.lock; } static Either evalWithTimeoutReleasingDBLock(int timeoutMS, final Object r) { dbLock().unlock(); try { return assertNotNull(evalWithTimeout(timeoutMS, r)); } finally { lock(dbLock()); } } static Either evalWithTimeoutReleasingDBLock(double timeoutSeconds, final Object r) { return evalWithTimeoutReleasingDBLock(toMS_int(timeoutSeconds), r); } static boolean nempty(Collection c) { return !isEmpty(c); } static boolean nempty(CharSequence s) { return !isEmpty(s); } static boolean nempty(Object[] o) { return !isEmpty(o); } static boolean nempty(Map m) { return !isEmpty(m); } static boolean nempty(Iterator i) { return i != null && i.hasNext(); } static String dropSuffixTrim(String suffix, String s) { return dropSuffixAndTrim(suffix, s); } 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; } static boolean possibleGlobalID(String s) { return l(s) == 16 && allLowerCaseCharacters(s); } static Object smallestList_first(Object l) { return smallestList_get(l, 0); } static boolean eqic(String a, String b) { if ((a == null) != (b == null)) return false; if (a == null) return true; return a.equalsIgnoreCase(b); } static Pair pair(A a, B b) { return new Pair(a, b); } // automagically encode a whole map (keys+values) static Map htmlencode(Map o) { HashMap bla = new HashMap(); for (Object key : keys(o)) { Object value = o.get(key); bla.put(htmlencode(key), htmlencode(value)); } return bla; } static String htmlencode(Object o) { return htmlencode(string(o)); } 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 >= 0x100) out.append("&#x").append(charToHex(c)).append(';'); else*/ if (c > 127 || c == '"' || c == '<' || c == '>' || c == '&') { out.append("&#"); out.append((int) c); out.append(';'); } else out.append(c); } return out.toString(); } static List scoredSearch(String query, Collection data) { Map scores = new HashMap(); for (String s : data) { int score = scoredSearch_score(s, query); if (score != 0) scores.put(s, score); } return keysSortedByValuesDesc(scores); } static int parseInt(String s) { return empty(s) ? 0 : Integer.parseInt(s); } static int parseInt(char c) { return Integer.parseInt(str(c)); } static void typicalDownload() { if (stefansPC()) return; { JWindow _loading_window = showLoadingAnimation("Doooownload!"); try { downloadTypicalWebs(); } finally { disposeWindow(_loading_window); }} } static volatile StringBuffer local_log = new StringBuffer(); // not redirected static volatile StringBuffer print_log = local_log; // might be redirected, e.g. to main bot // in bytes - will cut to half that static volatile int print_log_max = 1024*1024; static volatile int local_log_max = 100*1024; //static int print_maxLineLength = 0; // 0 = unset static boolean print_silent; // total mute if set static Object print_byThread_lock = new Object(); static volatile ThreadLocal> print_byThread; // special handling by thread static void print() { print(""); } // slightly overblown signature to return original object... static A print(A o) { ping(); if (print_silent) return o; String s = String.valueOf(o) + "\n"; print_noNewLine(s); return o; } static void print_noNewLine(String s) { if (print_byThread != null) { F1 f = print_byThread.get(); if (f != null) if (isFalse(f.get(s))) return; } print_raw(s); } static void print_raw(String s) { s = fixNewLines(s); // TODO if (print_maxLineLength != 0) 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); } static void print(long l) { print(String.valueOf(l)); } static void print(char c) { print(String.valueOf(c)); } 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); } } } static List prependAll(final String s, Collection l) { return map(new Object() { Object get(String x) { try { return s + x ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "s + x"; }}, l); } 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); } static void setOpt(Object o, String field, Object value) { try { if (o == null) 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; } // It's probably a subclass of Map. Use raw method setOpt_raw(o, field, value); return; } Field f = map.get(field); if (f != null) smartSet(f, o, value); // possible improvement: skip setAccessible } catch (Exception __e) { throw rethrow(__e); } } 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); } } 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) return f; _c = _c.getSuperclass(); } while (_c != null); return null; } static String ai_dropDroppableStuff(String s) { String s2; while (neq(s, s2 = ai_dropDroppablePrefix(ai_dropDroppableTail(s)))) s = s2; return s; } 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)); } static List ai_search_dollarX_verified(T3 t) { return ai_search_dollarX_verified(t.a, t.b, t.c); } static Throwable printStackTrace2(Throwable e) { // we go to system.out now - system.err is nonsense print(getStackTrace2(e)); return e; } static void printStackTrace2() { printStackTrace2(new Throwable()); } static void printStackTrace2(String msg) { printStackTrace2(new Throwable(msg)); } /*static void printStackTrace2(S indent, Throwable e) { if (endsWithLetter(indent)) indent += " "; printIndent(indent, getStackTrace2(e)); }*/ static boolean eqicOneOf(String s, String... l) { for (String x : l) if (eqic(s, x)) return true; return false; } static boolean ai_hasTriple(String a, String b, String c) { return ai_cache_hasTriple(a, b, c); } static boolean ai_hasTriple(T3 t) { return ai_cache_hasTriple(t); } static ThreadLocal saveTiming_last = new ThreadLocal(); static void saveTiming(long ms) { print(ms + " ms"); saveTiming_noPrint(ms); } static void saveTiming_noPrint(long ms) { saveTiming_last.set(ms); } static Web oneNodeWeb(String text) { Web web = simpleWeb(); web.newNode(text); return web; } static void clearTransientWebs() { List l = cloneAndClear(transientWebs_list); for (Web web : l) onTransientWebRemoved(web); } // TODO: test static Object smallestList_set(Object l, int i, Object o) { if (i < 0 || i >= smallestList_l(l)) throw fail("Index out of bounds"); if (l instanceof Pair) { if (i == 0) ((Pair) l).a = o; else ((Pair) l).b = o; return l; } if (l instanceof T3) { if (i == 0) ((T3) l).a = o; else if (i == 1) ((T3) l).b = o; else ((T3) l).c = o; return l; } if (l instanceof Object[]) { ((Object[]) l)[i] = o; return l; } return o; } static T3 ai_tripelize(String s) { String x = ""; if (endsWith(s, "?")) x = " (?)"; // statement with question mark, e.g. "Al Gore invented the Internet?" T3 t = ai_tripelize_impl(dropCertainPunctuationAtEnd(s, ":;,?!.")); ai_fixTriple(t); if (t != null && !endsWith(t.b, "?")) t.b += x; return t; } static T3 ai_tripelize_impl(String s) { List original = ai_tok_combinePhrases(tok_combineCurlyBrackets_keep(nlTok(s))); List tok = original; String a = get(tok, 1); if (a == null) return null; 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); // a = "do", middle, v = "love", rest = "me" 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 triple(joinWithSpace2(before, middle), v, rest); } } if (isVerb(a)) { tok = subList(tok, 2); int i = ai_splitTwoNounPhrases(tok); //int i = ai_endOfNounPhrase(tok); return triple( trimJoinSubList(tok, 0, i), a + "?", trimJoinSubList(tok, i)); } 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)); } static String urlencode(String x) { try { return URLEncoder.encode(unnull(x), "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } static String joinWithSpace(Collection c) { return join(" ", c); } static String joinWithSpace(String... c) { return join(" ", c); } 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; } static String urldecode(String x) { try { return URLDecoder.decode(unnull(x), "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } static void clearAll(Object... l) { for (Object o : l) callOpt(o, "clear"); } static String renderGMTDate(long time) { return formatGMTWithOptionalDate_24(time); } static void printShortException(Throwable e) { print(exceptionToStringShort(e)); } static NanoHTTPD.Response serveHTML(String text) { return NanoHTTPD.newFixedLengthResponse(String.valueOf(text)); } static NanoHTTPD.Response serveHTML(StringBuilder buf) { return NanoHTTPD.newFixedLengthResponse(buf.toString()); } static NanoHTTPD.Response serveHTML(StringBuffer buf) { return NanoHTTPD.newFixedLengthResponse(buf.toString()); } static String yn(boolean b) { return yesno(b); } static List indexedNodes(String s) { return ai_withoutInvalidNodes(indexedNodes_withInvalids(s)); } static VirtualNodeIndex hugeEnglishDictionary_virtualNodeIndex() { return new VirtualNodeIndex() { SortedCondensedStrings2 dict = hugeEnglishDictionary(); int numWebs() { return l(dict); } List get(String 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"))); } }; } static void lock(Lock lock) { try { ping(); lock.lockInterruptibly(); ping(); } catch (Exception __e) { throw rethrow(__e); } } static void lock(Lock lock, String msg) { print("Locking: " + msg); lock(lock); } static void lock(Lock lock, String msg, long timeout) { print("Locking: " + msg); lockOrFail(lock, timeout); } static ReentrantLock lock() { return fairLock(); } static String nWebs(int i) { return n_fancy(i, "web"); } static String nWebs(Collection c) { return n_fancy(c, "web"); } static String random_gac36k() { return random(keys(gac36k())); } 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)); } static String join(Iterable strings) { return join("", strings); } static String join(Iterable strings, String glue) { return join(glue, strings); } public static String join(String[] strings) { return join("", strings); } static String join(String glue, Pair p) { return p == null ? "" : str(p.a) + glue + str(p.b); } static long fileLength(String path) { return getFileSize(path); } static long fileLength(File f) { return getFileSize(f); } static String dropPrefix(String prefix, String s) { return s == null ? null : s.startsWith(prefix) ? s.substring(l(prefix)) : s; } static Collection allWebsFromCachedNodeIndex() { return values(allWebsByID()); } static String ai_html_linkedWeb(String globalID) { return ahref(ai_html_webLink(globalID), htmlencode(globalID)); } static String ai_html_linkedWeb(Web web) { return web == null ? "-" : ai_html_linkedWeb(web.globalID); } static String printLog() { return str(local_log); } static void addTransientWeb(Web web) { onTransientWebAdded(web); transientWebs_list.add(web); } static String computerVMs_text() { return numberedLines(map("renderMap_colonComma",values(computerVMs_shorter()))); } static String html_addTargetBlank(String html) { List tok = htmlTok(html); for (int i = 1; i < l(tok); i += 2) { 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))); } } } return join(tok); } static String firstQuoted(Collection l) { if (l != null) for (String s : l) if (isQuoted(s)) return s; return null; } 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); } static File directNohupJavax(String javaxargs) { return directNohupJavax(javaxargs, javaxDefaultVMArgs()); } static File directNohupJavax(String javaxargs, String vmArgs) { // TODO: handle "-case 123 1009326" 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(); 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); } static void addVirtualNodeIndex(VirtualNodeIndex i) { setAdd(virtualNodeIndices_list, i); } 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; } static Collection allIndexedWebs() { return values(allWebsByID()); } 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"))); } static void save(String varName) { saveLocally(varName); } static void save(String progID, String varName) { saveLocally(progID, varName); } // returns true if timeout static boolean ai_speculateWithTimeout(final String word, double timeoutSeconds) { return eitherIsB(evalWithTimeout(toMS(timeoutSeconds), new Runnable() { public void run() { try { ai_speculate(word) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ai_speculate(word)"; }})); } static int websMadeInThread() { return toInt(postSoftwareMadeWeb_websMadeInThread.get()); } static int websMadeInThread(Thread thread) { return toInt(postSoftwareMadeWeb_websMadeInThread.get(thread)); } static Cache> allWebs_cached_cache = new Cache("allWebs_cached_load"); static List allWebs_cached() { return allWebs_cached_cache.get(); } static List allWebs_cached_load() { return allWebs(); } static A get(List l, int idx) { return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null; } // seems to conflict with other signatures /*static B get(Map map, A key) { ret map != null ? map.get(key) : null; }*/ static A get(A[] l, int idx) { return idx >= 0 && idx < l(l) ? l[idx] : null; } // default to false static boolean get(boolean[] l, int idx) { return idx >= 0 && idx < l(l) ? l[idx] : false; } // get purpose 2: access a field by reflection or a map 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()); } 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); } } 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); } } 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()); } 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()); } static Web indexedWebWithGlobalID(String globalID) { return findByField(allIndexedWebs(), "globalID", globalID); } 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); } static List map(Iterable l, Object f) { return map(f, l); } 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; } static List map(Object f, Object[] l) { return map(f, asList(l)); } static List map(Object[] l, Object f) { return map(f, l); } static List map(Object f, Map map) { return map(map, f); } 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; } static String h1_title(String s) { return htitle_h1(s); } static Pair evalWithTimeout_numberOfCalculations() { int all = l(evalWithTimeout_allThreads); int inTime = l(evalWithTimeout_inTime); return pair(all, max(0, all-inTime)); } static void restart() { Object j = getJavaX(); call(j, "cleanRestart", get(j, "fullArgs")); } static int max(int a, int b) { return Math.max(a, b); } static int max(int a, int b, int c) { return max(max(a, b), c); } static long max(int a, long b) { return Math.max((long) a, b); } static long max(long a, long b) { return Math.max(a, b); } static double max(int a, double b) { return Math.max((double) a, b); } static float max(float a, float b) { return Math.max(a, b); } static double max(double a, double b) { return Math.max(a, b); } static int max(Collection c) { int x = Integer.MIN_VALUE; for (int i : c) x = max(x, i); return x; } 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; } 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; } static byte max(byte[] c) { byte x = -128; for (byte d : c) if (d > x) x = d; return x; } static short max(short[] c) { short x = -0x8000; for (short d : c) if (d > x) x = d; return x; } 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; // scan for whitespace 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); // scan for non-whitespace if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { if (s.charAt(j) == opener || s.charAt(j) == '\n') { // end at \n to not propagate unclosed string literal errors ++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)); // for stuff like "don't" else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L } 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(quickSubstring(s, i, j)); i = j; } return tok; } static String f2s(File f) { return f == null ? null : f.getAbsolutePath(); } static String n_fancy(long l, String name) { return formatWithThousandsSeparator(l) + " " + trim(l == 1 ? singular(name) : getPlural(name)); } static String n_fancy(Collection l, String name) { return n_fancy(l(l), name); } static String n_fancy(Map m, String name) { return n_fancy(l(m), name); } static String n_fancy(Object[] a, String name) { return n_fancy(l(a), name); } static String ai_speculate(final String word) { if (empty(word)) return word; ai_withMaker("ai_speculate", new Runnable() { public void run() { try { ai_blob(word, false); try { ping(); if (isAdjective(word)) ai_postTriple(word, "is", "an adjective"); } catch (Throwable __e) { printStackTrace2(__e); } try { ping(); ai_spec_addQuestionsToChatLine(word); } catch (Throwable __e) { printStackTrace2(__e); } try { ping(); ai_spec_addQuestionsToAdjective(word); } catch (Throwable __e) { printStackTrace2(__e); } try { ping(); ai_spec_possiblyIs(word); } catch (Throwable __e) { printStackTrace2(__e); } try { ping(); ai_spec_isAQuestion(word); } catch (Throwable __e) { printStackTrace2(__e); } try { ping(); ai_spec_substWebs(word); } catch (Throwable __e) { printStackTrace2(__e); } try { ping(); ai_spec_apostrophS(word); } catch (Throwable __e) { printStackTrace2(__e); } try { ping(); ai_spec_allWords(word); } catch (Throwable __e) { printStackTrace2(__e); } try { ping(); ai_spec_numbered(word); } catch (Throwable __e) { printStackTrace2(__e); } try { ping(); ai_spec_howMany(word); } catch (Throwable __e) { printStackTrace2(__e); } try { ping(); ai_spec_doYouThink(word); } catch (Throwable __e) { printStackTrace2(__e); } try { ping(); ai_spec_unquote(word); } catch (Throwable __e) { printStackTrace2(__e); } try { ping(); ai_spec_checkFact(word); } catch (Throwable __e) { printStackTrace2(__e); } // This involves a full-index search; do it later //pcall ping { ai_spec_similarWord(word); } //pcall ping { ai_spec_acronym(word); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ai_blob(word, false);\r\n pcall ping { if (isAdjective(word)) ai_postTriple(..."; }}); return word; } static String autoRestart_localMD5; static String autoRestart_localMD5() { if (autoRestart_localMD5 == null) autoRestart_localMD5 = md5(loadCachedTranspilation(programID())); return autoRestart_localMD5; } static Web webFromTriples(String... l) { Web web = simpleWeb(); for (int i = 0; i < l(l); i += 3) { String a = unnull(get(l, i)), b = unnull(get(l, i+1)), c = unnull(get(l, i+2)); web_addRelation(web, a, c, b); } return web; } static Web webFromTriples(List l) { return webFromTriples(toStringArray(l)); } static Map diagramServer_typicalSet() { return litorderedmap(toObjectArray(allToString((List) safeUnstructure(loadPage("http://ai1.lol/1010474/raw/typical-set"))))); } // default mode = "quick" static Pair transpileOnServer(String snippetID) { return transpileOnServer(snippetID, "quick"); } static Pair transpileOnServer(long snippetID) { return transpileOnServer(fsI(snippetID)); } static Pair transpileOnServer(long snippetID, String mode) { return transpileOnServer(fsI(snippetID), mode); } // mode = "quick", "medium", "fresh" // result = pair(ok, answer) static Pair transpileOnServer(String snippetID, String mode) { try { print("Transpiling " + formatSnippetID(snippetID)); String user = loadTextFile(new File(userHome(), ".tinybrain/username").getPath(), null); String pass = loadTextFile(new File(userHome(), ".tinybrain/userpass").getPath(), null); 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 + "&_user=" + urlencode(user) + "&_pass=" + urlencode(pass); 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); result = loadPage(url); boolean ok = result.contains("SUCCESS"); return pair(ok, result); } catch (Exception __e) { throw rethrow(__e); } } static Object ai_withMaker(String maker, Object f) { String prev = ai_setMaker(maker); try { return callF(f); } finally { ai_setMaker(prev); } } static Object first(Object list) { return empty((List) list) ? null : ((List) list).get(0); } static A first(List list) { return empty(list) ? null : list.get(0); } static A first(A[] bla) { return bla == null || bla.length == 0 ? null : bla[0]; } static A first(Iterable i) { if (i == null) return null; Iterator it = i.iterator(); return it.hasNext() ? it.next() : null; } static Character first(String s) { return empty(s) ? null : s.charAt(0); } static A first(Pair p) { return p == null ? null : p.a; } static String h2_title(String s) { return htitle_h2(s); } static T3 ai_parseArrowTriple(String s) { return listToTriple(trimAll(splitAtSimpleArrow(s))); } static List sortByFieldDesc(Collection c, final String field) { return sortedByFieldDesc(c, field); } static List sortByFieldDesc(String field, Collection c) { return sortedByFieldDesc(field, c); } static List ai_dropLeadingAdditions_list = ll("also", "tell me"); 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); } static Web webFromTriple(String a, String b, String c) { return webFromTriple(triple(a, b, c)); } static Web webFromTriple(T3 t) { return webFromTriple(t, 0.2, 0.2, 0.8, 0.7); } static Web webFromTriple(T3 t, double x1, double y1, double x2, double y2) { Web web = webFromTriples(t.a, t.b, t.c); web_setPosition(first(web.nodes), x1, y1); web_setPosition(second(web.nodes), x2, y2); return web; } static RuntimeException asRuntimeException(Throwable t) { return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t); } static long nowUnlessLoading() { return dynamicObjectIsLoading() ? 0 : now(); } static Class mc() { return main.class; } static List ai_subtypesOf(String type) { return web_uniqueTexts(webs_search_dollarX( webFromTriples("$X", "is a type of", type), indexedWebs(type))); } 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; } static boolean learnedFlexMatch_multi(String s, String... patterns) { return learnedFlexMatch_multi(s, null, patterns); } static boolean learnedFlexMatch_multi(String s, Matches m, String... patterns) { for (String pattern : patterns) if (learnedFlexMatch(pattern, s, m)) return true; return false; } static boolean neq(Object a, Object b) { return !eq(a, b); } 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)"; }}); } static List takeFirst(List l, int n) { return l(l) <= n ? l : newSubList(l, 0, n); } static List takeFirst(int n, List l) { return takeFirst(l, n); } static String takeFirst(int n, String s) { return substring(s, 0, n); } static List takeFirst(int n, Iterable i) { List l = new ArrayList(); Iterator it = i.iterator(); for (int _repeat_422 = 0; _repeat_422 < n; _repeat_422++) { if (it.hasNext()) l.add(it.next()); else break; } return l; } static String getSingularFromWordHippo(String word) { word = tolower(word); Map hippoSingulars = pairsToMap((List>) scanStructureLog("#1011041", "singulars")); { String _a_426 = hippoSingulars.get(word); if (!empty(_a_426)) return _a_426; } 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; } static boolean ai_cache_hasTriple_debug; static boolean ai_cache_hasTriple(String a, String b, String c) { List webs = ai_fewestIndexedWebs(a, b, c); boolean x = webs_search_noVar_bool(webFromTriples(a, b, c), webs); if (ai_cache_hasTriple_debug) print("hasTriple: " + sfuLL(a, b, c, x) + ", searched " + nWebs(webs)); return x; } static boolean ai_cache_hasTriple(T3 t) { return ai_cache_hasTriple(t.a, t.b, t.c); } static String ai_subst_web(String webID) { int n = websMadeInThread(); Web web = assertNotNull("Web not found: " + webID, allWebsByID().getFirst(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"; } 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}"); } static List concatLists(Collection... lists) { List l = new ArrayList(); for (Collection list : lists) if (list != null) l.addAll(list); return l; } static List concatLists(Collection> lists) { List l = new ArrayList(); for (List list : lists) if (list != null) l.addAll(list); return l; } static A uniq(Class c, Object... params) { return uniqueConcept(c, params); } static A uniq(Concepts cc, Class c, Object... params) { return uniqueConcept(cc, c, params); } static String randomIndexedTerm() { return random(ai_expandShortenedKeys(random(indexedTerms()))); } static boolean gac3k_verbose; static Cache> gac36k_cache = new Cache("gac36k_load"); static TreeMap gac36k() { return gac36k_cache.get(); } 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) { printStackTrace2(__e); } print("GAC-36K: Have " + n(map, "question")); return map; } static List smallestList_toList(Object l) { return smallestList_asList(l); } static List collect(Collection c, String field) { return collectField(c, field); } static List collect(String field, Collection c) { return collectField(c, field); } static List collect(Class c, String field) { return collect(list(c), field); } static boolean ai_nodeNameAllowedToPost(String s) { return !isDollarVar(s); } static void sleepSeconds(double s) { if (s > 0) sleep(round(s*1000)); } static List ai_unverifiedWebs() { return filterByField(allIndexedWebs(), "unverified", true); } static Set keys(Map map) { return map == null ? new HashSet() : map.keySet(); } static Set keys(Object map) { return keys((Map) map); } static Set keys(MultiMap mm) { return mm.keySet(); } static Android3 bot(String greeting) { return makeAndroid3(greeting); } static Android3 bot(Android3 a) { return makeBot(a); } static Android3 bot(String greeting, Object responder) { return makeBot(greeting, responder); } static Android3 bot() { return makeBot(); } static boolean ai_tripleAllowedToPost(T3 t) { return all("ai_nodeNameAllowedToPost", tripleToList(t)); } static double toSeconds(long ms) { return ms/1000.0; } static String toSeconds(long ms, int digits) { return formatDouble(toSeconds(ms), digits); } static B mapGet(Map map, A a) { return map == null || a == null ? null : map.get(a); } static long sysNow() { return System.nanoTime()/1000000; } static boolean cleanUp_interruptThreads; // experimental 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; } //if (!(c instanceof Class)) ret; try { // revoke license callOpt(c, "licensed_off"); // unpause setOpt(c, "ping_pauseAll", false); // call custom cleanMeUp() and cleanMeUp_*() functions if (!isFalse(pcallOpt(c, "cleanMeUp"))) for (String name : methodsStartingWith(c, "cleanMeUp_")) pcallOpt(c, name); // Java spec says finalize should only be called by GC, // but we care to differ. pcallOpt(c, "finalize"); // remove all virtual bots (hope this works) List androids = (List) getOpt(c, "record_list"); for (Object android : unnull(androids)) pcallOpt(android, "dispose"); // heck we'll dispose anything // sub-cleanup List classes = (List) ( getOpt(c, "hotwire_classes")); if (classes != null) for (WeakReference cc : classes) try { cleanUp(cc.get()); } catch (Throwable __e) { printStackTrace2(__e); } // interrupt all threads (experimental, they might be doing cleanup?) if (cleanUp_interruptThreads) interruptThreads(registeredThreads(c)); } catch (Throwable __e) { printStackTrace2(__e); } } static void cleanUp(Collection l) { if (l == null) return; for (Object c : l) cleanUp(c); l.clear(); } static List web_labels(WebNode n) { return n == null ? null : n.labels(); } static boolean learnedFlexMatch(String pattern, String s) { return learnedFlexMatch(pattern, s, null); } static boolean learnedFlexMatch(String pattern, String s, Matches m) { if (!flexMatchIC(dropPunctuation(pattern), dropPunctuation(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; } static NanoHTTPD.Response serveJPEG(BufferedImage img) { return serveByteArray(toJPG(img), "image/jpeg"); } static RuntimeException rethrow(Throwable e) { throw asRuntimeException(e); } static double autoRestart_interval = 10; static boolean autoRestart_on, autoRestart_debug, autoRestart_simulate; static void autoRestart(double interval) { autoRestart_interval = interval; autoRestart(); } static void autoRestart() { if (autoRestart_on) return; autoRestart_on = true; autoRestart_schedule(); preloadProgramTitle(); } static void autoRestart_schedule() { doLater_daemon(toMS(autoRestart_interval), "autoRestart_check"); } static void autoRestart_check() { try { String newMD5 = loadPageSilently("http://ai1.lol/1010693/raw?id=" + psI(programID())); if (!isMD5(newMD5)) { if (autoRestart_debug) print("autoRestart: no server transpilation"); return; } if (autoRestart_localMD5 == null) autoRestart_localMD5 = md5(loadCachedTranspilation(programID())); String localMD5 = autoRestart_localMD5(); if (neq(localMD5, newMD5)) { if (autoRestart_simulate) print("Would upgrade now. " + localMD5 + " -> " + newMD5); else { infoBox("Upgrading " + programTitle()); restartWithDelay(1000); sleep(); } } } finally { if (autoRestart_debug) print("autoRestart: Done"); autoRestart_schedule(); } } 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)); } static int toInt(long l) { if (l != (int) l) throw fail("Too large for int: " + l); return (int) l; } static Web ai_postTriple(T3 triple) { if (triple_anyPartNull(triple)) return null; triple = trimTriple(triple); if (!ai_tripleAllowedToPost(triple)) throw fail("No"); if (ai_cache_hasTriple(triple)) return null; Web web = webFromTriple(triple); postSoftwareMadeWeb(web); return web; } static Web ai_postTriple(String a, String b, String c) { return ai_postTriple(t3(a, b, c)); } static Pair ai_countBoth_dollarX(T3 t) { return pair(l(ai_search_dollarX_verified(t)), l(ai_search_dollarX(t))); } static boolean match(String pat, String s) { return match3(pat, s); } static boolean match(String pat, String s, Matches matches) { return match3(pat, s, matches); } static List indexedWebs(String s) { return ai_withoutInvalidWebs(nodesToWebs(indexedNodes_withInvalids(s))); } 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(); } static String ai_subst_web_charBasedReplace(String webID, final String input) { int n = websMadeInThread(); Web web = assertNotNull("Web not found: " + webID, allWebsByID().getFirst(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 Object() { 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"; } static String unicode_rightPointingTriangle() { return "\u25B8"; } static NanoHTTPD.Response serve404() { return serve404("Not Found"); } static NanoHTTPD.Response serve404(String msg) { return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Status.NOT_FOUND, NanoHTTPD.MIME_PLAINTEXT, "Code 404 - " + msg); } 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)); } static boolean startsWith(String a, String b) { return a != null && a.startsWith(b); } static boolean startsWith(String a, char c) { return nempty(a) && a.charAt(0) == c; } 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; } 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; } 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); } } static List web_search_dollarX(Web searchWeb, Web web) { return webs_search_dollarX(searchWeb, ll(web)); } static WeakHashMap> callF_cache = new WeakHashMap(); static A callF(F0 f) { return f == null ? null : f.get(); } 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 methods.get(0).invoke(f, args); for (int i = 0; i < n; i++) { Method m = methods.get(i); if (call_checkArgs(m, args, false)) return m.invoke(f, args); } throw fail("No matching get method in " + getClassName(c)); } catch (Exception __e) { throw rethrow(__e); } } // used internally 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; } static void downloadTypicalWebs() { Class c = hotwire("#1010484"); try { call(c, "init"); call(c, "typicalDownload"); clearCacheVar("downloadedWebs"); } finally { cleanUp(c); // important - release DB } } static List htmlTok(String s) { return htmlcoarsetok(s); } 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; } else l.add(t); } return l; } static ArrayList asList(A[] a) { return a == null ? new ArrayList() : new ArrayList(Arrays.asList(a)); } static ArrayList asList(int[] a) { ArrayList l = new ArrayList(); for (int i : a) l.add(i); return l; } 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; } static ArrayList asList(Enumeration e) { ArrayList l = new ArrayList(); if (e != null) while (e.hasMoreElements()) l.add(e.nextElement()); return l; } 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; } static List dropLast(List l) { return subList(l, 0, l(l)-1); } static List dropLast(int n, List l) { return subList(l, 0, l(l)-n); } static List dropLast(Iterable l) { return dropLast(asList(l)); } static String dropLast(String s) { return substring(s, 0, l(s)-1); } static String dropLast(String s, int n) { return substring(s, 0, l(s)-n); } static String dropLast(int n, String s) { return dropLast(s, n); } static String javaxDefaultVMArgs() { return trim(loadProgramTextFile("#1005850", "default-vm-args", "")); } static boolean pairContains(Pair p, Object o) { return p != null && (p.a == o || p.b == o); } static WebNode web_operand(WebNode node, String... rels) { return web_findForwardRelated_multiRel(node, rels); } 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); } static HashSet litset(A... items) { return lithashset(items); } static A uniqueConcept(Class c, Object... params) { return uniqueConcept(mainConcepts, c, params); } static A uniqueConcept(Concepts cc, Class c, Object... params) { params = expandParams(c, params); A x = findConceptWhere(cc, c, params); if (x == null) { x = nuObject(c); csetAll(x, params); cc.register(x); } return x; } static List getPlural_specials = ll("sheep", "fish"); 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"; } static String smartBotURL() { return "http://fastbob.botcompany.de:4678"; } static String htitle_h2(String s) { return htitle(dropTags(s)) + h2(s); } static String htitle_h1(String s) { return htitle(dropTags(s)) + h1(s); } static String yesno(boolean b) { return b ? "Yes. " : "No. "; } // does not store null values static MultiMap multiMapIndexByField(Collection c, String field) { MultiMap map = new MultiMap(); for (Object a : c) { Object val = getOpt(a, field); if (val != null) map.put(val, a); } return map; } static boolean contains(Collection c, Object o) { return c != null && c.contains(o); } static boolean contains(Object[] x, Object o) { if (x != null) for (Object a : x) if (eq(a, o)) return true; return false; } static boolean contains(String s, char c) { return s != null && s.indexOf(c) >= 0; } static boolean contains(String s, String b) { return s != null && s.indexOf(b) >= 0; } static String getType(Object o) { return getClassName(o); } static boolean endsWith(String a, String b) { return a != null && a.endsWith(b); } static boolean endsWith(String a, char c) { return nempty(a) && lastChar(a) == c; } 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); } static String quickSubstring(String s, int i, int j) { if (i == j) return ""; return s.substring(i, j); } // replacement for class JavaTok // maybe incomplete, might want to add floating point numbers // todo also: extended multi-line strings static int javaTok_n, javaTok_elements; static boolean javaTok_opt; static List javaTok(String s) { return javaTok(s, null); } static List javaTok(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; // scan for whitespace 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 && javaTok_isCopyable(existing.get(n), s, i, j)) tok.add(existing.get(n)); else 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); // scan for non-whitespace // Special JavaX syntax: 'identifier 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 /*|| s.charAt(j) == '\n'*/) { // allow multi-line strings ++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)); // for stuff like "don't" else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L } 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 && javaTok_isCopyable(existing.get(n), s, i, j)) tok.add(existing.get(n)); else tok.add(quickSubstring(s, i, j)); ++n; i = j; } if ((tok.size() % 2) == 0) tok.add(""); javaTok_elements += tok.size(); return tok; } static List javaTok(List tok) { return javaTok(join(tok), tok); } static boolean javaTok_isCopyable(String t, String s, int i, int j) { return t.length() == j-i && s.regionMatches(i, t, 0, j-i); // << could be left out, but that's brave } static int ai_splitTwoNounPhrases(List tok1) { return ai_splitTwoNounPhrasesC(codeTokens(tok1))*2; } // tok is code tokens only 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 (eqic(get(tok, 2), "a")) return 2; int i = 0; if (a1) ++i; ai_splitTwoNounPhrases_assumeNoun(get(tok, i)); return i+1; } static void ai_splitTwoNounPhrases_assumeNoun(String s) { /*if (!isNoun(s)) print("Assuming noun: " + s);*/ } static int indexOfFirstVerb(List tok, int i) { return indexOfAny(tok, i, knownVerbs()); } static int indexOfFirstVerb(List tok) { return indexOfFirstVerb(tok, 0); } static Object[] paramsPlus(Object[] a1, Object... a2) { if (l(a2) == 2 && a2[0] == null) return a1; return concatArrays(assertEvenLength(a1), assertEvenLength(a2)); } static volatile boolean sleep_noSleep; static void sleep(long ms) { ping(); if (ms < 0) return; // allow spin locks if (isAWTThread() && ms > 100) throw fail("Should not sleep on AWT thread"); try { Thread.sleep(ms); } catch (Exception e) { throw new RuntimeException(e); } } static void sleep() { try { if (sleep_noSleep) throw fail("nosleep"); print("Sleeping."); sleepQuietly(); } catch (Exception __e) { throw rethrow(__e); } } static boolean isDollarVar(String s) { return startsWith(s, "$") && isIdentifier(substring(s, 1)); } static int scoredSearch_score(String s, String query) { int score = 0; if (swic(s, query)) score += l(s) == l(query) ? 3 : 2; else if (containsIgnoreCase(s, query)) ++score; return score; } static int ai_tok_combinePhrases_maxPhraseLength = 3; // 3 words, see #1011207 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; } static List virtualNodeIndices_list = synchroList(); static List virtualNodeIndices() { return virtualNodeIndices_list; } static List ai_expandShortenedKeys(String sKey) { // get full text(s) if (endsWith(sKey, "...")) return web_texts(cachedNodeIndex().get(sKey)); else return ll(sKey); } static void 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); } static Class __javax; static Class getJavaX() { return __javax; } static String renderConcept(Concept.Ref ref) { return renderConcept(ref.get()); } 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); } 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); // for ConceptInList et al return struct(val, data); } } 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); } static Throwable getInnerException(Throwable e) { while (e.getCause() != null) e = e.getCause(); return e; } 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 > 127 || c == '<' || c == '>' || c == '&') { out.append("&#"); out.append((int) c); out.append(';'); } else out.append(c); } return out.toString(); } static boolean startsWithIgnoreCase(String a, String b) { return a != null && a.regionMatches(true, 0, b, 0, b.length()); } static long getLastTiming() { ThreadLocal tl = (ThreadLocal) ( getOpt(getMainClass(), "saveTiming_last")); if (tl == null) return -1; Long l = tl.get(); return l == null ? -1 : l; } 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); } 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)); } static boolean match3(String pat, String s) { return match3(pat, s, null); } static boolean match3(String pat, String s, Matches matches) { if (pat == null || s == null) return false; return match3(pat, parse3_cached(s), matches); } static boolean match3(String pat, List toks, Matches matches) { List tokpat = parse3(pat); return match3(tokpat,toks,matches); } static boolean match3(List tokpat, List toks, Matches matches) { String[] m = match2(tokpat, toks); //print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m)); if (m == null) return false; if (matches != null) matches.m = m; return true; } // An "Android" is a program that accepts text questions (on console or TCP) and outputs one response text per question static boolean makeAndroid3_disable; // disable all android making static class Android3 { String greeting; boolean publicOverride; // optionally set this in client int startPort = 5000; // optionally set this in client Responder responder; boolean console = true; boolean quiet; // no messages on console boolean daemon = false; boolean incomingSilent = false; int incomingPrintLimit = 200; boolean useMultiPort = true; boolean recordHistory; boolean verbose; int answerPrintLimit = 500; boolean newLineAboveAnswer, newLineBelowAnswer; // set by system int port; long vport; DialogHandler handler; ServerSocket server; Android3(String greeting) { this.greeting = greeting;} Android3() {} 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) { printStackTrace2(__e); } } public String toString() { return "Bot: " + greeting + " [vport " + vport + "]"; } } static abstract class Responder { abstract String answer(String s, List history); } static Android3 makeAndroid3(final String greeting) { return makeAndroid3(new Android3(greeting)); } static Android3 makeAndroid3(final String greeting, Responder responder) { Android3 android = new Android3(greeting); android.responder = responder; return makeAndroid3(android); } static Android3 makeAndroid3(final Android3 a) { if (makeAndroid3_disable) return a; if (a.responder == null) a.responder = new Responder() { String answer(String s, List history) { return callStaticAnswerMethod(s, history); } }; if (!a.quiet) print("[bot] " + a.greeting); if (a.console && makeAndroid3_consoleInUse() && !readLine_noReadLine) 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; } static void makeAndroid3_handleConsole(final Android3 a) { // Console handling stuff 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; /*if (eq(line, "bye")) { print("> bye stranger"); history = new ArrayList(); } else*/ { history.add(line); history.add(makeAndroid3_getAnswer(line, history, a)); // prints answer on console too } } } catch (Throwable __e) { printStackTrace2(__e); } } }; startThread(_t_0); } } 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); //appendToLog(logFile, s); } } }}; } 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; } static String makeAndroid3_fallback(String s, List history, String answer) { // Now we only do the safe thing instead of VM inspection - give out our process ID if (answer == null && match3("what is your pid", s)) return getPID(); if (answer == null && match3("what is your program id", s)) // should be fairly safe, right? 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; } static boolean makeAndroid3_consoleInUse() { for (Object o : record_list) if (o instanceof Android3 && ((Android3) o).console) return true; return false; } static Responder makeAndroid3_verboseResponder(final Android3 a) { return new Responder() { 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; } }; } static ThreadLocal makeAndroid3_io = new ThreadLocal(); static Android3 makeAndroid3() { return makeAndroid3(getProgramTitle() + "."); } static void interruptThreads(List threads) { for (Thread t : unnull(threads)) t.interrupt(); } static void interruptThreads(Class mainClass) { interruptThreads(registeredThreads(mainClass)); } static A callMain(A c, String... args) { callOpt(c, "main", new Object[] {args}); return c; } static void callMain() { callMain(mc()); } 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; } // tok must come from htmlTok // returns all container tags found (including content) as CNC // honors nested tags correctly static List> findContainerTagWithParamsDeep(List tok, String tag, Object... params) { List < List < String > > l = new ArrayList(); for (List t : findContainerTagDeep(tok, tag)) if (htmlCheckParams(second(t), params)) l.add(t); return l; } static List> findContainerTagWithParamsDeep(String html, String tag, Object... params) { return findContainerTagWithParamsDeep(htmlTok(html), tag, params); } static String ai1_cookiesToday() { String info = ai1_visitorsInfo(); return or2( jextract(" cookie", info), jextract(" cookies", info)); } static A oneOf(List l) { return l.isEmpty() ? null : l.get(new Random().nextInt(l.size())); } static char oneOf(String s) { return empty(s) ? '?' : s.charAt(random(l(s))); } static String oneOf(String... l) { return oneOf(asList(l)); } static String getStackTrace(Throwable throwable) { lastException(throwable); StringWriter writer = new StringWriter(); throwable.printStackTrace(new PrintWriter(writer)); return hideCredentials(writer.toString()); } // supports the usual quotings (', ", variable length double brackets) static boolean isQuoted(String s) { if (startsWith(s, "'") || startsWith(s, "\"")) return true; return isMultilineQuoted(s); } static boolean flexMatchIC(String pat, String s) { return flexMatchIC2(pat, s); } static boolean flexMatchIC(String pat, String s, Matches m) { return flexMatchIC2(pat, s, m); } static boolean flexMatchIC(String pat, String s, Matches m, boolean joinBrackets) { return flexMatchIC2(pat, s, m, joinBrackets); } static Cache> cachedNodeIndex2_cache = new Cache("dwlw_fullNodeIndex_ci"); static boolean cachedNodeIndex2_monitorCircleEditor; static boolean cachedNodeIndex2_first = true; static FileStatus cachedNodeIndex2_status; 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(); } static void cachedNodeIndex2_clear() { cachedNodeIndex2_cache.clear(); } static boolean cachedNodeIndex2_onNewWeb(Web web, Object[] params) { return cachedNodeIndex2_onNewWeb(web); } static boolean cachedNodeIndex2_onNewWeb(Web web) { Lock _lock_596 = cachedNodeIndex2_cache.lock; lock(_lock_596); try { //print("Incremental update"); ai_addWebToIndex(cachedNodeIndex2_cache.get(), web); cachedNodeIndex2_cache.changeCount++; return false; // don't trigger full update } finally { unlock(_lock_596); } } static void cachedNodeIndex2_onWebRemoved(Web web) { Lock _lock_597 = cachedNodeIndex2_cache.lock; lock(_lock_597); try { print("Incremental removal"); ai_removeWebFromIndex(cachedNodeIndex2_cache.get(), web); cachedNodeIndex2_cache.changeCount++; } finally { unlock(_lock_597); } } static long cachedNodeIndex_changeCount() { return cachedNodeIndex2_cache.changeCount; } static Object[] arrayPlus(Object[] a1, Object... a2) { return concatArrays(a1, a2); } // drop opening and closing tag, return new CNC static List contentsOfContainerTag(List tok) { return tok == null ? null : subList(tok, 2, l(tok)-2); } static List contentsOfContainerTag(List tok, String tagName) { return contentsOfContainerTag(first(findContainerTag(tok, tagName))); } static String callAnswerMethod(Object responder, String s) { return callAnswerMethod(responder, s, litlist(s)); } 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); } static Map webToCAL_visMap = caseInsensitiveSynchroMap(); static void webToCAL_visStructures(String... structures) { for (String s : structures) try { webToCAL_visStructures(cal_unstructure(s)); } catch (Throwable __e) { printStackTrace2(__e); } } 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); } static List transientWebs_list = synchroList(); static List transientWebs() { return transientWebs_list; } static String renderMap_colonComma(Map map) { return joinWithComma(mapMapToList(new Object() { 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)); } static boolean isFalse(Object o) { return eq(false, o); } static List web_uniqueTexts(List l) { return asList(new LinkedHashSet(map("web_text",l))); } static String verbToThirdPerson(String s) { { String _a_617 = verbToThirdPersonMap().get(lower(s)); if (!empty(_a_617)) return _a_617; } return s + "s"; } static String hcss(Object contents) { return htag("style", contents); } static boolean pcallF_anyFalse(List functions, Object... args) { boolean flag = false; for (Object f : functions) if (isFalse(pcallF(f, args))) flag = true; return flag; } static String ai_renderNodeHTML(WebNode n) { return ai_renderNodeHTML(n, "-"); } static String ai_renderNodeHTML(WebNode n, String defaultText) { if (n == null || !web_hasLabels(n)) return defaultText; return joinWithComma(map("html_encyclopediaTopic",web_texts(n))); } static void saveLocally(String variableName) { saveLocally(programID(), variableName); } static void saveLocally(String progID, String variableName) { saveLocally2(mc(), progID, variableName); } static void saveLocally2(Object obj, String variableName) { saveLocally2(obj, programID(), variableName); } static void saveLocally2(Object obj, String progID, String variableName) { Lock _lock_626 = saveLock(); lock(_lock_626); 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(_lock_626); } } 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; } static List ai_postTriple_multi(String a, String b, List c) { List webs = new ArrayList(); for (String s : unnull(c)) addIfNotNull(webs, ai_postTriple(a, b, s)); return webs; } static String prependIfNempty(String prefix, String s) { return empty(s) ? s : prefix + s; } static List splitAtJavaToken(String s, String splitToken) { return splitByJavaToken(s, splitToken); } 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); } } static boolean eitherIsB(Either e) { return e != null && e.isB(); } static byte[] toUtf8(String s) { try { return s.getBytes("UTF-8"); } catch (Exception __e) { throw rethrow(__e); } } volatile static boolean conceptsAndBot_running; static void conceptsAndBot() { conceptsAndBot(null); } static void conceptsAndBot(Integer autoSaveInterval) { if (conceptsAndBot_running) return; conceptsAndBot_running = true; try { ensureDBNotRunning(dbBotStandardName()); } catch (Throwable _e) { mainConcepts.dontSave = true; // SAFETY throw rethrow(_e); } if (autoSaveInterval != null) loadAndAutoSaveConcepts(autoSaveInterval); else loadAndAutoSaveConcepts(); dbBot(); } static void ai_spec_substWebs(String word) { //for (WebNode n : ai_search_dollarX_verified("$X", "is", "a generally applicable substition web")) // ai_subst_web_charBasedReplace(web_text(n), word); Matches m = new Matches(); if (swic_trim(word, "a ", m)) ai_subst_web_charBasedReplace("nuoqkgtowqtrjeqo", m.rest()); } static Collection allWebsFromCachedNodeIndex_uncached() { return nodesToWebs(values(cachedNodeIndex())); } static class FG extends F1 { F1 f, g; FG() {} FG(F1 f, F1 g) { this.g = g; this.f = f;} Object get(Object o) { return f.get(g.get(o)); } } static Object fg(final Object f, final Object g) { return new FG(f1(f), f1(g)); } static Object fg(final Object f, final Object g, final Object h) { return fg(f, fg(g, h)); } static List nthColumn(List> matrix, int idx) { List l = new ArrayList(); for (List row : matrix) l.add(get(row, idx)); return l; } static boolean isVerb(String s) { return containsNotNull(knownVerbs(), lower(s)); } static String h3(String s, Object... params) { return tag("h3", s, params) + "\n"; } 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); } static String loadCachedTranspilation(String id) { return loadTextFile(new File(getCodeProgramDir(id), "Transpilation")); } static List emptyList() { return new ArrayList(); //ret Collections.emptyList(); } static List emptyList(int capacity) { return new ArrayList(capacity); } // Try to match capacity static List emptyList(Iterable l) { return l instanceof Collection ? emptyList(((Collection) l).size()) : emptyList(); } 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; } static A collectionGet(Collection c, int idx) { if (c == null || idx < 0 || idx >= l(c)) return null; if (c instanceof List) return get((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; } static void pcallFAll(Collection l, Object... args) { if (l != null) for (Object f : l) pcallF(f, args); } static boolean startsWithWords(String a, String b, Matches m) { return startsWithWord(a, b, m); } static boolean startsWithWords(String a, String b) { return startsWithWord(a, b); } static String clUnparse(Lisp l) { if (l == null) return ""; return join(" ", (List) concatLists(ll(conceptQuote(l.head)), map("clUnparse_sub", l))); } static String clUnparse_sub(Lisp l) { return l.empty() ? clUnparse(l) : "(" + clUnparse(l) + ")"; } static List clUnparse(List l) { return map("clUnparse", l); } static boolean isEmpty(Collection c) { return c == null || c.isEmpty(); } static boolean isEmpty(CharSequence s) { return s == null || s.length() == 0; } static boolean isEmpty(Object[] a) { return a == null || a.length == 0; } static boolean isEmpty(Map map) { return map == null || map.isEmpty(); } static List dropLeadingPunctuation_keep = ll("*", "<", ">"); 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; } static String dropLeadingPunctuation(String s) { return join(dropLeadingPunctuation(nlTok(s))); } static String web_unquote(WebNode node) { return node == null ? null : unquote(web_text(node)); } static List nlTok(String s) { return javaTokPlusPeriod(s); } 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; } static String containerTag(String tag) { return containerTag(tag, ""); } static String containerTag(String tag, Object contents, Object... params) { String openingTag = hopeningTag(tag, params); String s = str(contents); return openingTag + s + ""; } static List indexedNodes_rawLookup(String s) { return cachedNodeIndex().get(ai_shortenForIndex(s)); } static String struct(Object o) { return structure(o); } static String struct(Object o, structure_Data data) { return structure(o, data); } // o can be a class, then we search for a static method called "html". static Object callHtmlMethod2(Object o, String uri) { return callHtmlMethod2(o, uri, null); } 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; } static String html_encyclopediaTopic(String topic) { return ahref(encyclopediaLink(topic), htmlencode_noQuotes(topic), "style" , dottedStyleForLinks() + "; color: inherit"); } static File classicNohupJavax(String javaxargs) { return classicNohupJavax(javaxargs, ""); } static File classicNohupJavax(String javaxargs, String vmArgs) { try { File jar = pathToJavaxJar(); if (javaxargs.startsWith("#")) javaxargs = javaxargs.substring(1); String tempPrefix = ymd_minus_hms() + "_" + javaxargs + "_"; File outFile = File.createTempFile(tempPrefix, ""); String cmd = "java " + vmArgs + " -jar " + (isWindows() ? winQuote(jar) : bashQuote(jar)) + " " + javaxargs; nohup(cmd, outFile, false); return outFile; } catch (Exception __e) { throw rethrow(__e); } } static long getFileSize(String path) { return path == null ? 0 : new File(path).length(); } static long getFileSize(File f) { return f == null ? 0 : f.length(); } static List listWithout(List a, Object... b) { return minus(a, b); } static String ai_html_triangle; static String ai_html_triangle() { if (ai_html_triangle == null) ai_html_triangle = " " + htmlencode(unicode_rightPointingTriangle()) + " "; return ai_html_triangle; } static List uniquify(Collection l) { return uniquifyList(l); } static String fsI(String id) { return formatSnippetID(id); } static String fsI(long id) { return formatSnippetID(id); } static Web simpleWeb() { Web web = new Web(); return web; } static String lower(String s) { return s == null ? null : s.toLowerCase(); } static char lower(char c) { return Character.toLowerCase(c); } static List cloneList(Collection l) { if (l == null) return new ArrayList(); // assume collection's mutex is equal to collection, which will be true unless you explicitly pass a mutex to synchronizedList() which no one ever does. synchronized(l) { return new ArrayList(l); } } static boolean ai_is(String a, String b) { return ai_hasTriple(a, "is", b); } static Map litmap(Object... x) { HashMap map = new HashMap(); litmap_impl(map, x); return map; } 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]); } static Object makeAndCall_cached(String functionName, Object... args) { return preciseCall(loadFunctions_cached(functionName), functionName, args); } static Object safeUnstructure(String s) { return unstructure(s, true); } static List newSubList(List l, int startIndex, int endIndex) { return cloneList(subList(l, startIndex, endIndex)); } static List newSubList(List l, int startIndex) { return cloneList(subList(l, startIndex)); } static T3 listToTriple(List l) { return l(l) != 3 ? null : triple(get(l, 0), get(l, 1), get(l, 2)); } static WebNode web_findNode(Web web, String name) { return web.findNode(name); } static void logStructure(File logFile, Object o) { logQuoted(logFile, structure(o)); } // quick version - log to file in program directory static void logStructure(String fileName, Object o) { logStructure(getProgramFile(fileName), o); } static void logStructure(String progID, String fileName, Object o) { logStructure(getProgramFile(progID, fileName), o); } static WebRelation web_findRelationNamed(Web web, String s) { return web_findRelationWithLabel(web, s); } static Matcher regexp(String pat, String s) { return Pattern.compile(pat).matcher(s); } static List allWebs() { return concatWebLists(allWebs_fullList()); } 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; } static int indexOfAny(List l, Collection x) { return indexOfAny(l, 0, x); } 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; } 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; } static void restartWithDelay(int delay) { Object j = getJavaX(); call(j, "preKill"); call(j, "nohupJavax", smartJoin((String[]) get(j, "fullArgs")), call(j, "fullVMArguments")); sleep(delay); System.exit(0); sleep(); } static volatile boolean ping_pauseAll; static int ping_sleep = 100; // poll pauseAll flag every 100 static volatile boolean ping_anyActions; static Map ping_actions = (Map) synchroMap(newWeakHashMap()); // always returns true static boolean ping() { if (ping_pauseAll || ping_anyActions) ping_impl(); return true; } // returns true when it slept 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); } } 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 dropSuffixTrim(tail, s); } } } return s; } static String formatDateAndTime(long timestamp) { return formatDate(timestamp); } static String formatDateAndTime() { return formatDate(); } static void markWebsPosted_createMarker() { createMarkerFile("#1007609", "webs.posted.at." + psI(programID())); } static long unixHour() { return unixTime()/(60*60); } static ThreadLocal loadPage_charset = new ThreadLocal(); static boolean loadPage_allowGzip = true, loadPage_debug; static boolean loadPage_anonymous; // don't send computer ID static int loadPage_verboseness = 100000; static int loadPage_retries = 1; //60; // seconds static ThreadLocal loadPage_silent = new ThreadLocal(); static volatile int loadPage_forcedTimeout; // ms static ThreadLocal>> loadPage_responseHeaders = 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 { 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(e); sleepSeconds(1); } throw e; } catch (Exception __e) { throw rethrow(__e); } } static String loadPage_preprocess(String url) { if (url.startsWith("tb/")) // don't think we use this anymore 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())) print("Loading: " + hideCredentials(url)); return loadPageSilently(new URL(url)); } catch (Exception __e) { throw rethrow(__e); } } public static String loadPage(URL url) { print("Loading: " + hideCredentials(url.toExternalForm())); return loadPageSilently(url); } static String loadPage(URLConnection con, URL url) throws IOException { return loadPage(con, url, true); } static String loadPage(URLConnection con, URL url, boolean addHeaders) throws IOException { if (addHeaders) try { if (!loadPage_anonymous) setHeaders(con); if (loadPage_allowGzip) con.setRequestProperty("Accept-Encoding", "gzip"); con.setRequestProperty("X-No-Cookies", "1"); } catch (Throwable e) {} // fails if within doPost String contentType = con.getContentType(); if (contentType == null) { //printStruct("Headers: ", con.getHeaderFields()); throw new IOException("Page could not be read: " + url); } //print("Content-Type: " + contentType); String charset = loadPage_charset == null ? null : loadPage_charset.get(); if (charset == null) charset = loadPage_guessCharset(contentType); InputStream in = con.getInputStream(); try { loadPage_responseHeaders.set(con.getHeaderFields()); if ("gzip".equals(con.getContentEncoding())) { if (loadPage_debug) print("loadPage: Using gzip."); in = new GZIPInputStream(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 { in.close(); } } 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); /* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */ return or(match, "ISO-8859-1"); } static URLConnection loadPage_openConnection(URL url) { URLConnection con = openConnection(url); if (loadPage_forcedTimeout != 0) setURLConnectionTimeouts(con, loadPage_forcedTimeout); return con; } static WebNode web_findNodeOrRelation(Web web, String name) { WebNode n = web.findNode(name); if (n != null) return n; return web_findRelationWithLabel(web, name); } static List web_collectBackwardRelations(Collection nodes) { List l = new ArrayList(); for (WebNode n : unnull(nodes)) l.addAll(web_backwardRelations(n)); return l; } static Set ai_yesNoQuestionWord_1_list = littreeset("am", "are", "is", "can", "does", "do", "did", "would", "might", "will", "must", "should", "has", "have"); static boolean ai_yesNoQuestionWord_1(String s) { return ai_yesNoQuestionWord_1_list.contains(lower(s)); } static void assertTrue(Object o) { assertEquals(true, o); } static boolean assertTrue(String msg, boolean b) { if (!b) throw fail(msg); return b; } static boolean assertTrue(boolean b) { if (!b) throw fail("oops"); return b; } static long toMS(double seconds) { return (long) (seconds*1000); } static int makeBot(String greeting) { return makeAndroid3(greeting).port; } static Android3 makeBot(Android3 a) { makeAndroid3(a); return a; } static Android3 makeBot(String greeting, Object responder) { Android3 a = new Android3(greeting); a.responder = makeResponder(responder); makeBot(a); return a; } static Android3 makeBot() { return makeAndroid3(getProgramTitle() + "."); } static List onTransientWebRemoved_list = synchroList(); // f: voidfunc(Web) static void onTransientWebRemoved_do(Object f) { setAdd(onTransientWebRemoved_list, f); } static void onTransientWebRemoved(Web web) { pcallFAll(onTransientWebRemoved_list, web); } static long toMB(long l) { return toM(l); } static String toMB(long l, int digits) { return toM(l, digits); } 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; } static String html_linkURLs_targetBlank(String html) { return html_linkURLs(html, " target=\"_blank\""); } static A assertEquals(Object x, A y) { return assertEquals(null, x, y); } 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; } static List allToLower(Collection l) { return map("lower", l); } static List tlft(String s) { return toLinesFullTrim(s); } static List tlft(File f) { return toLinesFullTrim(f); } static String programTitle() { return getProgramName(); } 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; } static String webToStringShort(Web web) { try { 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) { printException(e); return "Error"; } } static boolean usePreSpunVMs() { return eq("1", trim(loadProgramTextFile("#1005457", "enable-prespun-vms"))); } static String md5(String text) { try { if (text == null) return "-"; return bytesToHex(md5_impl(text.getBytes("UTF-8"))); // maybe different than the way PHP does it... } catch (Exception __e) { throw rethrow(__e); } } static String md5(byte[] data) { if (data == null) return "-"; return bytesToHex(md5_impl(data)); } static MessageDigest md5_md; /*static byte[] md5_impl(byte[] data) ctex { if (md5_md == null) md5_md = MessageDigest.getInstance("MD5"); return ((MessageDigest) md5_md.clone()).digest(data); }*/ static byte[] md5_impl(byte[] data) { try { return MessageDigest.getInstance("MD5").digest(data); } catch (Exception __e) { throw rethrow(__e); } } static String md5(File file) { try { return md5(loadBinaryFile(file)); } catch (Exception __e) { throw rethrow(__e); } } static List ll(A... a) { return litlist(a); } static boolean dynamicObjectIsLoading() { return isTrue(getThreadLocal(DynamicObject_loading)); } static List scanStructureLog(String progID, String fileName) { return scanStructureLog(getProgramFile(progID, fileName)); } static List scanStructureLog(String fileName) { return scanStructureLog(getProgramFile(fileName)); } static List scanStructureLog(File file) { List l = new ArrayList(); for (String s : scanLog(file)) try { l.add(unstructure(s)); } catch (Throwable __e) { printStackTrace2(__e); } return l; } static void ai_spec_isAQuestion(String _s) { String s = unquote(_s); if (ai_isQuestion_1(s) || endsWith(s, "?")) ai_postTriple(_s, "is", "a question"); } static TreeSet numbersOfStandardFunctionArguments(String sfName) { return numbersOfMethodArguments(loadFunctions_cached(sfName), sfName); } static WebNode web_secondNode(Web web) { return web == null ? null : second(web.nodes); } 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); } // tok must come from htmlTok // returns all container tags found (including content) as CNC // honors nested tags correctly static List> findContainerTagDeep(List tok, String tag) { List < List < String > > 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)); // actual CNC break; } } } return l; } static List> findContainerTagDeep(String html, String tag) { return findContainerTagDeep(htmlTok(html), tag); } static A second(List l) { return get(l, 1); } static A second(A[] bla) { return bla == null || bla.length <= 1 ? null : bla[1]; } static B second(Pair p) { return p == null ? null : p.b; } static B second(T3 t) { return t == null ? null : t.b; } 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; } static boolean webs_search_noVar_bool(Web searchWeb, Collection webs) { return webs_search_noVar(searchWeb, webs) != null; } 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; } static int indexOf(List l, int startIndex, A a) { return indexOf(l, a, startIndex); } static int indexOf(List l, A a) { if (l == null) return -1; return l.indexOf(a); } static int indexOf(String a, String b) { return a == null || b == null ? -1 : a.indexOf(b); } static int indexOf(String a, String b, int i) { return a == null || b == null ? -1 : a.indexOf(b, i); } static int indexOf(String a, char b) { return a == null ? -1 : a.indexOf(b); } static int indexOf(String a, char b, int i) { return a == null ? -1 : a.indexOf(b, i); } static int indexOf(String a, int i, String b) { return a == null || b == null ? -1 : a.indexOf(b, i); } 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; } 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; } 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; } 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; } 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; } static Object[] subArray(Object[] b, int start) { return subArray(b, start, l(b)); } 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; } static String dropTags(String html) { return dropAllTags(html); } static List dropTags(List tok) { return dropAllTags(tok); } static boolean all(Object pred, Collection l) { if (l != null) for (Object o : l) if (!isTrue(callF(pred, o))) return false; return true; } static String dropSuffixAndTrim(String suffix, String s) { return trim(dropSuffix(suffix, s)); } static void preloadProgramTitle() { { Thread _t_0 = new Thread() { public void run() { try { programTitle(); } catch (Throwable __e) { printStackTrace2(__e); } } }; startThread(_t_0); } } 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))); } static String ai_tripleToString(T3 t) { return t == null ? "" : t.a + " -> " + t.b + " -> " + t.c; } static String trimJoin(List s) { return trim(join(s)); } static String hijackPrint(Runnable r) { final StringBuilder buf = new StringBuilder(); F1 old = interceptPrintInThisThread(new F1() { Boolean get(String s) { buf.append(s); return false; } }); try { callF(r); return str(buf); } finally { interceptPrintInThisThread(old); } } static List cloneAndClear(List l) { return cloneAndClearList(l); } static A nu(Class c, Object... values) { A a = nuObject(c); setAll(a, values); return a; } // port -> various VM info static Map computerVMs_shorter() { Map vms = computerVMs(); for (Map map : values(vms)) mapRemoveAll(map, "More info", "Injection ID"); return vms; } static JWindow infoBox(String text) { return infoMessage(text); } static JWindow infoBox(String text, double seconds) { return infoMessage(text, seconds); } static JWindow infoBox(Throwable e) { return infoMessage(e); } static SortedCondensedStrings2 hugeEnglishDictionary() { File zip = loadLibrary("#1002028"); List lines = lines(fromUTF8(loadBinaryFromZip(zip, "words.txt"))); return new SortedCondensedStrings2(lines); } static String substring(String s, int x) { return substring(s, x, l(s)); } 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); } static String assertIdentifier(String s) { return assertIsIdentifier(s); } static final WeakHashMap> getOpt_cache = new WeakHashMap(); static final HashMap getOpt_special = new HashMap(); // just a marker static { getOpt_cache.put(Class.class, getOpt_special); getOpt_cache.put(String.class, getOpt_special); } 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 S) ret getOpt(getBot((S) 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); } } // used internally - we are in synchronized block 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; } static T3 trimTriple(T3 t) { return t == null ? null : triple(trim(t.a), trim(t.b), trim(t.c)); } static String structureForUser(Object o) { return beautifyStructure(struct_noStringSharing(o)); } static A or(A a, A b) { return a != null ? a : b; } 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; } 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); } } static double stefansChat_onLine_safetyDelay = 1; // seconds static double stefansChat_onLine_shortSafetyDelay = 0.1; static double stefansChat_onLine_errorDelay = 10; static int stefansChat_onLine_lookback = 0; static int stefansChat_onLine_firstRead = -1; static ThreadLocal stefansChat_onLine_fullParams = new ThreadLocal(); static ThreadLocal stefansChat_onLine_onHistoryRead = new ThreadLocal(); static void stefansChat_onLine(int lookback, Object f) { stefansChat_onLine_lookback = lookback; stefansChat_onLine(f); } 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 = loadPage("http://ai1.lol/1008998/raw/incremental?json=1&a=" + a); if (empty(trim(s))) { sleepSeconds(stefansChat_onLine_safetyDelay); print("Re-polling."); 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((Map) msg); 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) { printStackTrace2(__e); }} // end pcall, while licensed } catch (Throwable __e) { printStackTrace2(__e); } } }; startThread(_t_0); } } static List dropPunctuation_keep = litlist("*", "<", ">"); 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; } static String dropPunctuation(String s) { return join(dropPunctuation(nlTok(s))); } static String dropSuffix(String suffix, String s) { return s.endsWith(suffix) ? s.substring(0, l(s)-l(suffix)) : s; } static List dropTagsAndHTMLComments(List tok) { return dropHTMLComments(dropTags(tok)); } static void smartSet(Field f, Object o, Object value) throws Exception { f.setAccessible(true); // take care of common case (long to int) if (f.getType() == int.class && value instanceof Long) value = ((Long) value).intValue(); try { f.set(o, value); } catch (Exception e) { 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; } } static boolean isMD5(String s) { return l(s) == 32 && isLowerHexString(s); } // can probably be merged with next variant static void disposeWindow(final Window window) { if (window != null) { swing(new Runnable() { public void run() { try { window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING)); // call listeners window.dispose(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING)); //..."; }}); } } static void disposeWindow(final Component c) { disposeWindow(getWindow(c)); } static void disposeWindow(Object o) { if (o != null) disposeWindow((Component) o); } static int toMS_int(double seconds) { return toInt_checked((long) (seconds*1000)); } static byte[] toJPG(BufferedImage img) { return toJPEG(img); } static byte[] toJPG(File f) { return toJPEG(f); } static Lock aiLock() { return dbLock(); } static String htag(String tag) { return htag(tag, ""); } static String htag(String tag, Object contents, Object... params) { String openingTag = hopeningTag(tag, params); String s = str(contents); if (empty(s)) return dropLast(openingTag) + "/>"; return openingTag + s + ""; } static Cache> baseFormVerbs_cache = new Cache("baseFormVerbs_load"); static Set baseFormVerbs() { return baseFormVerbs_cache.get(); } static Set baseFormVerbs_load() { return new TreeSet(nthColumn(englishVerbQuintuples(), 0)); } static Object callFunction(Object f, Object... args) { return callF(f, args); } static List onWebsChanged_listeners = synchroList(); static void triggerWebsChanged() { pcallF_all(onWebsChanged_listeners); } static void onWebsChanged(Object f) { setAdd(onWebsChanged_listeners, f); } 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; } static String replaceIC(String s, String a, String b) { return s.replaceAll("(?i)" + patternQuote(a), quoteReplacement(b)); } static File programFile(String name) { return prepareProgramFile(name); } static File programFile(String progID, String name) { return prepareProgramFile(progID, name); } static A assertNotNull(A a) { assertTrue(a != null); return a; } static A assertNotNull(String msg, A a) { assertTrue(msg, a != null); return a; } 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); } static List onTransientWebAdded_list = synchroList(); // f: voidfunc(Web) static void onTransientWebAdded_do(Object f) { setAdd(onTransientWebAdded_list, f); } static void onTransientWebAdded(Web web) { pcallFAll(onTransientWebAdded_list, web); } 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; } static Set allIndexedTermsStartingWith(String prefix) { return prefixSubSet(navigableKeys(cachedNodeIndex()), prefix); } static List web_nodes(Web web) { return web_nonRelationNodes(web); } static String tagGet(String tag, String param) { return mapGet(htmlgetparams(tag), param); } static void ai_activeSpec_answerOnChat(String s) { String response = ""; { Lock _lock_902 = ai_activeSpecLock(); lock(_lock_902); try { if (ai_hasTriple(s, "should be", "answered in the chat")) { List answers = ai_texts("$X", "answers", s); print("activeSpec: have answers " + answers); if (nempty(answers)) { ai_invalidateTriple(s, "should be", "answered in the chat"); response = s + "\n=> " + joinWithComma(answers); } } } finally { unlock(_lock_902); } } if (nempty(response) && postToStefansChat(response)) ai_postTriple(s, "was", "answered in the chat"); } static void web_addRelation(WebNode a, WebNode b, String rel) { if (a != null && b != null && rel != null) a.web.getRelation(a, b).addLabel(rel); } static void web_addRelation(Web web, String a, String b, String rel) { web_addRelation(web.node(a), web.node(b), rel); } static void web_addRelation(WebNode a, String rel, WebNode b) { web_addRelation(a, b, rel); } static Object pcallOpt(Object o, String method, Object... args) { try { return callOpt(o, method, args); } catch (Throwable __e) { printStackTrace2(__e); } return null; } static String firstWord(String s) { return firstJavaToken(s); } // nodeMatcher: func(WebNode, WebNode) -> double 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; //fail("Can't match, differing number of nodes: " + l(patternNodes) + "/" + l(inputNodes)); List> perms = allPermutations(inputNodes); Best < List < WebNode > > 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); } static void ai_spec_addQuestionsToAdjective(String s) { if (!ai_hasTriple(s, "is", "an adjective")) return; ai_postTriple(s, "begs the question", quote("What is the opposite of " + s + "?")); } 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)); } static boolean triple_anyPartNull(T3 t) { return t == null || t.a == null || t.b == null || t.c == null; } static T3 t3(A a, B b, C c) { return new T3(a, b, c); } static String fixNewLines(String s) { return s.replace("\r\n", "\n").replace("\r", "\n"); } 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; } static String dropCertainPunctuationAtEnd(String s, String chars) { return join(dropCertainPunctuationAtEnd(nlTok(s), chars)); } // runnable = Runnable or String (method name) static Thread newThread(Object runnable) { return new Thread(_topLevelErrorHandling(toRunnable(runnable))); } static Thread newThread(Object runnable, String name) { return new Thread(_topLevelErrorHandling(toRunnable(runnable)), name); } static String joinWithSpace2(Collection c) { return joinNempties(" ", c); } static String joinWithSpace2(String... c) { return joinNempties(" ", c); } static String programID; static String getProgramID() { return nempty(programID) ? formatSnippetIDOpt(programID) : "?"; } // TODO: ask JavaX instead static String getProgramID(Class c) { String id = (String) getOpt(c, "programID"); if (nempty(id)) return formatSnippetID(id); return "?"; } static String getProgramID(Object o) { return getProgramID(getMainClass(o)); } static String unnull(String s) { return s == null ? "" : s; } static List unnull(List l) { return l == null ? emptyList() : l; } static Iterable unnull(Iterable i) { return i == null ? emptyList() : i; } static A[] unnull(A[] a) { return a == null ? (A[]) new Object[0] : a; } static BitSet unnull(BitSet b) { return b == null ? new BitSet() : b; } static Pt unnull(Pt p) { return p == null ? new Pt() : p; } static String joinWithComma(Collection c) { return join(", ", c); } static String joinWithComma(String... c) { return join(", ", c); } 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())); } static long memoryUsedAfterLastGC() { try { return toLong(get(javax(), "gc_memoryUsedAfterGC")); } catch (Throwable e) { printException(e); upgradeJavaXAndRestart(); return 0; } } static boolean flexMatchIC2_debug; static boolean flexMatchIC2(String pat, String s) { return flexMatchIC2(pat, s, null); } static boolean flexMatchIC2(String pat, String s, Matches m) { return flexMatchIC2(javaTok(pat), javaTok_cached(unnull(s)), m); } static boolean flexMatchIC2(String pat, String s, Matches m, boolean joinBrackets) { return flexMatchIC2(javaTok(pat), javaTok_cached(unnull(s)), m, joinBrackets); } static boolean flexMatchIC2(List tokpat, List tokfull, Matches m) { return flexMatchIC2(tokpat, tokfull, m, true); } 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++, "!*"); // insert single-token wildcard in front to avoid empty matches 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; } 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, "*")) { // the flex wildcard (0 or more tokens) 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; // success, leave mark } } if (flexMatchIC2_debug) print("Failed * matching"); bla.clear(itok); // fail, undo marking return false; } if (itok >= l(tok)) { if (flexMatchIC2_debug) print("too much pattern"); return false; } if (eq(t, "!*")) { // the single-token wildcard bla.set(itok); if (flexMatchIC2_impl(pat, ipat+1, tok, itok+1, bla, bla2)) return true; // success, leave mark bla.clear(itok); // fail, undo marking return false; } String realt = tok.get(itok); if (t.startsWith("(") && t.endsWith(")")) { // quick pre-check if (flexMatchIC2_debug) print("flexMatchIC2 precheck " + t + " " + realt); if (!containsIgnoreCase(t, realt)) return false; // real check 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; } // it is a token match. consume and proceed if (flexMatchIC2_impl(pat, ipat+1, tok, itok+1, bla, bla2)) return true; else { bla2.clear(itok); return false; } } static JWindow showLoadingAnimation() { return showLoadingAnimation("Hold on user..."); } static JWindow showLoadingAnimation(String text) { return showAnimationInTopRightCorner("#1003543", text); } // undefined color, seems to be all black in practice static BufferedImage newBufferedImage(int w, int h) { return new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); } 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; } static BufferedImage newBufferedImage(Pt p, Color color) { return newBufferedImage(p.x, p.y, color); } static Set ai_ignoredWebs() { Matches m = new Matches(); Set ignoredWebs = new HashSet(); for (WebNode n : ai_index_search_dollarX("$X", "can be", "ignored")) if (swic(web_text(n), "web ", m)) ignoredWebs.add(m.unq(0)); return ignoredWebs; } static boolean tripleContains(T3 t, Object o) { return t != null && (t.a == o || t.b == o || t.c == o); } static Class getMainClass() { return main.class; } static Class getMainClass(Object o) { try { return (o instanceof Class ? (Class) o : o.getClass()).getClassLoader().loadClass("main"); } catch (Exception __e) { throw rethrow(__e); } } static List subList(List l, int startIndex) { return subList(l, startIndex, l(l)); } static List subList(List l, int startIndex, int endIndex) { startIndex = max(0, min(l(l), startIndex)); endIndex = max(0, min(l(l), endIndex)); if (startIndex > endIndex) return litlist(); return l.subList(startIndex, endIndex); } 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); } static List collectField(Collection c, String field) { List l = new ArrayList(); for (Object a : c) l.add(getOpt(a, field)); return l; } static List tripleToList(T3 t) { return t == null ? (List) ll() : (List) ll(t.a, t.b, t.c); } 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())); return web; } 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; } 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)); } static String web_unquoted(WebNode node) { return web_unquote(node); } static int parseFirstInt(String s) { return parseInt(jextract("", s)); } 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); } } static String tolower(String s) { return s == null ? null : s.toLowerCase(); } static char tolower(char c) { return Character.toLowerCase(c); } 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)"; }}; } static List methodsStartingWith(Object o, final String prefix) { return filter(allMethodNames(o), new Object() { Object get(String s) { try { return startsWith(s, prefix) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "startsWith(s, prefix)"; }}); } static Web ai_blob(String word, boolean verified) { if (hasIndexedNode(word)) return null; Web web = oneNodeWeb(word); web.unverified = !verified; postSoftwareMadeWeb(web); return web; } static int indexOfSubList(List x, List y) { return indexOfSubList(x, y, 0); } 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; } 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; } static HashMap pairsToMap(Collection> l) { HashMap map = new HashMap(); if (l != null) for (Pair p : l) map.put(p.a, p.b); return map; } static String _computerID; public static String computerID() { try { if (_computerID == null) { File file = new File(userHome(), ".tinybrain/computer-id"); _computerID = loadTextFile(file.getPath(), null); if (_computerID == null) { _computerID = makeRandomID(12); saveTextFile(file.getPath(), _computerID); } } return _computerID; } catch (Exception __e) { throw rethrow(__e); } } static WebRelation web_getRelationOpt(WebNode a, WebNode b) { return a.web.getRelationOpt(a, b); } static A findByField(Collection c, String field, Object value) { for (A a : c) if (eq(getOpt(a, field), value)) return a; return null; } static String formatSnippetID(String id) { return "#" + parseSnippetID(id); } static String formatSnippetID(long id) { return "#" + id; } static long psI(String snippetID) { return parseSnippetID(snippetID); } static boolean warn_on = true; static void warn(String s) { if (warn_on) print("Warning: " + s); } static void warn(String s, List warnings) { warn(s); if (warnings != null) warnings.add(s); } static Set indexedTerms() { return keys(cachedNodeIndex()); } // tok must come from htmlTok // returns all container tags found (including content) as CNC // should be OK for both HTML and XML static List> findContainerTag(List tok, String tag) { List < List < String > > 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)); // actual CNC break; } } i = j; } return l; } static List> findContainerTag(String html, String tag) { return findContainerTag(htmlTok(html), tag); } static double cal_randomCircleCoord() { return random(0.2, 0.8); } static String ai_setMaker(String maker) { return setThreadLocal(postSoftwareMadeWeb_maker, maker); } static String string(Object o) { return String.valueOf(o); } static void ai_fixTriple(T3 t) { if (t == null) return; 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"))) { t.a = trim(dropLastJavaToken(t.a)); t.b = s + " " + t.b; } tok = javaTokC(t.c); s = last(tok); if (l(tok) > 1 && isAdverb(s)) { t.c = trim(dropLastJavaToken(t.c)); t.b = s + " " + t.b; } if (empty(t.c)) t.c = "."; } // usually L 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(); } static String fromLines(String... lines) { return fromLines(asList(lines)); } static List allMultiMapValues(MultiMap mm) { return mm == null ? emptyList() : concatLists(values(mm.data)); } static List allMultiMapValues(Map> mm) { return concatLists(values(mm)); } 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 dropPrefixTrim(tail, s); } } } return s; } static List> englishVerbQuintuples() { return english1000VerbQuintuples(); } static void addIfNotNull(Collection l, A a) { if (a != null) l.add(a); } static List synchroList() { return Collections.synchronizedList(new ArrayList()); } static List synchroList(List l) { return Collections.synchronizedList(l); } static long now_virtualTime; static long now() { return now_virtualTime != 0 ? now_virtualTime : System.currentTimeMillis(); } static String ai_textHTML(WebNode n) { String text = web_text(n); return n == null ? "-" : html_encyclopediaTopic(text); } static boolean setAdd(Collection c, A a) { if (c.contains(a)) return false; c.add(a); return true; } static Set evalWithTimeout_inTime = synchroSet(); static Set evalWithTimeout_allThreads = synchroSet(); // Either calculation result or Thread with timed-out computation // Tries to interrupt thread after timeout // Errors are rethrown. 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 = startThread(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..."; }}); evalWithTimeout_inTime.add(t); evalWithTimeout_allThreads.add(t); try { done.waitUntilUp(timeoutMS); doneWaiting.raise(); } finally { evalWithTimeout_inTime.remove(t); } // timeout! cancel/interrupt and return thread object if (!done.isUp()) { cancelAndInterruptThread(t); return either2(t); } // thread ended with error if (error.get() != null) rethrow(error.get()); // thread ended naturally return either1(var.get()); } static Either evalWithTimeout(double timeoutSeconds, final Object r) { return evalWithTimeout(toMS_int(timeoutSeconds), r); } static File getProgramFile(String progID, String fileName) { if (new File(fileName).isAbsolute()) return new File(fileName); return new File(getProgramDir(progID), fileName); } static File getProgramFile(String fileName) { return getProgramFile(getProgramID(), fileName); } static ReentrantLock fairLock() { return new ReentrantLock(true); } 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; } static boolean ai_hasTriple_verified(String a, String b, String c) { return ai_cache_hasTriple_verified(a, b, c); } static boolean ai_hasTriple_verified(T3 t) { return ai_cache_hasTriple_verified(t); } static List registeredThreads(Object o) { Map map = (Map) ( getOpt(o, "_registerThread_threads")); if (map == null) return ll(); synchronized(map) { return asList(keys(map)); } } static List registeredThreads() { return asList(keys(_registerThread_threads)); } 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; } static Object getOpt(Object o, String field) { return getOpt_cached(o, field); } 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); } } // access of static fields is not yet optimized 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); } } 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; } static int indexOfIC(Object a, Object b) { return indexOfIgnoreCase(a, b); } static WebNode web_setPosition(WebNode node, double x, double y) { node.x = x; node.y = y; return node; } static List indexedNodes_withInvalids(String s) { List l = indexedNodes_rawLookup(s), l1 = l; for (VirtualNodeIndex i : virtualNodeIndices()) { List x = i.get(s); if (nempty(x)) { if (l == l1) l = cloneList(l); l.addAll(x); } } return l; } 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; } static String[] dropFirst(int n, String[] a) { return drop(n, a); } static String[] dropFirst(String[] a) { return drop(1, a); } static Object[] dropFirst(Object[] a) { return drop(1, a); } static List dropFirst(List l) { return dropFirst(1, l); } static List dropFirst(Iterable i) { return dropFirst(toList(i)); } static List dropFirst(int n, List l) { return n <= 0 ? l : new ArrayList(l.subList(Math.min(n, l.size()), l.size())); } static List dropFirst(List l, int n) { return dropFirst(n, l); } static String dropFirst(int n, String s) { return substring(s, n); } static String dropFirst(String s) { return substring(s, 1); } 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; } static volatile Throwable lastException_lastException; static Throwable lastException() { return lastException_lastException; } static void lastException(Throwable e) { lastException_lastException = e; } static String sfuLL(Object... x) { return sfu(ll(x)); } static WebNode web_firstNode(Web web) { return web == null ? null : first(web.nodes); } static List indexedTerms_scoredSearch(String query, int max) { return takeFirst(max, scoredSearch(query, allIndexedTerms())); } 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); } } static boolean matchStart(String pat, String s) { return matchStart(pat, s, null); } // matches are as you expect, plus an extra item for the rest string 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())); //print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m)); 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())); // for Matches.rest() } return true; } } static void setOpt_raw(Object o, String field, Object value) { if (o == null) return; if (o instanceof Class) setOpt_raw((Class) o, field, value); else try { Field f = setOpt_raw_findField(o.getClass(), field); if (f != null) smartSet(f, o, value); } catch (Exception e) { throw new RuntimeException(e); } } static void setOpt_raw(Class c, String field, Object value) { if (c == null) return; try { Field f = setOpt_raw_findStaticField(c, field); if (f != null) smartSet(f, null, value); } catch (Exception e) { throw new RuntimeException(e); } } 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; } 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; } static String href(String link, Object contents, Object... params) { if (link == null) return str(contents); return tag("a", contents, arrayPlus(params, "href", link)); } static void printWeb(Web web) { if (web == null) { print("-"); return; } List> relations = web_nonEmptyRelations(web); print((web.unverified ? "UNVERIFIED " : "") + "Web " + web.globalID + " from " + web.source + ": " + n(web.nodes, "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); } static String getStackTrace2(Throwable throwable) { return hideCredentials(getStackTrace(throwable) + replacePrefix("java.lang.RuntimeException: ", "FAIL: ", hideCredentials(str(getInnerException(throwable)))) + "\n"); } 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; } 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; } static void incIntThreadLocal(ThreadLocal tl) { tl.set(toInt(tl.get())+1); } static void incIntThreadLocal(BetterThreadLocal tl) { tl.set(toInt(tl.get())+1); } static boolean isGlobalID(String s) { return possibleGlobalID(s); } static String defaultThreadName_name; static String defaultThreadName() { if (defaultThreadName_name == null) defaultThreadName_name = "A thread by " + programID(); return defaultThreadName_name; } 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); } static List web_nodesWithoutRelations(Web web) { return web_nonRelationNodes(web); } static RuntimeException fail() { throw new RuntimeException("fail"); } static RuntimeException fail(Throwable e) { throw asRuntimeException(e); } static RuntimeException fail(Object msg) { throw new RuntimeException(String.valueOf(msg)); } static RuntimeException fail(String msg) { throw new RuntimeException(msg == null ? "" : msg); } static RuntimeException fail(String msg, Throwable innerException) { throw new RuntimeException(msg, innerException); } static List keysSortedByValuesDesc(final Map map) { List l = new ArrayList(map.keySet()); sort(l, mapComparatorDesc(map)); return l; } static void rotateLogFile(File f) { Lock _lock_1084 = appendToFile_lock; lock(_lock_1084); try { 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(_lock_1084); } } static Set allIndexedTerms() { return keys(cachedNodeIndex()); } static java.util.Timer doLater_daemon(long delay, final Object r) { final java.util.Timer timer = new java.util.Timer(true); timer.schedule(timerTask(r), delay); return timer; } static float parseFloat(String s) { return Float.parseFloat(s); } static void ai_spec_unquote(String s) { if (isProperlyQuoted(s)) ai_postTriple(s, "unquotes to:", unquote(s)); } 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; } static String getInnerMessage(Throwable e) { if (e == null) return null; return getInnerException(e).getMessage(); } static void ai_onNewOrRemovedWeb(Object onNewWeb, Object onRemovedWeb) { ai_onNewWeb(onNewWeb); if (onRemovedWeb != null) onTransientWebRemoved_do(onRemovedWeb); } static List sortedByFieldDesc(Collection c, String field) { return reversedList(sortedByField(c, field)); } static List sortedByFieldDesc(String field, Collection c) { return sortedByFieldDesc(c, field); } static String _userHome; static String userHome() { if (_userHome == null) { if (isAndroid()) _userHome = "/storage/sdcard0/"; else _userHome = System.getProperty("user.home"); //System.out.println("userHome: " + _userHome); } return _userHome; } static File userHome(String path) { return new File(userDir(), path); } static IterableIterator toLines(File f) { return linesFromFile(f); } 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; } 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; } static Map hippoSingulars() { return pairsToMap((List>) scanStructureLog("#1011041", "singulars")); } static String baseClassName(String className) { return substring(className, className.lastIndexOf('.')+1); } static String baseClassName(Object o) { return baseClassName(getClassName(o)); } static Set ai_questionWord_1_list = littreeset("what", "who", "why", "when", "where", "how"); static boolean ai_questionWord_1(String s) { return ai_questionWord_1_list.contains(lower(s)); } static List allToString(Collection c) { List l = new ArrayList(); for (Object o : c) l.add(str(o)); return l; } static String[] allToString(Object[] c) { String[] l = new String[l(c)]; for (int i = 0; i < l(c); i++) l[i] = str(c[i]); return l; } static List codeTokens(List tok) { return codeTokensOnly(tok); } static boolean containsAll(Collection a, Collection b) { for (A o : b) if (!a.contains(o)) return false; return true; } static int ai_endOfNounPhrase(List tok) { int i = 1; while (i < l(tok)) { String t = get(tok, i); 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; } 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); } static void ai_spec_possiblyIs(String s) { List l = javaTokC(s); if (l(l) == 3 && allContainCharacters(l)) ai_postTriple(s, "possibly is", first(l) + " " + last(l)); } static void ai_spec_numbered(String s) { List l = javaTokC(s); if (l(l) > 1 && isInteger(last(l))) ai_postTriple(s, "probably is", a(dropLastJavaToken(s))); } static boolean ai_isYesNoQuestion_1(String s) { return ai_yesNoQuestionWord_1(firstWord(s)); } static MultiMap orderedMultiMap() { MultiMap mm = new MultiMap(); mm.data = new LinkedHashMap(); return mm; } static NavigableMap prefixSubMap(NavigableMap map, String prefix) { return map.subMap(prefix, true, prefix + Character.MAX_VALUE, true); } static List nodesToWebs(Collection nodes) { Set webs = litorderedset(); for (WebNode node : nodes) webs.add(node.web); return asList(webs); } static LinkedHashMap litorderedmap(Object... x) { LinkedHashMap map = new LinkedHashMap(); litmap_impl(map, x); return map; } static Object pcallF(Object f, Object... args) { return pcallFunction(f, args); } static B pcallF(F1 f, A a) { try { return f == null ? null : f.get(a); } catch (Throwable __e) { return null; } } static List web_collectForwardRelations(Collection nodes) { List l = new ArrayList(); for (WebNode n : unnull(nodes)) l.addAll(web_forwardRelations(n)); return l; } static String tag(String tag) { return htag(tag); } static String tag(String tag, Object contents, Object... params) { return htag(tag, str(contents), params); } static String tag(String tag, StringBuilder contents, Object... params) { return htag(tag, contents, params); } static String tag(String tag, StringBuffer contents, Object... params) { return htag(tag, contents, params); } static boolean isAbsoluteURL(String s) { return contains(s, "://"); } static List splitAtSimpleArrow(String s) { List tok = javaTok(s); 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(join(tok.subList(i, j))); i = j+3; } return l; } // extended over Class.isInstance() to handle primitive types 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); } static boolean isAdjective(String s) { return contains(knownAdjectives(), lower(s)); } 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; } static List ai_withoutInvalidNodes(List l) { return (List) ai_withoutInvalidNodes((Collection) l); } 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; } static List ai_withoutInvalidWebs(List l) { return (List) ai_withoutInvalidWebs((Collection) l); } static String ai_html_webLink(String globalID) { return "http://botcompany.de/" + globalID; } 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; } static Object[] mapToParams(Map map) { return mapToObjectArray(map); } static List ai_fewestIndexedWebs(String... l) { return ai_withoutInvalidWebs(nodesToWebs(shortestList(map("indexedNodes_withInvalids",l)))); } static String getClassName(Object o) { return o == null ? "null" : o instanceof Class ? ((Class) o).getName() : o.getClass().getName(); } static List indexedVerifiedWebs(String s) { return web_verifiedWebsOnly(indexedWebs(s)); } static Object callOpt(Object o) { if (o == null) return null; return callF(o); } 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 m.invoke(null, args); } else { Method m = callOpt_findMethod(o, method, args, false); if (m == null) return null; m.setAccessible(true); return m.invoke(o, args); } } catch (Exception e) { //fail(e.getMessage() + " | Method: " + method + ", receiver: " + className(o) + ", args: (" + join(", ", map(f className, args) + ")"); throw new RuntimeException(e); } } 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; } 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; } private 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; } static Collection values(Map map) { return map == null ? emptyList() : map.values(); } static Collection values(MultiMap mm) { return mm == null ? emptyList() : concatLists(values(mm.data)); } static void callMainAsChild(Object child, String... args) { moveThisThreadToChild(child); callMain(child, args); } static long ai_numberOfVirtualWebs() { long n = 0; try { for (VirtualNodeIndex i : virtualNodeIndices()) try { n += i.numWebs(); } catch (Throwable __e) { printStackTrace2(__e); }} catch (Throwable __e) { printStackTrace2(__e); } return n; } 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; } static Object callAndMake(String functionName, Object... args) { return makeAndCall_cached(functionName, args); } static boolean neqicOneOf(String s, String... l) { return !eqicOneOf(s, l); } static Object[] toObjectArray(Collection c) { List l = asList(c); return l.toArray(new Object[l.size()]); } static boolean preferCached = false; static boolean loadSnippet_debug = false; static ThreadLocal loadSnippet_silent = new ThreadLocal(); static String loadSnippet(String snippetID) { try { return loadSnippet(parseSnippetID(snippetID), preferCached); } catch (IOException e) { throw new RuntimeException(e); } } 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 (IOException e) { throw new RuntimeException(e); } } public static String loadSnippet(long snippetID, boolean preferCached) throws IOException { String text; // boss bot disabled for now for shorter transpilations /*text = getSnippetFromBossBot(snippetID); if (text != null) return 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 = isTrue(loadSnippet_silent.get()) ? loadPageSilently(url) : loadPage(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; } 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); } private 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()); } static void readLocally(String progID, String varNames) { readLocally2(mc(), progID, varNames); } static void readLocally(String varNames) { readLocally2(mc(), programID(), varNames); } static void readLocally2(Object obj, String varNames) { readLocally2(obj, programID(), varNames); } static int readLocally_stringLength; static ThreadLocal readLocally2_allDynamic = new ThreadLocal(); // read a string variable from standard storage // does not overwrite variable contents if there is no file 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; //value = loadGZTextFile(structureGZFile); InputStream fis = new FileInputStream(structureGZFile); try { GZIPInputStream gis = new GZIPInputStream(fis); InputStreamReader reader = new InputStreamReader(gis, "UTF-8"); BufferedReader bufferedReader = new BufferedReader(reader); //O o = unstructure_reader(bufferedReader); 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); } } static void readLocally_set(Object c, String varName, Object value) { Object oldValue = get(c, varName); if (oldValue instanceof List && !(oldValue instanceof ArrayList) && value != null) { // Assume it's a synchroList. value = synchroList((List) value); } set(c, varName, value); } static Class hotwire(String src) { assertFalse(_inCore()); Class j = getJavaX(); if (isAndroid()) { synchronized(j) { // hopefully this goes well... 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 { // ret hotwire_overInternalBot(src); Class c = (Class) ( call(j, "hotwire", src)); hotwire_copyOver(c); return c; } } static String trimJoinSubList(List l, int i, int j) { return trim(join(subList(l, i, j))); } static String trimJoinSubList(List l, int i) { return trim(join(subList(l, i))); } static boolean stefansPC() { return eq(computerID(), "tvejysmllsmz"); } public static String loadTextFile(String fileName) { return loadTextFile(fileName, null); } public static String loadTextFile(File fileName, String defaultContents) { ping(); try { if (fileName == null || !fileName.exists()) return defaultContents; FileInputStream fileInputStream = new FileInputStream(fileName); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); return loadTextFile(inputStreamReader); } catch (IOException e) { throw new RuntimeException(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 builder.toString(); } static String formatWithThousandsSeparator(long l) { return NumberFormat.getInstance(new Locale("en_US")).format(l); } 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); } static double nodeMatch_dollarVars(WebNode a, WebNode b) { if (a.text().startsWith("$")) return b.text().startsWith("$") ? -1 : 0; return containsAll(web_ucTexts(a), web_ucTexts(b)) ? 0 : -1; } static int randomID_defaultLength = 12; static String randomID(int length) { return makeRandomID(length); } static String randomID() { return randomID(randomID_defaultLength); } static long round(double d) { return Math.round(d); } static String verbToPast(String s) { { String _a_1204 = verbToPastMap().get(lower(s)); if (!empty(_a_1204)) return _a_1204; } return s + "s"; } 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(); } } static Map htmlParams(String tag) { return getHtmlTagParameters(tag); } static String ai_fix(String s) { s = trim(s); if (startsWith(s, "{") && endsWith(s, "}")) s = trim(dropFirstAndLast(s)); return s; } static List web_ucTexts(WebNode node) { return allToUpper(web_texts(node)); } static List collectMethod(Collection c, String methodName) { return mapMethod(c, methodName); } static List Collection(String methodName, Collection c) { return mapMethod(c, methodName); } 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') { // ok } else return false; } return true; } static boolean infoMessage_alwaysOnTop = true; static double infoMessage_defaultTime = 5.0; // automatically switches to AWT thread for you static JWindow infoMessage(String text) { return infoMessage(text, infoMessage_defaultTime); } static JWindow infoMessage(final String text, final double seconds) { print(text); return infoMessage_noprint(text, seconds); } static JWindow infoMessage_noprint(String text) { return infoMessage_noprint(text, infoMessage_defaultTime); } static JWindow infoMessage_noprint(final String text, final double seconds) { if (isHeadless()) return null; return (JWindow) swingAndWait(new Object() { Object get() { try { JTextArea ta = wrappedTextArea(text); 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); final JWindow window = showWindow(withMargin(sp)); window.setSize(300, 150); moveToTopRightCorner(window); onClick(ta, new Runnable() { public void run() { try { window.dispose() ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "window.dispose()"; }}); 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 "JTextArea ta = wrappedTextArea(text);\r\n int size = 14;\r\n if (l(text) <=..."; }}); } static JWindow infoMessage(Throwable e) { showConsole(); printStackTrace(e); return infoMessage(str(e)); } static TreeSet littreeset(A... items) { TreeSet set = new TreeSet(); for (A a : items) set.add(a); return set; } static int toInt_checked(long l) { if (l != (int) l) throw fail("Too large for int: " + l); return (int) l; } static FileStatus conceptsFileStatus(String progID) { return fileStatus(conceptsFile(progID)); } static String replacePrefix(String prefix, String replacement, String s) { if (!startsWith(s, prefix)) return s; return replacement + substring(s, l(prefix)); } static Lock saveLock_lock = fairLock(); static Lock saveLock() { return saveLock_lock; } static Object cget(Object c, String field) { Object o = getOpt(c, field); if (o instanceof Concept.Ref) return ((Concept.Ref) o).get(); return o; } 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); } } 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) { //printShortException(e); } } static void copyList(Collection a, Collection b) { if (a == null || b == null) return; b.clear(); b.addAll(a); } static Object swing(Object f) { return swingAndWait(f); } static A swing(F0 f) { return (A) swingAndWait(f); } static A findConceptWhere(Class c, Object... params) { return findConceptWhere(mainConcepts, c, params); } static A findConceptWhere(Concepts concepts, Class c, Object... params) { params = expandParams(c, params); // indexed 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; } } // table scan for (A x : concepts.list(c)) if (checkConceptFields(x, params)) return x; return null; } static Concept findConceptWhere(Concepts concepts, String c, Object... params) { for (Concept x : concepts.list(c)) if (checkConceptFields(x, params)) return x; return null; } /** possibly improvable */ public static String winQuote(String text) { if (text == null) return null; return "\"" + text .replace("\\", "\\\\") .replace("\"", "\\\"") .replace("\n", "\\n") .replace("\r", "\\r") + "\""; } static String winQuote(File f) { return winQuote(f.getAbsolutePath()); } // match2 matches multiple "*" (matches a single token) wildcards and zero or one "..." wildcards (matches multiple tokens) static String[] match2(List pat, List tok) { // standard case (no ...) int i = pat.indexOf("..."); if (i < 0) return match2_match(pat, tok); pat = new ArrayList(pat); // We're modifying it, so copy first pat.set(i, "*"); while (pat.size() < tok.size()) { pat.add(i, "*"); pat.add(i+1, ""); // doesn't matter } return match2_match(pat, tok); } 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))) // bold change - match quoted and unquoted now return null; } return result.toArray(new String[result.size()]); } static File prepareProgramFile(String name) { return mkdirsForFile(getProgramFile(name)); } static File prepareProgramFile(String progID, String name) { return mkdirsForFile(getProgramFile(progID, name)); } static void ai_addWebToIndex(MultiMap index, Web web) { if (web != null) for (WebNode node : web_nodesAndRelations(web)) for (String text : asSet(node.texts())) { String key = ai_shortenForIndex(text); boolean newText = !contains(web_fullTextSet(index.get(key)), text); index.put(key, node); if (newText) ai_onNewIndexedText(text); } } static void cancelAndInterruptThread(Thread t) { if (t == null) return; cancelThread(t); t.interrupt(); } static Either either2(B b) { return new Either(2, b); } static Either either1(A a) { return new Either(1, a); } static String rep(int n, char c) { return repeat(c, n); } static String rep(char c, int n) { return repeat(c, n); } static List rep(A a, int n) { return repeat(a, n); } static List rep(int n, A a) { return repeat(n, a); } static boolean isNonNegativeInteger(String s) { return s != null && Pattern.matches("\\d+", s); } static File getCodeProgramDir() { return getCodeProgramDir(getProgramID()); } static File getCodeProgramDir(String snippetID) { return new File(javaxCodeDir(), formatSnippetID(snippetID)); } static File getCodeProgramDir(long snippetID) { return getCodeProgramDir(formatSnippetID(snippetID)); } static URLConnection openConnection(URL url) { try { ping(); callOpt(javax(), "recordOpenURLConnection", str(url)); return url.openConnection(); } catch (Exception __e) { throw rethrow(__e); } } static WebNode cloneWeb_handlingNode(WebNode node) { return cloneWebWithMap(node.web).b.get(node); } /** writes safely (to temp file, then rename) */ static File saveTextFile(String fileName, String contents) throws IOException { CriticalAction action = beginCriticalAction("Saving file " + fileName + " (" + l(contents) + " chars)"); try { File file = new File(fileName); File parentFile = file.getParentFile(); if (parentFile != null) parentFile.mkdirs(); 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(); } } static File saveTextFile(File fileName, String contents) { try { saveTextFile(fileName.getPath(), contents); return fileName; } catch (Exception __e) { throw rethrow(__e); } } // This is made for NL parsing. // It's javaTok extended with "..." token, "$n" and "#n" and // special quotes (which are converted to normal ones). 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; // scan for whitespace 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)); // scan for non-whitespace if (c == '\u201C' || c == '\u201D') c = '"'; // normalize quotes if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { char _c = s.charAt(j); if (_c == '\u201C' || _c == '\u201D') _c = '"'; // normalize quotes 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) == '\'')); // for things like "this one's" 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; } 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; } static void web_printRelations(Web web) { List> relations = web_nonEmptyRelations(web); if (nempty(relations)) { for (Pair p : relations) { WebNode rel = web.relations.get(p); String lbl = rel.text(); if (l(rel.labels) > 1) lbl += "+"; printIndent(p.a.text() + " -> " + lbl + " -> " + p.b.text()); } print(); } } static AtomicInteger dialogServer_clients = new AtomicInteger(); static boolean dialogServer_printConnects; static ThreadLocal startDialogServer_quiet = new ThreadLocal(); static Set dialogServer_knownClients = synchroTreeSet(); static int startDialogServerOnPortAbove(int port, DialogHandler handler) { while (!forbiddenPort(port) && !startDialogServerIfPortAvailable(port, handler)) ++port; return port; } static int startDialogServerOnPortAboveDaemon(int port, DialogHandler handler) { while (!forbiddenPort(port) && !startDialogServerIfPortAvailable(port, handler, true)) ++port; return port; } static void startDialogServer(int port, DialogHandler handler) { if (!startDialogServerIfPortAvailable(port, handler)) throw fail("Can't start dialog server on port " + port); } static boolean startDialogServerIfPortAvailable(int port, final DialogHandler handler) { return startDialogServerIfPortAvailable(port, handler, false); } static ServerSocket startDialogServer_serverSocket; static boolean startDialogServerIfPortAvailable(int port, final DialogHandler handler, boolean daemon) { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(port); } catch (IOException e) { // probably the port number is used - let's assume there already is a chat server. 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() { // This should be the same as #1001076 (talkTo) boolean isLocalConnection() { return s.getInetAddress().isLoopbackAddress(); } boolean isStillConnected() { return !(eos || s.isClosed()); } void sendLine(String line) { try { w.write(line + "\n"); w.flush(); } catch (Exception __e) { throw rethrow(__e); } } String readLineImpl() { try { return in.readLine(); } catch (Exception __e) { throw rethrow(__e); } } void close() { try { s.close(); } catch (IOException e) { // whatever } } Socket getSocket() { return s; } }; try { handler.run(io); } finally { s.close(); } } catch (IOException e) { print("[internal] " + e); } finally { //print("client disconnect - " + dialogServer_clients.decrementAndGet() + " remaining"); } } }; // Thread t2 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; } static void pnlIndent(Collection l) { print_noNewLine(indentx(numberedLines(l))); } static List web_nonRelationNodes(Web web) { return listMinusSet(web.nodes, values(web.relations)); } 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); } static Lisp lisp_intern(Lisp l) { if (l == null) return null; return lisp(intern(l.head), map("lisp_intern",l.args)); } static Producer javaTokC_noMLS_onReader(final BufferedReader r) { final class X implements Producer { StringBuilder buf = new StringBuilder(); // stores from "i" char c, d, e = 'x'; // just not '\0' X() { // fill c, d and e nc(); nc(); nc(); } // get next character(s) into c, d and e void nc() { try { c = d; d = e; if (e == '\0') return; int i = r.read(); e = i < 0 ? '\0' : (char) i; } catch (Exception __e) { throw rethrow(__e); } } void ncSave() { if (c != '\0') { buf.append(c); nc(); } } public String next() { // scan for whitespace 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; // scan for non-whitespace if (c == '\'' || c == '"') { char opener = c; ncSave(); while (c != '\0') { if (c == opener || c == '\n') { // end at \n to not propagate unclosed string literal errors ncSave(); break; } else if (c == '\\') { ncSave(); ncSave(); } else ncSave(); } } else if (Character.isJavaIdentifierStart(c)) do ncSave(); while (Character.isJavaIdentifierPart(c) || c == '\''); // for stuff like "don't" else if (Character.isDigit(c)) { do ncSave(); while (Character.isDigit(c)); if (c == 'L') ncSave(); // Long constants like 1L } else ncSave(); String t = buf.toString(); buf.setLength(0); return t; } } return new X(); } static int shorten_default = 100; static String shorten(String s) { return shorten(s, shorten_default); } static String shorten(String s, int max) { if (s == null) return ""; if (max < 0) return s; return s.length() <= max ? s : substring(s, 0, min(s.length(), max-3)) + "..."; } static String shorten(int max, String s) { return shorten(s, max); } 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); } static void web_setLabels_forced(WebNode node, List labels) { web_deindex(node); node.labels = cloneList(labels); web_index(node); } static long toM(long l) { return (l+1024*1024-1)/(1024*1024); } static String toM(long l, int digits) { return formatDouble(toM_double(l), digits); } static String htmlQuote(String s) { return "\"" + htmlencode(s) + "\""; } static boolean _inCore() { return false; } static String formatGMT_24(long time) { SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); sdf.setTimeZone(TimeZone.getTimeZone("GMT")); return sdf.format(time) + " GMT"; } static Web web_replaceDollarVars(Web web, final Map map) { for (WebNode node : web.nodes) web_modifyTexts(node, new Object() { Object get(String s) { try { return formatDollarVars(s, map) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "formatDollarVars(s, map)"; }}); return web; } static String parse3_cached_s; static List parse3_cached_l; 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; } static String ai1_visitorsInfo() { return loadPageSilently("http://ai1.lol/1002157/raw"); } static boolean isIdentifier(String s) { return isJavaIdentifier(s); } static boolean web_hasLabels(WebNode n) { return n.hasLabels(); } static ArrayList toList(A[] a) { return asList(a); } static ArrayList toList(int[] a) { return asList(a); } static ArrayList toList(Set s) { return asList(s); } static ArrayList toList(Iterable s) { return asList(s); } static boolean eqOneOf(Object o, Object... l) { for (Object x : l) if (eq(o, x)) return true; return false; } static boolean containsWord(String s, String word) { return cic(nlTok3(s), word); } static String emptyToNull(String s) { return eq(s, "") ? null : s; } static String javaTokWordWrap(String s) { int cols = 120, 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 += "\n"); int idx = t.lastIndexOf('\n'); if (idx >= 0) col = l(t)-(idx+1); else col += l(t); } return join(tok); } static void mapRemoveAll(Map map, A... keys) { if (map != null) for (A a : keys) map.remove(a); } static String dbBotStandardName() { return dbBotName(getDBProgramID()) + "."; } static String joinNempties(String sep, String... strings) { return joinStrings(sep, strings); } static String joinNempties(String sep, Collection strings) { return joinStrings(sep, strings); } static List web_forwardRelated(WebNode node) { return web_allForwardRelated(node); } static String tb_mainServer_default = "http://tinybrain.de:8080"; static Object tb_mainServer_override; // func -> S static String tb_mainServer() { if (tb_mainServer_override != null) return (String) callF(tb_mainServer_override); return trim(loadTextFile(tb_mainServer_file(), tb_mainServer_default)); } static File tb_mainServer_file() { return getProgramFile("#1001638", "mainserver.txt"); } static boolean tb_mainServer_isDefault() { return eq(tb_mainServer(), tb_mainServer_default); } static int stdcompare(Number a, Number b) { return cmp(a, b); } static int stdcompare(String a, String b) { return cmp(a, b); } static int stdcompare(long a, long b) { return a < b ? -1 : a > b ? 1 : 0; } static int stdcompare(Object a, Object b) { return cmp(a, b); } static final HashMap> callMC_cache = new HashMap(); static String callMC_key; static Method callMC_value; // varargs assignment fixer for a single string array argument static Object callMC(String method, String[] arg) { return callMC(method, new Object[] {arg}); } static Object callMC(String method, Object... args) { try { Method me; synchronized(callMC_cache) { me = method == callMC_key ? callMC_value : null; } if (me != null) return callMC_value.invoke(null, args); 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 me.invoke(null, args); } catch (IllegalArgumentException e) { throw new RuntimeException("Can't call " + method + " with " + l(args) + " argument(s)", e); } } for (int i = 0; i < n; i++) { me = m.get(i); if (call_checkArgs(me, args, false)) return me.invoke(null, args); } throw fail("No method called " + method + " with matching arguments found in main"); } catch (Exception __e) { throw rethrow(__e); } } 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(); } } } 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)); } 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); } static List web_quotedNodesOnly(Collection l) { return filter(l, "web_isQuotedNode"); } static double toDouble(Object o) { if (o instanceof Number) return ((Number) o).doubleValue(); if (o instanceof BigInteger) return ((BigInteger) o).doubleValue(); throw fail(o); } static List dropFirstAndLast(int n, List l) { return new ArrayList(subList(l, n, l(l)-n)); } static String dropFirstAndLast(String s) { return substring(s, 1, l(s)-1); } static void removeFromMultiPort(long vport) { if (vport == 0) return; for (Object port : getMultiPorts()) call(port, "removePort", vport); } static String ai_textOfChatLine(String line) { if (!matchStart("chat line", line)) return null; return unquote(firstQuoted(web_texts(ai_search_dollarX_verified(selectedWord, "is", "$X")))); } static MultiMap dwlw_fullNodeIndex_ci() { return ai_indexWebs(allWebs()); } static String ai_html_encyclopediaTopic(WebNode n) { String topic = web_text(n); return topic == null ? "" : html_encyclopediaTopic(topic); } 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(); } 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; } static String quoteReplacement(String s) { return Matcher.quoteReplacement(s); } static String getInjectionID() { return (String) call(getJavaX(), "getInjectionID", getMainClass()); } static long unixTime() { return now()/1000; } static WebRelation web_findRelationWithLabel(Web web, String s) { for (WebRelation r : web_relationObjects(web)) if (web_matchNodeName(r, s)) return r; return null; } static A firstKey(Map map) { return first(keys(map)); } static String beautifyStructure(String s) { return structure_addTokenMarkers(s); } static Set synchroSet() { return synchroHashSet(); } static Set synchroSet(Set set) { return Collections.synchronizedSet(set); } static HashMap, Class> loadFunctions_cache = new HashMap(); static Lock loadFunctions_cached_lock = lock(); static Class loadFunctions_cached(String... functions) { return loadFunctions_cached(asList(functions)); } static Class loadFunctions_cached(List functions) { Set functionSet = new TreeSet(functions); Lock _lock_1365 = loadFunctions_cached_lock; lock(_lock_1365); try { Class c = loadFunctions_cache.get(functionSet); if (c == null) { loadFunctions_cache.put(functionSet, c = loadFunctions(functionSet)); assertNotNull(loadFunctions_cache.get(functionSet)); } return c; } finally { unlock(_lock_1365); } } 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 : keys(web.relations)) if (p.a == node) { WebNode rel = web.relations.get(p); if (web_hasOneOfTheLabels(rel, labels)) return p.b; } return null; } static boolean isAdverb(String s) { return containsNotNull(knownAdverbs(), s); } static String htitle(String title) { return tag("title", htmlencode(title)); } static Map safeUnstructMap(String s) { return safeUnstructureMap(s); } static String formatDate() { return formatDate(now()); } static String formatDate(long timestamp) { return timestamp == 0 ? "-" : str(new Date(timestamp)); } static boolean allWebs_transientOnly; static List allWebs_fullList() { if (allWebs_transientOnly) return transientWebs(); return concatLists(transientWebs(), downloadedWebs(), localSoftwareMadeWebs(), localUserWebEdits(), localWebs()); } static URLConnection setURLConnectionTimeouts(URLConnection con, int timeout) { con.setConnectTimeout(timeout); con.setReadTimeout(timeout); if (con.getConnectTimeout() != timeout || con.getReadTimeout() != timeout) print("Warning: Timeouts not set by JDK."); return con; } 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; } static boolean getHtmlTagParameters_debug; 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; } static String joinSubList(List l, int i, int j) { return join(subList(l, i, j)); } static String joinSubList(List l, int i) { return join(subList(l, i)); } static String dottedStyleForLinks() { return dottedStyle() + "; text-decoration: none"; } 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) == '['; } 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; } static Cache> nounPhraseContinuers_cache = new Cache("nounPhraseContinuers_load"); static Set nounPhraseContinuers() { return nounPhraseContinuers_cache.get(); } static Set nounPhraseContinuers_load() { return new TreeSet(tlft(loadSnippet("#1011080"))); } static List> web_nonEmptyRelations(Web web) { List> l = new ArrayList(); for (Pair r : keys(web.relations)) if (nempty(web.relations.get(r).labels())) l.add(r); return l; } static boolean web_hasText(WebNode node, String text) { return web_hasLabelIC(node, text); } static boolean web_hasText(Pair p, String text) { return web_hasLabelIC(p, text); } // test relation static boolean web_hasText(WebNode a, WebNode b, String text) { return web_hasLabelIC(a, b, text); } static Object pcallFunction(Object f, Object... args) { try { return callFunction(f, args); } catch (Throwable __e) { printStackTrace2(__e); } return null; } // works on lists and strings and null static int indexOfIgnoreCase(Object a, Object b) { if (a == null) return -1; if (a instanceof String) { Matcher m = Pattern.compile((String) b, Pattern.CASE_INSENSITIVE + Pattern.LITERAL).matcher((String) a); if (m.find()) return m.start(); else return -1; } if (a instanceof List) { for (int i = 0; i < ((List) a).size(); i++) { Object o = ((List) a).get(i); if (o != null && ((String) o).equalsIgnoreCase((String) b)) return i; } return -1; } throw fail("Unknown type: " + a); } // Try to get the quoting right... 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); } static String smartJoin(List args) { return smartJoin(toStringArray(args)); } static final boolean loadPageThroughProxy_enabled = false; static String loadPageThroughProxy(String url) { return null; } static void hotwire_copyOver(Class c) { synchronized(StringBuffer.class) { for (String field : litlist("print_log", "print_silent", "androidContext")) { 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())); } } static void moveThisThreadToChild(Object child) { Thread t = currentThread(); callOpt(child, "_registerThread", t); _unregisterThread(t); } static File loadLibrary(String snippetID) { return loadBinarySnippet(snippetID); } 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; } static String format(String pat, Object... args) { return format3(pat, args); } static boolean ai_isInvalidWeb(Web web) { return ai_invalidatedWebs().contains(web.globalID); } static List sortedByField(Collection c, final String field) { List l = new ArrayList(c); sort(l, new Comparator() { public int compare(A a, A b) { return cmp(getOpt(a, field), getOpt(b, field)); } }); return l; } static List sortedByField(String field, Collection c) { return sortedByField(c, field); } static boolean isLetter(char c) { return Character.isLetter(c); } 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; } static boolean containsNulls(Collection c) { return contains(c, null); } static List uniquifyList(Collection l) { HashSet set = new HashSet(); List out = new ArrayList(); for (A a : unnull(l)) if (set.add(a)) out.add(a); return out; } static String formatSnippetIDOpt(String s) { return isSnippetID(s) ? formatSnippetID(s) : s; } static String processID_cached; // try to get our current process ID static String getPID() { if (processID_cached == null) { String name = ManagementFactory.getRuntimeMXBean().getName(); processID_cached = name.replaceAll("@.*", ""); } return processID_cached; } static void setAll(Object o, Map fields) { if (fields == null) return; for (String field : keys(fields)) set(o, field, fields.get(field)); } static void setAll(Object o, Object... values) { //values = expandParams(c.getClass(), 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); } } static String[] conceptFields_drop = {"className", "fieldValues", "id", "created", "refs", "backRefs", "_concepts"}; static Set conceptFields(Concept c) { return setMinus(mergeSets(allNonStaticFields(c), keys(c.fieldValues)), conceptFields_drop); } static Set conceptFields(Class c) { return setMinus(allNonStaticFields(c), conceptFields_drop); } static Cache> knownAdjectives_cache = new Cache("knownAdjectives_load"); static Set knownAdjectives() { return knownAdjectives_cache.get(); } static Set knownAdjectives_load() { return new TreeSet(tlft(loadSnippet("#1011077"))); } static A setThreadLocal(ThreadLocal tl, A value) { A old = tl.get(); tl.set(value); return old; } static void ai_onNewWeb(Object onNewWeb) { if (onNewWeb == null) return; setAdd(postSoftwareMadeWeb_onNewWeb, onNewWeb); onTransientWebAdded_do(onNewWeb); } static List record_list = synchroList(); static void record(Object o) { record_list.add(o); } public static boolean isWindows() { return System.getProperty("os.name").contains("Windows"); } /** possibly improvable */ static String bashQuote(String text) { if (text == null) return null; return "\"" + text .replace("\\", "\\\\") .replace("\"", "\\\"") .replace("\n", "\\n") .replace("\r", "\\r") + "\""; } static String bashQuote(File f) { return bashQuote(f.getAbsolutePath()); } 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); } static Cache>> english1000VerbQuintuples_cache = new Cache("english1000VerbQuintuples_load"); static List> english1000VerbQuintuples() { return english1000VerbQuintuples_cache.get(); } static List> english1000VerbQuintuples_load() { List < List < String > > out = new ArrayList(); for (String s : tlft_j(loadSnippet("#1010402"))) { List l = splitAtSpace(s); if (l(l) != 6) { /*print("e1vq huh? " + s);*/ } else { l = dropFirst(l); out.add(l); } } return out; } static Map caseInsensitiveSynchroMap() { return (Map) synchroMap(caseInsensitiveMap()); } static boolean isProperlyQuoted(String s) { return s.length() >= 2 && s.startsWith("\"") && s.endsWith("\"") && (!s.endsWith("\\\"") || s.endsWith("\\\\\"")); } static String dropSuffixIgnoreCase(String suffix, String s) { return ewic(s, suffix) ? s.substring(0, l(s)-l(suffix)) : s; } static void pcallF_all(Collection l, Object... args) { if (l != null) for (Object f : l) pcallF(f, args); } static List> allPermutations(List l) { List < List < A > > out = new ArrayList(); allPermutations_impl(cloneList(l), l(l), out); return out; } 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); } } static void copyFields(Object x, Object y, String... fields) { if (empty(fields)) { // assume we should copy all 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); } } static NavigableSet prefixSubSet(NavigableSet set, String prefix) { return set.subSet(prefix, true, prefix + Character.MAX_VALUE, true); } 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); smartSet(f, o, value); } catch (Exception e) { throw new RuntimeException(e); } } static void set(Class c, String field, Object value) { try { Field f = set_findStaticField(c, field); smartSet(f, null, value); } catch (Exception e) { throw new RuntimeException(e); } } 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()); } 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()); } static Map findBot_cache = synchroHashMap(); static int findBot_timeout = 5000; static DialogIO findBot(String searchPattern) { // first split off sub-bot suffix 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 + "."; } // assume it's a port if it's an integer 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(/*findBot_timeout*/); // TODO: implement String line = io.readLineNoBlock(); if (indexOfIgnoreCase(line, searchPattern) == 0) { call(io, "pushback", line); // put hello string back in return talkToSubBot(subBot, io); } } catch (Exception e) { e.printStackTrace(); } List bots = quickBotScan(); // find top-level bots for (ProgramScan.Program p : bots) { if (indexOfIgnoreCase(p.helloString, searchPattern) == 0) { // strict matching - start of hello string only, but case-insensitive findBot_cache.put(searchPattern, p.port); return talkToSubBot(subBot, talkTo("localhost", p.port)); } } // find sub-bots 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; } // tok should be the output of htmlcoarsetok 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; } // alternatively, call this convenient function static String dropAllTags(String html) { return join(dropAllTags(htmlcoarsetok(html))); } static A getAndClearThreadLocal(ThreadLocal tl) { A a = tl.get(); tl.set(null); return a; } static F1 f1(Object f) { return functionToF1(f); } static boolean isOpeningTag(String token, String tag) { return isTag(token, tag) && !token.endsWith("/>"); } static boolean isOpeningTag(String token) { return token.startsWith("<") && token.endsWith(">") && !token.endsWith("/>") && isLetter(token.charAt(1)); } static byte[] loadBinaryFromZip(File inZip, String fileName) { try { ZipFile zip = new ZipFile(inZip); try { ZipEntry entry = zip.getEntry(fileName); if (entry == null) //fail("Entry " + fileName + " not found in zip file: " + inZip.getAbsolutePath()); 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); } } static boolean showAnimationInTopRightCorner_alwaysOnTop = true; // automatically switches to AWT thread for you // text is optional text below image static JWindow showAnimationInTopRightCorner(String imageID, String text) { if (isHeadless()) return null; return showAnimationInTopRightCorner(imageIcon(imageID), text); } static JWindow showAnimationInTopRightCorner(final BufferedImage image, final String text) { if (isHeadless()) return null; return showAnimationInTopRightCorner(imageIcon(image), text); } static JWindow showAnimationInTopRightCorner(final ImageIcon imageIcon, final String text) { if (isHeadless()) return null; return (JWindow) swingAndWait(new Object() { 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..."; }}); } static JWindow showAnimationInTopRightCorner(final String imageID) { return showAnimationInTopRightCorner(imageID, ""); } static JWindow showAnimationInTopRightCorner(String imageID, double seconds) { return showAnimationInTopRightCorner(imageID, "", seconds); } static JWindow showAnimationInTopRightCorner(String imageID, String text, double seconds) { if (isHeadless()) return null; return disposeWindowAfter(iround(seconds*1000), showAnimationInTopRightCorner(imageID, text)); } static JWindow showAnimationInTopRightCorner(BufferedImage img, String text, double seconds) { return disposeWindowAfter(iround(seconds*1000), showAnimationInTopRightCorner(img, text)); } static Class javax() { return getJavaX(); } // f: func(key, value) -> entry static List mapMapToList(Map map, Object f) { return map(map, f); } static List mapMapToList(Object f, Map map) { return map(f, map); } static String getProgramTitle() { return getProgramName(); } 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; } static List web_nodesAndRelations(Web web) { return web.nodes; } static HashSet lithashset(A... items) { HashSet set = new HashSet(); for (A a : items) set.add(a); return set; } 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); } static String html_linkURLs(String html) { return html_linkURLs(html, ""); } static String h1(String s, Object... params) { return tag("h1", s, params); } static String h2(String s, Object... params) { return tag("h2", s, params); } static void logQuoted(String logFile, String line) { logQuoted(getProgramFile(logFile), line); } static void logQuoted(File logFile, String line) { appendToFile(logFile, quote(line) + "\n"); } static List shortestList(List> lists) { Lowest < List < A > > best = new Lowest(); for (List l : lists) best.put(l, l(l)); return best.get(); } static Comparator mapComparatorDesc(final Map map) { return new Comparator() { public int compare(A a, A b) { return cmp(map.get(b), map.get(a)); } }; } static boolean containsIgnoreCase(Collection l, String s) { for (String x : l) if (eqic(x, s)) return true; return false; } static boolean containsIgnoreCase(String[] l, String s) { for (String x : l) if (eqic(x, s)) return true; return false; } static boolean containsIgnoreCase(String s, char c) { return indexOfIgnoreCase(s, String.valueOf(c)) >= 0; } static boolean containsIgnoreCase(String a, String b) { return indexOfIgnoreCase(a, b) >= 0; } static String getString(Map map, Object key) { return map == null ? null : (String) map.get(key); } static String getString(List l, int idx) { return (String) get(l, idx); } 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 byte[] loadBinaryFile(String fileName) { try { if (!new File(fileName).exists()) return null; FileInputStream in = new FileInputStream(fileName); byte buf[] = new byte[1024]; ByteArrayOutputStream out = new ByteArrayOutputStream(); int l; while (true) { l = in.read(buf); if (l <= 0) break; out.write(buf, 0, l); } in.close(); return out.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } } public static byte[] loadBinaryFile(File file) { return loadBinaryFile(file.getPath()); } static Object[] assertEvenLength(Object[] a) { assertTrue(even(l(a))); return a; } 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); } static Runnable _topLevelErrorHandling(final Runnable runnable) { return new Runnable() { public void run() { try { try { runnable.run(); } catch (Throwable __e) { printStackTrace2(__e); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pcall { runnable.run(); }"; }}; } static Web webs_search_noVar(Web searchWeb, Collection webs) { for (Web web : webs) if (web_matchAllPerms(searchWeb, web) != null) return web; return null; } static F1 interceptPrintInThisThread(F1 f) { F1 old = print_byThread().get(); print_byThread().set(f); return old; } static SimpleDateFormat simpleDateFormat_GMT(String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); sdf.setTimeZone(TimeZone.getTimeZone("GMT")); return sdf; } static String encyclopediaLink(String topic) { return smartBot_encyclopediaLink(topic); } // i must point at the (possibly imaginary) opening bracket // index returned is index of closing bracket + 1 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(); } static boolean odd(int i) { return (i & 1) != 0; } static boolean odd(long i) { return (i & 1) != 0; } static boolean isSubtypeOf(Class a, Class b) { return b.isAssignableFrom(a); // << always hated that method, let's replace it! } static int getInt(List c, int i) { return toInt(get(c, i)); } static int getInt(Map map, Object key) { return toInt(mapGet(map, key)); } static int getInt(Object o, Object field) { return toInt(getOpt(o, (String) field)); } static Cache> verbToThirdPersonMap_cache = new Cache("verbToThirdPersonMap_load"); static Map verbToThirdPersonMap() { return verbToThirdPersonMap_cache.get(); } static Map verbToThirdPersonMap_load() { HashMap map = new HashMap(); for (List l : englishVerbQuintuples()) map.put(get(l, 0), get(l, 3)); return map; } static TimerTask timerTask(final Object r, final java.util.Timer timer) { return new TimerTask() { public void run() { if (!licensed()) timer.cancel(); else pcallF(r); } }; } static TimerTask timerTask(final Object r) { return new TimerTask() { public void run() { ping(); pcallF(r); } }; } static String myJavaSource_code; static String myJavaSource() { return myJavaSource_code; } static byte[] toJPEG(BufferedImage img) { try { ByteArrayOutputStream stream = new ByteArrayOutputStream(); ImageIO.write(img, "jpeg", stream); return stream.toByteArray(); } catch (Exception __e) { throw rethrow(__e); } } static byte[] toJPEG(File f) { return toJPEG(loadImage2(f)); } static ArrayList litlist(A... a) { return new ArrayList(Arrays.asList(a)); } static String assertIsIdentifier(String s) { if (!isIdentifier(s)) throw fail("Not an identifier: " + quote(s)); return s; } static List ai_texts(String a, String b, String c) { return web_texts(ai_search_dollarX(a, b, c)); } static boolean ewic(String a, String b) { return endsWithIgnoreCase(a, b); } static int ai_shortenForIndex_maxLength = 100; static String ai_shortenForIndex(String text) { return shorten(text, ai_shortenForIndex_maxLength); } static List cloneAndClearList(List l) { List l2 = cloneList(l); l.clear(); return l2; } static boolean htmlCheckParams(String tag, Object... params) { return checkFields(htmlgetparams(tag), params); } static List dropPunctuationAtEnd_keep = litlist("*", "<", ">"); 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; } static String dropPunctuationAtEnd(String s) { return join(dropPunctuationAtEnd(nlTok(s))); } // TODO: process CDATA? static List htmlcoarsetok(String s) { List tok = new ArrayList(); int l = s.length(); int i = 0; while (i < l) { int j = i; char c; // scan for non-tags while (j < l) { if (s.charAt(j) != '<') // regular character ++j; else if (s.substring(j, Math.min(j+4, l)).equals("")); j = Math.min(j+3, l); } else // it's a tag break; } tok.add(s.substring(i, j)); i = j; if (i >= l) break; c = s.charAt(i); // scan for tags if (c == '<') { ++j; while (j < l && s.charAt(j) != '>') ++j; // TODO: strings? if (j < l) ++j; } tok.add(s.substring(i, j)); i = j; } if ((tok.size() % 2) == 0) tok.add(""); return tok; } 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; } static Lock appendToFile_lock = lock(); static boolean appendToFile_keepOpen; static HashMap appendToFile_writers = new HashMap(); static void appendToFile(String path, String s) { try { Lock _lock_1566 = appendToFile_lock; lock(_lock_1566); try { // Let's just generally synchronize this to be safe. new File(path).getParentFile().mkdirs(); path = getCanonicalPath(path); Writer writer = appendToFile_writers.get(path); if (writer == null) { //print("[Logging to " + path + "]"); 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(_lock_1566); } } catch (Exception __e) { throw rethrow(__e); } } static void appendToFile(File path, String s) { if (path != null) appendToFile(path.getPath(), s); } static void cleanMeUp_appendToFile() { Lock _lock_1567 = appendToFile_lock; lock(_lock_1567); try { closeAllWriters(values(appendToFile_writers)); appendToFile_writers.clear(); } finally { unlock(_lock_1567); } } static boolean publicCommOn() { return "1".equals(loadTextFile(new File(userHome(), ".javax/public-communication"))); } 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; } static boolean startsWithWord(String a, String b) { return startsWithWord(a, b, null); } static Object getThreadLocal(Object o, String name) { ThreadLocal t = (ThreadLocal) ( getOpt(o, name)); return t != null ? t.get() : null; } static A getThreadLocal(ThreadLocal tl) { return tl == null ? null : tl.get(); } static void sort(T[] a, Comparator c) { Arrays.sort(a, c); } static void sort(T[] a) { Arrays.sort(a); } static void sort(List a, Comparator c) { Collections.sort(a, c); } static void sort(List a) { Collections.sort(a); } static void createMarkerFile(String progID, String name) { File f = getProgramFile(progID, name); if (fileLength(f) == 0) saveTextFile(f, "1"); } static Set ai_is_myYourEtc = litciset("a", "my", "your", "their", "his", "our", "the"); static boolean ai_is_myYourEtc(String s) { return containsNotNull(ai_is_myYourEtc, s); } // returns from C to C static String jextract(String pat, String s) { List tok = javaTok(s); 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)); } static WebRelation web_findRelationObject(WebNode a, WebNode b) { return a.web.relations.get(pair(a, b)); } static Map synchroMap() { return synchroHashMap(); } static Map synchroMap(Map map) { return Collections.synchronizedMap(map); } static boolean isNoun(String s) { return isSingularNoun(s) || isPluralNoun(s); } static volatile Android3 dbBot_instance; static Android3 dbBot() { return dbBot(dbBotStandardName()); } static Android3 dbBot(String name) { ensureDBNotRunning(name); assertNotNull(mainConcepts); return dbBot_instance = methodsBot2(name, mainConcepts, exposedDBMethods, mainConcepts.lock); } static Object sleepQuietly_monitor = new Object(); static void sleepQuietly() { try { assertFalse(isAWTThread()); synchronized(sleepQuietly_monitor) { sleepQuietly_monitor.wait(); } } catch (Exception __e) { throw rethrow(__e); } } static Object[] expandParams(Class c, Object[] params) { if (l(params) == 1) params = new Object[] { singleFieldName(c), params[0] }; else warnIfOddCount(params); return params; } static Map matchDollarVarsIC(String pat, String s) { 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; } static boolean nohup_debug; static boolean nohup_noSlashB; 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); } } /** outFile takes stdout and stderr. */ 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); //System.out.println("exit value: " + 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; } static boolean containsNotNull(Collection c, Object o) { return o != null && contains(c, o); } static boolean containsNotNull(Object[] x, Object o) { return o != null && contains(x, o); } public static String rtrim(String s) { 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; } 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; } 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); } static String callStaticAnswerMethod(String s) { return callStaticAnswerMethod(mc(), s); } static String callStaticAnswerMethod(String s, List history) { return callStaticAnswerMethod(mc(), s, history); } 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); } static boolean structure_showTiming, structure_checkTokenCount; static String structure(Object o) { return structure(o, new structure_Data()); } 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; } static void structure_go(Object o, structure_Data d) { structure_1(o, d); while (nempty(d.stack)) popLast(d.stack).run(); } static void structureToPrintWriter(Object o, PrintWriter out) { structure_Data d = new structure_Data(); d.out = out; structure_go(o, d); } // leave to false, unless unstructure() breaks static boolean structure_allowShortening = false; static class structure_Data { PrintWriter out; int stringSizeLimit; int shareStringsLongerThan = 20; boolean noStringSharing; IdentityHashMap seen = new IdentityHashMap(); //new BitSet refd; HashMap strings = new HashMap(); HashSet concepts = new HashSet(); HashMap> fieldsByClass = new HashMap(); int n; // token count List stack = new ArrayList(); // append single token structure_Data append(String token) { out.print(token); ++n; return this; } structure_Data append(int i) { out.print(i); ++n; return this; } // append multiple tokens structure_Data append(String token, int tokCount) { out.print(token); n += tokCount; return this; } // extend last token structure_Data app(String token) { out.print(token); return this; } structure_Data app(int i) { out.print(i); return this; } } 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) { // these are never back-referenced (for readability) 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 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; } // referencable objects follow Integer ref = d.seen.get(o); if (o instanceof String && ref == null) ref = d.strings.get((String) o); if (ref != null) { /*d.refd.set(ref);*/ d.append("t").app(ref); return; } if (!(o instanceof String)) d.seen.put(o, d.n); // record token number 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$") /* && neq(name, "main$Concept$RefL") */) { 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"); d.append("{"); final int l = d.n; final Iterator it = ((Map) o).entrySet().iterator(); d.stack.add(new Runnable() { boolean v; 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[]) { //ret "intarray " + quote(intArrayToHex((int[]) o)); atype = "intarray"; sep = " "; } d.append(atype).append("{"); d.stack.add(new Runnable() { 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; } // Need more cases? This should cover all library classes... if (name.startsWith("java.") || name.startsWith("javax.")) { d.append("j ").append(quote(str(o))); return; // Hm. this is not unstructure-able } 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; } /*if (name.equals("main$Lisp")) { fail("lisp not supported right now"); }*/ String dynName = shortDynamicClassName(o); if (concept && !d.concepts.contains(dynName)) { d.concepts.add(dynName); d.append("c "); } // serialize an object with fields. // first, collect all fields and values in fv. 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); // put special cases here... } cc = cc.getSuperclass(); } lFields = asList(fields); // Render this$1 first because unstructure needs it for constructor call. 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); } // << if (lFields == null) else { // ref handling for lFields != null Integer ref = d.seen.get(o); if (ref != null) { /*d.refd.set(ref);*/ d.append("t").app(ref); return; } d.seen.put(o, d.n); // record token number } 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); // Now we have fields & values. Process fieldValues if it's a DynamicObject. // omit field "className" if equal to class's 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..."; }}); } static boolean neqic(String a, String b) { return !eqic(a, b); } static Map newWeakHashMap() { return _registerWeakMap(synchroMap(new WeakHashMap())); } static Iterator iterator(Collection c) { return c == null ? emptyIterator() : c.iterator(); } static String dropLastJavaToken(String s) { return join(dropLast(2, javaTok(s))); } static long ai_hasTriple_vary_worstCase; static boolean ai_hasTriple_vary(String a, String b, String c) { // TODO: optimize! 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; } static Cache> verbToPastMap_cache = new Cache("verbToPastMap_load"); static Map verbToPastMap() { return verbToPastMap_cache.get(); } static Map verbToPastMap_load() { HashMap map = new HashMap(); for (List l : englishVerbQuintuples()) map.put(get(l, 0), get(l, 1)); return map; } static boolean isAWTThread() { if (isAndroid()) return false; if (isHeadless()) return false; return isAWTThread_awt(); } static boolean isAWTThread_awt() { return SwingUtilities.isEventDispatchThread(); } static String shortDynamicClassName(Object o) { if (o instanceof DynamicObject && ((DynamicObject) o).className != null) return ((DynamicObject) o).className; return shortClassName(o); } static Thread currentThread() { return Thread.currentThread(); } static String standardCredentials() { String user = standardCredentialsUser(); String pass = standardCredentialsPass(); if (nempty(user) && nempty(pass)) return "&_user=" + urlencode(user) + "&_pass=" + urlencode(pass); return ""; } static List toLinesFullTrim(String s) { List l = toLines(s); for (ListIterator i = l.listIterator(); i.hasNext(); ) { String line = i.next().trim(); if (line.length() == 0) i.remove(); else i.set(line); } return l; } static List toLinesFullTrim(File f) { return toLinesFullTrim(loadTextFile(f)); } static Object addToMultiPort_responder; 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; } }); } static long addToMultiPort(final String botName, final Object responder) { //print(botName); 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); } 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; } static BigInteger minus(BigInteger a, BigInteger b) { return a.subtract(b); } static Object unstructure(String text) { return unstructure(text, false); } static Object unstructure(String text, final boolean allDynamic) { return unstructure(text, allDynamic, null); } static int structure_internStringsLongerThan = 50; static int unstructure_tokrefs; // stats abstract static class unstructure_Receiver { abstract void set(Object o); } // classFinder: func(name) -> class (optional) static Object unstructure(String text, boolean allDynamic, Object classFinder) { if (text == null) return null; return unstructure_tok(javaTokC_noMLS_iterator(text), allDynamic, classFinder); } static Object unstructure_reader(BufferedReader reader) { return unstructure_tok(javaTokC_noMLS_onReader(reader), false, null); } static Object unstructure_tok(final Producer tok, final boolean allDynamic, final Object classFinder) { final boolean debug = unstructure_debug; final class X { int i = -1; HashMap refs = new HashMap(); HashMap tokrefs = new HashMap(); HashSet concepts = new HashSet(); HashMap classesMap = new HashMap(); List stack = new ArrayList(); String curT; // look at current token String t() { return curT; } // get current token, move to next String tpp() { String t = curT; consume(); return t; } 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; // if (debug) print("parse: " + quote(t)); final int tokIndex = i; parse_inner(refID, tokIndex, new unstructure_Receiver() { void set(Object o) { if (_refID != 0) refs.put(_refID, o); if (o != null) tokrefs.put(tokIndex, o); out.set(o); } }); } void parse_inner(int refID, int tokIndex, final unstructure_Receiver out) { String t = t(); // if (debug) print("parse_inner: " + quote(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("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 (debug) print("isLongConstant " + quote(t) + " => " + isLongConstant(t)); 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("{")) { parseMap(out); return; } if (t.equals("[")) { 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); // any other class name if (c == null) { // First, find class if (allDynamic) c = null; else c = classFinder != null ? (Class) callF(classFinder, t) : findClass(t); if (c != null) classesMap.put(t, c); } // Check if it has an outer reference consume(); boolean hasBracket = eq(t(), "("); if (hasBracket) consume(); boolean hasOuter = hasBracket && eq(t(), "this$1"); DynamicObject dO = null; Object o = null; 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)); } // Save in references list early because contents of object // might link back to main object if (refID != 0) refs.put(refID, o != null ? o : dO); tokrefs.put(tokIndex, o != null ? o : dO); // NOW parse the fields! final LinkedHashMap fields = new LinkedHashMap(); // preserve order 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()); consume("="); stack.add(this); parse(new unstructure_Receiver() { 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); } } void objRead(Object o, DynamicObject dO, Map fields) { if (o != null) if (dO != null) { if (debug) printStructure("setOptAllDyn", fields); setOptAllDyn(dO, fields); } else { setOptAll(o, fields); } else for (String field : keys(fields)) dO.fieldValues.put(field.intern(), fields.get(field)); if (o != null) pcallOpt_noArgs(o, "_doneLoading"); } void parseSet(final Set set, final unstructure_Receiver out) { parseList(new unstructure_Receiver() { void set(Object o) { set.addAll((List) o); out.set(set); } }); } 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(newObject("main$Lisp", (String) list.get(0), subList(list, 1))); } else { stack.add(this); parse(new unstructure_Receiver() { 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(newObject(\"ma..."; }}); } 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() { 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 ..."; }}); } 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() { void set(Object o) { //if (debug) print("List element type: " + getClassName(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 ..."; }}); } 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() { 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(\"..."; }}); } 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; } Object parseBigInt() { consume("bigint"); consume("("); String val = tpp(); if (eq(val, "-")) val = "-" + tpp(); consume(")"); return new BigInteger(val); } Object parseDouble() { consume("d"); consume("("); String val = unquote(tpp()); consume(")"); return Double.parseDouble(val); } Object parseFloat() { consume("fl"); String val; if (eq(t(), "(")) { consume("("); val = unquote(tpp()); consume(")"); } else { val = unquote(tpp()); } return Float.parseFloat(val); } void parseHashSet(unstructure_Receiver out) { consume("hashset"); parseSet(new HashSet(), out); } void parseTreeSet(unstructure_Receiver out) { consume("treeset"); parseSet(new TreeSet(), out); } void parseMap(unstructure_Receiver out) { parseMap(new TreeMap(), out); } 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; } } void parseMap(final Map map, final unstructure_Receiver out) { consume("{"); stack.add(new Runnable() { boolean v; Object key; public void run() { if (v) { v = false; stack.add(this); consume("="); parse(new unstructure_Receiver() { 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() { void set(Object o) { key = o; } }); } } // if v else } // run() }); } /*void parseSub(unstructure_Receiver out) { int n = l(stack); parse(out); while (l(stack) > n) stack }*/ void consume() { curT = tok.next(); ++i; } void consume(String s) { if (!eq(t(), s)) { /*S prevToken = i-1 >= 0 ? tok.get(i-1) : ""; S nextTokens = join(tok.subList(i, Math.min(i+2, tok.size()))); fail(quote(s) + " expected: " + prevToken + " " + nextTokens + " (" + i + "/" + tok.size() + ")");*/ throw fail(quote(s) + " expected, got " + quote(t())); } consume(); } void parse_x(unstructure_Receiver out) { consume(); // get first token 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() { void set(Object o) { v.set(o); } }); unstructure_tokrefs = x.tokrefs.size(); return v.get(); } finally { DynamicObject_loading.set(b); } } static boolean unstructure_debug; 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]; } static void arraycopy(Object src, int srcPos, Object dest, int destPos, int n) { System.arraycopy(src, srcPos, dest, destPos, n); } static String hideCredentials(URL url) { return url == null ? null : hideCredentials(str(url)); } static String hideCredentials(String url) { return url.replaceAll("([&?])_pass=[^&\\s\"]*", "$1_pass="); } static List scanLog(String progID, String fileName) { return scanLog(getProgramFile(progID, fileName)); } static List scanLog(String fileName) { return scanLog(getProgramFile(fileName)); } 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; } static boolean web_matchNodeLists_x_debug; // nodeMatcher: func(WebNode, WebNode) -> double 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; // Score nodes for (int i = 0; i < l(l1); i++) { WebNode a = l1.get(i), b = l2.get(i); score += toDouble(callF(nodeMatcher, a, b)); } // Score relations 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 || !containsAll(rel2.labels, rel.labels)) // --score; if (rel2 == null) --score; else score += toDouble(callF(nodeMatcher, rel, rel2)); } return score; } static List reversedList(Collection l) { List x = cloneList(l); Collections.reverse(x); return x; } static Map cloneMap(Map map) { if (map == null) return litmap(); // assume mutex is equal to collection synchronized(map) { return map instanceof TreeMap ? new TreeMap(map) : map instanceof LinkedHashMap ? new LinkedHashMap(map) : new HashMap(map); } } static WebRelation web_getRelation(WebNode a, WebNode b) { return a.web.getRelation(a, b); } static File getGlobalCache() { File file = new File(javaxCachesDir(), "Binary Snippets"); file.mkdirs(); return file; } 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; } static String javaTok_cached_s; static List javaTok_cached_l; 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; } static Lock ai_activeSpecLock = lock(); static Lock ai_activeSpecLock() { return ai_activeSpecLock; } static LinkedHashSet litorderedset(A... items) { LinkedHashSet set = new LinkedHashSet(); for (A a : items) set.add(a); return set; } static Set ai_is_myYourEtc_maybe = litciset("this", "that", "all", "some"); static boolean ai_is_myYourEtc_maybe(String s) { return containsNotNull(ai_is_myYourEtc_maybe, s); } static String indentx(String s) { return indentx(indent_default, s); } static String indentx(int n, String s) { return dropSuffix(repeat(' ', n), indent(n, s)); } static String indentx(String indent, String s) { return dropSuffix(indent, indent(indent, s)); } static File programDir_mine; // set this to relocate program's data static File programDir() { return programDir(getProgramID()); } 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; } static IterableIterator linesFromFile(File f) { try { if (!f.exists()) return emptyIterableIterator(); if (ewic(f.getName(), ".gz")) return linesFromReader(utf8bufferedReader(new GZIPInputStream(new FileInputStream(f)))); return linesFromReader(utf8bufferedReader(f)); } catch (Exception __e) { throw rethrow(__e); } } static String ymd_minus_hms() { return ymd() + "-" + hms(); } static Map similarEmptyMap(Map m) { if (m instanceof TreeMap) return new TreeMap(); if (m instanceof LinkedHashMap) return new LinkedHashMap(); // default to a hash map return new HashMap(); } static boolean allContainCharacters(Collection l) { return all("hasLetters", l); } static boolean isPrimitiveBoolField(Object o, String field) { Field f = findField2(o, field); return f != null && eq(f.getType(), boolean.class); } static Collection web_relationObjects(Web web) { return values(web.relations); } 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; } 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; } static File newFile(File base, String... names) { for (String name : names) base = new File(base, name); return base; } static File newFile(String name) { return name == null ? null : new File(name); } static NavigableSet navigableKeys(NavigableMap map) { return map == null ? new TreeSet() : map.navigableKeySet(); } static NavigableSet navigableKeys(MultiMap mm) { return ((NavigableMap) mm.data).navigableKeySet(); } static String loadProgramTextFile(String name) { return loadTextFile(getProgramFile(name)); } static String loadProgramTextFile(String progID, String name) { return loadTextFile(getProgramFile(progID, name)); } static String loadProgramTextFile(String progID, String name, String defaultText) { return loadTextFile(getProgramFile(progID, name), defaultText); } static String conceptQuote(String s) { if (isIdentifier(s) || isInteger(s) || isProperlyQuoted(s)) return s; return quote(s); } static volatile boolean readLine_noReadLine; static String readLine() { if (readLine_noReadLine) return null; return (String) call(getJavaX(), "readLine"); } 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; } static Responder makeResponder(final Object bot) { if (bot instanceof Responder) return (Responder) bot; if (bot instanceof String) { String f = (String) ( bot); return new Responder() { 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() { String answer(String s, List history) { return makeResponder_callAnswerMethod(bot, s, history); } }; } static List concatWebLists(Collection... lists) { return uniqueByField_favorLast(concatLists(lists), "globalID"); } static String struct_noStringSharing(Object o) { structure_Data d = new structure_Data(); d.noStringSharing = true; return structure(o, d); } static String circlesEditorDBID() { return "#1007609"; } 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; } static void upgradeJavaXAndRestart() { run("#1001639"); restart(); sleep(); } 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; } static void gzipFile(File in, File out) { try { saveStreamToGZFile(newFileInputStream(in), out); } catch (Exception __e) { throw rethrow(__e); } } static List parse3(String s) { return dropPunctuation(javaTokPlusPeriod(s)); } static Set asSet(Object[] array) { HashSet set = new HashSet(); for (Object o : array) if (o != null) set.add(o); return set; } static Set asSet(String[] array) { TreeSet set = new TreeSet(); for (String o : array) if (o != null) set.add(o); return set; } 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; } static Map htmlgetparams(String tag) { return getHtmlTagParameters(tag); } static boolean preciseCall_debug; 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 m.invoke(null, args); } else { throw todo(); } } catch (Exception __e) { throw rethrow(__e); } } static File getProgramDir() { return programDir(); } static File getProgramDir(String snippetID) { return programDir(snippetID); } static List filter(Iterable c, Object pred) { List x = new ArrayList(); for (Object o : c) if (isTrue(callF(pred, o))) x.add(o); return x; } static List filter(Object pred, Iterable c) { return filter(c, pred); } 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) { printStackTrace2(__e); } throw fail("Already running: " + name); } } static volatile int stefansChat_n_value; static Lock stefansChat_n_lock = lock(); static List stefansChat_n_onChange = synchroList(); // runnables static int stefansChat_n() { return stefansChat_n_notify(toInt(loadPage("http://botcompany.de/1008998/raw/n"))); } static int stefansChat_n_notify(int n) { boolean changed = false; if (n > stefansChat_n_value) { Lock _lock_1717 = stefansChat_n_lock; lock(_lock_1717); try { if (n > stefansChat_n_value) { stefansChat_n_value = n; changed = true; } } finally { unlock(_lock_1717); } } if (changed) pcallF_all(stefansChat_n_onChange); return n; } static void stefansChat_n_onChange(Object r) { if (stefansChat_n_value == 0) stefansChat_n(); setAdd(stefansChat_n_onChange, r); } // port -> various VM info static Map computerVMs() { LinkedHashMap vms = new LinkedHashMap(); for (Map inj : computerInjectionsData()) putIfNotExisting(vms, getInt(inj, "VM"), inj); return vms; } static boolean isKnownPhrase(String s) { return containsNotNull(knownPhrases(), s); } static void mapPut(Map map, A key, B value) { if (map != null && key != null && value != null) map.put(key, value); } static void clearCacheVar(String var) { callOpt(getOptMC(var + "_cache"), "clear"); } static String patternQuote(String s) { return s.length() == 0 ? "" : Pattern.quote(s); } static long toLong(Object o) { if (o instanceof Number) return ((Number) o).longValue(); if (o instanceof String) return parseLong((String) o); return 0; } static Object nuObject(String className, Object... args) { try { return nuObject(Class.forName(className), args); } catch (Exception __e) { throw rethrow(__e); } } // too ambiguous - maybe need to fix some callers /*static O nuObject(O realm, S className, O... args) { ret nuObject(_getClass(realm, className), args); }*/ static A nuObject(Class c, Object... args) { try { Constructor m = nuObject_findConstructor(c, args); m.setAccessible(true); return (A) m.newInstance(args); } catch (Exception __e) { throw rethrow(__e); } } 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!" : "")); } 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; } static CirclesAndLines cal_unstructure(String s) { return (CirclesAndLines) unstructure(s); } static void ai_invalidateTriple(String a, String b, String c) { ai_invalidateWebs(ai_cache_findTriple(a, b, c)); } static void assertFalse(Object o) { assertEquals(false, o); } static boolean assertFalse(boolean b) { if (b) throw fail("oops"); return b; } static boolean assertFalse(String msg, boolean b) { if (b) throw fail(msg); return b; } 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; } static Cache> knownVerbs_cache = new Cache("knownVerbs_load"); static Set knownVerbs() { return knownVerbs_cache.get(); } static Set knownVerbs_load() { return caseInsensitiveSet(tlft(loadSnippet("#1009502"))); } static boolean ai_cache_hasTriple_verified(String a, String b, String c) { List webs = ai_fewestIndexedWebs_verified(a, b, c); return webs_search_noVar_bool(webFromTriples(a, b, c), webs); } static boolean ai_cache_hasTriple_verified(T3 t) { return ai_cache_hasTriple_verified(t.a, t.b, t.c); } static String dropPrefixTrim(String prefix, String s) { return trim(dropPrefix(prefix, s)); } static String firstJavaToken(String s) { if (s == null) return null; int l = s.length(); int j = 0; char c, d; // scan for whitespace 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); // scan for non-whitespace if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { if (s.charAt(j) == opener || s.charAt(j) == '\n') { // end at \n to not propagate unclosed string literal errors ++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)); // for stuff like "don't" else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L } 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); } static String fromUTF8(byte[] bytes) { return fromUtf8(bytes); } 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; } static String getProgramName_cache; static synchronized String getProgramName() { if (getProgramName_cache == null) getProgramName_cache = getSnippetTitleOpt(programID()); return getProgramName_cache; } static char lastChar(String s) { return empty(s) ? '\0' : s.charAt(l(s)-1); } static File userDir() { return new File(userHome()); } static File userDir(String path) { return new File(userHome(), path); } static int isAndroid_flag; static boolean isAndroid() { if (isAndroid_flag == 0) isAndroid_flag = System.getProperty("java.vendor").toLowerCase().indexOf("android") >= 0 ? 1 : -1; return isAndroid_flag > 0; } 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; } 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 + "'"; } static JTextArea wrappedTextArea(JTextArea ta) { ta.setLineWrap(true); ta.setWrapStyleWord(true); return ta; } static JTextArea wrappedTextArea() { return wrappedTextArea(jtextarea()); } static JTextArea wrappedTextArea(String text) { JTextArea ta = wrappedTextArea(); ta.setText(text); return ta; } static boolean hasLetters(String s) { for (int i = 0; i < s.length(); i++) if (Character.isLetter(s.charAt(i))) return true; return false; } static Set synchroTreeSet() { return Collections.synchronizedSet(new TreeSet()); } static boolean web_hasOneOfTheLabels(WebNode node, List labels) { for (Lisp l : labels) if (node.hasLabel(l)) return true; return false; } static List ai_cache_findTriple(String a, String b, String c) { List webs = ai_fewestIndexedWebs(a, b, c); return webs_search_noVar_all(webFromTriples(a, b, c), webs); } static List ai_cache_findTriple(T3 t) { return ai_cache_findTriple(t.a, t.b, t.c); } 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; } 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); } static RuntimeException todo() { throw new RuntimeException("TODO"); } static RuntimeException todo(String msg) { throw new RuntimeException("TODO: " + msg); } static String boolArrayToHex(boolean[] a) { return bytesToHex(boolArrayToBytes(a)); } static Boolean isHeadless_cache; static boolean isHeadless() { if (isHeadless_cache != null) return isHeadless_cache; if (GraphicsEnvironment.isHeadless()) return isHeadless_cache = true; // Also check if AWT actually works. // If DISPLAY variable is set but no X server up, this will notice. try { SwingUtilities.isEventDispatchThread(); return isHeadless_cache = false; } catch (Throwable e) { return isHeadless_cache = true; } } static Iterator emptyIterator() { return Collections.emptyIterator(); } static Class _getClass(String name) { try { return Class.forName(name); } catch (ClassNotFoundException e) { return null; // could optimize this } } static Class _getClass(Object o) { return o == null ? null : o instanceof Class ? (Class) o : o.getClass(); } static Class _getClass(Object realm, String name) { try { return getClass(realm).getClassLoader().loadClass(classNameToVM(name)); } catch (Exception __e) { throw rethrow(__e); } } static boolean jmatch(String pat, String s) { return jmatch(pat, s, null); } static boolean jmatch(String pat, String s, Matches matches) { if (s == null) return false; return jmatch(pat, javaTok(s), matches); } static boolean jmatch(String pat, List toks) { return jmatch(pat, toks, null); } static boolean jmatch(String pat, List toks, Matches matches) { List tokpat = javaTok(pat); String[] m = match2(tokpat, toks); //print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m)); if (m == null) return false; else { if (matches != null) matches.m = m; return true; } } static int withMargin_defaultWidth = 6; static JPanel withMargin(Component c) { return withMargin(withMargin_defaultWidth, c); } static JPanel withMargin(final int w, final Component c) { return swing(new F0() { JPanel get() { try { JPanel p = new JPanel(new BorderLayout()); p.setBorder(BorderFactory.createEmptyBorder(w, w, w, 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..."; }}); } static boolean cic(List l, String s) { return containsIgnoreCase(l, s); } static boolean cic(String[] l, String s) { return containsIgnoreCase(l, s); } static boolean cic(String s, char c) { return containsIgnoreCase(s, c); } static boolean cic(String a, String b) { return containsIgnoreCase(a, b); } static A printStruct(String prefix, A a) { printStructure(prefix, a); return a; } static A printStruct(A a) { printStructure(a); return a; } /** writes safely (to temp file, then rename) */ 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); } static void saveBinaryFile(File fileName, byte[] contents) { try { saveBinaryFile(fileName.getPath(), contents); } catch (IOException e) { throw new RuntimeException(e); } } 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); } static ThreadLocal> print_byThread() { synchronized(print_byThread_lock) { if (print_byThread == null) print_byThread = new ThreadLocal(); } return print_byThread; } static A popLast(List l) { return liftLast(l); } 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); } } 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; } static HashMap twoListsToHashMap(A[] l1, A[] l2) { return twoListsToHashMap(Arrays.asList(l1), Arrays.asList(l2)); } // map: index of opening bracket -> index of closing bracket 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; } static List getBracketMap_opening = ll("{", "("); static List getBracketMap_closing = ll("}", ")"); static Pair> cloneWebWithMap(Web web) { Web w = web_similarEmptyWeb(web); return pair(w, (Map) web_addWebToWeb(web, w)); } 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); } static boolean even(int i) { return (i & 1) == 0; } static boolean even(long i) { return (i & 1) == 0; } static List _registerWeakMap_preList; static A _registerWeakMap(A map) { if (javax() == null) { // We're in class init 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; } static void _onLoad_registerWeakMap() { assertNotNull(javax()); if (_registerWeakMap_preList == null) return; for (Object o : _registerWeakMap_preList) _registerWeakMap(o); _registerWeakMap_preList = null; } static Object getOptMC(String field) { return getOpt(mc(), field); } 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; } static List ai_variationsOf(String s) { return uniquify(concatLists(ll(s), ai_texts_bidirectionalOperands(s, "unquotes to:"))); } static int cmp(Number a, Number b) { return a == null ? b == null ? 0 : -1 : cmp(a.doubleValue(), b.doubleValue()); } static int cmp(double a, double b) { return a < b ? -1 : a == b ? 0 : 1; } static int cmp(String a, String b) { return a == null ? b == null ? 0 : -1 : a.compareTo(b); } 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); } static void web_deindex(WebNode n) { for (Lisp label : n.labels()) n.web.index.remove(label, n); } static int jfind(List tok, String in) { return jfind(tok, 1, in); } static int jfind(List tok, int startIdx, String in) { return jfind(tok, startIdx, in, null); } static int jfind(List tok, String in, Object condition) { return jfind(tok, 1, in, condition); } static int jfind(List tok, int startIdx, String in, Object condition) { List tokin = javaTok(in); jfind_preprocess(tokin); return jfind(tok, startIdx, tokin, condition); } // assumes you preprocessed tokin static int jfind(List tok, List tokin) { return jfind(tok, 1, tokin); } static int jfind(List tok, int startIdx, List tokin) { return jfind(tok, startIdx, tokin, null); } static int jfind(List tok, int startIdx, List tokin, Object condition) { return findCodeTokens(tok, startIdx, false, toStringArray(codeTokensOnly(tokin)), condition); } static void jfind_preprocess(List tok) { for (String type : litlist("quoted", "id", "int")) replaceSublist(tok, ll("<", "", type, "", ">"), ll("<" + type + ">")); replaceSublist(tok, ll("\\", "", "*"), ll("\\*")); } static boolean web_matchNodeName(WebNode node, String pat) { return eq(pat, "*") || eqic(pat, web_text(node)); } static String getDBProgramID_id; static String getDBProgramID() { return nempty(getDBProgramID_id) ? getDBProgramID_id : programIDWithCase(); } static long parseLong(String s) { if (s == null) return 0; return Long.parseLong(dropSuffix("L", s)); } static long parseLong(Object s) { return Long.parseLong((String) s); } static String joinStrings(String sep, String... strings) { return joinStrings(sep, Arrays.asList(strings)); } 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); } 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; } static List getMultiPorts() { return (List) callOpt(getJavaX(), "getMultiPorts"); } static void cancelThread(Thread t) { if (t == null) return; ping(); synchronized(mc()) { ping_actions.put(t, "cancelled"); ping_anyActions = true; } } 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; // scan for whitespace 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); // scan for non-whitespace if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { if (s.charAt(j) == opener /*|| s.charAt(j) == '\n'*/) { // allow multi-line strings ++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)); // for stuff like "don't" else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L } else ++j; tok.add(quickSubstring(s, i, j)); ++n; i = j; } if ((tok.size() % 2) == 0) tok.add(""); return tok; } static File javaxDataDir_dir; // can be set to work on different base dir static File javaxDataDir() { return javaxDataDir_dir != null ? javaxDataDir_dir : new File(userHome(), "JavaX-Data"); } static int indent_default = 2; static String indent(int indent) { return repeat(' ', indent); } static String indent(int indent, String s) { return indent(repeat(' ', indent), s); } static String indent(String indent, String s) { return indent + s.replace("\n", "\n" + indent); } static String indent(String s) { return indent(indent_default, s); } static List indent(String indent, List lines) { List l = new ArrayList(); for (String s : lines) l.add(indent + s); return l; } static String dottedStyle() { return "border-bottom: dotted 1px"; } static byte[] loadBinaryPage(String url) { try { print("Loading " + url); return loadBinaryPage(loadPage_openConnection(new URL(url))); } catch (Exception __e) { throw rethrow(__e); } } static byte[] loadBinaryPage(URLConnection con) { try { setHeaders(con); return loadBinaryPage_noHeaders(con); } catch (Exception __e) { throw rethrow(__e); } } static byte[] loadBinaryPage_noHeaders(URLConnection con) { try { ByteArrayOutputStream buf = new ByteArrayOutputStream(); InputStream inputStream = con.getInputStream(); long len = 0; try { len = con.getContentLengthLong(); } 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); } } 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)); } static Set allNonStaticFields(Object o) { TreeSet fields = new TreeSet(); Class _c = _getClass(o); do { for (Field f : _c.getDeclaredFields()) if ((f.getModifiers() & Modifier.STATIC) == 0) fields.add(f.getName()); _c = _c.getSuperclass(); } while (_c != null); return fields; } static int moveToTopRightCorner_inset = 20; static A moveToTopRightCorner(A a) { Window w = getWindow(a); if (w != null) w.setLocation(getScreenSize().width-w.getWidth()-moveToTopRightCorner_inset, moveToTopRightCorner_inset); return a; } 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; // scan for whitespace 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)); // scan for non-whitespace if (c == '\u201C' || c == '\u201D') c = '"'; // normalize quotes if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { char _c = s.charAt(j); if (_c == '\u201C' || _c == '\u201D') _c = '"'; // normalize quotes 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; } // result might not be modifiable static String[] litstringarray(String... data) { return data; } static boolean equalsIgnoreCase(String a, String b) { return a == null ? b == null : a.equalsIgnoreCase(b); } static boolean equalsIgnoreCase(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); } static void ai_invalidateWebs(Collection webs) { for (Web web : unnull(webs)) ai_invalidateWeb(web); } static boolean setOptAllDyn_debug; static void setOptAllDyn(DynamicObject o, Map fields) { if (fields == null) return; for (String field : keys(fields)) { Object val = fields.get(field); boolean has = hasField(o, field); if (has) setOpt(o, field, val); else { o.fieldValues.put(field.intern(), val); if (setOptAllDyn_debug) print("setOptAllDyn added dyn " + field + " to " + o + " [value: " + val + ", fieldValues = " + systemHashCode(o.fieldValues) + ", " + struct(keys(o.fieldValues)) + "]"); } } } static Object mainBot; static Object getMainBot() { return mainBot; } static IterableIterator emptyIterableIterator_instance = new IterableIterator() { public Object next() { throw fail(); } public boolean hasNext() { return false; } }; static IterableIterator emptyIterableIterator() { return emptyIterableIterator_instance; } static Set setMinus(Set set, Object... stuff) { Set s2 = cloneSet(set); for (Object o : stuff) s2.remove(o); return s2; } static String structure_addTokenMarkers(String s) { List tok = javaTok(s); // find references 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; // add markers 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); } static void failIfOddCount(Object... list) { if (odd(l(list))) throw fail("Odd list size: " + list); } // the map is: invalidated id -> invalidators static DependentCache> ai_invalidatedWebs_cache = new DependentCache("ai_invalidatedWebs_calc", "cachedNodeIndex" /*_changeCount*/); static Set ai_invalidatedWebs() { return keys(ai_invalidatedWebs_cache.get()); } static MultiMap ai_invalidatedWebs_calc() { print("ai_invalidatedWebs_calc"); ai_onNewOrRemovedWeb("ai_invalidatedWebs_onNewWeb", "ai_invalidatedWebs_onRemovedWeb"); ai_invalidatedWebs_cache.set(new MultiMap()); Collection webs = allWebsFromCachedNodeIndex_uncached(); print("Have " + nWebs(webs)); for (Web web : webs) ai_invalidatedWebs_onNewWeb(web); print("calc done"); return ai_invalidatedWebs_cache.get(); } static void ai_invalidatedWebs_onNewWeb(Web web) { if (web.unverified) return; Lock _lock_1876 = ai_invalidatedWebs_cache.lock; lock(_lock_1876); 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(m.unq(0), web.globalID); } finally { unlock(_lock_1876); } } static void ai_invalidatedWebs_onRemovedWeb(Web web) { if (web.unverified) return; Lock _lock_1877 = ai_invalidatedWebs_cache.lock; lock(_lock_1877); 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(m.unq(0), web.globalID); } finally { unlock(_lock_1877); } } static TreeSet caseInsensitiveSet() { return new TreeSet(String.CASE_INSENSITIVE_ORDER); } static TreeSet caseInsensitiveSet(Collection c) { return toCaseInsensitiveSet(c); } 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); } static TreeSet litciset(String... items) { TreeSet set = caseInsensitiveSet(); for (String a : items) set.add(a); return set; } static Set fieldNames(Object o) { return allFields(o); } static MultiMap ai_indexWebs(Collection webs) { MultiMap index = caseInsensitiveMultiMap(); for (Web web : unnull(webs)) ai_addWebToIndex(index, web); return index; } static List splitAtSpace(String s) { return asList(s.split("\\s+")); } static String getComputerID_quick() { return computerID(); } static void printIndent(Object o) { print(indentx(str(o))); } static void printIndent(String indent, Object o) { print(indentx(indent, str(o))); } static void printIndent(int indent, Object o) { print(indentx(indent, str(o))); } 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; } static String standardCredentialsUser() { return trim(loadTextFile(new File(userHome(), ".tinybrain/username"))); } static F1 functionToF1(final Object f) { if (isString(f)) return mainFunctionToF1((String) f); if (f instanceof F1) return (F1) f; return new F1() { Object get(Object a) { return callF(f, a); } }; } 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")); //print("VM " + vmPort + " injections: " + x); 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) { printStackTrace2(__e); } } catch (Throwable __e) { printStackTrace2(__e); } vm.close(); } done2(time, "Scanned " + n(l, "VMs")); return l; } public static boolean isSnippetID(String s) { try { parseSnippetID(s); return true; } catch (RuntimeException e) { return false; } } static File javaxCachesDir_dir; // can be set to work on different base dir static File javaxCachesDir() { return javaxCachesDir_dir != null ? javaxCachesDir_dir : new File(userHome(), "JavaX-Caches"); } static List getClasses(Object[] array) { List l = new ArrayList(); for (Object o : array) l.add(_getClass(o)); return l; } static void pcallOpt_noArgs(Object o, String method) { try { callOpt_noArgs(o, method); } catch (Throwable __e) { printStackTrace2(__e); } } static int iround(double d) { return (int) Math.round(d); } static boolean isUpperCase(char c) { return Character.isUpperCase(c); } static boolean sameSnippetID(String a, String b) { if (!isSnippetID(a) || !isSnippetID(b)) return false; return parseSnippetID(a) == parseSnippetID(b); } static List allToUpper(Collection l) { return map("upper", l); } static TreeMap caseInsensitiveMap() { return new TreeMap(String.CASE_INSENSITIVE_ORDER); } static List tlft_j(String text) { return toLinesFullTrim_java(text); } static boolean containsNewLine(String s) { return contains(s, '\n'); // screw \r, nobody needs it } static boolean hasBot(String searchPattern) { DialogIO io = findBot(searchPattern); if (io != null) { io.close(); return true; } else return false; } static String ymd() { return year() + formatInt(month(), 2) + formatInt(dayOfMonth(), 2); } static Android3 methodsBot2(String name, final Object receiver, final List exposedMethods) { return methodsBot2(name, receiver, exposedMethods, null); } 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() { String answer(String s, List history) { return exposeMethods2(receiver, s, exposedMethods, lock); } }; return makeBot(android); } static String fromUtf8(byte[] bytes) { try { return new String(bytes, "UTF-8"); } catch (Exception __e) { throw rethrow(__e); } } static String standardCredentialsPass() { return trim(loadTextFile(new File(userHome(), ".tinybrain/userpass"))); } static Font typeWriterFont() { return typeWriterFont(14); } static Font typeWriterFont(int size) { return new Font("Courier", Font.PLAIN, size); } static List beginCriticalAction_inFlight = synchroList(); static class CriticalAction { String description; CriticalAction() {} CriticalAction(String description) { this.description = description;} void done() { beginCriticalAction_inFlight.remove(this); } } static CriticalAction beginCriticalAction(String description) { ping(); CriticalAction c = new CriticalAction(description); beginCriticalAction_inFlight.add(c); return c; } 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 " + n(n, "critical actions") + ": " + join(", ", collect(beginCriticalAction_inFlight, "description"))); } catch (Throwable __e) { printStackTrace2(__e); } } sleepInCleanUp(10); } } static String sendOpt(String bot, String text, Object... args) { return sendToLocalBotOpt(bot, text, args); } 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; } static String hms() { return hms(now()); } static String hms(long time) { return new SimpleDateFormat("HHmmss").format(time); } static byte[] hexToBytes(String s) { int n = l(s) / 2; byte[] bytes = new byte[n]; for (int i = 0; i < n; i++) { String hex = substring(s, i*2, i*2+2); try { bytes[i] = (byte) parseHexByte(hex); } catch (Throwable _e) { throw fail("Bad hex byte: " + quote(hex) + " at " + i*2 + "/" + l(s)); } } return bytes; } static TreeSet web_fullTextSet(List l) { return new TreeSet(concatMap("web_texts", l)); } static HashMap nuEmptyObject_cache = new HashMap(); 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); } } return (A) ctr.newInstance(); } catch (Exception __e) { throw rethrow(__e); } } 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()); } static JWindow showInTopRightCorner(Component c) { JWindow w = new JWindow(); w.add(c); w.pack(); moveToTopRightCorner(w); w.setVisible(true); return w; } static void showConsole() { showFrame(consoleFrame()); } static Set mergeSets(Set... l) { return joinSets(l); } static boolean isSingularNoun(String s) { return contains(knownNouns(), lower(s)); } static Object newObject(Class c, Object... args) { return nuObject(c, args); } static Object newObject(String className, Object... args) { return nuObject(className, args); } static List web_allForwardRelated(WebNode node, String relationLabel) { return web_allForwardRelated(node.web, node, relationLabel); } static List web_allForwardRelated(Web web, WebNode node, String relationLabel) { return web_allForwardRelated(web, node, web.parseLabel(relationLabel)); } static List web_allForwardRelated(Web web, WebNode node, Lisp relationLabel) { List l = new ArrayList(); for (Pair p : keys(web.relations)) if (p.a == node) { WebNode rel = web.relations.get(p); if (rel.hasLabel(relationLabel)) l.add(p.b); } return l; } static List web_allForwardRelated(WebNode node) { return pairList_forwardLookup(keys(node.web.relations), node); } 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); } static String getCanonicalPath(String path) { try { return new File(path).getCanonicalPath(); } catch (Exception __e) { throw rethrow(__e); } } static Thread _unregisterThread(Thread t) { _registerThread_threads.remove(t); return t; } static void _unregisterThread() { _unregisterThread(currentThread()); } static boolean forbiddenPort(int port) { return port == 5037; // adb } static String dbBotName(String progID) { return fsI(progID) + " Concepts"; } static boolean isOK(String s) { s = trim(s); return swic(s, "ok ") || eqic(s, "ok") || matchStart("ok", s); } 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(); } } 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(); } } static void warnIfOddCount(Object... list) { if (odd(l(list))) printStackTrace("Odd list size: " + list); } 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; } static boolean web_hasLabelIC(Pair p, String text) { return web_hasLabelIC(p.a.web.relation(p), text); } static boolean web_hasLabelIC(WebNode a, WebNode b, String text) { return web_hasLabelIC(a.web.relation(a, b), text); } static FileOutputStream newFileOutputStream(File path) throws IOException { return newFileOutputStream(path.getPath()); } static FileOutputStream newFileOutputStream(String path) throws IOException { return newFileOutputStream(path, false); } static FileOutputStream newFileOutputStream(String path, boolean append) throws IOException { FileOutputStream f = new // Line break for ancient translator FileOutputStream(path, append); callJavaX("registerIO", f, path, true); return f; } 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); } 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; } static void setOptAll(Object o, Map fields) { if (fields == null) return; for (String field : keys(fields)) setOpt(o, field, fields.get(field)); } static void setOptAll(Object o, Object... values) { //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]; setOpt(o, field, value); } } static IterableIterator linesFromReader(Reader r) { final BufferedReader br = bufferedReader(r); return iteratorFromFunction(new Object() { Object get() { try { return readLineFromReaderWithClose(br) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "readLineFromReaderWithClose(br)"; }}); } static boolean isPluralNoun(String s) { return contains(knownPluralNouns(), lower(s)); } static Map synchroHashMap() { return Collections.synchronizedMap(new HashMap()); } static File conceptsFile(String progID) { return getProgramFile(progID, "concepts.structure.gz"); } static List web_parseLabels(final Web web, String... labels) { return map(new Object() { Object get(String s) { try { return web.parseLabel(s) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "web.parseLabel(s)"; }}, labels); } static String nohup_sanitize(String s) { return takeFirst(50, s.replaceAll("[^a-zA-Z0-9\\-_]", "")); } static File javaxCodeDir_dir; // can be set to work on different base dir static File javaxCodeDir() { return javaxCodeDir_dir != null ? javaxCodeDir_dir : new File(userHome(), "JavaX-Code"); } static void closeAllWriters(Collection l) { for (Writer w : unnull(l)) try { w.close(); } catch (Throwable __e) { printStackTrace2(__e); } } static void swingAndWait(Runnable r) { try { if (isAWTThread()) r.run(); else EventQueue.invokeAndWait(r); } catch (Exception __e) { throw rethrow(__e); } } 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(); } } static boolean endsWithIgnoreCase(String a, String b) { return a != null && l(a) >= l(b) && a.regionMatches(true, l(a)-l(b), b, 0, l(b)); } static void cleanKill() { cleanKillVM(); } 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; } static boolean isLongConstant(String s) { if (!s.endsWith("L")) return false; s = s.substring(0, l(s)-1); return isInteger(s); } // return Int.MAX_VALUE if not applicable // Lower score is better 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()) {} // perfect match else if (isInstanceX(c, a)) ++score; else return Integer.MAX_VALUE; } return score; } static boolean endsWithLetterOrDigit(String s) { return nempty(s) && isLetterOrDigit(lastCharacter(s)); } 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) { printStackTrace2(__e); } } catch (Throwable __e) { printStackTrace2(__e); } return webs; } static List ai_fewestIndexedWebs_verified(String... l) { return web_verifiedWebsOnly(ai_fewestIndexedWebs(l)); } static List localWebs() { return allWebsFromCircleEditor(); } static void onClick(JComponent c, final Object runnable) { c.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { callF(runnable, e); } }); } // re-interpreted for buttons static void onClick(JButton btn, final Object runnable) { onEnter(btn, runnable); } 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 out.print(c); } out.print('"'); } 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); } static JWindow showWindow(Component c) { JWindow w = new JWindow(); w.add(wrap(c)); return w; } 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; } // returns l(s) if not found 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); } 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); } static int smartIndexOf(String s, String sub) { return smartIndexOf(s, sub, 0); } static int smartIndexOf(String s, char c) { return smartIndexOf(s, c, 0); } static int smartIndexOf(List l, A sub) { return smartIndexOf(l, sub, 0); } static int smartIndexOf(List l, A sub, int start) { int i = indexOf(l, sub, start); return i < 0 ? l(l) : i; } static double parseDouble(String s) { return Double.parseDouble(s); } 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); } static List repeat(A a, int n) { List l = new ArrayList(n); for (int i = 0; i < n; i++) l.add(a); return l; } static List repeat(int n, A a) { return repeat(a, n); } // start multi-port if none exists in current VM. static void startMultiPort() { List mp = getMultiPorts(); if (mp != null && mp.isEmpty()) { nohupJavax("#1001639"); throw fail("Upgrading JavaX, please restart this program afterwards."); //callMain(hotwire("#1001672")); } } static Set synchroHashSet() { return Collections.synchronizedSet(new HashSet()); } static String dropAfterSpace(String s) { return s == null ? null : substring(s, 0, smartIndexOf(s, ' ')); } static List mapMethod(Collection c, final String methodName) { return map(c, new Object() { Object get(Object o) { try { return callOpt(o, methodName) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callOpt(o, methodName)"; }}); } static List mapMethod(String methodName, Collection c) { return mapMethod(c, methodName); } static List findMethodsNamed2(Object obj, String method, boolean staticOnly) { if (obj == null) return null; return findMethodsNamed2(_getClass(obj), method, staticOnly); } 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; } static volatile String caseID_caseID; static String caseID() { return caseID_caseID; } static void caseID(String id) { caseID_caseID = id; } 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); } } static DialogIO talkToSubBot(final long vport, final DialogIO io) { return talkToSubBot(String.valueOf(vport), io); } static DialogIO talkToSubBot(final String subBot, final DialogIO io) { if (subBot == null) return io; return new talkToSubBot_IO(subBot, io); } static class talkToSubBot_IO extends DialogIO { String subBot; DialogIO io; talkToSubBot_IO(String subBot, DialogIO io) { this.io = io; this.subBot = subBot;} // delegate all but sendLine boolean isStillConnected() { return io.isStillConnected(); } String readLineImpl() { return io.readLineImpl(); } boolean isLocalConnection() { return io.isLocalConnection(); } Socket getSocket() { return io.getSocket(); } void close() { io.close(); } void sendLine(String line) { io.sendLine(format3("please forward to bot *: *", subBot, line)); } } static String getSnippetTitleOpt(String s) { return isSnippetID(s) ? getSnippetTitle(s) : s; } static String intern(String s) { return s == null ? null : s.intern(); } static HashMap getDeclaredFields_cache = new HashMap(); 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; } 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) { printStackTrace2(__e); } return valuesList(map); } catch (Throwable e) { printException(e); return ll(); } } static JScrollPane jscroll(final Component c) { return swing(new F0() { JScrollPane get() { try { return new JScrollPane(c) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "new JScrollPane(c)"; }}); } static String firstPartOfHelloString(String s) { int i = s.lastIndexOf('/'); return i < 0 ? s : rtrim(s.substring(0, i)); } static boolean web_isQuotedNode(WebNode node) { return isQuoted(web_text(node)); } static Producer javaTokC_noMLS_iterator(final String s) { return new Producer() { final int l = s.length(); int i = 0; public String next() { if (i >= l) return null; int j = i; char c, d; // scan for whitespace 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); // scan for non-whitespace if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { if (s.charAt(j) == opener || s.charAt(j) == '\n') { // end at \n to not propagate unclosed string literal errors ++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)); // for stuff like "don't" else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L } else ++j; String t = quickSubstring(s, i, j); i = j; return t; } }; } static boolean startsWithIC(String a, String b) { return startsWithIgnoreCase(a, b); } public static File mkdirsForFile(File file) { File dir = file.getParentFile(); if (dir != null) // is null if file is in current dir dir.mkdirs(); return file; } static List quickBotScan() { return ProgramScan.quickBotScan(); } static List quickBotScan(int[] preferredPorts) { return ProgramScan.quickBotScan(preferredPorts); } 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; } static void putIfNotExisting(Map map, A key, B value) { putIfNotThere(map, key, value); } 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; } static boolean loadFunctions_debug; static Class loadFunctions(String... functions) { return loadFunctions(asList(functions)); } static Class loadFunctions(Collection functions) { StringBuilder buf = new StringBuilder(); for (String s : asSet(functions)) buf.append("please include function " + assertIdentifier(s) + ".\n"); long time = sysNow(); Class c = veryQuickJava(str(buf)); if (loadFunctions_debug) done2_always(time, "loadFunctions: " + join(" ", functions)); return c; } 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); } } static String dollarVarsToStars(String s) { return dollarVarsToStars(s, null); } 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); } 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); } } static Field findField2(Object o, String field) { if (o instanceof Class) return findField2_findStaticField((Class) o, field); return findField2_findField(o.getClass(), field); } 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; } static Field findField2_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; } static Object callOptMC(String method, Object... args) { return callOpt(mc(), method, args); } // f : S -> S 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)); } } static FileInputStream newFileInputStream(File path) throws IOException { return newFileInputStream(path.getPath()); } static FileInputStream newFileInputStream(String path) throws IOException { FileInputStream f = new // Line break for ancient translator FileInputStream(path); //callJavaX("registerIO", f, path, true); return f; } static BufferedImage loadImage2(String snippetIDOrURL) { return loadBufferedImage(snippetIDOrURL); } static BufferedImage loadImage2(File file) { return loadBufferedImage(file); } static HashMap findClass_cache = new HashMap(); // currently finds only inner classes of class "main" // returns null on not found // this is the simple version that is not case-tolerant 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; } } static DialogIO talkTo(int port) { return talkTo("localhost", port); } static int talkTo_defaultTimeout = 10000; // This is the CONNECT timeout static int talkTo_timeoutForReads = 0; // Timeout waiting for answers (0 = no timeout) static ThreadLocal> talkTo_byThread = new ThreadLocal(); 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); } } static class talkTo_IO extends DialogIO { String ip; int port; Socket s; Writer w; BufferedReader in; 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); } } boolean isLocalConnection() { return s.getInetAddress().isLoopbackAddress(); } boolean isStillConnected() { return !(eos || s.isClosed()); } void sendLine(String line) { try { w.write(line + "\n"); w.flush(); } catch (Exception __e) { throw rethrow(__e); } } String readLineImpl() { try { return in.readLine(); } catch (Exception __e) { throw rethrow(__e); } } void close() { try { if (!noClose) s.close(); } catch (IOException e) { // whatever } } Socket getSocket() { return s; } } public static void copyFile(File src, File dest) { try { mkdirsForFile(dest); 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); } } static List localSoftwareMadeWebs() { List webs = new ArrayList(); for (String progID : websPostedIn()) try { webs.addAll(websMadeByProgram(progID)); } catch (Throwable __e) { printStackTrace2(__e); } return webs; } static List downloadedWebs() { /*ret concatLists(downloadedTypicalWebs(), downloadedWebsFromFileNamed("Webs from program #1011161 on tvejysmllsmz.gz"));*/ return downloadedTypicalWebs(); } static String internIfLongerThan(String s, int l) { return s == null ? null : l(s) >= l ? s.intern() : s; } static BufferedReader utf8bufferedReader(InputStream in) { try { return new BufferedReader(new InputStreamReader(in, "UTF-8")); } catch (Exception __e) { throw rethrow(__e); } } static BufferedReader utf8bufferedReader(File f) { try { return utf8bufferedReader(newFileInputStream(f)); } catch (Exception __e) { throw rethrow(__e); } } static double toM_double(long l) { return l/(1024*1024.0); } 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; //fail("Can't match, differing number of nodes: " + l(patternNodes) + "/" + l(inputNodes)); List> perms = allPermutations(inputNodes); Best < List < WebNode > > 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); } 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); } 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); } static Cache> knownAdverbs_cache = new Cache("knownAdverbs_load"); static Set knownAdverbs() { return knownAdverbs_cache.get(); } static Set knownAdverbs_load() { return toCaseInsensitiveSet( concatLists( map("adjectiveToAdverb",knownAdjectives()), tlft(loadSnippet("#1011128")))); } static Map safeUnstructureMap(String s) { return (Map) safeUnstructure(s); } static void printStructure(String prefix, Object o) { if (endsWithLetter(prefix)) prefix += ": "; print(prefix + structureForUser(o)); } static void printStructure(Object o) { print(structureForUser(o)); } // o is either a map already (string->object) or an arbitrary object, // in which case its fields are converted into a map. 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(); } return map; } catch (Exception __e) { throw rethrow(__e); } } // same for a collection (convert each element) static List> objectToMap(Collection l) { List x = new ArrayList(); for (Object o : l) x.add(objectToMap(o)); return x; } static boolean isMainProgram() { return creator() == null; } static void web_index(WebNode n) { for (Lisp label : n.labels()) n.web.index.setPut(label, n); } static boolean methodIsStatic(Method m) { return (m.getModifiers() & Modifier.STATIC) != 0; } 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; } static class FileStatus { long time, size; boolean isDir; public boolean equals(Object o) { return stdEq2(this, o); } public int hashCode() { return stdHash2(this); } } static FileStatus fileStatus(String path) { return fileStatus(newFile(path)); } static FileStatus fileStatus(File f) { return nu(FileStatus.class, "time" , f.lastModified(), "size" , fileLength(f), "isDir" , f.isDirectory()); } static ImageIcon imageIcon(String imageID) { try { return new ImageIcon(loadBinarySnippet(imageID).toURI().toURL()); } catch (Exception __e) { throw rethrow(__e); } } static ImageIcon imageIcon(BufferedImage img) { return new ImageIcon(img); } static ImageIcon imageIcon(RGBImage img) { return imageIcon(img.getBufferedImage()); } static Cache> knownPhrases_cache = new Cache("knownPhrases_load"); static Set knownPhrases() { return knownPhrases_cache.get(); } static Set knownPhrases_load() { return toCaseInsensitiveSet(tlft(loadSnippet("#1011207"))); } static int year() { return Calendar.getInstance().get(Calendar.YEAR); } static F1 mainFunctionToF1(final String fname) { return new F1() { Object get(Object a) { return callMC(fname, a); } }; } static String programIDWithCase() { return nempty(caseID()) ? programID() + "/" + quoteUnlessIdentifierOrInteger(caseID()) : programID(); } static int vmPort() { return myVMPort(); } static String readLineFromReaderWithClose(BufferedReader r) { try { String s = r.readLine(); if (s == null) r.close(); return s; } catch (Exception __e) { throw rethrow(__e); } } // c = JComponent or something implementing swing() static JComponent wrap(Object swingable) { if (swingable == null) return null; JComponent c = (JComponent) ( swingable instanceof JComponent ? swingable : 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; } static boolean hasField(Object o, String field) { return findField2(o, field) != null; } 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; } static Set joinSets(Set... l) { TreeSet set = new TreeSet(); for (Set o : l) if (o != null) set.addAll(o); return set; } static BufferedReader bufferedReader(Reader r) { return r instanceof BufferedReader ? (BufferedReader) r : new BufferedReader(r); } static void swingLater(long delay, final Object r) { javax.swing.Timer timer = new javax.swing.Timer(toInt(delay), actionListener(r)); timer.setRepeats(false); timer.start(); } static void swingLater(Object r) { SwingUtilities.invokeLater(toRunnable(r)); } 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); } } static void sleepInCleanUp(long ms) { try { if (ms < 0) return; Thread.sleep(ms); } catch (Exception __e) { throw rethrow(__e); } } 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; } static int done2_minPrint = 10; static long done2(long startTime, String desc) { return done2(startTime, desc, done2_minPrint); } 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; } static long done2(String desc, long startTime) { return done2(startTime, desc); } static long done2(long startTime) { return done2(startTime, ""); } static DialogIO talkToThisVM() { return new talkToThisVM_IO(); } static class talkToThisVM_IO extends DialogIO { List answers = ll(thisVMGreeting()); boolean isLocalConnection() { return true; } boolean isStillConnected() { return true; } int getPort() { return vmPort(); } void sendLine(String line) { answers.add(or2(sendToThisVM_newThread(line), "?")); } String readLineImpl() { try { return popFirst(answers); } catch (Exception __e) { throw rethrow(__e); } } void close() {} Socket getSocket() { return null; } } static void nohupJavax(final String javaxargs) { { Thread _t_0 = new Thread() { public void run() { try { call(hotwireOnce("#1008562"), "nohupJavax", javaxargs); } catch (Throwable __e) { printStackTrace2(__e); } } }; startThread(_t_0); } } 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) { printStackTrace2(__e); } } }; startThread(_t_1); } } static JFrame showFrame() { return makeFrame(); } static JFrame showFrame(Object content) { return makeFrame(content); } static JFrame showFrame(String title) { return makeFrame(title); } static JFrame showFrame(String title, Object content) { return makeFrame(title, content); } static JFrame showFrame(final JFrame frame) { if (frame != null) { swing(new Runnable() { public void run() { try { if (frameTooSmall(frame)) frameStandardSize(frame); frame.setVisible(true); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (frameTooSmall(frame)) frameStandardSize(frame);\r\n frame.setVisible(true);"; }}); } return frame; } // make or update frame 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; } } static Web parseWeb(String s) { Object o = unstructure(s); if (o instanceof CirclesAndLines) return calToWeb((CirclesAndLines) o); return (Web) o; } 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; } static void putIfNotThere(Map map, A key, B value) { if (map != null && key != null && value != null && !map.containsKey(key)) map.put(key, value); } 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.lol/images/raw/title/" + parsedID; else url = tb_mainServer() + "/tb-int/getfield.php?id=" + parsedID + "&field=title" + standardCredentials(); return trim(loadPageSilently(url)); } catch (Exception __e) { throw rethrow(__e); } } static String getSnippetTitle(long id) { return getSnippetTitle(fsI(id)); } static void cleanKillVM() { ping(); cleanKillVM_noSleep(); sleep(); } static void cleanKillVM_noSleep() { call(getJavaX(), "cleanKill"); } static Web web_similarEmptyWeb(Web web) { Web w = new Web(); w.labelsToUpper = web.labelsToUpper; return w; } static boolean isLetterOrDigit(char c) { return Character.isLetterOrDigit(c); } // f: func -> A (stream ends when f returns null) static IterableIterator iteratorFromFunction(final Object f) { return new IterableIterator() { A a; boolean done; public boolean hasNext() { getNext(); return !done; } public A next() { getNext(); if (done) throw fail(); A _a = a; a = null; return _a; } void getNext() { if (done || a != null) return; a = (A) callF(f); done = a == null; } }; } static int stdHash2(Object a) { if (a == null) return 0; return stdHash(a, toStringArray(allFields(a))); } static File[] listFiles(File dir) { File[] files = dir.listFiles(); return files == null ? new File[0] : files; } static File[] listFiles(String dir) { return listFiles(new File(dir)); } static void listAdd(Collection c, A a) { if (c != null) c.add(a); } // This is for main classes that are all static. // (We don't go to base classes.) static Set listFields(Object c) { TreeSet fields = new TreeSet(); for (Field f : _getClass(c).getDeclaredFields()) fields.add(f.getName()); return fields; } 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 : keys(web.relations)) w.getRelation(assertNotNull(map.get(p.a)), assertNotNull(map.get(p.b))).addLabels(web.getRelation(p).labels()); return map; } static List valuesList(Map map) { return new ArrayList(values(map)); } static void copyStreamAndCloseAll(InputStream in, OutputStream out) { try { try { copyStreamAndCloseInput(in, out); } finally { out.close(); } } catch (Exception __e) { throw rethrow(__e); } } static List allWebsFromCircleEditor() { return map_pcall(new Object() { 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))); } 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; } static boolean loadBufferedImage_useImageCache = true; static BufferedImage loadBufferedImage(String snippetIDOrURL) { try { if (snippetIDOrURL == null) return null; if (isURL(snippetIDOrURL)) return ImageIO.read(new URL(snippetIDOrURL)); if (!isSnippetID(snippetIDOrURL)) throw fail("Not a URL or snippet ID: " + snippetIDOrURL); String snippetID = "" + parseSnippetID(snippetIDOrURL); try { 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(); // fall back to loading from sourceforge } } String imageURL = snippetImageURL(snippetID); System.err.println("Loading image: " + imageURL); BufferedImage image = ImageIO.read(new URL(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")); //Log.info("Cached image."); } //Log.info("Loaded image."); return image; } catch (IOException e) { throw new RuntimeException(e); } } catch (Exception __e) { throw rethrow(__e); } } static BufferedImage loadBufferedImage(File file) { try { return file.isFile() ? ImageIO.read(file) : null; } catch (Exception __e) { throw rethrow(__e); } } static boolean exposeMethods2_debug; static String exposeMethods2(Object receiver, String s, List methodNames) { return exposeMethods2(receiver, s, methodNames, null); } 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)); // we used to have safeUnstructure here 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; } static String classNameToVM(String name) { return name.replace(".", "$"); } static List web_allNodesNamed(Web web, String name) { return web.index.get(web.parseLabel(name)); } static List web_allNodesNamed(Web web, Lisp name) { return web.index.get(name); } 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); } static File loadDataSnippetToFile(String snippetID) { try { snippetID = fsI(snippetID); File f = DiskSnippetCache_file(parseSnippetID(snippetID)); try { URL url = new URL(dataSnippetLink(snippetID)); print("Loading library: " + hideCredentials(url)); try { loadBinaryPageToFile(openConnection(url), f); if (fileSize(f) == 0) throw fail(); } catch (Throwable _e) { url = new URL("http://data.tinybrain.de/blobs/" + psI(snippetID)); print("Trying other server: " + hideCredentials(url)); loadBinaryPageToFile(openConnection(url), f); print("Got bytes: " + fileSize(f)); } // TODO: check if we hit the "LOADING" message if (fileSize(f) == 0) throw fail(); System.err.println("Bytes loaded: " + fileSize(f)); } catch (Throwable e) { printStackTrace(e); throw fail("Binary snippet " + snippetID + " not found or not public"); } return f; } catch (Exception __e) { throw rethrow(__e); } } static Object callJavaX(String method, Object... args) { return callOpt(getJavaX(), method, args); } static boolean isString(Object o) { return o instanceof String; } static String adjectiveToAdverb(String s) { s = replaceSuffixIC("y", "i", s); s = replaceSuffixIC("c", "cal", s); s = dropSuffixIC("le", s); return s + "ly"; } static Iterable talkToAllVMs() { return talkToAll("This is a JavaX VM."); } 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; } 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; // It's inefficient :D for (int j = 0; j < l(x); j++) l.remove(i); l.addAll(i, y); i += l(y); } return l; } static List replaceSublist(List l, int fromIndex, int toIndex, List y) { // inefficient while (toIndex > fromIndex) l.remove(--toIndex); l.addAll(fromIndex, y); return l; } static List toLinesFullTrim_java(String text) { return toLinesFullTrim(joinLines(map("javaDropComments", toLinesFullTrim(text)))); } static MultiMap caseInsensitiveMultiMap() { MultiMap mm = new MultiMap(); mm.data = caseInsensitiveMap(); return mm; } static String dropUntilColon(String s) { if (s == null) return ""; int i = s.indexOf(':'); return trim(i >= 0 ? substring(s, i+1) : s); } static char lastCharacter(String s) { return empty(s) ? 0 : s.charAt(l(s)-1); } static Class getClass(String name) { try { return Class.forName(name); } catch (ClassNotFoundException e) { return null; } } static Class getClass(Object o) { return o instanceof Class ? (Class) o : o.getClass(); } 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); } } static String formatInt(int i, int digits) { return padLeft(str(i), '0', digits); } static int findCodeTokens(List tok, String... tokens) { return findCodeTokens(tok, 1, false, tokens); } static int findCodeTokens(List tok, boolean ignoreCase, String... tokens) { return findCodeTokens(tok, 1, ignoreCase, tokens); } static int findCodeTokens(List tok, int startIdx, boolean ignoreCase, String... tokens) { return findCodeTokens(tok, startIdx, ignoreCase, tokens, null); } static List findCodeTokens_specials = litlist("*", "", "", "", "\\*"); static boolean findCodeTokens_debug; static int findCodeTokens_indexed, findCodeTokens_unindexed; static int findCodeTokens_bails, findCodeTokens_nonbails; 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++; } // bail out early if first token not found (works great with IndexedList) if (!findCodeTokens_specials.contains(tokens[0]) && !tok.contains(tokens[0] /*, startIdx << no signature in List for this, unfortunately */)) { ++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 || checkCondition(condition, tok, i-1)) // pass N index return i; } return -1; } static boolean web_matchNodeLists_debug; 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; // Score nodes for (int i = 0; i < l(l1); i++) { WebNode a = l1.get(i), b = l2.get(i); if (web_isAnythingVariable(a)) continue; //if (neq(web_labels(a), web_labels(b))) --score; if (!containsAllIC_lisp(web_labels(b), web_labels(a))) --score; } // Score relations 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; } static JTextField onEnter(JTextField tf, final Object action) { if (action == null || tf == null) return tf; tf.addActionListener(actionListener(action)); return tf; } static JButton onEnter(JButton btn, final Object action) { if (action == null || btn == null) return btn; btn.addActionListener(actionListener(action)); return btn; } static JList onEnter(JList list, Object action) { list.addKeyListener(enterKeyListener(action)); return list; } // editable only static JComboBox onEnter(final JComboBox cb, final Object action) { JTextField text = (JTextField) cb.getEditor().getEditorComponent(); /*onEnter(text, r { cb.hidePopup(); pcallF(action); });*/ onEnter(text, action); return cb; } static Dimension getScreenSize() { return Toolkit.getDefaultToolkit().getScreenSize(); } static Cache> knownNouns_cache = new Cache("knownNouns_load"); static Set knownNouns() { return knownNouns_cache.get(); } static Set knownNouns_load() { return new TreeSet(tlft(loadSnippet("#1011082"))); } static int parseHexByte(String s) { return Integer.parseInt(s, 16); } static Map newFindBot2_cache = synchroHashMap(); static boolean newFindBot2_verbose; 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", "?"); // put some hello string in (yes, this should be improved.) return io; } // bot not there anymore - remove cache entry 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; } 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) { // look for '&' while (i < len && input.charAt(i-1) != '&') i++; if (i >= len) break; // found '&', look for ';' 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; } // found escape if (input.charAt(i) == '#') { // numeric escape 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 { // named escape 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); } // skip escape st = j + 1; i = st; } if (writer != null) { writer.append(input.substring(st, len)); return writer.toString(); } return input; } private static final String[][] htmldecode_ESCAPES = { {"\"", "quot"}, // " - double-quote {"&", "amp"}, // & - ampersand {"<", "lt"}, // < - less-than {">", "gt"}, // > - greater-than // Mapping to escape ISO-8859-1 characters to their named HTML 3.x equivalents. {"\u00A0", "nbsp"}, // non-breaking space {"\u00A1", "iexcl"}, // inverted exclamation mark {"\u00A2", "cent"}, // cent sign {"\u00A3", "pound"}, // pound sign {"\u00A4", "curren"}, // currency sign {"\u00A5", "yen"}, // yen sign = yuan sign {"\u00A6", "brvbar"}, // broken bar = broken vertical bar {"\u00A7", "sect"}, // section sign {"\u00A8", "uml"}, // diaeresis = spacing diaeresis {"\u00A9", "copy"}, // copyright sign {"\u00AA", "ordf"}, // feminine ordinal indicator {"\u00AB", "laquo"}, // left-pointing double angle quotation mark = left pointing guillemet {"\u00AC", "not"}, // not sign {"\u00AD", "shy"}, // soft hyphen = discretionary hyphen {"\u00AE", "reg"}, // registered trademark sign {"\u00AF", "macr"}, // macron = spacing macron = overline = APL overbar {"\u00B0", "deg"}, // degree sign {"\u00B1", "plusmn"}, // plus-minus sign = plus-or-minus sign {"\u00B2", "sup2"}, // superscript two = superscript digit two = squared {"\u00B3", "sup3"}, // superscript three = superscript digit three = cubed {"\u00B4", "acute"}, // acute accent = spacing acute {"\u00B5", "micro"}, // micro sign {"\u00B6", "para"}, // pilcrow sign = paragraph sign {"\u00B7", "middot"}, // middle dot = Georgian comma = Greek middle dot {"\u00B8", "cedil"}, // cedilla = spacing cedilla {"\u00B9", "sup1"}, // superscript one = superscript digit one {"\u00BA", "ordm"}, // masculine ordinal indicator {"\u00BB", "raquo"}, // right-pointing double angle quotation mark = right pointing guillemet {"\u00BC", "frac14"}, // vulgar fraction one quarter = fraction one quarter {"\u00BD", "frac12"}, // vulgar fraction one half = fraction one half {"\u00BE", "frac34"}, // vulgar fraction three quarters = fraction three quarters {"\u00BF", "iquest"}, // inverted question mark = turned question mark {"\u00C0", "Agrave"}, // ? - uppercase A, grave accent {"\u00C1", "Aacute"}, // ? - uppercase A, acute accent {"\u00C2", "Acirc"}, // ? - uppercase A, circumflex accent {"\u00C3", "Atilde"}, // ? - uppercase A, tilde {"\u00C4", "Auml"}, // ? - uppercase A, umlaut {"\u00C5", "Aring"}, // ? - uppercase A, ring {"\u00C6", "AElig"}, // ? - uppercase AE {"\u00C7", "Ccedil"}, // ? - uppercase C, cedilla {"\u00C8", "Egrave"}, // ? - uppercase E, grave accent {"\u00C9", "Eacute"}, // ? - uppercase E, acute accent {"\u00CA", "Ecirc"}, // ? - uppercase E, circumflex accent {"\u00CB", "Euml"}, // ? - uppercase E, umlaut {"\u00CC", "Igrave"}, // ? - uppercase I, grave accent {"\u00CD", "Iacute"}, // ? - uppercase I, acute accent {"\u00CE", "Icirc"}, // ? - uppercase I, circumflex accent {"\u00CF", "Iuml"}, // ? - uppercase I, umlaut {"\u00D0", "ETH"}, // ? - uppercase Eth, Icelandic {"\u00D1", "Ntilde"}, // ? - uppercase N, tilde {"\u00D2", "Ograve"}, // ? - uppercase O, grave accent {"\u00D3", "Oacute"}, // ? - uppercase O, acute accent {"\u00D4", "Ocirc"}, // ? - uppercase O, circumflex accent {"\u00D5", "Otilde"}, // ? - uppercase O, tilde {"\u00D6", "Ouml"}, // ? - uppercase O, umlaut {"\u00D7", "times"}, // multiplication sign {"\u00D8", "Oslash"}, // ? - uppercase O, slash {"\u00D9", "Ugrave"}, // ? - uppercase U, grave accent {"\u00DA", "Uacute"}, // ? - uppercase U, acute accent {"\u00DB", "Ucirc"}, // ? - uppercase U, circumflex accent {"\u00DC", "Uuml"}, // ? - uppercase U, umlaut {"\u00DD", "Yacute"}, // ? - uppercase Y, acute accent {"\u00DE", "THORN"}, // ? - uppercase THORN, Icelandic {"\u00DF", "szlig"}, // ? - lowercase sharps, German {"\u00E0", "agrave"}, // ? - lowercase a, grave accent {"\u00E1", "aacute"}, // ? - lowercase a, acute accent {"\u00E2", "acirc"}, // ? - lowercase a, circumflex accent {"\u00E3", "atilde"}, // ? - lowercase a, tilde {"\u00E4", "auml"}, // ? - lowercase a, umlaut {"\u00E5", "aring"}, // ? - lowercase a, ring {"\u00E6", "aelig"}, // ? - lowercase ae {"\u00E7", "ccedil"}, // ? - lowercase c, cedilla {"\u00E8", "egrave"}, // ? - lowercase e, grave accent {"\u00E9", "eacute"}, // ? - lowercase e, acute accent {"\u00EA", "ecirc"}, // ? - lowercase e, circumflex accent {"\u00EB", "euml"}, // ? - lowercase e, umlaut {"\u00EC", "igrave"}, // ? - lowercase i, grave accent {"\u00ED", "iacute"}, // ? - lowercase i, acute accent {"\u00EE", "icirc"}, // ? - lowercase i, circumflex accent {"\u00EF", "iuml"}, // ? - lowercase i, umlaut {"\u00F0", "eth"}, // ? - lowercase eth, Icelandic {"\u00F1", "ntilde"}, // ? - lowercase n, tilde {"\u00F2", "ograve"}, // ? - lowercase o, grave accent {"\u00F3", "oacute"}, // ? - lowercase o, acute accent {"\u00F4", "ocirc"}, // ? - lowercase o, circumflex accent {"\u00F5", "otilde"}, // ? - lowercase o, tilde {"\u00F6", "ouml"}, // ? - lowercase o, umlaut {"\u00F7", "divide"}, // division sign {"\u00F8", "oslash"}, // ? - lowercase o, slash {"\u00F9", "ugrave"}, // ? - lowercase u, grave accent {"\u00FA", "uacute"}, // ? - lowercase u, acute accent {"\u00FB", "ucirc"}, // ? - lowercase u, circumflex accent {"\u00FC", "uuml"}, // ? - lowercase u, umlaut {"\u00FD", "yacute"}, // ? - lowercase y, acute accent {"\u00FE", "thorn"}, // ? - lowercase thorn, Icelandic {"\u00FF", "yuml"}, // ? - lowercase y, umlaut {"\u2013", "ndash"}, {"\u2018", "lsquo"}, {"\u2019", "rsquo"}, {"\u201D", "rdquo"}, {"\u201C", "ldquo"}, {"\u2014", "mdash"}, {"'", "apos"}, // the controversial (but who cares!) ' // stackoverflow.com/questions/2083754/why-shouldnt-apos-be-used-to-escape-single-quotes }; private static final HashMap htmldecode_lookupMap; static { htmldecode_lookupMap = new HashMap(); for (final CharSequence[] seq : htmldecode_ESCAPES) htmldecode_lookupMap.put(seq[1].toString(), seq[0]); } static int month() { return Calendar.getInstance().get(Calendar.MONTH)+1; } static TreeSet toCaseInsensitiveSet(Collection c) { // TODO: check if it already is a CI set TreeSet set = caseInsensitiveSet(); if (c != null) set.addAll(c); return set; } static JFrame consoleFrame() { return (JFrame) getOpt(get(getJavaX(), "console"), "frame"); } // TODO: does not detect set type (hash/tree) when it's synchronized static Set cloneSet(Set set) { // assume mutex is equal to collection, which will be true unless you explicitly pass a mutex to synchronizedList() which no one ever does. synchronized(set) { return set instanceof TreeSet ? new TreeSet(set) : new HashSet(set); } } static JTextArea jtextarea() { return jTextArea(); } static JTextArea jtextarea(String text) { return jTextArea(text); } static boolean endsWithLetter(String s) { return nempty(s) && isLetter(last(s)); } static WeakReference creator_class; static Class creator() { return creator_class == null ? null : creator_class.get(); } static boolean equals(Object a, Object b) { return a == null ? b == null : a.equals(b); } static Object deref(Object o) { if (o instanceof Derefable) o = ((Derefable) o).get(); return o; } static A println(A a) { return print(a); } 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; } 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) { printStackTrace2(__e); } return programsToScan; } static int dayOfMonth() { return days(); } static /*cached*/ List downloadedTypicalWebs() { List webs = new ArrayList(); long _startTime_2293 = 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("Unpacking webs", _startTime_2293); return webs; } static int systemHashCode(Object o) { return identityHashCode(o); } static File DiskSnippetCache_file(long snippetID) { return new File(getGlobalCache(), "data_" + snippetID + ".jar"); } // Data files are immutable, use centralized cache 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); } static byte[] loadDataSnippetImpl(String snippetID) throws IOException { byte[] data; try { URL url = new URL("http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + parseSnippetID(snippetID) + "&contentType=application/binary"); System.err.println("Loading library: " + 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)); System.err.println("Loading library: " + url); data = loadBinaryPage(url.openConnection()); } System.err.println("Bytes loaded: " + data.length); } catch (FileNotFoundException e) { throw new IOException("Binary snippet #" + snippetID + " not found or not public"); } return data; } // f must return a list static List concatMap(Object f, Iterable l) { return concatLists(map(f, l)); } static List concatMap(Iterable l, Object f) { return concatMap(f, l); } static List concatMap(Object f, Object[] l) { return concatLists(map(f, l)); } 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); } static List websMadeByProgram(String progID) { List webs = new ArrayList(); // TODO: clean up file handle in case of error IterableIterator l = scanLog_safeUnstructure_iterator(progID, "webs-made.txt"); String src = progID; for (Object o : l) try { String structure = (String) getOpt(o, "structure"); if (structure != null) webs.add(web_intern(web_setSourceIfEmpty((Web) unstructure(structure), src))); } catch (Throwable __e) { printStackTrace2(__e); } return webs; } static WeakHashMap> callOpt_noArgs_cache = new WeakHashMap(); static Object callOpt_noArgs(Object o, String method) { try { if (o == null) return null; if (o instanceof Class) return callOpt(o, method); // not optimized 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); } } // used internally - we are in synchronized block 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; } static Cache> knownPluralNouns_cache = new Cache("knownPluralNouns_load"); static Set knownPluralNouns() { return knownPluralNouns_cache.get(); } static Set knownPluralNouns_load() { return new TreeSet(map("plural",knownNouns())); } static long fileSize(String path) { return getFileSize(path); } static long fileSize(File f) { return getFileSize(f); } static String afterSpace(String s) { return dropUntilSpace(s); } static List ai_texts_bidirectionalOperands(String s, String rel) { return concatLists(ai_texts(s, rel, "$X"), ai_texts("$X", rel, s)); } // mainJava is a complete program, but without the !752/!759 at the top // returns link to main class static Class veryQuickJava(String mainJava) { return veryQuickJava3(mainJava); // Latest version with internal compiler bot. } static boolean isLocalhost(String ip) { return isLoopbackIP(ip) || eqic(ip, "localhost"); } static boolean isImageServerSnippet(long id) { return id >= 1100000 && id < 1200000; } static Web web_setSourceIfEmpty(Web web, String src) { if (web == null) return null; if (empty(web.source)) web.source = src; return web; } static int days() { return Calendar.getInstance().get(Calendar.DAY_OF_MONTH); } static String thisVMGreeting() { List record_list = (List) ( get(getJavaX(), "record_list")); Object android = first(record_list); // Should be of class Android3 return getString(android, "greeting"); } static boolean containsAllIC_lisp(Collection a, Collection b) { for (Lisp o : b) if (!containsLispIC(a, o)) return false; return true; } static KeyListener enterKeyListener(final Object action) { return new KeyAdapter() { public void keyReleased(KeyEvent ke) { if (ke.getKeyCode() == KeyEvent.VK_ENTER) pcallF(action); } }; } static JTextArea jTextArea() { return jTextArea(""); } static JTextArea jTextArea(final String text) { return swing(new F0() { JTextArea get() { try { return new JTextArea(text) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "new JTextArea(text)"; }}); } static List reversed(Collection l) { return reversedList(l); } static WeakHashMap makeFrame_myFrames = new WeakHashMap(); static JFrame makeFrame() { return makeFrame((Component) null); } static JFrame makeFrame(Object content) { return makeFrame(programTitle(), content); } static JFrame makeFrame(String title) { return makeFrame(title, null); } static JFrame makeFrame(String title, Object content) { return makeFrame(title, content, true); } static JFrame makeFrame(final String title, final Object content, final boolean showIt) { return swing(new F0() { JFrame get() { try { if (getFrame(content) != null) return getFrame(setFrameTitle((Component) content, title)); final JFrame frame = new JFrame(title); makeFrame_myFrames.put(frame, Boolean.TRUE); JComponent wrapped = wrap(content); if (wrapped != null) frame.getContentPane().add(wrapped); frame.setBounds(defaultNewFrameBounds()); if (showIt) frame.setVisible(true); //callOpt(content, "requestFocus"); //exitOnFrameClose(frame); standardTitlePopupMenu(frame); return frame; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (getFrame(content) != null)\r\n ret getFrame(setFrameTitle((Component) ..."; }}); } 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..."; }}); } } static IterableIterator scanLog_safeUnstructure_iterator(String progID, String fileName) { return scanLog_safeUnstructure_iterator(getProgramFile(progID, fileName)); } static IterableIterator scanLog_safeUnstructure_iterator(String fileName) { return scanLog_safeUnstructure_iterator(getProgramFile(fileName)); } static IterableIterator scanLog_safeUnstructure_iterator(File file) { final Iterator it = scanLog_iterator(file); return iteratorFromFunction(new Object() { Object get() { try { while (it.hasNext()) try { return safeUnstructure(it.next()); } catch (Throwable __e) { printStackTrace2(__e); } return null; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "while (it.hasNext()) pcall {\r\n ret safeUnstructure(it.next());\r\n }\r\n ..."; }}); } static int myVMPort() { List records = (List) ( get(getJavaX(), "record_list")); Object android = records.get(records.size()-1); return (Integer) get(android, "port"); } static Web web_setSource(String source, Web web) { web.source = source; return web; } static boolean frameTooSmall(JFrame frame) { return frame.getWidth() < 100 || frame.getHeight() < 50; } static String joinLines(List lines) { return fromLines(lines); } static String joinLines(String glue, String text) { return join(glue, toLines(text)); } static boolean checkCondition(Object condition, Object... args) { return isTrue(callF(condition, args)); } static String plural(String s) { return getPlural(s); } static boolean veryQuickJava_silent = true; static boolean veryQuickJava_useCompilerBot = true; // we always use it now // mainJava is a complete program, but without the !752/!759 at the top // returns link to main class static Class veryQuickJava3(String mainJava) { return veryQuickJava3(mainJava, emptyList()); } static Class veryQuickJava3(String mainJava, List libs) { transpileRaw_silent = veryQuickJava_silent; String src = transpileRaw(mainJava); // transpiled, with lib references libs = cloneList(libs); src = findTranslators2(src, libs); //print("Libs found: " + struct(libs)); String dehlibs = join(" ", libs); File bytecode = null; bytecode = javaCompile_overInternalBot(src, dehlibs); return hotwireCore(concatLists(ll(bytecode), loadLibraries(libs))); } static int lastIndexOf(String a, String b) { return a == null || b == null ? -1 : a.lastIndexOf(b); } static List listFileNames(File dir) { return map("fileName",listFiles(dir)); } static String replaceSuffixIC(String a, String b, String s) { if (endsWithIC(s, a)) return dropLast(s, l(a)) + b; return s; } static void copyStreamAndCloseInput(InputStream in, OutputStream out) { try { try { copyStream(in, out); } finally { in.close(); } } catch (Exception __e) { throw rethrow(__e); } } 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; } static Class hotwireOnce(String programID) { return hotwireCached(programID, false); } static Iterable talkToAll(String searchPattern) { final List list = quickBotScan(searchPattern); return mapI(list, new Object() { Object get(ProgramScan.Program p) { try { return talkTo(p.port) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "talkTo(p.port)"; }}); } static Iterable talkToAll() { return talkToAll(""); } static String dropSuffixIC(String suffix, String s) { return s == null ? null : ewic(s, suffix) ? s.substring(0, l(s)-l(suffix)) : s; } static ActionListener actionListener(final Object runnable) { if (runnable instanceof ActionListener) return (ActionListener) runnable; return new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent _evt) { try { callF(runnable); } catch (Throwable __e) { messageBox(__e); }}}; } static String javaDropComments(String s) { List tok = javaTok(s); replaceLastElement(tok, ""); return join(tok); } static List map_pcall(Iterable l, Object f) { return map_pcall(f, l); } 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) { printStackTrace2(__e); } return x; } static List downloadedTypicalDiagramStructures() { Map map = safeUnstructureMap(loadTextFile(getProgramFile("#1010484", "typical.structure"))); if (map == null) return ll(); return downloadedDiagramStructures(map(values(map), new Object() { Object get(String md5) { try { return getProgramFile("#1010484", md5) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "getProgramFile(\"#1010484\", md5)"; }})); } static void frameStandardSize(JFrame frame) { frame.setBounds(300, 100, 500, 400); } static Object[] asObjectArray(List l) { return toObjectArray(l); } static String padLeft(String s, char c, int n) { return rep(c, n-l(s)) + s; } // default to space static String padLeft(String s, int n) { return padLeft(s, ' ', n); } static String dataSnippetLink(String snippetID) { long id = parseSnippetID(snippetID); if (id >= 1200000 && id < 1300000) { String pw = muricaPassword(); if (empty(pw)) throw fail("Please set 'murica password by running #1008829"); return "http://ai1.lol/1008823/raw/" + id + "?_pass=" + pw; // XXX, although it typically gets hidden when printing } else return "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + id + "&contentType=application/binary"; } static int identityHashCode(Object o) { return System.identityHashCode(o); } static void loadBinaryPageToFile(String url, File file) { try { print("Loading " + url); loadBinaryPageToFile(openConnection(new URL(url)), file); } catch (Exception __e) { throw rethrow(__e); } } static void loadBinaryPageToFile(URLConnection con, File file) { try { setHeaders(con); loadBinaryPageToFile_noHeaders(con, file); } catch (Exception __e) { throw rethrow(__e); } } static void loadBinaryPageToFile_noHeaders(URLConnection con, File file) { try { File ftemp = new File(f2s(file) + "_temp"); FileOutputStream buf = newFileOutputStream(mkdirsFor(ftemp)); InputStream inputStream = con.getInputStream(); try { long len = 0; try { len = con.getContentLengthLong(); } 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."); } buf.close(); buf = null; file.delete(); ftemp.renameTo(file); inputStream.close(); } finally { if (buf != null) buf.close(); } } catch (Exception __e) { throw rethrow(__e); } } static String quoteUnlessIdentifierOrInteger(String s) { return quoteIfNotIdentifierOrInteger(s); } static String ok2(String s) { return "ok " + s; } static boolean isURL(String s) { return s.startsWith("http://") || s.startsWith("https://"); } static boolean isLoopbackIP(String ip) { return eq(ip, "127.0.0.1"); } static String snippetImageURL(String snippetID) { return snippetImageURL(snippetID, "png"); } 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 = "http://ai1.lol/images/raw/" + id; else url = "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + id + "&contentType=image/" + contentType; return url; } 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; } static WebNode cloneNodeToWeb(WebNode node, Web w) { WebNode n = w.newNode(); n.addLabels(web_labels(node)); copyFields(node, n, "x", "y"); return n; } 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); } static String sendToThisVM_newThread(String s, Object... args) { final String _s = format(s, args); try { return (String) evalInNewThread(new Object() { 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); } } static File getCacheProgramDir() { return getCacheProgramDir(getProgramID()); } static File getCacheProgramDir(String snippetID) { return new File(userHome(), "JavaX-Caches/" + formatSnippetIDOpt(snippetID)); } static A popFirst(List l) { if (empty(l)) return null; A a = first(l); l.remove(0); return a; } 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)); } static boolean web_isAnythingVariable(WebNode n) { return endsWithIC(web_text(n), " [Anything]"); } static IterableIterator scanLog_iterator(String progID, String fileName) { return scanLog_iterator(getProgramFile(progID, fileName)); } static IterableIterator scanLog_iterator(String fileName) { return scanLog_iterator(getProgramFile(fileName)); } static IterableIterator scanLog_iterator(File file) { // TODO: opens all at once final Iterator it = chainIterators(map("linesFromFile", concatLists(earlierPartsOfLogFile(file), ll(file)))); return iteratorFromFunction(new F0() { 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)..."; }}); } static A setFrameTitle(A c, String title) { JFrame f = getFrame(c); if (f == null) showFrame(title, c); else f.setTitle(title); return c; } static A setFrameTitle(String title, A c) { return setFrameTitle(c, title); } // magically find a field called "frame" in main class :-) static JFrame setFrameTitle(String title) { Object f = getOpt(mc(), "frame"); if (f instanceof JFrame) return setFrameTitle((JFrame) f, title); return null; } public static File mkdirsFor(File file) { return mkdirsForFile(file); } static String fileName(File f) { return f == null ? null : f.getName(); } // probably better than findTranslators (uses tokens) // removes invocations from src 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); } static Rectangle defaultNewFrameBounds_r = new Rectangle(300, 100, 500, 400); static Rectangle defaultNewFrameBounds() { return swing(new F0() { 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..."; }}); } static String muricaPassword() { return trim(loadTextFile(muricaPasswordFile())); } static Web web_useCLParse(Web web, boolean b) { return web; } static List downloadedDiagramStructures() { return downloadedDiagramStructures(listFilesOfProgram_notDirs("#1010484")); } 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)) { //print("GZipped!"); 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; } static JFrame getFrame(final Object _o) { return swing(new F0() { 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..."; }}); } static void replaceLastElement(List l, A a) { if (nempty(l)) l.set(l(l)-1, a); } static File javaCompile_overInternalBot(String src) { return javaCompile_overInternalBot(src, ""); } // returns path to jar static synchronized File javaCompile_overInternalBot(String src, String dehlibs) { return CompilerBot.compile(src, dehlibs); } static TreeMap hotwireCached_cache = new TreeMap(); static Lock hotwireCached_lock = lock(); static Class hotwireCached(String programID) { return hotwireCached(programID, true); } static Class hotwireCached(String programID, boolean runMain) { Lock _lock_2442 = hotwireCached_lock; lock(_lock_2442); try { programID = formatSnippetID(programID); Class c = hotwireCached_cache.get(programID); if (c == null) { c = hotwire(programID); if (runMain) callMain(c); hotwireCached_cache.put(programID, c); } return c; } finally { unlock(_lock_2442); } } static int hashCode(Object a) { return a == null ? 0 : a.hashCode(); } static List loadLibraries(List snippetIDs) { return map("loadLibrary", snippetIDs); } static Class hotwireCore(List urlsOrFiles) { List urls = map("toURL", urlsOrFiles); return hotwireCore((URL[]) asArray(URL.class, urls)); } static Class hotwireCore(URL... urls) { try { // make class loader URLClassLoader classLoader = new URLClassLoader(urls); // load & return main class Class theClass = classLoader.loadClass("main"); setOpt(theClass, "__javax", getJavaX()); setOpt(theClass, "programID", "#3999999"); if (!_inCore()) hotwire_copyOver(theClass); return theClass; } catch (Exception __e) { throw rethrow(__e); } } static boolean endsWithIC(String a, String b) { return endsWithIgnoreCase(a, b); } static String quoteIfNotIdentifierOrInteger(String s) { if (s == null) return null; return isJavaIdentifier(s) || isInteger(s) ? s : quote(s); } static boolean containsLispIC(Collection l, Lisp s) { for (Lisp x : l) if (lispEqic(x, s)) return true; return false; } 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) { printStackTrace2(__e); } } }; startThread(_t_0); } flag.waitUntilUp(); if (exception.has()) throw rethrow(exception.get()); return var.get(); } static Class transpileRaw_trans; static Object transpileRaw_silent = true; static boolean transpileRaw_useDiskCache; static Lock transpileRaw_lock = lock(); static String transpileRaw(String mainJava) { Lock _lock_2460 = transpileRaw_lock; lock(_lock_2460); try { File cacheFile = null; if (transpileRaw_useDiskCache) { String md5 = md5(mainJava); cacheFile = new File(javaxCodeDir(), "Transpilations/" + md5 + ".java"); { String _a_2462 = loadTextFile(cacheFile); if (!empty(_a_2462)) return _a_2462; } } transpileRaw_translator(); set(transpileRaw_trans, "mainJava", mainJava); callMain(transpileRaw_trans); String main = (String) ( get(transpileRaw_trans, "mainJava")); if (transpileRaw_useDiskCache) saveTextFile(cacheFile, main); return main; } finally { unlock(_lock_2460); } } static Class transpileRaw_translator() { if (transpileRaw_trans == null) transpileRaw_trans = hotwireSilently("#759"); setOpt(transpileRaw_trans, "print_silent", transpileRaw_silent); return transpileRaw_trans; } // apply a function to an iterator 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()); } }; } static IterableIterator mapI(Iterator i, Object f) { return mapI(f, i); } static IterableIterator mapI(Iterable i, Object f) { return mapI(f, i.iterator()); } static void revalidate(final Component c) { if (c == null || !c.isShowing()) return; { swing(new Runnable() { public void run() { try { // magic combo to actually relayout and repaint 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..."; }}); } } static void standardTitlePopupMenu(final JFrame frame) { // standard right-click behavior on titles if (isSubstanceLAF()) titlePopupMenu(frame, new Object() { 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..."; }}); } 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..."; }}); } } static void messageBox(Throwable e) { showConsole(); printStackTrace(e); messageBox(str(innerException(e))); } static List buttonsInGroup(ButtonGroup g) { if (g == null) return ll(); return asList(g.getElements()); } // menuMaker = voidfunc(JPopupMenu) 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(getFrame(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..."; }}); } static File muricaPasswordFile() { return new File(javaxSecretDir(), "murica/muricaPasswordFile"); } static void clearTokens(List tok) { clearAllTokens(tok); } static void clearTokens(List tok, int i, int j) { clearAllTokens(tok, i, j); } 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; } static boolean lispEqic(Lisp a, String b) { if (a == null) return b == null; return nempty(a.args) && eqic(a.head, b); } static Iterator chainIterators(Collection> l) { final List> _l = new LinkedList(l); return iteratorFromFunction(new Object() { Object 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..."; }}); } static boolean fileStartsWith(File f, byte[] data) { return byteArrayEquals(loadBeginningOfBinaryFile(f, l(data)), data); } static URL toURL(Object o) { try { if (o instanceof URL) return (URL) o; if (o instanceof String) return new URL((String) o); if (o instanceof File) return fileToURL((File) o); throw fail("Can't convert to URL: " + o); } catch (Exception __e) { throw rethrow(__e); } } static boolean isPossibleMD5(String s) { return isMD5(s); } static List listFilesOfProgram_notDirs(String programID) { return asList(listFilesOnly(programDir(programID))); } static boolean headless() { return isHeadless(); } static JMenuItem jmenuItem(String text, final Object r) { JMenuItem mi = new JMenuItem(text); mi.addActionListener(actionListener(r)); return mi; } static List getFieldOfAllConceptClasses(Concepts cc, String field) { return notNullOnly(c_collect(values(cc.concepts), field)); } static byte[] isGZ_magic = bytesFromHex("1f8b"); static boolean isGZ(byte[] data) { return byteArrayStartsWith(data, isGZ_magic); } static boolean isGZ(File f) { return fileStartsWith(f, isGZ_magic); } static JMenuItem jMenuItem(String text, Object r) { return jmenuItem(text, r); } static Rectangle screenRectangle() { return new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); } static void toggleAlwaysOnTop(JFrame frame) { frame.setAlwaysOnTop(!frame.isAlwaysOnTop()); } static void duplicateThisProgram() { nohupJavax(trim(programID() + " " + smartJoin((String[]) get(getJavaX(), "fullArgs")))); } static boolean isSubstanceLAF() { return substanceLookAndFeelEnabled(); } static JCheckBoxMenuItem jCheckBoxMenuItem(String text, boolean checked, final Object r) { JCheckBoxMenuItem mi = new JCheckBoxMenuItem(text, checked); mi.addActionListener(actionListener(r)); return mi; } static void shootWindowGUI_external(JFrame frame) { call(hotwireOnce("#1007178"), "shootWindowGUI", frame); } static void shootWindowGUI_external(final JFrame frame, int delay) { call(hotwireOnce("#1007178"), "shootWindowGUI", frame, delay); } static Object[] asArray(List l) { return toObjectArray(l); } static A[] asArray(Class type, List l) { return (A[]) l.toArray((Object[]) Array.newInstance(type, l.size())); } static Class hotwireSilently(String src) { Object j = getJavaX(); Boolean b = (Boolean) ( getThreadLocalOpt(j, "loadPage_silent")); setThreadLocalOpt(j, "loadPage_silent", true); try { return hotwire(src); } finally { setThreadLocalOpt(j, "loadPage_silent", b); } } static List scanQuotedLogLines(String text) { List l = new ArrayList(); for (String s : toLines(text)) if (isProperlyQuoted(s)) l.add(unquote(s)); return l; } static String loadGZippedTextFile(File file) { return loadGZTextFile(file); } static Throwable innerException(Throwable e) { return getInnerException(e); } static class componentPopupMenu_Maker { List menuMakers = new ArrayList(); } static Map componentPopupMenu_map = new WeakHashMap(); static ThreadLocal componentPopupMenu_mouseEvent = new ThreadLocal(); // menuMaker = voidfunc(JPopupMenu) static void componentPopupMenu(final JComponent component, final Object menuMaker) { swingNowOrLater(new Runnable() { public void run() { try { componentPopupMenu_Maker maker = componentPopupMenu_map.get(component); if (maker == null) { componentPopupMenu_map.put(component, maker = new componentPopupMenu_Maker()); component.addMouseListener(new componentPopupMenu_Adapter(maker)); } maker.menuMakers.add(menuMaker); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "componentPopupMenu_Maker maker = componentPopupMenu_map.get(component);\r\n ..."; }}); } static class componentPopupMenu_Adapter extends MouseAdapter { componentPopupMenu_Maker maker; componentPopupMenu_Adapter(componentPopupMenu_Maker maker) { this.maker = maker;} public void mousePressed(MouseEvent e) { displayMenu(e); } public void mouseReleased(MouseEvent e) { displayMenu(e); } void displayMenu(MouseEvent e) { if (e.isPopupTrigger()) displayMenu2(e); } void displayMenu2(MouseEvent e) { JPopupMenu menu = new JPopupMenu(); int emptyCount = menu.getComponentCount(); componentPopupMenu_mouseEvent.set(e); for (Object menuMaker : maker.menuMakers) pcallF(menuMaker, menu); // show menu if any items in it if (menu.getComponentCount() != emptyCount) menu.show(e.getComponent(), e.getX(), e.getY()); } } static byte[] bytesFromHex(String s) { return hexToBytes(s); } static void swingNowOrLater(Runnable r) { if (isAWTThread()) r.run(); else swingLater(r); } static boolean substanceLookAndFeelEnabled() { return startsWith(getLookAndFeel(), "org.pushingpixels."); } 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; } static File javaxSecretDir_dir; // can be set to work on different base dir static File javaxSecretDir() { return javaxSecretDir_dir != null ? javaxSecretDir_dir : new File(userHome(), "JavaX-Secret"); } static URL fileToURL(File f) { try { return f.toURI().toURL(); } catch (Exception __e) { throw rethrow(__e); } } static JComponent getTitlePaneComponent(Window window) { if (!substanceLookAndFeelEnabled()) return null; JRootPane rootPane = null; if (window instanceof JFrame) rootPane = ((JFrame) window).getRootPane(); if (window instanceof JDialog) rootPane = ((JDialog) window).getRootPane(); if (rootPane != null) { Object /*SubstanceRootPaneUI*/ ui = rootPane.getUI(); return (JComponent) call(ui, "getTitlePane"); } return null; } static void clearAllTokens(List tok) { for (int i = 0; i < tok.size(); i++) tok.set(i, ""); } static void clearAllTokens(List tok, int i, int j) { for (; i < j; i++) tok.set(i, ""); } 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; } static List notNullOnly(List l) { List out = new ArrayList(); for (A a : unnull(l)) if (a != null) out.add(a); return out; } static List notNullOnly(A... l) { return notNullOnly(asList(l)); } 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; } static List listFilesOnly(String dir) { return listFilesOnly(new File(dir)); } static List listFilesOnly(File... dirs) { return concatMap(rcurry("listFilesWithSuffix", ""), dirs); } static void setThreadLocalOpt(Object o, String name, Object value) { ThreadLocal t = (ThreadLocal) ( getOpt(o, name)); if (t != null) t.set(value); } static Object getThreadLocalOpt(Object o, String name) { ThreadLocal t = (ThreadLocal) ( getOpt(o, name)); return t != null ? t.get() : null; } static byte[] loadBeginningOfBinaryFile(File file, int maxBytes) { return loadBinaryFilePart(file, 0, maxBytes); } static String loadGZTextFile(File file) { try { if (!file.isFile()) return null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream fis = new FileInputStream(file); GZIPInputStream gis = new GZIPInputStream(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()); // TODO: use a Reader } catch (Exception __e) { throw rethrow(__e); } } 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); } } 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; } static String getLookAndFeel() { return getClassName(UIManager.getLookAndFeel()); } 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() { Object get() { return callF(f, arg); } }; if (n == 2) return new F1() { Object get(Object a) { return callF(f, a, arg); } }; throw todo("currying a function with " + n + "arguments"); } static int numberOfFunctionArguments(Object f) { if (f instanceof F1) return 1; if (f instanceof String) return numberOfMethodArguments(mc(), (String) f); return numberOfMethodArguments(f, "get"); } 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(); 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; } static class SortedCondensedStrings2 extends CondensedStrings2 { SortedCondensedStrings2() {} 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; } } abstract static class VirtualNodeIndex { abstract int numWebs(); abstract List get(String query); } static class BetterThreadLocal { Map map = newWeakHashMap(); A get() { return map.get(currentThread()); } A get(Thread thread) { return map.get(thread); } void set(A a) { mapPutOrRemove(map, currentThread(), a); } } // f is a function or an instance of Cache (the dependee) static class DependentCache extends Cache { Object f, lastDep; DependentCache() {} DependentCache(Object maker, Object f) { this.f = f; this.maker = maker;} A get() { if (hasLock(lock)) return value; // Must be called from within maker { Lock _lock_2588 = lock; lock(_lock_2588); 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(_lock_2588); } } return super.get(); } } // allows skipping patterns when they don't apply static class MatchLearner extends Concept { static String _fieldOrder = "pattern examples"; String pattern; Map examples = mapInConcept(this, litorderedmap()); // maps to 'assumed, 'confirmed or 'rejected } static class SoftwareMadeWeb { long date; String computerID, programID; String structure; // web or cal Map furtherInfo; } static class Drawing extends Concept { static String _fieldOrder = "globalID name calStructure"; String globalID = aGlobalID(); String name; String calStructure; CirclesAndLines cal() { return (CirclesAndLines) unstructure(calStructure); } } static class Either { byte which; Object value; Either() {} Either(int which, Object value) { this.value = value; this.which = (byte) which; } boolean isA() { return which == 1; } boolean isB() { return which == 2; } A a() { if (which != 1) _failMe(); return (A) value; } B b() { if (which != 2) _failMe(); return (B) value; } void _failMe() { throw fail("Either object is of wrong type: " + shortClassName(value)); } } static abstract class F0 { abstract A get(); } static abstract class F1 { abstract B get(A a); } static class Cache { Object maker; // func -> A A value; long loaded; static boolean debug; long changeCount; Lock lock = lock(); Cache() {} Cache(Object maker) { this.maker = maker;} A get() { if (hasLock(lock)) return value; // Must be called from within maker Lock _lock_2601 = lock; lock(_lock_2601); try { if (loaded == 0) { value = (A) callF(maker); changeCount++; loaded = sysNow(); } return value; } finally { unlock(_lock_2601); } } void clear() { Lock _lock_2602 = lock; lock(_lock_2602); try { if (debug && loaded != 0) print("Clearing cache"); value = null; changeCount++; loaded = 0; } finally { unlock(_lock_2602); } } // clear if older than x seconds // 0 does not do anything void clear(double seconds) { Lock _lock_2603 = lock; lock(_lock_2603); try { if (seconds != 0 && loaded != 0 && sysNow() >= loaded+seconds*1000) clear(); } finally { unlock(_lock_2603); } } // override void set(A a) { Lock _lock_2604 = lock; lock(_lock_2604); try { value = a; ++changeCount; loaded = sysNow(); } finally { unlock(_lock_2604); } } } static class BaseBase { String globalID = aGlobalID(); String text; String textForRender() { return text; } } static boolean traits_multiLine = true; static class Base extends BaseBase { List traits = new ArrayList(); boolean hasTrait(String t) { return containsIC(traits(), t); } List traits() { if (nempty(text) && neq(first(traits), text)) traits.add(0, text); return traits; } void addTraits(List l) { setAddAll(traits(), l); } void addTrait(String t) { if (nempty(t)) setAdd(traits(), t); } String textForRender() { List traits = traits(); if (traits_multiLine) return lines(traits); if (l(traits) <= 1) return first(traits); return first(traits) + " [" + join(", ", dropFirst(traits)) + "]"; } void setText(String text) { this.text = text; traits = ll(text); } } static class CirclesAndLines { List circles = new ArrayList(); List lines = new ArrayList(); Class arrowClass = Arrow.class; Class circleClass = Circle.class; String title; String globalID = aGlobalID(); long created = nowUnlessLoading(); transient Lock lock = fairLock(); transient String defaultImageID = "#1007372"; transient double imgZoom = 1; // zoom for the circle images transient Pt translate; Circle hoverCircle; // which one we are hovering over transient Object onUserMadeArrow, onUserMadeCircle, onLayoutChange; transient Object onFullLayoutChange, onDeleteCircle, onDeleteLine; transient Object onRenameCircle, onRenameLine, onStructureChange; transient BufferedImage imageForUserMadeNodes; static int maxDistanceToLine = 20; // for clicking transient String backgroundImageID = defaultBackgroundImageID; static String defaultBackgroundImageID = "#1007195"; static Color defaultLineColor = Color.white; static boolean debugRender; static Object staticPopupExtender; transient double scale = 1; // zoom whole image transient boolean recordHistory = true; List history; // auto-visualize 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)))); } String makeVisualizationText(String text) { return possibleGlobalID(text) ? "" : text; } Circle circle_autoVis(String text, double x, double y) { return circle_autoVis(text, makeVisualizationText(text), x, y); } Circle circle(BufferedImage img, double x, double y, String text) { return addAndReturn(circles, nu(circleClass, "x", x, "y", y, "text", text, "img" , processImage(img))); } Circle circle(String text, BufferedImage img, double x, double y) { return circle(img, x, y, text); } Circle circle(String text, double x, double y) { return addAndReturn(circles, nu(circleClass, "x", x, "y", y, "text", text, "img" , processImage(imageForUserMadeNodes()))); } Circle addCircle(String imageID, double x, double y) { return addCircle(imageID, x, y, ""); } 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)))); } Arrow findArrow(Circle a, Circle b) { for (Line l : getWhere(lines, "a", a, "b", b)) if (l instanceof Arrow) return (Arrow) l; return null; } 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; } Arrow arrow(Circle a, String text, Circle b) { return addArrow(a, b, text); } Arrow addArrow(Circle a, Circle b) { return addArrow(a, b, ""); } Arrow addArrow(Circle a, Circle b, String text) { return addAndReturn(lines, nu(arrowClass, "a", a, "b", b, "text", text)); } 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 Object() { void get(Graphics2D g) { try { g.scale(scale, scale); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "g.scale(scale, scale);"; }}); // Lines 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 /*l.color*/); } } // Circles 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; } Canvas showAsFrame(int w, int h) { Canvas canvas = showAsFrame(); frameInnerSize(canvas, w, h); centerFrame(getFrame(canvas)); return canvas; } Canvas showAsFrame() { return (Canvas) swing(new Object() { 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;"; }}); } Canvas makeCanvas() { final Object makeImg = new Object() { Object get(int w, int 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 Object() { void get(JPopupMenu menu) { try { // POPUP MENU START 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) { printStackTrace2(__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); // POPUP MENU END } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "// POPUP MENU START\r\n Pt p = pointFromEvent(canvas, componentPopupMenu_m..."; }}); return canvas; } 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..."; }})); } Canvas show() { return showAsFrame(); } Canvas show(int w, int h) { return showAsFrame(w, h); } 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; } 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..."; }}); } 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..."; }}); } void clear() { clearAll(circles, lines); } // only finds actually containing circles 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(); } 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(); } BufferedImage processImage(BufferedImage img) { return scaleImage(img, imgZoom); } 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(); } void deleteLine(Line l) { lines.remove(l); pcallF(onDeleteLine, l); schange(); } 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 Object() { 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)"; }}); } void schange() { pcallF(onStructureChange); logQuoted("user-web-edits", now() + " " + cal_structure(this)); markWebsPosted(); } Circle findOrMakeCircle(String text) { Circle c = findCircle(text); if (c != null) return c; return makeCircle(text); } 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; } BufferedImage imageForUserMadeNodes() { if (imageForUserMadeNodes == null) imageForUserMadeNodes = whiteImage(20, 20); return imageForUserMadeNodes; } 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(); } Pt pointFromEvent(ImageSurface canvas, MouseEvent e) { return scalePt(canvas.pointFromEvent(e), 1/scale); } void historyLog(Object o) { if (!recordHistory) return; if (history == null) history = new ArrayList(); history.add(o); } } // end of class CirclesAndLines static class Circle extends Base { transient BufferedImage img; double x, y; //static BufferedImage defaultImage; String quickvis; BufferedImage img(CirclesAndLines cal) { if (img != null) return img; if (nempty(quickvis)) img = quickVisualize(quickvis); //if (defaultImage == null) defaultImage = loadImage2(#1007372); return cal.imageForUserMadeNodes(); } Pt pt2(CirclesAndLines cal, ImageSurface is) { return pt2(is.getWidth(), is.getHeight(), cal); } Pt pt(CirclesAndLines cal, ImageSurface is) { return pt(is.getWidth(), is.getHeight(), cal); } Pt pt(int w, int h, CirclesAndLines cal) { return new Pt(iround(x*w/cal.scale), iround(y*h/cal.scale)); } Pt pt2(int w, int h, CirclesAndLines cal) { return new Pt(iround(x*w), iround(y*h)); } DoublePt doublePt(int w, int h, CirclesAndLines cal) { return new DoublePt(x*w/cal.scale, y*h/cal.scale); } boolean contains(CirclesAndLines cal, ImageSurface is, Pt p) { return pointDistance(p, pt2(cal, is)) <= iround(thoughtCircleSize(img(cal))*cal.scale)/2+1; } } static class Line extends Base { Circle a, b; transient Color color = CirclesAndLines.defaultLineColor; Line setColor(Color color) { this.color = color; return this; } } static class Arrow extends Line {} static class CircleDragger extends MouseAdapter { CirclesAndLines cal; ImageSurface is; Object update; int dx, dy; Circle circle; Pt startPoint; 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()); //printVars("mousePressed", +dx, +dy, +p, cx := circle.x, cy := circle.y, w := is.getWidth(), h := 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(); //printVars("mouseDragged", +dx, +dy, +p, cx := circle.x, cy := circle.y, w := is.getWidth(), h := 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; } } static class Lowest { A best; double score; transient Object onChange; boolean isNewBest(double score) { return best == null || score < this.score; } double bestScore() { return best == null ? Double.NaN : score; } float floatScore() { return best == null ? Float.NaN : (float) score; } float floatScoreOr(float defaultValue) { return best == null ? defaultValue : (float) score; } boolean put(A a, double score) { if (a != null && isNewBest(score)) { best = a; this.score = score; pcallF(onChange); return true; } return false; } A get() { return best; } boolean has() { return best != null; } } static class Best { A best; double score; transient Object onChange; boolean isNewBest(double score) { return best == null || !isNaN(score) && score > this.score; } double bestScore() { return best == null ? Double.NaN : score; } double score() { return bestScore(); } double getScore() { return bestScore(); } boolean put(A a, double score) { if (a != null && isNewBest(score)) { best = a; this.score = score; pcallF(onChange); return true; } return false; } A get() { return best; } boolean has() { return best != null; } Pair pair() { return main.pair(best, bestScore()); } A getIfScoreAbove(double x) { return score() >= x ? best : null; } } static interface Producer { public A next(); } static class CompilerBot { static boolean verbose; static File compileSnippet(String snippetID) { return compileSnippet(snippetID, ""); } static Pair compileSnippet2(String snippetID) { return compileSnippet2(snippetID, ""); } // returns jar path static File compileSnippet(String snippetID, String javaTarget) { return compileSnippet2(snippetID, javaTarget).a; } // returns jar path, Java source 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); } static File compile(String src) { return compile(src, ""); } static File compile(String src, String libs) { return compile(src, libs, null); } static File compile(String src, String dehlibs, String javaTarget) { return compile(src, dehlibs, javaTarget, null); } static File compile(String src, String dehlibs, String javaTarget, String progID) { if (verbose) print("Compiling " + l(src) + " chars"); // Note: This is different from the calculation in x30 // (might lead to programs being compiled twice) String md5 = md5(dehlibs + "\n" + src + "\n" + progID); File jar = getJarFile(md5); if (jar == null || jar.length() <= 22) { // have to compile 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); // so we can find the unused ones easier } return jar; } static File getJarFile(String md5) { assertTrue(isMD5(md5)); return new File(getCacheProgramDir("#1002203"), md5 + ".jar"); } } static class RemoteDB { DialogIO db; String name; // s = bot name or snippet ID RemoteDB(String s) { this(s, false); } 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"); } boolean functional() { return db != null; } // now always true List list() { return adopt((List) rpc(db, "xlist")); } List list(String className) { return adopt((List) rpc(db, "xlist", className)); } List xlist() { return list(); } List xlist(String className) { return list(className); } // adopt is an internal method List adopt(List l) { if (l != null) for (RC rc : l) adopt(rc); return l; } RC adopt(RC rc) { if (rc != null) rc.db = this; return rc; } Object adopt(Object o) { if (o instanceof RC) return adopt((RC) o); return o; } String xclass(RC o) { return (String) rpc(db, "xclass", o); } Object xget(RC o, String field) { return adopt(rpc(db, "xget", o, field)); } String xS(RC o, String field) { return (String) xget(o, field); } RC xgetref(RC o, String field) { return adopt((RC) xget(o, field)); } void xset(RC o, String field, Object value) { rpc(db, "xset", o, field, value); } RC uniq(String className) { RC ref = first(list(className)); if (ref == null) ref = xnew(className); return ref; } RC xuniq(String className) { return uniq(className); } RC xnew(String className, Object... values) { return adopt((RC) rpc(db, "xnew", className, values)); } void xdelete(RC o) { rpc(db, "xdelete", o); } void xdelete(List l) { rpc(db, "xdelete", l); } void close() { if (db != null) db.close(); } String fullgrab() { return (String) rpc(db, "xfullgrab"); } String xfullgrab() { return fullgrab(); } void xshutdown() { rpc(db, "xshutdown"); } long xchangeCount() { return (long) rpc(db, "xchangeCount"); } int xcount() { return (int) rpc(db, "xcount"); } void reconnect() { close(); db = findBot(name); } RC rc(long id) { return new RC(this, id); } } static ThreadLocal DynamicObject_loading = new ThreadLocal(); static class DynamicObject { String className; // just the name, without the "main$" LinkedHashMap fieldValues = new LinkedHashMap(); DynamicObject() {} // className = just the name, without the "main$" DynamicObject(String className) { this.className = className;} } // you still need to implement hasNext() and next() static abstract class IterableIterator implements Iterator, Iterable { public Iterator iterator() { return this; } public void remove() { unsupportedOperation(); } } /** this class is fully thread-safe */ static class Flag { private boolean up; /** returns true if flag was down before */ 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"; } // currently does a semi-active wait with latency = 50 ms public void waitForThisOr(Flag otherFlag) { try { while (!isUp() && !otherFlag.isUp()) Thread.sleep(50); } catch (Exception __e) { throw rethrow(__e); } } } static class T3 { A a; B b; C c; T3() {} T3(A a, B b, C c) { this.c = c; this.b = b; this.a = a;} public int hashCode() { return main.hashCode(a) + 2*main.hashCode(b) - 4*main.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); } } static class Pair { A a; B b; Pair() {} 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 + ">"; } } static class Var implements IVar { A v; // you can access this directly if you use one thread Var() {} 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()); } } static class ProgramScan { static int threads = isWindows() ? 500 : 10; static int timeout = 5000; // hmm... static String ip = "127.0.0.1"; // This range is not used anymore anyway static int quickScanFrom = 10000, quickScanTo = 10999; static int maxNumberOfVMs_android = 4; // Android will always only have one if we don't screw up static int maxNumberOfVMs_nonAndroid = 50; // 100; static int maxNumberOfVMs; static boolean verbose; static class Program { int port; String helloString; Program(int port, String helloString) { this.helloString = helloString; this.port = port;} } static List scan() { try { return scan(1, 65535); } catch (Exception __e) { throw rethrow(__e); } } static List scan(int fromPort, int toPort) { return scan(fromPort, toPort, new int[0]); } 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 < Future < Program > > 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); } //stopTiming("Port Scan " + scanSize + ", " + n(threads, "threads") + ": ", 250); if (verbose) print("Found " + programs.size() + " " + name + "(s) on " + ip); return programs; } catch (Exception __e) { throw rethrow(__e); } } 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); //if (verbose) print("Connected to " + ip + ":" + port); 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; } } }); } static List quickScan() { return scan(quickScanFrom, quickScanTo); } static List quickBotScan() { return quickBotScan(new int[0]); } static List quickBotScan(int[] preferredPorts) { if (maxNumberOfVMs == 0) maxNumberOfVMs = isAndroid() ? maxNumberOfVMs_android : maxNumberOfVMs_nonAndroid; return scan(4999, 5000+maxNumberOfVMs-1, preferredPorts); } } // a Lisp-like form static class Lisp implements Iterable { String head; List args; // O more; // additional info, user-defined Lisp() {} Lisp(String head) { this.head = _compactString(head); } Lisp(String head, Lisp... args) { this.head = head; argsForEdit().addAll(asList(args)); } Lisp(String head, Collection args) { this.head = _compactString(head); for (Object arg : args) add(arg); } List argsForEdit() { return args == null ? (args = new ArrayList()) : args; } // INEFFICIENT 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 + "}"; // list else return quoteIfNotIdentifier(head) + "(" + inner + ")"; } String raw() { if (!isEmpty ()) throw fail("not raw: " + this); return head; } Lisp add(Lisp l) { argsForEdit().add(l); return this; } Lisp add(String s) { argsForEdit().add(new Lisp(s)); return this; } 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; } int size() { return l(args); } boolean empty() { return main.empty(args); } boolean isEmpty() { return main.empty(args); } boolean isLeaf() { return main.empty(args); } Lisp get(int i) { return main.get(args, i); } String getString(int i) { Lisp a = get(i); return a == null ? null : a.head; } String s(int i) { return getString(i); } String rawOrNull(int i) { Lisp a = get(i); return a != null && a.isLeaf() ? a.head : null; } String raw(int i) { return assertNotNull(rawOrNull(i)); } boolean isLeaf(int i) { return rawOrNull(i) != null; } boolean isA(String head) { return eq(head, this.head); } boolean is(String head, int size) { return isA(head) && size() == size; } boolean is(String head) { return isA(head); } boolean headIs(String head) { return isA(head); } boolean is(String... heads) { return asList(heads).contains(head); } // check head for one of these (ignore case) boolean isic(String... heads) { return containsIgnoreCase(heads, head); } public Iterator iterator() { return main.iterator(args); } Lisp subList(int fromIndex, int toIndex) { Lisp l = new Lisp(head); l.argsForEdit().addAll(args.subList(fromIndex, toIndex)); // better to copy here I guess - safe 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); } Lisp addAll(List args) { for (Object arg : args) add(arg); return this; } String unquoted() { return unquote(raw()); } String unq() { return unquoted(); } String unq(int i) { return get(i).unq(); } // heads of arguments List heads() { return collect(args, "head"); } } // not changed yet: allow cookie to be read right after being set static abstract class NanoHTTPD { static ThreadLocal currentSession = new ThreadLocal(); /** * Pluggable strategy for asynchronously executing requests. */ public interface AsyncRunner { void closeAll(); void closed(ClientHandler clientHandler); void exec(ClientHandler code); } /** * The runnable that will be used for every new client connection. */ public class ClientHandler implements Runnable { private final InputStream inputStream; final Socket acceptSocket; private 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 = wrapStuff("SocketOutputStream", this.acceptSocket.getOutputStream(), this.acceptSocket); 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.execute(); } } catch (Exception e) { // When the socket is closed by the client, // we throw our own SocketException // to break the "keep alive" loop above. If // the exception was anything other // than the expected SocketException OR a // SocketTimeoutException, print the // stacktrace 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()); } 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); } } /** * Provides rudimentary support for cookies. Doesn't support 'path', * 'secure' nor 'httpOnly'. Feel free to improve it and/or add unsupported * features. * * @author LordFokas */ public class CookieHandler implements Iterable { private final HashMap cookies = new HashMap(); private 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]); } } } } /** * Set a cookie with an expiration date from a month ago, effectively * deleting it on the client side. * * @param name * The cookie name. */ public void delete(String name) { set(name, "-delete-", -30); } @Override public Iterator iterator() { return this.cookies.keySet().iterator(); } /** * Read a cookie from the HTTP Headers. * * @param name * The cookie's name. * @return The cookie's value if it exists, null otherwise. */ public String read(String name) { return this.cookies.get(name); } public void set(Cookie cookie) { this.queue.add(cookie); //cookies.put(cookie.n, cookie.v); // CHANGED } /** * Sets a cookie. * * @param name * The cookie's name. * @param value * The cookie's value. * @param expires * How many days until the cookie expires. */ public void set(String name, String value, int expires) { this.queue.add(new Cookie(name, value, Cookie.getHTTPTime(expires))); } /** * Internally used by the webserver to add all queued cookies into the * Response's HTTP Headers. * * @param response * The Response object to which headers the queued cookies * will be added. */ public void unloadQueue(Response response) { for (Cookie cookie : this.queue) { response.addHeader("Set-Cookie", cookie.getHTTPHeader()); } } } /** * Default threading strategy for NanoHTTPD. *

*

* By default, the server spawns a new Thread for every incoming request. * These are set to daemon status, and named according to the request * number. The name is useful when profiling the application. *

*/ public static class DefaultAsyncRunner implements AsyncRunner { private long requestCount; private final List running = Collections.synchronizedList(new ArrayList()); /** * @return a list with currently running clients. */ public List getRunning() { return running; } @Override public void closeAll() { // copy of the list for concurrency 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); //t.setDaemon(true); String clientIP = "?"; try { clientIP = clientHandler.acceptSocket.getInetAddress().getHostAddress().toString(); } catch (Throwable __e) { printStackTrace2(__e); } t.setName("NanoHttpd serving request #" + this.requestCount + " to " + clientIP); this.running.add(clientHandler); t.start(); } } /** * Default strategy for creating and cleaning up temporary files. *

*

* By default, files are created by File.createTempFile() in * the directory specified. *

*/ public static class DefaultTempFile implements TempFile { private final File file; private 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; } } /** * Default strategy for creating and cleaning up temporary files. *

*

* This class stores its files in the standard location (that is, wherever * java.io.tmpdir points to). Files are added to an internal * list, and deleted when no longer needed (that is, when * clear() is invoked at the end of processing a request). *

*/ public static class DefaultTempFileManager implements TempFileManager { private final String tmpdir; private final List tempFiles; public DefaultTempFileManager() { this.tmpdir = tempDir().getPath(); // use JavaX, dude // System.getProperty("java.io.tmpdir"); 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; } } /** * Default strategy for creating and cleaning up temporary files. */ private class DefaultTempFileManagerFactory implements TempFileManagerFactory { @Override public TempFileManager create() { return new DefaultTempFileManager(); } } private static final String CONTENT_DISPOSITION_REGEX = "([ |\t]*Content-Disposition[ |\t]*:)(.*)"; private static final Pattern CONTENT_DISPOSITION_PATTERN = Pattern.compile(CONTENT_DISPOSITION_REGEX, Pattern.CASE_INSENSITIVE); private static final String CONTENT_TYPE_REGEX = "([ |\t]*content-type[ |\t]*:)(.*)"; private static final Pattern CONTENT_TYPE_PATTERN = Pattern.compile(CONTENT_TYPE_REGEX, Pattern.CASE_INSENSITIVE); private static final String CONTENT_DISPOSITION_ATTRIBUTE_REGEX = "[ |\t]*([a-zA-Z]*)[ |\t]*=[ |\t]*['|\"]([^\"^']*)['|\"]"; private static final Pattern CONTENT_DISPOSITION_ATTRIBUTE_PATTERN = Pattern.compile(CONTENT_DISPOSITION_ATTRIBUTE_REGEX); class HTTPSession implements IHTTPSession { public static final int BUFSIZE = 8192; private final TempFileManager tempFileManager; private final OutputStream outputStream; private final PushbackInputStream inputStream; private int splitbyte; private int rlen; private String uri; private Method method; private Map parms; private Map headers; private CookieHandler cookies; private String queryParameterString; private String remoteIp; private 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 HashMap(); } /** * Decodes the sent headers and loads the data into Key/value pairs */ private void decodeHeader(BufferedReader in, Map pre, Map parms, Map headers) throws ResponseException { try { // Read the request line 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(); // Decode parameters from the URI int qmi = uri.indexOf('?'); if (qmi >= 0) { decodeParms(uri.substring(qmi + 1), parms); uri = decodePercent(uri.substring(0, qmi)); } else { uri = decodePercent(uri); } // If there's another token, its protocol version, // followed by HTTP headers. // NOTE: this now forces header names lower case since they are // case insensitive and vary by client. 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); } } /** * Decodes the Multipart Body data and put it into Key/Value pairs. */ private 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"))); // First line is boundary string 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; // Parse the reset of the header lines 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(); } // Read the part data 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) { // Read the part into a string byte[] data_bytes = new byte[part_data_end - part_data_start]; fbuf.get(data_bytes); parms.put(part_name, new String(data_bytes)); } else { // Read it into a file 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()); } } /** * Decodes parameters in percent-encoded URI-format ( e.g. * "name=Jack%20Daniels&pass=Single%20Malt" ) and adds them to given * Map. NOTE: this doesn't support multiple identical keys due to the * simplicity of Map. */ private 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 { // Read the first 8192 bytes. // The full header should fit in here. // Apache's default header limit is 8KB. // Do NOT assume that a single read will get the entire header // at once! 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) { // socket was been closed safeClose(this.inputStream); safeClose(this.outputStream); throw new SocketException("NanoHttpd Shutdown"); } while (read > 0) { this.rlen += read; 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); } // Create a BufferedReader for parsing the header. BufferedReader hin = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(buf, 0, this.rlen))); // Decode the header into parms and header java properties Map pre = new HashMap(); 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.*")); // Ok, now do the serve() r = serve(this); 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 it out to close socket object (finalAccept) throw e; } catch (SocketTimeoutException ste) { // treat socket timeouts the same way we treat socket exceptions // i.e. close the stream & finalAccept object by throwing the // exception up the call stack. 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 { safeClose(r); this.tempFileManager.clear(); } } /** * Find byte index separating header from body. It must be the last byte * of the first two sequential new lines. */ private 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; } /** * Find the byte positions where multipart boundaries start. This reads * a large block at a time and uses a temporary buffer to optimize * (memory mapped) file access. */ private 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 { // Search the search_window 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) { // Match found, add it to results 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; // Copy the end of the buffer to the start System.arraycopy(search_window, search_window.length - boundary.length, search_window, 0, boundary.length); // Refill search_window 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; } private RandomAccessFile getTmpBucket() { try { TempFile tempFile = this.tempFileManager.createTempFile(); return new RandomAccessFile(tempFile.getName(), "rw"); } catch (Exception e) { throw new Error(e); // we won't recover, so throw an error } } @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; // Store the request in memory or a file, depending on size if (size < MEMORY_STORE_LIMIT) { baos = new ByteArrayOutputStream(); request_data_output = new DataOutputStream(baos); } else { randomAccessFile = getTmpBucket(); request_data_output = randomAccessFile; } // Read all the body and write it to request_data_output 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 the method is POST, there may be parameters // in data section, too, read it: 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)) { // Handle multipart/form-data 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()]; print("NanoHTTPD: Handling POST data (" + l(postBytes) + " bytes)"); fbuf.get(postBytes); String postLine = new String(postBytes).trim(); // Handle application/x-www-form-urlencoded if ("application/x-www-form-urlencoded".equalsIgnoreCase(contentType)) { decodeParms(postLine, this.parms); } else if (postLine.length() != 0) { // Special case for raw POST data => create a // special files entry "postData" with raw content // data files.put("postData", postLine); } } } else if (Method.PUT.equals(this.method)) { files.put("content", saveTmpFile(fbuf, 0, fbuf.limit())); } } finally { safeClose(randomAccessFile); } } /** * Retrieves the content of a sent file and saves it to a temporary * file. The full path to the saved file is returned. */ private 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) { // Catch exception if any throw new Error(e); // we won't recover, so throw an error } finally { safeClose(fileOutputStream); } } return path; } } /** * Handles one session, i.e. parses the HTTP request and returns the * response. */ public interface IHTTPSession { void execute() throws IOException; CookieHandler getCookies(); Map getHeaders(); InputStream getInputStream(); Method getMethod(); Map getParms(); String getQueryParameterString(); /** * @return the path part of the URL. */ String getUri(); /** * Adds the files in the request body to the files map. * * @param files * map to modify */ void parseBody(Map files) throws IOException, ResponseException; } /** * HTTP Request methods, with the ability to decode a String * back to its enum value. */ public enum Method { GET, PUT, POST, DELETE, HEAD, OPTIONS, TRACE, CONNECT, PATCH; static Method lookup(String method) { for (Method m : Method.values()) { if (m.toString().equalsIgnoreCase(method)) { return m; } } return null; } } /** * HTTP response. Return one of these from serve(). */ public static class Response implements Closeable { /** * Output stream that will automatically send every write to the wrapped * OutputStream according to chunked transfer: * http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1 */ private 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()); } } /** * HTTP status code after processing, e.g. "200 OK", Status.OK */ private IStatus status; /** * MIME type of content, e.g. "text/html" */ private String mimeType; /** * Data of the response, may be null. */ private InputStream data; private long contentLength; /** * Headers for the HTTP response. Use addHeader() to add lines. */ //private final Map header = new HashMap(); final Map header = lithashmap("X-Powered-By", "JavaX"); /** * The request method that spawned this response. */ private Method requestMethod; /** * Use chunkedTransfer */ private boolean chunkedTransfer; private boolean encodeAsGzip; private boolean keepAlive; /** * Creates a fixed length response if totalBytes>=0, otherwise chunked. */ protected 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(); } } /** * Adds given line to the header. */ 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; } private boolean headerAlreadySent(Map header, String name) { boolean alreadySent = false; for (String headerName : header.keySet()) { alreadySent |= headerName.equalsIgnoreCase(name); } return alreadySent; } /** * Sends given response to the socket. */ protected 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); } } private 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); } } private void sendBodyWithCorrectEncoding(OutputStream outputStream, long pending) throws IOException { if (encodeAsGzip) { GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream); sendBody(gzipOutputStream, -1); gzipOutputStream.finish(); } else { sendBody(outputStream, pending); } } /** * Sends the body to the specified OutputStream. The pending parameter * limits the maximum amounts of bytes sent unless it is -1, in which * case everything is sent. * * @param outputStream * the OutputStream to send data to * @param pending * -1 to send everything, otherwise sets a max limit to the * number of bytes sent * @throws IOException * if something goes wrong while sending the data. */ private 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; } } } protected 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 { private static final long serialVersionUID = 6569838532917408380L; private 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; } } /** * The runnable that will be used for the main listening thread. */ public class ServerRunnable implements Runnable { private final int timeout; private IOException bindException; private boolean hasBinded = false; private 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 (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()); } } /** * A temp file. *

*

* Temp files are responsible for managing the actual temporary storage and * cleaning themselves up when no longer needed. *

*/ public interface TempFile { void delete() throws Exception; String getName(); OutputStream open() throws Exception; } /** * Temp file manager. *

*

* Temp file managers are created 1-to-1 with incoming requests, to create * and cleanup temporary files created as a result of handling the request. *

*/ public interface TempFileManager { void clear(); TempFile createTempFile() throws Exception; } /** * Factory to create temp file managers. */ public interface TempFileManagerFactory { TempFileManager create(); } /** * Maximum time to wait on Socket.getInputStream().read() (in milliseconds) * This is required as the Keep-Alive HTTP connections would otherwise block * the socket reading thread forever (or as long the browser is open). */ public static int SOCKET_READ_TIMEOUT = 5000; /** * Common MIME type for dynamic content: plain text */ public static final String MIME_PLAINTEXT = "text/plain"; /** * Common MIME type for dynamic content: html */ public static final String MIME_HTML = "text/html"; /** * Pseudo-Parameter to use to store the actual query string in the * parameters map for later re-processing. */ private static final String QUERY_STRING_PARAMETER = "NanoHttpd.QUERY_STRING"; /** * logger to log to. */ private static final Logger LOG = Logger.getLogger(NanoHTTPD.class.getName()); /** * Creates an SSLSocketFactory for HTTPS. Pass a loaded KeyStore and an * array of loaded KeyManagers. These objects must properly * loaded/initialized by the caller. */ 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; } /** * Creates an SSLSocketFactory for HTTPS. Pass a loaded KeyStore and a * loaded KeyManagerFactory. These objects must properly loaded/initialized * by the caller. */ 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; } /** * Creates an SSLSocketFactory for HTTPS. Pass a KeyStore resource with your * certificate and passphrase */ 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; } private 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); } } private final String hostname; private final int myPort; private ServerSocket myServerSocket; private SSLServerSocketFactory sslServerSocketFactory; private Thread myThread; /** * Pluggable strategy for asynchronously executing requests. */ protected AsyncRunner asyncRunner; /** * Pluggable strategy for creating and cleaning up temporary files. */ private TempFileManagerFactory tempFileManagerFactory; /** * Constructs an HTTP server on given port. */ public NanoHTTPD(int port) { this(null, port); } // ------------------------------------------------------------------------------- // // // // Threading Strategy. // // ------------------------------------------------------------------------------- // // /** * Constructs an HTTP server on given hostname and port. */ public NanoHTTPD(String hostname, int port) { this.hostname = hostname; this.myPort = port; setTempFileManagerFactory(new DefaultTempFileManagerFactory()); setAsyncRunner(new DefaultAsyncRunner()); } /** * Forcibly closes all connections that are open. */ public synchronized void closeAllConnections() { stop(); } /** * create a instance of the client handler, subclasses can return a subclass * of the ClientHandler. * * @param finalAccept * the socket the cleint is connected to * @param inputStream * the input stream * @return the client handler */ protected ClientHandler createClientHandler(final Socket finalAccept, final InputStream inputStream) { return new ClientHandler(inputStream, finalAccept); } /** * Instantiate the server runnable, can be overwritten by subclasses to * provide a subclass of the ServerRunnable. * * @param timeout * the socet timeout to use. * @return the server runnable. */ protected ServerRunnable createServerRunnable(final int timeout) { return new ServerRunnable(timeout); } /** * Decode parameters from a URL, handing the case where a single parameter * name might have been supplied several times, by return lists of values. * In general these lists will contain a single element. * * @param parms * original NanoHTTPD parameters values, as passed to the * serve() method. * @return a map of String (parameter name) to * List<String> (a list of the values supplied). */ protected Map> decodeParameters(Map parms) { return this.decodeParameters(parms.get(NanoHTTPD.QUERY_STRING_PARAMETER)); } // ------------------------------------------------------------------------------- // // /** * Decode parameters from a URL, handing the case where a single parameter * name might have been supplied several times, by return lists of values. * In general these lists will contain a single element. * * @param queryString * a query string pulled from the URL. * @return a map of String (parameter name) to * List<String> (a list of the values supplied). */ protected 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; } /** * Decode percent encoded String values. * * @param str * the percent encoded String * @return expanded form of the input, for example "foo%20bar" becomes * "foo bar" */ protected 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; } /** * @return true if the gzip compression should be used if the client * accespts it. Default this option is on for text content and off * for everything else. */ protected 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(); } /** * Call before start() to serve over HTTPS instead of HTTP */ public void makeSecure(SSLServerSocketFactory sslServerSocketFactory) { this.sslServerSocketFactory = sslServerSocketFactory; } /** * Create a response with unknown length (using HTTP 1.1 chunking). */ public Response newChunkedResponse(IStatus status, String mimeType, InputStream data) { return new Response(status, mimeType, data, -1); } /** * Create a response with known length. */ public static Response newFixedLengthResponse(IStatus status, String mimeType, InputStream data, long totalBytes) { return new Response(status, mimeType, data, totalBytes); } /** * Create a text response with known length. */ 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); } } /** * Create a text response with known length. */ public static Response newFixedLengthResponse(String msg) { return newFixedLengthResponse(Status.OK, NanoHTTPD.MIME_HTML, msg); } /** * Override this to customize the server. *

*

* (By default, this returns a 404 "Not Found" plain text error response.) * * @param session * The HTTP session * @return HTTP response, see class Response for details */ 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); } /** * Override this to customize the server. *

*

* (By default, this returns a 404 "Not Found" plain text error response.) * * @param uri * Percent-decoded URI without parameters, for example * "/index.cgi" * @param method * "GET", "POST" etc. * @param parms * Parsed, percent decoded parameters from URI and, in case of * POST, data. * @param headers * Header entries, percent decoded * @return HTTP response, see class Response for details */ @Deprecated public Response serve(String uri, Method method, Map headers, Map parms, Map files) { return newFixedLengthResponse(Status.NOT_FOUND, NanoHTTPD.MIME_PLAINTEXT, "Not Found"); } /** * Pluggable strategy for asynchronously executing requests. * * @param asyncRunner * new strategy for handling threads. */ public void setAsyncRunner(AsyncRunner asyncRunner) { this.asyncRunner = asyncRunner; } /** * Pluggable strategy for creating and cleaning up temporary files. * * @param tempFileManagerFactory * new strategy for handling temp files. */ public void setTempFileManagerFactory(TempFileManagerFactory tempFileManagerFactory) { this.tempFileManagerFactory = tempFileManagerFactory; } /** * Start the server. * * @throws IOException * if the socket is in use. */ public void start() throws IOException { start(NanoHTTPD.SOCKET_READ_TIMEOUT); } /** * Start the server. * * @param timeout * timeout to use for socket connections. * @throws IOException * if the socket is in use. */ public void start(final int timeout) throws IOException { if (this.sslServerSocketFactory != null) { 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.setDaemon(true); this.myThread.setName("NanoHttpd Main Listener"); this.myThread.start(); while (!serverRunnable.hasBinded && serverRunnable.bindException == null) { try { Thread.sleep(10L); } catch (Throwable e) { // on android this may not be allowed, that's why we // catch throwable the wait should be very short because we are // just waiting for the bind of the socket } } if (serverRunnable.bindException != null) { throw serverRunnable.bindException; } System.out.println("HTTP server started (listening on port " + getListeningPort() + "!)"); printMyIPs(); } /** * Stop the server. */ 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; } static void printMyIPs() { String ip; try { Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); // filters out 127.0.0.1 and inactive interfaces 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(); } } static interface IStatus { String getDescription(); int getRequestStatus(); } /** * Some HTTP response status codes */ 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"); private final int requestStatus; private 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; } } } static abstract class DialogIO { String line; boolean eos, loud, noClose; abstract String readLineImpl(); abstract boolean isStillConnected(); abstract void sendLine(String line); abstract boolean isLocalConnection(); abstract Socket getSocket(); abstract void close(); int getPort() { Socket s = getSocket(); return s == null ? 0 : s.getPort(); } boolean helloRead; int shortenOutputTo = 500; String readLineNoBlock() { String l = line; line = null; return l; } boolean waitForLine() { try { if (line != null) return true; //print("Readline"); line = readLineImpl(); //print("Readline done: " + line); if (line == null) eos = true; return line != null; } catch (Exception __e) { throw rethrow(__e); } } String readLine() { waitForLine(); helloRead = true; return readLineNoBlock(); } 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(); } 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; } void pushback(String l) { if (line != null) throw fail(); line = l; helloRead = false; } } static abstract class DialogHandler { abstract void run(DialogIO io); } static class Matches { String[] m; Matches() {} Matches(String... m) { this.m = m;} String get(int i) { return i < m.length ? m[i] : null; } String unq(int i) { return unquote(get(i)); } String fsi(int i) { return formatSnippetID(unq(i)); } String fsi() { return fsi(0); } String tlc(int i) { return unq(i).toLowerCase(); } boolean bool(int i) { return "true".equals(unq(i)); } String rest() { return m[m.length-1]; } // for matchStart int psi(int i) { return Integer.parseInt(unq(i)); } } static class MultiMap { Map> data = new HashMap>(); MultiMap() {} MultiMap(boolean useTreeMap) { if (useTreeMap) data = new TreeMap(); } MultiMap(MultiMap map) { putAll(map); } synchronized void put(A key, B value) { List list = data.get(key); if (list == null) data.put(key, list = new ArrayList()); list.add(value); } synchronized void addAll(A key, Collection values) { putAll(key, values); } synchronized void addAllIfNotThere(A key, Collection values) { for (B value : values) setPut(key, value); } synchronized void setPut(A key, B value) { if (!containsPair(key, value)) put(key, value); } synchronized boolean containsPair(A key, B value) { return get(key).contains(value); } synchronized void putAll(A key, Collection values) { for (B value : values) put(key, value); } synchronized void removeAll(A key, Collection values) { for (B value : values) remove(key, value); } synchronized List get(A key) { List list = data.get(key); return list == null ? Collections. emptyList() : list; } // returns actual mutable live list // creates the list if not there synchronized List getActual(A key) { List list = data.get(key); if (list == null) data.put(key, list = smallEmptyList()); return list; } synchronized void clean(A key) { List list = data.get(key); if (list != null && list.isEmpty()) data.remove(key); } synchronized Set keySet() { return data.keySet(); } synchronized Set keys() { return data.keySet(); } synchronized void remove(A key) { data.remove(key); } synchronized void remove(A key, B value) { List list = data.get(key); if (list != null) { list.remove(value); if (list.isEmpty()) data.remove(key); } } synchronized void clear() { data.clear(); } synchronized boolean containsKey(A key) { return data.containsKey(key); } synchronized B getFirst(A key) { List list = get(key); return list.isEmpty() ? null : list.get(0); } synchronized void putAll(MultiMap map) { for (A key : map.keySet()) putAll(key, map.get(key)); } synchronized int keysSize() { return l(data); } // full size - note: expensive operation synchronized int size() { int n = 0; for (List l : data.values()) n += l(l); return n; } // expensive operation synchronized List reverseGet(B b) { List l = new ArrayList(); for (A key : data.keySet()) if (data.get(key).contains(b)) l.add(key); return l; } synchronized Map> asMap() { return cloneMap(data); } boolean isEmpty() { return data.isEmpty(); } } static UnsupportedOperationException unsupportedOperation() { throw new UnsupportedOperationException(); } static String quickVisualize_progID = "#1007145"; static Lock quickVisualize_lock = lock(); static boolean quickVisualize_hasCached(String query) { return quickVisualize_imageFile(query).length() != 0; } static BufferedImage quickVisualize_fromCache(String query) { File f = quickVisualize_imageFile(query); if (f.length() != 0) try { return loadPNG(f); } catch (Throwable __e) { printStackTrace2(__e); } return null; } static String quickVisualize_preprocess(String query) { return toUpper(shorten(trim(query), 200)); } 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); /*L urls = googleImageSearch_multi(query); saveTextFile(quickVisualize_urlsFile(query), joinLines(urls)); if (empty(urls)) null; img = loadBufferedImage(first(urls));*/ Lock _lock_2645 = quickVisualize_lock; lock(_lock_2645); try { img = googleImageSearchFirst(query); if (img == null) return null; savePNG(f, img); return img; } finally { unlock(_lock_2645); } } static String quickVisualize_imagePath(String query) { query = quickVisualize_preprocess(query); return fsI(quickVisualize_progID) + "/" + urlencode(query) + ".png"; } static File quickVisualize_imageFile(String query) { query = quickVisualize_preprocess(query); return prepareProgramFile(quickVisualize_progID, urlencode(query) + ".png"); } static File quickVisualize_urlsFile(String query) { query = quickVisualize_preprocess(query); return prepareProgramFile(quickVisualize_progID, "urls-" + urlencode(query) + ".txt"); } static List sorted(Collection c, final Object comparator) { List l = cloneList(c); sort(l, makeComparator(comparator)); return l; } static List sorted(Collection c) { List l = cloneList(c); sort(l); return l; } static int thoughtCircleSize(BufferedImage img) { return min(img.getWidth(), img.getHeight()) + 20; } static Concept getConcept(long id) { return mainConcepts.getConcept(id); } static A getConcept(Class cc, long id) { return getConcept(mainConcepts, cc, id); } 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; } static Collection allConcepts() { return mainConcepts.allConcepts(); } static Collection allConcepts(Concepts concepts) { return concepts.allConcepts(); } static A centerFrame(A c) { Window w = getWindow(c); if (w != null) w.setLocationRelativeTo(null); // magic trick return c; } static A centerFrame(int w, int h, A c) { return centerFrame(setFrameSize(w, h, c)); } static String b(Object contents, Object... params) { return tag("b", contents, params); } // firstDelay = delay static java.util.Timer doEvery_daemon(long delay, final Object r) { return doEvery_daemon(delay, delay, r); } static java.util.Timer doEvery_daemon(long delay, long firstDelay, final Object r) { final java.util.Timer timer = new java.util.Timer(true); timer.scheduleAtFixedRate(smartTimerTask(r, timer, delay), firstDelay, delay); return timer; } static float abs(float f) { return Math.abs(f); } static int abs(int i) { return Math.abs(i); } static double abs(double d) { return Math.abs(d); } static String loadConceptsStructure(String progID) { return loadTextFilePossiblyGZipped(getProgramFile(progID, "concepts.structure")); } static String loadConceptsStructure() { return loadConceptsStructure(dbProgramID()); } static String copyTextToClipboard(String text) { StringSelection selection = new StringSelection(text); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, selection); return text; } static BufferedImage whiteImage(int w, int h) { return newBufferedImage(w, h, Color.white); } static void remove(List l, int i) { if (l != null && i >= 0 && i < l(l)) l.remove(i); } static void remove(Collection l, A a) { if (l != null) l.remove(a); } static boolean tok_classHasModifier(List classDecl, String modifier) { int i = classDecl.indexOf("class"); return subList(classDecl, 0, i).contains(modifier); } static int hashCodeFor(Object a) { return a == null ? 0 : a.hashCode(); } static Pt scalePt(Pt p, double f) { return new Pt(iround(p.x*f), iround(p.y*f)); } static String cal_structure(CirclesAndLines cal) { return cal_structure_impl(restructure(cal)); } 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); } static void cal_structure_simplifyElement(Base b) { if (eq(b.traits, ll(b.text))) b.traits = null; } static long toK(long l) { return (l+1023)/1024; } static void addMenuItem(JPopupMenu menu, String text, Object action) { menu.add(jmenuItem(text, action)); } static void addMenuItem(JPopupMenu menu, JMenuItem menuItem) { menu.add(menuItem); } static void addMenuItem(JMenu menu, String text, Object action) { menu.add(jmenuItem(text, action)); } static void addMenuItem(JMenu menu, JMenuItem menuItem) { menu.add(menuItem); } static void clearConcepts() { mainConcepts.clearConcepts(); } static void clearConcepts(Concepts concepts) { concepts.clearConcepts(); } static SortedMap synchroTreeMap() { return Collections.synchronizedSortedMap(new TreeMap()); } static BufferedImage drawThoughtCirclePlus_img; static int drawThoughtCirclePlus_size = 24; 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)); } static double pointDistance(Pt a, Pt b) { return sqrt(sqr(a.x-b.x) + sqr(a.y-b.y)); } static double pointDistance(double x1, double y1, double x2, double y2) { return sqrt(sqr(x1-x2) + sqr(y1-y2)); } static boolean hasType(Collection c, Class type) { for (Object x : c) if (isInstanceX(type, x)) return true; return false; } 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); } static Object rpc(String botName, String method, Object... args) { return unstructure_debug(matchOK2OrFail( sendToLocalBot(botName, rpc_makeCall(method, args)))); } static Object rpc(DialogIO bot, String method, Object... args) { return unstructure_debug(matchOK2OrFail( bot.ask(rpc_makeCall(method, args)))); } static String rpc_makeCall(String method, Object... args) { if (empty(args)) return "call " + method; return format("call *", concatLists((List) ll(method), asList(args))); } static int drawThoughtCircleText_margin = 5; static Color drawThoughtCircleText_color = Color.yellow; 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(); } static boolean containsInstance(Iterable i, Class c) { if (i != null) for (Object o : i) if (isInstanceX(c, o)) return true; return false; } static A firstOfType(Collection c, Class type) { for (Object x : c) if (isInstanceX(type, x)) return (A) x; return null; } static String quoteIfNotIdentifier(String s) { if (s == null) return null; return isJavaIdentifier(s) ? s : quote(s); } static void markWebsPosted() { markWebsPosted_createMarker(); triggerWebsChanged(); } 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; } 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; } static A cnew(Class cc, Object... values) { A c = nuObject(cc); csetAll(c, values); return c; } 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; } static void addAll(Collection c, Collection b) { if (c != null && b != null) c.addAll(b); } static void addAll(Collection c, A... b) { if (c != null) c.addAll(Arrays.asList(b)); } static boolean bareDBMode_on; static void bareDBMode() { bareDBMode(null); // default autoSaveInterval } static void bareDBMode(Integer autoSaveInterval) { bareDBMode_on = true; conceptsAndBot(autoSaveInterval); } static void moveAllMenuItems(JPopupMenu src, JMenu dest) { Component[] l = src.getComponents(); src.removeAll(); for (Component c : l) dest.add(c); } 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); } static int ptY(Pt p) { return p == null ? 0 : p.y; } static int ptX(Pt p) { return p == null ? 0 : p.x; } static int hours() { return hours(Calendar.getInstance()); } static int hours(Calendar c) { return c.get(Calendar.HOUR_OF_DAY); } static File tempDir() { return makeTempDir(); } static String _compactString(String s) { return s; } static void put(Map map, A a, B b) { if (map != null) map.put(a, b); } static A findBackRef(Concept c, Class type) { for (Concept.Ref r : c.backRefs) if (instanceOf(r.concept(), type)) return (A) r.concept(); return null; } static A findBackRef(Class type, Concept c) { return findBackRef(c, type); } static List removeDyn(List l, A a) { if (l == null) return null; l.remove(a); return empty(l) ? null : l; } static A findWhere(Collection c, Object... data) { for (A x : c) if (checkFields(x, data)) return x; return null; } // independent timer static void awtLater(int delay, final Object r) { swingLater(delay, r); } static void awtLater(Object r) { swingLater(r); } // dependent timer (runs only when component is visible) static void awtLater(JComponent component, int delay, Object r) { installTimer(component, r, delay, delay, false); } static void awtLater(JFrame frame, int delay, Object r) { awtLater(frame.getRootPane(), delay, r); } static class Canvas extends ImageSurface { Object makeImg; boolean updating; Canvas() { zoomable = false; } Canvas(Object makeImg) { this(); this.makeImg = makeImg; } void update() { updateCanvas(this, makeImg); } } static Canvas jcanvas() { return jcanvas(null, 0); } // f: (int w, int h) -> BufferedImage static Canvas jcanvas(Object f) { return jcanvas(f, 0); // 100 } static Canvas jcanvas(final Object f, final int updateDelay) { return (Canvas) swing(new Object() { Object get() { try { final Canvas is = new Canvas(f); is.specialPurposed = true; final Runnable update = new Runnable() { 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); // first 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..."; }}); } 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; } static List filterByType(Object[] c, Class type) { return filterByType(asList(c), type); } static int done_minPrint = 10; static long done(long startTime, String desc) { long time = now()-startTime; if (time >= done_minPrint) print(desc + " [" + time + " ms]"); return time; } static long done(String desc, long startTime) { return done(startTime, desc); } static long done(long startTime) { return done(startTime, ""); } static void setAddAll(List a, Collection b) { for (A x : b) setAdd(a, x); } static JLabel jlabel(final String text) { return swingConstruct(BetterLabel.class, text); } static JLabel jlabel() { return jlabel(" "); } static boolean containsIC(Collection l, String s) { return containsIgnoreCase(l, s); } static boolean containsIC(String[] l, String s) { return containsIgnoreCase(l, s); } static boolean containsIC(String s, char c) { return containsIgnoreCase(s, c); } static boolean containsIC(String a, String b) { return containsIgnoreCase(a, b); } static int drawThoughtLineText_shift = 10; 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(); } 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()"; }}; } static void clear(Collection c) { if (c != null) c.clear(); } static String firstToUpper(String s) { if (empty(s)) return s; return Character.toUpperCase(s.charAt(0)) + s.substring(1); } static int updateCanvas_retryInterval = 50; // if makeImg returns null, it is recalled after a delay 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 =..."; }}); } static void updateCanvas(final Canvas canvas) { if (canvas != null) canvas.update(); } static Map mapInConcept(Concept c, Map m) { return new MapInConcept(m, c); } 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); } static String getTextTrim(JTextComponent c) { return trim(getText(c)); } // tested for editable combo box - returns the contents of text field static String getTextTrim(JComboBox cb) { return trim(getText(cb)); } 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); } 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; } // returns actual CNC 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); } static File createTempFile(String prefix, String suffix) { try { // File.createTempFile needs at least 3 prefix characters, so we // fíll them up for you return File.createTempFile(pad(nohup_sanitize(prefix), 3, '-'), nohup_sanitize(suffix)); } catch (Exception __e) { throw rethrow(__e); } } static void saveGZStructureToFile(String file, Object o) { saveGZStructureToFile(getProgramFile(file), o); } 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); } } // will create the file or update its last modified timestamp static void touchFile(File file) { try { closeRandomAccessFile(newRandomAccessFile(mkdirsForFile(file), "rw")); } catch (Exception __e) { throw rethrow(__e); } } static RemoteDB connectToDBOpt(String dbNameOrID) { try { return new RemoteDB(dbNameOrID); } catch (Throwable __e) { return null; } } static AutoComboBox autoComboBox() { return autoComboBox(new ArrayList()); } static AutoComboBox autoComboBox(final Collection items) { return swing(new F0() { 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;"; }}); } static AutoComboBox autoComboBox(String value, Collection items) { return setText(autoComboBox(items), value); } static int drawThoughtArrow_size = 15; 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(); } static JFrame showCenterFrame(String title, int w, int h) { return showCenterFrame(title, w, h, null); } static JFrame showCenterFrame(String title, int w, int h, Component content) { JFrame frame = makeFrame(title, content); frame.setSize(w, h); return centerFrame(frame); } static JFrame showCenterFrame(String title, Component content) { return centerFrame(makeFrame(title, content)); } static JFrame showCenterFrame(Component content) { return centerFrame(makeFrame(content)); } static List addDyn(List l, A a) { if (l == null) l = new ArrayList(); l.add(a); return l; } // returns number of changes static int cset(Concept c, Object... values) { try { 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); //print("cset: " + c.id + " " + field + " " + struct(value) + " " + f); if (value instanceof RC) value = c._concepts.getConcept((RC) value); value = deref(value); if (value instanceof String && l((String) value) >= concepts_internStringsLongerThan) value = ((String) value).intern(); if (f == null) { // TODO: keep ref if it exists 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); } } static Pt untranslatePt(Pt a, Pt b) { if (a == null) return b; return new Pt(b.x-a.x, b.y-a.y); } static String fsi(String id) { return formatSnippetID(id); } 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); } } static BufferedImage quickVisualizeOr(String query, String defaultImageID) { BufferedImage img = quickVisualize(query); return img != null ? img : loadImage2(defaultImageID); } static void change() { //mainConcepts.allChanged(); // safe version for now cause function is sometimes included unnecessarily (e.g. by EGDiff) callOpt(getOptMC("mainConcepts"), "allChanged"); } static String cal_simplifiedStructure(CirclesAndLines cal) { return cal_simplifiedStructure(structure(cal)); } 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); } static HashMap lithashmap(Object... x) { HashMap map = new HashMap(); litmap_impl(map, x); return map; } static String web_defaultRelationName = ""; static String web_defaultRelationName() { return web_defaultRelationName; } 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; } static void frameInnerSize(JFrame frame, Dimension d) { frameInnerSize(frame, d.width, d.height); } static JFrame frameInnerSize(Pt p, JFrame frame) { frameInnerSize(frame, p.x, p.y); return frame; } static Map putAll(Map a, Map b) { if (a != null) a.putAll(b); return a; } static Pt pt(int x, int y) { return new Pt(x, y); } 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; } static JTextField jtextfield() { return jTextField(); } static JTextField jtextfield(String text) { return jTextField(text); } static JTextField jtextfield(Object o) { return jTextField(o); } static long waitForBotStartUp_timeoutSeconds = 60; // returns address or fails 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)); } // before you use this, add a RAM disk cleaner static boolean javaCompileToJar_useRAMDisk; static File javaCompileToJar_optionalRename(String src, File destJar, String progID) { return javaCompileToJar_optionalRename(src, "", destJar, progID); } // returns path to jar static synchronized File javaCompileToJar_optionalRename(String src, String dehlibs, File destJar, String progID) { String javaTarget = null; // use default target print("Compiling " + l(src) + " chars"); 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); //setOpt(j, "verbose", true); File srcDir = tempDir(); String className = "main"; String fileName = dummyClass + ".java"; File mainJava = new File(srcDir, fileName); //print("main java: " + mainJava.getAbsolutePath()); saveTextFile(mainJava, src); File classesDir = javaCompileToJar_useRAMDisk ? tempDirPossiblyInRAMDisk() : tempDir(); print("Compiling to " + f2s(classesDir)); try { List libraries = new ArrayList(); Matcher m = Pattern.compile("\\d+").matcher(dehlibs); while (m.find()) { String libID = m.group(); //print("libID=" + quote(libID)); 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); //fail("Compile Error. " + compilerOutput); } // sanity test if (!new File(classesDir, className + ".class").exists()) throw fail("No class generated (" + className + ")"); // add sources to .jar saveTextFile(new File(classesDir, "main.java"), src); // add information about libraries to jar if (nempty(dehlibs)) saveTextFile(new File(classesDir, "libraries"), dehlibs); //print("Zipping: " + classesDir.getAbsolutePath() + " to " + jar.getAbsolutePath()); dir2zip_recurse_verbose = false; int n = dir2zip_recurse(classesDir, jar); // cache on success only //print("Files zipped: " + n); return jar; } finally { if (isInRAMDisk(classesDir)) deleteDirectory(classesDir); } } //please include function withMargin. static int showForm_defaultGap = 4; static JPanel showFormTitled(final String title, final Object... _parts) { return swing(new F0() { JPanel get() { try { final Var frame = new Var(); JPanel panel = showForm_makePanel(frame, _parts); frame.set(handleEscapeKey(minFrameWidth(showPackedFrame(title, withMargin(panel)), 400))); return panel; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "final new Var frame;\r\n JPanel panel = showForm_makePanel(frame, _p..."; }}); } static JPanel showForm_makePanel(final Var frame, Object... _parts) { List l = new ArrayList(); List parts = asList(_parts); Runnable submit = null; for (int i = 0; i < l(parts); i++) { final Object o = parts.get(i), next = get(parts, i+1); if (o instanceof Component || o instanceof String || next instanceof Component) { // smartAdd accepts strings l.add(ll(o == null ? new JPanel() : o, next)); if (next instanceof JButton && submit == null) submit = new Runnable() { public void run() { try { ((JButton) next).doClick() ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "((JButton) next).doClick()"; }}; i++; } else if (isRunnable(o)) l.add(ll(null, jbutton(showFormSubmitButtonName(), submit = new Runnable() { public void run() { try { Object result = call(o); if (neq(Boolean.FALSE, result)) disposeFrame(frame.get()); } 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)); } JPanel panel = hvgrid((List) l, showForm_defaultGap); if (submit != null) for (JTextField textField : childrenOfType(panel, JTextField.class)) onEnter(textField, submit); return panel; } static A addAndReturn(Collection c, A a) { if (c != null) c.add(a); return a; } static List getWhere(Collection c, Object... data) { List l = new ArrayList(); for (A x : c) if (checkFields(x, data)) l.add(x); return l; } static Color drawThoughtCircle_defaultColor = Color.white; static void drawThoughtCircle(BufferedImage bg, BufferedImage img, double x, double y) { // TODO: crop to certain size 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)); } static boolean isNaN(double d) { return Double.isNaN(d); } static long stopTiming_defaultMin = 10; static long startTiming_startTime; static void startTiming() { startTiming_startTime = now(); } static void stopTiming() { stopTiming(null); } static void stopTiming(String text) { stopTiming(text, stopTiming_defaultMin); } static void stopTiming(String text, long minToPrint) { long time = now()-startTiming_startTime; if (time >= minToPrint) { text = or2(text, "Time: "); print(text + time + " ms"); } } 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); } static ArrayList smallEmptyList() { return new ArrayList(0); } static JMenu jmenu(final String title, final Object... items) { return swing(new F0() { 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;"; }}); } static void mapPutOrRemove(Map map, A key, B value) { if (map != null && key != null) if (value != null) map.put(key, value); else map.remove(key); } // name currently not used, we only call a method called "_wrap" in // the main class with the to-wrapped object as first argument and // a list of other parameters as second argument. static A wrapStuff(String name, A object, Object... args) { //ret or(object, (A) callOpt(mc(), "wrap" + name, // toObjectArray(concatLists(litlist((O) object), toList(args))))); return or((A) callOpt(mc(), "_wrap", object, asList(args)), object); } static BufferedImage renderTiledBackground(String tileImageID, int w, int h) { return renderTiledBackground(tileImageID, w, h, 0, 0); } 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; } static int drawThoughtLine_width = 10; 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(); } static void printMyIPs() { String ip; try { Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); // filters out 127.0.0.1 and inactive interfaces 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(); } } static boolean useDummyMainClasses() { return eq("1", trim(loadTextFile(getProgramFile("#1008755", "use-dummy-main-classes")))); } static Map createGraphics_modulators = synchroIdentityHashMap(); static Graphics2D createGraphics(BufferedImage img) { Graphics2D g = img.createGraphics(); Object mod = createGraphics_modulators.get(img); if (mod != null) callF(mod, g); return g; } // mod: voidfunc(Graphics2D) static void createGraphics_modulate(BufferedImage img, Object mod) { mapPut2(createGraphics_modulators, img, mod); } // uses bilinear interpolation 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); } static boolean hasLock(Lock lock) { return ((ReentrantLock) lock).isHeldByCurrentThread(); } static boolean hasConcept(Class c, Object... params) { return findConceptWhere(c, params) != null; } static void requestFocus(final JComponent c) { focus(c); } static int countConcepts(Class c, Object... params) { return mainConcepts.countConcepts(c, params); } static int countConcepts() { return mainConcepts.countConcepts(); } static int countConcepts(String className) { return mainConcepts.countConcepts(className); } static int countConcepts(Concepts concepts, String className) { return concepts.countConcepts(className); } static List getAll(Map map, Collection l) { return lookupAllOpt(map, l); } static List getAll(Collection l, Map map) { return lookupAllOpt(map, l); } static JTextField jTextField() { return jTextField(""); } static JTextField jTextField(final String text) { return swing(new F0() { 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 ..."; }}); } static JTextField jTextField(Object o) { return jTextField(strOrEmpty(o)); } static int vectorLength(int x, int y) { return isqrt(sqr((long) x)+sqr((long) y)); } static boolean isRunnable(Object o) { return o instanceof Runnable || hasMethod(o, "get"); } static List childrenOfType(Component c, Class theClass) { List l = new ArrayList(); scanForComponents(c, theClass, l); return l; } // It's the super precise version! static double roundToOneHundredth(double d) { return new BigDecimal(d).setScale(2, RoundingMode.HALF_UP).doubleValue(); } static File tempDirPossiblyInRAMDisk() { File f = linux_fileInRamDisk(aGlobalID()); if (f != null) { f.mkdirs(); return f; } return makeTempDir(); } static void closeRandomAccessFile(RandomAccessFile f) { if (f != null) try { f.close(); callJavaX("dropIO", f); } catch (Throwable e) { printStackTrace(e); } } static Comparator makeComparator(final Object f) { return new Comparator() { public int compare(Object a, Object b) { return (int) callF(f, a, b); } }; } 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); } static boolean instanceOf(Object o, Class c) { if (c == null) return false; return c.isInstance(o); } static String pad(String s, int l) { return pad(s, l, ' '); } static String pad(String s, int l, char c) { if (l(s) >= l) return s; return rep(c, l-l(s)) + s; } 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); } } // gotta love convenience & program-smartness static void savePNG(File file, BufferedImage img) { savePNG(img, file); } static void savePNG(File file, RGBImage img) { savePNG(file, img.getBufferedImage()); } static JFrame showPackedFrame(String title, Component contents) { return packFrame(showFrame(title, contents)); } static JFrame showPackedFrame(Component contents) { return packFrame(showFrame(contents)); } static String dynamicClassName(Object o) { if (o instanceof DynamicObject && ((DynamicObject) o).className != null) return "main$" + ((DynamicObject) o).className; return className(o); } 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; } 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; } static List lookupAllOpt(Collection l, Map map) { return lookupAllOpt(map, l); } // TODO: all the fringe cases (?) static JPanel hvgrid(List> components) { if (empty(components)) return new JPanel(); int h = l(components), w = l(first(components)); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(h, w)); for (List row : components) smartAdd(panel, row); return panel; } static JPanel hvgrid(List> components, int gap) { JPanel panel = hvgrid(components); GridLayout g = (GridLayout) ( panel.getLayout()); g.setHgap(gap); g.setVgap(gap); return panel; } 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); } static Object derefRef(Object o) { if (o instanceof Concept.Ref) o = ((Concept.Ref) o).get(); return o; } static boolean isInRAMDisk(File f) { return startsWith(f2s(f), "/dev/shm"); } static boolean isInstance(Class type, Object arg) { return type.isInstance(arg); } static void disposeFrame(final Component c) { disposeWindow(c); } static String getClassDeclarationName(List c) { if (c != null) for (int i = 1; i+2 < c.size(); i += 2) if (eqOneOf(c.get(i), "class", "interface", "enum")) return c.get(i+2); return null; } static List findBlock(String pat, List tok) { List tokpat = javaTok(pat); int i = findCodeTokens(tok, toStringArray(codeTokensOnly(tokpat))); //print("index of block " + quote(pat) + ": " + i); 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); // make it actual CNC } static boolean dir2zip_recurse_verbose; static int dir2zip_recurse(File inDir, File zip) { return dir2zip_recurse(inDir, zip, ""); } // TODO: the zero files case? 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); } } 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); } } static ThreadLocal drawOutlineTextAlongLine_flip = new ThreadLocal(); 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); } 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.getLeading()+fm.getMaxAscent(); // below int y = -shift-fm.getMaxDescent(); // above //print("drawText y=" + y + ", shift=" + shift); AffineTransform old = g.getTransform(); g.transform(tx); drawTextWithOutline(g, text, -fm.stringWidth(text)/2.0f, y+yshift, fillColor, outlineColor); g.setTransform(old); } static JFrame minFrameWidth(JFrame frame, int w) { if (frame != null && frame.getWidth() < w) frame.setSize(w, frame.getHeight()); return frame; } static JFrame minFrameWidth(int w, JFrame frame) { return minFrameWidth(frame, w); } static ThreadLocal imageGraphics_antiAlias = new ThreadLocal(); static Graphics2D imageGraphics(BufferedImage img) { return !isFalse(imageGraphics_antiAlias.get()) ? antiAliasGraphics(img) : createGraphics(img); } // lists returned are actual CNC (N/C/N/.../C/N) - and connected to // original list // only returns the top level classes static List> allClasses(List tok) { List> l = new ArrayList(); for (int i = 1; i < tok.size(); i += 2) { if (eq(tok.get(i), "{")) // skip functions i = findEndOfBlock(tok, i)-1; else if (eqOneOf(tok.get(i), "class", "interface", "enum") && (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; } static List> allClasses(String text) { return allClasses(javaTok(text)); } static String getServerTranspiled(String snippetID) { return getServerTranspiled(snippetID, null); } // returns "SAME" if md5 matches static String getServerTranspiled(String snippetID, String expectedMD5) { try { long id = parseSnippetID(snippetID); /*S t = getTranspilationFromBossBot(id); if (t != null) return t;*/ 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); } } // r may return false to cancel timer static TimerTask smartTimerTask(final Object r, final java.util.Timer timer, final long delay) { return new TimerTask() { long lastRun; public void run() { if (!licensed()) timer.cancel(); else { lastRun = fixTimestamp(lastRun); long now = now(); if (now >= lastRun + delay*0.9) { lastRun = now; if (eq(false, pcallF(r))) timer.cancel(); } } } }; } // first delay = delay static Timer installTimer(JComponent component, Object r, long delay) { return installTimer(component, r, delay, delay); } // first delay = delay static Timer installTimer(RootPaneContainer frame, long delay, Object r) { return installTimer(frame.getRootPane(), r, delay, delay); } // first delay = delay static Timer installTimer(JComponent component, long delay, Object r) { return installTimer(component, r, delay, delay); } static Timer installTimer(JComponent component, long delay, long firstDelay, Object r) { return installTimer(component, r, delay, firstDelay); } static Timer installTimer(final JComponent component, final Object r, final long delay, final long firstDelay) { return installTimer(component, r, delay, firstDelay, true); } static Timer installTimer(final JComponent component, final Object r, final long delay, final long firstDelay, final boolean repeats) { return (Timer) swingAndWait(new Object() { 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 { try { if (!allPaused()) if (isFalse(callF(r))) cancelTimer(timer.get()); } catch (Throwable __e) { printStackTrace2(__e); } } 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..."; }}); } static Timer installTimer(JFrame frame, long delay, long firstDelay, Object r) { return installTimer(frame.getRootPane(), r, delay, firstDelay); } 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; } static String showFormSubmitButtonName() { return "Submit"; } static BufferedImage cutImageToCircle(String imageID) { return cutImageToCircle(loadImage2(imageID)); } static BufferedImage cutImageToCircle(BufferedImage image) { int w = min(image.getWidth(), image.getHeight()); return cutImageToCircle(image, w); } 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; } static boolean setText_opt = true; // optimize by calling getText first static A setText(A c, Object text) { setText((Object) c, text); return c; } static A setText(final A c, Object text) { // only for editable combo boxes at this point 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; } static void setText(JLabel c, Object text) { setText((Object) c, text); } static JButton setText(JButton c, Object text) { setText((Object) c, text); return c; } 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; } static String loadTextFilePossiblyGZipped(String fileName) { return loadTextFilePossiblyGZipped(fileName, null); } static String loadTextFilePossiblyGZipped(String fileName, String defaultContents) { File gz = new File(fileName + ".gz"); return gz.exists() ? loadGZTextFile(gz) : loadTextFile(fileName, defaultContents); } static String loadTextFilePossiblyGZipped(File fileName) { return loadTextFilePossiblyGZipped(fileName, null); } static String loadTextFilePossiblyGZipped(File fileName, String defaultContents) { return loadTextFilePossiblyGZipped(fileName.getPath(), defaultContents); } static int img_minOfWidthAndHeight(BufferedImage image) { return image == null ? 0 : min(image.getWidth(), image.getHeight()); } static BufferedImage loadPNG(File file) { return loadBufferedImage(file); } 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); } static float drawArrowHead_length = 2f; 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); } static Object unstructure_debug(String s) { try { return unstructure(s); } catch (Throwable _e) { print("Was unstructuring: " + s); throw rethrow(_e); } } static void onResize(Component c, final Object r) { c.addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent e) { pcallF(r); } }); } static void deleteDirectory(File dir) { deleteDirectory(dir, false, false); } static void deleteDirectory(File dir, boolean verbose, boolean testRun) { 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(); } static Map synchroIdentityHashMap() { return synchroMap(new IdentityHashMap()); } static BufferedImage asBufferedImage(Object o) { BufferedImage bi = toBufferedImageOpt(o); if (bi == null) throw fail(getClassName(o)); return bi; } static String getBotAddress(String bot) { List l = fullBotScan(bot); return empty(l) ? null : first(l).address; } static String googleImageSearch(String q) { return (String) call(hotwireOnce("#1010230"), "googleImageSearch", q); } static List googleImageSearch_multi(String q) { return (List) call(hotwireOnce("#1010230"), "googleImageSearch_multi", q); } static BufferedImage googleImageSearchFirst(String q) { return (BufferedImage) call(hotwireOnce("#1010230"), "googleImageSearchFirst", q); } 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; } static String dbProgramID() { return getDBProgramID(); } static boolean isOfflineMode() { return eq("1", trim(loadProgramTextFile("#1005806", "offline-mode"))); } static String matchOK2OrFail(String s) { Matches m = new Matches(); if (swic(s, "ok ")) return substring(s, 3); else if (eqic(s, "ok")) return "ok"; else throw fail(s); } static A setFrameSize(A c, int w, int h) { JFrame f = getFrame(c); if (f != null) f.setSize(w, h); return c; } static A setFrameSize(int w, int h, A c) { return setFrameSize(c, w, h); } static double sqrt(double x) { return Math.sqrt(x); } static JButton jbutton(String text, Object action) { return newButton(text, action); } // button without action static JButton jbutton(String text) { return newButton(text, null); } static JButton jbutton(BufferedImage img, Object action) { return setButtonImage(img, jbutton("", action)); } static JButton jbutton(Action action) { return new JButton(action); } 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 String && isRunnableX(y)) { m.add(jmenuItem((String) o, y)); ++i; } else if (o instanceof JMenuItem) m.add((JMenuItem) o); // "call" might use wrong method else if (o instanceof String || o instanceof Action || o instanceof Component) call(m, "add", o); else print("Unknown menu item: " + o); } } 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(); } } 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(); } } // changes & returns canvas static BufferedImage drawImageOnImage(BufferedImage img, BufferedImage canvas, int x, int y) { Graphics2D g = createGraphics(canvas); g.drawImage(img, x, y, null); g.dispose(); return canvas; } static Font sansSerifBold(int fontSize) { return new Font(Font.SANS_SERIF, Font.BOLD, fontSize); } static A firstInstance(Collection c, Class type) { return firstOfType(c, type); } static long sqr(long l) { return l*l; } static double sqr(double d) { return d*d; } static File makeTempDir() { return (File) call(getJavaX(), "TempDirMaker_make"); } static A swingConstruct(final Class c, final Object... args) { return swing(new F0() { A get() { try { return nuObject(c, args) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "nuObject(c, args)"; }}); } 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; } static String getText(final JTextComponent c) { return c == null ? "" : (String) swingAndWait(new Object() { Object get() { try { return c.getText() ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "c.getText()"; }}); } static String getText(final JLabel l) { return l == null ? "" : (String) swingAndWait(new Object() { Object get() { try { return l.getText() ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "l.getText()"; }}); } // returns the contents of text field for editable combo box static String getText(final JComboBox cb) { if (cb.isEditable()) return unnull((String) cb.getEditor().getItem()); else return str(cb.getSelectedItem()); } static BufferedImage resizeImage(BufferedImage img, int newW, int newH) { return resizeImage(img, newW, newH, Image.SCALE_SMOOTH); } 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; } static BufferedImage resizeImage(BufferedImage img, int newW) { int newH = iround(img.getHeight()*(double) newW/img.getWidth()); return resizeImage(img, newW, newH); } static Font sansSerif(int fontSize) { return new Font(Font.SANS_SERIF, Font.PLAIN, fontSize); } 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; } static A bindToComponent(A component, Runnable onShow) { return bindToComponent(component, onShow, null); } // better modulo that gives positive numbers always static int mod(int n, int m) { return (n % m + m) % m; } static String dummyMainClassName(String progID) { return "m" + psI(progID); } static File tempFileFor(File f) { return new File(f.getPath() + "_temp"); } static A restructure(A a) { return (A) unstructure(structure(a)); } 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); // remember original settings Stroke originalStroke = g2.getStroke(); RenderingHints originalHints = g2.getRenderingHints(); // create a glyph vector from your text GlyphVector glyphVector = g2.getFont().createGlyphVector(g2.getFontRenderContext(), text); // get the shape object Shape textShape = glyphVector.getOutline(); g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2.setColor(outlineColor); g2.setStroke(outlineStroke); g2.draw(textShape); // draw outline g2.setColor(fillColor); g2.fill(textShape); // fill the shape // reset to original settings after painting g2.setStroke(originalStroke); g2.setRenderingHints(originalHints); g2.translate(-x, -y); } 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"); } static boolean hasMethod(Object o, String method, Object... args) { return findMethod(o, method, args) != null; } static void bindTimerToComponent(final Timer timer, JFrame f) { bindTimerToComponent(timer, f.getRootPane()); } 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) { } }); } 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); } // i must point at the (possibly imaginary) opening bracket ("{") // index returned is index of closing bracket + 1 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(); } static boolean allPaused() { return ping_pauseAll; } static String strOrEmpty(Object o) { return o == null ? "" : str(o); } static File linux_fileInRamDisk(String name) { if (!isLinux()) return null; File dir = newFile("/dev/shm"); if (dir.isDirectory()) return newFile(dir, name); return null; } static int leftScanModifiers(List tok, int i) { List mod = getJavaModifiers(); while (i > 1 && mod.contains(tok.get(i-2))) i -= 2; return i; } static A optPar(ThreadLocal tl, A defaultValue) { A a = tl.get(); if (a != null) { tl.set(null); return a; } return defaultValue; } static A optPar(ThreadLocal tl) { return optPar(tl, null); } static long fixTimestamp(long timestamp) { return timestamp > now() ? 0 : timestamp; } static String className(Object o) { return getClassName(o); } // action can be Runnable or a function name static JButton newButton(final String text, final Object action) { return swing(new F0() { JButton get() { try { JButton btn = new JButton(text); if (action != null) btn.addActionListener(actionListener(action)); return btn; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JButton btn = new JButton(text);\r\n if (action != null)\r\n btn.addActio..."; }}); } static BufferedImage toBufferedImageOpt(Object o) { if (o instanceof BufferedImage) return (BufferedImage) o; 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; } static String strUnnull(Object o) { return o == null ? "" : str(o); } static String loadPage_utf8(URL url) { return loadPage_utf8(url.toString()); } static String loadPage_utf8(String url) { loadPage_charset.set("UTF-8"); try { return loadPage(url); } finally { loadPage_charset.set(null); } } static Graphics2D antiAliasGraphics(BufferedImage img) { Graphics2D g = createGraphics(img); g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); return g; } static void saveTranspiledCode(String progID, String code) { saveTextFile(new File(getCodeProgramDir(progID), "Transpilation"), code); } static class ScannedBot { String helloString; String address; ScannedBot(String helloString, String address) { this.address = address; this.helloString = helloString;} ScannedBot() {} } static List fullBotScan() { return fullBotScan(""); } 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; } static int isqrt(double x) { return iround(Math.sqrt(x)); } static void cancelTimer(javax.swing.Timer timer) { if (timer != null) timer.stop(); } 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; } static JButton setButtonImage(BufferedImage img, JButton btn) { btn.setIcon(imageIcon(img)); return btn; } static double pi() { return Math.PI; } static JPanel smartAdd(JPanel panel, List parts) { for (Object o : parts) addToContainer(panel, wrapForSmartAdd(o)); return panel; } static JPanel smartAdd(JPanel panel, Object... parts) { return smartAdd(panel, asList(parts)); } static BufferedImage createBufferedImage(int w, int h, Color color) { return newBufferedImage(w, h, color); } static int packFrame_minw = 150, packFrame_minh = 50; static A packFrame(final A c) { { swing(new Runnable() { public void run() { try { JFrame frame = getFrame(c); if (frame != null) { frame.pack(); int maxW = getScreenWidth()-50, maxH = getScreenHeight()-50; frame.setSize( min(maxW, max(frame.getWidth(), packFrame_minw)), min(maxH, max(frame.getHeight(), packFrame_minh))); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JFrame frame = getFrame(c);\r\n if (frame != null) {\r\n frame.pack();\r\n ..."; }}); } return c; } static JFrame packFrame(ButtonGroup g) { return packFrame(getFrame(g)); } 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); } static void sortFilesByName(List l) { sort(l, new Comparator() { public int compare(File a, File b) { return stdcompare(a.getName(), b.getName()); } }); } 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); } } 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; } 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; } 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; } static int getScreenWidth() { return getScreenSize().width; } static List getJavaModifiers_list = litlist("static", "abstract", "public", "private", "protected", "final", "native", "volatile", "synchronized", "transient"); static List getJavaModifiers() { return getJavaModifiers_list; } static BufferedImage clipBufferedImage(BufferedImage src, Rectangle clip) { return src.getSubimage(clip.x, clip.y, clip.width, clip.height); } static BufferedImage clipBufferedImage(BufferedImage src, Rect clip) { return clipBufferedImage(src, clip.getRectangle()); } static BufferedImage clipBufferedImage(BufferedImage src, int x, int y, int w, int h) { return src.getSubimage(x, y, w, h); } static int getScreenHeight() { return getScreenSize().height; } static boolean isLinux() { return !isWindows() && !isMac() && !isAndroid(); } static Component wrapForSmartAdd(Object o) { if (o == null) return new JPanel(); if (o instanceof String) return jlabel((String) o); return wrap(o); } static void addToContainer(Container a, Component b) { if (a != null && b != null) a.add(b); } static AbstractAction abstractAction(String name, final Object runnable) { return new AbstractAction(name) { public void actionPerformed(ActionEvent evt) { pcallF(runnable); } }; } static boolean isMac() { return System.getProperty("os.name").toLowerCase().contains("mac"); } static class BetterLabel extends JLabel { BetterLabel() { componentPopupMenu(this, new Object() { void get(JPopupMenu menu) { try { addMenuItem(menu, "Copy text to clipboard", new Runnable() { public void run() { try { copyTextToClipboard(getText()); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "copyTextToClipboard(getText());"; }}); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "addMenuItem(menu, \"Copy text to clipboard\", r {\r\n copyTextToClipboard(..."; }}); } BetterLabel(String text) { this(); setText(text); } public void setText(String text) { super.setText(text); setToolTipText(text); } } static interface IVar { void set(A a); A get(); boolean has(); void clear(); } static class MapInConcept implements Map { Map m; // Backing Map Concept cc; MapInConcept() {} // for persistence 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(); } private transient Set keySet; private transient Set> entrySet; private 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);} // TODO: deadlock? } public int hashCode() { synchronized (this) {return m.hashCode();} } public String toString() { synchronized (this) {return m.toString();} } // 1.8 stuff omitted Map unwrap() { return m; } } static class DoublePt { double x, y; DoublePt() {} DoublePt(Point p) { x = p.x; y = p.y; } 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; } } // stores all strings as UTF8 (typically half as big) static class CondensedStrings2 extends AbstractList implements RandomAccess { byte[] data; int[] indices; CondensedStrings2() {} // only for serialisation 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; } int totalSize() { return data.length; } } static class AutoComboBox extends JComboBox { String keyWord[] = {"item1", "item2", "item3"}; Vector myVector = new Vector(); 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)); setMyVector(); } /** * set the item list of the AutoComboBox * @param keyWord an String array */ void setKeyWord(String[] keyWord) { this.keyWord = keyWord; setMyVector(); } void setKeyWord(Collection keyWord) { setKeyWord(toStringArray(keyWord)); } private void setMyVector() { copyArrayToVector(keyWord, myVector); } } static class ComboListener extends KeyAdapter { JComboBox cbListener; Vector vector; public ComboListener(JComboBox cbListenerParam, Vector vectorParam) { cbListener = cbListenerParam; vector = vectorParam; } public void keyReleased(KeyEvent key) { if (key.getKeyCode() == KeyEvent.VK_ENTER) return; JTextField tf = (JTextField) ( cbListener.getEditor().getEditorComponent()); if (tf.getCaretPosition() != l(tf.getText())) return; String text = ((JTextField) key.getSource()).getText(); Vector list = getFilteredList(text); if (nempty(list)) { cbListener.setModel(new DefaultComboBoxModel(list)); cbListener.setSelectedIndex(-1); tf.setText(text); // necessary? cbListener.showPopup(); } else cbListener.hidePopup(); } public Vector getFilteredList(String text) { return new Vector(scoredSearch(text, vector)); } } static class Pt { int x, y; Pt() {} Pt(Point p) { x = p.x; y = p.y; } Pt(int x, int y) { this.y = y; this.x = x;} 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; } } static class ImageSurfaceSelector extends MouseAdapter { ImageSurface is; Point startingPoint; boolean enabled = true; static boolean verbose = false; 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; } Point getPoint(MouseEvent e) { return new Point((int) (e.getX()/is.getZoomX()), (int) (e.getY()/is.getZoomY())); } } static class Str extends Concept { String name; List otherNames = new ArrayList(); Str() {} Str(String name) { this.name = name;} public String toString() { return name; } } static class Rect { int x, y, w, h; Rect() {} Rect(Rectangle r) { x = r.x; y = r.y; w = r.width; h = r.height; } Rect(int x, int y, int w, int h) { this.h = h; this.w = w; this.y = y; this.x = x;} 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; } int x2() { return x + w; } int y2() { return y + h; } boolean contains(Pt p) { return contains(p.x, p.y); } boolean contains(int _x, int _y) { return _x >= x && _y >= y && _x < x+w && _y < y+h; } boolean empty() { return w <= 0 || h <= 0; } } static class ImageSurface extends Surface { private BufferedImage image; private double zoomX = 1, zoomY = 1; private Rectangle selection; List tools = new ArrayList(); Object overlay; // voidfunc(Graphics2D) Runnable onSelectionChange; static boolean verbose; boolean noMinimumSize = true; String titleForUpload; Object onZoom; boolean specialPurposed; // true = don't show image changing commands in popup menu boolean zoomable = true; public ImageSurface() { this(dummyImage()); } 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; componentPopupMenu(this, new Object() { void get(JPopupMenu menu) { try { Point p = pointFromEvent(componentPopupMenu_mouseEvent.get()).getPoint(); fillPopupMenu(menu, p); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Point p = pointFromEvent(componentPopupMenu_mouseEvent.get()).getPoint();\r\n ..."; }}); new ImageSurfaceSelector(this); jHandleFileDrop(this, new Object() { 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); } // point is already in image coordinates protected 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(2.0); centerPoint(point); } }); menu.add(miZoomIn); JMenuItem miZoomOut = new JMenuItem("Zoom out"); miZoomOut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { zoomOut(2.0); 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()"; }}); } void noImage() { setImage((BufferedImage) null); } void crop() { if (selection == null) return; BufferedImage img = clipBufferedImage(getImage(), selection); selection = null; setImage(img); } void loadFromClipboard() { BufferedImage img = getImageFromClipboard(); if (img != null) setImage(img); } 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.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) { // drawRect is inclusive, selection is exclusive, so... whatever, tests show it's cool. drawSelectionRect(g, selection, Color.green, Color.white); } } public void drawSelectionRect(Graphics2D g, Rectangle selection, Color green, Color white) { 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)); } private int getZoomedHeight() { return (int) (image.getHeight() * zoomY); } private 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()); } /** returns a scrollpane with the scroll-mode prevent-garbage-drawing fix applied */ 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(); } /** tricky magic to get parent scroll pane */ private 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()); } // p is in image coordinates 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(); //_print("centerPoint " + p); int x = max(0, p.x-viewSize.width/2); int y = max(0, p.y-viewSize.height/2); //_print("centerPoint " + p + " => " + x + "/" + y); p = new Point(x,y); //_print("centerPoint " + p); 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);"; }}); } Pt pointFromEvent(MouseEvent e) { return pointFromComponentCoordinates(new Pt(e.getX(), e.getY())); } Pt pointFromComponentCoordinates(Pt p) { return new Pt((int) (p.x/zoomX), (int) (p.y/zoomY)); } void uploadTheImage() { call(hotwire("#1007313"), "go", getImage(), titleForUpload); } void showFullScreen() { showFullScreenImageSurface(getImage()); } void zoomIn(double f) { setZoom(getZoomX()*f, getZoomY()*f); } void zoomOut(double f) { setZoom(getZoomX()/f, getZoomY()/f); } } static JFrame showFullScreen(final JComponent c) { return (JFrame) swingAndWait(new Object() { 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(); 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); // Only this hides the task bar in Peppermint Linux w/Substance 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 ..."; }}); } static void copyArrayToVector(Object[] array, Vector v) { v.clear(); v.addAll(toList(array)); } static String fastu_fromUtf8(byte[] data) { return fastu_fromUtf8(data, 0, data.length); } 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) { // 0xxxxxxx - it is an ASCII char, so copy it exactly as it is 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); } 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; } static JScrollPane enclosingScrollPane(Component c) { while (c.getParent() != null && !(c.getParent() instanceof JViewport) && c.getParent().getComponentCount() == 1) c = c.getParent(); // for jscroll_center if (!(c.getParent() instanceof JViewport)) return null; c = c.getParent().getParent(); return c instanceof JScrollPane ? (JScrollPane) c : null; } 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)"); } static boolean hasTransparency(BufferedImage img) { return img.getColorModel().hasAlpha(); } 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; } static void showFullScreenImageSurface(BufferedImage img) { showFullScreen(jscroll_centered(disposeFrameOnClick(new ImageSurface(img)))); } static void popup(final Throwable throwable) { popupError(throwable); } static void popup(final String msg) { print(msg); SwingUtilities.invokeLater(new Runnable() { public void run() { JOptionPane.showMessageDialog(null, msg); } }); } // onDrop: voidfunc(File) 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) { printStackTrace2(__e); } e.rejectDrop(); } }); } static JComponent unwrap(JComponent c) { return c instanceof JScrollPane ? unwrapScrollPane((JScrollPane) c) : c; } 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); } } static void popupError(final Throwable throwable) { throwable.printStackTrace(); // print stack trace to console for the experts SwingUtilities.invokeLater(new Runnable() { public void run() { String text = throwable.toString(); //text = cutPrefix(text, "java.lang.RuntimeException: "); JOptionPane.showMessageDialog(null, text); } }); } 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; } static String nullIfEmpty(String s) { return isEmpty(s) ? null : s; } static JScrollPane jscroll_centered(Component c) { return new JScrollPane(jFullCenter(c)); } static void registerEscape(JFrame frame, final Runnable r) { String name = "Escape"; Action action = abstractAction(name, r); JComponent pnl = frame.getRootPane(); KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); pnl.getActionMap().put(name, action); pnl.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, name); } static JComponent unwrapScrollPane(JScrollPane sp) { return sp == null ? null : (JComponent) sp.getViewport().getView(); } 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)))); } static JPanel jFullCenter(final Component c) { return swing(new F0() { 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;"; }}); } 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]; // Translate all full groups from base64 to byte array elements int inCursor = 0, outCursor = 0; for (int i=0; i> 4)); result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2)); result[outCursor++] = (byte) ((ch2 << 6) | ch3); } // Translate partial group, if present 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)); } } // assert inCursor == s.length()-missingBytesInLastGroup; // assert outCursor == result.length; return result; } static int base64decode_base64toint(char c, byte[] alphaToInt) { int result = alphaToInt[c]; if (result < 0) throw new IllegalArgumentException("Illegal character " + c); return result; } 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 }; static BufferedImage decodeImage(byte[] data) { try { return ImageIO.read(new ByteArrayInputStream(data)); } catch (Exception __e) { throw rethrow(__e); } } static class SynchronizedSet extends SynchronizedCollection implements Set { SynchronizedSet() {} // for persistence SynchronizedSet(Set set) { super(set); } 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();} } } static class SynchronizedCollection implements Collection { Collection c; // Backing Collection Object mutex; // Object on which to synchronize SynchronizedCollection() {} // for persistence SynchronizedCollection(Collection c) { this.c = Objects.requireNonNull(c); mutex = this; } 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(); // Must be manually synched by user! } public boolean add(E e) { synchronized (mutex) {return c.add(e);} } 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();} } /*public void forEach(Consumer consumer) { synchronized (mutex) {c.forEach(consumer);} }*/ /*public boolean removeIf(Predicate filter) { synchronized (mutex) {return c.removeIf(filter);} }*/ @Override public Spliterator spliterator() { return c.spliterator(); // Must be manually synched by user! } /*public Stream stream() { return c.stream(); // Must be manually synched by user! }*/ /*public Stream parallelStream() { return c.parallelStream(); // Must be manually synched by user! }*/ } static class TransferableImage implements Transferable { Image i; 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; } } abstract static class Surface extends JPanel { public boolean clearSurface = true; private boolean clearOnce; Surface() { setDoubleBuffered(false); } 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(); } } static class RGB { public float r, g, b; // can't be final cause persistence 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) { r = Integer.parseInt(hex.substring(0, 2), 16)/255f; g = Integer.parseInt(hex.substring(2, 4), 16)/255f; b = Integer.parseInt(hex.substring(4, 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))); } int asInt() { return getColor().getRGB() & 0xFFFFFF; } 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(); } } static class RGBImage { transient BufferedImage bufferedImage; File file; int width, height; int[] pixels; RGBImage() {} RGBImage(BufferedImage image) { this(image, null); } 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(); // set upper byte to 0 } catch (InterruptedException e) { throw new RuntimeException(e); } } /** We assume it's a file name to load from */ RGBImage(String file) throws IOException { this(new File(file)); } RGBImage(Dimension size, Color color) { this(size.width, size.height, color); } RGBImage(Dimension size, RGB color) { this(size.width, size.height, color); } private void cleanPixels() { for (int i = 0; i < pixels.length; i++) pixels[i] &= 0xFFFFFF; } RGBImage(int width, int height, int[] pixels) { this.width = width; this.height = height; this.pixels = pixels; } 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); } 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; } RGBImage(RGBImage image) { this(image.width, image.height, copyPixels(image.pixels)); } RGBImage(int width, int height, Color color) { this(width, height, new RGB(color)); } RGBImage(File file) throws IOException { this(javax.imageio.ImageIO.read(file)); } private 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); } /** alias of getRGB - I kept typing getPixel instead of getRGB all the time, so I finally created it */ RGB getPixel(int x, int y) { return getRGB(x, y); } RGB getPixel(Pt p) { return getPixel(p.x, p.y); } int getWidth() { return width; } int getHeight() { return height; } int w() { return width; } int h() { return height; } /** Attention: cached, i.e. does not change when image itself changes */ /** @NotNull */ public BufferedImage getBufferedImage() { if (bufferedImage == null) { bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //bufferedImage.setData(Raster.createRaster(new SampleModel())); for (int y = 0; y < height; y++) for (int x = 0; x < width; x++) bufferedImage.setRGB(x, y, pixels[y*width+x]); } return bufferedImage; } RGBImage clip(Rect r) { return r == null ? null : clip(r.getRectangle()); } 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); } private 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; } /** can now also do GIF (not just JPEG) */ public static RGBImage load(String fileName) { return load(new File(fileName)); } /** can now also do GIF (not just JPEG) */ 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; } void setPixel(Pt p, RGB rgb) { setPixel(p.x, p.y, rgb); } 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; } } static boolean inRange(int x, int n) { return x >= 0 && x < n; } static int asInt(Object o) { return toInt(o); } }