Warning : session_start(): open(/var/lib/php/sessions/sess_i7m44djh3h8s3agipgkc6h42b3, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning : session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.math.*;
import java.text.NumberFormat;
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 javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;
import javax.swing.Timer;
import java.awt.font.GlyphVector;
import javax.swing.undo.UndoManager;
import java.awt.datatransfer.UnsupportedFlavorException;
class main {
public static void main(final String[] args) throws Exception {
ai_alternativeSmartBot();
WordSequence pat = new WordSequence(
new SomethingWhichIs("a noun"),
new SomethingWhichIs("a verb"));
printStruct(pat);
WordSequence real = new WordSequence("plants", "grow");
printStruct(real);
IdentityHashMap map = new IdentityHashMap();
if (recursiveMatch(ll("matchWordSequence", "matchSomethingWhichIs"), pat, real, map))
printStruct(map);
else
print("no match");
}
static A printStruct(String prefix, A a) {
printStructure(prefix, a);
return a;
}
static A printStruct(A a) {
printStructure(a);
return a;
}
static List ll(A... a) {
ArrayList l = new ArrayList(a.length);
for (A x : a) l.add(x);
return l;
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static volatile StringBuffer print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
//static int print_maxLineLength = 0; // 0 = unset
static boolean print_silent; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal print_byThread; // special handling by thread - prefers F1
static void print() {
print("");
}
// slightly overblown signature to return original object...
static A print(A o) {
ping();
if (print_silent) return o;
String s = String.valueOf(o) + "\n";
print_noNewLine(s);
return o;
}
static void print_noNewLine(String s) {
if (print_byThread != null) {
Object f = print_byThread.get();
if (f != null)
if (isFalse(f instanceof F1 ? ((F1) f).get(s) : callF(f, s))) return;
}
print_raw(s);
}
static void print_raw(String s) {
s = fixNewLines(s);
// TODO if (print_maxLineLength != 0)
StringBuffer loc = local_log;
StringBuffer buf = print_log;
int loc_max = print_log_max;
if (buf != loc && buf != null) {
print_append(buf, s, print_log_max);
loc_max = local_log_max;
}
if (loc != null)
print_append(loc, s, loc_max);
System.out.print(s);
}
static void print(long l) {
print(String.valueOf(l));
}
static void print(char c) {
print(String.valueOf(c));
}
static void print_append(StringBuffer buf, String s, int max) {
synchronized(buf) {
buf.append(s);
max /= 2;
if (buf.length() > max) try {
int newLength = max/2;
int ofs = buf.length()-newLength;
String newString = buf.substring(ofs);
buf.setLength(0);
buf.append("[...] ").append(newString);
} catch (Exception e) {
buf.setLength(0);
}
}
}
static boolean matchSomethingWhichIs(Object pat, Object real, IMatchF elementMatcher, Map map) {
if (pat instanceof SomethingWhichIs && real instanceof String) {
if (!ai_is(quote((String) real), ((SomethingWhichIs) pat).type)) return false;
print("Assigning " + pat + " => " + real);
return strictPut(map, pat, real);
}
return false;
}
static boolean matchWordSequence(Object _pat, Object _real, IMatchF matchF, Map map) {
if (!(_pat instanceof WordSequence && _real instanceof WordSequence)) return false;
WordSequence pat = (WordSequence) ( _pat);
WordSequence real = (WordSequence) ( _real);
int n = l(pat.l);
if (n != l(real.l)) return false;
for (int i = 0; i < n; i++)
if (!matchF.get(pat.l.get(i), real.l.get(i), map)) return false;
return true;
}
static Map _registerThread_threads = newWeakHashMap();
static Thread _registerThread(Thread t) {
_registerThread_threads.put(t, true);
return t;
}
static void _registerThread() { _registerThread(Thread.currentThread()); }
static void ai_alternativeSmartBot() {
localSoftwareMadeWebs_programsToRead_set(ll("#1010745", programID()));
ai_smartBotInit();
}
static boolean recursiveMatch(List matchFunctions, Object pat, Object real, Map map) {
return new RecursiveMatcher(matchFunctions).get(pat, real, map);
}
static String fixNewLines(String s) {
return s.replace("\r\n", "\n").replace("\r", "\n");
}
static boolean strictPut(Map map, A key, B value) {
return mapPutStrictly(map, key, value);
}
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();
// always returns true
static boolean ping() {
if (ping_pauseAll || ping_anyActions ) ping_impl();
return true;
}
// returns true when it slept
static boolean ping_impl() { try {
if (ping_pauseAll && !isAWTThread()) {
do
Thread.sleep(ping_sleep);
while (ping_pauseAll);
return true;
}
if (ping_anyActions) {
Object action;
synchronized(ping_actions) {
action = ping_actions.get(currentThread());
if (action instanceof Runnable)
ping_actions.remove(currentThread());
if (ping_actions.isEmpty()) ping_anyActions = false;
}
if (action instanceof Runnable)
((Runnable) action).run();
else if (eq(action, "cancelled"))
throw fail("Thread cancelled.");
}
return false;
} catch (Exception __e) { throw rethrow(__e); } }
static WeakHashMap> callF_cache = new WeakHashMap();
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 Object callF(Object f, Object... args) { try {
if (f instanceof String)
return callMC((String) f, args);
if (f instanceof Runnable) {
((Runnable) f).run();
return null;
}
if (f == null) return null;
Class c = f.getClass();
ArrayList methods;
synchronized(callF_cache) {
methods = callF_cache.get(c);
if (methods == null)
methods = callF_makeCache(c);
}
int n = l(methods);
if (n == 0) throw fail("No get method in " + getClassName(c));
if (n == 1) return methods.get(0).invoke(f, args);
for (int i = 0; i < n; i++) {
Method m = methods.get(i);
if (call_checkArgs(m, args, false))
return m.invoke(f, args);
}
throw fail("No matching get method in " + getClassName(c));
} catch (Exception __e) { throw rethrow(__e); } }
// used internally
static ArrayList callF_makeCache(Class c) {
ArrayList l = new ArrayList();
Class _c = c;
do {
for (Method m : _c.getDeclaredMethods())
if (m.getName().equals("get")) {
m.setAccessible(true);
l.add(m);
}
if (!l.isEmpty()) break;
_c = _c.getSuperclass();
} while (_c != null);
callF_cache.put(c, l);
return l;
}
static void localSoftwareMadeWebs_programsToRead_set(List l) {
localSoftwareMadeWebs_programsToRead_list = l;
}
static boolean ai_is(String a, String b) {
return ai_hasTriple(a, "is", b);
}
static boolean isFalse(Object o) {
return eq(false, o);
}
static Map newWeakHashMap() {
return _registerWeakMap(synchroMap(new WeakHashMap()));
}
static int l(Object[] a) { return a == null ? 0 : a.length; }
static int l(boolean[] a) { return a == null ? 0 : a.length; }
static int l(byte[] a) { return a == null ? 0 : a.length; }
static int l(int[] a) { return a == null ? 0 : a.length; }
static int l(float[] a) { return a == null ? 0 : a.length; }
static int l(char[] a) { return a == null ? 0 : a.length; }
static int l(Collection c) { return c == null ? 0 : c.size(); }
static int l(Map m) { return m == null ? 0 : m.size(); }
static int l(CharSequence s) { return s == null ? 0 : s.length(); } static long l(File f) { return f == null ? 0 : f.length(); }
static int l(Object o) {
return o instanceof String ? l((String) o)
: o instanceof Map ? l((Map) o)
: l((Collection) o); // incomplete
}
static int l(Lisp l) { return l == null ? 0 : l.size(); }
static void ai_smartBotInit() {
ai_tripleMode();
ai_useThoughtSpaces(true);
ai_enabled(true);
}
static String quote(Object o) {
if (o == null) return "null";
return quote(str(o));
}
static String quote(String s) {
if (s == null) return "null";
StringBuilder out = new StringBuilder((int) (l(s)*1.5+2));
quote_impl(s, out);
return out.toString();
}
static void quote_impl(String s, StringBuilder out) {
out.append('"');
int l = s.length();
for (int i = 0; i < l; i++) {
char c = s.charAt(i);
if (c == '\\' || c == '"')
out.append('\\').append(c);
else if (c == '\r')
out.append("\\r");
else if (c == '\n')
out.append("\\n");
else
out.append(c);
}
out.append('"');
}
static String programID() {
return getProgramID();
}
static void printStructure(String prefix, Object o) {
if (endsWithLetter(prefix)) prefix += ": ";
print(prefix + structureForUser(o));
}
static void printStructure(Object o) {
print(structureForUser(o));
}
static Map synchroMap() {
return synchroHashMap();
}
static Map synchroMap(Map map) {
return Collections.synchronizedMap(map);
}
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 boolean ai_useThoughtSpaces_value;
static boolean ai_useThoughtSpaces() {
return ai_useThoughtSpaces_value;
}
static void ai_useThoughtSpaces(boolean flag) {
ai_useThoughtSpaces_value = flag;
}
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 me.invoke(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 me.invoke(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 me.invoke(null, args);
}
throw fail("No method called " + method + " with matching arguments found in main");
} catch (Exception __e) { throw rethrow(__e); } }
static void callMC_makeCache() {
synchronized(callMC_cache) {
callMC_cache.clear();
Class _c = (Class) mc(), c = _c;
while (c != null) {
for (Method m : c.getDeclaredMethods())
if ((m.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) {
m.setAccessible(true);
multiMapPut(callMC_cache, m.getName(), m);
}
c = c.getSuperclass();
}
}
}
static List localSoftwareMadeWebs_programsToRead_list = ll("#1010745");
static List localSoftwareMadeWebs_programsToRead() {
return localSoftwareMadeWebs_programsToRead_list;
//ret websPostedIn();
}
static String structureForUser(Object o) {
return beautifyStructure(struct_noStringSharing(o));
}
// 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 boolean endsWithLetter(String s) {
return nempty(s) && isLetter(last(s));
}
static RuntimeException rethrow(Throwable e) {
throw asRuntimeException(e);
}
static Thread currentThread() {
return Thread.currentThread();
}
static boolean mapPutStrictly(Map map, A key, B value) {
if (!(map != null && key != null && value != null)) return true;
B old = map.get(key);
if (old == null) { map.put(key, value); return true; }
return eq(old, value);
}
static boolean eq(Object a, Object b) {
return a == null ? b == null : a == b || a.equals(b);
}
// a little kludge for stuff like eq(symbol, "$X")
static void ai_tripleMode() {
if (cachedNodeIndex_autoMake) {
cachedNodeIndex_autoMake = false;
ai_fillTripleIndex();
}
}
static boolean ai_enabled_value;
static boolean ai_enabled() {
return ai_enabled_value;
}
static void ai_enabled(boolean value) {
ai_enabled_value = value;
}
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return new String(c);
}
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 Object call(Object o) {
return callFunction(o);
}
// varargs assignment fixer for a single string array argument
static Object call(Object o, String method, String[] arg) {
return call(o, method, new Object[] {arg});
}
static Object call(Object o, String method, Object... args) {
try {
if (o instanceof Class) {
Method m = call_findStaticMethod((Class) o, method, args, false);
m.setAccessible(true);
return m.invoke(null, args);
} else {
Method m = call_findMethod(o, method, args, false);
m.setAccessible(true);
return m.invoke(o, args);
}
} catch (Exception e) {
throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
}
}
static Method call_findStaticMethod(Class c, String method, Object[] args, boolean debug) {
Class _c = c;
while (c != null) {
for (Method m : c.getDeclaredMethods()) {
if (debug)
System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
if (!m.getName().equals(method)) {
if (debug) System.out.println("Method name mismatch: " + method);
continue;
}
if ((m.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0 || !call_checkArgs(m, args, debug))
continue;
return m;
}
c = c.getSuperclass();
}
throw new RuntimeException("Method '" + method + "' (static) with " + args.length + " parameter(s) not found in " + _c.getName());
}
static Method call_findMethod(Object o, String method, Object[] args, boolean debug) {
Class c = o.getClass();
while (c != null) {
for (Method m : c.getDeclaredMethods()) {
if (debug)
System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
if (m.getName().equals(method) && call_checkArgs(m, args, debug))
return m;
}
c = c.getSuperclass();
}
throw new RuntimeException("Method '" + method + "' (non-static) with " + args.length + " parameter(s) not found in " + o.getClass().getName());
}
private static boolean call_checkArgs(Method m, Object[] args, boolean debug) {
Class>[] types = m.getParameterTypes();
if (types.length != args.length) {
if (debug)
System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
return false;
}
for (int i = 0; i < types.length; i++)
if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
if (debug)
System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
return false;
}
return true;
}
static 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 ai_hasTriple(String a, String b, String c) {
return ai_cache_hasTriple(a, b, c);
}
static boolean ai_hasTriple(T3 t) {
return ai_cache_hasTriple(t);
}
static String getClassName(Object o) {
return o == null ? "null" : o instanceof Class ? ((Class) o).getName() : o.getClass().getName();
}
static Boolean isHeadless_cache;
static boolean isHeadless() {
if (isHeadless_cache != null) return isHeadless_cache;
if (GraphicsEnvironment.isHeadless()) return isHeadless_cache = true;
// Also check if AWT actually works.
// If DISPLAY variable is set but no X server up, this will notice.
try {
SwingUtilities.isEventDispatchThread();
return isHeadless_cache = false;
} catch (Throwable e) { return isHeadless_cache = true; }
}
static String struct_noStringSharing(Object o) {
structure_Data d = new structure_Data();
d.noStringSharing = true;
return structure(o, d);
}
static Object callFunction(Object f, Object... args) {
return callF(f, args);
}
static List classNames(Collection l) {
return getClassNames(l);
}
static List classNames(Object[] l) {
return getClassNames(Arrays.asList(l));
}
static RuntimeException asRuntimeException(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static Class javax() {
return getJavaX();
}
static Class mc() {
return main.class;
}
static A assertNotNull(A a) {
assertTrue(a != null);
return a;
}
static A assertNotNull(String msg, A a) {
assertTrue(msg, a != null);
return a;
}
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 String formatSnippetID(String id) {
return "#" + parseSnippetID(id);
}
static String formatSnippetID(long id) {
return "#" + id;
}
// extended over Class.isInstance() to handle primitive types
static boolean isInstanceX(Class type, Object arg) {
if (type == boolean.class) return arg instanceof Boolean;
if (type == int.class) return arg instanceof Integer;
if (type == long.class) return arg instanceof Long;
if (type == float.class) return arg instanceof Float;
if (type == short.class) return arg instanceof Short;
if (type == char.class) return arg instanceof Character;
if (type == byte.class) return arg instanceof Byte;
if (type == double.class) return arg instanceof Double;
return type.isInstance(arg);
}
static boolean ai_cache_hasTriple_debug;
static boolean ai_cache_hasTriple(String a, String b, String c) {
if (a == null || b == null || c == null) return false;
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 A last(List l) {
return empty(l) ? null : l.get(l.size()-1);
}
static char last(String s) {
return empty(s) ? '#' : s.charAt(l(s)-1);
}
static int last(int[] a) {
return l(a) != 0 ? a[l(a)-1] : 0;
}
static Object last(Object[] a) {
return l(a) != 0 ? a[l(a)-1] : null;
}
static void printException(Throwable e) {
printStackTrace(e);
}
static String beautifyStructure(String s) {
return structure_addTokenMarkers(s);
}
static void ai_fillTripleIndex() {
print("Getting triples.");
long _startTime_41 = sysNow();
List triples = localSoftwareMadeTriples();
_startTime_41 = sysNow()-_startTime_41; saveTiming(_startTime_41);
long time = lastTiming();
long us = ratio(time*1000, l(triples));
//print("Got triples in " + time + " ms. Making triple index of " + nTriples(triples) + ".");
print("Got " + nTriples(triples) + " in " + us + " "+ unicode_micro() + "s per triple. Making triple index.");
long _startTime_42 = sysNow();
ai_replaceTripleIndexWithTriples(triples);
done2_always("Triple index made.", _startTime_42);
}
static boolean isLetter(char c) {
return Character.isLetter(c);
}
static List synchroList() {
return Collections.synchronizedList(new ArrayList ());
}
static List synchroList(List l) {
return Collections.synchronizedList(l);
}
static String formatSnippetIDOpt(String s) {
return isSnippetID(s) ? formatSnippetID(s) : s;
}
static Class getMainClass() {
return main.class;
}
static Class getMainClass(Object o) { try {
return (o instanceof Class ? (Class) o : o.getClass()).getClassLoader().loadClass("main");
} catch (Exception __e) { throw rethrow(__e); } }
static boolean nempty(Collection c) {
return !isEmpty(c);
}
static boolean nempty(CharSequence s) {
return !isEmpty(s);
}
static boolean nempty(Object[] o) {
return !isEmpty(o);
}
static boolean nempty(Map m) {
return !isEmpty(m);
}
static boolean nempty(Iterator i) {
return i != null && i.hasNext();
}
static Object getOpt(Object o, String field) {
return getOpt_cached(o, field);
}
static Object getOpt_raw(Object o, String field) {
try {
Field f = getOpt_findField(o.getClass(), field);
if (f == null) return null;
f.setAccessible(true);
return f.get(o);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// access of static fields is not yet optimized
static Object getOpt(Class c, String field) {
try {
if (c == null) return null;
Field f = getOpt_findStaticField(c, field);
if (f == null) return null;
f.setAccessible(true);
return f.get(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Field getOpt_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
return null;
}
static Map synchroHashMap() {
return Collections.synchronizedMap(new HashMap());
}
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 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 AtomicLong _handleError_nonVMErrors = new AtomicLong();
static AtomicLong _handleError_vmErrors = new AtomicLong();
static AtomicLong _handleError_outOfMemoryErrors = new AtomicLong();
static volatile long _handleError_lastOutOfMemoryError;
static volatile Error _handleError_lastHardError;
static void _handleError(Error e) {
if (!(e instanceof VirtualMachineError)) {
incAtomicLong(_handleError_nonVMErrors);
return;
}
print("\nHARD ERROR\n");
printStackTrace2(e);
print("\nHARD ERROR\n");
_handleError_lastHardError = e;
incAtomicLong(_handleError_vmErrors);
if (e instanceof OutOfMemoryError) {
incAtomicLong(_handleError_outOfMemoryErrors);
_handleError_lastOutOfMemoryError = sysNow();
}
}
static String sfuLL(Object... x) {
return sfu(ll(x));
}
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 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 boolean empty(Collection c) { return c == null || c.isEmpty(); }
static boolean empty(String s) { return s == null || s.length() == 0; }
static boolean empty(Map map) { return map == null || map.isEmpty(); }
static boolean empty(Object[] o) { return o == null || o.length == 0; }
static boolean empty(Object o) {
if (o instanceof Collection) return empty((Collection) o);
if (o instanceof String) return empty((String) o);
if (o instanceof Map) return empty((Map) o);
if (o instanceof Object[]) return empty((Object[]) o);
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 webs_search_noVar_bool(Web searchWeb, Collection webs) {
return webs_search_noVar(searchWeb, webs) != null;
}
static String structure_addTokenMarkers(String s) {
List tok = javaTok(s);
// find references
TreeSet refs = new TreeSet();
for (int i = 1; i < l(tok); i += 2) {
String t = tok.get(i);
if (t.startsWith("t") && isInteger(t.substring(1)))
refs.add(parseInt(t.substring(1)));
}
if (empty(refs)) return s;
// add markers
for (int i : refs) {
int idx = i*2+1;
String t = "";
if (endsWithLetterOrDigit(tok.get(idx-1))) t = " ";
tok.set(idx, t + "m" + i + " " + tok.get(idx));
}
return join(tok);
}
static String nWebs(long i) {
return n_fancy2(i, "web", "webs");
}
static String nWebs(Collection c) {
return n_fancy2(c, "web", "webs");
}
static ThreadLocal ai_cacheChecker = new ThreadLocal();
static AI_CacheChecker ai_cacheChecker() {
return ai_cacheChecker.get();
}
static void ai_replaceTripleIndexWithTriples(List l) {
TripleIndex i = tripleIndex();
i.clear();
for (TripleWeb w : l)
i.addTriple(w);
i.trimToSize();
}
static Throwable printStackTrace(Throwable e) {
// we go to system.out now - system.err is nonsense
print(getStackTrace(e));
return e;
}
static void printStackTrace() {
printStackTrace(new Throwable());
}
static void printStackTrace(String msg) {
printStackTrace(new Throwable(msg));
}
/*static void printStackTrace(S indent, Throwable e) {
if (endsWithLetter(indent)) indent += " ";
printIndent(indent, getStackTrace(e));
}*/
static long lastTiming() {
return getLastTiming();
}
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 String unicode_micro() {
return "\u00B5";
}
public static boolean isSnippetID(String s) {
try {
parseSnippetID(s);
return true;
} catch (RuntimeException e) {
return false;
}
}
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 long sysNow() {
return System.nanoTime()/1000000;
}
//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();
Class _c = c;
do {
for (Field f : _c.getDeclaredFields()) {
f.setAccessible(true);
String name = f.getName();
if (!map.containsKey(name))
map.put(name, f);
}
_c = _c.getSuperclass();
} while (_c != null);
}
getOpt_cache.put(c, map);
return map;
}
static 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 String nTriples(long i) {
return n_fancy2(i, "triple", "triples");
}
static String nTriples(Collection c) {
return n_fancy2(c, "triple", "triples");
}
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 Class __javax;
static Class getJavaX() {
return __javax;
}
static boolean isEmpty(Collection c) {
return c == null || c.isEmpty();
}
static boolean isEmpty(CharSequence s) {
return s == null || s.length() == 0;
}
static boolean isEmpty(Object[] a) {
return a == null || a.length == 0;
}
static boolean isEmpty(Map map) {
return map == null || map.isEmpty();
}
static boolean structure_showTiming, structure_checkTokenCount;
static String structure(Object o) {
return structure(o, new structure_Data());
}
static String structure(Object o, structure_Data d) {
StringWriter sw = new StringWriter();
d.out = new PrintWriter(sw);
structure_go(o, d);
String s = str(sw);
if (structure_checkTokenCount) {
print("token count=" + d.n);
assertEquals("token count", l(javaTokC(s)), d.n);
}
return s;
}
static void structure_go(Object o, structure_Data d) {
structure_1(o, d);
while (nempty(d.stack))
popLast(d.stack).run();
}
static void structureToPrintWriter(Object o, PrintWriter out) {
structure_Data d = new structure_Data();
d.out = out;
structure_go(o, d);
}
// leave to false, unless unstructure() breaks
static boolean structure_allowShortening = false;
static class structure_Data {
PrintWriter out;
int stringSizeLimit;
int shareStringsLongerThan = 20;
boolean noStringSharing;
IdentityHashMap seen = new IdentityHashMap();
//new BitSet refd;
HashMap strings = new HashMap();
HashSet concepts = new HashSet();
HashMap> fieldsByClass = new HashMap();
int n; // token count
List stack = new ArrayList();
// append single token
structure_Data append(String token) { out.print(token); ++n; return this; }
structure_Data append(int i) { out.print(i); ++n; return this; }
// append multiple tokens
structure_Data append(String token, int tokCount) { out.print(token); n += tokCount; return this; }
// extend last token
structure_Data app(String token) { out.print(token); return this; }
structure_Data app(int i) { out.print(i); return this; }
}
static void structure_1(final Object o, final structure_Data d) {
if (o == null) { d.append("null"); return; }
Class c = o.getClass();
boolean concept = false;
concept = o instanceof Concept;
List lFields = d.fieldsByClass.get(c);
if (lFields == null) {
// these are never back-referenced (for readability)
if (o instanceof Number) {
PrintWriter out = d.out;
if (o instanceof Integer) { int i = ((Integer) o).intValue(); out.print(i); d.n += i < 0 ? 2 : 1; return; }
if (o instanceof Long) { long l = ((Long) o).longValue(); out.print(l); out.print("L"); d.n += l < 0 ? 2 : 1; return; }
if (o instanceof Float) { d.append("fl ", 2); quoteToPrintWriter(str(o), out); return; }
if (o instanceof Double) { d.append("d(", 3); quoteToPrintWriter(str(o), out); d.append(")"); return; }
if (o instanceof BigInteger) { out.print("bigint("); out.print(o); out.print(")"); d.n += ((BigInteger) o).signum() < 0 ? 5 : 4; return; }
}
if (o instanceof Boolean) {
d.append(((Boolean) o).booleanValue() ? "t" : "f"); return;
}
if (o instanceof Character) {
d.append(quoteCharacter((Character) o)); return;
}
if (o instanceof File) {
d.append("File ").append(quote(((File) o).getPath())); return;
}
// referencable objects follow
Integer ref = d.seen.get(o);
if (o instanceof String && ref == null) ref = d.strings.get((String) o);
if (ref != null) { /*d.refd.set(ref);*/ d.append("t").app(ref); return; }
if (!(o instanceof String))
d.seen.put(o, d.n); // record token number
else {
String s = d.stringSizeLimit != 0 ? shorten((String) o, d.stringSizeLimit) : (String) o;
if (!d.noStringSharing) {
if (d.shareStringsLongerThan == Integer.MAX_VALUE)
d.seen.put(o, d.n);
if (l(s) >= d.shareStringsLongerThan)
d.strings.put(s, d.n);
}
quoteToPrintWriter(s, d.out); d.n++; return;
}
if (o instanceof HashSet) {
d.append("hashset ");
structure_1(new ArrayList((Set) o), d);
return;
}
if (o instanceof TreeSet) {
d.append("treeset ");
structure_1(new ArrayList((Set) o), d);
return;
}
String name = c.getName();
if (o instanceof Collection
&& !startsWith(name, "main$")
/* && neq(name, "main$Concept$RefL") */) {
d.append("[");
final int l = d.n;
final Iterator it = ((Collection) o).iterator();
d.stack.add(new Runnable() { public void run() { try {
if (!it.hasNext())
d.append("]");
else {
d.stack.add(this);
if (d.n != l) d.append(", ");
structure_1(it.next(), d);
}
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (!it.hasNext())\r\n d.append(\"]\");\r\n else {\r\n d.sta..."; }});
return;
}
if (o instanceof Map && !startsWith(name, "main$")) {
if (o instanceof LinkedHashMap) d.append("lhm");
else if (o instanceof HashMap) d.append("hm");
d.append("{");
final int l = d.n;
final Iterator it = ((Map) o).entrySet().iterator();
d.stack.add(new Runnable() {
boolean v;
Map.Entry e;
public void run() {
if (v) {
d.append("=");
v = false;
d.stack.add(this);
structure_1(e.getValue(), d);
} else {
if (!it.hasNext())
d.append("}");
else {
e = (Map.Entry) it.next();
v = true;
d.stack.add(this);
if (d.n != l) d.append(", ");
structure_1(e.getKey(), d);
}
}
}
});
return;
}
if (c.isArray()) {
if (o instanceof byte[]) {
d.append("ba ").append(quote(bytesToHex((byte[]) o))); return;
}
final int n = Array.getLength(o);
if (o instanceof boolean[]) {
String hex = boolArrayToHex((boolean[]) o);
int i = l(hex);
while (i > 0 && hex.charAt(i-1) == '0' && hex.charAt(i-2) == '0') i -= 2;
d.append("boolarray ").append(n).app(" ").append(quote(substring(hex, 0, i))); return;
}
String atype = "array", sep = ", ";
if (o instanceof int[]) {
//ret "intarray " + quote(intArrayToHex((int[]) o));
atype = "intarray";
sep = " ";
}
d.append(atype).append("{");
d.stack.add(new Runnable() {
int i;
public void run() {
if (i >= n)
d.append("}");
else {
d.stack.add(this);
if (i > 0) d.append(", ");
structure_1(Array.get(o, i++), d);
}
}
});
return;
}
if (o instanceof Class) {
d.append("class(", 2).append(quote(((Class) o).getName())).append(")"); return;
}
if (o instanceof Throwable) {
d.append("exception(", 2).append(quote(((Throwable) o).getMessage())).append(")"); return;
}
if (o instanceof BitSet) {
BitSet bs = (BitSet) o;
d.append("bitset{", 2);
int l = d.n;
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
if (d.n != l) d.append(", ");
d.append(i);
}
d.append("}"); return;
}
// Need more cases? This should cover all library classes...
if (name.startsWith("java.") || name.startsWith("javax.")) {
d.append("j ").append(quote(str(o))); return; // Hm. this is not unstructure-able
}
if (o instanceof Lisp) {
d.append("l(", 2);
final Lisp lisp = (Lisp) ( o);
structure_1(lisp.head, d);
final Iterator it = lisp.args == null ? emptyIterator() : lisp.args.iterator();
d.stack.add(new Runnable() { public void run() { try {
if (!it.hasNext())
d.append(")");
else {
d.stack.add(this);
d.append(", ");
structure_1(it.next(), d);
}
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (!it.hasNext())\r\n d.append(\")\");\r\n else {\r\n ..."; }});
return;
}
/*if (name.equals("main$Lisp")) {
fail("lisp not supported right now");
}*/
String dynName = shortDynamicClassName(o);
if (concept && !d.concepts.contains(dynName)) {
d.concepts.add(dynName);
d.append("c ");
}
// serialize an object with fields.
// first, collect all fields and values in fv.
TreeSet fields = new TreeSet(new Comparator() {
public int compare(Field a, Field b) {
return stdcompare(a.getName(), b.getName());
}
});
Class cc = c;
while (cc != Object.class) {
for (Field field : getDeclaredFields_cached(cc)) {
if ((field.getModifiers() & (java.lang.reflect.Modifier.STATIC | java.lang.reflect.Modifier.TRANSIENT)) != 0)
continue;
String fieldName = field.getName();
fields.add(field);
// put special cases here...
}
cc = cc.getSuperclass();
}
lFields = asList(fields);
// Render this$1 first because unstructure needs it for constructor call.
for (int i = 0; i < l(lFields); i++) {
Field f = lFields.get(i);
if (f.getName().equals("this$1")) {
lFields.remove(i);
lFields.add(0, f);
break;
}
}
d.fieldsByClass.put(c, lFields);
} // << if (lFields == null)
else { // ref handling for lFields != null
Integer ref = d.seen.get(o);
if (ref != null) { /*d.refd.set(ref);*/ d.append("t").app(ref); return; }
d.seen.put(o, d.n); // record token number
}
LinkedHashMap fv = new LinkedHashMap();
for (Field f : lFields) {
Object value;
try {
value = f.get(o);
} catch (Exception e) {
value = "?";
}
if (value != null)
fv.put(f.getName(), value);
}
String name = c.getName();
String shortName = dropPrefix("main$", name);
// Now we have fields & values. Process fieldValues if it's a DynamicObject.
// omit field "className" if equal to class's name
if (concept && eq(fv.get("className"), shortName))
fv.remove("className");
if (o instanceof DynamicObject) {
fv.putAll((Map) fv.get("fieldValues"));
fv.remove("fieldValues");
shortName = shortDynamicClassName(o);
fv.remove("className");
}
String singleField = fv.size() == 1 ? first(fv.keySet()) : null;
d.append(shortName);
final int l = d.n;
final Iterator it = fv.entrySet().iterator();
d.stack.add(new Runnable() { public void run() { try {
if (!it.hasNext()) {
if (d.n != l)
d.append(")");
} else {
Map.Entry e = (Map.Entry) it.next();
d.append(d.n == l ? "(" : ", ");
d.append((String) e.getKey()).append("=");
d.stack.add(this);
structure_1(e.getValue(), d);
}
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (!it.hasNext()) {\r\n if (d.n != l)\r\n d.append(\")\");\r\n } else..."; }});
}
static List localSoftwareMadeTriples() {
List triples = new ArrayList();
for (String progID : localSoftwareMadeWebs_programsToRead()) try {
triples.addAll(websMadeByProgramAsTriples(progID));
} catch (Throwable __e) { printStackTrace2(__e); }
return triples;
}
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 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 ratio(long x, long y) {
return y == 0 ? 0 : x/y;
}
static void put(Map map, A a, B b) {
if (map != null) map.put(a, b);
}
static ArrayList asList(A[] a) {
return a == null ? new ArrayList () : new ArrayList (Arrays.asList(a));
}
static ArrayList asList(int[] a) {
ArrayList l = new ArrayList();
for (int i : a) l.add(i);
return l;
}
static ArrayList asList(Iterable s) {
if (s instanceof ArrayList) return (ArrayList) s;
ArrayList l = new ArrayList();
if (s != null)
for (A a : s)
l.add(a);
return l;
}
static ArrayList asList(Enumeration e) {
ArrayList l = new ArrayList();
if (e != null)
while (e.hasMoreElements())
l.add(e.nextElement());
return l;
}
static 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 String quoteCharacter(char c) {
if (c == '\'') return "'\\''";
if (c == '\\') return "'\\\\'";
if (c == '\r') return "'\\r'";
if (c == '\n') return "'\\n'";
if (c == '\t') return "'\\t'";
return "'" + c + "'";
}
public static String join(String glue, Iterable strings) {
if (strings == null) return "";
StringBuilder buf = new StringBuilder();
Iterator i = strings.iterator();
if (i.hasNext()) {
buf.append(i.next());
while (i.hasNext())
buf.append(glue).append(i.next());
}
return buf.toString();
}
public static String join(String glue, String... strings) {
return join(glue, Arrays.asList(strings));
}
static String join(Iterable strings) {
return join("", strings);
}
static String join(Iterable strings, String glue) {
return join(glue, strings);
}
public static String join(String[] strings) {
return join("", strings);
}
static String join(String glue, Pair p) {
return p == null ? "" : str(p.a) + glue + str(p.b);
}
static 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 shortDynamicClassName(Object o) {
if (o instanceof DynamicObject && ((DynamicObject) o).className != null)
return ((DynamicObject) o).className;
return shortClassName(o);
}
static String dropPrefix(String prefix, String s) {
return s == null ? null : s.startsWith(prefix) ? s.substring(l(prefix)) : s;
}
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 boolean isSubtypeOf(Class a, Class b) {
return b.isAssignableFrom(a); // << always hated that method, let's replace it!
}
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 Object() { 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;
}
try {
call(javax(), "_registerDangerousWeakMap", map, init);
} catch (Throwable e) { printException(e);
upgradeJavaXAndRestart();
}
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 T3 triple(A a, B b, C c) {
return new T3(a, b, c);
}
static String getType(Object o) {
return getClassName(o);
}
static String unnull(String s) {
return s == null ? "" : s;
}
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;
}
// replacement for class JavaTok
// maybe incomplete, might want to add floating point numbers
// todo also: extended multi-line strings
static int javaTok_n, javaTok_elements;
static boolean javaTok_opt;
static List javaTok(String s) {
return javaTok(s, null);
}
static List javaTok(String s, List existing) {
++javaTok_n;
int nExisting = javaTok_opt && existing != null ? existing.size() : 0;
ArrayList tok = existing != null ? new ArrayList(nExisting) : new ArrayList();
int l = s.length();
int i = 0, n = 0;
while (i < l) {
int j = i;
char c, d;
// scan for whitespace
while (j < l) {
c = s.charAt(j);
d = j+1 >= l ? '\0' : s.charAt(j+1);
if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
++j;
else if (c == '/' && d == '*') {
do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
j = Math.min(j+2, l);
} else if (c == '/' && d == '/') {
do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
} else
break;
}
if (n < nExisting && javaTok_isCopyable(existing.get(n), s, i, j))
tok.add(existing.get(n));
else
tok.add(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 && javaTok_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 List javaTok(List tok) {
return javaTok(join(tok), tok);
}
static boolean javaTok_isCopyable(String t, String s, int i, int j) {
return t.length() == j-i
&& s.regionMatches(i, t, 0, j-i); // << could be left out, but that's brave
}
static 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 void incAtomicLong(AtomicLong l) {
l.incrementAndGet();
}
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 assertEquals(Object x, A y) {
return assertEquals(null, x, y);
}
static A assertEquals(String msg, Object x, A y) {
if (!(x == null ? y == null : x.equals(y)))
throw fail((msg != null ? msg + ": " : "") + y + " != " + x);
return y;
}
static HashMap getDeclaredFields_cache = new HashMap();
static Field[] getDeclaredFields_cached(Class c) {
Field[] fields;
synchronized(getDeclaredFields_cache) {
fields = getDeclaredFields_cache.get(c);
if (fields == null) {
getDeclaredFields_cache.put(c, fields = c.getDeclaredFields());
for (Field f : fields)
f.setAccessible(true);
}
}
return fields;
}
static String boolArrayToHex(boolean[] a) {
return bytesToHex(boolArrayToBytes(a));
}
static boolean endsWithLetterOrDigit(String s) {
return nempty(s) && isLetterOrDigit(lastCharacter(s));
}
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 Object first(Object list) {
return empty((List) list) ? null : ((List) list).get(0);
}
static A first(List list) {
return empty(list) ? null : list.get(0);
}
static A first(A[] bla) {
return bla == null || bla.length == 0 ? null : bla[0];
}
static A first(Iterable i) {
if (i == null) return null;
Iterator it = i.iterator();
return it.hasNext() ? it.next() : null;
}
static Character first(String s) { return empty(s) ? null : s.charAt(0); }
static A first(Pair p) {
return p == null ? null : p.a;
}
static long getLastTiming() {
ThreadLocal tl = (ThreadLocal) ( getOpt(getMainClass(), "saveTiming_last"));
if (tl == null) return -1;
Long l = tl.get();
return l == null ? -1 : l;
}
static Iterator emptyIterator() {
return Collections.emptyIterator();
}
static String sfu(Object o) { return structureForUser(o); }
static TripleIndex tripleIndex_instance;
static TripleIndex tripleIndex() {
if (tripleIndex_instance == null) {
tripleIndex_instance = addVirtualNodeIndex(new TripleIndex());
tripleIndex_instance.activate();
}
return tripleIndex_instance;
}
static A second(List l) {
return get(l, 1);
}
static A second(A[] bla) {
return bla == null || bla.length <= 1 ? null : bla[1];
}
static B second(Pair p) {
return p == null ? null : p.b;
}
static B second(T3 t) {
return t == null ? null : t.b;
}
public static String bytesToHex(byte[] bytes) {
return bytesToHex(bytes, 0, bytes.length);
}
public static String bytesToHex(byte[] bytes, int ofs, int len) {
StringBuilder stringBuilder = new StringBuilder(len*2);
for (int i = 0; i < len; i++) {
String s = "0" + Integer.toHexString(bytes[ofs+i]);
stringBuilder.append(s.substring(s.length()-2, s.length()));
}
return stringBuilder.toString();
}
static boolean isInteger(String s) {
if (s == null) return false;
int n = l(s);
if (n == 0) return false;
int i = 0;
if (s.charAt(0) == '-')
if (++i >= n) return false;
while (i < n) {
char c = s.charAt(i);
if (c < '0' || c > '9') return false;
++i;
}
return true;
}
static String getStackTrace(Throwable throwable) {
lastException(throwable);
StringWriter writer = new StringWriter();
throwable.printStackTrace(new PrintWriter(writer));
return hideCredentials(writer.toString());
}
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 void quoteToPrintWriter(String s, PrintWriter out) {
if (s == null) { out.print("null"); return; }
out.print('"');
int l = s.length();
for (int i = 0; i < l; i++) {
char c = s.charAt(i);
if (c == '\\' || c == '"') {
out.print('\\'); out.print(c);
} else if (c == '\r')
out.print("\\r");
else if (c == '\n')
out.print("\\n");
else
out.print(c);
}
out.print('"');
}
static Collection ai_withoutInvalidWebs(Collection l) {
if (l == null) return null;
for (Web n : l)
if (ai_isInvalidWeb(n)) {
Iterator it = iterator(l);
Web node;
List x = new ArrayList();
while ((node = it.next()) != n)
x.add(node);
while (it.hasNext()) {
node = it.next();
if (!ai_isInvalidWeb(node))
x.add(node);
}
return x;
}
return l;
}
static List ai_withoutInvalidWebs(List l) {
return (List) ai_withoutInvalidWebs((Collection) l);
}
static String 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 substring(String s, int x) {
return substring(s, x, l(s));
}
static String substring(String s, int x, int y) {
if (s == null) return null;
if (x < 0) x = 0;
if (x > s.length()) return "";
if (y < x) y = x;
if (y > s.length()) y = s.length();
return s.substring(x, y);
}
static void clear(Collection c) {
if (c != null) c.clear();
}
static int parseInt(String s) {
return empty(s) ? 0 : Integer.parseInt(s);
}
static int parseInt(char c) {
return Integer.parseInt(str(c));
}
static A popLast(List l) {
return liftLast(l);
}
static Throwable printStackTrace2(Throwable e) {
// we go to system.out now - system.err is nonsense
print(getStackTrace2(e));
return e;
}
static void printStackTrace2() {
printStackTrace2(new Throwable());
}
static void printStackTrace2(String msg) {
printStackTrace2(new Throwable(msg));
}
/*static void printStackTrace2(S indent, Throwable e) {
if (endsWithLetter(indent)) indent += " ";
printIndent(indent, getStackTrace2(e));
}*/
static WebNode web_setPosition(WebNode node, double x, double y) {
node.x = x;
node.y = y;
return node;
}
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 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 boolean startsWith(String a, String b) {
return a != null && a.startsWith(b);
}
static boolean startsWith(String a, char c) {
return nempty(a) && a.charAt(0) == c;
}
static boolean startsWith(String a, String b, Matches m) {
if (!startsWith(a, b)) return false;
m.m = new String[] {substring(a, l(b))};
return true;
}
static boolean startsWith(List a, List b) {
if (a == null || l(b) > l(a)) return false;
for (int i = 0; i < l(b); i++)
if (neq(a.get(i), b.get(i)))
return false;
return true;
}
static volatile Throwable lastException_lastException;
static Throwable lastException() {
return lastException_lastException;
}
static void lastException(Throwable e) {
lastException_lastException = e;
}
static String javaTok_substringC(String s, int i, int j) {
return s.substring(i, j);
}
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 Iterator iterator(Collection c) {
return c == null ? emptyIterator() : c.iterator();
}
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 File programFile(String name) {
return prepareProgramFile(name);
}
static File programFile(String progID, String name) {
return prepareProgramFile(progID, name);
}
static String getStackTrace2(Throwable throwable) {
return hideCredentials(getStackTrace(throwable) + replacePrefix("java.lang.RuntimeException: ", "FAIL: ",
hideCredentials(str(getInnerException(throwable)))) + "\n");
}
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 Web simpleWeb() {
Web web = new Web();
web.useCLParse = false;
return web;
}
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) { printStackTrace2(__e); }
}
static void upgradeJavaXAndRestart() {
run("#1001639");
restart();
sleep();
}
static A addVirtualNodeIndex(A i) {
setAdd(virtualNodeIndices_list, i);
return i;
}
// 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 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 cmp(Number a, Number b) {
return a == null ? b == null ? 0 : -1 : cmp(a.doubleValue(), b.doubleValue());
}
static int cmp(double a, double b) {
return a < b ? -1 : a == b ? 0 : 1;
}
static int cmp(String a, String b) {
return a == null ? b == null ? 0 : -1 : a.compareTo(b);
}
static int cmp(Object a, Object b) {
if (a == null) return b == null ? 0 : -1;
if (b == null) return 1;
return ((Comparable) a).compareTo(b);
}
static 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;
}
// 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 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 Map emptyMap() {
return new HashMap();
}
static String hideCredentials(URL url) { return url == null ? null : hideCredentials(str(url)); }
static String hideCredentials(String url) {
return url.replaceAll("([&?])_pass=[^&\\s\"]*", "$1_pass=");
}
static List virtualNodeIndices_list = synchroList();
static List virtualNodeIndices() {
return virtualNodeIndices_list;
}
static Map web_matchAllPerms(Web patternWeb, Web inputWeb) {
List patternNodes = web_nodes(patternWeb);
List inputNodes = web_nodes(inputWeb);
if (l(patternNodes) != l(inputNodes))
return null;
//fail("Can't match, differing number of nodes: " + l(patternNodes) + "/" + l(inputNodes));
List> perms = allPermutations(inputNodes);
Best < List < WebNode > > best = new Best();
for (List perm : perms)
best.put(perm, web_matchNodeLists(patternNodes, perm));
List l = best.getIfScoreAbove(0);
return l == null ? null : twoListsToOrderedMap(patternNodes, l);
}
static char lastCharacter(String s) {
return empty(s) ? 0 : s.charAt(l(s)-1);
}
static long parseLong(String s) {
if (s == null) return 0;
return Long.parseLong(dropSuffix("L", s));
}
static long parseLong(Object s) {
return Long.parseLong((String) s);
}
static List websToTripleWebs(Collection l) {
return map("ai_webToTripleWeb",l);
}
static LinkedHashSet litorderedset(A... items) {
LinkedHashSet set = new LinkedHashSet();
for (A a : items) set.add(a);
return set;
}
static String emptySymbol_value;
static String emptySymbol() {
if (emptySymbol_value == null) emptySymbol_value = symbol("");
return emptySymbol_value;
}
static boolean neq(Object a, Object b) {
return !eq(a, b);
}
static boolean ai_isInvalidWeb(Web web) {
return ai_invalidatedWebs().contains(web.globalID);
}
static boolean ai_isInvalidWeb(String id) {
return ai_invalidatedWebs().contains(id);
}
static boolean isLetterOrDigit(char c) {
return Character.isLetterOrDigit(c);
}
static CombinedList combinedList_ll(List l) {
CombinedList cl = new CombinedList();
cl.addList(l);
return cl;
}
static List emptyList() {
return new ArrayList();
//ret Collections.emptyList();
}
static List emptyList(int capacity) {
return new ArrayList(capacity);
}
// Try to match capacity
static List emptyList(Iterable l) {
return l instanceof Collection ? emptyList(((Collection) l).size()) : emptyList();
}
static void addIfNotNull(Collection l, A a) {
if (a != null) l.add(a);
}
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 List ai_triplesMadeByProgramAsTriples(String progID) {
return webs_readTripleFileAsTriples(getProgramFile(progID, "triples.gz"));
}
static String formatWithThousandsSeparator(long l) {
return NumberFormat.getInstance(new Locale("en_US")).format(l);
}
static Pair pair(A a, B b) {
return new Pair(a, b);
}
static Pair pair(A a) {
return new Pair(a, 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;
}
static List concatLists_conservative(List a, List b) {
if (empty(a)) return b;
if (empty(b)) return a;
return concatLists(a, b);
}
static List web_nodes(Web web) {
return web_nonRelationNodes(web);
}
static String replacePrefix(String prefix, String replacement, String s) {
if (!startsWith(s, prefix)) return s;
return replacement + substring(s, l(prefix));
}
static List> allPermutations(List l) {
List < List < A > > out = new ArrayList();
allPermutations_impl(cloneList(l), l(l), out);
return out;
}
static void allPermutations_impl(List l, int n, List> out) {
if (n <= 1) {
ping();
out.add(cloneList(l));
} else for(int i = 0; i < n; i++) {
allPermutations_impl(l, n-1, out);
swapElements(l, n % 2 == 0 ? i : 0, n-1);
}
}
static MultiMap cachedNodeIndex() {
return cachedNodeIndex2();
}
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 TripleWeb ai_webToTripleWeb(Web web) {
T3 t = ai_webToTriple(web);
if (t == null) return null;
TripleWeb w = new TripleWeb();
w.a = t.a;
w.b = t.b;
w.c = t.c;
w.globalID(web.globalIDObj());
w.created = web.created;
w.source = web.source;
w.flags = web.unverified ? w.UNVERIFIED : 0;
return w;
}
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 Throwable getInnerException(Throwable e) {
while (e.getCause() != null)
e = e.getCause();
return e;
}
static Web web_setSourceIfEmpty(Web web, String src) {
if (web == null) return null;
if (empty(web.source)) web.source = src;
return web;
}
static File prepareProgramFile(String name) {
return mkdirsForFile(getProgramFile(name));
}
static File prepareProgramFile(String progID, String name) {
return mkdirsForFile(getProgramFile(progID, name));
}
static Class run(String progID, String... args) {
Class main = hotwire(progID);
callMain(main, args);
return main;
}
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)));
}
List triples = new ArrayList();
while (it.hasNext()) {
String s = it.next();
try {
addIfNotNull(triples, webs_readTripleFileAsTriples_line(s, names));
} catch (Throwable __e) { printStackTrace2(__e); }
}
return triples;
}
static TripleWeb webs_readTripleFileAsTriples_line(String s, List names) {
List l = javaTokC(s);
if (l(l) == 8) {
TripleWeb t = new TripleWeb();
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.source = intern(unquote(l.get(5)));
t.flags = eq(l.get(6), "v") ? 0 : TripleWeb.UNVERIFIED;
t.created = parseLong(l.get(7));
return t;
}
return null;
}
// 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 _lock_143 = ai_invalidatedWebs_cache.lock; lock(_lock_143); 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(_lock_143); } }
static void ai_invalidatedWebs_onRemovedWeb(Web web) {
if (web.unverified) return;
Lock _lock_144 = ai_invalidatedWebs_cache.lock; lock(_lock_144); 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(_lock_144); } }
static boolean setAdd(Collection c, A a) {
if (c == null || c.contains(a)) return false;
c.add(a);
return true;
}
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 ThreadLocal ai_currentSubSpace = new ThreadLocal();
static AI_SubSpace ai_currentSubSpace() {
return ai_currentSubSpace.get();
}
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) { printStackTrace2(__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 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 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 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(F1 f, Iterable l) {
List x = emptyList(l);
if (l != null) for (Object o : l)
x.add(callF(f, o));
return x;
}
static List map(Object f, Object[] l) { return map(f, asList(l)); }
static List map(Object[] l, Object f) { return map(f, l); }
static List map(Object f, Map map) {
return map(map, f);
}
// 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 int ai_shortenForIndex_maxLength = 100;
static String ai_shortenForIndex(String text) {
return shorten(text, ai_shortenForIndex_maxLength);
}
static void restart() {
Object j = getJavaX();
call(j, "cleanRestart", get(j, "fullArgs"));
}
static void mapPut(Map map, A key, B value) {
if (map != null && key != null && value != null) map.put(key, value);
}
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 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 Web web_unstructure(String s) {
//ret ((Web) unstructure(s)).afterLoad();
return (Web) unstructure(s);
}
static String dropSuffix(String suffix, String s) {
return s.endsWith(suffix) ? s.substring(0, l(s)-l(suffix)) : s;
}
static List web_search_dollarX(Web searchWeb, Web web) {
return webs_search_dollarX(searchWeb, ll(web));
}
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 Object unstructure(String text) {
return unstructure(text, false);
}
static Object unstructure(String text, final boolean allDynamic) {
return unstructure(text, allDynamic, null);
}
static int structure_internStringsLongerThan = 50;
static int unstructure_tokrefs; // stats
abstract static class unstructure_Receiver {
abstract void set(Object o);
}
// classFinder: func(name) -> class (optional)
static Object unstructure(String text, boolean allDynamic,
Object classFinder) {
if (text == null) return null;
return unstructure_tok(javaTokC_noMLS_iterator(text), allDynamic, classFinder);
}
static Object unstructure_reader(BufferedReader reader) {
return unstructure_tok(javaTokC_noMLS_onReader(reader), false, null);
}
static Object unstructure_tok(final Producer tok, final boolean allDynamic, final Object classFinder) {
final boolean debug = unstructure_debug;
final class X {
int i = -1;
HashMap refs = new HashMap();
HashMap tokrefs = new HashMap();
HashSet concepts = new HashSet();
HashMap classesMap = new HashMap();
List stack = new ArrayList();
String curT;
// look at current token
String t() {
return curT;
}
// get current token, move to next
String tpp() {
String t = curT;
consume();
return t;
}
void parse(final unstructure_Receiver out) {
String t = t();
int refID = 0;
if (structure_isMarker(t, 0, l(t))) {
refID = parseInt(t.substring(1));
consume();
}
final int _refID = refID;
// if (debug) print("parse: " + quote(t));
final int tokIndex = i;
parse_inner(refID, tokIndex, new unstructure_Receiver() {
void set(Object o) {
if (_refID != 0)
refs.put(_refID, o);
if (o != null)
tokrefs.put(tokIndex, o);
out.set(o);
}
});
}
void parse_inner(int refID, int tokIndex, final unstructure_Receiver out) {
String t = t();
// if (debug) print("parse_inner: " + quote(t));
Class c = classesMap.get(t);
if (c == null) {
if (t.startsWith("\"")) {
String s = internIfLongerThan(unquote(tpp()), structure_internStringsLongerThan);
out.set(s); return;
}
if (t.startsWith("'")) {
out.set(unquoteCharacter(tpp())); return;
}
if (t.equals("bigint")) {
out.set(parseBigInt()); return;
}
if (t.equals("d")) {
out.set(parseDouble()); return;
}
if (t.equals("fl")) {
out.set(parseFloat()); return;
}
if (t.equals("false") || t.equals("f")) {
consume(); out.set(false); return;
}
if (t.equals("true") || t.equals("t")) {
consume(); out.set(true); return;
}
if (t.equals("-")) {
consume();
t = tpp();
out.set(isLongConstant(t) ? (Object) (-parseLong(t)) : (Object) (-parseInt(t))); return;
}
if (isInteger(t) || isLongConstant(t)) {
consume();
//if (debug) print("isLongConstant " + quote(t) + " => " + isLongConstant(t));
if (isLongConstant(t)) {
out.set(parseLong(t)); return;
}
long l = parseLong(t);
boolean isInt = l == (int) l;
if (debug)
print("l=" + l + ", isInt: " + isInt);
out.set(isInt ? (Object) new Integer((int) l) : (Object) new Long(l)); return;
}
if (t.equals("File")) {
consume();
File f = new File(unquote(tpp()));
out.set(f); return;
}
if (t.startsWith("r") && isInteger(t.substring(1))) {
consume();
int ref = Integer.parseInt(t.substring(1));
Object o = refs.get(ref);
if (o == null)
print("Warning: unsatisfied back reference " + ref);
out.set(o); return;
}
if (t.startsWith("t") && isInteger(t.substring(1))) {
consume();
int ref = Integer.parseInt(t.substring(1));
Object o = tokrefs.get(ref);
if (o == null)
print("Warning: unsatisfied token reference " + ref);
out.set(o); return;
}
if (t.equals("hashset")) {
parseHashSet(out); return;
}
if (t.equals("treeset")) {
parseTreeSet(out); return;
}
if (eqOneOf(t, "hashmap", "hm")) {
consume();
parseMap(new HashMap(), out);
return;
}
if (t.equals("lhm")) {
consume();
parseMap(new LinkedHashMap(), out);
return;
}
if (t.equals("{")) {
parseMap(out); return;
}
if (t.equals("[")) {
parseList(out); return;
}
if (t.equals("bitset")) {
parseBitSet(out); return;
}
if (t.equals("array") || t.equals("intarray")) {
parseArray(out); return;
}
if (t.equals("ba")) {
consume();
String hex = unquote(tpp());
out.set(hexToBytes(hex)); return;
}
if (t.equals("boolarray")) {
consume();
int n = parseInt(tpp());
String hex = unquote(tpp());
out.set(boolArrayFromBytes(hexToBytes(hex), n)); return;
}
if (t.equals("class")) {
out.set(parseClass()); return;
}
if (t.equals("l")) {
parseLisp(out); return;
}
if (t.equals("null")) {
consume(); out.set(null); return;
}
if (eq(t, "c")) {
consume("c");
t = t();
assertTrue(isJavaIdentifier(t));
concepts.add(t);
}
}
if (eq(t, "j")) {
consume("j");
out.set(parseJava()); return;
}
if (c == null && !isJavaIdentifier(t))
throw new RuntimeException("Unknown token " + (i+1) + ": " + t);
// any other class name
if (c == null) {
// First, find class
if (allDynamic) c = null;
else c = classFinder != null ? (Class) callF(classFinder, t) : findClass(t);
if (c != null)
classesMap.put(t, c);
}
// Check if it has an outer reference
consume();
boolean hasBracket = eq(t(), "(");
if (hasBracket) consume();
boolean hasOuter = hasBracket && eq(t(), "this$1");
DynamicObject dO = null;
Object o = null;
if (c != null) {
o = hasOuter ? nuStubInnerObject(c) : nuEmptyObject(c);
if (o instanceof DynamicObject) dO = (DynamicObject) o;
} else {
if (concepts.contains(t) && (c = findClass("Concept")) != null)
o = dO = (DynamicObject) nuEmptyObject(c);
else
dO = new DynamicObject();
dO.className = t;
if (debug) print("Made dynamic object " + t + " " + shortClassName(dO));
}
// Save in references list early because contents of object
// might link back to main object
if (refID != 0)
refs.put(refID, o != null ? o : dO);
tokrefs.put(tokIndex, o != null ? o : dO);
// NOW parse the fields!
final LinkedHashMap fields = new LinkedHashMap(); // preserve order
final Object _o = o;
final DynamicObject _dO = dO;
if (hasBracket) {
stack.add(new Runnable() { public void run() { try {
if (eq(t(), ")")) {
consume(")");
objRead(_o, _dO, fields);
out.set(_o != null ? _o : _dO);
} else {
final String key = unquote(tpp());
consume("=");
stack.add(this);
parse(new unstructure_Receiver() {
void set(Object value) {
fields.put(key, value);
if (eq(t(), ",")) consume();
}
});
}
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (eq(t(), \")\")) {\r\n consume(\")\");\r\n objRead(_o, _dO, ..."; }});
} else {
objRead(o, dO, fields);
out.set(o != null ? o : dO);
}
}
void objRead(Object o, DynamicObject dO, Map fields) {
if (o != null)
if (dO != null) {
if (debug)
printStructure("setOptAllDyn", fields);
setOptAllDyn(dO, fields);
} else {
setOptAll_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) {
parseList(new unstructure_Receiver() {
void set(Object o) {
set.addAll((List) o);
out.set(set);
}
});
}
void parseLisp(final unstructure_Receiver out) {
consume("l");
consume("(");
final ArrayList list = new ArrayList();
stack.add(new Runnable() { public void run() { try {
if (eq(t(), ")")) {
consume(")");
out.set(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 unstructure_Receiver out) {
consume("[");
final ArrayList list = new ArrayList();
stack.add(new Runnable() { public void run() { try {
if (eq(t(), "]")) {
consume("]");
out.set(list);
} else {
stack.add(this);
parse(new unstructure_Receiver() {
void set(Object o) {
//if (debug) print("List element type: " + getClassName(o));
list.add(o);
if (eq(t(), ",")) consume();
}
});
}
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (eq(t(), \"]\")) {\r\n consume(\"]\");\r\n out.set(list);\r\n ..."; }});
}
void parseArray(final unstructure_Receiver out) {
final String type = tpp();
consume("{");
final List list = new ArrayList();
stack.add(new Runnable() { public void run() { try {
if (eq(t(), "}")) {
consume("}");
out.set(type.equals("intarray") ? toIntArray(list) : list.toArray());
} else {
stack.add(this);
parse(new unstructure_Receiver() {
void set(Object o) {
list.add(o);
if (eq(t(), ",")) consume();
}
});
}
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (eq(t(), \"}\")) {\r\n consume(\"}\");\r\n out.set(type.equals(\"..."; }});
}
Object parseClass() {
consume("class");
consume("(");
String name = unquote(tpp());
consume(")");
name = dropPrefix("main$", name);
Class c = allDynamic ? null : classFinder != null ? (Class) callF(classFinder, name) : findClass(name);
if (c != null) return c;
DynamicObject dO = new DynamicObject();
dO.className = "java.lang.Class";
dO.fieldValues.put("name", name);
return dO;
}
Object parseBigInt() {
consume("bigint");
consume("(");
String val = tpp();
if (eq(val, "-"))
val = "-" + tpp();
consume(")");
return new BigInteger(val);
}
Object parseDouble() {
consume("d");
consume("(");
String val = unquote(tpp());
consume(")");
return Double.parseDouble(val);
}
Object parseFloat() {
consume("fl");
String val;
if (eq(t(), "(")) {
consume("(");
val = unquote(tpp());
consume(")");
} else {
val = unquote(tpp());
}
return Float.parseFloat(val);
}
void parseHashSet(unstructure_Receiver out) {
consume("hashset");
parseSet(new HashSet(), out);
}
void parseTreeSet(unstructure_Receiver out) {
consume("treeset");
parseSet(new TreeSet(), out);
}
void parseMap(unstructure_Receiver out) {
parseMap(new TreeMap(), out);
}
Object parseJava() {
String j = unquote(tpp());
Matches m = new Matches();
if (jmatch("java.awt.Color[r=*,g=*,b=*]", j, m))
return nuObject("java.awt.Color", parseInt(m.unq(0)), parseInt(m.unq(1)), parseInt(m.unq(2)));
else {
warn("Unknown Java object: " + j);
return null;
}
}
void parseMap(final Map map, final unstructure_Receiver out) {
consume("{");
stack.add(new Runnable() {
boolean v;
Object key;
public void run() {
if (v) {
v = false;
stack.add(this);
consume("=");
parse(new unstructure_Receiver() {
void set(Object value) {
map.put(key, value);
if (debug)
print("parseMap: Got value " + getClassName(value) + ", next token: " + quote(t()));
if (eq(t(), ",")) consume();
}
});
} else {
if (eq(t(), "}")) {
consume("}");
out.set(map);
} else {
v = true;
stack.add(this);
parse(new unstructure_Receiver() {
void set(Object o) {
key = o;
}
});
}
} // if v else
} // run()
});
}
/*void parseSub(unstructure_Receiver out) {
int n = l(stack);
parse(out);
while (l(stack) > n)
stack
}*/
void consume() { curT = tok.next(); ++i; }
void consume(String s) {
if (!eq(t(), s)) {
/*S prevToken = i-1 >= 0 ? tok.get(i-1) : "";
S nextTokens = join(tok.subList(i, Math.min(i+2, tok.size())));
fail(quote(s) + " expected: " + prevToken + " " + nextTokens + " (" + i + "/" + tok.size() + ")");*/
throw fail(quote(s) + " expected, got " + quote(t()));
}
consume();
}
void parse_x(unstructure_Receiver out) {
consume(); // get first token
parse(out);
while (nempty(stack))
popLast(stack).run();
}
}
Boolean b = DynamicObject_loading.get();
DynamicObject_loading.set(true);
try {
final Var v = new Var();
X x = new X();
x.parse_x(new unstructure_Receiver() {
void set(Object o) { v.set(o); }
});
unstructure_tokrefs = x.tokrefs.size();
return v.get();
} finally {
DynamicObject_loading.set(b);
}
}
static boolean unstructure_debug;
// requires ugly casting when used (O -> A)
static Object iteratorFromFunction_endMarker = new Object();
// f: func -> A (stream ends when f returns null)
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 String intern(String s) {
return fastIntern(s);
}
static GlobalID asGlobalID(String id) {
return id == null ? null : new GlobalID(id);
}
static A callMain(A c, String... args) {
callOpt(c, "main", new Object[] {args});
return c;
}
static void callMain() {
callMain(mc());
}
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 _lock_179 = cachedNodeIndex2_cache.lock; lock(_lock_179); try {
//print("Incremental update");
ai_addWebToIndex(cachedNodeIndex2_cache.get(), web);
cachedNodeIndex2_cache.changeCount++;
return false; // don't trigger full update
} finally { unlock(_lock_179); } }
static void cachedNodeIndex2_onWebRemoved(Web web) {
if (!cachedNodeIndex_autoMake) return;
Lock _lock_180 = cachedNodeIndex2_cache.lock; lock(_lock_180); try {
print("Incremental removal");
ai_removeWebFromIndex(cachedNodeIndex2_cache.get(), web);
cachedNodeIndex2_cache.changeCount++;
} finally { unlock(_lock_180); } }
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 void unlock(Lock lock, String msg) {
print("Unlocking: " + msg);
lock.unlock();
}
static void unlock(Lock lock) {
lock.unlock();
}
static IterableIterator linesFromFile(File f) { try {
if (!f.exists()) return emptyIterableIterator();
if (ewic(f.getName(), ".gz"))
return linesFromReader(utf8bufferedReader(new GZIPInputStream(new FileInputStream(f))));
return linesFromReader(utf8bufferedReader(f));
} catch (Exception __e) { throw rethrow(__e); } }
static List web_nodesAndRelations(Web web) {
return web.nodes;
}
static boolean containsAllIC_lisp(Collection a, Collection b) {
for (Lisp o : b)
if (!containsLispIC(a, o))
return false;
return true;
}
static Collection web_relationObjects(Web web) {
List l = new ArrayList();
for (WebNode n : web.nodes)
if (n instanceof WebRelation)
l.add(((WebRelation) n));
return l;
}
public static File mkdirsForFile(File file) {
File dir = file.getParentFile();
if (dir != null) // is null if file is in current dir
dir.mkdirs();
return file;
}
public static String mkdirsForFile(String path) {
mkdirsForFile(new File(path));
return path;
}
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 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 List web_nonRelationNodes(Web web) {
List l = new ArrayList();
for (WebNode n : web.nodes)
if (!(n instanceof WebRelation))
l.add(n);
return l;
}
static int lastIndexOf(String a, String b) {
return a == null || b == null ? -1 : a.lastIndexOf(b);
}
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_match(String pat, WebNode node, Matches m) {
for (String s : web_texts(node))
if (match(pat, s, m)) return true;
return false;
}
static void lock(Lock lock) { try {
ping();
lock.lockInterruptibly();
ping();
} catch (Exception __e) { throw rethrow(__e); } }
static void lock(Lock lock, String msg) {
print("Locking: " + msg);
lock(lock);
}
static void lock(Lock lock, String msg, long timeout) {
print("Locking: " + msg);
lockOrFail(lock, timeout);
}
static ReentrantLock lock() {
return fairLock();
}
static void web_setLabels_forced(WebNode node, List labels) {
web_deindex(node);
node.labels = cloneList(labels);
web_index(node);
}
static boolean isGlobalID(String s) {
return possibleGlobalID(s);
}
static ArrayList cloneList(Collection l) {
if (l == null) return new ArrayList();
// TODO: is this correct when l is a keySet?
synchronized(l) {
return new ArrayList (l);
}
}
static IterableIterator scanLog_iterator(String progID, String fileName) {
return scanLog_iterator(getProgramFile(progID, fileName));
}
static IterableIterator scanLog_iterator(String fileName) {
return scanLog_iterator(getProgramFile(fileName));
}
static IterableIterator scanLog_iterator(File file) {
// TODO: opens all at once
final Iterator it = chainIterators(map("linesFromFile",
concatLists(earlierPartsOfLogFile(file), ll(file))));
return iteratorFromFunction(new F0() { String get() { try {
while (it.hasNext()) {
String s = it.next();
if (isProperlyQuoted(s))
return unquote(s);
}
return null;
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "while (it.hasNext()) {\r\n S s = it.next();\r\n if (isProperlyQuoted(s)..."; }});
}
static 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 ai_onNewOrRemovedWeb(Object onNewWeb, Object onRemovedWeb) {
ai_onNewWeb(onNewWeb);
if (onRemovedWeb != null)
onTransientWebRemoved_do(onRemovedWeb);
}
static boolean hasMultipleCores() {
return isMultiCore();
}
static List trimToSize(List l) {
if (l instanceof ArrayList)
((ArrayList) l).trimToSize();
else if (l instanceof SynchronizedArrayList)
((SynchronizedArrayList) l).trimToSize();
return l;
}
static List webs_readTripleFileAsTriples_parallel2(File f) { try {
if (!f.exists()) return ll();
long _startTime_297 = 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_297);
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) { printStackTrace2(__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 File getProgramDir() {
return programDir();
}
static File getProgramDir(String snippetID) {
return programDir(snippetID);
}
static String n2(long l, String singular, String plural) {
return n_fancy2(l, singular, plural);
}
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(Object[] a, String singular, String plural) {
return n_fancy2(a, singular, plural);
}
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 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 List concatLists(Collection ... lists) {
List l = new ArrayList();
for (Collection list : lists)
if (list != null)
l.addAll(list);
return l;
}
static List concatLists(Collection> lists) {
List l = new ArrayList();
for (List list : lists)
if (list != null)
l.addAll(list);
return l;
}
// 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 Set keys(Map map) {
return map == null ? new HashSet() : map.keySet();
}
static Set keys(Object map) {
return keys((Map) map);
}
static Set keys(MultiMap mm) {
return mm.keySet();
}
static 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 Object sleepQuietly_monitor = new Object();
static void sleepQuietly() { try {
assertFalse(isAWTThread());
synchronized(sleepQuietly_monitor) { sleepQuietly_monitor.wait(); }
} catch (Exception __e) { throw rethrow(__e); } }
static List web_labels(WebNode n) {
return n == null ? null : n.labels();
}
static Class> hotwire(String src) {
assertFalse(_inCore());
Class j = getJavaX();
if (isAndroid()) {
synchronized(j) { // hopefully this goes well...
List libraries = new ArrayList();
File srcDir = (File) call(j, "transpileMain", src, libraries);
if (srcDir == null)
throw fail("transpileMain returned null (src=" + quote(src) + ")");
Object androidContext = get(j, "androidContext");
return (Class) call(j, "loadx2android", srcDir, src);
}
} else {
// ret hotwire_overInternalBot(src);
Class c = (Class) ( call(j, "hotwire", src));
hotwire_copyOver(c);
return c;
}
}
static boolean web_isAnythingVariable(WebNode n) {
return endsWithIC(web_text(n), " [Anything]");
}
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 String web_text(WebNode node) {
return node == null ? null : node.text();
}
static void ai_onNewWeb(Object onNewWeb) {
if (onNewWeb == null) return;
setAdd(postSoftwareMadeWeb_onNewWeb, onNewWeb);
onTransientWebAdded_do(onNewWeb);
}
static MultiMap ai_emptyWebIndex() {
MultiMap index = caseInsensitiveSyncListMultiMap();
makeMultiMapsInnerNavigableMapSynchronized(index);
return index;
}
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 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 Iterator chainIterators(Collection> l) {
final List> _l = new LinkedList(l);
return iteratorFromFunction(new Object() { Object get() { try {
while (nempty(_l)) {
if (first(_l).hasNext())
return first(_l).next();
_l.remove(0);
}
return null;
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "while (nempty(_l)) {\r\n if (first(_l).hasNext())\r\n ret first(_l).n..."; }});
}
static byte[] hexToBytes(String s) {
int n = l(s) / 2;
byte[] bytes = new byte[n];
for (int i = 0; i < n; i++) {
String hex = substring(s, i*2, i*2+2);
try {
bytes[i] = (byte) parseHexByte(hex);
} catch (Throwable _e) {
throw fail("Bad hex byte: " + quote(hex) + " at " + i*2 + "/" + l(s));
}
}
return bytes;
}
static boolean isMultiCore() {
return numberOfCores() > 1;
}
static FileStatus conceptsFileStatus(String progID) {
return fileStatus(conceptsFile(progID));
}
static boolean isProperlyQuoted(String s) {
return s.length() >= 2
&& s.startsWith("\"")
&& s.endsWith("\"")
&& (!s.endsWith("\\\"") || s.endsWith("\\\\\""));
}
static HashMap nuEmptyObject_cache = new HashMap();
static A nuEmptyObject(Class c) { try {
Constructor ctr;
synchronized(nuEmptyObject_cache) {
ctr = nuEmptyObject_cache.get(c);
if (ctr == null) {
nuEmptyObject_cache.put(c, ctr = nuEmptyObject_findConstructor(c));
ctr.setAccessible(true);
}
}
return (A) ctr.newInstance();
} catch (Exception __e) { throw rethrow(__e); } }
static Constructor nuEmptyObject_findConstructor(Class c) {
for (Constructor m : c.getDeclaredConstructors())
if (m.getParameterTypes().length == 0)
return m;
throw fail("No default constructor declared in " + c.getName());
}
static 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 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 boolean setOptAllDyn_debug;
static void setOptAllDyn(DynamicObject o, Map fields) {
if (fields == null) return;
for (String field : keys(fields)) {
Object val = fields.get(field);
boolean has = hasField(o, field);
if (has)
setOpt(o, field, val);
else {
o.fieldValues.put(intern(field), val);
if (setOptAllDyn_debug) print("setOptAllDyn added dyn " + field + " to " + o + " [value: " + val + ", fieldValues = " + systemHashCode(o.fieldValues) + ", " + struct(keys(o.fieldValues)) + "]");
}
}
}
static T3 t3(A a, B b, C c) {
return new T3(a, b, c);
}
static List web_texts(WebNode node) {
return node == null ? new ArrayList() : node.texts();
}
static List web_texts(Collection extends WebNode> l) {
List texts = new ArrayList(l(l));
if (l != null) for (WebNode n : l)
texts.add(web_text(n));
return texts;
}
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 IterableIterator emptyIterableIterator_instance = new IterableIterator() {
public Object next() { throw fail(); }
public boolean hasNext() { return false; }
};
static IterableIterator emptyIterableIterator() {
return emptyIterableIterator_instance;
}
static List webs_search_dollarX(Web searchWeb, Collection webs) {
List < Map < WebNode , WebNode > > mappings = new ArrayList();
for (Web web : webs)
addIfNotNull(mappings, web_matchAllPerms_x(searchWeb, web, "nodeMatch_dollarVars"));
return getFromAll(mappings, web_findNode(searchWeb, "$X"));
}
static List subList(List l, int startIndex) {
return subList(l, startIndex, l(l));
}
static List subList(List l, int startIndex, int endIndex) {
startIndex = max(0, min(l(l), startIndex));
endIndex = max(0, min(l(l), endIndex));
if (startIndex > endIndex) return litlist();
return l.subList(startIndex, endIndex);
}
static volatile boolean licensed_yes = true;
static boolean licensed() {
ping();
return licensed_yes;
}
static void licensed_off() {
licensed_yes = false;
}
static List earlierPartsOfLogFile(File file) {
String name = file.getName() + ".part";
try {
Matches m = new Matches();
TreeMap map = new TreeMap();
for (File p : listFiles(file.getParent())) try {
String n = p.getName();
if (startsWith(n, name, m))
map.put(parseFirstInt(m.rest()), p);
} catch (Throwable __e) { printStackTrace2(__e); }
return valuesList(map);
} catch (Throwable e) { printException(e);
return ll();
}
}
static 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 MultiMap dwlw_fullNodeIndex_ci() {
print("Getting webs.");
long _startTime_256 = sysNow();
List webs = ai_filterBadWebs(allWebs());
_startTime_256 = sysNow()-_startTime_256; saveTiming(_startTime_256);
print("Got webs in " + lastTiming() + " ms. Making index of " + nWebs(webs) + ".");
long _startTime_257 = sysNow();
MultiMap index = ai_indexWebs(webs);
done2_always("Index made.", _startTime_257);
return index;
}
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 ThreadLocal postSoftwareMadeWeb_disabled = new ThreadLocal();
static BetterThreadLocal postSoftwareMadeWeb_websMadeInThread = new BetterThreadLocal();
static ThreadLocal postSoftwareMadeWeb_maker = new ThreadLocal();
static ThreadLocal postSoftwareMadeWeb_programID = new ThreadLocal();
// L
// function can also return false to stop triggerWebsChanged
static List postSoftwareMadeWeb_onNewWeb = synchroList();
// L
static List postSoftwareMadeWeb_onDisabledPost = synchroList();
static void postSoftwareMadeWeb(Web web, Object... params) {
Lock _lock_262 = aiLock(); lock(_lock_262); 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(_lock_262); } }
static String postSoftwareMadeWeb_programID() {
return or2(postSoftwareMadeWeb_programID.get(), programID());
}
static File postSoftwareMadeWeb_file() {
return getProgramFile(postSoftwareMadeWeb_programID(), "webs-made.txt");
}
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 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 File programDir_mine; // set this to relocate program's data
static File programDir() {
return programDir(getProgramID());
}
static File programDir(String snippetID) {
boolean me = sameSnippetID(snippetID, programID());
if (programDir_mine != null && me)
return programDir_mine;
File dir = new File(javaxDataDir(), formatSnippetID(snippetID));
if (me) {
String c = caseID();
if (nempty(c)) dir = newFile(dir, c);
}
return dir;
}
static File programDir(String snippetID, String subPath) {
return new File(programDir(snippetID), subPath);
}
static Producer javaTokC_noMLS_iterator(final String s) {
return new Producer() {
final int l = s.length();
int i = 0;
public String next() {
if (i >= l) return null;
int j = i;
char c, d;
// scan for whitespace
while (j < l) {
c = s.charAt(j);
d = j+1 >= l ? '\0' : s.charAt(j+1);
if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
++j;
else if (c == '/' && d == '*') {
do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
j = Math.min(j+2, l);
} else if (c == '/' && d == '/') {
do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
} else
break;
}
i = j;
if (i >= l) return null;
c = s.charAt(i);
d = i+1 >= l ? '\0' : s.charAt(i+1);
// scan for non-whitespace
if (c == '\'' || c == '"') {
char opener = c;
++j;
while (j < l) {
if (s.charAt(j) == opener || s.charAt(j) == '\n') { // end at \n to not propagate unclosed string literal errors
++j;
break;
} else if (s.charAt(j) == '\\' && j+1 < l)
j += 2;
else
++j;
}
} else if (Character.isJavaIdentifierStart(c))
do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't"
else if (Character.isDigit(c)) {
do ++j; while (j < l && Character.isDigit(s.charAt(j)));
if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
} else
++j;
String t = quickSubstring(s, i, j);
i = j;
return t;
}
};
}
static boolean warn_on = true;
static void warn(String s) {
if (warn_on)
print("Warning: " + s);
}
static void warn(String s, List warnings) {
warn(s);
if (warnings != null)
warnings.add(s);
}
static 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 ReentrantLock fairLock() {
return new ReentrantLock(true);
}
static IterableIterator linesFromReader(Reader r) {
final BufferedReader br = bufferedReader(r);
return iteratorFromFunction_f0(new F0() { String get() { try { return readLineFromReaderWithClose(br) ; } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "readLineFromReaderWithClose(br)"; }});
}
static boolean possibleGlobalID(String s) {
return l(s) == 16 && allLowerCaseCharacters(s);
}
static boolean eqic(String a, String b) {
if ((a == null) != (b == null)) return false;
if (a == null) return true;
return a.equalsIgnoreCase(b);
}
static Lisp lisp_intern(Lisp l) {
if (l == null) return null;
return lisp(intern(l.head), map_lisp_intern(l.args));
}
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 Producer javaTokC_noMLS_onReader(final BufferedReader r) {
final class X implements Producer {
StringBuilder buf = new StringBuilder(); // stores from "i"
char c, d, e = 'x'; // just not '\0'
X() {
// fill c, d and e
nc();
nc();
nc();
}
// get next character(s) into c, d and e
void nc() { try {
c = d;
d = e;
if (e == '\0') return;
int i = r.read();
e = i < 0 ? '\0' : (char) i;
} catch (Exception __e) { throw rethrow(__e); } }
void ncSave() {
if (c != '\0') {
buf.append(c);
nc();
}
}
public String next() {
// scan for whitespace
while (c != '\0') {
if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
nc();
else if (c == '/' && d == '*') {
do nc(); while (c != '\0' && !(c == '*' && d == '/'));
nc(); nc();
} else if (c == '/' && d == '/') {
do nc(); while (c != '\0' && "\r\n".indexOf(c) < 0);
} else
break;
}
if (c == '\0') return null;
// scan for non-whitespace
if (c == '\'' || c == '"') {
char opener = c;
ncSave();
while (c != '\0') {
if (c == opener || c == '\n') { // end at \n to not propagate unclosed string literal errors
ncSave();
break;
} else if (c == '\\') {
ncSave();
ncSave();
} else
ncSave();
}
} else if (Character.isJavaIdentifierStart(c))
do ncSave(); while (Character.isJavaIdentifierPart(c) || c == '\''); // for stuff like "don't"
else if (Character.isDigit(c)) {
do ncSave(); while (Character.isDigit(c));
if (c == 'L') ncSave(); // Long constants like 1L
} else
ncSave();
String t = buf.toString();
buf.setLength(0);
return t;
}
}
return new X();
}
static A nuStubInnerObject(Class c) { try {
Class outerType = getOuterClass(c);
Constructor m = c.getDeclaredConstructor(outerType);
m.setAccessible(true);
return (A) m.newInstance(new Object[] {null});
} catch (Exception __e) { throw rethrow(__e); } }
static 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 circlesEditorDBID() {
return "#1007609";
}
static void ai_removeWebFromIndex(MultiMap