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.*; // Content in: http://botcompany.de/1013927/raw/list/News+%28HTML%29 //set flag NoAI. import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken; import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload; import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.client.http.javanet.NetHttpTransport; import javax.net.ssl.*; import java.security.SecureRandom; import java.security.cert.X509Certificate; import java.text.SimpleDateFormat; import java.text.NumberFormat; import javax.swing.undo.UndoManager; import net.coobird.thumbnailator.Thumbnailator; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.DataFlavor; import java.awt.geom.AffineTransform; import java.awt.datatransfer.*; import java.awt.dnd.*; import java.awt.geom.*; import java.awt.font.GlyphVector; import org.jsoup.*; import org.jsoup.nodes.*; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import javax.swing.event.AncestorListener; import javax.swing.event.AncestorEvent; import javax.swing.Timer; import javax.imageio.metadata.*; import javax.imageio.stream.*; import java.awt.datatransfer.UnsupportedFlavorException; import java.text.*; import java.util.TimeZone; class main { static String denkmodulID = ""; //#1013938; static Object denkmodul; static boolean towly, signIn; static long tokenTimeout = 60*60*1000; // one hour public static void main(final String[] args) throws Exception { independentSmartBot(); if (nempty(denkmodulID)) runInMCVarWithAutoRestart("denkmodul", denkmodulID); } static ExpiringHashMap < String , Payload > tokensToPayload = new ExpiringHashMap(tokenTimeout); static Object html(String uri, final Map params) { try { Matches m = new Matches(); if (eq(uri, "/") && !cic(domain(), "butter") && !swic(domain(), "www.")) return hrefresh("https://www.botcompany.de:8443/1013826/raw"); if (eq(uri, "/tokens")) return lstr(tokensToPayload); String token = params.get("token"); String q = params.get("q"); Payload payload = null; boolean loaded = false; if (nempty(token)) { payload = tokensToPayload.get(token); if (payload == null) { payload = googleVerifyUserToken2(botCompanyGoogleSignInID(), token); loaded = true; mapPut(tokensToPayload, token, payload); } } if (payload != null) { long time = unixTime(); print("loaded=" + loaded + ", issued=" + (payload.getIssuedAtTimeSeconds()-time) + ", expiration=" + (payload.getExpirationTimeSeconds()-time)); } String email = payload == null ? null : payload.getEmail(); if (nempty(q)) { if (nempty(email)) { appendToMechList_noUniq("Things said on the home page by logged in users", now() + ", " + email + ": " + q); post(email, "said", quote(q)); } else appendToMechList_noUniq("Things said on the home page by anonymous users", now() + ": " + q); } if (eq(uri, "/content-script")) { String text = ""; if (nempty(q)) { text += p(small("> " + span(htmlencode(q), "style" , "color: gray"))); String a = "Fehler"; try { a = callAnswerFunction(denkmodul, q); } catch (Throwable __e) { _handleException(__e); } if (nempty(a)) { if (swic_trim(a, "[VIDEO] ", m)) text += embedYoutube_newer_autoplay(m.rest(), 600); else text += p(htmlencode(a)); } } boolean again = false, de = false; /*if (payload == null) text += "You are not logged in.";*/ if (nempty(email)) { pnlStruct(payload); again = post(email, "logged in", ".") == null; if (!payload.getEmailVerified()) text = "Unverified email (huh?)"; } if (empty(text)) { de = cic((String) payload.get("locale"), "de"); String firstName = (String) payload.get("given_name"); text = p(htmlencode((de ? "Hallo " : "Hello ") + (again ? (de ? "mal wieder, " : "again ") : "") + firstName + "!")); String pic = (String) payload.get("picture"); if (de && isURL(pic)) text += p(htmlencode("Was für ein hübsches Bild:")) + p(himg(pic, "title" , firstName + ", ein toller Mensch")); } String script = "$(\".BUTTON_OUT\").css('display', 'inline-block');\r\n document.getElementById(\"content\").innerHTML = " + jsQuote(text) + ";" + "\r\n $(\"input[name='token']\").val($TOKEN);\r\n $(\"#inputdiv\").show();\r\n $(\"input[name='q']\").focus();\r\n ".replace("$TOKEN", jsQuote(token)); if (de) script += "$(\"input[name='q']\").attr('placeholder', 'Sprich mit mir');"; print(script); return script; } String defaultInput = "Spiel George Michael"; return hhtml(hhead(htitle("BotCompany.de") + hmobilefix() + loadJQuery() + googleSignIn_header() + hcss(andrzejButtonStyle()) + hcookieconsent() + hGoogleFont("Raleway:500") + loadHtml2canvas() + hcss("body { font-family: Raleway; font-weight: 500; }") + hplaceholderStyle("color: blue; opacity: 1 !important; text-align: center;")) + hbody( (nempty(params.get("noworker")) ? "" : hSilentComputator()) + jsIsDesktopChrome() + hfullcenter( p(ahref("http://BotCompany.de", "BotCompany.de")) + htableRaw2_singleRow(ll( youtubeEmbed("wmA4VPl9RnY", 254, 150, "Stefan's OS"), youtubeEmbed("xYMFQs549i0", 254, 150, "Optical Character Recognition Demo"), youtubeEmbed("bS8j3gQyATM", 254, 150, "A New Approach To Image Recognition"), youtubeEmbed("nMRGcamgc_U", 254, 150, "Stefan's OS: Working With Files") ), ll("style" , "margin-bottom: 40px"), null, ll("align" , "center", "style" , "padding-left: 10px; padding-right: 10px")) // towly + (!towly ? "" : p(hsnippetimg("#1013948", "width" , 125, "height" , 141, "id" , "towly", "title" , "Towly greets you")) + hscript("\r\n //var towlyPhase = 1;\r\n var f1, f2;\r\n f1 = function() {\r\n $(\"#towly\").attr(\"src\", \"https://www.botcompany.de:8443/img/1013945\");\r\n setTimeout(f2, 300);\r\n };\r\n f2 = function() {\r\n $(\"#towly\").attr(\"src\", \r\n \"https://www.botcompany.de:8443/img/1013948\");\r\n setTimeout(f1, 1000+Math.random()*6000);\r\n };\r\n setTimeout(f1, 1000);\r\n ")) + p(/*nlToBr*/(rtrim(mechList_raw("News (HTML)")))) + "

" + (!signIn ? "" : googleSignIn_signInButton(relativeRawBotLink(programID(), "content-script"), "\r\n eval(data);\r\n ", "\r\n $(\".BUTTON_OUT\").css('display', 'none');\r\n document.getElementById(\"content\").innerHTML = \"\";\r\n ") //+ hsmall(ahref(googleSignIn_signOutURL(), "Sign out")) + ahref(googleSignIn_signOutURL(), div("Sign out", "class" , "BUTTON_OUT", "style" , "display: none")) + "

" + div("", "id" , "content", "style" , "font-size: 30px") + hjavascript("\r\n function postIt() {\r\n var q = $(\"input[name='q']\").val();\r\n var token = $(\"input[name='token']\").val();\r\n console.log(\"Posting to ${POSTLINK}, q=\" + q);\r\n $.post('${POSTLINK}', { q : q, token: token },\r\n function(data, status) {\r\n console.log(\"Got data: \" + data + \"\\nStatus: \" + status);\r\n eval(data);\r\n });\r\n }\r\n ".replace("${POSTLINK}", myRelativeRawLink("/content-script"))) + hform(hhidden("token", "") + div(htextinput("q", "style" , "text-align: center; width: 600px; font-size: 20px", "placeholder" , "Tell me something / Sag mir was", "value" , defaultInput, "autofocus" , "autofocus", "onfocus" , "this.select()") + " " + hsubmit("OK"), "id" , "inputdiv") /*+ hjavascript([[ var inp = $("input[name='q']"); //inp.setSelectionRange(0, inp.value.length); inp.focus(function() { var save_this = $(this); window.setTimeout(function(){ save_this.select(); }, 100); }); ]])*/, "onSubmit" , "postIt(); return false;")) + p(ahref("https://botcompany.de/chat", "[more]") + " " + ahref("mailto:info@botcompany.de", "[mail]")) ))); } catch (Exception __e) { throw rethrow(__e); } } static void independentSmartBot() { ai_independentSmartBot(); } static void independentSmartBot(String progID) { ai_independentSmartBot(progID); } static boolean nempty(Collection c) { return !empty(c); } static boolean nempty(CharSequence s) { return !empty(s); } static boolean nempty(Object[] o) { return !empty(o); } static boolean nempty(byte[] o) { return !empty(o); } static boolean nempty(int[] o) { return !empty(o); } static boolean nempty(Map m) { return !empty(m); } static boolean nempty(Iterator i) { return i != null && i.hasNext(); } static boolean nempty(Object o) { return !empty(o); } static void runInMCVarWithAutoRestart(String varName, String programID) { runInMCVarWithAutoRestart(varName, programID, null); } static void runInMCVarWithAutoRestart(String varName, String programID, Object afterReload) { set(mc(), varName, runDependent(programID)); callF(afterReload); autoRestartModule(varName, programID, true, afterReload); } static boolean eq(Object a, Object b) { return a == null ? b == null : a == b || b != null && a.equals(b); } static boolean cic(Collection 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 String domain() { return domainName(); } 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 String hrefresh(String target) { return hrefresh(0, target); } static String hrefresh(double seconds) { return hrefresh(seconds, ""); } static String hrefresh(double seconds, String target) { return tag("meta", "", "http-equiv", "refresh", "content", iceil(seconds) + (nempty(target) ? "; url=" + target : "")); } 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)); } // google http client // google api client // google oauth client // jackson // google jackson2 // google http client appengine static Payload googleVerifyUserToken2(String clientID, String token) { try { NetHttpTransport transport = new NetHttpTransport(); GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(transport, new JacksonFactory()) .setAudience(Collections.singletonList(clientID)) .build(); GoogleIdToken idToken = verifier.verify(token); if (idToken != null) return idToken.getPayload(); return null; } catch (Exception __e) { throw rethrow(__e); } } static String botCompanyGoogleSignInID() { return "709303807085-rvu63dqa5hjcii6h0j70lhn7me27cegc.apps.googleusercontent.com"; } static void mapPut(Map map, A key, B value) { if (map != null && key != null && value != null) map.put(key, value); } static long unixTime() { return unixTime(now()); } static long unixTime(long timestamp) { return timestamp/1000; } static volatile StringBuffer local_log = new StringBuffer(); // not redirected static volatile Appendable 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 boolean print_silent; // total mute if set static Object print_byThread_lock = new Object(); static volatile ThreadLocal print_byThread; // special handling by thread - prefers F1 static volatile Object print_allThreads; static volatile Object print_preprocess; static void print() { print(""); } static A print(String s, A o) { print((endsWithLetterOrDigit(s) ? s + ": " : s) + o); return o; } // slightly overblown signature to return original object... static A print(A o) { ping_okInCleanUp(); if (print_silent) return o; String s = String.valueOf(o) + "\n"; print_noNewLine(s); return o; } static void print_noNewLine(String s) { Object f = getThreadLocal(print_byThread_dontCreate()); if (f == null) f = print_allThreads; if (f != null) // We do need the general callF machinery here as print_byThread is sometimes shared between modules if (isFalse( f instanceof F1 ? ((F1) f).get(s) : callF(f, s))) return; print_raw(s); } static void print_raw(String s) { if (print_preprocess != null) s = (String) callF(print_preprocess, s); s = fixNewLines(s); Appendable loc = local_log; Appendable 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 String appendToMechList_noUniq(String name, String text) { return botAppendToMechList(name, text); } static long now_virtualTime; static long now() { return now_virtualTime != 0 ? now_virtualTime : System.currentTimeMillis(); } static String post(T3 t) { return ai_postTriple(t); } static String post(String a, String b, String c) { return ai_postTriple(a, b, c); } static String post(String a, String b, int c) { return post(a, b, symbol(str(c))); } 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 if (c == '\0') out.append("\\0"); else out.append(c); } out.append('"'); } static String p(Object contents, Object... params) { return tag("p", contents, params) + "\n"; } static String small(Object contents, Object... params) { return tag("small", contents, params); } static String span(Object contents, Object... params) { return tag("span", contents, params); } static String htmlencode(Object o) { return htmlencode(str(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("&#").append((int) c).append(';'); else out.append(c); } return out.toString(); } static String callAnswerFunction(Object responder, String s) { return callAnswerMethod(responder, s); } static String callAnswerFunction(String s) { return callAnswerMethod(s); } static String callAnswerFunction(Object responder, String s, List history) { return callAnswerMethod(responder, s, history); } static volatile PersistableThrowable _handleException_lastException; static List _handleException_onException = synchroList(ll("printStackTrace2")); static void _handleException(Throwable e) { _handleException_lastException = persistableThrowable(e); Throwable e2 = innerException(e); if (e2.getClass() == RuntimeException.class && eq(e2.getMessage(), "Thread cancelled.") || e2 instanceof InterruptedException) return; for (Object f : cloneList(_handleException_onException)) try { callF(f, e); } catch (Throwable e3) { printStackTrace2(e3); // not using pcall here - it could lead to endless loops } } 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 embedYoutube_newer_autoplay(String youtubeID) { return embedYoutube_newer_autoplay(youtubeID, 400); // no volume level at 300 } static String embedYoutube_newer_autoplay(String youtubeID, int w) { int h = sixteenToNine(w); youtubeID = extractYouTubeID(youtubeID); return ("").replace("{*}", youtubeID); } // currently collection and map only static void pnlStruct(Object o) { if (o instanceof Collection) pnlStruct(((Collection) o)); else if (o instanceof Map) pnlStruct(((Map) o)); else if (o != null) pnlStruct(ll(o)); } static > B pnlStruct(B l) { pnlStruct((Iterable) l); return l; } static Iterable pnlStruct(Iterable l) { return pnlStruct("", l); } static Iterable pnlStruct(String prefix, Iterable l) { int i = 0; if (l != null) for (A a : l) print(prefix + (++i) + ". " + struct_noStringSharing(a)); return l; } static A[] pnlStruct(A[] l) { pnlStruct(asList(l)); return l; } static Map pnlStruct(Map map) { pnl(map(map, new F2() { Object get(A a, B b) { try { return sfu(a) + " = " + sfu(b); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "sfu(a) + \" = \" + sfu(b)"; }})); return map; } static boolean empty(Collection c) { return c == null || c.isEmpty(); } static boolean empty(CharSequence 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); if (o instanceof byte[]) return empty((byte[]) o); if (o == null) return true; 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 boolean empty(byte[] a) { return a == null || a.length == 0; } static boolean empty(MultiSet ms) { return ms == null || ms.isEmpty(); } static boolean empty(File f) { return getFileSize(f) == 0; } static boolean isURL(String s) { return startsWithOneOf(s, "http://", "https://", "file:"); } static String himg(String src, Object... params) { return tag("img", "", arrayPlus(params, "src", src)); } static String jsQuote(String s) { return javascriptQuote(s); } static String hhtml(Object contents) { return containerTag("html", contents); } static String hhead(Object contents) { return tag("head", contents); } static String htitle(String title) { return tag("title", htmlencode_noQuotes(title)); } static String hmobilefix() { return ""; } static String hmobilefix(String html) { return hAddToHead(html, hmobilefix()); } static String loadJQuery() { return ""; } static String googleSignIn_header() { return googleSignIn_header(""); } static String googleSignIn_header(String onLoad) { return "\r\n " .replace("$PARAM", nempty(onLoad) ? "?onload=" + urlencode(onLoad) : "") .replace("$CLIENTID", botCompanyGoogleSignInID()); } static String hcss(Object contents) { return htag("style", contents); } static String andrzejButtonStyle() { return "\r\n.BUTTON_OUT {\r\n -webkit-border-radius: 1px;\r\n -moz-border-radius: 1px;\r\n border-radius: 1px;\r\n height: 10px;\r\n line-height: 10px;\r\n color: #757575;\r\n font-family: Arial;\r\n width: 86px;\r\n font-size: 12px;\r\n font-weight: 400;\r\n padding: 10px;\r\n background-color: #FFFFFF;\r\n box-shadow: 1px 1px 20px 0px #757575;\r\n -webkit-box-shadow: 1px 1px 20px 0px #757575;\r\n -moz-box-shadow: 1px 1px 20px 0px #757575;\r\n border: solid #F5F5F5 1px;\r\n text-decoration: none;\r\n display: inline-block;\r\n cursor: pointer;\r\n}\r\n\r\n.BUTTON_OUT:hover {\r\n background: #FFFFFF;\r\n text-decoration: none;\r\n}\r\n"; } static String hcookieconsent() { return hStylesheetSnippet("#1014043") + hjssnippet("#1014042") + "\r\n\r\n"; } // font may include weights, like "Nunito Sans:400,900" static String hGoogleFont(String font) { return "\n"; } static String loadHtml2canvas() { return ""; } static String hplaceholderStyle(String style) { return hcss("\r\ninput::-webkit-input-placeholder {\r\n STYLE\r\n}\r\ninput::-moz-placeholder {\r\n STYLE\r\n}".replace("STYLE", style)); } static String hbody(Object contents, Object... params) { return tag("body", contents, params); } static String hSilentComputator() { return hSilentComputator(hSilentComputator_defaultURI()); } static String hSilentComputator(String wsUri) { return hSilentComputator(wsUri, ""); } static String hSilentComputator(String wsUri, String flag) { if (nempty(flag)) wsUri = addSlash(wsUri) + flag; return div("Calculating", "id" , "calcing") + hcss("#calcing {\r\nposition: fixed;\r\nwidth: 100%;\r\nheight: 100vh;\r\nz-index: 9999;\r\nvisibility: hidden;\r\n}") + hMakeComputerID() + hreconnectingWebSockets() + hjavascript("\r\n function sendPing() {\r\n ${PINGCMD}\r\n }\r\n \r\n var working = false;\r\n var benchResultl;\r\n var ws = new ReconnectingWebSocket(\"${URI}\");\r\n ws.onopen = function(event) {\r\n console.log(\"WebSocket opened.\");\r\n };\r\n ws.onmessage = function(event) {\r\n var d = event.data;\r\n while (d.length > 0 && d.charCodeAt(d.length-1) == 0)\r\n d = d.substring(0, d.length-1);\r\n console.log(\"Evaluating: \" + d);\r\n var geval = eval; // use global scope\r\n geval(d);\r\n console.log(\"Evaluated.\");\r\n };\r\n \r\n //working = true; // XXX\r\n //console.log(\"Eval test: \" + eval('working'));\r\n ".replace("${URI}", wsUri) .replace("${PINGCMD}", nodes_pingCmd())); } static String jsIsDesktopChrome() { return hjavascript("\r\n function isDesktopChrome() {\r\n var ua = navigator.userAgent;\r\n\r\n return /Chrome/i.test(ua) && !/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|mobile|CriOS/i.test(ua);\r\n }"); } static String hfullcenter(Object contents) { return tag("table", tr(td(contents, "align" , "center")), "width" , "100%", "height" , "100%"); } static String ahref(String link, Object contents, Object... params) { return link == null ? str(contents) : href(link, contents, params); } static String htableRaw2_singleRow(List rows, List paramsTable, List paramsTR, List paramsTD) { return htableRaw2(ll(rows), paramsTable, paramsTR, paramsTD); } static List ll(A... a) { ArrayList l = new ArrayList(a.length); if (a != null) for (A x : a) l.add(x); return l; } static String youtubeEmbed(String youtubeID) { return hEmbedYoutube(youtubeID); } static String youtubeEmbed(String youtubeID, int w, int h) { return hEmbedYoutube(youtubeID, w, h); } static String youtubeEmbed(String youtubeID, int w, int h, String text) { return hEmbedYoutube(youtubeID, w, h, text); } static String hsnippetimg(String imageID, Object... params) { return himg(snippetImageLink(imageID), params); } static String hscript(String script) { return hjavascript(script); } public static String rtrim(String s) { if (s == null) return null; int i = s.length(); while (i > 0 && " \t\r\n".indexOf(s.charAt(i-1)) >= 0) --i; return i < s.length() ? s.substring(0, i) : s; } static ExpiringMap2 mechList_raw_cache = new ExpiringMap2(10000).setMap(ciMap()); static Lock mechList_raw_lock = lock(); // timeout for loading static int mechList_raw_timeout = 60000; static String mechList_raw(String name) { try { Lock __632 = mechList_raw_lock; lock(__632); try { if (empty(name)) return ""; // get from cache String src = mechList_raw_cache.get(name); if (src != null) return src; // load & put in cache - TODO: make postPage honor this //temp tempSetThreadLocal(loadPage_forcedTimeout_byThread, mechList_raw_timeout); src = mechList_raw_fresh(name); if (src != null) mechList_raw_cache.put(name, src); return src; } finally { unlock(__632); } } catch (Exception __e) { throw rethrow(__e); } } static VF2 mechList_raw_listener = new VF2() { public void get(String msg, Object arg) { try { if (eq(msg, "mechChange") && arg instanceof String) { //print("Got change notification: " + arg); mechList_raw_cache.remove((String) arg); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (eq(msg, 'mechChange) && arg instanceof S) {\r\n //print(\"Got change noti..."; }}; static void _onLoad_mechList_raw() { add(vm_busListeners_live(), mechList_raw_listener); } static void cleanMeUp_mechList_raw() { remove(vm_busListeners_live(), mechList_raw_listener); } static String googleSignIn_signInButton(String verifyURL, String onSuccess, String onSignOut) { return "
\r\n \r\n ".replace("$VERIFYURL", verifyURL) .replace("$ONSUCCESS", onSuccess) .replace("$ONSIGNOUT", onSignOut); } static String relativeRawBotLink() { return relativeRawBotLink(programID()); } static String relativeRawBotLink(long botID) { return "/" + botID + "/raw"; } static String relativeRawBotLink(String botID) { return relativeRawBotLink(parseSnippetID(botID)); } static String relativeRawBotLink(long botID, String uri) { return "/" + botID + "/raw" + addPrefix("/", uri); } static String relativeRawBotLink(String botID, String uri) { return relativeRawBotLink(parseSnippetID(botID), uri); } static String programID() { return getProgramID(); } static String programID(Object o) { return getProgramID(o); } static String googleSignIn_signOutURL() { return "javascript:signOut()"; } static String div(Object contents, Object... params) { return hfulltag("div", contents, params); } static BigInteger div(BigInteger a, BigInteger b) { return a.divide(b); } static BigInteger div(BigInteger a, int b) { return a.divide(bigint(b)); } static String hjavascript(String script) { return tag("script", script, "type", "text/javascript"); } static String myRelativeRawLink(String uri) { return relativeRawBotLink(programID(), uri); } static String hform(Object contents, Object... params) { return htag("form", contents, params); } static String hhidden(String name, Object value, Object... params) { return tag("input", "", concatArrays(new Object[] {"type", "hidden", "name", name, "value", value}, params)); } // first element of params can be the value static String htextinput(String name, Object... params) { Object value = ""; if (odd(l(params))) { value = params[0]; params = dropFirst(params); } Object autofocus = optPar("autofocus",params); params = changeParam(params, "autofocus" , eqOneOf(autofocus, true, 1, "1", "autofocus") ? html_valueLessParam() : null); return tag("input", "", concatArrays(new Object[] {"type", "text", "name", name, "value", value}, params)); } static String htextinput(String name) { return htextinput(name, ""); } static String hsubmit(String text, Object... params) { return tag("input", "", concatArrays(new Object[] {"type", "submit", "value", text}, params)); } static String hsubmit() { return hsubmit("Submit"); } static RuntimeException rethrow(Throwable t) { if (t instanceof Error) _handleError((Error) t); throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t); } static RuntimeException rethrow(String msg, Throwable t) { throw new RuntimeException(msg, t); } 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 ai_independentSmartBot() { ai_independentSmartBot(programID()); } static void ai_independentSmartBot(String progID) { ai_useProgramID(progID); ai_smartBotInit(); } static
A set(A o, String field, Object value) { if (o == null) return null; if (o instanceof Class) set((Class) o, field, value); else try { Field f = set_findField(o.getClass(), field); f.setAccessible(true); smartSet(f, o, value); } catch (Exception e) { throw new RuntimeException(e); } return o; } static void set(Class c, String field, Object value) { if (c == null) return; try { Field f = set_findStaticField(c, field); f.setAccessible(true); 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 Class mc() { return main.class; } static Class runDependent(String src) { Class c = hotwireDependent(src); callMain(c); return c; } static Map> callF_cache = newDangerousWeakHashMap(); static A callF(F0 f) { return f == null ? null : f.get(); } static B callF(F1 f, A a) { return f == null ? null : f.get(a); } static B callF(IF1 f, A a) { return f == null ? null : f.get(a); } static C callF(F2 f, A a, B b) { return f == null ? null : f.get(a, b); } static void callF(VF1 f, A a) { if (f != null) f.get(a); } static Object callF(Object f, Object... args) { try { if (f instanceof String) return callMC((String) f, args); if (f instanceof Runnable) { ((Runnable) f).run(); return null; } if (f == null) return null; Class c = f.getClass(); ArrayList methods; synchronized(callF_cache) { methods = callF_cache.get(c); if (methods == null) methods = callF_makeCache(c); } int n = l(methods); if (n == 0) { throw fail("No get method in " + getClassName(c)); } if (n == 1) return invokeMethod(methods.get(0), f, args); for (int i = 0; i < n; i++) { Method m = methods.get(i); if (call_checkArgs(m, args, false)) return invokeMethod(m, f, args); } throw fail("No matching get method in " + getClassName(c)); } catch (Exception __e) { throw rethrow(__e); } } // 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 ThreadLocal autoRestartModule_checkInterval = new ThreadLocal(); static java.util.Timer autoRestartModule(final String moduleVar, final String programID, final boolean callMain) { return autoRestartModule(moduleVar, programID, callMain, null); } static java.util.Timer autoRestartModule(final String moduleVar, final String programID, final boolean callMain, final Object afterReload) { return doEvery_daemon(optPar(autoRestartModule_checkInterval, 10.0), new Runnable() { Var name = evaluateInBackground(new F0() { String get() { try { return snippetWithTitle(programID); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret snippetWithTitle(programID);"; }}); String localMD5 = serverAutoRestartMD5(programID); public void run() { String newMD5 = serverAutoRestartMD5(programID); //print("MD5: " + localMD5 + " -> " + newMD5); if (isMD5(newMD5) && neq(localMD5, newMD5)) { localMD5 = newMD5; String _name = name.has() ? name.get() : fsI(programID); upgradeModuleVar(moduleVar, programID, _name, callMain, afterReload); } } }); } static String str(Object o) { return o == null ? "null" : o.toString(); } static String str(char[] c) { return new String(c); } static boolean containsIgnoreCase(Collection l, String s) { if (l != null) for (String x : l) if (eqic(x, s)) return true; return false; } static boolean containsIgnoreCase(String[] l, String s) { if (l != null) 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 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 boolean contains(BitSet bs, int i) { return bs != null && bs.get(i); } static String domainName() { Object session = call(getMainBot(), "getSession"); Map headers = (Map) (call(session, "getHeaders")); String host = (String) (headers.get("host")); if (host == null) return null; return dropFrom(host, ":"); } static boolean startsWithIgnoreCase(String a, String b) { return regionMatchesIC(a, 0, b, 0, b.length()); } static String substring(String s, int x) { return substring(s, x, strL(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 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(short[] a) { return a == null ? 0 : a.length; } static int l(long[] 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(double[] 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 == null ? 0 : o instanceof String ? l((String) o) : o instanceof Map ? l((Map) o) : o instanceof Collection ? l((Collection) o) : o instanceof Object[] ? l((Object[]) o) : o instanceof boolean[] ? l((boolean[]) o) : o instanceof byte[] ? l((byte[]) o) : o instanceof char[] ? l((char[]) o) : o instanceof short[] ? l((short[]) o) : o instanceof int[] ? l((int[]) o) : o instanceof float[] ? l((float[]) o) : o instanceof double[] ? l((double[]) o) : o instanceof long[] ? l((long[]) o) : (Integer) call(o, "size"); } static int l(MultiSet ms) { return ms == null ? 0 : ms.size(); } static int l(Lisp l) { return l == null ? 0 : l.size(); } 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 int iceil(double d) { return (int) Math.ceil(d); } static boolean endsWithLetterOrDigit(String s) { return s != null && s.length() > 0 && Character.isLetterOrDigit(s.charAt(s.length()-1)); } static void ping_okInCleanUp() { if (ping_pauseAll || ping_anyActions ) ping_impl(true); } // this syntax should be removed... 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 A getThreadLocal(ThreadLocal tl, A defaultValue) { return or(getThreadLocal(tl), defaultValue); } static ThreadLocal print_byThread_dontCreate() { return print_byThread; } static boolean isFalse(Object o) { return eq(false, o); } static String fixNewLines(String s) { int i = indexOf(s, '\r'); if (i < 0) return s; int l = s.length(); StringBuilder out = new StringBuilder(l); out.append(s, 0, i); for (; i < l; i++) { char c = s.charAt(i); if (c != '\r') out.append(c); else { out.append('\n'); if (i+1 < l && s.charAt(i+1) == '\n') ++i; } } return out.toString(); } static void print_append(Appendable _buf, String s, int max) { try { synchronized(_buf) { _buf.append(s); if (!(_buf instanceof StringBuilder)) return; StringBuilder buf = (StringBuilder) _buf; 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); } } } catch (Exception __e) { throw rethrow(__e); } } static String botAppendToMechList(String name, String text) { return botAppendToMechList_now(name, text); } static ThreadLocal ai_postTriple_verified = new ThreadLocal(); static ThreadLocal ai_postTriple_recursion = new ThreadLocal(); static String ai_postTriple(T3 triple) { // check, clean ping(); if (triple_anyPartNull(triple)) return null; triple = trimTriple(triple); if (!ai_tripleAllowedToPost(triple)) return null; // fail("No"); // existing? if (has(triple)) return null; // invalid? if (has(ai_tripleToInvalid(triple))) return null; // post verified? boolean ver = !isFalse(ai_postTriple_verified.get()); // post handler? AI_PostHandler handler = ai_postHandler(); T3 striple = toSymbolTriple(triple); if (handler != null && !isTrue(ai_postTriple_recursion.get())) { ai_postTriple_recursion.set(true); try { return strOrNull(handler.postTriple(striple, ver)); } finally { ai_postTriple_recursion.set(null); } } else { // post web the normal way Web web = webFromTriple(striple); web.unverified = !ver; postSoftwareMadeWeb(web); return web.globalID(); } } static String ai_postTriple(String a, String b, String c) { return ai_postTriple(t3(a, b, c)); } static String symbol(String s) { return s; } static String symbol(CharSequence s) { if (s == null) return null; return str(s); } static String symbol(Object o) { return symbol((CharSequence) o); } static int callAnswerMethod_version; static String callAnswerMethod(String s) { if (callAnswerMethod_version == 0) if (hasMethod(mc(), "answer", "", new ArrayList())) callAnswerMethod_version = 1; else if (hasMethod(mc(), "answer", "")) callAnswerMethod_version = 2; else callAnswerMethod_version = -1; if (callAnswerMethod_version == 1) return (String) callMC("answer", s, emptyList()); else if (callAnswerMethod_version == 2) return (String) callMC("answer", s); return null; } 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 List synchroList() { return Collections.synchronizedList(new ArrayList()); } static List synchroList(List l) { return Collections.synchronizedList(l); } static PersistableThrowable persistableThrowable(Throwable e) { return e == null ? null : new PersistableThrowable(e); } static Throwable innerException(Throwable e) { return getInnerException(e); } static ArrayList cloneList(Iterable l) { return l instanceof Collection ? cloneList((Collection) l) : asList(l); } static ArrayList cloneList(Collection l) { if (l == null) return new ArrayList(); synchronized(collectionMutex(l)) { return new ArrayList(l); } } 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 int sixteenToNine(int w) { return iround(w*9.0/16); } // TODO: check validity static String extractYouTubeID(String s) { if (empty(s)) return null; int i = indexOfAfter(s, "youtu.be/"); if (i >= 0) return substring(s, i); s = substring(s, indexOf(s, '?')+1); try { { String _a_119 = mapGet(decodeHQuery(s), "v"); if (!empty(_a_119)) return _a_119; } } catch (Throwable _e) {} return s; } static String struct_noStringSharing(Object o) { structure_Data d = new structure_Data(); d.noStringSharing = true; return structure(o, d); } static ArrayList asList(A[] a) { return a == null ? new ArrayList() : new ArrayList(Arrays.asList(a)); } static ArrayList asList(int[] a) { if (a == null) return null; ArrayList l = emptyList(a.length); for (int i : a) l.add(i); return l; } static ArrayList asList(float[] a) { if (a == null) return null; ArrayList l = emptyList(a.length); for (float 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(Producer p) { ArrayList l = new ArrayList(); A a; if (p != null) while ((a = p.next()) != null) 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 A pnl(String prefix, A l) { printNumberedLines(prefix, l); return l; } static A pnl(A l) { printNumberedLines(l); return l; } static void pnl(Map map) { printNumberedLines(map); } static void pnl(Object[] a) { printNumberedLines(a); } static void pnl(String s) { printNumberedLines(lines(s)); } 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(Iterable l, F1 f) { return map(f, l); } static List map(F1 f, Iterable l) { List x = emptyList(l); if (l != null) for (A o : l) x.add(callF(f, o)); return x; } static List map(IF1 f, Iterable l) { return map(l, f); } static List map(Iterable l, IF1 f) { List x = emptyList(l); if (l != null) for (A o : l) x.add(f.get(o)); return x; } static List map(IF1 f, A[] l) { return map(l, f); } static List map(A[] l, IF1 f) { List x = emptyList(l); if (l != null) for (A o : l) x.add(f.get(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); } // map: func(key, value) -> list element 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 sfu(Object o) { return structureForUser(o); } 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 String getType(Object o) { return getClassName(o); } 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 boolean startsWithOneOf(String s, String... l) { for (String x : l) if (startsWith(s, x)) return true; return false; } // one array plus more elements static Object[] arrayPlus(Object[] a1, Object... a2) { return concatArrays(a1, a2); } static String javascriptQuote(String s) { return quote(s); // use regular Java quoting } 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 String htmlencode_noQuotes(String s) { if (s == null) return ""; StringBuilder out = new StringBuilder(Math.max(16, s.length())); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == '<') out.append("<"); else if (c == '>') out.append(">"); else if (c > 127 || c == '&') { out.append("&#"); out.append((int) c); out.append(';'); } else out.append(c); } return out.toString(); } static String hAddToHead(String html, String tag) { List tok = htmlTok(html); List head = first(findContainerTag(tok, "head")); if (head == null) return tag + html; head.set(2, addLineBreak(tag) + head.get(2)); return join(tok); } static String urlencode(String x) { try { return URLEncoder.encode(unnull(x), "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } 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) && neqic(tag, "script")) return dropLast(openingTag) + "/>"; return openingTag + s + ""; } static String hStylesheetSnippet(String snippetID) { return tag("link", "", "rel" , "stylesheet", "type" , "text/css", "href" , rawSnippetURL(snippetID)); } static String hjssnippet(String snippetID) { return hjavascript_src_snippet(snippetID); } static String hSilentComputator_defaultURI() { int port = subBot_currentPort(); return (subBot_isHttps() ? "wss://" : "ws://") + domain() + (port == 80 ? "" : ":" + port) + "/"; } static String addSlash(String s) { return empty(s) || s.endsWith("/") ? s : s + "/"; } static String hMakeComputerID() { return hjavascript("\r\n function makeComputerID() {\r\n var text = \"\";\r\n var possible = \"abcdefghijklmnopqrstuvwxyz\";\r\n\r\n for (var i = 0; i < 12; i++)\r\n text += possible.charAt(Math.floor(Math.random() * possible.length));\r\n return text;\r\n }\r\n \r\n var computerID = localStorage.getItem('computer-id');\r\n if (computerID == null || computerID.length != 12) {\r\n computerID = makeComputerID();\r\n localStorage.setItem('computer-id', computerID);\r\n }\r\n "); } static String hreconnectingWebSockets() { return hjavascript_src_snippet("#1013374" /*#1013375*/); } static String nodes_pingCmd() { return "\r\n ws.send((working ? 'working' : 'idle') + '; cores=' + navigator.hardwareConcurrency + \"; id=\" + window.computerID\r\n + (window.benchResult != null ? \"; bench=\" + window.benchResult : \"\") \r\n + \"; b=\" + navigator.userAgent);\r\n "; } static String tr(Object contents) { return tag("tr", contents); } static String td(Object contents, Object... params) { return tag("td", contents, params); } static String href(String link, Object contents, Object... params) { if (link == null) return str(contents); return tag("a", contents, arrayPlus(params, "href", link)); } // params arrays can also be null static String htableRaw2(List data, List paramsTable, List paramsTR, List paramsTD) { StringBuilder buf = new StringBuilder(); for (List row : data) { buf.append(hopeningTag("tr", toObjectArray(paramsTR))); for (Object cell : row) buf.append(htd(cell, toObjectArray(paramsTD))).append("\n"); buf.append("\n"); } return htag("table", buf, toObjectArray(paramsTable)); } static String hEmbedYoutube(String youtubeID) { return hEmbedYoutube(youtubeID, 300, 250); } // text is HTML actually static String hEmbedYoutube(String youtubeID, int w, int h, String text) { return hEmbedYoutube(youtubeID, w, h).replace("View on YouTube", text); } static String hEmbedYoutube(String youtubeID, int w, int h) { youtubeID = extractYouTubeID(youtubeID); return "
View on YouTube".replace("{*}", youtubeID); } static String snippetImageLink(String snippetID) { return snippetImageURL(snippetID); } static TreeMap ciMap() { return caseInsensitiveMap(); } static void lock(Lock lock) { try { ping(); if (lock == null) return; try { lock.lockInterruptibly(); } catch (InterruptedException e) { print("Locking interrupted! I probably deadlocked, oops."); printStackTrace(e); rethrow(e); } ping(); } catch (Exception __e) { throw rethrow(__e); } } 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 mechList_raw_fresh(String name) { return mechList_opt_raw_fresh(name); } static void unlock(Lock lock, String msg) { if (lock == null) return; print("Unlocking: " + msg); lock.unlock(); } static void unlock(Lock lock) { if (lock == null) return; lock.unlock(); } static void add(BitSet bs, int i) { bs.set(i); } static boolean add(Collection c, A a) { return c != null && c.add(a); } static void add(Container c, Component x) { addToContainer(c, x); } static Set vm_busListeners_live_cache; static Set vm_busListeners_live() { if (vm_busListeners_live_cache == null) vm_busListeners_live_cache = vm_busListeners_live_load(); return vm_busListeners_live_cache; } static Set vm_busListeners_live_load() { return vm_generalIdentityHashSet("busListeners"); } 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); } 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 String addPrefix(String prefix, String s) { return s.startsWith(prefix) ? s : prefix + s; } 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 hfulltag(String tag) { return hfulltag(tag, ""); } static String hfulltag(String tag, Object contents, Object... params) { return hopeningTag(tag, params) + str(contents) + ""; } static BigInteger bigint(String s) { return new BigInteger(s); } static BigInteger bigint(long l) { return BigInteger.valueOf(l); } 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 boolean odd(int i) { return (i & 1) != 0; } static boolean odd(long i) { return (i & 1) != 0; } static boolean odd(BigInteger i) { return odd(toInt(i)); } 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(int n, Iterable i) { return dropFirst(n, toList(i)); } 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, int n) { return substring(s, n); } static String dropFirst(String s) { return substring(s, 1); } 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 Object optPar(Object[] params, String name) { return optParam(params, name); } static Object optPar(String name, Object[] params) { return optParam(params, name); } static Object optPar(String name, Map params) { return optParam(name, params); } static A optPar(Object[] params, String name, A defaultValue) { return optParam(params, name, defaultValue); } static A optPar(String name, Object[] params, A defaultValue) { return optParam(params, name, defaultValue); } static Object[] changeParam(Object[] params, String name, Object value) { if (eq(optPar(params, name), value)) return params; Map map = paramsToOrderedMap(params); map.put(name, value); return mapToParams(map); } static boolean eqOneOf(Object o, Object... l) { for (Object x : l) if (eq(o, x)) return true; return false; } static Object html_valueLessParam_cache; static Object html_valueLessParam() { if (html_valueLessParam_cache == null) html_valueLessParam_cache = html_valueLessParam_load(); return html_valueLessParam_cache; } static Object html_valueLessParam_load() { return new Object(); } static void _handleError(Error e) { call(javax(), "_handleError", e); } static String getStackTrace2(Throwable e) { return hideCredentials(getStackTrace(unwrapTrivialExceptionWraps(e)) + replacePrefix("java.lang.RuntimeException: ", "FAIL: ", hideCredentials(str(innerException2(e)))) + "\n"); } static void ai_useProgramID(String progID) { localSoftwareMadeWebs_programsToRead_set(ll(progID)); postSoftwareMadeWeb_programID_allThreads = progID; } static void ai_smartBotInit() { ai_tripleMode(); ai_useThoughtSpaces(true); ai_enabled(true); releaseDBLockOnTimeoutEval(); } static void smartSet(Field f, Object o, Object value) throws Exception { try { f.set(o, value); } catch (Exception e) { Class type = f.getType(); // take care of common case (long to int) if (type == int.class && value instanceof Long) value = ((Long) value).intValue(); if (type == LinkedHashMap.class && value instanceof Map) { f.set(o, asLinkedHashMap((Map) value)); return; } try { if (f.getType() == Concept.Ref.class) { f.set(o, ((Concept) o).new Ref((Concept) value)); return; } if (o instanceof Concept.Ref) { f.set(o, ((Concept.Ref) o).get()); return; } } catch (Throwable _e) {} throw e; } } static List> hotwire_classes = synchroList(); static Class hotwireDependent(String src) { Class c = hotwire(src); makeDependent(c); return c; } static A callMain(A c, String... args) { callOpt(c, "main", new Object[] {args}); return c; } static void callMain() { callMain(mc()); } static Map newDangerousWeakHashMap() { return _registerDangerousWeakMap(synchroMap(new WeakHashMap())); } // initFunction: voidfunc(Map) - is called initially, and after clearing the map static Map newDangerousWeakHashMap(Object initFunction) { return _registerDangerousWeakMap(synchroMap(new WeakHashMap()), initFunction); } static 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; if (callMC_cache == null) callMC_cache = new HashMap(); // initializer time workaround synchronized(callMC_cache) { me = method == callMC_key ? callMC_value : null; } if (me != null) try { return invokeMethod(me, null, args); } catch (IllegalArgumentException e) { throw new RuntimeException("Can't call " + me + " with arguments " + classNames(args), e); } List m; synchronized(callMC_cache) { m = callMC_cache.get(method); } if (m == null) { if (callMC_cache.isEmpty()) { callMC_makeCache(); m = callMC_cache.get(method); } if (m == null) throw fail("Method named " + method + " not found in main"); } int n = m.size(); if (n == 1) { me = m.get(0); synchronized(callMC_cache) { callMC_key = method; callMC_value = me; } try { return invokeMethod(me, null, args); } catch (IllegalArgumentException e) { throw new RuntimeException("Can't call " + me + " with arguments " + classNames(args), e); } } for (int i = 0; i < n; i++) { me = m.get(i); if (call_checkArgs(me, args, false)) return invokeMethod(me, null, args); } throw fail("No method called " + method + " with matching arguments found in main"); } catch (Exception __e) { throw rethrow(__e); } } 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 String getClassName(Object o) { return o == null ? "null" : o instanceof Class ? ((Class) o).getName() : o.getClass().getName(); } static Object invokeMethod(Method m, Object o, Object... args) { try { try { return m.invoke(o, args); } catch (InvocationTargetException e) { throw rethrow(getExceptionCause(e)); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(e.getMessage() + " - was calling: " + m + ", args: " + joinWithSpace(classNames(args))); } } catch (Exception __e) { throw rethrow(__e); } } static boolean call_checkArgs(Method m, Object[] args, boolean debug) { Class[] types = m.getParameterTypes(); if (types.length != args.length) { if (debug) print("Bad parameter length: " + args.length + " vs " + types.length); return false; } for (int i = 0; i < types.length; i++) { Object arg = args[i]; if (!(arg == null ? !types[i].isPrimitive() : isInstanceX(types[i], arg))) { if (debug) print("Bad parameter " + i + ": " + arg + " vs " + types[i]); return false; } } return true; } // firstDelay = delay static FixedRateTimer doEvery_daemon(long delay, final Object r) { return doEvery_daemon(delay, delay, r); } static FixedRateTimer doEvery_daemon(long delay, long firstDelay, final Object r) { FixedRateTimer timer = new FixedRateTimer(true); timer.scheduleAtFixedRate(smartTimerTask(r, timer, delay), firstDelay, delay); return timer; } static FixedRateTimer doEvery_daemon(double delaySeconds, final Object r) { return doEvery_daemon(toMS(delaySeconds), r); } static Var evaluateInBackground(final F0 f) { final Var var = new Var(); startThread(new Runnable() { public void run() { try { var.set(f.get()); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "var.set(f.get());"; }}); return var; } static String snippetWithTitle(String id) { return snippetWithTitle_cached(id); } static String snippetWithTitle(Snippet s) { return s == null ? "-" : s.id + " - " + s.title; } static int serverAutoRestartMD5_timeout = 10000; static String serverAutoRestartMD5(String programID) { return loadPageSilentlyWithTimeout("http://botcompany.de/1010693/raw?id=" + psI(programID), serverAutoRestartMD5_timeout); } static String serverAutoRestartMD5() { return serverAutoRestartMD5(programID()); } static boolean isMD5(String s) { return l(s) == 32 && isLowerHexString(s); } static boolean neq(Object a, Object b) { return !eq(a, b); } static String fsI(String id) { return formatSnippetID(id); } static String fsI(long id) { return formatSnippetID(id); } static void upgradeModuleVar(final String moduleVar, final String programID, final boolean callMain) { String name = snippetWithTitle(programID); upgradeModuleVar(moduleVar, programID, name, callMain); } static void upgradeModuleVar(final String moduleVar, final String programID, String name, final boolean callMain) { upgradeModuleVar(moduleVar, programID, name, callMain, null); } static void upgradeModuleVar(final String moduleVar, final String programID, String name, final boolean callMain, Object afterRestart) { infoBox("Upgrading module " + name); Object module = get(mc(), moduleVar); cleanUp(module); set(mc(), moduleVar, null); module = hotwireDependent(programID); if (callMain) callMain(module); set(mc(), moduleVar, module); pcallF(afterRestart); infoBox("Upgraded module " + name); } static boolean eqic(String a, String b) { if ((a == null) != (b == null)) return false; if (a == null) return true; return a.equalsIgnoreCase(b); } static boolean eqic(char a, char b) { if (a == b) return true; char u1 = Character.toUpperCase(a); char u2 = Character.toUpperCase(b); if (u1 == u2) return true; return Character.toLowerCase(u1) == Character.toLowerCase(u2); } // works on lists and strings and null static int indexOfIgnoreCase(List a, String b) { return indexOfIgnoreCase(a, b, 0); } static int indexOfIgnoreCase(List a, String b, int i) { int n = a == null ? 0 : a.size(); for (; i < n; i++) if (eqic(a.get(i), b)) return i; return -1; } static int indexOfIgnoreCase(String a, String b) { return indexOfIgnoreCase_manual(a, b); /*Matcher m = Pattern.compile(b, Pattern.CASE_INSENSITIVE + Pattern.LITERAL).matcher(a); if (m.find()) return m.start(); else ret -1;*/ } static Object call(Object o) { return callF(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) { //ret call_cached(o, method, args); return call_withVarargs(o, method, args); } static Object mainBot; static Object getMainBot() { return mainBot; } static String dropFrom(String s, String x) { if (s == null) return null; int i = s.indexOf(x); if (i < 0) return s; return substring(s, 0, i); } static boolean regionMatchesIC(String a, int offsetA, String b, int offsetB, int len) { return a != null && a.regionMatches(true, offsetA, b, offsetB, len); } static int strL(String s) { return s == null ? 0 : s.length(); } //sbool ping_actions_shareable = true; static volatile boolean ping_pauseAll; static int ping_sleep = 100; // poll pauseAll flag every 100 static volatile boolean ping_anyActions; static Map ping_actions = newWeakHashMap(); static ThreadLocal ping_isCleanUpThread = new ThreadLocal(); // always returns true static boolean ping() { if (ping_pauseAll || ping_anyActions ) ping_impl(true /* XXX */); //ifndef LeanMode ping_impl(); endifndef return true; } // returns true when it slept static boolean ping_impl(boolean okInCleanUp) { try { if (ping_pauseAll && !isAWTThread()) { do Thread.sleep(ping_sleep); while (ping_pauseAll); return true; } if (ping_anyActions) { // don't allow sharing ping_actions if (!okInCleanUp && !isTrue(ping_isCleanUpThread.get())) failIfUnlicensed(); Object action = null; synchronized(ping_actions) { if (!ping_actions.isEmpty()) { 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 Object getOpt(Object o, String field) { return getOpt_cached(o, field); } static Object getOpt(String field, Object o) { 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 rethrow(__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 rethrow(__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 A or(A a, A b) { return a != null ? a : b; } static int indexOf(List l, A a, int startIndex) { if (l == null) return -1; int n = l(l); for (int i = startIndex; i < n; 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, int i, char b) { return indexOf(a, b, i); } 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) { int n = l(x); for (int i = 0; i < n; i++) if (eq(x[i], a)) return i; return -1; } static String botAppendToMechList_now(String name, String text) { return botAppendToMechList_mode(name, text, "noUniq"); } static boolean triple_anyPartNull(T3 t) { return t == null || t.a == null || t.b == null || t.c == null; } static T3 trimTriple(T3 t) { return t == null ? null : triple(trim(t.a), trim(t.b), trim(t.c)); } static boolean ai_tripleAllowedToPost(T3 t) { //ret all(f ai_nodeNameAllowedToPost, tripleToList(t)); return ai_nodeNameAllowedToPost(t.a) && ai_nodeNameAllowedToPost(t.b) && ai_nodeNameAllowedToPost(t.c); } static boolean has(String a, String b, String c) { if (ai_cache_hasTriple(a, b, c)) return true; return false; } static boolean has(T3 t) { if (ai_cache_hasTriple(t)) return true; return false; } static T3 ai_tripleToInvalid(T3 t) { return triple(t.a, "[invalid] " + t.b, t.c); } static ThreadLocal ai_postHandler = new ThreadLocal(); static AI_PostHandler ai_postHandler() { if (ai_useThoughtSpaces()) return currentThoughtSpace(); return ai_postHandler.get(); } static T3 toSymbolTriple(T3 t) { return t3(symbol(t.a), symbol(t.b), symbol(t.c)); } static boolean isTrue(Object o) { if (o instanceof Boolean) return ((Boolean) o).booleanValue(); if (o == null) return false; if (o instanceof ThreadLocal) return isTrue(((ThreadLocal) o).get()); throw fail(getClassName(o)); } static String strOrNull(Object o) { return o == null ? null : str(o); } static Web webFromTriple(CharSequence a, CharSequence b, CharSequence c) { return webFromTriple(triple(symbol(a), symbol(b), symbol(c))); } static Web webFromTriple(T3 t) { if (t == null) return null; return webFromTriple(t, 0.2, 0.2, 0.8, 0.7); } static Web webFromTriple(T3 t, double x1, double y1, double x2, double y2) { if (t == null) return null; Web web = webWithoutIndexAndLock(); // save space String a = unnull(str(t.a)), b = unnull(str(t.b)), c = unnull(str(t.c)); // ensure we are making 3 nodes, even if texts are identical // ("singular is singular") WebNode nodeA = web.newNode(a), nodeC = web.newNode(c); web_addRelation(nodeA, nodeC, b); web_setPosition(first(web.nodes), x1, y1); web_setPosition(second(web.nodes), x2, y2); if (t instanceof TripleWeb) { TripleWeb w = (TripleWeb) t; web.globalID = w.globalID(); web.created = w.created(); web.source = w.source(); web.unverified = w.unverified(); } return web; } static ThreadLocal postSoftwareMadeWeb_disabled = new ThreadLocal(); static BetterThreadLocal postSoftwareMadeWeb_websMadeInThread = new BetterThreadLocal(); static ThreadLocal postSoftwareMadeWeb_maker = new ThreadLocal(); static ThreadLocal postSoftwareMadeWeb_programID = new ThreadLocal(); static String postSoftwareMadeWeb_programID_allThreads; // 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 __191 = aiLock(); lock(__191); try { web_intern(web); incIntThreadLocal(postSoftwareMadeWeb_websMadeInThread); String progID = postSoftwareMadeWeb_programID(); String maker = postSoftwareMadeWeb_maker.get(); if (maker != null) if (empty(web.source)) web.source = maker; else params = paramsPlus(params, "maker", postSoftwareMadeWeb_maker.get()); if (isTrue(postSoftwareMadeWeb_disabled.get())) { print(" Would post: " + webToStringShort(web) + " [" + web.source + "]"); pcallFAll(postSoftwareMadeWeb_onDisabledPost, web); } else { logStructure(postSoftwareMadeWeb_file(), nu(SoftwareMadeWeb.class, "date" , now(), "computerID" , computerID(), "programID" , progID, //structure := struct(web), "web", web, "furtherInfo" , empty(params) ? null : litorderedmap(params))); markWebsPosted_createMarker(progID); pcallFAll(postSoftwareMadeWeb_onNewWeb, web); } } finally { unlock(__191); } } static String postSoftwareMadeWeb_programID() { return or2(postSoftwareMadeWeb_programID.get(), postSoftwareMadeWeb_programID_allThreads, programID()); } static File postSoftwareMadeWeb_file() { return getProgramFile(postSoftwareMadeWeb_programID(), "webs-made.txt"); } static T3 t3(A a, B b, C c) { return new T3(a, b, c); } static boolean hasMethod(Object o, String method, Object... args) { return findMethod_cached(o, method, args) != null; } static ArrayList emptyList() { return new ArrayList(); //ret Collections.emptyList(); } static ArrayList emptyList(int capacity) { return new ArrayList(max(0, capacity)); } // Try to match capacity static ArrayList emptyList(Iterable l) { return l instanceof Collection ? emptyList(((Collection) l).size()) : emptyList(); } static ArrayList emptyList(Object[] l) { return emptyList(l(l)); } // get correct type at once static ArrayList emptyList(Class c) { return new ArrayList(); } static ArrayList litlist(A... a) { ArrayList l = new ArrayList(a.length); for (A x : a) l.add(x); return l; } static Object callOpt(Object o) { return callF(o); } static A callOpt(Object o, String method, Object... args) { return (A) callOpt_withVarargs(o, method, args); } static String emptyToNull(String s) { return eq(s, "") ? null : s; } static Map emptyToNull(Map map) { return empty(map) ? null : map; } static Throwable getInnerException(Throwable e) { if (e == null) return null; while (e.getCause() != null) e = e.getCause(); return e; } static Throwable getInnerException(Runnable r) { return getInnerException(getException(r)); } static Object collectionMutex(Object o) { String c = className(o); if (eq(c, "java.util.TreeMap$KeySet")) c = className(o = getOpt(o, "m")); else if (eq(c, "java.util.HashMap$KeySet")) c = className(o = get_raw(o, "this$0")); if (eqOneOf(c, "java.util.TreeMap$AscendingSubMap", "java.util.TreeMap$DescendingSubMap")) c = className(o = get_raw(o, "m")); return o; } static int iround(double d) { return (int) Math.round(d); } static int iround(Number n) { return iround(toDouble(n)); } static int indexOfAfter(String a, String b) { int i = indexOf(a, b); return i >= 0 ? i+l(b) : i; } static B mapGet(Map map, A a) { return map == null || a == null ? null : map.get(a); } static B mapGet(A a, Map map) { return map == null || a == null ? null : map.get(a); } static Map decodeHQuery(String query) { Map map = new HashMap(); for (String s : splitAtAmpersand(query)) { int i = s.indexOf('='); if (i >= 0) map.put(urldecode(s.substring(0, i)), urldecode(s.substring(i+1))); } return map; } 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(); HashMap persistenceInfo = 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) { try { 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 Short) { short s = ((Short) o).shortValue(); d.append("sh ", 2); out.print(s); d.n += s < 0 ? 2 : 1; return; } if (o instanceof Float) { d.append("fl ", 2); quoteToPrintWriter(str(o), out); return; } if (o instanceof Double) { d.append("d(", 3); quoteToPrintWriter(str(o), out); d.append(")"); return; } if (o instanceof BigInteger) { out.print("bigint("); out.print(o); out.print(")"); d.n += ((BigInteger) o).signum() < 0 ? 5 : 4; return; } } if (o instanceof Boolean) { d.append(((Boolean) o).booleanValue() ? "t" : "f"); return; } if (o instanceof Character) { d.append(quoteCharacter((Character) o)); return; } if (o instanceof File) { d.append("File ").append(quote(((File) o).getPath())); return; } // 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 Set) { /*O set2 = unwrapSynchronizedSet(o); if (set2 != o) { d.append("sync"); o = set2; } TODO */ if (((Set) o) instanceof TreeSet) { d.append(isCISet_gen(((Set) o)) ? "ciset" : "treeset"); structure_1(new ArrayList(((Set) o)), d); return; } // assume it's a HashSet or LinkedHashSet d.append(((Set) o) instanceof LinkedHashSet ? "lhs" : "hashset"); structure_1(new ArrayList(((Set) o)), d); return; } String name = c.getName(); if (o instanceof Collection && !startsWith(name, "main$") /* && neq(name, "main$Concept$RefL") */) { // it's a list if (name.equals("java.util.Collections$SynchronizedList") || name.equals("java.util.Collections$SynchronizedRandomAccessList")) d.append("sync"); if (name.equals("java.util.LinkedList")) d.append("ll"); d.append("["); final int l = d.n; final Iterator it = ((Collection) o).iterator(); d.stack.add(new Runnable() { public void run() { try { if (!it.hasNext()) d.append("]"); else { d.stack.add(this); if (d.n != l) d.append(", "); structure_1(it.next(), d); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (!it.hasNext())\r\n d.append(\"]\");\r\n else {\r\n d.sta..."; }}); return; } if (o instanceof Map && !startsWith(name, "main$")) { if (o instanceof LinkedHashMap) d.append("lhm"); else if (o instanceof HashMap) d.append("hm"); else if (o instanceof TreeMap) d.append(isCIMap_gen(((TreeMap) o)) ? "cimap" : "tm"); else if (name.equals("java.util.Collections$SynchronizedMap")) d.append("sync"); else if (name.equals("java.util.Collections$SynchronizedSortedMap")) { d.append("sync tm", 2); } d.append("{"); final int l = d.n; final Iterator it = ((Map) o).entrySet().iterator(); d.stack.add(new Runnable() { 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)) { String fieldName = field.getName(); if (fieldName.equals("_persistenceInfo")) d.persistenceInfo.put(c, field); if ((field.getModifiers() & (java.lang.reflect.Modifier.STATIC | java.lang.reflect.Modifier.TRANSIENT)) != 0) continue; fields.add(field); // put special cases here...? } cc = cc.getSuperclass(); } // TODO: S fieldOrder = getOpt(c, "_fieldOrder"); 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 } Field persistenceInfoField = (Field) (d.persistenceInfo.get(c)); Map persistenceInfo = persistenceInfoField == null ? null : (Map) persistenceInfoField.get(o); LinkedHashMap fv = new LinkedHashMap(); for (Field f : lFields) { Object value; try { value = f.get(o); } catch (Exception e) { value = "?"; } if (value != null && (persistenceInfo == null || !Boolean.FALSE.equals(persistenceInfo.get(f.getName())))) fv.put(f.getName(), value); } String name = c.getName(); String shortName = dropPrefix("main$", name); if (startsWithDigit(shortName)) shortName = name; // for anonymous classes // 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..."; }}); } catch (Exception __e) { throw rethrow(__e); } } /*static L printNumberedLines(L l) { printNumberedLines((Collection) l); ret l; } static L printNumberedLines(S prefix, L l) { printNumberedLines(prefix, (Collection) l); ret l; }*/ static void printNumberedLines(Map map) { printNumberedLines(mapToLines(map)); } static A printNumberedLines(A l) { int i = 0; if (l != null) for (Object a : l) print((++i) + ". " + str(a)); return l; } static A printNumberedLines(String prefix, A l) { int i = 0; if (l != null) for (Object a : l) print(prefix + (++i) + ". " + str(a)); return l; } static void printNumberedLines(Object[] l) { printNumberedLines(asList(l)); } static void printNumberedLines(Object o) { printNumberedLines(lines(str(o))); } static String lines(Iterable lines) { return fromLines(lines); } static String lines(Object[] lines) { return fromLines(asList(lines)); } static List lines(String s) { return toLines(s); } static String structureForUser(Object o) { return beautifyStructure(struct_noStringSharing(o)); } static RuntimeException asRuntimeException(Throwable t) { if (t instanceof Error) _handleError((Error) t); return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t); } static boolean startsWith(String a, String b) { return a != null && a.startsWith(b); } static boolean startsWith(String a, char c) { return nemptyString(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, strL(b))}; return true; } static boolean startsWith(List a, List b) { if (a == null || listL(b) > listL(a)) return false; for (int i = 0; i < listL(b); i++) if (neq(a.get(i), b.get(i))) return false; return true; } static String hopeningTag(String tag, Map params) { return hopeningTag(tag, mapToParams(params)); } 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) { if (val == html_valueLessParam()) buf.append(" " + name); else { String s = str(val); if (!empty(s)) buf.append(" " + name + "=" + htmlQuote(s)); } } } buf.append(">"); return str(buf); } static List htmlTok(String s) { return htmlcoarsetok(s); } static Object first(Object list) { return first((Iterable) list); } 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(IterableIterator i) { return first((Iterator) i); } static A first(Iterator i) { return i == null || !i.hasNext() ? null : i.next(); } 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 A first(T3 t) { return t == null ? null : t.a; } // 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> 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 String addLineBreak(String s) { return addSuffix(s, "\n"); } public static String join(String glue, Iterable strings) { if (strings == null) return ""; if (strings instanceof Collection) { if (((Collection) strings).size() == 1) return str(first(((Collection) strings))); } 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 String unnull(String s) { return s == null ? "" : s; } static Collection unnull(Collection l) { return l == null ? emptyList() : l; } static List unnull(List l) { return l == null ? emptyList() : l; } static Map unnull(Map l) { return l == null ? emptyMap() : 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; } //ifclass Symbol static Pair unnull(Pair p) { return p != null ? p : new Pair(null, null); } static boolean neqic(String a, String b) { return !eqic(a, b); } static boolean neqic(char a, char b) { return !eqic(a, b); } 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 rawSnippetURL(String snippetID) { return snippetRawURL(snippetID); } static String rawSnippetURL(String snippetID, String contentType) { return snippetRawURL(snippetID, contentType); } static String hjavascript_src_snippet(String snippetID) { return tag("script", "", "src" , snippetRawURL(snippetID)); } static int subBot_currentPort() { return toInt(getOpt(subBot_httpd(), "myPort")); } static boolean subBot_isHttps() { Object httpd = subBot_httpd(); return eqOneOf(httpd, getOpt(mainBot(), "serveHttps_server"), getOpt(mainBot(), "serveHttpsWithWebsockets_server")) || contains((Collection) getOpt(mainBot(), "serveHttpsWithWebsockets_multiplePorts_servers"), httpd); } static Object[] toObjectArray(Collection c) { List l = asList(c); return l.toArray(new Object[l.size()]); } static String htd(Object contents, Object... params) { return htag("td", contents, params); } static String snippetImageURL(String snippetID) { return snippetImageURL(snippetID, "png"); } static String snippetImageURL(String snippetID, String contentType) { if (isURL(snippetID)) return snippetID; long id = parseSnippetID(snippetID); String url; if (id == 1000010 || id == 1000012) url = "http://tinybrain.de:8080/tb/show-blobimage.php?id=" + id; else if (isImageServerSnippet(id)) url = imageServerLink(id); else //url = "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + id + "&contentType=image/" + contentType; url = "https://www.botcompany.de:8443/img/" + id; return url; } static TreeMap caseInsensitiveMap() { return new TreeMap(caseInsensitiveComparator()); } 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(String msg, Throwable e) { printStackTrace(new Throwable(msg, e)); } 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 ReentrantLock fairLock() { return new ReentrantLock(true); } static String mechList_opt_raw_fresh(String name) { Object readMode = mechMode().readMode; if (readMode instanceof File) { String md5Name = uniqueFileNameUsingMD5_80_v2(upper(name)); return loadTextFileFromZipFile((File) readMode, md5Name); } boolean useLocal = false, useRemote = true; if (eq(readMode, "mergeLocalAndRemote")) useLocal = true; else if (eq(readMode, "local")) { useLocal = true; useRemote = false; } String s = ""; if (useRemote) { if (eq(mechMode().readMode, "localCopies")) s = unnull(loadTextFile(remoteMechListMirrorFile(name))); else s = serverMechList_raw_fresh(name, true); } if (useLocal) s = appendNewLineIfNempty(s) + localMechList_opt_raw_fresh(name); return s; } static void addToContainer(final Container a, final Component b) { if (a != null && b != null) { swing(new Runnable() { public void run() { try { a.add(b); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "a.add(b);"; }}); } } static Set vm_generalIdentityHashSet(Object name) { synchronized(get(javax(), "generalMap")) { Set set = (Set) (vm_generalMap_get(name)); if (set == null) vm_generalMap_put(name, set = syncIdentityHashSet()); return set; } } 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 String formatSnippetIDOpt(String s) { return isSnippetID(s) ? formatSnippetID(s) : s; } static String formatSnippetID(String id) { return "#" + parseSnippetID(id); } static String formatSnippetID(long id) { return "#" + id; } static Class getMainClass() { return mc(); } static Class getMainClass(Object o) { try { if (o == null) return null; if (o instanceof Class && eq(((Class) o).getName(), "x30")) return (Class) o; return (o instanceof Class ? (Class) o : o.getClass()).getClassLoader().loadClass("main"); } catch (Exception __e) { throw rethrow(__e); } } 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 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 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 A optParam(ThreadLocal tl, A defaultValue) { return optPar(tl, defaultValue); } static A optParam(ThreadLocal tl) { return optPar(tl); } static Object optParam(String name, Map params) { return mapGet(params, name); } // now also takes a map as single array entry static A optParam(Object[] opt, String name, A defaultValue) { int n = l(opt); if (n == 1 && opt[0] instanceof Map) { Map map = (Map) (opt[0]); return map.containsKey(name) ? (A) map.get(name) : defaultValue; } if (!even(l(opt))) throw fail("Odd parameter length"); for (int i = 0; i < l(opt); i += 2) if (eq(opt[i], name)) return (A) opt[i+1]; return defaultValue; } static Object optParam(Object[] opt, String name) { return optParam(opt, name, null); } static LinkedHashMap paramsToOrderedMap(Object... params) { return asLinkedHashMap(paramsToMap(params)); } static Object[] mapToParams(Map map) { return mapToObjectArray(map); } static Class javax() { return getJavaX(); } static String hideCredentials(URL url) { return url == null ? null : hideCredentials(str(url)); } static String hideCredentials(String url) { try { if (startsWithOneOf(url, "http://", "https://") && isAGIBlueDomain(hostNameFromURL(url))) return url; } catch (Throwable e) { print("HideCredentials", e); } return url.replaceAll("([&?])(_pass|key)=[^&\\s\"]*", "$1$2="); } static String hideCredentials(Object o) { return hideCredentials(str(o)); } static String getStackTrace(Throwable throwable) { lastException(throwable); return getStackTrace_noRecord(throwable); } static String getStackTrace_noRecord(Throwable throwable) { StringWriter writer = new StringWriter(); throwable.printStackTrace(new PrintWriter(writer)); return hideCredentials(writer.toString()); } static String getStackTrace() { return getStackTrace_noRecord(new Throwable()); } static Throwable unwrapTrivialExceptionWraps(Throwable e) { if (e == null) return e; while (e.getClass() == RuntimeException.class && e.getCause() != null && eq(e.getMessage(), str(e.getCause()))) e = e.getCause(); return e; } static String replacePrefix(String prefix, String replacement, String s) { if (!startsWith(s, prefix)) return s; return replacement + substring(s, l(prefix)); } static Throwable innerException2(Throwable e) { if (e == null) return null; while (empty(e.getMessage()) && e.getCause() != null) e = e.getCause(); return e; } static void localSoftwareMadeWebs_programsToRead_set(List l) { localSoftwareMadeWebs_programsToRead_list = l; } static void ai_tripleMode() { if (cachedNodeIndex_autoMake) { cachedNodeIndex_autoMake = false; ai_fillTripleIndex(); } } static boolean ai_useThoughtSpaces_value; static boolean ai_useThoughtSpaces() { return ai_useThoughtSpaces_value; } static void ai_useThoughtSpaces(boolean flag) { ai_useThoughtSpaces_value = flag; } static boolean ai_enabled_value; static boolean ai_enabled() { return ai_enabled_value; } static void ai_enabled(boolean value) { ai_enabled_value = value; } static void releaseDBLockOnTimeoutEval() { callOptMC("beforeDelegatingToThread_do", "releaseDBLockOnTimeoutEval_before"); callOptMC("afterDelegatingToThread_do", "releaseDBLockOnTimeoutEval_after"); } static ThreadLocal releaseDBLockOnTimeoutEval_hadLock = new ThreadLocal(); static void releaseDBLockOnTimeoutEval_before(Thread t) { releaseDBLockOnTimeoutEval_hadLock.set(unlockIfHolding(dbLock())); } static void releaseDBLockOnTimeoutEval_after(Thread t) { if (isTrue(getAndClearThreadLocal(releaseDBLockOnTimeoutEval_hadLock))) lock(dbLock()); } static LinkedHashMap asLinkedHashMap(Map map) { if (map instanceof LinkedHashMap) return (LinkedHashMap) map; LinkedHashMap m = new LinkedHashMap(); if (map != null) synchronized(collectionMutex(map)) { m.putAll(map); } return m; } 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 { Class c = (Class) (call(j, "hotwire", src)); hotwire_copyOver(c); return c; } } static Object makeDependent_postProcess; static void makeDependent(Object c) { if (c == null) return; assertTrue("Not a class", c instanceof Class); dependentClasses(); // cleans up the list hotwire_classes.add(new WeakReference(c)); Object local_log = getOpt(mc(), "local_log"); if (local_log != null) setOpt(c, "local_log", local_log); /*if (isTrue(getOpt(c, 'ping_actions_shareable))) setOpt(c, +ping_actions);*/ Object print_byThread = getOpt(mc(), "print_byThread"); if (print_byThread != null) setOpt(c, "print_byThread", print_byThread); callF(makeDependent_postProcess, c); } static List _registerDangerousWeakMap_preList; static A _registerDangerousWeakMap(A map) { return _registerDangerousWeakMap(map, null); } static A _registerDangerousWeakMap(A map, Object init) { callF(init, map); if (init instanceof String) { final String f = (String) init; init = new VF1() { public void get(Map map) { try { callMC(f, map) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callMC(f, map)"; }}; } if (javax() == null) { // We're in class init if (_registerDangerousWeakMap_preList == null) _registerDangerousWeakMap_preList = synchroList(); _registerDangerousWeakMap_preList.add(pair(map, init)); return map; } call(javax(), "_registerDangerousWeakMap", map, init); return map; } static void _onLoad_registerDangerousWeakMap() { assertNotNull(javax()); if (_registerDangerousWeakMap_preList == null) return; for (Pair p : _registerDangerousWeakMap_preList) _registerDangerousWeakMap(p.a, p.b); _registerDangerousWeakMap_preList = null; } static Map synchroMap() { return synchroHashMap(); } static Map synchroMap(Map map) { return Collections.synchronizedMap(map); } static List classNames(Collection l) { return getClassNames(l); } static List classNames(Object[] l) { return getClassNames(Arrays.asList(l)); } 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 void multiMapPut(MultiMap mm, A key, B value) { if (mm != null && key != null && value != null) mm.put(key, value); } static Throwable getExceptionCause(Throwable e) { Throwable c = e.getCause(); return c != null ? c : e; } static String joinWithSpace(Collection c) { return join(" ", c); } static String joinWithSpace(String... c) { return join(" ", c); } 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); } // r may return false to cancel timer static TimerTask smartTimerTask(Object r, java.util.Timer timer, long delay) { return new SmartTimerTask(r, timer, delay, _threadInfo()); } static class SmartTimerTask extends TimerTask { static String _fieldOrder = "r timer delay threadInfo lastRun"; Object r; java.util.Timer timer; long delay; Object threadInfo; SmartTimerTask() {} SmartTimerTask(Object r, java.util.Timer timer, long delay, Object threadInfo) { this.threadInfo = threadInfo; this.delay = delay; this.timer = timer; this.r = r;} public String toString() { return "SmartTimerTask(" + r + ", " + timer + ", " + delay + ", " + threadInfo + ")"; } long lastRun; public void run() { if (!licensed()) timer.cancel(); else { _threadInheritInfo(threadInfo); AutoCloseable __317 = tempActivity(r); try { lastRun = fixTimestamp(lastRun); long now = now(); if (now >= lastRun + delay*0.9) { lastRun = now; if (eq(false, pcallF(r))) timer.cancel(); } } finally { _close(__317); }} } } static long toMS(double seconds) { return (long) (seconds*1000); } static Thread startThread(Object runnable) { return startThread(defaultThreadName(), runnable); } static Thread startThread(String name, Object runnable) { runnable = wrapAsActivity(runnable); return startThread(newThread(toRunnable(runnable), name)); } static Thread startThread(Thread t) { _registerThread(t); t.start(); return t; } static String snippetWithTitle_cached(String id) { if (!isSnippetID(id)) return id; id = formatSnippetID(id); return id + " - " + getSnippetTitle_cached(id); } static String snippetWithTitle_cached(Snippet s) { return s == null ? "-" : s.id + " - " + s.title; } static String loadPageSilentlyWithTimeout(int timeout, String url) { return loadPageSilentlyWithTimeout(url, timeout); } static String loadPageSilentlyWithTimeout(String url, int timeout) { try { url = loadPage_preprocess(url); URL _url = new URL(url); return loadPage( setURLConnectionTimeouts(_url.openConnection(), timeout), _url); } catch (Exception __e) { throw rethrow(__e); } } static long psI(String snippetID) { return parseSnippetID(snippetID); } 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 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); } // get purpose 1: access a list/array/map (safer version of x.get(y)) 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 == null) return null; 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 Object get(String field, Object o) { return get(o, field); } 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 preCleanUp(c); // unpause setOpt(c, "ping_pauseAll", false); // call custom cleanMeUp() and cleanMeUp_*() functions innerCleanUp(c); // Java spec says finalize should only be called by GC, // but we care to differ. // Edit: Not anymore (illegal access warnings) /*if (isTrue(vmMap_get('callFinalize))) 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) { _handleException(__e); }} // interrupt all threads (experimental, they might be doing cleanup?) if (cleanUp_interruptThreads) { List threads = registeredThreads(c); if (nempty(threads)) { print("cleanUp: Interrupting " + n2(threads, "thread") + ": " + joinWithComma(allToString(threads))); interruptThreads(threads); } } } catch (Throwable __e) { _handleException(__e); } setOpt(c, "cleaningUp_flag" , false); if (c instanceof Class && ((Class) c).getName().equals("main")) retireClassLoader(((Class) c).getClassLoader()); } static void cleanUp(Collection l) { if (l == null) return; for (Object c : l) cleanUp(c); l.clear(); } static Object pcallF(Object f, Object... args) { return pcallFunction(f, args); } static A pcallF(F0 f) { try { return f == null ? null : f.get(); } catch (Throwable __e) { return null; } } static B pcallF(F1 f, A a) { try { return f == null ? null : f.get(a); } catch (Throwable __e) { return null; } } static void pcallF(VF1 f, A a) { try { if (f != null) f.get(a); } catch (Throwable __e) { _handleException(__e); } } static String asString(Object o) { return o == null ? null : o.toString(); } static int indexOfIgnoreCase_manual(String a, String b) { int la = strL(a), lb = strL(b); if (la < lb) return -1; int n = la-lb; loop: for (int i = 0; i <= n; i++) { for (int j = 0; j < lb; j++) { char c1 = a.charAt(i+j), c2 = b.charAt(j); if (!eqic(c1, c2)) continue loop; } return i; } return -1; } static Object call_withVarargs(Object o, String method, Object... args) { try { if (o == null) return null; if (o instanceof Class) { Class c = (Class) o; _MethodCache cache = callOpt_getCache(c); Method me = cache.findStaticMethod(method, args); if (me != null) return invokeMethod(me, null, args); // try varargs List methods = cache.cache.get(method); if (methods != null) methodSearch: for (Method m : methods) { { if (!(m.isVarArgs())) continue; } { if (!(isStaticMethod(m))) continue; } Object[] newArgs = massageArgsForVarArgsCall(m, args); if (newArgs != null) return invokeMethod(m, null, newArgs); } throw fail("Method " + c.getName() + "." + method + "(" + joinWithComma(classNames(args)) + ") not found"); } else { Class c = o.getClass(); _MethodCache cache = callOpt_getCache(c); Method me = cache.findMethod(method, args); if (me != null) return invokeMethod(me, o, args); // try varargs List methods = cache.cache.get(method); if (methods != null) methodSearch: for (Method m : methods) { { if (!(m.isVarArgs())) continue; } Object[] newArgs = massageArgsForVarArgsCall(m, args); if (newArgs != null) return invokeMethod(m, o, newArgs); } throw fail("Method " + c.getName() + "." + method + "(" + joinWithComma(classNames(args)) + ") not found"); } } catch (Exception __e) { throw rethrow(__e); } } static Map newWeakHashMap() { return _registerWeakMap(synchroMap(new WeakHashMap())); } // TODO: test if android complains about this static boolean isAWTThread() { if (isAndroid()) return false; if (isHeadless()) return false; return isAWTThread_awt(); } static boolean isAWTThread_awt() { return SwingUtilities.isEventDispatchThread(); } static void failIfUnlicensed() { assertTrue("license off", licensed()); } static Thread currentThread() { return Thread.currentThread(); } //static final Map> getOpt_cache = newDangerousWeakHashMap(f getOpt_special_init); static class getOpt_Map extends WeakHashMap { getOpt_Map() { if (getOpt_special == null) getOpt_special = new HashMap(); clear(); } public void clear() { super.clear(); //print("getOpt clear"); put(Class.class, getOpt_special); put(String.class, getOpt_special); } } static final Map> getOpt_cache = _registerDangerousWeakMap(synchroMap(new getOpt_Map())); //static final Map> getOpt_cache = _registerWeakMap(synchroMap(new getOpt_Map)); static HashMap getOpt_special; // just a marker /*static void getOpt_special_init(Map map) { map.put(Class.class, getOpt_special); map.put(S.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(); if (!reflection_classesNotToScan().contains(c.getName())) { 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); } } if (getOpt_cache != null) getOpt_cache.put(c, map); return map; } 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 botAppendToMechList_mode(String name, String text, String mode) { if (empty(name) || empty(text)) return ""; String result = postPageSilently("http://butter.botcompany.de:8080/mech/raw/bot-list-append", arrayPlus(muricaCredentials(), "name", name, "text", text, "mode", mode)); if (!swic(result, "No change")) { if (eq(mechMode().readMode, "localCopies")) { print("Appending to local list copy: " + name); appendToLocalMechListCopy(name, text); } mech_notifyBusOfChange(name); mechList_clearCache(name); } //infoBox(name + ": " + result); return result; } static String botAppendToMechList_mode(String name, Collection lines, String mode) { if (empty(lines)) return ""; return botAppendToMechList_mode(name, lines(lines), mode); } static T3 triple(A a, B b, C c) { return new T3(a, b, c); } static boolean ai_nodeNameAllowedToPost(String s) { return !isDollarVar(s); } static boolean ai_cache_hasTriple_debug; static boolean ai_cache_hasTriple(String a, String b, String c) { if (a == null || b == null || c == null) return false; if (ai_useThoughtSpaces()) return thoughtSpace().hasTriple(symbol(a), symbol(b), symbol(c)); AI_CacheChecker checker = ai_cacheChecker(); if (checker != null && checker.hasTriple(symbol(a), symbol(b), symbol(c))) return true; List webs = ai_fewestIndexedWebs(a, b, c); boolean x = webs_search_noVar_bool(webFromTriple(a, b, c), webs); if (ai_cache_hasTriple_debug) print("hasTriple: " + sfuLL(a, b, c, x) + ", searched " + nWebs(webs)); return x; } static boolean ai_cache_hasTriple(T3 t) { return ai_cache_hasTriple(t.a, t.b, t.c); } static ThreadLocal currentThoughtSpace_value = new ThreadLocal() { public AbstractThoughtSpace initialValue() { return new GlobalThoughtSpace(); } }; static AbstractThoughtSpace currentThoughtSpace() { return currentThoughtSpace_value.get(); } static Web webWithoutIndexAndLock() { Web web = simpleWeb(); return web; } 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 WebNode web_setPosition(WebNode node, double x, double y) { node.x = x; node.y = y; return node; } static A second(List l) { return get(l, 1); } static A second(Iterable l) { if (l == null) return null; Iterator it = iterator(l); if (!it.hasNext()) return null; it.next(); return it.hasNext() ? it.next() : null; } 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 A second(Producer p) { if (p == null) return null; if (p.next() == null) return null; return p.next(); } static Lock aiLock() { return dbLock(); } static Web web_intern(Web web) { if (web == null) return null; for (WebNode n : web_nodesAndRelations(web)) web_setLabels_forced(n, map_lisp_intern(n.labels())); web.source = intern(web.source); web.title = intern(web.title); trimToSize(web.nodes); for (WebNode n : web.nodes) if (n.labels instanceof List) trimToSize((List) n.labels); //web.globalID = intern(web.globalID); if (web.globalID instanceof String) web.globalID = new GlobalID((String) web.globalID); return web; } static void incIntThreadLocal(ThreadLocal tl) { tl.set(toInt(tl.get())+1); } static void incIntThreadLocal(BetterThreadLocal tl) { tl.set(toInt(tl.get())+1); } static Object[] paramsPlus(Object[] a1, Object... a2) { if (a2 == null) return a1; if (a1 == null) return a2; if (l(a1) == 1 && a1[0] instanceof Map) return new Object[] { mapPlus((Map) a1[0], a2) }; assertEvenLength(a1); assertEvenLength(a2); Map map = paramsToOrderedMap(a1); int n = l(a2); for (int i = 0; i < n; i += 2) { Object key = a2[i]; if (key != null) map.put(key, a2[i+1]); } return mapToParams(map); } static String webToStringShort(Web web) { try { if (web == null) return "-"; int n = l(web_nodes(web)); if (n == 0) return "Empty web"; WebNode a = web_firstNode(web); if (n == 1) return join(" = ", web_texts(a)); if (n == 2) { WebNode b = web_secondNode(web); return ai_renderTriple(web_text(a), web_text(web_getRelation(a, b)), web_text(b)); } return "Includes " + joinWithComma(map("web_text",web_nodes(web))); } catch (Throwable e) { _handleException(e); return "Error"; } } static void pcallFAll(Collection l, Object... args) { if (l != null) for (Object f : cloneList(l)) pcallF(f, args); } static void pcallFAll(Iterator it, Object... args) { while (it.hasNext()) pcallF(it.next(), args); } 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 A nu(Class c, Object... values) { A a = nuObject(c); setAll(a, values); return a; } static String _computerID; static Lock computerID_lock = lock(); public static String computerID() { if (_computerID == null) { Lock __574 = computerID_lock; lock(__574); try { if (_computerID != null) return _computerID; File file = computerIDFile(); _computerID = loadTextFile(file.getPath()); if (_computerID == null) { // legacy load _computerID = loadTextFile(userDir(".tinybrain/computer-id")); if (_computerID == null) _computerID = makeRandomID(12, new SecureRandom()); saveTextFile(file, _computerID); } } finally { unlock(__574); } } return _computerID; } static LinkedHashMap litorderedmap(Object... x) { LinkedHashMap map = new LinkedHashMap(); litmap_impl(map, x); return map; } static void markWebsPosted_createMarker() { markWebsPosted_createMarker(programID()); } static void markWebsPosted_createMarker(String progID) { createMarkerFile("#1007609", "webs.posted.at." + psI(progID)); } static String or2(String a, String b) { return nempty(a) ? a : b; } static String or2(String a, String b, String c) { return or2(or2(a, b), c); } 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 Method findMethod_cached(Object o, String method, Object... args) { try { if (o == null) return null; if (o instanceof Class) { _MethodCache cache = callOpt_getCache(((Class) o)); List methods = cache.cache.get(method); if (methods != null) for (Method m : methods) if (isStaticMethod(m) && findMethod_checkArgs(m, args, false)) return m; return null; } else { _MethodCache cache = callOpt_getCache(o.getClass()); List methods = cache.cache.get(method); if (methods != null) for (Method m : methods) if (findMethod_checkArgs(m, args, false)) return m; return null; } } catch (Exception __e) { throw rethrow(__e); } } 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 int max(int[] c) { int x = Integer.MIN_VALUE; for (int d : c) if (d > x) x = d; return x; } static Object callOpt_withVarargs(Object o, String method, Object... args) { try { if (o == null) return null; if (o instanceof Class) { Class c = (Class) o; _MethodCache cache = callOpt_getCache(c); Method me = cache.findMethod(method, args); if (me == null) { // TODO: varargs return null; } if ((me.getModifiers() & Modifier.STATIC) == 0) return null; return invokeMethod(me, null, args); } else { Class c = o.getClass(); _MethodCache cache = callOpt_getCache(c); Method me = cache.findMethod(method, args); if (me != null) return invokeMethod(me, o, args); // try varargs List methods = cache.cache.get(method); if (methods != null) methodSearch: for (Method m : methods) { { if (!(m.isVarArgs())) continue; } Object[] newArgs = massageArgsForVarArgsCall(m, args); if (newArgs != null) return invokeMethod(m, o, newArgs); } return null; } } catch (Exception __e) { throw rethrow(__e); } } static Throwable getException(Runnable r) { try { callF(r); return null; } catch (Throwable e) { return e; } } static String className(Object o) { return getClassName(o); } static double toDouble(Object o) { if (o instanceof Number) return ((Number) o).doubleValue(); if (o instanceof BigInteger) return ((BigInteger) o).doubleValue(); if (o == null) return 0.0; throw fail(o); } static List splitAtAmpersand(String s) { return asList(s.split("&")); } static String urldecode(String x) { try { return URLDecoder.decode(unnull(x), "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } static A assertEquals(Object x, A y) { return assertEquals(null, x, y); } static A assertEquals(String msg, Object x, A y) { if (assertVerbose()) return assertEqualsVerbose(msg, x, y); if (!(x == null ? y == null : x.equals(y))) throw fail((msg != null ? msg + ": " : "") + y + " != " + x); return y; } 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(javaTok_substringC(s, i, j)); i = j; } return tok; } static A popLast(List l) { return liftLast(l); } static void quoteToPrintWriter(String s, PrintWriter out) { if (s == null) { out.print("null"); return; } out.print('"'); int l = s.length(); for (int i = 0; i < l; i++) { char c = s.charAt(i); if (c == '\\' || c == '"') { out.print('\\'); out.print(c); } else if (c == '\r') out.print("\\r"); else if (c == '\n') out.print("\\n"); else if (c == '\0') out.print("\\0"); else out.print(c); } out.print('"'); } 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 int shorten_default = 100; static String shorten(String s) { return shorten(s, shorten_default); } static String shorten(String s, int max) { return shorten(s, max, "..."); } static String shorten(String s, int max, String shortener) { if (s == null) return ""; if (max < 0) return s; return s.length() <= max ? s : substring(s, 0, min(s.length(), max-l(shortener))) + shortener; } static String shorten(int max, String s) { return shorten(s, max); } static boolean isCISet_gen(Iterable l) { return l instanceof TreeSet && className(((TreeSet) l).comparator()).contains("CIComp"); } static boolean isCIMap_gen(Map map) { return map instanceof TreeMap && className(((TreeMap) map).comparator()).contains("CIComp"); } 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 String boolArrayToHex(boolean[] a) { return bytesToHex(boolArrayToBytes(a)); } static Iterator emptyIterator() { return Collections.emptyIterator(); } static String shortDynamicClassName(Object o) { if (o instanceof DynamicObject && ((DynamicObject) o).className != null) return ((DynamicObject) o).className; return shortClassName(o); } 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 Map getDeclaredFields_cache = newDangerousWeakHashMap(); 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 String dropPrefix(String prefix, String s) { return s == null ? null : s.startsWith(prefix) ? s.substring(l(prefix)) : s; } static boolean startsWithDigit(String s) { return nempty(s) && isDigit(s.charAt(0)); } static List mapToLines(Map map) { List l = new ArrayList(); for (Object key : keys(map)) l.add(str(key) + " = " + str(map.get(key))); return l; } static String mapToLines(Map map, Object f) { return lines(map(map, f)); } static String mapToLines(Object f, Map map) { return lines(map(map, f)); } static String mapToLines(Object f, Iterable l) { return lines(map(f, l)); } // usually L static String fromLines(Iterable 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 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 String beautifyStructure(String s) { List tok = javaTok(s); structure_addTokenMarkers(tok); jreplace(tok, "lhm", ""); return join(tok); } static boolean nemptyString(String s) { return s != null && s.length() > 0; } static int listL(Collection l) { return l == null ? 0 : l.size(); } static String htmlQuote(String s) { return "\"" + htmlencode_forParams(s) + "\""; } // TODO: process CDATA, scripts 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 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 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 String addSuffix(String s, String suffix) { return s == null || s.endsWith(suffix) ? s : s + suffix; } static Map emptyMap() { return new HashMap(); } static List subList(List l, int startIndex) { return subList(l, startIndex, l(l)); } static List subList(List l, int startIndex, int endIndex) { if (l == null) return null; int n = l(l); startIndex = Math.max(0, startIndex); endIndex = Math.min(n, endIndex); if (startIndex >= endIndex) return ll(); if (startIndex == 0 && endIndex == n) return l; return l.subList(startIndex, endIndex); } static String snippetRawURL(String snippetID) { return snippetRawURL(snippetID, null); } static String snippetRawURL(String snippetID, String contentType) { return "https://www.botcompany.de:8443/serve/" + psI(snippetID) + htmlQuery("ct" , contentType); } static Object subBot_httpd() { Object httpd = getThreadLocal((ThreadLocal) getOpt(mainBot(), "MyHTTPD_current")); if (httpd == null) httpd = getThreadLocal((ThreadLocal) getOpt(mainBot(), "WebSocketHTTPD_current")); return httpd; } static Object mainBot() { return getMainBot(); } static boolean isImageServerSnippet(long id) { return id >= 1100000 && id < 1200000; } static String imageServerLink(String md5OrID) { if (possibleMD5(md5OrID)) return "https://botcompany.de/images/md5/" + md5OrID; return imageServerLink(parseSnippetID(md5OrID)); } static String imageServerLink(long id) { return "https://botcompany.de/images/" + id; } static Comparator caseInsensitiveComparator() { return betterCIComparator(); } static MechMode mechMode_value = new MechMode(); static MechMode mechMode() { return mechMode_value; } static String uniqueFileNameUsingMD5_80_v2(String fullName) { return uniqueFileNameUsingMD5_80_v2(fullName, md5(fullName)); } static String uniqueFileNameUsingMD5_80_v2(String fullName, String md5) { return takeFirst(80-33, fileNameEncode(fullName)) + " - " + md5; } static String upper(String s) { return s == null ? null : s.toUpperCase(); } static char upper(char c) { return Character.toUpperCase(c); } static String loadTextFileFromZipFile(File inZip, String fileName) { try { if (!fileExists(inZip)) return null; try { ZipFile zip = new ZipFile(inZip); try { return loadTextFileFromZipFile(zip, fileName); } finally { _close(zip); }} catch (Throwable e) { throw fail(f2s(inZip), e); } } catch (Exception __e) { throw rethrow(__e); } } static String loadTextFileFromZipFile(ZipFile zip, String fileName) { try { ZipEntry entry = zip.getEntry(fileName); if (entry == null) return null; InputStream fin = zip.getInputStream(entry); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); copyStream(fin, baos); return fromUTF8(baos.toByteArray()); } finally { _close(fin); }} catch (Exception __e) { throw rethrow(__e); } } static String loadTextFile(String fileName) { return loadTextFile(fileName, null); } static String loadTextFile(File f, String defaultContents) { try { checkFileNotTooBigToRead(f); if (f == null || !f.exists()) return defaultContents; FileInputStream fileInputStream = new FileInputStream(f); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); return loadTextFile(inputStreamReader); } catch (Exception __e) { throw rethrow(__e); } } public static String loadTextFile(File fileName) { return loadTextFile(fileName, null); } static String loadTextFile(String fileName, String defaultContents) { return fileName == null ? defaultContents : loadTextFile(newFile(fileName), defaultContents); } static String loadTextFile(Reader reader) throws IOException { StringBuilder builder = new StringBuilder(); try { char[] buffer = new char[1024]; int n; while (-1 != (n = reader.read(buffer))) builder.append(buffer, 0, n); } finally { reader.close(); } return str(builder); } static File remoteMechListMirrorFile(String listName) { return newFile(remoteMechListMirrorsDir(), uniqueFileNameUsingMD5_80_v2(upper(listName))); } static boolean serverMechList_raw_fresh_verbose; static String serverMechList_raw_fresh(String name) { return serverMechList_raw_fresh(name, false); } static String serverMechList_raw_fresh(String name, boolean opt) { Lock __1217 = downloadLock(); lock(__1217); try { /*if (serverMechList_raw_fresh_verbose) print("serverMechList_raw_fresh " + name);*/ String text = null; try { text = loadTextFile(remoteMechListMirrorFile(name)); } catch (Throwable __e) { _handleException(__e); } Object[] params = muricaCredentialsPlus("md5" , md5OrNull(text), "l" , l(text), "opt" , opt ? 1 : 0, "withStatus" , 1); String url = "http://butter.botcompany.de:8080/mech/raw/list-text/" + urlencode(name); String page = postPageSilently(url, params); Map map = jsonDecodeMap(page); boolean same = eq(map.get("Same"), true); boolean appended = eq(map.get("Appended"), true); saveTextFile(remoteMechListMirrorMetaFile(name), struct(getMultipleKeys(map, "Name", "Status"))); if (!same) { if (appended) text += (String) map.get("Text"); else text = (String) map.get("Text"); saveTextFile(remoteMechListMirrorFile(name), text); File nameFile = remoteMechListMirrorNameFile(name); if (!fileExists(nameFile)) { String actualName = or((String) map.get("Name"), name); saveTextFile(nameFile, actualName); } } if (serverMechList_raw_fresh_verbose) print("Mech list " + name + ": " + (appended ? "appended" : same ? "same" : "downloaded") + ": " + n2(countLines(text), "line")); if (!same) vmBus_send("remoteMechListMirrorChanged", name); return text; } finally { unlock(__1217); } } static String appendNewLineIfNempty(String s) { return empty(s) ? "" : s + "\n"; } static String localMechList_opt_raw_fresh(String name) { return unnull(loadTextFile(localMechListFile(name))); } static Object swing(Object f) { return swingAndWait(f); } static A swing(F0 f) { return (A) swingAndWait(f); } static Object vm_generalMap_get(Object key) { return vm_generalMap().get(key); } static Object vm_generalMap_put(Object key, Object value) { return mapPutOrRemove(vm_generalMap(), key, value); } static Set syncIdentityHashSet() { return (Set) synchronizedSet(identityHashSet()); } 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); } public static boolean isSnippetID(String s) { try { parseSnippetID(s); return true; } catch (RuntimeException e) { return false; } } static int parseInt(String s) { return empty(s) ? 0 : Integer.parseInt(s); } static int parseInt(char c) { return Integer.parseInt(str(c)); } static boolean even(int i) { return (i & 1) == 0; } static boolean even(long i) { return (i & 1) == 0; } static Map paramsToMap(Object... params) { int n = l(params); if (l(params) == 1 && params[0] instanceof Map) return (Map) params[0]; LinkedHashMap map = new LinkedHashMap(); for (int i = 0; i+1 < n; i += 2) mapPut(map, params[i], params[i+1]); return map; } 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 Class __javax; static Class getJavaX() { try { return __javax; } catch (Exception __e) { throw rethrow(__e); } } static List localSoftwareMadeWebs_programsToRead_list = ll("#1010745"); static List localSoftwareMadeWebs_programsToRead() { return localSoftwareMadeWebs_programsToRead_list; //ret websPostedIn(); } static boolean isAGIBlueDomain(String domain) { return domainIsUnder(domain, theAGIBlueDomain()); } static String hostNameFromURL(String url) { try { return new URL(url).getHost(); } catch (Exception __e) { throw rethrow(__e); } } // PersistableThrowable doesn't hold GC-disturbing class references in backtrace static volatile PersistableThrowable lastException_lastException; static PersistableThrowable lastException() { return lastException_lastException; } static void lastException(Throwable e) { lastException_lastException = persistableThrowable(e); } static List ai_fillTripleIndex_useThese; static Object ai_fillTripleIndex_useTripleIndex; static void ai_fillTripleIndex() { if (ai_fillTripleIndex_useTripleIndex != null) { print("Copying triple index."); ai_reuseTripleIndex(ai_fillTripleIndex_useTripleIndex); ai_fillTripleIndex_useTripleIndex = null; ai_fillTripleIndex_useThese = null; return; } print("Getting triples."); long _startTime_268 = sysNow(); List triples = ai_fillTripleIndex_useThese != null ? ai_fillTripleIndex_useThese : localSoftwareMadeTriples(); ai_fillTripleIndex_useThese = null; _startTime_268 = sysNow()-_startTime_268; saveTiming(_startTime_268); long time = lastTiming(); long us = ratio(time*1000, l(triples)); //print("Got triples in "T + time + " ms. Making triple index of " + nTriples(triples) + "."); print("Got " + nTriples(triples) + " in " + us + " "+ unicode_micro() + "s per triple. Making triple index."); long _startTime_269 = sysNow(); ai_replaceTripleIndexWithTriples(triples); //ai_tripleIndexToInvalidatedWebs(); ai_deleteInvalidTriples(); done2_always("Triple index made.", _startTime_269); } static Object callOptMC(String method, Object... args) { return callOpt(mc(), method, args); } static boolean unlockIfHolding(Lock lock) { if (lock == null || !hasLock(lock)) return false; unlock(lock); return true; } static Lock dbLock() { return db_mainConcepts().lock; } static A getAndClearThreadLocal(ThreadLocal tl) { A a = tl.get(); tl.set(null); return a; } static void assertFalse(Object o) { if (!(eq(o, false) /*|| isFalse(pcallF(o))*/)) throw fail(str(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 boolean _inCore() { return false; } 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 List hotwire_copyOver_after = synchroList(); static void hotwire_copyOver(Class c) { // TODO: make a mechanism for making such "inheritable" fields for (String field : ll("print_log", "print_silent", "androidContext", "_userHome")) setOptIfNotNull(c, field, getOpt(mc(), field)); setOptIfNotNull(c, "mainBot" , getMainBot()); setOpt(c, "creator_class" , new WeakReference(mc())); pcallFAll(hotwire_copyOver_after, c); } static void assertTrue(Object o) { if (!(eq(o, true) /*|| isTrue(pcallF(o))*/)) throw fail(str(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 List dependentClasses() { return cleanUpAndGetWeakReferencesList(hotwire_classes); } 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; if (o instanceof _SetField) { ((_SetField) o)._setField(field, value); return; } Class c = o.getClass(); HashMap map; if (getOpt_cache == null) map = getOpt_makeCache(c); // in class init else 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) { f.setAccessible(true); return f; } _c = _c.getSuperclass(); } while (_c != null); return null; } static Pair pair(A a, B b) { return new Pair(a, b); } static Pair pair(A a) { return new Pair(a, a); } static A assertNotNull(A a) { assertTrue(a != null); return a; } static A assertNotNull(String msg, A a) { assertTrue(msg, a != null); return a; } static Map synchroHashMap() { return Collections.synchronizedMap(new HashMap()); } static List getClassNames(Collection l) { List out = new ArrayList(); if (l != null) for (Object o : l) out.add(o == null ? null : getClassName(o)); return out; } static List> _threadInfo_makers = synchroList(); static Object _threadInfo() { if (empty(_threadInfo_makers)) return null; HashMap map = new HashMap(); pcallFAll(_threadInfo_makers, map); return map; } static volatile boolean licensed_yes = true; static boolean licensed() { if (!licensed_yes) return false; ping_okInCleanUp(); return true; } static void licensed_off() { licensed_yes = false; } static List> _threadInheritInfo_retrievers = synchroList(); static void _threadInheritInfo(Object info) { if (info == null) return; pcallFAll(_threadInheritInfo_retrievers, (Map) info); } static AutoCloseable tempActivity(Object r) { return null; } static long fixTimestamp(long timestamp) { return timestamp > now() ? 0 : timestamp; } static void _close(AutoCloseable c) { if (c != null) try { c.close(); } catch (Throwable e) { // Some classes stupidly throw an exception on double-closing if (c instanceof javax.imageio.stream.ImageOutputStream) return; else throw rethrow(e); } } static String defaultThreadName_name; static String defaultThreadName() { if (defaultThreadName_name == null) defaultThreadName_name = "A thread by " + programID(); return defaultThreadName_name; } static Runnable wrapAsActivity(Object r) { return toRunnable(r); } // runnable = Runnable or String (method name) static Thread newThread(Object runnable) { return new Thread(_topLevelErrorHandling(toRunnable(runnable))); } static Thread newThread(Object runnable, String name) { if (name == null) name = defaultThreadName(); return new Thread(_topLevelErrorHandling(toRunnable(runnable)), name); } static Thread newThread(String name, Object runnable) { return newThread(runnable, name); } 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 Map _registerThread_threads; static Object _onRegisterThread; // voidfunc(Thread) static Thread _registerThread(Thread t) { if (_registerThread_threads == null) _registerThread_threads = newWeakHashMap(); _registerThread_threads.put(t, true); vm_generalWeakSubMap("thread2mc").put(t, weakRef(mc())); callF(_onRegisterThread, t); return t; } static void _registerThread() { _registerThread(Thread.currentThread()); } static SnippetTitles getSnippetTitle_cached_cache = new SnippetTitles(); static String getSnippetTitle_cached(String id) { return getSnippetTitle_cached_cache.get(id); } static void getSnippetTitle_cached_put(String id, String title) { getSnippetTitle_cached_cache.put(id, title); } static boolean getSnippetTitle_cached_has(String id) { return getSnippetTitle_cached_cache.has(id); } static String getSnippetTitle_cached_probe(String id) { return getSnippetTitle_cached_cache.probe(id); } static int loadPage_defaultTimeout = 60000; 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_forcedTimeout_byThread = new ThreadLocal(); // ms static ThreadLocal>> loadPage_responseHeaders = new ThreadLocal(); static ThreadLocal> loadPage_extraHeaders = new ThreadLocal(); public static String loadPageSilently(String url) { try { return loadPageSilently(new URL(loadPage_preprocess(url))); } catch (Exception __e) { throw rethrow(__e); } } public static String loadPageSilently(URL url) { try { if (url.getProtocol().equals("https")) disableCertificateValidation(); if (!networkAllowanceTest(str(url))) throw fail("Not allowed: " + url); IOException e = null; for (int tries = 0; tries < loadPage_retries; tries++) try { URLConnection con = loadPage_openConnection(url); return loadPage(con, url); } catch (IOException _e) { e = _e; if (loadPageThroughProxy_enabled) { print("Trying proxy because of: " + e); try { return loadPageThroughProxy(str(url)); } catch (Throwable e2) { print(" " + exceptionToStringShort(e2)); } } else if (loadPage_debug) print(exceptionToStringShort(e)); if (tries < loadPage_retries-1) sleepSeconds(1); } throw e; } catch (Exception __e) { throw rethrow(__e); } } 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; } static String loadPage(String url) { try { url = loadPage_preprocess(url); if (!isTrue(loadPage_silent.get())) printWithTime("Loading: " + hideCredentials(url)); return loadPageSilently(new URL(url)); } catch (Exception __e) { throw rethrow(__e); } } static String loadPage(URL url) { return loadPage(url.toExternalForm()); } 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 { Map extraHeaders = getAndClearThreadLocal(loadPage_extraHeaders); if (addHeaders) try { if (!loadPage_anonymous) setHeaders(con); if (loadPage_allowGzip) con.setRequestProperty("Accept-Encoding", "gzip"); con.setRequestProperty("X-No-Cookies", "1"); for (String key : keys(extraHeaders)) con.setRequestProperty(key, extraHeaders.get(key)); } catch (Throwable e) {} // fails if within doPost vm_generalSubMap("URLConnection per thread").put(currentThread(), con); loadPage_responseHeaders.set(con.getHeaderFields()); InputStream in = null; try { in = urlConnection_getInputStream(con); //vm_generalSubMap("InputStream per thread").put(currentThread(), in); if (loadPage_debug) print("Put stream in map: " + currentThread()); String contentType = con.getContentType(); if (contentType == null) { //printStruct("Headers: ", con.getHeaderFields()); throw new IOException("Page could not be read: " + hideCredentials(url)); } //print("Content-Type: " + contentType); String charset = loadPage_charset == null ? null : loadPage_charset.get(); if (charset == null) charset = loadPage_guessCharset(contentType); if ("gzip".equals(con.getContentEncoding())) { if (loadPage_debug) print("loadPage: Using gzip."); in = newGZIPInputStream(in); } Reader r; try { r = new InputStreamReader(in, unquote(charset)); } catch (UnsupportedEncodingException e) { print(toHex(utf8(charset))); throw e; } StringBuilder buf = new StringBuilder(); int n = 0; while (true) { int ch = r.read(); if (ch < 0) break; buf.append((char) ch); ++n; if ((n % loadPage_verboseness) == 0) print(" " + n + " chars read"); } return buf.toString(); } finally { if (loadPage_debug) print("loadPage done"); //vm_generalSubMap("InputStream per thread").remove(currentThread()); vm_generalSubMap("URLConnection per thread").remove(currentThread()); if (in != null) in.close(); } } static String loadPage_guessCharset(String contentType) { Matcher m = regexpMatcher("text/[a-z]+;\\s*charset=([^\\s]+)\\s*", 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"); return or(match, "UTF-8"); } static URLConnection loadPage_openConnection(URL url) { URLConnection con = openConnection(url); int timeout = toInt(loadPage_forcedTimeout_byThread.get()); if (timeout == 0) timeout = loadPage_forcedTimeout; if (timeout != 0) setURLConnectionTimeouts(con, loadPage_forcedTimeout); else setURLConnectionDefaultTimeouts(con, loadPage_defaultTimeout); return con; } static URLConnection setURLConnectionTimeouts(URLConnection con, long timeout) { con.setConnectTimeout(toInt(timeout)); con.setReadTimeout(toInt(timeout)); if (con.getConnectTimeout() != timeout || con.getReadTimeout() != timeout) print("Warning: Timeouts not set by JDK."); return con; } 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) { printHidingCredentials(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) { final String text = hideCredentials(_text); if (empty(text)) return null; logQuotedWithDate(infoBoxesLogFile(), text); if (isHeadless()) return null; return (JWindow) swingAndWait(new F0() { Object get() { try { final JWindow window = showWindow(infoMessage_makePanel(text)); window.setSize(300, 150); moveToTopRightCorner(window); if (infoMessage_alwaysOnTop) window.setAlwaysOnTop(true); window.setVisible(true); if (seconds != 0) disposeWindowAfter(iround(seconds*1000), window); return window; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "final JWindow window = showWindow(infoMessage_makePanel(text));\r\n window.s..."; }}); } static JWindow infoMessage(Throwable e) { //showConsole(); printStackTrace(e); return infoMessage(exceptionToStringShort(e)); } static Set keys(Map map) { return map == null ? new HashSet() : map.keySet(); } static Set keys(Object map) { return keys((Map) map); } static Set keys(MultiSet ms) { return ms.keySet(); } static Set keys(MultiMap mm) { return mm.keySet(); } 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 preCleanUp(Object c) { if (c instanceof Collection) { for (Object o : ((Collection) c)) cleanUp(o); return; } callOpt(c, "licensed_off"); setOpt(c, "ping_anyActions" , true); // so ping notices setOpt(c, "cleaningUp_flag" , true); } static void innerCleanUp(Object c) { // call custom cleanMeUp() and cleanMeUp_*() functions if (!isFalse(pcallOpt(c, "cleanMeUp"))) for (String name : sorted(methodsStartingWith(c, "cleanMeUp_"))) try { callOpt(c, name); } catch (Throwable e) { print("Error cleaning up: " + programID(c)); _handleException(e); } } static void innerCleanUp() { innerCleanUp(mc()); } static Object pcallOpt(Object o, String method, Object... args) { try { return callOpt(o, method, args); } catch (Throwable __e) { _handleException(__e); } return null; } static List registeredThreads(Object o) { Map map = (Map) (getOpt(o, "_registerThread_threads")); if (map == null) return ll(); map.size(); // force clean-up synchronized(map) { return asList(keys(map)); } } static List registeredThreads() { _registerThread_threads.size(); // force clean-up return asList(keys(_registerThread_threads)); } static String n2(long l) { return formatWithThousands(l); } static String n2(Collection l) { return n2(l(l)); } static String n2(double l, String singular) { return n2(l, singular, singular + "s"); } static String n2(double l, String singular, String plural) { if (fraction(l) == 0) return n2((long) l, singular, plural); else return l + " " + plural; } static String n2(long l, String singular, String plural) { return n_fancy2(l, singular, plural); } static String n2(long l, String singular) { return n_fancy2(l, singular, singular + "s"); } static String n2(Collection l, String singular) { return n2(l(l), singular); } static String n2(Collection l, String singular, String plural) { return n_fancy2(l, singular, plural); } static String n2(Map m, String singular, String plural) { return n_fancy2(m, singular, plural); } static String n2(Map m, String singular) { return n2(l(m), singular); } static String n2(Object[] a, String singular) { return n2(l(a), singular); } static String n2(Object[] a, String singular, String plural) { return n_fancy2(a, singular, plural); } static String n2(MultiSet ms, String singular, String plural) { return n_fancy2(ms, singular, plural); } static String joinWithComma(Collection c) { return join(", ", c); } static String joinWithComma(String... c) { return join(", ", c); } static String joinWithComma(Pair p) { return p == null ? "" : joinWithComma(str(p.a), str(p.b)); } static List allToString(Iterable c) { List l = new ArrayList(); for (Object o : unnull(c)) l.add(str(o)); return l; } static List allToString(Object[] c) { List l = new ArrayList(); for (Object o : unnull(c)) l.add(str(o)); return l; } static void interruptThreads(Collection threads) { for (Thread t : unnull(threads)) t.interrupt(); } static void interruptThreads(Class mainClass) { interruptThreads(registeredThreads(mainClass)); } static void retireClassLoader(ClassLoader cl) { if (isJavaXClassLoader(cl)) setOptAll(cl, "retired" , true, "retiredMarker" , new DefunctClassLoader()); } static Object pcallFunction(Object f, Object... args) { try { return callFunction(f, args); } catch (Throwable __e) { _handleException(__e); } return null; } static final Map callOpt_cache = newDangerousWeakHashMap(); static Object callOpt_cached(Object o, String methodName, Object... args) { try { if (o == null) return null; if (o instanceof Class) { Class c = (Class) o; _MethodCache cache = callOpt_getCache(c); // TODO: (super-rare) case where method exists static and non-static // with different args Method me = cache.findMethod(methodName, args); if (me == null || (me.getModifiers() & Modifier.STATIC) == 0) return null; return invokeMethod(me, null, args); } else { Class c = o.getClass(); _MethodCache cache = callOpt_getCache(c); Method me = cache.findMethod(methodName, args); if (me == null) return null; return invokeMethod(me, o, args); } } catch (Exception __e) { throw rethrow(__e); } } static _MethodCache callOpt_getCache(Class c) { synchronized(callOpt_cache) { _MethodCache cache = callOpt_cache.get(c); if (cache == null) callOpt_cache.put(c, cache = new _MethodCache(c)); return cache; } } static boolean isStaticMethod(Method m) { return methodIsStatic(m); } static Object[] massageArgsForVarArgsCall(Method m, Object[] args) { Class[] types = m.getParameterTypes(); int n = types.length-1, nArgs = args.length; if (nArgs < n) return null; for (int i = 0; i < n; i++) if (!argumentCompatibleWithType(args[i], types[i])) return null; Class varArgType = types[n].getComponentType(); for (int i = n; i < nArgs; i++) if (!argumentCompatibleWithType(args[i], varArgType)) return null; Object[] newArgs = new Object[n+1]; arraycopy(args, 0, newArgs, 0, n); Object[] varArgs = arrayOfType(varArgType, nArgs-n); arraycopy(args, n, varArgs, 0, nArgs-n); newArgs[n] = varArgs; return newArgs; } 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 Boolean isHeadless_cache; static boolean isHeadless() { if (isHeadless_cache != null) return isHeadless_cache; if (isAndroid()) return isHeadless_cache = true; 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 void clear(Collection c) { if (c != null) c.clear(); } static void put(Map map, A a, B b) { if (map != null) map.put(a, b); } static void put(List l, int i, A a) { if (l != null && i >= 0 && i < l(l)) l.set(i, a); } static boolean isSubtypeOf(Class a, Class b) { return b.isAssignableFrom(a); // << always hated that method, let's replace it! } static Set reflection_classesNotToScan_value = litset( "jdk.internal.loader.URLClassPath" ); static Set reflection_classesNotToScan() { return reflection_classesNotToScan_value; } static List listMinusSet(Iterable l, Collection stuff) { if (l == null) return null; 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 List listMinusSet(Iterable l, Collection stuff, Collection stuff2) { return listMinusSet(listMinusSet(l, stuff), stuff2); } static List tlft(String s) { return toLinesFullTrim(s); } static List tlft(File f) { return toLinesFullTrim(f); } static TreeSet asCISet(Iterable c) { return toCaseInsensitiveSet(c); } static TreeSet asCISet(String... x) { return toCaseInsensitiveSet(x); } static void mech_notifyBusOfChange(String listName) { vmBus_send("mechChange", listName); } static A printIndent(A o) { print(indentx(str(o))); return o; } static A printIndent(String indent, A o) { print(indentx(indent, str(o))); return o; } static void printIndent(int indent, Object o) { print(indentx(indent, str(o))); } static ThreadLocal mechList_clearCache_inFunction = new ThreadLocal(); static void mechList_clearCache(String listName) { { AutoCloseable __38 = tempSetTL(mechList_clearCache_inFunction, true); try { print("Sending mech clear: " + listName); vmBus_send("mechList_clearCache", listName); } finally { _close(__38); }} mechList_clearCache_noSend(listName); } static void mechList_clearCache_noSend(String listName) { for (String field : mcFields()) if (startsWithEndsWith(field, "mech", "_cache")) { Object o = getOptMC(field); if (o instanceof Map) removeFromMapCI_sync((Map) o, listName); } /*if (mechList_goToCreator()) callCreatorOpt('mechList_clearCache, listName);*/ } static String postPageSilently(String url, Object... params) { return doPostSilently(litmap(params), url); } static Object[] muricaCredentials() { String pass = muricaPassword(); return nempty(pass) ? new Object[] {"_pass", pass } : new Object[0]; } static void appendToLocalMechListCopy(String listName, String text) { appendToTextFile(remoteMechListMirrorFile(listName), "\n" + text); } static boolean isDollarVar(String s) { return startsWith(s, '$') && isJavaIdentifierAfter(s, 1); } static AbstractThoughtSpace thoughtSpace() { return currentThoughtSpace(); } // set for current thread static A thoughtSpace(A ts) { currentThoughtSpace_value.set(ts); return ts; } static ThreadLocal ai_cacheChecker = new ThreadLocal(); static AI_CacheChecker ai_cacheChecker() { return ai_cacheChecker.get(); } static List ai_fewestIndexedWebs(String a, String b, String c) { return ai_withoutInvalidWebs(nodesToWebs(shortestList3( indexedNodes_withInvalids(a), indexedNodes_withInvalids(b), indexedNodes_withInvalids(c)))); } static boolean webs_search_noVar_bool(Web searchWeb, Collection webs) { return webs_search_noVar(searchWeb, webs) != null; } static String sfuLL(Object... x) { return sfu(ll(x)); } static String nWebs(long i) { return n_fancy2(i, "web", "webs"); } static String nWebs(Collection c) { return n_fancy2(c, "web", "webs"); } static Web simpleWeb() { Web web = new Web(); web.useCLParse = false; return web; } static Iterator iterator(Iterable c) { return c == null ? emptyIterator() : c.iterator(); } static List web_nodesAndRelations(Web web) { return web.nodes; } static void web_setLabels_forced(WebNode node, List labels) { web_deindex(node); node.labels = cloneList(labels); web_index(node); } static List map_lisp_intern(Collection l) { List out = new ArrayList(l(l)); if (l != null) for (Lisp x : l) out.add(lisp_intern(x)); return out; } static String intern(String s) { return fastIntern(s); } static List trimToSize(List l) { synchronized(l) { if (l instanceof ArrayList) ((ArrayList) l).trimToSize(); else if (l instanceof SynchronizedArrayList) ((SynchronizedArrayList) l).trimToSize(); } return l; } static Map mapPlus(Map m, Object... data) { m = cloneMap(m); litmap_impl(m, data); return m; } static Object[] assertEvenLength(Object[] a) { assertTrue(even(l(a))); return a; } static List web_nodes(Web web) { return web_nonRelationNodes(web); } static WebNode web_firstNode(Web web) { return web == null ? null : first(web.nodes); } static List web_texts(WebNode node) { return node == null ? new ArrayList() : node.texts(); } static List web_texts(Collection l) { List texts = new ArrayList(l(l)); if (l != null) for (WebNode n : l) texts.add(web_text(n)); return texts; } static WebNode web_secondNode(Web web) { return web == null ? null : second(web.nodes); } 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 String web_text(WebNode node) { return node == null ? null : node.text(); } static WebRelation web_getRelation(WebNode a, WebNode b) { return a.web.getRelation(a, b); } static void logQuoted(String logFile, String line) { logQuoted(getProgramFile(logFile), line); } static void logQuoted(File logFile, String line) { appendToFile(logFile, quote(line) + "\n"); } static Object nuObject(String className, Object... args) { try { return nuObject(classForName(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 { if (args.length == 0) return nuObjectWithoutArguments(c); // cached! 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 A setAll(A o, Map fields) { if (fields == null) return o; for (String field : keys(fields)) set(o, field, fields.get(field)); return o; } static A setAll(A 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); } return o; } static File computerIDFile() { return javaxDataDir("Basic Info/computer-id.txt"); } static File userDir() { return new File(userHome()); } static File userDir(String path) { return new File(userHome(), path); } static String makeRandomID(int length) { return makeRandomID(length, defaultRandomGenerator()); } static String makeRandomID(int length, Random 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); } /** 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); mkdirsForFile(file); String tempFileName = fileName + "_temp"; File tempFile = new File(tempFileName); if (contents != null) { if (tempFile.exists()) try { String saveName = tempFileName + ".saved." + now(); copyFile(tempFile, new File(saveName)); } catch (Throwable e) { printStackTrace(e); } FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath()); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8"); PrintWriter printWriter = new PrintWriter(outputStreamWriter); printWriter.print(contents); printWriter.close(); } if (file.exists() && !file.delete()) throw new IOException("Can't delete " + fileName); if (contents != null) if (!tempFile.renameTo(file)) throw new IOException("Can't rename " + tempFile + " to " + file); vmBus_send("wroteFile", 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); } } static HashMap 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 void createMarkerFile(String progID, String name) { File f = getProgramFile(progID, name); if (fileLength(f) == 0) saveTextFile(f, "1"); } static File getProgramDir() { return programDir(); } static File getProgramDir(String snippetID) { return programDir(snippetID); } static Method findMethod(Object o, String method, Object... args) { return findMethod_cached(o, method, args); } 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 ThreadLocal assertVerbose_value = new ThreadLocal(); static void assertVerbose(boolean b) { assertVerbose_value.set(b); } static boolean assertVerbose() { return isTrue(assertVerbose_value.get()); } static A assertEqualsVerbose(Object x, A y) { assertEqualsVerbose((String) null, x, y); return y; } static A assertEqualsVerbose(String msg, Object x, A y) { if (!eq(x, y)) { throw fail((msg != null ? msg + ": " : "") + /*sfu*/(y) + " != " + /*sfu*/(x)); } else print("OK: " + /*sfu*/(x)); return y; } static String javaTok_substringC(String s, int i, int j) { return s.substring(i, j); } 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 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 min(int[] c) { int x = Integer.MAX_VALUE; for (int d : c) if (d < x) x = d; return x; } 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 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 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(Object a, Object b) { if (a == null) return b == null ? 0 : -1; if (b == null) return 1; return ((Comparable) a).compareTo(b); } static boolean isDigit(char c) { return Character.isDigit(c); } static CloseableIterableIterator linesFromFile(File f) { try { if (!f.exists()) return emptyCloseableIterableIterator(); if (ewic(f.getName(), ".gz")) return linesFromReader(utf8bufferedReader(newGZIPInputStream(f))); return linesFromReader(utf8bufferedReader(f)); } catch (Exception __e) { throw rethrow(__e); } } // TODO: extended multi-line strings static int javaTok_n, javaTok_elements; static boolean javaTok_opt; static List javaTok(String s) { ++javaTok_n; ArrayList tok = new ArrayList(); int l = s == null ? 0 : s.length(); int i = 0, n = 0; while (i < l) { int j = i; char c, d; // 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; } tok.add(javaTok_substringN(s, i, j)); ++n; i = j; if (i >= l) break; c = s.charAt(i); d = i+1 >= l ? '\0' : s.charAt(i+1); // 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) { int c2 = s.charAt(j); if (c2 == opener || c2 == '\n' && opener == '\'') { // allow multi-line strings, but not for ' ++j; break; } else if (c2 == '\\' && j+1 < l) j += 2; else ++j; } } else if (Character.isJavaIdentifierStart(c)) do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || s.charAt(j) == '\'')); // 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(javaTok_substringC(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 javaTokWithExisting(join(tok), tok); } static String structure_addTokenMarkers(String s) { return join(structure_addTokenMarkers(javaTok(s))); } static List structure_addTokenMarkers(List tok) { // 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 tok; // 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 tok; } static String jreplace(String s, String in, String out) { return jreplace(s, in, out, null); } static String jreplace(String s, String in, String out, Object condition) { List tok = javaTok(s); return jreplace(tok, in, out, condition) ? join(tok) : s; } // leaves tok properly tokenized // returns true iff anything was replaced static boolean jreplace(List tok, String in, String out) { return jreplace(tok, in, out, false, true, null); } static boolean jreplace(List tok, String in, String out, Object condition) { return jreplace(tok, in, out, false, true, condition); } static boolean jreplace(List tok, String in, String out, boolean ignoreCase, boolean reTok, Object condition) { String[] toks = javaTokForJFind_array(in); int lTokin = toks.length*2+1; boolean anyChange = false; int i = -1; for (int n = 0; n < 10000; n++) { // TODO: don't need this check anymore i = findCodeTokens(tok, i+1, ignoreCase, toks, condition); if (i < 0) return anyChange; List subList = tok.subList(i-1, i+lTokin-1); // N to N String expansion = jreplaceExpandRefs(out, subList); int end = i+lTokin-2; clearAllTokens(tok, i, end); // C to C tok.set(i, expansion); if (reTok) // would this ever be false?? reTok(tok, i, end); i = end; anyChange = true; } throw fail("woot? 10000! " + quote(in) + " => " + quote(out)); } static boolean jreplace_debug; static String htmlencode_forParams(String s) { if (s == null) return ""; StringBuilder out = new StringBuilder(Math.max(16, s.length())); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); /*if (c >= 0x100) out.append("&#x").append(charToHex(c)).append(';'); else*/ if (c > 127 || c == '"' || c == '<' || c == '>') { out.append("&#"); out.append((int) c); out.append(';'); } else out.append(c); } return out.toString(); } static boolean isLetter(char c) { return Character.isLetter(c); } static String htmlQuery(Map params) { return params.isEmpty() ? "" : "?" + makePostData(params); } static String htmlQuery(Object... data) { return htmlQuery(litorderedmap(data)); } static boolean possibleMD5(String s) { return isMD5(s); } static betterCIComparator_C betterCIComparator_instance; static betterCIComparator_C betterCIComparator() { if (betterCIComparator_instance == null) betterCIComparator_instance = new betterCIComparator_C(); return betterCIComparator_instance; } static class betterCIComparator_C implements Comparator { public int compare(String s1, String s2) { if (s1 == null) return s2 == null ? 0 : -1; if (s2 == null) return 1; int n1 = s1.length(); int n2 = s2.length(); int min = Math.min(n1, n2); for (int i = 0; i < min; i++) { char c1 = s1.charAt(i); char c2 = s2.charAt(i); if (c1 != c2) { c1 = Character.toUpperCase(c1); c2 = Character.toUpperCase(c2); if (c1 != c2) { c1 = Character.toLowerCase(c1); c2 = Character.toLowerCase(c2); if (c1 != c2) { // No overflow because of numeric promotion return c1 - c2; } } } } return n1 - n2; } } static String md5(String text) { try { if (text == null) return "-"; return bytesToHex(md5_impl(toUtf8(text))); // 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 byte[] md5_impl(byte[] data) { try { return MessageDigest.getInstance("MD5").digest(data); } catch (Exception __e) { throw rethrow(__e); } } static String md5(File file) { return md5OfFile(file); } static List takeFirst(List l, int n) { return l(l) <= n ? l : newSubListOrSame(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 String takeFirst(String s, int n) { return substring(s, 0, n); } static List takeFirst(int n, Iterable i) { List l = new ArrayList(); Iterator it = i.iterator(); for (int _repeat_11 = 0; _repeat_11 < n; _repeat_11++) { if (it.hasNext()) l.add(it.next()); else break; } return l; } static String fileNameEncode_safeChars = " ()[]#,!"; static String fileNameEncode(String s) { StringBuilder buf = new StringBuilder(); int n = l(s); for (int i = 0; i < n; i++) { char c = s.charAt(i); if (contains(fileNameEncode_safeChars, c)) buf.append(c); else buf.append(urlencode(str(c))); } return str(buf); } static boolean fileExists(String path) { return path != null && new File(path).exists(); } static boolean fileExists(File f) { return f != null && f.exists(); } static String f2s(File f) { return f == null ? null : f.getAbsolutePath(); } static String f2s(java.nio.file.Path p) { return p == null ? null : f2s(p.toFile()); } 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 fromUTF8(byte[] bytes) { return fromUtf8(bytes); } static ThreadLocal> checkFileNotTooBigToRead_tl = new ThreadLocal(); static void checkFileNotTooBigToRead(File f) { callF(checkFileNotTooBigToRead_tl.get(), f); } 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 File remoteMechListMirrorsDir() { return javaxDataDir("Remote Mech Lists"); } static Lock downloadLock_lock = fairLock(); static Lock downloadLock() { return downloadLock_lock; } static Object[] muricaCredentialsPlus(Object... params) { return concatArrays(muricaCredentials(), params); } static String md5OrNull(String s) { return s == null ? null : md5(s); } static Map jsonDecodeMap(String s) { Object o = jsonDecode(s); if (o instanceof List && empty((List) o)) return new HashMap(); if (o instanceof Map) return (Map) o; else throw fail("Not a JSON map: " + s); } static File remoteMechListMirrorMetaFile(String listName) { return javaxDataDir("Remote Mech Lists/" + uniqueFileNameUsingMD5_80_v2(upper(listName)) + ".meta"); } static String struct(Object o) { return structure(o); } static String struct(Object o, structure_Data data) { return structure(o, data); } static Map getMultipleKeys(Map map, A... keys) { Map map2 = similarEmptyMap(map); if (map != null) for (A key : keys) map2.put(key, map.get(key)); return map2; } static File remoteMechListMirrorNameFile(String listName) { return javaxDataDir("Remote Mech Lists/" + uniqueFileNameUsingMD5_80_v2(upper(listName)) + ".name"); } static int countLines(String s) { return l(toLines(s)); // yeah could be optimized :-) } static void vmBus_send(String msg, Object... args) { Object arg = vmBus_wrapArgs(args); pcallFAll(vm_busListeners_live(), msg, arg); pcallFAll(vm_busListenersByMessage_live().get(msg), msg, arg); } static void vmBus_send(String msg) { vmBus_send(msg, (Object) null); } static File localMechListFile(String listName) { return newFile(localMechListsDir(), uniqueFileNameUsingMD5_80_v2(listName) + ".text"); } static void swingAndWait(Runnable r) { try { if (isAWTThread()) r.run(); else EventQueue.invokeAndWait(addThreadInfoToRunnable(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 Map vm_generalMap_map; static Map vm_generalMap() { if (vm_generalMap_map == null) vm_generalMap_map = (Map) get(javax(), "generalMap"); return vm_generalMap_map; } static B mapPutOrRemove(Map map, A key, B value) { if (map != null && key != null) if (value != null) return map.put(key, value); else return map.remove(key); return null; } static Set synchronizedSet() { return synchroHashSet(); } static Set synchronizedSet(Set set) { return Collections.synchronizedSet(set); } static Set identityHashSet() { return Collections.newSetFromMap(new IdentityHashMap()); } static String dropSuffix(String suffix, String s) { return s.endsWith(suffix) ? s.substring(0, l(s)-l(suffix)) : s; } static boolean domainIsUnder(String domain, String mainDomain) { return eqic(domain, mainDomain) || ewic(domain, "." + mainDomain); } static String theAGIBlueDomain() { return "agi.blue"; } static void ai_reuseTripleIndex(Object index) { copyAllFields(index, tripleIndex()); } static long sysNow() { ping(); return System.nanoTime()/1000000; } static List localSoftwareMadeTriples() { List triples = new ArrayList(); for (String progID : localSoftwareMadeWebs_programsToRead()) { try { triples.addAll(websMadeByProgramAsTriples(progID)); } catch (Throwable __e) { _handleException(__e); }} return triples; } 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 long lastTiming() { return getLastTiming(); } static long ratio(long x, long y) { return y == 0 ? 0 : x/y; } static String nTriples(long i) { return n_fancy2(i, "triple", "triples"); } static String nTriples(Collection c) { return n_fancy2(c, "triple", "triples"); } static String unicode_micro() { return "\u00B5"; } static void ai_replaceTripleIndexWithTriples(List l) { TripleIndex i = tripleIndex(); i.clear(); for (TripleWeb w : l) i.addTriple(w); i.trimToSize(); } static int ai_deleteInvalidTriples() { Matches m = new Matches(); int count = 0; for (TripleWeb w : ai_triples("$X", "is", "invalid")) if (swic_trim(w.a, "Web ", m) && isGlobalID(m.unq(0))) { String id = m.unq(0); if (eq(id, "rjncddmdgpisijjq")) print("rjncddmdgpisijjq!"); TripleWeb ww = tripleIndex().getTriple(asGlobalID(id)); if (ww != null) ++count; tripleIndex().removeTriple(ww); ai_invalidatedWebs_cache_put(asGlobalID(id), w.globalID()); } if (count > 0) print("Deleted " + n2(count, "invalid triple")); return count; } 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 boolean hasLock(Lock lock) { return ((ReentrantLock) lock).isHeldByCurrentThread(); } static volatile Concepts mainConcepts; // Where we create new concepts static Concepts db_mainConcepts() { if (mainConcepts == null) { mainConcepts = new Concepts(getDBProgramID()); mainConcepts.classFinder = _defaultClassFinder(); } return mainConcepts; } static void setOptIfNotNull(Object o, String field, Object value) { if (value != null) setOpt(o, field, value); } static List cleanUpAndGetWeakReferencesList(List> l) { if (l == null) return null; synchronized(l) { List out = new ArrayList(); for (int i = 0; i < l(l); i++) { A a = l.get(i).get(); if (a == null) l.remove(i--); else out.add(a); } return out; } } static void setOpt_raw(Object o, String field, Object value) { try { if (o == null) return; if (o instanceof Class) setOpt_raw((Class) o, field, value); else { Field f = setOpt_raw_findField(o.getClass(), field); if (f != null) { f.setAccessible(true); smartSet(f, o, value); } } } catch (Exception __e) { throw rethrow(__e); } } static void setOpt_raw(Class c, String field, Object value) { try { if (c == null) return; Field f = setOpt_raw_findStaticField(c, field); if (f != null) { f.setAccessible(true); smartSet(f, null, value); } } catch (Exception __e) { throw rethrow(__e); } } 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 Runnable _topLevelErrorHandling(final Runnable runnable) { final Object info = _threadInfo(); return new Runnable() { public void run() { try { try { _threadInheritInfo(info); runnable.run(); } catch (Throwable __e) { _handleException(__e); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pcall {\r\n _threadInheritInfo(info);\r\n runnable.run();\r\n }"; }}; } static Map vm_generalWeakSubMap(Object name) { synchronized(get(javax(), "generalMap")) { Map map = (Map) (vm_generalMap_get(name)); if (map == null) vm_generalMap_put(name, map = newWeakMap()); return map; } } static WeakReference weakRef(A a) { return newWeakReference(a); } static volatile boolean disableCertificateValidation_attempted; static void disableCertificateValidation() { try { if (disableCertificateValidation_attempted) return; disableCertificateValidation_attempted = true; try { // Create a trust manager that does not validate certificate chains TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } public void checkClientTrusted(X509Certificate[] certs, String authType) {} public void checkServerTrusted(X509Certificate[] certs, String authType) {} }}; // Ignore differences between given hostname and certificate hostname HostnameVerifier hv = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; // Install the all-trusting trust manager SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); HttpsURLConnection.setDefaultHostnameVerifier(hv); } catch (Throwable __e) { _handleException(__e); } } catch (Exception __e) { throw rethrow(__e); } } static boolean networkAllowanceTest(String url) { return isAllowed("networkAllowanceTest", url); } static final boolean loadPageThroughProxy_enabled = false; static String loadPageThroughProxy(String url) { return null; } static String exceptionToStringShort(Throwable e) { lastException(e); e = getInnerException(e); String msg = hideCredentials(unnull(e.getMessage())); if (msg.indexOf("Error") < 0 && msg.indexOf("Exception") < 0) return baseClassName(e) + prependIfNempty(": ", msg); else return msg; } static void sleepSeconds(double s) { if (s > 0) sleep(round(s*1000)); } static String tb_mainServer_default = "http://code.botcompany.de:8081"; 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 A printWithTime(A a) { print(hmsWithColons() + ": " + a); return a; } 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 Map vm_generalSubMap(Object name) { synchronized(get(javax(), "generalMap")) { Map map = (Map) (vm_generalMap_get(name)); if (map == null) vm_generalMap_put(name, map = synchroMap()); return map; } } static InputStream urlConnection_getInputStream(URLConnection con) throws IOException { UnknownHostException lastException = null; for (int _repeat_398 = 0; _repeat_398 < 2; _repeat_398++) { try { if (con instanceof HttpURLConnection) if (((HttpURLConnection) con).getResponseCode() == 500) throw new IOException(joinNemptiesWithColonSpace("Server code 500", tryToReadErrorStreamFromURLConnection(((HttpURLConnection) con)))); return con.getInputStream(); } catch (UnknownHostException e) { lastException = e; print("Retrying because of: " + e); continue; } } throw lastException; } static GZIPInputStream newGZIPInputStream(File f) { return gzInputStream(f); } static GZIPInputStream newGZIPInputStream(InputStream in) { return gzInputStream(in); } static String unquote(String s) { if (s == null) return null; if (startsWith(s, '[')) { int i = 1; while (i < s.length() && s.charAt(i) == '=') ++i; if (i < s.length() && s.charAt(i) == '[') { String m = s.substring(1, i); if (s.endsWith("]" + m + "]")) return s.substring(i+1, s.length()-i-1); } } if (s.length() > 1) { char c = s.charAt(0); if (c == '\"' || c == '\'') { int l = endsWith(s, c) ? s.length()-1 : s.length(); StringBuilder sb = new StringBuilder(l-1); for (int i = 1; i < l; i++) { char ch = s.charAt(i); if (ch == '\\') { char nextChar = (i == l - 1) ? '\\' : s.charAt(i + 1); // 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 '\\': 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; // 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 String toHex(byte[] bytes) { return bytesToHex(bytes); } static String toHex(byte[] bytes, int ofs, int len) { return bytesToHex(bytes, ofs, len); } static byte[] utf8(String s) { return toUtf8(s); } static Matcher regexpMatcher(String pat, String s) { return compileRegexp(pat).matcher(unnull(s)); } static URLConnection openConnection(String url) { try { return openConnection(new URL(url)); } catch (Exception __e) { throw rethrow(__e); } } static URLConnection openConnection(URL url) { try { ping(); callOpt(javax(), "recordOpenURLConnection", str(url)); return url.openConnection(); } catch (Exception __e) { throw rethrow(__e); } } static URLConnection setURLConnectionDefaultTimeouts(URLConnection con, long timeout) { if (con.getConnectTimeout() == 0) { con.setConnectTimeout(toInt(timeout)); if (con.getConnectTimeout() != timeout) print("Warning: URL connect timeout not set by JDK."); } if (con.getReadTimeout() == 0) { con.setReadTimeout(toInt(timeout)); if (con.getReadTimeout() != timeout) print("Warning: URL read timeout not set by JDK."); } return con; } static A printHidingCredentials(A o) { print(hideCredentials(str(o))); return o; } static void logQuotedWithDate(String s) { logQuotedWithTime(s); } static void logQuotedWithDate(String logFile, String s) { logQuotedWithTime(logFile, s); } static void logQuotedWithDate(File logFile, String s) { logQuotedWithTime(logFile, s); } static File infoBoxesLogFile() { return new File(javaxDataDir(), "Logs/infoBoxes.txt"); } static JWindow showWindow(Component c) { JWindow w = new JWindow(); w.add(wrap(c)); return w; } static JPanel infoMessage_makePanel(String text) { final JTextArea ta = wrappedTextArea(text); onClick(ta, new Runnable() { public void run() { try { disposeWindow(ta) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "disposeWindow(ta)"; }}); int size = 14; if (l(text) <= 50) size *= 2; else if (l(text) < 100) size = iround(size*1.5); ta.setFont(typeWriterFont(size)); JScrollPane sp = jscroll(ta); return withMargin(sp); } static int moveToTopRightCorner_inset = 20; static A moveToTopRightCorner(A a) { return moveToTopRightCorner(moveToTopRightCorner_inset, moveToTopRightCorner_inset, a); } static A moveToTopRightCorner(int insetX, int insetY, A a) { Window w = getWindow(a); if (w != null) w.setLocation(getScreenSize().width-w.getWidth()-insetX, insetY); return a; } 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 A disposeWindowAfter(A w, double seconds) { return disposeWindowAfter(toMS_int(seconds), w); } static A disposeWindowAfter(double seconds, A w) { return disposeWindowAfter(w, seconds); } static List concatLists(Collection... lists) { List l = new ArrayList(); if (lists != null) for (Collection list : lists) if (list != null) l.addAll(list); return l; } static List concatLists(Collection> lists) { List l = new ArrayList(); if (lists != null) for (Collection list : lists) if (list != null) l.addAll(list); return l; } 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 List methodsStartingWith(Object o, final String prefix) { return filter(allMethodNames(o), new F1() { Object get(String s) { try { return startsWith(s, prefix); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "startsWith(s, prefix)"; }}); } static String formatWithThousands(long l) { return formatWithThousandsSeparator(l); } static double fraction(double d) { return d % 1; } static String n_fancy2(long l, String singular, String plural) { return formatWithThousandsSeparator(l) + " " + trim(l == 1 ? singular : plural); } static String n_fancy2(Collection l, String singular, String plural) { return n_fancy2(l(l), singular, plural); } static String n_fancy2(Map m, String singular, String plural) { return n_fancy2(l(m), singular, plural); } static String n_fancy2(Object[] a, String singular, String plural) { return n_fancy2(l(a), singular, plural); } static String n_fancy2(MultiSet ms, String singular, String plural) { return n_fancy2(l(ms), singular, plural); } static boolean isJavaXClassLoader(ClassLoader cl) { return startsWithOneOf(className(cl), "main$JavaXClassLoader", "x30$JavaXClassLoader"); } static void setOptAll(Object o, Map fields) { if (fields == null) return; for (String field : keys(fields)) setOpt/*_flex*/(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 Object callFunction(Object f, Object... args) { return callF(f, args); } static boolean methodIsStatic(Method m) { return (m.getModifiers() & Modifier.STATIC) != 0; } static boolean argumentCompatibleWithType(Object arg, Class type) { return arg == null ? !type.isPrimitive() : isInstanceX(type, arg); } static void arraycopy(Object[] a, Object[] b) { if (a != null && b != null) arraycopy(a, 0, b, 0, min(a.length, b.length)); } static void arraycopy(Object src, int srcPos, Object dest, int destPos, int n) { if (n != 0) System.arraycopy(src, srcPos, dest, destPos, n); } static A[] arrayOfType(Class type, int n) { return makeArray(type, n); } static A[] arrayOfType(int n, Class type) { return arrayOfType(type, n); } static A printException(A e) { printStackTrace(e); return e; } static HashSet litset(A... items) { return lithashset(items); } 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(Iterable l) { if (l instanceof Set) return (Set) l; HashSet set = new HashSet(); for (A o : unnull(l)) if (o != null) set.add(o); return set; } static List toLinesFullTrim(String s) { List l = new ArrayList(); for (String line : toLines(s)) if (nempty(line = trim(line))) l.add(line); return l; } static List toLinesFullTrim(File f) { List l = new ArrayList(); for (String line : linesFromFile(f)) if (nempty(line = trim(line))) l.add(line); return l; } static TreeSet toCaseInsensitiveSet(Iterable c) { if (isCISet(c)) return (TreeSet) c; TreeSet set = caseInsensitiveSet(); addAll(set, c); return set; } static TreeSet toCaseInsensitiveSet(String... x) { TreeSet set = caseInsensitiveSet(); addAll(set, x); return set; } 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 AutoCloseable tempSetTL(ThreadLocal tl, A a) { return tempSetThreadLocal(tl, a); } static Set mcFields() { return staticFieldNames(mc()); } static boolean startsWithEndsWith(String s, String prefix, String suffix) { return startsWith(s, prefix) && endsWith(s, suffix); } static Object getOptMC(String field) { return getOpt(mc(), field); } static void removeFromMapCI_sync(Map map, String key) { if (map == null) return; synchronized(getMutex(map)) { for (String k : cloneList(keys(map))) if (eqic(key, k)) map.remove(k); } } static String doPostSilently(Map urlParameters, String url) { return doPostSilently(makePostData(urlParameters), url); } static String doPostSilently(String urlParameters, String url) { doPost_silently.set(true); return doPost(urlParameters, url); } static volatile boolean muricaPassword_pretendNotAuthed; static String muricaPassword() { if (muricaPassword_pretendNotAuthed) return null; return trim(loadTextFile(muricaPasswordFile())); } static void appendToTextFile(File file, String s) { appendToFile(file, s); } static void appendToTextFile(String file, String s) { appendToTextFile(programFile(file), s); } static boolean isJavaIdentifierAfter(String s, int i) { int n = l(s); if (i >= n || !Character.isJavaIdentifierStart(s.charAt(i))) return false; for (i++; i < n; i++) if (!Character.isJavaIdentifierPart(s.charAt(i))) return false; return true; } 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 List nodesToWebs(Collection nodes) { Set webs = litorderedset(); if (nodes != null) for (WebNode node : nodes) if (node != null) addIfNotNull(webs, node.web); return asList(webs); } static List shortestList3(List a, List b, List c) { int la = l(a), lb = l(b); if (la < lb) return l(c) < la ? c : a; else return l(c) < lb ? c : b; } static List indexedNodes_withInvalids(CharSequence _s) { String s = str(_s); List l = indexedNodes_rawLookup(s); for (VirtualNodeIndex i : concurrentlyIterateList(virtualNodeIndices())) { List x = i.get(s); if (nempty(x)) { //print("Word " + s + ". " + className(i) + " adding " + l(x) + " to " + l(l)); if (empty(l)) l = x; else { if (!(l instanceof CombinedList)) l = combinedList_ll(l); ((CombinedList) l).addList(x); } } } return assertNotNull(l); } static List indexedNodes_withInvalids(AI_SubSpace ss, CharSequence s) { return ss.indexedNodes_rawLookup(symbol(s)); } static Web webs_search_noVar(Web searchWeb, Collection webs) { for (Web web : webs) if (web_matchAllPerms(searchWeb, web) != null) return web; return null; } static void web_deindex(WebNode n) { MultiMap index = n.web.index; if (index != null) for (Lisp label : n.labels()) index.remove(label, n); } static void web_index(WebNode n) { MultiMap index = n.web.index; if (index != null) for (Lisp label : n.labels()) index.setPut(label, n); } static Lisp lisp_intern(Lisp l) { if (l == null) return null; return lisp(intern(l.head), map_lisp_intern(l.args)); } static Method fastIntern_method; static String fastIntern(String s) { try { if (s == null) return null; if (fastIntern_method == null) { fastIntern_method = findMethodNamed(javax(), "internPerProgram"); if (fastIntern_method == null) upgradeJavaXAndRestart(); } return (String) fastIntern_method.invoke(null, s); } catch (Exception __e) { throw rethrow(__e); } } static Map cloneMap(Map map) { if (map == null) return new HashMap(); // assume mutex is equal to collection synchronized(map) { return map instanceof TreeMap ? new TreeMap((TreeMap) map) // copies comparator : map instanceof LinkedHashMap ? new LinkedHashMap(map) : new HashMap(map); } } static List web_nonRelationNodes(Web web) { List l = new ArrayList(); for (WebNode n : web.nodes) if (!(n instanceof WebRelation)) l.add(n); return l; } static String ai_tripleToString(T3 t) { return t == null ? "" : curlyBraceIfContainsArrow(t.a) + " -> " + curlyBraceIfContainsArrow(t.b) + " -> " + curlyBraceIfContainsArrow(t.c); } static Lock appendToFile_lock = lock(); static boolean appendToFile_keepOpen; static HashMap appendToFile_writers = new HashMap(); static void appendToFile(String path, String s) { try { Lock __353 = appendToFile_lock; lock(__353); try { // Let's just generally synchronize this to be safe. mkdirsForFile(new File(path)); 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(__353); } } catch (Exception __e) { throw rethrow(__e); } } static void appendToFile(File path, String s) { if (path != null) appendToFile(path.getPath(), s); } static void cleanMeUp_appendToFile() { AutoCloseable __390 = tempCleaningUp(); try { Lock __354 = appendToFile_lock; lock(__354); try { closeAllWriters(values(appendToFile_writers)); appendToFile_writers.clear(); } finally { unlock(__354); } } finally { _close(__390); }} static Map classForName_cache = synchroHashMap(); static Class classForName(String name) { try { Class c = classForName_cache.get(name); if (c == null) classForName_cache.put(name, c = Class.forName(name)); return c; } catch (Exception __e) { throw rethrow(__e); } } static Map nuObjectWithoutArguments_cache = newDangerousWeakHashMap(); static Object nuObjectWithoutArguments(String className) { try { return nuObjectWithoutArguments(classForName(className)); } catch (Exception __e) { throw rethrow(__e); } } static A nuObjectWithoutArguments(Class c) { try { if (nuObjectWithoutArguments_cache == null) // in class init return (A) nuObjectWithoutArguments_findConstructor(c).newInstance(); Constructor m = nuObjectWithoutArguments_cache.get(c); if (m == null) nuObjectWithoutArguments_cache.put(c, m = nuObjectWithoutArguments_findConstructor(c)); return (A) m.newInstance(); } catch (Exception __e) { throw rethrow(__e); } } static Constructor nuObjectWithoutArguments_findConstructor(Class c) { for (Constructor m : c.getDeclaredConstructors()) if (empty(m.getParameterTypes())) { m.setAccessible(true); return m; } throw fail("No default constructor found in " + c.getName()); } static List getClasses(Object[] array) { List l = new ArrayList(); for (Object o : array) l.add(_getClass(o)); return l; } static void failIfOddCount(Object... list) { if (odd(l(list))) throw fail("Odd list size: " + list); } 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 File javaxDataDir(String... subs) { return newFile(javaxDataDir(), subs); } static String _userHome; static String userHome() { if (_userHome == null) return actualUserHome(); return _userHome; } static File userHome(String path) { return new File(userDir(), path); } static Random defaultRandomGenerator() { return ThreadLocalRandom.current(); } 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 " + n2(n, "critical actions") + ": " + join(", ", collect(beginCriticalAction_inFlight, "description"))); } catch (Throwable __e) { _handleException(__e); } } sleepInCleanUp(10); } } public static File mkdirsForFile(File file) { File dir = file.getParentFile(); if (dir != null) { // is null if file is in current dir dir.mkdirs(); if (!dir.isDirectory()) if (dir.isFile()) throw fail("Please delete the file " + f2s(dir) + " - it is supposed to be a directory!"); else throw fail("Unknown IO exception during mkdirs of " + f2s(file)); } return file; } public static String mkdirsForFile(String path) { mkdirsForFile(new File(path)); return path; } public static void copyFile(File src, File dest) { try { FileInputStream inputStream = new FileInputStream(src.getPath()); FileOutputStream outputStream = newFileOutputStream(dest.getPath()); try { copyStream(inputStream, outputStream); inputStream.close(); } finally { outputStream.close(); } } catch (Exception __e) { throw rethrow(__e); } } 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(File path, boolean append) throws IOException { return newFileOutputStream(path.getPath(), append); } static FileOutputStream newFileOutputStream(String path, boolean append) throws IOException { mkdirsForFile(path); FileOutputStream f = new FileOutputStream(path, append); _registerIO(f, path, true); return f; } static long fileLength(String path) { return getFileSize(path); } static long fileLength(File f) { return getFileSize(f); } 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(), formatSnippetIDOpt(snippetID)); if (me) { String c = caseID(); if (nempty(c)) dir = newFile(dir, c); } return dir; } static File programDir(String snippetID, String subPath) { return new File(programDir(snippetID), subPath); } 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 CloseableIterableIterator emptyCloseableIterableIterator_instance = new CloseableIterableIterator() { public Object next() { throw fail(); } public boolean hasNext() { return false; } }; static CloseableIterableIterator emptyCloseableIterableIterator() { return emptyCloseableIterableIterator_instance; } static boolean ewic(String a, String b) { return endsWithIgnoreCase(a, b); } static boolean ewic(String a, String b, Matches m) { return endsWithIgnoreCase(a, b, m); } static CloseableIterableIterator linesFromReader(Reader r) { final BufferedReader br = bufferedReader(r); return iteratorFromFunction_f0_autoCloseable(new F0() { String get() { try { return readLineFromReaderWithClose(br); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret readLineFromReaderWithClose(br);"; }}, _wrapIOCloseable(r)); } static BufferedReader utf8bufferedReader(InputStream in) { try { return bufferedReader(_registerIOWrap(new InputStreamReader(in, "UTF-8"), in)); } catch (Exception __e) { throw rethrow(__e); } } static BufferedReader utf8bufferedReader(File f) { try { return utf8bufferedReader(newFileInputStream(f)); } catch (Exception __e) { throw rethrow(__e); } } static String javaTok_substringN(String s, int i, int j) { if (i == j) return ""; if (j == i+1 && s.charAt(i) == ' ') return " "; return s.substring(i, j); } static List javaTokWithExisting(String s, List existing) { ++javaTok_n; int nExisting = javaTok_opt && existing != null ? existing.size() : 0; ArrayList tok = existing != null ? new ArrayList(nExisting) : new ArrayList(); int l = s.length(); int i = 0, n = 0; while (i < l) { int j = i; char c, d; // 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 && javaTokWithExisting_isCopyable(existing.get(n), s, i, j)) tok.add(existing.get(n)); else tok.add(javaTok_substringN(s, i, j)); ++n; i = j; if (i >= l) break; c = s.charAt(i); d = i+1 >= l ? '\0' : s.charAt(i+1); // 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 && javaTokWithExisting_isCopyable(existing.get(n), s, i, j)) tok.add(existing.get(n)); else tok.add(javaTok_substringC(s, i, j)); ++n; i = j; } if ((tok.size() % 2) == 0) tok.add(""); javaTok_elements += tok.size(); return tok; } static boolean javaTokWithExisting_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 boolean isInteger(String s) { 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 Map javaTokForJFind_array_cache = synchronizedMRUCache(1000); static String[] javaTokForJFind_array(String s) { String[] tok = javaTokForJFind_array_cache.get(s); if (tok == null) javaTokForJFind_array_cache.put(s, tok = codeTokensAsStringArray(jfind_preprocess(javaTok(s)))); return tok; } 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 HashSet findCodeTokens_specials = lithashset("*", "", "", "", "\\*"); 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++; } int end = tok.size()-tokens.length*2+2, nTokens = tokens.length; int i = startIdx | 1; // bail out early if first token not found (works great with IndexedList) String firstToken = tokens[0]; if (!ignoreCase && !findCodeTokens_specials.contains(firstToken)) { // quickly scan for first token while (i < end && !firstToken.equals(tok.get(i))) i += 2; } outer: for (; i < end; i += 2) { for (int j = 0; j < nTokens; j++) { String p = tokens[j], t = tok.get(i+j*2); boolean match; if (eq(p, "*")) match = true; else if (eq(p, "")) match = isQuoted(t); else if (eq(p, "")) match = isIdentifier(t); else if (eq(p, "")) match = isInteger(t); else if (eq(p, "\\*")) match = eq("*", t); else match = ignoreCase ? eqic(p, t) : eq(p, t); if (!match) continue outer; } if (condition == null || checkTokCondition(condition, tok, i-1)) // pass N index return i; } return -1; } // "$1" is first code token, "$2" second code token etc. static String jreplaceExpandRefs(String s, List tokref) { List tok = javaTok(s); for (int i = 1; i < l(tok); i += 2) { if (tok.get(i).startsWith("$") && isInteger(tok.get(i).substring(1))) { String x = tokref.get(-1+parseInt(tok.get(i).substring(1))*2); tok.set(i, x); } } return join(tok); } 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 List reTok(List tok) { replaceCollection(tok, javaTok(tok)); return tok; } static List reTok(List tok, int i) { return reTok(tok, i, i+1); } static List reTok(List tok, int i, int j) { // extend i to an "N" token // and j to "C" (so j-1 is an "N" token) i = i & ~1; j = j | 1; List t = javaTok(join(subList(tok, i, j))); replaceListPart(tok, i, j, t); // fallback to safety // reTok(tok); return tok; } static String makePostData(Map map) { List l = new ArrayList(); for (Map.Entry e : map.entrySet()) { String key = (String) (e.getKey()); Object val = e.getValue(); if (val != null) { String value = str(val); //structureOrText(val); l.add(urlencode(key) + "=" + urlencode(/*escapeMultichars*/(value))); } } return join("&", l); } static String makePostData(Object... params) { return makePostData(litorderedmap(params)); } static byte[] toUtf8(String s) { try { return s.getBytes("UTF-8"); } catch (Exception __e) { throw rethrow(__e); } } static boolean md5OfFile_verbose; static String md5OfFile(String path) { return md5OfFile(newFile(path)); } static String md5OfFile(File f) { try { if (!f.exists()) return "-"; if (md5OfFile_verbose) print("Getting MD5 of " + f); MessageDigest md5 = MessageDigest.getInstance("MD5"); FileInputStream in = new FileInputStream(f); try { byte buf[] = new byte[65536]; int l; while (true) { l = in.read(buf); if (l <= 0) break; md5.update(buf, 0, l); } return bytesToHex(md5.digest()); } finally { _close(in); }} catch (Exception __e) { throw rethrow(__e); } } static List newSubListOrSame(List l, int startIndex) { return newSubListOrSame(l, startIndex, l(l)); } static List newSubListOrSame(List l, int startIndex, int endIndex) { if (l == null) return null; int n = l(l); startIndex = max(0, startIndex); endIndex = min(n, endIndex); if (startIndex >= endIndex) return ll(); if (startIndex == 0 && endIndex == n) return l; return cloneList(l.subList(startIndex, endIndex)); } static String fromUtf8(byte[] bytes) { try { return bytes == null ? null : new String(bytes, "UTF-8"); } catch (Exception __e) { throw rethrow(__e); } } static boolean jsonDecode_useOrderedMaps = true; static Object jsonDecode(final String text) { final List tok = jsonTok(text); if (l(tok) == 1) return null; class Y { int i = 1; Object parse() { String t = tok.get(i); if (t.startsWith("\"")) { String s = unquote(tok.get(i)); i += 2; return s; } if (t.equals("{")) return parseMap(); if (t.equals("[")) return this.parseList(); // avoid loading standard function "parseList" if (t.equals("null")) { i += 2; return null; } if (t.equals("false")) { i += 2; return false; } if (t.equals("true")) { i += 2; return true; } boolean minus = false; if (t.equals("-")) { minus = true; i += 2; t = get(tok, i); } if (isInteger(t)) { i += 2; if (eq(get(tok, i), ".")) { String x = t + "." + get(tok, i+2); i += 4; double d = parseDouble(x); if (minus) d = -d; return d; } else { long l = parseLong(t); if (minus) l = -l; return l != (int) l ? new Long(l) : new Integer((int) l); } } throw new RuntimeException("Unknown token " + (i+1) + ": " + t + ": " + text); } Object parseList() { consume("["); List list = new ArrayList(); while (!tok.get(i).equals("]")) { list.add(parse()); if (tok.get(i).equals(",")) i += 2; } consume("]"); return list; } Object parseMap() { consume("{"); Map map = jsonDecode_useOrderedMaps ? new LinkedHashMap() : new TreeMap(); while (!tok.get(i).equals("}")) { String key = unquote(tok.get(i)); i += 2; consume(":"); Object value = parse(); map.put(key, value); if (tok.get(i).equals(",")) i += 2; } consume("}"); return map; } void consume(String s) { if (!tok.get(i).equals(s)) { String prevToken = i-2 >= 0 ? tok.get(i-2) : ""; String nextTokens = join(tok.subList(i, Math.min(i+4, tok.size()))); throw fail(quote(s) + " expected: " + prevToken + " " + nextTokens + " (" + i + "/" + tok.size() + ")"); } i += 2; } } return new Y().parse(); } static Map similarEmptyMap(Map m) { if (m instanceof TreeMap) return new TreeMap(((TreeMap) m).comparator()); if (m instanceof LinkedHashMap) return new LinkedHashMap(); // default to a hash map return new HashMap(); } static Object vmBus_wrapArgs(Object... args) { return empty(args) ? null : l(args) == 1 ? args[0] : args; } static Map vm_busListenersByMessage_live_cache; static Map vm_busListenersByMessage_live() { if (vm_busListenersByMessage_live_cache == null) vm_busListenersByMessage_live_cache = vm_busListenersByMessage_live_load(); return vm_busListenersByMessage_live_cache; } static Map vm_busListenersByMessage_live_load() { return vm_generalHashMap("busListenersByMessage"); } static File localMechListsDir() { return javaxDataDir("Mech Lists"); } static Runnable addThreadInfoToRunnable(final Object r) { final Object info = _threadInfo(); return info == null ? asRunnable(r) : new Runnable() { public void run() { try { _inheritThreadInfo(info); callF(r); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "_inheritThreadInfo(info); callF(r);"; }}; } static Set synchroHashSet() { return Collections.synchronizedSet(new HashSet()); } // 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()); // old method //Collection webs = allWebsFromCachedNodeIndex_uncached(); //print("Have " + nWebs(webs)); //for (Web web : webs) // ai_invalidatedWebs_onNewWeb(web); // new method Matches m = new Matches(); for (WebNode n : indexedNodes_withInvalids("invalid")) for (WebRelation r : web_backwardRelations(n)) if (web_hasLabelIC(r, "is") && web_match("Web *", r.a, m) && isGlobalID(m.unq(0))) ai_invalidatedWebs_cache.get().put(asGlobalID(m.unq(0)), r.web.globalIDObj()); print("ai_invalidatedWebs calc done with " + n2(ai_invalidatedWebs_cache.get().keysSize(), "invalidated web", "invalidated webs")); return ai_invalidatedWebs_cache.get(); } static void ai_invalidatedWebs_onNewWeb(Web web) { if (web.unverified) return; Lock __366 = ai_invalidatedWebs_cache.lock; lock(__366); try { Matches m = new Matches(); for (WebNode n : web_search_dollarX(webFromTriple("$X", "is", "invalid"), web)) if (web_match("Web *", n, m) && isGlobalID(m.unq(0))) ai_invalidatedWebs_cache.get().put(asGlobalID(m.unq(0)), web.globalIDObj()); } finally { unlock(__366); } } static void ai_invalidatedWebs_onRemovedWeb(Web web) { if (web.unverified) return; Lock __367 = ai_invalidatedWebs_cache.lock; lock(__367); try { Matches m = new Matches(); for (WebNode n : web_search_dollarX(webFromTriple("$X", "is", "invalid"), web)) if (web_match("Web *", n, m) && isGlobalID(m.unq(0))) ai_invalidatedWebs_cache.get().remove(asGlobalID(m.unq(0)), web.globalIDObj()); } finally { unlock(__367); } } static void ai_invalidatedWebs_cache_put(GlobalID a, GlobalID b) { ai_invalidatedWebs_cache.get().put(a, b); } static void copyAllFields(Object x, Object y) { copyFields(x, y); } static TripleIndex tripleIndex_instance; static TripleIndex tripleIndex() { if (tripleIndex_instance == null) { tripleIndex_instance = addVirtualNodeIndex(new TripleIndex()); tripleIndex_instance.activate(); } return tripleIndex_instance; } static List websMadeByProgramAsTriples(String progID) { return websMadeByProgramAsTriples(progID, null); } static List websMadeByProgramAsTriples(String progID, Map furtherInfoMap) { List triples = ai_triplesMadeByProgramAsTriples(progID); List webs = new ArrayList(); ai_loadWebsFromFile(programFile(progID, "webs-made.txt"), progID, webs, furtherInfoMap); triples.addAll(websToTripleWebs(webs)); return triples; } 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 List ai_triples(String a, String b, String c) { return ai_filterTriples(a, b, c, ai_thoughtSpace_queryTriples(a, b, c, thoughtSpace())); } static List ai_triples(String a, String b, String c, AbstractThoughtSpace ts) { return ai_filterTriples(a, b, c, ai_thoughtSpace_queryTriples(a, b, c, ts)); } static boolean isGlobalID(String s) { return possibleGlobalID(s); } static GlobalID asGlobalID(String id) { return id == null ? null : new GlobalID(id); } static String getDBProgramID_id; static String getDBProgramID() { return nempty(getDBProgramID_id) ? getDBProgramID_id : programIDWithCase(); } static Object _defaultClassFinder_value = defaultDefaultClassFinder(); static Object _defaultClassFinder() { return _defaultClassFinder_value; } static Map newWeakMap() { return newWeakHashMap(); } static WeakReference newWeakReference(A a) { return a == null ? null : new WeakReference(a); } static volatile Object isAllowed_function; // func(S, O[]) -> bool static volatile boolean isAllowed_all = true; static boolean isAllowed(String askingMethod, Object... args) { // check on VM level Object f = vm_generalMap_get("isAllowed_function"); if (f != null && !isTrue(callF(f, askingMethod, args))) return false; // check locally return isAllowed_all || isTrue(callF(isAllowed_function, askingMethod, args)); } static String baseClassName(String className) { return substring(className, className.lastIndexOf('.')+1); } static String baseClassName(Object o) { return baseClassName(getClassName(o)); } static String prependIfNempty(String prefix, String s) { return empty(s) ? s : prefix + s; } 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 long round(double d) { return Math.round(d); } static String hmsWithColons() { return hmsWithColons(now()); } static String hmsWithColons(long time) { return new SimpleDateFormat("HH:mm:ss").format(time); } static String getComputerID_quick() { return computerID(); } static String joinNemptiesWithColonSpace(String... strings) { return joinNempties(": ", strings); } static String joinNemptiesWithColonSpace(Collection strings) { return joinNempties(": ", strings); } static String tryToReadErrorStreamFromURLConnection(URLConnection conn) { try { if (conn instanceof HttpURLConnection) return stream2string(((HttpURLConnection) conn).getErrorStream()); // TODO: ensure some max length return null; } catch (Throwable __e) { return null; } } static int gzInputStream_defaultBufferSize = 65536; static GZIPInputStream gzInputStream(File f) { try { return gzInputStream(new FileInputStream(f)); } catch (Exception __e) { throw rethrow(__e); } } static GZIPInputStream gzInputStream(File f, int bufferSize) { try { return gzInputStream(new FileInputStream(f), bufferSize); } catch (Exception __e) { throw rethrow(__e); } } static GZIPInputStream gzInputStream(InputStream in) { return gzInputStream(in, gzInputStream_defaultBufferSize); } static GZIPInputStream gzInputStream(InputStream in, int bufferSize) { try { return _registerIOWrap(new GZIPInputStream(in, gzInputStream_defaultBufferSize), in); } catch (Exception __e) { throw rethrow(__e); } } 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 boolean endsWith(String a, String b, Matches m) { if (!endsWith(a, b)) return false; m.m = new String[] {dropLast(l(b), a)}; return true; } static Map compileRegexp_cache = syncMRUCache(10); static java.util.regex.Pattern compileRegexp(String pat) { java.util.regex.Pattern p = compileRegexp_cache.get(pat); if (p == null) { compileRegexp_cache.put(pat, p = java.util.regex.Pattern.compile(pat)); } return p; } static void logQuotedWithTime(String s) { logQuotedWithTime(standardLogFile(), s); } static void logQuotedWithTime(File logFile, String s) { logQuoted(logFile, logQuotedWithTime_format(s)); } static void logQuotedWithTime(String logFile, String s) { logQuoted(logFile, logQuotedWithTime_format(s)); } static String logQuotedWithTime_format(String s) { return /*formatGMTWithDate_24*/(now()) + " " + s; } // c = Component or something implementing swing() static JComponent wrap(Object swingable) { return _recordNewSwingComponent(wrap_2(swingable)); } static JComponent wrap_2(Object swingable) { if (swingable == null) return null; JComponent c; if (swingable instanceof Component) c = componentToJComponent(((Component) swingable)); else c = componentToJComponent((Component) callOpt(swingable, "swing")); if (c instanceof JTable || c instanceof JList || c instanceof JTextArea || c instanceof JEditorPane || c instanceof JTextPane || c instanceof JTree) return jscroll(c); return c == null ? jlabel(str(swingable)) : c; } static JTextArea wrappedTextArea(final JTextArea ta) { enableWordWrapForTextArea(ta); return ta; } static JTextArea wrappedTextArea() { return wrappedTextArea(jtextarea()); } static JTextArea wrappedTextArea(String text) { JTextArea ta = wrappedTextArea(); setText(ta, text); return ta; } static A onClick(final A c, final Object runnable) { if (c != null) { swing(new Runnable() { public void run() { try { c.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { callF(runnable, e); } }); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "c.addMouseListener(new MouseAdapter {\r\n public void mouseClicked(MouseEv..."; }}); } return c; } // re-interpreted for buttons static void onClick(JButton btn, final Object runnable) { onEnter(btn, runnable); } 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 myFrames_list.remove(window); 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 void disposeWindow() { disposeWindow(heldInstance(Component.class)); } static Font typeWriterFont() { return typeWriterFont(iround(14*getSwingFontScale())); } static Font typeWriterFont(int size) { return new Font("Courier", Font.PLAIN, size); } 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 "ret new JScrollPane(c);"; }}); } static int withMargin_defaultWidth = 6; static JPanel withMargin(Component c) { return withMargin(withMargin_defaultWidth, c); } static JPanel withMargin(int w, Component c) { return withMargin(w, w, c); } static JPanel withMargin(int w, int h, Component c) { return withMargin(w, h, w, h, c); } static JPanel withMargin(final int top, final int left, final int bottom, final int right, final Component c) { return swing(new F0() { JPanel get() { try { JPanel p = new JPanel(new BorderLayout()); p.setBorder(BorderFactory.createEmptyBorder(top, left, bottom, right)); p.add(c); return p; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JPanel p = new JPanel(new BorderLayout);\r\n p.setBorder(BorderFactory.creat..."; }}); } 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 Dimension getScreenSize() { return Toolkit.getDefaultToolkit().getScreenSize(); } static void swingLater(long delay, final Object r) { javax.swing.Timer timer = new javax.swing.Timer(toInt(delay), actionListener(wrapAsActivity(r))); timer.setRepeats(false); timer.start(); } static void swingLater(Object r) { SwingUtilities.invokeLater(toRunnable(r)); } static int toMS_int(double seconds) { return toInt_checked((long) (seconds*1000)); } 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 Comparator makeComparator(final Object f) { if (f instanceof Comparator) return (Comparator) f; return new Comparator() { public int compare(Object a, Object b) { return (Integer) callF(f, a, b); } }; } static List filter(Iterable c, Object pred) { if (pred instanceof F1) return filter(c, (F1) pred); List x = new ArrayList(); if (c != null) for (Object o : c) if (isTrue(callF(pred, o))) x.add(o); return x; } static List filter(Object pred, Iterable c) { return filter(c, pred); } static List filter(Iterable c, F1 pred) { List x = new ArrayList(); if (c != null) for (B o : c) if (pred.get(o).booleanValue()) x.add(o); return x; } static List filter(F1 pred, Iterable c) { return filter(c, pred); } //ifclass IF1 static List filter(Iterable c, IF1 pred) { List x = new ArrayList(); if (c != null) for (B o : c) if (pred.get(o).booleanValue()) x.add(o); return x; } static List filter(IF1 pred, Iterable c) { return filter(c, pred); } //endif 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 String formatWithThousandsSeparator(long l) { return NumberFormat.getInstance(new Locale("en_US")).format(l); } static void warnIfOddCount(Object... list) { if (odd(l(list))) printStackTrace("Odd list size: " + list); } static A[] makeArray(Class type, int n) { return (A[]) Array.newInstance(type, n); } static HashSet lithashset(A... items) { HashSet set = new HashSet(); for (A a : items) set.add(a); return set; } static boolean isCISet(Iterable l) { return l instanceof TreeSet && ((TreeSet) l).comparator() == caseInsensitiveComparator(); } static TreeSet caseInsensitiveSet() { return caseInsensitiveSet_treeSet(); } static TreeSet caseInsensitiveSet(Collection c) { return caseInsensitiveSet_treeSet(c); } static void addAll(Collection c, Iterable b) { if (c != null && b != null) for (A a : b) c.add(a); } static boolean addAll(Collection c, Collection b) { return c != null && b != null && c.addAll(b); } static boolean addAll(Collection c, B... b) { return c != null && c.addAll(Arrays.asList(b)); } static Map addAll(Map a, Map b) { if (a != null) a.putAll(b); return a; } 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) { n = Math.max(n, 0); 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); } 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(); if (lines != null) for (String s : lines) l.add(indent + s); return l; } static AutoCloseable tempSetThreadLocal(final ThreadLocal tl, A a) { if (tl == null) return null; final A prev = setThreadLocal(tl, a); return new AutoCloseable() { public String toString() { return "tl.set(prev);"; } public void close() throws Exception { tl.set(prev); }}; } static Set staticFieldNames(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 Object getMutex(Object o) { return collectionMutex(o); } static ThreadLocal doPost_silently = new ThreadLocal(); static ThreadLocal doPost_timeout = new ThreadLocal(); static ThreadLocal> doPost_extraHeaders = new ThreadLocal(); static String doPost(Map urlParameters, String url) { return doPost(makePostData(urlParameters), url); } static String doPost(String urlParameters, String url) { try { URL _url = new URL(url); ping(); return doPost(urlParameters, _url.openConnection(), _url); } catch (Exception __e) { throw rethrow(__e); } } static String doPost(String urlParameters, URLConnection conn, URL url) { try { boolean silently = isTrue(optParam(doPost_silently)); Long timeout = optParam(doPost_timeout); Map extraHeaders = optPar(doPost_extraHeaders); setHeaders(conn); for (String key : keys(extraHeaders)) conn.setRequestProperty(key, extraHeaders.get(key)); int l = lUtf8(urlParameters); if (!silently) print("Sending POST request: " + hideCredentials(url) + " (" + l + " bytes)"); // connect and do POST if (timeout != null) setURLConnectionTimeouts(conn, timeout); ((HttpURLConnection) conn).setRequestMethod("POST"); conn.setDoOutput(true); conn.setRequestProperty("Content-Length", str(l)); OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); writer.write(urlParameters); writer.flush(); String contents = loadPage_utf8(conn, url, false); writer.close(); return contents; } catch (Exception __e) { throw rethrow(__e); } } static File muricaPasswordFile() { return new File(javaxSecretDir(), "murica/muricaPasswordFile"); } static File programFile(String name) { return prepareProgramFile(name); } static File programFile(String progID, String name) { return prepareProgramFile(progID, name); } static boolean ai_isInvalidWeb(Web web) { return ai_invalidatedWebs().contains(web.globalID); } static boolean ai_isInvalidWeb(String id) { return ai_invalidatedWebs().contains(id); } static LinkedHashSet litorderedset(A... items) { LinkedHashSet set = new LinkedHashSet(); for (A a : items) set.add(a); return set; } static void addIfNotNull(Collection l, A a) { if (a != null && l != null) l.add(a); } static void addIfNotNull(MultiSet ms, A a) { if (a != null && ms != null) ms.add(a); } static List indexedNodes_rawLookup(String s) { List l = cachedNodeIndex().get(ai_shortenForIndex(s)); AI_SubSpace ss = ai_currentSubSpace(); if (ss != null) { List l2 = ss.indexedNodes_rawLookup(symbol(s)); return concatLists_conservative(l2, l); } return l; } // iterate safely (& quickly) in the face of concurrent modifications static IterableIterator concurrentlyIterateList(final List l) { return iteratorFromFunction_withEndMarker_f0(new F0() { int i; A get() { int _i = i++; synchronized(l) { if (_i < l(l)) { A a = l.get(_i); if (a == iteratorFromFunction_endMarker) throw fail("no"); // ugly comparison fail-fast redemption return a; } return (A) iteratorFromFunction_endMarker; // ugly cast } } }); } static List virtualNodeIndices_list = synchroList(); static List virtualNodeIndices() { return virtualNodeIndices_list; } static CombinedList combinedList_ll(List l) { CombinedList cl = new CombinedList(); cl.addList(l); return cl; } 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> 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); } // 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); } // This is a bit rough... finds static and non-static methods. static Method findMethodNamed(Object obj, String method) { if (obj == null) return null; if (obj instanceof Class) return findMethodNamed((Class) obj, method); return findMethodNamed(obj.getClass(), method); } static Method findMethodNamed(Class c, String method) { while (c != null) { for (Method m : c.getDeclaredMethods()) if (m.getName().equals(method)) { m.setAccessible(true); return m; } c = c.getSuperclass(); } return null; } static void upgradeJavaXAndRestart() { run("#1001639"); restart(); sleep(); } static String curlyBraceIfContainsArrow(String s) { return tok_containsSimpleArrow(s) ? optionalCurlyBrace(s) : s; } static String getCanonicalPath(File f) { try { return f == null ? null : f.getCanonicalPath(); } catch (Exception __e) { throw rethrow(__e); } } static String getCanonicalPath(String path) { return getCanonicalPath(newFile(path)); } static AutoCloseable tempCleaningUp() { return tempSetTL(ping_isCleanUpThread, true); } static void closeAllWriters(Collection l) { for (Writer w : unnull(l)) { try { w.close(); } catch (Throwable __e) { _handleException(__e); }} } 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 (ClassNotFoundException e) { return null; // could optimize this } } static String actualUserHome_value; static String actualUserHome() { if (actualUserHome_value == null) { if (isAndroid()) actualUserHome_value = "/storage/emulated/0/"; else actualUserHome_value = System.getProperty("user.home"); } return actualUserHome_value; } static File actualUserHome(String sub) { return newFile(new File(actualUserHome()), sub); } static List collect(Collection c, String field) { return collectField(c, field); } static List collect(String field, Collection c) { return collectField(c, field); } /*ifclass Concept static L collect(Class c, S field) { ret collect(list(c), field); } endif TODO: make translator ignore stuff in ifclass until resolved */ static void sleepInCleanUp(long ms) { try { if (ms < 0) return; Thread.sleep(ms); } catch (Exception __e) { throw rethrow(__e); } } static void _registerIO(Object object, String path, boolean opened) { } static boolean sameSnippetID(String a, String b) { if (!isSnippetID(a) || !isSnippetID(b)) return false; return parseSnippetID(a) == parseSnippetID(b); } static volatile String caseID_caseID; static String caseID() { return caseID_caseID; } static void caseID(String id) { caseID_caseID = id; } static int lastIndexOf(String a, String b) { return a == null || b == null ? -1 : a.lastIndexOf(b); } static int lastIndexOf(String a, char b) { return a == null ? -1 : a.lastIndexOf(b); } static boolean endsWithIgnoreCase(String a, String b) { int la = l(a), lb = l(b); return la >= lb && regionMatchesIC(a, la-lb, b, 0, lb); } static boolean endsWithIgnoreCase(String a, String b, Matches m) { if (!endsWithIgnoreCase(a, b)) return false; m.m = new String[] { substring(a, 0, l(a)-l(b)) }; return true; } static BufferedReader bufferedReader(Reader r) { return r instanceof BufferedReader ? (BufferedReader) r : _registerIOWrap(new BufferedReader(r), r); } static CloseableIterableIterator iteratorFromFunction_f0_autoCloseable(final F0 f, final AutoCloseable closeable) { class IFF2 extends CloseableIterableIterator { 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 = f.get(); done = a == null; } public void close() throws Exception { if (closeable != null) closeable.close(); } }; return new IFF2(); } static String readLineFromReaderWithClose(BufferedReader r) { try { String s = r.readLine(); if (s == null) r.close(); return s; } catch (Exception __e) { throw rethrow(__e); } } static AutoCloseable _wrapIOCloseable(final AutoCloseable c) { return c == null ? null : new AutoCloseable() { public String toString() { return "c.close();\r\n _registerIO(c, null, false);"; } public void close() throws Exception { c.close(); _registerIO(c, null, false); }}; } static A _registerIOWrap(A wrapper, Object wrapped) { return wrapper; } static FileInputStream newFileInputStream(File path) throws IOException { return newFileInputStream(path.getPath()); } static FileInputStream newFileInputStream(String path) throws IOException { FileInputStream f = new FileInputStream(path); _registerIO(f, path, true); return f; } static Map synchronizedMRUCache(int maxSize) { return synchroMap(new MRUCache(maxSize)); } static String[] codeTokensAsStringArray(List tok) { int n = max(0, (l(tok)-1)/2); String[] out = new String[n]; for (int i = 0; i < n; i++) out[i] = tok.get(i*2+1); return out; } static int jfind(String s, String in) { return jfind(javaTok(s), in); } 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) { //LS tokin = jfind_preprocess(javaTok(in)); return jfind(tok, startIdx, javaTokForJFind_array(in), 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, String[] tokinC, Object condition) { return findCodeTokens(tok, startIdx, false, tokinC, condition); } static int jfind(List tok, int startIdx, List tokin, Object condition) { return findCodeTokens(tok, startIdx, false, codeTokensAsStringArray(tokin), condition); } static List jfind_preprocess(List tok) { for (String type : litlist("quoted", "id", "int")) replaceSublist(tok, ll("<", "", type, "", ">"), ll("<" + type + ">")); replaceSublist(tok, ll("\\", "", "*"), ll("\\*")); return tok; } // supports the usual quotings (", variable length double brackets) except ' quoting static boolean isQuoted(String s) { if (isNormalQuoted(s)) return true; // use the exact version return isMultilineQuoted(s); } static boolean isIdentifier(String s) { return isJavaIdentifier(s); } static boolean checkTokCondition(Object condition, List tok, int i) { if (condition instanceof TokCondition) return ((TokCondition) condition).get(tok, i); return checkCondition(condition, tok, i); } static void replaceCollection(Collection dest, Collection src) { dest.clear(); dest.addAll(src); } static void replaceListPart(List l, int i, int j, List l2) { int j2 = i+l(l2); if (j2 == j) { copyListPart(l2, 0, l, i, l(l2)); return; } l.subList(i, j).clear(); l.addAll(i, l2); } static List jsonTok(String s) { List tok = new ArrayList(); int l = l(s); 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 is not needed in rest of loop body // scan for non-whitespace (json strings, "null" identifier, numbers. everything else automatically becomes a one character token.) if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { if (s.charAt(j) == opener) { ++j; break; } else if (s.charAt(j) == '\\' && j+1 < l) j += 2; else ++j; } } else if (Character.isLetter(c)) do ++j; while (j < l && Character.isLetter(s.charAt(j))); else if (Character.isDigit(c)) do ++j; while (j < l && Character.isDigit(s.charAt(j))); else ++j; tok.add(s.substring(i, j)); i = j; } if ((tok.size() % 2) == 0) tok.add(""); return tok; } static double parseDouble(String s) { return Double.parseDouble(s); } static Map vm_generalHashMap(Object name) { synchronized(get(javax(), "generalMap")) { Map m = (Map) (vm_generalMap_get(name)); if (m == null) vm_generalMap_put(name, m = syncHashMap()); return m; } } static Runnable asRunnable(Object o) { return toRunnable(o); } static void _inheritThreadInfo(Object info) { _threadInheritInfo(info); } static MultiMap cachedNodeIndex() { return cachedNodeIndex2(); } static Map myFrames_list = weakHashMap(); static List myFrames() { return swing(new F0>() { List get() { try { return keysList(myFrames_list); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret keysList(myFrames_list);"; }}); } static void ai_onNewOrRemovedWeb(Object onNewWeb, Object onRemovedWeb) { ai_onNewWeb(onNewWeb); if (onRemovedWeb != null) onTransientWebRemoved_do(onRemovedWeb); } 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 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 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 List web_search_dollarX(Web searchWeb, Web web) { return webs_search_dollarX(searchWeb, ll(web)); } static A copyFields(Object x, A 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); } return y; } static A copyFields(Object x, A y, Collection fields) { return copyFields(x, y, asStringArray(fields)); } static A addVirtualNodeIndex(A i) { setAdd(virtualNodeIndices_list, i); return i; } static List ai_triplesMadeByProgramAsTriples(String progID) { return webs_readTripleFileAsTriples(getProgramFile(progID, "triples.gz")); } static List ai_loadWebsFromFile(File f, String src) { List webs = new ArrayList(); ai_loadWebsFromFile(f, src, webs); return webs; } static void ai_loadWebsFromFile(File f, String src, List websOut) { ai_loadWebsFromFile(f, src, websOut, null); } static void ai_loadWebsFromFile(File f, String src, List websOut, Map furtherInfoMap) { //if (hasMultipleCores()) ai_loadWebsFromFile_parallel(f, src, websOut, furtherInfoMap); IterableIterator l = scanLog_unstructure_iterator(f); // TODO: clean up file handle in case of error for (SoftwareMadeWeb o : l) { try { Web web = o.web; if (web == null) web = web_unstructure(o.structure); if (web != null) { websOut.add(web_intern(web_setSourceIfEmpty(web, src))); mapPut(furtherInfoMap, web, o); } } catch (Throwable __e) { _handleException(__e); }} } static List websToTripleWebs(Collection l) { return map("ai_webToTripleWeb",l); } static List ai_filterTriples(String a, String b, String c, List l) { List out = new ArrayList(); for (TripleWeb w : l) if (canSimpleMatchTriple_dollarVars(a, b, c, w)) out.add(w); return out; } static List ai_thoughtSpace_queryTriples(String a, String b, String c, AbstractThoughtSpace ts) { List l = null; if (!isDollarVar(a)) { if (!isDollarVar(b)) return ts.getOneTwo(a, b); l = shortestListUnlessNull2(l, ts.getTriples(a, 0)); if (l(l) <= 1) return l; } if (!isDollarVar(b)) { l = shortestListUnlessNull2(l, ts.getTriples(b, 1)); if (l(l) <= 1) return l; } if (!isDollarVar(c)) l = shortestListUnlessNull2(l, ts.getTriples(c, 2)); return l; } static boolean possibleGlobalID(String s) { return l(s) == globalIDLength() && allLowerCaseCharacters(s); } static String programIDWithCase() { return nempty(caseID()) ? programID() + "/" + quoteUnlessIdentifierOrInteger(caseID()) : programID(); } static Object defaultDefaultClassFinder() { return new F1() { Class get(String name) { try { Class c = findClass_fullName(name); if (c != null) return c; if (startsWith(name, "loadableUtils.utils$")) return findClass_fullName("main" + substring(name, 19)); return null; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Class c = findClass_fullName(name);\r\n if (c != null) ret c;\r\n if (start..."; }}; } 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 String joinNempties(String sep, String... strings) { return joinStrings(sep, strings); } static String joinNempties(String sep, Collection strings) { return joinStrings(sep, strings); } static String stream2string(InputStream in) { return utf8streamToString(in); } static char lastChar(String s) { return empty(s) ? '\0' : s.charAt(l(s)-1); } static Map syncMRUCache(int size) { return synchroMap(new MRUCache(size)); } static File standardLogFile() { return getProgramFile("log"); } static A _recordNewSwingComponent(A c) { if (c != null) callF((Object) vm_generalMap_get("newSwingComponentRegistry"), (Object) c); return c; } static JComponent componentToJComponent(Component c) { if (c instanceof JComponent) return (JComponent) c; if (c instanceof JFrame) return ((JFrame) c).getRootPane(); if (c == null) return null; throw fail("boohoo " + getClassName(c)); } static JLabel jlabel(final String text) { return swingConstruct(BetterLabel.class, text); } static JLabel jlabel() { return jlabel(" "); } static void enableWordWrapForTextArea(final JTextArea ta) { if (ta != null) { swing(new Runnable() { public void run() { try { ta.setLineWrap(true); ta.setWrapStyleWord(true); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ta.setLineWrap(true);\r\n ta.setWrapStyleWord(true);"; }}); } } static JTextArea jtextarea() { return jTextArea(); } static JTextArea jtextarea(String text) { return jTextArea(text); } static boolean setText_opt = true; // optimize by calling getText first static A setText(A c, Object text) { setText((JComponent) 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((JComponent) c, text); } static JButton setText(JButton c, Object text) { setText((JComponent) c, jlabel_textAsHTML_center_ifNeeded(strUnnull(text))); return c; } static A setText(final A c, Object text) { if (c == null) return null; final String s = strUnnull(text); { swing(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 JTextField onEnter(final JTextField tf, final Object action) { if (action == null || tf == null) return tf; tf.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent _evt) { try { tf.selectAll(); callF(action); } catch (Throwable __e) { messageBox(__e); }}}); return tf; } 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(rCallOnSelectedListItem(list, action))); return list; } static JComboBox onEnter(final JComboBox cb, final Object action) { { swing(new Runnable() { public void run() { try { if (cb.isEditable()) { JTextField text = (JTextField) cb.getEditor().getEditorComponent(); onEnter(text, action); } else { cb.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "enter"); cb.getActionMap().put("enter", abstractAction("", new Runnable() { public void run() { try { cb.hidePopup(); callF(action); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "cb.hidePopup(); callF(action);"; }})); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (cb.isEditable()) {\r\n JTextField text = (JTextField) cb.getEditor().g..."; }}); } return cb; } static JTable onEnter(final JTable table, final Object action) { table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) .put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter"); table.getActionMap().put("Enter", new AbstractAction() { public void actionPerformed(ActionEvent e) { callF(action, table.getSelectedRow()); } }); return table; } /*static JTextArea onEnter(final JTextArea ta, fO action) { addKeyListener(ta, enterKeyListener(action)); ret ta; }*/ static JTextField onEnter(Object action, JTextField tf) { return onEnter(tf, action); } static A heldInstance(Class c) { List l = holdInstance_l.get(); for (int i = l(l)-1; i >= 0; i--) { Object o = l.get(i); if (isInstanceOf(o, c)) return (A) o; } throw fail("No instance of " + className(c) + " held"); } static float getSwingFontScale() { return or((Float) vm_generalMap_get("swingFontScale_value"), 1f); } static ActionListener actionListener(final Object runnable) { return actionListener(runnable, null); } static ActionListener actionListener(final Object runnable, final Object instanceToHold) { if (runnable instanceof ActionListener) return (ActionListener) runnable; final Object info = _threadInfo(); return new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent _evt) { try { _threadInheritInfo(info); AutoCloseable __524 = holdInstance(instanceToHold); try { callF(runnable); } finally { _close(__524); }} catch (Throwable __e) { messageBox(__e); }}}; } static int toInt_checked(long l) { if (l != (int) l) throw fail("Too large for int: " + l); return (int) l; } static TreeSet caseInsensitiveSet_treeSet() { return new TreeSet(caseInsensitiveComparator()); } static TreeSet caseInsensitiveSet_treeSet(Collection c) { return toCaseInsensitiveSet_treeSet(c); } static A setThreadLocal(ThreadLocal tl, A value) { if (tl == null) return null; A old = tl.get(); tl.set(value); return old; } static int lUtf8(String s) { return l(utf8(s)); } static String loadPage_utf8(URL url) { return loadPage_utf8(url.toString()); } static String loadPage_utf8(String url) { AutoCloseable __1934 = tempSetTL(loadPage_charset, "UTF-8"); try { return loadPage(url); } finally { _close(__1934); }} static String loadPage_utf8(URLConnection con, URL url, boolean addHeaders) throws IOException { AutoCloseable __1935 = tempSetTL(loadPage_charset, "UTF-8"); try { return loadPage(con, url, addHeaders); } finally { _close(__1935); }} 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 File javaxSecretDir(String sub) { return newFile(javaxSecretDir(), sub); } static File prepareProgramFile(String name) { return mkdirsForFile(getProgramFile(name)); } static File prepareProgramFile(String progID, String name) { return mkdirsForFile(getProgramFile(progID, name)); } static int ai_shortenForIndex_maxLength = 100; static String ai_shortenForIndex(String text) { return shorten(text, ai_shortenForIndex_maxLength); } static ThreadLocal ai_currentSubSpace = new ThreadLocal(); static AI_SubSpace ai_currentSubSpace() { return ai_currentSubSpace.get(); } static List concatLists_conservative(List a, List b) { if (empty(a)) return b; if (empty(b)) return a; return concatLists(a, b); } static List concatLists_conservative(Collection a, Collection b) { if (empty(a) && b instanceof List) return ((List) b); if (empty(b) && a instanceof List) return ((List) a); return concatLists(a, b); } static IterableIterator iteratorFromFunction_withEndMarker_f0(final F0 f) { class IFF2 extends IterableIterator { A a; boolean have, done; public boolean hasNext() { getNext(); return !done; } public A next() { getNext(); if (done) throw fail(); A _a = a; a = null; have = false; return _a; } void getNext() { if (done || have) return; a = f.get(); have = true; if (a == iteratorFromFunction_endMarker) done = true; } }; return new IFF2(); } static List> allPermutations(List l) { List> 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 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 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 Class run(String progID, String... args) { Class main = hotwire(progID); callMain(main, args); return main; } static void restart() { Object j = getJavaX(); call(j, "cleanRestart", get(j, "fullArgs")); } static boolean tok_containsSimpleArrow(String s) { return tok_containsSimpleArrow(javaTok(s)); } static boolean tok_containsSimpleArrow(List tok) { return containsSubList(tok, "-", "", ">"); } static String optionalCurlyBrace(String s) { return isCurlyBraced(s) ? s : curlyBrace(s); } 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 classNameToVM(String name) { return name.replace(".", "$"); } static List collectField(Collection c, String field) { List l = new ArrayList(); if (c != null) for (Object a : c) l.add(getOpt(a, field)); return l; } static List collectField(String field, Collection c) { return collectField(c, field); } 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; removeSubList(l, i, i+l(x)); l.addAll(i, y); i += l(y); } return l; } static List replaceSublist(List l, int fromIndex, int toIndex, List y) { // TODO: optimize more removeSubList(l, fromIndex, toIndex); l.addAll(fromIndex, y); return l; } static boolean isNormalQuoted(String s) { int l = l(s); if (!(l >= 2 && s.charAt(0) == '"' && lastChar(s) == '"')) return false; int j = 1; while (j < l) if (s.charAt(j) == '"') return j == l-1; else if (s.charAt(j) == '\\' && j+1 < l) j += 2; else ++j; return false; } 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 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 boolean checkCondition(Object condition, Object... args) { return isTrue(callF(condition, args)); } static boolean checkCondition(IF1 condition, A arg) { return isTrue(callF(condition, arg)); } static void copyListPart(List a, int i1, List b, int i2, int n) { if (a == null || b == null) return; for (int i = 0; i < n; i++) b.set(i2+i, a.get(i1+i)); } static Map syncHashMap() { return synchroHashMap(); } static ThreadLocal> holdInstance_l = new ThreadLocal(); static AutoCloseable holdInstance(Object o) { if (o == null) return null; listThreadLocalAdd(holdInstance_l, o); return new AutoCloseable() { public void close() { listThreadLocalPopLast(holdInstance_l); } }; } // requires ugly casting when used (O -> A) static Object iteratorFromFunction_endMarker = new Object(); // f: func -> A | endMarker static IterableIterator iteratorFromFunction_withEndMarker(final Object f) { class IFF extends 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); if (a == iteratorFromFunction_endMarker) done = true; } }; return new IFF(); } // optimized version for F0 argument static IterableIterator iteratorFromFunction_withEndMarker(final F0 f) { return iteratorFromFunction_withEndMarker_f0(f); } static Cache> cachedNodeIndex2_cache = new Cache("cachedNodeIndex2_make"); static boolean cachedNodeIndex_autoMake = true; 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) { if (!cachedNodeIndex_autoMake) return false; Lock __439 = cachedNodeIndex2_cache.lock; lock(__439); try { //print("Incremental update"); ai_addWebToIndex(cachedNodeIndex2_cache.get(), web); cachedNodeIndex2_cache.changeCount++; return false; // don't trigger full update } finally { unlock(__439); } } static void cachedNodeIndex2_onWebRemoved(Web web) { if (!cachedNodeIndex_autoMake) return; Lock __440 = cachedNodeIndex2_cache.lock; lock(__440); try { print("Incremental removal"); ai_removeWebFromIndex(cachedNodeIndex2_cache.get(), web); cachedNodeIndex2_cache.changeCount++; } finally { unlock(__440); } } static long cachedNodeIndex_changeCount() { return cachedNodeIndex2_cache.changeCount; } static MultiMap cachedNodeIndex2_make() { if (cachedNodeIndex_autoMake) { print("Making index"); return dwlw_fullNodeIndex_ci(); } else { //print("Skipping making index"); return ai_emptyWebIndex(); } } static Map weakHashMap() { return newWeakHashMap(); } static List keysList(Map map) { return cloneListSynchronizingOn(keys(map), map); } static List keysList(MultiSet ms) { return ms == null ? null : keysList(ms.map); } static void ai_onNewWeb(Object onNewWeb) { if (onNewWeb == null) return; setAdd(postSoftwareMadeWeb_onNewWeb, onNewWeb); onTransientWebAdded_do(onNewWeb); } 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 Collection web_relationObjects(Web web) { List l = new ArrayList(); for (WebNode n : web.nodes) if (n instanceof WebRelation) l.add(((WebRelation) n)); return l; } 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 boolean match(String pat, List toks, Matches matches) { return match3(pat, toks, matches); } static List webs_search_dollarX(Web searchWeb, Collection webs) { List> mappings = new ArrayList(); for (Web web : webs) addIfNotNull(mappings, web_matchAllPerms_x(searchWeb, web, "nodeMatch_dollarVars")); return getFromAll(mappings, web_findNode(searchWeb, "$X")); } 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(); } // XXX NEW - hopefully this doesn't break anything if (o instanceof DynamicObject) map.putAll(((DynamicObject) o).fieldValues); return map; } catch (Exception __e) { throw rethrow(__e); } } // same for a collection (convert each element) static List> objectToMap(Iterable l) { if (l == null) return null; List x = new ArrayList(); for (Object o : l) x.add(objectToMap(o)); return x; } static String[] asStringArray(Collection c) { return toStringArray(c); } static String[] asStringArray(Object o) { return toStringArray(o); } static boolean setAdd(Collection c, A a) { if (c == null || c.contains(a)) return false; c.add(a); return true; } static boolean webs_readTripleFileAsTriples_debug; static List webs_readTripleFileAsTriples(File f) { if (hasMultipleCores()) return webs_readTripleFileAsTriples_parallel2(f); if (!f.exists()) return ll(); Iterator it = linesFromFile(f); List names = new ArrayList(); while (it.hasNext()) { String s = trim(it.next()); if (empty(s)) break; names.add(symbol(unquote(s))); } if (webs_readTripleFileAsTriples_debug) print("Have " + l(names) + " names"); List triples = new ArrayList(); while (it.hasNext()) { String s = it.next(); try { addIfNotNull(triples, webs_readTripleFileAsTriples_line(s, names)); } catch (Throwable __e) { _handleException(__e); } } return triples; } static TripleWeb webs_readTripleFileAsTriples_line(String s, List names) { List l = javaTokC(s); if (l(l) >= 8) { String source = intern(unquote(l.get(5))); Object result = null; // result try { if (eq(get(l, 8), "(")) { int i = indexOf(l, ")", 9); if (i >= 0) result = safeUnstructure(joinWithSpace(subList(l, 9, i))); } } catch (Throwable __e) { _handleException(__e); } TripleWeb t = newTripleWebWithSourceAndResult(source, result); t.a = names.get(parseInt(l.get(0))); t.b = names.get(parseInt(l.get(1))); t.c = names.get(parseInt(l.get(2))); t.globalID(unquote(l.get(3))); // t.title = unquote(l.get(4)); // unused t.flags = eq(l.get(6), "v") ? 0 : TripleWeb.UNVERIFIED; t.created(parseLong(l.get(7))); return t; } else if (webs_readTripleFileAsTriples_debug) print("Wrong number of tokens in line: " + l(l) + " - " + sfu(l)); return null; } static IterableIterator scanLog_unstructure_iterator(String progID, String fileName) { return scanLog_unstructure_iterator(getProgramFile(progID, fileName)); } static IterableIterator scanLog_unstructure_iterator(String fileName) { return scanLog_unstructure_iterator(getProgramFile(fileName)); } static IterableIterator scanLog_unstructure_iterator(File file) { final Iterator it = scanLog_iterator(file); return iteratorFromFunction(new F0() { Object get() { try { while (it.hasNext()) { try { return unstructure(it.next()); } catch (Throwable __e) { _handleException(__e); }} return null; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "while (it.hasNext()) pcall {\r\n ret unstructure(it.next());\r\n }\r\n n..."; }}); } static Web web_unstructure(String s) { //ret ((Web) unstructure(s)).afterLoad(); return (Web) unstructure(s); } static Web web_setSourceIfEmpty(Web web, String src) { if (web == null) return null; if (empty(web.source)) web.source = src; return web; } static TripleWeb ai_webToTripleWeb(Web web) { T3 t = ai_webToTriple(web); if (t == null) return null; TripleWeb w = newTripleWebWithSource(web.source); w.a = t.a; w.b = t.b; w.c = t.c; w.globalID(web.globalIDObj()); w.created(web.created); w.flags = web.unverified ? w.UNVERIFIED : 0; return w; } static boolean canSimpleMatchTriple_dollarVars(T3 pat, T3 t) { if (pat == null || t == null) return false; return simpleMatchTriple_dollarVars(pat.a, pat.b, pat.c, t) != null; } static boolean canSimpleMatchTriple_dollarVars(String patA, String patB, String patC, T3 t) { return simpleMatchTriple_dollarVars(patA, patB, patC, t) != null; } static List shortestListUnlessNull2(List a, List b) { if (a == null) return b; if (b == null) return a; return l(a) < l(b) ? a : b; } static int globalIDLength() { return 16; } 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 String quoteUnlessIdentifierOrInteger(String s) { return quoteIfNotIdentifierOrInteger(s); } static HashMap findClass_fullName_cache = new HashMap(); // returns null on not found // this is the simple version that is not case-tolerant static Class findClass_fullName(String name) { synchronized(findClass_fullName_cache) { if (findClass_fullName_cache.containsKey(name)) return findClass_fullName_cache.get(name); Class c; try { c = Class.forName(name); } catch (ClassNotFoundException e) { c = null; } findClass_fullName_cache.put(name, c); return c; } } 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 String utf8streamToString(InputStream in) { return readerToString(utf8bufferedReader(in)); } 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 "ret nuObject(c, args);"; }}); } static JTextArea jTextArea() { return jTextArea(""); } static JTextArea jTextArea(final String text) { return jTextAreaWithUndo(text); } static String strUnnull(Object o) { return o == null ? "" : str(o); } static String jlabel_textAsHTML_center_ifNeeded(String text) { if (swic(text, "") && ewic(text, "")) return text; if (!containsNewLines(text)) return text; return jlabel_textAsHTML_center(text); } 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(hideCredentials(innerException2(e))); } static KeyListener enterKeyListener(final Object action) { return new KeyAdapter() { public void keyPressed(KeyEvent ke) { if (ke.getKeyCode() == KeyEvent.VK_ENTER) pcallF(action); } }; } static Runnable rCallOnSelectedListItem(final JList list, final Object action) { return new Runnable() { public void run() { try { pcallF(action, getSelectedItem(list)) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pcallF(action, getSelectedItem(list))"; }}; } static AbstractAction abstractAction(String name, final Object runnable) { return new AbstractAction(name) { public void actionPerformed(ActionEvent evt) { pcallF(runnable); } }; } static boolean isInstanceOf(Object o, Class type) { return type.isInstance(o); } static TreeSet toCaseInsensitiveSet_treeSet(Iterable c) { if (isCISet(c)) return (TreeSet) c; TreeSet set = caseInsensitiveSet_treeSet(); addAll(set, c); return set; } static TreeSet toCaseInsensitiveSet_treeSet(String... x) { TreeSet set = caseInsensitiveSet_treeSet(); addAll(set, x); return set; } static String shortenSymbol(String s) { return shortenSymbol(s, shorten_default); } static String shortenSymbol(String s, int max) { return shortenSymbol(s, max, "..."); } static String shortenSymbol(String s, int max, String shortener) { if (s == null) return emptySymbol(); if (max < 0) return s; return s.length() <= max ? s : symbol(substring(str(s), 0, min(s.length(), max-l(shortener))) + shortener); } static String shortenSymbol(int max, String s) { return shortenSymbol(s, max); } 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 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)); } static boolean web_isAnythingVariable(WebNode n) { return endsWithIC(web_text(n), " [Anything]"); } static boolean containsAllIC_lisp(Collection a, Collection b) { for (Lisp o : b) if (!containsLispIC(a, o)) return false; return true; } static List web_labels(WebNode n) { return n == null ? null : n.labels(); } static boolean containsSubList(List x, List y) { return indexOfSubList(x, y) >= 0; } static boolean containsSubList(List x, A... y) { return indexOfSubList(x, y, 0) >= 0; } static boolean isCurlyBraced(String s) { List tok = tok_combineCurlyBrackets_keep(javaTok(s)); return l(tok) == 3 && startsWithAndEndsWith(tok.get(1), "{", "}"); } static String curlyBrace(String s) { return "{" + s + "}"; } 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 void removeSubList(List l, int from, int to) { if (l != null) subList(l, from, to).clear(); } static void removeSubList(List l, int from) { if (l != null) subList(l, from).clear(); } static double nodeMatch_dollarVars(WebNode a, WebNode b) { if (isDollarVar(a.text())) return isDollarVar(b.text()) ? -1 : 0; for (String s : web_texts(b)) if (!web_hasLabel(a, s)) return -1; return 0; //ret containsAll(web_ucTexts(a), web_ucTexts(b)) ? 0 : -1; } static double nodeMatch_dollarVars(CharSequence _text, WebNode b) { String text = str(_text); if (b == null) return -1; if (isDollarVar(text)) return isDollarVar(b.text()) ? -1 : 0; return cic(web_texts(b), text) ? 0 : -1; } static void listThreadLocalAdd(ThreadLocal> tl, A a) { List l = tl.get(); if (l == null) tl.set(l = new ArrayList()); l.add(a); } static A listThreadLocalPopLast(ThreadLocal> tl) { List l = tl.get(); if (l == null) return null; A a = popLast(l); if (empty(l)) tl.set(null); return a; } static List onWebsChanged_listeners = synchroList(); static void triggerWebsChanged() { pcallF_all(onWebsChanged_listeners); } static void onWebsChanged(Object f) { setAdd(onWebsChanged_listeners, f); } 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 FileStatus conceptsFileStatus(String progID) { return fileStatus(conceptsFile(progID)); } static String circlesEditorDBID() { return "#1007609"; } static void ai_addWebToIndex(MultiMap index, Web web) { if (web == null) return; for (WebNode node : web_nodesAndRelations(web)) for (String text : asSet(node.texts())) { String key = ai_shortenForIndex(text); boolean newText; synchronized(index) { if (key == text) // unchanged by shortening newText = true; else newText = !web_nodesContainText(index.get(key), text); index.put(key, node); } if (newText) ai_onNewIndexedText(text); } } 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 MultiMap dwlw_fullNodeIndex_ci() { print("Getting webs."); long _startTime_492 = sysNow(); List webs = ai_filterBadWebs(allWebs()); _startTime_492 = sysNow()-_startTime_492; saveTiming(_startTime_492); print("Got webs in " + lastTiming() + " ms. Making index of " + nWebs(webs) + "."); long _startTime_493 = sysNow(); MultiMap index = ai_indexWebs(webs); done2_always("Index made.", _startTime_493); return index; } static MultiMap ai_emptyWebIndex() { MultiMap index = caseInsensitiveSyncListMultiMap(); makeMultiMapsInnerNavigableMapSynchronized(index); return index; } static ArrayList cloneListSynchronizingOn(Collection l, Object mutex) { if (l == null) return new ArrayList(); synchronized(mutex) { return new ArrayList(l); } } 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_cachedInput(s), matches); } static boolean match3(String pat, List toks, Matches matches) { List tokpat = parse3_cachedPattern(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; } // 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> 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 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 WebNode web_findNode(Web web, String name) { return web.findNode(name); } 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 boolean hasMultipleCores() { return isMultiCore(); } static List webs_readTripleFileAsTriples_parallel2(File f) { try { if (!f.exists()) return ll(); long _startTime_510 = sysNow(); Iterator it = linesFromFile(f); final List names = new ArrayList(); while (it.hasNext()) { String s = trim(it.next()); if (empty(s)) break; names.add(symbol(unquote(s))); } done2_always("Getting names", _startTime_510); if (webs_readTripleFileAsTriples_debug) print("Have " + l(names) + " names"); final List, Integer>> triples = new ArrayList(); int poolSize = numberOfCores(), queueSize = 10000; int blockSize = 1000; NotifyingBlockingThreadPoolExecutor e = new NotifyingBlockingThreadPoolExecutor(poolSize, queueSize, 15, TimeUnit.SECONDS); print("Using " + poolSize + " cores, block size: " + blockSize + "."); try { int i = 0; List block = emptyListWithCapacity(blockSize); while (licensed()) { ++i; final Integer _i = i; boolean hasNext = it.hasNext(); if ((!hasNext || l(block) >= blockSize) && nempty(block)) { final List _block = cloneList(block); e.execute(new Runnable() { public void run() { try { try { List l = emptyListWithCapacity(_block); for (String s : _block) { TripleWeb w = webs_readTripleFileAsTriples_line(s, names); if (w != null) l.add(w); } synchronized(triples) { triples.add(pair(l, _i)); } } catch (Throwable __e) { _handleException(__e); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pcall {\r\n List l = emptyListWithCapacity(_block);\r\n ..."; }}); block.clear(); } if (hasNext) block.add(it.next()); else break; } e.shutdown(); e.awaitTermination(1, TimeUnit.DAYS); } finally { e.shutdown(); } sortBySecondOfPairs_inPlace(triples); int n = 0; for (Pair, Integer> p : triples) n += l(p.a); List l = emptyListWithCapacity(n); for (Pair, Integer> p : triples) l.addAll(p.a); return l; } catch (Exception __e) { throw rethrow(__e); } } static Object safeUnstructure(String s) { return unstructure(s, true); } static TripleWeb newTripleWebWithSourceAndResult(String source, Object result) { if (result == null) return newTripleWebWithSource(source); return new TripleWebWithSourceAndResult(source, result); } 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)..."; }}); } // f: func -> A (stream ends when f returns null) static IterableIterator iteratorFromFunction(final Object f) { class IFF extends 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; } }; return new IFF(); } // optimized version for F0 argument static IterableIterator iteratorFromFunction(final F0 f) { return iteratorFromFunction_f0(f); } 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_unquoteBufSize = 100; 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; final Object classFinder = _classFinder != null ? _classFinder : _defaultClassFinder(); HashMap refs = new HashMap(); HashMap tokrefs = new HashMap(); HashSet concepts = new HashSet(); HashMap classesMap = new HashMap(); List stack = new ArrayList(); String curT; char[] unquoteBuf = new char[unstructure_unquoteBufSize]; String unquote(String s) { return unquoteUsingCharArray(s, unquoteBuf); } // 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("sh")) { consume(); t = tpp(); if (t.equals("-")) { t = tpp(); out.set((short) (-parseInt(t))); return; } out.set((short) parseInt(t)); return; } if (t.equals("-")) { consume(); t = tpp(); out.set(isLongConstant(t) ? (Object) (-parseLong(t)) : (Object) (-parseInt(t))); return; } if (isInteger(t) || isLongConstant(t)) { consume(); //if (debug) print("isLongConstant " + quote(t) + " => " + isLongConstant(t)); if (isLongConstant(t)) { out.set(parseLong(t)); return; } long l = parseLong(t); boolean isInt = l == (int) l; out.set(isInt ? (Object) Integer.valueOf((int) l) : (Object) Long.valueOf(l)); return; } if (t.equals("false") || t.equals("f")) { consume(); out.set(false); return; } if (t.equals("true") || t.equals("t")) { consume(); out.set(true); return; } if (t.equals("-")) { consume(); t = tpp(); out.set(isLongConstant(t) ? (Object) (-parseLong(t)) : (Object) (-parseInt(t))); return; } if (isInteger(t) || isLongConstant(t)) { consume(); //if (debug) print("isLongConstant " + quote(t) + " => " + isLongConstant(t)); if (isLongConstant(t)) { out.set(parseLong(t)); return; } long l = parseLong(t); boolean isInt = l == (int) l; out.set(isInt ? (Object) Integer.valueOf((int) l) : (Object) Long.valueOf(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("lhs")) { parseLinkedHashSet(out); return; } if (t.equals("treeset")) { parseTreeSet(out); return; } if (t.equals("ciset")) { parseCISet(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("tm")) { consume(); parseMap(new TreeMap(), out); return; } if (t.equals("cimap")) { consume(); parseMap(ciMap(), out); return; } if (t.equals("ll")) { consume(); { parseList(new LinkedList(), out); return; } } if (t.equals("sync")) { consume(); if (t().equals("tm")) { consume(); { parseMap(synchronizedTreeMap(), out); return; } } if (t().equals("[")) { parseList(synchroList(), out); return; } { parseMap(synchronizedMap(), out); return; } } if (t.equals("{")) { parseMap(out); return; } if (t.equals("[")) { this.parseList(new ArrayList(), 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 (or package name) consume(); String className, fullClassName; // Is it a package name? if (eq(t(), ".")) { consume(); className = fullClassName = t + "." + assertIdentifier(tpp()); } else { className = t; fullClassName = "main$" + t; } if (c == null) { // First, find class if (allDynamic) c = null; else c = classFinder != null ? (Class) callF(classFinder, fullClassName) : findClass_fullName(fullClassName); if (c != null) classesMap.put(className, c); } // Check if it has an outer reference boolean hasBracket = eq(t(), "("); if (hasBracket) consume(); boolean hasOuter = hasBracket && eq(t(), "this$1"); DynamicObject dO = null; Object o = null; final String thingName = t; if (c != null) { o = hasOuter ? nuStubInnerObject(c, classFinder) : 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 = className; } // 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()); if (!eq(tpp(), "=")) throw fail("= expected, got " + t() + " after " + quote(key) + " in object " + thingName /*+ " " + sfu(fields)*/); 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) { setOptAllDyn(dO, fields); } else { setOptAll_pcall(o, fields); } else for (String field : keys(fields)) dO.fieldValues.put(intern(field), fields.get(field)); if (o != null) pcallOpt_noArgs(o, "_doneLoading"); } void parseSet(final Set set, final unstructure_Receiver out) { this.parseList(new ArrayList(), 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(new 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(Lisp((Str..."; }}); if (false) // skip fail line throw fail("class Lisp not included"); } 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 List list, final unstructure_Receiver out) { consume("["); 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(")"); Class c = allDynamic ? null : classFinder != null ? (Class) callF(classFinder, name) : findClass_fullName(name); if (c != null) return c; DynamicObject dO = new DynamicObject(); dO.className = "java.lang.Class"; name = dropPrefix("main$", name); 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 parseLinkedHashSet(unstructure_Receiver out) { consume("lhs"); parseSet(new LinkedHashSet(), out); } void parseTreeSet(unstructure_Receiver out) { consume("treeset"); parseSet(new TreeSet(), out); } void parseCISet(unstructure_Receiver out) { consume("ciset"); parseSet(ciSet(), 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); if (!eq(tpp(), "=")) throw fail("= expected, got " + t() + " in map of size " + l(map)); parse(new unstructure_Receiver() { void set(Object value) { map.put(key, value); 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 T3 ai_webToTriple(Web web) { if (web == null) return null; Collection l = web_relationObjects(web); if (l(l) != 1) return null; WebRelation r = first(l); if (!web_tripelizableNode(r.a) || !web_tripelizableNode(r.b) || !web_tripelizableNode(r)) return null; return t3(web_sym(r.a), web_sym(r), web_sym(r.b)); } static TripleWeb newTripleWebWithSource(String source) { return empty(source) ? new TripleWeb() : new TripleWebWithSource(source); } static Map simpleMatchTriple_dollarVars(T3 pat, T3 t) { if (pat == null || t == null) return null; return simpleMatchTriple_dollarVars(pat.a, pat.b, pat.c, t); } static Map simpleMatchTriple_dollarVars(String patA, String patB, String patC, T3 t) { if (t == null) return null; Map match = new HashMap(); return matchDollarVarIC(match, patA, t.a) && matchDollarVarIC(match, patB, t.b) && matchDollarVarIC(match, patC, t.c) ? match : null; } static String quoteIfNotIdentifierOrInteger(String s) { if (s == null) return null; return isJavaIdentifier(s) || isInteger(s) ? s : quote(s); } static String readerToString(Reader r) { try { try { 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 { r.close(); } } catch (Exception __e) { throw rethrow(__e); } } static JTextArea jTextAreaWithUndo() { return jTextAreaWithUndo(""); } static JTextArea jTextAreaWithUndo(final String text) { return jenableUndoRedo(swingNu(JTextArea.class, text)); } static boolean containsNewLines(String s) { return containsNewLine(s); } static String jlabel_textAsHTML_center(String text) { return "
" + replace(htmlencode(text), "\n", "
") + "
"; } static boolean headless() { return isHeadless(); } static String getSelectedItem(JList l) { return (String) l.getSelectedValue(); } static String getSelectedItem(JComboBox cb) { return strOrNull(cb.getSelectedItem()); } static String emptySymbol_value; static String emptySymbol() { if (emptySymbol_value == null) emptySymbol_value = symbol(""); return emptySymbol_value; } static boolean endsWithIC(String a, String b) { return endsWithIgnoreCase(a, b); } static boolean containsLispIC(Collection l, Lisp s) { for (Lisp x : l) if (lispEqic(x, s)) return true; return false; } static List tok_combineCurlyBrackets_keep(List tok) { List l = new ArrayList(); for (int i = 0; i < l(tok); i++) { String t = tok.get(i); if (odd(i) && eq(t, "{")) { int j = findEndOfCurlyBracketPart(tok, i); l.add(joinSubList(tok, i, j)); i = j-1; } else l.add(t); } return l; } static boolean startsWithAndEndsWith(String s, String prefix, String suffix) { return startsWith(s, prefix) && endsWith(s, suffix); } static boolean web_hasLabel(WebNode node, String text) { return web_hasLabelIC(node, text); } static boolean web_hasLabel(Pair p, String text) { return web_hasLabelIC(p, text); } // test relation static boolean web_hasLabel(WebNode a, WebNode b, String text) { return web_hasLabelIC(a, b, text); } static void pcallF_all(Collection l, Object... args) { if (l != null) for (Object f : cloneList(l)) pcallF(f, args); } static FileStatus fileStatus(String path) { return fileStatus(newFile(path)); } static FileStatus fileStatus(File f) { return nu(FileStatus.class, "path" , f2s(f), "time" , f.lastModified(), "size" , fileLength(f), "isDir" , f.isDirectory()); } static File conceptsFile(String progID) { return getProgramFile(progID, conceptsFileName()); } static File conceptsFile() { return conceptsFile(dbProgramID()); } static boolean web_nodesContainText(Collection l, String text) { if (l != null) for (WebNode n : l) if (web_hasText(n, text)) return true; return false; } 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 List ai_filterBadWebs(List _webs) { List webs = emptyListWithCapacity(_webs); int dropped = 0, dropped2 = 0; for (Web web : _webs) if (web_nodesTooLong(web)) dropped++; else if (!web_tripelizable(web)) dropped2++; else webs.add(web); if (dropped != 0) print("Dropped " + nWeb(dropped) + " with too long nodes"); if (dropped2 != 0) print("Dropped " + n(dropped2, "non-triple")); return webs; } static List allWebs() { return concatWebLists(allWebs_fullList()); } static MultiMap ai_indexWebs(Collection webs) { if (hasMultiCore() && l(webs) >= 2000) return ai_indexWebs_parallel2(webs); return ai_indexWebs_serial(webs); } static MultiMap ai_indexWebs_serial(Collection webs) { MultiMap index = ai_emptyWebIndex(); for (Web web : unnull(webs)) ai_addWebToIndex(index, web); return index; } static
SyncListMultiMap caseInsensitiveSyncListMultiMap() { SyncListMultiMap mm = new SyncListMultiMap(); mm.data = caseInsensitiveMap(); return mm; } static boolean makeMultiMapsInnerNavigableMapSynchronized(MultiMap mm) { if (!startsWith(className(mm.data), "java.util.Collections$")) { mm.data = synchroNavigableMap((NavigableMap) mm.data); return true; } return false; } static Map> parse3_cachedInput_cache = synchronizedMRUCache(1000); static List parse3_cachedInput(String s) { List tok = parse3_cachedInput_cache.get(s); if (tok == null) parse3_cachedInput_cache.put(s, tok = parse3(s)); return tok; } static Map> parse3_cachedPattern_cache = synchronizedMRUCache(1000); static synchronized List parse3_cachedPattern(String s) { List tok = parse3_cachedPattern_cache.get(s); if (tok == null) parse3_cachedPattern_cache.put(s, tok = parse3(s)); return tok; } // 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 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 boolean isMultiCore() { return numberOfCores() > 1; } static volatile int numberOfCores_value; static int numberOfCores() { if (numberOfCores_value == 0) numberOfCores_value = Runtime.getRuntime().availableProcessors(); return numberOfCores_value; } static List emptyListWithCapacity(Collection c) { return new ArrayList(l(c)); } static List emptyListWithCapacity(int n) { return new ArrayList(n); } static List> sortBySecondOfPairs_inPlace(List> l) { sort(l, new Comparator>() { public int compare(Pair a, Pair b) { return stdcompare(a.b, b.b); } }); return l; } static Iterator chainIterators(Collection> l) { final List> _l = new LinkedList(l); return iteratorFromFunction(new F0() { A get() { try { while (nempty(_l)) { if (first(_l).hasNext()) return first(_l).next(); _l.remove(0); } return null; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "while (nempty(_l)) {\r\n if (first(_l).hasNext())\r\n ret first(_l).n..."; }}); } static List earlierPartsOfLogFile(File file) { String name = file.getName() + ".part"; try { Matches m = new Matches(); TreeMap map = new TreeMap(); for (File p : listFiles(file.getParent())) { try { String n = p.getName(); if (startsWith(n, name, m)) map.put(parseFirstInt(m.rest()), p); } catch (Throwable __e) { _handleException(__e); }} return valuesList(map); } catch (Throwable e) { _handleException(e); return ll(); } } static boolean isProperlyQuoted(String s) { return s.length() >= 2 && s.startsWith("\"") && s.endsWith("\"") && (!s.endsWith("\\\"") || s.endsWith("\\\\\"")) && !containsNewLine(s); } static IterableIterator iteratorFromFunction_f0(final F0 f) { class IFF2 extends 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 = f.get(); done = a == null; } }; return new IFF2(); } static Producer javaTokC_noMLS_iterator(final String s) { return javaTokC_noMLS_iterator(s, 0); } static Producer javaTokC_noMLS_iterator(final String s, final int startIndex) { return new Producer() { final int l = s.length(); int i = startIndex; 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 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' : i == '\0' ? '_' // shouldn't happen anymore : (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 String unquoteUsingCharArray(String s, char[] buf) { if (s == null) return null; if (startsWith(s, '[')) { int i = 1; while (i < s.length() && s.charAt(i) == '=') ++i; if (i < s.length() && s.charAt(i) == '[') { String m = s.substring(1, i); if (s.endsWith("]" + m + "]")) return s.substring(i+1, s.length()-i-1); } } if (s.length() > 1) { char c = s.charAt(0); if (c == '\"' || c == '\'') { int l = endsWith(s, c) ? s.length()-1 : s.length(); if (l > buf.length) return unquote(s); // fallback int n = 0; for (int i = 1; i < l; i++) { char ch = s.charAt(i); if (ch == '\\') { char nextChar = (i == l - 1) ? '\\' : s.charAt(i + 1); // 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++; } } buf[n++] = (char) Integer.parseInt(code, 8); continue; } switch (nextChar) { case '\"': ch = '\"'; break; case '\\': ch = '\\'; break; case 'b': ch = '\b'; break; case 'f': ch = '\f'; break; case 'n': ch = '\n'; break; case 'r': ch = '\r'; break; case 't': ch = '\t'; break; case '\'': ch = '\''; break; // 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); char[] x = Character.toChars(code); int lx = x.length; for (int j = 0; j < lx; j++) buf[n++] = x[j]; i += 5; continue; default: ch = nextChar; // added by Stefan } i++; } buf[n++] = ch; } return new String(buf, 0, n); } } return s; // not quoted - return original } 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 internIfLongerThan(String s, int l) { return s == null ? null : l(s) >= l ? intern(s) : s; } 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 float parseFloat(String s) { return Float.parseFloat(s); } static boolean isLongConstant(String s) { if (!s.endsWith("L")) return false; s = s.substring(0, l(s)-1); return isInteger(s); } static List parseList(String s) { return (List) safeUnstructure(s); } static SortedMap synchronizedTreeMap() { return synchroTreeMap(); } static Map synchronizedMap() { return synchroMap(); } static Map synchronizedMap(Map map) { return synchroMap(map); } static byte[] hexToBytes(String s) { if (odd(l(s))) throw fail("Hex string has odd length: " + quote(shorten(10, s))); int n = l(s) / 2; byte[] bytes = new byte[n]; for (int i = 0; i < n; i++) { int a = parseHexChar(s.charAt(i*2)); int b = parseHexChar(s.charAt(i*2+1)); if (a < 0 || b < 0) throw fail("Bad hex byte: " + quote(substring(s, i*2, i*2+2)) + " at " + i*2 + "/" + l(s)); bytes[i] = (byte) ((a << 4) | b); } return bytes; } 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 assertIdentifier(String s) { return assertIsIdentifier(s); } static String assertIdentifier(String msg, String s) { return assertIsIdentifier(msg, s); } static A nuStubInnerObject(Class c) { return nuStubInnerObject(c, null); } static A nuStubInnerObject(Class c, Object classFinder) { try { Class outerType = getOuterClass(c, classFinder); Constructor m = c.getDeclaredConstructor(outerType); m.setAccessible(true); return (A) m.newInstance(new Object[] {null}); } catch (Exception __e) { throw rethrow(__e); } } static Map nuEmptyObject_cache = newDangerousWeakHashMap(); static A nuEmptyObject(Class c) { try { Constructor ctr; synchronized(nuEmptyObject_cache) { ctr = nuEmptyObject_cache.get(c); if (ctr == null) { nuEmptyObject_cache.put(c, ctr = nuEmptyObject_findConstructor(c)); ctr.setAccessible(true); } } try { return (A) ctr.newInstance(); } catch (InstantiationException e) { if (empty(e.getMessage())) if ((c.getModifiers() & Modifier.ABSTRACT) != 0) throw fail("Can't instantiate abstract class " + className(c), e); else throw fail("Can't instantiate " + className(c), e); else throw rethrow(e); } } catch (Exception __e) { throw rethrow(__e); } } 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 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 void setOptAllDyn(DynamicObject o, Map fields) { if (fields == null) return; HashMap fieldMap = instanceFieldsMap(o); for (Map.Entry e : fields.entrySet()) { String field = e.getKey(); Object val = e.getValue(); boolean has = fieldMap.containsKey(field); if (has) setOpt(o, field, val); else { o.fieldValues.put(intern(field), val); } } } static void setOptAll_pcall(Object o, Map fields) { if (fields == null) return; for (String field : keys(fields)) try { setOpt(o, field, fields.get(field)); } catch (Throwable __e) { print(exceptionToStringShort(__e)); } } static void setOptAll_pcall(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]; try { setOpt(o, field, value); } catch (Throwable __e) { print(exceptionToStringShort(__e)); } } } static void pcallOpt_noArgs(Object o, String method) { try { callOpt_noArgs(o, method); } catch (Throwable __e) { _handleException(__e); } } 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 TreeSet ciSet() { return caseInsensitiveSet(); } 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 boolean warn_on = true; static ThreadLocal> warn_warnings = new ThreadLocal(); 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); addToCollection(warn_warnings.get(), s); } static boolean web_tripelizableNode(WebNode n) { return web_countLabels(n) == 1 && (n.visInfo() == null || eq(n.visInfo(), "")); } static String web_sym(WebNode node) { return node == null ? null : symbol(node.text()); } static boolean matchDollarVarIC(Map match, String pat, String s) { if (isDollarVar(pat)) return strictPutIC(match, pat, s); else return eqic(pat, s); } static A jenableUndoRedo(final A textcomp) { { swing(new Runnable() { public void run() { try { final UndoManager undo = new UndoManager(); vm_generalWeakSet("Undo Managers").add(undo); 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"); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "final new UndoManager undo;\r\n vm_generalWeakSet(\"Undo Managers\").add(undo)..."; }}); } return textcomp; } static A swingNu(final Class c, final Object... args) { return swingConstruct(c, args); } static boolean containsNewLine(String s) { return contains(s, '\n'); // screw \r, nobody needs it } static List replace(List l, A a, A b) { for (int i = 0; i < l(l); i++) if (eq(l.get(i), a)) l.set(i, b); return l; } static String replace(String s, String a, String b) { return s == null ? null : a == null || b == null ? s : s.replace(a, b); } static String replace(String s, char a, char b) { return s == null ? null : s.replace(a, b); } 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); } // 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 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 conceptsFileName() { return "concepts.structure.gz"; } static String dbProgramID() { return getDBProgramID(); } 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); } // returns true if web contains nodes that are too long static boolean web_nodesTooLong(Web web) { if (web == null) return false; int max = ai_maxNodeTextLength(); for (WebNode n : web_nodesAndRelations(web)) for (String s : web_texts(n)) if (l(s) > max) return true; return false; } static boolean web_tripelizable(Web web) { return ai_webToTriple(web) != null; } static String nWeb(long i) { return nWebs(i); } static String nWeb(Collection c) { return nWebs(c); } 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 String n(MultiSet ms, String name) { return n(l(ms), name); } static List concatWebLists(Collection... lists) { return uniqueByField_favorLast(concatLists(lists), "globalID"); } static boolean allWebs_transientOnly; static List allWebs_fullList() { if (allWebs_transientOnly) return transientWebs(); return concatLists(transientWebs(), downloadedWebs(), localSoftwareMadeWebs(), localUserWebEdits(), localWebs()); } static boolean hasMultiCore() { return isMultiCore(); } static MultiMap ai_indexWebs_parallel2(Collection webs) { try { print("Making index in parallel."); long _startTime_588 = sysNow(); List> indexes = parallelMap(chunksOf1000(webs), "ai_indexWebs_serial"); done2_always("Parallel indexing", _startTime_588); long _startTime_589 = sysNow(); MultiMap index = caseInsensitiveSyncListMultiMap(); for (MultiMap i : indexes) index.putAll(i); done2_always("Tying up", _startTime_589); return index; } catch (Exception __e) { throw rethrow(__e); } } static NavigableMap synchroNavigableMap(NavigableMap map) { return (NavigableMap) call(Collections.class, "synchronizedNavigableMap", map); } static List parse3(String s) { return dropPunctuation(javaTokPlusPeriod(s)); } static boolean equalsIgnoreCase(String a, String b) { return eqic(a, b); } static boolean equalsIgnoreCase(char a, char b) { return eqic(a, b); } 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 int parseFirstInt(String s) { return parseInt(jextract("", s)); } static List valuesList(Map map) { return cloneListSynchronizingOn(values(map), map); } static List valuesList(MultiMap mm) { return mm == null ? emptyList() : concatLists(values(mm.data)); } static String quickSubstring(String s, int i, int j) { if (i == j) return ""; return s.substring(i, j); } static NavigableMap synchroTreeMap() { return Collections.synchronizedNavigableMap(new TreeMap()); } static int parseHexChar(char c) { if (c >= '0' && c <= '9') return charDiff(c, '0'); if (c >= 'a' && c <= 'f') return charDiff(c, 'a')+10; if (c >= 'A' && c <= 'F') return charDiff(c, 'A')+10; return -1; } static String assertIsIdentifier(String s) { if (!isIdentifier(s)) throw fail("Not an identifier: " + quote(s)); return s; } static String assertIsIdentifier(String msg, String s) { if (!isIdentifier(s)) throw fail(msg + " - Not an identifier: " + quote(s)); return s; } static Class getOuterClass(Class c) { return getOuterClass(c, null); } static Class getOuterClass(Class c, Object classFinder) { try { String s = c.getName(); int i = s.lastIndexOf('$'); String name = substring(s, 0, i); if (classFinder != null) return (Class) callF(classFinder, name); return Class.forName(name); } catch (Exception __e) { throw rethrow(__e); } } static HashMap instanceFieldsMap(Object o) { Class c = o.getClass(); HashMap map; synchronized(getOpt_cache) { map = getOpt_cache.get(c); if (map == null) map = getOpt_makeCache(c); } return map; } static Map> callOpt_noArgs_cache = newDangerousWeakHashMap(); 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 && !reflection_isForbiddenMethod(m)) { 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 boolean addToCollection(Collection c, A a) { return c != null && c.add(a); } static int web_countLabels(WebNode n) { return web_numberOfLabels(n); } static boolean strictPutIC(Map map, A key, String value) { if (!(map != null && key != null && value != null)) return true; String old = map.get(key); if (old == null) { map.put(key, value); return true; } return eqic(old, value); } static Set vm_generalWeakSet(Object name) { synchronized(get(javax(), "generalMap")) { Set set = (Set) (vm_generalMap_get(name)); if (set == null) vm_generalMap_put(name, set = newWeakHashSet()); return set; } } static int ai_maxNodeTextLength = 65536; static int ai_maxNodeTextLength() { return ai_maxNodeTextLength; } static Map singular_specials = litmap( "children", "child", "images", "image", "chess", "chess"); static Set singular_specials2 = litset("time", "machine", "line"); static String singular(String s) { if (s == null) return null; { String _a_297 = singular_specials.get(s); if (!empty(_a_297)) return _a_297; } //try answer hippoSingulars().get(lower(s)); if (singular_specials2.contains(dropSuffix("s", afterLastSpace(s)))) return dropSuffix("s", s); if (s.endsWith("ness")) return s; if (s.endsWith("ges")) return dropSuffix("s", s); if (endsWith(s, "bases")) return dropLast(s); s = dropSuffix("es", s); s = dropSuffix("s", s); return s; } 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 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 List transientWebs_list = synchroList(); static List transientWebs() { return transientWebs_list; } static List downloadedWebs() { /*ret concatLists(downloadedTypicalWebs(), downloadedWebsFromFileNamed("Webs from program #1011161 on tvejysmllsmz.gz"));*/ return downloadedTypicalWebs(); } static List localSoftwareMadeWebs() { List webs = new ArrayList(); for (String progID : localSoftwareMadeWebs_programsToRead()) { try { webs.addAll(websMadeByProgram(progID)); } catch (Throwable __e) { _handleException(__e); }} return webs; } static List localUserWebEdits() { List webs = new ArrayList(); for (String progID : websPostedIn()) { try { if (neq(progID, "#1009961")) for (String s : scanLog(progID, "user-web-edits")) { try { webs.add(parseWeb(trim(afterSpace(s)))); } catch (Throwable __e) { _handleException(__e); }} } catch (Throwable __e) { _handleException(__e); }} return webs; } static List localWebs() { return allWebsFromCircleEditor(); } static > List parallelMap(A l, final Object f) { try { return parallelMap(iterator((Collection) l), f); } catch (Exception __e) { throw rethrow(__e); } } static List parallelMap(Iterator it, final Object f) { try { final List < Pair < Object , Integer > > out = new ArrayList(); int poolSize = coresToUse(), queueSize = 10; NotifyingBlockingThreadPoolExecutor e = new NotifyingBlockingThreadPoolExecutor(poolSize, queueSize, 15, TimeUnit.SECONDS); try { int i = 0; for (final Object o : iterable(it)) { ++i; final int _i = i; e.execute(new Runnable() { public void run() { try { try { Object x = callF(f, o); synchronized(out) { out.add(pair(x, _i)); } } catch (Throwable __e) { _handleException(__e); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pcall {\r\n Object x = callF(f, o);\r\n synchronized(out) {\r\n ..."; }}); } e.shutdown(); e.awaitTermination(1, TimeUnit.DAYS); } finally { e.shutdown(); } return firstOfPairs(sortBySecondOfPairs_inPlace(out)); } catch (Exception __e) { throw rethrow(__e); } } static List> chunksOf1000(Collection c) { List> out = new ArrayList(); Iterator it = iterator(c); List l = new ArrayList(1000); while (it.hasNext()) { l.add(it.next()); if (l(l) >= 1000) { out.add(l); l = new ArrayList(1000); } } if (nempty(l)) out.add(l); return out; } static List dropPunctuation_keep = ll("*", "<", ">"); 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))); } // 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 == (char) 0x201C || c == (char) 0x201D) c = '"'; // normalize quotes if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { char _c = s.charAt(j); if (_c == (char) 0x201C || _c == (char) 0x201D) _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; } // returns from C to C static String jextract(String pat, String s) { return jextract(pat, javaTok(s)); } static String jextract(String pat, List tok) { List tokpat = javaTok(pat); jfind_preprocess(tokpat); int i = jfind(tok, tokpat); if (i < 0) return null; int j = i + l(tokpat) - 2; return joinSubList(tok, i, j); } static int charDiff(char a, char b) { return (int) a-(int) b; } static int charDiff(String a, char b) { return charDiff(stringToChar(a), b); } static boolean reflection_isForbiddenMethod(Method m) { return m.getDeclaringClass() == Object.class && eqOneOf(m.getName(), "finalize", "clone", "registerNatives"); } static int web_numberOfLabels(WebNode n) { return n == null ? 0 : l(n.labels); } static Set newWeakHashSet() { return synchroWeakHashSet(); } static String afterLastSpace(String s) { return s == null ? null : substring(s, s.lastIndexOf(' ')+1); } static String dropSuffixIgnoreCase(String suffix, String s) { return ewic(s, suffix) ? s.substring(0, l(s)-l(suffix)) : s; } static /*cached*/ List downloadedTypicalWebs() { List webs = new ArrayList(); long _startTime_621 = sysNow(); for (String struct : downloadedTypicalDiagramStructures()) try { Web web = web_flexUnstructure(struct); webs.add(web_setSource("downloaded typical", web)); } catch (Throwable __e) { print(exceptionToStringShort(__e)); } done2_always("Unpacking webs", _startTime_621); return webs; } static List websMadeByProgram(String progID) { return websMadeByProgram(progID, null); } static List websMadeByProgram(String progID, Map furtherInfoMap) { if (hasMultipleCores()) return websMadeByProgram_parallel(progID, furtherInfoMap); List webs = ai_triplesMadeByProgram(progID); ai_loadWebsFromFile(programFile(progID, "webs-made.txt"), progID, webs, furtherInfoMap); return webs; } static List websPostedIn() { Matches m = new Matches(); List programsToScan = new ArrayList(); for (String s : listFileNames(getProgramDir(circlesEditorDBID()))) { try { if (swic(s, "webs.posted.at.", m)) programsToScan.add(fsI(m.unq(0))); } catch (Throwable __e) { _handleException(__e); }} return programsToScan; } 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 Web parseWeb(String s) { Object o = unstructure(s); if (o instanceof CirclesAndLines) return calToWeb((CirclesAndLines) o); return (Web) o; } static String afterSpace(String s) { return dropUntilSpace(s); } static List allWebsFromCircleEditor() { return map_pcall(new F1() { 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 int coresToUse() { //ret 1; return max(1, numberOfCores()-1); } static Iterable iterable(final Iterator i) { return new Iterable() { public Iterator iterator() { return i; } }; } static List firstOfPairs(Collection> l) { List out = new ArrayList(); for (Pair p : unnull(l)) out.add(firstOfPair(p)); return out; } static List nlTok(String s) { return javaTokPlusPeriod(s); } static char stringToChar(String s) { if (l(s) != 1) throw fail("bad stringToChar: " + s); return firstChar(s); } static Set synchroWeakHashSet() { return Collections.newSetFromMap((Map) newWeakHashMap()); } static List downloadedTypicalDiagramStructures() { Map map = safeUnstructureMap(loadTextFile(getProgramFile("#1010484", "typical.structure"))); if (map == null) return ll(); return downloadedDiagramStructures(map(values(map), new F1() { File get(String md5) { try { return getProgramFile("#1010484", md5); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "getProgramFile(#1010484, md5)"; }})); } 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 Web web_setSource(String source, Web web) { web.source = source; return web; } static List websMadeByProgram_parallel(String progID) { return websMadeByProgram_parallel(progID, null); } static List websMadeByProgram_parallel(String progID, final Map furtherInfoMap) { try { // TODO: clean up file handle in case of error IterableIterator l = scanLog_iterator(progID, "webs-made.txt"); final String src = progID; final List < Pair < Web , Integer > > webs = new ArrayList(); int poolSize = numberOfCores(), queueSize = 20; NotifyingBlockingThreadPoolExecutor e = new NotifyingBlockingThreadPoolExecutor(poolSize, queueSize, 15, TimeUnit.SECONDS); try { int i = 0; for (final String s : l) { ++i; final int _i = i; e.execute(new Runnable() { public void run() { try { try { SoftwareMadeWeb o = (SoftwareMadeWeb) (unstructure(s)); Web web = o.web; if (web == null) web = (Web) unstructure(o.structure); if (web != null) synchronized(webs) { webs.add(pair(web_intern(web_setSourceIfEmpty(web, src)), _i)); mapPut(furtherInfoMap, web, o); } } catch (Throwable __e) { _handleException(__e); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pcall {\r\n SoftwareMadeWeb o = (SoftwareMadeWeb) (unstructure(s));\r\n ..."; }}); } //e.await(); // ConcurrentModificationException sometimes?? e.shutdown(); e.awaitTermination(1, TimeUnit.DAYS); } finally { e.shutdown(); } long _startTime_645 = sysNow(); sortBySecondOfPairs_inPlace(webs); done2_always("Sort", _startTime_645); return concatLists(ai_triplesMadeByProgram(progID), firstOfPairs(webs)); } catch (Exception __e) { throw rethrow(__e); } } static List ai_triplesMadeByProgram(String progID) { return webs_readTripleFile(getProgramFile(progID, "triples.gz")); } static List listFileNames(File dir) { return asList(dir.list()); } static List listFileNames(String dir) { return listFileNames(new File(dir)); } 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 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 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) { _handleException(__e); }} return x; } static List map_pcall(Object f, Object[] l) { List x = new ArrayList(); for (Object o : unnull(l)) { try { x.add(callF(f, o)); } catch (Throwable __e) { _handleException(__e); }} return x; } static List map_pcall(Iterable l, IF1 f) { List x = emptyList(l); if (l != null) for (A o : l) { try { x.add(f.get(o)); } catch (Throwable __e) { _handleException(__e); }} return x; } static List reversed(Collection l) { return reversedList(l); } static List reversed(A[] l) { return reversedList(asList(l)); } static String reversed(String s) { return reversedString(s); } static List list(Class type) { return db_mainConcepts().list(type); } static List list(Concepts concepts, Class type) { return concepts.list(type); } static List list(String type) { return db_mainConcepts().list(type); } static List list(Concepts concepts, String type) { return concepts.list(type); } static A firstOfPair(Pair p) { return p == null ? null : p.a; } static char firstChar(String s) { return s.charAt(0); } static Map safeUnstructureMap(String s) { return (Map) safeUnstructure(s); } 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 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 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)); } static String getString(String key, Object o) { return getString(o, (Object) key); } static CirclesAndLines cal_unstructure(String s) { return (CirclesAndLines) unstructure(s); } static List webs_readTripleFile(File f) { // parallel isn't even faster //if (hasMultipleCores()) ret webs_readTripleFile_parallel(f); if (!f.exists()) return ll(); Iterator it = linesFromFile(f); //L names = (L) unstructure(it.next()); List names = new ArrayList(); while (it.hasNext()) { String s = trim(it.next()); if (empty(s)) break; names.add(unquote(s)); } List webs = new ArrayList(); while (it.hasNext()) { String s = it.next(); try { addIfNotNull(webs, webs_readTripleFile_line(s, names)); } catch (Throwable __e) { _handleException(__e); } } return webs; } static Web webs_readTripleFile_line(String s, List names) { // S moreInfo = quote(unnull(web.globalID)) + " " + quote(unnull(web.title)) + " " + quote(unnull(web.source)) + " " + (web.unverified ? "u" : "v") + " " + web.created; List l = javaTokC(s); if (l(l) == 8) { Web web = webFromTriple( names.get(parseInt(first(l))), names.get(parseInt(second(l))), names.get(parseInt(third(l)))); web.globalID = unquote(l.get(3)); web.title = unquote(l.get(4)); web.source = unquote(l.get(5)); web.unverified = !eq(l.get(6), "v"); web.created = parseLong(l.get(7)); return web_intern(web); } return null; } static Web web_useCLParse(Web web, boolean b) { web.useCLParse = b; return web; } static List reversedList(Collection l) { List x = cloneList(l); Collections.reverse(x); return x; } static String reversedString(String s) { return reverseString(s); } static List listFilesOfProgram_notDirs(String programID) { return asList(listFilesOnly(programDir(programID))); } static boolean isPossibleMD5(String s) { return isMD5(s); } static boolean fileStartsWith(File f, byte[] data) { return byteArrayEquals(loadBeginningOfBinaryFile(f, l(data)), data); } 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 String loadGZippedTextFile(File file) { return loadGZTextFile(file); } static List scanQuotedLogLines(String text) { List l = new ArrayList(); for (String s : toLines(text)) if (isProperlyQuoted(s)) l.add(unquote(s)); return l; } static List getFieldOfAllConceptClasses(Concepts cc, String field) { return notNullOnly(c_collect(values(cc.concepts), field)); } static A third(List l) { return _get(l, 2); } static A third(Iterable l) { if (l == null) return null; Iterator it = iterator(l); for (int _repeat_185 = 0; _repeat_185 < 2; _repeat_185++) { if (!it.hasNext()) return null; it.next(); } return it.hasNext() ? it.next() : null; } static A third(Producer p) { if (p == null) return null; for (int _repeat_186 = 0; _repeat_186 < 2; _repeat_186++) { if (p.next() == null) return null; } return p.next(); } static A third(A[] bla) { return bla == null || bla.length <= 2 ? null : bla[2]; } static C third(T3 t) { return t == null ? null : t.c; } static String reverseString(String s) { return new StringBuilder(s).reverse().toString(); } static List listFilesOnly(String dir) { return listFilesOnly(new File(dir)); } static List listFilesOnly(File... dirs) { return concatMap(rcurry("listFilesWithSuffix", ""), dirs); } 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 byte[] loadBeginningOfBinaryFile(File file, int maxBytes) { return loadBinaryFilePart(file, 0, maxBytes); } static byte[] bytesFromHex(String s) { return hexToBytes(s); } 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 String loadGZTextFile(File file) { try { if (!file.isFile()) return null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream fis = new FileInputStream(file); GZIPInputStream gis = newGZIPInputStream(fis); try { byte[] buffer = new byte[1024]; int len; while((len = gis.read(buffer)) != -1){ baos.write(buffer, 0, len); } } finally { fis.close(); } baos.close(); return fromUtf8(baos.toByteArray()); // TODO: use a Reader } catch (Exception __e) { throw rethrow(__e); } } 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 A _get(List l, int idx) { return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null; } static Object _get(Object o, String field) { return get(o, field); } static Object _get(String field, Object o) { return get(o, field); } static A _get(A[] l, int idx) { return idx >= 0 && idx < l(l) ? l[idx] : null; } 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; } // 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 List concatMap(Object[] l, Object f) { return concatMap(f, l); } 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 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); } } // magic cast static A cget(Object c, String field) { Object o = getOpt(c, field); if (o instanceof Concept.Ref) return (A) ((Concept.Ref) o).get(); return (A) o; } static A cget(String field, Object c) { return cget(c, field); } static int numberOfFunctionArguments(Object f) { if (f instanceof F0) return 0; if (f instanceof F1) return 1; if (f instanceof F2) return 2; if (f instanceof VF1) return 1; if (f instanceof VF2) return 2; if (f instanceof String) return numberOfMethodArguments(mc(), (String) f); return numberOfMethodArguments(f, "get"); } static RuntimeException todo() { throw new RuntimeException("TODO"); } static RuntimeException todo(String msg) { throw new RuntimeException("TODO: " + msg); } static int numberOfMethodArguments(Object o, String method) { Class c; boolean mustBeStatic = false; if (o instanceof Class) { c = (Class) o; mustBeStatic = true; } else c = o.getClass(); Class _c = c; int n = -1; while (c != null) { for (Method m : c.getDeclaredMethods()) { if (!m.getName().equals(method)) continue; if (mustBeStatic && !methodIsStatic(m)) continue; int nn = l(m.getParameterTypes()); if (n == -1) n = nn; else if (n != nn) throw fail("Variable number of method arguments: " + _c + "." + method); } c = c.getSuperclass(); } if (n == -1) throw fail("Method not found: " + _c + "." + method); return n; } static abstract class VF1 implements IVF1 { public abstract void get(A a); }static class FileStatus { String path; long time, size; boolean isDir; public boolean equals(Object o) { return stdEq2(this, o); } public int hashCode() { return stdHash2(this); } }static abstract class VF2 { abstract void get(A a, B b); }static class CombinedList extends RandomAccessAbstractList { int size; List> lists = new ArrayList(); CombinedList() {} void addList(List l) { lists.add(l); size += l(l); } public int size() { return size; } public A get(int index) { int idx = 0; for (List l : lists) { int j = idx+l(l); if (index < j) return l.get(index-idx); idx = j; } throw new NoSuchElementException(); } }static class Var implements IVar { Var() {} Var(A v) { this.v = v;} A v; // you can access this directly if you use one thread 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 DefunctClassLoader {}static class GlobalThoughtSpace extends AbstractThoughtSpace { // existence of triple has already been checked public GlobalID postTriple(T3 t, boolean verified) { Boolean prev = setThreadLocal(ai_postTriple_verified, verified); try { return asGlobalID(post(str(t.a), str(t.b), str(t.c))); } finally { setThreadLocal(ai_postTriple_verified, prev); } } public GlobalID postTriple(T3 t) { return asGlobalID(post(str(t.a), str(t.b), str(t.c))); } List> get(String s) { return tripleIndex().getTripleRefs(s); } List> get(String s, int position) { return tripleIndex().getTripleRefs(s, position); } List getTriples(String s) { return tripleIndex().getTriples(s); } List getTriples(String s, int position) { return tripleIndex().getTriples(s, position); } List getOneTwo(String a, String b) { return tripleIndex().getOneTwo(a, b); } int size() { return tripleIndex().numWebs(); } }static abstract class TokCondition { abstract boolean get(List tok, int i); // i = N Index }static class NotifyingBlockingThreadPoolExecutor extends ThreadPoolExecutor { private AtomicInteger tasksInProcess = new AtomicInteger(); private Synchronizer synchronizer = new Synchronizer(); public NotifyingBlockingThreadPoolExecutor(int poolSize, int queueSize, long keepAliveTime, TimeUnit keepAliveTimeUnit, long maxBlockingTime, TimeUnit maxBlockingTimeUnit, Callable blockingTimeCallback) { super(poolSize, // Core size poolSize, // Max size keepAliveTime, keepAliveTimeUnit, new ArrayBlockingQueue(Math.max(poolSize, queueSize)), new BlockThenRunPolicy(maxBlockingTime, maxBlockingTimeUnit, blockingTimeCallback)); super.allowCoreThreadTimeOut(true); } public NotifyingBlockingThreadPoolExecutor(int poolSize, int queueSize, long keepAliveTime, TimeUnit unit) { super(poolSize, // Core size poolSize, // Max size keepAliveTime, unit, new ArrayBlockingQueue(Math.max(poolSize, queueSize)), // not smaller than the poolSize (to avoid redundant threads) new BlockThenRunPolicy()); // When super invokes the reject method this class will ensure a blocking try. super.allowCoreThreadTimeOut(true); // Time out the core threads. } @Override public void execute(Runnable task) { // count a new task in process tasksInProcess.incrementAndGet(); try { super.execute(task); } catch(RuntimeException e) { // specifically handle RejectedExecutionException tasksInProcess.decrementAndGet(); throw e; } catch(Error e) { tasksInProcess.decrementAndGet(); throw e; } } @Override protected void afterExecute(Runnable r, Throwable t) { super.afterExecute(r, t); synchronized(this) { tasksInProcess.decrementAndGet(); if (tasksInProcess.intValue() == 0) { synchronizer.signalAll(); } } } @Override public void setCorePoolSize(int corePoolSize) { super.setCorePoolSize(corePoolSize); super.setMaximumPoolSize(corePoolSize); } @Override public void setMaximumPoolSize(int maximumPoolSize) { throw new UnsupportedOperationException("setMaximumPoolSize is not supported."); } public void setRejectedExecutionHandler(RejectedExecutionHandler handler) { throw new UnsupportedOperationException("setRejectedExecutionHandler is not allowed on this class."); } public void await() throws InterruptedException { synchronizer.await(); } public boolean await(long timeout, TimeUnit timeUnit) throws InterruptedException { return synchronizer.await(timeout, timeUnit); } private class Synchronizer { private final Lock lock = new ReentrantLock(); private final Condition done = lock.newCondition(); private boolean isDone = false; private void signalAll() { lock.lock(); try { isDone = true; done.signalAll(); } finally { lock.unlock(); } } public void await() throws InterruptedException { lock.lock(); try { while (!isDone) { done.await(); } } finally { isDone = false; lock.unlock(); } } public boolean await(long timeout, TimeUnit timeUnit) throws InterruptedException { boolean await_result = false; lock.lock(); boolean localIsDone; try { await_result = done.await(timeout, timeUnit); } finally { localIsDone = isDone; isDone = false; lock.unlock(); } return await_result && localIsDone; } } private static class BlockThenRunPolicy implements RejectedExecutionHandler { private long maxBlockingTime; private TimeUnit maxBlockingTimeUnit; private Callable blockingTimeCallback; public BlockThenRunPolicy(long maxBlockingTime, TimeUnit maxBlockingTimeUnit, Callable blockingTimeCallback) { this.maxBlockingTime = maxBlockingTime; this.maxBlockingTimeUnit = maxBlockingTimeUnit; this.blockingTimeCallback = blockingTimeCallback; } public BlockThenRunPolicy() { } @Override public void rejectedExecution(Runnable task, ThreadPoolExecutor executor) { BlockingQueue workQueue = executor.getQueue(); boolean taskSent = false; while (!taskSent) { if (executor.isShutdown()) { throw new RejectedExecutionException( "ThreadPoolExecutor has shutdown while attempting to offer a new task."); } try { if(blockingTimeCallback != null) { if (workQueue.offer(task, maxBlockingTime, maxBlockingTimeUnit)) { taskSent = true; } else { // task was not accepted - call the Callback Boolean result = null; try { result = blockingTimeCallback.call(); } catch(Exception e) { throw new RejectedExecutionException(e); } if(result == false) { throw new RejectedExecutionException("User decided to stop waiting for task insertion"); } else { continue; } } } else { workQueue.put(task); taskSent = true; } } catch (InterruptedException e) { } } } } }static class GlobalID { // We need 76 bits for 26^16 IDs long a; // all bits used int b; // 76-64=12 bits used; could be short. change to short when unstructure() is smarter GlobalID() {} GlobalID(String id) { assertGlobalID(id); BigInteger value = bigint(0); for (int i = 0; i < l(id); i++) value = plus(mul(value, 26), charDiff(id.charAt(i), 'a')); a = value.longValue(); value = value.shiftRight(64); b = value.shortValue(); } public String toString() { BigInteger value = bigint(b); value = value.shiftLeft(32); value = plus(value, (a >> 32) & 0xFFFFFFFFL); value = value.shiftLeft(32); value = plus(value, a & 0xFFFFFFFFL); return bigintToGlobalID(value); } public boolean equals(Object o) { if (!(o instanceof GlobalID)) return false; return ((GlobalID) o).a == a && ((GlobalID) o).b == b; } public int hashCode() { return (int) a; } }// f is a function or an instance of Cache (the dependee) static class DependentCache extends Cache { Object depF, lastDep; DependentCache() {} DependentCache(Object maker, Object depF) { this.depF = depF; this.maker = maker;} A get() { if (hasLock(lock)) return value; // Must be called from within maker { Lock __703 = lock; lock(__703); try { Object dep; if (depF instanceof Cache) dep = ((Cache) depF).changeCount; else { dep = callF(depF); if (dep instanceof Cache) dep = ((Cache) dep).changeCount; } if (neq(dep, lastDep)) { clear(); lastDep = dep; } } finally { unlock(__703); } } return super.get(); } }static interface AI_CacheChecker { boolean hasTriple(String a, String b, String c); }static class Best { A best; double score; transient Object onChange; transient Object stringifier; // func(A) -> S synchronized boolean isNewBest(double score) { return best == null || !isNaN(score) && score > this.score; } synchronized double bestScore() { return best == null ? minusInfinity() : score; } double score() { return bestScore(); } double getScore() { return bestScore(); } synchronized float floatScoreOr(float defaultValue) { return best == null ? defaultValue : (float) score; } boolean put(Pair p) { return p != null && put(p.a, p.b); } boolean put(A a, double score) { ping(); boolean change = false; if (a != null) synchronized(this) { if (isNewBest(score)) { best = a; this.score = score; change = true; } } if (change) pcallF(onChange); return change; } synchronized A get() { return best; } synchronized boolean has() { return best != null; } synchronized Pair pair() { return main.pair(best, bestScore()); } synchronized A getIfScoreAbove(double x) { return score() >= x ? best : null; } public String toString() { return "Score " + formatDouble_significant2(score, 4) + ": " + callStringifier(stringifier, best); } boolean putAndPrintIfNewBest(A a, double score) { if (!put(a, score)) return false; { print(this); return true; } } }static class FixedRateTimer extends java.util.Timer { FixedRateTimer() { this(false); } FixedRateTimer(boolean daemon) { this(defaultTimerName(), daemon); } FixedRateTimer(String name) { this(name, false); } FixedRateTimer(String name, boolean daemon) { super(name, daemon); _registerTimer(this); } List entries = synchroList(); static class Entry { static String _fieldOrder = "task firstTime period"; TimerTask task; long firstTime; long period; Entry() {} Entry(TimerTask task, long firstTime, long period) { this.period = period; this.firstTime = firstTime; this.task = task;} public String toString() { return "Entry(" + task + ", " + firstTime + ", " + period + ")"; }} // Note: not all methods overridden; only use these ones public void scheduleAtFixedRate(TimerTask task, long delay, long period) { entries.add(new Entry(task, now()+delay, period)); super.scheduleAtFixedRate(task, delay, period); } public void cancel() { entries.clear(); super.cancel(); } public int purge() { entries.clear(); return super.purge(); } FixedRateTimer changeRate(int newPeriod) { Object r = ((SmartTimerTask) first(entries).task).r; cancel(); return doEvery(newPeriod, r); } }static class TripleWeb extends T3 { long globalID1; short globalID2; byte flags; long created; // full time stamp //S source; String source() { return null; } // overridable Object result() { return null; } // overridable static final byte UNVERIFIED = 1; final GlobalID globalID() { return globalIDFromParts(globalID1, globalID2); } final void globalID(GlobalID id) { globalID1 = globalIDPart1(id); globalID2 = globalIDPart2(id); } final void globalID(String id) { globalID(asGlobalID(id)); } final public int hashCode() { return (int) globalID1; } final public boolean equals(Object o) { return o instanceof TripleWeb && globalID1 == ((TripleWeb) o).globalID1 && globalID2 == ((TripleWeb) o).globalID2; } final boolean unverified() { return (flags & UNVERIFIED) != 0; } final boolean verified() { return !unverified(); } final void verified(boolean v) { flags = v ? 0 : UNVERIFIED; } public String toString() { return "[" + globalID() + "] " + super.toString(); } final long created() { return created; } final void created(long created) { this.created = created; } }// 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"); } } // Copyright (c) 2017 Pierantonio Cangianiello (original name: SelfExpiringHashMap) // thread-safe. static class ExpiringHashMap implements Map { final Map internalMap; final Map> expiringKeys; final DelayQueue delayQueue = new DelayQueue(); long maxLifeTimeMillis; boolean renewOnGet, renewOnOverwrite = true; ExpiringHashMap() { this(Long.MAX_VALUE); } ExpiringHashMap(long defaultMaxLifeTimeMillis) { internalMap = new ConcurrentHashMap(); expiringKeys = new WeakHashMap>(); this.maxLifeTimeMillis = defaultMaxLifeTimeMillis; } public int size() { cleanup(); return internalMap.size(); } public boolean isEmpty() { cleanup(); return internalMap.isEmpty(); } public boolean containsKey(Object key) { cleanup(); return internalMap.containsKey((K) key); } public boolean containsValue(Object value) { cleanup(); return internalMap.containsValue((V) value); } public V get(Object key) { cleanup(); if (renewOnGet) renewKey((K) key); return internalMap.get((K) key); } public V put(K key, V value) { return this.put(key, value, maxLifeTimeMillis); } public V put(K key, V value, long lifeTimeMillis) { cleanup(); ExpiringKey delayedKey = new ExpiringKey(key, lifeTimeMillis); ExpiringKey oldKey = expiringKeys.put((K) key, delayedKey); if(oldKey != null) { if (!renewOnOverwrite) { delayedKey.startTime = oldKey.startTime; delayedKey.maxLifeTimeMillis = oldKey.maxLifeTimeMillis; } expireKey(oldKey); expiringKeys.put((K) key, delayedKey); } delayQueue.offer(delayedKey); return internalMap.put(key, value); } public V remove(Object key) { V removedValue = internalMap.remove((K) key); expireKey(expiringKeys.remove((K) key)); return removedValue; } public void putAll(Map m) { throw new UnsupportedOperationException(); } public boolean renewKey(K key) { ExpiringKey delayedKey = expiringKeys.get((K) key); if (delayedKey != null) { delayedKey.renew(); return true; } return false; } private void expireKey(ExpiringKey delayedKey) { if (delayedKey != null) { delayedKey.expire(); cleanup(); } } public void clear() { delayQueue.clear(); expiringKeys.clear(); internalMap.clear(); } public Set keySet() { cleanup(); return internalMap.keySet(); // TODO: sync } public Collection values() { throw new UnsupportedOperationException(); } public Set> entrySet() { throw new UnsupportedOperationException(); } private void cleanup() { ExpiringKey delayedKey = delayQueue.poll(); while (delayedKey != null) { internalMap.remove(delayedKey.getKey()); expiringKeys.remove(delayedKey.getKey()); delayedKey = delayQueue.poll(); } } class ExpiringKey implements Delayed { private long startTime = System.currentTimeMillis(); private long maxLifeTimeMillis; private final K key; public ExpiringKey(K key, long maxLifeTimeMillis) { this.maxLifeTimeMillis = maxLifeTimeMillis; this.key = key; } public K getKey() { return key; } public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final ExpiringKey other = (ExpiringKey) obj; if (this.key != other.key && (this.key == null || !this.key.equals(other.key))) { return false; } return true; } public int hashCode() { int hash = 7; hash = 31 * hash + (this.key != null ? this.key.hashCode() : 0); return hash; } public long getDelay(TimeUnit unit) { return unit.convert(getDelayMillis(), TimeUnit.MILLISECONDS); } private long getDelayMillis() { return (startTime + maxLifeTimeMillis) - System.currentTimeMillis(); } public void renew() { startTime = System.currentTimeMillis(); } public void expire() { startTime = System.currentTimeMillis() - maxLifeTimeMillis - 1; } public int compareTo(Delayed that) { return Long.compare(this.getDelayMillis(), ((ExpiringKey) that).getDelayMillis()); } } ExpiringHashMap dontRenewOnOverwrite() { renewOnOverwrite = false; return this; } }static class TripleWebWithSourceAndResult extends TripleWebWithSource { Object result; TripleWebWithSourceAndResult() {} TripleWebWithSourceAndResult(String source, Object result) { this.result = result; this.source = source;} Object result() { return result; } }// Technically this violates the one-synced-object-per-thread // principle (syncing on data and on an individual). Don't think // it's a problem. static class SyncListMultiMap extends MultiMap { SyncListMultiMap() {} SyncListMultiMap(boolean useTreeMap) { super(useTreeMap); } SyncListMultiMap(Map> data) { this.data = data;} List _makeEmptyList() { return new SynchronizedArrayList(); } }static abstract class F0 { abstract A get(); }static abstract class F1 { abstract B get(A a); }// you still need to implement hasNext() and next() static abstract class IterableIterator implements Iterator, Iterable { public Iterator iterator() { return this; } public void remove() { unsupportedOperation(); } }static class Snippet { String id, title, md5, type, text; Snippet() {} Snippet(String id, String title) { this.title = title; this.id = id;} Snippet(String id, String title, String md5) { this.md5 = md5; this.title = title; this.id = id;} public String toString() { return id + " - " + title; } public boolean equals(Object o) { return stdEq2(this, o); } public int hashCode() { return stdHash2(this); } }static abstract class F2 { abstract C get(A a, B b); }static abstract class CloseableIterableIterator extends IterableIterator implements AutoCloseable { public void close() throws Exception {} }static class ExpiringMap2 extends AbstractMap { Map> byKey = new HashMap(); // key -> pair(expiry sys time, value) PriorityBlockingQueue < Pair < Long , A > > queue = new PriorityBlockingQueue(); // queue(pair(expiry sys time, key)) long standardExpiryTime; // ms boolean renewOnOverwrite = true, renewOnGet; Object onChange; // new RestartableCountdown countdown; // TODO ExpiringMap2() {} ExpiringMap2(long standardExpiryTime) { this.standardExpiryTime = standardExpiryTime;} ExpiringMap2(long standardExpiryTime, Object onChange) { this.onChange = onChange; this.standardExpiryTime = standardExpiryTime;} synchronized boolean clean() { boolean changes = false; Pair p; while ((p = queue.peek()) != null && sysTime() >= p.a) { p = queue.poll(); Pair v = byKey.get(p.b); if (v != null /*&& v.a == p.a*/) { byKey.remove(p.b); changes = true; change(); } } return changes; } void change() { callF(onChange); } synchronized public B put(A a, B b) { clean(); long timeout = sysTime()+standardExpiryTime; Pair p = byKey.get(a); if (p != null && renewOnOverwrite) queue.remove(new Pair(p.a, a)); byKey.put(a, pair(timeout, b)); change(); if (p == null || renewOnOverwrite) queue.add(new Pair(timeout, a)); return pairB(p); } synchronized public B remove(Object a) { clean(); Pair p = byKey.get(a); if (p == null) return null; queue.remove(new Pair(p.a, a)); byKey.remove(a); change(); return p.b; } synchronized public B get(Object a) { clean(); Pair p = byKey.get(a); if (renewOnGet && p != null) { queue.remove(new Pair(p.a, a)); long timeout = sysTime()+standardExpiryTime; byKey.put((A) a, pair(timeout, p.b)); queue.add(new Pair(timeout, a)); } return pairB(p); } synchronized public Set> entrySet() { clean(); // TODO: mutex return synchronizedSet(mapValues("pairB", byKey).entrySet()); } synchronized public Set keySet() { clean(); return synchronizedSet(byKey.keySet()); } synchronized public int size() { clean(); return byKey.size(); } void setStandardExpiryTime(long ms) { standardExpiryTime = ms; } synchronized ExpiringMap2 setMap(Map innerMap) { byKey = innerMap; return this; } } static class TripleWebWithSource extends TripleWeb { String source; TripleWebWithSource() {} TripleWebWithSource(String source) { this.source = source;} String source() { return source; } }static interface Producer { public A next(); }static interface IF1 { B get(A a); }static class WebRelation extends WebNode { WebNode a, b; public void _setField(String f, Object x) { if (f.equals("a")) a = (WebNode) x; else if (f.equals("b")) b = (WebNode) x; else super._setField(f, x); } 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 static final MultiMap index = null; //Map> pots = new Map; //int size; //int flags; // TODO boolean useCLParse = true; boolean labelsToUpper; String title; Object globalID = aGlobalIDObj(); String source; boolean unverified; long created = nowUnlessLoading(); static List onNewNode = new ArrayList(); // L static List onNewLabel = new ArrayList(); // L 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(Pair p) { return getRelationOpt(p.a, p.b); } WebRelation getRelationOpt(WebNode a, WebNode b) { for (WebNode n : nodes) if (n instanceof WebRelation) { WebRelation r = ((WebRelation) n); if (r.a == a && r.b == b) return r; } return null; } WebRelation getRelation(Pair p) { WebRelation r = getRelationOpt(p.a, p.b); if (r == null) { 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) { if (index != null) return first(index.get(l)); for (WebNode n : nodes) if (n.hasLabel(l)) return n; return null; } 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) { if (index != null) index.put(label, n); fireNewLabel(n, label); } void clear() { clearAll(nodes, index/*, pots*/); } Lisp parseLabel(String s) { if (useCLParse) return clParse(s); return lisp(labelsToUpper ? upper(s) : s); } String unparseLabel(Lisp l) { if (useCLParse) return clUnparse(l); return lispHead(l); } List parseLabels(List l) { List x = new ArrayList(l(l)); for (String s : l) x.add(parseLabel(s)); return x; } List unparseLabels(List l) { List x = new ArrayList(l(l)); for (Lisp lbl : l) x.add(unparseLabel(lbl)); return x; } void fireNewNode(WebNode node) { for (Object f : onNewNode) pcallF(f, node); } void fireNewLabel(WebNode node, Lisp label) { for (Object f : onNewLabel) pcallF(f, node, label); } void removeNode(WebNode n) { if (n == null || !nodes.contains(n)) return; n.web = null; if (index != null) for (Lisp label : n.labels()) index.remove(label, n); nodes.remove(n); } void removeRelation(WebNode a, WebNode b) { Pair p = pair(a, b); WebNode r = getRelationOpt(p); if (r == null) return; removeNode(r); } boolean verified() { return !unverified; } String globalID() { return strOrNull(globalID); } GlobalID globalIDObj() { return globalID instanceof String ? new GlobalID((String) globalID) : (GlobalID) globalID; } void setGlobalID(String id) { globalID = asGlobalID(id); } public int hashCode() { return globalIDObj().hashCode(); } public boolean equals(Object o) { return o instanceof Web && eq(globalIDObj(), ((Web) o).globalIDObj()); } }abstract static class VirtualNodeIndex { // concepts that are used in the generated webs, // but can't be queried TreeSet implicitConcepts = new TreeSet(); abstract int numWebs(); abstract int numTerms(); abstract List get(CharSequence query); boolean hasShortTerm(String s) { return false; } Web getWeb(GlobalID id) { return null; } Collection indexedTerms() { return null; } Collection allTriples() { return null; } Set implicitConcepts() { return implicitConcepts; } }static class MechMode { Object readMode; Object writeMode; MechMode() {} MechMode(Object readMode, Object writeMode) { this.writeMode = writeMode; this.readMode = readMode;} public String toString() { return "MechMode(" + readMode + ", " + writeMode + ")"; }public boolean equals(Object o) { if (!(o instanceof MechMode)) return false; MechMode x = (MechMode) o; return eq(readMode, x.readMode) && eq(writeMode, x.writeMode); } public int hashCode() { int h = -866263200; h = h*2+_hashCode(readMode); h = h*2+_hashCode(writeMode); return h; } }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 __2040 = lock; lock(__2040); try { if (loaded == 0) { value = make(); changeCount++; loaded = sysNow(); } return value; } finally { unlock(__2040); } } void clear() { Lock __2041 = lock; lock(__2041); try { if (debug && loaded != 0) print("Clearing cache"); value = null; changeCount++; loaded = 0; } finally { unlock(__2041); } } // clear if older than x seconds // 0 does not do anything void clear(double seconds) { Lock __2042 = lock; lock(__2042); try { if (seconds != 0 && loaded != 0 && sysNow() >= loaded+seconds*1000) clear(); } finally { unlock(__2042); } } // override void set(A a) { Lock __2043 = lock; lock(__2043); try { value = a; ++changeCount; loaded = sysNow(); } finally { unlock(__2043); } } A make() { return (A) callF(maker); } }// immutable, has strong refs final static class _MethodCache { final Class c; final HashMap> cache = new HashMap(); _MethodCache(Class c) { this.c = c; _init(); } void _init() { Class _c = c; while (_c != null) { for (Method m : _c.getDeclaredMethods()) if (!reflection_isForbiddenMethod(m)) multiMapPut(cache, m.getName(), makeAccessible(m)); _c = _c.getSuperclass(); } // add default methods - this might lead to a duplication // because the overridden method is also added, but it's not // a problem except for minimal performance loss. for (Class intf : allInterfacesImplementedBy(c)) for (Method m : intf.getDeclaredMethods()) if (m.isDefault() && !reflection_isForbiddenMethod(m)) multiMapPut(cache, m.getName(), makeAccessible(m)); } // Returns only matching methods Method findMethod(String method, Object[] args) { try { List m = cache.get(method); if (m == null) return null; int n = m.size(); for (int i = 0; i < n; i++) { Method me = m.get(i); if (call_checkArgs(me, args, false)) return me; } return null; } catch (Exception __e) { throw rethrow(__e); } } Method findStaticMethod(String method, Object[] args) { try { List m = cache.get(method); if (m == null) return null; int n = m.size(); for (int i = 0; i < n; i++) { Method me = m.get(i); if (isStaticMethod(me) && call_checkArgs(me, args, false)) return me; } return null; } catch (Exception __e) { throw rethrow(__e); } } }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 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)); } public String toString() { return "Matches(" + joinWithComma(quoteAll(asList(m))) + ")"; } } 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 VF1() { public void get(Graphics2D g) { try { g.scale(scale, scale); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "g.scale(scale, scale);"; }}); // 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 F0() { 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 F2() { Object get(Integer w, Integer h) { try { return makeImage(w, h); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "makeImage(w, h)"; }}; final Canvas canvas = jcanvas(makeImg); disableImageSurfaceSelector(canvas); new CircleDragger(this, canvas, new Runnable() { public void run() { try { updateCanvas(canvas, makeImg) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "updateCanvas(canvas, makeImg)"; }}); componentPopupMenu(canvas, new VF1() { public void get(JPopupMenu menu) { try { // 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 { startThread("Visualizing", new Runnable() { 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 (Exception __e) { throw rethrow(__e); } } public String toString() { return "quickVisualize(c.text);\r\n print(\"Quickvis done\");\r\n ..."; }}); } 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 { { AutoCloseable __3 = tempShowLoadingAnimation(); try { String theText = getTextTrim(text); makeCircle(theText); canvas.update(); } finally { _close(__3); }} } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "{ AutoCloseable __3 = tempShowLoadingAnimation(); 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 F0() { 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 MultiMap { Map> data = new HashMap>(); MultiMap() {} MultiMap(boolean useTreeMap) { if (useTreeMap) data = new TreeMap(); } MultiMap(MultiMap map) { putAll(map); } MultiMap(Map> data) { this.data = data;} void put(A key, B value) { synchronized(data) { List list = data.get(key); if (list == null) data.put(key, list = _makeEmptyList()); list.add(value); }} void addAll(A key, Collection values) { synchronized(data) { putAll(key, values); }} void addAllIfNotThere(A key, Collection values) { synchronized(data) { for (B value : values) setPut(key, value); }} void setPut(A key, B value) { synchronized(data) { if (!containsPair(key, value)) put(key, value); }} boolean containsPair(A key, B value) { synchronized(data) { return get(key).contains(value); }} void putAll(A key, Collection values) { synchronized(data) { for (B value : values) put(key, value); }} void removeAll(A key, Collection values) { synchronized(data) { for (B value : values) remove(key, value); }} List get(A key) { synchronized(data) { List list = data.get(key); return list == null ? Collections. emptyList() : list; }} // returns actual mutable live list // creates the list if not there List getActual(A key) { synchronized(data) { List list = data.get(key); if (list == null) data.put(key, list = _makeEmptyList()); return list; }} void clean(A key) { synchronized(data) { List list = data.get(key); if (list != null && list.isEmpty()) data.remove(key); }} Set keySet() { synchronized(data) { return data.keySet(); }} Set keys() { synchronized(data) { return data.keySet(); }} void remove(A key) { synchronized(data) { data.remove(key); }} void remove(A key, B value) { synchronized(data) { List list = data.get(key); if (list != null) { list.remove(value); if (list.isEmpty()) data.remove(key); } }} void clear() { synchronized(data) { data.clear(); }} boolean containsKey(A key) { synchronized(data) { return data.containsKey(key); }} B getFirst(A key) { synchronized(data) { List list = get(key); return list.isEmpty() ? null : list.get(0); }} void addAll(MultiMap map) { putAll(map); } void putAll(MultiMap map) { synchronized(data) { for (A key : map.keySet()) putAll(key, map.get(key)); }} void putAll(Map map) { synchronized(data) { if (map != null) for (Map.Entry e : map.entrySet()) put(e.getKey(), e.getValue()); }} int keysSize() { synchronized(data) { return l(data); }} // full size - note: expensive operation int size() { synchronized(data) { int n = 0; for (List l : data.values()) n += l(l); return n; }} // expensive operation List reverseGet(B b) { synchronized(data) { List l = new ArrayList(); for (A key : data.keySet()) if (data.get(key).contains(b)) l.add(key); return l; }} Map> asMap() { synchronized(data) { return cloneMap(data); }} boolean isEmpty() { synchronized(data) { return data.isEmpty(); }} // override in subclasses List _makeEmptyList() { return new ArrayList(); } Collection> allLists() { synchronized(data) { return new ArrayList(data.values()); } } List allValues() { return concatLists(values(data)); } Object mutex() { return data; } public String toString() { return "mm" + str(data); } }static class SoftwareMadeWeb { long date; String computerID, programID; String structure; // web or cal Web web; Map furtherInfo; }// Note: This does have the values problem (complicated values can cause memory leaks) 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); } }static class SnippetTitles { Map titles = synchroHashMap(); String get(String snippetID) { if (snippetID == null) return null; long id = parseSnippetID(snippetID); if (titles.containsKey(id)) return titles.get(id); else { String title = getSnippetTitle_safe(snippetID); titles.put(id, title); return title; } } // like get, but only check the cache String probe(String id) { return titles.get(parseSnippetID(id)); } // advanced uses void put(String id, String title) { titles.put(parseSnippetID(id), title); } boolean has(String id) { return titles.containsKey(parseSnippetID(id)); } void clear() { titles.clear(); } }static class AI_SubSpace implements AI_PostHandler, AI_CacheChecker { int limit; MultiMap index = new MultiMap(); HashMap websByID = new HashMap(); List webs = new ArrayList(); boolean changed; AI_SubSpace() {} AI_SubSpace(int limit) { this.limit = limit;} // existence of triple has already been checked public GlobalID postTriple(T3 t) { if (limitReached()) throw fail("Subspace limit reached"); Web web = webFromTriple(t); web.source = ai_currentMaker(); addWeb(web); return web.globalIDObj(); } public GlobalID postTriple(T3 t, boolean verified) { return postTriple(t); // XXX } void addWeb(Web web) { websByID.put(web.globalIDObj(), web); webs.add(web); changed = true; for (WebNode node : web_nodesAndRelations(web)) for (String text : asSet(node.texts())) index.put(symbol(ai_shortenForIndex(text)), node); } public boolean hasTriple(String a, String b, String c) { List webs = ai_withoutInvalidWebs(nodesToWebs(shortestList3( index.get(a), index.get(b), index.get(c)))); return webs_search_noVar_bool(webFromTriple(a, b, c), webs); } boolean limitReached() { return limit > 0 && l(websByID) >= limit; } List indexedNodes_rawLookup(String s) { return index.get(ai_shortenForIndex(s)); } int numWebs() { return l(websByID); } }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;} T3(T3 t) { a = t.a; b = t.b; c = t.c; } public int hashCode() { return _hashCode(a) + 2*_hashCode(b) - 4*_hashCode(c); } public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof T3)) return false; T3 t = (T3) o; return eq(a, t.a) && eq(b, t.b) && eq(c, t.c); } public String toString() { return "(" + quoteBorderless(a) + ", " + quoteBorderless(b) + ", " + quoteBorderless(c) + ")"; } }static class Pair implements Comparable> { 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 + ">"; } public int compareTo(Pair p) { if (p == null) return 1; int i = ((Comparable) a).compareTo(p.a); if (i != 0) return i; return ((Comparable) b).compareTo(p.b); } }static interface AI_PostHandler { GlobalID postTriple(T3 triple); // return web ID GlobalID postTriple(T3 triple, boolean verified); }static class BetterLabel extends JLabel { boolean autoToolTip = true; BetterLabel() { // Listeners given out to componentPopupMenu must not directly // reference the outer object (-> weak map problem). final WeakReference < BetterLabel > me = new WeakReference(this); componentPopupMenu(this, BetterLabel_menuItems(me)); } BetterLabel(String text) { this(); this.setText(text); } public void setText(String text) { super.setText(text); if (autoToolTip) if (!swic(text, "")) // HTML labels make super-huge, confusing tool tips setToolTipText(nullIfEmpty(text)); } } // moved outside of class for GC reasons (see above) static VF1 BetterLabel_menuItems(final WeakReference me) { return new VF1() { public void get(JPopupMenu menu) { try { addMenuItem(menu, "Copy text to clipboard", new Runnable() { public void run() { try { copyTextToClipboard(me.get().getText()); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "copyTextToClipboard(me.get().getText());"; }}); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "addMenuItem(menu, \"Copy text to clipboard\", r {\r\n copyTextToClipboard(me..."; }}; }// elements are put to front when added (not when accessed) static class MRUCache extends LinkedHashMap { int maxSize = 10; MRUCache() {} MRUCache(int maxSize) { this.maxSize = maxSize;} protected boolean removeEldestEntry(Map.Entry eldest) { return size() > maxSize; } }static class TripleIndex extends VirtualNodeIndex { static boolean useExactIndex; // experimental boolean activated; // are we the main index (see tripleIndex()) // index of word in any position - not used anymore SyncListMultiMap index;// = symbolSyncListMultiMap(); // count per word MultiSet wordIndex = //caseInsensitiveCompactMultiSet(); // not a NavigableMap caseInsensitiveMultiSet(); // word in position SyncListMultiMap[] positionalIndices = new SyncListMultiMap[] { symbolSyncListMultiMap(), symbolSyncListMultiMap(), symbolSyncListMultiMap() }; // all three words ExactTripleIndex exactIndex = useExactIndex ? exactTripleIndex() : null; // triples by ID CompactHashSet websByID = new CompactHashSet(); // first word -> relation -> triple Map> oneTwoIndex; // = (Map) symbolMap(); // SyncListMultiMap, TripleWeb> oneTwoIndex2 = new SyncListMultiMap; // works, but wastes space SyncListMultiMap, TripleWeb> oneTwoIndex2 = new SyncListMultiMap(new CompactPairKeyHashMap()); // try compact version int size() { return numWebs(); } int numWebs() { return l(websByID); } int numTerms() { return index != null ? index.keysSize() : wordIndex.uniqueSize(); } Set mainKeys() { return index != null ? keys(index) : wordIndex.asSet(); } NavigableSet navigableMainKeys() { return index != null ? navigableKeys(index) : wordIndex.navigableSet(); } // for synchronizing, e.g. in fullIndexedTermsStartingWith Map mainIndexMap() { return index != null ? index.data : wordIndex.map; } Collection indexedTerms() { return mainKeys(); } // query is shortened term List get(CharSequence _query) { String query = symbol(_query); List triples = index != null ? index.get(query) : combineLists3(getTriples(query, 0), getTriples(query, 1), getTriples(query, 2)); return ai_triplesToWebNodes_lazyList(query, triples); } // terms are NOT shortened /*TripleWeb getExact(Symbol a, Symbol b, Symbol c) { if (exactIndex == null) null; ret exactIndex.find(dummyTripleWebWithEntries(a, b, c)); }*/ // query is shortened term List> getTripleRefs(String query) { if (index == null) throw fail("no index"); return ai_triplesToTripleRefs_lazyList(query, index.get(query)); } List> getTripleRefs(String query, int position) { return ai_triplesToTripleRefs_lazyList(query, getTriples(query, position)); } // query is shortened term List getTriples(String query) { if (index == null) throw fail("no index"); return index.get(query); } // query is shortened term List getTriples(String query, int position) { return positionalIndices[position].get(query); } boolean hasShortTerm(String s) { return index != null ? index.containsKey(s) : wordIndex.contains(s); } Web getWeb(GlobalID id) { return webFromTriple(getTriple(id)); } TripleWeb getTriple(GlobalID id) { return websByID.find(dummyTripleWebWithGlobalID(id)); } void addWeb(Web web) { if (web == null) return; TripleWeb w = ai_webToTripleWeb(web); if (w == null) throw fail("Skipping non-tripelizable web: " + webToStringShort(web)); addTriple(w); } void addTriple(TripleWeb w) { if (w == null) return; if (index != null) for (String s : sortedInPlace(ll(w.a, w.b, w.c))) index.put(s, w); if (wordIndex != null) { wordIndex.add(w.a); wordIndex.add(w.b); wordIndex.add(w.c); } positionalIndices[0].put(w.a, w); positionalIndices[1].put(w.b, w); positionalIndices[2].put(w.c, w); if (exactIndex != null) exactIndex.add(w); if (oneTwoIndex != null) synchronized(oneTwoIndex) { SyncListMultiMap map = oneTwoIndex.get(w.a); if (map == null) oneTwoIndex.put(w.a, map = symbolSyncListMultiMap()); map.put(w.b, w); } if (oneTwoIndex2 != null) oneTwoIndex2.put(pair(w.a, w.b), w); websByID.add(w); if (activated) ai_fireNewTriple(w); } void removeWeb(Web web) { if (web != null) removeTriple(ai_webToTripleWeb(web)); } void removeTriples(Collection l) { MultiMap mm = new MultiMap(); for (TripleWeb w : l) { mm.put(ai_shortenForIndex(w.a), w); mm.put(ai_shortenForIndex(w.b), w); mm.put(ai_shortenForIndex(w.c), w); removeFromSets(w); } for (String term : keys(mm)) { HashSet webs = asHashSet(mm.get(term)); if (index != null) indexRemoveMulti(index, term, webs); for (int i = 0; i < 3; i++) indexRemoveMulti(positionalIndices[i], term, webs); } if (activated) for (TripleWeb w : l) ai_fireForgottenTriple(w); } // internal void removeFromSets(TripleWeb w) { websByID.remove(w); if (exactIndex != null) exactIndex.remove(w); if (oneTwoIndex != null) synchronized(oneTwoIndex) { SyncListMultiMap map = oneTwoIndex.get(w.a); if (map != null) { map.remove(w.a, w); if (empty(map)) oneTwoIndex.remove(w.a); } } if (oneTwoIndex2 != null) oneTwoIndex2.remove(pair(w.a, w.b), w); if (wordIndex != null) { wordIndex.remove(w.a); wordIndex.remove(w.b); wordIndex.remove(w.c); } } void removeTriple(TripleWeb w) { if (w == null) return; removeFromSets(w); GlobalID id = w.globalID(); indexRemove(0, ai_shortenForIndex(w.a), id); indexRemove(1, ai_shortenForIndex(w.b), id); indexRemove(2, ai_shortenForIndex(w.c), id); if (activated) ai_fireForgottenTriple(w); } // internal void indexRemove(int position, String term, GlobalID globalID) { if (index != null) indexRemove2(index, term, globalID); indexRemove2(positionalIndices[position], term, globalID); } // remove from a single list void indexRemove2(MultiMap mm, String term, GlobalID globalID) { List l = mm.get(term); for (int i = 0; i < l(l); i++) if (eq(l.get(i).globalID(), globalID)) { mm.remove(term, l.get(i)); return; } } // internal void indexRemoveMulti(MultiMap mm, String term, Set set) { List l = mm.get(term); synchronized(l) { removeSetFromListQuickly(l, set); } } void clear() { if (index != null) index.clear(); if (wordIndex != null) wordIndex.clear(); websByID.clear(); if (exactIndex != null) exactIndex.clear(); if (oneTwoIndex != null) oneTwoIndex.clear(); if (oneTwoIndex2 != null) oneTwoIndex2.clear(); } void activate() { if (!activated) { activated = true; ai_onNewOrRemovedWeb(new F1() { Object get(Web web) { try { addWeb(web); return false; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "addWeb(web);\r\n false;"; }}, new F1() { Object get(Web web) { try { removeWeb(web); return false; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "removeWeb(web);\r\n false;"; }}); } } Collection allTriples() { return websByID; } void trimToSize() { if (index != null) trimToSizeAll(index.allLists()); for (int i = 0; i < 3; i++) trimToSizeAll(positionalIndices[i].allLists()); for (SyncListMultiMap map : values(oneTwoIndex)) trimToSizeAll(map.allLists()); } void sortAllLists() { if (index != null) sortLists(index.allLists()); for (int i = 0; i < 3; i++) sortLists(positionalIndices[i].allLists()); for (SyncListMultiMap map : values(oneTwoIndex)) sortLists(map.allLists()); } void sortLists(Collection> ll) { for (List l : ll) ai_sortTriplesListByPriority_inPlace(l); } List getOneTwo(String a, String b) { if (oneTwoIndex2 != null) return oneTwoIndex2.get(pair(a, b)); return shortestList2(getTriples(a), getTriples(b)); } }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;} Map _map() { return fieldValues; } }abstract static class AbstractThoughtSpace implements AI_PostHandler { AbstractThoughtSpace parent; AbstractThoughtSpace() {} AbstractThoughtSpace(AbstractThoughtSpace parent) { this.parent = parent;} // take unshortened queries abstract List> get(String query); abstract List> get(String query, int position); abstract List getTriples(String query); abstract List getTriples(String query, int position); abstract List getOneTwo(String a, String b); // existence of triple has already been checked public abstract GlobalID postTriple(T3 t); abstract int size(); // doesn't take variables public boolean hasTriple(String a, String b, String c) { return findTriple(a, b, c) != null; } // doesn't take variables public TripleWeb findTriple(String a, String b, String c) { List l = shortestList3(getTriples(a, 0), getTriples(b, 1), getTriples(c, 2)); if (l != null) for (TripleWeb w : l) if (w != null && tripleEqic(w, a, b, c)) return w; return null; } public boolean hasTriple_verified(String a, String b, String c) { List l = shortestList3(getTriples(a, 0), getTriples(b, 1), getTriples(c, 2)); if (l != null) for (TripleWeb w : l) if (w != null && w.verified() && tripleEqic(w, a, b, c)) return true; return false; } }static class WebNode implements _SetField { Web web; List labels = new ArrayList(); Object visInfo; // visualisation info public void _setField(String f, Object x) { if (f.equals("web")) web = (Web) x; else if (f.equals("labels")) labels = (List) x; else if (f.equals("visInfo")) visInfo = x; } WebNode() {} WebNode(Web web) { this.web = web; web.fireNewNode(this); } void addLabel(String label) { addLabel(web.parseLabel(label)); } void addLabel(Lisp label) { if (setAdd(labels, label) && 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 labels.contains(label); } boolean hasLabel(String label) { return labels.contains(web.parseLabel(label)); } public String toString() { return l(labels) == 1 ? str(first(labels)) : str(labels); } int count() { return 1 + l(labels); } String text() { return web.unparseLabel(first(labels)); } List texts() { return web.unparseLabels(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 labels; } boolean hasLabels() { return nempty(labels); } void setLabel(int i, Lisp l) { labels.set(i, l); } Object visInfo() { return visInfo; } void visInfo(Object o) { visInfo = o; } double x, y; // placement on screen } static class PersistableThrowable { String className; String msg; String stacktrace; PersistableThrowable() {} PersistableThrowable(Throwable e) { if (e == null) className = "Crazy Null Error"; else { className = getClassName(e).replace('/', '.'); msg = e.getMessage(); stacktrace = getStackTrace_noRecord(e); } } public String toString() { return nempty(msg) ? className + ": " + msg : className; } } 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; } }/* * #! * Ontopia Engine * #- * Copyright (C) 2001 - 2013 The Ontopia Project * #- * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * !# */ static class CompactPairKeyHashMap extends AbstractMap, V> { final static int INITIAL_SIZE = 3; final static double LOAD_FACTOR = 0.6; // This object is used to represent null, should clients use that as final static Object nullObject = new Object(); /** * When a key is deleted this object is put into the hashtable in * its place, so that other entries with the same key (collisions) * further down the hashtable are not lost after we delete an object * in the collision chain. */ final static Object deletedObject = new Object(); int elements; int freecells; K[] keys1; K[] keys2; V[] values; // object at pos x corresponds to key at pos x int modCount; CompactPairKeyHashMap() { this(INITIAL_SIZE); } CompactPairKeyHashMap(int size) { keys1 = (K[]) new Object[(size==0 ? 1 : size)]; keys2 = (K[]) new Object[(size==0 ? 1 : size)]; values = (V[]) new Object[(size==0 ? 1 : size)]; elements = 0; freecells = keys1.length; modCount = 0; } // ===== MAP IMPLEMENTATION ============================================= /** * Returns the number of key/value mappings in this map. */ public synchronized int size() { return elements; } /** * Returns true if this map contains no mappings. */ public synchronized boolean isEmpty() { return elements == 0; } /** * Removes all key/value mappings in the map. */ public synchronized void clear() { elements = 0; for (int ix = 0; ix < keys1.length; ix++) { keys1[ix] = keys2[ix] = null; values[ix] = null; } freecells = values.length; modCount++; } /** * Returns true if this map contains the specified key. */ public synchronized boolean containsKey(Object k) { return keys1[findKeyIndex(k)] != null; } /** * Returns true if this map contains the specified value. */ public synchronized boolean containsValue(Object v) { if (v == null) v = (V)nullObject; for (int ix = 0; ix < values.length; ix++) if (values[ix] != null && values[ix].equals(v)) return true; return false; } /** * Returns a read-only set view of the map's keys. */ public synchronized Set, V>> entrySet() { throw new UnsupportedOperationException(); } /** * Removes the mapping with key k, if there is one, and returns its * value, if there is one, and null if there is none. */ public synchronized V remove(Object k) { int index = findKeyIndex(k); // we found the right position, now do the removal if (keys1[index] != null) { // we found the object // same problem here as with put V v = values[index]; keys1[index] = keys2[index] = (K) deletedObject; values[index] = (V) deletedObject; modCount++; elements--; return v; } else // we did not find the key return null; } /** * Adds the specified mapping to this map, returning the old value for * the mapping, if there was one. */ public synchronized V put(Pair k, V v) { if (k.a == null) k = pair((K)nullObject, k.b); if (k.b == null) k = pair(k.a, (K)nullObject); int hash = k.hashCode(); int hash1 = k.a.hashCode(); int hash2 = k.b.hashCode(); int index = (hash & 0x7FFFFFFF) % keys1.length; int offset = 1; int deletedix = -1; // search for the key (continue while !null and !this key) while(keys1[index] != null && !(keys1[index].hashCode() == hash1 && keys2[index].hashCode() == hash2 && keys1[index].equals(k.a) && keys2[index].equals(k.b))) { // if there's a deleted mapping here we can put this mapping here, // provided it's not in here somewhere else already if (keys1[index] == deletedObject) deletedix = index; index = ((index + offset) & 0x7FFFFFFF) % keys1.length; offset = offset*2 + 1; if (offset == -1) offset = 2; } if (keys1[index] == null) { // wasn't present already if (deletedix != -1) // reusing a deleted cell index = deletedix; else freecells--; modCount++; elements++; keys1[index] = k.a; keys2[index] = k.b; values[index] = (V) v; // rehash with increased capacity if (1 - (freecells / (double) keys1.length) > LOAD_FACTOR) rehash(keys1.length*2 + 1); return null; } else { // was there already modCount++; V oldv = values[index]; values[index] = (V) v; return oldv; } } /** * INTERNAL: Rehashes the hashmap to a bigger size. */ void rehash(int newCapacity) { int oldCapacity = keys1.length; K[] newKeys1 = (K[]) new Object[newCapacity]; K[] newKeys2 = (K[]) new Object[newCapacity]; V[] newValues = (V[]) new Object[newCapacity]; for (int ix = 0; ix < oldCapacity; ix++) { Object k1 = keys1[ix]; if (k1 == null || k1 == deletedObject) continue; Object k2 = keys2[ix]; int hash = pair(k1, k2).hashCode(); int index = (hash & 0x7FFFFFFF) % newCapacity; int offset = 1; // search for the key while(newKeys1[index] != null) { // no need to test for duplicates index = ((index + offset) & 0x7FFFFFFF) % newCapacity; offset = offset*2 + 1; if (offset == -1) offset = 2; } newKeys1[index] = (K) k1; newKeys2[index] = (K) k2; newValues[index] = values[ix]; } keys1 = newKeys1; keys2 = newKeys2; values = newValues; freecells = keys1.length - elements; } /** * Returns the value for the key k, if there is one, and null if * there is none. */ public synchronized V get(Object k) { return values[findKeyIndex(k)]; } /** * Returns a virtual read-only collection containing all the values * in the map. */ public synchronized Collection values() { return new ValueCollection(); } /** * Returns a virtual read-only set of all the keys in the map. */ public synchronized Set> keySet() { return new KeySet(); } // --- Internal utilities final int findKeyIndex(Object _k) { Pair k = (Pair) _k; if (k.a == null) k = pair((K)nullObject, k.b); if (k.b == null) k = pair(k.a, (K)nullObject); int hash = k.hashCode(); int hash1 = k.a.hashCode(); int hash2 = k.b.hashCode(); int index = (hash & 0x7FFFFFFF) % keys1.length; int offset = 1; int deletedix = -1; // search for the key (continue while !null and !this key) while(keys1[index] != null && !(keys1[index].hashCode() == hash1 && keys2[index].hashCode() == hash2 && keys1[index].equals(k.a) && keys2[index].equals(k.b))) { index = ((index + offset) & 0x7FFFFFFF) % keys1.length; offset = offset*2 + 1; if (offset == -1) offset = 2; } return index; } // --- Key set class KeySet extends AbstractSet> { public synchronized int size() { return elements; } public synchronized boolean contains(Object k) { return containsKey(k); } public synchronized Iterator> iterator() { return new KeyIterator(); } } class KeyIterator implements Iterator> { private int ix; private KeyIterator() { // walk up to first value, so that hasNext() and next() return // correct results for (; ix < keys1.length; ix++) if (values[ix] != null && keys1[ix] != deletedObject) break; } public synchronized boolean hasNext() { return ix < keys1.length; } public synchronized void remove() { throw new UnsupportedOperationException("Collection is read-only"); } public synchronized Pair next() { if (ix >= keys1.length) throw new NoSuchElementException(); K key1 = (K) keys1[ix]; K key2 = (K) keys2[ix]; ix++; // walk up to next value for (; ix < keys1.length; ix++) if (keys1[ix] != null && keys1[ix] != deletedObject) break; // ix now either points to next key, or outside array (if no next) return pair(key1, key2); } } // --- Value collection class ValueCollection extends AbstractCollection { public synchronized int size() { return elements; } public synchronized Iterator iterator() { return new ValueIterator(); } public synchronized boolean contains(Object v) { return containsValue(v); } } class ValueIterator implements Iterator { private int ix; private ValueIterator() { // walk up to first value, so that hasNext() and next() return // correct results for (; ix < values.length; ix++) if (values[ix] != null && values[ix] != deletedObject) break; } public synchronized boolean hasNext() { return ix < values.length; } public synchronized void remove() { throw new UnsupportedOperationException("Collection is read-only"); } public synchronized V next() { if (ix >= values.length) throw new NoSuchElementException(); V value = (V) values[ix++]; // walk up to next value for (; ix < values.length; ix++) if (values[ix] != null && values[ix] != deletedObject) break; // ix now either points to next value, or outside array (if no next) return value; } } }abstract static class RandomAccessAbstractList extends AbstractList implements RandomAccess { } // thumbnailator static class ImageSurface extends Surface { BufferedImage image; double zoomX = 1, zoomY = 1, zoomFactor = 1.5; 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; boolean noAlpha; // set to true to speed up drawing if you don't use alpha Object interpolationMode = RenderingHints.VALUE_INTERPOLATION_BILINEAR; Object onNewImage; public ImageSurface() { this(dummyImage()); } static BufferedImage dummyImage() { return new RGBImage(1, 1, new int[] { 0xFFFFFF }).getBufferedImage(); } ImageSurface(MakesBufferedImage image) { this(image != null ? image.getBufferedImage() : dummyImage()); } ImageSurface(BufferedImage image) { setImage(image); clearSurface = false; componentPopupMenu2(this, ImageSurface_popupMenuMaker()); new ImageSurfaceSelector(this); jHandleFileDrop(this, new VF1() { public void get(File f) { try { setImage(loadBufferedImage(f)) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "setImage(loadBufferedImage(f))"; }}); } public ImageSurface(RGBImage image, double zoom) { this(image); setZoom(zoom); } // 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(zoomFactor); centerPoint(point); } }); menu.add(miZoomIn); JMenuItem miZoomOut = new JMenuItem("Zoom out"); miZoomOut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { zoomOut(zoomFactor); centerPoint(point); } }); menu.add(miZoomOut); JMenuItem miZoomToWindow = new JMenuItem("Zoom to window"); miZoomToWindow.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { zoomToDisplaySize(); } }); menu.add(miZoomToWindow); addMenuItem(menu, "Show full screen", new Runnable() { public void run() { try { showFullScreen() ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "showFullScreen()"; }}); addMenuItem(menu, "Point: " + point.x + "," + point.y + " (image: " + image.getWidth() + "*" + image.getHeight() + ")", null); menu.addSeparator(); } addMenuItem(menu, "Save image...", new Runnable() { public void run() { try { saveImage() ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "saveImage()"; }}); addMenuItem(menu, "Upload image...", new Runnable() { public void run() { try { uploadTheImage() ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "uploadTheImage()"; }}); addMenuItem(menu, "Copy image to clipboard", new Runnable() { public void run() { try { copyImageToClipboard(getImage()) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "copyImageToClipboard(getImage())"; }}); if (!specialPurposed) { addMenuItem(menu, "Paste image from clipboard", new Runnable() { public void run() { try { loadFromClipboard() ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "loadFromClipboard()"; }}); addMenuItem(menu, "Load image snippet...", new Runnable() { public void run() { try { selectImageSnippet(new VF1() { public void get(String imageID) { try { setImage(loadImage2(imageID)) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "setImage(loadImage2(imageID))"; }}); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "selectImageSnippet(new VF1() { public void get(String imageID) ctex {..."; }}); } 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 = cloneClipBufferedImage(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) _print("render"); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, interpolationMode); g.setColor(Color.white); if (image == null) g.fillRect(0, 0, w, h); else { int iw = getZoomedWidth(), ih = getZoomedHeight(); boolean alpha = !noAlpha && hasTransparency(image); if (alpha) g.fillRect(0, 0, w, h); if (eq(interpolationMode, "thumbnailator")) g.drawImage(Thumbnailator.createThumbnail(image, iw, ih), 0, 0, null); else if (interpolationMode == RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR || zoomX >= 1 || zoomY >= 1) g.drawImage(image, 0, 0, iw, ih, null); else g.drawImage(resizeImage(image, iw, ih), 0, 0, null); // smoother if (!alpha) { g.fillRect(iw, 0, w-iw, h); g.fillRect(0, ih, iw, h-ih); } } if (overlay != null) { if (verbose) _print("render overlay"); pcallF(overlay, g); } if (selection != null) { if (verbose) _print("render selection"); // 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) { drawSelectionRect(g, selection, green, white, zoomX, zoomY); } public void drawSelectionRect(Graphics2D g, Rectangle selection, Color green, Color white, double zoomX, double zoomY) { g.setColor(green); int top = (int) (selection.y * zoomY); int bottom = (int) ((selection.y+selection.height) * zoomY); int left = (int) (selection.x * zoomX); int right = (int) ((selection.x+selection.width) * zoomX); g.drawRect(left-1, top-1, right-left+1, bottom-top+1); g.setColor(white); g.drawRect(left - 2, top - 2, right - left + 3, bottom - top + 3); } public ImageSurface setZoom(double zoom) { setZoom(zoom, zoom); return this; } public void setZoom(double zoomX, double zoomY) { if (this.zoomX == zoomX && this.zoomY == zoomY) return; if (verbose) _print("Setting zoom"); 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(MakesBufferedImage image) { setImage(image.getBufferedImage()); } public void setImage(final BufferedImage img) { { swing(new Runnable() { public void run() { try { BufferedImage newImage = img != null ? img : dummyImage(); BufferedImage oldImage = image; image = newImage; if (!imagesHaveSameSize(oldImage, newImage)) { if (verbose) _print("New image size"); revalidate(); // do we need this? } repaint(); pcallF(onNewImage); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "BufferedImage newImage = img != null ? img : dummyImage();\r\n BufferedIma..."; }}); } } 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-5)/(double) image.getWidth(); double yRatio = (display.height-5)/(double) image.getHeight(); setZoom(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)); } Pt pointToComponentCoordinates(double x, double y) { return new Pt((int) (x*zoomX), (int) (y*zoomY)); } void uploadTheImage() { call(hotwire(/*#1007313*/"#1016427"), "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 function allows garbage collection static VF2 ImageSurface_popupMenuMaker() { return new VF2() { public void get(ImageSurface is, JPopupMenu menu) { try { Point p = is.pointFromEvent(componentPopupMenu_mouseEvent.get()).getPoint(); is.fillPopupMenu(menu, p); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Point p = is.pointFromEvent(componentPopupMenu_mouseEvent.get()).getPoint();\r..."; }}; }// Modified from java.util.ArrayList static class SynchronizedArrayList extends SynchronizedArrayList_Base implements RandomAccess, Cloneable { private static final int DEFAULT_CAPACITY = 10; private static final Object[] EMPTY_ELEMENTDATA = {}; private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; transient Object[] elementData; // non-private to simplify nested class access private int size; SynchronizedArrayList(int initialCapacity) { if (initialCapacity > 0) { this.elementData = new Object[initialCapacity]; } else if (initialCapacity == 0) { this.elementData = EMPTY_ELEMENTDATA; } else { throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); } } SynchronizedArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; } SynchronizedArrayList(Collection c) { elementData = c.toArray(); if ((size = elementData.length) != 0) { // c.toArray might (incorrectly) not return Object[] (see 6260652) if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, size, Object[].class); } else { // replace with empty array. this.elementData = EMPTY_ELEMENTDATA; } } public synchronized void trimToSize() { modCount++; if (size < elementData.length) { elementData = (size == 0) ? EMPTY_ELEMENTDATA : Arrays.copyOf(elementData, size); } } public synchronized void ensureCapacity(int minCapacity) { int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA) // any size if not default element table ? 0 // larger than default for default empty table. It's already // supposed to be at default size. : DEFAULT_CAPACITY; if (minCapacity > minExpand) { ensureExplicitCapacity(minCapacity); } } private void ensureCapacityInternal(int minCapacity) { if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); } ensureExplicitCapacity(minCapacity); } private void ensureExplicitCapacity(int minCapacity) { modCount++; // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); } private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); } private static int hugeCapacity(int minCapacity) { if (minCapacity < 0) // overflow throw new OutOfMemoryError(); return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; } public synchronized int size() { return size; } public synchronized boolean isEmpty() { return size == 0; } public boolean contains(Object o) { return indexOf(o) >= 0; } public synchronized int indexOf(Object o) { if (o == null) { for (int i = 0; i < size; i++) if (elementData[i]==null) return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1; } public synchronized int lastIndexOf(Object o) { if (o == null) { for (int i = size-1; i >= 0; i--) if (elementData[i]==null) return i; } else { for (int i = size-1; i >= 0; i--) if (o.equals(elementData[i])) return i; } return -1; } public synchronized Object clone() { return new SynchronizedArrayList(this); } public synchronized Object[] toArray() { return Arrays.copyOf(elementData, size); } @SuppressWarnings("unchecked") public synchronized T[] toArray(T[] a) { if (a.length < size) // Make a new array of a's runtime type, but my contents: return (T[]) Arrays.copyOf(elementData, size, a.getClass()); System.arraycopy(elementData, 0, a, 0, size); if (a.length > size) a[size] = null; return a; } // Positional Access Operations @SuppressWarnings("unchecked") A elementData(int index) { return (A) elementData[index]; } public synchronized A get(int index) { rangeCheck(index); return elementData(index); } public synchronized A set(int index, A element) { rangeCheck(index); A oldValue = elementData(index); elementData[index] = element; return oldValue; } public synchronized boolean add(A e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; } public synchronized void add(int index, A element) { rangeCheckForAdd(index); ensureCapacityInternal(size + 1); // Increments modCount!! System.arraycopy(elementData, index, elementData, index + 1, size - index); elementData[index] = element; size++; } public synchronized A remove(int index) { rangeCheck(index); modCount++; A oldValue = elementData(index); int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // clear to let GC do its work return oldValue; } public synchronized boolean remove(Object o) { if (o == null) { for (int index = 0; index < size; index++) if (elementData[index] == null) { fastRemove(index); return true; } } else { for (int index = 0; index < size; index++) if (o.equals(elementData[index])) { fastRemove(index); return true; } } return false; } private void fastRemove(int index) { modCount++; int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // clear to let GC do its work } public synchronized void clear() { modCount++; // clear to let GC do its work for (int i = 0; i < size; i++) elementData[i] = null; size = 0; } public synchronized boolean addAll(Collection c) { Object[] a = c.toArray(); int numNew = a.length; ensureCapacityInternal(size + numNew); // Increments modCount System.arraycopy(a, 0, elementData, size, numNew); size += numNew; return numNew != 0; } public synchronized boolean addAll(int index, Collection c) { rangeCheckForAdd(index); Object[] a = c.toArray(); int numNew = a.length; ensureCapacityInternal(size + numNew); // Increments modCount int numMoved = size - index; if (numMoved > 0) System.arraycopy(elementData, index, elementData, index + numNew, numMoved); System.arraycopy(a, 0, elementData, index, numNew); size += numNew; return numNew != 0; } public synchronized void removeRange(int fromIndex, int toIndex) { modCount++; int numMoved = size - toIndex; System.arraycopy(elementData, toIndex, elementData, fromIndex, numMoved); // clear to let GC do its work int newSize = size - (toIndex-fromIndex); for (int i = newSize; i < size; i++) { elementData[i] = null; } size = newSize; } private void rangeCheck(int index) { if (index >= size) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } private void rangeCheckForAdd(int index) { if (index > size || index < 0) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } /** * Constructs an IndexOutOfBoundsException detail message. * Of the many possible refactorings of the error handling code, * this "outlining" performs best with both server and client VMs. */ private String outOfBoundsMsg(int index) { return "Index: "+index+", Size: "+size; } public synchronized boolean removeAll(Collection c) { Objects.requireNonNull(c); return batchRemove(c, false); } public synchronized boolean retainAll(Collection c) { Objects.requireNonNull(c); return batchRemove(c, true); } private boolean batchRemove(Collection c, boolean complement) { final Object[] elementData = this.elementData; int r = 0, w = 0; boolean modified = false; try { for (; r < size; r++) if (c.contains(elementData[r]) == complement) elementData[w++] = elementData[r]; } finally { // Preserve behavioral compatibility with AbstractCollection, // even if c.contains() throws. if (r != size) { System.arraycopy(elementData, r, elementData, w, size - r); w += size - r; } if (w != size) { // clear to let GC do its work for (int i = w; i < size; i++) elementData[i] = null; modCount += size - w; size = w; modified = true; } } return modified; } /** * Save the state of the ArrayList instance to a stream (that * is, serialize it). * * @serialData The length of the array backing the ArrayList * instance is emitted (int), followed by all of its elements * (each an Object) in the proper order. */ private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException{ // Write out element count, and any hidden stuff int expectedModCount = modCount; s.defaultWriteObject(); // Write out size as capacity for behavioural compatibility with clone() s.writeInt(size); // Write out all elements in the proper order. for (int i=0; iArrayList instance from a stream (that is, * deserialize it). */ private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { elementData = EMPTY_ELEMENTDATA; // Read in size, and any hidden stuff s.defaultReadObject(); // Read in capacity s.readInt(); // ignored if (size > 0) { // be like clone(), allocate array based upon size not capacity ensureCapacityInternal(size); Object[] a = elementData; // Read in all elements in the proper order. for (int i=0; iThe returned list iterator is fail-fast. * * @throws IndexOutOfBoundsException {@inheritDoc} */ public synchronized ListIterator listIterator(int index) { if (index < 0 || index > size) throw new IndexOutOfBoundsException("Index: "+index); return new ListItr(index); } /** * Returns a list iterator over the elements in this list (in proper * sequence). * *

The returned list iterator is fail-fast. * * @see #listIterator(int) */ public ListIterator listIterator() { return new ListItr(0); } /** * Returns an iterator over the elements in this list in proper sequence. * *

The returned iterator is fail-fast. * * @return an iterator over the elements in this list in proper sequence */ public Iterator iterator() { return concurrentlyIterateList(this); } /** * An optimized version of AbstractList.Itr */ private class Itr implements Iterator { int cursor; // index of next element to return int lastRet = -1; // index of last element returned; -1 if no such int expectedModCount = modCount; public boolean hasNext() { return cursor != size; } @SuppressWarnings("unchecked") public A next() { checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException(); Object[] elementData = SynchronizedArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (A) elementData[lastRet = i]; } public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { SynchronizedArrayList.this.remove(lastRet); cursor = lastRet; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } } /** * An optimized version of AbstractList.ListItr - TODO: replace with concurrent iterator or synchronize */ private class ListItr extends Itr implements ListIterator { ListItr(int index) { super(); cursor = index; } public boolean hasPrevious() { return cursor != 0; } public int nextIndex() { return cursor; } public int previousIndex() { return cursor - 1; } @SuppressWarnings("unchecked") public A previous() { checkForComodification(); int i = cursor - 1; if (i < 0) throw new NoSuchElementException(); Object[] elementData = SynchronizedArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i; return (A) elementData[lastRet = i]; } public void set(A e) { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { SynchronizedArrayList.this.set(lastRet, e); } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } public void add(A e) { checkForComodification(); try { int i = cursor; SynchronizedArrayList.this.add(i, e); cursor = i + 1; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } } public List subList(int fromIndex, int toIndex) { subListRangeCheck(fromIndex, toIndex, size); return new SubList(this, 0, fromIndex, toIndex); } static void subListRangeCheck(int fromIndex, int toIndex, int size) { if (fromIndex < 0) throw new IndexOutOfBoundsException("fromIndex = " + fromIndex); if (toIndex > size) throw new IndexOutOfBoundsException("toIndex = " + toIndex); if (fromIndex > toIndex) throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")"); } private class SubList extends SynchronizedArrayList_Base implements RandomAccess { private final SynchronizedArrayList_Base parent; private final int parentOffset; private final int offset; int size; SubList(SynchronizedArrayList_Base parent, int offset, int fromIndex, int toIndex) { this.parent = parent; this.parentOffset = fromIndex; this.offset = offset + fromIndex; this.size = toIndex - fromIndex; this.modCount = SynchronizedArrayList.this.modCount; } public A set(int index, A e) { synchronized(SynchronizedArrayList.this) { rangeCheck(index); checkForComodification(); A oldValue = SynchronizedArrayList.this.elementData(offset + index); SynchronizedArrayList.this.elementData[offset + index] = e; return oldValue; }} public A get(int index) { synchronized(SynchronizedArrayList.this) { rangeCheck(index); checkForComodification(); return SynchronizedArrayList.this.elementData(offset + index); }} public int size() { synchronized(SynchronizedArrayList.this) { checkForComodification(); return this.size; }} public void add(int index, A e) { synchronized(SynchronizedArrayList.this) { rangeCheckForAdd(index); checkForComodification(); parent.add(parentOffset + index, e); this.modCount = parent.modCount(); this.size++; }} public A remove(int index) { synchronized(SynchronizedArrayList.this) { rangeCheck(index); checkForComodification(); A result = parent.remove(parentOffset + index); this.modCount = parent.modCount(); this.size--; return result; }} public void removeRange(int fromIndex, int toIndex) { synchronized(SynchronizedArrayList.this) { checkForComodification(); parent.removeRange(parentOffset + fromIndex, parentOffset + toIndex); this.modCount = parent.modCount(); this.size -= toIndex - fromIndex; }} public boolean addAll(Collection c) { return addAll(this.size, c); } public boolean addAll(int index, Collection c) { synchronized(SynchronizedArrayList.this) { rangeCheckForAdd(index); int cSize = c.size(); if (cSize==0) return false; checkForComodification(); parent.addAll(parentOffset + index, c); this.modCount = parent.modCount(); this.size += cSize; return true; }} public Iterator iterator() { return listIterator(); } public ListIterator listIterator(final int index) { synchronized(SynchronizedArrayList.this) { checkForComodification(); rangeCheckForAdd(index); final int offset = this.offset; return new ListIterator() { int cursor = index; int lastRet = -1; int expectedModCount = SynchronizedArrayList.this.modCount; public boolean hasNext() { return cursor != SubList.this.size; } @SuppressWarnings("unchecked") public A next() { checkForComodification(); int i = cursor; if (i >= SubList.this.size) throw new NoSuchElementException(); Object[] elementData = SynchronizedArrayList.this.elementData; if (offset + i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (A) elementData[offset + (lastRet = i)]; } public boolean hasPrevious() { return cursor != 0; } @SuppressWarnings("unchecked") public A previous() { checkForComodification(); int i = cursor - 1; if (i < 0) throw new NoSuchElementException(); Object[] elementData = SynchronizedArrayList.this.elementData; if (offset + i >= elementData.length) throw new ConcurrentModificationException(); cursor = i; return (A) elementData[offset + (lastRet = i)]; } public int nextIndex() { return cursor; } public int previousIndex() { return cursor - 1; } public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { SubList.this.remove(lastRet); cursor = lastRet; lastRet = -1; expectedModCount = SynchronizedArrayList.this.modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } public void set(A e) { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { SynchronizedArrayList.this.set(offset + lastRet, e); } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } public void add(A e) { checkForComodification(); try { int i = cursor; SubList.this.add(i, e); cursor = i + 1; lastRet = -1; expectedModCount = SynchronizedArrayList.this.modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } final void checkForComodification() { if (expectedModCount != SynchronizedArrayList.this.modCount) throw new ConcurrentModificationException(); } }; }} public List subList(int fromIndex, int toIndex) { synchronized(SynchronizedArrayList.this) { subListRangeCheck(fromIndex, toIndex, size); return new SubList(parent, offset, fromIndex, toIndex); }} private void rangeCheck(int index) { if (index < 0 || index >= this.size) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } private void rangeCheckForAdd(int index) { if (index < 0 || index > this.size) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } private String outOfBoundsMsg(int index) { return "Index: "+index+", Size: "+this.size; } private void checkForComodification() { if (SynchronizedArrayList.this.modCount != this.modCount) throw new ConcurrentModificationException(); } } @Override @SuppressWarnings("unchecked") public synchronized void sort(Comparator c) { final int expectedModCount = modCount; Arrays.sort((A[]) elementData, 0, size, c); if (modCount != expectedModCount) { throw new ConcurrentModificationException(); } modCount++; } } static interface _SetField { void _setField(String name, Object value); }static class Lowest { A best; double score; transient Object onChange; synchronized boolean isNewBest(double score) { return best == null || score < this.score; } synchronized double bestScore() { return best == null ? Double.NaN : score; } double score() { return bestScore(); } synchronized float floatScore() { return best == null ? Float.NaN : (float) score; } synchronized float floatScoreOr(float defaultValue) { return best == null ? defaultValue : (float) score; } boolean put(A a, double score) { boolean change = false; synchronized(this) { if (a != null && isNewBest(score)) { best = a; this.score = score; change = true; } } if (change) pcallF(onChange); return change; } synchronized A get() { return best; } synchronized boolean has() { return best != null; } }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; } }static interface IVF1 { void get(A a); }// uses HashMap by default static class MultiSet { Map map = new HashMap(); MultiSet(boolean useTreeMap) { if (useTreeMap) map = new TreeMap(); } MultiSet() {} MultiSet(Iterable c) { addAll(c); } MultiSet(MultiSet ms) { synchronized(ms) { for (A a : ms.keySet()) add(a, ms.get(a)); }} synchronized void add(A key) { add(key, 1); } synchronized void addAll(Iterable c) { if (c != null) for (A a : c) add(a); } synchronized void addAll(MultiSet ms) { for (A a : ms.keySet()) add(a, ms.get(a)); } synchronized void add(A key, int count) { if (count <= 0) return; if (map.containsKey(key)) map.put(key, map.get(key)+count); else map.put(key, count); } synchronized int get(A key) { Integer i = map.get(key); return i != null ? i : 0; //ret key != null && map.containsKey(key) ? map.get(key) : 0; } synchronized boolean contains(A key) { return map.containsKey(key); } synchronized void remove(A key) { Integer i = map.get(key); if (i != null && i > 1) map.put(key, i - 1); else map.remove(key); } synchronized List topTen() { return getTopTen(); } synchronized List getTopTen() { return getTopTen(10); } synchronized List getTopTen(int maxSize) { List list = getSortedListDescending(); return list.size() > maxSize ? list.subList(0, maxSize) : list; } synchronized List highestFirst() { return getSortedListDescending(); } synchronized List lowestFirst() { return reversedList(getSortedListDescending()); } synchronized List getSortedListDescending() { List list = new ArrayList(map.keySet()); Collections.sort(list, new Comparator() { public int compare(A a, A b) { return map.get(b).compareTo(map.get(a)); } }); return list; } synchronized int getNumberOfUniqueElements() { return map.size(); } synchronized int uniqueSize() { return map.size(); } synchronized Set asSet() { return map.keySet(); } synchronized NavigableSet navigableSet() { return navigableKeys((NavigableMap) map); } synchronized Set keySet() { return map.keySet(); } synchronized A getMostPopularEntry() { int max = 0; A a = null; for (Map.Entry entry : map.entrySet()) { if (entry.getValue() > max) { max = entry.getValue(); a = entry.getKey(); } } return a; } synchronized void removeAll(A key) { map.remove(key); } synchronized int size() { int size = 0; for (int i : map.values()) size += i; return size; } synchronized MultiSet mergeWith(MultiSet set) { MultiSet result = new MultiSet(); for (A a : set.asSet()) { result.add(a, set.get(a)); } return result; } synchronized boolean isEmpty() { return map.isEmpty(); } synchronized public String toString() { // hmm. sync this? return str(map); } synchronized void clear() { map.clear(); } synchronized Map asMap() { return cloneMap(map); } }static interface IVar { void set(A a); A get(); boolean has(); void clear(); }/* * #! * Ontopia Engine * #- * Copyright (C) 2001 - 2013 The Ontopia Project * #- * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * !# */ // modified by Stefan Reich // Implements the Set interface more compactly than // java.util.HashSet by using a closed hashtable. static class CompactHashSet extends java.util.AbstractSet { protected final static int INITIAL_SIZE = 3; protected final static double LOAD_FACTOR = 0.75; protected final static Object nullObject = new Object(); protected final static Object deletedObject = new Object(); protected int elements; protected int freecells; protected A[] objects; protected int modCount; CompactHashSet() { this(INITIAL_SIZE); } CompactHashSet(int size) { // NOTE: If array size is 0, we get a // "java.lang.ArithmeticException: / by zero" in add(Object). objects = (A[]) new Object[(size==0 ? 1 : size)]; elements = 0; freecells = objects.length; modCount = 0; } CompactHashSet(Collection c) { this(c.size()); addAll(c); } @Override public Iterator iterator() { return new CompactHashIterator(); } @Override public int size() { return elements; } @Override public boolean isEmpty() { return elements == 0; } @Override public boolean contains(Object o) { return find(o) != null; } synchronized A find(Object o) { if (o == null) o = nullObject; int hash = o.hashCode(); int index = (hash & 0x7FFFFFFF) % objects.length; int offset = 1; // search for the object (continue while !null and !this object) while(objects[index] != null && !(objects[index].hashCode() == hash && objects[index].equals(o))) { index = ((index + offset) & 0x7FFFFFFF) % objects.length; offset = offset*2 + 1; if (offset == -1) offset = 2; } return objects[index]; } boolean removeIfSame(Object o) { A value = find(o); if (value == o) { remove(value); return true; } return false; } @Override synchronized public boolean add(Object o) { if (o == null) o = nullObject; int hash = o.hashCode(); int index = (hash & 0x7FFFFFFF) % objects.length; int offset = 1; int deletedix = -1; // search for the object (continue while !null and !this object) while(objects[index] != null && !(objects[index].hashCode() == hash && objects[index].equals(o))) { // if there's a deleted object here we can put this object here, // provided it's not in here somewhere else already if (objects[index] == deletedObject) deletedix = index; index = ((index + offset) & 0x7FFFFFFF) % objects.length; offset = offset*2 + 1; if (offset == -1) offset = 2; } if (objects[index] == null) { // wasn't present already if (deletedix != -1) // reusing a deleted cell index = deletedix; else freecells--; modCount++; elements++; // here we face a problem regarding generics: // add(A o) is not possible because of the null Object. We cant do 'new A()' or '(A) new Object()' // so adding an empty object is a problem here // If (! o instanceof A) : This will cause a class cast exception // If (o instanceof A) : This will work fine objects[index] = (A) o; // do we need to rehash? if (1 - (freecells / (double) objects.length) > LOAD_FACTOR) rehash(); return true; } else // was there already return false; } @Override synchronized public boolean remove(Object o) { if (o == null) o = nullObject; int hash = o.hashCode(); int index = (hash & 0x7FFFFFFF) % objects.length; int offset = 1; // search for the object (continue while !null and !this object) while(objects[index] != null && !(objects[index].hashCode() == hash && objects[index].equals(o))) { index = ((index + offset) & 0x7FFFFFFF) % objects.length; offset = offset*2 + 1; if (offset == -1) offset = 2; } // we found the right position, now do the removal if (objects[index] != null) { // we found the object // same problem here as with add objects[index] = (A) deletedObject; modCount++; elements--; return true; } else // we did not find the object return false; } @Override synchronized public void clear() { elements = 0; for (int ix = 0; ix < objects.length; ix++) objects[ix] = null; freecells = objects.length; modCount++; } @Override synchronized public Object[] toArray() { Object[] result = new Object[elements]; Object[] objects = this.objects; int pos = 0; for (int i = 0; i < objects.length; i++) if (objects[i] != null && objects[i] != deletedObject) { if (objects[i] == nullObject) result[pos++] = null; else result[pos++] = objects[i]; } // unchecked because it should only contain A return result; } // not sure if this needs to have generics @Override synchronized public T[] toArray(T[] a) { int size = elements; if (a.length < size) a = (T[])java.lang.reflect.Array.newInstance( a.getClass().getComponentType(), size); A[] objects = this.objects; int pos = 0; for (int i = 0; i < objects.length; i++) if (objects[i] != null && objects[i] != deletedObject) { if (objects[i] == nullObject) a[pos++] = null; else a[pos++] = (T) objects[i]; } return a; } protected void rehash() { int gargagecells = objects.length - (elements + freecells); if (gargagecells / (double) objects.length > 0.05) // rehash with same size rehash(objects.length); else // rehash with increased capacity rehash(objects.length*2 + 1); } protected void rehash(int newCapacity) { int oldCapacity = objects.length; @SuppressWarnings("unchecked") A[] newObjects = (A[]) new Object[newCapacity]; for (int ix = 0; ix < oldCapacity; ix++) { Object o = objects[ix]; if (o == null || o == deletedObject) continue; int hash = o.hashCode(); int index = (hash & 0x7FFFFFFF) % newCapacity; int offset = 1; // search for the object while(newObjects[index] != null) { // no need to test for duplicates index = ((index + offset) & 0x7FFFFFFF) % newCapacity; offset = offset*2 + 1; if (offset == -1) offset = 2; } newObjects[index] = (A) o; } objects = newObjects; freecells = objects.length - elements; } private class CompactHashIterator implements Iterator { private int index; private int lastReturned = -1; private int expectedModCount; @SuppressWarnings("empty-statement") public CompactHashIterator() { synchronized(CompactHashSet.this) { for (index = 0; index < objects.length && (objects[index] == null || objects[index] == deletedObject); index++) ; expectedModCount = modCount; } } @Override public boolean hasNext() { synchronized(CompactHashSet.this) { return index < objects.length; } } @SuppressWarnings("empty-statement") @Override public T next() { synchronized(CompactHashSet.this) { /*if (modCount != expectedModCount) throw new ConcurrentModificationException();*/ int length = objects.length; if (index >= length) { lastReturned = -2; throw new NoSuchElementException(); } lastReturned = index; for (index += 1; index < length && (objects[index] == null || objects[index] == deletedObject); index++) ; if (objects[lastReturned] == nullObject) return null; else return (T) objects[lastReturned]; } } @Override public void remove() { synchronized(CompactHashSet.this) { if (modCount != expectedModCount) throw new ConcurrentModificationException(); if (lastReturned == -1 || lastReturned == -2) throw new IllegalStateException(); // delete object if (objects[lastReturned] != null && objects[lastReturned] != deletedObject) { objects[lastReturned] = (A) deletedObject; elements--; modCount++; expectedModCount = modCount; // this is expected; we made the change } } } } }abstract static class TripleRef { T3 triple; TripleRef() {} TripleRef(T3 triple) { this.triple = triple;} A get() { return tripleAtPosition(triple(), position()); } T3 triple() { return triple; } // 0 = a, 1 = b, 2 = c abstract int position(); public String toString() { return position() + "@" + triple; } } abstract static class SynchronizedArrayList_Base extends AbstractList { final int modCount() { return modCount; } public void removeRange(int i, int j) { super.removeRange(i, j); } }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 RGBImage implements MakesBufferedImage { 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); } public int getWidth() { return width; } public 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); if (r.x == 0 && r.y == 0 && r.width == width && r.height == height) return this; 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; } void setPixel(int x, int y, int r, int g, int b) { if (x >= 0 && y >= 0 && x < width && y < height) pixels[y*width+x] = (limitToUByte(r) << 16) | (limitToUByte(g) << 8) | limitToUByte(b); } 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)); } void setInt(int x, int y, int rgb) { setPixel(x, y, rgb); } 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 interface MakesBufferedImage { BufferedImage getBufferedImage(); int getWidth(); int getHeight(); }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 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;} Rect(Pt p, int w, int h) { this.h = h; this.w = w; x = p.x; y = p.y; } Rect(Rect r) { x = r.x; y = r.y; w = r.w; h = r.h; } 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 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) { int i = l(hex)-6; r = Integer.parseInt(hex.substring(i, i+2), 16)/255f; g = Integer.parseInt(hex.substring(i+2, i+4), 16)/255f; b = Integer.parseInt(hex.substring(i+4, i+6), 16)/255f; } public float getComponent(int i) { return i == 0 ? r : i == 1 ? g : b; } public Color getColor() { return new Color(r, g, b); } public static RGB newSafe(float r, float g, float b) { return new RGB(Math.max(0, Math.min(1, r)), Math.max(0, Math.min(1, g)), Math.max(0, Math.min(1, b))); } 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(); } int redInt() { return iround(r*255); } int greenInt() { return iround(g*255); } int blueInt() { return iround(b*255); } } // 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, noXFullGrab; boolean vmBusSend = true; boolean initialSave = true; // set to false to avoid initial useless saving 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; Map, Map> ciFieldIndices; List saveActions = synchroList(); Object classFinder; List onAllChanged = synchroList(); // list of runnables transient Object saveWrapper; // VF1, to profile saving 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(); try { if (tryToGrab(allDynamic)) return this; } catch (Throwable e) { if (!exceptionMessageContains(e, "no xfullgrab")) printShortException(e); print("xfullgrab failed - loading DB of " + programID + " from disk"); } return loadFromDisk(allDynamic); } Concepts loadFromDisk() { return loadFromDisk(false); } Concepts loadFromDisk(boolean allDynamic) { if (nempty(concepts)) clearConcepts(); //DynamicObject_loading.set(true); // now done in unstructure() //try { // minimal crash recovery restoreLatestBackupIfConceptsFileEmpty(programID, "doIt" , true); long time = now(); Map _concepts = concepts; // empty map readLocally2_allDynamic.set(allDynamic); AutoCloseable __13 = tempSetTL(readLocally2_classFinder, classFinder); try { 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); }*/ if (initialSave) allChanged(); return this; } finally { _close(__13); }} 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; } return false; } finally { _close(db); }} 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) unstructure(grab, allDynamic, classFinder); 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; final File f = getProgramFile(programID, useGZIP ? "concepts.structure.gz" : "concepts.structure"); lock.lock(); long fullTime = now(); try { saveLocally2(this, programID, "idCounter"); if (useGZIP) { callRunnableWithWrapper(saveWrapper, new Runnable() { public void run() { try { saveGZStructureToFile(f, cloneMap(concepts)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "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; if (vmBusSend) vmBus_send("conceptsChanged", this); pcallFAll(onAllChanged); } // 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() { boolean shouldSave = autoSaver != null; if (autoSaver != null) { autoSaver.cancel(); autoSaver = null; } while (savingConcepts) sleepInCleanUp(10); if (shouldSave) 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) { if (l != null) for (Object o : cloneList(l)) if (o instanceof Long) { Concept c = concepts.get(o); if (c != null) c.delete(); } 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) { if (empty(params)) return l(list(c)); int n = 0; for (A x : list(c)) if (checkConceptFields(x, params)) ++n; return n; } int countConcepts(String c, Object... params) { if (empty(params)) return l(list(c)); 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 removeConceptIndex(IConceptIndex index) { if (conceptIndices == null) return; conceptIndices.remove(index); if (empty(conceptIndices)) conceptIndices = null; } 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); } synchronized void addCIFieldIndex(Class c, String field, IFieldIndex index) { if (ciFieldIndices == null) ciFieldIndices = new HashMap(); Map map = ciFieldIndices.get(c); if (map == null) ciFieldIndices.put(c, map = new HashMap()); map.put(field, index); } synchronized IFieldIndex getCIFieldIndex(Class c, String field) { if (ciFieldIndices == null) return null; Map map = ciFieldIndices.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() { if (noXFullGrab) throw fail("no xfullgrab (DB too large)"); Lock __12 = lock(); lock(__12); try { if (changes == changesWritten && !isTransient()) return loadConceptsStructure(programID); return structure(cloneMap(concepts)); } finally { unlock(__12); } } /* dev. Either xfullgrabGZipped() { lock lock(); if (changes == changesWritten && !isTransient()) ret loadConceptsStructure(programID); ret structure(cloneMap(concepts)); }*/ 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(); } void conceptChanged(Concept c) { allChanged(); if (conceptIndices != null) for (IConceptIndex index : conceptIndices) index.update(c); } } // class Concepts static class Concept extends DynamicObject { transient Concepts _concepts; // Where we belong long id; long created, _modified; List refs; List backRefs; // 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(); } static boolean loading() { return _loading(); } static boolean _loading() { return dynamicObjectIsLoading(); } void _created() { if (!isTrue(concepts_unlisted.get())) db_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 (!dynamicObjectIsLoading()) 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); } // TODO: sync all the indexing and unindexing!? void index() { if (value != null) value._addBackRef(this); change(); } void unindex() { if (value != null) value._removeBackRef(this); } void change() { Concept.this.change(); } } class RefL extends AbstractList { List> l = new ArrayList(); public A set(int i, A o) { A prev = l.get(i).get(); l.get(i).set(o); return prev; } public void add(int i, A o) { l.add(i, new Ref(o)); } public A get(int i) { return l.get(i).get(); } public A remove(int i) { return l.remove(i).get(); } public int size() { return l.size(); } public boolean contains(Object o) { if (o instanceof Concept) for (Ref r : l) if (eq(r.get(), o)) return true; return super.contains(o); } } 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() { _modified = now(); _change_withoutUpdatingModifiedField(); } void _change_withoutUpdatingModifiedField() { if (_concepts != null) _concepts.conceptChanged(this); } void _change() { change(); } String _programID() { return _concepts == null ? getDBProgramID() : _concepts.progID(); } // overridable void _addBackRef(Concept.Ref ref) { backRefs = addDyn(backRefs, ref); } void _removeBackRef(Concept.Ref ref) { backRefs = removeDyn(backRefs, ref); } // convenience methods void _setField(String field, Object value) { cset(this, field, value); } void _setFields(Object... values) { cset(this, values); } } // 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(db_mainConcepts()).get(ref); if (xref == null) xref = new XRef(ref); return xref; } // define standard concept functions to use main concepts static void cleanMeUp_concepts() { if (db_mainConcepts() != null) db_mainConcepts().cleanMeUp(); // mainConcepts = null; // TODO } static void loadAndAutoSaveConcepts() { db_mainConcepts().persist(); } static void loadAndAutoSaveConcepts(int interval) { db_mainConcepts().persist(interval); } static RC toPassRef(Concept c) { return new RC(c); } // so we can instantiate the program to run as a bare DB bot static B pairB(Pair p) { return p == null ? null : p.b; } 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 stdHash2(Object a) { if (a == null) return 0; return stdHash(a, toStringArray(allFields(a))); } static String assertGlobalID(String s) { return assertPossibleGlobalID(s); } static BigInteger plus(BigInteger a, BigInteger b) { return a.add(b); } static BigInteger plus(BigInteger a, long b) { return a.add(bigint(b)); } static BigInteger mul(BigInteger a, BigInteger b) { return a.multiply(b); } static BigInteger mul(BigInteger a, long b) { return a.multiply(bigint(b)); } static String bigintToGlobalID(BigInteger value) { char[] buf = new char[16]; for (int i = 16-1; i >= 0; i--) { buf[i] = charPlus('a', mod(value, 26).intValue()); value = div(value, 26); } return str(buf); } static boolean isNaN(double d) { return Double.isNaN(d); } static double minusInfinity() { return negativeInfinity(); } static float getScore(Scored s) { return s == null ? 0 : s.score; } static String formatDouble_significant2(double d, int digits) { try { digits -= max(0, Math.floor(Math.log10(abs(d))+1)); return formatDouble(d, digits); } catch (Throwable _e) { print("Had number: " + d + ", digits: " + digits); throw rethrow(_e); } } static String callStringifier(Object stringifier, Object o) { return stringifier != null ? str(callF(stringifier, o)) : str(o); } static String defaultTimerName_name; static String defaultTimerName() { if (defaultTimerName_name == null) defaultTimerName_name = "A timer by " + programID(); return defaultTimerName_name; } static Set _registerTimer_list = newWeakHashSet(); static void _registerTimer(java.util.Timer timer) { _registerTimer_list.add(timer); } static void cleanMeUp__registerTimer() { cancelTimers(getAndClearList(_registerTimer_list)); } // firstDelay = delay static FixedRateTimer doEvery(long delay, final Object r) { return doEvery(delay, delay, r); } static FixedRateTimer doEvery(long delay, long firstDelay, final Object r) { FixedRateTimer timer = new FixedRateTimer(shorten(programID() + ": " + r, 80)); timer.scheduleAtFixedRate(smartTimerTask(r, timer, toInt(delay)), toInt(firstDelay), toInt(delay)); return timer; } // reversed argument order for fun static FixedRateTimer doEvery(double initialSeconds, double delaySeconds, final Object r) { return doEvery(toMS(delaySeconds), toMS(initialSeconds), r); } static FixedRateTimer doEvery(double delaySeconds, final Object r) { return doEvery(toMS(delaySeconds), r); } static GlobalID globalIDFromParts(long a, short b) { GlobalID id = new GlobalID(); id.a = a; id.b = b; return id; } static long globalIDPart1(GlobalID id) { return id == null ? 0 : id.a; } static short globalIDPart2(GlobalID id) { return id == null ? 0 : (short) id.b; } static String _compactString(String s) { return s; } static String quoteIfNotIdentifier(String s) { if (s == null) return null; return isJavaIdentifier(s) ? s : quote(s); } 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(byte[] a) { return a == null || a.length == 0; } static boolean isEmpty(Map map) { return map == null || map.isEmpty(); } static int hashCode(Object a) { return a == null ? 0 : a.hashCode(); } static Set keySet(Map map) { return map == null ? new HashSet() : map.keySet(); } static Set keySet(Object map) { return keys((Map) map); } static Set keySet(MultiSet ms) { return ms.keySet(); } static Set keySet(MultiMap mm) { return mm.keySet(); } static UnsupportedOperationException unsupportedOperation() { throw new UnsupportedOperationException(); } static long sysTime() { return sysNow(); } static void change() { //mainConcepts.allChanged(); // safe version for now cause function is sometimes included unnecessarily (e.g. by EGDiff) callOpt(getOptMC("mainConcepts"), "allChanged"); } static Map mapValues(Object func, Map map) { Map m = similarEmptyMap(map); for (Object key : keys(map)) m.put(key, callF(func, map.get(key))); return m; } static Map mapValues(Map map, IF1 f) { return mapValues(f, map); } static Map mapValues(Map map, Object func) { return mapValues(func, map); } static GlobalID aGlobalIDObj() { return asGlobalID(randomID(16)); } static long nowUnlessLoading() { return /*dynamicObjectIsLoading() ? 0 :*/ now(); } static String lisp2label(Lisp l) { return l.isLeaf() ? l.head : clUnparse(l); } static String webToString(Web web) { List out = new ArrayList(); Map index = new HashMap(); for (int i = 0; i < l(web.nodes); i++) { out.add("Node " + (i+1) + ": " + web.nodes.get(i)); index.put(web.nodes.get(i), i+1); } for (Pair p : web_relations(web)) out.add(index.get(p.a) + " -> " + index.get(p.b) + " = " + web.getRelation(p)); return fromLines(out); } static void clearAll(Object... l) { for (Object o : l) callOpt(o, "clear"); } static Lisp clParse(String s) { List tok = tok_groupRoundBrackets(s); if (l(tok) == 1) return null; Lisp l = lisp(unquote(tok.get(1))); for (int i = 3; i < l(tok); i += 2) { String t = tok.get(i); if (t.startsWith("(") && t.endsWith(")")) l.add(assertNotNull(clParse(dropFirstAndLast(t)))); else l.add(lisp(aiUsing(unquote(t)))); } return l; } static List clParse(List l) { return map("clParse", l); } 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 String lispHead(Lisp l) { return l == null ? null : l.head; } static Set indexedTerms() { return ai_mainIndexKeys(); } static int _hashCode(Object a) { return a == null ? 0 : a.hashCode(); } static Field makeAccessible(Field f) { f.setAccessible(true); return f; } static Method makeAccessible(Method m) { m.setAccessible(true); return m; } static Set allInterfacesImplementedBy(Class c) { if (c == null) return null; HashSet set = new HashSet(); allInterfacesImplementedBy_find(c, set); return set; } static void allInterfacesImplementedBy_find(Class c, Set set) { if (c.isInterface() && !set.add(c)) return; do { for (Class intf : c.getInterfaces()) allInterfacesImplementedBy_find(intf, set); } while ((c = c.getSuperclass()) != null); } static Method findStaticMethod(Class c, String method, Object... args) { Class _c = c; while (c != null) { for (Method m : c.getDeclaredMethods()) { if (!m.getName().equals(method)) continue; if ((m.getModifiers() & Modifier.STATIC) == 0 || !findStaticMethod_checkArgs(m, args)) continue; return m; } c = c.getSuperclass(); } return null; } static boolean findStaticMethod_checkArgs(Method m, Object[] args) { Class[] types = m.getParameterTypes(); if (types.length != args.length) return false; for (int i = 0; i < types.length; i++) if (!(args[i] == null || isInstanceX(types[i], args[i]))) return false; return true; } static String aGlobalID() { return randomID(globalIDLength()); } static String fsi(String id) { return formatSnippetID(id); } static List quoteAll(Collection l) { List x = new ArrayList(); for (String s : l) x.add(quote(s)); return x; } 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 void setAddAll(List a, Collection b) { for (A x : b) setAdd(a, x); } static A addAndReturn(Collection c, A a) { if (c != null) c.add(a); return a; } static BufferedImage quickVisualizeOr(String query, String defaultImageID) { BufferedImage img = quickVisualize(query); return img != null ? img : loadImage2(defaultImageID); } static BufferedImage loadImage2(String snippetIDOrURL) { return loadBufferedImage(snippetIDOrURL); } static BufferedImage loadImage2(File file) { return loadBufferedImage(file); } 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 A findWhere(Collection c, Object... data) { if (c != null) for (A x : c) if (checkFields(x, data)) return x; return null; } 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 ptX(Pt p) { return p == null ? 0 : p.x; } static int ptY(Pt p) { return p == null ? 0 : p.y; } 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); } 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 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 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 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 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 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 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 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 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 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 JFrame showCenterFrame(String title, int w, int h) { return showCenterFrame(title, w, h, null); } static JFrame showCenterFrame(int w, int h, Component content) { return showCenterFrame(defaultFrameTitle(), w, h, content); } 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 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 F0() { 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 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 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 ThreadLocal componentPopupMenu_mouseEvent; static void componentPopupMenu_init() { { swing(new Runnable() { public void run() { try { if (componentPopupMenu_mouseEvent == null) componentPopupMenu_mouseEvent = (ThreadLocal) vm_generalMap_get("mouseEvent"); if (componentPopupMenu_mouseEvent == null) vm_generalMap_put("componentPopupMenu_mouseEvent" , componentPopupMenu_mouseEvent = new ThreadLocal()); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (componentPopupMenu_mouseEvent == null)\r\n componentPopupMenu_mouseEve..."; }}); } } // menuMaker = voidfunc(JPopupMenu) static void componentPopupMenu(final JComponent component, final Object menuMaker) { if (component == null || menuMaker == null) return; { swing(new Runnable() { public void run() { try { Object adapter = componentPopupMenu_initForComponent(component); ((List) _get(adapter, "maker")).add(menuMaker); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Object adapter = componentPopupMenu_initForComponent(component);\r\n ((List)..."; }}); } } static Object componentPopupMenu_initForComponent(final JComponent component) { return component == null ? null : swing(new F0() { Object get() { try { componentPopupMenu_init(); Object adapter = findComponentPopupMenuListener_gen(component); if (adapter == null) { componentPopupMenu_Adapter a = new componentPopupMenu_Adapter(); component.addMouseListener(a); adapter = a; } return adapter; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "componentPopupMenu_init();\r\n O adapter = findComponentPopupMenuListener_ge..."; }}); } static class componentPopupMenu_Adapter extends MouseAdapter { List maker = new ArrayList(); boolean internalFrameLeftButtonMagic; Point pressedAt; public void mousePressed(MouseEvent e) { displayMenu(e); pressedAt = internalFrameLeftButtonMagic && e.getClickCount() == 1 && internalFrameActive(e.getComponent()) ? e.getLocationOnScreen() : null; } public void mouseReleased(MouseEvent e) { // TODO: show a little less often on left mouse click if (internalFrameLeftButtonMagic && eq(pressedAt, e.getLocationOnScreen())) displayMenu2(e); else displayMenu(e); } void displayMenu(MouseEvent e) { if (e.getSource() instanceof JInternalFrame) return; if (e.isPopupTrigger()) displayMenu2(e); } void displayMenu2(MouseEvent e) { JPopupMenu menu = new JPopupMenu(); int emptyCount = menu.getComponentCount(); AutoCloseable __651 = tempSetTL(componentPopupMenu_mouseEvent, e); try { for (Object menuMaker : maker) pcallF(menuMaker, menu); vmBus_send("showingPopupMenu", e.getComponent(), menu); // show menu if any items in it if (menu.getComponentCount() != emptyCount) menu.show(e.getComponent(), e.getX(), e.getY()); } finally { _close(__651); }} } 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 moveAllMenuItems(JPopupMenu src, JMenu dest) { Component[] l = src.getComponents(); src.removeAll(); for (Component c : l) dest.add(c); } 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(Menu menu, String text, Object action) { menu.add(menuItem(text, action)); } static void addMenuItem(JMenu menu, JMenuItem menuItem) { menu.add(menuItem); } 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) { _handleException(__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 __856 = quickVisualize_lock; lock(__856); try { img = googleImageSearch_new(query); if (img == null) return null; savePNG(f, img); return img; } finally { unlock(__856); } } 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 String copyTextToClipboard(Object _text) { String text = str(_text); StringSelection selection = new StringSelection(text); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, selection); vmBus_send("newClipboardContents", text); return text; } 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 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 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 JTextField jtextfield() { return jTextField(); } static JTextField jtextfield(String text) { return jTextField(text); } static JTextField jtextfield(Object o) { return jTextField(o); } static int showForm_defaultGap = 4; static int showForm_gapBetweenColumns = 10; static JPanel showFormTitled(final String title, final Object... _parts) { JDesktopPane desktop = mainDesktopPane(); if (desktop != null) return showInternalFrameFormTitled(desktop, title, _parts); return swing(new F0() { JPanel get() { try { final Var frame = new Var(); JPanel panel = showForm_makePanel(false, _parts); frame.set(showForm_makeFrame(title, panel)); return panel; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "final new Var frame;\r\n JPanel panel = showForm_makePanel(false, _p..."; }}); } static JPanel showForm_makePanel(Boolean internalFrame, Object... _parts) { List out = showForm_arrange1(showForm_makeComponents(internalFrame, _parts)); return vstackWithSpacing(out, showForm_defaultGap); } static Runnable runnableThread(final Runnable r) { return new Runnable() { public void run() { try { startThread(r) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "startThread(r)"; }}; } static AutoCloseable tempShowLoadingAnimation() { return tempShowLoadingAnimation("Hold on user..."); } static AutoCloseable tempShowLoadingAnimation(String text) { try { return tempDisposeWindow(showAnimationInTopRightCorner("#1003543", text)); } catch (Throwable __e) { return null; } } 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 Pt untranslatePt(Pt a, Pt b) { if (a == null) return b; return new Pt(b.x-a.x, b.y-a.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 double pointDistance(DoublePt a, DoublePt b) { return pointDistance(a.x, a.y, b.x, b.y); } // uses bilinear interpolation static BufferedImage scaleImage(BufferedImage before, double scale) { return scaleImage(before, scale, scale); } static BufferedImage scaleImage(BufferedImage before, double scaleX, double scaleY) { if (scaleX == 1 && scaleY == 1) return before; int w = before.getWidth(); int h = before.getHeight(); int neww = max(1, iround(w*scaleX)), newh = max(1, iround(h*scaleY)); BufferedImage after = new BufferedImage(neww, newh, BufferedImage.TYPE_INT_ARGB); AffineTransform at = new AffineTransform(); at.scale(scaleX, scaleY); AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR); return scaleOp.filter(before, after); } static BufferedImage scaleImage(double scale, BufferedImage before) { return scaleImage(before, scale); } static String web_defaultRelationName = ""; static String web_defaultRelationName() { return web_defaultRelationName; } 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); } // 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 void requestFocus(final JComponent c) { focus(c); } 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 void markWebsPosted() { markWebsPosted_createMarker(); triggerWebsChanged(); } 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 BufferedImage whiteImage(int w, int h) { return newBufferedImage(w, h, Color.white); } 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 Pt scalePt(Pt p, double f) { return new Pt(iround(p.x*f), iround(p.y*f)); } static Pt pt(int x, int y) { return new Pt(x, y); } static int thoughtCircleSize(BufferedImage img) { return min(img.getWidth(), img.getHeight()) + 20; } static boolean containsInstance(Iterable i, Class c) { if (i != null) for (Object o : i) if (isInstanceX(c, o)) return true; return false; } static Map putAll(Map a, Map b) { if (a != null) a.putAll(b); return a; } static A reverseGet(List l, int idx) { if (l == null || idx < 0) return null; int n = l(l); return idx < n ? l.get(n-1-idx) : null; } static String getSnippetTitle_safe(String id) { if (isLocalSnippetID(id)) return localSnippetTitle(id); String title = or(loadTextFile(snippetTitle_cacheFile(id)), "?"); try { title = getSnippetTitle(id); } catch (Throwable __e) { _handleException(__e); } return title; } static String ai_currentMaker() { return postSoftwareMadeWeb_maker.get(); } static String quoteBorderless(Object o) { if (o == null) return "null"; return quoteBorderless(str(o)); } static String quoteBorderless(String s) { if (s == null) return "null"; StringBuilder out = new StringBuilder((int) (l(s)*1.5)); quoteBorderless_impl(s, out); return out.toString(); } static void quoteBorderless_impl(String s, StringBuilder out) { int l = s.length(); for (int i = 0; i < l; i++) { char c = s.charAt(i); if (c == '\\' || c == '"') out.append('\\').append(c); else if (c == '\r') out.append("\\r"); else if (c == '\n') out.append("\\n"); else out.append(c); } } static int hashCodeFor(Object a) { return a == null ? 0 : a.hashCode(); } 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 A setToolTipText(Object toolTip, A c) { return setToolTipText(c, toolTip); } static String nullIfEmpty(String s) { return isEmpty(s) ? null : s; } static Map nullIfEmpty(Map map) { return isEmpty(map) ? null : map; } static List nullIfEmpty(List l) { return isEmpty(l) ? null : l; } static MultiSet caseInsensitiveMultiSet() { MultiSet ms = new MultiSet(); ms.map = caseInsensitiveMap(); return ms; } static SyncListMultiMap symbolSyncListMultiMap() { return caseInsensitiveSyncListMultiMap(); } static ExactTripleIndex exactTripleIndex() { return new ExactTripleIndex(); } static class ExactTripleHasher implements Hasher { public int hashCode(TripleWeb w) { return tripleHashCodeByContentsIC(w); } public boolean equals(TripleWeb a, TripleWeb b) { return tripleEqualsByContentsIC(a, b); } } static class ExactTripleIndex extends CustomCompactHashSet { ExactTripleIndex() { super(new ExactTripleHasher()); } } static NavigableSet navigableKeys(NavigableMap map) { return map == null ? new TreeSet() : map.navigableKeySet(); } static NavigableSet navigableKeys(MultiSet ms) { return ((NavigableMap) ms.map).navigableKeySet(); } static NavigableSet navigableKeys(MultiMap mm) { return ((NavigableMap) mm.data).navigableKeySet(); } static List combineLists3(List a, List b, List c) { return combineLists(a, combineLists(b, c)); } static List ai_triplesToWebNodes_lazyList(final String searchTerm, final List l) { return new LazyList() { final int n = l(l); public int size() { return n; } public WebNode get(int i) { TripleWeb w = syncGet(l, i); if (w == null) return null; int runLength = 0; while (i-runLength > 0 && l.get(i-runLength-1) == w) ++runLength; return ai_tripleToWebNode(l.get(i), searchTerm, runLength); } }; } static boolean ai_triplesToTripleRefs_lazyList_debug; static List> ai_triplesToTripleRefs_lazyList(final String searchTerm, final List l) { if (l == null) return null; class TTTRList extends LazyList> { final int n = l(l); public int size() { return n; } public TripleRef get(int i) { if (ai_triplesToTripleRefs_lazyList_debug) print("tttrlist.get " + i + "/" + l(l)); TripleWeb w = syncGet(l, i); if (w == null) return null; // run-length magic - TODO: concatenated lists int runLength = 0; while (i-runLength > 0 && l.get(i-runLength-1) == w) ++runLength; return ai_tripleToTripleRef(l.get(i), searchTerm, runLength); // may be null } } return new TTTRList(); } static TripleWeb dummyTripleWebWithGlobalID(GlobalID id) { TripleWeb w = new TripleWeb(); w.globalID(id); return w; } static List sortedInPlace(List l, final Object comparator) { sort(l, makeComparator(comparator)); return l; } static List sortedInPlace(List l) { sort(l); return l; } static void ai_fireNewTriple(TripleWeb w) { for (TripleListener l : ai_tripleListeners()) { try { l.onNewTriple(w); } catch (Throwable __e) { _handleException(__e); }} } static HashSet asHashSet(Collection c) { synchronized(collectionMutex(c)) { return new HashSet(c); } } static HashSet asHashSet(A[] a) { return a == null ? null : new HashSet(Arrays.asList(a)); } static void ai_fireForgottenTriple(TripleWeb w) { for (TripleListener l : ai_tripleListeners()) { try { l.onForgottenTriple(w); } catch (Throwable __e) { _handleException(__e); }} } static void removeSetFromListQuickly(List l, Collection _set) { Set set = asSet(_set); if (l(set) < 10) { ListIterator it = l.listIterator(); while (it.hasNext()) if (set.contains(it.next())) it.remove(); } else { List l2 = emptyListWithCapacity(l); for (A a : l) if (!set.contains(a)) l2.add(a); copyList(l2, l); } } static void trimToSizeAll(Collection> l) { if (l != null) for (List ll : l) trimToSize(ll); } static void ai_sortTriplesListByPriority_inPlace(List l) { sortInPlaceByCalculatedFieldDesc_f1(l, new F1() { Boolean get(TripleWeb w) { try { return ai_isPriorityTriple(w); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ai_isPriorityTriple(w)"; }}); } static List shortestList2(List a, List b) { return l(a) < l(b) ? a : b; } static boolean tripleEqic(T3 a, T3 b) { if (a == null) return b == null; else if (b == null) return false; return eqic(a.a, b.a) && eqic(a.b, b.b) && eqic(a.c, b.c); } static boolean tripleEqic(T3 a, String ba, String bb, String bc) { if (a == null) return false; return eqic(a.a, ba) && eqic(a.b, bb) && eqic(a.c, bc); } static boolean containsKey(Map map, A key) { return map != null && map.containsKey(key); } static JLabel setImage(final BufferedImage img, final JLabel lbl) { if (lbl != null) { swing(new Runnable() { public void run() { try { lbl.setIcon(imageIcon(img)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "lbl.setIcon(imageIcon(img));"; }}); } return lbl; } static JLabel setImage(JLabel lbl, BufferedImage img) { return setImage(img, lbl); } static JLabel setImage(final String imageID, final JLabel lbl) { if (lbl != null) { swing(new Runnable() { public void run() { try { lbl.setIcon(imageIcon(imageID)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "lbl.setIcon(imageIcon(imageID));"; }}); } return lbl; } static JLabel setImage(JLabel lbl, String imageID) { return setImage(imageID, lbl); } static void componentPopupMenu2(A component, final VF2 menuMaker) { final WeakReference < A > ref = new WeakReference(component); componentPopupMenu(component, new VF1() { public void get(JPopupMenu menu) { try { callF(menuMaker, ref.get(), menu); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callF(menuMaker, ref!, menu);"; }}); } // onDrop: voidfunc(File), but may also return false static A jHandleFileDrop(A c, final Object onDrop) { new DropTarget(c, new DropTargetAdapter() { public void drop(DropTargetDropEvent e) { try { Transferable tr = e.getTransferable(); DataFlavor[] flavors = tr.getTransferDataFlavors(); for (DataFlavor flavor : flavors) { if (flavor.isFlavorJavaFileListType()) { e.acceptDrop(e.getDropAction()); File file = first((List) tr.getTransferData(flavor)); if (file != null && !isFalse(callF(onDrop, file))) e.dropComplete(true); return; } } } catch (Throwable __e) { _handleException(__e); } e.rejectDrop(); } }); return c; } static A jHandleFileDrop(Object onDrop, A c) { return jHandleFileDrop(c, onDrop); } static boolean loadBufferedImage_useImageCache = true; static BufferedImage loadBufferedImage(String snippetIDOrURLOrFile) { try { ping(); if (snippetIDOrURLOrFile == null) return null; if (isURL(snippetIDOrURLOrFile)) return imageIO_readURL(snippetIDOrURLOrFile); if (isAbsolutePath(snippetIDOrURLOrFile)) return loadBufferedImage(new File(snippetIDOrURLOrFile)); if (!isSnippetID(snippetIDOrURLOrFile)) throw fail("Not a URL or snippet ID or file: " + snippetIDOrURLOrFile); String snippetID = "" + parseSnippetID(snippetIDOrURLOrFile); IResourceLoader rl = vm_getResourceLoader(); if (rl != null) return loadBufferedImage(rl.loadLibrary(snippetID)); File dir = imageSnippetsCacheDir(); 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_http(snippetID); print("Loading image: " + imageURL); BufferedImage image = imageIO_readURL(imageURL); if (loadBufferedImage_useImageCache) { File tempFile = new File(dir, snippetID + ".tmp." + System.currentTimeMillis()); ImageIO.write(image, "png", tempFile); tempFile.renameTo(new File(dir, snippetID + ".png")); //Log.info("Cached image."); } //Log.info("Loaded image."); return image; } 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 JFrame showFullScreen(JComponent c) { return showFullScreen(defaultFrameTitle(), c); } static JFrame showFullScreen(final String title, final JComponent c) { return (JFrame) swingAndWait(new F0() { Object get() { try { GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice(); if (!gd.isFullScreenSupported()) throw fail("No full-screen mode supported!"); boolean dec = JFrame.isDefaultLookAndFeelDecorated(); if (dec) JFrame.setDefaultLookAndFeelDecorated(false); final JFrame window = new JFrame(title); window.setUndecorated(true); if (dec) JFrame.setDefaultLookAndFeelDecorated(true); registerEscape(window, new Runnable() { public void run() { try { disposeWindow(window) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "disposeWindow(window)"; }}); window.add(wrap(c)); gd.setFullScreenWindow(window); // 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 copyImageToClipboard(Image img) { TransferableImage trans = new TransferableImage(img); Toolkit.getDefaultToolkit().getSystemClipboard().setContents( trans, null); vmBus_send("newClipboardContents", img); print("Copied image to clipboard (" + img.getWidth(null) + "*" + img.getHeight(null) + " px)"); } static JComponent selectImageSnippet(VF1 onSelect) { return selectSnippetID_v1(onSelect); } static JComponent selectImageSnippet(String defaultID, VF1 onSelect) { return selectSnippetID_v1(defaultID, onSelect); } static BufferedImage cloneClipBufferedImage(BufferedImage src, Rectangle clip) { return cloneBufferedImage(clipBufferedImage(src, clip)); } static BufferedImage cloneClipBufferedImage(BufferedImage src, Rect r) { return cloneBufferedImage(clipBufferedImage(src, r)); } static BufferedImage cloneClipBufferedImage(BufferedImage src, int x, int y, int w, int h) { return cloneBufferedImage(clipBufferedImage(src, x, y, w, h)); } static BufferedImage getImageFromClipboard() { try { Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); if (t == null) return null; List l = (List) (getTransferData(t, DataFlavor.javaFileListFlavor)); if (nempty(l)) return loadImage2(first(l)); if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) return (BufferedImage) t.getTransferData(DataFlavor.imageFlavor); return imageFromDataURL(getTextFromClipboard()); } catch (Exception __e) { throw rethrow(__e); } } 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); } }); } static A _print(A a) { return print(a); } static void _print() { print(); } static boolean hasTransparency(BufferedImage img) { return img.getColorModel().hasAlpha(); } 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) { if (newW == img.getWidth() && newH == img.getHeight()) return img; 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 BufferedImage resizeImage(int newW, BufferedImage img) { return resizeImage(img, newW); } static A revalidate(final A c) { if (c == null || !c.isShowing()) return c; { 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..."; }}); } return c; } static void revalidate(JFrame f) { revalidate((Component) f); } static void revalidate(JInternalFrame f) { revalidate((Component) f); } static A repaint(A c) { if (c != null) c.repaint(); return c; } static Dimension getMinimumSize(final Component c) { return c == null ? null : swing(new F0() { Dimension get() { try { return c.getMinimumSize(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret c.getMinimumSize();"; }}); } static boolean imagesHaveSameSize(BufferedImage a, BufferedImage b) { return a != null && b != null && a.getWidth() == b.getWidth() && a.getHeight() == b.getHeight(); } static Dimension getPreferredSize(final Component c) { return c == null ? null : swing(new F0() { Dimension get() { try { return c.getPreferredSize(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret c.getPreferredSize();"; }}); } static Container getParent(final Component c) { return c == null ? null : swing(new F0() { Container get() { try { return c.getParent(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret c.getParent();"; }}); } 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 showFullScreenImageSurface(BufferedImage img) { showFullScreen(jscroll_centered(disposeFrameOnClick(new ImageSurface(img)))); } static Object[] toArray(Collection c) { return toObjectArray(c); } static A[] toArray(Collection c, Class type) { A[] a = arrayOfType(l(c), type); if (a.length == 0) return a; asList(c).toArray(a); return a; } static ListIterator listIterator(List l) { return l == null ? emptyListIterator() : l.listIterator(); } static String find(String pattern, String text) { Matcher matcher = Pattern.compile(pattern).matcher(text); if (matcher.find()) return matcher.group(1); return null; } static A find(Collection c, Object... data) { for (A x : c) if (checkFields(x, data)) return x; return null; } static A tripleAtPosition(T3 t, int i) { if (t == null) return null; if (i == 0) return t.a; if (i == 1) return t.b; if (i == 2) return t.c; return null; } static boolean inRange(int x, int n) { return x >= 0 && x < n; } static int limitToUByte(int i) { return max(0, min(255, i)); } static Color getBackground(final Component c) { return c == null ? null : swing(new F0() { Color get() { try { return c.getBackground(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret c.getBackground();"; }}); } static int asInt(Object o) { return toInt(o); } 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 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 String assertPossibleGlobalID(String s) { if (!possibleGlobalID(s)) throw fail("Not an acceptable global ID: " + s); return s; } static char charPlus(char a, int b) { return (char) (((int) a) + b); } // better modulo that gives positive numbers always static int mod(int n, int m) { return (n % m + m) % m; } static long mod(long n, long m) { return (n % m + m) % m; } static BigInteger mod(BigInteger n, int m) { return n.mod(bigint(m)); } static double mod(double n, double m) { return (n % m + m) % m; } static double negativeInfinity() { return Double.NEGATIVE_INFINITY; } 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 formatDouble(double d, int digits) { String format = digits <= 0 ? "0" : "0." + rep(digits, '#'); return new java.text.DecimalFormat(format, new java.text.DecimalFormatSymbols(Locale.ENGLISH)).format(d); } static void cancelTimers(Collection timers) { for (Object timer : timers) cancelTimer(timer); } static List getAndClearList(Collection l) { if (l == null) return emptyList(); synchronized(collectionMutex(l)) { List out = cloneList(l); l.clear(); return out; } } static int randomID_defaultLength = 12; static String randomID(int length) { return makeRandomID(length); } static String randomID() { return randomID(randomID_defaultLength); } static Set> web_relations(Web web) { LinkedHashSet> l = new LinkedHashSet(); for (WebNode n : web.nodes) if (n instanceof WebRelation) l.add(pair(((WebRelation) n).a, ((WebRelation) n).b)); return l; } static List tok_groupRoundBrackets(String s) { return tok_groupRoundBrackets(javaTok(s)); } static List tok_groupRoundBrackets(List tok) { while (true) { int i = tok.lastIndexOf("("); if (i < 0) return tok; int j = indexOf(tok, ")", i); if (j < 0) return tok; tok.set(i, join(subList(tok, i, j+1))); tok.subList(i+1, j+1).clear(); assertTrue(odd(l(tok))); } } static List dropFirstAndLast(int n, List l) { return new ArrayList(subList(l, n, l(l)-n)); } static List dropFirstAndLast(List l) { return dropFirstAndLast(1, l); } static String dropFirstAndLast(String s) { return substring(s, 1, l(s)-1); } static boolean aiUsing_enabled; static Set aiUsing_set = synchroTreeSet(); static String aiUsing(String s) { if (aiUsing_enabled) aiUsing_set.addAll(aggressivelyCollectPossibleGlobalIDs(s)); return s; } static void aiUsing(Object o) { // TODO } static List aiUsing(List l) { if (aiUsing_enabled) for (Object li : unnull(l)) aiUsing(li); return l; } static Lisp aiUsing(Lisp l) { if (aiUsing_enabled && l != null) { aiUsing(l.head); for (Lisp sub : l) aiUsing(sub); } return l; } static String conceptQuote(String s) { if (isIdentifier(s) || isInteger(s) || isProperlyQuoted(s)) return s; return quote(s); } static NavigableSet ai_mainIndexKeys() { return cachedNodeIndex_autoMake ? navigableKeys(cachedNodeIndex()) : tripleIndex().navigableMainKeys(); } 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; } // 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 Map synchroIdentityHashMap() { return synchroMap(new IdentityHashMap()); } 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 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 ThreadLocal imageGraphics_antiAlias = new ThreadLocal(); static Graphics2D imageGraphics(BufferedImage img) { return !isFalse(imageGraphics_antiAlias.get()) ? antiAliasGraphics(img) : createGraphics(img); } 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 Font sansSerif(int fontSize) { return new Font(Font.SANS_SERIF, Font.PLAIN, fontSize); } static int img_minOfWidthAndHeight(BufferedImage image) { return image == null ? 0 : min(image.getWidth(), image.getHeight()); } static int vectorLength(int x, int y) { return isqrt(sqr((long) x)+sqr((long) y)); } 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 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 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; } // changes & returns canvas static BufferedImage drawImageOnImage(BufferedImage img, BufferedImage canvas, int x, int y) { createGraphics(canvas).drawImage(img, x, y, null); return canvas; } static Font sansSerifBold(int fontSize) { return new Font(Font.SANS_SERIF, Font.BOLD, fontSize); } 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 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 List buttonsInGroup(ButtonGroup g) { if (g == null) return ll(); return asList(g.getElements()); } static String defaultFrameTitle() { return autoFrameTitle(); } static void defaultFrameTitle(String title) { autoFrameTitle_value = title; } static String makeFrame_defaultIcon; static boolean makeFrame_hideConsole; static ThreadLocal> makeFrame_post = new ThreadLocal(); 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) { final VF1 post = optParam(makeFrame_post); return swing(new F0() { JFrame get() { try { if (getFrame(content) != null) return getFrame(setFrameTitle((Component) content, title)); final JFrame frame = new JFrame(title); if (makeFrame_defaultIcon != null) setFrameIconLater(frame, makeFrame_defaultIcon); _initFrame(frame); Component wrapped = wrap(content); if (wrapped != null) frame.getContentPane().add(wrapped); frame.setBounds(defaultNewFrameBounds()); callF(post, frame); if (showIt) frame.setVisible(true); //callOpt(content, "requestFocus"); //exitOnFrameClose(frame); if (showIt && makeFrame_hideConsole) { hideConsole(); makeFrame_hideConsole = false; } return frame; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (getFrame(content) != null)\r\n ret getFrame(setFrameTitle((Component) ..."; }}); } static A onResize(A c, final Object r) { if (c != null && r != null) { swing(new Runnable() { public void run() { try { c.addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent e) { pcallF(r); } }); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "c.addComponentListener(new ComponentAdapter {\r\n public void componentRes..."; }}); } return c; } static A onResize(Object r, A c) { return onResize(c, r); } static A bindToComponent(final A component, final Runnable onShow, final Runnable onUnShow) { { swing(new Runnable() { public void run() { try { final Var < Boolean > flag = new Var(false); component.addAncestorListener(new AncestorListener() { public void ancestorAdded(AncestorEvent event) { if (flag.get()) print("Warning: bindToComponent logic failure"); flag.set(true); pcallF(onShow); } public void ancestorRemoved(AncestorEvent event) { if (!flag.get()) print("Warning: bindToComponent logic failure"); flag.set(false); pcallF(onUnShow); } public void ancestorMoved(AncestorEvent event) { } }); if (component.isShowing()) { // Hopefully this matches the AncestorListener logic flag.set(true); pcallF(onShow); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "final Var flag = new(false);\r\n component.addAncestorListener(new ..."; }}); } return component; } static A bindToComponent(A component, Runnable onShow) { return bindToComponent(component, onShow, null); } static A firstInstance(Collection c, Class type) { return firstOfType(c, type); } static void swingNowOrLater(Runnable r) { if (isAWTThread()) r.run(); else swingLater(r); } static BufferedImage asBufferedImage(Object o) { BufferedImage bi = toBufferedImageOpt(o); if (bi == null) throw fail(getClassName(o)); return bi; } static Object findComponentPopupMenuListener_gen(final JComponent c) { return c == null ? null : swing(new F0() { Object get() { try { return firstWithClassShortNamed("componentPopupMenu_Adapter", c.getMouseListeners()); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret firstWithClassShortNamed('componentPopupMenu_Adapter, c.getMouseListeners..."; }}); } static boolean internalFrameActive(Component c) { final JInternalFrame f = getInternalFrame(c); return f != null && swing(new F0() { Boolean get() { try { return f.isSelected(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret f.isSelected();"; }}); } static void fillJMenu(final JMenu m, Object... x) { //ifdef fillJMenu_debug //print("fillJMenu " + m); //endifdef 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 (isMenuSeparatorIndicator(o)) m.addSeparator(); else if (o instanceof LiveValue && ((LiveValue) o).getType() == String.class && isRunnableX(y)) { final LiveValue lv = (LiveValue) o; final JMenuItem mi = jmenuItem(or2(unCurlyBracket(lv.get()), "..."), y); bindLiveValueListenerToComponent(mi, lv, new Runnable() { public void run() { try { String s = lv.get(); if (isCurlyBracketed(s)) { setEnabled(mi, false); s = unCurlyBracket(s); } else setEnabled(mi, true); setText(mi, s); revalidate(m); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "String s = lv.get();\r\n if (isCurlyBracketed(s)) {\r\n setEnable..."; }}); print("bound live value " + lv + " to menu item " + mi); m.add(mi); ++i; } 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 if (o == null && y instanceof Runnable) ++i; // text == null => disabled item else print("Unknown menu item: " + o); } } static boolean jmenuItem_newThreads; static JMenuItem jmenuItem(final String text) { return jMenuItem(text, null); } static JMenuItem jmenuItem(final String text, final Object r) { return swing(new F0() { JMenuItem get() { try { Pair p = jmenu_autoMnemonic(dropPrefix("[disabled] ", text)); JMenuItem mi = new JMenuItem(p.a); if (startsWith(text, "[disabled] ")) disableMenuItem(mi); if (p.b != 0) mi.setMnemonic(p.b); mi.addActionListener(jmenuItem_newThreads ? actionListenerInNewThread(r) : actionListener(r)); return mi; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Pair p = jmenu_autoMnemonic(dropPrefix(\"[disabled] \", text));\r\n JM..."; }}); } static MenuItem menuItem(String text, final Object r) { MenuItem mi = new MenuItem(text); mi.addActionListener(actionListener(r)); return mi; } static BufferedImage loadPNG(File file) { return loadBufferedImage(file); } static String toUpper(String s) { return s == null ? null : s.toUpperCase(); } static List toUpper(Collection s) { return allToUpper(s); } // jsoup static String googleImageSearch_new_userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"; static int googleImageSearch_new_timeout = 10*1000; static BufferedImage googleImageSearch_new(String q) { String html = googleImageSearch_new_loadPage_cached(q); String group = regexpFirstGroup("image/jpeg;base64,([a-zA-Z0-9/+]*)={0,2}", html); return group == null ? null : imageFromBytes(base64decode(dropLast(l(group) & 3, group))); } static String googleImageSearch_new_loadPage_cached(String q) { File f = googleImageSearch_htmlCacheFile(q); if (fileSize(f) > 0) return loadTextFile(f); String html = str(googleImageSearch_new_loadPage(q)); saveTextFile(f, html); return html; } static Document googleImageSearch_new_loadPage(String q) { try { String googleUrl = "https://www.google.com/search?tbm=isch&q=" + urlencode(q); print("Googling " + quote(q)); return Jsoup.connect(googleUrl).userAgent(googleImageSearch_new_userAgent).timeout(googleImageSearch_new_timeout).get(); } catch (Exception __e) { throw rethrow(__e); } } 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()); } // It's the super precise version! static double roundToOneHundredth(double d) { return new BigDecimal(d).setScale(2, RoundingMode.HALF_UP).doubleValue(); } static Collection copyList(Collection a, Collection b) { if (a != null && b != null) { b.clear(); b.addAll(a); } return a; } 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 JTextField jTextField() { return jTextField(""); } static JTextField jTextField(final String text) { return swing(new F0() { JTextField get() { try { JTextField tf = new JTextField(unnull(text)); standardTextFieldPopupMenu(tf); jenableUndoRedo(tf); tf.selectAll(); return tf; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JTextField tf = new JTextField(unnull(text));\r\n standardTextFieldPopupMenu..."; }}); } static JTextField jTextField(Object o) { return jTextField(strOrEmpty(o)); } static JDesktopPane mainDesktopPane_value; static JDesktopPane mainDesktopPane() { return mainDesktopPane_value; } static JPanel showInternalFrameFormTitled(final JDesktopPane desktop, final String title, final Object... _parts) { JPanel panel = showForm_makePanel(true, _parts); showForm_makeInternalFrame(desktop, title, panel); return panel; } static JFrame showForm_makeFrame(String title, JPanel panel) { return handleEscapeKey(minFrameWidth(showPackedFrame(title, withMargin(panel)), 400)); } static List showForm_arrange1(List> l) { int minW = showForm_leftWidth(l); List out = new ArrayList(); for (List row : l) out.add(westAndCenter(withRightMargin(showForm_gapBetweenColumns, jMinWidth(minW, first(row))), second(row))); return out; } static List> showForm_makeComponents(final Boolean internalFrame, Object... _parts) { List> l = new ArrayList(); List parts = asList(_parts); JButton submitButton = null; for (int i = 0; i < l(parts); i++) { final Object o = parts.get(i), next = get(parts, i+1); if (o instanceof String && next instanceof Component) setComponentID((Component) next, (String) o); if (o instanceof Component || o instanceof String || next instanceof Component) { // smartAdd accepts strings l.add(mapLL("wrapForSmartAdd", o == null ? new JPanel() : o instanceof String ? humanizeFormLabel((String) o) : o, next)); if (next instanceof JButton && submitButton == null) submitButton = (JButton) next; i++; } else if (isRunnable(o)) l.add(mapLL("wrapForSmartAdd",null, submitButton = jbutton(showFormSubmitButtonName(), new Runnable() { public void run() { try { Object result = call(o); if (neq(Boolean.FALSE, result)) { if (isTrue(internalFrame)) disposeInternalFrame(heldInstance(JButton.class)); else if (isFalse(internalFrame)) disposeFrame(heldInstance(JButton.class)); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Object result = call(o);\r\n if (neq(Boolean.FALSE, result)) {\r\n ..."; }}))); else print("showForm: Unknown element type: " + getClassName(o)); } if (submitButton != null) { final JButton _submitButton = submitButton; onEnterInAllTextFields(concatLists(l), new Runnable() { public void run() { try { clickButton(_submitButton) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "clickButton(_submitButton)"; }}); } // massage labels for (List row : l) { JComponent left = first(row); if (left instanceof JLabel) makeBold((JLabel) left).setVerticalAlignment(JLabel.TOP); } return l; } static int vstackWithSpacing_default = 10; static JPanel vstackWithSpacing(final List parts) { return vstackWithSpacing(parts, vstackWithSpacing_default); } static JPanel vstackWithSpacing(final List parts, final int spacing) { return swing(new F0() { JPanel get() { try { JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.weightx = 1; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.insets = new Insets(spacing/2, 0, spacing/2, 0); // well... smartAddWithLayout(panel, gbc, toObjectArray(nonNulls(parts))); //gbc = (GridBagConstraints) gbc.clone(); //gbc.fill = GridBagConstraints.BOTH; gbc.weighty = 1; gbc.insets = new Insets(0, 0, 0, 0); panel.add(jrigid(), gbc); return panel; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JPanel panel = new JPanel(new GridBagLayout);\r\n new GridBagConstraints gbc..."; }}); } static JPanel vstackWithSpacing(Component... parts) { return vstackWithSpacing(asList(parts), vstackWithSpacing_default); } static AutoCloseable tempDisposeWindow(final Window w) { return new AutoCloseable() { public void close() { disposeWindow(w); } }; } static boolean showAnimationInTopRightCorner_alwaysOnTop = true; static boolean showAnimationInTopRightCorner_on = true; // automatically switches to AWT thread for you // text is optional text below image static JWindow showAnimationInTopRightCorner(String imageID, String text) { if (isHeadless() || !showAnimationInTopRightCorner_on) return null; return showAnimationInTopRightCorner(imageIcon(imageID), text); } static JWindow showAnimationInTopRightCorner(final Image image, final String text) { if (image == null || isHeadless() || !showAnimationInTopRightCorner_on) return null; return showAnimationInTopRightCorner(imageIcon(image), text); } static JWindow showAnimationInTopRightCorner(final ImageIcon imageIcon, final String text) { if (isHeadless() || !showAnimationInTopRightCorner_on) return null; return (JWindow) swingAndWait(new F0() { 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 String getText(final AbstractButton c) { return c == null ? "" : (String) swingAndWait(new F0() { Object get() { try { return c.getText(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret c.getText();"; }}); } static String getText(final JTextComponent c) { return c == null ? "" : (String) swingAndWait(new F0() { Object get() { try { return c.getText(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret c.getText();"; }}); } static String getText(final JLabel l) { return l == null ? "" : (String) swingAndWait(new F0() { Object get() { try { return l.getText(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret l.getText();"; }}); } // returns the contents of text field for editable combo box static String getText(final JComboBox cb) { if (cb == null) return null; if (isEditableComboBox(cb)) return unnull((String) cb.getEditor().getItem()); else return str(cb.getSelectedItem()); } static double sqrt(double x) { return Math.sqrt(x); } static long sqr(long l) { return l*l; } static double sqr(double d) { return d*d; } // 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) { if (component == null) return null; return (Timer) swingAndWait(new F0() { Object get() { try { final Var timer = new Var(); timer.set(new Timer(toInt(delay), new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent _evt) { try { AutoCloseable __1320 = tempActivity(r); try { try { if (!allPaused()) if (isFalse(callF(r))) cancelTimer(timer.get()); } catch (Throwable __e) { _handleException(__e); } } finally { _close(__1320); }} 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(RootPaneContainer frame, long delay, long firstDelay, Object r) { return installTimer(frame.getRootPane(), delay, firstDelay, r); } 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 A restructure(A a) { return (A) unstructure(structure(a)); } 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 A collectionGet(Collection c, int idx) { if (c == null || idx < 0 || idx >= l(c)) return null; if (c instanceof List) return listGet((List) c, idx); Iterator it = c.iterator(); for (int i = 0; i < idx; i++) if (it.hasNext()) it.next(); else return null; return it.hasNext() ? it.next() : null; } static boolean isLocalSnippetID(String snippetID) { return isSnippetID(snippetID) && isLocalSnippetID(psI(snippetID)); } static boolean isLocalSnippetID(long snippetID) { return snippetID >= 1000 && snippetID <= 9999; } static String localSnippetTitle(String snippetID) { if (!isLocalSnippetID(snippetID)) return null; File f = localSnippetFile(snippetID); if (!f.exists()) return null; return or2(getFileInfoField(dropExtension(f), "Title"), "Unnamed"); } static File snippetTitle_cacheFile(String snippetID) { return javaxCachesDir("Snippet Titles/" + psI(snippetID)); } static String getSnippetTitle(String id) { try { if (id == null) return null; if (!isSnippetID(id)) return "?"; IResourceLoader rl = vm_getResourceLoader(); if (rl != null) return rl.getSnippetTitle(id); if (isLocalSnippetID(id)) return localSnippetTitle(id); long parsedID = parseSnippetID(id); String url; if (isImageServerSnippet(parsedID)) url = imageServerURL() + "title/" + parsedID + muricaCredentialsQuery(); else if (isGeneralFileServerSnippet(parsedID)) url = "http://butter.botcompany.de:8080/files/name/" + parsedID; else url = tb_mainServer() + "/tb-int/getfield.php?id=" + parsedID + "&field=title" + standardCredentials_noCookies(); String title = trim(loadPageSilently(url)); if (title != null) try { saveTextFileIfChanged(snippetTitle_cacheFile(id), title); } catch (Throwable __e) { print(exceptionToStringShort(__e)); } return or(title, "?"); } catch (Exception __e) { throw rethrow(__e); } } static String getSnippetTitle(long id) { return getSnippetTitle(fsI(id)); } static int tripleHashCodeByContentsIC(T3 t) { return t == null ? 0 : lowerHashCode(t.a) + 2*lowerHashCode(t.b) - 4*lowerHashCode(t.c); } static boolean tripleEqualsByContentsIC(T3 a, T3 b) { return a == null ? b == null : eqic(a.a, b.a) && eqic(a.b, b.b) && eqic(a.c, b.c); } static List combineLists(List a, List b) { if (empty(a)) return b; if (empty(b)) return a; if (!(a instanceof CombinedList)) a = combinedList_ll(a); ((CombinedList) a).addList(b); return a; } static A syncGet(List l, int idx) { if (l == null || idx < 0) return null; synchronized(l) { return idx < l(l) ? l.get(idx) : null; } } static B syncGet(Map map, A a) { if (map == null) return null; synchronized(map) { return map.get(a); } } static WebNode ai_tripleToWebNode(TripleWeb w, String searchTerm, int occurrence) { if (w == null) return null; Web web = webFromTripleWeb(w); try { for (int i = 0; i < l(web.nodes); i++) if (eqic(web_sym(web.nodes.get(i)), searchTerm)) { if (occurrence-- == 0) return web.nodes.get(i); } return null; } catch (Throwable e) { print("ai_tripleToWebNode: " + e + " " + sfu(w) + " => " + webToStringShort(web)); throw rethrow(e); } } static boolean ai_tripleToTripleRef_debug; static TripleRef ai_tripleToTripleRef(TripleWeb w, String searchTerm, int occurrence) { if (ai_tripleToTripleRef_debug) { print("ai_tripleToTripleRef: " + w + " " + searchTerm + " " + occurrence); //print("a: " + symbolToStringFull(w.a)); //print("searchTerm: " + symbolToStringFull(searchTerm)); print("eq: "+ eqic(w.a, searchTerm) + " " + eqic(w.b, searchTerm) + " " + eqic(w.c, searchTerm)); } if (w == null) return null; // occurrence count magic if (occurrence < 3) for (int i = 0; i < 3; i++) if (eqic(tripleGet(w, i), searchTerm)) { if (occurrence-- == 0) { if (ai_tripleToTripleRef_debug) print("ai_tripleToTripleRef returning: " + i); return tripleRef(w, i); } } return null; } static List ai_tripleListeners() { return cloneList(ai_addTripleListener_l); } // f: A -> Comparable static List sortInPlaceByCalculatedFieldDesc_f1(List l, final F1 f) { sort(l, new Comparator() { public int compare(A a, A b) { return stdcompare(f.get(b), f.get(a)); } }); return l; } static boolean ai_isPriorityTriple(TripleWeb w) { return swic(w.b, "[priority] "); } static int imageIcon_cacheSize = 10; static boolean imageIcon_verbose; static Map imageIcon_cache; static Lock imageIcon_lock = lock(); static ThreadLocal imageIcon_fixGIF = new ThreadLocal(); // not going through BufferedImage preserves animations static ImageIcon imageIcon(String imageID) { try { if (imageID == null) return null; Lock __950 = imageIcon_lock; lock(__950); try { if (imageIcon_cache == null) imageIcon_cache = new MRUCache(imageIcon_cacheSize); imageID = fsI(imageID); ImageIcon ii = imageIcon_cache.get(imageID); if (ii == null) { if (imageIcon_verbose) print("Loading image icon: " + imageID); File f = loadBinarySnippet(imageID); Boolean b = imageIcon_fixGIF.get(); if (!isFalse(b)) ii = new ImageIcon(loadBufferedImageFixingGIFs(f)); else ii = new ImageIcon(f.toURI().toURL()); } else imageIcon_cache.remove(imageID); // move to front of cache on access imageIcon_cache.put(imageID, ii); return ii; } finally { unlock(__950); } } catch (Exception __e) { throw rethrow(__e); } } // doesn't fix GIFs static ImageIcon imageIcon(File f) { try { return new ImageIcon(f.toURI().toURL()); } catch (Exception __e) { throw rethrow(__e); } } static ImageIcon imageIcon(Image img) { return new ImageIcon(img); } static ImageIcon imageIcon(RGBImage img) { return imageIcon(img.getBufferedImage()); } static BufferedImage imageIO_readURL(String url) { try { if (startsWith(url, "https:")) // Java is still buggy there :( disableCertificateValidation(); return ImageIO.read(new URL(url)); } catch (Exception __e) { throw rethrow(__e); } } static boolean isAbsolutePath(String s) { return s != null && new File(s).isAbsolute(); } static boolean isAbsolutePath(File f) { return f != null && f.isAbsolute(); } static IResourceLoader vm_getResourceLoader() { return proxy(IResourceLoader.class, vm_generalMap_get("_officialResourceLoader")); } static File imageSnippetsCacheDir() { return javaxCachesDir("Image-Snippets"); } static String snippetImageURL_http(String snippetID) { return snippetImageURL_http(snippetID, "png"); } static String snippetImageURL_http(String snippetID, String contentType) { return replacePrefix("https://", "http://", snippetImageURL(snippetID, contentType)).replace(":8443", ":8080"); } static void registerEscape(JFrame frame, final Runnable r) { registerEscape_rootPane(frame.getRootPane(), r); } static JComponent selectSnippetID_v1(final VF1 onSelect) { return selectSnippetID_v1("#", onSelect); } static JComponent selectSnippetID_v1(String defaultID, final VF1 onSelect) { final JTextField tfSnippetID = jtextfield(defaultID); if (eq(defaultID, "#")) moveCaretToEnd(tfSnippetID); JComponent panel; renameSubmitButton(panel = showTitledForm("Select Snippet", "Snippet ID:", tfSnippetID, runnableThread(new Runnable() { public void run() { try { callF(onSelect, fsI(getTextTrim(tfSnippetID))); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callF(onSelect, fsI(getTextTrim(tfSnippetID)));"; }})), "Select snippet"); return panel; } static BufferedImage cloneBufferedImage(BufferedImage image) { return copyImage(image); } static BufferedImage clipBufferedImage(BufferedImage src, Rectangle clip) { return clipBufferedImage(src, new Rect(clip)); } static BufferedImage clipBufferedImage(BufferedImage src, Rect r) { if (src == null) return null; // fixClipRect r = intersectRects(r, new Rect(0, 0, src.getWidth(), src.getHeight())); if (rectEmpty(r)) return null; // can't make zero-sized BufferedImage return src.getSubimage(r.x, r.y, r.w, r.h); } static BufferedImage clipBufferedImage(BufferedImage src, int x, int y, int w, int h) { return clipBufferedImage(src, new Rect(x, y, w, h)); } static Object getTransferData(Transferable t, DataFlavor flavor) { try { return t != null && t.isDataFlavorSupported(flavor) ? t.getTransferData(flavor) : null; } catch (Exception __e) { throw rethrow(__e); } } 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 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 JScrollPane jscroll_centered(Component c) { return jscroll(jFullCenter(c)); } 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 ListIterator emptyListIterator() { return Collections.emptyListIterator(); } static Set ai_addTripleListener_l = synchroHashSet(); static void ai_addTripleListener(TripleListener l) { ai_addTripleListener_l.add(l); } 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 void cancelTimer(javax.swing.Timer timer) { if (timer != null) timer.stop(); } static void cancelTimer(java.util.Timer timer) { if (timer != null) timer.cancel(); } static void cancelTimer(Object o) { if (o instanceof java.util.Timer) cancelTimer((java.util.Timer) o); else if (o instanceof javax.swing.Timer) cancelTimer((javax.swing.Timer) o); else if (o instanceof AutoCloseable) { try { ((AutoCloseable) o).close(); } catch (Throwable __e) { _handleException(__e); }} } static Set synchroTreeSet() { return Collections.synchronizedSet(new TreeSet()); } static Set aggressivelyCollectPossibleGlobalIDs(String s) { LinkedHashSet ids = new LinkedHashSet(); if (s == null) return ids; for (int i = 0; i < l(s); i++) { int j = i; while (j < l(s) && Character.isLowerCase(s.charAt(j))) ++j; if (j-i == 16) ids.add(substring(s, i, j)); i = j; } return ids; } static Graphics2D antiAliasGraphics(BufferedImage img) { return antiAliasOn(createGraphics(img)); } static int isqrt(double x) { return iround(Math.sqrt(x)); } static double pi() { return Math.PI; } 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 BufferedImage createBufferedImage(int w, int h, Color color) { return newBufferedImage(w, h, color); } static String autoFrameTitle_value; static String autoFrameTitle() { return autoFrameTitle_value != null ? autoFrameTitle_value : getProgramTitle(); } static void autoFrameTitle(Component c) { setFrameTitle(getFrame(c), autoFrameTitle()); } static String programTitle() { return getProgramName(); } static A setFrameTitle(A c, final String title) { final Frame f = getAWTFrame(c); if (f != null) { swing(new Runnable() { public void run() { try { f.setTitle(title); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "f.setTitle(title);"; }}); } return c; } 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; } static JFrame setFrameIconLater(Component c, final String imageID) { final JFrame frame = getFrame(c); if (frame != null) startThread("Loading Icon", new Runnable() { public void run() { try { final Image i = imageIcon(or2(imageID, "#1005557")).getImage(); swingLater(new Runnable() { public void run() { try { frame.setIconImage(i); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "frame.setIconImage(i);"; }}); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "final Image i = imageIcon(or2(imageID, \"#1005557\")).getImage();\r\n swingL..."; }}); return frame; } static void _initFrame(JFrame f) { myFrames_list.put(f, Boolean.TRUE); standardTitlePopupMenu(f); } 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 void hideConsole() { final JFrame frame = consoleFrame(); if (frame != null) { autoVMExit(); swingLater(new Runnable() { public void run() { try { frame.setVisible(false); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "frame.setVisible(false);"; }}); } } static A firstOfType(Collection c, Class type) { for (Object x : c) if (isInstanceX(type, x)) return (A) x; return null; } static BufferedImage toBufferedImageOpt(Object o) { if (o instanceof BufferedImage) return (BufferedImage) o; if (o instanceof MakesBufferedImage) return ((MakesBufferedImage) o).getBufferedImage(); if (o instanceof File) if (isImageFile(((File) o))) return loadImage2(((File) o)); String c = getClassName(o); // Keep this because it also works on imported objects 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 A firstWithClassShortNamed(String shortName, Iterable l) { if (l != null) for (A o : l) if (eq(shortClassName(o), shortName)) return o; return null; } static A firstWithClassShortNamed(String shortName, A[] l) { if (l != null) for (A o : l) if (eq(shortClassName(o), shortName)) return o; return null; } static JInternalFrame getInternalFrame(final Object _o) { return _o == null ? null : swing(new F0() { JInternalFrame get() { try { Object o = _o; if (o instanceof ButtonGroup) o = first(buttonsInGroup((ButtonGroup) o)); if (!(o instanceof Component)) return null; Component c = (Component) o; while (c != null) { if (c instanceof JInternalFrame) return (JInternalFrame) c; c = c.getParent(); } return null; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "O o = _o;\r\n if (o instanceof ButtonGroup) o = first(buttonsInGroup((Button..."; }}); } 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 boolean isMenuSeparatorIndicator(Object o) { return eqOneOf(o, "***", "---", "===", ""); } 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 String unCurlyBracket(String s) { return tok_unCurlyBracket(s); } static A bindLiveValueListenerToComponent(A component, final LiveValue lv, final Runnable listener) { if (lv != null) bindToComponent(component, new Runnable() { public void run() { try { lv.onChangeAndNow(listener); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ifdef bindLiveValueListenerToComponent_debug\r\n print(\"bindLiveValueL..."; }}, new Runnable() { public void run() { try { lv.removeOnChangeListener(listener) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "lv.removeOnChangeListener(listener)"; }}); return component; } static boolean isCurlyBracketed(String s) { return isCurlyBraced(s); } static A setEnabled(A c, boolean enable) { if (c != null) { swing(new Runnable() { public void run() { try { c.setEnabled(enable); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "c.setEnabled(enable);"; }}); } return c; } static A setEnabled(boolean enable, A c) { return setEnabled(c, enable); } static JMenuItem jMenuItem(final String text) { return jmenuItem(text); } static JMenuItem jMenuItem(String text, Object r) { return jmenuItem(text, r); } static Pair jmenu_autoMnemonic(String s) { int i = indexOf(s, '&'); if (i >= 0 && i < l(s) && isLetterOrDigit(s.charAt(i+1))) return pair(substring(s, 0, i) + substring(s, i+1), (int) s.charAt(i+1)); return pair(s, 0); } static JMenuItem disableMenuItem(final JMenuItem mi) { if (mi != null) { swing(new Runnable() { public void run() { try { mi.setEnabled(false); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "mi.setEnabled(false);"; }}); } return mi; } static ActionListener actionListenerInNewThread(final Object runnable) { return actionListenerInNewThread(runnable, null); } static ActionListener actionListenerInNewThread(final Object runnable, final Object instanceToHold) { if (runnable instanceof ActionListener) return (ActionListener) runnable; return new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent _evt) { try { startThread("Action Listener", new Runnable() { public void run() { try { AutoCloseable __1366 = holdInstance(instanceToHold); try { callF(runnable); } finally { _close(__1366); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "AutoCloseable __1366 = holdInstance(instanceToHold); try {\r\n callF(runna..."; }}); } catch (Throwable __e) { messageBox(__e); }}}; } static List allToUpper(Collection l) { List x = new ArrayList(l(l)); if (l != null) for (String s : l) x.add(upper(s)); return x; } static String regexpFirstGroup(String pat, String s) { Matcher m = regexpMatcher(pat, s); if (m.find()) return m.group(1); else return null; } static BufferedImage imageFromBytes(byte[] b) { try { return b == null ? null : ImageIO.read(new ByteArrayInputStream(b)); } catch (Exception __e) { throw rethrow(__e); } } 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 File googleImageSearch_htmlCacheFile(String q) { return javaxCachesDir("Google Image Search/" + uniqueFileNameUsingMD5_80_v2(upper(q)) + ".html"); } static long fileSize(String path) { return getFileSize(path); } static long fileSize(File f) { return getFileSize(f); } public static File mkdirsFor(File file) { return mkdirsForFile(file); } static Set fieldNames(Object o) { return allFields(o); } static boolean isUpperCase(char c) { return Character.isUpperCase(c); } static JTextField standardTextFieldPopupMenu(final JTextField tf) { final WeakReference ref = weakRef(tf); componentPopupMenuItem(tf, "Copy text to clipboard", new Runnable() { public void run() { try { copyTextToClipboard(ref.get().getText()) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "copyTextToClipboard(ref.get().getText())"; }}); componentPopupMenuItem(tf, "Paste", new Runnable() { public void run() { try { ref.get().paste() ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ref.get().paste()"; }}); return tf; } static String strOrEmpty(Object o) { return o == null ? "" : str(o); } static JInternalFrame showForm_makeInternalFrame(JDesktopPane desktop, String title, JPanel panel) { JInternalFrame f = addInternalFrame(desktop, title, withMargin(panel)); minInternalFrameWidth(f, 400); packInternalFrameVertically(f); centerInternalFrame(f); // TODO: handleEscapeKey(f); return f; } 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 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 JFrame showPackedFrame(String title, Component contents) { return packFrame(showFrame(title, contents)); } static JFrame showPackedFrame(Component contents) { return packFrame(showFrame(contents)); } static int showForm_leftWidth(List> l) { int minW = 0; for (List row : l) minW = max(minW, getMinimumSize(first(row)).width); return minW; } static JPanel westAndCenter(final Component w, final Component c) { return swing(new F0() { JPanel get() { try { JPanel panel = new JPanel(new BorderLayout()); panel.add(BorderLayout.WEST, wrap(w)); panel.add(BorderLayout.CENTER, wrap(c)); return panel; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JPanel panel = new JPanel(new BorderLayout);\r\n panel.add(BorderLayout.WEST..."; }}); } static int withRightMargin_defaultWidth = 6; static JPanel withRightMargin(Component c) { return withRightMargin(withRightMargin_defaultWidth, c); } static JPanel withRightMargin(final int w, final Component c) { return swing(new F0() { JPanel get() { try { JPanel p = new JPanel(new BorderLayout()); p.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, w)); p.add(c); return p; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JPanel p = new JPanel(new BorderLayout);\r\n p.setBorder(BorderFactory.creat..."; }}); } static A jMinWidth(final int w, final A c) { if (c == null) return null; return swing(new F0() { A get() { try { Dimension size = c.getMinimumSize(); c.setMinimumSize(new Dimension(/*max(w, size.width) ??? */w, size.height)); return jPreferWidth(w, c); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Dimension size = c.getMinimumSize();\r\n c.setMinimumSize(new Dimension(/*ma..."; }}); } static void setComponentID(Component c, String id) { if (c != null) componentID_map.put(c, id); } static List mapLL(Object f, Object... data) { return map(f, ll(data)); } static Component wrapForSmartAdd(Object o) { if (o == null) return jpanel(); if (o instanceof String) return jlabel((String) o); return wrap(o); } static Map humanizeFormLabel_replacements = litmap("id" , "ID", "md5" , "MD5"); static String humanizeFormLabel(String s) { if (containsSpace(s)) return s; return firstToUpper( joinWithSpace(replaceElementsUsingMap(splitCamelCase(s), humanizeFormLabel_replacements)).replace("I D", "ID") ); } static boolean isRunnable(Object o) { return o instanceof Runnable || hasMethod(o, "get"); } 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, O action) { ret setButtonImage(img, jbutton("", action)); }*/ static JButton jbutton(Action action) { return swingNu(JButton.class, action); } static String showFormSubmitButtonName() { return "Submit"; } static void disposeInternalFrame(Component c) { final JInternalFrame f = getInternalFrame(c); if (f != null) { swing(new Runnable() { public void run() { try { f.dispose(); setOpt(f, "lastFocusOwner" , null); // Help GC } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "f.dispose();\r\n setOpt(f, lastFocusOwner := null); // Help GC"; }}); } } static void disposeFrame(final Component c) { disposeWindow(c); } static void onEnterInAllTextFields(JComponent c, Object action) { if (action == null) return; for (Component tf : allChildren(c)) onEnterIfTextField(tf, action); } static void onEnterInAllTextFields(List c, Object action) { for (Object o : unnull(c)) if (o instanceof JComponent) onEnterInAllTextFields((JComponent) o, action); } static void clickButton(final JButton b) { if (b != null) { swing(new Runnable() { public void run() { try { if (b.isEnabled()) b.doClick(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (b.isEnabled())\r\n b.doClick();"; }}); } } static A makeBold(final A c) { if (c != null) { swing(new Runnable() { public void run() { try { c.setFont(c.getFont().deriveFont(Font.BOLD)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "c.setFont(c.getFont().deriveFont(Font.BOLD));"; }}); } return c; } static JPanel smartAddWithLayout(JPanel panel, Object layout, List parts) { for (Object o : parts) panel.add(wrapForSmartAdd(o), layout); return panel; } static JPanel smartAddWithLayout(JPanel panel, Object layout, Object... parts) { return smartAddWithLayout(panel, layout, asList(flattenArray2(parts))); } static List nonNulls(List l) { return withoutNulls(l); } static List nonNulls(A[] l) { return withoutNulls(l); } static Map nonNulls(Map map) { return withoutNulls(map); } static Component jrigid() { return javax.swing.Box.createRigidArea(new Dimension(0, 0)); } static JWindow showInTopRightCorner(Component c) { JWindow w = new JWindow(); w.add(c); w.pack(); moveToTopRightCorner(w); w.setVisible(true); return w; } static boolean isEditableComboBox(final JComboBox cb) { return cb != null && swing(new F0() { Boolean get() { try { return cb.isEditable(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret cb.isEditable();"; }}); } static boolean allPaused() { return ping_pauseAll; } 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 A listGet(List l, int idx) { return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null; } static File localSnippetFile(long snippetID) { return localSnippetsDir(snippetID + ".text"); } static File localSnippetFile(String snippetID) { return localSnippetFile(parseSnippetID(snippetID)); } static String getFileInfoField(File f, String field) { return getOneLineFileInfoField(f, field); } static File dropExtension(File f) { return f == null ? null : fileInSameDir(f, dropExtension(f.getName())); } static String dropExtension(String s) { return takeFirst(s, smartLastIndexOf(s, '.')); } 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 File javaxCachesDir(String sub) { return newFile(javaxCachesDir(), sub); } static String imageServerURL() { return or2(trim(loadTextFile(javaxDataDir("image-server-url.txt"))), "http://botcompany.de/images/raw/"); } static String muricaCredentialsQuery() { return htmlQuery(muricaCredentials()); } static boolean isGeneralFileServerSnippet(long id) { return id >= 1400000 && id < 1500000; } static String standardCredentials_noCookies() { return standardCredentials() + "&noCookies=1"; } static boolean saveTextFileIfChanged(File f, String contents) { return saveTextFileIfDifferent(f, contents); } static int lowerHashCode(String s) { return s == null ? 0 : lower(s).hashCode(); } static Web webFromTripleWeb(TripleWeb w) { return webFromTriple(w); } static A tripleGet(T3 t, int i) { if (t == null) return null; if (i == 0) return t.a; if (i == 1) return t.b; if (i == 2) return t.c; return null; } static TripleRef tripleRef(T3 t, int position) { if (position == 0) return new TripleA(t); if (position == 1) return new TripleB(t); if (position == 2) return new TripleC(t); throw fail("crazy position"); } static File loadBinarySnippet(String snippetID) { try { IResourceLoader rl = vm_getResourceLoader(); if (rl != null) return rl.loadLibrary(snippetID); long id = parseSnippetID(snippetID); if (isImageServerSnippet(id)) return loadImageAsFile(snippetID); File f = DiskSnippetCache_getLibrary(id); if (fileSize(f) == 0) f = loadDataSnippetToFile(snippetID); return f; } catch (Exception __e) { throw rethrow(__e); } } static boolean loadBufferedImageFixingGIFs_debug; static ThreadLocal> loadBufferedImageFixingGIFs_output = new ThreadLocal(); static Image loadBufferedImageFixingGIFs(File file) { try { if (!file.exists()) return null; // Load anything but GIF the normal way if (!isGIF(file)) return ImageIO.read(file); if (loadBufferedImageFixingGIFs_debug) print("loadBufferedImageFixingGIFs" + ": checking gif"); // Get GIF reader ImageReader reader = ImageIO.getImageReadersByFormatName("gif").next(); // Give it the stream to decode from reader.setInput(ImageIO.createImageInputStream(file)); int numImages = reader.getNumImages(true); // Get 'metaFormatName'. Need first frame for that. IIOMetadata imageMetaData = reader.getImageMetadata(0); String metaFormatName = imageMetaData.getNativeMetadataFormatName(); // Find out if GIF is bugged boolean foundBug = false; for (int i = 0; i < numImages && !foundBug; i++) { // Get metadata IIOMetadataNode root = (IIOMetadataNode)reader.getImageMetadata(i).getAsTree(metaFormatName); // Find GraphicControlExtension node int nNodes = root.getLength(); for (int j = 0; j < nNodes; j++) { org.w3c.dom.Node node = root.item(j); if (node.getNodeName().equalsIgnoreCase("GraphicControlExtension")) { // Get delay value String delay = ((IIOMetadataNode)node).getAttribute("delayTime"); // Check if delay is bugged if (Integer.parseInt(delay) == 0) { foundBug = true; } break; } } } if (loadBufferedImageFixingGIFs_debug) print("loadBufferedImageFixingGIFs" + ": " + f2s(file) + " foundBug=" + foundBug); // Load non-bugged GIF the normal way Image image; if (!foundBug) { image = Toolkit.getDefaultToolkit().createImage(f2s(file)); } else { // Prepare streams for image encoding ByteArrayOutputStream baoStream = new ByteArrayOutputStream(); { ImageOutputStream ios = ImageIO.createImageOutputStream(baoStream); try { // Get GIF writer that's compatible with reader ImageWriter writer = ImageIO.getImageWriter(reader); // Give it the stream to encode to writer.setOutput(ios); writer.prepareWriteSequence(null); for (int i = 0; i < numImages; i++) { // Get input image BufferedImage frameIn = reader.read(i); // Get input metadata IIOMetadataNode root = (IIOMetadataNode)reader.getImageMetadata(i).getAsTree(metaFormatName); // Find GraphicControlExtension node int nNodes = root.getLength(); for (int j = 0; j < nNodes; j++) { org.w3c.dom.Node node = root.item(j); if (node.getNodeName().equalsIgnoreCase("GraphicControlExtension")) { // Get delay value String delay = ((IIOMetadataNode)node).getAttribute("delayTime"); // Check if delay is bugged if (Integer.parseInt(delay) == 0) { // Overwrite with a valid delay value ((IIOMetadataNode)node).setAttribute("delayTime", "10"); } break; } } // Create output metadata IIOMetadata metadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(frameIn), null); // Copy metadata to output metadata metadata.setFromTree(metadata.getNativeMetadataFormatName(), root); // Create output image IIOImage frameOut = new IIOImage(frameIn, null, metadata); // Encode output image writer.writeToSequence(frameOut, writer.getDefaultWriteParam()); } writer.endWriteSequence(); } finally { _close(ios); }} // Create image using encoded data byte[] data = baoStream.toByteArray(); setVar(loadBufferedImageFixingGIFs_output.get(), data); if (loadBufferedImageFixingGIFs_debug) print("Data size: " + l(data)); image = Toolkit.getDefaultToolkit().createImage(data); } return image; } catch (Exception __e) { throw rethrow(__e); } } static A proxy(Class intrface, final Object target) { if (target == null) return null; if (isInstance(intrface, target)) return (A) target; return (A) java.lang.reflect.Proxy.newProxyInstance(intrface.getClassLoader(), new Class[] { intrface }, new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) { return call(target, method.getName(), unnull(args)); } }); } static A proxy(Object target, Class intrface) { return proxy(intrface, target); } static void registerEscape_rootPane(JComponent rootPane, final Runnable r) { String name = "Escape"; Action action = abstractAction(name, r); KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); rootPane.getActionMap().put(name, action); rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, name); } static A moveCaretToEnd(A ta) { setCaretPosition(ta, textAreaTextLength(ta)); return ta; } static A renameSubmitButton(A form, String newName) { renameButton(form, showFormSubmitButtonName(), newName); return form; } static A renameSubmitButton(String newName, A form) { return renameSubmitButton(form, newName); } static JComponent showTitledForm(String title, Object... _parts) { return showFormTitled(title, _parts); } static BufferedImage copyImage(BufferedImage bi) { if (bi == null) return null; ColorModel cm = bi.getColorModel(); boolean isAlphaPremultiplied = cm.isAlphaPremultiplied(); WritableRaster raster = bi.copyData(bi.getRaster().createCompatibleWritableRaster()); return new BufferedImage(cm, raster, isAlphaPremultiplied, null); } static Rect intersectRects(Rect a, Rect b) { int x = max(a.x, b.x), y = max(a.y, b.y); int x2 = min(a.x+a.w, b.x+b.w), y2 = min(a.y+a.h, b.y+b.h); return new Rect(x, y, x2-x, y2-y); } static boolean rectEmpty(Rect r) { return r == null || r.w <= 0 || r.h <= 0; } static BufferedImage decodeImage(byte[] data) { try { return ImageIO.read(new ByteArrayInputStream(data)); } catch (Exception __e) { throw rethrow(__e); } } 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 Map componentID_map = weakHashMap(); static String componentID(Component c) { return c == null ? null : componentID_map.get(c); } static Graphics2D antiAliasOn(Graphics2D g) { g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); return g; } static String getProgramTitle() { return getProgramName(); } static String getProgramName_cache; static String getProgramName() { Lock __245 = downloadLock(); lock(__245); try { if (getProgramName_cache == null) getProgramName_cache = getSnippetTitleOpt(programID()); return getProgramName_cache; } finally { unlock(__245); } } static void _onLoad_getProgramName() { startThread(new Runnable() { public void run() { try { getProgramName(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "getProgramName();"; }}); } static Frame getAWTFrame(final Object _o) { return swing(new F0() { Frame get() { try { Object o = _o; /* ifdef HaveProcessing if (o instanceof PApplet) o = ((PApplet) o).getSurface(); endifdef */ if (o instanceof ButtonGroup) o = first(buttonsInGroup((ButtonGroup) o)); if (!(o instanceof Component)) return null; Component c = (Component) o; while (c != null) { if (c instanceof Frame) return (Frame) c; c = c.getParent(); } return null; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "O o = _o;\r\n /*\r\n ifdef HaveProcessing\r\n if (o instanceof PApplet) ..."; }}); } static void standardTitlePopupMenu(final JFrame frame) { // standard right-click behavior on titles if (isSubstanceLAF()) titlePopupMenu(frame, new VF1() { public void get(JPopupMenu menu) { try { boolean alwaysOnTop = frame.isAlwaysOnTop(); menu.add(jmenuItem("Restart Program", "restart")); menu.add(jmenuItem("Duplicate Program", "duplicateThisProgram")); menu.add(jmenuItem("Show Console", "showConsole")); menu.add(jCheckBoxMenuItem("Always On Top", alwaysOnTop, new Runnable() { public void run() { try { toggleAlwaysOnTop(frame) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "toggleAlwaysOnTop(frame)"; }})); menu.add(jMenuItem("Shoot Window", new Runnable() { public void run() { try { shootWindowGUI_external(frame, 500) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "shootWindowGUI_external(frame, 500)"; }})); //addMenuItem(menu, "Bigger fonts", f swingBiggerFonts); //addMenuItem(menu, "Smaller fonts", f swingSmallerFonts); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "bool alwaysOnTop = frame.isAlwaysOnTop();\r\n menu.add(jmenuItem(\"Restar..."; }}); } static Rectangle screenRectangle() { return new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); } static JFrame consoleFrame() { return (JFrame) getOpt(get(getJavaX(), "console"), "frame"); } static void autoVMExit() { call(getJavaX(), "autoVMExit"); } static boolean isImageFile(File f) { return isImageFileName(fileName(f)); } static String tok_unCurlyBracket(String s) { return isCurlyBraced(s) ? join(dropFirstThreeAndLastThree(javaTok(s))) : s; } static boolean isLetterOrDigit(char c) { return Character.isLetterOrDigit(c); } static A componentPopupMenuItem(A c, final String name, final Object action) { componentPopupMenu(c, new VF1() { public void get(JPopupMenu menu) { try { addMenuItem(menu, name, action); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "addMenuItem(menu, name, action);"; }}); return c; } static void componentPopupMenuItem(JComponent c, final JMenuItem menuItem) { componentPopupMenu(c, new VF1() { public void get(JPopupMenu menu) { try { addMenuItem(menu, menuItem); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "addMenuItem(menu, menuItem);"; }}); } static ThreadLocal addInternalFrame_dontSelect = new ThreadLocal(); static ThreadLocal addInternalFrame_layer = new ThreadLocal(); static ThreadLocal addInternalFrame_toBack = new ThreadLocal(); static JInternalFrame addInternalFrame(final JDesktopPane desktop, final String title, final int x, final int y, final int w, final int h) { return addInternalFrame(desktop, title, x, y, w, h, null); } static JInternalFrame addInternalFrame(final JDesktopPane desktop, final String title, final int x, final int y, final int w, final int h, final Component contents) { return addInternalFrame(desktop, title, rect(x, y, w, h), contents); } static JInternalFrame addInternalFrame(final JDesktopPane desktop, final String title, final Component contents) { return addInternalFrame(desktop, title, null, contents); } static JInternalFrame addInternalFrame(final JDesktopPane desktop, final String title, final Rect r, final Component contents) { final boolean dontSelect = isTrue(optParam(addInternalFrame_dontSelect)); final boolean toBack = isTrue(optParam(addInternalFrame_toBack)); final Integer layer = optParam(addInternalFrame_layer); return swing(new F0() { JInternalFrame get() { try { JInternalFrame frame; if (contents instanceof JInternalFrame) frame = (JInternalFrame) contents; else { frame = jInternalFrame(title); setInternalFrameContents(frame, contents); } frame.setVisible(true); desktop.add(frame, layer); if (r != null) setBounds(frame, r); else internalFrameDefaultPosition(frame); if (dontSelect) if (toBack) frame.toBack(); else frame.toFront(); else frame.setSelected(true); return fixInternalFrame(frame); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JInternalFrame frame;\r\n if (contents instanceof JInternalFrame)\r\n fra..."; }}); } static JInternalFrame addInternalFrame(JDesktopPane desktop, String title) { return addInternalFrame(desktop, title, jpanel()); } static JInternalFrame minInternalFrameWidth(final JInternalFrame frame, final int w) { { swing(new Runnable() { public void run() { try { if (frame != null && frame.getWidth() < w) frame.setSize(w, frame.getHeight()); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (frame != null && frame.getWidth() < w)\r\n frame.setSize(w, frame.getH..."; }}); } return frame; } static JInternalFrame minInternalFrameWidth(int w, JInternalFrame frame) { return minInternalFrameWidth(frame, w); } static A packInternalFrameVertically(A c) { return packInternalFrameVertically(-1, c); } static A packInternalFrameVertically(int width, A c) { final JInternalFrame win = getInternalFrame(c); if (win == null) return c; final int w = width < 0 ? win.getWidth() : width; { swing(new Runnable() { public void run() { try { win.pack(); win.setSize(w, win.getHeight()); fixInternalFrame(win); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "win.pack();\r\n win.setSize(w, win.getHeight());\r\n fixInternalFrame(win);"; }}); } return c; } static JInternalFrame centerInternalFrame(final JInternalFrame f) { { swing(new Runnable() { public void run() { try { Container c = f.getParent(); if (c != null) { //print("Container type: " + className(c) + ", bounds: " + c.getBounds()); f.setLocation((c.getWidth()-f.getWidth())/2, (c.getHeight()-f.getHeight())/2); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Container c = f.getParent();\r\n if (c != null) {\r\n //print(\"Container ..."; }}); } return f; } static JInternalFrame centerInternalFrame(final int w, final int h, final JInternalFrame f) { { swing(new Runnable() { public void run() { try { f.setSize(w, h); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "f.setSize(w, h);"; }}); } return centerInternalFrame(f); } static int packFrame_minw = 150, packFrame_minh = 50; static A packFrame(final A c) { { swing(new Runnable() { public void run() { try { Window w = getWindow(c); if (w != null) { w.pack(); int maxW = getScreenWidth()-50, maxH = getScreenHeight()-50; w.setSize( min(maxW, max(w.getWidth(), packFrame_minw)), min(maxH, max(w.getHeight(), packFrame_minh))); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Window w = getWindow(c);\r\n if (w != null) {\r\n w.pack();\r\n int ma..."; }}); } return c; } static JFrame packFrame(ButtonGroup g) { return packFrame(getFrame(g)); } 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 f) { if (f != null) { swing(new Runnable() { public void run() { try { if (frameTooSmall(f)) frameStandardSize(f); if (!f.isVisible()) f.setVisible(true); // XXX if (f.getState() == Frame.ICONIFIED) f.setState(Frame.NORMAL); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (frameTooSmall(f)) frameStandardSize(f);\r\n if (!f.isVisible()) f.setVis..."; }}); } return f; } // 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 A jPreferWidth(int w, A c) { Dimension size = c.getPreferredSize(); c.setPreferredSize(new Dimension(/*max(w, size.width) ??? */w, size.height)); return c; } static JPanel jpanel(LayoutManager layout) { return swingNu(JPanel.class, layout); } static JPanel jpanel() { return swingNu(JPanel.class); } static boolean containsSpace(String s) { return containsSpaces(s); } static String firstToUpper(String s) { if (empty(s)) return s; return Character.toUpperCase(s.charAt(0)) + s.substring(1); } static List replaceElementsUsingMap(Iterable l, final Map map) { return map(l, new F1() { A get(A a) { try { return getOrKeep(map, a); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "getOrKeep(map, a)"; }}); } static List splitCamelCase(String s) { return ai_splitCamelCase(s); } static boolean newButton_autoToolTip = true; // action can be Runnable or a function name static JButton newButton(final String text, final Object action) { return swing(new F0() { JButton get() { try { String text2 = dropPrefix("[disabled] ", text); final JButton btn = new JButton(text2); if (l(text2) < l(text)) btn.setEnabled(false); if (newButton_autoToolTip) { btn.setToolTipText(btn.getText()); //onChangeAndNow(btn, r { btn.setToolTipText(btn.getText()) }); } // submitButtonOnEnter(btn); // test this first if (action != null) btn.addActionListener(actionListener(action, btn)); return btn; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "S text2 = dropPrefix(\"[disabled] \", text);\r\n final JButton btn = new JButt..."; }}); } static List allChildren(Component c) { return childrenOfType(c, Component.class); } static void onEnterIfTextField(Component c, Object action) { if (action == null) return; if (c instanceof JTextField) onEnter((JTextField) c, action); else if (c instanceof JComboBox) onEnter((JComboBox) c, action); } static Object[] flattenArray2(Object... a) { List l = new ArrayList(); if (a != null) for (Object x : a) if (x instanceof Object[]) l.addAll(asList((Object[]) x)); else if (x instanceof Collection) l.addAll((Collection) x); else l.add(x); return asObjectArray(l); } 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 List withoutNulls(A[] l) { List l2 = new ArrayList(); if (l != null) for (A a : l) if (a != null) l2.add(a); return l2; } static File localSnippetsDir() { return javaxDataDir("Personal Programs"); } static File localSnippetsDir(String sub) { return newFile(localSnippetsDir(), sub); } static String getOneLineFileInfoField(File f, String field) { File infoFile = associatedInfosFile(f); List lines = lines(loadTextFile(infoFile)); return firstStartingWithIC_drop(lines, field + ": "); } static File fileInSameDir(File f, String newName) { return newFile(parentFile(f), newName); } static int smartLastIndexOf(String s, char c) { if (s == null) return 0; int i = s.lastIndexOf(c); return i >= 0 ? i : l(s); } static String standardCredentials() { String user = standardCredentialsUser(); String pass = standardCredentialsPass(); if (nempty(user) && nempty(pass)) return "&_user=" + urlencode(user) + "&_pass=" + urlencode(pass); return ""; } static boolean saveTextFileIfDifferent(File f, String contents) { if (eq(loadTextFile(f), contents)) return false; { saveTextFile(f, contents); return true; } } static String lower(String s) { return s == null ? null : s.toLowerCase(); } static char lower(char c) { return Character.toLowerCase(c); } static File loadImageAsFile(String snippetIDOrURL) { try { if (isURL(snippetIDOrURL)) throw fail("not implemented"); if (!isSnippetID(snippetIDOrURL)) throw fail("Not a URL or snippet ID: " + snippetIDOrURL); String snippetID = "" + parseSnippetID(snippetIDOrURL); File file = imageSnippetCacheFile(snippetID); if (fileSize(file) > 0) return file; String imageURL = snippetImageURL_noHttps(snippetID); System.err.println("Loading image: " + imageURL); byte[] data = loadBinaryPage(imageURL); saveBinaryFile(file, data); return file; } catch (Exception __e) { throw rethrow(__e); } } // If you change this, also change DiskSnippetCache_fileToLibID 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(dataSnippetLink(snippetID)); print("Loading library: " + hideCredentials(url)); try { data = loadBinaryPage(url.openConnection()); } catch (RuntimeException e) { data = null; } if (data == null || data.length == 0) { url = new URL("http://data.tinybrain.de/blobs/" + parseSnippetID(snippetID)); print("Loading library: " + hideCredentials(url)); data = loadBinaryPage(url.openConnection()); } print("Bytes loaded: " + data.length); } catch (FileNotFoundException e) { throw new IOException("Binary snippet #" + snippetID + " not found or not public"); } return data; } static File loadDataSnippetToFile(String snippetID) { try { snippetID = fsI(snippetID); IResourceLoader rl = vm_getResourceLoader(); if (rl != null) return rl.loadLibrary(snippetID); File f = DiskSnippetCache_file(parseSnippetID(snippetID)); List urlsTried = new ArrayList(); List errors = new ArrayList(); try { URL url = addAndReturn(urlsTried, new URL(dataSnippetLink(snippetID))); print("Loading library: " + hideCredentials(url)); try { loadBinaryPageToFile(openConnection(url), f); if (fileSize(f) == 0) throw fail(); } catch (Throwable e) { errors.add(e); url = addAndReturn(urlsTried, new URL("http://data.tinybrain.de/blobs/" + psI(snippetID))); print("Trying other server: " + hideCredentials(url)); loadBinaryPageToFile(openConnection(url), f); print("Got bytes: " + fileSize(f)); } // 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); errors.add(e); throw fail("Binary snippet " + snippetID + " not found or not public. URLs tried: " + allToString(urlsTried) + ", errors: " + allToString(errors)); } return f; } catch (Exception __e) { throw rethrow(__e); } } static byte[] isGIF_magic = bytesFromHex("47494638"); // Actual signature is longer, but we're lazy static boolean isGIF(byte[] data) { return byteArrayStartsWith(data, isGIF_magic); } static boolean isGIF(File f) { return isGIF(loadBeginningOfBinaryFile(f, l(isGIF_magic))); } static void setVar(IVar v, A value) { if (v != null) v.set(value); } static boolean isInstance(Class type, Object arg) { return type.isInstance(arg); } static void setCaretPosition(final JTextComponent c, final int pos) { if (c != null) { swing(new Runnable() { public void run() { try { try { int _pos = max(0, min(l(c.getText()), pos)); c.setCaretPosition(_pos); } catch (Throwable __e) { _handleException(__e); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pcall {\r\n int _pos = max(0, min(l(c.getText()), pos));\r\n c.setCaret..."; }}); } } static int textAreaTextLength(JTextComponent ta) { return l(getText(ta)); } static JButton renameButton(JComponent c, String name) { JButton b = first(childrenOfType(c, JButton.class)); if (b != null) b.setText(name); return b; } static JButton renameButton(JComponent c, String oldName, String newName) { JButton b = findButton(c, oldName); if (b != null) b.setText(newName); return b; } static void duplicateThisProgram() { nohupJavax(trim(programID() + " " + smartJoin((String[]) get(getJavaX(), "fullArgs")))); } static void showConsole() { callOpt(get(javax(), "console"), "showConsole"); } static String getSnippetTitleOpt(String s) { return isSnippetID(s) ? getSnippetTitle(s) : s; } static boolean isSubstanceLAF() { return substanceLookAndFeelEnabled(); } // menuMaker = voidfunc(JPopupMenu) // return true if menu could be added static boolean titlePopupMenu(final Component c, final Object menuMaker) { JComponent titleBar = getTitlePaneComponent(getPossiblyInternalFrame(c)); if (titleBar == null) { print("Can't add title right click!"); return false; } else { componentPopupMenu(titleBar, menuMaker); return true; } } // r : runnable or voidfunc(bool) static JCheckBoxMenuItem jCheckBoxMenuItem(String text, boolean checked, final Object r) { final JCheckBoxMenuItem mi = new JCheckBoxMenuItem(text, checked); addActionListener(mi, new Runnable() { public void run() { try { callF(r, isChecked(mi)) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callF(r, isChecked(mi))"; }}); return mi; } static void toggleAlwaysOnTop(JFrame frame) { frame.setAlwaysOnTop(!frame.isAlwaysOnTop()); } 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 boolean isImageFileName(String s) { return eqicOneOf(fileExtension(s), ".png", ".jpg", ".jpeg", ".gif"); } static String fileName(File f) { return f == null ? null : f.getName(); } static List dropFirstThreeAndLastThree(List l) { return dropFirstAndLast(3, l); } static Rect rect(int x, int y, int w, int h) { return new Rect(x, y, w, h); } static boolean jInternalFrame_iconifiable = true; static JInternalFrame jInternalFrame() { return jInternalFrame(""); } static JInternalFrame jInternalFrame(final String title) { return swing(new F0() { JInternalFrame get() { try { JInternalFrame f = new JInternalFrame(title, true, true, true, jInternalFrame_iconifiable); f.setVisible(true); return f; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JInternalFrame f = new JInternalFrame(title, true, true, true, jInternalFrame..."; }}); } static void setInternalFrameContents(final Component c, final Object contents) { { swing(new Runnable() { public void run() { try { JInternalFrame frame = getInternalFrame(c); if (frame == null) return; frame.getContentPane().removeAll(); frame.getContentPane().setLayout(new BorderLayout()); if (contents != null) frame.getContentPane().add(wrap(contents)); revalidate(frame); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JInternalFrame frame = getInternalFrame(c);\r\n if (frame == null) return;\r\n..."; }}); } } static A setBounds(final int x, final int y, final int w, final int h, final A a) { if (a != null) { swing(new Runnable() { public void run() { try { a.setBounds(x, y, w, h); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "a.setBounds(x, y, w, h);"; }}); } return a; } static A setBounds(final A a, final Rect r) { if (a != null) { swing(new Runnable() { public void run() { try { a.setBounds(toRectangle(r)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "a.setBounds(toRectangle(r));"; }}); } return a; } static A setBounds(A a, int x, int y, int w, int h) { return setBounds(x, y, w, h, a); } static void internalFrameDefaultPosition(JInternalFrame f) { f.setSize(500, 300); centerInternalFrame(f); } static int fixInternalFrame_borderTopLeft = 0; static int fixInternalFrame_borderBottomRight = 40; // for title bar static JInternalFrame fixInternalFrame(final JInternalFrame f) { return swing(new F0() { JInternalFrame get() { try { Container c = f.getParent(); if (c == null) return f; Rect r = toRect(f.getBounds()); int a = fixInternalFrame_borderTopLeft, b = fixInternalFrame_borderBottomRight; Rect outer = new Rect(a, a, c.getWidth()-b, c.getHeight()-b); if (!rectContains(outer, r)) f.setLocation( max(a, min(r.x, outer.x2())), max(a, min(r.y, outer.y2()))); if (r.w > c.getWidth() || r.h > c.getHeight()) f.setSize(c.getWidth()-a, c.getHeight()-a); return f; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Container c = f.getParent();\r\n if (c == null) ret f;\r\n Rect r = toRect(..."; }}); } static int getScreenWidth() { return getScreenSize().width; } static int getScreenHeight() { return getScreenSize().height; } static boolean frameTooSmall(JFrame frame) { return frame.getWidth() < 100 || frame.getHeight() < 50; } static void frameStandardSize(JFrame frame) { frame.setBounds(300, 100, 500, 400); } 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 boolean containsSpaces(String s) { return indexOf(s, ' ') >= 0; } static A getOrKeep(Map map, A a) { if (map == null) return a; A v = map.get(a); return v != null ? v : a; } static List ai_splitCamelCase(String s) { int j = 0; List l = new ArrayList(); // new addition if (isAllUpperCase(s)) { l.add(s); return l; } for (int i = 0; i < l(s); i++) if (isUpperCaseLetter(s.charAt(i)) && i > j) { l.add(substring(s, j, i)); j = i; } if (j < l(s)) l.add(substring(s, j)); return l; } static List childrenOfType(Component c, Class theClass) { List l = new ArrayList(); scanForComponents(c, theClass, l); return l; } static List childrenOfType(Class theClass, Component c) { return childrenOfType(c, theClass); } static Object[] asObjectArray(List l) { return toObjectArray(l); } static boolean containsNulls(Collection c) { return contains(c, null); } static File associatedInfosFile(File f) { return replaceExtension(f, ".infos"); } static String firstStartingWithIC_drop(Collection l, final String prefix) { for (String s : unnull(l)) if (swic(s, prefix)) return substring(s, l(prefix)); return null; } static String firstStartingWithIC_drop(String prefix, Collection l) { return firstStartingWithIC_drop(l, prefix); } static File parentFile(File f) { return dirOfFile(f); } static String standardCredentialsUser() { return trim(loadTextFile( oneOfTheFiles( javaxSecretDir("tinybrain-username"), userDir(".tinybrain/username")))); } static String standardCredentialsPass() { return trim(loadTextFile( oneOfTheFiles( javaxSecretDir("tinybrain-userpass"), userDir(".tinybrain/userpass")))); } static File imageSnippetCacheFile(String snippetID) { File dir = imageSnippetsCacheDir(); if (!loadBufferedImage_useImageCache) return null; return new File(dir, parseSnippetID(snippetID) + ".png"); } static String snippetImageURL_noHttps(String snippetID) { return snippetImageURL_noHttps(snippetID, "png"); } static String snippetImageURL_noHttps(String snippetID, String contentType) { return snippetImageURL(snippetID, contentType) .replace("https://www.botcompany.de:8443/", "http://www.botcompany.de:8080/") .replace("https://botcompany.de/", "http://botcompany.de/"); } static ThreadLocal>> loadBinaryPage_responseHeaders = new ThreadLocal(); static ThreadLocal> loadBinaryPage_extraHeaders = new ThreadLocal(); 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 { Map extraHeaders = getAndClearThreadLocal(loadBinaryPage_extraHeaders); setHeaders(con); for (String key : keys(extraHeaders)) con.setRequestProperty(key, extraHeaders.get(key)); return loadBinaryPage_noHeaders(con); } catch (Exception __e) { throw rethrow(__e); } } static byte[] loadBinaryPage_noHeaders(URLConnection con) { try { ByteArrayOutputStream buf = new ByteArrayOutputStream(); InputStream inputStream = con.getInputStream(); loadBinaryPage_responseHeaders.set(con.getHeaderFields()); long len = 0; try { len = con.getContentLength/*Long*/(); } 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); } } /** writes safely (to temp file, then rename) */ public static byte[] saveBinaryFile(String fileName, byte[] contents) { try { 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); vmBus_send("wroteFile", file); return contents; } catch (Exception __e) { throw rethrow(__e); } } static byte[] saveBinaryFile(File fileName, byte[] contents) { return saveBinaryFile(fileName.getPath(), contents); } static File getGlobalCache() { File file = new File(javaxCachesDir(), "Binary Snippets"); file.mkdirs(); return file; } static String dataSnippetLink(String snippetID) { long id = parseSnippetID(snippetID); if (id >= 1100000 && id < 1200000) return imageServerURL() + id; if (id >= 1400000 && id < 1500000) return "http://butter.botcompany.de:8080/files/" + id + "?_pass=" + muricaPassword(); if (id >= 1200000 && id < 1300000) { // Woody files, actually String pw = muricaPassword(); if (empty(pw)) throw fail("Please set 'murica password by running #1008829"); return "http://butter.botcompany.de:8080/1008823/raw/" + id + "?_pass=" + pw; // 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 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)); try { InputStream inputStream = con.getInputStream(); long len = 0; try { len = con.getContentLength/*Long*/(); } catch (Throwable e) { printStackTrace(e); } String pat = " {*}" + (len != 0 ? "/" + len : "") + " bytes loaded."; copyStreamWithPrints(inputStream, buf, pat); inputStream.close(); buf.close(); file.delete(); renameFile_assertTrue(ftemp, file); } finally { if (buf != null) buf.close(); } } catch (Exception __e) { throw rethrow(__e); } } static JButton findButton(Component c, String name) { for (JButton b : childrenOfType(c, JButton.class)) if (eq(b.getText(), name)) return b; for (JButton b : childrenOfType(getFrame(c), JButton.class)) if (eq(b.getText(), name)) return b; return null; } static JButton findButton(Component c) { return childOfType(c, JButton.class); } static void nohupJavax(final String javaxargs) { startThread(new Runnable() { public void run() { try { call(hotwireOnce("#1008562"), "nohupJavax", javaxargs); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "call(hotwireOnce(\"#1008562\"), \"nohupJavax\", javaxargs);"; }}); } static void nohupJavax(final String javaxargs, final String vmArgs) { startThread(new Runnable() { public void run() { try { call(hotwireOnce("#1008562"), "nohupJavax", javaxargs, vmArgs); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "call(hotwireOnce(\"#1008562\"), \"nohupJavax\", javaxargs, vmArgs);"; }}); } // Try to get the quoting right... static String smartJoin(String[] args) { if (empty(args)) return ""; 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 boolean substanceLookAndFeelEnabled() { return startsWith(getLookAndFeel(), "org.pushingpixels."); } static JComponent getTitlePaneComponent(RootPaneContainer window) { if (window instanceof JInternalFrame) return getInternalFrameTitlePaneComponent((JInternalFrame) window); if (!substanceLookAndFeelEnabled() || window == null) return null; JRootPane rootPane = window.getRootPane(); if (rootPane != null) { Object /*SubstanceRootPaneUI*/ ui = rootPane.getUI(); return (JComponent) call(ui, "getTitlePane"); } return null; } static RootPaneContainer getPossiblyInternalFrame(Component c) { JInternalFrame f = getInternalFrame(c); if (f != null) return f; return optCast(RootPaneContainer.class, getWindow(c)); } static void addActionListener(JTextField tf, final Runnable action) { onEnter(tf, action); } static void addActionListener(final JComboBox cb, final Runnable action) { if (cb != null) { swing(new Runnable() { public void run() { try { cb.addActionListener(actionListener(action)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "cb.addActionListener(actionListener(action));"; }}); } } static void addActionListener(final AbstractButton b, final Runnable action) { if (b != null) { swing(new Runnable() { public void run() { try { b.addActionListener(actionListener(action)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "b.addActionListener(actionListener(action));"; }}); } } static boolean isChecked(final JCheckBox checkBox) { return checkBox != null && (boolean) swing(new F0() { Boolean get() { try { return checkBox.isSelected(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret checkBox.isSelected();"; }}); } static boolean isChecked(final JCheckBoxMenuItem mi) { return mi != null && (boolean) swing(new F0() { Boolean get() { try { return mi.isSelected(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret mi.isSelected();"; }}); } static Class hotwireOnce(String programID) { return hotwireCached(programID, false); } static boolean eqicOneOf(String s, String... l) { for (String x : l) if (eqic(s, x)) return true; return false; } static String fileExtension(File f) { if (f == null) return null; return fileExtension(f.getName()); } static String fileExtension(String s) { return substring(s, smartLastIndexOf(s, '.')); } static Rectangle toRectangle(Rect r) { return r == null ? null : r.getRectangle(); } static Rect toRect(Rectangle r) { return r == null ? null : new Rect(r); } static Rect toRect(RectangularShape r) { return r == null ? null : toRect(r.getBounds()); } static boolean rectContains(int x1, int y1, int w, int h, Pt p) { return p.x >= x1 && p.y >= y1 && p.x < x1+w && p.y < y1+h; } static boolean rectContains(Rect a, Rect b) { return b.x >= a.x && b.y >= a.y && b.x2() <= a.x2() && b.y2() <= a.y2(); } static boolean rectContains(Rect a, int x, int y) { return a != null && a.contains(x, y); } static boolean isAllUpperCase(String s) { return hasLettersAllUpperCase(s); } static boolean isUpperCaseLetter(char c) { return Character.isUpperCase(c); } static void scanForComponents(final Component c, final Class theClass, final List l) { if (theClass.isInstance(c)) l.add((A) c); if (c instanceof Container) { swing(new Runnable() { public void run() { try { for (Component comp : ((Container) c).getComponents()) scanForComponents(comp, theClass, l); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "for (Component comp : ((Container) c).getComponents())\r\n scanForComponen..."; }}); } } static File replaceExtension(File f, String extOld, String extNew) { return newFile(replaceExtension(f2s(f), extOld, extNew)); } static File replaceExtension(File f, String extNew) { return replaceExtension(f, fileExtension(f), extNew); } static String replaceExtension(String s, String extOld, String extNew) { s = dropSuffixIC(addPrefixOptIfNempty(".", extOld), s); return s + addPrefixOptIfNempty(".", extNew); } static String replaceExtension(String name, String extNew) { return replaceExtension(name, fileExtension(name), extNew); } static File dirOfFile(File f) { return f == null ? null : f.getParentFile(); } static File oneOfTheFiles(String... paths) { if (paths != null) for (String path : paths) if (fileExists(path)) return newFile(path); return null; } static File oneOfTheFiles(File... files) { if (files != null) for (File f : files) if (fileExists(f)) return f; return null; } static A println(A a) { return print(a); } static void copyStreamWithPrints(InputStream in, OutputStream out, String pat) { try { byte[] buf = new byte[65536]; int total = 0; while (true) { int n = in.read(buf); if (n <= 0) return; out.write(buf, 0, n); if ((total+n)/100000 > total/100000) print(pat.replace("{*}", str(roundDownTo(total, 100000)))); total += n; } } catch (Exception __e) { throw rethrow(__e); } } static void renameFile_assertTrue(File a, File b) { try { if (!a.exists()) throw fail("Source file not found: " + f2s(a)); if (b.exists()) throw fail("Target file exists: " + f2s(b)); mkdirsForFile(b); if (!a.renameTo(b)) throw fail("Can't rename " + f2s(a) + " to " + f2s(b)); } catch (Exception __e) { throw rethrow(__e); } } static A childOfType(Component c, Class theClass) { return first(childrenOfType(c, theClass)); } static A childOfType(Class theClass, Component c) { return childOfType(c, theClass); } static String getLookAndFeel() { return getClassName(UIManager.getLookAndFeel()); } static JComponent getInternalFrameTitlePaneComponent(JInternalFrame f) { return (JComponent) childWithClassNameEndingWith(f, "InternalFrameTitlePane"); } static A optCast(Class c, Object o) { return isInstance(c, o) ? (A) o : null; } 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) { return hotwireCached(programID, runMain, false); } static Class hotwireCached(String programID, boolean runMain, boolean dependent) { Lock __841 = hotwireCached_lock; lock(__841); try { programID = formatSnippetID(programID); Class c = hotwireCached_cache.get(programID); if (c == null) { c = hotwire(programID); if (dependent) makeDependent(c); if (runMain) callMain(c); hotwireCached_cache.put(programID, c); } return c; } finally { unlock(__841); } } static boolean hasLettersAllUpperCase(String s) { return hasLetters(s) && !containsLowerCase(s); } static String dropSuffixIC(String suffix, String s) { return s == null ? null : ewic(s, suffix) ? s.substring(0, l(s)-l(suffix)) : s; } static String addPrefixOptIfNempty(String prefix, String s) { return addPrefixIfNotEmpty2(prefix, s); } static int roundDownTo(int x, int n) { return x/n*n; } static long roundDownTo(long x, long n) { return x/n*n; } static Component childWithClassNameEndingWith(Component c, String suffix) { if (endsWith(className(c), suffix)) return c; Component x; for (Component comp : getComponents(c)) if ((x = childWithClassNameEndingWith(comp, suffix)) != null) return x; return null; } static boolean hasLetters(String s) { for (int i = 0; i < s.length(); i++) if (Character.isLetter(s.charAt(i))) return true; return false; } static boolean containsLowerCase(String s) { for (int i = 0; i < l(s); i++) if (isLowerCase(s.charAt(i))) return true; return false; } static String addPrefixIfNotEmpty2(String prefix, String s) { return empty(s) ? "" : addPrefix(prefix, s); } static List getComponents(final Component c) { return !(c instanceof Container) ? emptyList() : asList(swing(new F0() { Component[] get() { try { return ((Container) c).getComponents(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret ((Container) c).getComponents();"; }})); } static boolean isLowerCase(char c) { return Character.isLowerCase(c); } static abstract class LazyList extends RandomAccessAbstractList { public Iterator iterator() { return concurrentlyIterateList(this); } }static class AutoComboBox extends JComboBox { String keyWord[] = {"item1", "item2", "item3"}; Vector myVector = new Vector(); boolean acceptOnTab; // don't work so good 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)); if (acceptOnTab) text.setFocusTraversalKeysEnabled(false); 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); } class ComboListener extends KeyAdapter { JComboBox cb; Vector vector; ComboListener(JComboBox cb, Vector vector) { this.vector = vector; this.cb = cb;} public void /*keyReleased*/keyPressed(KeyEvent key) { if (key.getKeyCode() == KeyEvent.VK_ENTER) return; if (key.getKeyCode() == KeyEvent.VK_ESCAPE) { cb.hidePopup(); return; } if (acceptOnTab && key.getKeyCode() == KeyEvent.VK_TAB /*&& key.getModifiers() == 0*/) { _print("Have tab event (modifiers=" + key.getModifiers() + ")"); if ((key.getModifiers() & ActionEvent.SHIFT_MASK) == 0 && cb.isPopupVisible()) { cb.setSelectedIndex(0); // accept item cb.hidePopup(); } else // standard tab behavior swing_standardTabBehavior(key); return; } JTextField tf = (JTextField) (cb.getEditor().getEditorComponent()); if (tf.getCaretPosition() != l(tf.getText())) return; String text = ((JTextField) key.getSource()).getText(); Vector list = getFilteredList(text); if (nempty(list)) { cb.setModel(new DefaultComboBoxModel(list)); cb.setSelectedIndex(-1); tf.setText(text); // necessary? cb.showPopup(); } else cb.hidePopup(); } public Vector getFilteredList(String text) { return new Vector(scoredSearch(text, vector)); } } }static abstract class LiveValue { abstract Class getType(); abstract A get(); abstract void onChange(Runnable l); abstract void removeOnChangeListener(Runnable l); void onChangeAndNow(Runnable l) { onChange(l); callF(l); } }static class RemoteDB implements AutoCloseable { 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); } public void close() { _close(db); } 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 abstract class TripleListener { void onNewTriple(TripleWeb w) {} void onForgottenTriple(TripleWeb w) {} }/* * #! * Ontopia Engine * #- * Copyright (C) 2001 - 2013 The Ontopia Project * #- * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * !# */ // modified by Stefan Reich // Implements the Set interface more compactly than // java.util.HashSet by using a closed hashtable // & allows custom hasher. static class CustomCompactHashSet extends AbstractSet { protected final static int INITIAL_SIZE = 3; protected final static double LOAD_FACTOR = 0.75; protected final static Object nullObject = new Object(); protected final static Object deletedObject = new Object(); protected int elements; protected int freecells; protected A[] objects; protected int modCount; Hasher hasher; // optional CustomCompactHashSet() { this(INITIAL_SIZE); } CustomCompactHashSet(int size) { // NOTE: If array size is 0, we get a // "java.lang.ArithmeticException: / by zero" in add(Object). objects = (A[]) new Object[(size==0 ? 1 : size)]; elements = 0; freecells = objects.length; modCount = 0; } CustomCompactHashSet(Collection c) { this(c.size()); addAll(c); } CustomCompactHashSet(Hasher hasher) { this(); this.hasher = hasher; } @Override public Iterator iterator() { return new CompactHashIterator(); } @Override public int size() { return elements; } @Override public boolean isEmpty() { return elements == 0; } @Override public boolean contains(Object o) { return find(o) != null; } synchronized A find(Object o) { if (o == null) o = nullObject; int hash = elementHashCode(o); int index = (hash & 0x7FFFFFFF) % objects.length; int offset = 1; // search for the object (continue while !null and !this object) while(objects[index] != null && !(elementHashCode(objects[index]) == hash && elementEquals(objects[index], o))) { index = ((index + offset) & 0x7FFFFFFF) % objects.length; offset = offset*2 + 1; if (offset == -1) offset = 2; } return objects[index]; } boolean removeIfSame(Object o) { A value = find(o); if (value == o) { remove(value); return true; } return false; } @Override synchronized public boolean add(Object o) { if (o == null) o = nullObject; int hash = elementHashCode(o); int index = (hash & 0x7FFFFFFF) % objects.length; int offset = 1; int deletedix = -1; // search for the object (continue while !null and !this object) while(objects[index] != null && !(elementHashCode(objects[index]) == hash && elementEquals(objects[index], o))) { // if there's a deleted object here we can put this object here, // provided it's not in here somewhere else already if (objects[index] == deletedObject) deletedix = index; index = ((index + offset) & 0x7FFFFFFF) % objects.length; offset = offset*2 + 1; if (offset == -1) offset = 2; } if (objects[index] == null) { // wasn't present already if (deletedix != -1) // reusing a deleted cell index = deletedix; else freecells--; modCount++; elements++; // here we face a problem regarding generics: // add(A o) is not possible because of the null Object. We cant do 'new A()' or '(A) new Object()' // so adding an empty object is a problem here // If (! o instanceof A) : This will cause a class cast exception // If (o instanceof A) : This will work fine objects[index] = (A) o; // do we need to rehash? if (1 - (freecells / (double) objects.length) > LOAD_FACTOR) rehash(); return true; } else // was there already return false; } @Override synchronized public boolean remove(Object o) { if (o == null) o = nullObject; int hash = elementHashCode(o); int index = (hash & 0x7FFFFFFF) % objects.length; int offset = 1; // search for the object (continue while !null and !this object) while(objects[index] != null && !(elementHashCode(objects[index]) == hash && elementEquals(objects[index], o))) { index = ((index + offset) & 0x7FFFFFFF) % objects.length; offset = offset*2 + 1; if (offset == -1) offset = 2; } // we found the right position, now do the removal if (objects[index] != null) { // we found the object // same problem here as with add objects[index] = (A) deletedObject; modCount++; elements--; return true; } else // we did not find the object return false; } @Override synchronized public void clear() { elements = 0; for (int ix = 0; ix < objects.length; ix++) objects[ix] = null; freecells = objects.length; modCount++; } @Override synchronized public Object[] toArray() { Object[] result = new Object[elements]; Object[] objects = this.objects; int pos = 0; for (int i = 0; i < objects.length; i++) if (objects[i] != null && objects[i] != deletedObject) { if (objects[i] == nullObject) result[pos++] = null; else result[pos++] = objects[i]; } // unchecked because it should only contain A return result; } // not sure if this needs to have generics @Override synchronized public T[] toArray(T[] a) { int size = elements; if (a.length < size) a = (T[])java.lang.reflect.Array.newInstance( a.getClass().getComponentType(), size); A[] objects = this.objects; int pos = 0; for (int i = 0; i < objects.length; i++) if (objects[i] != null && objects[i] != deletedObject) { if (objects[i] == nullObject) a[pos++] = null; else a[pos++] = (T) objects[i]; } return a; } protected void rehash() { int gargagecells = objects.length - (elements + freecells); if (gargagecells / (double) objects.length > 0.05) // rehash with same size rehash(objects.length); else // rehash with increased capacity rehash(objects.length*2 + 1); } protected void rehash(int newCapacity) { int oldCapacity = objects.length; @SuppressWarnings("unchecked") A[] newObjects = (A[]) new Object[newCapacity]; for (int ix = 0; ix < oldCapacity; ix++) { Object o = objects[ix]; if (o == null || o == deletedObject) continue; int hash = elementHashCode(o); int index = (hash & 0x7FFFFFFF) % newCapacity; int offset = 1; // search for the object while(newObjects[index] != null) { // no need to test for duplicates index = ((index + offset) & 0x7FFFFFFF) % newCapacity; offset = offset*2 + 1; if (offset == -1) offset = 2; } newObjects[index] = (A) o; } objects = newObjects; freecells = objects.length - elements; } private class CompactHashIterator implements Iterator { private int index; private int lastReturned = -1; private int expectedModCount; @SuppressWarnings("empty-statement") public CompactHashIterator() { synchronized(CustomCompactHashSet.this) { for (index = 0; index < objects.length && (objects[index] == null || objects[index] == deletedObject); index++) ; expectedModCount = modCount; } } @Override public boolean hasNext() { synchronized(CustomCompactHashSet.this) { return index < objects.length; } } @SuppressWarnings("empty-statement") @Override public T next() { synchronized(CustomCompactHashSet.this) { /*if (modCount != expectedModCount) throw new ConcurrentModificationException();*/ int length = objects.length; if (index >= length) { lastReturned = -2; throw new NoSuchElementException(); } lastReturned = index; for (index += 1; index < length && (objects[index] == null || objects[index] == deletedObject); index++) ; if (objects[lastReturned] == nullObject) return null; else return (T) objects[lastReturned]; } } @Override public void remove() { synchronized(CustomCompactHashSet.this) { if (modCount != expectedModCount) throw new ConcurrentModificationException(); if (lastReturned == -1 || lastReturned == -2) throw new IllegalStateException(); // delete object if (objects[lastReturned] != null && objects[lastReturned] != deletedObject) { objects[lastReturned] = (A) deletedObject; elements--; modCount++; expectedModCount = modCount; // this is expected; we made the change } } } } int elementHashCode(Object o) { if (hasher == null || o == nullObject || o == deletedObject) return _hashCode(o); return hasher.hashCode((A) o); } boolean elementEquals(Object a, Object b) { if (hasher == null) return eq(a, b); if (a == nullObject || a == deletedObject || b == nullObject || b == deletedObject) return a == b; return hasher.equals((A) a, (A) b); } } 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; } }static interface Hasher { int hashCode(A a); boolean equals(A a, A b); }static class TripleA extends TripleRef { TripleA() {} TripleA(T3 triple) { this.triple = triple;} int position() { return 0; } }static class TripleB extends TripleRef { TripleB() {} TripleB(T3 triple) { this.triple = triple;} int position() { return 1; } }static class TripleC extends TripleRef { TripleC() {} TripleC(T3 triple) { this.triple = triple;} int position() { return 2; } }static interface IResourceLoader { String loadSnippet(String snippetID); String getTranspiled(String snippetID); // with libs int getSnippetType(String snippetID); String getSnippetTitle(String snippetID); File loadLibrary(String snippetID); File pathToJavaXJar(); File getSnippetJar(String snippetID, String transpiledSrc); }static class Scored extends Var { float score; Scored() {} Scored(A a, float score) { super(a); this.score = score; } float score() { return score; } public String toString() { return toIntPercent(score) + "%: " + str(get()); } } static abstract class DialogIO implements AutoCloseable { String line; boolean eos, loud, noClose; Lock lock = lock(); abstract String readLineImpl(); abstract boolean isStillConnected(); abstract void sendLine(String line); abstract boolean isLocalConnection(); abstract Socket getSocket(); 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 { ping(); 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 boolean bareDBMode_on; static void bareDBMode() { bareDBMode(null); // default autoSaveInterval } static void bareDBMode(Integer autoSaveInterval) { bareDBMode_on = true; conceptsAndBot(autoSaveInterval); } static List getAll(Map map, Collection l) { return lookupAllOpt(map, l); } static List getAll(Collection l, Map map) { return lookupAllOpt(map, l); } static boolean hasConcept(Class c, Object... params) { return findConceptWhere(c, params) != null; } static boolean exceptionMessageContains(Throwable e, String s) { return cic(getInnerMessage(e), s); } static void printShortException(Throwable e) { print(exceptionToStringShort(e)); } static void printShortException(String s, Throwable e) { print(s, exceptionToStringShort(e)); } static void clearConcepts() { db_mainConcepts().clearConcepts(); } static void clearConcepts(Concepts concepts) { concepts.clearConcepts(); } static void restoreLatestBackupIfConceptsFileEmpty(String dbID, Object... __) { boolean doIt = boolPar("doIt",__); File file = conceptsFile(dbID); if (fileExists(file) && fileSize(file) == 0) { print(file + " corrupted, trying to restore"); File backup = lastThat("fileNotEmpty",sortByFileName(conceptBackupFiles(dbID))); if (backup == null) { print("No usable backup found :("); return; } String msg = "RESTORING: " + backup; File log = javaxDataDir("db-restores.log"); if (doIt) logQuotedWithTime(log, msg); print(stringIf(!doIt, "[would be] ") + msg); if (doIt) { clearConceptsOf(dbID); copyFile(backup, file); print(msg = "DB RESTORED!"); if (doIt) logQuotedWithTime(log, msg); } } } 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(); static ThreadLocal readLocally2_classFinder = 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 = newGZIPInputStream(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, readLocally2_classFinder.get()); readLocally_set(obj, variableName, o); return; } finally { _close(fis); }} readLocally_stringLength = l(value); if (nempty(value)) readLocally_set(obj, variableName, unstructure(value, allDynamic, readLocally2_classFinder.get())); } } } 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 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 RemoteDB connectToDBOpt(String dbNameOrID) { try { return new RemoteDB(dbNameOrID); } catch (Throwable __e) { return null; } } static Concept getConcept(long id) { return db_mainConcepts().getConcept(id); } static A getConcept(Class cc, long id) { return getConcept(db_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 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 __62 = saveLock(); lock(__62); 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(__62); } } // wrapper: VF1 or null static void callRunnableWithWrapper(Object wrapper, Runnable r) { if (wrapper == null) callF(r); else callF(wrapper, r); } 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); } } static A popFirst(List l) { if (empty(l)) return null; A a = first(l); l.remove(0); return a; } static A popFirst(Collection l) { if (empty(l)) return null; A a = first(l); l.remove(a); return a; } 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 javaTokWordWrap(String s) { return javaTokWordWrap(120, s); } // TODO: complete trimming static String javaTokWordWrap(int cols, String s) { int col = 0; List tok = javaTok(s); for (int i = 0; i < l(tok); i++) { String t = tok.get(i); if (odd(i) && col >= cols && !containsNewLine(t)) tok.set(i, t = rtrimSpaces(t) + "\n"); int idx = t.lastIndexOf('\n'); if (idx >= 0) col = l(t)-(idx+1); else col += l(t); } return join(tok); } static String ymd() { return ymd(now()); } static String ymd(long now) { return year(now) + formatInt(month(now), 2) + formatInt(dayOfMonth(now), 2); } static String formatInt(int i, int digits) { return padLeft(str(i), '0', digits); } static String formatInt(long l, int digits) { return padLeft(str(l), '0', digits); } static int hours() { return hours(java.util.Calendar.getInstance()); } static int hours(java.util.Calendar c) { return c.get(java.util.Calendar.HOUR_OF_DAY); } static long toK(long l) { return (l+1023)/1024; } static Collection allConcepts() { return db_mainConcepts().allConcepts(); } static Collection allConcepts(Concepts concepts) { return concepts.allConcepts(); } static List conceptsOfType(String type) { return db_mainConcepts().conceptsOfType(type); } static List filterByType(Iterable c, Class type) { List l = new ArrayList(); if (c != null) 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 List filterByType(Class type, Iterable c) { return filterByType(c, type); } 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 boolean hasType(Collection c, Class type) { for (Object x : c) if (isInstanceX(type, x)) return true; return false; } 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 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 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; } // returns number of changes static int cset(Concept c, Object... values) { try { if (c == null) return 0; int changes = 0; values = expandParams(c.getClass(), values); warnIfOddCount(values); for (int i = 0; i+1 < l(values); i += 2) { String field = (String) values[i]; Object value = values[i+1]; Field f = setOpt_findField(c.getClass(), field); //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 = intern((String) value); if (f == null) { // TODO: keep ref if it exists mapPut2(c.fieldValues, assertIdentifier(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 Object deref(Object o) { if (o instanceof Derefable) o = ((Derefable) o).get(); return o; } static String loadConceptsStructure(String progID) { return loadTextFilePossiblyGZipped(getProgramFile(progID, "concepts.structure")); } static String loadConceptsStructure() { return loadConceptsStructure(dbProgramID()); } static void cleanKillVM() { try { ping(); assertNotOnAWTThread(); cleanKillVM_noSleep(); Object o = new Object(); synchronized(o) { o.wait(); } } catch (Exception __e) { throw rethrow(__e); } } static void cleanKillVM_noSleep() { call(getJavaX(), "cleanKill"); } static int countConcepts(Concepts concepts, Class c, Object... params) { return concepts.countConcepts(c, params); } static int countConcepts(Class c, Object... params) { return db_mainConcepts().countConcepts(c, params); } static int countConcepts() { return db_mainConcepts().countConcepts(); } static int countConcepts(String className) { return db_mainConcepts().countConcepts(className); } static int countConcepts(Concepts concepts, String className) { return concepts.countConcepts(className); } static int countConcepts(Concepts concepts) { return concepts.countConcepts(); } static boolean dynamicObjectIsLoading() { return isTrue(getThreadLocal(DynamicObject_loading)); } static List addDyn(List l, A a) { if (l == null) l = new ArrayList(); l.add(a); return l; } 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 List removeDyn(List l, A a) { if (l == null) return null; l.remove(a); return empty(l) ? null : l; } static void setSelectedIndex(final JList l, final int i) { if (l != null) { swing(new Runnable() { public void run() { try { l.setSelectedIndex(i); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "l.setSelectedIndex(i);"; }}); } } static void setSelectedIndex(final JComboBox cb, final int i) { if (cb != null) { swing(new Runnable() { public void run() { try { cb.setSelectedIndex(i); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "cb.setSelectedIndex(i);"; }}); } } static void copyArrayToVector(Object[] array, Vector v) { v.clear(); v.addAll(toList(array)); } static void swing_standardTabBehavior(KeyEvent key) { if ((key.getModifiers() & ActionEvent.SHIFT_MASK) != 0) KeyboardFocusManager.getCurrentKeyboardFocusManager().focusPreviousComponent(); else KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent(); } static List scoredSearch(String query, Iterable data) { Map scores = new HashMap(); List prepared = scoredSearch_prepare(query); for (String s : data) { int score = scoredSearch_score(s, prepared); if (score != 0) scores.put(s, score); } return keysSortedByValuesDesc(scores); } static void onChange(JSpinner spinner, Object r) { spinner.addChangeListener(changeListener(r)); } static A onChange(A b, Object r) { b.addItemListener(itemListener(r)); return b; } static void onChange(JTextComponent tc, Object r) { onUpdate(tc, r); } static void onChange(final JSlider slider, final Object r) { { swing(new Runnable() { public void run() { try { slider.addChangeListener(changeListener(r)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "slider.addChangeListener(changeListener(r));"; }}); } } static void onChange(JComboBox cb, final Object r) { if (isEditableComboBox(cb)) onChange(textFieldFromComboBox(cb), r); else onSelectedItem(cb, new VF1() { public void get(String s) { try { callF(r) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callF(r)"; }}); } static String dbBotName(String progIDWithCase) { return fsI_flex(progIDWithCase) + " Concepts"; } 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; } 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)); } static Object rpc(String botName, String method, Object... args) { return unstructure_matchOK2OrFail( sendToLocalBot(botName, rpc_makeCall(method, args))); } static Object rpc(DialogIO bot, String method, Object... args) { return unstructure_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 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 void close(AutoCloseable c) { _close(c); } static File loadLibrary(String snippetID) { return loadBinarySnippet(snippetID); } static int toIntPercent(double ratio) { return roundToInt(ratio*100); } static volatile boolean readLine_noReadLine; static String readLine_lastInput; static String readLine_prefix = "[] "; static String readLine() { if (readLine_noReadLine) return null; String s = readLineHidden(); if (s != null) { readLine_lastInput = s; print(readLine_prefix + s); } return s; } 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)); } 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) { db_mainConcepts().dontSave = true; // SAFETY throw rethrow(_e); } if (autoSaveInterval != null) db_mainConcepts().persist(autoSaveInterval); else db_mainConcepts().persist(); dbBot(); } 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: if field is a Ref<>, you can thoretically use findBackRefs static A findConceptWhere(Class c, Object... params) { return findConceptWhere(db_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; } static String getInnerMessage(Throwable e) { if (e == null) return null; return getInnerException(e).getMessage(); } static boolean boolPar(ThreadLocal tl) { return boolOptParam(tl); } // defaults to false static boolean boolPar(Object[] __, String name) { return boolOptParam(__, name); } static boolean boolPar(String name, Object[] __) { return boolOptParam(__, name); } static boolean boolPar(String name, Map __) { return boolOptParam(name, __); } static boolean boolPar(String name, Object[] params, boolean defaultValue) { return optParam(params, name, defaultValue); } static A lastThat(List l, Object pred) { for (int i = l(l)-1; i >= 0; i--) { A a = l.get(i); if (checkCondition(pred, a)) return a; } return null; } static A lastThat(Object pred, List l) { return lastThat(l, pred); } static boolean fileNotEmpty(File f) { return isFile(f) && fileSize(f) > 0; } static List sortByFileName(List l) { return sortFilesByName(l); } static List conceptBackupFiles(String progID) { Pattern pat = Pattern.compile("^(.*)\\.backup(20\\d\\d)(\\d\\d)(\\d\\d)-(\\d\\d)$"); File dir = programDir(progID); List l = new ArrayList(); for (File f : listFilesNotDirs(dir, newFile(dir, "backups"))) { String s = f.getName(); Matcher matcher = pat.matcher(s); { if (!(matcher.find())) continue; } String originalName = matcher.group(1); { if (!(eq(originalName, "concepts.structure.gz"))) continue; } l.add(f); } return l; } static String stringIf(boolean b, String s) { return stringIfTrue(b, s); } static void clearConceptsOf(String progID) { getProgramFile(progID, "concepts.structure").delete(); getProgramFile(progID, "idCounter.structure").delete(); } static Lock saveLock_lock = fairLock(); static Lock saveLock() { return saveLock_lock; } static File tempFileFor(File f) { return new File(f.getPath() + "_temp"); } static double toM_double(long l) { return l/(1024*1024.0); } public static String rtrimSpaces(String s) { if (s == null) return null; int i = s.length(); while (i > 0 && " \t".indexOf(s.charAt(i-1)) >= 0) --i; return i < s.length() ? s.substring(0, i) : s; } static int year() { return localYear(); } static int year(long now) { return localYear(now); } static int month() { return localMonth(); } static int month(long now) { return localMonth(now); } static int dayOfMonth() { return localDayOfMonth(); } static int dayOfMonth(long now) { return localDayOfMonth(now); } 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 dynamicClassName(Object o) { if (o instanceof DynamicObject && ((DynamicObject) o).className != null) return "main$" + ((DynamicObject) o).className; return className(o); } 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 int csetAll(Concept c, Object... values) { return cset(c, values); } static Object[] expandParams(Class c, Object[] params) { if (l(params) == 1) params = new Object[] { singleFieldName(c), params[0] }; else warnIfOddCount(params); return params; } static Object derefRef(Object o) { if (o instanceof Concept.Ref) o = ((Concept.Ref) o).get(); return o; } 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 void assertNotOnAWTThread() { assertFalse("Can't do this in AWT thread", isAWTThread()); } static List scoredSearch_prepare(String query) { return map("replacePlusWithSpace",splitAtSpace(query)); } static int scoredSearch_score(Iterable l, List words) { int score = 0; if (l != null) for (String s : l) score += scoredSearch_score(s, words); return score; } static int scoredSearch_score(String s, List words) { int score = 0; if (nempty(s)) for (String word : words) score += scoredSearch_score_single(s, word); return score; } static int scoredSearch_score(String s, String query) { return scoredSearch_score(s, scoredSearch_prepare(query)); } static List keysSortedByValuesDesc(final Map map) { List l = new ArrayList(map.keySet()); sort(l, mapComparatorDesc(map)); return l; } static ChangeListener changeListener(final Object r) { return new ChangeListener() { public void stateChanged(ChangeEvent e) { pcallF(r); } }; } static ItemListener itemListener(final Object r) { return new ItemListener() { public void itemStateChanged(ItemEvent e) { pcallF(r); } }; } // action = runnable or method name static void onUpdate(JComponent c, final Object r) { if (c instanceof JTextComponent) ((JTextComponent) c).getDocument().addDocumentListener(new DocumentListener() { public void insertUpdate(DocumentEvent e) { call(r); } public void removeUpdate(DocumentEvent e) { call(r); } public void changedUpdate(DocumentEvent e) { call(r); } }); else if (c instanceof ItemSelectable) // JCheckBox and others ((ItemSelectable) c).addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { call(r); } }); else print("Warning: onUpdate doesn't know " + getClassName(c)); } static void onUpdate(List l, Object r) { for (JComponent c : l) onUpdate(c, r); } static JTextField textFieldFromComboBox(JComboBox cb) { return (JTextField) cb.getEditor().getEditorComponent(); } static JComboBox onSelectedItem(final JComboBox cb, final VF1 f) { addActionListener(cb, new Runnable() { public void run() { try { pcallF(f, selectedItem(cb)) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pcallF(f, selectedItem(cb))"; }}); return cb; } static String fsI_flex(String s) { return startsWithDigit(s) ? "#" + s : s; } 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(); } public void close() { try { io.close(); } catch (Exception __e) { throw rethrow(__e); } } void sendLine(String line) { io.sendLine(format3("please forward to bot *: *", subBot, line)); } } 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 { Lock __6 = lock; lock(__6); try { w.write(line + "\n"); w.flush(); } finally { unlock(__6); } } catch (Exception __e) { throw rethrow(__e); } } String readLineImpl() { try { return in.readLine(); } catch (Exception __e) { throw rethrow(__e); } } public void close() { try { if (!noClose) s.close(); } catch (IOException e) { // whatever } } Socket getSocket() { return s; } } 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 String firstPartOfHelloString(String s) { int i = s.lastIndexOf('/'); return i < 0 ? s : rtrim(s.substring(0, i)); } static String sendToLocalBotQuietly(String bot, String text, Object... args) { text = format3(text, args); DialogIO channel = newFindBot2(bot); try { 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 { _close(channel); }} static String sendToLocalBotQuietly(int port, String text, Object... args) { text = format3(text, args); DialogIO channel = talkTo(port); try { try { channel.readLine(); channel.sendLine(text); String s = channel.readLine(); return s; } catch (Throwable e) { e.printStackTrace(); return null; } } finally { _close(channel); }} static String getBotAddress(String bot) { List l = fullBotScan(bot); return empty(l) ? null : first(l).address; } static Object unstructure_matchOK2OrFail(String s) { if (swic(s, "ok ")) return unstructure_startingAtIndex(s, 3); else throw fail(s); } static String sendToLocalBot(String bot, String text, Object... args) { text = format3(text, args); DialogIO channel = findBot(bot); try { 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 { _close(channel); }} static String sendToLocalBot(int port, String text, Object... args) { text = format3(text, args); DialogIO channel = talkTo(port); try { 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 { _close(channel); }} static String format(String pat, Object... args) { return format3(pat, args); } static A uniqueConcept(Class c, Object... params) { return uniqueConcept(db_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 = unlisted(c); csetAll(x, params); cc.register(x); } return x; } static int roundToInt(double d) { return (int) Math.round(d); } static String readLineHidden() { try { if (get(javax(), "readLine_reader") == null) set(javax(), "readLine_reader" , new BufferedReader(new InputStreamReader(System.in, "UTF-8"))); try { return ((BufferedReader) get(javax(), "readLine_reader")).readLine(); } finally { consoleClearInput(); } } catch (Exception __e) { throw rethrow(__e); } } static boolean isNonNegativeInteger(String s) { int n = l(s); if (n == 0) return false; int i = 0; while (i < n) { char c = s.charAt(i); if (c < '0' || c > '9') return false; ++i; } return true; } static void ensureDBNotRunning(String name) { if (hasBot(name)) { try { String framesBot = dropSuffix(".", name) + " Frames"; print("Trying to activate frames of running DB: " + framesBot); if (isOK(sendOpt(framesBot, "activate frames")) && isMainProgram()) cleanKill(); } catch (Throwable __e) { _handleException(__e); } throw fail("Already running: " + name); } } static void ensureDBNotRunning() { ensureDBNotRunning(dbBotStandardName()); } static String dbBotStandardName() { String home = userHome(); String name = dbBotName(getDBProgramID()); if (neq(home, actualUserHome())) name += " " + quote(home); return name + "."; } static volatile Android3 dbBot_instance; static Android3 dbBot() { return dbBot(dbBotStandardName()); } static Android3 dbBot(String name) { ensureDBNotRunning(name); return dbBot_instance = methodsBot2(name, assertNotNull(db_mainConcepts()), db_standardExposedMethods(), db_mainConcepts().lock); } static boolean boolOptParam(ThreadLocal tl) { return isTrue(optPar(tl)); } // defaults to false static boolean boolOptParam(Object[] __, String name) { return isTrue(optParam(__, name)); } static boolean boolOptParam(String name, Object[] __) { return boolOptParam(__, name); } static boolean boolOptParam(String name, Map __) { return isTrue(optPar(name, __)); } static boolean isFile(File f) { return f != null && f.isFile(); } static boolean isFile(String path) { return isFile(newFile(path)); } static List sortFilesByName(List l) { sort(l, new Comparator() { public int compare(File a, File b) { return stdcompare(a.getName(), b.getName()); } }); return l; } static List listFilesNotDirs(String dir) { return listFilesOnly(dir); } static List listFilesNotDirs(File... dirs) { return listFilesOnly(dirs); } static String stringIfTrue(boolean b, String s) { return b ? s : ""; } static int localYear() { return localYear(now()); } static int localYear(long time) { return parseInt(simpleDateFormat_local("yyyy").format(time)); } static int localMonth(long time) { return parseInt(simpleDateFormat_local("MM").format(time)); } static int localMonth() { return localMonth(now()); } static int localDayOfMonth(long time) { return parseInt(simpleDateFormat_local("dd").format(time)); } static int localDayOfMonth() { return localDayOfMonth(now()); } 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 String replacePlusWithSpace(String s) { return replace(s, '+', ' '); } static List splitAtSpace(String s) { return empty(s) ? emptyList() : asList(s.split("\\s+")); } static int scoredSearch_score_single(String s, String query) { int i = indexOfIC_underscore(s, query); if (i < 0) return 0; if (i > 0) return 1; return l(s) == l(query) ? 3 : 2; } 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 String selectedItem(JList l) { return getSelectedItem(l); } static String selectedItem(JComboBox cb) { return getSelectedItem(cb); } static boolean isLocalhost(String ip) { return isLoopbackIP(ip) || eqic(ip, "localhost"); } static int vmPort() { return myVMPort(); } 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); } } public void close() {} Socket getSocket() { return null; } } 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 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 Object unstructure_startingAtIndex(String s, int i) { return unstructure_tok(javaTokC_noMLS_iterator(s, i), false, null); } // make concept instance that is not connected to DB static A unlisted(Class c, Object... args) { concepts_unlisted.set(true); try { return nuObject(c, args); } finally { concepts_unlisted.set(null); } } static void consoleClearInput() { consoleSetInput(""); } static boolean hasBot(String searchPattern) { try { DialogIO io = findBot(searchPattern); if (io != null) { io.close(); return true; } else return false; } catch (Exception __e) { throw rethrow(__e); } } static boolean isOK(String s) { s = trim(s); return swic(s, "ok ") || eqic(s, "ok") || matchStart("ok", s); } static String sendOpt(String bot, String text, Object... args) { return sendToLocalBotOpt(bot, text, args); } static boolean isMainProgram() { return creator() == null; } static void cleanKill() { cleanKillVM(); } 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 List db_standardExposedMethods_list = ll("xlist", "xnew", "xset", "xdelete", "xget", "xclass", "xfullgrab", "xshutdown", "xchangeCount", "xcount"); static List db_standardExposedMethods() { return db_standardExposedMethods_list; } static SimpleDateFormat simpleDateFormat_local(String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); sdf.setTimeZone(localTimeZone()); return sdf; } // 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 int indexOfIC_underscore(String a, String b) { int la = l(a), lb = l(b); if (la < lb) return -1; int n = la-lb; elsewhere: for (int i = 0; i <= n; i++) { for (int j = 0; j < lb; j++) { char c2 = b.charAt(j); if (c2 == '_' || eqic(c2, a.charAt(i+j))) { /* matching char */ } else continue elsewhere; } return i; } return -1; } static boolean isLoopbackIP(String ip) { return eq(ip, "127.0.0.1"); } static int myVMPort() { List records = (List) (get(getJavaX(), "record_list")); Object android = last(records); return or0((Integer) get(android, "port")); } 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 String sendToThisVM_newThread(String s, Object... args) { final String _s = format(s, args); try { return (String) evalInNewThread(new F0() { Object get() { try { return callStaticAnswerMethod(getJavaX(), _s); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret callStaticAnswerMethod(getJavaX(), _s);"; }}); } catch (Throwable e) { e = getInnerException(e); printStackTrace(e); return str(e); } } static void consoleSetInput(final String text) { if (headless()) return; setTextAndSelectAll(consoleInputField(), text); focusConsole(); } 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; return matchStart(pat, parse3_cachedInput(s), matches); } static boolean matchStart(String pat, List toks, Matches matches) { if (toks == null) return false; List tokpat = parse3_cachedPattern(pat); 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; if (matches != null) { matches.m = new String[m.length+1]; arraycopy(m, matches.m); matches.m[m.length] = joinSubList(toks, tokpat.size(), toks.size()); // for Matches.rest() } return true; } static String sendToLocalBotOpt(String bot, String text, Object... args) { if (bot == null) return null; text = format(text, args); DialogIO channel = findBot(bot); try { 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 { _close(channel); }} static WeakReference creator_class; static Object creator() { return creator_class == null ? null : creator_class.get(); } 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 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(defaultBotName()); } static TimeZone localTimeZone() { return getTimeZone(standardTimeZone()); // TimeZone.getDefault()? } 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 A last(A[] a) { return l(a) != 0 ? a[l(a)-1] : null; } static A last(Iterator it) { A a = null; while (it.hasNext()) { ping(); a = it.next(); } return a; } static int or0(Integer i) { return i == null ? 0 : i; } static long or0(Long l) { return l == null ? 0L : l; } static double or0(Double d) { return d == null ? 0.0 : d; } static Object evalInNewThread(final Object f) { final Flag flag = new Flag(); final Var var = new Var(); final Var exception = new Var(); startThread(new Runnable() { public void run() { try { try { var.set(callF(f)); } catch (Throwable e) { exception.set(e); } flag.raise(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "try {\r\n var.set(callF(f));\r\n } catch (Throwable e) {\r\n exception..."; }}); flag.waitUntilUp(); if (exception.has()) throw rethrow(exception.get()); return var.get(); } 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 JTextField setTextAndSelectAll(final JTextField tf, final String text) { { swing(new Runnable() { public void run() { try { tf.setText(text); tf.selectAll(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "tf.setText(text);\r\n tf.selectAll();"; }}); } return tf; } static JTextField consoleInputField() { Object console = get(getJavaX(), "console"); return (JTextField) getOpt(console, "tfInput"); } static void focusConsole(String s) { setConsoleInput(s); focusConsole(); } static void focusConsole() { JComponent tf = consoleInputFieldOrComboBox(); if (tf != null) { //print("Focusing console"); tf.requestFocus(); } } static String ok2(String s) { return "ok " + s; } // An "Android" is a program that accepts text questions (on console or TCP) and outputs one response text per question //please include function myJavaSource. // for getting my known commands 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) { _handleException(__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 && (readLine_noReadLine || makeAndroid3_consoleInUse())) a.console = false; record(a); if (a.useMultiPort) a.vport = addToMultiPort(a.greeting, makeAndroid3_verboseResponder(a)); if (a.console) makeAndroid3_handleConsole(a); if (a.useMultiPort) return a; a.handler = makeAndroid3_makeDialogHandler(a); if (a.quiet) startDialogServer_quiet.set(true); try { a.port = a.daemon ? startDialogServerOnPortAboveDaemon(a.startPort, a.handler) : startDialogServerOnPortAbove(a.startPort, a.handler); } finally { startDialogServer_quiet.set(null); } a.server = startDialogServer_serverSocket; return a; } static void makeAndroid3_handleConsole(final Android3 a) { // Console handling stuff if (!a.quiet) print("You may also type on this console."); startThread(new Runnable() { 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 (Exception __e) { throw rethrow(__e); } } public String toString() { return "List history = new ArrayList();\r\n while (licensed()) {\r\n Stri..."; }}); } 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() { if (isTrue(vm_generalMap_get("consoleInUse"))) return true; 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 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 String defaultBotName() { return getProgramTitle() + "."; } static TimeZone getTimeZone(String name) { return TimeZone.getTimeZone(name); } static String standardTimeZone_name = "Europe/Berlin"; static String standardTimeZone() { return standardTimeZone_name; } static void setConsoleInput(String text) { consoleSetInput(text); } static JComponent consoleInputFieldOrComboBox() { Object console = get(getJavaX(), "console"); JComboBox cb = (JComboBox) (getOpt(console, "cbInput")); if (cb != null) return cb; return (JTextField) getOpt(console, "tfInput"); } static void removeFromMultiPort(long vport) { if (vport == 0) return; for (Object port : getMultiPorts()) call(port, "removePort", vport); } static List record_list = synchroList(); static void record(Object o) { record_list.add(o); } 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 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); } } public void close() { try { s.close(); } catch (IOException e) { // whatever } } Socket getSocket() { return s; } }; try { handler.run(io); } finally { if (!io.noClose) 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 boolean publicCommOn() { return "1".equals(loadTextFile(new File(userHome(), ".javax/public-communication"))); } 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 String getInjectionID() { return (String) call(getJavaX(), "getInjectionID", getMainClass()); } static List getMultiPorts() { return (List) callOpt(getJavaX(), "getMultiPorts"); } // 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 boolean forbiddenPort(int port) { return port == 5037; // adb } 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 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> 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); } }/** this class is fully thread-safe */ static class Flag implements Runnable { 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; } boolean get() { return isUp(); } 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); } } public void run() { raise(); } } public static boolean isWindows() { return System.getProperty("os.name").contains("Windows"); } 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"); } } }