Warning : session_start(): open(/var/lib/php/sessions/sess_7qk01on6jsv8kt8vdc4vsbngs8, 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.text.SimpleDateFormat;
import java.awt.datatransfer.UnsupportedFlavorException;
class main {
public static void main(final String[] args) throws Exception {
ai_alternativeSmartBot(); // load triples
ai_compactLiveDB(false); // Phase 1 only
}
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 void ai_compactLiveDB(boolean withPhase2) {
ai_compactLiveDB_Class c = new ai_compactLiveDB_Class();
c.phase1();
if (!withPhase2) return;
print("PHASE 2 in 5 SECONDS");
sleepSeconds(5);
c.phase2();
}
static class ai_compactLiveDB_Class {
File mainLog, fTriples, file, file2;
void phase1() { try {
// First, rotate webs-made.txt
mainLog = programFile("#1010745", "webs-made.txt");
rotateLogFile(mainLog);
File tripelizedDir = programDir("#1010745", "tripelized");
fTriples = programFile("#1010745", "triples.gz");
// Save triples
file = new File(tripelizedDir, "triples.new.gz");
OutputStream out = newGZIPOutputStream(file);
int n = ai_writeTripleFile(ai_allTriples(), out);
out.close();
// Save non-triples (don't have any)
file2 = new File(tripelizedDir, "webs-made.new.gz");
deleteFile(file2);
print("Stored " + nTriple(n) + " in " + f2s(file) + ", nothing in " + f2s(file2));
} catch (Exception __e) { throw rethrow(__e); } }
void phase2() {
print("PHASE 2.");
// Move data to backups
File fBackups = programFile("#1010745", "backups");
for (File f : earlierPartsOfLogFile(mainLog)) {
int n = 1;
File p;
while ((p = newFile(fBackups, dropSuffix(".gz", f.getName()) + ".part" + n + ".gz")).exists()) ++n;
renameFile_assertTrue(f, p);
}
// Backup triples.gz
int n = 1;
File p;
while ((p = newFile(fBackups, dropSuffix(".gz", fTriples.getName()) + ".part" + n + ".gz")).exists()) ++n;
renameFile_assertTrue(fTriples, p);
// Rename main files
renameFile_assertTrue(file, fTriples);
renameFile_assertTrue(file2, programFile("#1010745", "webs-made.txt.part1.gz"));
print("DONE.");
}
}
static List ll(A... a) {
ArrayList l = new ArrayList(a.length);
for (A x : a) l.add(x);
return l;
}
static boolean deleteFile(File file) {
return file != null && file.delete();
}
static RuntimeException rethrow(Throwable e) {
throw asRuntimeException(e);
}
static void sleepSeconds(double s) {
if (s > 0) sleep(round(s*1000));
}
static String f2s(File f) {
return f == null ? null : f.getAbsolutePath();
}
static void localSoftwareMadeWebs_programsToRead_set(List l) {
localSoftwareMadeWebs_programsToRead_list = l;
}
static String dropSuffix(String suffix, String s) {
return s.endsWith(suffix) ? s.substring(0, l(s)-l(suffix)) : s;
}
static Collection ai_allTriples() {
return tripleIndex().allTriples();
}
static File newFile(File base, String... names) {
for (String name : names) base = new File(base, name);
return base;
}
static File newFile(String name) {
return name == null ? null : new File(name);
}
static Map newWeakHashMap() {
return _registerWeakMap(synchroMap(new WeakHashMap()));
}
static void ai_smartBotInit() {
ai_tripleMode();
ai_useThoughtSpaces(true);
ai_enabled(true);
releaseDBLockOnTimeoutEval();
}
static String nTriple(long i) {
return n_fancy2(i, "triple", "triples");
}
static String nTriple(Collection c) {
return n_fancy2(c, "triple", "triples");
}
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 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 GZIPOutputStream newGZIPOutputStream(File f) { try {
return new GZIPOutputStream(newFileOutputStream(f));
} catch (Exception __e) { throw rethrow(__e); } }
static int ai_writeTripleFile(Collection triples, OutputStream out) { try {
print("Writing triple file");
List names = new ArrayList();
Map index = new HashMap();
ai_writeTripleFile_precollectNames(names, index, triples);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter writer = utf8printWriter(baos);
int n = 0;
for (TripleWeb w : triples) {
++n;
String title = null;
String moreInfo = quote(str(w.globalID())) + " " + quote(unnull(title)) + " " + quote(unnull(w.source())) + " " + (w.unverified() ? "u" : "v") + " " + w.created();
writer.write(
ai_writeTripleFile_indexGet(names, index, w.a) + " " +
ai_writeTripleFile_indexGet(names, index, w.b) + " " +
ai_writeTripleFile_indexGet(names, index, w.c) + " " + moreInfo + "\n");
}
//out.println(struct(names));
print("Have " + n2(names, "name") + ", " + toM(baos.size()) + "M buffered bytes");
PrintWriter writer2 = utf8printWriter(out);
for (String s : names)
writer2.println(quoteUnlessIdentifierOrInteger(s));
writer2.println();
baos.writeTo(out);
return n;
} catch (Exception __e) { throw rethrow(__e); } }
static int ai_writeTripleFile_indexGet(List names, Map index, String s) {
Integer i = index.get(s);
if (i == null) {
i = l(names);
names.add(s);
index.put(s, i);
}
return i;
}
static void ai_writeTripleFile_precollectNames(List names, Map index, Collection triples) {
for (TripleWeb w : triples) {
ai_writeTripleFile_indexGet(names, index, w.a);
ai_writeTripleFile_indexGet(names, index, w.b);
ai_writeTripleFile_indexGet(names, index, w.c);
}
sortInPlaceIC(names);
index.clear();
for (int i = 0; i < l(names); i++) index.put(names.get(i), i);
}
static File programFile(String name) {
return prepareProgramFile(name);
}
static File programFile(String progID, String name) {
return prepareProgramFile(progID, name);
}
static void rotateLogFile(File f) {
Lock _lock_10 = appendToFile_lock; lock(_lock_10); try {
if (!f.exists()) return;
int n = 1;
File p;
while ((p = newFile(f.getParentFile(), f.getName() + ".part" + n + ".gz")).exists())
++n;
gzipFile(f, p);
assertTrue(f.delete());
} finally { unlock(_lock_10); } }
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 String programID() {
return getProgramID();
}
static void renameFile_assertTrue(File a, File b) {
if (!renameFile(a, b))
throw fail("Can't rename " + f2s(a) + " to " + f2s(b));
}
static File prepareProgramFile(String name) {
return mkdirsForFile(getProgramFile(name));
}
static File prepareProgramFile(String progID, String name) {
return mkdirsForFile(getProgramFile(progID, name));
}
static String n2(long l, String singular, String plural) {
return n_fancy2(l, singular, plural);
}
static String n2(long l, String singular) {
return n_fancy2(l, singular, singular + "s");
}
static String n2(Collection l, String singular) {
return n2(l(l), singular);
}
static String n2(Collection l, String singular, String plural) {
return n_fancy2(l, singular, plural);
}
static String n2(Map m, String singular, String plural) {
return n_fancy2(m, singular, plural);
}
static String n2(Object[] a, String singular, String plural) {
return n_fancy2(a, singular, plural);
}
static String n2(MultiSet ms, String singular, String plural) {
return n_fancy2(ms, singular, plural);
}
static 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 invokeMethod(methods.get(0), f, args);
for (int i = 0; i < n; i++) {
Method m = methods.get(i);
if (call_checkArgs(m, args, false))
return invokeMethod(m, f, args);
}
throw fail("No matching get method in " + getClassName(c));
} catch (Exception __e) { throw rethrow(__e); } }
// used internally
static ArrayList callF_makeCache(Class c) {
ArrayList l = new ArrayList();
Class _c = c;
do {
for (Method m : _c.getDeclaredMethods())
if (m.getName().equals("get")) {
m.setAccessible(true);
l.add(m);
}
if (!l.isEmpty()) break;
_c = _c.getSuperclass();
} while (_c != null);
callF_cache.put(c, l);
return l;
}
static TripleIndex tripleIndex_instance;
static TripleIndex tripleIndex() {
if (tripleIndex_instance == null) {
tripleIndex_instance = addVirtualNodeIndex(new TripleIndex());
tripleIndex_instance.activate();
}
return tripleIndex_instance;
}
static List localSoftwareMadeWebs_programsToRead_list = ll("#1010745");
static List localSoftwareMadeWebs_programsToRead() {
return localSoftwareMadeWebs_programsToRead_list;
//ret websPostedIn();
}
static RuntimeException asRuntimeException(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
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 Lock appendToFile_lock = lock();
static boolean appendToFile_keepOpen;
static HashMap appendToFile_writers = new HashMap();
static void appendToFile(String path, String s) { try {
Lock _lock_702 = appendToFile_lock; lock(_lock_702); try { // Let's just generally synchronize this to be safe.
new File(path).getParentFile().mkdirs();
path = getCanonicalPath(path);
Writer writer = appendToFile_writers.get(path);
if (writer == null) {
//print("[Logging to " + path + "]");
writer = new BufferedWriter(new OutputStreamWriter(
newFileOutputStream(path, true), "UTF-8"));
if (appendToFile_keepOpen)
appendToFile_writers.put(path, writer);
}
writer.write(s);
if (!appendToFile_keepOpen)
writer.close();
} finally { unlock(_lock_702); } } catch (Exception __e) { throw rethrow(__e); } }
static void appendToFile(File path, String s) {
if (path != null)
appendToFile(path.getPath(), s);
}
static void cleanMeUp_appendToFile() {
Lock _lock_704 = appendToFile_lock; lock(_lock_704); try {
closeAllWriters(values(appendToFile_writers));
appendToFile_writers.clear();
} finally { unlock(_lock_704); } }
static void lock(Lock lock) { try {
ping();
lock.lockInterruptibly();
ping();
} catch (Exception __e) { throw rethrow(__e); } }
static void lock(Lock lock, String msg) {
print("Locking: " + msg);
lock(lock);
}
static void lock(Lock lock, String msg, long timeout) {
print("Locking: " + msg);
lockOrFail(lock, timeout);
}
static ReentrantLock lock() {
return fairLock();
}
static String quoteUnlessIdentifierOrInteger(String s) {
return quoteIfNotIdentifierOrInteger(s);
}
static String formatSnippetID(String id) {
return "#" + parseSnippetID(id);
}
static String formatSnippetID(long id) {
return "#" + id;
}
static void unlock(Lock lock, String msg) {
print("Unlocking: " + msg);
lock.unlock();
}
static void unlock(Lock lock) {
lock.unlock();
}
static void printException(Throwable e) {
printStackTrace(e);
}
static boolean isFalse(Object o) {
return eq(false, o);
}
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 List valuesList(Map map) {
return new ArrayList(values(map));
}
static String n_fancy2(long l, String singular, String plural) {
return formatWithThousandsSeparator(l) + " " + trim(l == 1 ? singular : plural);
}
static String n_fancy2(Collection l, String singular, String plural) {
return n_fancy2(l(l), singular, plural);
}
static String n_fancy2(Map m, String singular, String plural) {
return n_fancy2(l(m), singular, plural);
}
static String n_fancy2(Object[] a, String singular, String plural) {
return n_fancy2(l(a), singular, plural);
}
static String n_fancy2(MultiSet ms, String singular, String plural) {
return n_fancy2(l(ms), singular, plural);
}
static long toM(long l) {
return (l+1024*1024-1)/(1024*1024);
}
static String toM(long l, int digits) {
return formatDouble(toM_double(l), digits);
}
static 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 volatile String caseID_caseID;
static String caseID() { return caseID_caseID; }
static void caseID(String id) {
caseID_caseID = id;
}
static boolean renameFile(File a, File b) {
mkdirsForFile(b);
return a.renameTo(b);
}
static void gzipFile(File in, File out) { try {
saveStreamToGZFile(newFileInputStream(in), out);
} catch (Exception __e) { throw rethrow(__e); } }
static Map synchroMap() {
return synchroHashMap();
}
static Map synchroMap(Map map) {
return Collections.synchronizedMap(map);
}
static String fixNewLines(String s) {
return s.replace("\r\n", "\n").replace("\r", "\n");
}
static PrintWriter utf8printWriter(OutputStream out) {
return new PrintWriter(outputStreamToWriter(out));
}
static void releaseDBLockOnTimeoutEval() {
callOptMC("beforeDelegatingToThread_do", "releaseDBLockOnTimeoutEval_before");
callOptMC("afterDelegatingToThread_do", "releaseDBLockOnTimeoutEval_after");
}
static ThreadLocal releaseDBLockOnTimeoutEval_hadLock = new ThreadLocal();
static void releaseDBLockOnTimeoutEval_before(Thread t) {
releaseDBLockOnTimeoutEval_hadLock.set(unlockIfHolding(dbLock()));
}
static void releaseDBLockOnTimeoutEval_after(Thread t) {
if (isTrue(getAndClearThreadLocal(releaseDBLockOnTimeoutEval_hadLock)))
lock(dbLock());
}
static File javaxDataDir_dir; // can be set to work on different base dir
static File javaxDataDir() {
return javaxDataDir_dir != null ? javaxDataDir_dir : new File(userHome(), "JavaX-Data");
}
static 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 String programID;
static String getProgramID() {
return nempty(programID) ? formatSnippetIDOpt(programID) : "?";
}
// TODO: ask JavaX instead
static String getProgramID(Class c) {
String id = (String) getOpt(c, "programID");
if (nempty(id))
return formatSnippetID(id);
return "?";
}
static String getProgramID(Object o) {
return getProgramID(getMainClass(o));
}
static String unnull(String s) {
return s == null ? "" : s;
}
static List unnull(List l) {
return l == null ? emptyList() : l;
}
static 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;
}
static boolean ai_useThoughtSpaces_value;
static boolean ai_useThoughtSpaces() {
return ai_useThoughtSpaces_value;
}
static void ai_useThoughtSpaces(boolean flag) {
ai_useThoughtSpaces_value = flag;
}
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 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 FileOutputStream newFileOutputStream(File path) throws IOException {
return newFileOutputStream(path.getPath());
}
static FileOutputStream newFileOutputStream(String path) throws IOException {
return newFileOutputStream(path, false);
}
static FileOutputStream newFileOutputStream(String path, boolean append) throws IOException {
mkdirsForFile(path);
FileOutputStream f = new // Line break for ancient translator
FileOutputStream(path, append);
callJavaX("registerIO", f, path, true);
return f;
}
static File[] listFiles(File dir) {
File[] files = dir.listFiles();
return files == null ? new File[0] : files;
}
static File[] listFiles(String dir) {
return listFiles(new File(dir));
}
static 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 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(MultiSet ms) { return ms == null ? 0 : ms.size(); }
static int l(Lisp l) { return l == null ? 0 : l.size(); }
static boolean sameSnippetID(String a, String b) {
if (!isSnippetID(a) || !isSnippetID(b)) return false;
return parseSnippetID(a) == parseSnippetID(b);
}
static long round(double d) {
return Math.round(d);
}
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 void sortInPlaceIC(List l) {
sortInPlace(l, caseInsensitiveComparator());
}
static int parseFirstInt(String s) {
return parseInt(jextract("", s));
}
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 Object callOptMC(String method, Object... args) {
return callOpt(mc(), method, args);
}
static Writer outputStreamToWriter(OutputStream out) { try {
return new OutputStreamWriter(out, "UTF-8");
} catch (Exception __e) { throw rethrow(__e); } }
static FileInputStream newFileInputStream(File path) throws IOException {
return newFileInputStream(path.getPath());
}
static FileInputStream newFileInputStream(String path) throws IOException {
FileInputStream f = new // Line break for ancient translator
FileInputStream(path);
//callJavaX("registerIO", f, path, true);
return f;
}
static HashMap> callMC_cache = new HashMap();
static String callMC_key;
static Method callMC_value;
// varargs assignment fixer for a single string array argument
static Object callMC(String method, String[] arg) {
return callMC(method, new Object[] {arg});
}
static Object callMC(String method, Object... args) { try {
Method me;
if (callMC_cache == null) callMC_cache = new HashMap(); // initializer time workaround
synchronized(callMC_cache) {
me = method == callMC_key ? callMC_value : null;
}
if (me != null) try {
return invokeMethod(me, null, args);
} catch (IllegalArgumentException e) {
throw new RuntimeException("Can't call " + me + " with arguments " + classNames(args), e);
}
List m;
synchronized(callMC_cache) {
m = callMC_cache.get(method);
}
if (m == null) {
if (callMC_cache.isEmpty()) {
callMC_makeCache();
m = callMC_cache.get(method);
}
if (m == null) throw fail("Method named " + method + " not found in main");
}
int n = m.size();
if (n == 1) {
me = m.get(0);
synchronized(callMC_cache) {
callMC_key = method;
callMC_value = me;
}
try {
return invokeMethod(me, null, args);
} catch (IllegalArgumentException e) {
throw new RuntimeException("Can't call " + me + " with arguments " + classNames(args), e);
}
}
for (int i = 0; i < n; i++) {
me = m.get(i);
if (call_checkArgs(me, args, false))
return invokeMethod(me, null, args);
}
throw fail("No method called " + method + " with matching arguments found in main");
} catch (Exception __e) { throw rethrow(__e); } }
static void callMC_makeCache() {
synchronized(callMC_cache) {
callMC_cache.clear();
Class _c = (Class) mc(), c = _c;
while (c != null) {
for (Method m : c.getDeclaredMethods())
if ((m.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) {
m.setAccessible(true);
multiMapPut(callMC_cache, m.getName(), m);
}
c = c.getSuperclass();
}
}
}
static String 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 A assertNotNull(A a) {
assertTrue(a != null);
return a;
}
static A assertNotNull(String msg, A a) {
assertTrue(msg, a != null);
return a;
}
// TODO: test if android complains about this
static boolean isAWTThread() {
if (isAndroid()) return false;
if (isHeadless()) return false;
return isAWTThread_awt();
}
static boolean isAWTThread_awt() {
return SwingUtilities.isEventDispatchThread();
}
static void closeAllWriters(Collection extends Writer> l) {
for (Writer w : unnull(l)) try {
w.close();
} catch (Throwable __e) { printStackTrace2(__e); }
}
static String getStackTrace2(Throwable throwable) {
return hideCredentials(getStackTrace(throwable) + replacePrefix("java.lang.RuntimeException: ", "FAIL: ",
hideCredentials(str(getInnerException(throwable)))) + "\n");
}
static Thread currentThread() {
return Thread.currentThread();
}
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 A addVirtualNodeIndex(A i) {
setAdd(virtualNodeIndices_list, i);
return i;
}
public static boolean isSnippetID(String s) {
try {
parseSnippetID(s);
return true;
} catch (RuntimeException e) {
return false;
}
}
static String _userHome;
static String userHome() {
if (_userHome == null) {
if (isAndroid())
_userHome = "/storage/sdcard0/";
else
_userHome = System.getProperty("user.home");
//System.out.println("userHome: " + _userHome);
}
return _userHome;
}
static File userHome(String path) {
return new File(userDir(), path);
}
static Map emptyMap() {
return new HashMap();
}
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 String getCanonicalPath(String path) { try {
return new File(path).getCanonicalPath();
} catch (Exception __e) { throw rethrow(__e); } }
static String formatDouble(double d, int digits) {
String format = "0." + rep(digits, '#');
return new java.text.DecimalFormat(format, new java.text.DecimalFormatSymbols(Locale.ENGLISH)).format(d);
}
static String quoteIfNotIdentifierOrInteger(String s) {
if (s == null) return null;
return isJavaIdentifier(s) || isInteger(s) ? s : quote(s);
}
static double toM_double(long l) {
return l/(1024*1024.0);
}
static List sortInPlace(List l, final Object comparator) {
return sortedInPlace(l, comparator);
}
static List sortInPlace(List l) {
return sortedInPlace(l);
}
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 invokeMethod(m, null, args);
} else {
Method m = call_findMethod(o, method, args, false);
m.setAccessible(true);
return invokeMethod(m, o, args);
}
} catch (Exception e) {
throw 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 boolean isTrue(Object o) {
if (o instanceof Boolean)
return ((Boolean) o).booleanValue();
if (o == null) return false;
if (o instanceof ThreadLocal)
return isTrue(((ThreadLocal) o).get());
throw fail(getClassName(o));
}
static 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 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 A getAndClearThreadLocal(ThreadLocal tl) {
A a = tl.get();
tl.set(null);
return a;
}
static String emptySymbol_value;
static String emptySymbol() {
if (emptySymbol_value == null) emptySymbol_value = symbol("");
return emptySymbol_value;
}
static boolean unlockIfHolding(Lock lock) {
if (lock == null || !hasLock(lock)) return false;
unlock(lock);
return true;
}
static Comparator caseInsensitiveComparator() {
return String.CASE_INSENSITIVE_ORDER;
}
static Class javax() {
return getJavaX();
}
static boolean neq(Object a, Object b) {
return !eq(a, b);
}
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")
// returns from C to C
static String jextract(String pat, String s) {
List tok = javaTok(s);
List tokpat = javaTok(pat);
jfind_preprocess(tokpat);
int i = jfind(tok, tokpat);
if (i < 0) return null;
int j = i + l(tokpat) - 2;
return join(subList(tok, i, j));
}
static String getClassName(Object o) {
return o == null ? "null" : o instanceof Class ? ((Class) o).getName() : o.getClass().getName();
}
static Object invokeMethod(Method m, Object o, Object... args) { try {
try {
return m.invoke(o, args);
} catch (InvocationTargetException e) {
throw rethrow(getExceptionCause(e));
}
} catch (Exception __e) { throw rethrow(__e); } }
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 Collection values(Map map) {
return map == null ? emptyList() : map.values();
}
static Collection values(MultiMap mm) {
return mm == null ? emptyList() : concatLists(values(mm.data));
}
static List ai_fillTripleIndex_useThese;
static Object ai_fillTripleIndex_useTripleIndex;
static void ai_fillTripleIndex() {
if (ai_fillTripleIndex_useTripleIndex != null) {
print("Copying triple index.");
ai_reuseTripleIndex(ai_fillTripleIndex_useTripleIndex);
ai_fillTripleIndex_useTripleIndex = null;
ai_fillTripleIndex_useThese = null;
return;
}
print("Getting triples.");
long _startTime_22 = sysNow();
List triples = ai_fillTripleIndex_useThese != null
? ai_fillTripleIndex_useThese
: localSoftwareMadeTriples();
ai_fillTripleIndex_useThese = null;
_startTime_22 = sysNow()-_startTime_22; saveTiming(_startTime_22);
long time = lastTiming();
long us = ratio(time*1000, l(triples));
//print("Got triples in "T + time + " ms. Making triple index of " + nTriples(triples) + ".");
print("Got " + nTriples(triples) + " in " + us + " "+ unicode_micro() + "s per triple. Making triple index.");
long _startTime_23 = sysNow();
ai_replaceTripleIndexWithTriples(triples);
//ai_tripleIndexToInvalidatedWebs();
ai_deleteInvalidTriples();
done2_always("Triple index made.", _startTime_23);
}
static Object callJavaX(String method, Object... args) {
return callOpt(getJavaX(), method, args);
}
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);
}
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 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 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 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 File getProgramFile(String progID, String fileName) {
if (new File(fileName).isAbsolute())
return new File(fileName);
return new File(getProgramDir(progID), fileName);
}
static File getProgramFile(String fileName) {
return getProgramFile(getProgramID(), fileName);
}
static ReentrantLock fairLock() {
return new ReentrantLock(true);
}
// A concept should be an object, not just a string.
// Functions that should always be there for child processes:
static int concepts_internStringsLongerThan = 10;
static ThreadLocal concepts_unlisted = new ThreadLocal();
static interface Derefable {
Concept get();
}
static interface IConceptIndex {
void update(Concept c); // also for adding
void remove(Concept c);
}
static interface IFieldIndex {
List getAll(Val val);
}
static class Concepts {
Map concepts = synchroTreeMap();
HashMap perClassData = new HashMap();
String programID; // set to "-" for non-persistent (possibly not implemented)
long idCounter;
volatile long changes, changesWritten;
volatile java.util.Timer autoSaver;
volatile boolean savingConcepts, dontSave;
boolean initialSave = true; // set to false to avoid initial useless saving
int autoSaveInterval = -1000; // 1 second + wait logic
boolean useGZIP = true, quietSave;
ReentrantLock lock = new ReentrantLock(true);
ReentrantLock saverLock = new ReentrantLock(true);
long lastSaveTook, lastSaveWas;
float maxAutoSavePercentage = 10;
List conceptIndices;
Map, Map> fieldIndices;
List saveActions = synchroList();
Concepts() {}
Concepts(String programID) {
this.programID = programID;}
synchronized long internalID() {
do {
++idCounter;
} while (hasConcept(idCounter));
return idCounter;
}
void initProgramID() {
if (programID == null)
programID = getDBProgramID();
}
// Now tries to load from bot first, then go to disk.
Concepts load() {
return load(false);
}
Concepts safeLoad() {
return load(true);
}
Concepts load(boolean allDynamic) {
initProgramID();
if (tryToGrab(allDynamic)) return this;
return loadFromDisk(allDynamic);
}
Concepts loadFromDisk() { return loadFromDisk(false); }
Concepts loadFromDisk(boolean allDynamic) {
if (nempty(concepts)) clearConcepts();
//DynamicObject_loading.set(true); // now done in unstructure()
//try {
long time = now();
Map _concepts = concepts; // empty map
readLocally2_allDynamic.set(allDynamic);
readLocally2(this, programID, "concepts");
Map __concepts = concepts;
concepts = _concepts;
concepts.putAll(__concepts);
int l = readLocally_stringLength;
int tokrefs = unstructure_tokrefs;
assignConceptsToUs();
done("Loaded " + n(l(concepts), "concepts"), time);
if (fileSize(getProgramFile(programID, "idCounter.structure")) != 0)
readLocally2(this, programID, "idCounter");
else
calcIdCounter();
/*} finally {
DynamicObject_loading.set(null);
}*/
if (initialSave) allChanged();
return this;
}
Concepts loadConcepts() { return load(); }
boolean tryToGrab(boolean allDynamic) {
if (sameSnippetID(programID, getDBProgramID())) return false;
RemoteDB db = connectToDBOpt(programID);
try {
if (db != null) {
loadGrab(db.fullgrab(), allDynamic);
return true;
}
} finally {
if (db != null) db.close();
}
return false;
}
Concepts load(String grab) {
return loadGrab(grab, false);
}
Concepts safeLoad(String grab) {
return loadGrab(grab, true);
}
Concepts loadGrab(String grab, boolean allDynamic) {
clearConcepts();
DynamicObject_loading.set(true);
try {
Map map = (Map)
(allDynamic ? safeUnstructure(grab) : unstructure(grab));
concepts.putAll(map);
assignConceptsToUs();
for (long l : map.keySet())
idCounter = max(idCounter, l);
} finally {
DynamicObject_loading.set(null);
}
allChanged();
return this;
}
void assignConceptsToUs() {
for (Concept c : values(concepts)) {
c._concepts = this;
callOpt_noArgs(c, "_doneLoading2");
}
}
String progID() {
return programID == null ? getDBProgramID() : programID;
}
Concept getConcept(String id) {
return empty(id) ? null : getConcept(parseLong(id));
}
Concept getConcept(long id) {
return (Concept) concepts.get((long) id);
}
Concept getConcept(RC ref) {
return ref == null ? null : getConcept(ref.longID());
}
boolean hasConcept(long id) {
return concepts.containsKey((long) id);
}
void deleteConcept(long id) {
Concept c = getConcept(id);
if (c == null)
print("Concept " + id + " not found");
else
c.delete();
}
void calcIdCounter() {
long id_ = 0;
for (long id : keys(concepts))
id_ = max(id_, id);
idCounter = id_+1;
saveLocally2(this, programID, "idCounter");
}
void saveConceptsIfDirty() { saveConcepts(); }
void save() { saveConcepts(); }
void saveConcepts() {
if (dontSave) return;
initProgramID();
saverLock.lock();
savingConcepts = true;
long start = now(), time;
try {
String s = null;
//synchronized(main.class) {
long _changes = changes;
if (_changes == changesWritten) return;
File f = getProgramFile(programID, useGZIP ? "concepts.structure.gz" : "concepts.structure");
lock.lock();
long fullTime = now();
try {
saveLocally2(this, programID, "idCounter");
if (useGZIP) {
saveGZStructureToFile(f, cloneMap(concepts));
getProgramFile(programID, "concepts.structure").delete();
} else
s = structure(cloneMap(concepts));
} finally {
lock.unlock();
}
while (nempty(saveActions))
pcallF(popFirst(saveActions));
changesWritten = _changes; // only update when structure didn't fail
if (!useGZIP) {
time = now()-start;
if (!quietSave)
print("Saving " + toM(l(s)) + "M chars (" /*+ changesWritten + ", "*/ + time + " ms)");
start = now();
saveTextFile(f, javaTokWordWrap(s));
getProgramFile(programID, "concepts.structure.gz").delete();
}
copyFile(f, getProgramFile(programID, "backups/concepts.structure" + (useGZIP ? ".gz" : "") + ".backup" + ymd() + "-" + formatInt(hours(), 2)));
time = now()-start;
if (!quietSave)
print(programID + ": Saved " + toK(f.length()) + " K, " + n(concepts, "concepts") + " (" + time + " ms)");
lastSaveWas = fullTime;
lastSaveTook = now()-fullTime;
} finally {
savingConcepts = false;
saverLock.unlock();
}
}
void _autoSaveConcepts() {
if (autoSaveInterval < 0 && maxAutoSavePercentage != 0) {
long pivotTime = Math.round(lastSaveWas+lastSaveTook*100.0/maxAutoSavePercentage);
if (now() < pivotTime) {
//print("Skipping auto-save (last save took " + lastSaveTook + ")");
return;
}
}
try {
saveConcepts();
} catch (Throwable e) {
print("Concept save failed, will try again: " + e);
}
}
void clearConcepts() {
concepts.clear();
allChanged();
}
synchronized void allChanged() {
++changes;
}
// auto-save every second if dirty
synchronized void autoSaveConcepts() {
if (autoSaver == null) {
if (isTransient()) throw fail("Can't persist transient database");
autoSaver = doEvery_daemon(abs(autoSaveInterval), new Runnable() { public void run() { try { _autoSaveConcepts() ;
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "_autoSaveConcepts()"; }});
// print("Installed auto-saver (" + autoSaveInterval + " ms, " + progID() + ")");
}
}
void cleanMeUp() {
if (autoSaver != null) {
autoSaver.cancel();
autoSaver = null;
}
while (savingConcepts) sleepInCleanUp(10);
saveConceptsIfDirty();
}
Map getIDsAndNames() {
Map map = new HashMap();
Map cloned = cloneMap(concepts);
for (long id : keys(cloned))
map.put(id, cloned.get(id).className);
return map;
}
void deleteConcepts(List l) {
for (Object o : l)
if (o instanceof Long)
concepts.remove((Long) o);
else if (o instanceof Concept)
((Concept) o).delete();
else
warn("Can't delete " + getClassName(o));
}
A conceptOfType(Class type) {
return firstOfType(allConcepts(), type);
}
List conceptsOfType(Class type) {
return filterByType(allConcepts(), type);
}
List listConcepts(Class type) {
return conceptsOfType(type);
}
List list(Class type) {
return conceptsOfType(type);
}
List list(String type) {
return conceptsOfType(type);
}
List conceptsOfType(String type) {
return filterByDynamicType(allConcepts(), "main$" + type);
}
boolean hasConceptOfType(Class extends Concept> type) {
return hasType(allConcepts(), type);
}
void persistConcepts() {
loadConcepts();
autoSaveConcepts();
}
// We love synonyms
void conceptPersistence() {
persistConcepts();
}
Concepts persist() { persistConcepts(); return this; }
void persist(int interval) { autoSaveInterval = interval; persist(); }
// Runs r if there is no concept of that type
A ensureHas(Class c, Runnable r) {
A a = conceptOfType(c);
if (a == null) {
r.run();
a = conceptOfType(c);
if (a == null)
throw fail("Concept not made by " + r + ": " + shortClassName(c));
}
return a;
}
// Ensures that every concept of type c1 is ref'd by a concept of
// type c2.
// Type of func: voidfunc(concept)
void ensureHas(Class extends Concept> c1, Class extends Concept> c2, Object func) {
for (Concept a : conceptsOfType(c1)) {
Concept b = findBackRef(a, c2);
if (b == null) {
callF(func, a);
b = findBackRef(a, c2);
if (b == null)
throw fail("Concept not made by " + func + ": " + shortClassName(c2));
}
}
}
// Type of func: voidfunc(concept)
void forEvery(Class extends Concept> type, Object func) {
for (Concept c : conceptsOfType(type))
callF(func, c);
}
int deleteAll(Class extends Concept> type) {
List l = (List) conceptsOfType(type);
for (Concept c : l) c.delete();
return l(l);
}
Collection allConcepts() {
synchronized(concepts) {
return new ArrayList(values(concepts));
}
}
int countConcepts(Class c, Object... params) {
int n = 0;
for (A x : list(c)) if (checkConceptFields(x, params)) ++n;
return n;
}
int countConcepts(String c, Object... params) {
int n = 0;
for (Concept x : list(c)) if (checkConceptFields(x, params)) ++n;
return n;
}
int countConcepts() {
return l(concepts);
}
synchronized void addConceptIndex(IConceptIndex index) {
if (conceptIndices == null)
conceptIndices = new ArrayList();
conceptIndices.add(index);
}
synchronized void addFieldIndex(Class extends Concept> c, String field, IFieldIndex index) {
if (fieldIndices == null)
fieldIndices = new HashMap();
Map map = fieldIndices.get(c);
if (map == null)
fieldIndices.put(c, map = new HashMap());
map.put(field, index);
}
synchronized IFieldIndex getFieldIndex(Class extends Concept> c, String field) {
if (fieldIndices == null) return null;
Map map = fieldIndices.get(c);
return map == null ? null : map.get(field);
}
// inter-process methods
RC xnew(String name, Object... values) {
return new RC(cnew(name, values));
}
void xset(long id, String field, Object value) {
xset(new RC(id), field, value);
}
void xset(RC c, String field, Object value) {
if (value instanceof RC)
value = getConcept((RC) value);
cset(getConcept(c), field, value);
}
Object xget(long id, String field) {
return xget(new RC(id), field);
}
Object xget(RC c, String field) {
return xgetPost(cget(getConcept(c), field));
}
Object xgetPost(Object o) {
o = deref(o);
if (o instanceof Concept)
return new RC((Concept) o);
return o;
}
void xdelete(long id) {
xdelete(new RC(id));
}
void xdelete(RC c) {
getConcept(c).delete();
}
void xdelete(List l) {
for (RC c : l)
xdelete(c);
}
List xlist() {
return map("toPassRef", allConcepts());
}
List xlist(String className) {
return map("toPassRef", conceptsOfType(className));
}
boolean isTransient() { return eq(programID, "-"); }
String xfullgrab() {
lock.lock();
try {
if (changes == changesWritten && !isTransient())
return loadConceptsStructure(programID);
return structure(cloneMap(concepts));
} finally {
lock.unlock();
}
}
void xshutdown() {
// Killing whole VM if someone wants this DB to shut down
cleanKillVM();
}
long xchangeCount() { return changes; }
int xcount() { return countConcepts(); }
void register(Concept c) {
if (c._concepts == this) return;
if (c._concepts != null) throw fail("Can't re-register");
c._concepts = this;
c.id = internalID();
c.created = now();
concepts.put((long) c.id, c);
c.change();
}
} // class Concepts
static volatile Concepts mainConcepts = new Concepts(); // Where we create new concepts
static class Concept extends DynamicObject {
transient Concepts _concepts; // Where we belong
long id;
//O madeBy;
//double energy;
//bool defunct;
long created;
// used only internally (cnew)
Concept(String className) {
super(className);
_created();
}
Concept() {
if (!_loading()) {
//className = shortClassName(this); // XXX - necessary?
//print("New concept of type " + className);
_created();
}
}
Concept(boolean unlisted) {
if (!unlisted) _created();
}
List[ refs;
List][ backRefs;
static boolean loading() { return _loading(); }
static boolean _loading() { return isTrue(DynamicObject_loading.get()); }
void _created() {
if (!isTrue(concepts_unlisted.get()))
mainConcepts.register(this);
}
/*void put(S field, O value) {
fieldValues.put(field, value);
change();
}
O get(S field) {
ret fieldValues.get(field);
}*/
class Ref] {
A value;
Ref() {
if (!isTrue(DynamicObject_loading.get())) refs = addDyn(refs, this);
}
Ref(A value) {
this.value = value;
refs = addDyn(refs, this);
index();
}
// get owning concept (source)
Concept concept() {
return Concept.this;
}
// get target
A get() { return value; }
boolean has() { return value != null; }
void set(A a) {
if (a == value) return;
unindex();
value = a;
index();
}
void set(Ref ref) { set(ref.get()); }
void clear() { set((A) null); }
void index() {
if (value != null)
value.backRefs = addDyn(value.backRefs, this);
change();
}
void unindex() {
if (value != null)
value.backRefs = removeDyn(value.backRefs, this);
}
void change() {
Concept.this.change();
}
}
class RefL extends AbstractList {
List < Ref < A > > l = new ArrayList();
public A set(int i, A o) {
A prev = l.get(i).get();
l.get(i).set(o);
return prev;
}
public void add(int i, A o) {
l.add(i, new Ref(o));
}
public A get(int i) {
return l.get(i).get();
}
public A remove(int i) {
return l.remove(i).get();
}
public int size() {
return l.size();
}
public boolean contains(Object o) {
if (o instanceof Concept)
for (Ref r : l) if (eq(r.get(), o)) return true;
return super.contains(o);
}
}
void delete() {
//name = "[defunct " + name + "]";
//defunct = true;
//energy = 0;
for (Ref r : unnull(refs))
r.unindex();
refs = null;
for (Ref r : cloneList(backRefs))
r.set((Concept) null);
backRefs = null;
if (_concepts != null) {
_concepts.concepts.remove((long) id);
_concepts.allChanged();
if (_concepts.conceptIndices != null)
for (IConceptIndex index : _concepts.conceptIndices)
index.remove(this);
_concepts = null;
}
id = 0;
}
BaseXRef export() {
return new BaseXRef(_concepts.progID(), id);
}
// notice system of a change in this object
void change() {
if (_concepts != null) {
_concepts.allChanged();
if (_concepts.conceptIndices != null)
for (IConceptIndex index : _concepts.conceptIndices)
index.update(this);
}
}
void _change() { change(); }
String _programID() {
return _concepts == null ? getDBProgramID() : _concepts.progID();
}
} // class Concept
// remote reference (for inter-process communication or
// external databases). Formerly "PassRef".
// prepared for string ids if we do them later
static class RC {
transient Object owner;
String id;
RC() {} // make serialisation happy
RC(long id) { this.id = str(id); }
RC(Object owner, long id) { this.id = str(id); this.owner = owner; }
RC(Concept c) { this(c.id); }
long longID() { return parseLong(id); }
public String toString() {
return id;
}
transient RemoteDB db;
String getString(String field) { return db.xS(this, field); }
Object get(String field) { return db.xget(this, field); }
void set(String field, Object value) { db.xset(this, field, value); }
}
// Reference to a concept in another program
static class BaseXRef {
String programID;
long id;
BaseXRef() {}
BaseXRef(String programID, long id) {
this.id = id;
this.programID = programID;}
public boolean equals(Object o) {
if (!(o instanceof BaseXRef)) return false;
BaseXRef r = (BaseXRef) ( o);
return eq(programID, r.programID) && eq(id, r.id);
}
public int hashCode() {
return programID.hashCode() + (int) id;
}
}
// BaseXRef as a concept
static class XRef extends Concept {
BaseXRef ref;
XRef() {}
XRef(BaseXRef ref) {
this.ref = ref; _doneLoading2(); }
// after we have been added to concepts
void _doneLoading2() {
getIndex().put(ref, this);
}
HashMap getIndex() {
return getXRefIndex(_concepts);
}
}
static synchronized HashMap getXRefIndex(Concepts concepts) {
HashMap cache = (HashMap) concepts.perClassData.get(XRef.class);
if (cache == null)
concepts.perClassData.put(XRef.class, cache = new HashMap());
return cache;
}
// uses mainConcepts
static XRef lookupOrCreateXRef(BaseXRef ref) {
XRef xref = getXRefIndex(mainConcepts).get(ref);
if (xref == null)
xref = new XRef(ref);
return xref;
}
// define standard concept functions to use main concepts
static List list(Class type) {
return mainConcepts.list(type);
}
static List list(Concepts concepts, Class type) {
return concepts.list(type);
}
static List list(String type) {
return mainConcepts.list(type);
}
static List list(Concepts concepts, String type) {
return concepts.list(type);
}
static int csetAll(Concept c, Object... values) {
return cset(c, values);
}
static void cleanMeUp_concepts() {
mainConcepts.cleanMeUp();
}
static void loadAndAutoSaveConcepts() {
mainConcepts.persist();
}
static void loadAndAutoSaveConcepts(int interval) {
mainConcepts.persist(interval);
}
static void loadConceptsFrom(String progID) {
mainConcepts.programID = progID;
mainConcepts.load();
}
static List conceptsOfType(String type) {
return mainConcepts.conceptsOfType(type);
}
static long changeCount() {
return mainConcepts.changes;
}
static List exposedDBMethods = ll("xlist", "xnew", "xset", "xdelete", "xget", "xclass", "xfullgrab", "xshutdown", "xchangeCount", "xcount");
static RC toPassRef(Concept c) {
return new RC(c);
}
// so we can instantiate the program to run as a bare DB bot
static Lock dbLock() {
return mainConcepts.lock;
}
static String formatWithThousandsSeparator(long l) {
return NumberFormat.getInstance(new Locale("en_US")).format(l);
}
static boolean isEmpty(Collection c) {
return c == null || c.isEmpty();
}
static boolean isEmpty(CharSequence s) {
return s == null || s.length() == 0;
}
static boolean isEmpty(Object[] a) {
return a == null || a.length == 0;
}
static boolean isEmpty(Map map) {
return map == null || map.isEmpty();
}
static int parseInt(String s) {
return empty(s) ? 0 : Integer.parseInt(s);
}
static int parseInt(char c) {
return Integer.parseInt(str(c));
}
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 void saveStreamToGZFile(InputStream in, File file) { try {
File parentFile = file.getParentFile();
if (parentFile != null)
parentFile.mkdirs();
String tempFileName = file.getPath() + "_temp";
File tempFile = new File(tempFileName);
if (tempFile.exists()) try {
String saveName = tempFileName + ".saved." + now();
copyFile(tempFile, new File(saveName));
} catch (Throwable e) { printStackTrace(e); }
FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath());
GZIPOutputStream gos = new GZIPOutputStream(fileOutputStream);
copyStreamAndCloseAll(in, gos);
if (file.exists() && !file.delete())
throw fail("Can't delete " + file.getPath());
if (!tempFile.renameTo(file))
throw fail("Can't rename " + tempFile + " to " + file);
} catch (Exception __e) { throw rethrow(__e); } }
static Throwable getExceptionCause(Throwable e) {
Throwable c = e.getCause();
return c != null ? c : e;
}
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 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 int ai_deleteInvalidTriples() {
Matches m = new Matches();
int count = 0;
for (TripleWeb w : ai_triples("$X", "is", "invalid"))
if (swic_trim(w.a, "Web ", m) && isGlobalID(m.unq(0))) {
String id = m.unq(0);
if (eq(id, "rjncddmdgpisijjq")) print("rjncddmdgpisijjq!");
TripleWeb ww = tripleIndex().getTriple(asGlobalID(id));
if (ww != null) ++count;
tripleIndex().removeTriple(ww);
ai_invalidatedWebs_cache_put(asGlobalID(id), w.globalID());
}
if (count > 0) print("Deleted " + n2(count, "invalid triple"));
return count;
}
public static 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 int jfind(String s, String in) {
return jfind(javaTok(s), in);
}
static int jfind(List tok, String in) {
return jfind(tok, 1, in);
}
static int jfind(List tok, int startIdx, String in) {
return jfind(tok, startIdx, in, null);
}
static int jfind(List tok, String in, Object condition) {
return jfind(tok, 1, in, condition);
}
static int jfind(List tok, int startIdx, String in, Object condition) {
List tokin = javaTok(in);
jfind_preprocess(tokin);
return jfind(tok, startIdx, tokin, condition);
}
// assumes you preprocessed tokin
static int jfind(List tok, List tokin) {
return jfind(tok, 1, tokin);
}
static int jfind(List tok, int startIdx, List tokin) {
return jfind(tok, startIdx, tokin, null);
}
static int jfind(List tok, int startIdx, List tokin, Object condition) {
return findCodeTokens(tok, startIdx, false, toStringArray(codeTokensOnly(tokin)), condition);
}
static List jfind_preprocess(List tok) {
for (String type : litlist("quoted", "id", "int"))
replaceSublist(tok, ll("<", "", type, "", ">"), ll("<" + type + ">"));
replaceSublist(tok, ll("\\", "", "*"), ll("\\*"));
return tok;
}
static String replacePrefix(String prefix, String replacement, String s) {
if (!startsWith(s, prefix)) return s;
return replacement + substring(s, l(prefix));
}
static long lastTiming() {
return getLastTiming();
}
static boolean isJavaIdentifier(String s) {
if (empty(s) || !Character.isJavaIdentifierStart(s.charAt(0)))
return false;
for (int i = 1; i < s.length(); i++)
if (!Character.isJavaIdentifierPart(s.charAt(i)))
return false;
return true;
}
static void copyStreamAndCloseAll(InputStream in, OutputStream out) { try {
try {
copyStreamAndCloseInput(in, out);
} finally {
out.close();
}
} catch (Exception __e) { throw rethrow(__e); } }
public static void copyFile(File src, File dest) { try {
mkdirsForFile(dest);
FileInputStream inputStream = new FileInputStream(src.getPath());
FileOutputStream outputStream = newFileOutputStream(dest.getPath());
try {
copyStream(inputStream, outputStream);
inputStream.close();
} finally {
outputStream.close();
}
} catch (Exception __e) { throw rethrow(__e); } }
// 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 String nTriples(long i) {
return n_fancy2(i, "triple", "triples");
}
static String nTriples(Collection c) {
return n_fancy2(c, "triple", "triples");
}
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 void incAtomicLong(AtomicLong l) {
l.incrementAndGet();
}
static Class __javax;
static Class getJavaX() {
return __javax;
}
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 boolean hasLock(Lock lock) {
return ((ReentrantLock) lock).isHeldByCurrentThread();
}
static Throwable getInnerException(Throwable e) {
while (e.getCause() != null)
e = e.getCause();
return e;
}
static File getProgramDir() {
return programDir();
}
static File getProgramDir(String snippetID) {
return programDir(snippetID);
}
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 void ai_reuseTripleIndex(Object index) {
copyAllFields(index, tripleIndex());
}
static long ratio(long x, long y) {
return y == 0 ? 0 : x/y;
}
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 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 List sortedInPlace(List l, final Object comparator) {
sort(l, makeComparator(comparator));
return l;
}
static List sortedInPlace(List l) {
sort(l);
return l;
}
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 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);
return getStackTrace_noRecord(throwable);
}
static String getStackTrace_noRecord(Throwable throwable) {
StringWriter writer = new StringWriter();
throwable.printStackTrace(new PrintWriter(writer));
return hideCredentials(writer.toString());
}
static String getStackTrace() {
return getStackTrace_noRecord(new Throwable());
}
static Class mc() {
return main.class;
}
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 rep(int n, char c) {
return repeat(c, n);
}
static String rep(char c, int n) {
return repeat(c, n);
}
static List rep(A a, int n) {
return repeat(a, n);
}
static List rep(int n, A a) {
return repeat(n, a);
}
// 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 void ai_replaceTripleIndexWithTriples(List l) {
TripleIndex i = tripleIndex();
i.clear();
for (TripleWeb w : l)
i.addTriple(w);
i.trimToSize();
}
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 extends Collection > lists) {
List l = new ArrayList();
for (Collection list : lists)
if (list != null)
l.addAll(list);
return l;
}
static Object callOpt(Object o) {
if (o == null) return null;
return callF(o);
}
static Object callOpt(Object o, String method, Object... args) {
try {
if (o == null) return null;
if (o instanceof Class) {
Method m = callOpt_findStaticMethod((Class) o, method, args, false);
if (m == null) return null;
m.setAccessible(true);
return invokeMethod(m, null, args);
} else {
Method m = callOpt_findMethod(o, method, args, false);
if (m == null) return null;
m.setAccessible(true);
return invokeMethod(m, o, args);
}
} catch (Exception e) {
//fail(e.getMessage() + " | Method: " + method + ", receiver: " + className(o) + ", args: (" + join(", ", map(f className, args) + ")");
throw rethrow(e);
}
}
static Method callOpt_findStaticMethod(Class c, String method, Object[] args, boolean debug) {
Class _c = c;
while (c != null) {
for (Method m : c.getDeclaredMethods()) {
if (debug)
System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
if (!m.getName().equals(method)) {
if (debug) System.out.println("Method name mismatch: " + method);
continue;
}
if ((m.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0 || !callOpt_checkArgs(m, args, debug))
continue;
return m;
}
c = c.getSuperclass();
}
return null;
}
static Method callOpt_findMethod(Object o, String method, Object[] args, boolean debug) {
Class c = o.getClass();
while (c != null) {
for (Method m : c.getDeclaredMethods()) {
if (debug)
System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
if (m.getName().equals(method) && callOpt_checkArgs(m, args, debug))
return m;
}
c = c.getSuperclass();
}
return null;
}
private static boolean callOpt_checkArgs(Method m, Object[] args, boolean debug) {
Class>[] types = m.getParameterTypes();
if (types.length != args.length) {
if (debug)
System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
return false;
}
for (int i = 0; i < types.length; i++)
if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
if (debug)
System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
return false;
}
return true;
}
static void assertFalse(Object o) {
if (!(eq(o, false) /*|| isFalse(pcallF(o))*/))
throw fail(str(o));
}
static boolean assertFalse(boolean b) {
if (b) throw fail("oops");
return b;
}
static boolean assertFalse(String msg, boolean b) {
if (b) throw fail(msg);
return b;
}
static String unicode_micro() {
return "\u00B5";
}
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 long now_virtualTime;
static long now() {
return now_virtualTime != 0 ? now_virtualTime : System.currentTimeMillis();
}
static boolean setAdd(Collection c, A a) {
if (c == null || c.contains(a)) return false;
c.add(a);
return true;
}
static File userDir() {
return new File(userHome());
}
static File userDir(String path) {
return new File(userHome(), path);
}
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 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 getType(Object o) {
return getClassName(o);
}
static int min(int a, int b) {
return Math.min(a, b);
}
static long min(long a, long b) {
return Math.min(a, b);
}
static float min(float a, float b) { return Math.min(a, b); }
static float min(float a, float b, float c) { return min(min(a, b), c); }
static double min(double a, double b) {
return Math.min(a, b);
}
static double min(double[] c) {
double x = Double.MAX_VALUE;
for (double d : c) x = Math.min(x, d);
return x;
}
static float min(float[] c) {
float x = Float.MAX_VALUE;
for (float d : c) x = Math.min(x, d);
return x;
}
static byte min(byte[] c) {
byte x = 127;
for (byte d : c) if (d < x) x = d;
return x;
}
static short min(short[] c) {
short x = 0x7FFF;
for (short d : c) if (d < x) x = d;
return x;
}
static String[] toStringArray(Collection c) {
String[] a = new String[l(c)];
Iterator it = c.iterator();
for (int i = 0; i < l(a); i++)
a[i] = it.next();
return a;
}
static String[] toStringArray(Object o) {
if (o instanceof String[])
return (String[]) o;
else if (o instanceof Collection)
return toStringArray((Collection) o);
else
throw fail("Not a collection or array: " + getClassName(o));
}
static Comparator makeComparator(final Object f) {
if (f instanceof Comparator) return (Comparator) f;
return new Comparator() {
public int compare(Object a, Object b) {
return (int) callF(f, a, b);
}
};
}
static GlobalID asGlobalID(String id) {
return id == null ? null : new GlobalID(id);
}
static boolean swic_trim(String a, String b, Matches m) {
if (!swic(a, b)) return false;
m.m = new String[] {trim(substring(a, l(b)))};
return true;
}
static long getLastTiming() {
ThreadLocal tl = (ThreadLocal) ( getOpt(getMainClass(), "saveTiming_last"));
if (tl == null) return -1;
Long l = tl.get();
return l == null ? -1 : l;
}
// 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_95 = ai_invalidatedWebs_cache.lock; lock(_lock_95); 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_95); } }
static void ai_invalidatedWebs_onRemovedWeb(Web web) {
if (web.unverified) return;
Lock _lock_97 = ai_invalidatedWebs_cache.lock; lock(_lock_97); 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_97); } }
static void ai_invalidatedWebs_cache_put(GlobalID a, GlobalID b) {
ai_invalidatedWebs_cache.get().put(a, b);
}
static void copyStream(InputStream in, OutputStream out) { try {
byte[] buf = new byte[65536];
while (true) {
int n = in.read(buf);
if (n <= 0) return;
out.write(buf, 0, n);
}
} catch (Exception __e) { throw rethrow(__e); } }
static 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 void put(Map map, A a, B b) {
if (map != null) map.put(a, b);
}
static List replaceSublist(List l, List x, List y) {
if (x == null) return l;
int i = 0;
while (true) {
i = indexOfSubList(l, x, i);
if (i < 0) break;
// It's inefficient :D
for (int j = 0; j < l(x); j++) l.remove(i);
l.addAll(i, y);
i += l(y);
}
return l;
}
static List replaceSublist(List l, int fromIndex, int toIndex, List y) {
// inefficient
while (toIndex > fromIndex) l.remove(--toIndex);
l.addAll(fromIndex, y);
return l;
}
static boolean isGlobalID(String s) {
return possibleGlobalID(s);
}
static List codeTokensOnly(List tok) {
List l = new ArrayList();
for (int i = 1; i < tok.size(); i += 2)
l.add(tok.get(i));
return l;
}
static boolean 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 VF1() { 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 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 ArrayList litlist(A... a) {
ArrayList l = new ArrayList(a.length);
for (A x : a) l.add(x);
return l;
}
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 int max(int a, int b) { return Math.max(a, b); }
static int max(int a, int b, int c) { return max(max(a, b), c); }
static long max(int a, long b) { return Math.max((long) a, b); }
static long max(long a, long b) { return Math.max(a, b); }
static double max(int a, double b) { return Math.max((double) a, b); }
static float max(float a, float b) { return Math.max(a, b); }
static double max(double a, double b) { return Math.max(a, b); }
static int max(Collection c) {
int x = Integer.MIN_VALUE;
for (int i : c) x = max(x, i);
return x;
}
static double max(double[] c) {
if (c.length == 0) return Double.MIN_VALUE;
double x = c[0];
for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]);
return x;
}
static float max(float[] c) {
if (c.length == 0) return Float.MAX_VALUE;
float x = c[0];
for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]);
return x;
}
static byte max(byte[] c) {
byte x = -128;
for (byte d : c) if (d > x) x = d;
return x;
}
static short max(short[] c) {
short x = -0x8000;
for (short d : c) if (d > x) x = d;
return x;
}
static 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 int findCodeTokens(List tok, String... tokens) {
return findCodeTokens(tok, 1, false, tokens);
}
static int findCodeTokens(List tok, boolean ignoreCase, String... tokens) {
return findCodeTokens(tok, 1, ignoreCase, tokens);
}
static int findCodeTokens(List tok, int startIdx, boolean ignoreCase, String... tokens) {
return findCodeTokens(tok, startIdx, ignoreCase, tokens, null);
}
static List findCodeTokens_specials = litlist("*", "", "", "", "\\*");
static boolean findCodeTokens_debug;
static int findCodeTokens_indexed, findCodeTokens_unindexed;
static int findCodeTokens_bails, findCodeTokens_nonbails;
static int findCodeTokens(List tok, int startIdx, boolean ignoreCase, String[] tokens, Object condition) {
if (findCodeTokens_debug) {
if (eq(getClassName(tok), "main$IndexedList2"))
findCodeTokens_indexed++;
else
findCodeTokens_unindexed++;
}
// bail out early if first token not found (works great with IndexedList)
if (!findCodeTokens_specials.contains(tokens[0])
&& !tok.contains(tokens[0] /*, startIdx << no signature in List for this, unfortunately */)) {
++findCodeTokens_bails;
return -1;
}
++findCodeTokens_nonbails;
outer: for (int i = startIdx | 1; i+tokens.length*2-2 < tok.size(); i += 2) {
for (int j = 0; j < tokens.length; j++) {
String p = tokens[j], t = tok.get(i+j*2);
boolean match;
if (eq(p, "*")) match = true;
else if (eq(p, "")) match = isQuoted(t);
else if (eq(p, "")) match = isIdentifier(t);
else if (eq(p, "")) match = isInteger(t);
else if (eq(p, "\\*")) match = eq("*", t);
else match = ignoreCase ? eqic(p, t) : eq(p, t);
if (!match)
continue outer;
}
if (condition == null || checkCondition(condition, tok, i-1)) // pass N index
return i;
}
return -1;
}
static void copyAllFields(Object x, Object y) {
copyFields(x, y);
}
static void sort(T[] a, Comparator super T> c) {
Arrays.sort(a, c);
}
static void sort(T[] a) {
Arrays.sort(a);
}
static void sort(List a, Comparator super T> c) {
Collections.sort(a, c);
}
static void sort(List a) {
Collections.sort(a);
}
static void copyStreamAndCloseInput(InputStream in, OutputStream out) { try {
try {
copyStream(in, out);
} finally {
in.close();
}
} catch (Exception __e) { throw rethrow(__e); } }
static String repeat(char c, int n) {
n = Math.max(n, 0);
char[] chars = new char[n];
for (int i = 0; i < n; i++)
chars[i] = c;
return new String(chars);
}
static List repeat(A a, int n) {
n = Math.max(n, 0);
List l = new ArrayList(n);
for (int i = 0; i < n; i++)
l.add(a);
return l;
}
static List repeat(int n, A a) {
return repeat(a, n);
}
static void clear(Collection c) {
if (c != null) c.clear();
}
static List ai_triples(String a, String b, String c) {
return ai_filterTriples(a, b, c, ai_thoughtSpace_queryTriples(a, b, c, thoughtSpace()));
}
static List ai_triples(String a, String b, String c, AbstractThoughtSpace ts) {
return ai_filterTriples(a, b, c, ai_thoughtSpace_queryTriples(a, b, c, ts));
}
static List web_search_dollarX(Web searchWeb, Web web) {
return webs_search_dollarX(searchWeb, ll(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 AbstractThoughtSpace thoughtSpace() {
return currentThoughtSpace();
}
// set for current thread
static A thoughtSpace(A ts) {
currentThoughtSpace_value.set(ts);
return ts;
}
static boolean swic(String a, String b) {
return startsWithIgnoreCase(a, b);
}
static boolean swic(String a, String b, Matches m) {
if (!swic(a, b)) return false;
m.m = new String[] {substring(a, l(b))};
return true;
}
static MultiMap cachedNodeIndex() {
return cachedNodeIndex2();
}
static void copyFields(Object x, Object y, String... fields) {
if (empty(fields)) { // assume we should copy all fields
Map map = objectToMap(x);
for (String field : map.keySet())
setOpt(y, field, map.get(field));
} else
for (String field : fields) {
Object o = getOpt(x, field);
if (o != null)
setOpt(y, field, o);
}
}
static int indexOfSubList(List x, List y) {
return indexOfSubList(x, y, 0);
}
static int indexOfSubList(List x, List y, int i) {
outer: for (; i+l(y) <= l(x); i++) {
for (int j = 0; j < l(y); j++)
if (neq(x.get(i+j), y.get(j)))
continue outer;
return i;
}
return -1;
}
static int indexOfSubList(List x, A[] y, int i) {
outer: for (; i+l(y) <= l(x); i++) {
for (int j = 0; j < l(y); j++)
if (neq(x.get(i+j), y[j]))
continue outer;
return i;
}
return -1;
}
// supports the usual quotings (", variable length double brackets) except ' quoting
static boolean isQuoted(String s) {
if (isNormalQuoted(s)) return true; // use the exact version
return isMultilineQuoted(s);
}
static boolean checkCondition(Object condition, Object... args) {
return isTrue(callF(condition, args));
}
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 List ai_filterTriples(String a, String b, String c, List l) {
List out = new ArrayList();
for (TripleWeb w : l)
if (canSimpleMatchTriple_dollarVars(a, b, c, w))
out.add(w);
return out;
}
static List ai_triplesMadeByProgramAsTriples(String progID) {
return webs_readTripleFileAsTriples(getProgramFile(progID, "triples.gz"));
}
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 boolean eqic(char a, char b) {
if (a == b) return true;
char u1 = Character.toUpperCase(a);
char u2 = Character.toUpperCase(b);
if (u1 == u2) return true;
return Character.toLowerCase(u1) == Character.toLowerCase(u2);
}
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_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 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 upgradeJavaXAndRestart() {
run("#1001639");
restart();
sleep();
}
static void ai_onNewOrRemovedWeb(Object onNewWeb, Object onRemovedWeb) {
ai_onNewWeb(onNewWeb);
if (onRemovedWeb != null)
onTransientWebRemoved_do(onRemovedWeb);
}
static List ai_thoughtSpace_queryTriples(String a, String b, String c, AbstractThoughtSpace ts) {
List l = null;
if (!isDollarVar(a)) {
l = shortestListUnlessNull2(l, ts.getTriples(a, 0));
if (l(l) <= 1) return l;
}
if (!isDollarVar(b)) {
l = shortestListUnlessNull2(l, ts.getTriples(b, 1));
if (l(l) <= 1) return l;
}
if (!isDollarVar(c)) l = shortestListUnlessNull2(l, ts.getTriples(c, 2));
return l;
}
static List websToTripleWebs(Collection l) {
return map("ai_webToTripleWeb",l);
}
static boolean isIdentifier(String s) {
return isJavaIdentifier(s);
}
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 Set keys(Map map) {
return map == null ? new HashSet() : map.keySet();
}
static Set keys(Object map) {
return keys((Map) map);
}
static Set keys(MultiSet ms) {
return ms.keySet();
}
static Set keys(MultiMap mm) {
return mm.keySet();
}
static 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 void ai_onNewWeb(Object onNewWeb) {
if (onNewWeb == null) return;
setAdd(postSoftwareMadeWeb_onNewWeb, onNewWeb);
onTransientWebAdded_do(onNewWeb);
}
static ThreadLocal currentThoughtSpace_value = new ThreadLocal() {
public AbstractThoughtSpace initialValue() {
return new GlobalThoughtSpace();
}
};
static AbstractThoughtSpace currentThoughtSpace() {
return currentThoughtSpace_value.get();
}
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 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 T3 triple(A a, B b, C c) {
return new T3(a, b, c);
}
static boolean canSimpleMatchTriple_dollarVars(T3 pat, T3 t) {
if (pat == null || t == null) return false;
return simpleMatchTriple_dollarVars(pat.a, pat.b, pat.c, t) != null;
}
static boolean canSimpleMatchTriple_dollarVars(String patA, String patB, String patC, T3 t) {
return simpleMatchTriple_dollarVars(patA, patB, patC, t) != null;
}
// iterate safely (& quickly) in the face of concurrent modifications
static IterableIterator concurrentlyIterateList(final List l) {
return iteratorFromFunction_withEndMarker_f0(new F0 () {
int i;
A get() {
int _i = i++;
synchronized(l) {
if (_i < l(l)) {
A a = l.get(_i);
if (a == iteratorFromFunction_endMarker) throw fail("no"); // ugly comparison fail-fast redemption
return a;
}
return (A) iteratorFromFunction_endMarker; // ugly cast
}
}
});
}
static List 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 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 boolean isDollarVar(String s) {
return startsWith(s, '$') && isJavaIdentifierAfter(s, 1);
}
static TripleWeb ai_webToTripleWeb(Web web) {
T3 t = ai_webToTriple(web);
if (t == null) return null;
TripleWeb w = newTripleWebWithSource(web.source);
w.a = t.a;
w.b = t.b;
w.c = t.c;
w.globalID(web.globalIDObj());
w.created(web.created);
w.flags = web.unverified ? w.UNVERIFIED : 0;
return w;
}
static 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 List shortestListUnlessNull2(List a, List b) {
if (a == null) return b;
if (b == null) return a;
return l(a) < l(b) ? a : b;
}
static Web web_setSourceIfEmpty(Web web, String src) {
if (web == null) return null;
if (empty(web.source)) web.source = src;
return web;
}
static boolean startsWithIgnoreCase(String a, String b) {
return regionMatchesIC(a, 0, b, 0, b.length());
}
static Class run(String progID, String... args) {
Class main = hotwire(progID);
callMain(main, args);
return main;
}
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_207 = cachedNodeIndex2_cache.lock; lock(_lock_207); try {
//print("Incremental update");
ai_addWebToIndex(cachedNodeIndex2_cache.get(), web);
cachedNodeIndex2_cache.changeCount++;
return false; // don't trigger full update
} finally { unlock(_lock_207); } }
static void cachedNodeIndex2_onWebRemoved(Web web) {
if (!cachedNodeIndex_autoMake) return;
Lock _lock_208 = cachedNodeIndex2_cache.lock; lock(_lock_208); try {
print("Incremental removal");
ai_removeWebFromIndex(cachedNodeIndex2_cache.get(), web);
cachedNodeIndex2_cache.changeCount++;
} finally { unlock(_lock_208); } }
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 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) {
String source = intern(unquote(l.get(5)));
TripleWeb t = newTripleWebWithSource(source);
t.a = names.get(parseInt(l.get(0)));
t.b = names.get(parseInt(l.get(1)));
t.c = names.get(parseInt(l.get(2)));
t.globalID(unquote(l.get(3)));
// t.title = unquote(l.get(4)); // unused
t.flags = eq(l.get(6), "v") ? 0 : TripleWeb.UNVERIFIED;
t.created(parseLong(l.get(7)));
return t;
}
return null;
}
static Collection web_relationObjects(Web web) {
List l = new ArrayList();
for (WebNode n : web.nodes)
if (n instanceof WebRelation)
l.add(((WebRelation) n));
return l;
}
static CombinedList combinedList_ll(List l) {
CombinedList cl = new CombinedList();
cl.addList(l);
return cl;
}
static Field setOpt_findField(Class c, String field) {
HashMap map;
synchronized(getOpt_cache) {
map = getOpt_cache.get(c);
if (map == null)
map = getOpt_makeCache(c);
}
return map.get(field);
}
static void setOpt(Object o, String field, Object value) { try {
if (o == null) return;
if (o instanceof _SetField) { ((_SetField) o)._setField(field, value); return; }
Class c = o.getClass();
HashMap map;
synchronized(getOpt_cache) {
map = getOpt_cache.get(c);
if (map == null)
map = getOpt_makeCache(c);
}
if (map == getOpt_special) {
if (o instanceof Class) {
setOpt((Class) o, field, value);
return;
}
// It's probably a subclass of Map. Use raw method
setOpt_raw(o, field, value);
return;
}
Field f = map.get(field);
if (f != null)
smartSet(f, o, value); // possible improvement: skip setAccessible
} catch (Exception __e) { throw rethrow(__e); } }
static void setOpt(Class c, String field, Object value) {
if (c == null) return;
try {
Field f = setOpt_findStaticField(c, field);
if (f != null)
smartSet(f, null, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Field setOpt_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
return null;
}
static boolean allLowerCaseCharacters(String s) {
for (int i = 0; i < l(s); i++)
if (Character.getType(s.charAt(i)) != Character.LOWERCASE_LETTER) return false;
return true;
}
static WebNode web_setPosition(WebNode node, double x, double y) {
node.x = x;
node.y = y;
return node;
}
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 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 asString(Object o) {
return o == null ? null : o.toString();
}
static Web webWithoutIndexAndLock() {
Web web = simpleWeb();
return web;
}
static boolean isNormalQuoted(String s) {
int l = l(s);
if (!(l >= 2 && s.charAt(0) == '"' && lastChar(s) == '"')) return false;
int j = 1;
while (j < l)
if (s.charAt(j) == '"')
return j == l-1;
else if (s.charAt(j) == '\\' && j+1 < l)
j += 2;
else
++j;
return false;
}
static boolean isMultilineQuoted(String s) {
if (!startsWith(s, "[")) return false;
int i = 1;
while (i < s.length() && s.charAt(i) == '=') ++i;
return i < s.length() && s.charAt(i) == '[';
}
static 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 void restart() {
Object j = getJavaX();
call(j, "cleanRestart", get(j, "fullArgs"));
}
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 A second(List l) {
return get(l, 1);
}
static A second(A[] bla) {
return bla == null || bla.length <= 1 ? null : bla[1];
}
static B second(Pair p) {
return p == null ? null : p.b;
}
static B second(T3 t) {
return t == null ? null : t.b;
}
static void mapPut(Map map, A key, B value) {
if (map != null && key != null && value != null) map.put(key, value);
}
// o is either a map already (string->object) or an arbitrary object,
// in which case its fields are converted into a map.
static Map objectToMap(Object o) { try {
if (o instanceof Map) return (Map) o;
TreeMap map = new TreeMap();
Class c = o.getClass();
while (c != Object.class) {
Field[] fields = c.getDeclaredFields();
for (final Field field : fields) {
if ((field.getModifiers() & Modifier.STATIC) != 0)
continue;
field.setAccessible(true);
final Object value = field.get(o);
if (value != null)
map.put(field.getName(), value);
}
c = c.getSuperclass();
}
return map;
} catch (Exception __e) { throw rethrow(__e); } }
// same for a collection (convert each element)
static List> objectToMap(Collection l) {
List x = new ArrayList();
for (Object o : l)
x.add(objectToMap(o));
return x;
}
static Web web_unstructure(String s) {
//ret ((Web) unstructure(s)).afterLoad();
return (Web) unstructure(s);
}
static boolean match(String pat, String s) {
return match3(pat, s);
}
static boolean match(String pat, String s, Matches matches) {
return match3(pat, s, matches);
}
static ArrayList asList(A[] a) {
return a == null ? new ArrayList () : new ArrayList (Arrays.asList(a));
}
static ArrayList asList(int[] a) {
ArrayList l = new ArrayList();
for (int i : a) l.add(i);
return l;
}
static ArrayList asList(Iterable s) {
if (s instanceof ArrayList) return (ArrayList) s;
ArrayList l = new ArrayList();
if (s != null)
for (A a : s)
l.add(a);
return l;
}
static ArrayList asList(Enumeration e) {
ArrayList l = new ArrayList();
if (e != null)
while (e.hasMoreElements())
l.add(e.nextElement());
return l;
}
static String web_text(WebNode node) {
return node == null ? null : node.text();
}
static List concatLists_conservative(List a, List b) {
if (empty(a)) return b;
if (empty(b)) return a;
return concatLists(a, b);
}
static MultiMap ai_emptyWebIndex() {
MultiMap index = caseInsensitiveSyncListMultiMap();
makeMultiMapsInnerNavigableMapSynchronized(index);
return index;
}
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 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 FileStatus conceptsFileStatus(String progID) {
return fileStatus(conceptsFile(progID));
}
// nodeMatcher: func(WebNode, WebNode) -> double
static Map web_matchAllPerms_x(Web patternWeb, Web inputWeb, Object nodeMatcher) {
List patternNodes = web_nodes(patternWeb);
List inputNodes = web_nodes(inputWeb);
if (l(patternNodes) != l(inputNodes))
return null;
//fail("Can't match, differing number of nodes: " + l(patternNodes) + "/" + l(inputNodes));
List> perms = allPermutations(inputNodes);
Best < List < WebNode > > best = new Best();
for (List perm : perms)
best.put(perm, web_matchNodeLists_x(patternNodes, perm, nodeMatcher));
List l = best.getIfScoreAbove(0);
return l == null ? null : twoListsToOrderedMap(patternNodes, l);
}
static 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