Warning : session_start(): open(/var/lib/php/sessions/sess_45od2dulss235heihbbd2pj29d, 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 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.*;
public class main {
// A concept should be an object, not just a string.
// By default, we will LOAD (from program's dir), but not SAVE.
// Call saveConcepts() to save.
static interface Derefable {
Concept get();
}
static class Concept extends DynamicObject {
long id;
//double energy;
//bool defunct;
long created;
// used only internally (cnew)
Concept(String className) {
super(className);
_created();
}
Concept() {
className = shortClassName(this);
if (!DynamicObject_loading) {
//print("New concept of type " + className);
_created();
}
}
List[ refs = new ArrayList][();
List][ backRefs = new ArrayList][();
void _created() {
created = now();
if (!isTrue(_unlisted.get())) register();
}
void put(String field, Object value) {
fieldValues.put(field, value);
change();
}
Object get(String field) {
return fieldValues.get(field);
}
class Ref] implements Derefable {
A value;
Ref() {
if (!DynamicObject_loading) refs.add(this);
}
Ref(A value) {
this.value = value;
refs.add(this);
index();
}
// get owning concept (source)
Concept concept() { return Concept.this; }
// get target
public A get() { return value; }
boolean has() { return value != null; }
boolean empty() { 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.add(this);
change();
}
void unindex() {
if (value != null)
value.backRefs.remove(this);
}
public String toString() {
return str(get());
}
}
class RefL extends AbstractList {
List[> l = new ArrayList][>();
public A set(int i, A o) {
A prev = l.get(i).get();
l.get(i).set(o);
change();
return prev;
}
public void add(int i, A o) {
l.add(i, new Ref(o));
change();
}
public A get(int i) {
return l.get(i).get();
}
public A remove(int i) {
A a = l.remove(i).get();
change();
return a;
}
public int size() {
return l.size();
}
// derefs
void addAll_(List l) {
for (Object x : l) add_(x);
}
// derefs
void add_(Object o) {
add((A) deref(o));
}
}
void delete() {
if (id == 0) return;
concepts.remove(id);
conceptsByClass.get(getClass()).remove(id);
id = 0;
//name = "[defunct " + name + "]";
//defunct = true;
//energy = 0;
for (Ref r : refs)
r.unindex();
refs.clear();
change();
}
// register a previously unlisted concept
void register() {
if (id != 0) return; // already registered
id = newID();
concepts.put(id, this);
_addToClassMap();
change();
}
void _addToClassMap() {
Class c = getClass();
Map] map = conceptsByClass.get(c);
if (map == null) {
// we're the first object, register this class
Class superclass = c.getSuperclass();
if (superclass != Concept.class)
subclasses.put(superclass, c);
conceptsByClass.put(c, map = new TreeMap());
}
map.put(id, this);
}
} // class Concept
// for inter-process communication
// prepared for string ids if we do them later
static class PassRef {
String id;
PassRef() {} // make serialisation happy
PassRef(long id) { this.id = str(id); }
PassRef(Concept c) { this(c.id); }
long longID() { return parseLong(id); }
public String toString() {
return id;
}
}
static class Event extends Concept {}
// This can be set by client
static boolean concepts_quietSave;
static Map concepts = synchroTreeMap();
static Map> conceptsByClass = synchroHashMap();
static MultiMap subclasses = new MultiMap();
static long idCounter;
static volatile long changes = 1, changesWritten;
static volatile java.util.Timer autoSaver;
static volatile boolean savingConcepts;
static ThreadLocal _unlisted = new ThreadLocal();
static boolean concepts_saveOneMore;
static String conceptsLoadedFrom;
static int concepts_internStringsLongerThan = 10;
static int concepts_autoSaveInterval = 1000;
static long newID() {
do {
++idCounter;
} while (hasConcept(idCounter));
return idCounter;
}
static synchronized void loadConceptsFrom(String programID) {
conceptsLoadedFrom = programID;
clearConcepts();
DynamicObject_loading = true;
try {
structure_internStringsLongerThan = concepts_internStringsLongerThan;
readLocally(programID, "concepts");
readLocally(programID, "idCounter");
assignConceptsToUs();
} finally {
DynamicObject_loading = false;
}
}
static void assignConceptsToUs() {
for (Concept c : values(concepts)) {
c._addToClassMap();
callOpt(c, "_doneLoading2");
}
}
static synchronized void loadConcepts() {
loadConceptsFrom(programID());
}
static Concept getConcept(String id) {
return empty(id) ? null : getConcept(parseLong(id));
}
static Concept getConcept(long id) {
return (Concept) concepts.get((long) id);
}
static Concept getConcept(PassRef ref) {
return ref == null ? null : getConcept(ref.longID());
}
static boolean hasConcept(long id) {
return concepts.containsKey((long) id);
}
static boolean hasConcept(Class c, Object... params) {
return conceptWhere(c, params) != null;
}
static Concept deleteConcept(long id) {
Concept c = getConcept(id);
if (c == null)
print("Concept " + id + " not found");
else
c.delete();
return c;
}
// call in only one thread
static void saveConceptsIfDirty() {
saveConceptsIfDirty(programID());
}
static void saveConceptsIfDirty(String programID) {
savingConcepts = true;
try {
String s;
synchronized(main.class) {
boolean change = changes != changesWritten;
if (!change && !concepts_saveOneMore) return;
changesWritten = changes;
concepts_saveOneMore = change;
save(programID, "idCounter");
s = structure(cloneMap(concepts));
}
if (!concepts_quietSave)
print("Saving " + l(s) + " chars (" + changesWritten + ")");
saveTextFile(getProgramFile(programID, "concepts.structure"), s);
copyFile(getProgramFile(programID, "concepts.structure"), getProgramFile(programID, "concepts.structure.backup" + ymd() + "-" + formatInt(hours(), 2)));
} finally {
savingConcepts = false;
}
}
static void saveConcepts() {
saveConceptsIfDirty();
}
static void saveConceptsTo(String programID) {
saveConceptsIfDirty(programID);
}
static void clearConcepts() {
concepts.clear();
conceptsByClass.clear();
subclasses.clear();
change();
}
static synchronized void change() {
++changes;
}
// auto-save every second if dirty
static synchronized void autoSaveConcepts() {
if (autoSaver == null)
autoSaver = doEvery(concepts_autoSaveInterval, new Runnable() { public void run() { try { saveConcepts();
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } });
}
static void cleanMeUp() {
if (autoSaver != null) {
autoSaver.cancel();
autoSaver = null;
}
while (savingConcepts) sleep(10);
saveConceptsIfDirty();
}
static Map getIDsAndNames() {
Map map = new HashMap();
Map cloned = cloneMap(concepts);
for (long id : keys(cloned))
map.put(id, cloned.get(id).className);
return map;
}
static A findBackRef(Concept c, Class type) {
return findBackRefOfType(c, type);
}
static A findBackRefOfType(Concept c, Class type) {
for (Concept.Ref r : c.backRefs)
if (instanceOf(r.concept(), type))
return (A) r.concept(); return null;
}
static List findBackRefs(Concept c, Class type) {
List l = new ArrayList ();
for (Concept.Ref r : c.backRefs)
if (instanceOf(r.concept(), type))
l.add((A) r.concept());
return l;
}
static A conceptOfType(Class type) {
return firstOfType(allConcepts(), type);
}
static List conceptsOfType(Class type) {
//ret filterByType(allConcepts(), type);
List l = new ArrayList ();
_collectInstances(type, l);
return l;
}
static void _collectInstances(Class c, List l) {
l.addAll(values(conceptsByClass.get(c)));
for (Class sub : subclasses.get(c))
_collectInstances(sub, l);
}
static List listConcepts(Class type) {
return conceptsOfType(type);
}
static List list(String type, Object... params) {
return empty(params) ? conceptsOfType(type)
: conceptsWhere(type, params);
}
static List list(Class type, Object... params) {
return empty(params) ? conceptsOfType(type)
: conceptsWhere(type, params);
}
static List conceptsOfType(String type) {
return filterByDynamicType(allConcepts(), "main$" + type);
}
static boolean hasConceptOfType(Class extends Concept> type) {
return hasType(allConcepts(), type);
}
static void persistConcepts() {
concepts_quietSave = true;
loadConcepts();
autoSaveConcepts();
}
static void loadAndAutoSaveConcepts() {
persistConcepts();
}
// We love synonyms
static void conceptPersistence() {
persistConcepts();
}
// Runs r if there is no concept of that type
static 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)
static 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)
static void forEvery(Class extends Concept> type, Object func) {
for (Concept c : conceptsOfType(type))
callF(func, c);
}
static int deleteAll(Class extends Concept> type) {
List l = (List) conceptsOfType(type);
for (Concept c : l) c.delete();
return l(l);
}
static Collection allConcepts() {
synchronized(concepts) {
return new ArrayList(values(concepts));
}
}
static Concept cnew(String name, Object... values) {
Class extends Concept> cc = findClass(name);
Concept c = cc != null ? nuObject(cc) : new Concept(name);
csetAll(c, values);
return c;
}
static A cnew(Class cc, Object... values) {
A c = nuObject(cc);
csetAll(c, values);
return c;
}
static void csetAll(Concept c, Object... values) {
cset(c, values);
}
static void cset(Concept c, Object... values) { try {
values = expandParams(c.getClass(), values);
warnIfOddCount(values);
for (int i = 0; i+1 < l(values); i += 2) {
String field = (String) values[i];
Object value = values[i+1];
Field f = setOpt_findField(c.getClass(), field);
//print("cset: " + c.id + " " + field + " " + struct(value) + " " + f);
if (value instanceof PassRef) value = getConcept((PassRef) value);
value = deref(value);
if (value instanceof String && l((String) value) >= concepts_internStringsLongerThan) value = ((String) value).intern();
if (f == null)
c.fieldValues.put(field, value instanceof Concept ? c.new Ref((Concept) value) : value);
else if (isSubtypeOf(f.getType(), Concept.Ref.class))
((Concept.Ref) f.get(c)).set((Concept) value);
else
f.set(c, value);
}
change();
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static Object cget(Concept c, String field) {
Object o = getOptDyn(c, field);
if (o instanceof Concept.Ref) return ((Concept.Ref) o).get();
return o;
}
static PassRef toPassRef(Concept c) {
return new PassRef(c);
}
// inter-process methods
static PassRef xnew(String name, Object... values) {
return new PassRef(cnew(name, values));
}
static void xset(long id, String field, Object value) {
xset(new PassRef(id), field, value);
}
static void xset(PassRef c, String field, Object value) {
if (value instanceof PassRef)
value = getConcept((PassRef) value);
cset(getConcept(c), field, value);
}
// get class name of concept
static Object xclass(PassRef id) {
Concept c = getConcept(id);
return c == null ? null : c.className;
}
static Object xget(long id, String field) {
return xget(new PassRef(id), field);
}
static Object xget(PassRef c, String field) {
return export(cget(getConcept(c), field));
}
static void xdelete(long id) {
xdelete(new PassRef(id));
}
static void xdelete(PassRef c) {
getConcept(c).delete();
}
static List xlist() {
return map("toPassRef", allConcepts());
}
static List xlist(String className) {
return map("toPassRef", conceptsOfType(className));
}
// end of IPC methods
// make concept instance that is not connected to DB
static A unlisted(Class c) {
_unlisted.set(true);
try {
return nuObject(c);
} finally {
_unlisted.set(null);
}
}
static A unary(Class c) {
A a = conceptOfType(c);
if (a == null)
a = nuObject(c);
return a;
}
static Android3 makeDBBot(String name) {
return makeBot(name, makeDBResponder());
}
static List exposedDBMethods = ll("xlist", "xnew", "xset", "xdelete", "xget", "xclass");
static Object makeDBResponder() {
return new Object() {
String answer(String s) {
return exposeMethods(s, exposedDBMethods);
}
};
}
static Object export(Object o) {
if (o instanceof Concept)
return new PassRef(((Concept) o).id);
return o;
}
static synchronized void saveConceptsBack() {
saveConceptsTo(assertNotNull(conceptsLoadedFrom));
}
static boolean eq(Object a, Object b) {
return _eq(deref(a), deref(b));
} // Concepts
static class Imperative extends Concept {
Ref v = new Ref(); // verb
Ref o = new Ref(); // object
static String _fieldOrder = "v o";
}
static class Measurement extends Concept {
Ref x = new Ref(); // e.g. "all"
Ref y = new Ref(); // e.g. "programs"
}
static class Addition extends Concept {
Ref x = new Ref(); // e.g. "programs"
Ref prep = new Ref(); // preposition, e.g. "with"
Ref z = new Ref(); // e.g. "a large number of files"
static String _fieldOrder = "x prep z";
}
public static void main(String[] args) throws Exception {
testParse("Find all programs with a file called \"concepts.structure\"");
}
static boolean properSentence(Concept c) {
if (!(c instanceof Imperative) || neq(((Imperative) c).v, cstr("find"))) return false;
c = ((Imperative) c).o.get();
return matchCstr("all programs with a file called *", c);
//if (!c << Measurement || neq(c/Measurement.x, cstr("all"))) false;
//c = c/Measurement.y;
}
static void testParse(String s) {
print(("** " + (s) + " **"));
print();
List l = unrollOptions(parseToOptions(s));
for (int i = 0; i < l(l); i++) {
Concept x = l.get(i);
boolean prop = properSentence(x);
print((prop ? "! " : "") + (i+1) + ". " + structureConcept(x));
}
print();
}
static Concept parseToOptions(String s) {
List l = new ArrayList();
parse(s, listCollector(l));
return options(l);
} // testParse + parseToOptions using unrollOptions
static void parse(String s, Collector out) {
Matches m = new Matches();
if (flexMatchStartIC(ll("find"), s, m))
{ if (out.add(cnew(Imperative.class,
"v" , cstr(m.unq(0)),
"o" , parseToOptions(m.unq(1))))) return; }
if (flexMatchStartIC(ll("all", "a"), s, m))
{ if (out.add(cnew(Measurement.class,
"x" , cstr(m.unq(0)),
"y" , parseToOptions(m.unq(1))))) return; }
for (String prep : ll("called", "with"))
if (flexMatchIC(("* " + (prep) + " *"), s, m))
{ if (out.add(cnew(Addition.class,
"x" , parseToOptions(m.unq(0)),
"prep" , cstr(prep),
"z" , parseToOptions(m.unq(1))))) return; }
out.add(cstr(s)); // default
}
static A conceptWhere(Class c, Object... params) {
return findConceptWhere(c, params);
}
static ArrayList ll(A... a) {
return litlist(a);
}
static Class> getClass(String name) {
try {
return Class.forName(name);
} catch (ClassNotFoundException e) {
return null;
}
}
static Class getClass(Object o) {
return o instanceof Class ? (Class) o : o.getClass();
}
static Class getClass(Object realm, String name) { try {
try {
return getClass(realm).getClassLoader().loadClass(classNameToVM(name));
} catch (ClassNotFoundException e) {
return null;
}
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static String formatInt(int i, int digits) {
return padLeft(str(i), '0', digits);
}
static Object callF(Object f, Object... args) {
return callFunction(f, args);
}
// firstDelay = delay
static java.util.Timer doEvery(int delay, Runnable r) {
java.util.Timer timer = new java.util.Timer();
timer.scheduleAtFixedRate(timerTask(r), delay, delay);
return timer;
}
static boolean empty(Collection c) {
return isEmpty(c);
}
static boolean empty(String s) {
return isEmpty(s);
}
static boolean empty(Map map) {
return map == null || map.isEmpty();
}
static boolean empty(Object[] o) {
return o == null || o.length == 0;
}
static boolean empty(Object o) {
if (o instanceof Collection) return empty((Collection) o);
if (o instanceof String) return empty((String) o);
if (o instanceof Map) return empty((Map) o);
if (o instanceof Object[]) return empty((Object[]) o);
throw fail("unknown type for 'empty': " + getType(o));
}
static boolean instanceOf(Object o, String className) {
if (o == null) return false;
String c = o.getClass().getName();
return eq(c, className) || eq(c, "main$" + className);
}
static boolean instanceOf(Object o, Class c) {
if (c == null) return false;
return c.isInstance(o);
}
static A assertNotNull(A a) {
assertTrue(a != null);
return a;
}
static A assertNotNull(String msg, A a) {
assertTrue(msg, a != null);
return a;
}
static Concept options(List l) {
return l(l) == 1 ? first(l) : new Options(l);
}
static boolean flexMatchIC_debug;
static boolean flexMatchIC(String pat, String s) {
return flexMatchIC(pat, s, null);
}
static boolean flexMatchIC(String pat, String s, Matches m) {
List tokpat = codeTokens(javaTok(pat));
List tokfull = joinBrackets(javaTok(s));
List tok = codeTokens(tokfull);
BitSet bla = new BitSet();
if (!flexMatchIC_impl(tokpat, 0, tok, 0, bla)) return false;
if (m != null) {
List l = new ArrayList();
for (int i = 1; i < l(tokfull); i += 2) {
if (bla.get(i/2)) {
int j = i;
while (j < l(tokfull) && bla.get(j/2)) j += 2;
l.add(join(subList(tokfull, i, j-1)));
i = j-2;
}
}
m.m = toStringArray(l);
}
return true;
}
static boolean flexMatchIC_impl(List pat, int ipat, List tok, int itok, BitSet bla) {
if (flexMatchIC_debug)
print("flexMatchIC " + structure(subList(pat, ipat)) + " " + structure(subList(tok, itok)) + " " + structure(bla));
if (ipat >= l(pat))
return itok >= l(tok);
String t = pat.get(ipat);
if (eq(t, "*"))
if (itok >= l(tok)) // nothing to consume - just advance in pattern
return flexMatchIC_impl(pat, ipat+1, tok, itok, bla);
else { // something to consume - try both continuing and ending to fill "*"
bla.set(itok);
if (flexMatchIC_impl(pat, ipat, tok, itok+1, bla)
|| flexMatchIC_impl(pat, ipat+1, tok, itok+1, bla))
return true; // success, leave mark
bla.clear(itok); // fail, undo marking
return false;
}
if (itok >= l(tok)) {
if (flexMatchIC_debug)
print("too much pattern");
return false;
}
if (neqic(tok.get(itok), t)) {
if (flexMatchIC_debug)
print("mismatch");
return false;
}
// ok, consume and proceed
return flexMatchIC_impl(pat, ipat+1, tok, itok+1, bla);
}
static boolean neq(Object a, Object b) {
return !eq(a, b);
}
// this one could surely be optimized
static boolean flexMatchStartIC(List patterns, String s, Matches m) {
for (String pat : patterns)
if (flexMatchIC(pat + " *", s, m)) {
m.m = toStringArray(concatLists(ll(pat), asList(m.m))); return true;
} return false;
}
static List filterByDynamicType(Collection c, String type) {
List l = new ArrayList ();
for (A x : c)
if (eq(dynamicClassName(x), type))
l.add(x);
return l;
}
// currently finds only inner classes of class "main"
// returns null on not found
// this is the simple version that is not case-tolerant
static Class findClass(String name) {
if (!isJavaIdentifier(name)) return null;
try {
return Class.forName("main$" + name);
} catch (ClassNotFoundException e) {
return null;
}
}
static List unrollOptions(Concept c) {
if (c instanceof Options)
return concatMap("unrollOptions", ((Options) c));
if (!conceptHasOptions(c))
return ll(c);
else { // c has Options hidden somewhere within - recurse & "multiply"
Map simples = new TreeMap();
Map> multiples = new TreeMap();
for (String field : conceptFields(c)) {
Object val = cget(c, field);
if (!(val instanceof Concept))
simples.put(field, val);
else {
List l = unrollOptions(((Concept) val));
if (empty(l)) return ll();
else if (l(l) == 1)
simples.put(field, first(l));
else
multiples.put(field, l);
}
}
final Concept d = unlisted(c.getClass());
csetMap(d, simples);
if (empty(multiples))
return ll(d);
else {
if (l(multiples) > 2) warn("todo johnny");
List out = new ArrayList();
List fields = asList(keys(multiples));
String field0 = fields.get(0);
for (Concept value0 : multiples.get(field0)) {
Concept e = unlisted(c.getClass());
copyConceptFields(d, e);
cset(e, field0, value0);
if (l(multiples) == 1) out.add(e); else {
String field1 = fields.get(1);
for (Concept value1 : multiples.get(field1)) {
Concept f = unlisted(c.getClass());
copyConceptFields(e, f);
cset(f, field1, value1);
out.add(f);
}
}
}
return out;
}
}
}
static String str(Object o) {
return String.valueOf(o);
}
static boolean isSubtypeOf(Class a, Class b) {
return b.isAssignableFrom(a); // << always hated that method, let's replace it!
}
static RuntimeException fail() {
throw new RuntimeException("fail");
}
static RuntimeException fail(Object msg) {
throw new RuntimeException(String.valueOf(msg));
}
static RuntimeException fail(String msg) {
throw new RuntimeException(unnull(msg));
}
// disabled for now to shorten some programs
/*static RuntimeException fail(S msg, O... args) {
throw new RuntimeException(format(msg, args));
}*/
/** writes safely (to temp file, then rename) */
public static void saveTextFile(String fileName, String contents) throws IOException {
File file = new File(fileName);
File parentFile = file.getParentFile();
if (parentFile != null)
parentFile.mkdirs();
String tempFileName = fileName + "_temp";
File tempFile = new File(tempFileName);
if (contents != null) {
if (tempFile.exists()) try {
String saveName = tempFileName + ".saved." + now();
copyFile(tempFile, new File(saveName));
} catch (Throwable e) { printStackTrace(e); }
FileOutputStream fileOutputStream = new FileOutputStream(tempFile.getPath());
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
PrintWriter printWriter = new PrintWriter(outputStreamWriter);
printWriter.print(contents);
printWriter.close();
}
if (file.exists() && !file.delete())
throw new IOException("Can't delete " + fileName);
if (contents != null)
if (!tempFile.renameTo(file))
throw new IOException("Can't rename " + tempFile + " to " + file);
}
public static void saveTextFile(File fileName, String contents) {
try {
saveTextFile(fileName.getPath(), contents);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
static Object nuObject(String className, Object... args) { try {
return nuObject(Class.forName(className), args);
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
// too ambiguous - maybe need to fix some callers
/*static O nuObject(O realm, S className, O... args) {
ret nuObject(_getClass(realm, className), args);
}*/
static A nuObject(Class c, Object... args) { try {
Constructor m = nuObject_findConstructor(c, args);
m.setAccessible(true);
return (A) m.newInstance(args);
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static Constructor nuObject_findConstructor(Class c, Object... args) {
for (Constructor m : c.getDeclaredConstructors()) {
if (!nuObject_checkArgs(m.getParameterTypes(), args, false))
continue;
return m;
}
throw new RuntimeException("Constructor with " + args.length + " matching parameter(s) not found in " + c.getName());
}
static boolean nuObject_checkArgs(Class[] types, Object[] args, boolean debug) {
if (types.length != args.length) {
if (debug)
System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
return false;
}
for (int i = 0; i < types.length; i++)
if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
if (debug)
System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
return false;
}
return true;
}
public static void copyFile(File src, File dest) { try {
mkdirsForFile(dest);
FileInputStream inputStream = new FileInputStream(src.getPath());
FileOutputStream outputStream = new FileOutputStream(dest.getPath());
try {
copyStream(inputStream, outputStream);
inputStream.close();
} finally {
outputStream.close();
}
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static Str cstr(String name) {
return concept(name);
}
static String structureConcept(Object o) {
HashSet refd = new HashSet();
StringBuilder out = new StringBuilder();
structureConcept_1(out, o, new structureConcept_Data(refd));
return structureConcept_2(str(out), refd);
}
// leave to false, unless unstructure() breaks
static boolean structureConcept_allowShortening = false;
static int structureConcept_shareStringsLongerThan = 20;
static class structureConcept_Data {
int stringSizeLimit;
IdentityHashMap seen = new IdentityHashMap();
HashSet refd;
HashMap strings = new HashMap();
HashSet concepts = new HashSet();
Class conceptClass = findClass("Concept");
boolean markConcepts;
Map unlisted = new TreeMap(); // unlisted empty instances to check default field values
structureConcept_Data(HashSet refd) {
this.refd = refd;}
}
static void structureConcept_1(StringBuilder out, Object o, structureConcept_Data d) {
if (o == null) { out.append("null"); return; }
// these are never back-referenced (for readability)
if (o instanceof BigInteger) {
out.append("bigint(").append(o).append(")"); return;
}
if (o instanceof Double) {
out.append("d(").append(quote(str(o))).append(")"); return;
}
if (o instanceof Float) {
out.append("fl ").append(quote(str(o))); return;
}
if (o instanceof Long) {
out.append(o).append("L"); return;
}
if (o instanceof Integer) {
out.append(str(o)); return;
}
if (o instanceof Boolean) {
out.append(((Boolean) o).booleanValue() ? "t" : "f"); return;
}
if (o instanceof Character) {
out.append(quoteCharacter((Character) o)); return;
}
if (o instanceof File) {
out.append("File ").append(quote(((File) o).getPath())); return;
}
// referencable objects follow
Integer ref = d.seen.get(o);
if (ref != null) {
d.refd.add(ref);
out.append("r").append(ref); return;
}
if (o instanceof String && (ref = d.strings.get((String) o)) != null) {
d.refd.add(ref);
out.append("r").append(ref); return;
}
ref = d.seen.size()+1;
d.seen.put(o, ref);
out.append("m").append(ref).append(" "); // marker
if (o instanceof String) {
String s = d.stringSizeLimit != 0 ? shorten((String) o, d.stringSizeLimit) : (String) o;
if (l(s) >= structureConcept_shareStringsLongerThan)
d.strings.put(s, ref);
out.append(quote(s)); return;
}
String name = o.getClass().getName();
if (o instanceof HashSet) {
out.append("hashset ");
structureConcept_1(out, new ArrayList((Set) o), d);
return;
}
if (o instanceof TreeSet) {
out.append("treeset ");
structureConcept_1(out, new ArrayList((Set) o), d);
return;
}
if (o instanceof Collection && neq(name, "main$Concept$RefL")) {
if (!(o instanceof ArrayList))
out.append(shortDynamicClassName(o));
out.append("[");
int l = out.length();
for (Object x : (Collection) o) {
if (out.length() != l) out.append(", ");
structureConcept_1(out, deref(x), d);
}
out.append("]");
return;
}
if (o instanceof Map) {
if (o instanceof HashMap) out.append("hm");
out.append("{");
int l = out.length();
for (Object e : ((Map) o).entrySet()) {
if (out.length() != l) out.append(", ");
structureConcept_1(out, ((Map.Entry) e).getKey(), d);
out.append("=");
structureConcept_1(out, ((Map.Entry) e).getValue(), d);
}
out.append("}");
return;
}
if (o.getClass().isArray()) {
if (o instanceof byte[]) {
out.append("ba ").append(quote(bytesToHex((byte[]) o))); return;
}
int n = Array.getLength(o);
if (o instanceof boolean[]) {
String hex = boolArrayToHex((boolean[]) o);
int i = l(hex);
while (i > 0 && hex.charAt(i-1) == '0' && hex.charAt(i-2) == '0') i -= 2;
out.append("boolarray ").append(n).append(" ").append(quote(substring(hex, 0, i))); return;
}
String atype = "array", sep = ", ";
if (o instanceof int[]) {
//ret "intarray " + quote(intArrayToHex((int[]) o));
atype = "intarray";
sep = " ";
}
out.append(atype).append("{");
for (int i = 0; i < n; i++) {
if (i != 0) out.append(sep);
structureConcept_1(out, Array.get(o, i), d);
}
out.append("}"); return;
}
if (o instanceof Class) {
out.append("class(").append(quote(((Class) o).getName())).append(")"); return;
}
if (o instanceof Throwable) {
out.append("exception(").append(quote(((Throwable) o).getMessage())).append(")"); return;
}
if (o instanceof BitSet) {
BitSet bs = (BitSet) o;
out.append("bitset{");
int l = out.length();
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
if (out.length() != l) out.append(", ");
out.append(i);
}
out.append("}"); return;
}
// Need more cases? This should cover all library classes...
if (name.startsWith("java.") || name.startsWith("javax.")) {
out.append(str(o)); return;
}
String shortName = o.getClass().getName().replaceAll("^main\\$", "");
if (shortName.equals("Lisp")) {
out.append("l(");
structureConcept_1(out, getOpt(o, "head"), d);
List args = (List) ( getOpt(o, "args"));
if (nempty(args))
for (int i = 0; i < l(args); i++) {
out.append(", ");
Object arg = args.get(i);
// sweet shortening
if (arg != null && eq(arg.getClass().getName(), "main$Lisp") && isTrue(call(arg, "isEmpty")))
arg = get(arg, "head");
structureConcept_1(out, arg, d);
}
out.append(")"); return;
}
Class c = o.getClass();
boolean concept = d.conceptClass != null && d.conceptClass.isInstance(o);
String dynName = shortDynamicClassName(o);
if (concept && !d.concepts.contains(dynName)) {
d.concepts.add(dynName);
if (d.markConcepts)
out.append("c ");
}
// serialize an object with fields.
// first, collect all fields and values in fv.
Map fv = new TreeMap();
while (c != Object.class) {
for (Field field : c.getDeclaredFields()) {
if ((field.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) != 0)
continue;
String fieldName = field.getName();
Object value;
try {
field.setAccessible(true);
value = field.get(o);
} catch (Exception e) {
value = "?";
}
// omit field "className" if equal to class's name
if (concept && eq(fieldName, "className")
&& eq(value, shortName)) value = null;
// put special cases here...
if (value != null)
fv.put(fieldName, value);
}
c = c.getSuperclass();
}
// Now we have fields & values. Process fieldValues if it's a DynamicObject.
if (o instanceof DynamicObject) {
fv.putAll((Map) fv.get("fieldValues"));
fv.remove("fieldValues");
shortName = dynName;
fv.remove("className");
}
String singleField = null;
if (o instanceof ConceptL) {
out.append(shortName).append("[");
int l = out.length();
for (Object x : ((ConceptL) o).l) {
if (out.length() != l) out.append(", ");
structureConcept_1(out, deref(x), d);
}
out.append("]");
return;
}
if (o instanceof Str && empty(((Str) o).otherNames)) {
out/*.append("Str ")*/.append(quote(((Str) o).name));
return;
}
if (o instanceof Concept) {
Concept cpt = (Concept) ( o);
Set fields = conceptFields(cpt);
if (l(fields) == 1) singleField = first(fields);
Concept unl = d.unlisted.get(dynName);
if (unl == null)
d.unlisted.put(dynName, unl = unlisted(cpt.getClass()));
fv.clear();
for (String field : fields) {
Object val = cget(cpt, field);
if (val != null && neq(cget(unl, field), val))
fv.put(field, val);
}
}
String fieldOrder = toStringOpt(getOpt(o, "_fieldOrder"));
// Render this$1 first because unstructure needs it for constructor call.
out.append(shortName).append("(");
int l = out.length();
if (fv.containsKey("this$1")) {
out.append("this$1=");
structureConcept_1(out, fv.get("this$1"), d);
fv.remove("this$1");
}
// Render the other fields.
if (fieldOrder != null)
fv = putKeysFirst(fv, toObjectArray(splitAtSpace(fieldOrder)));
for (String fieldName : keys(fv)) {
if (out.length() != l) out.append(", ");
if (neq(fieldName, singleField))
out.append(fieldName).append("=");
structureConcept_1(out, fv.get(fieldName), d);
}
if (out.length() == l) out.setLength(l-1); // drop "("
else out.append(")");
}
// drop unused markers
static String structureConcept_2(String s, HashSet refd) {
List tok = javaTok(s);
StringBuilder out = new StringBuilder();
for (int i = 1; i < l(tok); i += 2) {
String t = tok.get(i);
if (t.startsWith("m") && isInteger(t.substring(1))
&& !refd.contains(parseInt(t.substring(1))))
continue;
out.append(t).append(tok.get(i+1));
}
return str(out);
}
static Object deref(Object o) {
if (o instanceof Derefable) o = ((Derefable) o).get();
return o;
}
static boolean matchCstr(String s, Concept c, Matches m) {
return c instanceof Str && match(s, str(c), m);
}
static boolean matchCstr(String s, Concept c) {
return matchCstr(s, c, null);
}
static Object callOpt(Object o) {
if (o == null) return null;
return callF(o);
}
static Object callOpt(Object o, String method, Object... args) {
try {
if (o == null) return null;
if (o instanceof Class) {
Method m = callOpt_findStaticMethod((Class) o, method, args, false);
if (m == null) return null;
m.setAccessible(true);
return m.invoke(null, args);
} else {
Method m = callOpt_findMethod(o, method, args, false);
if (m == null) return null;
m.setAccessible(true);
return m.invoke(o, args);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Method callOpt_findStaticMethod(Class c, String method, Object[] args, boolean debug) {
Class _c = c;
while (c != null) {
for (Method m : c.getDeclaredMethods()) {
if (debug)
System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
if (!m.getName().equals(method)) {
if (debug) System.out.println("Method name mismatch: " + method);
continue;
}
if ((m.getModifiers() & 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 Set keys(Map map) {
return map.keySet();
}
static Set keys(Object map) {
return keys((Map) map);
}
static Str concept(String name) {
for (Str s : list(Str.class))
if (eqic(s.name, name) || containsIgnoreCase(s.otherNames, name))
return s;
return new Str(name);
}
static Collection values(Map map) {
return map == null ? emptyList() : map.values();
}
static A firstOfType(Collection c, Class type) {
for (Object x : c)
if (isInstanceX(type, x))
return (A) x;
return null;
}
static void save(String varName) {
saveLocally(varName);
}
static void save(String progID, String varName) {
saveLocally(progID, varName);
}
static void warnIfOddCount(Object... list) {
if (odd(l(list)))
warn("Odd list size: " + struct(list));
}
static boolean _eq(Object a, Object b) {
if (a == null) return b == null;
if (a.equals(b)) return true;
if (a instanceof BigInteger) {
if (b instanceof Integer) return a.equals(BigInteger.valueOf((Integer) b));
if (b instanceof Long) return a.equals(BigInteger.valueOf((Long) b));
}
return false;
}
static Map synchroTreeMap() {
return Collections.synchronizedMap(new TreeMap());
}
static void sleep(long ms) {
ping();
try {
Thread.sleep(ms);
} catch (Exception e) { throw new RuntimeException(e); }
}
static void sleep() { try {
print("Sleeping.");
synchronized(main.class) { main.class.wait(); }
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static String exposeMethods(String s, List methodNames) {
return exposeMethods(s, toStringArray(methodNames)); // TODO: optimize
}
static String exposeMethods(String s, String... methodNames) {
Matches m = new Matches();
if (match("call *", s, m)) {
List l;
if (isIdentifier(m.unq(0)))
l = ll(m.unq(0));
else
l = (List) unstructure(m.unq(0)); // we used to have safeUnstructure here
String method = getString(l, 0);
if (!contains(methodNames, method))
throw fail("Method not allowed: " + method);
return ok(structure(call(mc(), method, asObjectArray(subList(l, 1)))));
} return null;
}
static String ymd() {
return year() + formatInt(month(), 2) + formatInt(dayOfMonth(), 2);
}
// collects into l
// 1 billion entries are enough for everyone
static Collector listCollector(final Collection l) {
return limitedListCollector(l, 1024*1024*1024);
}
// get purpose 1: access a list/array (safer version of x.get(y))
static A get(List l, int idx) {
return idx >= 0 && idx < l(l) ? l.get(idx) : null;
}
static A get(A[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : null;
}
// default to false
static boolean get(boolean[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : false;
}
static Class get_dynamicObject = findClass("DynamicObject");
// get purpose 2: access a field by reflection or a map
static Object get(Object o, String field) {
try {
if (o instanceof Class) return get((Class) o, field);
if (o instanceof Map)
return ((Map) o).get(field);
Field f = getOpt_findField(o.getClass(), field);
if (f != null) {
f.setAccessible(true);
return f.get(o);
}
if (get_dynamicObject != null && get_dynamicObject.isInstance(o))
return call(get_raw(o, "fieldValues"), "get", field);
} catch (Exception e) {
throw asRuntimeException(e);
}
throw new RuntimeException("Field '" + field + "' not found in " + o.getClass().getName());
}
static Object get_raw(Object o, String field) {
try {
Field f = get_findField(o.getClass(), field);
f.setAccessible(true);
return f.get(o);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Object get(Class c, String field) {
try {
Field f = get_findStaticField(c, field);
f.setAccessible(true);
return f.get(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Field get_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field) && (f.getModifiers() & Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
}
static Field get_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
}
static long now_virtualTime;
static long now() {
return now_virtualTime != 0 ? now_virtualTime : System.currentTimeMillis();
}
static Object[] expandParams(Class c, Object[] params) {
if (l(params) == 1)
params = new Object[] { singleFieldName(c), params[0] };
else
warnIfOddCount(params);
return params;
}
static int makeBot(String greeting) {
return makeAndroid3(greeting).port;
}
static void makeBot(Android3 a) {
makeAndroid3(a);
}
static Android3 makeBot(String greeting, Object responder) {
Android3 a = new Android3(greeting);
a.responder = makeResponder(responder);
makeBot(a);
return a;
}
static Android3 makeBot() {
return makeAndroid3(getProgramTitle() + ".");
}
static String shortClassName(Object o) {
if (o == null) return null;
Class c = o instanceof Class ? (Class) o : o.getClass();
String name = c.getName();
return shortenClassName(name);
}
static void readLocally(String progID, String varNames) {
readLocally2(mc(), progID, varNames);
}
static void readLocally(String varNames) {
readLocally2(mc(), programID(), varNames);
}
static void readLocally2(Object obj, String varNames) {
readLocally2(obj, programID(), varNames);
}
// read a string variable from standard storage
// does not overwrite variable contents if there is no file
static synchronized void readLocally2(Object obj, String progID, String varNames) {
for (String variableName : codeTokensOnly(javaTok(varNames))) {
File textFile = new File(programDir(progID), variableName + ".text");
File structureFile = new File(programDir(progID), variableName + ".structure");
String value = loadTextFile(textFile);
if (value != null)
set(main.class, variableName, value);
else {
value = loadTextFile(structureFile);
if (value != null)
readLocally_set(obj, variableName, unstructure(value));
}
}
}
static void readLocally_set(Object c, String varName, Object value) {
Object oldValue = get(c, varName);
if (oldValue instanceof List && !(oldValue instanceof ArrayList) && value != null) {
// Assume it's a synchroList.
value = synchroList((List) value);
}
set(c, varName, value);
}
static Object getOptDyn(DynamicObject o, String field) {
Object val = o.fieldValues.get(field);
return val != null ? val : getOpt(o, field);
}
static Object getOptDyn(Object o, String field) {
return o instanceof DynamicObject ? getOptDyn((DynamicObject) o, field) : getOpt(o, field);
}
static List map(Iterable l, Object f) {
return map(f, l);
}
static List map(Object f, Iterable l) {
List x = new ArrayList();
Object mc = mc();
for (Object o : unnull(l))
x.add(callFunction(f, o));
return x;
}
static List map(Object f, Object[] l) {
return map(f, asList(l));
}
static List map(Map map, Object f) {
List x = new ArrayList();
for (Object _e : map.entrySet()) {
Map.Entry e = (Map.Entry) _e;
x.add(callFunction(f, e.getKey(), e.getValue()));
}
return x;
}
static String programID() {
return getProgramID();
}
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 int hours() {
return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
}
static void set(Object o, String field, Object value) {
if (o instanceof Class) set((Class) o, field, value);
else try {
Field f = set_findField(o.getClass(), field);
smartSet(f, o, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static void set(Class c, String field, Object value) {
try {
Field f = set_findStaticField(c, field);
smartSet(f, null, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Field set_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field) && (f.getModifiers() & Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
}
static Field set_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
}
static Map cloneMap(Map map) {
if (map == null) return litmap();
// assume mutex is equal to collection, which will be true unless you explicitly pass a mutex to synchronizedList() which no one ever does.
synchronized(map) {
return new HashMap(map);
}
}
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 int l(Object o) {
return l((List) o); // incomplete
}
static String structure(Object o) {
HashSet refd = new HashSet();
StringBuilder out = new StringBuilder();
structure_1(out, o, new structure_Data(refd));
return structure_2(str(out), refd);
}
// leave to false, unless unstructure() breaks
static boolean structure_allowShortening = false;
static int structure_shareStringsLongerThan = 20;
static class structure_Data {
int stringSizeLimit;
IdentityHashMap seen = new IdentityHashMap();
HashSet refd;
HashMap strings = new HashMap();
HashSet concepts = new HashSet();
Class conceptClass = findClass("Concept");
structure_Data(HashSet refd) {
this.refd = refd;}
}
static void structure_1(StringBuilder out, Object o, structure_Data d) {
if (o == null) { out.append("null"); return; }
// these are never back-referenced (for readability)
if (o instanceof BigInteger) {
out.append("bigint(").append(o).append(")"); return;
}
if (o instanceof Double) {
out.append("d(").append(quote(str(o))).append(")"); return;
}
if (o instanceof Float) {
out.append("fl ").append(quote(str(o))); return;
}
if (o instanceof Long) {
out.append(o).append("L"); return;
}
if (o instanceof Integer) {
out.append(str(o)); return;
}
if (o instanceof Boolean) {
out.append(((Boolean) o).booleanValue() ? "t" : "f"); return;
}
if (o instanceof Character) {
out.append(quoteCharacter((Character) o)); return;
}
if (o instanceof File) {
out.append("File ").append(quote(((File) o).getPath())); return;
}
// referencable objects follow
Integer ref = d.seen.get(o);
if (ref != null) {
d.refd.add(ref);
out.append("r").append(ref); return;
}
if (o instanceof String && (ref = d.strings.get((String) o)) != null) {
d.refd.add(ref);
out.append("r").append(ref); return;
}
ref = d.seen.size()+1;
d.seen.put(o, ref);
out.append("m").append(ref).append(" "); // marker
if (o instanceof String) {
String s = d.stringSizeLimit != 0 ? shorten((String) o, d.stringSizeLimit) : (String) o;
if (l(s) >= structure_shareStringsLongerThan)
d.strings.put(s, ref);
out.append(quote(s)); return;
}
String name = o.getClass().getName();
if (o instanceof HashSet) {
out.append("hashset ");
structure_1(out, new ArrayList((Set) o), d);
return;
}
if (o instanceof TreeSet) {
out.append("treeset ");
structure_1(out, new ArrayList((Set) o), d);
return;
}
if (o instanceof Collection && neq(name, "main$Concept$RefL")) {
out.append("[");
int l = out.length();
for (Object x : (Collection) o) {
if (out.length() != l) out.append(", ");
structure_1(out, x, d);
}
out.append("]");
return;
}
if (o instanceof Map) {
if (o instanceof HashMap) out.append("hm");
out.append("{");
int l = out.length();
for (Object e : ((Map) o).entrySet()) {
if (out.length() != l) out.append(", ");
structure_1(out, ((Map.Entry) e).getKey(), d);
out.append("=");
structure_1(out, ((Map.Entry) e).getValue(), d);
}
out.append("}");
return;
}
if (o.getClass().isArray()) {
if (o instanceof byte[]) {
out.append("ba ").append(quote(bytesToHex((byte[]) o))); return;
}
int n = Array.getLength(o);
if (o instanceof boolean[]) {
String hex = boolArrayToHex((boolean[]) o);
int i = l(hex);
while (i > 0 && hex.charAt(i-1) == '0' && hex.charAt(i-2) == '0') i -= 2;
out.append("boolarray ").append(n).append(" ").append(quote(substring(hex, 0, i))); return;
}
String atype = "array", sep = ", ";
if (o instanceof int[]) {
//ret "intarray " + quote(intArrayToHex((int[]) o));
atype = "intarray";
sep = " ";
}
out.append(atype).append("{");
for (int i = 0; i < n; i++) {
if (i != 0) out.append(sep);
structure_1(out, Array.get(o, i), d);
}
out.append("}"); return;
}
if (o instanceof Class) {
out.append("class(").append(quote(((Class) o).getName())).append(")"); return;
}
if (o instanceof Throwable) {
out.append("exception(").append(quote(((Throwable) o).getMessage())).append(")"); return;
}
if (o instanceof BitSet) {
BitSet bs = (BitSet) o;
out.append("bitset{");
int l = out.length();
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
if (out.length() != l) out.append(", ");
out.append(i);
}
out.append("}"); return;
}
// Need more cases? This should cover all library classes...
if (name.startsWith("java.") || name.startsWith("javax.")) {
out.append(str(o)); return;
}
String shortName = o.getClass().getName().replaceAll("^main\\$", "");
if (shortName.equals("Lisp")) {
out.append("l(");
structure_1(out, getOpt(o, "head"), d);
List args = (List) ( getOpt(o, "args"));
if (nempty(args))
for (int i = 0; i < l(args); i++) {
out.append(", ");
Object arg = args.get(i);
// sweet shortening
if (arg != null && eq(arg.getClass().getName(), "main$Lisp") && isTrue(call(arg, "isEmpty")))
arg = get(arg, "head");
structure_1(out, arg, d);
}
out.append(")"); return;
}
Class c = o.getClass();
boolean concept = d.conceptClass != null && d.conceptClass.isInstance(o);
String dynName = shortDynamicClassName(o);
if (concept && !d.concepts.contains(dynName)) {
d.concepts.add(dynName);
out.append("c ");
}
// serialize an object with fields.
// first, collect all fields and values in fv.
TreeMap fv = new TreeMap();
while (c != Object.class) {
for (Field field : c.getDeclaredFields()) {
if ((field.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) != 0)
continue;
String fieldName = field.getName();
Object value;
try {
field.setAccessible(true);
value = field.get(o);
} catch (Exception e) {
value = "?";
}
// omit field "className" if equal to class's name
if (concept && eq(fieldName, "className")
&& eq(value, shortName)) value = null;
// put special cases here...
if (value != null)
fv.put(fieldName, value);
}
c = c.getSuperclass();
}
// Now we have fields & values. Process fieldValues if it's a DynamicObject.
if (o instanceof DynamicObject) {
fv.putAll((Map) fv.get("fieldValues"));
fv.remove("fieldValues");
shortName = dynName;
fv.remove("className");
}
String singleField = fv.size() == 1 ? first(fv.keySet()) : null;
// Render this$1 first because unstructure needs it for constructor call.
out.append(shortName).append("(");
int l = out.length();
if (fv.containsKey("this$1")) {
out.append("this$1=");
structure_1(out, fv.get("this$1"), d);
fv.remove("this$1");
}
// Render the other fields.
for (String fieldName : fv.keySet()) {
if (out.length() != l) out.append(", ");
out.append(fieldName).append("=");
structure_1(out, fv.get(fieldName), d);
}
/*if (structure_allowShortening && singleField != null)
b = b.replaceAll("^" + singleField + "=", ""); // drop field name if only one*/
if (out.length() == l) out.setLength(l-1); // drop "("
else out.append(")");
}
// drop unused markers
static String structure_2(String s, HashSet refd) {
List tok = javaTok(s);
StringBuilder out = new StringBuilder();
for (int i = 1; i < l(tok); i += 2) {
String t = tok.get(i);
if (t.startsWith("m") && isInteger(t.substring(1))
&& !refd.contains(parseInt(t.substring(1))))
continue;
out.append(t).append(tok.get(i+1));
}
return str(out);
}
static List conceptsWhere(Class c, Object... params) {
return findConceptsWhere(c, params);
}
static List conceptsWhere(String c, Object... params) {
return findConceptsWhere(c, params);
}
// hmm, this shouldn't call functions really. That was just
// for coroutines.
static boolean isTrue(Object o) {
if (o instanceof Boolean)
return ((Boolean) o).booleanValue();
if (o == null) return false;
return ((Boolean) callF(o)).booleanValue();
}
static boolean isTrue(Object pred, Object arg) {
return booleanValue(callF(pred, arg));
}
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 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";
// 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);
return o;
}
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 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 Map synchroHashMap() {
return Collections.synchronizedMap(new HashMap());
}
static boolean hasType(Collection c, Class type) {
for (Object x : c)
if (isInstanceX(type, x))
return true;
return false;
}
static String struct(Object o) {
return structure(o);
}
static String toStringOpt(Object o) {
return o instanceof String ? ((String) o) : null;
}
static int year() {
return Calendar.getInstance().get(Calendar.YEAR);
}
static List splitAtSpace(String s) {
return asList(s.split("\\s+"));
}
static Object callFunction(Object f, Object... args) {
if (f == null) return null;
if (f instanceof Runnable) {
((Runnable) f).run();
return null;
} else if (f instanceof String)
return call(mc(), (String) f, args);
else
return call(f, "get", args);
//else throw fail("Can't call a " + getClassName(f));
}
static boolean neqic(String a, String b) {
return !eqic(a, b);
}
static ArrayList asList(A[] a) {
return 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 String quote(Object o) {
if (o == null) return "null";
String s = str(o);
return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"").replace("\r", "\\r").replace("\n", "\\n") + "\"";
}
static String shorten(String s, int max) {
if (s == null) return "";
if (max < 0) return s;
return s.length() <= max ? s : s.substring(0, Math.min(s.length(), max)) + "...";
}
static String quoteCharacter(char c) {
if (c == '\'') return "'\\''";
if (c == '\\') return "'\\\\'";
return "'" + c + "'";
}
public static String join(String glue, Iterable strings) {
StringBuilder buf = new StringBuilder();
Iterator i = strings.iterator();
if (i.hasNext()) {
buf.append(i.next());
while (i.hasNext())
buf.append(glue).append(i.next());
}
return buf.toString();
}
public static String join(String glue, String[] strings) {
return join(glue, Arrays.asList(strings));
}
public static String join(Iterable strings) {
return join("", strings);
}
public static String join(String[] strings) {
return join("", strings);
}
static List findConceptsWhere(Class c, Object... params) {
params = expandParams(c, params);
List l = new ArrayList ();
for (A x : list(c))
if (checkConceptFields(x, params))
l.add(x);
return l;
}
static List findConceptsWhere(String c, Object... params) {
List l = new ArrayList();
for (Concept x : list(c))
if (checkConceptFields(x, params))
l.add(x);
return l;
}
static String shortDynamicClassName(Object o) {
return shortenClassName(dynamicClassName(o));
}
static void printStackTrace(Throwable e) {
// we go to system.out now - system.err is nonsense
print(getStackTrace(e));
}
static void printStackTrace() {
printStackTrace(new Throwable());
}
static void printStackTrace(String indent, Throwable e) {
if (endsWithLetter(indent)) indent += " ";
printIndent(indent, getStackTrace(e));
}
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;
}
// f must return a list
static List concatMap(Object f, Iterable l) {
return concatLists(map(f, l));
}
static Map litmap(Object... x) {
TreeMap map = new TreeMap();
litmap_impl(map, x);
return map;
}
static void litmap_impl(Map map, Object... x) {
for (int i = 0; i < x.length-1; i += 2)
if (x[i+1] != null)
map.put(x[i], x[i+1]);
}
static boolean odd(int i) {
return (i & 1) != 0;
}
static String shortenClassName(String name) {
return name == null ? null : substring(name, xIndexOf(name, '$')+1);
}
static boolean contains(Collection c, Object o) {
return c != null && c.contains(o);
}
static boolean contains(Object[] x, Object o) {
if (x != null)
for (Object a : x)
if (eq(a, o))
return true;
return false;
}
static boolean contains(String s, char c) {
return s != null && s.indexOf(c) >= 0;
}
static boolean contains(String s, String b) {
return s != null && s.indexOf(b) >= 0;
}
static String getType(Object o) {
return getClassName(o);
}
static int dayOfMonth() {
return days();
}
static boolean isJavaIdentifier(String s) {
if (s.length() == 0 || !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 TimerTask timerTask(final Runnable r) {
return new TimerTask() {
public void run() {
r.run();
}
};
}
static volatile boolean ping_pauseAll;
static int ping_sleep = 100; // poll pauseAll flag every 100
static volatile boolean ping_anyActions;
static Map ping_actions = synchroMap(new WeakHashMap());
// returns true if it did anything
static boolean ping() {
if (ping_pauseAll) {
do
sleep(ping_sleep);
while (ping_pauseAll);
return true;
}
if (ping_anyActions) {
Object action;
synchronized(mc()) {
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;
}
static String programID;
static String getProgramID() {
return nempty(programID) ? formatSnippetID(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));
}
// 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;
List 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; String cc;
// scan for whitespace
while (j < l) {
c = s.charAt(j);
cc = s.substring(j, Math.min(j+2, l));
if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
++j;
else if (cc.equals("/*")) {
do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
j = Math.min(j+2, l);
} else if (cc.equals("//")) {
do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
} else
break;
}
if (n < nExisting && javaTok_isCopyable(existing.get(n), s, i, j))
tok.add(existing.get(n));
else
tok.add(quickSubstring(s, i, j));
++n;
i = j;
if (i >= l) break;
c = s.charAt(i); // cc is not needed in rest of loop body
cc = s.substring(i, Math.min(i+2, l));
// scan for non-whitespace
if (c == '\'' || c == '"') {
char opener = c;
++j;
while (j < l) {
if (s.charAt(j) == opener || s.charAt(j) == '\n') { // end at \n to not propagate unclosed string literal errors
++j;
break;
} else if (s.charAt(j) == '\\' && j+1 < l)
j += 2;
else
++j;
}
} else if (Character.isJavaIdentifierStart(c))
do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't"
else if (Character.isDigit(c)) {
do ++j; while (j < l && Character.isDigit(s.charAt(j)));
if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
} else if (cc.equals("[[")) {
do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
j = Math.min(j+2, l);
} else if (cc.equals("[=") && i+2 < l && s.charAt(i+2) == '[') {
do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
j = Math.min(j+3, l);
} else
++j;
if (n < nExisting && javaTok_isCopyable(existing.get(n), s, i, j))
tok.add(existing.get(n));
else
tok.add(quickSubstring(s, i, j));
++n;
i = j;
}
if ((tok.size() % 2) == 0) tok.add("");
javaTok_elements += tok.size();
return tok;
}
static List javaTok(List tok) {
return javaTok(join(tok), tok);
}
static boolean javaTok_isCopyable(String t, String s, int i, int j) {
return t.length() == j-i
&& s.regionMatches(i, t, 0, j-i); // << could be left out, but that's brave
}
static String unnull(String s) {
return s == null ? "" : s;
}
static List unnull(List l) {
return l == null ? emptyList() : l;
}
static Iterable unnull(Iterable i) {
return i == null ? emptyList() : i;
}
static Object[] unnull(Object[] a) {
return a == null ? new Object[0] : a;
}
static BitSet unnull(BitSet b) {
return b == null ? new BitSet() : b;
}
static Object unstructure(String text) {
return unstructure(text, false);
}
static Object unstructure(String text, final boolean allDynamic) {
return unstructure(text, allDynamic, null);
}
static int structure_internStringsLongerThan = 50;
// TODO: backrefs for hashmap{} etc
// classFinder: name -> class
static Object unstructure(String text, final boolean allDynamic,
final Object classFinder) {
if (text == null) return null;
final List tok = javaTok(text);
final boolean debug = unstructure_debug;
class X {
int i = 1;
HashMap refs = new HashMap();
HashSet concepts = new HashSet();
Object parse() {
String t = tok.get(i);
int refID = 0;
if (t.startsWith("m") && isInteger(t.substring(1))) {
refID = parseInt(t.substring(1));
i += 2;
t = tok.get(i);
}
// if (debug) print("parse: " + quote(t));
if (t.startsWith("'")) {
char c = unquoteCharacter(tok.get(i));
i += 2;
return c;
}
if (t.equals("bigint"))
return parseBigInt();
if (t.equals("d"))
return parseDouble();
if (t.equals("fl"))
return parseFloat();
if (t.equals("false") || t.equals("f")) {
i += 2; return false;
}
if (t.equals("true") || t.equals("t")) {
i += 2; return true;
}
if (t.equals("-")) {
t = tok.get(i+2);
i += 4;
return isLongConstant(t) ? (Object) (-parseLong(t)) : (Object) (-parseInt(t));
}
if (isInteger(t) || isLongConstant(t)) {
i += 2;
//if (debug) print("isLongConstant " + quote(t) + " => " + isLongConstant(t));
if (isLongConstant(t)) return parseLong(t);
long l = parseLong(t);
boolean isInt = l == (int) l;
if (debug)
print("l=" + l + ", isInt: " + isInt);
return isInt ? (Object) new Integer((int) l) : (Object) new Long(l);
}
if (t.equals("File")) {
File f = new File(unquote(tok.get(i+2)));
i += 4;
return f;
}
if (t.startsWith("r") && isInteger(t.substring(1))) {
i += 2;
int ref = Integer.parseInt(t.substring(1));
Object o = refs.get(ref);
if (o == null)
print("Warning: unsatisfied back reference " + ref);
return o;
}
Object o = parse_inner(refID);
if (refID != 0)
refs.put(refID, o);
return o;
}
// everything that can be backreferenced
Object parse_inner(int refID) {
String t = tok.get(i);
// if (debug) print("parse_inner: " + quote(t));
if (t.startsWith("\"")) {
String s = internIfLongerThan(unquote(tok.get(i)), structure_internStringsLongerThan);
i += 2;
return s;
}
if (t.equals("hashset"))
return parseHashSet();
if (t.equals("treeset"))
return parseTreeSet();
if (eqOneOf(t, "hashmap", "hm"))
return parseHashMap();
if (t.equals("{"))
return parseMap();
if (t.equals("["))
return parseList();
if (t.equals("bitset"))
return parseBitSet();
if (t.equals("array") || t.equals("intarray"))
return parseArray();
if (t.equals("ba")) {
String hex = unquote(tok.get(i+2));
i += 4;
return hexToBytes(hex);
}
if (t.equals("boolarray")) {
int n = parseInt(tok.get(i+2));
String hex = unquote(tok.get(i+4));
i += 6;
return boolArrayFromBytes(hexToBytes(hex), n);
}
if (t.equals("class"))
return parseClass();
if (t.equals("l"))
return parseLisp();
if (t.equals("null")) {
i += 2; return null;
}
/* in dev.
if (!allDynamic && t.equals("run")) {
S snippetID = unquote(t.get(i+2));
i += 4;
run(
}
*/
boolean concept = eq(t, "c");
if (concept) {
consume("c");
t = tok.get(i);
assertTrue(isJavaIdentifier(t));
concepts.add(t);
}
// any other class name
if (isJavaIdentifier(t)) {
// First, find class
Class c;
if (allDynamic) c = null;
else if (classFinder != null)
c = (Class) callF(classFinder, t);
else
c = findClass(t);
// Second, check if it has an outer reference
i += 2;
boolean hasOuter = eq(get(tok, i), "(") && eq(get(tok, i+2), "this$1");
DynamicObject dO = null;
Object o = null;
if (c != null)
o = hasOuter ? nuStubInnerObject(c) : nuObject(c);
else {
if (concepts.contains(t) && (c = findClass("Concept")) != null)
o = dO = (DynamicObject) nuObject(c);
else
dO = new DynamicObject();
dO.className = t;
if (debug) print("Made dynamic object " + t + " " + shortClassName(dO));
}
// Now, save in references list.
if (refID != 0)
refs.put(refID, o != null ? o : dO);
// NOW parse the fields!
Map fields = new TreeMap();
if (i < tok.size() && tok.get(i).equals("(")) {
consume("(");
while (!tok.get(i).equals(")")) {
String key = unquote(tok.get(i));
i += 2;
consume("=");
Object value = parse();
fields.put(key, value);
if (tok.get(i).equals(",")) i += 2;
}
consume(")");
}
if (o != null)
if (dO != null) {
if (debug)
printStructure("setOptAllDyn", fields);
setOptAllDyn(dO, fields);
} else
setOptAll(o, fields);
else
dO.fieldValues.putAll(fields);
if (o != null)
pcallOpt(o, "_doneLoading");
return o != null ? o : dO;
}
throw new RuntimeException("Unknown token " + (i+1) + ": " + t);
}
Object parseSet(Set set) {
set.addAll((List) parseList());
return set;
}
Object parseLisp() {
consume("l");
consume("(");
List list = new ArrayList();
while (!tok.get(i).equals(")")) {
list.add(parse());
if (tok.get(i).equals(",")) i += 2;
}
consume(")");
return newObject("main$Lisp", (String) list.get(0), subList(list, 1));
}
Object parseBitSet() {
consume("bitset");
consume("{");
BitSet bs = new BitSet();
while (!tok.get(i).equals("}")) {
bs.set((Integer) parse());
if (tok.get(i).equals(",")) i += 2;
}
consume("}");
return bs;
}
Object parseList() {
consume("[");
List list = new ArrayList();
while (!tok.get(i).equals("]")) {
Object o = parse();
//if (debug) print("List element type: " + getClassName(o));
list.add(o);
if (tok.get(i).equals(",")) i += 2;
}
consume("]");
return list;
}
Object parseArray() {
String type = tok.get(i);
i += 2;
consume("{");
List list = new ArrayList();
while (!tok.get(i).equals("}")) {
list.add(parse());
if (tok.get(i).equals(",")) i += 2;
}
consume("}");
if (type.equals("intarray"))
return toIntArray(list);
return list.toArray();
}
Object parseClass() {
consume("class");
consume("(");
String name = tok.get(i);
i += 2;
consume(")");
Class c = allDynamic ? null : findClass(name);
if (c != null) return c;
DynamicObject dO = new DynamicObject();
dO.className = "java.lang.Class";
dO.fieldValues.put("name", name);
return dO;
}
Object parseBigInt() {
consume("bigint");
consume("(");
String val = tok.get(i);
i += 2;
if (eq(val, "-")) {
val = "-" + tok.get(i);
i += 2;
}
consume(")");
return new BigInteger(val);
}
Object parseDouble() {
consume("d");
consume("(");
String val = unquote(tok.get(i));
i += 2;
consume(")");
return Double.parseDouble(val);
}
Object parseFloat() {
consume("fl");
String val;
if (eq(tok.get(i), "(")) {
consume("(");
val = unquote(tok.get(i));
i += 2;
consume(")");
} else {
val = unquote(tok.get(i));
i += 2;
}
return Float.parseFloat(val);
}
Object parseHashMap() {
i += 2;
return parseMap(new HashMap());
}
Object parseHashSet() {
consume("hashset");
return parseSet(new HashSet());
}
Object parseTreeSet() {
consume("treeset");
return parseSet(new TreeSet());
}
Object parseMap() {
return parseMap(new TreeMap());
}
Object parseMap(Map map) {
consume("{");
while (!tok.get(i).equals("}")) {
Object key = parse();
consume("=");
Object value = parse();
map.put(key, value);
if (tok.get(i).equals(",")) i += 2;
}
consume("}");
return map;
}
void consume(String s) {
if (!tok.get(i).equals(s)) {
String prevToken = i-2 >= 0 ? tok.get(i-2) : "";
String nextTokens = join(tok.subList(i, Math.min(i+4, tok.size())));
throw fail(quote(s) + " expected: " + prevToken + " " + nextTokens + " (" + i + "/" + tok.size() + ")");
}
i += 2;
}
}
return new X().parse();
}
static boolean unstructure_debug;
static Map putKeysFirst(Map map, Object... keys) {
Map m2 = litorderedmap();
Map remaining = cloneMap(map);
for (Object key : keys) {
Object value = remaining.get(key);
if (value != null) {
m2.put(key, value);
remaining.remove(key);
}
}
m2.putAll(remaining);
return m2;
}
static void assertTrue(Object o) {
assertEquals(true, o);
}
static boolean assertTrue(String msg, boolean b) {
if (!b)
throw fail(msg);
return b;
}
static boolean assertTrue(boolean b) {
if (!b)
throw fail("oops");
return b;
}
static ArrayList litlist(A... a) {
return new ArrayList (Arrays.asList(a));
}
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 List codeTokens(List tok) {
return codeTokensOnly(tok);
}
static String[] toStringArray(List list) {
return list.toArray(new String[list.size()]);
}
static String[] toStringArray(Object o) {
if (o instanceof String[])
return (String[]) o;
else if (o instanceof List)
return toStringArray((List) o);
else
throw fail("Not a list or array: " + structure(o));
}
static File getProgramDir() {
return programDir();
}
static File getProgramDir(String snippetID) {
return programDir(snippetID);
}
static Object call(Object o) {
return callFunction(o);
}
// varargs assignment fixer for a single string array argument
static Object call(Object o, String method, String[] arg) {
return call(o, method, new Object[] {arg});
}
static Object call(Object o, String method, Object... args) {
try {
if (o instanceof Class) {
Method m = call_findStaticMethod((Class) o, method, args, false);
m.setAccessible(true);
return m.invoke(null, args);
} else {
Method m = call_findMethod(o, method, args, false);
m.setAccessible(true);
return m.invoke(o, args);
}
} catch (Exception e) {
throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
}
}
static Method call_findStaticMethod(Class c, String method, Object[] args, boolean debug) {
Class _c = c;
while (c != null) {
for (Method m : c.getDeclaredMethods()) {
if (debug)
System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
if (!m.getName().equals(method)) {
if (debug) System.out.println("Method name mismatch: " + method);
continue;
}
if ((m.getModifiers() & 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 Object getOpt(Object o, String field) {
if (o instanceof String) o = getBot ((String) o);
if (o == null) return null;
if (o instanceof Class) return getOpt((Class) o, field);
if (o.getClass().getName().equals("main$DynamicObject"))
return ((Map) getOpt_raw(o, "fieldValues")).get(field);
if (o instanceof Map) return ((Map) o).get(field);
return getOpt_raw(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);
}
}
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() & Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
return null;
}
static Field getOpt_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
return null;
}
static String boolArrayToHex(boolean[] a) {
return bytesToHex(boolArrayToBytes(a));
}
static A findConceptWhere(Class c, Object... params) {
params = expandParams(c, params);
for (A x : list(c))
if (checkConceptFields(x, params))
return x;
return null;
}
static Object first(Object list) {
return ((List) list).isEmpty() ? null : ((List) list).get(0);
}
static A first(List list) {
return list.isEmpty() ? 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 boolean isIdentifier(String s) {
return isJavaIdentifier(s);
}
static boolean makeAndroid3_disable; // disable all android making
static class Android3 {
String greeting;
boolean publicOverride; // optionally set this in client
int startPort = 5000; // optionally set this in client
Responder responder;
boolean console = true;
boolean daemon = false;
boolean incomingSilent = false;
int incomingPrintLimit = 200;
boolean useMultiPort = true;
boolean recordHistory;
boolean verbose;
int answerPrintLimit = 500;
// set by system
int port;
long vport;
DialogHandler handler;
ServerSocket server;
Android3(String greeting) {
this.greeting = greeting;}
Android3() {}
synchronized void dispose() {
if (server != null) {
try {
server.close();
} catch (IOException e) {
print("[internal] " + e);
}
server = null;
}
if (vport != 0) try { /* pcall 1*/
//print("Dispoing virtual port " + vport);
removeFromMultiPort(vport);
vport = 0;
/* pcall 2 */ } catch (Throwable __e) { printStackTrace(__e); }
}
}
static abstract class Responder {
abstract String answer(String s, List history);
}
static Android3 makeAndroid3(final String greeting) {
return makeAndroid3(new Android3(greeting));
}
static Android3 makeAndroid3(final String greeting, Responder responder) {
Android3 android = new Android3(greeting);
android.responder = responder;
return makeAndroid3(android);
}
static Android3 makeAndroid3(final Android3 a) {
if (makeAndroid3_disable) return a;
if (a.responder == null)
a.responder = new Responder() {
String answer(String s, List history) {
return callStaticAnswerMethod(s, history);
}
};
print("[bot] " + a.greeting);
if (a.useMultiPort) {
a.vport = addToMultiPort(a.greeting,
makeAndroid3_verboseResponder(a));
if (a.vport == 1)
makeAndroid3_handleConsole(a);
return a;
}
a.handler = makeAndroid3_makeDialogHandler(a);
a.port = a.daemon
? startDialogServerOnPortAboveDaemon(a.startPort, a.handler)
: startDialogServerOnPortAbove(a.startPort, a.handler);
a.server = startDialogServer_serverSocket;
if (a.console && makeAndroid3_consoleInUse()) a.console = false;
if (a.console)
makeAndroid3_handleConsole(a);
record(a);
return a;
}
static void makeAndroid3_handleConsole(final Android3 a) {
// Console handling stuff
print("You may also type on this console.");
{ Thread _t_0 = new Thread() {
public void run() { try { /* pcall 1*/
List history = new ArrayList();
String line;
while ((line = readLine()) != null) {
/*if (eq(line, "bye")) {
print("> bye stranger");
history = new ArrayList();
} else*/ {
history.add(line);
history.add(makeAndroid3_getAnswer(line, history, a)); // prints answer on console too
}
}
/* pcall 2 */ } catch (Throwable __e) { printStackTrace(__e); } }
};
_t_0.start(); }
}
static DialogHandler makeAndroid3_makeDialogHandler(final Android3 a) {
return new DialogHandler() {
public void run(final DialogIO io) {
if (!a.publicOverride && !(publicCommOn() || io.isLocalConnection())) {
io.sendLine("Sorry, not allowed");
return;
}
String dialogID = randomID(8);
io.sendLine(a.greeting + " / Your ID: " + dialogID);
List history = new ArrayList();
while (io.isStillConnected()) {
if (io.waitForLine()) {
final String line = io.readLineNoBlock();
String s = dialogID + " at " + now() + ": " + quote(line);
if (!a.incomingSilent)
print(shorten(s, a.incomingPrintLimit));
if (eq(line, "bye")) {
io.sendLine("bye stranger");
return;
}
Matches m = new Matches();
if (a.recordHistory)
history.add(line);
String answer;
if (match3("this is a continuation of talk *", s, m)
|| match3("hello bot! this is a continuation of talk *", s, m)) {
dialogID = unquote(m.m[0]);
answer = "ok";
} else try {
makeAndroid3_io.set(io);
answer = makeAndroid3_getAnswer(line, history, a);
} finally {
makeAndroid3_io.set(null);
}
if (a.recordHistory)
history.add(answer);
io.sendLine(answer);
//appendToLog(logFile, s);
}
}
}};
}
static String makeAndroid3_getAnswer(String line, List history, Android3 a) {
String answer, originalAnswer;
try {
originalAnswer = a.responder.answer(line, history);
answer = makeAndroid3_fallback(line, history, originalAnswer);
} catch (Throwable e) {
e = getInnerException(e);
printStackTrace(e);
originalAnswer = answer = e.toString();
}
if (!a.incomingSilent) {
if (originalAnswer == null) originalAnswer = "?";
print("> " + shorten(originalAnswer, a.answerPrintLimit));
}
return answer;
}
static String makeAndroid3_fallback(String s, List history, String answer) {
// Now we only do the safe thing instead of VM inspection - give out our process ID
if (answer == null && match3("what is your pid", s))
return getPID();
if (answer == null && match3("what is your program id", s)) // should be fairly safe, right?
return getProgramID();
if (match3("get injection id", s))
return getInjectionID();
if (answer == null) answer = "?";
if (answer.indexOf('\n') >= 0 || answer.indexOf('\r') >= 0)
answer = quote(answer);
return answer;
}
static boolean makeAndroid3_consoleInUse() {
for (Object o : record_list)
if (o instanceof Android3 && ((Android3) o).console)
return true;
return false;
}
static Responder makeAndroid3_verboseResponder(final Android3 a) {
return new Responder() {
String answer(String s, List history) {
if (a.verbose)
print("> " + s);
String answer = a.responder.answer(s, history);
if (a.verbose)
print("< " + answer);
return answer;
}
};
}
static ThreadLocal makeAndroid3_io = new ThreadLocal();
public static String bytesToHex(byte[] bytes) {
return bytesToHex(bytes, 0, bytes.length);
}
public static String bytesToHex(byte[] bytes, int ofs, int len) {
StringBuilder stringBuilder = new StringBuilder(len*2);
for (int i = 0; i < len; i++) {
String s = "0" + Integer.toHexString(bytes[ofs+i]);
stringBuilder.append(s.substring(s.length()-2, s.length()));
}
return stringBuilder.toString();
}
static boolean isInteger(String s) {
return s != null && Pattern.matches("\\-?\\d+", s);
}
static RuntimeException asRuntimeException(Throwable t) {
return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static Object mc() {
return getMainClass();
}
static File programDir_mine; // set this to relocate program's data
static File programDir() {
return programDir(getProgramID());
}
static File programDir(String snippetID) {
if (programDir_mine != null && sameSnippetID(snippetID, programID()))
return programDir_mine;
return new File(javaxDataDir(), formatSnippetID(snippetID));
}
static String getProgramTitle() {
return getProgramName();
}
// 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 String singleFieldName(Class c) {
Set l = listFields(c);
if (l(l) != 1)
throw fail("No single field found in " + c + " (have " + n(l(l), "fields") + ")");
return first(l);
}
static String classNameToVM(String name) {
return name.replace(".", "$");
}
static void saveLocally(String variableName) {
saveLocally(programID(), variableName);
}
static void saveLocally(String progID, String variableName) {
saveLocally2(mc(), progID, variableName);
}
static void saveLocally2(Object obj, String variableName) {
saveLocally2(obj, programID(), variableName);
}
static synchronized void saveLocally2(Object obj, String progID, String variableName) {
File textFile = new File(programDir(progID), variableName + ".text");
File structureFile = new File(programDir(progID), variableName + ".structure");
Object x = get(obj, variableName);
if (x == null) {
textFile.delete();
structureFile.delete();
} else if (x instanceof String) {
saveTextFile(textFile, (String) x);
structureFile.delete();
} else {
saveTextFile(structureFile, structure(x));
textFile.delete();
}
}
static boolean conceptHasOptions(Concept c) {
return conceptGraphSearch(c, new Object() { Object get(Concept c) { return c instanceof Options ; }
public String toString() { return "c << Options"; }}) != null;
}
// collects into l
static Collector limitedListCollector(final Collection l, final int max) {
return new Collector () {
boolean full() {
return l(l) >= max;
}
boolean add(A a) {
if (full()) return false;
l.add(a);
return full();
}
boolean contains(A a) {
return l.contains(a);
}
};
}
static boolean warn_on = true;
static void warn(String s) {
if (warn_on)
print("Warning: " + s);
}
static void warn(String s, List warnings) {
warn(s);
if (warnings != null)
warnings.add(s);
}
static int month() {
return Calendar.getInstance().get(Calendar.MONTH)+1;
}
static List concatLists(List ... lists) {
List l = new ArrayList ();
for (List list : lists)
if (list != null)
l.addAll(list);
return l;
}
static List concatLists(Collection> lists) {
List l = new ArrayList ();
for (List list : lists)
if (list != null)
l.addAll(list);
return l;
}
static void csetMap(Concept c, Map map) {
for (String field : keys(map))
cset(c, field, map.get(field));
}
static boolean booleanValue(Object o) {
return eq(true, o);
}
public static File mkdirsForFile(File file) {
File dir = file.getParentFile();
if (dir != null) // is null if file is in current dir
dir.mkdirs();
return file;
}
static void copyConceptFields(Concept a, Concept b) {
for (String field : conceptFields(a))
cset(b, field, cget(a, field));
}
static String substring(String s, int x) {
return safeSubstring(s, x);
}
static String substring(String s, int x, int y) {
return safeSubstring(s, x, y);
}
static List emptyList() {
return new ArrayList();
//ret Collections.emptyList();
}
static List synchroList() {
return Collections.synchronizedList(new ArrayList ());
}
static List synchroList(List l) {
return Collections.synchronizedList(l);
}
static Object[] toObjectArray(Collection c) {
List l = asList(c);
return l.toArray(new Object[l.size()]);
}
static String ok(Object o) {
return format("ok *", o);
}
static Object[] asObjectArray(List l) {
return toObjectArray(l);
}
static String padLeft(String s, char c, int n) {
return rep(c, n-l(s)) + s;
}
static Set conceptFields(Concept c) {
return setMinus(mergeSets(allFields(c), keys(c.fieldValues)),
"className", "fieldValues", "id", "created", "refs", "backRefs");
}
public static String loadTextFile(String fileName) {
try {
return loadTextFile(fileName, null);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String loadTextFile(String fileName, String defaultContents) throws IOException {
if (!new File(fileName).exists())
return defaultContents;
FileInputStream fileInputStream = new FileInputStream(fileName);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
return loadTextFile(inputStreamReader);
}
public static String loadTextFile(File fileName) {
try {
return loadTextFile(fileName, null);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String loadTextFile(File fileName, String defaultContents) throws IOException {
try {
return loadTextFile(fileName.getPath(), defaultContents);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String loadTextFile(Reader reader) throws IOException {
StringBuilder builder = new StringBuilder();
try {
char[] buffer = new char[1024];
int n;
while (-1 != (n = reader.read(buffer)))
builder.append(buffer, 0, n);
} finally {
reader.close();
}
return builder.toString();
}
static boolean isEmpty(Collection c) {
return c == null || c.isEmpty();
}
static boolean isEmpty(CharSequence s) {
return s == null || s.length() == 0;
}
static boolean isEmpty(Object[] a) {
return a == null || a.length == 0;
}
static boolean isEmpty(Map map) {
return map == null || map.isEmpty();
}
static boolean 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 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 String dropSuffix(String suffix, String s) {
return s.endsWith(suffix) ? s.substring(0, l(s)-l(suffix)) : 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 void smartSet(Field f, Object o, Object value) throws Exception {
f.setAccessible(true);
// take care of common case (long to int)
if (f.getType() == int.class && value instanceof Long)
value = ((Long) value).intValue();
f.set(o, value);
}
static List joinBrackets(List tok) {
List t = new ArrayList();
Map map = getBracketMap(tok);
for (int i = 0; i < l(tok); i++) {
Integer dest = map.get(i);
if (dest != null) {
t.add(join(subList(tok, i, dest+1)));
i = dest;
} else
t.add(tok.get(i));
}
return t;
}
static boolean containsIgnoreCase(List l, String s) {
for (String x : l)
if (eqic(x, s))
return true;
return false;
}
static boolean containsIgnoreCase(String[] l, String s) {
for (String x : l)
if (eqic(x, s))
return true;
return false;
}
static boolean containsIgnoreCase(String s, char c) {
return indexOfIgnoreCase(s, String.valueOf(c)) >= 0;
}
static boolean containsIgnoreCase(String a, String b) {
return indexOfIgnoreCase(a, b) >= 0;
}
static String getString(Map map, Object key) {
return map == null ? null : (String) map.get(key);
}
static String getString(List l, int idx) {
return (String) get(l, idx);
}
static String getString(Object o, Object key) {
if (o instanceof Map) return getString((Map) o, key);
if (key instanceof String)
return (String) getOpt(o, (String) key);
throw fail("Not a string key: " + getClassName(key));
}
static String dynamicClassName(Object o) {
if (o instanceof DynamicObject && ((DynamicObject) o).className != null)
return "main$" + ((DynamicObject) o).className;
return className(o);
}
static int parseInt(String s) {
return empty(s) ? 0 : Integer.parseInt(s);
}
static String makeResponder_callAnswerMethod(Object bot, String s, List history) {
String answer = (String) callOpt(bot, "answer", s, history);
if (answer == null)
answer = (String) callOpt(bot, "answer", s);
return answer;
}
static Responder makeResponder(final Object bot) {
if (bot instanceof Responder) return (Responder) bot;
return new Responder() {
String answer(String s, List history) {
return makeResponder_callAnswerMethod(bot, s, history);
}
};
}
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 (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
// This is for main classes that are all static.
// (We don't go to base classes.)
static Set listFields(Object c) {
TreeSet fields = new TreeSet();
for (Field f : _getClass(c).getDeclaredFields())
fields.add(f.getName());
return fields;
}
static List record_list = synchroList();
static void record(Object o) {
record_list.add(o);
}
static Thread currentThread() {
return Thread.currentThread();
}
static byte[] hexToBytes(String s) {
int n = l(s) / 2;
byte[] bytes = new byte[n];
for (int i = 0; i < n; i++) {
String hex = substring(s, i*2, i*2+2);
try {
bytes[i] = (byte) parseHexByte(hex);
} catch (Throwable _e) {
throw fail("Bad hex byte: " + quote(hex) + " at " + i*2 + "/" + l(s));
}
}
return bytes;
}
// returns l(s) if not found
static int xIndexOf(String s, String sub, int i) {
if (s == null) return 0;
i = s.indexOf(sub, min(i, l(s)));
return i >= 0 ? i : l(s);
}
static int xIndexOf(String s, String sub) {
return xIndexOf(s, sub, 0);
}
static int xIndexOf(String s, char c) {
if (s == null) return 0;
int i = s.indexOf(c);
return i >= 0 ? i : l(s);
}
static void removeFromMultiPort(long vport) {
for (Object port : getMultiPorts())
call(port, "removePort", vport);
}
static void pcallOpt(Object o, String method, Object... args) {
try { /* pcall 1*/ callOpt(o, method, args); /* pcall 2 */ } catch (Throwable __e) { printStackTrace(__e); }
}
static boolean setOptAllDyn_debug;
static void setOptAllDyn(DynamicObject o, Map fields) {
if (fields == null) return;
for (String field : keys(fields)) {
Object val = fields.get(field);
boolean has = hasField(o, field);
if (has)
setOpt(o, field, val);
else {
o.fieldValues.put(field, val);
if (setOptAllDyn_debug) print("setOptAllDyn added dyn " + field + " to " + o + " [value: " + val + ", fieldValues = " + systemHashCode(o.fieldValues) + ", " + struct(keys(o.fieldValues)) + "]");
}
}
}
static String quickSubstring(String s, int i, int j) {
if (i == j) return "";
return s.substring(i, j);
}
static Object addToMultiPort_responder;
static long addToMultiPort(final String botName) {
return addToMultiPort(botName, new Object() {
public String answer(String s, List history) {
String answer = (String) ( callOpt(getMainClass(), "answer", s, history));
if (answer != null) return answer;
answer = (String) callOpt(getMainClass(), "answer", s);
if (answer != null) return answer;
if (match3("get injection id", s))
return getInjectionID();
return null;
}
});
}
static long addToMultiPort(final String botName, final Object responder) {
//print(botName);
addToMultiPort_responder = responder;
startMultiPort();
List ports = getMultiPorts();
if (ports == null) return 0;
if (ports.isEmpty())
throw fail("No multiports!");
if (ports.size() > 1)
print("Multiple multi-ports. Using last one.");
Object port = last(ports);
Object responder2 = new Object() {
public String answer(String s, List history) {
if (match3("get injection id", s))
return getInjectionID();
if (match3("your name", s))
return botName;
return (String) call(responder, "answer", s, history);
}
};
record(responder2);
return (Long) call(port, "addResponder", botName, responder2);
}
static int min(int a, int b) {
return Math.min(a, b);
}
static long min(long a, long b) {
return Math.min(a, b);
}
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 byte min(byte[] c) {
byte x = 127;
for (byte d : c) if (d < x) x = d;
return x;
}
static Set mergeSets(Set ... l) {
return joinSets(l);
}
static Object newObject(Class c, Object... args) {
return nuObject(c, args);
}
static Object newObject(String className, Object... args) {
return nuObject(className, args);
}
static Class getMainClass() { try {
return main.class;
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static Class getMainClass(Object o) { try {
return (o instanceof Class ? (Class) o : o.getClass()).getClassLoader().loadClass("main");
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static Throwable getInnerException(Throwable e) {
while (e.getCause() != null)
e = e.getCause();
return e;
}
static String n(long l, String name) {
return l + " " + (l == 1 ? singular(name) : getPlural(name));
}
static Set setMinus(Set set, Object... stuff) {
Set s2 = cloneSet(set);
for (Object o : stuff)
s2.remove(o);
return s2;
}
static int days() {
return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
}
// class Matches is added by #752
static boolean match3(String pat, String s) {
return match3(pat, s, null);
}
static boolean match3(String pat, String s, Matches matches) {
if (s == null) return false;
return match3(pat, parse3_cached(s), matches);
}
static boolean match3(String pat, List toks, Matches matches) {
List tokpat = parse3(pat);
return match3(tokpat,toks,matches);
}
static boolean match3(List tokpat, List toks, Matches matches) {
String[] m = match2(tokpat, toks);
//print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m));
if (m == null)
return false;
else {
if (matches != null) matches.m = m;
return true;
}
}
static String getStackTrace(Throwable throwable) {
StringWriter writer = new StringWriter();
throwable.printStackTrace(new PrintWriter(writer));
return writer.toString();
}
static String formatSnippetID(String id) {
return "#" + parseSnippetID(id);
}
static String formatSnippetID(long id) {
return "#" + id;
}
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 String getInjectionID() {
return (String) call(getJavaX(), "getInjectionID", getMainClass());
}
static Set allFields(Object o) {
TreeSet fields = new TreeSet();
Class _c = _getClass(o);
do {
for (Field f : _c.getDeclaredFields())
fields.add(f.getName());
_c = _c.getSuperclass();
} while (_c != null);
return fields;
}
static AtomicInteger dialogServer_clients = new AtomicInteger();
static boolean dialogServer_printConnects;
static Set dialogServer_knownClients = synchroTreeSet();
static int startDialogServerOnPortAbove(int port, DialogHandler handler) {
while (!forbiddenPort(port) && !startDialogServerIfPortAvailable(port, handler))
++port;
return port;
}
static int startDialogServerOnPortAboveDaemon(int port, DialogHandler handler) {
while (!forbiddenPort(port) && !startDialogServerIfPortAvailable(port, handler, true))
++port;
return port;
}
static void startDialogServer(int port, DialogHandler handler) {
if (!startDialogServerIfPortAvailable(port, handler))
throw fail("Can't start dialog server on port " + port);
}
static boolean startDialogServerIfPortAvailable(int port, final DialogHandler handler) {
return startDialogServerIfPortAvailable(port, handler, false);
}
static ServerSocket startDialogServer_serverSocket;
static boolean startDialogServerIfPortAvailable(int port, final DialogHandler handler, boolean daemon) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
// probably the port number is used - let's assume there already is a chat server.
return false;
}
final ServerSocket _serverSocket = serverSocket;
startDialogServer_serverSocket = serverSocket;
Thread thread = new Thread("Socket accept port " + port) { public void run() {
try {
while (true) {
try {
final Socket s = _serverSocket.accept();
String client = s.getInetAddress().toString();
if (!dialogServer_knownClients.contains(client) && neq(client, "/127.0.0.1")) {
print("connect from " + client + " - clients: " + dialogServer_clients.incrementAndGet());
dialogServer_knownClients.add(client);
}
String threadName = "Handling client " + s.getInetAddress();
Thread t2 = new Thread(threadName) {
public void run() {
try {
final Writer w = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
final BufferedReader in = new BufferedReader(
new InputStreamReader(s.getInputStream(), "UTF-8"));
DialogIO io = new DialogIO() {
// This should be the same as #1001076 (talkTo)
boolean isLocalConnection() {
return s.getInetAddress().isLoopbackAddress();
}
boolean isStillConnected() {
return !(eos || s.isClosed());
}
void sendLine(String line) { try {
w.write(line + "\n");
w.flush();
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
String readLineImpl() { try {
return in.readLine();
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
void close() {
try {
s.close();
} catch (IOException e) {
// whatever
}
}
Socket getSocket() {
return s;
}
};
try {
handler.run(io);
} finally {
s.close();
}
} catch (IOException e) {
print("[internal] " + e);
} finally {
//print("client disconnect - " + dialogServer_clients.decrementAndGet() + " remaining");
}
}
}; // Thread t2
t2.setDaemon(true); // ?
t2.start();
} catch (SocketTimeoutException e) {
}
}
} catch (IOException e) {
print("[internal] " + e);
}
}};
if (daemon) thread.setDaemon(true);
thread.start();
print("Dialog server on port " + port + " started.");
return true;
}
static boolean checkConceptFields(Concept x, Object... data) {
for (int i = 0; i < l(data); i += 2)
if (neq(cget(x, (String) data[i]), deref(data[i+1])))
return false;
return true;
}
public static String unquote(String s) {
if (s == null) return null;
if (s.startsWith("[")) {
int i = 1;
while (i < s.length() && s.charAt(i) == '=') ++i;
if (i < s.length() && s.charAt(i) == '[') {
String m = s.substring(1, i);
if (s.endsWith("]" + m + "]"))
return s.substring(i+1, s.length()-i-1);
}
}
if (s.startsWith("\"") /*&& s.endsWith("\"")*/ && s.length() > 1) {
String st = s.substring(1, s.endsWith("\"") ? s.length()-1 : s.length());
StringBuilder sb = new StringBuilder(st.length());
for (int i = 0; i < st.length(); i++) {
char ch = st.charAt(i);
if (ch == '\\') {
char nextChar = (i == st.length() - 1) ? '\\' : st
.charAt(i + 1);
// Octal escape?
if (nextChar >= '0' && nextChar <= '7') {
String code = "" + nextChar;
i++;
if ((i < st.length() - 1) && st.charAt(i + 1) >= '0'
&& st.charAt(i + 1) <= '7') {
code += st.charAt(i + 1);
i++;
if ((i < st.length() - 1) && st.charAt(i + 1) >= '0'
&& st.charAt(i + 1) <= '7') {
code += st.charAt(i + 1);
i++;
}
}
sb.append((char) Integer.parseInt(code, 8));
continue;
}
switch (nextChar) {
case '\\':
ch = '\\';
break;
case 'b':
ch = '\b';
break;
case 'f':
ch = '\f';
break;
case 'n':
ch = '\n';
break;
case 'r':
ch = '\r';
break;
case 't':
ch = '\t';
break;
case '\"':
ch = '\"';
break;
case '\'':
ch = '\'';
break;
// Hex Unicode: u????
case 'u':
if (i >= st.length() - 5) {
ch = 'u';
break;
}
int code = Integer.parseInt(
"" + st.charAt(i + 2) + st.charAt(i + 3)
+ st.charAt(i + 4) + st.charAt(i + 5), 16);
sb.append(Character.toChars(code));
i += 5;
continue;
default:
ch = nextChar; // added by Stefan
}
i++;
}
sb.append(ch);
}
return sb.toString();
} else
return s; // return original
}
static void setOptAll(Object o, Map fields) {
if (fields == null) return;
for (String field : keys(fields))
setOpt(o, field, fields.get(field));
}
static BufferedReader readLine_reader;
static String readLine() {
return (String) call(getJavaX(), "readLine");
}
static char unquoteCharacter(String s) {
assertTrue(s.startsWith("'") && s.length() > 1);
return unquote("\"" + s.substring(1, s.endsWith("'") ? s.length()-1 : s.length()) + "\"").charAt(0);
}
static A nuStubInnerObject(Class c) { try {
Class outerType = getOuterClass(c);
Constructor m = c.getDeclaredConstructor(outerType);
m.setAccessible(true);
return (A) m.newInstance(new Object[] {null});
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static Map getBracketMap(List tok) {
Map map = new HashMap();
List stack = new ArrayList();
for (int i = 1; i < l(tok); i+= 2) {
if (litlist("{", "(").contains(tok.get(i)))
stack.add(i);
else if (litlist("}", ")").contains(tok.get(i))) {
if (!empty(stack))
map.put(liftLast(stack), i);
}
}
return map;
}
static void printIndent(Object o) {
print(indentx(str(o)));
}
static void printIndent(String indent, Object o) {
print(indentx(indent, str(o)));
}
static void printIndent(int indent, Object o) {
print(indentx(indent, str(o)));
}
static boolean[] boolArrayFromBytes(byte[] a, int n) {
boolean[] b = new boolean[n];
int m = min(n, l(a)*8);
for (int i = 0; i < m; i++)
b[i] = (a[i/8] & 1 << (i & 7)) != 0;
return b;
}
// hopefully covers all cases :)
static String safeSubstring(String s, int x, int y) {
if (s == null) return null;
if (x < 0) x = 0;
if (x > s.length()) return "";
if (y < x) y = x;
if (y > s.length()) y = s.length();
return s.substring(x, y);
}
static String safeSubstring(String s, int x) {
return safeSubstring(s, x, l(s));
}
static Object getBot(String botID) {
return callOpt(getMainBot(), "getBot", botID);
}
static byte[] boolArrayToBytes(boolean[] a) {
byte[] b = new byte[(l(a)+7)/8];
for (int i = 0; i < l(a); i++)
if (a[i])
b[i/8] |= 1 << (i & 7);
return b;
}
static String className(Object o) {
return getClassName(o);
}
static boolean isLongConstant(String s) {
if (!s.endsWith("L")) return false;
s = s.substring(0, l(s)-1);
return isInteger(s);
}
static boolean endsWithLetter(String s) {
return nempty(s) && isLetter(last(s));
}
static String internIfLongerThan(String s, int l) {
return s == null ? null : l(s) >= l ? s.intern() : s;
}
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 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 byte max(byte[] c) {
byte x = -128;
for (byte d : c) if (d > x) x = d;
return x;
}
// f : Concept -> bool
static Concept conceptGraphSearch(Concept c, Object f) {
return conceptGraphSearch_impl(c, f, new IdentityHashMap());
}
static Concept conceptGraphSearch_impl(Concept c, Object f, IdentityHashMap seen) {
if (seen.containsKey(c)) return null;
if (callPred(f, c)) return c;
seen.put(c, c);
for (String field : conceptFields(c)) {
Object val = cget(c, field);
if (val instanceof Concept) {
Concept d = conceptGraphSearch_impl(((Concept) val), f, seen);
if (d != null) return d;
}
} return null;
}
static A assertEquals(Object x, A y) {
return assertEquals(null, x, y);
}
static A assertEquals(String msg, Object x, A y) {
if (!(x == null ? y == null : x.equals(y)))
throw fail((msg != null ? msg + ": " : "") + structure(x) + " != " + structure(y));
return y;
}
static boolean sameSnippetID(String a, String b) {
return a != null && b != null && parseSnippetID(a) == parseSnippetID(b);
}
// works on lists and strings and null
static int indexOfIgnoreCase(Object a, Object b) {
if (a == null) return -1;
if (a instanceof String) {
Matcher m = Pattern.compile((String) b, Pattern.CASE_INSENSITIVE + Pattern.LITERAL).matcher((String) a);
if (m.find()) return m.start(); else return -1;
}
if (a instanceof List) {
for (int i = 0; i < ((List) a).size(); i++) {
Object o = ((List) a).get(i);
if (o != null && ((String) o).equalsIgnoreCase((String) b))
return i;
}
return -1;
}
throw fail("Unknown type: " + a);
}
static LinkedHashMap litorderedmap(Object... x) {
LinkedHashMap map = new LinkedHashMap();
litmap_impl(map, x);
return map;
}
static boolean publicCommOn() {
return "1".equals(loadTextFile(new File(userHome(), ".javax/public-communication")));
}
static int[] toIntArray(List l) {
int[] a = new int[l(l)];
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
return a;
}
static void printStructure(String prefix, Object o) {
print(prefix + structure(o));
}
static void printStructure(Object o) {
print(structure(o));
}
static boolean eqOneOf(Object o, Object... l) {
for (Object x : l) if (eq(o, x)) return true; return false;
}
static String format(String pat, Object... args) {
return format3(pat, args);
}
static String getClassName(Object o) {
return o == null ? "null" : o.getClass().getName();
}
static Map synchroMap() {
return synchroHashMap();
}
static Map synchroMap(Map map) {
return Collections.synchronizedMap(map);
}
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 double parseDouble(String s) {
return Double.parseDouble(s);
}
// try to get our current process ID
static String getPID() {
String name = ManagementFactory.getRuntimeMXBean().getName();
return name.replaceAll("@.*", "");
}
static String callStaticAnswerMethod(List bots, String s) {
for (Object c : bots) try {
String answer = callStaticAnswerMethod(c, s);
if (!empty(answer)) return answer;
} catch (Throwable e) {
print("Error calling " + getProgramID(c));
e.printStackTrace();
}
return null;
}
static String callStaticAnswerMethod(Object c, String s) {
String answer = (String) callOpt(c, "answer", s, litlist(s));
if (answer == null)
answer = (String) callOpt(c, "answer", s);
return emptyToNull(answer);
}
static String callStaticAnswerMethod(String s) {
return callStaticAnswerMethod(mc(), s);
}
static String callStaticAnswerMethod(String s, List history) {
return callStaticAnswerMethod(mc(), s, history);
}
static String callStaticAnswerMethod(Object c, String s, List history) {
String answer = (String) callOpt(c, "answer", s, history);
if (answer == null)
answer = (String) callOpt(c, "answer", s);
return emptyToNull(answer);
}
static int randomID_defaultLength = 12;
static String randomID(int length) {
return makeRandomID(length);
}
static String randomID() {
return randomID(randomID_defaultLength);
}
static String getProgramName_cache;
static synchronized String getProgramName() {
if (getProgramName_cache == null)
getProgramName_cache = getSnippetTitle(getProgramID());
return getProgramName_cache;
}
static List getPlural_specials = ll("sheep", "fish");
static String getPlural(String s) {
s = toLower(s);
if (getPlural_specials.contains(s)) return s;
if (s.endsWith("y")) return dropSuffix("y", s) + "ies";
if (s.endsWith("ss")) return s + "es";
if (s.endsWith("s")) return s;
return s + "s";
}
static boolean callPred(Object condition, Object... args) {
return callPredicate(condition, args);
}
static Map singular_specials = litmap(
"children", "child", "images", "image", "chess", "chess");
static String singular(String s) {
if (s == null) return null;
s = toLower(s);
{ String _a_1 = singular_specials.get(s); if (!empty(_a_1)) return _a_1; }
if (s.endsWith("ness")) return s;
if (s.endsWith("ges")) return dropSuffix("s", s);
s = dropSuffix("es", s);
s = dropSuffix("s", s);
return s;
}
static Set synchroTreeSet() {
return Collections.synchronizedSet(new TreeSet ());
}
static A liftLast(List l) {
if (l.isEmpty()) return null;
int i = l(l)-1;
A a = l.get(i);
l.remove(i);
return a;
}
static Object mainBot;
static Object getMainBot() {
return mainBot;
}
static Class __javax;
static Class getJavaX() {
return __javax;
}
static boolean hasField(Object o, String field) {
return findField2(o, field) != null;
}
// match2 matches multiple "*" (matches a single token) wildcards and zero or one "..." wildcards (matches multiple tokens)
static String[] match2(List pat, List tok) {
// standard case (no ...)
int i = pat.indexOf("...");
if (i < 0) return match2_match(pat, tok);
pat = new ArrayList(pat); // We're modifying it, so copy first
pat.set(i, "*");
while (pat.size() < tok.size()) {
pat.add(i, "*");
pat.add(i+1, ""); // doesn't matter
}
return match2_match(pat, tok);
}
static String[] match2_match(List pat, List tok) {
List result = new ArrayList();
if (pat.size() != tok.size()) {
/*if (debug)
print("Size mismatch: " + structure(pat) + " vs " + structure(tok));*/
return null;
}
for (int i = 1; i < pat.size(); i += 2) {
String p = pat.get(i), t = tok.get(i);
/*if (debug)
print("Checking " + p + " against " + t);*/
if (eq(p, "*"))
result.add(t);
else if (!equalsIgnoreCase(unquote(p), unquote(t))) // bold change - match quoted and unquoted now
return null;
}
return result.toArray(new String[result.size()]);
}
static String format3(String pat, Object... args) {
if (args.length == 0) return pat;
List tok = javaTokPlusPeriod(pat);
int argidx = 0;
for (int i = 1; i < tok.size(); i += 2)
if (tok.get(i).equals("*"))
tok.set(i, format3_formatArg(argidx < args.length ? args[argidx++] : "null"));
return join(tok);
}
static String format3_formatArg(Object arg) {
if (arg == null) return "null";
if (arg instanceof String) {
String s = (String) arg;
return isIdentifier(s) || isNonNegativeInteger(s) ? s : quote(s);
}
if (arg instanceof Integer || arg instanceof Long) return String.valueOf(arg);
return quote(structure(arg));
}
static Class> _getClass(String name) {
try {
return Class.forName(name);
} catch (ClassNotFoundException e) {
return null;
}
}
static Class _getClass(Object o) {
return o instanceof Class ? (Class) o : o.getClass();
}
static Class _getClass(Object realm, String name) { try {
return getClass(realm).getClassLoader().loadClass(classNameToVM(name));
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static String indentx(String s) {
return indentx(indent_default, s);
}
static String indentx(int n, String s) {
return dropSuffix(repeat(' ', n), indent(n, s));
}
static String indentx(String indent, String s) {
return dropSuffix(indent, indent(indent, s));
}
static A last(List l) {
return l.isEmpty() ? null : l.get(l.size()-1);
}
static char last(String s) {
return empty(s) ? '#' : s.charAt(l(s)-1);
}
static int systemHashCode(Object o) {
return identityHashCode(o);
}
static boolean forbiddenPort(int port) {
return port == 5037; // adb
}
static String makeRandomID(int length) {
Random random = new Random();
char[] id = new char[length];
for (int i = 0; i < id.length; i++)
id[i] = (char) ((int) 'a' + random.nextInt(26));
return new String(id);
}
static Set joinSets(Set ... l) {
TreeSet set = new TreeSet();
for (Set o : l)
if (o != null)
set.addAll(o);
return set;
}
static void setOpt(Object o, String field, Object value) {
if (o == null) return;
if (o instanceof Class) setOpt((Class) o, field, value);
else try {
Field f = setOpt_findField(o.getClass(), field);
if (f != null)
smartSet(f, o, value);
} catch (Exception e) {
throw new RuntimeException(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() & Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
return null;
}
static Field setOpt_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;
}
// TODO: does not detect set type (hash/tree) when it's synchronized
static Set cloneSet(Set set) {
// assume mutex is equal to collection, which will be true unless you explicitly pass a mutex to synchronizedList() which no one ever does.
synchronized(set) {
return set instanceof TreeSet ? new TreeSet(set) : new HashSet(set);
}
}
static Class getOuterClass(Class c) { try {
String s = c.getName();
int i = s.lastIndexOf('$');
return Class.forName(substring(s, 0, i));
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static List parse3(String s) {
return dropPunctuation(javaTokPlusPeriod(s));
}
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 String parse3_cached_s;
static List parse3_cached_l;
static synchronized List parse3_cached(String s) {
if (neq(s, parse3_cached_s))
parse3_cached_l = parse3(parse3_cached_s = s);
return parse3_cached_l;
}
static String getSnippetTitle(String id) { try {
if (!isSnippetID(id)) return "?";
return trim(loadPageSilently(new URL("http://tinybrain.de:8080/tb-int/getfield.php?id=" + parseSnippetID(id) + "&field=title" + standardCredentials())));
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static List getMultiPorts() {
return (List) callOpt(getJavaX(), "getMultiPorts");
}
static String emptyToNull(String s) {
return eq(s, "") ? null : s;
}
static boolean isLetter(char c) {
return Character.isLetter(c);
}
public static long parseSnippetID(String snippetID) {
long id = Long.parseLong(shortenSnippetID(snippetID));
if (id == 0) throw fail("0 is not a snippet ID");
return id;
}
static String repeat(char c, int n) {
n = max(n, 0);
char[] chars = new char[n];
for (int i = 0; i < n; i++)
chars[i] = c;
return new String(chars);
}
static List repeat(A a, int n) {
List l = new ArrayList ();
for (int i = 0; i < n; i++)
l.add(a);
return l;
}
static int parseHexByte(String s) {
return Integer.parseInt(s, 16);
}
// start multi-port if none exists in current VM.
static void startMultiPort() {
List mp = getMultiPorts();
if (mp != null && mp.isEmpty())
callMain(hotwire("#1001672"));
}
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 boolean equalsIgnoreCase(String a, String b) {
return a == null ? b == null : a.equalsIgnoreCase(b);
}
static String shortenSnippetID(String snippetID) {
if (snippetID.startsWith("#"))
snippetID = snippetID.substring(1);
String httpBlaBla = "http://tinybrain.de/";
if (snippetID.startsWith(httpBlaBla))
snippetID = snippetID.substring(httpBlaBla.length());
return "" + parseLong(snippetID);
}
static String standardCredentials() {
String user = trim(loadTextFile(new File(userHome(), ".tinybrain/username")));
String pass = trim(loadTextFile(new File(userHome(), ".tinybrain/userpass")));
if (nempty(user) && nempty(pass))
return "&_user=" + urlencode(user) + "&_pass=" + urlencode(pass);
return "";
}
static void callMain(Object c, String... args) {
callOpt(c, "main", new Object[] {args});
}
static boolean isNonNegativeInteger(String s) {
return s != null && Pattern.matches("\\d+", s);
}
// This is made for NL parsing.
// It's javaTok extended with "..." token, "$n" and "#n" and
// special quotes (which are converted to normal ones).
static List javaTokPlusPeriod(String s) {
List tok = new ArrayList();
int l = s.length();
int i = 0;
while (i < l) {
int j = i;
char c; String cc;
// scan for whitespace
while (j < l) {
c = s.charAt(j);
cc = s.substring(j, Math.min(j+2, l));
if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
++j;
else if (cc.equals("/*")) {
do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
j = Math.min(j+2, l);
} else if (cc.equals("//")) {
do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
} else
break;
}
tok.add(s.substring(i, j));
i = j;
if (i >= l) break;
c = s.charAt(i);
cc = s.substring(i, Math.min(i+2, l));
// scan for non-whitespace
if (c == '\u201C' || c == '\u201D') c = '"'; // normalize quotes
if (c == '\'' || c == '"') {
char opener = c;
++j;
while (j < l) {
char _c = s.charAt(j);
if (_c == '\u201C' || _c == '\u201D') _c = '"'; // normalize quotes
if (_c == opener) {
++j;
break;
} else if (s.charAt(j) == '\\' && j+1 < l)
j += 2;
else
++j;
}
if (j-1 >= i+1) {
tok.add(opener + s.substring(i+1, j-1) + opener);
i = j;
continue;
}
} else if (Character.isJavaIdentifierStart(c))
do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || s.charAt(j) == '\'')); // for things like "this one's"
else if (Character.isDigit(c))
do ++j; while (j < l && Character.isDigit(s.charAt(j)));
else if (cc.equals("[[")) {
do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
j = Math.min(j+2, l);
} else if (cc.equals("[=") && i+2 < l && s.charAt(i+2) == '[') {
do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
j = Math.min(j+3, l);
} else if (s.substring(j, Math.min(j+3, l)).equals("..."))
j += 3;
else if (c == '$' || c == '#')
do ++j; while (j < l && Character.isDigit(s.charAt(j)));
else
++j;
tok.add(s.substring(i, j));
i = j;
}
if ((tok.size() % 2) == 0) tok.add("");
return tok;
}
static Field findField2(Object o, String field) {
if (o instanceof Class) return findField2_findStaticField((Class) o, field);
return findField2_findField(o.getClass(), field);
}
static Field findField2_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field) && (f.getModifiers() & Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
return null;
}
static Field findField2_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
return null;
}
static int identityHashCode(Object o) {
return System.identityHashCode(o);
}
public static boolean isSnippetID(String s) {
try {
parseSnippetID(s);
return true;
} catch (RuntimeException e) {
return false;
}
}
static ThreadLocal loadPage_charset = new ThreadLocal();
static boolean loadPage_allowGzip = true, loadPage_debug;
static boolean loadPage_anonymous; // don't send computer ID
static int loadPage_verboseness = 100000;
static int loadPage_retries = 60; // seconds
public static String loadPageSilently(String url) {
try {
return loadPageSilently(new URL(loadPage_preprocess(url)));
} catch (IOException e) { throw new RuntimeException(e); }
}
public static String loadPageSilently(URL url) {
try {
IOException e = null;
for (int tries = 0; tries < loadPage_retries; tries++)
try {
URLConnection con = openConnection(url);
return loadPage(con, url);
} catch (IOException _e) {
e = _e;
print("Trying proxy because of: " + e);
try {
return loadPageThroughProxy(str(url));
} catch (Throwable e2) {
print(" " + exceptionToStringShort(e2));
}
sleepSeconds(1);
}
throw e;
} catch (IOException e) { throw new RuntimeException(e); }
}
static String loadPage_preprocess(String url) {
if (url.startsWith("tb/"))
url = "tinybrain.de:8080/" + url;
if (url.indexOf("://") < 0)
url = "http://" + url;
return url;
}
public static String loadPage(String url) {
try {
url = loadPage_preprocess(url);
print("Loading: " + hideCredentials(url));
return loadPageSilently(new URL(url));
} catch (IOException e) { throw new RuntimeException(e); }
}
public static String loadPage(URL url) {
print("Loading: " + hideCredentials(url.toExternalForm()));
return loadPageSilently(url);
}
public static String loadPage(URLConnection con, URL url) throws IOException {
try {
if (!loadPage_anonymous) {
String computerID = getComputerID();
if (computerID != null)
con.setRequestProperty("X-ComputerID", computerID);
}
if (loadPage_allowGzip)
con.setRequestProperty("Accept-Encoding", "gzip");
} catch (Throwable e) {} // fails if within doPost
String contentType = con.getContentType();
if (contentType == null)
throw new IOException("Page could not be read: " + url);
//print("Content-Type: " + contentType);
String charset = loadPage_charset == null ? null : loadPage_charset.get();
if (charset == null) charset = loadPage_guessCharset(contentType);
InputStream in = con.getInputStream();
if ("gzip".equals(con.getContentEncoding())) {
if (loadPage_debug)
print("loadPage: Using gzip.");
in = new GZIPInputStream(in);
}
Reader r = new InputStreamReader(in, charset);
StringBuilder buf = new StringBuilder();
int n = 0;
while (true) {
int ch = r.read();
if (ch < 0)
break;
buf.append((char) ch);
++n;
if ((n % loadPage_verboseness) == 0) print(" " + n + " chars read");
}
return buf.toString();
}
static String loadPage_guessCharset(String contentType) {
Pattern p = Pattern.compile("text/[a-z]+;\\s+charset=([^\\s]+)\\s*");
Matcher m = p.matcher(contentType);
String match = m.matches() ? m.group(1) : null;
if (loadPage_debug)
print("loadPage: contentType=" + contentType + ", match: " + match);
/* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */
return or(match, "ISO-8859-1");
}
static String toLower(String s) {
return s == null ? null : s.toLowerCase();
}
static char toLower(char c) {
return Character.toLowerCase(c);
}
static boolean callPredicate(Object condition, Object... args) {
return isTrue(callF(condition, args));
}
static int indent_default = 2;
static String indent(int indent) {
return repeat(' ', indent);
}
static String indent(int indent, String s) {
return indent(repeat(' ', indent), s);
}
static String indent(String indent, String s) {
return indent + s.replace("\n", "\n" + indent);
}
static String indent(String s) {
return indent(indent_default, s);
}
static List indent(String indent, List lines) {
List l = new ArrayList();
for (String s : lines)
l.add(indent + s);
return l;
}
// compile JavaX source, load classes & return main class
// src can be a snippet ID or path to sources dir
// TODO: record injection?
static Class> hotwire(String src) {
try {
Class j = getJavaX();
synchronized(j) { // hopefully this goes well...
List libraries = new ArrayList();
File srcDir = (File) call(j, "transpileMain", src, libraries);
if (srcDir == null)
throw fail("transpileMain returned null (src=" + quote(src) + ")");
Object androidContext = get(j, "androidContext");
if (androidContext != null)
return (Class) call(j, "loadx2android", srcDir, src);
File classesDir = (File) call(j, "TempDirMaker_make");
String javacOutput = (String) call(j, "compileJava", srcDir, libraries, classesDir);
System.out.println(javacOutput);
URL[] urls = new URL[libraries.size()+1];
urls[0] = classesDir.toURI().toURL();
for (int i = 0; i < libraries.size(); i++)
urls[i+1] = libraries.get(i).toURI().toURL();
// make class loader
URLClassLoader classLoader = new URLClassLoader(urls);
// load & return main class
Class> theClass = classLoader.loadClass("main");
callOpt(j, "registerSourceCode", theClass, loadTextFile(new File(srcDir, "main.java")));
call(j, "setVars", theClass, isSnippetID(src) ? src : null);
if (isSnippetID(src))
callOpt(j, "addInstance", src, theClass);
if (!_inCore())
hotwire_copyOver(theClass);
return theClass;
}
} catch (Exception e) {
throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
}
}
static List dropPunctuation_keep = litlist("*", "<", ">");
static List dropPunctuation(List tok) {
tok = new ArrayList(tok);
for (int i = 1; i < tok.size(); i += 2) {
String t = tok.get(i);
if (t.length() == 1 && !Character.isLetter(t.charAt(0)) && !Character.isDigit(t.charAt(0)) && !dropPunctuation_keep.contains(t)) {
tok.set(i-1, tok.get(i-1) + tok.get(i+1));
tok.remove(i);
tok.remove(i);
i -= 2;
}
}
return tok;
}
static String dropPunctuation(String s) {
return join(dropPunctuation(nlTok(s)));
}
static File userDir() {
return new File(userHome());
}
static File userDir(String path) {
return new File(userHome(), path);
}
static boolean isAndroid() { return System.getProperty("java.vendor").toLowerCase().indexOf("android") >= 0; }
static String hideCredentials(String url) {
return url.replaceAll("&_pass=[^&]*", "&_pass=");
}
static String exceptionToStringShort(Throwable e) {
e = getInnerException(e);
String msg = unnull(e.getMessage());
if (msg.indexOf("Error") < 0 && msg.indexOf("Exception") < 0)
return baseClassName(e) + ": " + msg;
else
return msg;
}
static URLConnection openConnection(URL url) { try {
ping();
return url.openConnection();
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static List nlTok(String s) {
return javaTokPlusPeriod(s);
}
static String urlencode(String x) {
try {
return URLEncoder.encode(unnull(x), "UTF-8");
} catch (UnsupportedEncodingException e) { throw new RuntimeException(e); }
}
static boolean _inCore() {
return false;
}
static String getComputerID() { try {
return computerID();
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static String loadPageThroughProxy(String url) {
adbForward();
DialogIO io = talkToOpt("localhost", 4995); // Test ADB Bridge
if (io == null)
io = talkToOpt(gateway(), 4999); // Phone, Public Comm Bot
if (io == null) return null;
String answer = unquote(io.ask(forward("Awareness", format("loadPage *", url))));
io.close();
if (swic(answer, "ok "))
return answer.substring(3);
throw fail(answer);
}
static void hotwire_copyOver(Class c) {
synchronized(StringBuffer.class) {
for (String field : litlist("print_log", "print_silent", "androidContext")) {
Object o = getOpt(mc(), field);
if (o != null)
setOpt(c, field, o);
}
Object mainBot = getMainBot();
if (mainBot != null)
setOpt(c, "mainBot", mainBot);
setOpt(c, "creator_class", new WeakReference(mc()));
}
}
static void sleepSeconds(double s) {
if (s > 0) sleep(round(s*1000));
}
static A or(A a, A b) {
return a != null ? a : b;
}
static boolean swic(String a, String b) {
return startsWithIgnoreCase(a, b);
}
static String _computerID;
public static String computerID() { try {
if (_computerID == null) {
File file = new File(userHome(), ".tinybrain/computer-id");
_computerID = loadTextFile(file.getPath(), null);
if (_computerID == null) {
_computerID = makeRandomID(12);
saveTextFile(file.getPath(), _computerID);
}
}
return _computerID;
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static TimedCache adbForward_list = new TimedCache(10);
static void adbForward() {
if (!isOnPATH("adb"))
print("adb not on path");
else {
backtick_verbose = true;
String list = adbForward_list.get(new Object() { Object get() { return backtick("adb forward --list"); }});
if (!contains(list, "4995"))
backtick("adb forward tcp:4995 tcp:4999");
}
}
static String gateway() {
return first(detectGateways());
}
static DialogIO talkToOpt(String host, int port) {
try {
return talkTo(host, port);
} catch (Throwable e) {
return null;
}
}
static String baseClassName(String className) {
return substring(className, className.lastIndexOf('.'));
}
static String baseClassName(Object o) {
return baseClassName(getClassName(o));
}
static String forward(String bot, String msg, Object... args) {
return format("please forward to bot *: *", bot, format(msg, args));
}
static long round(double d) {
return Math.round(d);
}
static boolean startsWithIgnoreCase(String a, String b) {
return a != null && a.regionMatches(true, 0, b, 0, b.length());
}
static TimedCache> detectGateways_cache = new TimedCache(5);
static synchronized List detectGateways() { try {
if (detectGateways_cache.has()) return detectGateways_cache.getNoClean();
boolean win = isWindows();
String s = backtick(win ? "ipconfig" : "ip route show");
TreeSet ips = new TreeSet();
for (String line : toLines(s))
if (indexOfIgnoreCase(line, win ? "Gateway" : "via") >= 0)
ips.addAll(regexpAll("\\d+\\.\\d+\\.\\d+\\.\\d+", line));
return detectGateways_cache.set(new ArrayList(ips));
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static DialogIO talkTo(int port) {
return talkTo("localhost", port);
}
static int talkTo_defaultTimeout = 10000;
static DialogIO talkTo(String ip, int port) { try {
final Socket s = new Socket();
s.connect(new InetSocketAddress(ip, port), talkTo_defaultTimeout);
//print("Talking to " + ip + ":" + port);
final Writer w = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
final BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));
return new DialogIO() {
boolean isLocalConnection() {
return s.getInetAddress().isLoopbackAddress();
}
boolean isStillConnected() {
return !(eos || s.isClosed());
}
void sendLine(String line) { try {
w.write(line + "\n");
w.flush();
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
String readLineImpl() { try {
return in.readLine();
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
void close() {
try {
s.close();
} catch (IOException e) {
// whatever
}
}
Socket getSocket() {
return s;
}
};
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static int backtick_exitValue;
static boolean backtick_verbose;
public static String backtick(String cmd) { try {
File outFile = File.createTempFile("_backtick", "");
File scriptFile = File.createTempFile("_backtick", isWindows() ? ".bat" : "");
String command = cmd + " >" + bashQuote(outFile.getPath()) + " 2>&1";
//Log.info("[Backtick] " + command);
try {
if (backtick_verbose)
print("backtick: command " + command);
saveTextFile(scriptFile.getPath(), command);
String[] command2;
if (isWindows())
command2 = new String[] { scriptFile.getPath() };
else
command2 = new String[] { "/bin/bash", scriptFile.getPath() };
if (backtick_verbose)
print("backtick: command2 " + structure(command2));
Process process = Runtime.getRuntime().exec(command2);
try {
process.waitFor();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
backtick_exitValue = process.exitValue();
if (backtick_verbose)
System.out.println("Process return code: " + backtick_exitValue);
String result = loadTextFile(outFile.getPath(), "");
if (backtick_verbose)
print("[[\n" + result + "]]");
return result;
} finally {
scriptFile.delete();
}
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static boolean isOnPATH(String cmd) {
String path = System.getenv("PATH");
List dirs = splitAt(path, File.pathSeparator);
String c = isWindows() ? cmd + ".exe" : cmd;
for (String dir : dirs)
if (new File(dir, c).isFile())
return true;
return false;
}
public static boolean isWindows() {
return System.getProperty("os.name").contains("Windows");
}
/** possibly improvable */
static String bashQuote(String text) {
if (text == null) return null;
return "\"" + text
.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\n", "\\n")
.replace("\r", "\\r") + "\"";
}
static String bashQuote(File f) {
return bashQuote(f.getAbsolutePath());
}
static List splitAt(String s, String splitter) {
List parts = new ArrayList();
int i = 0;
if (s != null)
while (i < l(s)) {
int j = indexOf(s, splitter, i);
if (j < 0) j = l(s);
parts.add(substring(s, i, j));
i = j+l(splitter);
}
return parts;
}
static List toLines(File f) {
return toLines(loadTextFile(f));
}
public static List toLines(String s) {
List lines = new ArrayList();
if (s == null) return lines;
int start = 0;
while (true) {
int i = toLines_nextLineBreak(s, start);
if (i < 0) {
if (s.length() > start) lines.add(s.substring(start));
break;
}
lines.add(s.substring(start, i));
if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n')
i += 2;
else
++i;
start = i;
}
return lines;
}
private static int toLines_nextLineBreak(String s, int start) {
for (int i = start; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '\r' || c == '\n')
return i;
}
return -1;
}
static List regexpAll(String pattern, String text) {
List matches = new ArrayList();
Matcher matcher = Pattern.compile(pattern).matcher(text);
while (matcher.find())
matches.add(matcher.group());
return matches;
}
static int indexOf(List l, A a, int startIndex) {
if (l == null) return -1;
for (int i = startIndex; i < l(l); i++)
if (eq(l.get(i), a))
return i;
return -1;
}
static int indexOf(List l, A a) {
if (l == null) return -1;
return l.indexOf(a);
}
static int indexOf(String a, String b) {
return a == null || b == null ? -1 : a.indexOf(b);
}
static int indexOf(String a, String b, int i) {
return a == null || b == null ? -1 : a.indexOf(b, i);
}
static int indexOf(String a, char b, int i) {
return a == null ? -1 : a.indexOf(b, i);
}
static int indexOf(String a, int i, String b) {
return a == null || b == null ? -1 : a.indexOf(b, i);
}
static int indexOf(A[] x, A a) {
if (x == null) return -1;
for (int i = 0; i < l(x); i++)
if (eq(x[i], a))
return i;
return -1;
}
static class Options extends ConceptL {
Options() {}
Options(List l) { super(l); }
Options(Concept... l) { super(asList(l)); }
}
static class ConceptL extends Concept implements Iterable {
RefL l = new RefL();
ConceptL() {}
ConceptL(List l) { this.l.addAll_(l); }
void addAll(List l) {
this.l.addAll(l);
}
public Iterator iterator() {
return l.iterator();
}
public List getList() {
return l;
}
}
static class Str extends Concept {
String name;
List otherNames = new ArrayList();
Str() {}
Str(String name) {
this.name = name;}
public String toString() { return name; }
}
static abstract class Collector {
abstract boolean add(A a); // return true if full after this
boolean full() { return false; }
boolean contains(A a) { return false; }
}
static boolean DynamicObject_loading;
static class DynamicObject {
String className; // just the name, without the "main$"
Map fieldValues = new TreeMap();
DynamicObject() {}
// className = just the name, without the "main$"
DynamicObject(String className) {
this.className = className;}
}
static class TimedCache {
long timeout;
A value;
long set;
// stats
int stores, fails, hits;
// 0 = no timeout
TimedCache(double timeoutSeconds) {
timeout = toMS(timeoutSeconds);
}
synchronized A set(A a) {
++stores;
value = a;
set = now();
return a;
}
synchronized boolean has() {
clean();
if (set != 0) {
++hits; return true;
}
++fails; return false;
}
synchronized A get() {
clean();
if (set != 0) ++hits; else ++fails;
return value;
}
synchronized A get(Object makerFunction) {
if (has()) return getNoClean();
A a = (A) ( callF(makerFunction));
return set(a);
}
synchronized A getNoClean() {
return value;
}
// clear if timed out
synchronized void clean() {
if (timeout > 0 && now() > set+timeout) clear();
}
// clear now
synchronized void clear() {
set = 0;
value = null;
}
synchronized String stats() {
return "Stores: " + stores + ", hits: " + hits + ", fails: " + fails;
}
}
static abstract class DialogIO {
String line;
boolean eos, loud;
abstract String readLineImpl();
abstract boolean isStillConnected();
abstract void sendLine(String line);
abstract boolean isLocalConnection();
abstract Socket getSocket();
abstract void close();
int getPort() { return getSocket().getPort(); }
boolean helloRead;
String readLineNoBlock() {
String l = line;
line = null;
return l;
}
boolean waitForLine() { try {
if (line != null) return true;
//print("Readline");
line = readLineImpl();
//print("Readline done: " + line);
if (line == null) eos = true;
return line != null;
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
String readLine() {
waitForLine();
helloRead = true;
return readLineNoBlock();
}
String ask(String s, Object... args) {
if (loud) return askLoudly(s, args);
if (!helloRead) readLine();
if (args.length != 0) s = format3(s, args);
sendLine(s);
return readLine();
}
String askLoudly(String s, Object... args) {
if (!helloRead) readLine();
if (args.length != 0) s = format3(s, args);
print("> " + s);
sendLine(s);
String answer = readLine();
print("< " + answer);
return answer;
}
void pushback(String l) {
if (line != null)
throw fail();
line = l;
helloRead = false;
}
}
static abstract class DialogHandler {
abstract void run(DialogIO io);
}
static class Matches {
String[] m;
String get(int i) { return i < m.length ? m[i] : null; }
String unq(int i) { return unquote(get(i)); }
String fsi(int i) { return formatSnippetID(unq(i)); }
String fsi() { return fsi(0); }
String tlc(int i) { return unq(i).toLowerCase(); }
boolean bool(int i) { return "true".equals(unq(i)); }
String rest() { return m[m.length-1]; } // for matchStart
int psi(int i) { return Integer.parseInt(unq(i)); }
}
static class MultiMap {
Map > data = new HashMap >();
MultiMap() {}
MultiMap(boolean useTreeMap) { if (useTreeMap) data = new TreeMap(); }
MultiMap(MultiMap map) { putAll(map); }
synchronized void put(A key, B value) {
List list = data.get(key);
if (list == null)
data.put(key, list = new ArrayList());
list.add(value);
}
synchronized void addAll(A key, Collection values) {
putAll(key, values);
}
synchronized void addAllIfNotThere(A key, Collection values) {
for (B value : values)
setPut(key, value);
}
synchronized void setPut(A key, B value) {
if (!containsPair(key, value))
put(key, value);
}
synchronized boolean containsPair(A key, B value) {
return get(key).contains(value);
}
synchronized void putAll(A key, Collection values) {
for (B value : values)
put(key, value);
}
synchronized void removeAll(A key, Collection values) {
for (B value : values)
remove(key, value);
}
synchronized List