import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.math.*;


import java.text.NumberFormat;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.geom.AffineTransform;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.awt.geom.*;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;
import javax.swing.Timer;
import java.awt.font.GlyphVector;
import javax.swing.undo.UndoManager;
import java.awt.datatransfer.UnsupportedFlavorException;
class main {
public static void main(final String[] args) throws Exception {
  String q = "is 60 bigger than 50?";
  String a = ai_solveQuestionInSubSpace(q);
  if (a != null)
    print("Answer: " + a);
  else
    print("No answer found");
    
  setRegularGCCountdown(1);
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static volatile StringBuffer print_log = local_log; // might be redirected, e.g. to main bot

// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
//static int print_maxLineLength = 0; // 0 = unset

static boolean print_silent; // total mute if set

static Object print_byThread_lock = new Object();
static volatile ThreadLocal<F1<String, Boolean>> print_byThread; // special handling by thread

static void print() {
  print("");
}

// slightly overblown signature to return original object...
static <A> A print(A o) {
  ping();
  if (print_silent) return o;
  String s = String.valueOf(o) + "\n";
  print_noNewLine(s);
  return o;
}

static void print_noNewLine(String s) {
  if (print_byThread != null) {
    F1<String, Boolean> f = print_byThread.get();
    if (f != null)
      if (isFalse(f.get(s))) return;
  }

  print_raw(s);
}

static void print_raw(String s) {
  s = fixNewLines(s);
  // TODO if (print_maxLineLength != 0)
  StringBuffer loc = local_log;
  StringBuffer buf = print_log;
  int loc_max = print_log_max;
  if (buf != loc && buf != null) {
    print_append(buf, s, print_log_max);
    loc_max = local_log_max;
  }
  if (loc != null) 
    print_append(loc, s, loc_max);
  System.out.print(s);
}

static void print(long l) {
  print(String.valueOf(l));
}

static void print(char c) {
  print(String.valueOf(c));
}

static void print_append(StringBuffer buf, String s, int max) {
  synchronized(buf) {
    buf.append(s);
    max /= 2;
    if (buf.length() > max) try {
      int newLength = max/2;
      int ofs = buf.length()-newLength;
      String newString = buf.substring(ofs);
      buf.setLength(0);
      buf.append("[...] ").append(newString);
    } catch (Exception e) {
      buf.setLength(0);
    }
  }
}
static RuntimeException asRuntimeException(Throwable t) {
  if (t instanceof Error)
    _handleError((Error) t);
  return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static void setRegularGCCountdown(int countdown) {
  setOpt(javax(), "regularGC_countdown", countdown);
}
static double ai_solveQuestionInSubSpace_defautTimeOut = 60;

static String ai_solveQuestionInSubSpace(T3<String, String, String> seed, T3<String, String, String> lookingFor) {
  return ai_solveQuestionInSubSpace(seed, lookingFor, ai_solveQuestionInSubSpace_defautTimeOut);
}

static String ai_solveQuestionInSubSpace(final T3<String, String, String> seed, final T3<String, String, String> lookingFor, double maxSeconds) {
  return (String) eitherAOpt(evalWithTimeout(maxSeconds, new Object() { Object get() { try { return  ai_solveQuestionInSubSpace_noTimeOut(seed, lookingFor) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "ai_solveQuestionInSubSpace_noTimeOut(seed, lookingFor)"; }}));
}

static String ai_solveQuestionInSubSpace(String q) {
  return ai_solveQuestionInSubSpace(
    triple(q, "should be", "answered"),
    triple("$X", "answers", q));
}
static Map<Thread, Boolean> _registerThread_threads = newWeakHashMap();

static Thread _registerThread(Thread t) {
  _registerThread_threads.put(t, true);
  return t;
}

static void _registerThread() { _registerThread(Thread.currentThread()); }


static String fixNewLines(String s) {
  return s.replace("\r\n", "\n").replace("\r", "\n");
}
static Set<Thread> evalWithTimeout_inTime = synchroSet();
static Set<Thread> evalWithTimeout_allThreads = synchroSet();

// Either calculation result or Thread with timed-out computation
// Tries to interrupt thread after timeout
// Errors are rethrown.
static Either<Object, Thread> evalWithTimeout(int timeoutMS, final Object r) {
  final Flag done = new Flag();
  final Flag doneWaiting = new Flag();
  final Var var = new Var();
  final Var<Throwable> error = new Var();
  
  Thread t = startThread(new Runnable() { public void run() { try { 
    try {
      try {
        var.set(callF(r));
      } finally {
        evalWithTimeout_allThreads.remove(currentThread());
      }
    } catch (Throwable e) {
      error.set(e);
      if (doneWaiting.isUp())
        printStackTrace(e);
    } finally {
      done.raise();
    }
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "try {\r\n      try {\r\n        var.set(callF(r));\r\n      } finally {\r\n        ev..."; }});
  evalWithTimeout_inTime.add(t);
  evalWithTimeout_allThreads.add(t);
  try {
    done.waitUntilUp(timeoutMS);
    doneWaiting.raise();
  } finally {
    evalWithTimeout_inTime.remove(t);
  }
    
  // timeout! cancel/interrupt and return thread object
  if (!done.isUp()) {
    cancelAndInterruptThread(t);
    return either2(t);
  }
  
  // thread ended with error
  if (error.get() != null) rethrow(error.get());
  
  // thread ended naturally
  return either1(var.get());
}

static Either<Object, Thread> evalWithTimeout(double timeoutSeconds, final Object r) {
  return evalWithTimeout(toMS_int(timeoutSeconds), r);
}
static RuntimeException rethrow(Throwable e) {
  throw asRuntimeException(e);
}
static AtomicLong _handleError_nonVMErrors = new AtomicLong();
static AtomicLong _handleError_vmErrors = new AtomicLong();
static AtomicLong _handleError_outOfMemoryErrors = new AtomicLong();
static volatile long _handleError_lastOutOfMemoryError;
static volatile Error _handleError_lastHardError;

static void _handleError(Error e) {
  if (!(e instanceof VirtualMachineError)) {
    incAtomicLong(_handleError_nonVMErrors);
    return;
  }
  
  print("\nHARD ERROR\n");
  printStackTrace2(e);
  print("\nHARD ERROR\n");
  _handleError_lastHardError = e;
  
  incAtomicLong(_handleError_vmErrors);
  if (e instanceof OutOfMemoryError) {
    incAtomicLong(_handleError_outOfMemoryErrors);
    _handleError_lastOutOfMemoryError = sysNow();
  }
}
static volatile boolean ping_pauseAll;
static int ping_sleep = 100; // poll pauseAll flag every 100
static volatile boolean ping_anyActions;
static Map<Thread, Object> ping_actions = newWeakHashMap();

// always returns true
static boolean ping() {
  if (ping_pauseAll || ping_anyActions) ping_impl();
  return true;
}

// returns true when it slept
static boolean ping_impl() { try {
  if (ping_pauseAll && !isAWTThread()) {
    do
      Thread.sleep(ping_sleep);
    while (ping_pauseAll);
    return true;
  }
  
  if (ping_anyActions) {
    Object action;
    synchronized(ping_actions) {
      action = ping_actions.get(currentThread());
      if (action instanceof Runnable)
        ping_actions.remove(currentThread());
      if (ping_actions.isEmpty()) ping_anyActions = false;
    }
    
    if (action instanceof Runnable)
      ((Runnable) action).run();
    else if (eq(action, "cancelled"))
      throw fail("Thread cancelled.");
  }
  
  return false;
} catch (Exception __e) { throw rethrow(__e); } }
static boolean isFalse(Object o) {
  return eq(false, o);
}
static <A, B> Map<A, B> newWeakHashMap() {
  return _registerWeakMap(synchroMap(new WeakHashMap()));
}
static <A, B> A eitherAOpt(Either<A, B> e) {
  return e != null && e.isA() ? e.a() : null;
}
static Field setOpt_findField(Class c, String field) {
  HashMap<String, Field> map;
  synchronized(getOpt_cache) {
    map = getOpt_cache.get(c);
    if (map == null)
      map = getOpt_makeCache(c);
  }
  return map.get(field);
}

static void setOpt(Object o, String field, Object value) { try {
  if (o == null) return;
  
  
    if (o instanceof _SetField) { ((_SetField) o)._setField(field, value); return; }
  
  
  Class c = o.getClass();
  HashMap<String, Field> map;
  
  synchronized(getOpt_cache) {
    map = getOpt_cache.get(c);
    if (map == null)
      map = getOpt_makeCache(c);
  }
  
  if (map == getOpt_special) {
    if (o instanceof Class) {
      setOpt((Class) o, field, value);
      return;
    }
    
    // It's probably a subclass of Map. Use raw method
    setOpt_raw(o, field, value);
    return;
  }
  
  Field f = map.get(field);
  if (f != null)
    smartSet(f, o, value); // possible improvement: skip setAccessible
} catch (Exception __e) { throw rethrow(__e); } }

static void setOpt(Class c, String field, Object value) {
  if (c == null) return;
  try {
    Field f = setOpt_findStaticField(c, field);
    if (f != null)
      smartSet(f, null, value);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
  
static Field setOpt_findStaticField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  return null;
}
static <A, B, C> T3<A, B, C> triple(A a, B b, C c) {
  return new T3(a, b, c);
}
static Class javax() {
  return getJavaX();
}
// get purpose 1: access a list/array/map (safer version of x.get(y))

static <A> A get(List<A> l, int idx) {
  return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null;
}

// seems to conflict with other signatures
/*static <A, B> B get(Map<A, B> map, A key) {
  ret map != null ? map.get(key) : null;
}*/

static <A> A get(A[] l, int idx) {
  return idx >= 0 && idx < l(l) ? l[idx] : null;
}

// default to false
static boolean get(boolean[] l, int idx) {
  return idx >= 0 && idx < l(l) ? l[idx] : false;
}

// get purpose 2: access a field by reflection or a map

static Object get(Object o, String field) {
  try {
    if (o instanceof Class) return get((Class) o, field);
    
    if (o instanceof Map)
      return ((Map) o).get(field);
      
    Field f = getOpt_findField(o.getClass(), field);
    if (f != null) {
      f.setAccessible(true);
      return f.get(o);
    }
      
    
      if (o instanceof DynamicObject)
        return ((DynamicObject) o).fieldValues.get(field);
    
  } catch (Exception e) {
    throw asRuntimeException(e);
  }
  throw new RuntimeException("Field '" + field + "' not found in " + o.getClass().getName());
}

static Object get_raw(Object o, String field) {
  try {
    Field f = get_findField(o.getClass(), field);
    f.setAccessible(true);
    return f.get(o);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

static Object get(Class c, String field) {
  try {
    Field f = get_findStaticField(c, field);
    f.setAccessible(true);
    return f.get(null);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

static Field get_findStaticField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
}

static Field get_findField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field))
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
}

static String ai_solveQuestionInSubSpace_noTimeOut(T3<String, String, String> seed, T3<String, String, String> lookingFor) {
  AI_SubSpace subSpace = new AI_SubSpace(1000);
  ai_setSubSpace(subSpace);
  try {
    subSpace.postTriple(seed); // force it to be posted in SubSpace
    
    int step = 0;
    while (!subSpace.limitReached()) {
      ++step;
      int n = l(subSpace.webs);
      print("Solving question: " + lookingFor + " (step " + step + ", " + n + "/" + subSpace.limit + ")");
      
      try {
        for (String term : concatLists(keys(subSpace.index), keys(subSpace.websByID)))
          ai_speculate(term);
      } catch (Throwable __e) { print(exceptionToStringShort(__e)); }
      
      List<Web> newWebs = cloneSubList(subSpace.webs, n);
      ai_spec_possiblyToActually(newWebs);
      
      String a = ai_text(lookingFor);
      if (a != null) return a;
  
      if (l(subSpace.webs) == n) {
        print("No new webs made");
        break;
      }
    }
    
    return null;
  } finally {
    ai_setSubSpace(null);
  }
}


static WeakHashMap<Class, ArrayList<Method>> callF_cache = new WeakHashMap();


  static <A> A callF(F0<A> f) {
    return f == null ? null : f.get();
  }



  static <A, B> B callF(F1<A, B> f, A a) {
    return f == null ? null : f.get(a);
  }


static Object callF(Object f, Object... args) { try {
  if (f instanceof String)
    return callMC((String) f, args);
  if (f instanceof Runnable) {
    ((Runnable) f).run();
    return null;
  }
  if (f == null) return null;
  
  Class c = f.getClass();
  ArrayList<Method> methods;
  synchronized(callF_cache) {
    methods = callF_cache.get(c);
    if (methods == null)
      methods = callF_makeCache(c);
  }
  
  int n = l(methods);
  if (n == 0) throw fail("No get method in " + getClassName(c));
  if (n == 1) return methods.get(0).invoke(f, args);
  for (int i = 0; i < n; i++) {
    Method m = methods.get(i);
    if (call_checkArgs(m, args, false))
      return m.invoke(f, args);
  }
  throw fail("No matching get method in " + getClassName(c));
} catch (Exception __e) { throw rethrow(__e); } }

// used internally
static ArrayList<Method> callF_makeCache(Class c) {
  ArrayList<Method> l = new ArrayList();
  Class _c = c;
  do {
    for (Method m : _c.getDeclaredMethods())
      if (m.getName().equals("get")) {
        m.setAccessible(true);
        l.add(m);
      }
    if (!l.isEmpty()) break;
    _c = _c.getSuperclass();
  } while (_c != null);
  callF_cache.put(c, l);
  return l;
}
static void cancelAndInterruptThread(Thread t) {
  if (t == null) return;
  cancelThread(t);
  t.interrupt();
}
static <A, B> Either<A, B> either2(B b) {
  return new Either(2, b);
}
static <A, B> Either<A, B> either1(A a) {
  return new Either(1, a);
}
static void setOpt_raw(Object o, String field, Object value) { try {
  if (o == null) return;
  if (o instanceof Class) setOpt_raw((Class) o, field, value);
  else {
    Field f = setOpt_raw_findField(o.getClass(), field);
    if (f != null)
      smartSet(f, o, value);
  }
} catch (Exception __e) { throw rethrow(__e); } }

static void setOpt_raw(Class c, String field, Object value) { try {
  if (c == null) return;
  Field f = setOpt_raw_findStaticField(c, field);
  if (f != null)
    smartSet(f, null, value);
} catch (Exception __e) { throw rethrow(__e); } }
  
static Field setOpt_raw_findStaticField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  return null;
}

static Field setOpt_raw_findField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field))
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  return null;
}
// TODO: test if android complains about this
static boolean isAWTThread() {
  if (isAndroid()) return false;
  if (isHeadless()) return false;
  return isAWTThread_awt();
}

static boolean isAWTThread_awt() {
  return SwingUtilities.isEventDispatchThread();
}
static Thread currentThread() {
  return Thread.currentThread();
}
static Throwable printStackTrace(Throwable e) {
  // we go to system.out now - system.err is nonsense
  print(getStackTrace(e));
  return e;
}

static void printStackTrace() {
  printStackTrace(new Throwable());
}

static void printStackTrace(String msg) {
  printStackTrace(new Throwable(msg));
}

/*static void printStackTrace(S indent, Throwable e) {
  if (endsWithLetter(indent)) indent += " ";
  printIndent(indent, getStackTrace(e));
}*/
static boolean eq(Object a, Object b) {
  return a == null ? b == null : a == b || a.equals(b);
}
static RuntimeException fail() { throw new RuntimeException("fail"); }
static RuntimeException fail(Throwable e) { throw asRuntimeException(e); }
static RuntimeException fail(Object msg) { throw new RuntimeException(String.valueOf(msg)); }
static RuntimeException fail(String msg) { throw new RuntimeException(msg == null ? "" : msg); }
static RuntimeException fail(String msg, Throwable innerException) { throw new RuntimeException(msg, innerException); }

static <A> List<A> concatLists(Collection<A>... lists) {
  List<A> l = new ArrayList();
  for (Collection<A> list : lists)
    if (list != null)
      l.addAll(list);
  return l;
}

static <A> List<A> concatLists(Collection<List<A>> lists) {
  List<A> l = new ArrayList();
  for (List<A> list : lists)
    if (list != null)
      l.addAll(list);
  return l;
}

static void ai_setSubSpace(AI_SubSpace subSpace) {
  ai_currentSubSpace.set(subSpace);
  ai_setCacheChecker(subSpace);
  ai_setPostHandler(subSpace);
}
static List _registerWeakMap_preList;

static <A> A _registerWeakMap(A map) {
  if (javax() == null) {
    // We're in class init
    if (_registerWeakMap_preList == null) _registerWeakMap_preList = synchroList();
    _registerWeakMap_preList.add(map);
    return map;
  }
  
  try {
    call(javax(), "_registerWeakMap", map);
  } catch (Throwable e) {
    printException(e);
    print("Upgrade JavaX!!");
  }
  return map;
}

static void _onLoad_registerWeakMap() {
  assertNotNull(javax());
  if (_registerWeakMap_preList == null) return;
  for (Object o : _registerWeakMap_preList)
    _registerWeakMap(o);
  _registerWeakMap_preList = null;
}
static String ai_text(String a, String b, String c) {
  return web_text(first(ai_search_dollarX(a, b, c)));
}

static String ai_text(T3<String, String, String> t) {
  return ai_text(t.a, t.b, t.c);
}
static Map synchroMap() {
  return synchroHashMap();
}

static <A, B> Map<A, B> synchroMap(Map<A, B> map) {
  return Collections.synchronizedMap(map);
}
static <A, B> Set<A> keys(Map<A, B> map) {
  return map == null ? new HashSet() : map.keySet();
}

static Set keys(Object map) {
  return keys((Map) map);
}




  static <A, B> Set<A> keys(MultiMap<A, B> mm) {
    return mm.keySet();
  }

static <A> Set<A> synchroSet() {
  return synchroHashSet();
}

static <A> Set<A> synchroSet(Set<A> set) {
  return Collections.synchronizedSet(set);
}
//static final Map<Class, HashMap<S, Field>> getOpt_cache = newDangerousWeakHashMap(f getOpt_special_init);

static class getOpt_Map extends WeakHashMap {
  getOpt_Map() {
    if (getOpt_special == null) getOpt_special = new HashMap();
    clear();
  }
  
  public void clear() {
    super.clear();
    //print("getOpt clear");
    put(Class.class, getOpt_special);
    put(String.class, getOpt_special);
  }
}

static final Map<Class, HashMap<String, Field>> getOpt_cache = _registerDangerousWeakMap(synchroMap(new getOpt_Map()));
//static final Map<Class, HashMap<S, Field>> getOpt_cache = _registerWeakMap(synchroMap(new getOpt_Map));
static HashMap getOpt_special; // just a marker

/*static void getOpt_special_init(Map map) {
  map.put(Class.class, getOpt_special);
  map.put(S.class, getOpt_special);
}*/

static Object getOpt_cached(Object o, String field) { try {
  if (o == null) return null;

  Class c = o.getClass();
  HashMap<String, Field> map;
  synchronized(getOpt_cache) {
    map = getOpt_cache.get(c);
    if (map == null)
      map = getOpt_makeCache(c);
  }
  
  if (map == getOpt_special) {
    if (o instanceof Class)
      return getOpt((Class) o, field);
    /*if (o instanceof S)
      ret getOpt(getBot((S) o), field);*/
    if (o instanceof Map)
      return ((Map) o).get(field);
  }
    
  Field f = map.get(field);
  if (f != null) return f.get(o);
  
    if (o instanceof DynamicObject)
      return ((DynamicObject) o).fieldValues.get(field);
  
  return null;
} catch (Exception __e) { throw rethrow(__e); } }

// used internally - we are in synchronized block
static HashMap<String, Field> getOpt_makeCache(Class c) {
  HashMap<String, Field> map;
  if (isSubtypeOf(c, Map.class))
    map = getOpt_special;
  else {
    map = new HashMap();
    Class _c = c;
    do {
      for (Field f : _c.getDeclaredFields()) {
        f.setAccessible(true);
        String name = f.getName();
        if (!map.containsKey(name))
          map.put(name, f);
      }
      _c = _c.getSuperclass();
    } while (_c != null);
  }
  getOpt_cache.put(c, map);
  return map;
}
static long sysNow() {
  return System.nanoTime()/1000000;
}
static Thread startThread(Object runnable) {
  return startThread(defaultThreadName(), runnable);
}

static Thread startThread(String name, Object runnable) {
  return startThread(newThread(toRunnable(runnable), name));
}

static Thread startThread(Thread t) {
  _registerThread(t);
  t.start();
  return t;
}
static void incAtomicLong(AtomicLong l) {
  l.incrementAndGet();
}
static Class __javax;

static Class getJavaX() {
  return __javax;
}
static void smartSet(Field f, Object o, Object value) throws Exception {
  f.setAccessible(true);
  
  // take care of common case (long to int)
  if (f.getType() == int.class && value instanceof Long)
    value = ((Long) value).intValue();
    
  try {
    f.set(o, value);
  } catch (Exception e) {
    
      try {
        if (f.getType() == Concept.Ref.class) {
          f.set(o, ((Concept) o).new Ref((Concept) value));
          return;
        }
        if (o instanceof Concept.Ref) {
          f.set(o, ((Concept.Ref) o).get());
          return;
        }
      } catch (Throwable _e) {}
    
    throw e;
  }
}
static int l(Object[] a) { return a == null ? 0 : a.length; }
static int l(boolean[] a) { return a == null ? 0 : a.length; }
static int l(byte[] a) { return a == null ? 0 : a.length; }
static int l(int[] a) { return a == null ? 0 : a.length; }
static int l(float[] a) { return a == null ? 0 : a.length; }
static int l(char[] a) { return a == null ? 0 : a.length; }
static int l(Collection c) { return c == null ? 0 : c.size(); }
static int l(Map m) { return m == null ? 0 : m.size(); }
static int l(CharSequence s) { return s == null ? 0 : s.length(); } static long l(File f) { return f == null ? 0 : f.length(); }

static int l(Object o) {
  return o instanceof String ? l((String) o)
    : o instanceof Map ? l((Map) o)
    : l((Collection) o); // incomplete
}




  static int l(Lisp l) { return l == null ? 0 : l.size(); }

static String exceptionToStringShort(Throwable e) {
  lastException(e);
  e = getInnerException(e);
  String msg = unnull(e.getMessage());
  if (msg.indexOf("Error") < 0 && msg.indexOf("Exception") < 0)
    return baseClassName(e) + prependIfNempty(": ", msg);
  else
    return msg;
}
static int toMS_int(double seconds) {
  return toInt_checked((long) (seconds*1000));
}
static List<String> ai_speculate_functions = ll(
  "ai_spec_webIsTriple",
  "ai_spec_singularPlural",
  "ai_spec_giveMe",
  "ai_spec_parseIf",
  "ai_spec_parseAnd",
  "ai_spec_tripelize",
  "ai_spec_parseArrowTriple",
  "ai_spec_webContents",
  "ai_spec_addQuestionsToChatLine",
  "ai_spec_addQuestionsToAdjective",
  "ai_spec_containsThePhrase",
  "ai_spec_parseIf",
  "ai_spec_possiblyIs",
  "ai_spec_isAQuestion",
  "ai_spec_substWebs",
  "ai_spec_apostrophS",
  "ai_spec_allWords",
  "ai_spec_numbered",
  "ai_spec_howMany",
  "ai_spec_doYouThink",
  "ai_spec_unquote",
  "ai_spec_checkFact",
  "ai_spec_switcheroo",
  "ai_spec_groupingPrefix",
  "ai_spec_moveToMiddle",
  "ai_spec_compareNumbers",
  "ai_spec_compareNumbers_triple",
  "ai_spec_answerYesNoTriple",
  "ai_spec_moveToMiddle_triple",
  "ai_spec_searchForAnswer"
);

static String ai_speculate(final String word) {
  if (empty(word)) return word;
  ai_withMaker("ai_speculate", new Runnable() { public void run() { try { 
    ai_blob(word, false);
    if (isAdjective(word)) ai_postTriple(word, "is", "an adjective");
    for (String f : ai_speculate_functions)
      ai_callSpeculationFunction(f, word);
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "ai_blob(word, false);\r\n    if (isAdjective(word)) ai_postTriple(word, \"is\", \"..."; }});
  ai_speculateOnWeb(ai_getWebFromTerm(word));
  return word;
}

static Throwable printStackTrace2(Throwable e) {
  // we go to system.out now - system.err is nonsense
  print(getStackTrace2(e));
  return e;
}

static void printStackTrace2() {
  printStackTrace2(new Throwable());
}

static void printStackTrace2(String msg) {
  printStackTrace2(new Throwable(msg));
}

/*static void printStackTrace2(S indent, Throwable e) {
  if (endsWithLetter(indent)) indent += " ";
  printIndent(indent, getStackTrace2(e));
}*/
static 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 <A> List<A> cloneSubList(List<A> l, int startIndex, int endIndex) {
  return newSubList(l, startIndex, endIndex);
}

static <A> List<A> cloneSubList(List<A> l, int startIndex) {
  return newSubList(l, startIndex);
}
static void ai_spec_possiblyToActually(Collection<Web> webs) {
  for (Web web : webs)
    ai_spec_possiblyToActually(web);
}

static void ai_spec_possiblyToActually(Web web) {
  T3<String, String, String> t = webToTriple(web);
  if (t == null) return;
  Matches m = new Matches();
  if (swic(t.b, "possibly ", m))
    ai_postTriple(t.a, m.rest(), t.c);
}


static void ai_spec_compareNumbers(String term) {
  for  (Web web : indexedWebs(term))
    { ping(); ai_spec_compareNumbers(web); }
}

static void ai_spec_compareNumbers(Web web) {
  for (WebRelation r : web_relationsNamed(web, "is bigger than?")) {
    String a = web_text(r.a), b = web_text(r.b);
    if (isInteger(a) && isInteger(b)) {
      int cmp = bigint(a).compareTo(bigint(b));
      String rel = cmp > 0 ? "is bigger than" : "is not bigger than";
      ai_postTriple(a, rel, b);
    }
  }
}
static volatile Throwable lastException_lastException;

static Throwable lastException() {
  return lastException_lastException;
}

static void lastException(Throwable e) {
  lastException_lastException = e;
}
static HashMap<String, List<Method>> callMC_cache = new HashMap();
static String callMC_key;
static Method callMC_value;

// varargs assignment fixer for a single string array argument
static Object callMC(String method, String[] arg) {
  return callMC(method, new Object[] {arg});
}

static Object callMC(String method, Object... args) { try {
  Method me;
  if (callMC_cache == null) callMC_cache = new HashMap(); // initializer time workaround
  synchronized(callMC_cache) {
    me = method == callMC_key ? callMC_value : null;
  }
  if (me != null) try {
    return me.invoke(null, args);
  } catch (IllegalArgumentException e) {
    throw new RuntimeException("Can't call " + me + " with arguments " + classNames(args), e);
  }

  List<Method> m;
  synchronized(callMC_cache) {
    m = callMC_cache.get(method);
  }
  if (m == null) {
    if (callMC_cache.isEmpty()) {
      callMC_makeCache();
      m = callMC_cache.get(method);
    }
    if (m == null) throw fail("Method named " + method + " not found in main");
  }
  int n = m.size();
  if (n == 1) {
    me = m.get(0);
    synchronized(callMC_cache) {
      callMC_key = method;
      callMC_value = me;
    }
    try {
      return me.invoke(null, args);
    } catch (IllegalArgumentException e) {
      throw new RuntimeException("Can't call " + me + " with arguments " + classNames(args), e);
    }
  }
  for (int i = 0; i < n; i++) {
    me = m.get(i);
    if (call_checkArgs(me, args, false))
      return me.invoke(null, args);
  }
  throw fail("No method called " + method + " with matching arguments found in main");
} catch (Exception __e) { throw rethrow(__e); } }

static void callMC_makeCache() {
  synchronized(callMC_cache) {
    callMC_cache.clear();
    Class _c = (Class) mc(), c = _c;
    while (c != null) {
      for (Method m : c.getDeclaredMethods())
        if ((m.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) {
          m.setAccessible(true);
          multiMapPut(callMC_cache, m.getName(), m);
        }
      c = c.getSuperclass();
    }
  }
}
static <A, B> void put(Map<A, B> map, A a, B b) {
  if (map != null) map.put(a, b);
}
static void ai_spec_checkFact(String question) {
  for (String fact : ai_texts(question, "asks about fact:", "$X")) {
    if (ai_hasTriple_vary(fact, "is", "never a certain fact"))
      ai_postTriple("You can never be sure about that", "answers", question);
    if (ai_hasTriple_vary(fact, "is", "probably not true"))
      ai_postTriple("Probably not", "answers", question);
  }
}
static void ai_setCacheChecker(AI_CacheChecker cacheChecker) {
  ai_cacheChecker.set(cacheChecker);
}
static String web_text(WebNode node) {
  return node == null ? null : node.text();
}
static <A> A assertNotNull(A a) {
  assertTrue(a != null);
  return a;
}

static <A> A assertNotNull(String msg, A a) {
  assertTrue(msg, a != null);
  return a;
}
static String getStackTrace2(Throwable throwable) {
  return hideCredentials(getStackTrace(throwable) + replacePrefix("java.lang.RuntimeException: ", "FAIL: ",
    hideCredentials(str(getInnerException(throwable)))) + "\n");
}
static List<String> ai_speculateOnWeb_functions = ll(
  "ai_spec_include"
);

static void ai_speculateOnWeb(Web web) {
  if (web == null) return;
  for (String f : ai_speculateOnWeb_functions)
    ai_callSpeculationFunctionOnWeb(f, web);
}

static void ai_spec_giveMe(String chatLine) {
  String s = ai_textOfChatLine(chatLine);
  { if (s == null) return; }
  { if (ai_has("$X", "answers", chatLine)) return; }
  Matches m = new Matches();
  if (swic(s, "give me ", m) && swicOneOf(m.rest(), "a ", "an "))
    post(random(ai_texts("$X", "is", m.rest())), "answers", chatLine);
}
static List<WebNode> ai_search_dollarX(String a, String b, String c) {
  return ai_index_search_dollarX(a, b, c);
}

static List<WebNode> ai_search_dollarX(T3<String, String, String> t) {
  return ai_search_dollarX(t.a, t.b, t.c);
}
static int toInt_checked(long l) {
  if (l != (int) l) throw fail("Too large for int: " + l);
  return (int) l;
}
static String defaultThreadName_name;

static String defaultThreadName() {
  if (defaultThreadName_name == null)
    defaultThreadName_name = "A thread by " + programID();
  return defaultThreadName_name;
}
static void ai_spec_switcheroo(String s) {
  boolean q = isQuoted(s);
  String x = switcheroo(unquote(s));
  if (q) x = quote(x);
  if (neq(x, s))
    ai_postTriple(s, "switcheroo", x);
}
static T3<String, String, String> webToTriple(Web web) {
  return ai_webToTriple(web);
}
static boolean isSubtypeOf(Class a, Class b) {
  return b.isAssignableFrom(a); // << always hated that method, let's replace it!
}
static void ai_spec_parseIf(String s) {
  Matches m = new Matches();
  if (swic_trim(s, "if ", m) && l(javaTokC(m.rest())) > 1 && !isCurlyBraced(m.rest()))
    ai_postTriple(s, "possibly parses as", takeFirst(3, s) + curlyBrace(m.rest()));
}
static <A> List<A> newSubList(List<A> l, int startIndex, int endIndex) {
  return cloneList(subList(l, startIndex, endIndex));
}

static <A> List<A> newSubList(List<A> l, int startIndex) {
  return cloneList(subList(l, startIndex));
}
static List<Pair> _registerDangerousWeakMap_preList;

static <A> A _registerDangerousWeakMap(A map) {
  return _registerDangerousWeakMap(map, null);
}

static <A> A _registerDangerousWeakMap(A map, Object init) {
  callF(init, map);
  
  if (init instanceof String) {
    final String f = (String) ( init);
    init = new Object() { void get(Map map) { try {  callMC(f, map) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "callMC(f, map)"; }};
  }
    
  if (javax() == null) {
    // We're in class init
    if (_registerDangerousWeakMap_preList == null) _registerDangerousWeakMap_preList = synchroList();
    _registerDangerousWeakMap_preList.add(pair(map, init));
    return map;
  }
  
  try {
    call(javax(), "_registerDangerousWeakMap", map, init);
  } catch (Throwable e) { printException(e);
    upgradeJavaXAndRestart();
  }
  return map;
}

static void _onLoad_registerDangerousWeakMap() {
  assertNotNull(javax());
  if (_registerDangerousWeakMap_preList == null) return;
  for (Pair p : _registerDangerousWeakMap_preList)
    _registerDangerousWeakMap(p.a, p.b);
  _registerDangerousWeakMap_preList = null;
}
static void ai_spec_addQuestionsToAdjective(String s) {
  if (!ai_hasTriple(s, "is", "an adjective")) return;
  ai_postTriple(s, "begs the question", quote("What is the opposite of " + s + "?"));
}
static void ai_spec_apostrophS(String s) {
  int i = indexOf(s, "'s");
  if (i < 1) return;
  if (i+2 >= l(s) || !isLetter(s.charAt(i+2)))
    ai_postTriple(s, "possibly related to:", takeFirst(i, s));
}
static void ai_spec_howMany(String question) {
  String bla = web_text(first(ai_search_dollarX(question, "counts instances of", "$X")));
  if (bla == null) return;
  print("bla=" + quote(bla));
  int count =
      l(ai_search_dollarX("$X", "is", bla))
    + l(ai_search_dollarX("$X", "probably is", bla));
  ai_postTriple(str(count), "answers", question);
}
// runnable = Runnable or String (method name)
static Thread newThread(Object runnable) {
  return new Thread(_topLevelErrorHandling(toRunnable(runnable)));
}

static Thread newThread(Object runnable, String name) {
  return new Thread(_topLevelErrorHandling(toRunnable(runnable)), name);
}
static void ai_spec_unquote(String s) {
  if (isProperlyQuoted(s))
    ai_postTriple(s, "unquotes to:", unquote(s));
}
static String unnull(String s) {
  return s == null ? "" : s;
}

static <A> List<A> unnull(List<A> l) {
  return l == null ? emptyList() : l;
}

static <A, B> Map<A, B> unnull(Map<A, B> l) {
  return l == null ? emptyMap() : l;
}

static <A> Iterable<A> unnull(Iterable<A> i) {
  return i == null ? emptyList() : i;
}

static <A> A[] unnull(A[] a) {
  return a == null ? (A[]) new Object[0] : a;
}

static BitSet unnull(BitSet b) {
  return b == null ? new BitSet() : b;
}


static Pt unnull(Pt p) {
  return p == null ? new Pt() : p;
}

static boolean swic(String a, String b) {
  return startsWithIgnoreCase(a, b);
}


  static boolean swic(String a, String b, Matches m) {
    if (!swic(a, b)) return false;
    m.m = new String[] {substring(a, l(b))};
    return true;
  }

static void ai_spec_tripelize(String s) {
  if (tok_containsSimpleArrow(tok_combineCurlyBrackets(javaTok(s)))) return;
  Matches m = new Matches();
  T3<String, String, String> t = ai_tripelize(s);
  if (!ai_trivialTriple(t))
    ai_postTriple(s, "possibly parses to triple", ai_renderTriple(t));
}
static void ai_spec_doYouThink(String s) {
  Matches m = new Matches();
  if (learnedFlexMatch("do you think *", s, m)
    || learnedFlexMatch("Is it true that *", s, m))
    ai_postTriple(s, "asks about fact:", dropPunctuationAtEnd(m.rest()));
}
static void ai_callSpeculationFunction(final String f, final String word) {
  try { ping();
    ai_withMaker(f, new Runnable() { public void run() { try {  callMC(f, word) ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "callMC(f, word)"; }});
  } catch (Throwable __e) { printStackTrace2(__e); }
}
static String baseClassName(String className) {
  return substring(className, className.lastIndexOf('.')+1);
}

static String baseClassName(Object o) {
  return baseClassName(getClassName(o));
}
static void ai_spec_allWords(String _s) {
  String s = unquote(_s);
  List<String> l = javaTokC(s);
  if (l(l) > 1)
    for (String t : l)
      if (l(t) > 1)
        ai_postTriple(_s, "possibly related to:", t);
}
static void ai_spec_parseArrowTriple(String s) {
  Matches m = new Matches();
  T3<String, String, String> t = ai_parseArrowTriple(s);
  if (t == null) return;
  ai_postTriple(s, "is a triple with left part", t.a);
  ai_postTriple(s, "is a triple with middle part", t.b);
  ai_postTriple(s, "is a triple with right part", t.c);
}
static void ai_spec_possiblyIs(String s) {
  List<String> l = javaTokC(s);
  if (l(l) == 3 && allContainCharacters(l))
    ai_postTriple(s, "possibly is", first(l) + " " + last(l));
}
static void ai_spec_answerYesNoTriple(String s) {
  T3<String, String, String> t = ai_parseTriple(s);
  { if (!(t != null && endsWith(t.b, "?"))) return; }
  String rel = dropLastTrim(t.b);
  if (ai_hasTriple(t.a, rel, t.c)) {
    ai_postTriple("Yes", "answers triple", s);
    return;
  }
  
  String neg = ai_text("$X", "is negated relation of", rel);
  if (ai_hasTriple(t.a, neg, t.c)) {
    ai_postTriple("No", "answers triple", s);
    return;
  }    
}
static Throwable getInnerException(Throwable e) {
  while (e.getCause() != null)
    e = e.getCause();
  return e;
}
static void ai_spec_numbered(String s) {
  List<String> l = javaTokC(s);
  if (l(l) > 1 && isInteger(last(l)))
    ai_postTriple(s, "probably is", a(dropLastJavaToken(s)));
}
static Object call(Object o) {
  return callFunction(o);
}

// varargs assignment fixer for a single string array argument
static Object call(Object o, String method, String[] arg) {
  return call(o, method, new Object[] {arg});
}

static Object call(Object o, String method, Object... args) {
  try {
    if (o instanceof Class) {
      Method m = call_findStaticMethod((Class) o, method, args, false);
      m.setAccessible(true);
      return m.invoke(null, args);
    } else {
      Method m = call_findMethod(o, method, args, false);
      m.setAccessible(true);
      return m.invoke(o, args);
    }
  } catch (Exception e) {
    throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
  }
}

static Method call_findStaticMethod(Class c, String method, Object[] args, boolean debug) {
  Class _c = c;
  while (c != null) {
    for (Method m : c.getDeclaredMethods()) {
      if (debug)
        System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
      if (!m.getName().equals(method)) {
        if (debug) System.out.println("Method name mismatch: " + method);
        continue;
      }

      if ((m.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0 || !call_checkArgs(m, args, debug))
        continue;

      return m;
    }
    c = c.getSuperclass();
  }
  throw new RuntimeException("Method '" + method + "' (static) with " + args.length + " parameter(s) not found in " + _c.getName());
}

static Method call_findMethod(Object o, String method, Object[] args, boolean debug) {
  Class c = o.getClass();
  while (c != null) {
    for (Method m : c.getDeclaredMethods()) {
      if (debug)
        System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
      if (m.getName().equals(method) && call_checkArgs(m, args, debug))
        return m;
    }
    c = c.getSuperclass();
  }
  throw new RuntimeException("Method '" + method + "' (non-static) with " + args.length + " parameter(s) not found in " + o.getClass().getName());
}

private static boolean call_checkArgs(Method m, Object[] args, boolean debug) {
  Class<?>[] types = m.getParameterTypes();
  if (types.length != args.length) {
    if (debug)
      System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
    return false;
  }
  for (int i = 0; i < types.length; i++)
    if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
      if (debug)
        System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
      return false;
    }
  return true;
}
static void ai_spec_containsThePhrase(String s) {
  List<String> tok = javaTok(unquote(s));
  Map<Integer, Integer> map = curlyBracketMap(tok);
  for (int i : keys(map)) {
    int j = map.get(i);
    String phrase = trimJoinSubList(tok, i+1, j-1);
    ai_postTriple(s, "contains the phrase", phrase);
  }
}
static Object ai_withMaker(String maker, Object f) {
  //lock aiLock();
  String prev = ai_setMaker(maker);
  try {
    return callF(f);
  } finally {
    ai_setMaker(prev);
  }
}
static void ai_spec_searchForAnswer(String s) {
  Set<String> cluster = litciset(s);
  cluster = ai_cluster_forward(cluster, "parses to triple");
  //print("Cluster: " + cluster);
  if (empty(cluster)) return;
  ai_expandCluster(cluster);
  //print("Cluster expanded: " + cluster);
  cluster = ai_cluster_backward(cluster, "answers triple");
  //print("Cluster: " + cluster);
  if (empty(cluster)) return;
  ai_postTriple_multi(cluster, "answers", s);
}
static Object first(Object list) {
  return empty((List) list) ? null : ((List) list).get(0);
}

static <A> A first(List<A> list) {
  return empty(list) ? null : list.get(0);
}

static <A> A first(A[] bla) {
  return bla == null || bla.length == 0 ? null : bla[0];
}

static <A> A first(Iterable<A> i) {
  if (i == null) return null;
  Iterator<A> it = i.iterator();
  return it.hasNext() ? it.next() : null;
}

static Character first(String s) { return empty(s) ? null : s.charAt(0); }


static <A, B> A first(Pair<A, B> p) {
  return p == null ? null : p.a;
}

static <A> List<A> ll(A... a) {
  ArrayList l = new ArrayList(a.length);
  for (A x : a) l.add(x);
  return l;
}
static Boolean isHeadless_cache;

static boolean isHeadless() {
  if (isHeadless_cache != null) return isHeadless_cache;
  if (GraphicsEnvironment.isHeadless()) return isHeadless_cache = true;
  
  // Also check if AWT actually works.
  // If DISPLAY variable is set but no X server up, this will notice.
  
  try {
    SwingUtilities.isEventDispatchThread();
    return isHeadless_cache = false;
  } catch (Throwable e) { return isHeadless_cache = true; }
}
static void ai_spec_isAQuestion(String _s) {
  String s = unquote(_s);
  if (ai_isQuestion_1(s) || endsWith(s, "?"))
    ai_postTriple(_s, "is", "a question");
}
static Runnable toRunnable(final Object o) {
  if (o instanceof Runnable) return (Runnable) o;
  return new Runnable() { public void run() { try {  callF(o) ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "callF(o)"; }};
}
static Web ai_blob(String word, boolean verified) {
  if (hasIndexedNode(word)) return null;
  Web web = oneNodeWeb(word);
  web.unverified = !verified;
  postSoftwareMadeWeb(web);
  return web;
}
static boolean empty(Collection c) { return c == null || c.isEmpty(); }
static boolean empty(String s) { return s == null || s.length() == 0; }
static boolean empty(Map map) { return map == null || map.isEmpty(); }
static boolean empty(Object[] o) { return o == null || o.length == 0; }
static boolean empty(Object o) {
  if (o instanceof Collection) return empty((Collection) o);
  if (o instanceof String) return empty((String) o);
  if (o instanceof Map) return empty((Map) o);
  if (o instanceof Object[]) return empty((Object[]) o);
  if (o == null) return true;
  throw fail("unknown type for 'empty': " + getType(o));
}

static boolean empty(float[] a) { return a == null || a.length == 0; }
static boolean empty(int[] a) { return a == null || a.length == 0; }
static boolean empty(long[] a) { return a == null || a.length == 0; }
static String getStackTrace(Throwable throwable) {
  lastException(throwable);
  StringWriter writer = new StringWriter();
  throwable.printStackTrace(new PrintWriter(writer));
  return hideCredentials(writer.toString());
}
static boolean isAdjective(String s) {
  return contains(knownAdjectives(), lower(s));
}
static List<String> ai_spec_parseAnd_words = ll("and", "of", "or", "for");

static void ai_spec_parseAnd(String s) {
  List<String> tok = tok_combineCurlyBrackets_keep(javaTok(s));
  for (String word : ai_spec_parseAnd_words) {
    int i = indexOfIC(tok, word);
    if (i < 3 || i >= l(tok)-2) continue;
    T3<String, String, String> t = tok_tripleSplitAtToken(tok, i);
    ai_postTriple(s, "possibly parses as", optionalCurlyBrace(dropSuffix(",", t.a)) + " " + t.b + " " + optionalCurlyBrace(t.c));
  }
}
static void printException(Throwable e) {
  printStackTrace(e);
}
static void ai_spec_moveToMiddle(String term) {
  for  (Web web : indexedWebs(term))
    { ping(); ai_spec_moveToMiddle(web); }
}

static void ai_spec_moveToMiddle(Web web) {
  ai_spec_moveToMiddle(web, ai_spec_moveToMiddle_mtmRels());
}

static void ai_spec_moveToMiddle(Web web, List<WebRelation> mtmRels) {
  for (WebRelation r : web_relationObjects(web)) {
    String rel = web_text(r), b = web_text(r.b);
    for (WebRelation mtm : mtmRels) {
      List<String> l = splitAtSlash_keepAll(web_text(mtm.a));
      List<String> lReplacement = splitAtSlash_keepAll(web_text(mtm.b));
      //print("Trying " + sfu(l) + " -> " + sfu(lReplacement) + " on " + rel + " / " + b);
      { if (!(l(l) == 2 && l(lReplacement) == 2)) continue; }
      Matches m = new Matches();
      String prefix = second(l) + " ";
      { if (!(eqic(first(l), rel) && swic(b, prefix, m))) continue; }
      //print("Match.");
      
      ai_postTriple(web_text(r.a), first(lReplacement),
        second(lReplacement) + " " + m.rest());
    }
  }
}

static List<WebRelation> ai_spec_moveToMiddle_mtmRels() {
  return indexedRelations_verified("move to middle");
}
static void ai_spec_singularPlural(String s) {
  if (is(s, "singular") || is(s, "plural")) return;
  if (ewic(s, "s")) {
    post(s, "possibly is", "plural");
    post(s, "possibly is plural of", dropLast(s));
  } else
    post(s, "possibly is", "singular");
}
static String prependIfNempty(String prefix, String s) {
  return empty(s) ? s : prefix + s;
}
static void ai_setPostHandler(AI_PostHandler handler) {
  ai_postHandler.set(handler);
}
static void ai_spec_webIsTriple(String globalID) {
  ai_spec_webIsTriple(ai_getWebFromTerm(globalID));
}

static void ai_spec_webIsTriple(Web web) {
  if (web == null) return;
  ai_postTriple(web.globalID, "is a", "web");
  ai_postTriple(web.globalID, "see:", "Web " + web.globalID);
  T3<String, String, String> t = webToTriple(web);
  if (t != null)
    ai_postTriple("Web " + web.globalID, "is triple", ai_renderTriple(t));
  else
    ai_postTriple("Web " + web.globalID, "is not a", "triple");
}
static void cancelThread(Thread t) {
  if (t == null) return;
  ping();
  synchronized(mc()) {
    ping_actions.put(t, "cancelled");
    ping_anyActions = true;
  }
}
static String getClassName(Object o) {
  return o == null ? "null" : o instanceof Class ? ((Class) o).getName() : o.getClass().getName();
}
static void ai_spec_groupingPrefix(String s) {
  ai_spec_groupingPrefix(s, ai_groupingPrefixes());
}

static void ai_spec_groupingPrefix(String s, Set<String> prefixes) {
  Matches m = new Matches();
  for (String prefix : prefixes)
    if (swic(s, prefix + " ", m))
      ai_postTriple(s, "groups to", prefix + " " + curlyBraces(m.rest()));
}
static void ai_spec_substWebs(String word) {
  //for (WebNode n : ai_search_dollarX_verified("$X", "is", "a generally applicable substition web"))
  //  ai_subst_web_charBasedReplace(web_text(n), word);
  Matches m = new Matches();
  if (swic_trim(word, "a ", m))
    //ai_subst_web_charBasedReplace("nuoqkgtowqtrjeqo", m.rest());
    ai_subst_web_charBasedReplace("pypazhgrxubdmsqw", m.rest());
}
static void ai_spec_webContents(String s) {
  Matches m = new Matches();
  if (!swic_trim(s, "Web ", m)) return;
  Web web = ai_getWeb(m.rest());
  if (web == null) return;
  ai_postTriple(s, "is", "a web");
  
  T3<String, String, String> t = ai_webToTriple(web);
  if (t != null)
    ai_postTriple(s, "is triple", ai_renderTriple(t));
  else
    for (WebNode node : web_nodesAndRelations(web))
      ai_postTriple(s, node instanceof WebRelation ? "contains relation" : "contains node", web_text(node));
}
static <A> List<A> synchroList() {
  return Collections.synchronizedList(new ArrayList<A>());
}

static <A> List<A> synchroList(List<A> l) {
  return Collections.synchronizedList(l);
}

static void ai_spec_compareNumbers_triple(String s) {
  T3<String, String, String> t = ai_parseTriple(s);
  if (t == null || !eqic(t.b, "is bigger than?")) return;
  String a = t.a, b = t.c;
  if (isInteger(a) && isInteger(b)) {
    int cmp = bigint(a).compareTo(bigint(b));
    String rel = cmp > 0 ? "is bigger than" : "is not bigger than";
    ai_postTriple(a, rel, b);
  }
}
static void ai_spec_moveToMiddle_triple(String term) {
  T3<String, String, String> t = ai_parseTriple(term);
  if (t != null)
    ai_spec_moveToMiddle_triple(term, t, ai_spec_moveToMiddle_mtmRels());
}

static void ai_spec_moveToMiddle_triple(String term, T3<String, String, String> t, List<WebRelation> mtmRels) {
  if (t == null) return;
  String rel = t.b, b = t.c;
  for (WebRelation mtm : mtmRels) {
    List<String> l = splitAtSlash_keepAll(web_text(mtm.a));
    List<String> lReplacement = splitAtSlash_keepAll(web_text(mtm.b));
    //print("Trying " + sfu(l) + " -> " + sfu(lReplacement) + " on " + rel + " / " + b);
    { if (!(l(l) == 2 && l(lReplacement) == 2)) continue; }
    Matches m = new Matches();
    String prefix = second(l) + " ";
    { if (!(eqic(first(l), rel) && swic(b, prefix, m))) continue; }
    //print("Match.");
    
    ai_postTriple(term, "is identical to",
      ai_renderTriple(t.a, first(lReplacement), second(lReplacement) + " " + m.rest()));
  }
}
static String ai_postTriple(T3<String, String, String> triple) {
  // check, clean
  
  if (triple_anyPartNull(triple)) return null;
  triple = trimTriple(triple);
  if (!ai_tripleAllowedToPost(triple)) return null; // fail("No");
  
  // existing?

  if (ai_cache_hasTriple(triple)) return null;
  
  // post handler?
  
  AI_PostHandler handler = ai_postHandler();
  if (handler != null)
    return handler.postTriple(triple);
  else {
    // post web the normal way
    Web web = webFromTriple(triple);
    postSoftwareMadeWeb(web);
    return web.globalID;
  }
}

static String ai_postTriple(String a, String b, String c) {
  return ai_postTriple(t3(a, b, c));
}
static void ai_spec_addQuestionsToChatLine(String line) {
  String text = ai_textOfChatLine(line);
  if (empty(text)) return;
  if (containsWord(text, "it")) {
    ai_postTriple(line, "begs the question", quote("What is \"it\"?"));
    ai_postTriple(line, "see:", "The \"it\" in " + line);
  }
}
static void clear(Collection c) {
  if (c != null) c.clear();
}
static ThreadLocal<AI_SubSpace> ai_currentSubSpace = new ThreadLocal();

static AI_SubSpace ai_currentSubSpace() {
  return ai_currentSubSpace.get();
}
static Web ai_getWebFromTerm(String s) {
  if (possibleGlobalID(s)) {
    Web web = ai_getWeb(s);
    if (web != null) return web;
  }
  Matches m = new Matches();
  if (swic(s, "Web ", m))
    return ai_getWeb(m.rest());
  return null;
}
static Object getOpt(Object o, String field) {
  return getOpt_cached(o, field);
}

static Object getOpt_raw(Object o, String field) {
  try {
    Field f = getOpt_findField(o.getClass(), field);
    if (f == null) return null;
    f.setAccessible(true);
    return f.get(o);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

// access of static fields is not yet optimized
static Object getOpt(Class c, String field) {
  try {
    if (c == null) return null;
    Field f = getOpt_findStaticField(c, field);
    if (f == null) return null;
    f.setAccessible(true);
    return f.get(null);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

static Field getOpt_findStaticField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  return null;
}

static Map synchroHashMap() {
  return Collections.synchronizedMap(new HashMap());
}

static <A> Set<A> synchroHashSet() {
  return Collections.synchronizedSet(new HashSet<A>());
}

static int isAndroid_flag;

static boolean isAndroid() {
  if (isAndroid_flag == 0)
    isAndroid_flag = System.getProperty("java.vendor").toLowerCase().indexOf("android") >= 0 ? 1 : -1;
  return isAndroid_flag > 0;
}



static String dropLastTrim(String s) {
  return trim(dropLast(s));
}
static Object callFunction(Object f, Object... args) {
  return callF(f, args);
}
static List<String> tok_combineCurlyBrackets_keep(List<String> tok) {
  List<String> l = new ArrayList();
  for (int i = 0; i < l(tok); i++) {
    String t = tok.get(i);
    if (odd(i) && eq(t, "{")) {
      int j = findEndOfCurlyBracketPart(tok, i);
      l.add(joinSubList(tok, i, j));
      i = j-1;
    } else
      l.add(t);
  }
  return l;
}
static ThreadLocal<AI_PostHandler> ai_postHandler = new ThreadLocal();

static AI_PostHandler ai_postHandler() {
  return ai_postHandler.get();
}
static String[] dropLast(String[] a, int n) {
  n = Math.min(n, a.length);
  String[] b = new String[a.length-n];
  System.arraycopy(a, 0, b, 0, b.length);
  return b;
}

static <A> List<A> dropLast(List<A> l) {
  return subList(l, 0, l(l)-1);
}

static <A> List<A> dropLast(int n, List<A> l) {
  return subList(l, 0, l(l)-n);
}

static <A> List<A> dropLast(Iterable<A> l) {
  return dropLast(asList(l));
}

static String dropLast(String s) {
  return substring(s, 0, l(s)-1);
}

static String dropLast(String s, int n) {
  return substring(s, 0, l(s)-n);
}

static String dropLast(int n, String s) {
  return dropLast(s, n);
}

static String quote(Object o) {
  if (o == null) return "null";
  return quote(str(o));
}

static String quote(String s) {
  if (s == null) return "null";
  StringBuilder out = new StringBuilder((int) (l(s)*1.5+2));
  quote_impl(s, out);
  return out.toString();
}
  
static void quote_impl(String s, StringBuilder out) {
  out.append('"');
  int l = s.length();
  for (int i = 0; i < l; i++) {
    char c = s.charAt(i);
    if (c == '\\' || c == '"')
      out.append('\\').append(c);
    else if (c == '\r')
      out.append("\\r");
    else if (c == '\n')
      out.append("\\n");
    else
      out.append(c);
  }
  out.append('"');
}
static String dropLastJavaToken(String s) {
  return join(dropLast(2, javaTok(s)));
}
static long ai_hasTriple_vary_worstCase;

static boolean ai_hasTriple_vary(String a, String b, String c) {
  // TODO: optimize!
  List<String> x = ai_variationsOf(a);
  List<String> y = ai_variationsOf(b);
  List<String> z = ai_variationsOf(c);
  int n = l(x)*l(y)*l(z);
  ai_hasTriple_vary_worstCase = max(ai_hasTriple_vary_worstCase, n);
  
  for (String xx : x) for (String yy : y) for (String zz : z)
    if (ai_hasTriple(xx, yy, zz)) return true;
  return false;
}
static Set<String> ai_cluster_forward(Set<String> cluster, String rel) {
  Set<String> set = litciset();
  for (String s : cluster)
    set.addAll(ai_texts(s, rel, "$X"));
  return set;
}
static ThreadLocal<AI_CacheChecker> ai_cacheChecker = new ThreadLocal();

static AI_CacheChecker ai_cacheChecker() {
  return ai_cacheChecker.get();
}
static T3<String, String, String> ai_webToTriple(Web web) {
  if (web == null) return null;
  Collection<WebRelation> l = web_relationObjects(web);
  if (l(l) != 1) return null;
  WebRelation r = first(l);
  if (!web_tripelizableNode(r.a)
    || !web_tripelizableNode(r.b)
    || !web_tripelizableNode(r)) return null;
  return t3(web_text(r.a), web_text(r), web_text(r.b));
}
static String replacePrefix(String prefix, String replacement, String s) {
  if (!startsWith(s, prefix)) return s;
  return replacement + substring(s, l(prefix));
}
static boolean isProperlyQuoted(String s) {
  return s.length() >= 2
    && s.startsWith("\"")
    && s.endsWith("\"")
    && (!s.endsWith("\\\"") || s.endsWith("\\\\\""));
}
static String str(Object o) {
  return o == null ? "null" : o.toString();
}

static String str(char[] c) {
  return new String(c);
}
static boolean contains(Collection c, Object o) {
  return c != null && c.contains(o);
}

static boolean contains(Object[] x, Object o) {
  if (x != null)
    for (Object a : x)
      if (eq(a, o))
        return true;
  return false;
}

static boolean contains(String s, char c) {
  return s != null && s.indexOf(c) >= 0;
}

static boolean contains(String s, String b) {
  return s != null && s.indexOf(b) >= 0;
}
static String getType(Object o) {
  return getClassName(o);
}
static boolean ai_trivialTriple(T3<String, String, String> t) {
  if (t == null) return true;
  return eq(t.b, "?") && eq(t.c, "?");
}
static boolean endsWith(String a, String b) {
  return a != null && a.endsWith(b);
}

static boolean endsWith(String a, char c) {
  return nempty(a) && lastChar(a) == c;
}
static boolean triple_anyPartNull(T3 t) {
  return t == null || t.a == null || t.b == null || t.c == null;
}
static <A, B, C> T3<A, B, C> t3(A a, B b, C c) {
  return new T3(a, b, c);
}
static String ai_textOfChatLine(String line) {
  if (!swicOneOf(line, "chat line ", "Smart Bot's Chat line ")) return null;
  return unquote(firstQuoted(web_texts(ai_search_dollarX_verified(line, "is", "$X"))));
}
// 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<String> javaTok(String s) {
  return javaTok(s, null);
}

static List<String> javaTok(String s, List<String> existing) {
  ++javaTok_n;
  int nExisting = javaTok_opt && existing != null ? existing.size() : 0;
  ArrayList<String> tok = existing != null ? new ArrayList(nExisting) : new ArrayList();
  int l = s.length();
  
  int i = 0, n = 0;
  while (i < l) {
    int j = i;
    char c, d;
    
    // scan for whitespace
    while (j < l) {
      c = s.charAt(j);
      d = j+1 >= l ? '\0' : s.charAt(j+1);
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
        ++j;
      else if (c == '/' && d == '*') {
        do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
        j = Math.min(j+2, l);
      } else if (c == '/' && d == '/') {
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
      } else
        break;
    }
    
    if (n < nExisting && javaTok_isCopyable(existing.get(n), s, i, j))
      tok.add(existing.get(n));
    else
      tok.add(javaTok_substringN(s, i, j));
    ++n;
    i = j;
    if (i >= l) break;
    c = s.charAt(i);
    d = i+1 >= l ? '\0' : s.charAt(i+1);

    // scan for non-whitespace
    
    // Special JavaX syntax: 'identifier
    if (c == '\'' && Character.isJavaIdentifierStart(d) && i+2 < l && "'\\".indexOf(s.charAt(i+2)) < 0) {
      j += 2;
      while (j < l && Character.isJavaIdentifierPart(s.charAt(j)))
        ++j;
    } else if (c == '\'' || c == '"') {
      char opener = c;
      ++j;
      while (j < l) {
        if (s.charAt(j) == opener /*|| s.charAt(j) == '\n'*/) { // allow multi-line strings
          ++j;
          break;
        } else if (s.charAt(j) == '\\' && j+1 < l)
          j += 2;
        else
          ++j;
      }
    } else if (Character.isJavaIdentifierStart(c))
      do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't"
    else if (Character.isDigit(c)) {
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
      if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
    } else if (c == '[' && d == '[') {
      do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
      j = Math.min(j+2, l);
    } else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') {
      do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
      j = Math.min(j+3, l);
    } else
      ++j;
      
    if (n < nExisting && javaTok_isCopyable(existing.get(n), s, i, j))
      tok.add(existing.get(n));
    else
      tok.add(javaTok_substringC(s, i, j));
    ++n;
    i = j;
  }
  
  if ((tok.size() % 2) == 0) tok.add("");
  javaTok_elements += tok.size();
  return tok;
}

static List<String> javaTok(List<String> 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 Web ai_getWeb(String id) {
  return possibleGlobalID(id) ? allWebsByID().getFirst(id) : null;
}
static boolean hasIndexedNode(String s) {
  return cachedNodeIndex().containsKey(s);
}
static String hideCredentials(URL url) { return url == null ? null : hideCredentials(str(url)); }

static String hideCredentials(String url) {
  return url.replaceAll("([&?])_pass=[^&\\s\"]*", "$1_pass=<hidden>");
}
static boolean isCurlyBraced(String s) {
  List<String> tok = tok_combineCurlyBrackets_keep(javaTok(s));
  return l(tok) == 3 && startsWithAndEndsWith(tok.get(1), "{", "}");
}
static String a(String noun) {
  if (eq(noun, "")) return "?";
  return ("aeiou".indexOf(noun.charAt(0)) >= 0 ? "an " : "a ") + noun;
}

static String a(String contents, Object... params) {
  return htag("a", contents, params);
}

static <A> List<A> subList(List<A> l, int startIndex) {
  return subList(l, startIndex, l(l));
}

static <A> List<A> subList(List<A> l, int startIndex, int endIndex) {
  startIndex = max(0, min(l(l), startIndex));
  endIndex = max(0, min(l(l), endIndex));
  if (startIndex > endIndex) return litlist();
  return l.subList(startIndex, endIndex);
}


static boolean swic_trim(String a, String b, Matches m) {
  if (!swic(a, b)) return false;
  m.m = new String[] {trim(substring(a, l(b)))};
  return true;
}
static boolean startsWithIgnoreCase(String a, String b) {
  return a != null && a.regionMatches(true, 0, b, 0, b.length());
}
static ThreadLocal<Boolean> postSoftwareMadeWeb_disabled = new ThreadLocal();
static BetterThreadLocal<Integer> postSoftwareMadeWeb_websMadeInThread = new BetterThreadLocal();
static ThreadLocal<String> postSoftwareMadeWeb_maker = new ThreadLocal();

// L<voidfunc(Web web)>
// function can also return false to stop triggerWebsChanged
static List postSoftwareMadeWeb_onNewWeb = synchroList();

// L<voidfunc(Web web)>
static List postSoftwareMadeWeb_onDisabledPost = synchroList();

static void postSoftwareMadeWeb(Web web, Object... params) {
  Lock _lock_18 = aiLock(); lock(_lock_18); try {
  web_intern(web);
  incIntThreadLocal(postSoftwareMadeWeb_websMadeInThread);
  
  String maker = postSoftwareMadeWeb_maker.get();
  if (maker != null)
    if (empty(web.source)) web.source = maker;
    else params = paramsPlus(params, "maker", postSoftwareMadeWeb_maker.get());

  if (isTrue(postSoftwareMadeWeb_disabled.get())) {
    print("  Would post: " + webToStringShort(web));
    pcallFAll(postSoftwareMadeWeb_onDisabledPost, web);
  } else {
    logStructure(postSoftwareMadeWeb_file(), nu(SoftwareMadeWeb.class,
      "date" , now(),
      "computerID" , computerID(),
      "programID" , programID(),
      //structure := struct(web),
      "web", web,
      "furtherInfo" , empty(params) ? null : litorderedmap(params)));
      
    markWebsPosted_createMarker();
    if (!pcallF_anyFalse(postSoftwareMadeWeb_onNewWeb, web))
      triggerWebsChanged();
  }
} finally { unlock(_lock_18); } }

static File postSoftwareMadeWeb_file() {
  return getProgramFile("webs-made.txt");
}
static boolean ai_isQuestion_1(String s) {
  return ai_isYesNoQuestion_1(s) || ai_questionWord_1(firstWord(s));
}
static boolean isInteger(String s) {
  if (s == null) return false;
  int n = l(s);
  if (n == 0) return false;
  int i = 0;
  if (s.charAt(0) == '-')
    if (++i >= n) return false;
  while (i < n) {
    char c = s.charAt(i);
    if (c < '0' || c > '9') return false;
    ++i;
  }
  return true;
}
static Set<String> ai_groupingPrefixes() {
  return new TreeSet(ai_texts_verified("$X", "is a", "grouping prefix"));
}
static String post(T3<String, String, String> t) {
  return ai_postTriple(t);
}

static String post(String a, String b, String c) {
  return ai_postTriple(a, b, c);
}
// supports the usual quotings (', ", variable length double brackets)
static boolean isQuoted(String s) {
  if (startsWith(s, "'") || startsWith(s, "\"")) return true;
  return isMultilineQuoted(s);
}
static <A> A last(List<A> l) {
  return empty(l) ? null : l.get(l.size()-1);
}

static char last(String s) {
  return empty(s) ? '#' : s.charAt(l(s)-1);
}

static int last(int[] a) {
  return l(a) != 0 ? a[l(a)-1] : 0;
}

static Object last(Object[] a) {
  return l(a) != 0 ? a[l(a)-1] : null;
}
static boolean swicOneOf(String s, String... l) {
  for (String x : l) if (swic(s, x)) return true; return false;
}
static List<WebNode> web_nodesAndRelations(Web web) {
  return web.nodes;
}
static boolean allContainCharacters(Collection<String> l) {
  return all("hasLetters", l);
}

static List<String> ai_postTriple_multi(String a, String b, Collection<String> c) {
  List<String> webIDs = new ArrayList();
  for (String s : unnull(c))
    addIfNotNull(webIDs, ai_postTriple(a, b, s));
  return webIDs;
}

static List<String> ai_postTriple_multi(Collection<String> a, String b, String c) {
  List<String> webIDs = new ArrayList();
  for (String s : unnull(a))
    addIfNotNull(webIDs, ai_postTriple(s, b, c));
  return webIDs;
}
static String ai_setMaker(String maker) {
  return setThreadLocal(postSoftwareMadeWeb_maker, maker);
}
static Collection<WebRelation> web_relationObjects(Web web) {
  return values(web.relations);
}
static String optionalCurlyBrace(String s) {
  return isCurlyBraced(s) ? s : curlyBrace(s);
}
static String ai_renderTriple(T3<String, String, String> t) {
  return ai_tripleToString(t);
}

static String ai_renderTriple(String a, String b, String c) {
  return ai_tripleToString(triple(a, b, c));
}
static List<WebNode> ai_index_search_dollarX(String a, String b, String c) {
  return ai_index_search_dollarX_new(a, b, c);
  
  // OLD
  // S query = isDollarVar(a) ? c : a;
  // ret webs_search_dollarX(webFromTriples(a, b, c), indexedWebs(query));
}
static Random random_random = new Random();

static int random(int n) {
  return n <= 0 ? 0 : random_random.nextInt(n);
}

static double random(double max) {
  return random()*max;
}

static double random() {
  return random_random.nextInt(100001)/100000.0;
}

static double random(double min, double max) {
  return min+random()*(max-min);
}

// min <= value < max
static int random(int min, int max) {
  return min+random(max-min);
}

static <A> A random(List<A> l) {
  return oneOf(l);
}

static <A> A random(Collection<A> c) {
  if (c instanceof List) return random((List<A>) c);
  int i = random(l(c));
  return collectionGet(c, i);
}
static List emptyList() {
  return new ArrayList();
  //ret Collections.emptyList();
}

static List emptyList(int capacity) {
  return new ArrayList(capacity);
}

// Try to match capacity
static List emptyList(Iterable l) {
  return l instanceof Collection ? emptyList(((Collection) l).size()) : emptyList();
}
static String programID() {
  return getProgramID();
}
static String unquote(String s) {
  if (s == null) return null;
  if (startsWith(s, '[')) {
    int i = 1;
    while (i < s.length() && s.charAt(i) == '=') ++i;
    if (i < s.length() && s.charAt(i) == '[') {
      String m = s.substring(1, i);
      if (s.endsWith("]" + m + "]"))
        return s.substring(i+1, s.length()-i-1);
    }
  }
  
  if (s.length() > 1) {
    char c = s.charAt(0);
    if (c == '\"' || c == '\'') {
      int l = endsWith(s, c) ? s.length()-1 : s.length();
      StringBuilder sb = new StringBuilder(l-1);
  
      for (int i = 1; i < l; i++) {
        char ch = s.charAt(i);
        if (ch == '\\') {
          char nextChar = (i == l - 1) ? '\\' : s.charAt(i + 1);
          // Octal escape?
          if (nextChar >= '0' && nextChar <= '7') {
              String code = "" + nextChar;
              i++;
              if ((i < l - 1) && s.charAt(i + 1) >= '0'
                      && s.charAt(i + 1) <= '7') {
                  code += s.charAt(i + 1);
                  i++;
                  if ((i < l - 1) && s.charAt(i + 1) >= '0'
                          && s.charAt(i + 1) <= '7') {
                      code += s.charAt(i + 1);
                      i++;
                  }
              }
              sb.append((char) Integer.parseInt(code, 8));
              continue;
          }
          switch (nextChar) {
          case '\"':
              ch = '\"';
              break;
          case '\\':
              ch = '\\';
              break;
          case 'b':
              ch = '\b';
              break;
          case 'f':
              ch = '\f';
              break;
          case 'n':
              ch = '\n';
              break;
          case 'r':
              ch = '\r';
              break;
          case 't':
              ch = '\t';
              break;
          case '\'':
              ch = '\'';
              break;
          // Hex Unicode: u????
          case 'u':
              if (i >= l - 5) {
                  ch = 'u';
                  break;
              }
              int code = Integer.parseInt(
                      "" + s.charAt(i + 2) + s.charAt(i + 3)
                         + s.charAt(i + 4) + s.charAt(i + 5), 16);
              sb.append(Character.toChars(code));
              i += 5;
              continue;
          default:
            ch = nextChar; // added by Stefan
          }
          i++;
        }
        sb.append(ch);
      }
      return sb.toString();
    }
  }
    
  return s; // not quoted - return original
}
static boolean possibleGlobalID(String s) {
  return l(s) == 16 && allLowerCaseCharacters(s);
}
static boolean eqic(String a, String b) {
  if ((a == null) != (b == null)) return false;
  if (a == null) return true;
  return a.equalsIgnoreCase(b);
}
static <A, B> Pair<A, B> pair(A a, B b) {
  return new Pair(a, b);
}

static <A> Pair<A, A> pair(A a) {
  return new Pair(a, a);
}
static Runnable _topLevelErrorHandling(final Runnable runnable) {
  return new Runnable() { public void run() { try {  try { runnable.run(); } catch (Throwable __e) { printStackTrace2(__e); } 
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "pcall { runnable.run(); }"; }};
}
static int indexOfIC(Object a, Object b) {
  return indexOfIgnoreCase(a, b);
}
static boolean ai_hasTriple(String a, String b, String c) {
  return ai_cache_hasTriple(a, b, c);
}

static boolean ai_hasTriple(T3<String, String, String> t) {
  return ai_cache_hasTriple(t);
}
static Web oneNodeWeb(String text) {
  Web web = simpleWeb();
  web.newNode(text);
  return web;
}
static String curlyBrace(String s) {
  return "{" + s + "}";
}
static T3<String, String, String> ai_tripelize(String s) {
  String x = "";
  if (endsWith(s, "?")) x = " (?)"; // statement with question mark, e.g. "Al Gore invented the Internet?"
  T3<String, String, String> t = ai_tripelize_impl(dropCertainPunctuationAtEnd(s, ":;,?!."));
  ai_fixTriple(t);
  if (t != null && !endsWith(t.b, "?")) t.b += x;
  return t;
}
  
static T3<String, String, String> ai_tripelize_impl(String s) {
  List<String> original = ai_tok_combinePhrases(tok_combineCurlyBrackets_keep(nlTok(s)));
  List<String> tok = original;
  String a = get(tok, 1);
  if (a == null) return null;
  if (ai_questionWord_1(a)) { tok = subList(tok, 2); a = get(tok, 1); }
  String before = trimJoinSubList(original, 0, indexOfSubList(original, tok));
  
  if (ai_yesNoQuestionWord_1(a) && neqicOneOf(a, "am", "are", "is")) {
    List<String> tok2 = subList(tok, 2);
    int i = indexOfFirstVerb(tok2);
    String v = get(tok2, i);
    if (v != null) {
      String middle = trimJoinSubList(tok2, 0, i);
      String rest = trimJoinSubList(tok2, i+1);
      // a = "do", middle, v = "love", rest = "me"
      if (eqic(a, "does")) v = verbToThirdPerson(v) + "?";
      else if (eqic(a, "do")) v = v + "?";
      else if (eqic(a, "did")) v = verbToPast(v) + "?";
      else v = a + " " + v + "?";
      return triple(joinWithSpace2(before, middle), v, rest);
    }
  }
  if (isVerb(a)) {
    tok = subList(tok, 2);
    int i = ai_splitTwoNounPhrases(tok);
    //int i = ai_endOfNounPhrase(tok);
    return triple(
      trimJoinSubList(tok, 0, i),
      a + "?",
      trimJoinSubList(tok, i));
  }
  
  int i = ai_endOfNounPhrase(tok);
  int j = indexOfFirstVerb(tok, i);
  if (j < 0) return triple(s, "?", "?");
  return triple(
    joinWithSpace2(before, trimJoinSubList(tok, 0, j)),
    tok.get(j),
    trimJoinSubList(tok, j+2));
}
static TreeSet<String> litciset(String... items) {
  TreeSet<String> set = caseInsensitiveSet();
  for (String a : items) set.add(a);
  return set;
}
static List<WebRelation> web_relationsNamed(Web web, String label) {
  return web_allRelationsWithLabel(web, label);
}
static List<String> classNames(Collection l) {
  return getClassNames(l);
}

static List<String> classNames(Object[] l) {
  return getClassNames(Arrays.asList(l));
}
static String lower(String s) {
  return s == null ? null : s.toLowerCase();
}

static char lower(char c) {
  return Character.toLowerCase(c);
}
static <A> List<A> cloneList(Collection<A> l) {
  if (l == null) return new ArrayList();
  // TODO: is this correct when l is a keySet?
  synchronized(l) {
    return new ArrayList<A>(l);
  }
}
static boolean is(String a, String b) {
  return ai_hasTriple(a, "is", b);
}
static void upgradeJavaXAndRestart() {
  run("#1001639");
  restart();
  sleep();
}
static List<String> tok_combineCurlyBrackets(List<String> tok) {
  List<String> l = new ArrayList();
  for (int i = 0; i < l(tok); i++) {
    String t = tok.get(i);
    if (odd(i) && eq(t, "{")) {
      int j = findEndOfCurlyBracketPart(tok, i);
      l.add(joinSubList(tok, i+1, j-1));
      i = j;
    } else
      l.add(t);
  }
  return l;
}
static T3<String, String, String> tok_tripleSplitAtToken(List<String> tok, int i) {
  return triple(trimJoinSubList(tok, 0, i), tok.get(i), trimJoinSubList(tok, i+1));
}
static Set<String> ai_cluster_backward(Set<String> cluster, String rel) {
  Set<String> set = litciset();
  for (String s : cluster)
    set.addAll(ai_texts("$X", rel, s));
  return set;
}
static void assertTrue(Object o) {
  if (!(eq(o, true) || isTrue(pcallF(o))))
    throw fail(str(o));
}
  
static boolean assertTrue(String msg, boolean b) {
  if (!b)
    throw fail(msg);
  return b;
}

static boolean assertTrue(boolean b) {
  if (!b)
    throw fail("oops");
  return b;
}
static Map emptyMap() {
  return new HashMap();
}
static List<String> ai_texts(String a, String b, String c) {
  return web_texts(ai_search_dollarX(a, b, c));
}
static boolean ewic(String a, String b) {
  return endsWithIgnoreCase(a, b);
}
static List<String> javaTokC(String s) {
  if (s == null) return null;
  int l = s.length();
  ArrayList<String> tok = new ArrayList();
  
  int i = 0;
  while (i < l) {
    int j = i;
    char c, d;
    
    // scan for whitespace
    while (j < l) {
      c = s.charAt(j);
      d = j+1 >= l ? '\0' : s.charAt(j+1);
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
        ++j;
      else if (c == '/' && d == '*') {
        do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
        j = Math.min(j+2, l);
      } else if (c == '/' && d == '/') {
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
      } else
        break;
    }
    
    i = j;
    if (i >= l) break;
    c = s.charAt(i);
    d = i+1 >= l ? '\0' : s.charAt(i+1);

    // scan for non-whitespace
    if (c == '\'' || c == '"') {
      char opener = c;
      ++j;
      while (j < l) {
        if (s.charAt(j) == opener || s.charAt(j) == '\n') { // end at \n to not propagate unclosed string literal errors
          ++j;
          break;
        } else if (s.charAt(j) == '\\' && j+1 < l)
          j += 2;
        else
          ++j;
      }
    } else if (Character.isJavaIdentifierStart(c))
      do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't"
    else if (Character.isDigit(c)) {
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
      if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
    } else if (c == '[' && d == '[') {
      do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
      j = Math.min(j+2, l);
    } else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') {
      do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
      j = Math.min(j+3, l);
    } else
      ++j;
      
    tok.add(javaTok_substringC(s, i, j));
    i = j;
  }
  
  return tok;
}
static boolean ai_has(String a, String b, String c) {
  return ai_text(a, b, c) != null;
}

static boolean ai_has(T3<String, String, String> t) {
  return ai_has(t.a, t.b, t.c);
}
static List<String> dropPunctuationAtEnd_keep = litlist("*", "<", ">");

static List<String> dropPunctuationAtEnd(List<String> tok) {
  tok = new ArrayList<String>(tok);
  for (int i = tok.size()-2; i > 0; i -= 2) {
    String t = tok.get(i);
    if (t.length() == 1 && !Character.isLetter(t.charAt(0)) && !Character.isDigit(t.charAt(0)) && !dropPunctuationAtEnd_keep.contains(t)) {
      tok.remove(i);
      tok.remove(i);
    } else break;
  }
  return tok;
}

static String dropPunctuationAtEnd(String s) {
  return join(dropPunctuationAtEnd(nlTok(s)));
}
static boolean tok_containsSimpleArrow(String s) {
  return tok_containsSimpleArrow(javaTok(s));
}

static boolean tok_containsSimpleArrow(List<String> tok) {
  return jfind(tok, "->") >= 0;
}
static T3<String, String, String> ai_parseTriple(String s) {
  return ai_parseArrowTriple(s);
}
static void ai_spec_include(String a, String b, String c) {
  print("ai_spec_include " + a + " " + b + " " + c);
  { if (!(eqic(b, "include"))) return; }
  String x = a(singular(a));
  for (String s : ai_parseList(c))
    post(s, "is", x);
}
static T3<String, String, String> ai_parseArrowTriple(String s) {
  return listToTriple(map("tok_unCurlyBracket",splitAtSimpleArrow(tok_combineCurlyBrackets_keep(javaTok(s)))));
}

static Web webFromTriple(String a, String b, String c) {
  return webFromTriple(triple(a, b, c));
}

static Web webFromTriple(T3<String, String, String> t) {
  return webFromTriple(t, 0.2, 0.2, 0.8, 0.7);
}

static Web webFromTriple(T3<String, String, String> t, double x1, double y1, double x2, double y2) {
  Web web = webWithoutIndexAndLock(); // save space
  String a = unnull(t.a), b = unnull(t.b), c = unnull(t.c);
  web_addRelation(web, a, c, b);
  //Web web = webFromTriples(t.a, t.b, t.c);
  
  web_setPosition(first(web.nodes), x1, y1);
  web_setPosition(second(web.nodes), x2, y2);
  return web;
}
static <A> A second(List<A> l) {
  return get(l, 1);
}

static <A> A second(A[] bla) {
  return bla == null || bla.length <= 1 ? null : bla[1];
}


static <A, B> B second(Pair<A, B> p) {
  return p == null ? null : p.b;
}



static <A, B, C> B second(T3<A, B, C> t) {
  return t == null ? null : t.b;
}

static Class mc() {
  return main.class;
}
static <A, B> void multiMapPut(Map<A, List<B>> map, A a, B b) {
  List<B> l = map.get(a);
  if (l == null)
    map.put(a, l = new ArrayList());
  l.add(b);
}
static boolean neq(Object a, Object b) {
  return !eq(a, b);
}
static <A> List<A> takeFirst(List<A> l, int n) {
  return l(l) <= n ? l : newSubList(l, 0, n);
}

static <A> List<A> takeFirst(int n, List<A> l) {
  return takeFirst(l, n);
}

static String takeFirst(int n, String s) {
  return substring(s, 0, n);
}

static <A> List<A> takeFirst(int n, Iterable<A> i) {
  List l = new ArrayList();
  Iterator<A> it = i.iterator();
  for (int _repeat_872 = 0; _repeat_872 < n; _repeat_872++)  { if (it.hasNext()) l.add(it.next()); else break; }
  return l;
}

static <A> int indexOf(List<A> 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 <A> int indexOf(List<A> l, int startIndex, A a) {
  return indexOf(l, a, startIndex);
}

static <A> int indexOf(List<A> l, A a) {
  if (l == null) return -1;
  return l.indexOf(a);
}

static int indexOf(String a, String b) {
  return a == null || b == null ? -1 : a.indexOf(b);
}

static int indexOf(String a, String b, int i) {
  return a == null || b == null ? -1 : a.indexOf(b, i);
}

static int indexOf(String a, char b) {
  return a == null ? -1 : a.indexOf(b);
}

static int indexOf(String a, char b, int i) {
  return a == null ? -1 : a.indexOf(b, i);
}

static int indexOf(String a, int i, String b) {
  return a == null || b == null ? -1 : a.indexOf(b, i);
}

static <A> 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;
}
// extended over Class.isInstance() to handle primitive types
static boolean isInstanceX(Class type, Object arg) {
  if (type == boolean.class) return arg instanceof Boolean;
  if (type == int.class) return arg instanceof Integer;
  if (type == long.class) return arg instanceof Long;
  if (type == float.class) return arg instanceof Float;
  if (type == short.class) return arg instanceof Short;
  if (type == char.class) return arg instanceof Character;
  if (type == byte.class) return arg instanceof Byte;
  if (type == double.class) return arg instanceof Double;
  return type.isInstance(arg);
}
static boolean ai_cache_hasTriple_debug;

static boolean ai_cache_hasTriple(String a, String b, String c) {
  if (a == null || b == null || c == null) return false;
  
  AI_CacheChecker checker = ai_cacheChecker();
  if (checker != null && checker.hasTriple(a, b, c)) return true;

  List<Web> webs = ai_fewestIndexedWebs(a, b, c);
  boolean x = webs_search_noVar_bool(webFromTriples(a, b, c), webs);
  if (ai_cache_hasTriple_debug)
    print("hasTriple: " + sfuLL(a, b, c, x) + ", searched " + nWebs(webs));
  return x;
}

static boolean ai_cache_hasTriple(T3<String, String, String> t) {
  return ai_cache_hasTriple(t.a, t.b, t.c);
}
static List<String> splitAtSlash_keepAll(String s) {
  int i = 0;
  List<String> l = new ArrayList();
  while (true) {
    int j = smartIndexOf(s, '/', i);
    l.add(substring(s, i, j));
    if (j == l(s)) break;
    i = j+1;
  }
  return l;
}
static boolean containsWord(String s, String word) {
  return cic(nlTok3(s), word);
}
static void ai_callSpeculationFunctionOnWeb(final String f, String webID) {
  ai_callSpeculationFunctionOnWeb(f, ai_getWebFromTerm(webID));
}

static void ai_callSpeculationFunctionOnWeb(final String f, Web web) {
  final T3<String, String, String> t = webToTriple(web);
  { if (t == null) return; }
  try { ping();
    ai_withMaker(f, new Runnable() { public void run() { try {  callMC(f, t.a, t.b, t.c) ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "callMC(f, t.a, t.b, t.c)"; }});
  } catch (Throwable __e) { printStackTrace2(__e); }
}
static String curlyBraces(String s) {
  return curlyBrace(s);
}
static boolean ai_tripleAllowedToPost(T3<String, String, String> t) {
  //ret all(f ai_nodeNameAllowedToPost, tripleToList(t));
  return ai_nodeNameAllowedToPost(t.a)
    && ai_nodeNameAllowedToPost(t.b)
    && ai_nodeNameAllowedToPost(t.c);
}
static boolean isLetter(char c) {
  return Character.isLetter(c);
}
static String substring(String s, int x) {
  return substring(s, x, l(s));
}

static String substring(String s, int x, int y) {
  if (s == null) return null;
  if (x < 0) x = 0;
  if (x > s.length()) return "";
  if (y < x) y = x;
  if (y > s.length()) y = s.length();
  return s.substring(x, y);
}


static List<WebRelation> indexedRelations_verified(String s) {
  return web_relationsOnly(web_nodesFromVerifiedWebs(indexedNodes(s)));
}
static void ai_expandCluster(Set<String> cluster) {
  for (String s : cloneList(cluster)) {
    cluster.addAll(ai_texts(s, "is identical to", "$X"));
    cluster.addAll(ai_texts("$X", "is identical to", s));
  }
}
static T3<String, String, String> trimTriple(T3<String, String, String> t) {
  return t == null ? null : triple(trim(t.a), trim(t.b), trim(t.c));
}
static List<String> switcheroo_themap = ll(
  "mine", "yours",
  "my", "your",
  "you are", "i am",
  "are you", "am i",
  "you're", "i'm",
  "you've", "i've",
  "me", "you",
  "i", "you");

static String switcheroo(String s) {
  List<String> tok = nlTok5(s);
  for (int i = 1; i < l(tok); i += 2) {
    for (int j = 0; j < l(switcheroo_themap); j += 2) {
      String x = switcheroo_themap.get(j), y = switcheroo_themap.get(j+1);
      List<String> tokx = nlTok5(x), toky = nlTok5(y);
      switcheroo_matchReplace(tok, i, tokx, toky);
      switcheroo_matchReplace(tok, i, toky, tokx);
    }
  }
  return join(map("switcheroo_dropAsterisk", tok));
}

static String switcheroo_dropAsterisk(String t) {
  return startsWith(t, "*") && l(t) > 1 ? substring(t, 1) : t;
}

static void switcheroo_matchReplace(List<String> tok, int i, List<String> tokx, List<String> toky) {
  //printFormat("Matching * * *", i, get(tok, i), tokx);
  if (switcheroo_tokRegionMatch(tok, i, tokx)) {
    //printFormat("Replacing * *", tokx, toky);
    for (int j = 0; j*2+1 < l(tokx); j++)
      tok.set(i+j*2, "*" + toky.get(1+j*2));
  }
}

static boolean switcheroo_tokRegionMatch(List<String> tok, int i, List<String> tokx) {
  for (int j = 0; j*2+1 < l(tokx); j++)
    if (!eqic(get(tok, i+j*2), get(tokx, 1+j*2)))
      return false;
  return true;
}

static BigInteger bigint(String s) {
  return new BigInteger(s);
}

static BigInteger bigint(long l) {
  return BigInteger.valueOf(l);
}
static boolean learnedFlexMatch(String pattern, String s) {
  return learnedFlexMatch(pattern, s, null);
}

static boolean learnedFlexMatch(String pattern, String s, Matches m) {
  if (!flexMatchIC(dropPunctuationAtEnd(pattern), dropPunctuationAtEnd(s), m)) return false;
  MatchLearner l = uniq(MatchLearner.class, "pattern", pattern);
  String status = l.examples.get(s);
  if (eq(status, "rejected")) return false;
  if (empty(status))
    l.examples.put(s, "assumed");
  return true;
}
static String trimJoinSubList(List<String> l, int i, int j) {
  return trim(join(subList(l, i, j)));
}

static String trimJoinSubList(List<String> l, int i) {
  return trim(join(subList(l, i)));
}
static String dropSuffix(String suffix, String s) {
  return s.endsWith(suffix) ? s.substring(0, l(s)-l(suffix)) : s;
}
static Cache<Set<String>> knownAdjectives_cache = new Cache("knownAdjectives_load");
static Set<String> knownAdjectives() { return knownAdjectives_cache.get(); }

static Set<String> knownAdjectives_load() {
  return new TreeSet(tlft(loadSnippet("#1011077")));
}

static List<Web> indexedWebs(String s) {
  return ai_withoutInvalidWebs(nodesToWebs(indexedNodes_withInvalids(s)));
}
static String ai_subst_web_charBasedReplace(String webID, final String input) {
  int n = websMadeInThread();
  Web web = assertNotNull("Web not found: " + webID, allWebsByID().getFirst(webID));
  WebRelation rel = assertNotNull(web_findRelationNamed(web, "matches"));
  String pat = web_unquoted(rel.a), cmd = web_unquoted(rel.b);
  assertEquals("substitute * => * with *", pat);
  Matches m = new Matches();
  assertTrue(flexMatchIC(pat, cmd, m));
  String src = m.unq(0);
  final String var = m.unq(2);
  List<String> terms = unquoteAll(trimAll(splitAtJavaToken(m.get(1), ",")));
  Object replacer = new F1<String, Object>() { Object get(String s) { try { return  replaceIC(s, var, input) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "replaceIC(s, var, input)"; }};
  src = (String) callF(replacer, src);
  terms = map(replacer, terms);
  ai_postTriple_multi(src, "see:", terms);
  return nWebs(websMadeInThread()-n) + " made";
}
// map: index of opening bracket -> index of closing bracket
static Map<Integer, Integer> curlyBracketMap(List<String> tok) {
  TreeMap<Integer,Integer> map = new TreeMap();
  List<Integer> stack = new ArrayList();
  int n = l(tok);
  for (int i = 1; i < n; i += 2) {
    String t = tok.get(i);
    if (eq(t, "{"))
      stack.add(i);
    else if (eq(t, "}") && nempty(stack))
      map.put(liftLast(stack), i);
  }
  return map;
}


static Lock aiLock() {
  return dbLock();
}
static String htag(String tag) {
  return htag(tag, "");
}

static String htag(String tag, Object contents, Object... params) {
  String openingTag = hopeningTag(tag, params);
  String s = str(contents);
  if (empty(s))
    return dropLast(openingTag) + "/>";
  return openingTag + s + "</" + tag + ">";
}
static List onWebsChanged_listeners = synchroList();

static void triggerWebsChanged() {
  pcallF_all(onWebsChanged_listeners);
}

static void onWebsChanged(Object f) {
  setAdd(onWebsChanged_listeners, f);
}
static List<String> nlTok5(String s) {
  List<String> 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 == '"') {
      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)));
    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 <A> ArrayList<A> asList(A[] a) {
  return a == null ? new ArrayList<A>() : new ArrayList<A>(Arrays.asList(a));
}

static ArrayList<Integer> asList(int[] a) {
  ArrayList<Integer> l = new ArrayList();
  for (int i : a) l.add(i);
  return l;
}

static <A> ArrayList<A> asList(Iterable<A> 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 <A> ArrayList<A> asList(Enumeration<A> e) {
  ArrayList l = new ArrayList();
  if (e != null)
    while (e.hasMoreElements())
      l.add(e.nextElement());
  return l;
}
static List<String> nlTok3(String s) {
  List<String> tok = new ArrayList<String>();
  int l = s.length();
  
  int i = 0;
  while (i < l) {
    int j = i;
    char c; String cc;
    
    // scan for whitespace
    while (j < l) {
      c = s.charAt(j);
      cc = s.substring(j, Math.min(j+2, l));
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
        ++j;
      else if (cc.equals("/*")) {
        do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
        j = Math.min(j+2, l);
      } else if (cc.equals("//")) {
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
      } else
        break;
    }
    
    tok.add(s.substring(i, j));
    i = j;
    if (i >= l) break;
    c = s.charAt(i);
    cc = s.substring(i, Math.min(i+2, l));

    // scan for non-whitespace
    if (c == '\u201C' || c == '\u201D') c = '"'; // normalize quotes
    if (c == '\'' || c == '"') {
      char opener = c;
      ++j;
      while (j < l) {
        char _c = s.charAt(j);
        if (_c == '\u201C' || _c == '\u201D') _c = '"'; // normalize quotes
        if (_c == opener) {
          ++j;
          break;
        } else if (s.charAt(j) == '\\' && j+1 < l)
          j += 2;
        else
          ++j;
      }
      if (j-1 >= i+1) {
        tok.add(opener + s.substring(i+1, j-1) + opener);
        i = j;
        continue;
      }
    } else if (Character.isJavaIdentifierStart(c))
      do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) /*|| s.charAt(j) == '\''*/ ));
    else if (Character.isDigit(c))
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
    else if (cc.equals("[[")) {
      do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
      j = Math.min(j+2, l);
    } else if (cc.equals("[=") && i+2 < l && s.charAt(i+2) == '[') {
      do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
      j = Math.min(j+3, l);
    } else if (s.substring(j, Math.min(j+3, l)).equals("..."))
      j += 3;
    else if (c == '$' || c == '#')
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
    else
      ++j;

    tok.add(s.substring(i, j));
    i = j;
  }
  
  if ((tok.size() % 2) == 0) tok.add("");
  return tok;
}

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 List<String> replaceIC(List<String> l, String a, String b) {
  List<String> l2 = new ArrayList();
  for (int i = 0; i < l(l); i++) {
    String s = l.get(i);
    l2.add(eqic(s, a) ? b : s);
  }
  return l2;
}

static String replaceIC(String s, String a, String b) {
  return s.replaceAll("(?i)" + patternQuote(a), quoteReplacement(b));
}
static boolean hasLetters(String s) {
  for (int i = 0; i < s.length(); i++)
    if (Character.isLetter(s.charAt(i)))
      return true;
  return false;
}
static void web_addRelation(WebNode a, WebNode b, String rel) {
  if (a != null && b != null && rel != null)
    a.web.getRelation(a, b).addLabel(rel);
}

static void web_addRelation(Web web, String a, String b, String rel) {
  web_addRelation(web.node(a), web.node(b), rel);
}

static void web_addRelation(WebNode a, String rel, WebNode b) {
  web_addRelation(a, b, rel);
}
static String firstWord(String s) {
  return firstJavaToken(s);
}
static Map<String, String> singular_specials = litmap(
  "children", "child", "images", "image", "chess", "chess");
  
static Set<String> singular_specials2 = litset("time", "machine");

static String singular(String s) {
  if (s == null) return null;
  { String _a_729 = singular_specials.get(s); if (!empty(_a_729)) return _a_729; }
  { String _a_730 = hippoSingulars().get(lower(s)); if (!empty(_a_730)) return _a_730; }
  if (singular_specials2.contains(dropSuffix("s", s)))
    return dropSuffix("s", s);
  if (s.endsWith("ness")) return s;
  if (s.endsWith("ges")) return dropSuffix("s", s);
  if (endsWith(s, "bases")) return dropLast(s);
  s = dropSuffix("es", s);
  s = dropSuffix("s", s);
  return s;
}
static DependentCache<MultiMap<String, Web>> allWebsByID_cache = new DependentCache("allWebsByID_calc", "cachedNodeIndex" /*_changeCount*/);

static MultiMap<String, Web> allWebsByID() {
  return allWebsByID_cache.get();
}

static MultiMap<String, Web> allWebsByID_calc() {
  setAdd(postSoftwareMadeWeb_onNewWeb, "allWebsByID_onNewWeb");
  onTransientWebAdded_do("allWebsByID_onNewWeb");
  onTransientWebRemoved_do("allWebsByID_onRemovedWeb");
  return multiMapIndexByField(allWebsFromCachedNodeIndex_uncached(), "globalID");
}

static void allWebsByID_onNewWeb(Web web, Object[] params) {
  allWebsByID_onNewWeb(web);
}

static void allWebsByID_onNewWeb(Web web) {
  Lock _lock_172 = allWebsByID_cache.lock; lock(_lock_172); try {
  if (allWebsByID_cache.get() != null)
    allWebsByID_cache.get().put(web.globalID, web);
} finally { unlock(_lock_172); } }

static void allWebsByID_onRemovedWeb(Web web) {
  Lock _lock_173 = allWebsByID_cache.lock; lock(_lock_173); try {
  if (allWebsByID_cache.get() != null)
    allWebsByID_cache.get().remove(web.globalID);
} finally { unlock(_lock_173); } }

static List<String> dropCertainPunctuationAtEnd(List<String> tok, String charsToDrop) {
  tok = cloneList(tok);
  for (int i = tok.size()-2; i > 0; i -= 2) {
    String t = tok.get(i);
    if (t.length() == 1 && charsToDrop.contains(t)) {
      tok.remove(i+1);
      tok.remove(i);
    } else break;
  }
  return tok;
}

static String dropCertainPunctuationAtEnd(String s, String chars) {
  return join(dropCertainPunctuationAtEnd(nlTok(s), chars));
}
static <A> A liftLast(List<A> l) {
  if (l.isEmpty()) return null;
  int i = l(l)-1;
  A a = l.get(i);
  l.remove(i);
  return a;
}
static String joinWithSpace2(Collection<String> c) {
  return joinNempties(" ", c);
}

static String joinWithSpace2(String... c) {
  return joinNempties(" ", c);
}

static String programID;

static String getProgramID() {
  return nempty(programID) ? formatSnippetIDOpt(programID) : "?";
}

// TODO: ask JavaX instead
static String getProgramID(Class c) {
  String id = (String) getOpt(c, "programID");
  if (nempty(id))
    return formatSnippetID(id);
  return "?";
}

static String getProgramID(Object o) {
  return getProgramID(getMainClass(o));
}
static int ai_splitTwoNounPhrases(List<String> tok1) {
  return ai_splitTwoNounPhrasesC(codeTokens(tok1))*2;
}

// tok is code tokens only
static int ai_splitTwoNounPhrasesC(List<String> tok) {
  String a = first(tok), b = second(tok);
  boolean a1 = ai_is_myYourEtc(a);
  if ((a1 || ai_is_myYourEtc_maybe(a)) && isNoun(b)) return 2;
  if (eqic(get(tok, 2), "a")) return 2;
  int i = 0;
  if (a1) ++i;
  ai_splitTwoNounPhrases_assumeNoun(get(tok, i));
  return i+1;
}

static void ai_splitTwoNounPhrases_assumeNoun(String s) {
  /*if (!isNoun(s))
    print("Assuming noun: " + s);*/
}
static int indexOfFirstVerb(List<String> tok, int i) {
  return indexOfAny(tok, i, knownVerbs());
}

static int indexOfFirstVerb(List<String> tok) {
  return indexOfFirstVerb(tok, 0);
}
static List<String> web_texts(WebNode node) {
  return node == null ? new ArrayList() : node.texts();
}

static List<String> web_texts(Collection<WebNode> l) {
  List<String> texts = new ArrayList(l(l));
  if (l != null) for (WebNode n : l)
    texts.add(web_text(n));
  return texts;
}
static MultiMap<String, WebNode> cachedNodeIndex() {
  return cachedNodeIndex2();
}
static Object[] paramsPlus(Object[] a1, Object... a2) {
  if (l(a2) == 2 && a2[0] == null) return a1;
  return concatArrays(assertEvenLength(a1), assertEvenLength(a2));
}
static volatile boolean sleep_noSleep;

static void sleep(long ms) {
  ping();
  if (ms < 0) return;
  // allow spin locks
  if (isAWTThread() && ms > 100) throw fail("Should not sleep on AWT thread");
  try {
    Thread.sleep(ms);
  } catch (Exception e) { throw new RuntimeException(e); }
}

static void sleep() { try {
  if (sleep_noSleep) throw fail("nosleep");
  print("Sleeping.");
  sleepQuietly();
} catch (Exception __e) { throw rethrow(__e); } }
static int min(int a, int b) {
  return Math.min(a, b);
}

static long min(long a, long b) {
  return Math.min(a, b);
}

static float min(float a, float b) { return Math.min(a, b); }
static float min(float a, float b, float c) { return min(min(a, b), c); }

static double min(double a, double b) {
  return Math.min(a, b);
}

static double min(double[] c) {
  double x = Double.MAX_VALUE;
  for (double d : c) x = Math.min(x, d);
  return x;
}

static float min(float[] c) {
  float x = Float.MAX_VALUE;
  for (float d : c) x = Math.min(x, d);
  return x;
}

static byte min(byte[] c) {
  byte x = 127;
  for (byte d : c) if (d < x) x = d;
  return x;
}

static short min(short[] c) {
  short x = 0x7FFF;
  for (short d : c) if (d < x) x = d;
  return x;
}
static int ai_tok_combinePhrases_maxPhraseLength = 3; // 3 words, see #1011207

static List<String> ai_tok_combinePhrases(List<String> tok) {
  List<String> l = new ArrayList();
  int n = l(tok);
  bigloop: for (int i = 0; i < l(tok); i++) {
    String t = tok.get(i);
    if (odd(i)) {
      String phrase = t;
      int j = i+2;
      while (j < n && j < i+ai_tok_combinePhrases_maxPhraseLength*2) {
        phrase += " " + tok.get(j);
        if (isKnownPhrase(phrase)) {
          l.add(joinSubList(tok, i, j+1));
          i = j;
          continue bigloop;
        }
        j += 2;
      }
    }
    l.add(t);
  }
  return l;
}
static Web web_intern(Web web) {
  if (web == null) return null;
  for (WebNode n : web_nodesAndRelations(web))
    web_setLabels_forced(n, map_lisp_intern(n.labels()));
  web.globalID = intern(web.globalID);
  web.source = intern(web.source);
  web.title = intern(web.title);
  return web;
}
static String web_unquoted(WebNode node) {
  return web_unquote(node);
}
static boolean isTrue(Object o) {
  if (o instanceof Boolean)
    return ((Boolean) o).booleanValue();
  if (o == null) return false;
  throw fail(getClassName(o));
}
static List<WebRelation> web_relationsOnly(Collection<WebNode> c) {
  List<WebRelation> l = new ArrayList();
  for (WebNode n : unnull(c))
    if (n instanceof WebRelation) l.add(((WebRelation) n));
  return l;
}
static <A> int indexOfSubList(List<A> x, List<A> y) {
  return indexOfSubList(x, y, 0);
}

static <A> int indexOfSubList(List<A> x, List<A> y, int i) {
  outer: for (; i+l(y) <= l(x); i++) {
    for (int j = 0; j < l(y); j++)
      if (neq(x.get(i+j), y.get(j)))
        continue outer;
    return i;
  }
  return -1;
}

static <A> int indexOfSubList(List<A> x, A[] y, int i) {
  outer: for (; i+l(y) <= l(x); i++) {
    for (int j = 0; j < l(y); j++)
      if (neq(x.get(i+j), y[j]))
        continue outer;
    return i;
  }
  return -1;
}
static Class run(String progID, String... args) {
  Class main = hotwire(progID);
  callMain(main, args);
  return main;
}
static List<String> ai_parseList(String s) {
  return splitAtJavaToken(s, ",");
}
static String _computerID;
public static String computerID() { try {
  if (_computerID == null) {
    File file = new File(userHome(), ".tinybrain/computer-id");
    _computerID = loadTextFile(file.getPath(), null);
    if (_computerID == null) {
      _computerID = makeRandomID(12);
      saveTextFile(file.getPath(), _computerID);
    }
  }
  return _computerID;
} catch (Exception __e) { throw rethrow(__e); } }
static <A> A oneOf(List<A> l) {
  return l.isEmpty() ? null : l.get(new Random().nextInt(l.size()));
}

static char oneOf(String s) {
  return empty(s) ? '?' : s.charAt(random(l(s)));
}

static String oneOf(String... l) {
  return oneOf(asList(l));
}
static boolean flexMatchIC(String pat, String s) {
  return flexMatchIC2(pat, s);
}

static boolean flexMatchIC(String pat, String s, Matches m) {
  return flexMatchIC2(pat, s, m);
}

static boolean flexMatchIC(String pat, String s, Matches m, boolean joinBrackets) {
  return flexMatchIC2(pat, s, m, joinBrackets);
}
static boolean startsWithAndEndsWith(String s, String prefix, String suffix) {
  return startsWith(s, prefix) && endsWith(s, suffix);
}
static void unlock(Lock lock, String msg) {
  print("Unlocking: " + msg);
  lock.unlock();
}

static void unlock(Lock lock) {
  lock.unlock();
}
static TreeSet<String> caseInsensitiveSet() {
  return new TreeSet(String.CASE_INSENSITIVE_ORDER);
}

static TreeSet<String> caseInsensitiveSet(Collection<String> c) {
  return toCaseInsensitiveSet(c);
}
static String verbToThirdPerson(String s) {
  { String _a_196 = verbToThirdPersonMap().get(lower(s)); if (!empty(_a_196)) return _a_196; }
  return s + "s";
}
static boolean pcallF_anyFalse(List functions, Object... args) {
  boolean flag = false;
  for (Object f : functions)
    if (isFalse(pcallF(f, args)))
      flag = true;
  return flag;
}
static List<String> splitAtJavaToken(String s, String splitToken) {
  return splitByJavaToken(s, splitToken);
}
static boolean cic(List<String> l, String s) {
  return containsIgnoreCase(l, s);
}

static boolean cic(String[] l, String s) {
  return containsIgnoreCase(l, s);
}

static boolean cic(String s, char c) {
  return containsIgnoreCase(s, c);
}

static boolean cic(String a, String b) {
  return containsIgnoreCase(a, b);
}
static List<String> ai_texts_verified(String a, String b, String c) {
  return web_texts(ai_search_dollarX_verified(a, b, c));
}
static boolean isVerb(String s) {
  return containsNotNull(knownVerbs(), lower(s));
}
static void ai_fixTriple(T3<String, String, String> t) {
  if (t == null) return;
  t.a = ai_fix(t.a);
  t.b = ai_fix(t.b);
  t.c = ai_fix(t.c);
  
  List<String> tok = javaTokC(t.a);
  String s = last(tok);
  if (l(tok) > 1 && (isAdverb(s) || eqicOneOf(s, "can", "should", "rather", "will"))) {
    tok = javaTokC(t.a = trim(dropLastJavaToken(t.a)));
    t.b = s + " " + t.b;
  }
  if (l(tok) > 1 && eqic(first(tok), "why")) {
    t.a = trim(dropFirstJavaToken(t.a));
    t.b = first(tok) + " " + t.b;
  }
  
  tok = javaTokC(t.c);
  s = last(tok);
  if (l(tok) > 1 && isAdverb(s)) {
    t.c = trim(dropLastJavaToken(t.c));
    t.b = s + " " + t.b;
  }
  if (empty(t.c)) t.c = ".";
}
static List<String> trimAll(Collection<String> l) {
  List<String> l2 = new ArrayList();
  if (l != null) for (String s : l)
    l2.add(trim(s));
  return l2;
}
static <A> A collectionGet(Collection<A> c, int idx) {
  if (c == null || idx < 0 || idx >= l(c)) return null;
  if (c instanceof List) return get((List<A>) c, idx);
  Iterator<A> it = c.iterator();
  for (int i = 0; i < idx; i++) if (it.hasNext()) it.next(); else return null;
  return it.hasNext() ? it.next() : null;
}
static <A> void addIfNotNull(Collection<A> l, A a) {
  if (a != null) l.add(a);
}


static void pcallFAll(Collection l, Object... args) {
  if (l != null) for (Object f : l) pcallF(f, args);
}
static long now_virtualTime;
static long now() {
  return now_virtualTime != 0 ? now_virtualTime : System.currentTimeMillis();
}

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 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 List<String> nlTok(String s) {
  return javaTokPlusPeriod(s);
}
static List<WebNode> ai_search_dollarX_verified(String a, String b, String c) {
  String query = isDollarVar(a) ? c : a;
  return webs_search_dollarX(webFromTriples(a, b, c), indexedVerifiedWebs(query));
}

static List<WebNode> ai_search_dollarX_verified(T3<String, String, String> t) {
  return ai_search_dollarX_verified(t.a, t.b, t.c);
}
static boolean allLowerCaseCharacters(String s) {
  for (int i = 0; i < l(s); i++)
    if (Character.getType(s.charAt(i)) != Character.LOWERCASE_LETTER) return false;
  return true;
}

static WebNode web_setPosition(WebNode node, double x, double y) {
  node.x = x;
  node.y = y;
  return node;
}
static List<WebNode> indexedNodes_withInvalids(String s) {
  List<WebNode> l = indexedNodes_rawLookup(s), l1 = l;
  for (VirtualNodeIndex i : virtualNodeIndices()) {
    List x = i.get(s);
    if (nempty(x)) {
      if (l == l1) l = cloneList(l);
      l.addAll(x);
    }
  }
  return l;
}
static String javaTok_substringC(String s, int i, int j) {
  return s.substring(i, j);
}
static List<WebNode> ai_index_search_dollarX_new(String a, String b, String c) {
  String query = isDollarVar(a) ? c : a;
  return webs_tripleSearch_dollarX(a, b, c, indexedWebs(query));
}
static String sfuLL(Object... x) {
  return sfu(ll(x));
}
static String javaTok_substringN(String s, int i, int j) {
  if (i == j) return "";
  if (j == i+1 && s.charAt(i) == ' ') return " ";
  return s.substring(i, j);
}
// i must point at the (possibly imaginary) opening bracket
// index returned is index of closing bracket + 1
static int findEndOfCurlyBracketPart(List<String> cnc, int i) {
  int j = i+2, level = 1;
  while (j < cnc.size()) {
    if (eq(cnc.get(j), "{")) ++level;
    else if (eq(cnc.get(j), "}")) --level;
    if (level == 0)
      return j+1;
    ++j;
  }
  return cnc.size();
}

static List<WebNode> web_nodesFromVerifiedWebs(Collection<WebNode> nodes) {
  List<WebNode> l = new ArrayList();
  for (WebNode n : unnull(nodes))
    if (web_isVerified(n))
      l.add(n);
  return l;
}
static List<WebNode> indexedNodes(String s) {
  return ai_withoutInvalidNodes(cloneList(indexedNodes_withInvalids(s)));
}
static void lock(Lock lock) { try {
  ping();
  lock.lockInterruptibly();
  ping();
} catch (Exception __e) { throw rethrow(__e); } }

static void lock(Lock lock, String msg) {
  print("Locking: " + msg);
  lock(lock);
}

static void lock(Lock lock, String msg, long timeout) {
  print("Locking: " + msg);
  lockOrFail(lock, timeout);
}

static ReentrantLock lock() {
  return fairLock();
}
static String nWebs(int i) {
  return n_fancy(i, "web");
}

static String nWebs(Collection c) {
  return n_fancy(c, "web");
}
public static String join(String glue, Iterable<String> strings) {
  if (strings == null) return "";
  StringBuilder buf = new StringBuilder();
  Iterator<String> i = strings.iterator();
  if (i.hasNext()) {
    buf.append(i.next());
    while (i.hasNext())
      buf.append(glue).append(i.next());
  }
  return buf.toString();
}

public static String join(String glue, String... strings) {
  return join(glue, Arrays.asList(strings));
}

static String join(Iterable<String> strings) {
  return join("", strings);
}

static String join(Iterable<String> strings, String glue) {
  return join(glue, strings);
}

public static String join(String[] strings) {
  return join("", strings);
}


static String join(String glue, Pair p) {
  return p == null ? "" : str(p.a) + glue + str(p.b);
}

static String tok_unCurlyBracket(String s) {
  return isCurlyBraced(s) ? trim(dropFirstAndLast(s)) : s;
}
static void incIntThreadLocal(ThreadLocal<Integer> tl) {
  tl.set(toInt(tl.get())+1);
}


static void incIntThreadLocal(BetterThreadLocal<Integer> tl) {
  tl.set(toInt(tl.get())+1);
}

static Web simpleWeb() {
  Web web = new Web();
  
    web.useCLParse = false;
  
  return web;
}
static Web webWithoutIndexAndLock() {
  Web web = simpleWeb();
  web.index = null;
  web.lock = null;
  return web;
}
static int jfind(String s, String in) {
  return jfind(javaTok(s), in);
}

static int jfind(List<String> tok, String in) {
  return jfind(tok, 1, in);
}

static int jfind(List<String> tok, int startIdx, String in) {
  return jfind(tok, startIdx, in, null);
}

static int jfind(List<String> tok, String in, Object condition) {
  return jfind(tok, 1, in, condition);
}

static int jfind(List<String> tok, int startIdx, String in, Object condition) {
  List<String> tokin = javaTok(in);
  jfind_preprocess(tokin);
  return jfind(tok, startIdx, tokin, condition);
}

// assumes you preprocessed tokin
static int jfind(List<String> tok, List<String> tokin) {
  return jfind(tok, 1, tokin);
}

static int jfind(List<String> tok, int startIdx, List<String> tokin) {
  return jfind(tok, startIdx, tokin, null);
}

static int jfind(List<String> tok, int startIdx, List<String> tokin, Object condition) {
  return findCodeTokens(tok, startIdx, false, toStringArray(codeTokensOnly(tokin)), condition);
}

static void jfind_preprocess(List<String> tok) {
  for (String type : litlist("quoted", "id", "int"))
    replaceSublist(tok, ll("<", "", type, "", ">"), ll("<" + type + ">"));
  replaceSublist(tok, ll("\\", "", "*"), ll("\\*"));
}
static boolean odd(int i) {
  return (i & 1) != 0;
}

static boolean odd(long i) {
  return (i & 1) != 0;
}
static <A> T3<A, A, A> listToTriple(List<A> l) {
  return l(l) != 3 ? null : triple(get(l, 0), get(l, 1), get(l, 2));
}
static String firstQuoted(Collection<String> l) {
  if (l != null)
    for (String s : l)
      if (isQuoted(s))
        return s;
  return null;
}
static void logStructure(File logFile, Object o) {
  logQuoted(logFile, structure(o));
}

// quick version - log to file in program directory
static void logStructure(String fileName, Object o) {
  logStructure(getProgramFile(fileName), o);
}

static void logStructure(String progID, String fileName, Object o) {
  logStructure(getProgramFile(progID, fileName), o);
}
static WebRelation web_findRelationNamed(Web web, String s) {
  return web_findRelationWithLabel(web, s);
}
static boolean endsWithIgnoreCase(String a, String b) {
  return a != null && l(a) >= l(b) && a.regionMatches(true, l(a)-l(b), b, 0, l(b));
}
static List<String> ai_variationsOf(String s) {
  return uniquify(concatLists(ll(s), ai_texts_bidirectionalOperands(s, "unquotes to:")));
}
static String joinSubList(List<String> l, int i, int j) {
  return join(subList(l, i, j));
}

static String joinSubList(List<String> l, int i) {
  return join(subList(l, i));
}
static boolean isMultilineQuoted(String s) {
  if (!startsWith(s, "[")) return false;
  int i = 1;
  while (i < s.length() && s.charAt(i) == '=') ++i;
  return i < s.length() && s.charAt(i) == '[';
}
static void markWebsPosted_createMarker() {
  createMarkerFile("#1007609", "webs.posted.at." + psI(programID()));
}
static Set<String> ai_yesNoQuestionWord_1_list = litciset("am", "are", "is", "can", "does", "do", "did", "would", "might", "will", "must", "should", "has", "have");

static boolean ai_yesNoQuestionWord_1(String s) {
  return containsNotNull(ai_yesNoQuestionWord_1_list, s);
}
static int websMadeInThread() {
  return toInt(postSoftwareMadeWeb_websMadeInThread.get());
}

static int websMadeInThread(Thread thread) {
  return toInt(postSoftwareMadeWeb_websMadeInThread.get(thread));
}
static <A> ArrayList<A> litlist(A... a) {
  ArrayList l = new ArrayList(a.length);
  for (A x : a) l.add(x);
  return l;
}
static List<String> getClassNames(Collection l) {
  List<String> out = new ArrayList();
  if (l != null) for (Object o : l)
    out.add(o == null ? null : getClassName(o));
  return out;
}
static List map(Iterable l, Object f) {
  return map(f, l);
}

static List map(Object f, Iterable l) {
  List x = emptyList(l);
  if (l != null) for (Object o : l)
    x.add(callF(f, o));
  return x;
}


  static List map(F1 f, Iterable l) {
    List x = emptyList(l);
    if (l != null) for (Object o : l)
      x.add(callF(f, o));
    return x;
  }


static List map(Object f, Object[] l) { return map(f, asList(l)); }
static List map(Object[] l, Object f) { return map(f, l); }

static List map(Object f, Map map) {
  return map(map, f);
}

// map: func(key, value) -> list element
static List map(Map map, Object f) {
  List x = new ArrayList();
  if (map != null) for (Object _e : map.entrySet()) {
    Map.Entry e = (Map.Entry) _e;
    x.add(callF(f, e.getKey(), e.getValue()));
  }
  return x;
}
static Set<String> ai_questionWord_1_list = littreeset("what", "who", "why", "when", "where", "how");

static boolean ai_questionWord_1(String s) {
  return ai_questionWord_1_list.contains(lower(s));
}
static int max(int a, int b) { return Math.max(a, b); }
static int max(int a, int b, int c) { return max(max(a, b), c); }
static long max(int a, long b) { return Math.max((long) a, b); }
static long max(long a, long b) { return Math.max(a, b); }
static double max(int a, double b) { return Math.max((double) a, b); }
static float max(float a, float b) { return Math.max(a, b); }
static double max(double a, double b) { return Math.max(a, b); }

static int max(Collection<Integer> c) {
  int x = Integer.MIN_VALUE;
  for (int i : c) x = max(x, i);
  return x;
}

static double max(double[] c) {
  if (c.length == 0) return Double.MIN_VALUE;
  double x = c[0];
  for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]);
  return x;
}

static float max(float[] c) {
  if (c.length == 0) return Float.MAX_VALUE;
  float x = c[0];
  for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]);
  return x;
}

static byte max(byte[] c) {
  byte x = -128;
  for (byte d : c) if (d > x) x = d;
  return x;
}

static short max(short[] c) {
  short x = -0x8000;
  for (short d : c) if (d > x) x = d;
  return x;
}
static void restart() {
  Object j = getJavaX();
  call(j, "cleanRestart", get(j, "fullArgs"));
}
static int ai_endOfNounPhrase(List<String> tok) {
  int i = 1;
  while (i < l(tok)) {
    String t = get(tok, i);
    if (i == 1 && eqicOneOf(t, "a", "the")) i += 4;
    else if (nounPhraseContinuers().contains(lower(t)) || isAdjective(t) || isNoun(t)) i += 2;
    else break;
  }
  return i;
}
static <A> A assertEquals(Object x, A y) {
  return assertEquals(null, x, y);
}

static <A> A assertEquals(String msg, Object x, A y) {
  if (!(x == null ? y == null : x.equals(y)))
    throw fail((msg != null ? msg + ": " : "") + y + " != " + x);
  return y;
}
static List<String> tlft(String s) { return toLinesFullTrim(s); }
static List<String> tlft(File f) { return toLinesFullTrim(f); }

static boolean ai_isYesNoQuestion_1(String s) {
  return ai_yesNoQuestionWord_1(firstWord(s));
}
static Web webFromTriples(String... l) {
  Web web = simpleWeb();
  for (int i = 0; i < l(l); i += 3) {
    String a = unnull(get(l, i)), b = unnull(get(l, i+1)), c = unnull(get(l, i+2));
    web_addRelation(web, a, c, b);
  }
  return web;
}

static Web webFromTriples(List<String> l) {
  return webFromTriples(toStringArray(l));
}
// 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 String webToStringShort(Web web) {
  try {
    int n = l(web_nodes(web));
    if (n == 0) return "Empty web";
    WebNode a = web_firstNode(web);
    if (n == 1) return join(" = ", web_texts(a));
    if (n == 2) {
      WebNode b = web_secondNode(web);
      return ai_renderTriple(web_text(a), web_text(web_getRelation(a, b)), web_text(b));
    }
    return "Includes " + joinWithComma(map("web_text",web_nodes(web)));
  } catch (Throwable e) { printException(e);
    return "Error";
  }
}

static List<Web> nodesToWebs(Collection<WebNode> nodes) {
  Set<Web> webs = litorderedset();
  for (WebNode node : nodes)
    webs.add(node.web);
  return asList(webs);
}
static LinkedHashMap litorderedmap(Object... x) {
  LinkedHashMap map = new LinkedHashMap();
  litmap_impl(map, x);
  return map;
}
static Object pcallF(Object f, Object... args) {
  return pcallFunction(f, args);
}


static <A, B> B pcallF(F1<A, B> f, A a) { try {
  return f == null ? null : f.get(a);
} catch (Throwable __e) { return null; } }

static List<String> unquoteAll(List<String> l) {
  List<String> t = cloneList(l);
  for (int i = 0; i < l(t); i++)
    if (isQuoted(t.get(i)))
      t.set(i, unquote(t.get(i)));
  return t;
}
static boolean webs_search_noVar_bool(Web searchWeb, Collection<Web> webs) {
  return webs_search_noVar(searchWeb, webs) != null;
}
static List<String> splitAtSimpleArrow(String s) {
  return splitAtSimpleArrow(javaTok(s));
}

static List<String> splitAtSimpleArrow(List<String> tok) {
  List<String> l = new ArrayList();
  int i = 0;
  while (i < l(tok)) {
    int j = indexOfSubList(tok, ll("-", "", ">"), i);
    if (i >= l(tok)) break;
    if (j < 0) j = l(tok);
    l.add(trimJoin(tok.subList(i, j)));
    i = j+3;
  }
  return l;
}
static boolean all(Object pred, Collection l) {
  if (l != null) for (Object o : l) if (!isTrue(callF(pred, o))) return false;
  return true;
}
static Collection<Web> ai_withoutInvalidWebs(Collection<Web> l) {
  if (l == null) return null;
  for (Web n : l)
    if (ai_isInvalidWeb(n)) {
      Iterator<Web> it = iterator(l);
      Web node;
      List<Web> x = new ArrayList();
      while ((node = it.next()) != n)
        x.add(node);
      while (it.hasNext()) {
        node = it.next();
        if (!ai_isInvalidWeb(node))
          x.add(node);
      }
      return x;
    }
  return l;
}

static List<Web> ai_withoutInvalidWebs(List<Web> l) {
  return (List) ai_withoutInvalidWebs((Collection) l);
}
static String ai_tripleToString(T3<String, String, String> t) {
  return t == null ? "" : curlyBraceIfContainsArrow(t.a) + " -> " + curlyBraceIfContainsArrow(t.b) + " -> " + curlyBraceIfContainsArrow(t.c);
}
static <A extends Concept> A uniq(Class<A> c, Object... params) {
  return uniqueConcept(c, params);
}

static <A extends Concept> A uniq(Concepts cc, Class<A> c, Object... params) {
  return uniqueConcept(cc, c, params);
}
static List<Web> ai_fewestIndexedWebs(String a, String b, String c) {
  return ai_withoutInvalidWebs(nodesToWebs(shortestList3(
    indexedNodes_withInvalids(a),
    indexedNodes_withInvalids(b),
    indexedNodes_withInvalids(c))));
}
static boolean ai_nodeNameAllowedToPost(String s) {
  return !isDollarVar(s);
}
static <A> A nu(Class<A> c, Object... values) {
  A a = nuObject(c);
  setAll(a, values);
  return a;
}
static <A, B> Collection<B> values(Map<A, B> map) {
  return map == null ? emptyList() : map.values();
}


static <A, B> Collection<B> values(MultiMap<A, B> mm) {
  return mm == null ? emptyList() : concatLists(values(mm.data));
}

// returns l(s) if not found
static int smartIndexOf(String s, String sub, int i) {
  if (s == null) return 0;
  i = s.indexOf(sub, min(i, l(s)));
  return i >= 0 ? i : l(s);
}

static int smartIndexOf(String s, char c, int i) {
  if (s == null) return 0;
  i = s.indexOf(c, min(i, l(s)));
  return i >= 0 ? i : l(s);
}

static int smartIndexOf(String s, String sub) {
  return smartIndexOf(s, sub, 0);
}

static int smartIndexOf(String s, char c) {
  return smartIndexOf(s, c, 0);
}

static <A> int smartIndexOf(List<A> l, A sub) {
  return smartIndexOf(l, sub, 0);
}

static <A> int smartIndexOf(List<A> l, A sub, int start) {
  int i = indexOf(l, sub, start);
  return i < 0 ? l(l) : i;
}
static boolean neqicOneOf(String s, String... l) {
  return !eqicOneOf(s, l);
}
static boolean preferCached = false;
static boolean loadSnippet_debug = false;
static ThreadLocal<Boolean> loadSnippet_silent = new ThreadLocal();



static String loadSnippet(String snippetID) {
  try {
    return loadSnippet(parseSnippetID(snippetID), preferCached);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

static String loadSnippet(String snippetID, boolean preferCached) throws IOException {
  return loadSnippet(parseSnippetID(snippetID), preferCached);
}

public static String loadSnippet(long snippetID) {
  try {
    return loadSnippet(snippetID, preferCached);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

public static String loadSnippet(long snippetID, boolean preferCached) throws IOException {
  String text;
  
  // boss bot disabled for now for shorter transpilations
  
  /*text = getSnippetFromBossBot(snippetID);
  if (text != null) return text;*/
  
  initSnippetCache();
  text = DiskSnippetCache_get(snippetID);
  
  if (preferCached && text != null)
    return text;
  
  try {
    if (loadSnippet_debug && text != null) System.err.println("md5: " + md5(text));
    String url = tb_mainServer() + "/getraw.php?id=" + snippetID + "&utf8=1";
    if (nempty(text)) url += "&md5=" + md5(text);
    url += standardCredentials();
    String text2 = isTrue(loadSnippet_silent.get()) ? loadPageSilently(url) : loadPage(url);
    boolean same = eq(text2, "==*#*==");
    if (loadSnippet_debug) print("loadSnippet: same=" + same);
    if (!same) text = text2;
  } catch (RuntimeException e) {
    e.printStackTrace();
    throw new IOException("Snippet #" + snippetID + " not found or not public");
  }

  try {
    initSnippetCache();
    DiskSnippetCache_put(snippetID, text);
  } catch (IOException e) {
    System.err.println("Minor warning: Couldn't save snippet to cache ("  + DiskSnippetCache_getDir() + ")");
  }

  return text;
}

static File DiskSnippetCache_dir;

public static void initDiskSnippetCache(File dir) {
  DiskSnippetCache_dir = dir;
  dir.mkdirs();
}

public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException {
  return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null);
}

private static File DiskSnippetCache_getFile(long snippetID) {
  return new File(DiskSnippetCache_dir, "" + snippetID);
}

public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException {
  saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet);
}

public static File DiskSnippetCache_getDir() {
  return DiskSnippetCache_dir;
}

public static void initSnippetCache() {
  if (DiskSnippetCache_dir == null)
    initDiskSnippetCache(getGlobalCache());
}

static boolean web_tripelizableNode(WebNode n) {
  return web_countLabels(n) == 1 && (n.visInfo == null || eq(n.visInfo, ""));
}
static List<WebRelation> web_allRelationsWithLabel(Web web, String label) {
  Lisp l = web.parseLabel(label);
  List<WebRelation> out = new ArrayList();
  for (WebRelation r : web_relationObjects(web))
    if (r.hasLabel(l))
      out.add(r);
  return out;
}
static char lastChar(String s) {
  return empty(s) ? '\0' : s.charAt(l(s)-1);
}
static String verbToPast(String s) {
  { String _a_249 = verbToPastMap().get(lower(s)); if (!empty(_a_249)) return _a_249; }
  return s + "s";
}
static <A> A setThreadLocal(ThreadLocal<A> tl, A value) {
  A old = tl.get();
  tl.set(value);
  return old;
}
static boolean startsWith(String a, String b) {
  return a != null && a.startsWith(b);
}

static boolean startsWith(String a, char c) {
  return nempty(a) && a.charAt(0) == c;
}


  static boolean startsWith(String a, String b, Matches m) {
    if (!startsWith(a, b)) return false;
    m.m = new String[] {substring(a, l(b))};
    return true;
  }


static boolean startsWith(List a, List b) {
  if (a == null || l(b) > l(a)) return false;
  for (int i = 0; i < l(b); i++)
    if (neq(a.get(i), b.get(i)))
      return false;
  return true;
}




static String ai_fix(String s) {
  s = trim(s);
  if (startsWith(s, "{") && endsWith(s, "}")) s = trim(dropFirstAndLast(s));
  return s;
}
static <A> HashSet<A> litset(A... items) {
  return lithashset(items);
}
static <A> TreeSet<A> littreeset(A... items) {
  TreeSet<A> set = new TreeSet();
  for (A a : items) set.add(a);
  return set;
}
static <A extends Concept> A uniqueConcept(Class<A> c, Object... params) {
  return uniqueConcept(mainConcepts, c, params);
}

static <A extends Concept> A uniqueConcept(Concepts cc, Class<A> c, Object... params) {
  params = expandParams(c, params);
  A x = findConceptWhere(cc, c, params);
  if (x == null) {
    x = unlisted(c);
    csetAll(x, params);
    cc.register(x);
  }
  return x;
}
// does not store null values
static MultiMap multiMapIndexByField(Collection c, String field) {
  MultiMap map = new MultiMap();
  for (Object a : c) {
    Object val = getOpt(a, field);
    if (val != null)
      map.put(val, a);
  }
  return map;
}
static void pcallF_all(Collection l, Object... args) {
  if (l != null) for (Object f : l) pcallF(f, args);
}
static boolean isDollarVar(String s) {
  return startsWith(s, '$') && isJavaIdentifierAfter(s, 1);
}
static List<WebNode> webs_search_dollarX(Web searchWeb, Collection<Web> webs) {
  List < Map < WebNode , WebNode > > mappings = new ArrayList();
  for (Web web : webs)
    addIfNotNull(mappings, web_matchAllPerms_x(searchWeb, web, "nodeMatch_dollarVars"));
  return getFromAll(mappings, web_findNode(searchWeb, "$X"));
}

static List<VirtualNodeIndex> virtualNodeIndices_list = synchroList();

static List<VirtualNodeIndex> virtualNodeIndices() {
  return virtualNodeIndices_list;
}
static String sfu(Object o) { return structureForUser(o); }
static <A> A callMain(A c, String... args) {
  callOpt(c, "main", new Object[] {args});
  return c;
}

static void callMain() {
  callMain(mc());
}
static <A> List<A> shortestList3(List<A> a, List<A> b, List<A> c) {
  int la = l(a), lb = l(b);
  if (la < lb)
    return l(c) < la ? c : a;
  else
    return l(c) < lb ? c : b;
}
static Cache<MultiMap<String, WebNode>> cachedNodeIndex2_cache = new Cache("dwlw_fullNodeIndex_ci");
static boolean cachedNodeIndex2_monitorCircleEditor;
static boolean cachedNodeIndex2_first = true;
static FileStatus cachedNodeIndex2_status;

static MultiMap<String, WebNode> cachedNodeIndex2() {
  if (cachedNodeIndex2_first) {
    cachedNodeIndex2_first = false;
    onWebsChanged("cachedNodeIndex2_clear");
    onTransientWebAdded_do("cachedNodeIndex2_onNewWeb");
    onTransientWebRemoved_do("cachedNodeIndex2_onWebRemoved");
    setAdd(postSoftwareMadeWeb_onNewWeb, "cachedNodeIndex2_onNewWeb");
  }
  
  if (cachedNodeIndex2_monitorCircleEditor) {
    FileStatus status = conceptsFileStatus(circlesEditorDBID());
    if (neq(cachedNodeIndex2_status, status)) {
      cachedNodeIndex2_status = status;
      cachedNodeIndex2_cache.clear();
    }
  }
  
  return cachedNodeIndex2_cache.get();
}

static void cachedNodeIndex2_clear() {
  cachedNodeIndex2_cache.clear();
}

static boolean cachedNodeIndex2_onNewWeb(Web web, Object[] params) {
  return cachedNodeIndex2_onNewWeb(web);
}

static boolean cachedNodeIndex2_onNewWeb(Web web) {
  Lock _lock_182 = cachedNodeIndex2_cache.lock; lock(_lock_182); try {
  //print("Incremental update");
  ai_addWebToIndex(cachedNodeIndex2_cache.get(), web);
  cachedNodeIndex2_cache.changeCount++;
  return false; // don't trigger full update
} finally { unlock(_lock_182); } }

static void cachedNodeIndex2_onWebRemoved(Web web) {
  Lock _lock_183 = cachedNodeIndex2_cache.lock; lock(_lock_183); try {
  print("Incremental removal");
  ai_removeWebFromIndex(cachedNodeIndex2_cache.get(), web);
  cachedNodeIndex2_cache.changeCount++;
} finally { unlock(_lock_183); } }

static long cachedNodeIndex_changeCount() {
  return cachedNodeIndex2_cache.changeCount;
}

static String curlyBraceIfContainsArrow(String s) {
  return tok_containsSimpleArrow(s) ? optionalCurlyBrace(s) : s;
}
/** writes safely (to temp file, then rename) */
static File saveTextFile(String fileName, String contents) throws IOException {
  CriticalAction action = beginCriticalAction("Saving file " + fileName + " (" + l(contents) + " chars)");
  try {
    File file = new File(fileName);
    File parentFile = file.getParentFile();
    if (parentFile != null)
      parentFile.mkdirs();
    String tempFileName = fileName + "_temp";
    File tempFile = new File(tempFileName);
    if (contents != null) {
      if (tempFile.exists()) try {
        String saveName = tempFileName + ".saved." + now();
        copyFile(tempFile, new File(saveName));
      } catch (Throwable e) { printStackTrace(e); }
      FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath());
      OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
      PrintWriter printWriter = new PrintWriter(outputStreamWriter);
      printWriter.print(contents);
      printWriter.close();
    }
    
    if (file.exists() && !file.delete())
      throw new IOException("Can't delete " + fileName);
  
    if (contents != null)
      if (!tempFile.renameTo(file))
        throw new IOException("Can't rename " + tempFile + " to " + file);
        
    return file;
  } finally {
    action.done();
  }
}

static File saveTextFile(File fileName, String contents) { try {
  saveTextFile(fileName.getPath(), contents);
  return fileName;
} catch (Exception __e) { throw rethrow(__e); } }
// This is made for NL parsing.
// It's javaTok extended with "..." token, "$n" and "#n" and
// special quotes (which are converted to normal ones).

static List<String> javaTokPlusPeriod(String s) {
  List<String> tok = new ArrayList<String>();
  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 Collection<Web> allWebsFromCachedNodeIndex_uncached() {
  return nodesToWebs(values(cachedNodeIndex()));
}
static void logQuoted(String logFile, String line) {
  logQuoted(getProgramFile(logFile), line);
}

static void logQuoted(File logFile, String line) {
  appendToFile(logFile, quote(line) + "\n");
}
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 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 containsIgnoreCase(Collection<String> 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 web_unquote(WebNode node) {
  return node == null ? null : unquote(web_text(node));
}
static Object[] assertEvenLength(Object[] a) {
  assertTrue(even(l(a)));
  return a;
}
static TreeSet<String> toCaseInsensitiveSet(Collection<String> c) {
  // TODO: check if it already is a CI set
  TreeSet<String> set = caseInsensitiveSet();
  if (c != null) set.addAll(c);
  return set;
}
static Web webs_search_noVar(Web searchWeb, Collection<Web> webs) {
  for (Web web : webs)
    if (web_matchAllPerms(searchWeb, web) != null)
      return web;
  return null;
}
static List<WebNode> indexedNodes_rawLookup(String s) {
  List<WebNode> l = cachedNodeIndex().get(ai_shortenForIndex(s));
  AI_SubSpace ss = ai_currentSubSpace();
  if (ss != null) {
    List<WebNode> l2 = ss.indexedNodes_rawLookup(s);
    return concatLists_conservative(l2, l);
  }
  return l;
}
static <A> List<A> uniquify(Collection<A> l) {
  return uniquifyList(l);
}
static void web_setLabels_forced(WebNode node, List<Lisp> labels) {
  web_deindex(node);
  node.labels = cloneList(labels);
  web_index(node);
}
static Map litmap(Object... x) {
  HashMap map = new HashMap();
  litmap_impl(map, x);
  return map;
}

static void litmap_impl(Map map, Object... x) {
  for (int i = 0; i < x.length-1; i += 2)
    if (x[i+1] != null)
      map.put(x[i], x[i+1]);
}
static Cache<Map<String, String>> verbToThirdPersonMap_cache = new Cache("verbToThirdPersonMap_load");
static Map<String, String> verbToThirdPersonMap() { return verbToThirdPersonMap_cache.get(); }

static Map<String, String> verbToThirdPersonMap_load() {
  HashMap<String, String> map = new HashMap();
  for (List<String> l : englishVerbQuintuples())
    map.put(get(l, 0), get(l, 3));
  return map;
}

static <A> int indexOfAny(List<A> l, int i, A... x) {
  while (i < l(l))
    if (eqOneOf(l.get(i), x)) return i; else ++i;
  return -1;
}

static <A> int indexOfAny(List<A> l, Collection<A> x) {
  return indexOfAny(l, 0, x);
}

static <A> int indexOfAny(List<A> l, int i, Collection<A> x) {
  if (nempty(x))
    while (i < l(l))
      if (x.contains(l.get(i))) return i; else ++i;
  return -1;
}

static int indexOfAny(String s, int i, String chars) {
  for (; i < l(s); i++)
    if (chars.indexOf(s.charAt(i)) >= 0)
      return i;
  return -1;
}
static ThreadLocal<String> loadPage_charset = new ThreadLocal();
static boolean loadPage_allowGzip = true, loadPage_debug;
static boolean loadPage_anonymous; // don't send computer ID
static int loadPage_verboseness = 100000;
static int loadPage_retries = 1; //60; // seconds
static ThreadLocal<Boolean> loadPage_silent = new ThreadLocal();
static volatile int loadPage_forcedTimeout; // ms
static ThreadLocal<Map<String, List<String>>> loadPage_responseHeaders = new ThreadLocal();
static ThreadLocal<Map<String, String>> loadPage_extraHeaders = new ThreadLocal();

public static String loadPageSilently(String url) { try {
  return loadPageSilently(new URL(loadPage_preprocess(url)));
} catch (Exception __e) { throw rethrow(__e); } }

public static String loadPageSilently(URL url) { try {
  IOException e = null;
  for (int tries = 0; tries < loadPage_retries; tries++)
    try {
      URLConnection con = loadPage_openConnection(url);
      return loadPage(con, url);
    } catch (IOException _e) {
      e = _e;
      if (loadPageThroughProxy_enabled) {
        print("Trying proxy because of: " + e);
        try {
          return loadPageThroughProxy(str(url));
        } catch (Throwable e2) {
          print("  " + exceptionToStringShort(e2));
        }
      } else if (loadPage_debug)
        print(e);
      sleepSeconds(1);
    }
  throw e;
} catch (Exception __e) { throw rethrow(__e); } }

static String loadPage_preprocess(String url) {  
  if (url.startsWith("tb/")) // don't think we use this anymore
    url = tb_mainServer() + "/" + url;
  if (url.indexOf("://") < 0)
    url = "http://" + url;
  return url;
}

public static String loadPage(String url) { try {
  url = loadPage_preprocess(url);
  if (!isTrue(loadPage_silent.get()))
    print("Loading: " + hideCredentials(url));
  return loadPageSilently(new URL(url));
} catch (Exception __e) { throw rethrow(__e); } }

public static String loadPage(URL url) {
  print("Loading: " + hideCredentials(url.toExternalForm()));
  return loadPageSilently(url);
}

static String loadPage(URLConnection con, URL url) throws IOException {
  return loadPage(con, url, true);
}

static String loadPage(URLConnection con, URL url, boolean addHeaders) throws IOException {
  Map<String, String> extraHeaders = getAndClearThreadLocal(loadPage_extraHeaders);
  if (addHeaders) try {
    if (!loadPage_anonymous)
      setHeaders(con);
    if (loadPage_allowGzip)
      con.setRequestProperty("Accept-Encoding", "gzip");
    con.setRequestProperty("X-No-Cookies", "1");
    for (String key : keys(extraHeaders))
      con.setRequestProperty(key, extraHeaders.get(key));
  } catch (Throwable e) {} // fails if within doPost
  String contentType = con.getContentType();
  if (contentType == null) {
    //printStruct("Headers: ", con.getHeaderFields());
    throw new IOException("Page could not be read: " + url);
  }
  //print("Content-Type: " + contentType);
  String charset = loadPage_charset == null ? null : loadPage_charset.get();
  if (charset == null) charset = loadPage_guessCharset(contentType);
  
  InputStream in = con.getInputStream();
  try {
    loadPage_responseHeaders.set(con.getHeaderFields());
    if ("gzip".equals(con.getContentEncoding())) {
      if (loadPage_debug)
        print("loadPage: Using gzip.");
      in = new GZIPInputStream(in);
    }
    Reader r = new InputStreamReader(in, charset);
    
    StringBuilder buf = new StringBuilder();
    int n = 0;
    while (true) {
      int ch = r.read();
      if (ch < 0)
        break;
      buf.append((char) ch);
      ++n;
      if ((n % loadPage_verboseness) == 0) print("  " + n + " chars read");
    }
    return buf.toString();
  } finally { in.close(); }
}

static String loadPage_guessCharset(String contentType) {
  Pattern p = Pattern.compile("text/[a-z]+;\\s*charset=([^\\s]+)\\s*");
  Matcher m = p.matcher(contentType);
  String match = m.matches() ? m.group(1) : null;
  if (loadPage_debug)
    print("loadPage: contentType=" + contentType + ", match: " + match);
  /* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */
  //return or(match, "ISO-8859-1");
  return or(match, "UTF-8");
}

static URLConnection loadPage_openConnection(URL url) {
  URLConnection con = openConnection(url);
  if (loadPage_forcedTimeout != 0)
    setURLConnectionTimeouts(con, loadPage_forcedTimeout);
  return con;
}
static String n_fancy(long l, String name) {
  return formatWithThousandsSeparator(l) + " " + trim(l == 1 ? singular(name) : getPlural(name));
}

static String n_fancy(Collection l, String name) {
  return n_fancy(l(l), name);
}

static String n_fancy(Map m, String name) {
  return n_fancy(l(m), name);
}

static String n_fancy(Object[] a, String name) {
  return n_fancy(l(a), name);
}


static String md5(String text) { try {
  if (text == null) return "-";
  return bytesToHex(md5_impl(text.getBytes("UTF-8"))); // maybe different than the way PHP does it...
} catch (Exception __e) { throw rethrow(__e); } }

static String md5(byte[] data) {
  if (data == null) return "-";
  return bytesToHex(md5_impl(data));
}

static MessageDigest md5_md;

/*static byte[] md5_impl(byte[] data) ctex {
  if (md5_md == null)
    md5_md = MessageDigest.getInstance("MD5");
  return ((MessageDigest) md5_md.clone()).digest(data);
}*/

static byte[] md5_impl(byte[] data) { try {
  return MessageDigest.getInstance("MD5").digest(data);
} catch (Exception __e) { throw rethrow(__e); } }

static String md5(File file) { try {
  return md5(loadBinaryFile(file));
} catch (Exception __e) { throw rethrow(__e); } }
static Object[] concatArrays(Object[]... arrays) {
  int l = 0;
  for (Object[] a : arrays) l += l(a);
  Object[] x = new Object[l];
  int i = 0;
  for (Object[] a : arrays) if (a != null) {
    System.arraycopy(a, 0, x, i, l(a));
    i += l(a);
  }
  return x;
}

static List<Lisp> map_lisp_intern(Collection<Lisp> l) {
  List<Lisp> out = new ArrayList(l(l));
  if (l != null) for (Lisp x : l)
    out.add(lisp_intern(x));
  return out;
}
static WebNode web_secondNode(Web web) {
  return web == null ? null : second(web.nodes);
}
static String trimJoin(List<String> s) {
  return trim(join(s));
}
static void createMarkerFile(String progID, String name) {
  File f = getProgramFile(progID, name);
  if (fileLength(f) == 0)
    saveTextFile(f, "1");
}
static Set<String> ai_is_myYourEtc = litciset("a", "my", "your", "their", "his", "our", "the");

static boolean ai_is_myYourEtc(String s) {
  return containsNotNull(ai_is_myYourEtc, s);
}
static boolean isNoun(String s) {
  return isSingularNoun(s) || isPluralNoun(s);
}
static Object sleepQuietly_monitor = new Object();

static void sleepQuietly() { try {
  assertFalse(isAWTThread());
  synchronized(sleepQuietly_monitor) { sleepQuietly_monitor.wait(); }
} catch (Exception __e) { throw rethrow(__e); } }
static String joinNempties(String sep, String... strings) {
  return joinStrings(sep, strings);
}

static String joinNempties(String sep, Collection<String> strings) {
  return joinStrings(sep, strings);
}
static int toInt(Object o) {
  if (o == null) return 0;
  if (o instanceof Number)
    return ((Number) o).intValue();
  if (o instanceof String)
    return parseInt((String) o);
  throw fail("woot not int: " + getClassName(o));
}

static int toInt(long l) {
  if (l != (int) l) throw fail("Too large for int: " + l);
  return (int) l;
}
static boolean containsNotNull(Collection c, Object o) {
  return o != null && contains(c, o);
}

static boolean containsNotNull(Object[] x, Object o) {
  return o != null && contains(x, o);
}
static String tb_mainServer_default = "http://tinybrain.de:8080";
static Object tb_mainServer_override; // func -> S

static String tb_mainServer() {
  if (tb_mainServer_override != null) return (String) callF(tb_mainServer_override);
  return trim(loadTextFile(tb_mainServer_file(),
    tb_mainServer_default));
}

static File tb_mainServer_file() {
  return getProgramFile("#1001638", "mainserver.txt");
}

static boolean tb_mainServer_isDefault() {
  return eq(tb_mainServer(), tb_mainServer_default);
}
static boolean structure_showTiming, structure_checkTokenCount;

static String structure(Object o) {
  return structure(o, new structure_Data());
}

static String structure(Object o, structure_Data d) {
  StringWriter sw = new StringWriter();
  d.out = new PrintWriter(sw);
  structure_go(o, d);
  String s = str(sw);
  if (structure_checkTokenCount) {
    print("token count=" + d.n);
    assertEquals("token count", l(javaTokC(s)), d.n);
  }
  return s;
}

static void structure_go(Object o, structure_Data d) {
  structure_1(o, d);
  while (nempty(d.stack))
    popLast(d.stack).run();
}

static void structureToPrintWriter(Object o, PrintWriter out) {
  structure_Data d = new structure_Data();
  d.out = out;
  structure_go(o, d);
}

// leave to false, unless unstructure() breaks
static boolean structure_allowShortening = false;

static class structure_Data {
  PrintWriter out;
  int stringSizeLimit;
  int shareStringsLongerThan = 20;
  boolean noStringSharing;

  IdentityHashMap<Object,Integer> seen = new IdentityHashMap();
  //new BitSet refd;
  HashMap<String,Integer> strings = new HashMap();
  HashSet<String> concepts = new HashSet();
  HashMap<Class, List<Field>> fieldsByClass = new HashMap();
  int n; // token count
  List<Runnable> stack = new ArrayList();
  
  // append single token
  structure_Data append(String token) { out.print(token); ++n; return this; }
  structure_Data append(int i) { out.print(i); ++n; return this; }
  
  // append multiple tokens
  structure_Data append(String token, int tokCount) { out.print(token); n += tokCount; return this; }
  
  // extend last token
  structure_Data app(String token) { out.print(token); return this; }
  structure_Data app(int i) { out.print(i); return this; }
}

static void structure_1(final Object o, final structure_Data d) {
  if (o == null) { d.append("null"); return; }
  
  Class c = o.getClass();
  boolean concept = false;
  
    concept = o instanceof Concept;
  
  List<Field> lFields = d.fieldsByClass.get(c);
  
  if (lFields == null) {
    // these are never back-referenced (for readability)
    
    if (o instanceof Number) {
      PrintWriter out = d.out;
if (o instanceof Integer) { int i = ((Integer) o).intValue(); out.print(i); d.n += i < 0 ? 2 : 1; return; }
      if (o instanceof Long) { long l = ((Long) o).longValue(); out.print(l); out.print("L"); d.n += l < 0 ? 2 : 1; return; }
      if (o instanceof Float) { d.append("fl ", 2); quoteToPrintWriter(str(o), out); return; }
      if (o instanceof Double) { d.append("d(", 3); quoteToPrintWriter(str(o), out); d.append(")"); return; }
      if (o instanceof BigInteger) { out.print("bigint("); out.print(o); out.print(")"); d.n += ((BigInteger) o).signum() < 0 ? 5 : 4; return; }
    }
  
    if (o instanceof Boolean) {
      d.append(((Boolean) o).booleanValue() ? "t" : "f"); return;
    }
      
    if (o instanceof Character) {
      d.append(quoteCharacter((Character) o)); return;
    }
      
    if (o instanceof File) {
      d.append("File ").append(quote(((File) o).getPath())); return;
    }
      
    // referencable objects follow
    
    Integer ref = d.seen.get(o);
    if (o instanceof String && ref == null) ref = d.strings.get((String) o);
    if (ref != null) { /*d.refd.set(ref);*/ d.append("t").app(ref); return; }

    if (!(o instanceof String))
      d.seen.put(o, d.n); // record token number
    else {
      String s = d.stringSizeLimit != 0 ? shorten((String) o, d.stringSizeLimit) : (String) o;
      if (!d.noStringSharing) {
        if (d.shareStringsLongerThan == Integer.MAX_VALUE)
          d.seen.put(o, d.n);
        if (l(s) >= d.shareStringsLongerThan)
          d.strings.put(s, d.n);
      }
      quoteToPrintWriter(s, d.out); d.n++; return;
    }
      
    if (o instanceof HashSet) {
      d.append("hashset ");
      structure_1(new ArrayList((Set) o), d);
      return;
    }
  
    if (o instanceof TreeSet) {
      d.append("treeset ");
      structure_1(new ArrayList((Set) o), d);
      return;
    }
    
    String name = c.getName();
    if (o instanceof Collection
      && !startsWith(name, "main$")
      /* && neq(name, "main$Concept$RefL") */) {
      d.append("[");
      final int l = d.n;
      final Iterator it = ((Collection) o).iterator();
      d.stack.add(new Runnable() { public void run() { try { 
        if (!it.hasNext())
          d.append("]");
        else {
          d.stack.add(this);
          if (d.n != l) d.append(", ");
          structure_1(it.next(), d);
        }
      
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (!it.hasNext())\r\n          d.append(\"]\");\r\n        else {\r\n          d.sta..."; }});
      return;
    }
    
    if (o instanceof Map && !startsWith(name, "main$")) {
      if (o instanceof LinkedHashMap) d.append("lhm");
      else if (o instanceof HashMap) d.append("hm");
      d.append("{");
      final int l = d.n;
      final Iterator it = ((Map) o).entrySet().iterator();
      
      d.stack.add(new Runnable() {
        boolean v;
        Map.Entry e;
        
        public void run() {
          if (v) {
            d.append("=");
            v = false;
            d.stack.add(this);
            structure_1(e.getValue(), d);
          } else {
            if (!it.hasNext())
              d.append("}");
            else {
              e = (Map.Entry) it.next();
              v = true;
              d.stack.add(this);
              if (d.n != l) d.append(", ");
              structure_1(e.getKey(), d);
            }
          }
        }
      });
      return;
    }
    
    if (c.isArray()) {
      if (o instanceof byte[]) {
        d.append("ba ").append(quote(bytesToHex((byte[]) o))); return;
      }
  
      final int n = Array.getLength(o);
  
      if (o instanceof boolean[]) {
        String hex = boolArrayToHex((boolean[]) o);
        int i = l(hex);
        while (i > 0 && hex.charAt(i-1) == '0' && hex.charAt(i-2) == '0') i -= 2;
        d.append("boolarray ").append(n).app(" ").append(quote(substring(hex, 0, i))); return;
      }
      
      String atype = "array", sep = ", ";
  
      if (o instanceof int[]) {
        //ret "intarray " + quote(intArrayToHex((int[]) o));
        atype = "intarray";
        sep = " ";
      }
      
      d.append(atype).append("{");
      d.stack.add(new Runnable() {
        int i;
        public void run() {
          if (i >= n)
            d.append("}");
          else {
            d.stack.add(this);
            if (i > 0) d.append(", ");
            structure_1(Array.get(o, i++), d);
          }
        }
      });
      return;
    }
  
    if (o instanceof Class) {
      d.append("class(", 2).append(quote(((Class) o).getName())).append(")"); return;
    }
      
    if (o instanceof Throwable) {
      d.append("exception(", 2).append(quote(((Throwable) o).getMessage())).append(")"); return;
    }
      
    if (o instanceof BitSet) {
      BitSet bs = (BitSet) o;
      d.append("bitset{", 2);
      int l = d.n;
      for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
        if (d.n != l) d.append(", ");
        d.append(i);
      }
      d.append("}"); return;
    }
      
    // Need more cases? This should cover all library classes...
    if (name.startsWith("java.") || name.startsWith("javax.")) {
      d.append("j ").append(quote(str(o))); return; // Hm. this is not unstructure-able
    }
    
    
      if (o instanceof Lisp) {
        d.append("l(", 2);
        final Lisp lisp = (Lisp) ( o);
        structure_1(lisp.head, d);
        final Iterator it = lisp.args == null ? emptyIterator() : lisp.args.iterator();
        d.stack.add(new Runnable() { public void run() { try { 
          if (!it.hasNext())
            d.append(")");
          else {
            d.stack.add(this);
            d.append(", ");
            structure_1(it.next(), d);
          }
        
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (!it.hasNext())\r\n            d.append(\")\");\r\n          else {\r\n           ..."; }});
        return;
      }
    
      
    /*if (name.equals("main$Lisp")) {
      fail("lisp not supported right now");
    }*/
    
    String dynName = shortDynamicClassName(o);
    if (concept && !d.concepts.contains(dynName)) {
      d.concepts.add(dynName);
      d.append("c ");
    }
    
    // serialize an object with fields.
    // first, collect all fields and values in fv.
    
    TreeSet<Field> fields = new TreeSet<Field>(new Comparator<Field>() {
      public int compare(Field a, Field b) {
        return stdcompare(a.getName(), b.getName());
      }
    });
    
    Class cc = c;
    while (cc != Object.class) {
      for (Field field : getDeclaredFields_cached(cc)) {
        if ((field.getModifiers() & (java.lang.reflect.Modifier.STATIC | java.lang.reflect.Modifier.TRANSIENT)) != 0)
          continue;
        String fieldName = field.getName();
        
        fields.add(field);
        
        // put special cases here...
      }
        
      cc = cc.getSuperclass();
    }
    
    lFields = asList(fields);
    
    // Render this$1 first because unstructure needs it for constructor call.
    
    for (int i = 0; i < l(lFields); i++) {
      Field f = lFields.get(i);
      if (f.getName().equals("this$1")) {
        lFields.remove(i);
        lFields.add(0, f);
        break;
      }
    }
  
    
    d.fieldsByClass.put(c, lFields);
  } // << if (lFields == null)
  else { // ref handling for lFields != null
    Integer ref = d.seen.get(o);
    if (ref != null) { /*d.refd.set(ref);*/ d.append("t").app(ref); return; }
    d.seen.put(o, d.n); // record token number
  }

  LinkedHashMap<String,Object> fv = new LinkedHashMap();
  for (Field f : lFields) {
    Object value;
    try {
      value = f.get(o);
    } catch (Exception e) {
      value = "?";
    }
      
    if (value != null)
      fv.put(f.getName(), value);
    
  }
  
  String name = c.getName();
  String shortName = dropPrefix("main$", name);
    
  // Now we have fields & values. Process fieldValues if it's a DynamicObject.
  
  // omit field "className" if equal to class's name
  if (concept && eq(fv.get("className"), shortName))
    fv.remove("className");
          
  if (o instanceof DynamicObject) {
    fv.putAll((Map) fv.get("fieldValues"));
    fv.remove("fieldValues");
    shortName = shortDynamicClassName(o);
    fv.remove("className");
  }
  
  String singleField = fv.size() == 1 ? first(fv.keySet()) : null;
  
  d.append(shortName);
  
  
  final int l = d.n;
  final Iterator it = fv.entrySet().iterator();
  
  d.stack.add(new Runnable() { public void run() { try { 
    if (!it.hasNext()) {
      if (d.n != l)
        d.append(")");
    } else {
      Map.Entry e = (Map.Entry) it.next();
      d.append(d.n == l ? "(" : ", ");
      d.append((String) e.getKey()).append("=");
      d.stack.add(this);
      structure_1(e.getValue(), d);
    }
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (!it.hasNext()) {\r\n      if (d.n != l)\r\n        d.append(\")\");\r\n    } else..."; }});
}

static int web_countLabels(WebNode n) {
  return web_numberOfLabels(n);
}
static String dropFirstJavaToken(String s) {
  return join(dropFirst(2, javaTok(s)));
}
static <A> Iterator<A> iterator(Collection<A> c) {
  return c == null ? emptyIterator() : c.iterator();
}
static List onTransientWebRemoved_list = synchroList();

// f: voidfunc(Web)
static void onTransientWebRemoved_do(Object f) {
  setAdd(onTransientWebRemoved_list, f);
}

static void onTransientWebRemoved(Web web) {
  pcallFAll(onTransientWebRemoved_list, web);
}
static Cache<Map<String, String>> verbToPastMap_cache = new Cache("verbToPastMap_load");
static Map<String, String> verbToPastMap() { return verbToPastMap_cache.get(); }

static Map<String, String> verbToPastMap_load() {
  HashMap<String, String> map = new HashMap();
  for (List<String> l : englishVerbQuintuples())
    map.put(get(l, 0), get(l, 1));
  return map;
}

static List onTransientWebAdded_list = synchroList();

// f: voidfunc(Web)
static void onTransientWebAdded_do(Object f) {
  setAdd(onTransientWebAdded_list, f);
}

static void onTransientWebAdded(Web web) {
  pcallFAll(onTransientWebAdded_list, web);
}
static <A> List<A> dropFirstAndLast(int n, List<A> l) {
  return new ArrayList(subList(l, n, l(l)-n));
}

static String dropFirstAndLast(String s) {
  return substring(s, 1, l(s)-1);
}
static List<WebNode> web_nodes(Web web) {
  return web_nonRelationNodes(web);
}
static String standardCredentials() {
  String user = standardCredentialsUser();
  String pass = standardCredentialsPass();
  if (nempty(user) && nempty(pass))
    return "&_user=" + urlencode(user) + "&_pass=" + urlencode(pass);
  return "";
}
static List<String> toLinesFullTrim(String s) {
  List<String> l = new ArrayList();
  for (String line : toLines(s)) if (nempty(line = trim(line))) l.add(line);
  return l;
}

static List<String> toLinesFullTrim(File f) {
  List<String> l = new ArrayList();
  for (String line : linesFromFile(f)) if (nempty(line = trim(line))) l.add(line);
  return l;
}

static String joinWithComma(Collection<String> c) {
  return join(", ", c);
}

static String joinWithComma(String... c) {
  return join(", ", c);
}

static boolean flexMatchIC2_debug;

static boolean flexMatchIC2(String pat, String s) {
  return flexMatchIC2(pat, s, null);
}

static boolean flexMatchIC2(String pat, String s, Matches m) {
  return flexMatchIC2(javaTok(pat), javaTok_cached(unnull(s)), m);
}

static boolean flexMatchIC2(String pat, String s, Matches m, boolean joinBrackets) {
  return flexMatchIC2(javaTok(pat), javaTok_cached(unnull(s)), m, joinBrackets);
}

static boolean flexMatchIC2(List<String> tokpat, List<String> tokfull, Matches m) {
  return flexMatchIC2(tokpat, tokfull, m, true);
}

static boolean flexMatchIC2(List<String> tokpat, List<String> tokfull, Matches m, boolean joinBrackets) {
  tokpat = codeTokens(joinBrackets ? joinBrackets(tokpat) : tokpat);
  for (int i = 0; i < l(tokpat); i++)
    if (eq(tokpat.get(i), "*"))
      tokpat.add(i++, "!*"); // insert single-token wildcard in front to avoid empty matches
  if (joinBrackets) tokfull = joinBrackets(tokfull);
  List<String> tok = codeTokens(tokfull);
  BitSet bla = new BitSet();
  BitSet bla2 = new BitSet();
  if (!flexMatchIC2_impl(tokpat, 0, tok, 0, bla, bla2)) return false;
  if (m != null) {
    List<String> l = new ArrayList();
    for (int i = 1; i < l(tokfull); i += 2) {
      if (bla.get(i/2)) {
        int j = i;
        while (j < l(tokfull) && bla.get(j/2)) j += 2;
        l.add(join(subList(tokfull, i, j-1)));
        i = j-2;
      } else if (bla2.get(i/2))
        l.add(tokfull.get(i));
    }
    m.m = toStringArray(l);
  }
  return true;
}

static boolean flexMatchIC2_impl(List<String> pat, int ipat, List<String> tok, int itok, BitSet bla, BitSet bla2) {
  if (flexMatchIC2_debug)
    print("flexMatchIC2 pat=" + structure(subList(pat, ipat)) + " tok=" + structure(subList(tok, itok)) + " " + structure(bla));
  if (ipat >= l(pat))
    return itok >= l(tok);
  String t = pat.get(ipat);
  
  if (eq(t, "*")) { // the flex wildcard (0 or more tokens)
    if (flexMatchIC2_debug) print("Trying zero tokens");
    if (flexMatchIC2_impl(pat, ipat+1, tok, itok, bla, bla2)) {
      if (flexMatchIC2_debug) print("Success!");
      return true;
    }
    
    bla.set(itok);
    if (itok < l(tok)) {
      if (flexMatchIC2_debug) print("Trying one or more tokens");
      if (flexMatchIC2_impl(pat, ipat, tok, itok+1, bla, bla2)) {
        if (flexMatchIC2_debug) print("Success!");
        return true; // success, leave mark
      }
    }
    
    if (flexMatchIC2_debug) print("Failed * matching");
    bla.clear(itok); // fail, undo marking
    return false;
  }
  if (itok >= l(tok)) {
    if (flexMatchIC2_debug)
      print("too much pattern");
    return false;
  }
  if (eq(t, "!*")) { // the single-token wildcard
    bla.set(itok);
    if (flexMatchIC2_impl(pat, ipat+1, tok, itok+1, bla, bla2))
      return true; // success, leave mark
    bla.clear(itok); // fail, undo marking
    return false;
  }
  String realt = tok.get(itok);
  if (t.startsWith("(") && t.endsWith(")")) {
    // quick pre-check
    if (flexMatchIC2_debug)
      print("flexMatchIC2 precheck " + t + " " + realt);
    if (!containsIgnoreCase(t, realt)) return false;
    // real check
    List<String> list = splitAt(dropFirstAndLast(t), "|");
    if (flexMatchIC2_debug)
      print("flexMatchIC2 real check " + struct(list));
    if (!containsIgnoreCase(list, realt)) return false;
    bla2.set(itok);
  } else if (neqic(realt, t)) {
    if (flexMatchIC2_debug)
      print("mismatch");
    return false;
  }
  
  // it is a token match. consume and proceed
  if (flexMatchIC2_impl(pat, ipat+1, tok, itok+1, bla, bla2))
    return true;
  else {
    bla2.clear(itok);
    return false;
  }
}
static Class getMainClass() {
  return main.class;
}

static Class getMainClass(Object o) { try {
  return (o instanceof Class ? (Class) o : o.getClass()).getClassLoader().loadClass("main");
} catch (Exception __e) { throw rethrow(__e); } }
static String intern(String s) {
  return fastIntern(s);
}
static WebRelation web_getRelation(WebNode a, WebNode b) {
  return a.web.getRelation(a, b);
}
static String[] toStringArray(Collection<String> c) {
  String[] a = new String[l(c)];
  Iterator<String> it = c.iterator();
  for (int i = 0; i < l(a); i++)
    a[i] = it.next();
  return a;
}

static String[] toStringArray(Object o) {
  if (o instanceof String[])
    return (String[]) o;
  else if (o instanceof Collection)
    return toStringArray((Collection<String>) o);
  else
    throw fail("Not a collection or array: " + getClassName(o));
}

static File getGlobalCache() {
  File file = new File(javaxCachesDir(), "Binary Snippets");
  file.mkdirs();
  return file;
}

static List<String> splitByJavaToken(String s, String splitToken) {
  List<String> tok = javaTok(s);
  List<String> l = new ArrayList();
  int i = 1;
  while (i < l(tok)) {
    int j = smartIndexOf(tok, splitToken, i);
    l.add(join(subList(tok, i, j-1)));
    i = j+2;
  }
  return l;
}
static List<WebNode> webs_tripleSearch_dollarX(String a, String b, String c, Collection<Web> webs) {
  List<WebNode> l = new ArrayList();
  boolean isA = eq(a, "$X");
  for (Web web : webs) {
    Pair<WebNode, WebNode> p = web_matchTriple_dollarVars(a, b, c, web);
    if (p != null)
      l.add(isA ? p.a : p.b);
  }
  return l;
}
static void lockOrFail(Lock lock, long timeout) { try {
  ping();
  if (!lock.tryLock(timeout, TimeUnit.MILLISECONDS)) {
    String s = "Couldn't acquire lock after " + timeout + " ms.";
    if (lock instanceof ReentrantLock) {
      ReentrantLock l = (ReentrantLock) ( lock);
      s += " Hold count: " + l.getHoldCount() + ", owner: " + call(l, "getOwner");
    }
    throw fail(s);
  }
  ping();
} catch (Exception __e) { throw rethrow(__e); } }
static <A> LinkedHashSet<A> litorderedset(A... items) {
  LinkedHashSet<A> set = new LinkedHashSet();
  for (A a : items) set.add(a);
  return set;
}
static Set<String> ai_is_myYourEtc_maybe = litciset("this", "that", "all", "some");

static boolean ai_is_myYourEtc_maybe(String s) {
  return containsNotNull(ai_is_myYourEtc_maybe, s);
}
static String quoteReplacement(String s) {
  return Matcher.quoteReplacement(s);
}
static String formatSnippetID(String id) {
  return "#" + parseSnippetID(id);
}

static String formatSnippetID(long id) {
  return "#" + id;
}
static long psI(String snippetID) {
  return parseSnippetID(snippetID);
}
static WebRelation web_findRelationWithLabel(Web web, String s) {
  for (WebRelation r : web_relationObjects(web))
    if (web_matchNodeName(r, s))
      return r;
  return null;
}
static <A> boolean setAdd(Collection<A> c, A a) {
  if (c == null || c.contains(a)) return false;
  c.add(a);
  return true;
}

// A concept should be an object, not just a string.

// Functions that should always be there for child processes:
static int concepts_internStringsLongerThan = 10;

static ThreadLocal<Boolean> concepts_unlisted = new ThreadLocal();

static interface Derefable {
  Concept get();
}

static interface IConceptIndex {
  void update(Concept c); // also for adding
  void remove(Concept c);
}

static interface IFieldIndex<A extends Concept, Val> {
  List<A> getAll(Val val);
}

static class Concepts {
  Map<Long, Concept> concepts = synchroTreeMap();
  HashMap<Class,Object> perClassData = new HashMap();
  String programID; // set to "-" for non-persistent (possibly not implemented)
  long idCounter;
  volatile long changes, changesWritten;
  volatile java.util.Timer autoSaver;
  volatile boolean savingConcepts, dontSave;
  boolean initialSave = true; // set to false to avoid initial useless saving
  int autoSaveInterval = -1000; // 1 second + wait logic
  boolean useGZIP = true, quietSave;
  ReentrantLock lock = new ReentrantLock(true);
  ReentrantLock saverLock = new ReentrantLock(true);
  long lastSaveTook, lastSaveWas;
  float maxAutoSavePercentage = 10;
  List<IConceptIndex> conceptIndices;
  Map<Class<? extends Concept>, Map<String, IFieldIndex>> fieldIndices;
  List saveActions = synchroList();
  
  Concepts() {}
  Concepts(String programID) {
  this.programID = programID;}
  
  synchronized long internalID() {
    do {
      ++idCounter;
    } while (hasConcept(idCounter));
    return idCounter;
  }
  
  void initProgramID() {
    if (programID == null)
      programID = getDBProgramID();
  }
  
  // Now tries to load from bot first, then go to disk.
  Concepts load() {
    return load(false);
  }
  
  Concepts safeLoad() {
    return load(true);
  }
  
  Concepts load(boolean allDynamic) {
    initProgramID();
    if (tryToGrab(allDynamic)) return this;
    return loadFromDisk(allDynamic);
  }
  
  Concepts loadFromDisk() { return loadFromDisk(false); }
  
  Concepts loadFromDisk(boolean allDynamic) {
    if (nempty(concepts)) clearConcepts();
    //DynamicObject_loading.set(true); // now done in unstructure()
    //try {
      long time = now();
      Map<Long, Concept> _concepts = concepts; // empty map
      readLocally2_allDynamic.set(allDynamic);
      readLocally2(this, programID, "concepts");
      Map<Long, Concept> __concepts = concepts;
      concepts = _concepts;
      concepts.putAll(__concepts);
      int l = readLocally_stringLength;
      int tokrefs = unstructure_tokrefs;
      assignConceptsToUs();
      done("Loaded " + n(l(concepts), "concepts"), time);
      if (fileSize(getProgramFile(programID, "idCounter.structure")) != 0)
        readLocally2(this, programID, "idCounter");
      else
        calcIdCounter();
    /*} finally {
      DynamicObject_loading.set(null);
    }*/
    if (initialSave) allChanged();
    return this;
  }
  
  Concepts loadConcepts() { return load(); }
  
  boolean tryToGrab(boolean allDynamic) {
    if (sameSnippetID(programID, getDBProgramID())) return false;
    RemoteDB db = connectToDBOpt(programID);
    try {
      if (db != null) {
        loadGrab(db.fullgrab(), allDynamic);
        return true;
      }
    } finally {
      if (db != null) db.close();
    }
    return false;
  }
  
  Concepts load(String grab) {
    return loadGrab(grab, false);
  }
  
  Concepts safeLoad(String grab) {
    return loadGrab(grab, true);
  }
  
  Concepts loadGrab(String grab, boolean allDynamic) {
    clearConcepts();
    DynamicObject_loading.set(true);
    try {
      Map<Long, Concept> map = (Map)
        (allDynamic ? safeUnstructure(grab) : unstructure(grab));
      concepts.putAll(map);
      assignConceptsToUs();
      for (long l : map.keySet())
        idCounter = max(idCounter, l);
    } finally {
      DynamicObject_loading.set(null);
    }
    allChanged();
    return this;
  }
  
  void assignConceptsToUs() {
    for (Concept c : values(concepts)) {
      c._concepts = this;
      callOpt_noArgs(c, "_doneLoading2");
    }
  }

  String progID() {
    return programID == null ? getDBProgramID() : programID;
  }
  
  Concept getConcept(String id) {
    return empty(id) ? null : getConcept(parseLong(id));
  }
  
  Concept getConcept(long id) {
    return (Concept) concepts.get((long) id);
  }
  
  Concept getConcept(RC ref) {
    return ref == null ? null : getConcept(ref.longID());
  }
  
  boolean hasConcept(long id) {
    return concepts.containsKey((long) id);
  }
  
  void deleteConcept(long id) {
    Concept c = getConcept(id);
    if (c == null)
      print("Concept " + id + " not found");
    else
      c.delete();
  }
  
  void calcIdCounter() {
    long id_ = 0;
    for (long id : keys(concepts))
      id_ = max(id_, id);
    idCounter = id_+1;
    saveLocally2(this, programID, "idCounter");
  }
  
  void saveConceptsIfDirty() { saveConcepts(); }
  void save() { saveConcepts(); }

  void saveConcepts() {
    if (dontSave) return;
    initProgramID();
    saverLock.lock();
    savingConcepts = true;
    long start = now(), time;
    try {
      String s = null;
      //synchronized(main.class) {
      long _changes = changes;
      if (_changes == changesWritten) return;
      
      File f = getProgramFile(programID, useGZIP ? "concepts.structure.gz" : "concepts.structure");
      
      lock.lock();
      long fullTime = now();
      try {
        saveLocally2(this, programID, "idCounter");
        
        if (useGZIP) {
          saveGZStructureToFile(f, cloneMap(concepts));
          getProgramFile(programID, "concepts.structure").delete();
        } else
          s = structure(cloneMap(concepts));
      } finally {
        lock.unlock();
      }
      
      while (nempty(saveActions))
        pcallF(popFirst(saveActions));

      changesWritten = _changes; // only update when structure didn't fail
      
      if (!useGZIP) {
        time = now()-start;
        if (!quietSave)
          print("Saving " + toM(l(s)) + "M chars (" /*+ changesWritten + ", "*/ + time + " ms)");
        start = now();
        saveTextFile(f, javaTokWordWrap(s));
        getProgramFile(programID, "concepts.structure.gz").delete();
      }
      
      copyFile(f, getProgramFile(programID, "backups/concepts.structure" + (useGZIP ? ".gz" : "") + ".backup" + ymd() + "-" + formatInt(hours(), 2)));
      time = now()-start;
      if (!quietSave)
        print(programID + ": Saved " + toK(f.length()) + " K, " + n(concepts, "concepts") + " (" + time + " ms)");
      lastSaveWas = fullTime;
      lastSaveTook = now()-fullTime;
    } finally {
      savingConcepts = false;
      saverLock.unlock();
    }
  }
  
  void _autoSaveConcepts() {
    if (autoSaveInterval < 0 && maxAutoSavePercentage != 0) {
      long pivotTime = Math.round(lastSaveWas+lastSaveTook*100.0/maxAutoSavePercentage);
      if (now() < pivotTime) {
        //print("Skipping auto-save (last save took " + lastSaveTook + ")");
        return;
      }
    }
    try {
      saveConcepts();
    } catch (Throwable e) {
      print("Concept save failed, will try again: " + e);
    }
  }
  
  void clearConcepts() {
    concepts.clear();
    allChanged();
  }
  
  synchronized void allChanged() {
    ++changes;
  }
  
  // auto-save every second if dirty
  synchronized void autoSaveConcepts() {
    if (autoSaver == null) {
      if (isTransient()) throw fail("Can't persist transient database");
      autoSaver = doEvery_daemon(abs(autoSaveInterval), new Runnable() { public void run() { try {  _autoSaveConcepts() ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "_autoSaveConcepts()"; }});
      // print("Installed auto-saver (" + autoSaveInterval + " ms, " + progID() + ")");
    }
  }
  
  void cleanMeUp() {
    if (autoSaver != null) {
      autoSaver.cancel();
      autoSaver = null;
    }
    while (savingConcepts) sleepInCleanUp(10);
    saveConceptsIfDirty();
  }
  
  Map<Long, String> getIDsAndNames() {
    Map<Long,String> map = new HashMap();
    Map<Long, Concept> cloned = cloneMap(concepts);
    for (long id : keys(cloned)) 
      map.put(id, cloned.get(id).className);
    return map;
  }
  
  void deleteConcepts(List l) {
    for (Object o : l)
      if (o instanceof Long)
        concepts.remove((Long) o);
      else if (o instanceof Concept)
        ((Concept) o).delete();
      else
        warn("Can't delete " + getClassName(o));
  }
  
  <A extends Concept> A conceptOfType(Class<A> type) {
    return firstOfType(allConcepts(), type);
  }
  
  <A extends Concept> List<A> conceptsOfType(Class<A> type) {
    return filterByType(allConcepts(), type);
  }
  
  <A extends Concept> List<A> listConcepts(Class<A> type) {
    return conceptsOfType(type);
  }
  
  <A extends Concept> List<A> list(Class<A> type) {
    return conceptsOfType(type);
  }
  
  List<Concept> list(String type) {
    return conceptsOfType(type);
  }
  
  List<Concept> conceptsOfType(String type) {
    return filterByDynamicType(allConcepts(), "main$" + type);
  }
  
  boolean hasConceptOfType(Class<? extends Concept> type) {
    return hasType(allConcepts(), type);
  }
  
  void persistConcepts() {
    loadConcepts();
    autoSaveConcepts();
  }
  
  // We love synonyms
  void conceptPersistence() {
    persistConcepts();
  }
  
  Concepts persist() { persistConcepts(); return this; }
  void persist(int interval) { autoSaveInterval = interval; persist(); }
    
  // Runs r if there is no concept of that type
  <A extends Concept> A ensureHas(Class<A> c, Runnable r) {
    A a = conceptOfType(c);
    if (a == null) {
      r.run();
      a = conceptOfType(c);
      if (a == null)
        throw fail("Concept not made by " + r + ": " + shortClassName(c));
    }
    return a;
  }
  
  // Ensures that every concept of type c1 is ref'd by a concept of
  // type c2.
  // Type of func: voidfunc(concept)
  void ensureHas(Class<? extends Concept> c1, Class<? extends Concept> c2, Object func) {
    for (Concept a : conceptsOfType(c1)) {
      Concept b = findBackRef(a, c2);
      if (b == null) {
        callF(func, a);
        b = findBackRef(a, c2);
        if (b == null)
          throw fail("Concept not made by " + func + ": " + shortClassName(c2));
      }
    }
  }
  
  // Type of func: voidfunc(concept)
  void forEvery(Class<? extends Concept> type, Object func) {
    for (Concept c : conceptsOfType(type))
      callF(func, c);
  }
  
  int deleteAll(Class<? extends Concept> type) {
    List<Concept> l = (List) conceptsOfType(type);
    for (Concept c : l) c.delete();
    return l(l);
  }
  
  Collection<Concept> allConcepts() {
    synchronized(concepts) {
      return new ArrayList(values(concepts));
    }
  }
  
  <A extends Concept> int countConcepts(Class<A> c, Object... params) {
    int n = 0;
    for (A x : list(c)) if (checkConceptFields(x, params)) ++n;
    return n;
  }

  int countConcepts(String c, Object... params) {
    int n = 0;
    for (Concept x : list(c)) if (checkConceptFields(x, params)) ++n;
    return n;
  }

  int countConcepts() {
    return l(concepts);
  }
  
  synchronized void addConceptIndex(IConceptIndex index) {
    if (conceptIndices == null)
      conceptIndices = new ArrayList();
    conceptIndices.add(index);
  }
  
  synchronized void addFieldIndex(Class<? extends Concept> c, String field, IFieldIndex index) {
    if (fieldIndices == null)
      fieldIndices = new HashMap();
    Map<String, IFieldIndex> map = fieldIndices.get(c);
    if (map == null)
      fieldIndices.put(c, map = new HashMap());
    map.put(field, index);
  }
  
  synchronized IFieldIndex getFieldIndex(Class<? extends Concept> c, String field) {
    if (fieldIndices == null) return null;
    Map<String, IFieldIndex> map = fieldIndices.get(c);
    return map == null ? null : map.get(field);
  }
  
  // inter-process methods
  
  RC xnew(String name, Object... values) {
    return new RC(cnew(name, values));
  }
  
  void xset(long id, String field, Object value) {
    xset(new RC(id), field, value);
  }
  
  void xset(RC c, String field, Object value) {
    if (value instanceof RC)
      value = getConcept((RC) value);
    cset(getConcept(c), field, value);
  }
  
  Object xget(long id, String field) {
    return xget(new RC(id), field);
  }
  
  Object xget(RC c, String field) {
    return xgetPost(cget(getConcept(c), field));
  }
  
  Object xgetPost(Object o) {
    o = deref(o);
    if (o instanceof Concept)
      return new RC((Concept) o);
    return o;
  }
  
  void xdelete(long id) {
    xdelete(new RC(id));
  }
  
  void xdelete(RC c) {
    getConcept(c).delete();
  }
  
  void xdelete(List<RC> l) {
    for (RC c : l)
      xdelete(c);
  }
  
  List<RC> xlist() {
    return map("toPassRef", allConcepts());
  }
  
  List<RC> xlist(String className) {
    return map("toPassRef", conceptsOfType(className));
  }
  
  boolean isTransient() { return eq(programID, "-"); }
  
  String xfullgrab() {
    lock.lock();
    try {
      if (changes == changesWritten && !isTransient())
        return loadConceptsStructure(programID);
      return structure(cloneMap(concepts));
    } finally {
      lock.unlock();
    }
  }
  
  void xshutdown() {
    // Killing whole VM if someone wants this DB to shut down
    cleanKillVM();
  }
  
  long xchangeCount() { return changes; }
  int xcount() { return countConcepts(); }
  
  void register(Concept c) {
    if (c._concepts == this) return;
    if (c._concepts != null) throw fail("Can't re-register");
    c._concepts = this;
    c.id = internalID();
    c.created = now();
    concepts.put((long) c.id, c);
    c.change();
  }
  
} // class Concepts

static volatile Concepts mainConcepts = new Concepts(); // Where we create new concepts

static class Concept extends DynamicObject {
  transient Concepts _concepts; // Where we belong
  long id;
  //O madeBy;
  //double energy;
  //bool defunct;
  long created;
  
  // used only internally (cnew)
  Concept(String className) {
    super(className);
    _created();
  }
  
  Concept() {
    if (!_loading()) {
      //className = shortClassName(this); // XXX - necessary?
      //print("New concept of type " + className);
      _created();
    }
  }
  
  Concept(boolean unlisted) {
    if (!unlisted) _created();
  }
  
  
  List<Ref> refs;
  List<Ref> backRefs;
  
  static boolean loading() { return _loading(); }
  static boolean _loading() { return isTrue(DynamicObject_loading.get()); }

  void _created() {
    if (!isTrue(concepts_unlisted.get()))
      mainConcepts.register(this);
  }
  
  /*void put(S field, O value) {
    fieldValues.put(field, value);
    change();
  }
  
  O get(S field) {
    ret fieldValues.get(field);
  }*/
  
  class Ref<A extends Concept> {
    A value;
    
    Ref() {
      if (!isTrue(DynamicObject_loading.get())) refs = addDyn(refs, this);
    }
    
    Ref(A value) {
  this.value = value;
      refs = addDyn(refs, this);
      index();
    }
    
    // get owning concept (source)
    Concept concept() {
      return Concept.this;
    }
    
    // get target
    A get() { return value; }
    boolean has() { return value != null; }
    
    void set(A a) {
      if (a == value) return;
      unindex();
      value = a;
      index();
    }
    
    void set(Ref<A> ref) { set(ref.get()); }
    void clear() { set((A) null); }
    
    void index() { 
      if (value != null)
        value.backRefs = addDyn(value.backRefs, this);
      change();
    }
    
    void unindex() {
      if (value != null)
        value.backRefs = removeDyn(value.backRefs, this);
    }
    
    void change() {
      Concept.this.change();
    }
  }
  
  class RefL<A extends Concept> extends AbstractList<A> {
    List < Ref < A > > l = new ArrayList();
    
    public A set(int i, A o) {
      A prev = l.get(i).get();
      l.get(i).set(o);
      return prev;
    }
    
    public void add(int i, A o) {
      l.add(i, new Ref(o));
    }
    
    public A get(int i) {
      return l.get(i).get();
    }
    
    public A remove(int i) {
      return l.remove(i).get();
    }
    
    public int size() {
      return l.size();
    }
    
    public boolean contains(Object o) {
      if (o instanceof Concept)
        for (Ref<A> r : l) if (eq(r.get(), o)) return true;
      return super.contains(o);
    }
  }
  
  void delete() {
    //name = "[defunct " + name + "]";
    //defunct = true;
    //energy = 0;
    for (Ref r : unnull(refs))
      r.unindex();
    refs = null;
    for (Ref r : cloneList(backRefs))
      r.set((Concept) null);
    backRefs = null;
    
    if (_concepts != null) {
      _concepts.concepts.remove((long) id);
      _concepts.allChanged();
      if (_concepts.conceptIndices != null)
        for (IConceptIndex index : _concepts.conceptIndices)
          index.remove(this);
      _concepts = null;
    }
    id = 0;
  }
  
  BaseXRef export() {
    return new BaseXRef(_concepts.progID(), id);
  }
  
  // notice system of a change in this object
  void change() {
    if (_concepts != null) {
      _concepts.allChanged();
      if (_concepts.conceptIndices != null)
        for (IConceptIndex index : _concepts.conceptIndices)
          index.update(this);
    }
  }
  
  void _change() { change(); }
  
  String _programID() {
    return _concepts == null ? getDBProgramID() : _concepts.progID();
  }
} // class Concept

// remote reference (for inter-process communication or
// external databases). Formerly "PassRef".
// prepared for string ids if we do them later
static class RC {
  transient Object owner;
  String id;
  
  RC() {} // make serialisation happy
  RC(long id) { this.id = str(id); }
  RC(Object owner, long id) { this.id = str(id); this.owner = owner; }
  RC(Concept c) { this(c.id); }
  long longID() { return parseLong(id); }
  
  public String toString() {
    return id;
  }

  transient RemoteDB db;
  
  String getString(String field) { return db.xS(this, field); }
  Object get(String field) { return db.xget(this, field); }
  void set(String field, Object value) { db.xset(this, field, value); }

}

// Reference to a concept in another program
static class BaseXRef {
  String programID;
  long id;
    
  BaseXRef() {}
  BaseXRef(String programID, long id) {
  this.id = id;
  this.programID = programID;}
  
  public boolean equals(Object o) {
    if (!(o instanceof BaseXRef)) return false;
    BaseXRef r = (BaseXRef) ( o);
    return eq(programID, r.programID) && eq(id, r.id);
  }
  
  public int hashCode() {
    return programID.hashCode() + (int) id;
  }
}

// BaseXRef as a concept
static class XRef extends Concept {
  BaseXRef ref;
  
  XRef() {}
  XRef(BaseXRef ref) {
  this.ref = ref; _doneLoading2(); }
  
  // after we have been added to concepts
  void _doneLoading2() {
    getIndex().put(ref, this);
  }
    
  HashMap<BaseXRef, XRef> getIndex() {
    return getXRefIndex(_concepts);
  }
}

static synchronized HashMap<BaseXRef, XRef> getXRefIndex(Concepts concepts) {
  HashMap cache = (HashMap) concepts.perClassData.get(XRef.class);
  if (cache == null)
    concepts.perClassData.put(XRef.class, cache = new HashMap());
  return cache;
}

// uses mainConcepts
static XRef lookupOrCreateXRef(BaseXRef ref) {
  XRef xref = getXRefIndex(mainConcepts).get(ref);
  if (xref == null)
    xref = new XRef(ref);
  return xref;
}

// define standard concept functions to use main concepts

static <A extends Concept> List<A> list(Class<A> type) {
  return mainConcepts.list(type);
}

static <A extends Concept> List<A> list(Concepts concepts, Class<A> type) {
  return concepts.list(type);
}

static List<Concept> list(String type) {
  return mainConcepts.list(type);
}

static List<Concept> list(Concepts concepts, String type) {
  return concepts.list(type);
}

static int csetAll(Concept c, Object... values) {
  return cset(c, values);
}

static void cleanMeUp_concepts() {
  mainConcepts.cleanMeUp();
}

static void loadAndAutoSaveConcepts() {
  mainConcepts.persist();
}

static void loadAndAutoSaveConcepts(int interval) {
  mainConcepts.persist(interval);
}

static void loadConceptsFrom(String progID) {
  mainConcepts.programID = progID;
  mainConcepts.load();
}

static List<Concept> conceptsOfType(String type) {
  return mainConcepts.conceptsOfType(type);
}

static long changeCount() {
  return mainConcepts.changes;
}

static List<String> exposedDBMethods = ll("xlist", "xnew", "xset", "xdelete", "xget", "xclass", "xfullgrab", "xshutdown", "xchangeCount", "xcount");

static RC toPassRef(Concept c) {
  return new RC(c);
}

// so we can instantiate the program to run as a bare DB bot
static Lock dbLock() {
  return mainConcepts.lock;
}
static ReentrantLock fairLock() {
  return new ReentrantLock(true);
}
static boolean isAdverb(String s) {
  return containsNotNull(knownAdverbs(), s);
}
static boolean eqicOneOf(String s, String... l) {
  for (String x : l) if (eqic(s, x)) return true; return false;
}
static WebNode web_firstNode(Web web) {
  return web == null ? null : first(web.nodes);
}
static <A> List<A> replaceSublist(List<A> l, List<A> x, List<A> y) {
  if (x == null) return l;
  
  int i = 0;
  while (true) {
    i = indexOfSubList(l, x, i);
    if (i < 0) break;
    
    // It's inefficient :D
    for (int j = 0; j < l(x); j++) l.remove(i);
    l.addAll(i, y);
    i += l(y);
  }
  return l;
}

static <A> List<A> replaceSublist(List<A> l, int fromIndex, int toIndex, List<A> y) {
  // inefficient
  while (toIndex > fromIndex) l.remove(--toIndex);
  l.addAll(fromIndex, y);
  return l;
}
  static List<String> codeTokensOnly(List<String> tok) {
    List<String> l = new ArrayList();
    for (int i = 1; i < tok.size(); i += 2)
      l.add(tok.get(i));
    return l;
  }
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 Cache<Set<String>> nounPhraseContinuers_cache = new Cache("nounPhraseContinuers_load");
static Set<String> nounPhraseContinuers() { return nounPhraseContinuers_cache.get(); }

static Set<String> nounPhraseContinuers_load() {
  return new TreeSet(tlft(loadSnippet("#1011080")));
}

static Map<String, String> hippoSingulars() {
  return pairsToMap((List<Pair<String, String>>) scanStructureLog("#1011041", "singulars"));
}
static boolean web_isVerified(WebNode n) {
  return n != null && n.web.verified();
}
static List<String> codeTokens(List<String> tok) {
  return codeTokensOnly(tok);
}
static String hopeningTag(String tag, Object... params) {
  StringBuilder buf = new StringBuilder();
  buf.append("<" + tag);
  for (int i = 0; i < l(params); i += 2) {
    String name = (String) get(params, i);
    Object val = get(params, i+1);
    if (nempty(name) && val != null) {
      String s = str(val);
      if (!empty(s))
        buf.append(" " + name + "=" + htmlQuote(s));
    }
  }
  buf.append(">");
  return str(buf);
}
static File getProgramDir() {
  return programDir();
}

static File getProgramDir(String snippetID) {
  return programDir(snippetID);
}
static Object pcallFunction(Object f, Object... args) {
  try { return callFunction(f, args); } catch (Throwable __e) { printStackTrace2(__e); }
  return null;
}
static boolean isKnownPhrase(String s) {
  return containsNotNull(knownPhrases(), s);
}
static int findCodeTokens(List<String> tok, String... tokens) {
  return findCodeTokens(tok, 1, false, tokens);
}

static int findCodeTokens(List<String> tok, boolean ignoreCase, String... tokens) {
  return findCodeTokens(tok, 1, ignoreCase, tokens);
}

static int findCodeTokens(List<String> tok, int startIdx, boolean ignoreCase, String... tokens) {
  return findCodeTokens(tok, startIdx, ignoreCase, tokens, null);
}

static List<String> findCodeTokens_specials = litlist("*", "<quoted>", "<id>", "<int>", "\\*");
static boolean findCodeTokens_debug;
static int findCodeTokens_indexed, findCodeTokens_unindexed;
static int findCodeTokens_bails, findCodeTokens_nonbails;

static int findCodeTokens(List<String> tok, int startIdx, boolean ignoreCase, String[] tokens, Object condition) {
  if (findCodeTokens_debug) {
    if (eq(getClassName(tok), "main$IndexedList2"))
      findCodeTokens_indexed++;
    else
      findCodeTokens_unindexed++;
  }
  // bail out early if first token not found (works great with IndexedList)
  if (!findCodeTokens_specials.contains(tokens[0])
    && !tok.contains(tokens[0] /*, startIdx << no signature in List for this, unfortunately */)) {
      ++findCodeTokens_bails;
      return -1;
    }
  ++findCodeTokens_nonbails;
  
  outer: for (int i = startIdx | 1; i+tokens.length*2-2 < tok.size(); i += 2) {
    for (int j = 0; j < tokens.length; j++) {
      String p = tokens[j], t = tok.get(i+j*2);
      boolean match;
      if (eq(p, "*")) match = true;
      else if (eq(p, "<quoted>")) match = isQuoted(t);
      else if (eq(p, "<id>")) match = isIdentifier(t);
      else if (eq(p, "<int>")) match = isInteger(t);
      else if (eq(p, "\\*")) match = eq("*", t);
      else match = ignoreCase ? eqic(p, t) : eq(p, t);
      
      if (!match)
        continue outer;
    }
    
    if (condition == null || checkCondition(condition, tok, i-1)) // pass N index
      return i;
  }
  return -1;
}
static String patternQuote(String s) {
  return s.length() == 0 ? "" : Pattern.quote(s);
}
static Collection<WebNode> ai_withoutInvalidNodes(Collection<WebNode> l) {
  if (l == null) return null;
  for (WebNode n : l)
    if (ai_isInvalidWeb(n.web)) {
      Iterator<WebNode> it = iterator(l);
      WebNode node;
      List<WebNode> x = new ArrayList();
      while ((node = it.next()) != n)
        x.add(node);
      while (it.hasNext()) {
        node = it.next();
        if (!ai_isInvalidWeb(node.web))
          x.add(node);
      }
      return x;
    }
  return l;
}

static List<WebNode> ai_withoutInvalidNodes(List<WebNode> l) {
  return (List) ai_withoutInvalidNodes((Collection) l);
}
static boolean ai_isInvalidWeb(Web web) {
  return ai_invalidatedWebs().contains(web.globalID);
}
static Object nuObject(String className, Object... args) { try {
  return nuObject(classForName(className), args);
} catch (Exception __e) { throw rethrow(__e); } }

// too ambiguous - maybe need to fix some callers
/*static O nuObject(O realm, S className, O... args) {
  ret nuObject(_getClass(realm, className), args);
}*/

static <A> A nuObject(Class<A> c, Object... args) { try {
  Constructor m = nuObject_findConstructor(c, args);
  m.setAccessible(true);
  return (A) m.newInstance(args);
} catch (Exception __e) { throw rethrow(__e); } }

static Constructor nuObject_findConstructor(Class c, Object... args) {
  for (Constructor m : c.getDeclaredConstructors()) {
    if (!nuObject_checkArgs(m.getParameterTypes(), args, false))
      continue;
    return m;
  }
  throw fail("Constructor " + c.getName() + getClasses(args) + " not found"
    + (args.length == 0 && (c.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0 ? " - hint: it's a non-static class!" : ""));
}

 static boolean nuObject_checkArgs(Class[] types, Object[] args, boolean debug) {
    if (types.length != args.length) {
      if (debug)
        System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
      return false;
    }
    for (int i = 0; i < types.length; i++)
      if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
        if (debug)
          System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
        return false;
      }
    return true;
  }
static List<Web> indexedVerifiedWebs(String s) {
  return web_verifiedWebsOnly(indexedWebs(s));
}
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 Cache<Set<String>> knownVerbs_cache = new Cache("knownVerbs_load");
static Set<String> knownVerbs() { return knownVerbs_cache.get(); }

static Set<String> knownVerbs_load() {
  return caseInsensitiveSet(tlft(loadSnippet("#1009502")));
}

static String formatSnippetIDOpt(String s) {
  return isSnippetID(s) ? formatSnippetID(s) : s;
}
static void setAll(Object o, Map<String, Object> fields) {
  if (fields == null) return;
  for (String field : keys(fields))
    set(o, field, fields.get(field));
}

static void setAll(Object o, Object... values) {
  //values = expandParams(c.getClass(), values);
  failIfOddCount(values);
  for (int i = 0; i+1 < l(values); i += 2) {
    String field = (String) values[i];
    Object value = values[i+1];
    set(o, field, value);
  }
}
static Class<?> hotwire(String src) {
  assertFalse(_inCore());
  Class j = getJavaX();
  if (isAndroid()) {
    synchronized(j) { // hopefully this goes well...
      List<File> libraries = new ArrayList<File>();
      File srcDir = (File) call(j, "transpileMain", src, libraries);
      if (srcDir == null)
        throw fail("transpileMain returned null (src=" + quote(src) + ")");
    
      Object androidContext = get(j, "androidContext");
      return (Class) call(j, "loadx2android", srcDir, src);
    }
  } else {
    // ret hotwire_overInternalBot(src);
    Class c = (Class) ( call(j, "hotwire", src));
    hotwire_copyOver(c);
    return c;
  }
}
  public static String loadTextFile(String fileName) {
    return loadTextFile(fileName, null);
  }
  
  public static String loadTextFile(File fileName, String defaultContents) {
    ping();
    try {
      if (fileName == null || !fileName.exists())
        return defaultContents;
  
      FileInputStream fileInputStream = new FileInputStream(fileName);
      InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
      return loadTextFile(inputStreamReader);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  
  public static String loadTextFile(File fileName) {
    return loadTextFile(fileName, null);
  }

  public static String loadTextFile(String fileName, String defaultContents) {
    return fileName == null ? defaultContents : loadTextFile(newFile(fileName), defaultContents);
  }
  
  public static String loadTextFile(Reader reader) throws IOException {
    StringBuilder builder = new StringBuilder();
    try {
      char[] buffer = new char[1024];
      int n;
      while (-1 != (n = reader.read(buffer)))
        builder.append(buffer, 0, n);
        
    } finally {
      reader.close();
    }
    return builder.toString();
  }
static List<String> ai_texts_bidirectionalOperands(String s, String rel) {
  return concatLists(ai_texts(s, rel, "$X"), ai_texts("$X", rel, s));
}
static String firstJavaToken(String s) {
  if (s == null) return null;
  int l = s.length();

  int j = 0;
  char c, d;
    
  // scan for whitespace
  while (j < l) {
    c = s.charAt(j);
    d = j+1 >= l ? '\0' : s.charAt(j+1);
    if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
      ++j;
    else if (c == '/' && d == '*') {
      do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
      j = Math.min(j+2, l);
    } else if (c == '/' && d == '/') {
      do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
    } else
      break;
  }
    
  if (j >= l) return null;
  int i = j;
  c = s.charAt(i);
  d = i+1 >= l ? '\0' : s.charAt(i+1);

  // scan for non-whitespace
  if (c == '\'' || c == '"') {
    char opener = c;
    ++j;
    while (j < l) {
      if (s.charAt(j) == opener || s.charAt(j) == '\n') { // end at \n to not propagate unclosed string literal errors
        ++j;
        break;
      } else if (s.charAt(j) == '\\' && j+1 < l)
        j += 2;
      else
        ++j;
    }
  } else if (Character.isJavaIdentifierStart(c))
    do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't"
  else if (Character.isDigit(c)) {
    do ++j; while (j < l && Character.isDigit(s.charAt(j)));
    if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
  } else if (c == '[' && d == '[') {
    do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
    j = Math.min(j+2, l);
  } else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') {
    do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
    j = Math.min(j+3, l);
  } else
    ++j;
    
  return quickSubstring(s, i, j);
}


static List<CriticalAction> beginCriticalAction_inFlight = synchroList();

static class CriticalAction {
  String description;
  
  CriticalAction() {}
  CriticalAction(String description) {
  this.description = description;}
  
  void done() {
    beginCriticalAction_inFlight.remove(this);
  }
}

static CriticalAction beginCriticalAction(String description) {
  ping();
  CriticalAction c = new CriticalAction(description);
  beginCriticalAction_inFlight.add(c);
  return c;
}

static void cleanMeUp_beginCriticalAction() {
  int n = 0;
  while (nempty(beginCriticalAction_inFlight)) {
    int m = l(beginCriticalAction_inFlight);
    if (m != n) {
      n = m;
      try {
        print("Waiting for " + n(n, "critical actions") + ": " + join(", ", collect(beginCriticalAction_inFlight, "description")));
      } catch (Throwable __e) { printStackTrace2(__e); }
    }
    sleepInCleanUp(10);
  }
}
static <A> List<A> concatLists_conservative(List<A> a, List<A> b) {
  if (empty(a)) return b;
  if (empty(b)) return a;
  return concatLists(a, b);
}
static String quoteCharacter(char c) {
  if (c == '\'') return "'\\''";
  if (c == '\\') return "'\\\\'";
  if (c == '\r') return "'\\r'";
  if (c == '\n') return "'\\n'";
  if (c == '\t') return "'\\t'";
  return "'" + c + "'";
}

static 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 List<String> getPlural_specials = ll("sheep", "fish");

static String getPlural(String s) {
  if (containsIgnoreCase(getPlural_specials, s)) return s;
  if (ewic(s, "y")) return dropSuffixIgnoreCase("y", s) + "ies";
  if (ewic(s, "ss")) return s + "es";
  if (ewic(s, "s")) return s;
  return s + "s";
}
static FileStatus conceptsFileStatus(String progID) {
  return fileStatus(conceptsFile(progID));
}
static int web_numberOfLabels(WebNode n) {
  return n == null ? 0 : l(n.labels);
}
static String quickSubstring(String s, int i, int j) {
  if (i == j) return "";
  return s.substring(i, j);
}
// TODO: returns empty first, but not empty last
static List<String> splitAt(String s, String splitter) {
  List<String> 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 boolean isSingularNoun(String s) {
  return contains(knownNouns(), lower(s));
}
static void setHeaders(URLConnection con) throws IOException {
  String computerID = getComputerID_quick();
  if (computerID != null) try {
    con.setRequestProperty("X-ComputerID", computerID);
    con.setRequestProperty("X-OS", System.getProperty("os.name") + " " + System.getProperty("os.version"));
  } catch (Throwable e) {
    //printShortException(e);
  }
}
static void set(Object o, String field, Object value) {
  if (o instanceof Class) set((Class) o, field, value);
  else try {
    Field f = set_findField(o.getClass(), field);
    smartSet(f, o, value);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

static void set(Class c, String field, Object value) {
  try {
    Field f = set_findStaticField(c, field);
    smartSet(f, null, value);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
  
static Field set_findStaticField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
}

static Field set_findField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field))
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
}
static String boolArrayToHex(boolean[] a) {
  return bytesToHex(boolArrayToBytes(a));
}
static <A extends Concept> A findConceptWhere(Class<A> c, Object... params) {
  return findConceptWhere(mainConcepts, c, params);
}

static <A extends Concept> A findConceptWhere(Concepts concepts, Class<A> c, Object... params) {
  params = expandParams(c, params);
  
  // indexed
  if (concepts.fieldIndices != null)
    for (int i = 0; i < l(params); i += 2) {
      IFieldIndex<A, Object> index = concepts.getFieldIndex(c, (String) params[i]);
      if (index != null) {
        for (A x : index.getAll(params[i+1]))
          if (checkConceptFields(x, params)) return x;
        return null;
      }
    }
    
  // table scan
  for (A x : concepts.list(c)) if (checkConceptFields(x, params)) return x;
  return null;
}

static Concept findConceptWhere(Concepts concepts, String c, Object... params) {
  for (Concept x : concepts.list(c)) if (checkConceptFields(x, params)) return x;
  return null;
}

static Iterator emptyIterator() {
  return Collections.emptyIterator();
}
static <A> A getAndClearThreadLocal(ThreadLocal<A> tl) {
  A a = tl.get();
  tl.set(null);
  return a;
}
static void ai_addWebToIndex(MultiMap<String, WebNode> index, Web web) {
  if (web == null) return;
  for (WebNode node : web_nodesAndRelations(web))
    for (String text : asSet(node.texts())) {
      String key = ai_shortenForIndex(text);
      boolean newText;
      synchronized(index) {
        if (key == text) // unchanged by shortening
          newText = true;
        else
          newText = !web_nodesContainText(index.get(key), text);
        index.put(key, node);
      }
      if (newText)
        ai_onNewIndexedText(text);
    }
}
static List<Web> web_verifiedWebsOnly(Collection<Web> l) {
  List<Web> x = new ArrayList();
  if (l != null) for (Web web : l)
    if (!web.unverified)
      x.add(web);
  return x;
}
static URLConnection openConnection(URL url) { try {
  ping();
  callOpt(javax(), "recordOpenURLConnection", str(url));
  return url.openConnection();
} catch (Exception __e) { throw rethrow(__e); } }
static <A> HashSet<A> lithashset(A... items) {
  HashSet<A> set = new HashSet();
  for (A a : items) set.add(a);
  return set;
}
static FileOutputStream newFileOutputStream(File path) throws IOException {
  return newFileOutputStream(path.getPath());
}

static FileOutputStream newFileOutputStream(String path) throws IOException {
  return newFileOutputStream(path, false);
}

static FileOutputStream newFileOutputStream(String path, boolean append) throws IOException {
  mkdirsForFile(path);
  FileOutputStream f = new // Line break for ancient translator
    FileOutputStream(path, append);
  callJavaX("registerIO", f, path, true);
  return f;
}
static List<WebNode> web_nonRelationNodes(Web web) {
  return listMinusSet(web.nodes, values(web.relations));
}
static Lisp lisp_intern(Lisp l) {
  if (l == null) return null;
  return lisp(intern(l.head), map_lisp_intern(l.args));
}
static boolean isPluralNoun(String s) {
  return contains(knownPluralNouns(), lower(s));
}
public static byte[] loadBinaryFile(String fileName) {
 try {
  if (!new File(fileName).exists())
    return null;

  FileInputStream in = new FileInputStream(fileName);

  byte buf[] = new byte[1024];
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  int l;
  while (true) {
    l = in.read(buf);
    if (l <= 0) break;
    out.write(buf, 0, l);
  }
  in.close();
  return out.toByteArray();
 } catch (IOException e) { throw new RuntimeException(e); }
}

public static byte[] loadBinaryFile(File file) {
  return loadBinaryFile(file.getPath());
}

static <A> A popLast(List<A> l) {
  return liftLast(l);
}
static String struct(Object o) {
  return structure(o);
}

static String struct(Object o, structure_Data data) {
  return structure(o, data);
}
static String urlencode(String x) {
  try {
    return URLEncoder.encode(unnull(x), "UTF-8");
  } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); }
}
static int shorten_default = 100;

static String shorten(String s) { return shorten(s, shorten_default); }

static String shorten(String s, int max) {
  return shorten(s, max, "...");
}

static String shorten(String s, int max, String shortener) {
  if (s == null) return "";
  if (max < 0) return s;
  return s.length() <= max ? s : substring(s, 0, min(s.length(), max-l(shortener))) + shortener;
}

static String shorten(int max, String s) { return shorten(s, max); }
static void ai_removeWebFromIndex(MultiMap<String, WebNode> index, Web web) {
  if (web != null)
    for (WebNode node : web_nodesAndRelations(web))
      for (String text : asSet(node.texts()))
        index.remove(ai_shortenForIndex(text), node);
}
static long fileLength(String path) { return getFileSize(path); }
static long fileLength(File f) { return getFileSize(f); }

static WebNode web_findNode(Web web, String name) {
  return web.findNode(name);
}
static boolean even(int i) {
  return (i & 1) == 0;
}

static boolean even(long i) {
  return (i & 1) == 0;
}
static String htmlQuote(String s) {
  return "\"" + htmlencode(s) + "\"";
}
static void web_deindex(WebNode n) {
  MultiMap<Lisp, WebNode> index = n.web.index;
  if (index != null)
    for (Lisp label : n.labels())
      index.remove(label, n);
}
static boolean _inCore() {
  return false;
}
static Pair<WebNode, WebNode> web_matchTriple_dollarVars(String a, String b, String c, Web web) {
  List<WebNode> nodes = web_nodes(web);
  if (l(nodes) == 1) {
    // check self-relation
    WebNode n = first(nodes);
    if (nodeMatch_dollarVars(b, web_getRelationOpt(n, n)) == 0
      && nodeMatch_dollarVars(a, n) == 0
      && nodeMatch_dollarVars(c, n) == 0)
      return pair(n);
  } else if (l(nodes) == 2) {
    WebNode n1 = first(nodes), n2 = second(nodes);
    if (nodeMatch_dollarVars(b, web_getRelationOpt(n1, n2)) == 0
      && nodeMatch_dollarVars(a, n1) == 0
      && nodeMatch_dollarVars(c, n2) == 0)
      return pair(n1, n2);
    if (nodeMatch_dollarVars(b, web_getRelationOpt(n2, n1)) == 0
      && nodeMatch_dollarVars(a, n2) == 0
      && nodeMatch_dollarVars(c, n1) == 0)
      return pair(n2, n1);
  }
  return null;
}
static boolean web_matchNodeName(WebNode node, String pat) {
  return eq(pat, "*") || eqic(pat, web_text(node));
}
static int ai_shortenForIndex_maxLength = 100;

static String ai_shortenForIndex(String text) {
  return shorten(text, ai_shortenForIndex_maxLength);
}
static boolean isIdentifier(String s) {
  return isJavaIdentifier(s);
}
static List scanStructureLog(String progID, String fileName) {
  return scanStructureLog(getProgramFile(progID, fileName));
}

static List scanStructureLog(String fileName) {
  return scanStructureLog(getProgramFile(fileName));
}

static List scanStructureLog(File file) {
  List l = new ArrayList();
  for (String s : scanLog(file)) try {
    l.add(unstructure(s));
  } catch (Throwable __e) { printStackTrace2(__e); }
  return l;
}
static String joinStrings(String sep, String... strings) {
  return joinStrings(sep, Arrays.asList(strings));
}

static String joinStrings(String sep, Collection<String> strings) {
  StringBuilder buf = new StringBuilder();
  for (String s : unnull(strings)) if (nempty(s)) {
    if (nempty(buf)) buf.append(sep);
    buf.append(s);
  }
  return str(buf);
}
static Lock appendToFile_lock = lock();
static boolean appendToFile_keepOpen;
static HashMap<String,Writer> appendToFile_writers = new HashMap();

static void appendToFile(String path, String s) { try {
  Lock _lock_142 = appendToFile_lock; lock(_lock_142); try { // Let's just generally synchronize this to be safe.
  new File(path).getParentFile().mkdirs();
  path = getCanonicalPath(path);
  Writer writer = appendToFile_writers.get(path);
  if (writer == null) {
    //print("[Logging to " + path + "]");
    writer = new BufferedWriter(new OutputStreamWriter(
      newFileOutputStream(path, true), "UTF-8"));
    if (appendToFile_keepOpen)
      appendToFile_writers.put(path, writer);
  }
  writer.write(s);
  if (!appendToFile_keepOpen)
    writer.close();
} finally { unlock(_lock_142); } } catch (Exception __e) { throw rethrow(__e); } }
  
static void appendToFile(File path, String s) {
  if (path != null)
    appendToFile(path.getPath(), s);
}

static void cleanMeUp_appendToFile() {
  Lock _lock_143 = appendToFile_lock; lock(_lock_143); try {
  closeAllWriters(values(appendToFile_writers));
  appendToFile_writers.clear();
} finally { unlock(_lock_143); } }
static void quoteToPrintWriter(String s, PrintWriter out) {
  if (s == null) { out.print("null"); return; }
  out.print('"');
  int l = s.length();
  for (int i = 0; i < l; i++) {
    char c = s.charAt(i);
    if (c == '\\' || c == '"') {
      out.print('\\'); out.print(c);
    } else if (c == '\r')
      out.print("\\r");
    else if (c == '\n')
      out.print("\\n");
    else
      out.print(c);
  }
  out.print('"');
}
static boolean eqOneOf(Object o, Object... l) {
  for (Object x : l) if (eq(o, x)) return true; return false;
}
static <A extends Concept> Object[] expandParams(Class<A> c, Object[] params) {
  if (l(params) == 1)
    params = new Object[] { singleFieldName(c), params[0] };
  else
    warnIfOddCount(params);
  return params;
}

static String structureForUser(Object o) {
  return beautifyStructure(struct_noStringSharing(o));
}
static <A> A or(A a, A b) {
  return a != null ? a : b;
}
static int stdcompare(Number a, Number b) {
  return cmp(a, b);
}

static int stdcompare(String a, String b) {
  return cmp(a, b);
}

static int stdcompare(long a, long b) {
  return a < b ? -1 : a > b ? 1 : 0;
}

static int stdcompare(Object a, Object b) {
  return cmp(a, b);
}

static boolean neqic(String a, String b) {
  return !eqic(a, b);
}
static Method fastIntern_method;

static String fastIntern(String s) { try {
  if (s == null) return null;
  if (fastIntern_method == null) {
    fastIntern_method = findMethodNamed(javax(), "internPerProgram");
    if (fastIntern_method == null) upgradeJavaXAndRestart();
  }
    
  return (String) fastIntern_method.invoke(null, s);
} catch (Exception __e) { throw rethrow(__e); } }
static String shortDynamicClassName(Object o) {
 if (o instanceof DynamicObject && ((DynamicObject) o).className != null)
    return ((DynamicObject) o).className;
  return shortClassName(o);
}
// nodeMatcher: func(WebNode, WebNode) -> double
static Map<WebNode, WebNode> web_matchAllPerms_x(Web patternWeb, Web inputWeb, Object nodeMatcher) {
  List<WebNode> patternNodes = web_nodes(patternWeb);
  List<WebNode> inputNodes = web_nodes(inputWeb);
  if (l(patternNodes) != l(inputNodes))
    return null;
    //fail("Can't match, differing number of nodes: " + l(patternNodes) + "/" + l(inputNodes));
  
  List<List<WebNode>> perms = allPermutations(inputNodes);
  Best < List < WebNode > > best = new Best();
  for (List<WebNode> perm : perms)
    best.put(perm, web_matchNodeLists_x(patternNodes, perm, nodeMatcher));
  List<WebNode> l = best.getIfScoreAbove(0);
  return l == null ? null : twoListsToOrderedMap(patternNodes, l);
}
static HashMap<Class, Field[]> getDeclaredFields_cache = new HashMap();

static Field[] getDeclaredFields_cached(Class c) {
  Field[] fields;
  synchronized(getDeclaredFields_cache) {
    fields = getDeclaredFields_cache.get(c);
    if (fields == null) {
      getDeclaredFields_cache.put(c, fields = c.getDeclaredFields());
      for (Field f : fields)
        f.setAccessible(true);
    }
  }
  return fields;
}
static MultiMap<String, WebNode> dwlw_fullNodeIndex_ci() {
  print("Getting webs.");
  long _startTime_222 = sysNow();  
    List<Web> _webs = allWebs();
    List<Web> webs = emptyListWithCapacity(_webs);
    int dropped = 0, dropped2 = 0;
    for (Web web : _webs)
      if (web_nodesTooLong(web))
        dropped++;
      else if (!web_tripelizable(web))
        dropped2++;
      else
        webs.add(web);
    if (dropped != 0)
      print("Dropped " + nWeb(dropped) + " with too long nodes");
    if (dropped2 != 0)
      print("Dropped " + n(dropped2, "non-triple"));
   _startTime_222 = sysNow()-_startTime_222; saveTiming(_startTime_222); 
  print("Got webs in " + lastTiming() + " ms. Making index of " + nWebs(webs) + ".");
  long _startTime_224 = sysNow(); 
    MultiMap<String, WebNode> index = ai_indexWebs(webs);
   done2("Index made.", _startTime_224); 
  return index;
}
static String javaTok_cached_s;
static List<String> javaTok_cached_l;

static synchronized List<String> javaTok_cached(String s) {
  if (neq(s, javaTok_cached_s))
    javaTok_cached_l = javaTok(javaTok_cached_s = s);
  return javaTok_cached_l;
}
static <A, B> HashMap<A, B> pairsToMap(Collection<? extends Pair<A, B>> l) {
  HashMap<A,B> map = new HashMap();
  if (l != null) for (Pair<A, B> p : l)
    map.put(p.a, p.b);
  return map;
}
  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 File programDir_mine; // set this to relocate program's data

static File programDir() {
  return programDir(getProgramID());
}

static File programDir(String snippetID) {
  boolean me = sameSnippetID(snippetID, programID());
  if (programDir_mine != null && me)
    return programDir_mine;
  File dir = new File(javaxDataDir(), formatSnippetID(snippetID));
  if (me) {
    String c = caseID();
    if (nempty(c)) dir = newFile(dir, c);
  }
  return dir;
}

static File programDir(String snippetID, String subPath) {
  return new File(programDir(snippetID), subPath);
}
static void failIfOddCount(Object... list) {
  if (odd(l(list)))
    throw fail("Odd list size: " + list);
}
// the map is: invalidated id -> invalidators

static DependentCache<MultiMap<String, String>> ai_invalidatedWebs_cache = new DependentCache("ai_invalidatedWebs_calc", "cachedNodeIndex" /*_changeCount*/);

static Set<String> ai_invalidatedWebs() {
  return keys(ai_invalidatedWebs_cache.get());
}

static MultiMap<String, String> ai_invalidatedWebs_calc() {
  print("ai_invalidatedWebs_calc");
  ai_onNewOrRemovedWeb("ai_invalidatedWebs_onNewWeb", "ai_invalidatedWebs_onRemovedWeb");
  ai_invalidatedWebs_cache.set(new MultiMap());
  Collection<Web> webs = allWebsFromCachedNodeIndex_uncached();
  print("Have " + nWebs(webs));
  for (Web web : webs)
    ai_invalidatedWebs_onNewWeb(web);
  print("calc done");
  return ai_invalidatedWebs_cache.get();
}

static void ai_invalidatedWebs_onNewWeb(Web web) {
  if (web.unverified) return;
  Lock _lock_167 = ai_invalidatedWebs_cache.lock; lock(_lock_167); try {
  Matches m = new Matches();
  for (WebNode n : web_search_dollarX(webFromTriple("$X", "is", "invalid"), web))
    if (web_match("Web *", n, m) && isGlobalID(m.unq(0)))
      ai_invalidatedWebs_cache.get().put(m.unq(0), web.globalID);
} finally { unlock(_lock_167); } }

static void ai_invalidatedWebs_onRemovedWeb(Web web) {
  if (web.unverified) return;
  Lock _lock_168 = ai_invalidatedWebs_cache.lock; lock(_lock_168); try {
  Matches m = new Matches();
  for (WebNode n : web_search_dollarX(webFromTriple("$X", "is", "invalid"), web))
    if (web_match("Web *", n, m) && isGlobalID(m.unq(0)))
      ai_invalidatedWebs_cache.get().remove(m.unq(0), web.globalID);
} finally { unlock(_lock_168); } }

static IterableIterator<String> linesFromFile(File f) { try {
  if (!f.exists()) return emptyIterableIterator();
  if (ewic(f.getName(), ".gz"))
    return linesFromReader(utf8bufferedReader(new GZIPInputStream(new FileInputStream(f))));
  return linesFromReader(utf8bufferedReader(f));
} catch (Exception __e) { throw rethrow(__e); } }
static boolean checkCondition(Object condition, Object... args) {
  return isTrue(callF(condition, args));
}
// make concept instance that is not connected to DB
static <A extends Concept> A unlisted(Class<A> c, Object... args) {
  concepts_unlisted.set(true);
  try {
    return nuObject(c, args);
  } finally {
    concepts_unlisted.set(null);
  }
}
static File newFile(File base, String... names) {
  for (String name : names) base = new File(base, name);
  return base;
}

static File newFile(String name) {
  return name == null ? null : new File(name);
}
static List<List<String>> englishVerbQuintuples() {
  return english1000VerbQuintuples();
}
static int parseInt(String s) {
  return empty(s) ? 0 : Integer.parseInt(s);
}

static int parseInt(char c) {
  return Integer.parseInt(str(c));
}
static String[] dropFirst(int n, String[] a) {
  return drop(n, a);
}

static String[] dropFirst(String[] a) {
  return drop(1, a);
}

static Object[] dropFirst(Object[] a) {
  return drop(1, a);
}

static <A> List<A> dropFirst(List<A> l) {
  return dropFirst(1, l);
}

static <A> List<A> dropFirst(Iterable<A> i) {
  return dropFirst(toList(i));
}

static <A> List<A> dropFirst(int n, List<A> l) {
  return n <= 0 ? l : new ArrayList(l.subList(Math.min(n, l.size()), l.size()));
}

static <A> List<A> dropFirst(List<A> l, int n) {
  return dropFirst(n, l);
}

static String dropFirst(int n, String s) { return substring(s, n); }
static String dropFirst(String s) { return substring(s, 1); }
static <A, B> List<B> getFromAll(Collection<Map<A, B>> maps, A key) {
  List<B> l = new ArrayList();
  if (maps != null) for (Map<A, B> map : maps)
    addIfNotNull(l, map.get(key));
  return l;
}
static String circlesEditorDBID() {
  return "#1007609";
}
static URLConnection setURLConnectionTimeouts(URLConnection con, int timeout) {
  con.setConnectTimeout(timeout);
  con.setReadTimeout(timeout);
  if (con.getConnectTimeout() != timeout || con.getReadTimeout() != timeout)
    print("Warning: Timeouts not set by JDK.");
  return con;
}
static String dropPrefix(String prefix, String s) {
  return s == null ? null : s.startsWith(prefix) ? s.substring(l(prefix)) : s;
}
static String standardCredentialsUser() {
  return trim(loadTextFile(new File(userHome(), ".tinybrain/username")));
}
public static void copyFile(File src, File dest) { try {
  mkdirsForFile(dest);
  FileInputStream inputStream = new FileInputStream(src.getPath());
  FileOutputStream outputStream = newFileOutputStream(dest.getPath());
  try {
    copyStream(inputStream, outputStream);
    inputStream.close();
  } finally {
    outputStream.close();
  }
} catch (Exception __e) { throw rethrow(__e); } }
  public static boolean isSnippetID(String s) {
    try {
      parseSnippetID(s);
      return true;
    } catch (RuntimeException e) {
      return false;
    }
  }
static File javaxCachesDir_dir; // can be set to work on different base dir

static File javaxCachesDir() {
  return javaxCachesDir_dir != null ? javaxCachesDir_dir : new File(userHome(), "JavaX-Caches");
}
static IterableIterator<String> toLines(File f) {
  return linesFromFile(f);
}

static List<String> toLines(String s) {
  List<String> lines = new ArrayList<String>();
  if (s == null) return lines;
  int start = 0;
  while (true) {
    int i = toLines_nextLineBreak(s, start);
    if (i < 0) {
      if (s.length() > start) lines.add(s.substring(start));
      break;
    }

    lines.add(s.substring(start, i));
    if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n')
      i += 2;
    else
      ++i;

    start = i;
  }
  return lines;
}

static int toLines_nextLineBreak(String s, int start) {
  for (int i = start; i < s.length(); i++) {
    char c = s.charAt(i);
    if (c == '\r' || c == '\n')
      return i;
  }
  return -1;
}
static List<Class> getClasses(Object[] array) {
  List<Class> l = new ArrayList();
  for (Object o : array) l.add(_getClass(o));
  return l;
}
static Map<WebNode, WebNode> web_matchAllPerms(Web patternWeb, Web inputWeb) {
  List<WebNode> patternNodes = web_nodes(patternWeb);
  List<WebNode> inputNodes = web_nodes(inputWeb);
  if (l(patternNodes) != l(inputNodes))
    return null;
    //fail("Can't match, differing number of nodes: " + l(patternNodes) + "/" + l(inputNodes));
  
  List<List<WebNode>> perms = allPermutations(inputNodes);
  Best < List < WebNode > > best = new Best();
  for (List<WebNode> perm : perms)
    best.put(perm, web_matchNodeLists(patternNodes, perm));
  List<WebNode> l = best.getIfScoreAbove(0);
  return l == null ? null : twoListsToOrderedMap(patternNodes, l);
}
static final boolean loadPageThroughProxy_enabled = false;

static String loadPageThroughProxy(String url) {
  return null;
}
static void hotwire_copyOver(Class c) {
  synchronized(StringBuffer.class) {
    for (String field : litlist("print_log", "print_silent", "androidContext")) {
      Object o = getOpt(mc(), field);
      if (o != null)
        setOpt(c, field, o);
    }
      
    Object mainBot = getMainBot();
    if (mainBot != null)
      setOpt(c, "mainBot", mainBot);

    setOpt(c, "creator_class", new WeakReference(mc()));
  }
}
static Cache<Set<String>> knownAdverbs_cache = new Cache("knownAdverbs_load");
static Set<String> knownAdverbs() { return knownAdverbs_cache.get(); }

static Set<String> knownAdverbs_load() {
  return toCaseInsensitiveSet(
    concatLists(
      map("adjectiveToAdverb",knownAdjectives()),
      tlft(loadSnippet("#1011128"))));
}

static void sleepSeconds(double s) {
  if (s > 0) sleep(round(s*1000));
}
static Object callOpt(Object o) {
  if (o == null) return null;
  return callF(o);
}

static Object callOpt(Object o, String method, Object... args) {
  try {
    if (o == null) return null;
    if (o instanceof Class) {
      Method m = callOpt_findStaticMethod((Class) o, method, args, false);
      if (m == null) return null;
      m.setAccessible(true);
      return m.invoke(null, args);
    } else {
      Method m = callOpt_findMethod(o, method, args, false);
      if (m == null) return null;
      m.setAccessible(true);
      return m.invoke(o, args);
    }
  } catch (Exception e) {
    //fail(e.getMessage() + " | Method: " + method + ", receiver: " + className(o) + ", args: (" + join(", ", map(f className, args) + ")");
    throw new RuntimeException(e);
  }
}

static Method callOpt_findStaticMethod(Class c, String method, Object[] args, boolean debug) {
  Class _c = c;
  while (c != null) {
    for (Method m : c.getDeclaredMethods()) {
      if (debug)
        System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
      if (!m.getName().equals(method)) {
        if (debug) System.out.println("Method name mismatch: " + method);
        continue;
      }

      if ((m.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0 || !callOpt_checkArgs(m, args, debug))
        continue;

      return m;
    }
    c = c.getSuperclass();
  }
  return null;
}

static Method callOpt_findMethod(Object o, String method, Object[] args, boolean debug) {
  Class c = o.getClass();
  while (c != null) {
    for (Method m : c.getDeclaredMethods()) {
      if (debug)
        System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
      if (m.getName().equals(method) && callOpt_checkArgs(m, args, debug))
        return m;
    }
    c = c.getSuperclass();
  }
  return null;
}

private static boolean callOpt_checkArgs(Method m, Object[] args, boolean debug) {
  Class<?>[] types = m.getParameterTypes();
  if (types.length != args.length) {
    if (debug)
      System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
    return false;
  }
  for (int i = 0; i < types.length; i++)
    if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
      if (debug)
        System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
      return false;
    }
  return true;
}


static void web_index(WebNode n) {
  MultiMap<Lisp, WebNode> index = n.web.index;
  if (index != null)
    for (Lisp label : n.labels())
      index.setPut(label, n);
}
static void assertFalse(Object o) {
  if (!(eq(o, false) || isFalse(pcallF(o))))
    throw fail(str(o));
}
  
static boolean assertFalse(boolean b) {
  if (b) throw fail("oops");
  return b;
}

static boolean assertFalse(String msg, boolean b) {
  if (b) throw fail(msg);
  return b;
}

static <A> List<A> uniquifyList(Collection<A> l) {
  HashSet<A> set = new HashSet();
  List<A> out = new ArrayList();
  for (A a : unnull(l))
    if (set.add(a))
      out.add(a);
  return out;
}
static Cache<Set<String>> knownPhrases_cache = new Cache("knownPhrases_load");
static Set<String> knownPhrases() { return knownPhrases_cache.get(); }

static Set<String> knownPhrases_load() {
  return toCaseInsensitiveSet(tlft(loadSnippet("#1011207")));
}

static boolean isJavaIdentifierAfter(String s, int i) {
  int n = l(s);
  if (i >= n || !Character.isJavaIdentifierStart(s.charAt(i)))
    return false;
  for (i++; i < n; i++)
    if (!Character.isJavaIdentifierPart(s.charAt(i)))
      return false;
  return true;
}


static String formatWithThousandsSeparator(long l) {
  return NumberFormat.getInstance(new Locale("en_US")).format(l);
}
static List<String> joinBrackets(List<String> tok) {
  List<String> t = new ArrayList();
  Map<Integer, Integer> 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 Map<String, Class> classForName_cache = synchroHashMap();

static Class classForName(String name) { try {
  Class c = classForName_cache.get(name);
  if (c == null)
    classForName_cache.put(name, c = Class.forName(name));
  return c;
} catch (Exception __e) { throw rethrow(__e); } }
static double nodeMatch_dollarVars(WebNode a, WebNode b) {
  if (isDollarVar(a.text())) return isDollarVar(b.text()) ? -1 : 0;
  for (String s : web_texts(b))
    if (!web_hasLabel(a, s))
      return -1;
  return 0;
  //ret containsAll(web_ucTexts(a), web_ucTexts(b)) ? 0 : -1;
}

static double nodeMatch_dollarVars(String text, WebNode b) {
  if (b == null) return -1;
  if (isDollarVar(text)) return isDollarVar(b.text()) ? -1 : 0;
  return cic(web_texts(b), text) ? 0 : -1;
}
static String standardCredentialsPass() {
  return trim(loadTextFile(new File(userHome(), ".tinybrain/userpass")));
}
static File userDir() {
  return new File(userHome());
}

static File userDir(String path) {
  return new File(userHome(), path);
}


static List<WebNode> web_search_dollarX(Web searchWeb, Web web) {
  return webs_search_dollarX(searchWeb, ll(web));
}
static Cache<List<List<String>>> english1000VerbQuintuples_cache = new Cache("english1000VerbQuintuples_load");

static List<List<String>> english1000VerbQuintuples() {
  return english1000VerbQuintuples_cache.get();
}

static List<List<String>> english1000VerbQuintuples_load() {
  List < List < String > > out = new ArrayList();
  for (String s : tlft_j(loadSnippet("#1010402"))) {
    List<String> l = splitAtSpace(s);
    if (l(l) != 6) { /*print("e1vq huh? " + s);*/ } else {
      l = dropFirst(l);
      out.add(l);
    }
  }
  return out;
}

static String nWeb(int i) {
  return nWebs(i);
}

static String nWeb(Collection c) {
  return nWebs(c);
}
static String dropSuffixIgnoreCase(String suffix, String s) {
  return ewic(s, suffix) ? s.substring(0, l(s)-l(suffix)) : s;
}
static <A> List<List<A>> allPermutations(List<A> l) {
  List < List < A > > out = new ArrayList();
  allPermutations_impl(cloneList(l), l(l), out);
  return out;
}

static <A> void allPermutations_impl(List<A> l, int n, List<List<A>> out) {
  if (n <= 1) {
    ping();
    out.add(cloneList(l));
  } else for(int i = 0; i < n; i++) {
    allPermutations_impl(l, n-1, out);
    swapElements(l, n % 2 == 0 ? i : 0, n-1);
  }
}
static String getCanonicalPath(String path) { try {
  return new File(path).getCanonicalPath();
} catch (Exception __e) { throw rethrow(__e); } }
static String n(long l, String name) {
  return l + " " + trim(l == 1 ? singular(name) : getPlural(name));
}

static String n(Collection l, String name) {
  return n(l(l), name);
}

static String n(Map m, String name) {
  return n(l(m), name);
}

static String n(Object[] a, String name) {
  return n(l(a), name);
}


static Class<?> _getClass(String name) {
  try {
    return Class.forName(name);
  } catch (ClassNotFoundException e) {
    return null; // could optimize this
  }
}

static Class _getClass(Object o) {
  return o == null ? null
    : o instanceof Class ? (Class) o : o.getClass();
}

static Class _getClass(Object realm, String name) { try {
  return getClass(realm).getClassLoader().loadClass(classNameToVM(name));
} catch (Exception __e) { throw rethrow(__e); } }
// make a lisp form
static Lisp lisp(String head, Object... args) {
  Lisp l = new Lisp(head);
  for (Object o : args)
    l.add(o);
  return l;
}

static Lisp lisp(String head, Collection args) {
  return new Lisp(head, args);
}

static void warnIfOddCount(Object... list) {
  if (odd(l(list)))
    printStackTrace("Odd list size: " + list);
}
static <A, B extends A> List<A> listMinusSet(Collection<A> l, Collection<B> stuff) {
  if (empty(stuff)) return asList(l);
  Set<B> set = asSet(stuff);
  List<A> l2 = new ArrayList();
  for (A a : l) if (!set.contains(a)) l2.add(a);
  return l2;
}
static String shortClassName(Object o) {
  if (o == null) return null;
  Class c = o instanceof Class ? (Class) o : o.getClass();
  String name = c.getName();
  return shortenClassName(name);
}
static boolean checkConceptFields(Concept x, Object... data) {
  for (int i = 0; i < l(data); i += 2)
    if (neq(cget(x, (String) data[i]), deref(data[i+1])))
      return false;
  return true;
}
static IterableIterator<String> linesFromReader(Reader r) {
  final BufferedReader br = bufferedReader(r);
  return iteratorFromFunction(new F0<String>() { String get() { try { return  readLineFromReaderWithClose(br) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "readLineFromReaderWithClose(br)"; }});
}
static ThreadLocal<Long> saveTiming_last = new ThreadLocal();

static void saveTiming(long ms) {
  print(ms + " ms");
  saveTiming_noPrint(ms);
}

static void saveTiming_noPrint(long ms) {
  saveTiming_last.set(ms);
}
static File conceptsFile(String progID) {
  return getProgramFile(progID, "concepts.structure.gz");
}
// map: index of opening bracket -> index of closing bracket
static Map<Integer, Integer> getBracketMap(List tok) {
  TreeMap<Integer,Integer> map = new TreeMap();
  List<Integer> stack = new ArrayList();
  for (int i = 1; i < l(tok); i+= 2) {
    if (getBracketMap_opening.contains(tok.get(i)))
      stack.add(i);
    else if (getBracketMap_closing.contains(tok.get(i))) {
      if (!empty(stack))
        map.put(liftLast(stack), i);
    }
  }
  return map;
}

static List<String> getBracketMap_opening = ll("{", "(");
static List<String> getBracketMap_closing = ll("}", ")");
static boolean web_match(String pat, WebNode node, Matches m) {
  for (String s : web_texts(node))
    if (match(pat, s, m)) return true;
  return false;
}
static long getFileSize(String path) {
  return path == null ? 0 : new File(path).length();
}

static long getFileSize(File f) {
  return f == null ? 0 : f.length();
}
// This is a bit rough... finds static and non-static methods.

static Method findMethodNamed(Object obj, String method) {
  if (obj == null) return null;
  if (obj instanceof Class)
    return findMethodNamed((Class) obj, method);
  return findMethodNamed(obj.getClass(), method);
}

static Method findMethodNamed(Class c, String method) {
  while (c != null) {
    for (Method m : c.getDeclaredMethods())
      if (m.getName().equals(method)) {
        m.setAccessible(true);
        return m;
      }
    c = c.getSuperclass();
  }
  return null;
}
static void closeAllWriters(Collection<? extends Writer> l) {
  for (Writer w : unnull(l)) try {
    w.close();
  } catch (Throwable __e) { printStackTrace2(__e); }
}
static void sleepInCleanUp(long ms) { try {
  if (ms < 0) return;
  Thread.sleep(ms);
} catch (Exception __e) { throw rethrow(__e); } }
static boolean web_nodesContainText(Collection<WebNode> l, String text) {
  if (l != null) for (WebNode n : l)
    if (web_hasText(n, text)) return true;
  return false;
}

static boolean isJavaIdentifier(String s) {
  if (empty(s) || !Character.isJavaIdentifierStart(s.charAt(0)))
    return false;
  for (int i = 1; i < s.length(); i++)
    if (!Character.isJavaIdentifierPart(s.charAt(i)))
      return false;
  return true;
}
static List<Web> allWebs() {
  return concatWebLists(allWebs_fullList());
}
static int done2_minPrint = 10;

static long done2(long startTime, String desc) {
  return done2(startTime, desc, done2_minPrint);
}

static long done2(long startTime, String desc, int minPrint) {
  long time = sysNow()-startTime;
  saveTiming_noPrint(time);
  if (time >= minPrint)
    print(desc + " [" + time + " ms]");
  return time;
}

static long done2(String desc, long startTime) {
  return done2(startTime, desc);
}

static long done2(long startTime) {
  return done2(startTime, "");
}
static int cmp(Number a, Number b) {
  return a == null ? b == null ? 0 : -1 : cmp(a.doubleValue(), b.doubleValue());
}

static int cmp(double a, double b) {
  return a < b ? -1 : a == b ? 0 : 1;
}

static int cmp(String a, String b) {
  return a == null ? b == null ? 0 : -1 : a.compareTo(b);
}

static int cmp(Object a, Object b) {
  if (a == null) return b == null ? 0 : -1;
  if (b == null) return 1;
  return ((Comparable) a).compareTo(b);
}
static 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 <A> ArrayList<A> toList(A[] a) { return asList(a); }
static ArrayList<Integer> toList(int[] a) { return asList(a); }
static <A> ArrayList<A> toList(Set<A> s) { return asList(s); }
static <A> ArrayList<A> toList(Iterable<A> s) { return asList(s); }
static String singleFieldName(Class c) {
  Set<String> l = listFields(c);
  if (l(l) != 1)
    throw fail("No single field found in " + c + " (have " + n(l(l), "fields") + ")");
  return first(l);
}
static List collect(Collection c, String field) {
  return collectField(c, field);
}

static List collect(String field, Collection c) {
  return collectField(c, field);
}


static List collect(Class c, String field) {
  return collect(list(c), field);
}

static 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 List ai_onNewIndexedText_list = synchroList();

// f: voidfunc(S)
static void ai_onNewIndexedText_do(Object f) {
  setAdd(ai_onNewIndexedText_list, f);
}

static void ai_onNewIndexedText(String text) {
  pcallFAll(ai_onNewIndexedText_list, text);
}
static long lastTiming() {
  return getLastTiming();
}
static volatile String caseID_caseID;

static String caseID() { return caseID_caseID; }

static void caseID(String id) {
  caseID_caseID = id;
}
static Object unstructure(String text) {
  return unstructure(text, false);
}

static Object unstructure(String text, final boolean allDynamic) {
  return unstructure(text, allDynamic, null);
}

static int structure_internStringsLongerThan = 50;

static int unstructure_tokrefs; // stats

abstract static class unstructure_Receiver {
  abstract void set(Object o);
}

// classFinder: func(name) -> class (optional)
static Object unstructure(String text, boolean allDynamic,
  Object classFinder) {
  if (text == null) return null;
  return unstructure_tok(javaTokC_noMLS_iterator(text), allDynamic, classFinder);
}

static Object unstructure_reader(BufferedReader reader) {
  return unstructure_tok(javaTokC_noMLS_onReader(reader), false, null);
}

static Object unstructure_tok(final Producer<String> tok, final boolean allDynamic, final Object classFinder) {
  final boolean debug = unstructure_debug;
  
  final class X {
    int i = -1;
    HashMap<Integer,Object> refs = new HashMap();
    HashMap<Integer,Object> tokrefs = new HashMap();
    HashSet<String> concepts = new HashSet();
    HashMap<String,Class> classesMap = new HashMap();
    List<Runnable> stack = new ArrayList();
    String curT;
    
    // look at current token
    String t() {
      return curT;
    }
    
    // get current token, move to next
    String tpp() {
      String t = curT;
      consume();
      return t;
    }
    
    void parse(final unstructure_Receiver out) {
      String t = t();
      
      int refID = 0;
      if (structure_isMarker(t, 0, l(t))) {
        refID = parseInt(t.substring(1));
        consume();
      }
      final int _refID = refID;
      
      // if (debug) print("parse: " + quote(t));
      
      final int tokIndex = i;  
      parse_inner(refID, tokIndex, new unstructure_Receiver() {
        void set(Object o) {
          if (_refID != 0)
            refs.put(_refID, o);
          if (o != null)
            tokrefs.put(tokIndex, o);
          out.set(o);
        }
      });
    }
    
    void parse_inner(int refID, int tokIndex, final unstructure_Receiver out) {
      String t = t();
      
      // if (debug) print("parse_inner: " + quote(t));
      
      Class c = classesMap.get(t);
      if (c == null) {
        if (t.startsWith("\"")) {
          String s = internIfLongerThan(unquote(tpp()), structure_internStringsLongerThan);
          out.set(s); return;
        }
        
        if (t.startsWith("'")) {
          out.set(unquoteCharacter(tpp())); return;
        }
        if (t.equals("bigint")) {
          out.set(parseBigInt()); return;
        }
        if (t.equals("d")) {
          out.set(parseDouble()); return;
        }
        if (t.equals("fl")) {
          out.set(parseFloat()); return;
        }
        if (t.equals("false") || t.equals("f")) {
          consume(); out.set(false); return;
        }
        if (t.equals("true") || t.equals("t")) {
          consume(); out.set(true); return;
        }
        if (t.equals("-")) {
          consume();
          t = tpp();
          out.set(isLongConstant(t) ? (Object) (-parseLong(t)) : (Object) (-parseInt(t))); return;
        }
        if (isInteger(t) || isLongConstant(t)) {
          consume();
          //if (debug) print("isLongConstant " + quote(t) + " => " + isLongConstant(t));
          if (isLongConstant(t)) {
            out.set(parseLong(t)); return;
          }
          long l = parseLong(t);
          boolean isInt = l == (int) l;
          if (debug)
            print("l=" + l + ", isInt: " + isInt);
          out.set(isInt ? (Object) new Integer((int) l) : (Object) new Long(l)); return;
        }
        
        if (t.equals("File")) {
          consume();
          File f = new File(unquote(tpp()));
          out.set(f); return;
        }
        
        if (t.startsWith("r") && isInteger(t.substring(1))) {
          consume();
          int ref = Integer.parseInt(t.substring(1));
          Object o = refs.get(ref);
          if (o == null)
            print("Warning: unsatisfied back reference " + ref);
          out.set(o); return;
        }
      
        if (t.startsWith("t") && isInteger(t.substring(1))) {
          consume();
          int ref = Integer.parseInt(t.substring(1));
          Object o = tokrefs.get(ref);
          if (o == null)
            print("Warning: unsatisfied token reference " + ref);
          out.set(o); return;
        }
        
        if (t.equals("hashset")) {
          parseHashSet(out); return;
        }
        if (t.equals("treeset")) {
          parseTreeSet(out); return;
        }
        if (eqOneOf(t, "hashmap", "hm")) {
          consume();
          parseMap(new HashMap(), out);
          return;
        }
        if (t.equals("lhm")) {
          consume();
          parseMap(new LinkedHashMap(), out);
          return;
        }
        if (t.equals("{")) {
          parseMap(out); return;
        }
        if (t.equals("[")) {
          parseList(out); return;
        }
        if (t.equals("bitset")) {
          parseBitSet(out); return;
        }
        if (t.equals("array") || t.equals("intarray")) {
          parseArray(out); return;
        }
        if (t.equals("ba")) {
          consume();
          String hex = unquote(tpp());
          out.set(hexToBytes(hex)); return;
        }
        if (t.equals("boolarray")) {
          consume();
          int n = parseInt(tpp());
          String hex = unquote(tpp());
          out.set(boolArrayFromBytes(hexToBytes(hex), n)); return;
        }
        if (t.equals("class")) {
          out.set(parseClass()); return;
        }
        if (t.equals("l")) {
          parseLisp(out); return;
        }
        if (t.equals("null")) {
          consume(); out.set(null); return;
        }
        
        if (eq(t, "c")) {
          consume("c");
          t = t();
          assertTrue(isJavaIdentifier(t));
          concepts.add(t);
        }
      }
      
      if (eq(t, "j")) {
        consume("j");
        out.set(parseJava()); return;
      }

      if (c == null && !isJavaIdentifier(t))
        throw new RuntimeException("Unknown token " + (i+1) + ": " + t);
        
      // any other class name
      if (c == null) {
        // First, find class
        if (allDynamic) c = null;
        else c = classFinder != null ? (Class) callF(classFinder, t) : findClass(t);
        if (c != null)
          classesMap.put(t, c);
      }
          
      // Check if it has an outer reference
      consume();
      boolean hasBracket = eq(t(), "(");
      if (hasBracket) consume();
      boolean hasOuter = hasBracket && eq(t(), "this$1");
      
      DynamicObject dO = null;
      Object o = null;
      if (c != null) {
        o = hasOuter ? nuStubInnerObject(c) : nuEmptyObject(c);
        if (o instanceof DynamicObject) dO = (DynamicObject) o;
      } else {
        if (concepts.contains(t) && (c = findClass("Concept")) != null)
          o = dO = (DynamicObject) nuEmptyObject(c);
        else
          dO = new DynamicObject();
        dO.className = t;
        if (debug) print("Made dynamic object " + t + " " + shortClassName(dO));
      }
      
      // Save in references list early because contents of object
      // might link back to main object
      
      if (refID != 0)
        refs.put(refID, o != null ? o : dO);
      tokrefs.put(tokIndex, o != null ? o : dO);
      
      // NOW parse the fields!
      
      final LinkedHashMap<String,Object> fields = new LinkedHashMap(); // preserve order
      final Object _o = o;
      final DynamicObject _dO = dO;
      if (hasBracket) {
        stack.add(new Runnable() { public void run() { try { 
          if (eq(t(), ")")) {
            consume(")");
            objRead(_o, _dO, fields);
            out.set(_o != null ? _o : _dO);
          } else {
            final String key = unquote(tpp());
            consume("=");
            stack.add(this);
            parse(new unstructure_Receiver() {
              void set(Object value) {
                fields.put(key, value);
                if (eq(t(), ",")) consume();
              }
            });
          }
        
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (eq(t(), \")\")) {\r\n            consume(\")\");\r\n            objRead(_o, _dO, ..."; }});
      } else {
        objRead(o, dO, fields);
        out.set(o != null ? o : dO);
      }
    }
    
    void objRead(Object o, DynamicObject dO, Map<String, Object> fields) {
      
      if (o != null)
        if (dO != null) {
          if (debug)
            printStructure("setOptAllDyn", fields);
          setOptAllDyn(dO, fields);
        } else {
          setOptAll(o, fields);
          
        }
      else for (String field : keys(fields))
        dO.fieldValues.put(intern(field), fields.get(field));

      if (o != null)
        pcallOpt_noArgs(o, "_doneLoading");
    }
    
    void parseSet(final Set set, final unstructure_Receiver out) {
      parseList(new unstructure_Receiver() {
        void set(Object o) {
          set.addAll((List) o);
          out.set(set);
        }
      });
    }
    
    void parseLisp(final unstructure_Receiver out) {
      
        consume("l");
        consume("(");
        final ArrayList list = new ArrayList();
        stack.add(new Runnable() { public void run() { try { 
          if (eq(t(), ")")) {
            consume(")");
            out.set(new Lisp((String) list.get(0), subList(list, 1)));
          } else {
            stack.add(this);
            parse(new unstructure_Receiver() {
              void set(Object o) {
                list.add(o);
                if (eq(t(), ",")) consume();
              }
            });
          }
        
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (eq(t(), \")\")) {\r\n            consume(\")\");\r\n            out.set(Lisp((Str..."; }});
        if (false) // skip fail line
      
      
      throw fail("class Lisp not included");
    }
    
    void parseBitSet(final unstructure_Receiver out) {
      consume("bitset");
      consume("{");
      final BitSet bs = new BitSet();
      stack.add(new Runnable() { public void run() { try { 
        if (eq(t(), "}")) {
          consume("}");
          out.set(bs);
        } else {
          stack.add(this);
          parse(new unstructure_Receiver() {
            void set(Object o) {
              bs.set((Integer) o);
              if (eq(t(), ",")) consume();
            }
          });
        }
      
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (eq(t(), \"}\")) {\r\n          consume(\"}\");\r\n          out.set(bs);\r\n       ..."; }});
    }
    
    void parseList(final unstructure_Receiver out) {
      consume("[");
      final ArrayList list = new ArrayList();
      stack.add(new Runnable() { public void run() { try { 
        if (eq(t(), "]")) {
          consume("]");
          out.set(list);
        } else {
          stack.add(this);
          parse(new unstructure_Receiver() {
            void set(Object o) {
              //if (debug) print("List element type: " + getClassName(o));
              list.add(o);
              if (eq(t(), ",")) consume();
            }
          });
        }
      
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (eq(t(), \"]\")) {\r\n          consume(\"]\");\r\n          out.set(list);\r\n     ..."; }});
    }
    
    void parseArray(final unstructure_Receiver out) {
      final String type = tpp();
      consume("{");
      final List list = new ArrayList();
      
      stack.add(new Runnable() { public void run() { try { 
        if (eq(t(), "}")) {
          consume("}");
          out.set(type.equals("intarray") ? toIntArray(list) : list.toArray());
        } else {
          stack.add(this);
          parse(new unstructure_Receiver() {
            void set(Object o) {
              list.add(o);
              if (eq(t(), ",")) consume();
            }
          });
        }
      
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (eq(t(), \"}\")) {\r\n          consume(\"}\");\r\n          out.set(type.equals(\"..."; }});
    }
    
    Object parseClass() {
      consume("class");
      consume("(");
      String name = unquote(tpp());
      consume(")");
      name = dropPrefix("main$", name);
      Class c = allDynamic ? null : classFinder != null ? (Class) callF(classFinder, name) : findClass(name);
      if (c != null) return c;
      DynamicObject dO = new DynamicObject();
      dO.className = "java.lang.Class";
      dO.fieldValues.put("name", name);
      return dO;
    }
    
    Object parseBigInt() {
      consume("bigint");
      consume("(");
      String val = tpp();
      if (eq(val, "-"))
        val = "-" + tpp();
      consume(")");
      return new BigInteger(val);
    }
    
    Object parseDouble() {
      consume("d");
      consume("(");
      String val = unquote(tpp());
      consume(")");
      return Double.parseDouble(val);
    }
    
    Object parseFloat() {
      consume("fl");
      String val;
      if (eq(t(), "(")) {
        consume("(");
        val = unquote(tpp());
        consume(")");
      } else {
        val = unquote(tpp());
      }
      return Float.parseFloat(val);
    }
    
    void parseHashSet(unstructure_Receiver out) {
      consume("hashset");
      parseSet(new HashSet(), out);
    }
    
    void parseTreeSet(unstructure_Receiver out) {
      consume("treeset");
      parseSet(new TreeSet(), out);
    }
    
    void parseMap(unstructure_Receiver out) {
      parseMap(new TreeMap(), out);
    }
    
    Object parseJava() {
      String j = unquote(tpp());
      Matches m = new Matches();
      if (jmatch("java.awt.Color[r=*,g=*,b=*]", j, m))
        return nuObject("java.awt.Color", parseInt(m.unq(0)), parseInt(m.unq(1)), parseInt(m.unq(2)));
      else {
        warn("Unknown Java object: " + j);
        return null;
      }
    }
    
    void parseMap(final Map map, final unstructure_Receiver out) {
      consume("{");
      stack.add(new Runnable() {
        boolean v;
        Object key;
        
        public void run() { 
          if (v) {
            v = false;
            stack.add(this);
            consume("=");
            parse(new unstructure_Receiver() {
              void set(Object value) {
                map.put(key, value);
                if (debug)
                  print("parseMap: Got value " + getClassName(value) + ", next token: " + quote(t()));
                if (eq(t(), ",")) consume();
              }
            });
          } else {
            if (eq(t(), "}")) {
              consume("}");
              out.set(map);
            } else {
              v = true;
              stack.add(this);
              parse(new unstructure_Receiver() {
                void set(Object o) {
                  key = o;
                }
              });
            }
          } // if v else
        } // run()
      });
    }
    
    /*void parseSub(unstructure_Receiver out) {
      int n = l(stack);
      parse(out);
      while (l(stack) > n)
        stack
    }*/
    
    void consume() { curT = tok.next(); ++i; }
    
    void consume(String s) {
      if (!eq(t(), s)) {
        /*S prevToken = i-1 >= 0 ? tok.get(i-1) : "";
        S nextTokens = join(tok.subList(i, Math.min(i+2, tok.size())));
        fail(quote(s) + " expected: " + prevToken + " " + nextTokens + " (" + i + "/" + tok.size() + ")");*/
        throw fail(quote(s) + " expected, got " + quote(t()));
      }
      consume();
    }
    
    void parse_x(unstructure_Receiver out) {
      consume(); // get first token
      parse(out);
      while (nempty(stack))
        popLast(stack).run();
    }
  }
  
  Boolean b = DynamicObject_loading.get();
  DynamicObject_loading.set(true);
  try {
    final Var v = new Var();
    X x = new X();
    x.parse_x(new unstructure_Receiver() {
      void set(Object o) { v.set(o); }
    });
    unstructure_tokrefs = x.tokrefs.size();
    return v.get();
  } finally {
    DynamicObject_loading.set(b);
  }
}

static boolean unstructure_debug;
static Object mainBot;

static Object getMainBot() {
  return mainBot;
}
static IterableIterator emptyIterableIterator_instance = new IterableIterator() {
  public Object next() { throw fail(); }
  public boolean hasNext() { return false; }
};

static <A> IterableIterator<A> emptyIterableIterator() {
  return emptyIterableIterator_instance; 
}
static List<String> scanLog(String progID, String fileName) {
  return scanLog(getProgramFile(progID, fileName));
}

static List<String> scanLog(String fileName) {
  return scanLog(getProgramFile(fileName));
}

static List<String> scanLog(File file) {
  List<String> l = new ArrayList();
  for (File f : concatLists(earlierPartsOfLogFile(file), ll(file)))
    for (String s : toLines(file))
      if (isProperlyQuoted(s)) l.add(unquote(s));
  return l;
}
static boolean web_matchNodeLists_x_debug;

// nodeMatcher: func(WebNode, WebNode) -> double
static int web_matchNodeLists_x(List<WebNode> l1, List<WebNode> l2, Object nodeMatcher) {
  if (l(l1) != l(l2)) return -1000;
  int score = 0;
  if (empty(l1)) return score;
  HashMap<WebNode, WebNode> map = twoListsToHashMap(l1, l2);
  Web web = first(l1).web, web2 = first(l2).web;
  
  // Score nodes
  for (int i = 0; i < l(l1); i++) {
    WebNode a = l1.get(i), b = l2.get(i);
    score += toDouble(callF(nodeMatcher, a, b));
  }
  
  // Score relations
  for (WebRelation rel : web_relationObjects(web)) {
    WebNode aa = map.get(rel.a), bb = map.get(rel.b);
    if (aa == null || bb == null) continue;
    WebRelation rel2 = web2.getRelationOpt(aa, bb);
    if (web_matchNodeLists_x_debug)
      print("a=" + rel.a + ", b=" + rel.b + ", aa=" + aa + ", bb=" + bb + ", rel2 labels=" + (rel2 == null ? "-" : str(rel2.labels)) + ", labels=" + rel.labels);
    //if (rel2 == null || !containsAll(rel2.labels, rel.labels))
    //  --score;
    if (rel2 == null) --score;
    else score += toDouble(callF(nodeMatcher, rel, rel2));
  }
  return score;
}
static WebRelation web_getRelationOpt(WebNode a, WebNode b) {
  return a.web.getRelationOpt(a, b);
}
static String beautifyStructure(String s) {
  return structure_addTokenMarkers(s);
}
static String[] drop(int n, String[] a) {
  n = Math.min(n, a.length);
  String[] b = new String[a.length-n];
  System.arraycopy(a, n, b, 0, b.length);
  return b;
}

static Object[] drop(int n, Object[] a) {
  n = Math.min(n, a.length);
  Object[] b = new Object[a.length-n];
  System.arraycopy(a, n, b, 0, b.length);
  return b;
}
public static File mkdirsForFile(File file) {
  File dir = file.getParentFile();
  if (dir != null) // is null if file is in current dir
    dir.mkdirs();
  return file;
}

public static String mkdirsForFile(String path) {
  mkdirsForFile(new File(path));
  return path;
}
static Object callJavaX(String method, Object... args) {
  return callOpt(getJavaX(), method, args);
}
// automagically encode a whole map (keys+values)
static Map htmlencode(Map o) {
  HashMap bla = new HashMap();
  for (Object key : keys(o)) {
    Object value = o.get(key);
    bla.put(htmlencode(key), htmlencode(value));
  }
  return bla;
}

static String htmlencode(Object o) {
  return htmlencode(string(o));
}

static String htmlencode(String s) {
  if (s == null) return "";
  StringBuilder out = new StringBuilder(Math.max(16, s.length()));
  for (int i = 0; i < s.length(); i++) {
      char c = s.charAt(i);
      /*if (c >= 0x100)
        out.append("&#x").append(charToHex(c)).append(';');
      else*/ if (c > 127 || c == '"' || c == '<' || c == '>' || c == '&') {
          out.append("&#");
          out.append((int) c);
          out.append(';');
      } else
          out.append(c);
  }
  return out.toString();
}
static boolean web_tripelizable(Web web) {
  return ai_webToTriple(web) != null;
}
static void copyStream(InputStream in, OutputStream out) { try {
  byte[] buf = new byte[65536];
  while (true) {
    int n = in.read(buf);
    if (n <= 0) return;
    out.write(buf, 0, n);
  }
} catch (Exception __e) { throw rethrow(__e); } }
static String adjectiveToAdverb(String s) {
  s = replaceSuffixIC("y", "i", s);
  s = replaceSuffixIC("c", "cal", s);
  s = dropSuffixIC("le", s);
  return s + "ly";
}
static String struct_noStringSharing(Object o) {
  structure_Data d = new structure_Data();
  d.noStringSharing = true;
  return structure(o, d);
}
static MultiMap<String, WebNode> ai_indexWebs(Collection<Web> webs) {
  if (hasMultiCore() && l(webs) >= 2000) return ai_indexWebs_parallel2(webs);
  return ai_indexWebs_serial(webs);
}
    
static MultiMap<String, WebNode> ai_indexWebs_serial(Collection<Web> webs) {
  MultiMap<String, WebNode> index = caseInsensitiveMultiMap();
  for (Web web : unnull(webs))
    ai_addWebToIndex(index, web);
  return index;
}
static String getComputerID_quick() {
  return computerID();
}
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 boolean isGlobalID(String s) {
  return possibleGlobalID(s);
}
static <A> List<A> emptyListWithCapacity(Collection c) {
  return new ArrayList(l(c));
}

static <A> List<A> emptyListWithCapacity(int n) {
  return new ArrayList(n);
}
static <A> LinkedHashMap<A, A> twoListsToOrderedMap(List<A> l1, List<A> l2) {
  if (l(l1) != l(l2)) throw fail("Differing list size: " + l(l1) + "/" + l(l2));
  LinkedHashMap<A, A> map = new LinkedHashMap();
  int n = l(l1);
  for (int i = 0; i < n; i++)
    map.put(l1.get(i), l2.get(i));
  return map;
}
static void ai_onNewOrRemovedWeb(Object onNewWeb, Object onRemovedWeb) {
  ai_onNewWeb(onNewWeb);
  if (onRemovedWeb != null)
    onTransientWebRemoved_do(onRemovedWeb);
}
static Set asSet(Object[] array) {
  HashSet set = new HashSet();
  for (Object o : array)
    if (o != null)
      set.add(o);
  return set;
}

static Set<String> asSet(String[] array) {
  TreeSet<String> set = new TreeSet();
  for (String o : array)
    if (o != null)
      set.add(o);
  return set;
}

static <A> Set<A> asSet(Collection<A> l) {
  if (l instanceof Set) return (Set) l;
  HashSet<A> set = new HashSet();
  for (A o : l)
    if (o != null)
      set.add(o);
  return set;
}
static BufferedReader utf8bufferedReader(InputStream in) { try {
  return new BufferedReader(new InputStreamReader(in, "UTF-8"));
} catch (Exception __e) { throw rethrow(__e); } }

static BufferedReader utf8bufferedReader(File f) { try {
  return utf8bufferedReader(newFileInputStream(f));
} catch (Exception __e) { throw rethrow(__e); } }
static boolean sameSnippetID(String a, String b) {
  if (!isSnippetID(a) || !isSnippetID(b)) return false;
  return parseSnippetID(a) == parseSnippetID(b);
}
// returns true if web contains nodes that are too long
static boolean web_nodesTooLong(Web web) {
  if (web == null) return false;
  int max = ai_maxNodeTextLength();
  for (WebNode n : web_nodesAndRelations(web))
    for (String s : web_texts(n))
      if (l(s) > max)
        return true;
  return false;
}
static Cache<Set<String>> knownPluralNouns_cache = new Cache("knownPluralNouns_load");
static Set<String> knownPluralNouns() { return knownPluralNouns_cache.get(); }

static Set<String> knownPluralNouns_load() {
  return new TreeSet(map("plural",knownNouns()));
}

static boolean web_hasLabel(WebNode node, String text) {
  return web_hasLabelIC(node, text);
}

static boolean web_hasLabel(Pair<WebNode, WebNode> p, String text) {
  return web_hasLabelIC(p, text);
}

// test relation
static boolean web_hasLabel(WebNode a, WebNode b, String text) {
  return web_hasLabelIC(a, b, text);
}

static boolean web_matchNodeLists_debug;

static int web_matchNodeLists(List<WebNode> l1, List<WebNode> l2) {
  if (l(l1) != l(l2)) return -1000;
  int score = 0;
  if (empty(l1)) return score;
  HashMap<WebNode, WebNode> map = twoListsToHashMap(l1, l2);
  Web web = first(l1).web, web2 = first(l2).web;
  
  // Score nodes
  for (int i = 0; i < l(l1); i++) {
    WebNode a = l1.get(i), b = l2.get(i);
    if (web_isAnythingVariable(a)) continue;
    //if (neq(web_labels(a), web_labels(b))) --score;
    if (!containsAllIC_lisp(web_labels(b), web_labels(a))) --score;
  }
  
  // Score relations
  for (WebRelation rel : web_relationObjects(web)) {
    WebNode aa = map.get(rel.a), bb = map.get(rel.b);
    if (aa == null || bb == null) continue;
    WebRelation rel2 = web2.getRelationOpt(aa, bb);
    if (web_matchNodeLists_debug)
      print("a=" + rel.a + ", b=" + rel.b + ", aa=" + aa + ", bb=" + bb + ", rel2 labels=" + (rel2 == null ? "-" : str(web_labels(rel2)) + ", labels=" + rel.labels));
    if (rel2 == null || !containsAllIC_lisp(web_labels(rel2), web_labels(rel)))
      --score;
  }
  return score;
}
static class FileStatus {
  long time, size;
  boolean isDir;
  public boolean equals(Object o) { return stdEq2(this, o); }
public int hashCode() { return stdHash2(this); }
}

static FileStatus fileStatus(String path) {
  return fileStatus(newFile(path));
}

static FileStatus fileStatus(File f) {
  return nu(FileStatus.class, "time" , f.lastModified(), "size" , fileLength(f),
    "isDir" , f.isDirectory());
}
static Cache<Set<String>> knownNouns_cache = new Cache("knownNouns_load");
static Set<String> knownNouns() { return knownNouns_cache.get(); }

static Set<String> knownNouns_load() {
  return new TreeSet(tlft(loadSnippet("#1011082")));
}

static long round(double d) {
  return Math.round(d);
}


static void ai_onNewWeb(Object onNewWeb) {
  if (onNewWeb == null) return;
  setAdd(postSoftwareMadeWeb_onNewWeb, onNewWeb);
  onTransientWebAdded_do(onNewWeb);
}
static String dropSuffixIC(String suffix, String s) {
  return s == null ? null : ewic(s, suffix) ? s.substring(0, l(s)-l(suffix)) : s;
}
static byte[] hexToBytes(String s) {
  int n = l(s) / 2;
  byte[] bytes = new byte[n];
  for (int i = 0; i < n; i++) {
    String hex = substring(s, i*2, i*2+2);
    try {
      bytes[i] = (byte) parseHexByte(hex);
    } catch (Throwable _e) {
      throw fail("Bad hex byte: " + quote(hex) + " at " + i*2 + "/" + l(s));
    }
  }
  return bytes;
}
static HashMap<Class,Constructor> nuEmptyObject_cache = new HashMap();

static <A> A nuEmptyObject(Class<A> c) { try {
  Constructor ctr;
  
  synchronized(nuEmptyObject_cache) {
    ctr = nuEmptyObject_cache.get(c);
    if (ctr == null) {
      nuEmptyObject_cache.put(c, ctr = nuEmptyObject_findConstructor(c));
      ctr.setAccessible(true);
    }
  }

  return (A) ctr.newInstance();
} catch (Exception __e) { throw rethrow(__e); } }

static Constructor nuEmptyObject_findConstructor(Class c) {
  for (Constructor m : c.getDeclaredConstructors())
    if (m.getParameterTypes().length == 0)
      return m;
  throw fail("No default constructor declared in " + c.getName());
}

static Object cget(Object c, String field) {
  Object o = getOpt(c, field);
  if (o instanceof Concept.Ref) return ((Concept.Ref) o).get();
  return o;
}
static boolean structure_isMarker(String s, int i, int j) {
  if (i >= j) return false;
  if (s.charAt(i) != 'm') return false;
  ++i;
  while (i < j) {
    char c = s.charAt(i);
    if (c < '0' || c > '9') return false;
    ++i;
  }
  return true;
}
static String readLineFromReaderWithClose(BufferedReader r) { try {
  String s = r.readLine();
  if (s == null) r.close();
  return s;
} catch (Exception __e) { throw rethrow(__e); } }
static long getLastTiming() {
  ThreadLocal<Long> tl = (ThreadLocal<Long>) ( getOpt(getMainClass(), "saveTiming_last"));
  if (tl == null) return -1;
  Long l = tl.get();
  return l == null ? -1 : l;
}
static boolean jmatch(String pat, String s) {
  return jmatch(pat, s, null);
}

static boolean jmatch(String pat, String s, Matches matches) {
  if (s == null) return false;
  return jmatch(pat, javaTok(s), matches);
}

static boolean jmatch(String pat, List<String> toks) {
  return jmatch(pat, toks, null);
}

static boolean jmatch(String pat, List<String> toks, Matches matches) {
  List<String> tokpat = javaTok(pat);
  String[] m = match2(tokpat, toks);
  //print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m));
  if (m == null)
    return false;
  else {
    if (matches != null) matches.m = m;
    return true;
  }
}
static boolean containsAllIC_lisp(Collection<Lisp> a, Collection<Lisp> b) {
  for (Lisp o : b)
    if (!containsLispIC(a, o))
      return false;
  return true;
}
static boolean web_hasLabelIC(WebNode node, String text) {
  if (node != null) for (Lisp l : node.labels())
    if (eqic(text, node.web.unparseLabel(l)))
      return true;
  return false;
}

static boolean web_hasLabelIC(Pair<WebNode, WebNode> p, String text) {
  return web_hasLabelIC(p.a.web.relation(p), text);
}

static boolean web_hasLabelIC(WebNode a, WebNode b, String text) {
  return web_hasLabelIC(a.web.relation(a, b), text);
}

static void setOptAll(Object o, Map<String, Object> fields) {
  if (fields == null) return;
  for (String field : keys(fields))
    setOpt(o, field, fields.get(field));
}

static void setOptAll(Object o, Object... values) {
  //values = expandParams(c.getClass(), values);
  warnIfOddCount(values);
  for (int i = 0; i+1 < l(values); i += 2) {
    String field = (String) values[i];
    Object value = values[i+1];
    setOpt(o, field, value);
  }
}
static Producer<String> javaTokC_noMLS_onReader(final BufferedReader r) {
  final class X implements Producer<String> {
    StringBuilder buf = new StringBuilder(); // stores from "i"
    char c, d, e = 'x'; // just not '\0'
    
    X() {
      // fill c, d and e
      nc();
      nc();
      nc();
    }
    
    // get next character(s) into c, d and e
    void nc() { try {
      c = d;
      d = e;
      if (e == '\0') return;
      int i = r.read();
      e = i < 0 ? '\0' : (char) i;
    } catch (Exception __e) { throw rethrow(__e); } }
    
    void ncSave() {
      if (c != '\0') {
        buf.append(c);
        nc();
      }
    }
    
    public String next() {
      // scan for whitespace
      while (c != '\0') {
        if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
          nc();
        else if (c == '/' && d == '*') {
          do nc(); while (c != '\0' && !(c == '*' && d == '/'));
          nc(); nc();
        } else if (c == '/' && d == '/') {
          do nc(); while (c != '\0' && "\r\n".indexOf(c) < 0);
        } else
          break;
      }
      
      if (c == '\0') return null;

      // scan for non-whitespace
      if (c == '\'' || c == '"') {
        char opener = c;
        ncSave();
        while (c != '\0') {
          if (c == opener || c == '\n') { // end at \n to not propagate unclosed string literal errors
            ncSave();
            break;
          } else if (c == '\\') {
            ncSave();
            ncSave();
          } else
            ncSave();
        }
      } else if (Character.isJavaIdentifierStart(c))
        do ncSave(); while (Character.isJavaIdentifierPart(c) || c == '\''); // for stuff like "don't"
      else if (Character.isDigit(c)) {
        do ncSave(); while (Character.isDigit(c));
        if (c == 'L') ncSave(); // Long constants like 1L
      } else
        ncSave();
        
      String t = buf.toString();
      buf.setLength(0);
      return t;
    }
  }
  
  return new X();
}

static <A> HashMap<A, A> twoListsToHashMap(List<A> l1, List<A> l2) {
  if (l(l1) != l(l2)) throw fail("Differing list size: " + l(l1) + "/" + l(l2));
  HashMap<A, A> map = new HashMap();
  int n = l(l1);
  for (int i = 0; i < n; i++)
    map.put(l1.get(i), l2.get(i));
  return map;
}

static <A> HashMap<A, A> twoListsToHashMap(A[] l1, A[] l2) {
  return twoListsToHashMap(Arrays.asList(l1), Arrays.asList(l2));
}
static BufferedReader bufferedReader(Reader r) {
  return r instanceof BufferedReader ? (BufferedReader) r : new BufferedReader(r);
}
static boolean stdEq2(Object a, Object b) {
  if (a == null) return b == null;
  if (b == null) return false;
  if (a.getClass() != b.getClass()) return false;
  for (String field : allFields(a))
    if (neq(getOpt(a, field), getOpt(b, field)))
      return false;
  return true;
}
static boolean isLongConstant(String s) {
  if (!s.endsWith("L")) return false;
  s = s.substring(0, l(s)-1);
  return isInteger(s);
}
static int ai_maxNodeTextLength = 1000;

static int ai_maxNodeTextLength() {
  return ai_maxNodeTextLength;
}
static boolean equals(Object a, Object b) {
  return a == null ? b == null : a.equals(b);
}
static int[] toIntArray(List<Integer> l) {
  int[] a = new int[l(l)];
  for (int i = 0; i < a.length; i++)
    a[i] = l.get(i);
  return a;
}
static boolean hasMultiCore() {
  return isMultiCore();
}
// f: func -> A (stream ends when f returns null)
static <A> IterableIterator<A> iteratorFromFunction(final Object f) {
  class IFF extends IterableIterator<A> {
    A a;
    boolean done;
    
    public boolean hasNext() {
      getNext();
      return !done;
    }
    
    public A next() {
      getNext();
      if (done) throw fail();
      A _a = a;
      a = null;
      return _a;
    }
    
    void getNext() {
      if (done || a != null) return;
      a = (A) callF(f);
      done = a == null;
    }
  };
  return new IFF();
}

// optimized version for F0 argument
static <A> IterableIterator<A> iteratorFromFunction(final F0<A> f) {
  class IFF2 extends IterableIterator<A> {
    A a;
    boolean done;
    
    public boolean hasNext() {
      getNext();
      return !done;
    }
    
    public A next() {
      getNext();
      if (done) throw fail();
      A _a = a;
      a = null;
      return _a;
    }
    
    void getNext() {
      if (done || a != null) return;
      a = f.get();
      done = a == null;
    }
  };
  return new IFF2();
}
static Object deref(Object o) {
  if (o instanceof Derefable) o = ((Derefable) o).get();
  return o;
}

static int stdHash2(Object a) {
  if (a == null) return 0;
  return stdHash(a, toStringArray(allFields(a)));
}
static double parseDouble(String s) {
  return Double.parseDouble(s);
}
// This is for main classes that are all static.
// (We don't go to base classes.)
static Set<String> listFields(Object c) {
  TreeSet<String> fields = new TreeSet();
  for (Field f : _getClass(c).getDeclaredFields())
    fields.add(f.getName());
  return fields;
}
static double toDouble(Object o) {
  if (o instanceof Number)
    return ((Number) o).doubleValue();
  if (o instanceof BigInteger)
    return ((BigInteger) o).doubleValue();
  throw fail(o);
}
static boolean setOptAllDyn_debug;

static void setOptAllDyn(DynamicObject o, Map<String, Object> fields) {
  if (fields == null) return;
  for (String field : keys(fields)) {
    Object val = fields.get(field);
    boolean has = hasField(o, field);
    if (has)
      setOpt(o, field, val);
    else {
      o.fieldValues.put(intern(field), val);
      if (setOptAllDyn_debug) print("setOptAllDyn added dyn " + field + " to " + o + " [value: " + val + ", fieldValues = " + systemHashCode(o.fieldValues) + ", " + struct(keys(o.fieldValues)) + "]");
    }
  }
}
static List collectField(Collection c, String field) {
  List l = new ArrayList();
  for (Object a : c)
    l.add(getOpt(a, field));
  return l;
}
static List<File> earlierPartsOfLogFile(File file) {
  String name = file.getName() + ".part";
  try {
    Matches m = new Matches();
    TreeMap<Integer,File> map = new TreeMap();
    for (File p : listFiles(file.getParent())) try {
      String n = p.getName();
      if (startsWith(n, name, m))
        map.put(parseFirstInt(m.rest()), p);
    } catch (Throwable __e) { printStackTrace2(__e); }
    return valuesList(map);
  } catch (Throwable e) { printException(e);
    return ll();
  }
}
static String structure_addTokenMarkers(String s) {
  List<String> tok = javaTok(s);
  
  // find references
  
  TreeSet<Integer> refs = new TreeSet();
  for (int i = 1; i < l(tok); i += 2) {
    String t = tok.get(i);
    if (t.startsWith("t") && isInteger(t.substring(1)))
      refs.add(parseInt(t.substring(1)));
  }
  
  if (empty(refs)) return s;
  
  // add markers
  for (int i : refs) {
    int idx = i*2+1;
    String t = "";
    if (endsWithLetterOrDigit(tok.get(idx-1))) t = " ";
    tok.set(idx, t + "m" + i + " " + tok.get(idx));
  }
  
  return join(tok);
}
static String classNameToVM(String name) {
  return name.replace(".", "$");
}
static Producer<String> javaTokC_noMLS_iterator(final String s) {
  return new Producer<String>() {
    final int l = s.length();
    int i = 0;
    
    public String next() {
      if (i >= l) return null;
      
      int j = i;
      char c, d;
      
      // scan for whitespace
      while (j < l) {
        c = s.charAt(j);
        d = j+1 >= l ? '\0' : s.charAt(j+1);
        if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
          ++j;
        else if (c == '/' && d == '*') {
          do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
          j = Math.min(j+2, l);
        } else if (c == '/' && d == '/') {
          do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
        } else
          break;
      }
      
      i = j;
      if (i >= l) return null;
      c = s.charAt(i);
      d = i+1 >= l ? '\0' : s.charAt(i+1);
  
      // scan for non-whitespace
      if (c == '\'' || c == '"') {
        char opener = c;
        ++j;
        while (j < l) {
          if (s.charAt(j) == opener || s.charAt(j) == '\n') { // end at \n to not propagate unclosed string literal errors
            ++j;
            break;
          } else if (s.charAt(j) == '\\' && j+1 < l)
            j += 2;
          else
            ++j;
        }
      } else if (Character.isJavaIdentifierStart(c))
        do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't"
      else if (Character.isDigit(c)) {
        do ++j; while (j < l && Character.isDigit(s.charAt(j)));
        if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
      } else
        ++j;
        
      String t = quickSubstring(s, i, j);
      i = j;
      return t;
    }
  };
}

static boolean warn_on = true;

static void warn(String s) {
  if (warn_on)
    print("Warning: " + s);
}

static void warn(String s, List<String> warnings) {
  warn(s);
  if (warnings != null)
    warnings.add(s);
}
static String plural(String s) {
  return getPlural(s);
}
static String string(Object o) {
  return String.valueOf(o);
}
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 List<Web> concatWebLists(Collection<Web>... lists) {
  return uniqueByField_favorLast(concatLists(lists), "globalID");
}
static <A> A nuStubInnerObject(Class<A> c) { try {
  Class outerType = getOuterClass(c);
  Constructor m = c.getDeclaredConstructor(outerType);
  m.setAccessible(true);
  return (A) m.newInstance(new Object[] {null});
} catch (Exception __e) { throw rethrow(__e); } }
static List<String> splitAtSpace(String s) {
  return asList(s.split("\\s+"));
}
static boolean allWebs_transientOnly;

static List<Web> allWebs_fullList() {
  if (allWebs_transientOnly) return transientWebs();
  return concatLists(transientWebs(), downloadedWebs(), localSoftwareMadeWebs(), localUserWebEdits(), localWebs());
}
static MultiMap<String, WebNode> ai_indexWebs_parallel2(Collection<Web> webs) { try {
  print("Making index in parallel.");
  
  long _startTime_300 = sysNow(); 
    List<MultiMap<String, WebNode>> indexes = parallelMap(chunksOf1000(webs), "ai_indexWebs_serial");
   done2("Parallel indexing", _startTime_300); 
  
  long _startTime_301 = sysNow(); 
    MultiMap<String, WebNode> index = caseInsensitiveMultiMap();
    for (MultiMap<String, WebNode> i : indexes)
      index.putAll(i);
   done2("Tying up", _startTime_301); 

  return index;
} catch (Exception __e) { throw rethrow(__e); } }

static FileInputStream newFileInputStream(File path) throws IOException {
  return newFileInputStream(path.getPath());
}

static FileInputStream newFileInputStream(String path) throws IOException {
  FileInputStream f = new // Line break for ancient translator
    FileInputStream(path);
  //callJavaX("registerIO", f, path, true);
  return f;
}
static boolean[] boolArrayFromBytes(byte[] a, int n) {
  boolean[] b = new boolean[n];
  int m = min(n, l(a)*8);
  for (int i = 0; i < m; i++)
    b[i] = (a[i/8] & 1 << (i & 7)) != 0;
  return b;
}
static HashMap<String,Class> findClass_cache = new HashMap();

// currently finds only inner classes of class "main"
// returns null on not found
// this is the simple version that is not case-tolerant
static Class findClass(String name) {
  synchronized(findClass_cache) {
    if (findClass_cache.containsKey(name))
      return findClass_cache.get(name);
      
    if (!isJavaIdentifier(name)) return null;
    
    Class c;
    try {
      c = Class.forName("main$" + name);
    } catch (ClassNotFoundException e) {
      c = null;
    }
    findClass_cache.put(name, c);
    return c;
  }
}
static String shortenClassName(String name) {
  if (name == null) return null;
  int i = lastIndexOf(name, "$");
  if (i < 0) i = lastIndexOf(name, ".");
  return i < 0 ? name : substring(name, i+1);
}
static float parseFloat(String s) {
  return Float.parseFloat(s);
}
static String replaceSuffixIC(String a, String b, String s) {
  if (endsWithIC(s, a)) return dropLast(s, l(a)) + b;
  return s;
}
static String internIfLongerThan(String s, int l) {
  return s == null ? null : l(s) >= l ? intern(s) : s;
}
static void pcallOpt_noArgs(Object o, String method) {
  try { callOpt_noArgs(o, method); } catch (Throwable __e) { printStackTrace2(__e); }
}
static boolean web_hasText(WebNode node, String text) {
  return web_hasLabelIC(node, text);
}

static boolean web_hasText(Pair<WebNode, WebNode> p, String text) {
  return web_hasLabelIC(p, text);
}

// test relation
static boolean web_hasText(WebNode a, WebNode b, String text) {
  return web_hasLabelIC(a, b, text);
}

static <A> MultiMap<String, A> caseInsensitiveMultiMap() {
  MultiMap<String,A> mm = new MultiMap();
  mm.data = caseInsensitiveMap();
  return mm;
}
static Class<?> getClass(String name) {
  try {
    return Class.forName(name);
  } catch (ClassNotFoundException e) {
    return null;
  }
}

static Class getClass(Object o) {
  return o instanceof Class ? (Class) o : o.getClass();
}

static Class getClass(Object realm, String name) { try {
  try {
    return getClass(realm).getClassLoader().loadClass(classNameToVM(name));
  } catch (ClassNotFoundException e) {
    return null;
  }
} catch (Exception __e) { throw rethrow(__e); } }
static void swapElements(List l, int i, int j) {
  if (i == j) return;
  Object o = l.get(i);
  l.set(i, l.get(j));
  l.set(j, o);
}
static void printStructure(String prefix, Object o) {
  if (endsWithLetter(prefix)) prefix += ": ";
  print(prefix + structureForUser(o));
}

static void printStructure(Object o) {
  print(structureForUser(o));
}

static List<String> tlft_j(String text) {
  return toLinesFullTrim_java(text);
}
static List<Lisp> web_labels(WebNode n) {
  return n == null ? null : n.labels();
}
static boolean match(String pat, String s) {
  return match3(pat, s);
}

static boolean match(String pat, String s, Matches matches) {
  return match3(pat, s, matches);
}

static boolean web_isAnythingVariable(WebNode n) {
  return endsWithIC(web_text(n), " [Anything]");
}


static <A extends Collection<? extends Collection>> List parallelMap(A l, final Object f) { try {
  return parallelMap(iterator((Collection) l), f);
} catch (Exception __e) { throw rethrow(__e); } }

static List parallelMap(Iterator<? extends Collection> it, final Object f) { try {
  final List < Pair < Object , Integer > > out = new ArrayList();
    
  int poolSize = numberOfCores(), queueSize = 10;
  NotifyingBlockingThreadPoolExecutor e = new NotifyingBlockingThreadPoolExecutor(poolSize, queueSize, 15, TimeUnit.SECONDS);
    
  try {
    int i = 0;
    for (final Object o : iterable(it)) {
      ++i;
      final int _i = i;
      e.execute(new Runnable() { public void run() { try {  try {
        Object x = callF(f, o);
        synchronized(out) {
          out.add(pair(x, _i));
        }
      } catch (Throwable __e) { printStackTrace2(__e); }
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "pcall {\r\n        Object x = callF(f, o);\r\n        synchronized(out) {\r\n      ..."; }});
    }
    e.shutdown();
    e.awaitTermination(1, TimeUnit.DAYS);
  } finally {
    e.shutdown();
  }
    
  return firstOfPairs(sortBySecondOfPairs_inPlace(out));
} catch (Exception __e) { throw rethrow(__e); } }
static boolean isMultiCore() {
  return numberOfCores() > 1;
}
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<String> pat, List<String> tok) {
  // standard case (no ...)
  int i = pat.indexOf("...");
  if (i < 0) return match2_match(pat, tok);
  
  pat = new ArrayList<String>(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<String> pat, List<String> tok) {
  List<String> result = new ArrayList<String>();
  if (pat.size() != tok.size()) {
    
    return null;
  }
  for (int i = 1; i < pat.size(); i += 2) {
    String p = pat.get(i), t = tok.get(i);
    
    if (eq(p, "*"))
      result.add(t);
    else if (!equalsIgnoreCase(unquote(p), unquote(t))) // bold change - match quoted and unquoted now
      return null;
  }
  return result.toArray(new String[result.size()]);
}

static boolean match3(String pat, String s) {
  return match3(pat, s, null);
}

static boolean match3(String pat, String s, Matches matches) {
  if (pat == null || s == null) return false;
  return match3(pat, parse3_cached(s), matches);
}
  
static boolean match3(String pat, List<String> toks, Matches matches) {
  List<String> tokpat = parse3(pat);
  return match3(tokpat,toks,matches);
}

static boolean match3(List<String> tokpat, List<String> toks, Matches matches) {
  String[] m = match2(tokpat, toks);
  //print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m));
  if (m == null) return false;
  if (matches != null) matches.m = m; return true;
}
static List<Web> transientWebs_list = synchroList();

static List<Web> transientWebs() {
  return transientWebs_list;
}
static Set<String> allFields(Object o) {
  TreeSet<String> 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 Class getOuterClass(Class c) { try {
  String s = c.getName();
  int i = s.lastIndexOf('$');
  return Class.forName(substring(s, 0, i));
} catch (Exception __e) { throw rethrow(__e); } }
static boolean endsWithIC(String a, String b) {
  return endsWithIgnoreCase(a, b);
}
static <A> List<List<A>> chunksOf1000(Collection<A> c) {
  List < List < A > > out = new ArrayList();
  Iterator<A> it = iterator(c);
  List<A> l = new ArrayList(1000);
  while (it.hasNext()) {
    l.add(it.next());
    if (l(l) >= 1000) {
      out.add(l);
      l = new ArrayList(1000);
    }
  }
  if (nempty(l)) out.add(l);
  return out;
}
static boolean endsWithLetter(String s) {
  return nempty(s) && isLetter(last(s));
}
static boolean endsWithLetterOrDigit(String s) {
  return nempty(s) && isLetterOrDigit(lastCharacter(s));
}
static List<Web> localUserWebEdits() {
  List<Web> webs = new ArrayList();
  for (String progID : websPostedIn()) try {
    if (neq(progID, "#1009961"))
      for (String s : scanLog(progID, "user-web-edits")) try {
        webs.add(parseWeb(trim(afterSpace(s))));
      } catch (Throwable __e) { printStackTrace2(__e); }
  } catch (Throwable __e) { printStackTrace2(__e); }
  return webs;
}

static boolean containsLispIC(Collection<Lisp> l, Lisp s) {
  for (Lisp x : l)
    if (lispEqic(x, s))
      return true;
  return false;
}
static List<Web> localWebs() {
  return allWebsFromCircleEditor();
}

static File[] listFiles(File dir) {
  File[] files = dir.listFiles();
  return files == null ? new File[0] : files;
}

static File[] listFiles(String dir) {
  return listFiles(new File(dir));
}
static <A> List<A> uniqueByField_favorLast(Collection<A> l, String field) {
  LinkedHashMap<Object,A> map = new LinkedHashMap();
  if (l != null) for (A a : l)
    map.put(getOpt(a, field), a);
  return asList(values(map));
}
static <A, B> List<B> valuesList(Map<A, B> map) {
  return new ArrayList(values(map));
}
static int parseFirstInt(String s) {
  return parseInt(jextract("<int>", s));
}
static int systemHashCode(Object o) {
  return identityHashCode(o);
}
static int stdHash(Object a, String... fields) {
  if (a == null) return 0;
  int hash = getClassName(a).hashCode();
  for (String field : fields)
    hash = hash*2+hashCode(getOpt(a, field));
  return hash;
}
static int lastIndexOf(String a, String b) {
  return a == null || b == null ? -1 : a.lastIndexOf(b);
}
static List<String> toLinesFullTrim_java(String text) {
  return toLinesFullTrim(joinLines(map("javaDropComments", toLinesFullTrim(text))));
}

static List<Web> localSoftwareMadeWebs() {
  List<Web> webs = new ArrayList();
  for (String progID : localSoftwareMadeWebs_programsToRead()) try {
    webs.addAll(websMadeByProgram(progID));
  } catch (Throwable __e) { printStackTrace2(__e); }
  return webs;
}

static List<Web> downloadedWebs() {
  /*ret concatLists(downloadedTypicalWebs(),
    downloadedWebsFromFileNamed("Webs from program #1011161 on tvejysmllsmz.gz"));*/
  return downloadedTypicalWebs();
}
static WeakHashMap<Class, HashMap<String, Method>> callOpt_noArgs_cache = new WeakHashMap();

static Object callOpt_noArgs(Object o, String method) { try {
  if (o == null) return null;
  if (o instanceof Class)
    return callOpt(o, method); // not optimized
  
  Class c = o.getClass();
  HashMap<String, Method> map;
  synchronized(callOpt_noArgs_cache) {
    map = callOpt_noArgs_cache.get(c);
    if (map == null)
      map = callOpt_noArgs_makeCache(c);
  }

  Method m = map.get(method);
  return m != null ? m.invoke(o) : null;
} catch (Exception __e) { throw rethrow(__e); } }

// used internally - we are in synchronized block
static HashMap<String, Method> callOpt_noArgs_makeCache(Class c) {
  HashMap<String,Method> map = new HashMap();
  Class _c = c;
  do {
    for (Method m : c.getDeclaredMethods())
      if (m.getParameterTypes().length == 0) {
        m.setAccessible(true);
        String name = m.getName();
        if (!map.containsKey(name))
          map.put(name, m);
      }
    _c = _c.getSuperclass();
  } while (_c != null);
  callOpt_noArgs_cache.put(c, map);
  return map;
}
static <A> TreeMap<String, A> caseInsensitiveMap() {
  return new TreeMap(String.CASE_INSENSITIVE_ORDER);
}
static int parseHexByte(String s) {
  return Integer.parseInt(s, 16);
}


static String javaDropComments(String s) {
  List<String> tok = javaTok(s);
  replaceLastElement(tok, "");
  return join(tok);
}
static int hashCode(Object a) {
  return a == null ? 0 : a.hashCode();
}
static int identityHashCode(Object o) {
  return System.identityHashCode(o);
}
static String parse3_cached_s;
static List<String> parse3_cached_l;

  static synchronized List<String> parse3_cached(String s) {
    if (neq(s, parse3_cached_s))
      parse3_cached_l = parse3(parse3_cached_s = s);
    return parse3_cached_l;
  }
static Web parseWeb(String s) {
  Object o = unstructure(s);
  if (o instanceof CirclesAndLines) return calToWeb((CirclesAndLines) o);
  return (Web) o;
}
static boolean lispEqic(Lisp a, Lisp b) {
  if (a == null) return b == null;
  if (neqic(a.head, b.head)) return false;
  int n = l(a.args);
  if (n != l(b.args)) return false;
  for (int i = 0; i < n; i++)
    if (!lispEqic(a.args.get(i), b.args.get(i))) return false;
  return true;
}

static boolean lispEqic(Lisp a, String b) {
  if (a == null) return b == null;
  return nempty(a.args) && eqic(a.head, b);
}

static volatile int numberOfCores_value;

static int numberOfCores() {
  if (numberOfCores_value == 0)
    numberOfCores_value = Runtime.getRuntime().availableProcessors();
  return numberOfCores_value;
}
static boolean isLetterOrDigit(char c) {
  return Character.isLetterOrDigit(c);
}
// returns from C to C
static String jextract(String pat, String s) {
  List<String> tok = javaTok(s);
  List<String> tokpat = javaTok(pat);
  jfind_preprocess(tokpat);
  int i = jfind(tok, tokpat);
  if (i < 0) return null;
  int j = i + l(tokpat) - 2;
  return join(subList(tok, i, j));
}

static <A, B> List<A> firstOfPairs(Collection<Pair<A, B>> l) {
  List<A> out = new ArrayList();
  for (Pair<A, B> p : unnull(l)) out.add(firstOfPair(p));
  return out;
}
static <A> Iterable<A> iterable(final Iterator<A> i) {
  return new Iterable<A>() {
    public Iterator<A> iterator() {
      return i;
    }
  };
}
static List<String> localSoftwareMadeWebs_programsToRead() {
  return ll("#1010745");
  //ret websPostedIn();
}

static boolean equalsIgnoreCase(String a, String b) {
  return a == null ? b == null : a.equalsIgnoreCase(b);
}

static boolean equalsIgnoreCase(char a, char b) {
  if (a == b) return true;
  char u1 = Character.toUpperCase(a);
  char u2 = Character.toUpperCase(b);
  if (u1 == u2) return true;
  return Character.toLowerCase(u1) == Character.toLowerCase(u2);
}
static List<String> websPostedIn() {
  Matches m = new Matches();
  List<String> programsToScan = new ArrayList();
  for (String s : listFileNames(getProgramDir(circlesEditorDBID()))) try {
    if (swic(s, "webs.posted.at.", m))
      programsToScan.add(fsI(m.unq(0)));
  } catch (Throwable __e) { printStackTrace2(__e); }
  return programsToScan;
}

static List<Web> allWebsFromCircleEditor() {
  return map_pcall(new F1<Drawing, Object>() { Object get(Drawing d) { try { return 
    web_setSource("local", calToWeb(d.cal()))
  ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "web_setSource(\"local\", calToWeb(d.cal()))"; }}, reversed(list(new Concepts(circlesEditorDBID()).load(), Drawing.class)));
}

static <A, B> List<Pair<A, B>> sortBySecondOfPairs_inPlace(List<Pair<A, B>> l) {
  sort(l, new Comparator<Pair<A, B>>() {
    public int compare(Pair<A, B> a, Pair<A, B> b) {
      return stdcompare(a.b, b.b);
    }
  });
  return l;
}
static String joinLines(List<String> lines) {
  return fromLines(lines);
}

static String joinLines(String glue, String text) {
  return join(glue, toLines(text));
}
static /*cached*/ List<Web> downloadedTypicalWebs() {
  List<Web> webs = new ArrayList();
  long _startTime_366 = sysNow(); 
    for (String struct : downloadedTypicalDiagramStructures()) try {
      Web web = web_flexUnstructure(struct);
      webs.add(web_setSource("downloaded typical", web));
    } catch (Throwable __e) { print(exceptionToStringShort(__e)); }
   done2("Unpacking webs", _startTime_366); 
  return webs;
}
static Field findField2(Object o, String field) {
  if (o instanceof Class) return findField2_findStaticField((Class) o, field);
  return findField2_findField(o.getClass(), field);
}

static Field findField2_findStaticField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  return null;
}

static Field findField2_findField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field))
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  return null;
}
static List<Web> websMadeByProgram(String progID) {
  return websMadeByProgram(progID, null);
}

static List<Web> websMadeByProgram(String progID, Map<Web, SoftwareMadeWeb> furtherInfoMap) {
  if (hasMultipleCores()) return websMadeByProgram_parallel(progID, furtherInfoMap);
  
  List<Web> webs = ai_triplesMadeByProgram(progID);
  ai_loadWebsFromFile(programFile(progID, "webs-made.txt"), progID, webs, furtherInfoMap);
  return webs;
}

  static List<String> parse3(String s) {
    return dropPunctuation(javaTokPlusPeriod(s));
  }
static char lastCharacter(String s) {
  return empty(s) ? 0 : s.charAt(l(s)-1);
}
static String afterSpace(String s) {
  return dropUntilSpace(s);
}


static List<Web> ai_loadWebsFromFile(File f, String src) {
  List<Web> webs = new ArrayList();
  ai_loadWebsFromFile(f, src, webs);
  return webs;
}

static void ai_loadWebsFromFile(File f, String src, List<Web> websOut) {
  ai_loadWebsFromFile(f, src, websOut, null);
}

static void ai_loadWebsFromFile(File f, String src, List<Web> websOut, Map<Web, SoftwareMadeWeb> furtherInfoMap) {
  //if (hasMultipleCores()) ai_loadWebsFromFile_parallel(f, src, websOut, furtherInfoMap);
  
  IterableIterator<SoftwareMadeWeb> l = scanLog_unstructure_iterator(f);
  // TODO: clean up file handle in case of error
  for (SoftwareMadeWeb o : l) try {
    Web web = o.web;
    if (web == null)
      web = (Web) unstructure(o.structure);
    if (web != null) {
      websOut.add(web_intern(web_setSourceIfEmpty(web, src)));
      mapPut(furtherInfoMap, web, o);
    }
  } catch (Throwable __e) { printStackTrace2(__e); }
}
static List map_pcall(Iterable l, Object f) {
  return map_pcall(f, l);
}

static List map_pcall(Object f, Iterable l) {
  List x = new ArrayList();
  for (Object o : unnull(l)) try { x.add(callF(f, o)); } catch (Throwable __e) { printStackTrace2(__e); }
  return x;
}
static List<String> downloadedTypicalDiagramStructures() {
  Map<String, String> map = safeUnstructureMap(loadTextFile(getProgramFile("#1010484", "typical.structure")));
  if (map == null) return ll();
  return downloadedDiagramStructures(map(values(map), new F1<String, Object>() { Object get(String md5) { try { return  getProgramFile("#1010484", md5) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "getProgramFile(\"#1010484\", md5)"; }}));
}
static String fsI(String id) {
  return formatSnippetID(id);
}

static String fsI(long id) {
  return formatSnippetID(id);
}
static boolean hasMultipleCores() {
  return isMultiCore();
}
static <T> void sort(T[] a, Comparator<? super T> c) {
  Arrays.sort(a, c);
}

static <T> void sort(T[] a) {
  Arrays.sort(a);
}

static <T> void sort(List<T> a, Comparator<? super T> c) {
  Collections.sort(a, c);
}

static void sort(List a) {
  Collections.sort(a);
}
static List<String> dropPunctuation_keep = litlist("*", "<", ">");

static List<String> dropPunctuation(List<String> tok) {
  tok = new ArrayList<String>(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 <A> List<A> reversed(Collection<A> l) {
  return reversedList(l);
}
static File programFile(String name) {
  return prepareProgramFile(name);
}

static File programFile(String progID, String name) {
  return prepareProgramFile(progID, name);
}
static Web web_setSource(String source, Web web) {
  web.source = source;
  return web;
}
static List<Web> websMadeByProgram_parallel(String progID) {
  return websMadeByProgram_parallel(progID, null);
}

static List<Web> websMadeByProgram_parallel(String progID, final Map<Web, SoftwareMadeWeb> furtherInfoMap) { try {
  // TODO: clean up file handle in case of error
  IterableIterator<String> l = scanLog_iterator(progID, "webs-made.txt");
  final String src = progID;
  final List < Pair < Web , Integer > > webs = new ArrayList();
  
  int poolSize = numberOfCores(), queueSize = 20;
  NotifyingBlockingThreadPoolExecutor e = new NotifyingBlockingThreadPoolExecutor(poolSize, queueSize, 15, TimeUnit.SECONDS);
  
  try {
    int i = 0;
    for (final String s : l) {
      ++i;
      final int _i = i;
      e.execute(new Runnable() { public void run() { try {  try {
        SoftwareMadeWeb o = (SoftwareMadeWeb) ( unstructure(s));
        Web web = o.web;
        if (web == null)
          web = (Web) unstructure(o.structure);
        if (web != null) synchronized(webs) {
          webs.add(pair(web_intern(web_setSourceIfEmpty(web, src)), _i));
          mapPut(furtherInfoMap, web, o);
        }
      } catch (Throwable __e) { printStackTrace2(__e); }
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "pcall {\r\n        SoftwareMadeWeb o = (SoftwareMadeWeb) ( unstructure(s));\r\n  ..."; }});
    }
    //e.await(); // ConcurrentModificationException sometimes??
    e.shutdown();
    e.awaitTermination(1, TimeUnit.DAYS);
  } finally {
    e.shutdown();
  }
  
  long _startTime_392 = sysNow(); 
    sortBySecondOfPairs_inPlace(webs);
   done2("Sort", _startTime_392); 
  
  return concatLists(ai_triplesMadeByProgram(progID), firstOfPairs(webs));
} catch (Exception __e) { throw rethrow(__e); } }
// usually L<S>
static String fromLines(Collection lines) {
  StringBuilder buf = new StringBuilder();
  if (lines != null)
    for (Object line : lines)
      buf.append(str(line)).append('\n');
  return buf.toString();
}

static String fromLines(String... lines) {
  return fromLines(asList(lines));
}
static <A, B> A firstOfPair(Pair<A, B> p) {
  return p == null ? null : p.a;
}
static String dropUntilSpace(String s) {
  if (s == null) return "";
  int i = s.indexOf(' ')+1;
  while (i < l(s) && s.charAt(i) == ' ') ++i;
  return s.substring(i);
}
static List<String> listFileNames(File dir) {
  return map("fileName",listFiles(dir));
}

static List<Web> ai_triplesMadeByProgram(String progID) {
  return webs_readTripleFile(getProgramFile(progID, "triples.gz"));
}
static Web web_flexUnstructure(String struct) {
  if (eq(firstJavaToken(struct), "SoftwareMadeWeb")) {
    struct = getString(safeUnstructure(struct), "structure");
    return (Web) unstructure(struct);
  } else
    return calToWeb(cal_unstructure(struct));
}
static Web calToWeb(CirclesAndLines cal) {
  Web web = new Web();
  web.globalID = cal.globalID;
  web_useCLParse(web, false);
  web.title = cal.title;
  web.created = cal.created;
  HashMap<Circle,WebNode> map = new HashMap();
  for (Circle c : cal.circles) {
    WebNode node = web.newNode(c.text);
    copyFields(c, node, "x", "y");
    node.visInfo = c.quickvis;
    map.put(c, node);
  }
  for (Line l : cal.lines) {
    WebNode rel = web.getRelation(assertNotNull(map.get(l.a)), assertNotNull(map.get(l.b)));
    rel.addLabel(l.text);
  }
  return web;
}
static <A> void replaceLastElement(List<A> l, A a) {
  if (nempty(l))
    l.set(l(l)-1, a);
}


static void copyFields(Object x, Object y, String... fields) {
  if (empty(fields)) { // assume we should copy all fields
    Map<String, Object> map = objectToMap(x);
    for (String field : map.keySet())
      setOpt(y, field, map.get(field));
  } else 
    for (String field : fields) {
      Object o = getOpt(x, field);
      if (o != null)
        setOpt(y, field, o);
    }
}
static Web web_setSourceIfEmpty(Web web, String src) {
  if (web == null) return null;
  if (empty(web.source)) web.source = src;
  return web;
}
static File prepareProgramFile(String name) {
  return mkdirsForFile(getProgramFile(name));
}

static File prepareProgramFile(String progID, String name) {
  return mkdirsForFile(getProgramFile(progID, name));
}
static 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 IterableIterator<String> scanLog_iterator(String progID, String fileName) {
  return scanLog_iterator(getProgramFile(progID, fileName));
}

static IterableIterator<String> scanLog_iterator(String fileName) {
  return scanLog_iterator(getProgramFile(fileName));
}

static IterableIterator<String> scanLog_iterator(File file) {
  // TODO: opens all at once
  final Iterator<String> it = chainIterators(map("linesFromFile",
    concatLists(earlierPartsOfLogFile(file), ll(file))));
  return iteratorFromFunction(new F0<String>() { String get() { try { 
    while (it.hasNext()) {
      String s = it.next();
      if (isProperlyQuoted(s))
        return unquote(s);
    }
    return null;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "while (it.hasNext()) {\r\n      S s = it.next();\r\n      if (isProperlyQuoted(s)..."; }});
}

static Object safeUnstructure(String s) {
  return unstructure(s, true);
}
static List<Web> webs_readTripleFile(File f) {
  if (hasMultipleCores()) return webs_readTripleFile_parallel(f);
  
  if (!f.exists()) return ll();
  Iterator<String> it = linesFromFile(f);
  //L<S> names = (L) unstructure(it.next());
  List<String> names = new ArrayList();
  while (it.hasNext()) {
    String s = trim(it.next());
    if (empty(s)) break;
    names.add(unquote(s));
  }
  
  List<Web> webs = new ArrayList();
  while (it.hasNext()) {
    String s = it.next();
    try {
      addIfNotNull(webs, webs_readTripleFile_line(s, names));
    } catch (Throwable __e) { printStackTrace2(__e); }
  }
  return webs;
}

static Web webs_readTripleFile_line(String s, List<String> names) {
  // S moreInfo = quote(unnull(web.globalID)) + " " + quote(unnull(web.title)) + " " + quote(unnull(web.source)) + " " + (web.unverified ? "u" : "v") + " " + web.created;
  List<String> l = javaTokC(s);
  if (l(l) == 8) {
    Web web = webFromTriple(
      names.get(parseInt(first(l))), 
      names.get(parseInt(second(l))),
      names.get(parseInt(third(l))));
    web.globalID = unquote(l.get(3));
    web.title = unquote(l.get(4));
    web.source = unquote(l.get(5));
    web.unverified = !eq(l.get(6), "v");
    web.created = parseLong(l.get(7));
    return web_intern(web);
  }
  return null;
}
static String fileName(File f) {
  return f == null ? null : f.getName();
}
static <A> List<A> reversedList(Collection<A> l) {
  List<A> x = cloneList(l);
  Collections.reverse(x);
  return x;
}
static IterableIterator scanLog_unstructure_iterator(String progID, String fileName) {
  return scanLog_unstructure_iterator(getProgramFile(progID, fileName));
}

static IterableIterator scanLog_unstructure_iterator(String fileName) {
  return scanLog_unstructure_iterator(getProgramFile(fileName));
}

static IterableIterator scanLog_unstructure_iterator(File file) {
  final Iterator<String> it = scanLog_iterator(file);
  return iteratorFromFunction(new F0<Object>() { Object get() { try { 
    while (it.hasNext()) try {
      return unstructure(it.next());
    } catch (Throwable __e) { printStackTrace2(__e); }
    return null;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "while (it.hasNext()) pcall {\r\n      ret unstructure(it.next());\r\n    }\r\n    n..."; }});
}
static Web web_useCLParse(Web web, boolean b) {
  
    web.useCLParse = b;
  
  return web;
}
static <A, B> void mapPut(Map<A, B> map, A key, B value) {
  if (map != null && key != null && value != null) map.put(key, value);
}
static Map safeUnstructureMap(String s) {
  return (Map) safeUnstructure(s);
}
static List<String> downloadedDiagramStructures() {
  return downloadedDiagramStructures(listFilesOfProgram_notDirs("#1010484"));
}

static List<String> downloadedDiagramStructures(List<File> files) {
  List<String> diagrams = new ArrayList();
  for (File f : files) {
    if (!isPossibleMD5(f.getName()) || !f.isFile()) continue;
    print(f);
    if (fileStartsWith(f, toUtf8("CirclesAndLines"))) {
      print("Diagram!");
      diagrams.add(loadTextFile(f));
    } else if (isGZ(f)) {
      //print("GZipped!");
      String text = loadGZippedTextFile(f);
      if (isQuoted(firstJavaToken(text)))
        diagrams.addAll(scanQuotedLogLines(text));
      else {
        Concepts c = new Concepts().loadGrab(text, true);
        List<String> structures = getFieldOfAllConceptClasses(c, "calStructure");
        print("Got " + n(structures, "diagram"));
        diagrams.addAll(structures);
      }
    } else
      print("Huh?");
  }
  return diagrams;
}
static CirclesAndLines cal_unstructure(String s) {
  return (CirclesAndLines) unstructure(s);
}


static byte[] toUtf8(String s) { try {
  return s.getBytes("UTF-8");
} catch (Exception __e) { throw rethrow(__e); } }
static List<Web> webs_readTripleFile_parallel(File f) { try {
  if (!f.exists()) return ll();
  print("Reading " + f2s(f) + " in " + nCore(numberOfCores()));
  long _startTime_428 = sysNow(); 
    long _startTime_429 = sysNow(); 
      Iterator<String> it = linesFromFile(f);
    
      final List<String> names = new ArrayList();
      while (it.hasNext()) {
        String s = trim(it.next());
        if (empty(s)) break;
        names.add(unquote(s));
      }
     done2("Reading names", _startTime_429); 
    
    final List < Pair < Web , Integer > > webs = new ArrayList();
    
    int poolSize = numberOfCores(), queueSize = 1000;
    NotifyingBlockingThreadPoolExecutor e = new NotifyingBlockingThreadPoolExecutor(poolSize, queueSize, 15, TimeUnit.SECONDS);
    
    try {
      int i = 0;
      for (final String s : iterable(it)) {
        ++i;
        final int _i = i;
        e.execute(new Runnable() { public void run() { try {  try {
          Web web = webs_readTripleFile_line(s, names);
          if (web != null) synchronized(webs) {
            webs.add(pair(web, _i));
          }
        } catch (Throwable __e) { printStackTrace2(__e); }
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "pcall {\r\n          Web web = webs_readTripleFile_line(s, names);\r\n          i..."; }});
      }
      e.shutdown();
      e.awaitTermination(1, TimeUnit.DAYS);
    } finally {
      e.shutdown();
    }
    
    List<Web> result = firstOfPairs(sortBySecondOfPairs_inPlace(webs));
   done2("webs_readTripleFile_parallel", _startTime_428); 
  return result;
} catch (Exception __e) { throw rethrow(__e); } }
static <A> Iterator<A> chainIterators(Collection<Iterator<A>> l) {
  final List<Iterator<A>> _l = new LinkedList(l);
  return iteratorFromFunction(new Object() { Object get() { try { 
    while (nempty(_l)) {
      if (first(_l).hasNext())
        return first(_l).next();
      _l.remove(0);
    }
    return null;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "while (nempty(_l)) {\r\n      if (first(_l).hasNext())\r\n        ret first(_l).n..."; }});
}
static boolean fileStartsWith(File f, byte[] data) {
  return byteArrayEquals(loadBeginningOfBinaryFile(f, l(data)), data);
}
static boolean isPossibleMD5(String s) { return isMD5(s); }
static List<String> scanQuotedLogLines(String text) {
  List<String> l = new ArrayList();
  for (String s : toLines(text))
    if (isProperlyQuoted(s))
      l.add(unquote(s));
  return l;
}
static List<File> listFilesOfProgram_notDirs(String programID) {
  return asList(listFilesOnly(programDir(programID)));
}
static String loadGZippedTextFile(File file) {
  return loadGZTextFile(file);
}
static List getFieldOfAllConceptClasses(Concepts cc, String field) {
  return notNullOnly(c_collect(values(cc.concepts), field));
}
// o is either a map already (string->object) or an arbitrary object,
// in which case its fields are converted into a map.
static Map<String, Object> objectToMap(Object o) { try {
  if (o instanceof Map) return (Map) o;
  
  TreeMap<String,Object> map = new TreeMap();
  Class c = o.getClass();
  while (c != Object.class) {
    Field[] fields = c.getDeclaredFields();
    for (final Field field : fields) {
      if ((field.getModifiers() & Modifier.STATIC) != 0)
        continue;
      field.setAccessible(true);
      final Object value = field.get(o);
      if (value != null)
        map.put(field.getName(), value);
    }
    c = c.getSuperclass();
  }
  return map;
} catch (Exception __e) { throw rethrow(__e); } }

// same for a collection (convert each element)
static List<Map<String, Object>> objectToMap(Collection l) {
  List x = new ArrayList();
  for (Object o : l)
    x.add(objectToMap(o));
  return x;
}
static byte[] isGZ_magic = bytesFromHex("1f8b");

static boolean isGZ(byte[] data) {
  return byteArrayStartsWith(data, isGZ_magic);
}

static boolean isGZ(File f) {
  return fileStartsWith(f, isGZ_magic);
}
static <A> A third(List<A> l) {
  return _get(l, 2);
}


static byte[] bytesFromHex(String s) {
  return hexToBytes(s);
}
static boolean isMD5(String s) {
  return l(s) == 32 && isLowerHexString(s);
}
static String nCore(int i) {
  return n_fancy(i, "core");
}

static String nCore(Collection c) {
  return n_fancy(c, "core");
}
static boolean byteArrayEquals(byte[] a, byte[] b) {
  if (a == null || b == null) return false;
  if (a.length != b.length) return false;
  for (int i = 0; i < b.length; i++)
    if (a[i] != b[i])
      return false;
  return true;
}
static String f2s(File f) {
  return f == null ? null : f.getAbsolutePath();
}
static boolean byteArrayStartsWith(byte[] a, byte[] b) {
  if (a == null || b == null) return false;
  if (a.length < b.length) return false;
  for (int i = 0; i < b.length; i++)
    if (a[i] != b[i])
      return false;
  return true;
}
static <A> List<A> notNullOnly(List<A> l) {
  List<A> out = new ArrayList();
  for (A a : unnull(l)) if (a != null) out.add(a);
  return out;
}

static <A> List<A> notNullOnly(A... l) {
  return notNullOnly(asList(l));
}
static <A> A _get(List<A> l, int idx) {
  return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null;
}

static Object _get(Object o, String field) {
  return get(o, field);
}

static <A> A _get(A[] l, int idx) {
  return idx >= 0 && idx < l(l) ? l[idx] : null;
}
static List c_collect(Collection l, String field) {
  List out = new ArrayList();
  if (l != null) for (Object o : l)
    out.add(cget(o, field));
  return out;
}
static List<File> listFilesOnly(String dir) {
  return listFilesOnly(new File(dir));
}

static List<File> listFilesOnly(File... dirs) {
  return concatMap(rcurry("listFilesWithSuffix", ""), dirs);
}

static byte[] loadBeginningOfBinaryFile(File file, int maxBytes) {
  return loadBinaryFilePart(file, 0, maxBytes);
}
static String loadGZTextFile(File file) { try {
  if (!file.isFile()) return null;
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  InputStream fis = new FileInputStream(file);
  GZIPInputStream gis = new GZIPInputStream(fis);
  try {
    byte[] buffer = new byte[1024];
    int len;
    while((len = gis.read(buffer)) != -1){
        baos.write(buffer, 0, len);
    }
  } finally {
    fis.close();
  }
  baos.close();
  return fromUtf8(baos.toByteArray()); // TODO: use a Reader
} catch (Exception __e) { throw rethrow(__e); } }


static boolean isLowerHexString(String s) {
  for (int i = 0; i < l(s); i++) {
    char c = s.charAt(i);
    if (c >= '0' && c <= '9' || c >= 'a' && c <= 'f') {
      // ok
    } else
      return false;
  }
  return true;
}
static byte[] loadBinaryFilePart(File file, long start, long end) { try {
  RandomAccessFile raf = new RandomAccessFile(file, "r");
  int n = toInt(min(raf.length(), end-start));
  byte[] buffer = new byte[n];
  try {
    raf.seek(start);
    raf.readFully(buffer, 0, n);
    return buffer;
  } finally {
    raf.close();
  }
} catch (Exception __e) { throw rethrow(__e); } }
static String fromUtf8(byte[] bytes) { try {
  return new String(bytes, "UTF-8");
} catch (Exception __e) { throw rethrow(__e); } }
static List<File> listFilesWithSuffix(File dir, String suffix) {
  List<File> l = new ArrayList();
  for (File f : listFiles(dir))
    if (!f.isDirectory() && (empty(suffix) || endsWithIgnoreCase(f.getName(), suffix)))
      l.add(f);
  return l;
}
// f must return a list
static List concatMap(Object f, Iterable l) {
  return concatLists(map(f, l));
}

static List concatMap(Iterable l, Object f) {
  return concatMap(f, l);
}

static List concatMap(Object f, Object[] l) {
  return concatLists(map(f, l));
}
static Object rcurry(final Object f, final Object arg) {
  int n = numberOfFunctionArguments(f);
  if (n == 0) throw fail("function takes no arguments");
  if (n == 1) return new F0() {
    Object get() {
      return callF(f, arg);
    }
  };
  if (n == 2)
    return new F1() {
      Object get(Object a) {
        return callF(f, a, arg);
      }
    };
  throw todo("currying a function with " + n + "arguments");
}


  static RuntimeException todo() {
    throw new RuntimeException("TODO");
  }
  
  static RuntimeException todo(String msg) {
    throw new RuntimeException("TODO: " + msg);
  }
static int numberOfFunctionArguments(Object f) {
  if (f instanceof F1) return 1;
  if (f instanceof String) return numberOfMethodArguments(mc(), (String) f);
  return numberOfMethodArguments(f, "get");
}


static int numberOfMethodArguments(Object o, String method) {
  Class c;
  boolean mustBeStatic = false;
  if (o instanceof Class) {
    c = (Class) o;
    mustBeStatic = true;
  } else
    c = o.getClass();
      
  Class _c = c;
  int n = -1;
  while (c != null) {
    for (Method m : c.getDeclaredMethods()) {
      if (!m.getName().equals(method)) continue;
      if (mustBeStatic && !methodIsStatic(m)) continue;
      int nn = l(m.getParameterTypes());
      if (n == -1) n = nn;
      else if (n != nn) throw fail("Variable number of method arguments: " + _c + "." + method);
    }
    c = c.getSuperclass();
  }
  if (n == -1) throw fail("Method not found: " + _c + "." + method);
  return n;
}


static boolean methodIsStatic(Method m) {
  return (m.getModifiers() & Modifier.STATIC) != 0;
}


static class Drawing extends Concept {
  static String _fieldOrder = "globalID name calStructure";
  String globalID = aGlobalID();
  String name;
  String calStructure;
  
  CirclesAndLines cal() { return (CirclesAndLines) unstructure(calStructure); }
}// allows skipping patterns when they don't apply
static class MatchLearner extends Concept {
  static String _fieldOrder = "pattern examples";
  String pattern;
  Map<String, String> examples = mapInConcept(this, litorderedmap()); // maps to 'assumed, 'confirmed or 'rejected
}static class Matches {
  String[] m;
  
  Matches() {}
  Matches(String... m) {
  this.m = m;}
  
  String get(int i) { return i < m.length ? m[i] : null; }
  String unq(int i) { return unquote(get(i)); }
  String fsi(int i) { return formatSnippetID(unq(i)); }
  String fsi() { return fsi(0); }
  String tlc(int i) { return unq(i).toLowerCase(); }
  boolean bool(int i) { return "true".equals(unq(i)); }
  String rest() { return m[m.length-1]; } // for matchStart
  int psi(int i) { return Integer.parseInt(unq(i)); }
}
static class Var<A> implements IVar<A> {
  A v; // you can access this directly if you use one thread
  
  Var() {}
  Var(A v) {
  this.v = v;}
  
  public synchronized void set(A a) {
    if (v != a) {
      v = a;
      notifyAll();
    }
  }
  
  public synchronized A get() { return v; }
  public synchronized boolean has() { return v != null; }
  public synchronized void clear() { v = null; }
  
  public String toString() { return str(get()); }
}static class BaseBase {
  String globalID = aGlobalID();
  String text;
  String textForRender() { return text; }
}

static boolean traits_multiLine = true;

static class Base extends BaseBase {
  List<String> traits = new ArrayList();
  boolean hasTrait(String t) { return containsIC(traits(), t); }
  List<String> traits() { if (nempty(text) && neq(first(traits), text)) traits.add(0, text); return traits; }
  void addTraits(List<String> l) { setAddAll(traits(), l); }
  void addTrait(String t) { if (nempty(t)) setAdd(traits(), t); }
  
  String textForRender() {
    List<String> traits = traits();
    if (traits_multiLine) return lines(traits);
    if (l(traits) <= 1) return first(traits);
    return first(traits) + " [" + join(", ", dropFirst(traits)) + "]";
  }
  
  void setText(String text) {
    this.text = text;
    traits = ll(text);
  }
}

static class CirclesAndLines {
  List<Circle> circles = new ArrayList();
  List<Line> lines = new ArrayList();
  Class<? extends Arrow> arrowClass = Arrow.class;
  Class<? extends Circle> circleClass = Circle.class;
  String title;
  String globalID = aGlobalID();
  long created = nowUnlessLoading();
  transient Lock lock = fairLock();
  transient String defaultImageID = "#1007372";
  transient double imgZoom = 1; // zoom for the circle images
  transient Pt translate;
  Circle hoverCircle; // which one we are hovering over
  transient Object onUserMadeArrow, onUserMadeCircle, onLayoutChange;
  transient Object onFullLayoutChange, onDeleteCircle, onDeleteLine;
  transient Object onRenameCircle, onRenameLine, onStructureChange;
  transient BufferedImage imageForUserMadeNodes;
  static int maxDistanceToLine = 20; // for clicking
  transient String backgroundImageID = defaultBackgroundImageID;
  static String defaultBackgroundImageID = "#1007195";
  static Color defaultLineColor = Color.white;
  static boolean debugRender;
  static Object staticPopupExtender;
  transient double scale = 1; // zoom whole image
  transient boolean recordHistory = true;
  List history;

  // auto-visualize
  Circle circle_autoVis(String text, String visualizationText, double x, double y) {
    return addAndReturn(circles,
      nu(circleClass, "x", x, "y", y, "text", text,
        "quickvis" , visualizationText,
        "img" , processImage(quickVisualizeOr(visualizationText, defaultImageID))));
  }
  
  String makeVisualizationText(String text) {
    return possibleGlobalID(text) ? "" : text;
  }

  Circle circle_autoVis(String text, double x, double y) {
    return circle_autoVis(text, makeVisualizationText(text), x, y);
  }
  
  Circle circle(BufferedImage img, double x, double y, String text) {
    return addAndReturn(circles, nu(circleClass, "x", x, "y", y, "text", text, "img" , processImage(img)));
  }
  
  Circle circle(String text, BufferedImage img, double x, double y) {
    return circle(img, x, y, text);
  }
  
  Circle circle(String text, double x, double y) {
    return addAndReturn(circles, nu(circleClass, "x", x, "y", y, "text", text, "img" , processImage(imageForUserMadeNodes())));
  }
  
  Circle addCircle(String imageID, double x, double y) {
    return addCircle(imageID, x, y, "");
  }
  
  Circle addCircle(String imageID, double x, double y, String text) {
    return addAndReturn(circles, nu(circleClass, "x", x, "y", y, "text", text, "img" , processImage(loadImage2(imageID))));
  }
  
  Arrow findArrow(Circle a, Circle b) {
    for (Line l : getWhere(lines, "a", a, "b", b))
      if (l instanceof Arrow)
        return (Arrow) l;
    return null;
  }
  
  Line addLine(Circle a, Circle b) {
    Line line = findWhere(lines, "a", a, "b", b);
    if (line == null)
      lines.add(line = nu(Line.class, "a", a, "b", b));
    return line;
  }
  
  Arrow arrow(Circle a, String text, Circle b) {
    return addArrow(a, b, text);
  }
  
  Arrow addArrow(Circle a, Circle b) {
    return addArrow(a, b, "");
  }
  
  Arrow addArrow(Circle a, Circle b, String text) {
    return addAndReturn(lines, nu(arrowClass, "a", a, "b", b, "text", text));
  }
  
  BufferedImage makeImage(int w, int h) {
    BufferedImage bg = renderTiledBackground(backgroundImageID, w, h, ptX(translate), ptY(translate));
    if (!lock.tryLock()) return null;
    try {
      if (scale != 1)
        createGraphics_modulate(bg, new Object() { void get(Graphics2D g) { try { 
          g.scale(scale, scale);
         } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "g.scale(scale, scale);"; }});
      
      // Lines
      
      if (debugRender)
        print("Have " + n(lines, "line"));
      
      HashMap<Pair<Circle, Circle>, Line> hasLine = new HashMap();
      HashMap<Line,Boolean> flipMap = new HashMap();
      for (Line l : lines) {
        hasLine.put(pair(l.a, l.b), l);
        Line x = hasLine.get(pair(l.b, l.a));
        if (x != null) {
          if (debugRender)
            print("flipMap " + l.a.text + " / " + l.b.text);
          flipMap.put(x, false);
          flipMap.put(l, false);
        }
      }

      for (Line l : lines) {
        DoublePt a = translateDoublePt(translate, l.a.doublePt(w, h, this));
        DoublePt b = translateDoublePt(translate, l.b.doublePt(w, h, this));
        if (debugRender)
          print("Line " + a + " " + b);
        if (l instanceof Arrow)
          drawThoughtArrow(bg, l.a.img(this), iround(a.x), iround(a.y), l.b.img(this), iround(b.x), iround(b.y), l.color);
        else
          drawThoughtLine(bg, l.a.img(this), iround(a.x), iround(a.y), l.b.img(this), iround(b.x), iround(b.y), l.color);
        String text = l.textForRender();
        if (nempty(text)) {
          drawOutlineTextAlongLine_flip.set(flipMap.get(l));
          drawThoughtLineText_multiLine(bg, l.a.img(this), iround(a.x), iround(a.y), l.b.img(this), iround(b.x), iround(b.y), text, Color.white /*l.color*/);
        }
      }
      
      // Circles
      
      for (Circle c : circles) {
        DoublePt p = translateDoublePt(translate, c.doublePt(w, h, this));
        drawThoughtCircle(bg, c.img(this), p.x, p.y);
      }
      
      for (Circle c : circles) {
        DoublePt p = translateDoublePt(translate, c.doublePt(w, h, this));
        String text = c.textForRender();
        if (nempty(text))
          drawThoughtCircleText(bg, c.img(this), p, text);
          
        if (c == hoverCircle)
          drawThoughtCirclePlus(bg, c.img(this), p.x, p.y);
      }
    } finally {
      lock.unlock();
      createGraphics_modulate(bg, null);
    }
    return bg;
  }
  
  Canvas showAsFrame(int w, int h) {
    Canvas canvas = showAsFrame();
    frameInnerSize(canvas, w, h);
    centerFrame(getFrame(canvas));
    return canvas;
  }
  
  Canvas showAsFrame() {
    return (Canvas) swing(new Object() { Object get() { try { 
      Canvas canvas = makeCanvas();
      showCenterFrame(canvas);
      return canvas;
     } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "Canvas canvas = makeCanvas();\r\n      showCenterFrame(canvas);\r\n      ret canvas;"; }});
  }
  
  Canvas makeCanvas() {
    final Object makeImg = new Object() { Object get(int w, int h) { try { return  makeImage(w, h) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "makeImage(w, h)"; }};
    final Canvas canvas = jcanvas(makeImg);
    disableImageSurfaceSelector(canvas);
    new CircleDragger(this, canvas, new Runnable() { public void run() { try {  updateCanvas(canvas, makeImg) ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "updateCanvas(canvas, makeImg)"; }});
    
    componentPopupMenu(canvas, new Object() { void get(JPopupMenu menu) { try { 
      // POPUP MENU START
      Pt p = pointFromEvent(canvas, componentPopupMenu_mouseEvent.get());
      JMenu imageMenu = jmenu("Image");
      moveAllMenuItems(menu, imageMenu);

      addMenuItem(menu, "New Circle...", new Runnable() { public void run() { try {  newCircle(canvas) ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "newCircle(canvas)"; }});
      
      final Line l = findLine(canvas, p);
      if (l != null) {
        addMenuItem(menu, "Rename Relation...", new Runnable() { public void run() { try { 
          renameLine(canvas, l)
        ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "renameLine(canvas, l)"; }});
        
        addMenuItem(menu, "Delete Relation", new Runnable() { public void run() { try { 
          deleteLine(l);
          canvas.update();
        
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "deleteLine(l);\r\n          canvas.update();"; }});
      }
      
      final Circle c = findCircle(canvas, p);
      if (c != null) {
        addMenuItem(menu, "Rename Circle...", new Runnable() { public void run() { try {  renameCircle(canvas, c) ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "renameCircle(canvas, c)"; }});
        addMenuItem(menu, "Delete Circle", new Runnable() { public void run() { try { 
          deleteCircle(c);
          canvas.update();
        
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "deleteCircle(c);\r\n          canvas.update();"; }});
        
        if (c.img != null || c.quickvis != null)
          addMenuItem(menu, "Delete Image", new Runnable() { public void run() { try { 
            c.img = null;
            c.quickvis = null;
            canvas.update();
          
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "c.img = null;\r\n            c.quickvis = null;\r\n            canvas.update();"; }});
          
        if (neqic(c.text, c.quickvis))
          addMenuItem(menu, "Visualize", new Runnable() { public void run() { try { 
            { Thread _t_0 = new Thread("Visualizing") {
public void run() { try {
  
              quickVisualize(c.text);
              print("Quickvis done");
              { swing(new Runnable() { public void run() { try { 
                c.img = null;
                c.quickvis = c.text;
                canvas.update();
                pcallF(onRenameCircle, c); schange();
              
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "c.img = null;\r\n                c.quickvis = c.text;\r\n                canvas.u..."; }}); }
            } catch (Throwable __e) { printStackTrace2(__e); } }
};
startThread(_t_0); }
          
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "thread \"Visualizing\" {\r\n              quickVisualize(c.text);\r\n              ..."; }});
      }
      
      addMenuItem(menu, "Copy structure to clipboard", new Runnable() { public void run() { try { 
        copyTextToClipboard(cal_simplifiedStructure(CirclesAndLines.this))
      ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "copyTextToClipboard(cal_simplifiedStructure(CirclesAndLines.this))"; }});

      addMenuItem(menu, "Paste structure", new Runnable() { public void run() { try { 
        String text = getTextFromClipboard();
        if (nempty(text)) {
          copyCAL(cal_unstructure(text), CirclesAndLines.this);
          canvas.update();
          schange();
        }
      
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "String text = getTextFromClipboard();\r\n        if (nempty(text)) {\r\n         ..."; }});
      
      pcallF(staticPopupExtender, CirclesAndLines.this, canvas, menu);
      
      addMenuItem(menu, imageMenu);

      // POPUP MENU END
     } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "// POPUP MENU START\r\n      Pt p = pointFromEvent(canvas, componentPopupMenu_m..."; }});
    
    return canvas;
  }
  
  void newCircle(final Canvas canvas) {
    final JTextField text = jtextfield();
    showFormTitled("New Circle", "Text", text, runnableThread(new Runnable() { public void run() { try {  { JWindow _loading_window = showLoadingAnimation(); try {
      String theText = getTextTrim(text);
      makeCircle(theText);
      canvas.update();
    } finally { disposeWindow(_loading_window); }}
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "{ JWindow _loading_window = showLoadingAnimation(); try {\r\n      String theTe..."; }}));
  }
  
  Canvas show() { return showAsFrame(); }
  Canvas show(int w, int h) { return showAsFrame(w, h); }
  
  Circle findCircle(String text) {
    for (Circle c : circles) if (eq(c.text, text)) return c;
    for (Circle c : circles) if (eqic(c.text, text)) return c;
    return null;
  }
  
  void renameCircle(final Canvas canvas, final Circle c) {
    final JTextField tf = jtextfield(c.text);
    showFormTitled("Rename circle",
      "Old name", jlabel(c.text),
      "New name", tf,
      new Runnable() { public void run() { try { 
        c.setText(getTextTrim(tf));
        canvas.update();
        pcallF(onRenameCircle, c); schange();
      
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "c.setText(getTextTrim(tf));\r\n        canvas.update();\r\n        pcallF(onRenam..."; }});
  }
  
  void renameLine(final Canvas canvas, final Line l) {
    final JTextField tf = jtextfield(l.text);
    showFormTitled("Rename relation",
      "Old name", jlabel(l.text),
      "New name", tf,
      new Runnable() { public void run() { try { 
        l.setText(getTextTrim(tf));
        canvas.update();
        pcallF(onRenameLine, l); schange();
      
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "l.setText(getTextTrim(tf));\r\n        canvas.update();\r\n        pcallF(onRenam..."; }});
  }
  
  void clear() {
    clearAll(circles, lines);
  }
  
  // only finds actually containing circles
  Circle findCircle(ImageSurface canvas, Pt p) {
    p = untranslatePt(translate, p);
    Lowest<Circle> best = new Lowest();
    for (Circle c : circles)
      if (c.contains(this, canvas, p))
        best.put(c, pointDistance(p, c.pt2(this, canvas)));
    return best.get();
  }
  
  Circle findNearestCircle(ImageSurface canvas, Pt p) {
    Lowest<Circle> best = new Lowest();
    for (Circle c : circles)
      if (c.contains(this, canvas, p))
        best.put(c, pointDistance(p, c.pt2(this, canvas)));
    return best.get();
  }
  
  BufferedImage processImage(BufferedImage img) {
    return scaleImage(img, imgZoom);
  }
  
  void deleteCircle(Circle c) {
    for (Line l : cloneList(lines))
      if (l.a == c || l.b == c) deleteLine(l);
    circles.remove(c);
    pcallF(onDeleteCircle, c); schange();
  }
  
  void deleteLine(Line l) {
    lines.remove(l);
    pcallF(onDeleteLine, l); schange();
  }
  
  void openPlusDialog(final Circle c, final ImageSurface canvas) {
    if (c == null) return;
    
    final JTextField tfFrom = jtextfield(c.text);
    final JTextField tfRel = jtextfield(web_defaultRelationName());
    final JComboBox tfTo = autoComboBox(collect(circles, "text"));
    
    showFormTitled("Add connection",
      "From node", tfFrom,
      "Connection name", tfRel,
      "To node", tfTo,
      new Object() { Object get() { try { 
        String sA = getTextTrim(tfFrom);
        Circle a = eq(sA, c.text) ? c : findOrMakeCircle(sA);
        if (a == null) { messageBox("Not found: " + getTextTrim(tfFrom)); return false; }
        Circle b = findOrMakeCircle(getTextTrim(tfTo));
        if (b == null) { messageBox("Not found: " + getTextTrim(tfTo)); return false; }
        if (a == b) { infoBox("Can't connect circle to itself for now"); return false; }
        Arrow arrow = arrow(a, getTextTrim(tfRel), b);
        ((Canvas) canvas).update();
        pcallF(onUserMadeArrow, arrow); schange();
        return null;
       } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "S sA = getTextTrim(tfFrom);\r\n        Circle a = eq(sA, c.text) ? c : findOrMa..."; }});
    awtLater(tfRel, 100, new Runnable() { public void run() { try {  requestFocus(tfRel) ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "requestFocus(tfRel)"; }});
  }
  
  void schange() {
    pcallF(onStructureChange);
    logQuoted("user-web-edits", now() + " " + cal_structure(this));
    markWebsPosted();
  }
  
  Circle findOrMakeCircle(String text) {
    Circle c = findCircle(text);
    if (c != null) return c;
    return makeCircle(text);
  }

  Circle makeCircle(String text) {
    Circle c = circle(imageForUserMadeNodes(), random(0.1, 0.9), random(0.1, 0.9), text);
    pcallF(onUserMadeCircle, c); schange();
    historyLog(lisp("Made circle", text));
    return c;
  }

  BufferedImage imageForUserMadeNodes() {  
    if (imageForUserMadeNodes == null)
      imageForUserMadeNodes = whiteImage(20, 20);
    return imageForUserMadeNodes;
  }
  
  Line findLine(Canvas is, Pt p) {
    p = untranslatePt(translate, p);
    Lowest<Line> best = new Lowest();
    for (Line line : lines) {
      double d = distancePointToLineSegment(line.a.pt(this, is), line.b.pt(this, is), p);
      if (d <= maxDistanceToLine)
        best.put(line, d);
    }
    return best.get();
  }
  
  Pt pointFromEvent(ImageSurface canvas, MouseEvent e) {
    return scalePt(canvas.pointFromEvent(e), 1/scale);
  }
  
  void historyLog(Object o) {
    if (!recordHistory) return;
    if (history == null) history = new ArrayList();
    history.add(o);
  }
} // end of class CirclesAndLines

static class Circle extends Base {
  transient BufferedImage img;
  double x, y;
  //static BufferedImage defaultImage;
  String quickvis;
  
  BufferedImage img(CirclesAndLines cal) {
    if (img != null) return img;
    if (nempty(quickvis)) img = quickVisualize(quickvis);
    //if (defaultImage == null) defaultImage = loadImage2(#1007372);
    return cal.imageForUserMadeNodes();
  }
  
  Pt pt2(CirclesAndLines cal, ImageSurface is) {
    return pt2(is.getWidth(), is.getHeight(), cal);
  }
  
  Pt pt(CirclesAndLines cal, ImageSurface is) {
    return pt(is.getWidth(), is.getHeight(), cal);
  }
  
  Pt pt(int w, int h, CirclesAndLines cal) {
    return new Pt(iround(x*w/cal.scale), iround(y*h/cal.scale));
  }
  
  Pt pt2(int w, int h, CirclesAndLines cal) {
    return new Pt(iround(x*w), iround(y*h));
  }
  
  DoublePt doublePt(int w, int h, CirclesAndLines cal) {
    return new DoublePt(x*w/cal.scale, y*h/cal.scale);
  }
  
  boolean contains(CirclesAndLines cal, ImageSurface is, Pt p) {
    return pointDistance(p, pt2(cal, is)) <= iround(thoughtCircleSize(img(cal))*cal.scale)/2+1;
  }
}

static class Line extends Base {
  Circle a, b;
  transient Color color = CirclesAndLines.defaultLineColor;
  
  Line setColor(Color color) {
    this.color = color;
    return this;
  }
}

static class Arrow extends Line {}

static class CircleDragger extends MouseAdapter {
  CirclesAndLines cal;
  ImageSurface is;
  Object update;
  int dx, dy;
  Circle circle;
  Pt startPoint;
  
  CircleDragger(CirclesAndLines cal, ImageSurface is, Object update) {
  this.update = update;
  this.is = is;
  this.cal = cal;
    if (containsInstance(is.tools, CircleDragger.class)) return;
    is.tools.add(this);
    is.addMouseListener(this);
    is.addMouseMotionListener(this);
  }

  public void mouseMoved(MouseEvent e) {
    Pt p = is.pointFromEvent(e);
    Circle c = cal.findCircle(is, p);
    if (c != cal.hoverCircle) {
      cal.hoverCircle = c;
      callF(update);
    }
  }
  
  public void mousePressed(MouseEvent e) {
    if (e.getButton() == MouseEvent.BUTTON1) {
      Pt p = is.pointFromEvent(e);
      startPoint = p;
      circle = cal.findCircle(is, p);
      if (circle != null) {
        dx = p.x-iround(circle.x*is.getWidth());
        dy = p.y-iround(circle.y*is.getHeight());
        //printVars("mousePressed", +dx, +dy, +p, cx := circle.x, cy := circle.y, w := is.getWidth(), h := is.getHeight());
      } else { 
        Pt t = unnull(cal.translate);
        dx = p.x-t.x;
        dy = p.y-t.y;
      }
    }
  }

  public void mouseDragged(MouseEvent e) {
    if (startPoint == null) return;
    Pt p = is.pointFromEvent(e);
    if (circle != null) {
      circle.x = (p.x-dx)/(double) is.getWidth();
      circle.y = (p.y-dy)/(double) is.getHeight();
      //printVars("mouseDragged", +dx, +dy, +p, cx := circle.x, cy := circle.y, w := is.getWidth(), h := is.getHeight());
      pcallF(cal.onLayoutChange, circle);
      callF(update);
    } else {
      cal.translate = new Pt(p.x-dx, p.y-dy);
      callF(update);
    }
  }

  public void mouseReleased(MouseEvent e) {
    mouseDragged(e);
    if (eq(is.pointFromEvent(e), startPoint))
      cal.openPlusDialog(circle, is);
    circle = null;
    startPoint = null;
  }
}static class NotifyingBlockingThreadPoolExecutor extends ThreadPoolExecutor {

	private AtomicInteger tasksInProcess = new AtomicInteger();

	private Synchronizer synchronizer = new Synchronizer();

	public NotifyingBlockingThreadPoolExecutor(int poolSize, int queueSize, long keepAliveTime, TimeUnit keepAliveTimeUnit, long maxBlockingTime, TimeUnit maxBlockingTimeUnit, Callable<Boolean> blockingTimeCallback) {

		super(poolSize, // Core size
				poolSize, // Max size
				keepAliveTime,
				keepAliveTimeUnit,
				new ArrayBlockingQueue<Runnable>(Math.max(poolSize, queueSize)),
				new BlockThenRunPolicy(maxBlockingTime, maxBlockingTimeUnit, blockingTimeCallback));

		super.allowCoreThreadTimeOut(true);
	}

	public NotifyingBlockingThreadPoolExecutor(int poolSize, int queueSize, long keepAliveTime, TimeUnit unit) {

		super(poolSize, // Core size
				poolSize, // Max size
				keepAliveTime,
				unit,
				new ArrayBlockingQueue<Runnable>(Math.max(poolSize, queueSize)), // not smaller than the poolSize (to avoid redundant threads)
				new BlockThenRunPolicy()); // When super invokes the reject method this class will ensure a blocking try.

		super.allowCoreThreadTimeOut(true); // Time out the core threads.
	}

	@Override
	public void execute(Runnable task) {
		// count a new task in process
		tasksInProcess.incrementAndGet();
		try {
			super.execute(task);
		} catch(RuntimeException e) { // specifically handle RejectedExecutionException  
			tasksInProcess.decrementAndGet();
			throw e;
		} catch(Error e) {
			tasksInProcess.decrementAndGet();
			throw e;
		}
	}

	@Override
	protected void afterExecute(Runnable r, Throwable t) {
		
		super.afterExecute(r, t);

		synchronized(this) {
			tasksInProcess.decrementAndGet();
			if (tasksInProcess.intValue() == 0) {
				synchronizer.signalAll();
			}
		}
	}

	@Override
	public void setCorePoolSize(int corePoolSize) {
		super.setCorePoolSize(corePoolSize);
		super.setMaximumPoolSize(corePoolSize);
	}

	@Override
	public void setMaximumPoolSize(int maximumPoolSize) {
		throw new UnsupportedOperationException("setMaximumPoolSize is not supported.");
	}
	
	public void setRejectedExecutionHandler(RejectedExecutionHandler handler) {
		throw new UnsupportedOperationException("setRejectedExecutionHandler is not allowed on this class.");
	}

	public void await() throws InterruptedException {
		synchronizer.await();
	}

	public boolean await(long timeout, TimeUnit timeUnit) throws InterruptedException {
    	return synchronizer.await(timeout, timeUnit);
	}

	private class Synchronizer {

		private final Lock lock = new ReentrantLock();
		private final Condition done = lock.newCondition();
		private boolean isDone = false;

		private void signalAll() {

			lock.lock();
			try {
				isDone = true;
				done.signalAll();
			}
			finally {
				lock.unlock();
			}
		}

		public void await() throws InterruptedException {

			lock.lock();
			try {
				while (!isDone) {
					done.await();
				}
			}
			finally {
				isDone = false;
				lock.unlock();
			}
		}

		public boolean await(long timeout, TimeUnit timeUnit) throws InterruptedException {

			boolean await_result = false;
			lock.lock();
			boolean localIsDone;
			try {
				await_result = done.await(timeout, timeUnit);
			}
			finally {
				localIsDone = isDone;
				isDone = false;
				lock.unlock();
			}
			return await_result && localIsDone;
		}
	}

	private static class BlockThenRunPolicy implements RejectedExecutionHandler {

		private long maxBlockingTime;
		private TimeUnit maxBlockingTimeUnit;
		private Callable<Boolean> blockingTimeCallback;

		public BlockThenRunPolicy(long maxBlockingTime, TimeUnit maxBlockingTimeUnit, Callable<Boolean> blockingTimeCallback) {
			this.maxBlockingTime = maxBlockingTime;
			this.maxBlockingTimeUnit = maxBlockingTimeUnit;
			this.blockingTimeCallback = blockingTimeCallback;
		}

		public BlockThenRunPolicy() {
		}

		@Override
		public void rejectedExecution(Runnable task, ThreadPoolExecutor executor) {           

			BlockingQueue<Runnable> workQueue = executor.getQueue();
			boolean taskSent = false;

			while (!taskSent) {

				if (executor.isShutdown()) {
					throw new RejectedExecutionException(
							"ThreadPoolExecutor has shutdown while attempting to offer a new task.");
				}

				try {
					if(blockingTimeCallback != null) {
						if (workQueue.offer(task, maxBlockingTime, maxBlockingTimeUnit)) {
							taskSent = true;
						}
						else {
							// task was not accepted - call the Callback
							Boolean result = null;
							try {
								result = blockingTimeCallback.call();
							}
							catch(Exception e) {
								throw new RejectedExecutionException(e);
							}
							if(result == false) {
								throw new RejectedExecutionException("User decided to stop waiting for task insertion");                        		
							}
							else {
								continue;
							}
						}

					}
					else {
						workQueue.put(task);
						taskSent = true;
					}                    
				}
				catch (InterruptedException e) {
				}
			}
		}

	}

}// f is a function or an instance of Cache (the dependee)
static class DependentCache<A> extends Cache<A> {
  Object f, lastDep;
  
  DependentCache() {}
  DependentCache(Object maker, Object f) {
  this.f = f;
  this.maker = maker;}
  
  A get() {
    if (hasLock(lock)) return value; // Must be called from within maker
    {
      Lock _lock_498 = lock; lock(_lock_498); try {
      Object dep;
      if (f instanceof Cache) dep = ((Cache) f).changeCount;
      else {
        dep = callF(f);
        if (dep instanceof Cache) dep = ((Cache) dep).changeCount;
      }
      if (dep != lastDep) {
        clear();
        lastDep = dep;
      }
    } finally { unlock(_lock_498); } }
    return super.get();
  }
}static interface AI_CacheChecker {
  boolean hasTriple(String a, String b, String c);
}static class MultiMap<A,B> {
  Map<A, List<B>> data = new HashMap<A, List<B>>();
  
  MultiMap() {}
  MultiMap(boolean useTreeMap) { if (useTreeMap) data = new TreeMap(); }
  MultiMap(MultiMap<A, B> map) { putAll(map); }

  synchronized void put(A key, B value) {
    List<B> list = data.get(key);
    if (list == null)
      data.put(key, list = _makeEmptyList());
    list.add(value);
  }

  synchronized void addAll(A key, Collection<B> values) {
    putAll(key, values);
  }
  
  synchronized void addAllIfNotThere(A key, Collection<B> 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<B> values) {
    for (B value : values)
      put(key, value);
  }

  synchronized void removeAll(A key, Collection<B> values) {
    for (B value : values)
      remove(key, value);
  }
  
  synchronized List<B> get(A key) {
    List<B> list = data.get(key);
    return list == null ? Collections.<B> emptyList() : list;
  }

  // returns actual mutable live list
  // creates the list if not there
  synchronized List<B> getActual(A key) {
    List<B> list = data.get(key);
    if (list == null)
      data.put(key, list = _makeEmptyList());
    return list;
  }
 
  synchronized void clean(A key) {
    List<B> list = data.get(key);
    if (list != null && list.isEmpty())
      data.remove(key);
  }

  synchronized Set<A> keySet() {
    return data.keySet();
  }

  synchronized Set<A> keys() {
    return data.keySet();
  }

  synchronized void remove(A key) {
    data.remove(key);
  }

  synchronized void remove(A key, B value) {
    List<B> list = data.get(key);
    if (list != null) {
      list.remove(value);
      if (list.isEmpty())
        data.remove(key);
    }
  }

  synchronized void clear() {
    data.clear();
  }

  synchronized boolean containsKey(A key) {
    return data.containsKey(key);
  }

  synchronized B getFirst(A key) {
    List<B> list = get(key);
    return list.isEmpty() ? null : list.get(0);
  }
  
  synchronized void putAll(MultiMap<A, B> map) {
    for (A key : map.keySet())
      putAll(key, map.get(key));
  }
  
  synchronized int keysSize() { return l(data); }
  
  // full size - note: expensive operation
  synchronized int size() {
    int n = 0;
    for (List l : data.values())
      n += l(l);
    return n;
  }
  
  // expensive operation
  synchronized List<A> reverseGet(B b) {
    List<A> l = new ArrayList();
    for (A key : data.keySet())
      if (data.get(key).contains(b))
        l.add(key);
    return l;
  }
  
  synchronized Map<A, List<B>> asMap() {
    return cloneMap(data);
  }
  
  boolean isEmpty() { return data.isEmpty(); }
  
  List<B> _makeEmptyList() {
    return new ArrayList();
  }
}static class SoftwareMadeWeb {
  long date;
  String computerID, programID;
  String structure; // web or cal
  Web web;
  Map furtherInfo;
}static class BetterThreadLocal<A> {
  Map<Thread, A> map = newWeakHashMap();
  
  A get() {
    return map.get(currentThread());
  }
  
  A get(Thread thread) {
    return map.get(thread);
  }
  
  void set(A a) {
    mapPutOrRemove(map, currentThread(), a);
  }
}static class Either<A, B> {
  byte which;
  Object value;
  
  Either() {}
  Either(int which, Object value) {
  this.value = value; this.which = (byte) which; }
  
  boolean isA() { return which == 1; }
  boolean isB() { return which == 2; }
  
  A a() { if (which != 1) _failMe(); return (A) value; }
  B b() { if (which != 2) _failMe(); return (B) value; }
  
  void _failMe() {
    throw fail("Either object is of wrong type: " + shortClassName(value));
  }
}static class Best<A> {
  A best;
  double score;
  transient Object onChange;
  
  boolean isNewBest(double score) {
    return best == null || !isNaN(score) && score > this.score;
  }
  
  double bestScore() {
    return best == null ? Double.NaN : score;
  }
  
  double score() { return bestScore(); }
  double getScore() { return bestScore(); }
  
  boolean put(A a, double score) {
    if (a != null && isNewBest(score)) {
      best = a;
      this.score = score;
      pcallF(onChange);
      return true;
    }
    return false;
  }
  
  A get() { return best; }
  boolean has() { return best != null; }
  
  Pair<A, Double> pair() { return main.pair(best, bestScore()); }
  
  A getIfScoreAbove(double x) { return score() >= x ? best : null; }
}static class AI_SubSpace implements AI_PostHandler, AI_CacheChecker {
  int limit;
  MultiMap<String,WebNode> index = new MultiMap();
  HashMap<String,Web> websByID = new HashMap();
  List<Web> webs = new ArrayList();
  boolean changed;
  
  AI_SubSpace() {}
  AI_SubSpace(int limit) {
  this.limit = limit;}
  
  // existence of triple has already been checked
  public String postTriple(T3<String, String, String> t) {
    if (limitReached()) throw fail("Subspace limit reached");
    Web web = webFromTriple(t);
    addWeb(web);
    return web.globalID;
  }
  
  void addWeb(Web web) {
    websByID.put(web.globalID, web);
    webs.add(web);
    changed = true;
    for (WebNode node : web_nodesAndRelations(web))
      for (String text : asSet(node.texts())) 
        index.put(ai_shortenForIndex(text), node);
  }
  
  public boolean hasTriple(String a, String b, String c) {
    List<Web> webs = ai_withoutInvalidWebs(nodesToWebs(shortestList3(
      index.get(a),
      index.get(b),
      index.get(c))));
    return webs_search_noVar_bool(webFromTriples(a, b, c), webs);
  }
  
  boolean limitReached() {
    return limit > 0 && l(websByID) >= limit;
  }
  
  List<WebNode> indexedNodes_rawLookup(String s) {
    return index.get(ai_shortenForIndex(s));
  }
}// a Lisp-like form
static class Lisp implements Iterable<Lisp> {
  String head;
  List<Lisp> args;
  
  // O more; // additional info, user-defined
  
  Lisp() {}
  Lisp(String head) { this.head = _compactString(head); }
  Lisp(String head, Lisp... args) {
  this.head = head;
    argsForEdit().addAll(asList(args));
  }
  Lisp(String head, Collection args) {
    this.head = _compactString(head);
    for (Object arg : args) add(arg);
  }
  
  List<Lisp> argsForEdit() {
    return args == null ? (args = new ArrayList()) : args;
  }
  
  // INEFFICIENT
  public String toString() {
    if (empty())
      return quoteIfNotIdentifierOrInteger(head);
    List<String> bla = new ArrayList();
    for (Lisp a : args)
      bla.add(a.toString());
    String inner = join(", ", bla);
    if (head.equals(""))
      return "{" + inner + "}"; // list
    else
      return quoteIfNotIdentifier(head) + "(" + inner + ")";
  }

  String raw() {
    if (!isEmpty ()) throw fail("not raw: " + this);
    return head;
  }
  
  Lisp add(Lisp l) {
    argsForEdit().add(l);
    return this;
  }
  
  Lisp add(String s) {
    argsForEdit().add(new Lisp(s));
    return this;
  }
  
  Lisp add(Object o) {
    if (o instanceof Lisp) add((Lisp) o);
    else if (o instanceof String) add((String) o);
    else throw fail("Bad argument type: " + structure(o));
    return this;
  }
  
  int size() {
    return l(args);
  }
  
  boolean empty() { return main.empty(args); }
  boolean isEmpty() { return main.empty(args); }
  boolean isLeaf() { return main.empty(args); }
  
  Lisp get(int i) {
    return main.get(args, i);
  }
  
  String getString(int i) { Lisp a = get(i); return a == null ? null : a.head; }
  
  String s(int i) { return getString(i); }
  String rawOrNull(int i) {
    Lisp a = get(i); return a != null && a.isLeaf() ? a.head : null;
  }
  String raw(int i) { return assertNotNull(rawOrNull(i)); }
  boolean isLeaf(int i) { return rawOrNull(i) != null; }
  
  boolean isA(String head) {
    return eq(head, this.head);
  }

  boolean is(String head, int size) {
    return isA(head) && size() == size;
  }
  
  boolean is(String head) { return isA(head); }
  boolean headIs(String head) { return isA(head); }
  
  boolean is(String... heads) {
    return asList(heads).contains(head);
  }
  
  // check head for one of these (ignore case)
  boolean isic(String... heads) {
    return containsIgnoreCase(heads, head);
  }
  
  public Iterator<Lisp> iterator() {
    return main.iterator(args);
  }
  
  Lisp subList(int fromIndex, int toIndex) {
    Lisp l = new Lisp(head);
    l.argsForEdit().addAll(args.subList(fromIndex, toIndex)); // better to copy here I guess - safe
    return l;
  }

  public boolean equals(Object o) {
    if (o == null || o.getClass() != Lisp.class) return false;
    Lisp l = (Lisp) ( o);
    return eq(head, l.head) && (isLeaf() ? l.isLeaf()
      : l.args != null && eq(args, l.args));
  }
  
  public int hashCode() {
    return head.hashCode() + main.hashCode(args);
  }
  
  Lisp addAll(List args) {
    for (Object arg : args) add(arg);
    return this;
  }
  
  String unquoted() { return unquote(raw()); }
  String unq() { return unquoted(); }
  String unq(int i) { return get(i).unq(); }
  
  // heads of arguments
  List<String> heads() { return collect(args, "head"); }
}
static class T3<A, B, C> {
  A a;
  B b;
  C c;
  
  T3() {}
  T3(A a, B b, C c) {
  this.c = c;
  this.b = b;
  this.a = a;}
  
  public int hashCode() {
    return _hashCode(a) + 2*_hashCode(b) - 4*_hashCode(c);
  }
  
  public boolean equals(Object o) {
    if (o == this) return true;
    if (!(o instanceof T3)) return false;
    T3 t = (T3) o;
    return eq(a, t.a) && eq(b, t.b) && eq(c, t.c);
  }
  
  public String toString() {
    return "(" + str(a) + ", " + str(b) + ", " + str(c) + ")";
  }
}static class Pair<A, B> {
  A a;
  B b;

  Pair() {}
  Pair(A a, B b) {
  this.b = b;
  this.a = a;}
  
  public int hashCode() {
    return hashCodeFor(a) + 2*hashCodeFor(b);
  }
  
  public boolean equals(Object o) {
    if (o == this) return true;
    if (!(o instanceof Pair)) return false;
    Pair t = (Pair) o;
    return eq(a, t.a) && eq(b, t.b);
  }
  
  public String toString() {
    return "<" + a + ", " + b + ">";
  }
}static interface AI_PostHandler {
  String postTriple(T3<String, String, String> triple); // return web ID
}static class WebNode implements _SetField {
  Web web;
  List<Lisp> labels = new ArrayList();
  Object visInfo; // visualisation info
  
  public void _setField(String f, Object x) {
    if (f.equals("web")) web = (Web) x;
    else if (f.equals("labels")) labels = (List) x;
    else if (f.equals("visInfo")) visInfo = x;
  }
  
  WebNode() {}
  WebNode(Web web) {
  this.web = web; web.fireNewNode(this); }
  
  void addLabel(String label) { addLabel(web.parseLabel(label)); }
  
  void addLabel(Lisp label) {
    if (setAdd(labels, label) && web != null)
      web.index(label, this);
  }
  
  void addLabels(Collection<Lisp> l) { for (Lisp lbl : l) addLabel(lbl); }
  void addStrings(Collection<String> l) { for (String lbl : l) addLabel(lbl); }
  
  boolean hasLabel(Lisp label) { return labels.contains(label); }
  boolean hasLabel(String label) { return labels.contains(web.parseLabel(label)); }
  
  public String toString() { return l(labels) == 1 ? str(first(labels)) : str(labels); }
  
  int count() { return 1 + l(labels); }
  String text() { return web.unparseLabel(first(labels)); }
  List<String> texts() { return web.unparseLabels(labels); }
  
  void relation(String arrow, String b) {
    web.getRelation(this, web.node(b)).addLabel(arrow);
  }
  
  Lisp parseLabel(String l) { return web.parseLabel(l); }
  String unparseLabel(Lisp l) { return web.unparseLabel(l); }
  
  List<Lisp> labels() { return labels; }
  boolean hasLabels() { return nempty(labels); }
  void setLabel(int i, Lisp l) { labels.set(i, l); }

  double x, y; // placement on screen

}

static class WebRelation extends WebNode {
  WebNode a, b;
  
  public void _setField(String f, Object x) {
    if (f.equals("a")) a = (WebNode) x;
    else if (f.equals("b")) b = (WebNode) x;
    else super._setField(f, x);
  }
  
  WebRelation() {}
  WebRelation(Web web, WebNode a, WebNode b) { super(web); this.a = a; this.b = b; }
}

static class Web {
  List<WebNode> nodes = new ArrayList(); // Relations are also nodes
  Map<Pair<WebNode, WebNode>, WebRelation> relations = new HashMap();
  MultiMap<Lisp,WebNode> index = new MultiMap(); // label -> node
  //Map<S, L<WebNode>> pots = new Map;
  transient Lock lock = reentrantLock(true);
  //int size;
  //int flags; // TODO
  boolean useCLParse = true;
  boolean labelsToUpper;
  String title, globalID = aGlobalID();
  String source;
  boolean unverified;
  long created = nowUnlessLoading();
  
  static List onNewNode = new ArrayList(); // L<voidfunc(WebNode)>
  static List onNewLabel = new ArrayList(); // L<voidfunc(WebNode, Lisp)>
  
  static int F_useCLParse = 1;
  static int F_labelsToupper = 2;
  static int F_unverified = 3;
  
  /*bool potNotEmpty(S pot) { ret nempty(getPot(pot)); }
  
  L<WebNode> clearPot(S pot) {
    L<WebNode> l = getPot(pot);
    L<WebNode> l2 = cloneList(l);
    l.clear();
    ret l2;
  }*/
  
  /*L<WebNode> getPot(S pot) {
    L<WebNode> l = pots.get(pot);
    if (l == null)
      pots.put(pot, l = cloneList(nodes));
    ret l;
  }*/
  
  void relation(WebNode a, String arrow, WebNode b) {
    getRelation(a, b).addLabel(arrow);
  }
  
  Pair<WebNode, WebNode> relation(String a, String arrow, String b) {
    return relation(lisp(arrow, a, b));
  }
  
  Pair<WebNode, WebNode> relation(Lisp l) {
    if (l(l) == 1) {
      findNode(l.get(0)).addLabel(lisp("wvuyakuvuelmxpwp", l.head));
      return null;
    } else if (l(l) == 2) {
      String a = lisp2label(l.get(0)), b = lisp2label(l.get(1));
      if (l.is("fgvvrzypbkqomktd")) { // X is Y.
        findNode(a).addLabel(b);
        findNode(b).addLabel(a);
      }
      WebNode na = findNode(a), nb = findNode(b);
      getRelation(na, nb).addLabel(l.head);
      return pair(na, nb);
    }
    return null;
  }
  
  void relations(List<Lisp> l) {
    for (Lisp li : l) relation(li);
  }
  
  WebRelation getRelation(String a, String b) {
    return getRelation(findNode(a), findNode(b));
  }
  
  WebRelation getRelation(WebNode a, WebNode b) {
    return getRelation(pair(a, b));
  }
  
  WebRelation relation(WebNode a, WebNode b) { return getRelation(a, b); }
  WebRelation relation(Pair<WebNode, WebNode> p) { return getRelation(p); }
  
  WebRelation getRelationOpt(WebNode a, WebNode b) {
    return relations.get(pair(a, b));
  }
  
  WebRelation getRelation(Pair<WebNode, WebNode> p) {
    WebRelation r = relations.get(p);
    if (r == null) relations.put(p, r = _newRelation(p.a, p.b));
    return r;
  }
  
  WebRelation _newRelation(WebNode a, WebNode b) {
    WebRelation r = new WebRelation(this, a, b);
    nodes.add(r);
    //for (L<WebNode> l : values(pots)) l.add(r);
    return r;
  }
  
  WebNode newNode() {
    WebNode node = new WebNode(this);
    nodes.add(node);
    //for (L<WebNode> l : values(pots)) l.add(node);
    return node;
  }
  
  WebNode newNode(String s) {
    WebNode node = newNode();
    node.addLabel(parseLabel(s));
    return node;
  }
  
  WebNode node(String s) { return findNode(s); }
  WebNode node(Lisp l) { return findNode(l); }

  WebNode findNode(String s) {
    return findNode(parseLabel(s));
  }
  
  WebNode findNode(Lisp l) {
    WebNode n = findNodeOpt(l);
    return n != null ? n : newNode(l);
  }
  
  WebNode findNodeOpt(Lisp l) {
    if (index != null) return first(index.get(l));
    for (WebNode n : nodes) if (n.labels.contains(l)) return n;
    return null;
  }
  
  WebNode newNode(String... labels) {
    WebNode n = newNode();
    for (String label : labels) n.addLabel(label);
    return n;
  }
  
  WebNode newNode(Lisp... labels) {
    WebNode n = newNode();
    for (Lisp label : labels) n.addLabel(label);
    return n;
  }
  
  public String toString() { return webToString(this); }
  
  void index(Lisp label, WebNode n) {
    if (index != null) index.put(label, n);
    fireNewLabel(n, label);
  }
  
  void clear() {
    clearAll(nodes, relations, index/*, pots*/);
  }
  
  Lisp parseLabel(String s) {
    
      if (useCLParse) return clParse(s);
    
    return lisp(labelsToUpper ? upper(s) : s);
  }
  
  String unparseLabel(Lisp l) {
    
      if (useCLParse) return clUnparse(l);
    
    return lispHead(l);
  }
  
  List<Lisp> parseLabels(List<String> l) {
    List<Lisp> x = new ArrayList(l(l));
    for (String s : l) x.add(parseLabel(s));
    return x;
  }
  
  List<String> unparseLabels(List<Lisp> l) {
    List<String> x = new ArrayList(l(l));
    for (Lisp lbl : l) x.add(unparseLabel(lbl));
    return x;
  }
  
  void fireNewNode(WebNode node) { for (Object f : onNewNode) pcallF(f, node); }
  void fireNewLabel(WebNode node, Lisp label) { for (Object f : onNewLabel) pcallF(f, node, label); }
  
  void removeNode(WebNode n) {
    if (n == null || !nodes.contains(n)) return;
    
    n.web = null;
    
    List<Pair<WebNode, WebNode>> relationsToDelete = pairList_lookupAnySide(keys(relations), n);
    
    if (index != null) for (Lisp label : n.labels)
      index.remove(label, n);
    nodes.remove(n);

    for (Pair<WebNode, WebNode> p : relationsToDelete)
      removeRelation(p.a, p.b);
  }
  
  void removeRelation(WebNode a, WebNode b) {
    Pair<WebNode, WebNode> p = pair(a, b);
    WebNode r = relations.get(p);
    if (r == null) return;
    relations.remove(p);
    removeNode(r);
  }
  
  boolean verified() { return !unverified; }
}static abstract class F0<A> {
  abstract A get();
}static abstract class F1<A, B> {
  abstract B get(A a);
}// you still need to implement hasNext() and next()
static abstract class IterableIterator<A> implements Iterator<A>, Iterable<A> {
  public Iterator<A> iterator() {
    return this;
  }
  
  public void remove() {
    unsupportedOperation();
  }
}static class RemoteDB {
  DialogIO db;
  String name;
  
  // s = bot name or snippet ID
  RemoteDB(String s) {
    this(s, false);
  }
  
  RemoteDB(String s, boolean autoStart) {
    name = s;
    if (isSnippetID(s)) name = dbBotName(s);
    db = findBot(name);
    if (db == null)
      if (autoStart) {
        nohupJavax(fsI(s));
        waitForBotStartUp(name);
        assertNotNull("Weird problem", db = findBot(s));
      } else
        throw fail("DB " + s + " not running");
  }

  boolean functional() { return db != null; } // now always true
  
  List<RC> list() { return adopt((List<RC>) rpc(db, "xlist")); }
  List<RC> list(String className) { return adopt((List<RC>) rpc(db, "xlist", className)); }
  List<RC> xlist() { return list(); }
  List<RC> xlist(String className) { return list(className); }
  
  // adopt is an internal method
  List<RC> adopt(List<RC> l) {
    if (l != null) for (RC rc : l) adopt(rc);
    return l;
  }
  
  RC adopt(RC rc) { if (rc != null) rc.db = this; return rc; }
  
  Object adopt(Object o) {
    if (o instanceof RC) return adopt((RC) o);
    return o;
  }
  
  String xclass(RC o) {
    return (String) rpc(db, "xclass", o);
  }
  
  Object xget(RC o, String field) {
    return adopt(rpc(db, "xget", o, field));
  }
  
  String xS(RC o, String field) {
    return (String) xget(o, field);
  }
  
  RC xgetref(RC o, String field) {
    return adopt((RC) xget(o, field));
  }
  
  void xset(RC o, String field, Object value) {
    rpc(db, "xset", o, field, value);
  }
  
  RC uniq(String className) {
    RC ref = first(list(className));
    if (ref == null)
      ref = xnew(className);
    return ref;
  }
  RC xuniq(String className) { return uniq(className); }
  
  RC xnew(String className, Object... values) {
    return adopt((RC) rpc(db, "xnew", className, values));
  }
  
  void xdelete(RC o) {
    rpc(db, "xdelete", o);
  }
  
  void xdelete(List<RC> l) {
    rpc(db, "xdelete", l);
  }

  void close() {
    if (db != null)
      db.close();
  }
  
  String fullgrab() { return (String) rpc(db, "xfullgrab"); }
  String xfullgrab() { return fullgrab(); }
  
  void xshutdown() { rpc(db, "xshutdown"); }
  
  long xchangeCount() { return (long) rpc(db, "xchangeCount"); }
  int xcount() { return (int) rpc(db, "xcount"); }

  void reconnect() {
    close();
    db = findBot(name);
  }
  
  RC rc(long id) { return new RC(this, id); }
}/** this class is fully thread-safe */
static class Flag {
  private boolean up;

  /** returns true if flag was down before */
  public synchronized boolean raise() {
    if (!up) {
      up = true;
      notifyAll();
      return true;
    } else
      return false;
  }

  public synchronized void waitUntilUp() {
    while (!up) {
      try {
        wait();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }

  public synchronized void waitUntilUp(long timeout) {
    if (!up) {
      try {
        wait(timeout);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }

  public synchronized boolean isUp() {
    return up;
  }

  public String toString() {
    return isUp() ? "up" : "down";
  }

  // currently does a semi-active wait with latency = 50 ms
  public void waitForThisOr(Flag otherFlag) { try {
    while (!isUp() && !otherFlag.isUp())
      Thread.sleep(50);
  } catch (Exception __e) { throw rethrow(__e); } }
}static interface Producer<A> {
  public A next();
}static ThreadLocal<Boolean> DynamicObject_loading = new ThreadLocal();

static class DynamicObject {
  String className; // just the name, without the "main$"
  LinkedHashMap<String,Object> fieldValues = new LinkedHashMap();
  
  DynamicObject() {}
  // className = just the name, without the "main$"
  DynamicObject(String className) {
  this.className = className;}
}abstract static class VirtualNodeIndex {
  abstract int numWebs();
  abstract List<WebNode> get(String query);
}static class Cache<A> {
  Object maker; // func -> A
  A value;
  long loaded;
  static boolean debug;
  long changeCount;
  Lock lock = lock();
  
  Cache() {}
  Cache(Object maker) {
  this.maker = maker;}

  A get() {
    if (hasLock(lock)) return value; // Must be called from within maker
    Lock _lock_523 = lock; lock(_lock_523); try {
    if (loaded == 0) {
      value = (A) callF(maker);
      changeCount++;
      loaded = sysNow();
    }
    return value;
  } finally { unlock(_lock_523); } }

  void clear() {
    Lock _lock_524 = lock; lock(_lock_524); try {
    if (debug && loaded != 0)
      print("Clearing cache");
    value = null;
    changeCount++;
    loaded = 0;
  } finally { unlock(_lock_524); } }

  // clear if older than x seconds
  // 0 does not do anything
  void clear(double seconds) {
    Lock _lock_525 = lock; lock(_lock_525); try {
    if (seconds != 0 && loaded != 0 && sysNow() >= loaded+seconds*1000)
      clear();
  } finally { unlock(_lock_525); } }
  
  // override
  void set(A a) {
    Lock _lock_526 = lock; lock(_lock_526); try {
    value = a;
    ++changeCount;
    loaded = sysNow();
  } finally { unlock(_lock_526); } }
}

static class Pt {
  int x, y;
  
  Pt() {}
  Pt(Point p) {
    x = p.x;
    y = p.y;
  }
  Pt(int x, int y) {
  this.y = y;
  this.x = x;}
  
  Point getPoint() {
    return new Point(x, y);
  }
  
  public boolean equals(Object o) { return stdEq2(this, o); }
public int hashCode() { return stdHash2(this); }
  
  public String toString() {
    return x + ", " + y;
  }
}  static abstract class DialogIO {
    String line;
    boolean eos, loud, noClose;
    
    abstract String readLineImpl();
    abstract boolean isStillConnected();
    abstract void sendLine(String line);
    abstract boolean isLocalConnection();
    abstract Socket getSocket();
    abstract void close();
    
    int getPort() { Socket s = getSocket(); return s == null ? 0 : s.getPort(); }
    
    boolean helloRead;
    int shortenOutputTo = 500;
    
    String readLineNoBlock() {
      String l = line;
      line = null;
      return l;
    }
    
    boolean waitForLine() { try {
      if (line != null) return true;
      //print("Readline");
      line = readLineImpl();
      //print("Readline done: " + line);
      if (line == null) eos = true;
      return line != null;
    } catch (Exception __e) { throw rethrow(__e); } }
    
    String readLine() {
      waitForLine();
      helloRead = true;
      return readLineNoBlock();
    }
    
    String ask(String s, Object... args) {
      if (loud) return askLoudly(s, args);
      if (!helloRead) readLine();
      if (args.length != 0) s = format3(s, args);
      sendLine(s);
      return readLine();
    }
    
    String askLoudly(String s, Object... args) {
      if (!helloRead) readLine();
      if (args.length != 0) s = format3(s, args);
      print("> " + shorten(s, shortenOutputTo));
      sendLine(s);
      String answer = readLine();
      print("< " + shorten(answer, shortenOutputTo));
      return answer;
    }
    
    void pushback(String l) {
      if (line != null)
        throw fail();
      line = l;
      helloRead = false;
    }
  }
  
  static abstract class DialogHandler {
    abstract void run(DialogIO io);
  }static class Lowest<A> {
  A best;
  double score;
  transient Object onChange;
  
  boolean isNewBest(double score) {
    return best == null || score < this.score;
  }
  
  double bestScore() {
    return best == null ? Double.NaN : score;
  }
  
  float floatScore() {
    return best == null ? Float.NaN : (float) score;
  }
  
  float floatScoreOr(float defaultValue) {
    return best == null ? defaultValue : (float) score;
  }
  
  boolean put(A a, double score) {
    if (a != null && isNewBest(score)) {
      best = a;
      this.score = score;
      pcallF(onChange);
      return true;
    }
    return false;
  }
  
  A get() { return best; }
  boolean has() { return best != null; }
}static class DoublePt {
  double x, y;
  
  DoublePt() {}
  DoublePt(Point p) {
    x = p.x;
    y = p.y;
  }
  DoublePt(double x, double y) {
  this.y = y;
  this.x = x;}
  
  public boolean equals(Object o) { return stdEq2(this, o); }
public int hashCode() { return stdHash2(this); }
  
  public String toString() {
    return x + ", " + y;
  }
}static interface IVar<A> {
  void set(A a);
  A get();
  boolean has();
  void clear();
}static class ImageSurface extends Surface {
  private BufferedImage image;
  private double zoomX = 1, zoomY = 1;
  private Rectangle selection;
  List tools = new ArrayList();
  Object overlay; // voidfunc(Graphics2D)
  Runnable onSelectionChange;
  static boolean verbose;
  boolean noMinimumSize = true;
  String titleForUpload;
  Object onZoom;
  boolean specialPurposed; // true = don't show image changing commands in popup menu
  boolean zoomable = true;

  public ImageSurface() {
    this(dummyImage());
  }
  
  static BufferedImage dummyImage() {
    return new RGBImage(1, 1, new int[] { 0xFFFFFF }).getBufferedImage();
  }

  public ImageSurface(RGBImage image) {
    this(image != null ? image.getBufferedImage() : dummyImage());
  }

  public ImageSurface(BufferedImage image) {
    setImage(image);
    clearSurface = false;

    componentPopupMenu(this, new Object() { void get(JPopupMenu menu) { try { 
      Point p = pointFromEvent(componentPopupMenu_mouseEvent.get()).getPoint();
      fillPopupMenu(menu, p);
     } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "Point p = pointFromEvent(componentPopupMenu_mouseEvent.get()).getPoint();\r\n  ..."; }});
    
    new ImageSurfaceSelector(this);
    
    jHandleFileDrop(this, new Object() { void get(File f) { try {  setImage(loadBufferedImage(f)) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "setImage(loadBufferedImage(f))"; }});
  }

  public ImageSurface(RGBImage image, double zoom) {
    this(image);
    setZoom(zoom);
  }

  // point is already in image coordinates
  protected void fillPopupMenu(JPopupMenu menu, final Point point) {
    if (zoomable) {
      JMenuItem miZoomReset = new JMenuItem("Zoom 100%");
      miZoomReset.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
          setZoom(1.0);
          centerPoint(point);
        }
      });
      menu.add(miZoomReset);
  
      JMenuItem miZoomIn = new JMenuItem("Zoom in");
      miZoomIn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
          zoomIn(2.0);
          centerPoint(point);
        }
      });
      menu.add(miZoomIn);
  
      JMenuItem miZoomOut = new JMenuItem("Zoom out");
      miZoomOut.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
          zoomOut(2.0);
          centerPoint(point);
        }
      });
      menu.add(miZoomOut);
  
      JMenuItem miZoomToWindow = new JMenuItem("Zoom to window");
      miZoomToWindow.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
          zoomToDisplaySize();
        }
      });
      menu.add(miZoomToWindow);
      addMenuItem(menu, "Show full screen", new Runnable() { public void run() { try {  showFullScreen() ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "showFullScreen()"; }});
      
      addMenuItem(menu, "Point: " + point.x + "," + point.y + " (image: " + image.getWidth() + "*" + image.getHeight() + ")", null);
  
      menu.addSeparator();
    }

    addMenuItem(menu, "Save image...", new Runnable() { public void run() { try {  saveImage() ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "saveImage()"; }});
    addMenuItem(menu, "Upload image...", new Runnable() { public void run() { try {  uploadTheImage() ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "uploadTheImage()"; }});
    addMenuItem(menu, "Copy image to clipboard", new Runnable() { public void run() { try {  copyImageToClipboard(getImage()) ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "copyImageToClipboard(getImage())"; }});
    if (!specialPurposed)
      addMenuItem(menu, "Paste image from clipboard", new Runnable() { public void run() { try {  loadFromClipboard() ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "loadFromClipboard()"; }});
    if (selection != null)
      addMenuItem(menu, "Crop", new Runnable() { public void run() { try {  crop() ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "crop()"; }});
    if (!specialPurposed)
      addMenuItem(menu, "No image", new Runnable() { public void run() { try {  noImage() ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "noImage()"; }});
  }
  
  void noImage() { setImage((BufferedImage) null); }
  
  void crop() {
    if (selection == null) return;
    BufferedImage img = clipBufferedImage(getImage(), selection);
    selection = null;
    setImage(img);
  }
  
  void loadFromClipboard() {
    BufferedImage img = getImageFromClipboard();
    if (img != null)
      setImage(img);
  }

  void saveImage() {
    RGBImage image = new RGBImage(getImage(), null);
    JFileChooser fileChooser = new JFileChooser(getProgramDir());
    if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
      try {
        image.save(fileChooser.getSelectedFile());
      } catch (IOException e) {
        popup(e);
      }
    }
  }

  public void render(int w, int h, Graphics2D g) {
    if (verbose) main.print("render");
    g.setColor(Color.white);
    if (image == null)
      g.fillRect(0, 0, w, h);
    else {
      int iw = getZoomedWidth(), ih = getZoomedHeight();
      boolean alpha = hasTransparency(image);
      if (alpha) g.fillRect(0, 0, w, h);
      g.drawImage(image, 0, 0, iw, ih, null);
      if (!alpha) {
        g.fillRect(iw, 0, w-iw, h);
        g.fillRect(0, ih, iw, h-ih);
      }
    }

    if (verbose) main.print("render overlay");
    if (overlay != null) pcallF(overlay, g);

    if (verbose) main.print("render selection");
    if (selection != null) {
      // drawRect is inclusive, selection is exclusive, so... whatever, tests show it's cool.
      drawSelectionRect(g, selection, Color.green, Color.white);
    }
  }

  public void drawSelectionRect(Graphics2D g, Rectangle selection, Color green, Color white) {
    g.setColor(green);
    int top = (int) (selection.y * zoomY);
    int bottom = (int) ((selection.y+selection.height) * zoomY);
    int left = (int) (selection.x * zoomX);
    int right = (int) ((selection.x+selection.width) * zoomX);
    g.drawRect(left-1, top-1, right-left+1, bottom-top+1);
    g.setColor(white);
    g.drawRect(left - 2, top - 2, right - left + 3, bottom - top + 3);
  }

  public void setZoom(double zoom) {
    setZoom(zoom, zoom);
  }

  public void setZoom(double zoomX, double zoomY) {
    this.zoomX = zoomX;
    this.zoomY = zoomY;
    revalidate();
    repaint();
    centerPoint(new Point(getImage().getWidth()/2, getImage().getHeight()/2));

    pcallF(onZoom);
  }

  public Dimension getMinimumSize() {
    if (noMinimumSize) return new Dimension(1, 1);
    int w = getZoomedWidth();
    int h = getZoomedHeight();
    Dimension min = super.getMinimumSize();
    return new Dimension(Math.max(w, min.width), Math.max(h, min.height));
  }

  private int getZoomedHeight() {
    return (int) (image.getHeight() * zoomY);
  }

  private int getZoomedWidth() {
    return (int) (image.getWidth() * zoomX);
  }

  public void setImage(RGBImage image) {
    setImage(image.getBufferedImage());
  }

  public void setImage(BufferedImage image) {
    this.image = image != null ? image : dummyImage();
    revalidate();
    repaint();
  }

  public BufferedImage getImage() {
    return image;
  }

  public double getZoomX() {
    return zoomX;
  }

  public double getZoomY() {
    return zoomY;
  }

  public Dimension getPreferredSize() {
    return new Dimension(getZoomedWidth(), getZoomedHeight());
  }

  /** returns a scrollpane with the scroll-mode prevent-garbage-drawing fix applied */
  public JScrollPane makeScrollPane() {
    JScrollPane scrollPane = new JScrollPane(this);
    scrollPane.getViewport().setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE);
    return scrollPane;
  }

  public void zoomToWindow() { zoomToDisplaySize(); }
  public void zoomToDisplaySize() {
    if (image == null) return;
    Dimension display = getDisplaySize();
    double xRatio = display.width/(double) image.getWidth();
    double yRatio = display.height/(double) image.getHeight();
    setZoom(Math.min(xRatio, yRatio));
    revalidate();
  }

  /** tricky magic to get parent scroll pane */
  private Dimension getDisplaySize() {
    Container c = getParent();
    while (c != null) {
      if (c instanceof JScrollPane)
        return c.getSize();
      c = c.getParent();
    }
    return getSize();
  }

  public void setSelection(Rectangle r) {
    if (neq(selection, r)) {
      selection = r;
      pcallF(onSelectionChange);
      repaint();
    }
  }

  public Rectangle getSelection() {
    return selection;
  }

  public RGBImage getRGBImage() {
    return new RGBImage(getImage());
  }
  
  // p is in image coordinates
  void centerPoint(Point p) {
    JScrollPane sp = enclosingScrollPane(this);
    if (sp == null) return;
      
    p = new Point((int) (p.x*getZoomX()), (int) (p.y*getZoomY()));
    final JViewport viewport = sp.getViewport();
    Dimension viewSize = viewport.getExtentSize();
    
    //_print("centerPoint " + p);
    int x = max(0, p.x-viewSize.width/2);
    int y = max(0, p.y-viewSize.height/2);
    
    //_print("centerPoint " + p + " => " + x + "/" + y);
    p = new Point(x,y);
    //_print("centerPoint " + p);
    final Point _p = p;
    awtLater(new Runnable() { public void run() { try { 
      viewport.setViewPosition(_p);
    
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "viewport.setViewPosition(_p);"; }});
  }
  
  Pt pointFromEvent(MouseEvent e) {
    return pointFromComponentCoordinates(new Pt(e.getX(), e.getY()));
  }
  
  Pt pointFromComponentCoordinates(Pt p) {
    return new Pt((int) (p.x/zoomX), (int) (p.y/zoomY));
  }
  
  void uploadTheImage() {
    call(hotwire("#1007313"), "go", getImage(), titleForUpload);
  }
  
  void showFullScreen() {
    showFullScreenImageSurface(getImage());
  }
  
  void zoomIn(double f) { setZoom(getZoomX()*f, getZoomY()*f); }
  void zoomOut(double f) { setZoom(getZoomX()/f, getZoomY()/f); }
}static interface _SetField {
  void _setField(String name, Object value);
}

static class ImageSurfaceSelector extends MouseAdapter {
  ImageSurface is;
  Point startingPoint;
  boolean enabled = true;
  static boolean verbose = false;

  ImageSurfaceSelector(ImageSurface is) {
  this.is = is;
    if (containsInstance(is.tools, ImageSurfaceSelector.class)) return;
    is.tools.add(this);
    is.addMouseListener(this);
    is.addMouseMotionListener(this);
  }

  public void mousePressed(MouseEvent evt) {
    if (verbose) print("mousePressed");
    if (evt.getButton() != MouseEvent.BUTTON1) return;
    if (enabled)
      startingPoint = getPoint(evt);
  }

  public void mouseDragged(MouseEvent e) {
    if (verbose) print("mouseDragged");
    if (startingPoint != null) {
      Point endPoint = getPoint(e);
      Rectangle r = new Rectangle(startingPoint, new Dimension(endPoint.x-startingPoint.x+1, endPoint.y-startingPoint.y+1));
      normalize(r);
      r.width = min(r.width, is.getImage().getWidth()-r.x);
      r.height = min(r.height, is.getImage().getHeight()-r.y);
      is.setSelection(r);
    }
    if (verbose) print("mouseDragged done");
  }

  public static void normalize(Rectangle r) {
    if (r.width < 0) {
      r.x += r.width;
      r.width = -r.width;
    }
    if (r.height < 0) {
      r.y += r.height;
      r.height = -r.height;
    }
  }

  public void mouseReleased(MouseEvent e) {
    if (verbose) print("mouseReleased");
    mouseDragged(e);
    if (getPoint(e).equals(startingPoint))
      is.setSelection(null);
    startingPoint = null;
  }
  
  Point getPoint(MouseEvent e) {
    return new Point((int) (e.getX()/is.getZoomX()), (int) (e.getY()/is.getZoomY()));
  }
}static class RGB {
  public float r, g, b; // can't be final cause persistence
  
  RGB() {}
  
  public RGB(float r, float g, float b) {
    this.r = r;
    this.g = g;
    this.b = b;
  }

  public RGB(double r, double g, double b) {
    this.r = (float) r;
    this.g = (float) g;
    this.b = (float) b;
  }

  public RGB(int rgb) {
    this(new Color(rgb));
  }
  
  public RGB(double brightness) {
    this.r = this.g = this.b = max(0f, min(1f, (float) brightness));
  }

  public RGB(Color color) {
    this.r = color.getRed()/255f;
    this.g = color.getGreen()/255f;
    this.b = color.getBlue()/255f;
  }

  public RGB(String hex) {
    r = Integer.parseInt(hex.substring(0, 2), 16)/255f;
    g = Integer.parseInt(hex.substring(2, 4), 16)/255f;
    b = Integer.parseInt(hex.substring(4, 6), 16)/255f;
  }

  public float getComponent(int i) {
    return i == 0 ? r : i == 1 ? g : b;
  }

  public Color getColor() {
    return new Color(r, g, b);
  }

  public static RGB newSafe(float r, float g, float b) {
    return new RGB(Math.max(0, Math.min(1, r)), Math.max(0, Math.min(1, g)), Math.max(0, Math.min(1, b)));
  }

  int asInt() { return getColor().getRGB() & 0xFFFFFF; }
  int getInt() { return getColor().getRGB() & 0xFFFFFF; }

  public float getBrightness() {
    return (r+g+b)/3.0f;
  }

  public String getHexString() {
    return Integer.toHexString(asInt() | 0xFF000000).substring(2).toUpperCase();
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof RGB)) return false;

    RGB rgb = (RGB) o;

    if (Float.compare(rgb.b, b) != 0) return false;
    if (Float.compare(rgb.g, g) != 0) return false;
    if (Float.compare(rgb.r, r) != 0) return false;

    return true;
  }

  @Override
  public int hashCode() {
    int result = (r != +0.0f ? Float.floatToIntBits(r) : 0);
    result = 31 * result + (g != +0.0f ? Float.floatToIntBits(g) : 0);
    result = 31 * result + (b != +0.0f ? Float.floatToIntBits(b) : 0);
    return result;
  }

  public boolean isBlack() {
    return r == 0f && g == 0f && b == 0f;
  }

  public boolean isWhite() {
    return r == 1f && g == 1f && b == 1f;
  }

  public String toString() {
    return getHexString();
  }
}

static class RGBImage {
  transient BufferedImage bufferedImage;
  File file;
  int width, height;
  int[] pixels;

  RGBImage() {}

  RGBImage(BufferedImage image) {
    this(image, null);
  }

  RGBImage(BufferedImage image, File file) {
    this.file = file;
    bufferedImage = image;
    width = image.getWidth();
    height = image.getHeight();
    pixels = new int[width*height];
    PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width);
    try {
      if (!pixelGrabber.grabPixels())
        throw new RuntimeException("Could not grab pixels");
      cleanPixels(); // set upper byte to 0
    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    }
  }

  /** We assume it's a file name to load from */
  RGBImage(String file) throws IOException {
    this(new File(file));
  }

  RGBImage(Dimension size, Color color) {
    this(size.width, size.height, color);
  }

  RGBImage(Dimension size, RGB color) {
    this(size.width, size.height, color);
  }

  private void cleanPixels() {
    for (int i = 0; i < pixels.length; i++)
      pixels[i] &= 0xFFFFFF;
  }

  RGBImage(int width, int height, int[] pixels) {
    this.width = width;
    this.height = height;
    this.pixels = pixels;
  }

  RGBImage(int w, int h, RGB[] pixels) {
    this.width = w;
    this.height = h;
    this.pixels = asInts(pixels);
  }

  public static int[] asInts(RGB[] pixels) {
    int[] ints = new int[pixels.length];
    for (int i = 0; i < pixels.length; i++)
      ints[i] = pixels[i] == null ? 0 : pixels[i].getColor().getRGB();
    return ints;
  }

  public RGBImage(int w, int h) {
    this(w, h, Color.black);
  }
  
  RGBImage(int w, int h, RGB rgb) {
    this.width = w;
    this.height = h;
    this.pixels = new int[w*h];
    int col = rgb.asInt();
    if (col != 0)
      for (int i = 0; i < pixels.length; i++)
        pixels[i] = col;
  }

  RGBImage(RGBImage image) {
    this(image.width, image.height, copyPixels(image.pixels));
  }

  RGBImage(int width, int height, Color color) {
    this(width, height, new RGB(color));
  }

  RGBImage(File file) throws IOException {
    this(javax.imageio.ImageIO.read(file));
  }

  private static int[] copyPixels(int[] pixels) {
    int[] copy = new int[pixels.length];
    System.arraycopy(pixels, 0, copy, 0, pixels.length);
    return copy;
  }

  public int getIntPixel(int x, int y) {
    if (inRange(x, y))
      return pixels[y * width + x];
    else
      return 0xFFFFFF;
  }

  public static RGB asRGB(int packed) {
    int r = (packed >> 16) & 0xFF;
    int g = (packed >> 8) & 0xFF;
    int b = packed & 0xFF;
    return new RGB(r / 255f, g / 255f, b / 255f);
  }

  public RGB getRGB(int x, int y) {
    if (inRange(x, y))
      return asRGB(pixels[y * width + x]);
    else
      return new RGB(0xFFFFFF);
  }

  /** alias of getRGB - I kept typing getPixel instead of getRGB all the time, so I finally created it */
  RGB getPixel(int x, int y) {
    return getRGB(x, y);
  }
  
  RGB getPixel(Pt p) { return getPixel(p.x, p.y); }

  int getWidth() { return width; }
  int getHeight() { return height; }
  int w() { return width; }
  int h() { return height; }

  /** Attention: cached, i.e. does not change when image itself changes */
  /** @NotNull */
  public BufferedImage getBufferedImage() {
    if (bufferedImage == null) {
      bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      //bufferedImage.setData(Raster.createRaster(new SampleModel()));
      for (int y = 0; y < height; y++)
        for (int x = 0; x < width; x++)
          bufferedImage.setRGB(x, y, pixels[y*width+x]);
    }
    return bufferedImage;
  }

  RGBImage clip(Rect r) {
    return r == null ? null : clip(r.getRectangle());
  }
  
  RGBImage clip(Rectangle r) {
    r = fixClipRect(r);
    int[] newPixels;
    try {
      newPixels = new int[r.width*r.height];
    } catch (RuntimeException e) {
      System.out.println(r);
      throw e;
    }
    for (int y = 0; y < r.height; y++) {
      System.arraycopy(pixels, (y+r.y)*width+r.x, newPixels, y*r.width, r.width);
    }
    return new RGBImage(r.width, r.height, newPixels);
  }

  private Rectangle fixClipRect(Rectangle r) {
    r = r.intersection(new Rectangle(0, 0, width, height));
    if (r.isEmpty())
      r = new Rectangle(r.x, r.y, 0, 0);
    return r;
  }

  public File getFile() {
    return file;
  }

  /** can now also do GIF (not just JPEG) */
  public static RGBImage load(String fileName) {
    return load(new File(fileName));
  }

  /** can now also do GIF (not just JPEG) */
  public static RGBImage load(File file) {
    try {
      BufferedImage bufferedImage = javax.imageio.ImageIO.read(file);
      return new RGBImage(bufferedImage);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  public int getInt(int x, int y) {
    return pixels[y * width + x];
  }

  public void save(File file) throws IOException {
    String name = file.getName().toLowerCase();
    String type;
    if (name.endsWith(".png")) type = "png";
    else if (name.endsWith(".jpg") || name.endsWith(".jpeg")) type = "jpeg";
    else throw new IOException("Unknown image extension: " + name);
    javax.imageio.ImageIO.write(getBufferedImage(), type, file);
  }

  public static RGBImage dummyImage() {
    return new RGBImage(1, 1, new int[] {0xFFFFFF});
  }

  public int[] getPixels() {
    return pixels;
  }

  public void setPixel(int x, int y, RGB rgb) {
    if (x >= 0 && y >= 0 && x < width && y < height)
      pixels[y*width+x] = rgb.asInt();
  }

  public void setPixel(int x, int y, Color color) {
    setPixel(x, y, new RGB(color));
  }

  public void setPixel(int x, int y, int rgb) {
    if (x >= 0 && y >= 0 && x < width && y < height)
      pixels[y*width+x] = rgb;
  }
  
  void setPixel(Pt p, RGB rgb) { setPixel(p.x, p.y, rgb); }
  void setPixel(Pt p, Color color) { setPixel(p.x, p.y, color); }

  public RGBImage copy() {
    return new RGBImage(this);
  }

  public boolean inRange(int x, int y) {
    return x >= 0 && y >= 0 && x < width && y < height;
  }

  public Dimension getSize() {
    return new Dimension(width, height);
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    RGBImage rgbImage = (RGBImage) o;

    if (height != rgbImage.height) return false;
    if (width != rgbImage.width) return false;
    if (!Arrays.equals(pixels, rgbImage.pixels)) return false;

    return true;
  }

  @Override
  public int hashCode() {
    int result = width;
    result = 31 * result + height;
    result = 31 * result + Arrays.hashCode(pixels);
    return result;
  }

  public String getHex(int x, int y) {
    return getPixel(x, y).getHexString();
  }

  public RGBImage clip(int x, int y, int width, int height) {
    return clip(new Rectangle(x, y, width, height));
  }

  public RGBImage clipLine(int y) {
    return clip(0, y, width, 1);
  }

  public int numPixels() {
    return width*height;
  }
}

abstract static class Surface extends JPanel {
  public boolean clearSurface = true;
  private boolean clearOnce;

  Surface() {
    setDoubleBuffered(false);
  }

  Graphics2D createGraphics2D(int width, int height, Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setBackground(getBackground());
    if (clearSurface || clearOnce) {
      g2.clearRect(0, 0, width, height);
      clearOnce = false;
    }
    return g2;
  }

  public abstract void render(int w, int h, Graphics2D g);

  public void paintImmediately(int x,int y,int w, int h) {
    RepaintManager repaintManager = null;
    boolean save = true;
    if (!isDoubleBuffered()) {
      repaintManager = RepaintManager.currentManager(this);
      save = repaintManager.isDoubleBufferingEnabled();
      repaintManager.setDoubleBufferingEnabled(false);
    }
    super.paintImmediately(x, y, w, h);

    if (repaintManager != null)
      repaintManager.setDoubleBufferingEnabled(save);
  }

  public void paint(Graphics g) {
    Dimension d = getSize();
    Graphics2D g2 = createGraphics2D(d.width, d.height, g);
    render(d.width, d.height, g2);
    g2.dispose();
  }
}


static class Rect {
  int x, y, w, h;
  
  Rect() {}
  Rect(Rectangle r) {
    x = r.x;
    y = r.y;
    w = r.width;
    h = r.height;
  }
  Rect(int x, int y, int w, int h) {
  this.h = h;
  this.w = w;
  this.y = y;
  this.x = x;}
  
  Rectangle getRectangle() {
    return new Rectangle(x, y, w, h);
  }
  
  public boolean equals(Object o) { return stdEq2(this, o); }
public int hashCode() { return stdHash2(this); }
  
  public String toString() {
    return x + "," + y + " / " + w + "," + h;
  }
  
  int x2() { return x + w; }
  int y2() { return y + h; }
  
  boolean contains(Pt p) {
    return contains(p.x, p.y);
  }
  
  boolean contains(int _x, int _y) {
    return _x >= x && _y >= y && _x < x+w && _y < y+h;
  }
  
  boolean empty() { return w <= 0 || h <= 0; }
}

static UnsupportedOperationException unsupportedOperation() {
  throw new UnsupportedOperationException();
}
static String quickVisualize_progID = "#1007145";
static Lock quickVisualize_lock = lock();

static boolean quickVisualize_hasCached(String query) {
  return quickVisualize_imageFile(query).length() != 0;
}

static BufferedImage quickVisualize_fromCache(String query) {
  File f = quickVisualize_imageFile(query);
  if (f.length() != 0) try { return loadPNG(f); } catch (Throwable __e) { printStackTrace2(__e); }
  return null;
}

static String quickVisualize_preprocess(String query) {
  return toUpper(shorten(trim(query), 200));
}

static BufferedImage quickVisualize(String query) {
  query = quickVisualize_preprocess(query);
  if (empty(query)) return null;
  BufferedImage img = quickVisualize_fromCache(query);
  if (img != null) return img;
  File f = quickVisualize_imageFile(query);
  /*L<S> urls = googleImageSearch_multi(query);
  saveTextFile(quickVisualize_urlsFile(query), joinLines(urls));
  if (empty(urls)) null;
  img = loadBufferedImage(first(urls));*/
  Lock _lock_545 = quickVisualize_lock; lock(_lock_545); try {
  img = googleImageSearchFirst(query);
  if (img == null) return null;
  savePNG(f, img);
  return img;
} finally { unlock(_lock_545); } }

static String quickVisualize_imagePath(String query) {
  query = quickVisualize_preprocess(query);
  return fsI(quickVisualize_progID) + "/" + urlencode(query) + ".png";
}

static File quickVisualize_imageFile(String query) {
  query = quickVisualize_preprocess(query);
  return prepareProgramFile(quickVisualize_progID, urlencode(query) + ".png");
}

static File quickVisualize_urlsFile(String query) {
  query = quickVisualize_preprocess(query);
  return prepareProgramFile(quickVisualize_progID, "urls-" + urlencode(query) + ".txt");
}
static int thoughtCircleSize(BufferedImage img) {
  return min(img.getWidth(), img.getHeight()) + 20;
}
static JFrame showFullScreen(final JComponent c) {
  return (JFrame) swingAndWait(new Object() { Object get() { try { 
    GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()
      .getDefaultScreenDevice();
    if (!gd.isFullScreenSupported())
      throw fail("No full-screen mode supported!");
    boolean dec = JFrame.isDefaultLookAndFeelDecorated();
    if (dec) JFrame.setDefaultLookAndFeelDecorated(false);
    final JFrame window = new JFrame();
    window.setUndecorated(true);
    if (dec) JFrame.setDefaultLookAndFeelDecorated(true);
    registerEscape(window, new Runnable() { public void run() { try {  disposeWindow(window) ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "disposeWindow(window)"; }});
    window.add(wrap(c));
    gd.setFullScreenWindow(window);
    
    // Only this hides the task bar in Peppermint Linux w/Substance
    for (int i = 100; i <= 1000; i += 100)
      awtLater(i, new Runnable() { public void run() { try {  window.toFront() ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "window.toFront()"; }});
    
    return window;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()\r\n      ..."; }});
}
static Concept getConcept(long id) {
  return mainConcepts.getConcept(id);
}

static <A extends Concept> A getConcept(Class<A> cc, long id) {
  return getConcept(mainConcepts, cc, id);
}

static <A extends Concept> A getConcept(Concepts concepts, Class<A> cc, long id) {
  Concept c = concepts.getConcept(id);
  if (c == null) return null;
  if (!isInstance(cc, c))
    throw fail("Can't convert concept: " + getClassName(c) + " -> " + getClassName(cc) + " (" + id + ")");
  return (A) c;
}
static Collection<Concept> allConcepts() {
  return mainConcepts.allConcepts();
}

static Collection<Concept> allConcepts(Concepts concepts) {
  return concepts.allConcepts();
}

static <A extends Component> A centerFrame(A c) {
  Window w = getWindow(c);
  if (w != null)
    w.setLocationRelativeTo(null); // magic trick
  return c;
}

static <A extends Component> A centerFrame(int w, int h, A c) {
  return centerFrame(setFrameSize(w, h, c));
}

static class componentPopupMenu_Maker {
  List menuMakers = new ArrayList();
}

static Map<JComponent, componentPopupMenu_Maker> componentPopupMenu_map = new WeakHashMap();

static ThreadLocal<MouseEvent> componentPopupMenu_mouseEvent = new ThreadLocal();

// menuMaker = voidfunc(JPopupMenu)
static void componentPopupMenu(final JComponent component, final Object menuMaker) {
  swingNowOrLater(new Runnable() { public void run() { try { 
    componentPopupMenu_Maker maker = componentPopupMenu_map.get(component);
    if (maker == null) {
      componentPopupMenu_map.put(component, maker = new componentPopupMenu_Maker());
      component.addMouseListener(new componentPopupMenu_Adapter(maker));
    }
    maker.menuMakers.add(menuMaker);
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "componentPopupMenu_Maker maker = componentPopupMenu_map.get(component);\r\n    ..."; }});
}

static class componentPopupMenu_Adapter extends MouseAdapter {
  componentPopupMenu_Maker maker;
  
  componentPopupMenu_Adapter(componentPopupMenu_Maker maker) {
  this.maker = maker;}
  
  public void mousePressed(MouseEvent e) { displayMenu(e); }
  public void mouseReleased(MouseEvent e) { displayMenu(e); }

  void displayMenu(MouseEvent e) {
    if (e.isPopupTrigger()) displayMenu2(e);
  }
    
  void displayMenu2(MouseEvent e) {
    JPopupMenu menu = new JPopupMenu();
    int emptyCount = menu.getComponentCount();
    
    componentPopupMenu_mouseEvent.set(e);
    for (Object menuMaker : maker.menuMakers)
      pcallF(menuMaker, menu);
    
    // show menu if any items in it
    if (menu.getComponentCount() != emptyCount)
      menu.show(e.getComponent(), e.getX(), e.getY());
  }
}
static String b(Object contents, Object... params) {
  return tag("b", contents, params);
}
// firstDelay = delay
static java.util.Timer doEvery_daemon(long delay, final Object r) {
  return doEvery_daemon(delay, delay, r);
}

static java.util.Timer doEvery_daemon(long delay, long firstDelay, final Object r) {
  final java.util.Timer timer = new java.util.Timer(true);
  timer.scheduleAtFixedRate(smartTimerTask(r, timer, delay), firstDelay, delay);
  return timer;
}

static Object swing(Object f) {
  return swingAndWait(f);
}

static <A> A swing(F0<A> f) {
  return (A) swingAndWait(f);
}
static float abs(float f) { return Math.abs(f); }
static int abs(int i) { return Math.abs(i); }
static double abs(double d) { return Math.abs(d); }
static String loadConceptsStructure(String progID) {
  return loadTextFilePossiblyGZipped(getProgramFile(progID, "concepts.structure"));
}

static String loadConceptsStructure() {
  return loadConceptsStructure(dbProgramID());
}


static String copyTextToClipboard(String text) {
  StringSelection selection = new StringSelection(text);
  Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, selection);
  return text;
}
static BufferedImage whiteImage(int w, int h) {
  return newBufferedImage(w, h, Color.white);
}
  static String format3(String pat, Object... args) {
    if (args.length == 0) return pat;
    
    List<String> 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 <A> void remove(List<A> l, int i) {
  if (l != null && i >= 0 && i < l(l))
    l.remove(i);
}

static <A> void remove(Collection<A> l, A a) {
  if (l != null) l.remove(a);
}
static String lines(Collection<String> lines) { return fromLines(lines); }
static List<String> lines(String s) { return toLines(s); }
static int hashCodeFor(Object a) {
  return a == null ? 0 : a.hashCode();
}
static Pt scalePt(Pt p, double f) {
  return new Pt(iround(p.x*f), iround(p.y*f));
}
static String cal_structure(CirclesAndLines cal) {
  return cal_structure_impl(restructure(cal));
}

static String cal_structure_impl(CirclesAndLines cal) {
  if (cal.arrowClass == Arrow.class) cal.arrowClass = null;
  if (cal.circleClass == Circle.class) cal.circleClass = null;
  for (Circle c : cal.circles) cal_structure_simplifyElement(c);
  for (Line l : cal.lines) cal_structure_simplifyElement(l);
  return structure(cal);
}

static void cal_structure_simplifyElement(Base b) {
  if (eq(b.traits, ll(b.text))) b.traits = null;
}
static long toK(long l) {
  return (l+1023)/1024;
}
static long toM(long l) {
  return (l+1024*1024-1)/(1024*1024);
}

static String toM(long l, int digits) {
  return formatDouble(toM_double(l), digits);
}
static void addMenuItem(JPopupMenu menu, String text, Object action) {
  menu.add(jmenuItem(text, action));
}

static void addMenuItem(JPopupMenu menu, JMenuItem menuItem) {
  menu.add(menuItem);
}

static void addMenuItem(JMenu menu, String text, Object action) {
  menu.add(jmenuItem(text, action));
}

static void addMenuItem(JMenu menu, JMenuItem menuItem) {
  menu.add(menuItem);
}

static void clearConcepts() {
  mainConcepts.clearConcepts();
}

static void clearConcepts(Concepts concepts) {
  concepts.clearConcepts();
}
static SortedMap synchroTreeMap() {
  return Collections.synchronizedSortedMap(new TreeMap());
}

static BufferedImage drawThoughtCirclePlus_img;
static int drawThoughtCirclePlus_size = 24;

static void drawThoughtCirclePlus(BufferedImage bg, BufferedImage img, double x, double y) {
  if (drawThoughtCirclePlus_img == null)
    drawThoughtCirclePlus_img = resizeImage(loadImage2("#1009864"), drawThoughtCirclePlus_size);
  x -= drawThoughtCirclePlus_img.getWidth()/2;
  y -= drawThoughtCirclePlus_img.getHeight()/2;
  drawImageOnImage(drawThoughtCirclePlus_img, bg, iround(x), iround(y));
}
static void nohupJavax(final String javaxargs) {
  { Thread _t_0 = new Thread() {
public void run() { try { call(hotwireOnce("#1008562"), "nohupJavax", javaxargs); } catch (Throwable __e) { printStackTrace2(__e); } }
};
startThread(_t_0); }
}

static void nohupJavax(final String javaxargs, final String vmArgs) {
  { Thread _t_1 = new Thread() {
public void run() { try { call(hotwireOnce("#1008562"), "nohupJavax", javaxargs, vmArgs); } catch (Throwable __e) { printStackTrace2(__e); } }
};
startThread(_t_1); }
}
static String getDBProgramID_id;

static String getDBProgramID() {
  return nempty(getDBProgramID_id) ? getDBProgramID_id : programIDWithCase();
}
static double pointDistance(Pt a, Pt b) {
  return sqrt(sqr(a.x-b.x) + sqr(a.y-b.y));
}

static double pointDistance(double x1, double y1, double x2, double y2) {
  return sqrt(sqr(x1-x2) + sqr(y1-y2));
}
static boolean hasType(Collection c, Class type) {
  for (Object x : c)
    if (isInstanceX(type, x))
      return true;
  return false;
}
static void cleanKillVM() {
  ping();
  cleanKillVM_noSleep();
  sleep();
}

static void cleanKillVM_noSleep() {
  call(getJavaX(), "cleanKill");
}
static String javaTokWordWrap(String s) {
  int cols = 120, col = 0;
  List<String> tok = javaTok(s);
  for (int i = 0; i < l(tok); i++) {
    String t = tok.get(i);
    if (odd(i) && col >= cols && !containsNewLine(t))
      tok.set(i, t += "\n");
    int idx = t.lastIndexOf('\n');
    if (idx >= 0) col = l(t)-(idx+1);
    else col += l(t);
  }
  return join(tok);
}
static JWindow infoBox(String text) {
  return infoMessage(text);
}

static JWindow infoBox(String text, double seconds) {
  return infoMessage(text, seconds);
}

static JWindow infoBox(Throwable e) {
  return infoMessage(e);
}
static boolean inRange(int x, int n) {
  return x >= 0 && x < n;
}
static DoublePt translateDoublePt(Pt a, DoublePt b) {
  return a == null ? b : b == null ? new DoublePt(a.x, a.y) : new DoublePt(a.x+b.x, a.y+b.y);
}
static Object rpc(String botName, String method, Object... args) {
  return unstructure_debug(matchOK2OrFail(
    sendToLocalBot(botName, rpc_makeCall(method, args))));
}

static Object rpc(DialogIO bot, String method, Object... args) {
  return unstructure_debug(matchOK2OrFail(
    bot.ask(rpc_makeCall(method, args))));
}

static String rpc_makeCall(String method, Object... args) {
  if (empty(args))
    return "call " + method;
  return format("call *", concatLists((List) ll(method), asList(args)));
}
// can probably be merged with next variant
static void disposeWindow(final Window window) {
  if (window != null) { swing(new Runnable() { public void run() { try { 
    window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING)); // call listeners
    window.dispose();
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING)); //..."; }}); }
}

static void disposeWindow(final Component c) {
  disposeWindow(getWindow(c));
}

static void disposeWindow(Object o) {
  if (o != null) disposeWindow((Component) o);
}
static int drawThoughtCircleText_margin = 5;
static Color drawThoughtCircleText_color = Color.yellow;

static void drawThoughtCircleText(BufferedImage bg, BufferedImage img, DoublePt p, String text) {
  Graphics2D g = imageGraphics(bg);
  g.setFont(sansSerifBold(20));
  FontMetrics fm = g.getFontMetrics();
  int h = fm.getHeight();
  double y = p.y+thoughtCircleSize(img)/2+drawThoughtCircleText_margin;
  for (String s : lines(text)) {
    drawTextWithOutline(g, s, (float) (p.x-fm.stringWidth(s)/2), (float) (y+fm.getLeading()+fm.getMaxAscent()), drawThoughtCircleText_color, Color.black);
    y += h;
  }
  g.dispose();
}
static String aGlobalID() {
  return randomID(16);
}
static boolean containsInstance(Iterable i, Class c) {
  if (i != null) for (Object o : i)
    if (isInstanceX(c, o))
      return true;
  return false;
}
static <A> A firstOfType(Collection c, Class<A> type) {
  for (Object x : c)
    if (isInstanceX(type, x))
      return (A) x;
  return null;
}
static String quoteIfNotIdentifier(String s) {
  if (s == null) return null;
  return isJavaIdentifier(s) ? s : quote(s);
}
static JWindow showLoadingAnimation() {
  return showLoadingAnimation("Hold on user...");
}

static JWindow showLoadingAnimation(String text) {
  return showAnimationInTopRightCorner("#1003543", text);
}
static void markWebsPosted() {
  markWebsPosted_createMarker();
  triggerWebsChanged();
}
static String lispHead(Lisp l) {
  return l == null ? null : l.head;
}
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 Concept cnew(Concepts concepts, String name, Object... values) {
  Class<? extends Concept> cc = findClass(name);
  concepts_unlisted.set(true);
  Concept c;
  try {
    c = cc != null ? nuObject(cc) : new Concept(name);
  } finally {
    concepts_unlisted.set(null);
  }
  concepts.register(c);
  csetAll(c, values);
  return c;
}

static <A extends Concept> A cnew(Class<A> cc, Object... values) {
  A c = nuObject(cc);
  csetAll(c, values);
  return c;
}

static <A extends Concept> A cnew(Concepts concepts, Class<A> cc, Object... values) {
  concepts_unlisted.set(true);
  A c;
  try {
    c = nuObject(cc);
  } finally {
    concepts_unlisted.set(null);
  }
  concepts.register(c);
  csetAll(c, values);
  return c;
}

static <A> void addAll(Collection<A> c, Collection<A> b) {
  if (c != null && b != null) c.addAll(b);
}

static <A> void addAll(Collection<A> c, A... b) {
  if (c != null) c.addAll(Arrays.asList(b));
}
static boolean loadBufferedImage_useImageCache = true;

static BufferedImage loadBufferedImage(String snippetIDOrURL) { try {
  if (snippetIDOrURL == null) return null;
  if (isURL(snippetIDOrURL))
    return ImageIO.read(new URL(snippetIDOrURL));

  if (!isSnippetID(snippetIDOrURL)) throw fail("Not a URL or snippet ID: " + snippetIDOrURL);
  String snippetID = "" + parseSnippetID(snippetIDOrURL);
  
  File dir = getCacheProgramDir("Image-Snippets");
  if (loadBufferedImage_useImageCache) {
    dir.mkdirs();
    File file = new File(dir, snippetID + ".png");
    if (file.exists() && file.length() != 0)
      try {
        return ImageIO.read(file);
      } catch (Throwable e) {
        e.printStackTrace();
        // fall back to loading from sourceforge
      }
  }

  String imageURL = snippetImageURL(snippetID);
  System.err.println("Loading image: " + imageURL);
  BufferedImage image = ImageIO.read(new URL(imageURL));

  if (loadBufferedImage_useImageCache) {
    File tempFile = new File(dir, snippetID + ".tmp." + System.currentTimeMillis());
    ImageIO.write(image, "png", tempFile);
    tempFile.renameTo(new File(dir, snippetID + ".png"));
    //Log.info("Cached image.");
  }

  //Log.info("Loaded image.");
  return image;
} catch (Exception __e) { throw rethrow(__e); } }

static BufferedImage loadBufferedImage(File file) { try {
  return file.isFile() ? ImageIO.read(file) : null;
} catch (Exception __e) { throw rethrow(__e); } }
static String lisp2label(Lisp l) {
  return l.isLeaf() ? l.head : clUnparse(l);
}
static boolean bareDBMode_on;

static void bareDBMode() {
  bareDBMode(null); // default autoSaveInterval
}

static void bareDBMode(Integer autoSaveInterval) {
  bareDBMode_on = true;
  conceptsAndBot(autoSaveInterval);
}
static void moveAllMenuItems(JPopupMenu src, JMenu dest) {
  Component[] l = src.getComponents();
  src.removeAll();
  for (Component c : l)
    dest.add(c);
}
static boolean has(String a, String b, String c) {
  return ai_cache_hasTriple(a, b, c);
}

static boolean has(T3<String, String, String> t) {
  return ai_cache_hasTriple(t);
}
static <A> List<Pair<A, A>> pairList_lookupAnySide(Collection<Pair<A, A>> l, A a) {
  List < Pair < A , A > > out = new ArrayList();
  for (Pair<A, A> p : unnull(l))
    if (eq(p.a, a) || eq(p.b, a)) out.add(p);
  return out;
}
static Str concept(String name) {
  for (Str s : list(Str.class))
    if (eqic(s.name, name) || containsIgnoreCase(s.otherNames, name))
      return s;
  return new Str(name);
}
static int ptY(Pt p) {
  return p == null ? 0 : p.y;
}
static int ptX(Pt p) {
  return p == null ? 0 : p.x;
}
static BufferedImage clipBufferedImage(BufferedImage src, Rectangle clip) {
  return src.getSubimage(clip.x, clip.y, clip.width, clip.height);
}

static BufferedImage clipBufferedImage(BufferedImage src, Rect clip) {
  return clipBufferedImage(src, clip.getRectangle());
}

static BufferedImage clipBufferedImage(BufferedImage src, int x, int y, int w, int h) {
  return src.getSubimage(x, y, w, h);
}

static int hours() {
  return hours(Calendar.getInstance());
}

static int hours(Calendar c) {
  return c.get(Calendar.HOUR_OF_DAY);
}
static String _compactString(String s) {
  return s;
}
static void clearAll(Object... l) {
  for (Object o : l) callOpt(o, "clear");
}
static <A extends Concept> A findBackRef(Concept c, Class<A> type) {
  for (Concept.Ref r : c.backRefs)
    if (instanceOf(r.concept(), type))
      return (A) r.concept();
  return null;
}

static <A extends Concept> A findBackRef(Class<A> type, Concept c) {
  return findBackRef(c, type);
}
static JScrollPane enclosingScrollPane(Component c) {
  while (c.getParent() != null && !(c.getParent() instanceof JViewport) && c.getParent().getComponentCount() == 1) c = c.getParent(); // for jscroll_center
  if (!(c.getParent() instanceof JViewport)) return null;
  c = c.getParent().getParent();
  return c instanceof JScrollPane ? (JScrollPane) c : null;
}
static <A> List<A> removeDyn(List<A> l, A a) {
  if (l == null) return null;
  l.remove(a);
  return empty(l) ? null : l;
}
static int _hashCode(Object a) {
  return a == null ? 0 : a.hashCode();
}
static int iround(double d) {
  return (int) Math.round(d);
}
static String formatInt(int i, int digits) {
  return padLeft(str(i), '0', digits);
}

static String formatInt(long l, int digits) {
  return padLeft(str(l), '0', digits);
}
static <A> A findWhere(Collection<A> c, Object... data) {
  for (A x : c)
    if (checkFields(x, data))
      return x;
  return null;
}
// independent timer
static void awtLater(int delay, final Object r) {
  swingLater(delay, r);
}

static void awtLater(Object r) {
  swingLater(r);
}

// dependent timer (runs only when component is visible)
static void awtLater(JComponent component, int delay, Object r) {
  installTimer(component, r, delay, delay, false);
}

static void awtLater(JFrame frame, int delay, Object r) {
  awtLater(frame.getRootPane(), delay, r);
}
static class Canvas extends ImageSurface {
  Object makeImg;
  boolean updating;
  
  Canvas() { zoomable = false; }
  Canvas(Object makeImg) { this(); this.makeImg = makeImg; }
  
  void update() { updateCanvas(this, makeImg); }
}

static Canvas jcanvas() { return jcanvas(null, 0); }

// f: (int w, int h) -> BufferedImage
static Canvas jcanvas(Object f) {
  return jcanvas(f, 0); // 100
}

static Canvas jcanvas(final Object f, final int updateDelay) {
  return (Canvas) swing(new Object() { Object get() { try { 
    final Canvas is = new Canvas(f);
    is.specialPurposed = true;
    final Runnable update = new Runnable() {
      boolean first = true;
      public void run() {
        BufferedImage img = is.getImage();
        int w = is.getWidth(), h = is.getHeight();
        if (first || img.getWidth() != w || img.getHeight() != h) {
          updateCanvas(is, f);
          first = false;
        }
      }
    };
    onResize(is, new Runnable() { public void run() { try {  awtLater(is, updateDelay, update) ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "awtLater(is, updateDelay, update)"; }});
    bindToComponent(is, update); // first update
    return is;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "final Canvas is = new Canvas(f);\r\n    is.specialPurposed = true;\r\n    final R..."; }});
}
static <A> List<A> filterByType(Collection c, Class<A> type) {
  List<A> l = new ArrayList();
  for (Object x : c)
    if (isInstanceX(type, x))
      l.add((A) x);
  return l;
}

static <A> List<A> filterByType(Object[] c, Class<A> type) {
  return filterByType(asList(c), type);
}

static int done_minPrint = 10;

static long done(long startTime, String desc) {
  long time = now()-startTime;
  if (time >= done_minPrint)
    print(desc + " [" + time + " ms]");
  return time;
}

static long done(String desc, long startTime) {
  return done(startTime, desc);
}

static long done(long startTime) {
  return done(startTime, "");
}
static void readLocally(String progID, String varNames) {
  readLocally2(mc(), progID, varNames);
}

static void readLocally(String varNames) {
  readLocally2(mc(), programID(), varNames);
}

static void readLocally2(Object obj, String varNames) {
  readLocally2(obj, programID(), varNames);
}

static int readLocally_stringLength;

static ThreadLocal<Boolean> readLocally2_allDynamic = new ThreadLocal();

// read a string variable from standard storage
// does not overwrite variable contents if there is no file
static void readLocally2(Object obj, String progID, String varNames) { try {
  boolean allDynamic = isTrue(getAndClearThreadLocal(readLocally2_allDynamic));
  for (String variableName : javaTokC(varNames)) {
    File textFile = new File(programDir(progID), variableName + ".text");
    
    String value = loadTextFile(textFile);
    if (value != null)
      set(main.class, variableName, value);
    else {
      File structureFile = new File(programDir(progID), variableName + ".structure");
      value = loadTextFile(structureFile);
      
      if (value == null) {
        File structureGZFile = new File(programDir(progID), variableName + ".structure.gz");
        if (!structureGZFile.isFile()) return;
        //value = loadGZTextFile(structureGZFile);
        InputStream fis = new FileInputStream(structureGZFile);
        try {
          GZIPInputStream gis = new GZIPInputStream(fis);
          InputStreamReader reader = new InputStreamReader(gis, "UTF-8");
          BufferedReader bufferedReader = new BufferedReader(reader);
          //O o = unstructure_reader(bufferedReader);
          Object o = unstructure_tok(javaTokC_noMLS_onReader(bufferedReader), allDynamic, null);
          readLocally_set(obj, variableName, o);
        } finally {
          fis.close();
        }
        return;
      }
      
      readLocally_stringLength = l(value);
      if (nempty(value))
        readLocally_set(obj, variableName, allDynamic ? safeUnstructure(value) : unstructure(value));
    }
  }
} catch (Exception __e) { throw rethrow(__e); } }

static void readLocally_set(Object c, String varName, Object value) {
  Object oldValue = get(c, varName);
  if (oldValue instanceof List && !(oldValue instanceof ArrayList) && value != null) {
    // Assume it's a synchroList.
    value = synchroList((List) value);
  }
  set(c, varName, value);
}

static <A> void setAddAll(List<A> a, Collection<A> b) {
  for (A x : b)
    setAdd(a, x);
}
static JFrame getFrame(final Object _o) {
  return swing(new F0<JFrame>() { JFrame get() { try { 
    Object o = _o;
    if (o instanceof ButtonGroup) o = first(buttonsInGroup((ButtonGroup) o));
    if (!(o instanceof Component)) return null;
    Component c = (Component) o;
    while (c != null) {
      if (c instanceof JFrame) return (JFrame) c;
      c = c.getParent();
    }
    return null;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "O o = _o;\r\n    if (o instanceof ButtonGroup) o = first(buttonsInGroup((Button..."; }});
}
static void copyImageToClipboard(Image img) {
  TransferableImage trans = new TransferableImage(img);
  Toolkit.getDefaultToolkit().getSystemClipboard().setContents( trans, null);
  print("Copied image to clipboard (" + img.getWidth(null) + "*" + img.getHeight(null) + " px)");
}
static JLabel jlabel(final String text) {
  return swingConstruct(BetterLabel.class, text);
}

static JLabel jlabel() {
  return jlabel(" ");
}
static String ymd() {
  return year() + formatInt(month(), 2) + formatInt(dayOfMonth(), 2);
}
static boolean containsIC(Collection<String> l, String s) {
  return containsIgnoreCase(l, s);
}

static boolean containsIC(String[] l, String s) {
  return containsIgnoreCase(l, s);
}

static boolean containsIC(String s, char c) {
  return containsIgnoreCase(s, c);
}

static boolean containsIC(String a, String b) {
  return containsIgnoreCase(a, b);
}
static int asInt(Object o) {
  return toInt(o);
}

static int drawThoughtLineText_shift = 10;
static void drawThoughtLineText_multiLine(BufferedImage bg,
  BufferedImage img1, int x1, int y1,
  BufferedImage img2, int x2, int y2,
  String text, Color color) {
  
  Graphics2D g = imageGraphics(bg);
  g.setColor(color);
  g.setFont(sansSerif(20));
  
  int w1 = img_minOfWidthAndHeight(img1);
  int w2 = img_minOfWidthAndHeight(img2);
  int sideShift = (w1-w2)/4;
  int len = vectorLength(x2-x1, y2-y1);
  int dx = iround(sideShift*(x2-x1)/len), dy = iround(sideShift*(y2-y1)/len);
  x1 += dx; x2 += dx;
  y1 += dy; y2 += dy;
  
  FontMetrics fm = g.getFontMetrics();
  int lineHeight = fm.getHeight();
  
  float yshift = 0;
  for (String line : reversed(lines(text))) {
    drawOutlineTextAlongLine(g, line, x1, y1, x2, y2, drawThoughtLine_width/2+drawThoughtLineText_shift, yshift, color, Color.black);
    yshift -= lineHeight; 
  }
  
  g.dispose();
}
static Runnable runnableThread(final Runnable r) {
  return new Runnable() { public void run() { try {  new Thread(r).start() ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "new Thread(r).start()"; }};
}
static int updateCanvas_retryInterval = 50;

// if makeImg returns null, it is recalled after a delay
static void updateCanvas(final Canvas canvas, final Object makeImg) {
  swingNowOrLater(new Runnable() { public void run() { try { 
    if (canvas.updating || canvas.getWidth() == 0) return;
    canvas.updating = true;
    try {
      BufferedImage img = asBufferedImage(callF(makeImg, canvas.getWidth(), canvas.getHeight()));
      if (img != null) {
        canvas.setImage(img);
        canvas.updating = false;
      } else
        awtLater(updateCanvas_retryInterval, new Runnable() { public void run() { try { 
          canvas.updating = false;
          updateCanvas(canvas, makeImg);
        
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "canvas.updating = false;\r\n          updateCanvas(canvas, makeImg);"; }});
    } catch (Throwable e) {
      canvas.updating = false;
      throw rethrow(e);
    }
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (canvas.updating || canvas.getWidth() == 0) return;\r\n    canvas.updating =..."; }});
}

static void updateCanvas(final Canvas canvas) {
  if (canvas != null) canvas.update();
}
static <A, B> Map<A, B> mapInConcept(Concept c, Map<A, B> m) {
  return new MapInConcept(m, c);
}
static boolean hasTransparency(BufferedImage img) {
  return img.getColorModel().hasAlpha();
}
static double distancePointToLineSegment(Pt a, Pt b, Pt p) {
  int x1 = a.x, y1 = a.y, x2 = b.x, y2 = b.y, x3 = p.x, y3 = p.y;
  
  float px=x2-x1;
  float py=y2-y1;
  float temp=(px*px)+(py*py);
  float u=((x3 - x1) * px + (y3 - y1) * py) / (temp);
  if (u>1) u=1; else if(u<0) u=0;
  float x = x1 + u * px;
  float y = y1 + u * py;
  float dx = x - x3;
  float dy = y - y3;
  return sqrt(dx*dx + dy*dy);
}
static Map<String, Integer> findBot_cache = synchroHashMap();
static int findBot_timeout = 5000;

static DialogIO findBot(String searchPattern) {
  // first split off sub-bot suffix
  String subBot = null;
  int i = searchPattern.indexOf('/');
  if (i >= 0 && (isJavaIdentifier(searchPattern.substring(0, i)) || isInteger(searchPattern.substring(0, i)))) {
    subBot = searchPattern.substring(i+1);
    searchPattern = searchPattern.substring(0, i);
    if (!isInteger(searchPattern))
      searchPattern = "Multi-Port at " + searchPattern + ".";
  }
  
  // assume it's a port if it's an integer
  if (isInteger(searchPattern))
    return talkToSubBot(subBot, talkTo(parseInt(searchPattern)));
    
  if (eq(searchPattern, "remote"))
    return talkToSubBot(subBot, talkTo("second.tinybrain.de", 4999));
    
  Integer port = findBot_cache.get(searchPattern);
  if (port != null) try {
    DialogIO io = talkTo("localhost", port);
    io.waitForLine(/*findBot_timeout*/); // TODO: implement
    String line = io.readLineNoBlock();
    if (indexOfIgnoreCase(line, searchPattern) == 0) {
      call(io, "pushback", line); // put hello string back in
      return talkToSubBot(subBot, io);
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
  
  List<ProgramScan.Program> bots = quickBotScan();
  
  // find top-level bots
  for (ProgramScan.Program p : bots) {
    if (indexOfIgnoreCase(p.helloString, searchPattern) == 0) { // strict matching - start of hello string only, but case-insensitive
      findBot_cache.put(searchPattern, p.port);
      return talkToSubBot(subBot, talkTo("localhost", p.port));
    }
  }
  
  // find sub-bots
  for (ProgramScan.Program p : bots) {
    String botName = firstPartOfHelloString(p.helloString);
    boolean isVM = startsWithIgnoreCase(p.helloString, "This is a JavaX VM.");
    boolean shouldRecurse = startsWithIgnoreCase(botName, "Multi-Port") || isVM;
        
    if (shouldRecurse) try {
      Map<Number, String> subBots = (Map) unstructure(sendToLocalBotQuietly(p.port, "list bots"));
      for (Number vport : subBots.keySet()) {
        String name = subBots.get(vport);
        if (startsWithIgnoreCase(name, searchPattern))
          return talkToSubBot(vport.longValue(), talkTo("localhost", p.port));
      }
    } catch (Throwable __e) { print(exceptionToStringShort(__e)); }
  }
        
  return null;
}
static String getTextTrim(JTextComponent c) {
  return trim(getText(c));
}

// tested for editable combo box - returns the contents of text field
static String getTextTrim(JComboBox cb) {
  return trim(getText(cb));
}

static String getTextTrim(JComponent c) {
  if (c instanceof JLabel) return trim(((JLabel) c).getText());
  if (c instanceof JComboBox) return getTextTrim((JComboBox) c);
  return getTextTrim((JTextComponent) c);
}
static String dbBotName(String progID) {
  return fsI(progID) + " Concepts";
}
static void saveGZStructureToFile(String file, Object o) {
  saveGZStructureToFile(getProgramFile(file), o);
}
  
static void saveGZStructureToFile(File file, Object o) { try {
  File parentFile = file.getParentFile();
  if (parentFile != null)
    parentFile.mkdirs();
  File tempFile = tempFileFor(file);
  if (tempFile.exists()) try {
    String saveName = tempFile.getPath() + ".saved." + now();
    copyFile(tempFile, new File(saveName));
  } catch (Throwable e) { printStackTrace(e); }
  
  FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath());
  try {
    GZIPOutputStream gos = new GZIPOutputStream(fileOutputStream);
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(gos, "UTF-8");
    PrintWriter printWriter = new PrintWriter(outputStreamWriter);
    structureToPrintWriter(o, printWriter);
    printWriter.close();
    gos.close();
    fileOutputStream.close();
  } catch (Throwable e) {
    fileOutputStream.close();
    tempFile.delete();
    throw rethrow(e);
  }
  
  if (file.exists() && !file.delete())
    throw new IOException("Can't delete " + file.getPath());

  if (!tempFile.renameTo(file))
    throw new IOException("Can't rename " + tempFile + " to " + file);
} catch (Exception __e) { throw rethrow(__e); } }
static void copyCAL(CirclesAndLines cal1, CirclesAndLines cal2) {
  if (cal1 == cal2) return;
  copyList(cal1.circles, cal2.circles);
  copyList(cal1.lines, cal2.lines);
  copyFields(cal1, cal2, "defaultImageID",  "arrowClass", "circleClass", "imgZoom", "imageForUserMadeNodes", "title", "globalID");
  copyListeners(cal1, cal2);
}
static void saveLocally(String variableName) {
  saveLocally(programID(), variableName);
}

static void saveLocally(String progID, String variableName) {
  saveLocally2(mc(), progID, variableName);
}

static void saveLocally2(Object obj, String variableName) {
  saveLocally2(obj, programID(), variableName);
}

static void saveLocally2(Object obj, String progID, String variableName) {
  Lock _lock_590 = saveLock(); lock(_lock_590); try {
  File textFile = new File(programDir(progID), variableName + ".text");
  File structureFile = new File(programDir(progID), variableName + ".structure");
  Object x = get(obj, variableName);
  
  if (x == null) {
    textFile.delete();
    structureFile.delete();
  } else if (x instanceof String) {
    saveTextFile(textFile, (String) x);
    structureFile.delete();
  } else {
    saveTextFile(structureFile, javaTokWordWrap(structure(x)));
    textFile.delete();
  }
} finally { unlock(_lock_590); } }
static String clUnparse(Lisp l) {
  if (l == null) return "";
  return join(" ", (List) concatLists(ll(conceptQuote(l.head)), map("clUnparse_sub", l)));
}

static String clUnparse_sub(Lisp l) {
  return l.empty() ? clUnparse(l) : "(" + clUnparse(l) + ")";
}

static List<String> clUnparse(List<Lisp> l) {
  return map("clUnparse", l);
}

static RemoteDB connectToDBOpt(String dbNameOrID) { try {
  return new RemoteDB(dbNameOrID);
} catch (Throwable __e) { return null; } }
static AutoComboBox autoComboBox() {
  return autoComboBox(new ArrayList());
}

static AutoComboBox autoComboBox(final Collection<String> items) {
  return swing(new F0<AutoComboBox>() { AutoComboBox get() { try { 
    AutoComboBox cb = new AutoComboBox();
    cb.setKeyWord(items);
    return cb;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "new AutoComboBox cb;\r\n    cb.setKeyWord(items);\r\n    ret cb;"; }});
}

static AutoComboBox autoComboBox(String value, Collection<String> items) {
  return setText(autoComboBox(items), value);
}
static int drawThoughtArrow_size = 15;

static void drawThoughtArrow(BufferedImage bg,
  BufferedImage img1, double x1, double y1,
  BufferedImage img2, double x2, double y2, Color color) {
  double cs = thoughtCircleSize(img2)/2-1;
  double dist = pointDistance(x1, y1, x2, y2);
  double arrowLen = drawThoughtArrow_size*drawArrowHead_length-1;
  DoublePt v = blendDoublePts(new DoublePt(x2, y2), new DoublePt(x1, y1), (cs+arrowLen)/dist);
  DoublePt p = blendDoublePts(new DoublePt(x2, y2), new DoublePt(x1, y1), cs/dist);
  Graphics2D g = imageGraphics(bg);
  g.setColor(color);
  g.setStroke(new BasicStroke(drawThoughtLine_width));
  g.draw(new Line2D.Double(x1, y1, v.x, v.y));
  drawArrowHead(g, x1, y1, p.x, p.y, drawThoughtArrow_size);
  g.dispose();
}
static JFrame showCenterFrame(String title, int w, int h) {
  return showCenterFrame(title, w, h, null);
}

static JFrame showCenterFrame(String title, int w, int h, Component content) {
  JFrame frame = makeFrame(title, content);
  frame.setSize(w, h);
  return centerFrame(frame);
}

static JFrame showCenterFrame(String title, Component content) {
  return centerFrame(makeFrame(title, content));
}


static JFrame showCenterFrame(Component content) {
  return centerFrame(makeFrame(content));
}
static <A> List<A> addDyn(List<A> l, A a) {
  if (l == null) l = new ArrayList();
  l.add(a);
  return l;
}
// returns number of changes
static int cset(Concept c, Object... values) { try {
  int changes = 0;
  values = expandParams(c.getClass(), values);
  warnIfOddCount(values);
  for (int i = 0; i+1 < l(values); i += 2) {
    String field = (String) values[i];
    Object value = values[i+1];
    Field f = setOpt_findField(c.getClass(), field);
    //print("cset: " + c.id + " " + field + " " + struct(value) + " " + f);
    if (value instanceof RC) value = c._concepts.getConcept((RC) value);
    value = deref(value);
    
    if (value instanceof String && l((String) value) >= concepts_internStringsLongerThan) value = intern((String) value);
    
    if (f == null) {
      // TODO: keep ref if it exists
      mapPut2(c.fieldValues, field, value instanceof Concept ? c.new Ref((Concept) value) : value);
      c.change();
    } else if (isSubtypeOf(f.getType(), Concept.Ref.class)) {
      ((Concept.Ref) f.get(c)).set((Concept) derefRef(value));
      c.change(); ++changes;
    } else {
      Object old = f.get(c);
      if (neq(value, old)) {
        f.set(c, value);
        if ((f.getModifiers() & java.lang.reflect.Modifier.TRANSIENT) == 0) c.change(); 
        ++changes;
      }
    }
  }
  return changes;
} catch (Exception __e) { throw rethrow(__e); } }
static Pt untranslatePt(Pt a, Pt b) {
  if (a == null) return b;
  return new Pt(b.x-a.x, b.y-a.y);
}
static String fsi(String id) {
  return formatSnippetID(id);
}



static String getTextFromClipboard() { try {
  Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
  if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.stringFlavor))
    return (String) transferable.getTransferData(DataFlavor.stringFlavor);
  return null;
} catch (Exception __e) { throw rethrow(__e); } }
static BufferedImage quickVisualizeOr(String query, String defaultImageID) {
  BufferedImage img = quickVisualize(query);
  return img != null ? img : loadImage2(defaultImageID);
}
static void change() {
  //mainConcepts.allChanged();
  // safe version for now cause function is sometimes included unnecessarily (e.g. by EGDiff)
  callOpt(getOptMC("mainConcepts"), "allChanged");
}
static String quoteIfNotIdentifierOrInteger(String s) {
  if (s == null) return null;
  return isJavaIdentifier(s) || isInteger(s) ? s : quote(s);
}
static String cal_simplifiedStructure(CirclesAndLines cal) {
  return cal_simplifiedStructure(structure(cal));
}

static String cal_simplifiedStructure(String structure) {
  CirclesAndLines cal = (CirclesAndLines) ( unstructure(structure));
  for (Circle c : cal.circles) {
    c.x = roundToOneHundredth(c.x);
    c.y = roundToOneHundredth(c.y);
  }
  return cal_structure_impl(cal);
}
static String web_defaultRelationName = "";

static String web_defaultRelationName() {
  return web_defaultRelationName;
}
static JFrame frameInnerSize(final Component c, final double w, final double h) {
  final JFrame frame = getFrame(c);
  if (frame != null) { swing(new Runnable() { public void run() { try { 
    Container cp = frame.getContentPane();
    Dimension oldSize = cp.getPreferredSize();
    cp.setPreferredSize(new Dimension(iround(w), iround(h)));
    frame.pack();
    cp.setPreferredSize(oldSize);
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "Container cp = frame.getContentPane();\r\n    Dimension oldSize = cp.getPreferr..."; }}); }
  return frame;
}

static void frameInnerSize(JFrame frame, Dimension d) {
  frameInnerSize(frame, d.width, d.height);
}

static JFrame frameInnerSize(Pt p, JFrame frame) {
  frameInnerSize(frame, p.x, p.y);
  return frame;
}
static <A, B> Map<A, B> putAll(Map<A, B> a, Map<? extends A,? extends B> b) {
  if (a != null) a.putAll(b);
  return a;
}
static Pt pt(int x, int y) {
  return new Pt(x, y);
}
static String upper(String s) {
  return s == null ? null : s.toUpperCase();
}
static Lisp clParse(String s) {
  List<String> tok = tok_groupRoundBrackets(s);
  if (l(tok) == 1) return null;
  Lisp l = lisp(unquote(tok.get(1)));
  for (int i = 3; i < l(tok); i += 2) {
    String t = tok.get(i);
    if (t.startsWith("(") && t.endsWith(")"))
      l.add(assertNotNull(clParse(dropFirstAndLast(t))));
    else
      l.add(lisp(aiUsing(unquote(t))));
  }
  return l;
}

static List<Lisp> clParse(List<String> l) {
  return map("clParse", l);
}

static <A> List<A> filterByDynamicType(Collection<A> c, String type) {
  List<A> l = new ArrayList();
  for (A x : c)
    if (eq(dynamicClassName(x), type))
      l.add(x);
  return l;
}
static JTextField jtextfield() {
  return jTextField();
}

static JTextField jtextfield(String text) {
  return jTextField(text);
}

static JTextField jtextfield(Object o) {
  return jTextField(o);
}

static long waitForBotStartUp_timeoutSeconds = 60;

// returns address or fails
static String waitForBotStartUp(String botName) {
  for (int i = 0; i < waitForBotStartUp_timeoutSeconds; i++) {
    sleepSeconds(i == 0 ? 0 : 1);
    String addr = getBotAddress(botName);
    if (addr != null)
      return addr;
  }
  throw fail("Bot not found: " + quote(botName));
}
static void showFullScreenImageSurface(BufferedImage img) {
  showFullScreen(jscroll_centered(disposeFrameOnClick(new ImageSurface(img))));
}
//please include function withMargin.

static int showForm_defaultGap = 4;

static JPanel showFormTitled(final String title, final Object... _parts) {
  return swing(new F0<JPanel>() { JPanel get() { try { 
    final Var<JFrame> frame = new Var();
    JPanel panel = showForm_makePanel(frame, _parts);
    frame.set(handleEscapeKey(minFrameWidth(showPackedFrame(title, withMargin(panel)), 400)));
    return panel;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "final new Var<JFrame> frame;\r\n    JPanel panel = showForm_makePanel(frame, _p..."; }});
}

static JPanel showForm_makePanel(final Var<JFrame> frame, Object... _parts) {
  List<List> l = new ArrayList();
  List parts = asList(_parts);
  Runnable submit = null;
  for (int i = 0; i < l(parts); i++) {
    final Object o = parts.get(i), next = get(parts, i+1);
    if (o instanceof Component || o instanceof String || next instanceof Component) { // smartAdd accepts strings
      l.add(ll(o == null ? new JPanel() : o, next));
      if (next instanceof JButton && submit == null)
        submit = new Runnable() { public void run() { try {  ((JButton) next).doClick() ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "((JButton) next).doClick()"; }};
      i++;
    } else if (isRunnable(o))
      l.add(ll(null, jbutton(showFormSubmitButtonName(), submit = new Runnable() { public void run() { try { 
        Object result = call(o);
        if (neq(Boolean.FALSE, result))
          disposeFrame(frame.get());
      
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "Object result = call(o);\r\n        if (neq(Boolean.FALSE, result))\r\n          ..."; }})));
    else print("showForm: Unknown element type: " + getClassName(o));
  }
  JPanel panel = hvgrid((List) l, showForm_defaultGap);
  if (submit != null)
    for (JTextField textField : childrenOfType(panel, JTextField.class))
      onEnter(textField, submit);
  return panel;
}
static <B, A extends B> A addAndReturn(Collection<B> c, A a) {
  if (c != null) c.add(a);
  return a;
}
static <A, B> Map<A, B> cloneMap(Map<A, B> map) {
  if (map == null) return litmap();
  // assume mutex is equal to collection
  synchronized(map) {
    return map instanceof TreeMap ? new TreeMap(map)
      : map instanceof LinkedHashMap ? new LinkedHashMap(map)
      : new HashMap(map);
  }
}
static <A> List<A> getWhere(Collection<A> c, Object... data) {
  List l = new ArrayList();
  for (A x : c)
    if (checkFields(x, data))
      l.add(x);
  return l;
}
static Color drawThoughtCircle_defaultColor = Color.white;

static void drawThoughtCircle(BufferedImage bg, BufferedImage img, double x, double y) {
  // TODO: crop to certain size
  BufferedImage circle = cutImageToCircle(img_addBorder(cutImageToCircle(img), drawThoughtCircle_defaultColor, 10));
  x -= circle.getWidth()/2;
  y -= circle.getHeight()/2;
  drawImageOnImage(circle, bg, iround(x), iround(y));
}
static String webToString(Web web) {
  List<String> out = new ArrayList();
  Map<WebNode,Integer> index = new HashMap();
  for (int i = 0; i < l(web.nodes); i++) {
    out.add("Node " + (i+1) + ": " + web.nodes.get(i));
    index.put(web.nodes.get(i), i+1);
  }
  
  for (Pair<WebNode, WebNode> p : keys(web.relations))
    out.add(index.get(p.a) + " -> " + index.get(p.b) + " = " + web.relations.get(p));
    
  return fromLines(out);
}
static boolean isNaN(double d) {
  return Double.isNaN(d);
}
static void disableImageSurfaceSelector(ImageSurface is) {
  ImageSurfaceSelector s = firstInstance(is.tools, ImageSurfaceSelector.class);
  if (s == null) return;
  is.removeMouseListener(s);
  is.removeMouseMotionListener(s);
  is.tools.add(s);
}

static void revalidate(final Component c) {
  if (c == null || !c.isShowing()) return;
  { swing(new Runnable() { public void run() { try { 
    // magic combo to actually relayout and repaint
    c.revalidate();
    c.repaint();
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "// magic combo to actually relayout and repaint\r\n    c.revalidate();\r\n    c.r..."; }}); }
}
static JMenu jmenu(final String title, final Object... items) {
  return swing(new F0<JMenu>() { JMenu get() { try { 
    JMenu menu = new JMenu(title);
    fillJMenu(menu, items);
    return menu;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "JMenu menu = new(title);\r\n    fillJMenu(menu, items);\r\n    ret menu;"; }});
}
static volatile boolean readLine_noReadLine;

static String readLine() {
  if (readLine_noReadLine) return null;
  return (String) call(getJavaX(), "readLine");
}
static <A, B> void mapPutOrRemove(Map<A, B> map, A key, B value) {
  if (map != null && key != null)
    if (value != null) map.put(key, value);
    else map.remove(key);
}
static void popup(final Throwable throwable) {
  popupError(throwable);
}

static void popup(final String msg) {
  print(msg);
  SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      JOptionPane.showMessageDialog(null, msg);
    }
  });
}
static BufferedImage renderTiledBackground(String tileImageID, int w, int h) {
  return renderTiledBackground(tileImageID, w, h, 0, 0);
}

static BufferedImage renderTiledBackground(String tileImageID, int w, int h, int shiftX, int shiftY) {
  BufferedImage tileImage = loadImage2(tileImageID);
  BufferedImage img = newBufferedImage(w, h, Color.black);
  Graphics2D g = img.createGraphics();
  int tw = tileImage.getWidth(), th = tileImage.getHeight();
  for (int x = mod(shiftX-1, tw)-tw+1; x < w; x += tw)
    for (int y = mod(shiftY-1, th)-th+1; y < h; y += th)
      g.drawImage(tileImage, x, y, null);  
  g.dispose();
  return img;
} 
static int drawThoughtLine_width = 10;

static void drawThoughtLine(BufferedImage bg,
  BufferedImage img1, int x1, int y1,
  BufferedImage img2, int x2, int y2, Color color) {
  Graphics2D g = imageGraphics(bg);
  g.setColor(color);
  g.setStroke(new BasicStroke(drawThoughtLine_width));
  g.drawLine(x1, y1, x2, y2);
  g.dispose();
}
static BufferedImage loadImage2(String snippetIDOrURL) {
  return loadBufferedImage(snippetIDOrURL);
}

static BufferedImage loadImage2(File file) {
  return loadBufferedImage(file);
}
static ReentrantLock reentrantLock(boolean fair) {
  return new ReentrantLock(fair);
}
static Map<BufferedImage, Object> createGraphics_modulators = synchroIdentityHashMap();

static Graphics2D createGraphics(BufferedImage img) {
  Graphics2D g = img.createGraphics();
  Object mod = createGraphics_modulators.get(img);
  if (mod != null)
    callF(mod, g);
  return g;
}

// mod: voidfunc(Graphics2D)
static void createGraphics_modulate(BufferedImage img, Object mod) {
  mapPut2(createGraphics_modulators, img, mod);
}


// uses bilinear interpolation
static BufferedImage scaleImage(BufferedImage before, double scale) {
  if (scale == 1) return before;
  int w = before.getWidth();
  int h = before.getHeight();
  int neww = max(1, iround(w*scale)), newh = max(1, iround(h*scale));
  BufferedImage after = new BufferedImage(neww, newh, BufferedImage.TYPE_INT_ARGB);
  AffineTransform at = new AffineTransform();
  at.scale(scale, scale);
  AffineTransformOp scaleOp = 
     new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
  return scaleOp.filter(before, after);
}
static <A> A popFirst(List<A> l) {
  if (empty(l)) return null;
  A a = first(l);
  l.remove(0);
  return a;
}



// onDrop: voidfunc(File)
static void jHandleFileDrop(JComponent c, final Object onDrop) {
  new DropTarget(c, new DropTargetAdapter() {
    public void drop(DropTargetDropEvent e) {
      try {
        Transferable tr = e.getTransferable();
        DataFlavor[] flavors = tr.getTransferDataFlavors();
        for (DataFlavor flavor : flavors) {
          if (flavor.isFlavorJavaFileListType()) {
            e.acceptDrop(e.getDropAction());
            File file = first((List<File>) tr.getTransferData(flavor));
            if (file != null && !isFalse(callF(onDrop, file)))
              e.dropComplete(true);
            return;
          }
        }
      } catch (Throwable __e) { printStackTrace2(__e); }
      e.rejectDrop();
    }
  });
}
static boolean hasLock(Lock lock) {
  return ((ReentrantLock) lock).isHeldByCurrentThread();
}
static void messageBox(final String msg) {
  if (headless()) print(msg);
  else { swing(new Runnable() { public void run() { try { 
    JOptionPane.showMessageDialog(null, msg, "JavaX", JOptionPane.INFORMATION_MESSAGE);
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "JOptionPane.showMessageDialog(null, msg, \"JavaX\", JOptionPane.INFORMATION_MES..."; }}); }
}

static void messageBox(Throwable e) {
  showConsole();
  printStackTrace(e);
  messageBox(str(innerException(e)));
}
static long nowUnlessLoading() {
  return /*dynamicObjectIsLoading() ? 0 :*/ now();
}



static BufferedImage getImageFromClipboard() { try {
  Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
  if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.imageFlavor))
    return (BufferedImage) transferable.getTransferData(DataFlavor.imageFlavor);
  return imageFromDataURL(getTextFromClipboard());
} catch (Exception __e) { throw rethrow(__e); } }
static long fileSize(String path) { return getFileSize(path); }
static long fileSize(File f) { return getFileSize(f); }

static boolean hasConcept(Class<? extends Concept> c, Object... params) {
  return findConceptWhere(c, params) != null;
}
static void requestFocus(final JComponent c) { focus(c); }
static <A extends Concept> int countConcepts(Class<A> c, Object... params) {
  return mainConcepts.countConcepts(c, params);
}

static int countConcepts() {
  return mainConcepts.countConcepts();
}

static int countConcepts(String className) {
  return mainConcepts.countConcepts(className);
}

static <A extends Concept> int countConcepts(Concepts concepts, String className) {
  return concepts.countConcepts(className);
}
static <A, B> List<B> getAll(Map<A, B> map, Collection<A> l) {
  return lookupAllOpt(map, l);
}

static <A, B> List<B> getAll(Collection<A> l, Map<A, B> map) {
  return lookupAllOpt(map, l);
}


static int year() {
  return Calendar.getInstance().get(Calendar.YEAR);
}
  static void popupError(final Throwable throwable) {
    throwable.printStackTrace(); // print stack trace to console for the experts
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        String text = throwable.toString();
        //text = cutPrefix(text, "java.lang.RuntimeException: ");
        JOptionPane.showMessageDialog(null, text);
      }
    });
  }
static JTextField jTextField() {
  return jTextField("");
}

static JTextField jTextField(final String text) {
  return swing(new F0<JTextField>() { JTextField get() { try { 
    JTextField tf = new JTextField(unnull(text));
    jenableUndoRedo(tf);
    tf.selectAll();
    return tf;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "JTextField tf = new JTextField(unnull(text));\r\n    jenableUndoRedo(tf);\r\n    ..."; }});
}

static JTextField jTextField(Object o) {
  return jTextField(strOrEmpty(o));
}

static boolean infoMessage_alwaysOnTop = true;
static double infoMessage_defaultTime = 5.0;

// automatically switches to AWT thread for you
static JWindow infoMessage(String text) {
  return infoMessage(text, infoMessage_defaultTime);
}

static JWindow infoMessage(final String text, final double seconds) {
  print(text);
  return infoMessage_noprint(text, seconds);
}

static JWindow infoMessage_noprint(String text) {
  return infoMessage_noprint(text, infoMessage_defaultTime);
}

static JWindow infoMessage_noprint(final String text, final double seconds) {
  if (isHeadless()) return null;
  return (JWindow) swingAndWait(new Object() { Object get() { try { 
    JTextArea ta = wrappedTextArea(text);
    int size = 14;
    if (l(text) <= 50) size *= 2;
    else if (l(text) < 100) size = iround(size*1.5);
    ta.setFont(typeWriterFont(size));
    JScrollPane sp = jscroll(ta);
    final JWindow window = showWindow(withMargin(sp));
    window.setSize(300, 150);
    moveToTopRightCorner(window);
    onClick(ta, new Runnable() { public void run() { try {  window.dispose() ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "window.dispose()"; }});
    if (infoMessage_alwaysOnTop)
      window.setAlwaysOnTop(true);
    window.setVisible(true);
    disposeWindowAfter(iround(seconds*1000), window);
    return window;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "JTextArea ta = wrappedTextArea(text);\r\n    int size = 14;\r\n    if (l(text) <=..."; }});
}

static JWindow infoMessage(Throwable e) {
  showConsole();
  printStackTrace(e);
  return infoMessage(exceptionToStringShort(e));
}
static int vectorLength(int x, int y) {
  return isqrt(sqr((long) x)+sqr((long) y));
}
static Lock saveLock_lock = fairLock();

static Lock saveLock() {
  return saveLock_lock;
}
static boolean isRunnable(Object o) {
  return o instanceof Runnable || hasMethod(o, "get");
}
static <A> List<A> childrenOfType(Component c, Class<A> theClass) {
  List<A> l = new ArrayList();
  scanForComponents(c, theClass, l);
  return l;
}
static List<AbstractButton> buttonsInGroup(ButtonGroup g) {
  if (g == null) return ll();
  return asList(g.getElements());
}
// It's the super precise version!
static double roundToOneHundredth(double d) {
  return new BigDecimal(d).setScale(2, RoundingMode.HALF_UP).doubleValue();
}
static String programIDWithCase() {
  return nempty(caseID())
    ? programID() + "/" + quoteUnlessIdentifierOrInteger(caseID())
    : programID();
}
static <A> void copyList(Collection<? extends A> a, Collection<A> b) {
  if (a == null || b == null) return;
  b.clear();
  b.addAll(a);
}
// c = JComponent or something implementing swing()
static JComponent wrap(Object swingable) {
  if (swingable == null) return null;
  JComponent c = (JComponent) ( swingable instanceof JComponent ? swingable : callOpt(swingable, "swing"));
  if (c instanceof JTable || c instanceof JList
    || c instanceof JTextArea || c instanceof JEditorPane
    || c instanceof JTextPane || c instanceof JTree)
    return jscroll(c);
  return c;
}

static boolean 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 boolean isNonNegativeInteger(String s) {
  return s != null && Pattern.matches("\\d+", s);
}
static int withMargin_defaultWidth = 6;

static JPanel withMargin(Component c) {
  return withMargin(withMargin_defaultWidth, c);
}

static JPanel withMargin(final int w, final Component c) {
  return swing(new F0<JPanel>() { JPanel get() { try { 
    JPanel p = new JPanel(new BorderLayout());
    p.setBorder(BorderFactory.createEmptyBorder(w, w, w, w));
    p.add(c);
    return p;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "JPanel p = new JPanel(new BorderLayout);\r\n    p.setBorder(BorderFactory.creat..."; }});
}
static <A extends JComponent> A disposeFrameOnClick(final A c) {
  onClick(c, new Runnable() { public void run() { try {  disposeFrame(c) ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "disposeFrame(c)"; }});
  return c;
}
static Window getWindow(Object o) {
  if (!(o instanceof Component)) return null;
  Component c = (Component) o;
  while (c != null) {
    if (c instanceof Window) return (Window) c;
    c = c.getParent();
  }
  return null;
}
static void savePNG(BufferedImage img, File file) { try {
  File tempFile = new File(file.getPath() + "_temp");
  CriticalAction ca = beginCriticalAction("Save " + f2s(file));
  try {
    ImageIO.write(img, "png", mkdirsFor(tempFile));
    file.delete();
    tempFile.renameTo(file);
  } finally {
    ca.done();
  }
} catch (Exception __e) { throw rethrow(__e); } }

// gotta love convenience & program-smartness
static void savePNG(File file, BufferedImage img) {
  savePNG(img, file);
}


static void savePNG(File file, RGBImage img) {
  savePNG(file, img.getBufferedImage());
}

static JFrame showPackedFrame(String title, Component contents) {
  return packFrame(showFrame(title, contents));
}

static JFrame showPackedFrame(Component contents) {
  return packFrame(showFrame(contents));
}
static String dynamicClassName(Object o) {
  if (o instanceof DynamicObject && ((DynamicObject) o).className != null)
    return "main$" + ((DynamicObject) o).className;
  return className(o);
}
static <A extends JComponent> A focus(final A a) {
  if (a != null) swingLater(new Runnable() { public void run() { try {  a.requestFocus(); 
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "a.requestFocus();"; }});
  return a;
}
static void swingLater(long delay, final Object r) {
  javax.swing.Timer timer = new javax.swing.Timer(toInt(delay), actionListener(r));
  timer.setRepeats(false);
  timer.start();
}

static void swingLater(Object r) {
  SwingUtilities.invokeLater(toRunnable(r));
}

static <A, B> List<B> lookupAllOpt(Map<A, B> map, Collection<A> l) {
  List<B> out = new ArrayList();
  if (l != null) for (A a : l)
    addIfNotNull(out, map.get(a));
  return out; 
}

static <A, B> List<B> lookupAllOpt(Collection<A> l, Map<A, B> map) {
  return lookupAllOpt(map, l);
}
static void swingNowOrLater(Runnable r) {
  if (isAWTThread())
    r.run();
  else
    swingLater(r);
}
static Object getOptMC(String field) {
  return getOpt(mc(), field);
}
// TODO: all the fringe cases (?)
static <A> JPanel hvgrid(List<List<A>> components) {
  if (empty(components)) return new JPanel();
  int h = l(components), w = l(first(components));
  JPanel panel = new JPanel();
  panel.setLayout(new GridLayout(h, w));
  for (List<A> row : components)
    smartAdd(panel, row);
  return panel;  
}

static <A> JPanel hvgrid(List<List<A>> components, int gap) {
  JPanel panel = hvgrid(components);
  GridLayout g = (GridLayout) ( panel.getLayout());
  g.setHgap(gap);
  g.setVgap(gap);
  return panel;
}
static DoublePt blendDoublePts(DoublePt x, DoublePt y, double yish) {
  double xish = 1-yish;
  return new DoublePt(x.x*xish+y.x*yish, x.y*xish+y.y*yish);
}

static Object derefRef(Object o) {
  if (o instanceof Concept.Ref) o = ((Concept.Ref) o).get();
  return o;
}

static WeakHashMap<JFrame,Boolean> makeFrame_myFrames = new WeakHashMap();

static JFrame makeFrame() {
  return makeFrame((Component) null);
}

static JFrame makeFrame(Object content) {
  return makeFrame(programTitle(), content);
}

static JFrame makeFrame(String title) {
  return makeFrame(title, null);
}

static JFrame makeFrame(String title, Object content) {
  return makeFrame(title, content, true);
}

static JFrame makeFrame(final String title, final Object content, final boolean showIt) {
  return swing(new F0<JFrame>() { JFrame get() { try { 
    if (getFrame(content) != null)
      return getFrame(setFrameTitle((Component) content, title));
    final JFrame frame = new JFrame(title);
    makeFrame_myFrames.put(frame, Boolean.TRUE);
    JComponent wrapped = wrap(content);
    if (wrapped != null)
      frame.getContentPane().add(wrapped);
    frame.setBounds(defaultNewFrameBounds());
    if (showIt)
      frame.setVisible(true);
    //callOpt(content, "requestFocus");
    //exitOnFrameClose(frame);
    
    standardTitlePopupMenu(frame);
    return frame;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "if (getFrame(content) != null)\r\n      ret getFrame(setFrameTitle((Component) ..."; }});
}
static void copyListeners(Object a, Object b) {
  for (String f : fieldNames(a))
    if (l(f) > 2 && startsWith(f, "on") && isUpperCase(f.charAt(2)))
      setOpt(b, f, getOpt(a, f));
}
static boolean isInstance(Class type, Object arg) {
  return type.isInstance(arg);
}
static void disposeFrame(final Component c) {
  disposeWindow(c);
}
// undefined color, seems to be all black in practice
static BufferedImage newBufferedImage(int w, int h) {
  return new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
}

static BufferedImage newBufferedImage(int w, int h, Color color) {
  BufferedImage img = newBufferedImage(w, h);
  Graphics2D g = img.createGraphics();
  g.setColor(color);
  g.fillRect(0, 0, w, h);
  return img;
}


static BufferedImage newBufferedImage(Pt p, Color color) {
  return newBufferedImage(p.x, p.y, color);
}

static String formatDouble(double d, int digits) {
  String format = "0." + rep(digits, '#');
  return new java.text.DecimalFormat(format, new java.text.DecimalFormatSymbols(Locale.ENGLISH)).format(d);
}



static ThreadLocal<Boolean> drawOutlineTextAlongLine_flip = new ThreadLocal();

static void drawOutlineTextAlongLine(Graphics2D g, String text, int x1, int y1, int x2, int y2, int shift, Color fillColor, Color outlineColor) {
  drawOutlineTextAlongLine(g, text, x1, y1, x2, y2, shift, 0, fillColor, outlineColor);
}

static void drawOutlineTextAlongLine(Graphics2D g, String text, int x1, int y1, int x2, int y2, int shift, float yshift, Color fillColor, Color outlineColor) {
  Boolean flip = optPar(drawOutlineTextAlongLine_flip);
  if (y2 == y1 && x2 == x1) ++x2;
  
  AffineTransform tx = new AffineTransform();
  double angle = Math.atan2(y2-y1, x2-x1);
  if (flip == null)
    flip = abs(angle) > pi()/2;
  if (flip) angle -= pi();
  tx.translate((x1+x2)/2.0, (y1+y2)/2.0);
  tx.rotate(angle);

  FontMetrics fm = g.getFontMetrics();
  // int y = shift+fm.getLeading()+fm.getMaxAscent(); // below
  int y = -shift-fm.getMaxDescent(); // above
  //print("drawText y=" + y + ", shift=" + shift);
  
  AffineTransform old = g.getTransform();
  g.transform(tx);

  drawTextWithOutline(g, text, -fm.stringWidth(text)/2.0f, y+yshift, fillColor, outlineColor);
  g.setTransform(old);
}
static JFrame minFrameWidth(JFrame frame, int w) {
  if (frame != null && frame.getWidth() < w)
    frame.setSize(w, frame.getHeight());
  return frame;
}

static JFrame minFrameWidth(int w, JFrame frame) {
  return minFrameWidth(frame, w);
}

static boolean aiUsing_enabled;
static Set<String> aiUsing_set = synchroTreeSet();

static String aiUsing(String s) {
  if (aiUsing_enabled)
    aiUsing_set.addAll(aggressivelyCollectPossibleGlobalIDs(s));
  return s;
}

static void aiUsing(Object o) {
  // TODO
}

static <A> List<A> aiUsing(List<A> l) {
  if (aiUsing_enabled)
    for (Object li : unnull(l)) aiUsing(li);
  return l;
}

static Lisp aiUsing(Lisp l) {
  if (aiUsing_enabled && l != null) {
    aiUsing(l.head);
    for (Lisp sub : l) aiUsing(sub);
  }
  return l;
}
static ThreadLocal<Boolean> imageGraphics_antiAlias = new ThreadLocal();

static Graphics2D imageGraphics(BufferedImage img) {
  return !isFalse(imageGraphics_antiAlias.get()) ? antiAliasGraphics(img) : createGraphics(img);
}
// r may return false to cancel timer
static TimerTask smartTimerTask(final Object r, final java.util.Timer timer, final long delay) {
  return new TimerTask() {
    long lastRun;
    
    public void run() {
      if (!licensed())
        timer.cancel();
      else {
        lastRun = fixTimestamp(lastRun);
        long now = now();
        if (now >= lastRun + delay*0.9) {
          lastRun = now;
          if (eq(false, pcallF(r)))
            timer.cancel();
        }
      }
    }
  };
}




// first delay = delay
static Timer installTimer(JComponent component, Object r, long delay) {
  return installTimer(component, r, delay, delay);
}

// first delay = delay
static Timer installTimer(RootPaneContainer frame, long delay, Object r) {
  return installTimer(frame.getRootPane(), r, delay, delay);
}

// first delay = delay
static Timer installTimer(JComponent component, long delay, Object r) {
  return installTimer(component, r, delay, delay);
}

static Timer installTimer(JComponent component, long delay, long firstDelay, Object r) {
  return installTimer(component, r, delay, firstDelay);
}

static Timer installTimer(final JComponent component, final Object r, final long delay, final long firstDelay) {
  return installTimer(component, r, delay, firstDelay, true);
}

static Timer installTimer(final JComponent component, final Object r, final long delay, final long firstDelay, final boolean repeats) {
  return (Timer) swingAndWait(new Object() { Object get() { try { 
    final Var<Timer> timer = new Var();
    timer.set(new Timer(toInt(delay), new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent _evt) { try {
      try {
        if (!allPaused())
          if (isFalse(callF(r)))
            cancelTimer(timer.get());
      } catch (Throwable __e) { printStackTrace2(__e); }
    } catch (Throwable __e) { messageBox(__e); }}}));
    timer.get().setInitialDelay(toInt(firstDelay));
    timer.get().setRepeats(repeats);
    bindTimerToComponent(timer.get(), component);
    return timer.get();
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "final new Var<Timer> timer;\r\n    timer.set(new Timer(toInt(delay), actionList..."; }});
}

static Timer installTimer(JFrame frame, long delay, long firstDelay, Object r) {
  return installTimer(frame.getRootPane(), r, delay, firstDelay);
}

static BufferedImage img_addBorder(BufferedImage img, Color color, int border) {
  int top = border, bottom = border, left = border, right = border;
  int w = img.getWidth(), h = img.getHeight();
  BufferedImage img2 = createBufferedImage(left+w+right, top+h+bottom, color);
  Graphics2D g = img2.createGraphics();
  g.drawImage(img, left, top, null);
  g.dispose();
  return img2;
}
static String showFormSubmitButtonName() {
  return "Submit";
}


static BufferedImage cutImageToCircle(String imageID) {
  return cutImageToCircle(loadImage2(imageID));
}

static BufferedImage cutImageToCircle(BufferedImage image) {
  int w = min(image.getWidth(), image.getHeight());
  return cutImageToCircle(image, w);
}

static BufferedImage cutImageToCircle(BufferedImage image, int w) {
  int h = w;
  image = img_getCenterPortion(image, w, w);
  BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  Graphics2D g2 = output.createGraphics();

  g2.setComposite(AlphaComposite.Src);
  g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  g2.setColor(Color.WHITE);
  g2.fill(new Ellipse2D.Float(0, 0, w, h));

  g2.setComposite(AlphaComposite.SrcAtop);
  g2.drawImage(image, 0, 0, null);
  g2.dispose();
  return output;
}
static boolean headless() {
  return isHeadless();
}
static boolean setText_opt = true; // optimize by calling getText first

static <A extends JTextComponent> A setText(A c, Object text) {
  setText((Object) c, text);
  return c;
}

static <A extends JComboBox> A setText(final A c, Object text) {
  // only for editable combo boxes at this point
  final String s = strUnnull(text);
  { swing(new Runnable() { public void run() { try { 
    c.getEditor().setItem(s);
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "c.getEditor().setItem(s);"; }}); }
  return c;
}

static void setText(JLabel c, Object text) {
  setText((Object) c, text);
}

static JButton setText(JButton c, Object text) {
  setText((Object) c, text);
  return c;
}

static <A> A setText(final A c, Object text) {
  if (c == null) return null;
  final String s = strUnnull(text);
  swingAndWait(new Runnable() { public void run() { try { 
    if (!setText_opt || neq(callOpt(c, "getText"), s))
      call(c, "setText", s);
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (!setText_opt || neq(callOpt(c, \"getText\"), s))\r\n      call(c, \"setText\", s);"; }});
  return c;
}
static String loadTextFilePossiblyGZipped(String fileName) {
  return loadTextFilePossiblyGZipped(fileName, null);
}
  
static String loadTextFilePossiblyGZipped(String fileName, String defaultContents) {
  File gz = new File(fileName + ".gz");
  return gz.exists() ? loadGZTextFile(gz) : loadTextFile(fileName, defaultContents);
}

static String loadTextFilePossiblyGZipped(File fileName) {
  return loadTextFilePossiblyGZipped(fileName, null);
}

static String loadTextFilePossiblyGZipped(File fileName, String defaultContents) {
  return loadTextFilePossiblyGZipped(fileName.getPath(), defaultContents);
}

static JMenuItem jmenuItem(String text, final Object r) {
  JMenuItem mi = new JMenuItem(text);
  mi.addActionListener(actionListener(r));
  return mi;
}
static int img_minOfWidthAndHeight(BufferedImage image) {
  return image == null ? 0 : min(image.getWidth(), image.getHeight());
}
static BufferedImage loadPNG(File file) {
  return loadBufferedImage(file);
}
static <A, B> void mapPut2(Map<A, B> map, A key, B value) {
  if (map != null && key != null)
    if (value != null) map.put(key, value);
    else map.remove(key);
}
  static String format(String pat, Object... args) {
    return format3(pat, args);
  }

static JTextField onEnter(JTextField tf, final Object action) {
  if (action == null || tf == null) return tf;
  tf.addActionListener(actionListener(action));
  return tf;
}

static JButton onEnter(JButton btn, final Object action) {
  if (action == null || btn == null) return btn;
  btn.addActionListener(actionListener(action));
  return btn;
}

static JList onEnter(JList list, Object action) {
  list.addKeyListener(enterKeyListener(action));
  return list;
}

// editable only
static JComboBox onEnter(final JComboBox cb, final Object action) {
  JTextField text = (JTextField) cb.getEditor().getEditorComponent();
  /*onEnter(text, r {
    cb.hidePopup();
    pcallF(action);
  });*/
  onEnter(text, action);
  return cb;
}
static boolean containsNewLine(String s) {
  return contains(s, '\n'); // screw \r, nobody needs it
}
static Class hotwireOnce(String programID) {
  return hotwireCached(programID, false);
}


static float drawArrowHead_length = 2f;

static void drawArrowHead(Graphics2D g, double x1, double y1, double x2, double y2, double size) {
  if (y2 == y1 && x2 == x1) return;
  
  Path2D.Double arrowHead = new Path2D.Double();
  arrowHead.moveTo(0, 0);
  double l = drawArrowHead_length*size;
  arrowHead.lineTo(-size, -l);
  arrowHead.lineTo(size, -l);
  arrowHead.closePath();

  AffineTransform tx = new AffineTransform();
  double angle = Math.atan2(y2-y1, x2-x1);
  tx.translate(x2, y2);
  tx.rotate(angle-Math.PI/2d);

  AffineTransform old = g.getTransform();
  g.transform(tx);
  g.fill(arrowHead);
  g.setTransform(old);
}
static JScrollPane jscroll_centered(Component c) {
  return new JScrollPane(jFullCenter(c));
}
static Object unstructure_debug(String s) {
  try {
    return unstructure(s);
  } catch (Throwable _e) {
    print("Was unstructuring: " + s);
  
throw rethrow(_e); }
}
static void onResize(Component c, final Object r) {
  c.addComponentListener(new ComponentAdapter() {
    public void componentResized(ComponentEvent e) {
      pcallF(r);
    }
  });
}
static void showConsole() {
  showFrame(consoleFrame());
}
static <A, B> Map<A, B> synchroIdentityHashMap() {
  return synchroMap(new IdentityHashMap());
}
static String toUpper(String s) {
  return s == null ? null : s.toUpperCase();
}
static boolean showAnimationInTopRightCorner_alwaysOnTop = true;

// automatically switches to AWT thread for you
// text is optional text below image
static JWindow showAnimationInTopRightCorner(String imageID, String text) {
  if (isHeadless()) return null;
  return showAnimationInTopRightCorner(imageIcon(imageID), text);
}

static JWindow showAnimationInTopRightCorner(final BufferedImage image, final String text) {
  if (isHeadless()) return null;
  return showAnimationInTopRightCorner(imageIcon(image), text);
}

static JWindow showAnimationInTopRightCorner(final ImageIcon imageIcon, final String text) {
  if (isHeadless()) return null;
  return (JWindow) swingAndWait(new Object() { Object get() { try { 
    JLabel label = new JLabel(imageIcon);
    if (nempty(text)) {
      label.setText(text);
      label.setVerticalTextPosition(SwingConstants.BOTTOM);
      label.setHorizontalTextPosition(SwingConstants.CENTER);
    }
    final JWindow window = showInTopRightCorner(label);
    onClick(label, new Runnable() { public void run() { try {  window.dispose() ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "window.dispose()"; }});
    if (showAnimationInTopRightCorner_alwaysOnTop)
      window.setAlwaysOnTop(true);
    return window;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "JLabel label = new JLabel(imageIcon);\r\n    if (nempty(text)) {\r\n      label.s..."; }});
}

static JWindow showAnimationInTopRightCorner(final String imageID) {
  return showAnimationInTopRightCorner(imageID, "");
}

static JWindow showAnimationInTopRightCorner(String imageID, double seconds) {
  return showAnimationInTopRightCorner(imageID, "", seconds);
}

static JWindow showAnimationInTopRightCorner(String imageID, String text, double seconds) {
  if (isHeadless()) return null;
  return disposeWindowAfter(iround(seconds*1000), showAnimationInTopRightCorner(imageID, text));
}

static JWindow showAnimationInTopRightCorner(BufferedImage img, String text, double seconds) {
  return disposeWindowAfter(iround(seconds*1000), showAnimationInTopRightCorner(img, text));
}

static int month() {
  return Calendar.getInstance().get(Calendar.MONTH)+1;
}
volatile static boolean conceptsAndBot_running;

static void conceptsAndBot() {
  conceptsAndBot(null);
}

static void conceptsAndBot(Integer autoSaveInterval) {
  if (conceptsAndBot_running) return;
  conceptsAndBot_running = true;
  
  try {
    ensureDBNotRunning(dbBotStandardName());
  } catch (Throwable _e) {
    mainConcepts.dontSave = true; // SAFETY
  
throw rethrow(_e); }
  
  if (autoSaveInterval != null)
    loadAndAutoSaveConcepts(autoSaveInterval);
  else
    loadAndAutoSaveConcepts();
  dbBot();
}
static BufferedImage asBufferedImage(Object o) {
  BufferedImage bi = toBufferedImageOpt(o);
  if (bi == null) throw fail(getClassName(o));
  return bi;
}
static String sendToLocalBotQuietly(String bot, String text, Object... args) {
  text = format3(text, args);
  
  DialogIO channel = newFindBot2(bot);
  if (channel == null)
    throw fail(quote(bot) + " not found");
  try {
    channel.readLine();
    channel.sendLine(text);
    String s = channel.readLine();
    return s;
  } catch (Throwable e) {
    e.printStackTrace();
    return null;
  } finally {
    channel.close();
  }
}

static String sendToLocalBotQuietly(int port, String text, Object... args) {
  text = format3(text, args);
  DialogIO channel = talkTo(port);
  try {
    channel.readLine();
    channel.sendLine(text);
    String s = channel.readLine();
    return s;
  } catch (Throwable e) {
    e.printStackTrace();
    return null;
  } finally {
    if (channel != null)
      channel.close();
  }
}
static String getBotAddress(String bot) {
  List<ScannedBot> l = fullBotScan(bot);
  return empty(l) ? null : first(l).address;
}
static String googleImageSearch(String q) {
  return (String) call(hotwireOnce("#1010230"), "googleImageSearch", q);
}

static List<String> googleImageSearch_multi(String q) {
  return (List<String>) call(hotwireOnce("#1010230"), "googleImageSearch_multi", q);
}

static BufferedImage googleImageSearchFirst(String q) {
  return (BufferedImage) call(hotwireOnce("#1010230"), "googleImageSearchFirst", q);
}
static String padLeft(String s, char c, int n) {
  return rep(c, n-l(s)) + s;
}

// default to space
static String padLeft(String s, int n) {
  return padLeft(s, ' ', n);
}
static String dbProgramID() {
  return getDBProgramID();
}
static String matchOK2OrFail(String s) {
  Matches m = new Matches();
  if (swic(s, "ok "))
    return substring(s, 3);
  else if (eqic(s, "ok"))
    return "ok";
  else
    throw fail(s);
}
static <A extends Component> A setFrameSize(A c, int w, int h) {
  JFrame f = getFrame(c);
  if (f != null)
    f.setSize(w, h);
  return c;
}

static <A extends Component> A setFrameSize(int w, int h, A c) {
  return setFrameSize(c, w, h);
}
static double sqrt(double x) {
  return Math.sqrt(x);
}
static void swingAndWait(Runnable r) { try {
  if (isAWTThread())
    r.run();
  else
    EventQueue.invokeAndWait(r);
} catch (Exception __e) { throw rethrow(__e); } }

static Object swingAndWait(final Object f) {
  if (isAWTThread())
    return callF(f);
  else {
    final Var result = new Var();
    swingAndWait(new Runnable() { public void run() { try { 
      result.set(callF(f));
    
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "result.set(callF(f));"; }});
    return result.get();
  }
}
static JButton jbutton(String text, Object action) {
  return newButton(text, action);
}

// button without action
static JButton jbutton(String text) {
  return newButton(text, null);
}

static JButton jbutton(BufferedImage img, Object action) {
  return setButtonImage(img, jbutton("", action));
}

static JButton jbutton(Action action) {
  return new JButton(action);
}
static void fillJMenu(JMenu m, Object... x) {
  if (x == null) return;
  for (int i = 0; i < l(x); i++) {
    Object o = x[i], y = get(x, i+1);
    if (o instanceof List)
      fillJMenu(m, asArray((List) o));
    else if (eqOneOf(o, "***", "---", "===", ""))
      m.addSeparator();
    else if (o instanceof String && isRunnableX(y)) {
      m.add(jmenuItem((String) o, y));
      ++i;
    } else if (o instanceof JMenuItem)
      m.add((JMenuItem) o); // "call" might use wrong method
    else if (o instanceof String || o instanceof Action || o instanceof Component)
      call(m, "add", o);
    else
      print("Unknown menu item: " + o);
  }
}
static String sendToLocalBot(String bot, String text, Object... args) {
  text = format3(text, args);
  
  DialogIO channel = findBot(bot);
  if (channel == null)
    throw fail(quote(bot) + " not found");
  try {
    channel.readLine();
    print(bot + "> " + shorten(text, 80));
    channel.sendLine(text);
    String s = channel.readLine();
    print(bot + "< " + shorten(s, 80));
    return s;
  } catch (Throwable e) {
    e.printStackTrace();
    return null;
  } finally {
    channel.close();
  }
}

static String sendToLocalBot(int port, String text, Object... args) {
  text = format3(text, args);
  DialogIO channel = talkTo(port);
  try {
    channel.readLine();
    print(port + "> " + shorten(text, 80));
    channel.sendLine(text);
    String s = channel.readLine();
    print(port + "< " + shorten(s, 80));
    return s;
  } catch (Throwable e) {
    e.printStackTrace();
    return null;
  } finally {
    if (channel != null)
      channel.close();
  }
}
// changes & returns canvas
static BufferedImage drawImageOnImage(BufferedImage img, BufferedImage canvas, int x, int y) {
  Graphics2D g = createGraphics(canvas);
  g.drawImage(img, x, y, null);
  g.dispose();
  return canvas;
}
static Font sansSerifBold(int fontSize) {
  return new Font(Font.SANS_SERIF, Font.BOLD, fontSize);
}
static <A> A firstInstance(Collection c, Class<A> type) {
  return firstOfType(c, type);
}
static long sqr(long l) {
  return l*l;
}

static double sqr(double d) {
  return d*d;
}
static <A> A swingConstruct(final Class<A> c, final Object... args) {
  return swing(new F0<A>() { A get() { try { return  nuObject(c, args) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "nuObject(c, args)"; }});
}
static JFrame handleEscapeKey(final JFrame frame) {
  KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
  frame.getRootPane().registerKeyboardAction(new ActionListener() {
    public void actionPerformed(ActionEvent actionEvent) {
      frame.dispose();
    }
  }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
  return frame;
}

static boolean isURL(String s) {
  return s.startsWith("http://") || s.startsWith("https://");
}
static String getText(final JTextComponent c) {
  return c == null ? "" : (String) swingAndWait(new Object() { Object get() { try { return  c.getText() ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "c.getText()"; }});
}

static String getText(final JLabel l) {
  return l == null ? "" : (String) swingAndWait(new Object() { Object get() { try { return  l.getText() ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "l.getText()"; }});
}

// returns the contents of text field for editable combo box
static String getText(final JComboBox cb) {
  if (cb.isEditable())
    return unnull((String) cb.getEditor().getItem());
  else
    return str(cb.getSelectedItem());
}
static BufferedImage resizeImage(BufferedImage img, int newW, int newH) {
  return resizeImage(img, newW, newH, Image.SCALE_SMOOTH);
}

static BufferedImage resizeImage(BufferedImage img, int newW, int newH, int scaleType) {
  Image tmp = img.getScaledInstance(newW, newH, scaleType);
  BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
  Graphics2D g2d = dimg.createGraphics();
  g2d.drawImage(tmp, 0, 0, null);
  g2d.dispose();
  return dimg;
}

static BufferedImage resizeImage(BufferedImage img, int newW) {
  int newH = iround(img.getHeight()*(double) newW/img.getWidth());
  return resizeImage(img, newW, newH);
}
static int dayOfMonth() {
  return days();
}
static DialogIO talkToSubBot(final long vport, final DialogIO io) {
  return talkToSubBot(String.valueOf(vport), io);
}

static DialogIO talkToSubBot(final String subBot, final DialogIO io) {
  if (subBot == null) return io;
  return new talkToSubBot_IO(subBot, io);
}

static class talkToSubBot_IO extends DialogIO {
  String subBot;
  DialogIO io;
  
  talkToSubBot_IO(String subBot, DialogIO io) {
  this.io = io;
  this.subBot = subBot;}
  
  // delegate all but sendLine
  boolean isStillConnected() { return io.isStillConnected(); }
  String readLineImpl() { return io.readLineImpl(); }
  boolean isLocalConnection() { return io.isLocalConnection(); }
  Socket getSocket() { return io.getSocket(); }
  void close() { io.close(); }

  void sendLine(String line) {
    io.sendLine(format3("please forward to bot *: *", subBot, line));
  }
}
static Font sansSerif(int fontSize) {
  return new Font(Font.SANS_SERIF, Font.PLAIN, fontSize);
}
static void registerEscape(JFrame frame, final Runnable r) {
  String name = "Escape";
  Action action = abstractAction(name, r);
  JComponent pnl = frame.getRootPane();
  KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
  pnl.getActionMap().put(name, action);
  pnl.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, name);
}
static String firstPartOfHelloString(String s) {
  int i = s.lastIndexOf('/');
  return i < 0 ? s : rtrim(s.substring(0, i));
}
static String snippetImageURL(String snippetID) {
  return snippetImageURL(snippetID, "png");
}

static String snippetImageURL(String snippetID, String contentType) {
  long id = parseSnippetID(snippetID);
  String url;
  if (id == 1000010 || id == 1000012)
    url = "http://tinybrain.de:8080/tb/show-blobimage.php?id=" + id;
  else if (isImageServerSnippet(id))
    url = "http://ai1.lol/images/raw/" + id;
  else
    url = "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + id
      + "&contentType=image/" + contentType;
  return url;
}
static List<String> tok_groupRoundBrackets(String s) {
  List<String> tok = javaTok(s);
  while (true) {
    int i = tok.lastIndexOf("(");
    if (i < 0) return tok;
    int j = indexOf(tok, ")", i);
    if (j < 0) return tok;
    tok.set(i, join(subList(tok, i, j+1)));
    tok.subList(i+1, j+1).clear();
    assertTrue(odd(l(tok)));
  }
}
static List<ProgramScan.Program> quickBotScan() {
  return ProgramScan.quickBotScan();
}

static List<ProgramScan.Program> quickBotScan(int[] preferredPorts) {
  return ProgramScan.quickBotScan(preferredPorts);
}

static List<ProgramScan.Program> quickBotScan(String searchPattern) {
  List<ProgramScan.Program> l = new ArrayList<ProgramScan.Program>();
  for (ProgramScan.Program p : ProgramScan.quickBotScan())
    if (indexOfIgnoreCase(p.helloString, searchPattern) == 0)
      l.add(p);
  return l;
}

static <A extends JComponent> A bindToComponent(A component, final Runnable onShow, final Runnable onUnShow) {
  component.addAncestorListener(new AncestorListener() {
    public void ancestorAdded(AncestorEvent event) {
      pcallF(onShow);
    }

    public void ancestorRemoved(AncestorEvent event) {
      pcallF(onUnShow);
    }

    public void ancestorMoved(AncestorEvent event) {
    }
  });
  return component;
}

static <A extends JComponent> A bindToComponent(A component, Runnable onShow) {
  return bindToComponent(component, onShow, null);
}
static boolean checkFields(Object x, Object... data) {
  for (int i = 0; i < l(data); i += 2)
    if (neq(getOpt(x, (String) data[i]), data[i+1]))
      return false;
  return true;
}
static String conceptQuote(String s) {
  if (isIdentifier(s) || isInteger(s) || isProperlyQuoted(s)) return s;
  return quote(s);
}
// better modulo that gives positive numbers always
static int mod(int n, int m) {
  return (n % m + m) % m;
}
static BufferedImage imageFromDataURL(String url) {
  String pref = "base64,";
  int i = indexOf(url, pref);
  if (i < 0) return null;
  return decodeImage(base64decode(substring(url, i+l(pref))));
}
static DialogIO talkTo(int port) {
  return talkTo("localhost", port);
}

static int talkTo_defaultTimeout = 10000; // This is the CONNECT timeout
static int talkTo_timeoutForReads = 0; // Timeout waiting for answers (0 = no timeout)

static ThreadLocal<Map<String, DialogIO>> talkTo_byThread = new ThreadLocal();

static DialogIO talkTo(String ip, int port) { try {
  String full = ip + ":" + port;
  Map<String, DialogIO> map = talkTo_byThread.get();
  if (map != null && map.containsKey(full)) return map.get(full);
  
  if (isLocalhost(ip) && port == vmPort()) return talkToThisVM();

  return new talkTo_IO(ip, port);
} catch (Exception __e) { throw rethrow(__e); } }

static class talkTo_IO extends DialogIO { 
  String ip;
  int port;
  Socket s;
  Writer w;
  BufferedReader in;
  
  talkTo_IO(String ip, int port) {
  this.port = port;
  this.ip = ip; try {
    s = new Socket();
    try {
      if (talkTo_timeoutForReads != 0)
        s.setSoTimeout(talkTo_timeoutForReads);
      s.connect(new InetSocketAddress(ip, port), talkTo_defaultTimeout);
    } catch (Throwable e) {
      throw fail("Tried talking to " + ip + ":" + port, e);
    }
  
    w = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
    in = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));
  } catch (Exception __e) { throw rethrow(__e); } }
  
  boolean isLocalConnection() {
    return s.getInetAddress().isLoopbackAddress();
  }
  
  boolean isStillConnected() {
    return !(eos || s.isClosed());
  }
  
  void sendLine(String line) { try {
    w.write(line + "\n");
    w.flush();
  } catch (Exception __e) { throw rethrow(__e); } }
  
  String readLineImpl() { try {
    return in.readLine();
  } catch (Exception __e) { throw rethrow(__e); } }
  
  void close() {
    try {
      if (!noClose) s.close();
    } catch (IOException e) {
      // whatever
    }
  }
  
  Socket getSocket() {
    return s;
  }
}
static File getCacheProgramDir() {
  return getCacheProgramDir(getProgramID());
}

static File getCacheProgramDir(String snippetID) {
  return new File(userHome(), "JavaX-Caches/" + formatSnippetIDOpt(snippetID));
}
static double toM_double(long l) {
  return l/(1024*1024.0);
}
static String tag(String tag) {
  return htag(tag);
}

static String tag(String tag, Object contents, Object... params) {
  return htag(tag, str(contents), params);
}

static String tag(String tag, StringBuilder contents, Object... params) {
  return htag(tag, contents, params);
}

static String tag(String tag, StringBuffer contents, Object... params) {
  return htag(tag, contents, params);
}
static File tempFileFor(File f) {
  return new File(f.getPath() + "_temp");
}
static <A> A restructure(A a) {
  return (A) unstructure(structure(a));
}
static Throwable innerException(Throwable e) {
  return getInnerException(e);
}
static int randomID_defaultLength = 12;

static String randomID(int length) {
  return makeRandomID(length);
}

static String randomID() {
  return randomID(randomID_defaultLength);
}


static void drawTextWithOutline(Graphics2D g2, String text, float x, float y, Color fillColor, Color outlineColor) {
  BasicStroke outlineStroke = new BasicStroke(2.0f);

  g2.translate(x, y);

  // remember original settings
  Stroke originalStroke = g2.getStroke();
  RenderingHints originalHints = g2.getRenderingHints();

  // create a glyph vector from your text
  GlyphVector glyphVector = g2.getFont().createGlyphVector(g2.getFontRenderContext(), text);
  // get the shape object
  Shape textShape = glyphVector.getOutline();

  g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

  g2.setColor(outlineColor);
  g2.setStroke(outlineStroke);
  g2.draw(textShape); // draw outline

  g2.setColor(fillColor);
  g2.fill(textShape); // fill the shape

  // reset to original settings after painting
  g2.setStroke(originalStroke);
  g2.setRenderingHints(originalHints);
  
  g2.translate(-x, -y);
}


static boolean isRunnableX(Object o) {
  if (o == null) return false;
  if (o instanceof String) return hasMethod(mc(), (String) o);
  return o instanceof Runnable || hasMethod(o, "get");
}
static JTextArea wrappedTextArea(JTextArea ta) {
  ta.setLineWrap(true);
  ta.setWrapStyleWord(true);
  return ta;
}

static JTextArea wrappedTextArea() {
  return wrappedTextArea(jtextarea());
}

static JTextArea wrappedTextArea(String text) {
  JTextArea ta = wrappedTextArea();
  ta.setText(text);
  return ta;
}
static <A> Set<A> synchroTreeSet() {
  return Collections.synchronizedSet(new TreeSet<A>());
}

static boolean isImageServerSnippet(long id) {
  return id >= 1100000 && id < 1200000;
}
static int vmPort() {
  return myVMPort();
}
static volatile boolean licensed_yes = true;

static boolean licensed() {
  ping();
  return licensed_yes;
}

static void licensed_off() {
  licensed_yes = false;
}
static int days() {
  return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
}
static String rep(int n, char c) {
  return repeat(c, n);
}

static String rep(char c, int n) {
  return repeat(c, n);
}

static <A> List<A> rep(A a, int n) {
  return repeat(a, n);
}

static <A> List<A> rep(int n, A a) {
  return repeat(n, a);
}

static boolean hasMethod(Object o, String method, Object... args) {
  return findMethod(o, method, args) != null;
}
static KeyListener enterKeyListener(final Object action) {
  return new KeyAdapter() {
    public void keyReleased(KeyEvent ke) {
      if (ke.getKeyCode() == KeyEvent.VK_ENTER)
        pcallF(action);
    }
  };
}


static void bindTimerToComponent(final Timer timer, JFrame f) {
  bindTimerToComponent(timer, f.getRootPane());
}

static void bindTimerToComponent(final Timer timer, JComponent c) {
  if (c.isShowing())
    timer.start();
  
  c.addAncestorListener(new AncestorListener() {
    public void ancestorAdded(AncestorEvent event) {
      timer.start();
    }

    public void ancestorRemoved(AncestorEvent event) {
      timer.stop();
    }

    public void ancestorMoved(AncestorEvent event) {
    }
  });
}
static <A extends Component> A setFrameTitle(A c, String title) {
  JFrame f = getFrame(c);
  if (f == null)
    showFrame(title, c);
  else
    f.setTitle(title);
  return c;
}

static <A extends Component> A setFrameTitle(String title, A c) {
  return setFrameTitle(c, title);
}

// magically find a field called "frame" in main class :-)
static JFrame setFrameTitle(String title) {
  Object f = getOpt(mc(), "frame");
  if (f instanceof JFrame)
    return setFrameTitle((JFrame) f, title);
  return null;
}
static DialogIO talkToThisVM() {
  return new talkToThisVM_IO();
}

static class talkToThisVM_IO extends DialogIO { 
  List<String> answers = ll(thisVMGreeting());
  
  boolean isLocalConnection() { return true; }
  boolean isStillConnected() { return true; }
  int getPort() { return vmPort(); }
  
  void sendLine(String line) {
    answers.add(or2(sendToThisVM_newThread(line), "?"));
  }
  
  String readLineImpl() { try {
    return popFirst(answers);
  } catch (Exception __e) { throw rethrow(__e); } }
  
  void close() {}
  Socket getSocket() { return null; }
}
static JFrame showFrame() {
  return makeFrame();
}

static JFrame showFrame(Object content) {
  return makeFrame(content);
}

static JFrame showFrame(String title) {
  return makeFrame(title);
}

static JFrame showFrame(String title, Object content) {
  return makeFrame(title, content);
}

static JFrame showFrame(final JFrame frame) {
  if (frame != null) { swing(new Runnable() { public void run() { try { 
    if (frameTooSmall(frame)) frameStandardSize(frame);
    frame.setVisible(true);
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (frameTooSmall(frame)) frameStandardSize(frame);\r\n    frame.setVisible(true);"; }}); }
  return frame;
}

// make or update frame
static JFrame showFrame(String title, Object content, JFrame frame) {
  if (frame == null)
    return showFrame(title, content);
  else {
    frame.setTitle(title);
    setFrameContents(frame, content);
    return frame;
  }
}
static JPanel jFullCenter(final Component c) {
  return swing(new F0<JPanel>() { JPanel get() { try { 
    JPanel panel = new JPanel(new GridBagLayout());
    panel.add(c);
    return panel;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "JPanel panel = new JPanel(new GridBagLayout);\r\n    panel.add(c);\r\n    ret panel;"; }});
}
static <A> void scanForComponents(Component c, Class<A> theClass, List<A> l) {
  if (theClass.isInstance(c))
    l.add((A) c);
  if (c instanceof Container)
    for (Component comp : ((Container) c).getComponents())
      scanForComponents(comp, theClass, l);
}
static String dbBotStandardName() {
  return dbBotName(getDBProgramID()) + ".";
}
public static File mkdirsFor(File file) {
  return mkdirsForFile(file);
}

static BufferedImage decodeImage(byte[] data) { try {
  return ImageIO.read(new ByteArrayInputStream(data));
} catch (Exception __e) { throw rethrow(__e); } }
static int moveToTopRightCorner_inset = 20;

static <A extends Component> A moveToTopRightCorner(A a) {
  Window w = getWindow(a);
  if (w != null)
    w.setLocation(getScreenSize().width-w.getWidth()-moveToTopRightCorner_inset, moveToTopRightCorner_inset);
  return a;
}

static boolean allPaused() {
  return ping_pauseAll;
}
static String strOrEmpty(Object o) {
  return o == null ? "" : str(o);
}
static Rectangle defaultNewFrameBounds_r = new Rectangle(300, 100, 500, 400);

static Rectangle defaultNewFrameBounds() {
  return swing(new F0<Rectangle>() { Rectangle get() { try { 
    defaultNewFrameBounds_r.translate(60, 20);
    if (!screenRectangle().contains(defaultNewFrameBounds_r))
      defaultNewFrameBounds_r.setLocation(30+random(30), 20+random(20));
    return new Rectangle(defaultNewFrameBounds_r);
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "defaultNewFrameBounds_r.translate(60, 20);\r\n    if (!screenRectangle().contai..."; }});
}
static Set<String> fieldNames(Object o) {
  return allFields(o);
}
static <A> A optPar(ThreadLocal<A> tl, A defaultValue) {
  A a = tl.get();
  if (a != null) {
    tl.set(null);
    return a;
  }
  return defaultValue;
}

static <A> A optPar(ThreadLocal<A> tl) {
  return optPar(tl, null);
}
static long fixTimestamp(long timestamp) {
  return timestamp > now() ? 0 : timestamp;
}
static String className(Object o) {
  return getClassName(o);
}
static boolean isUpperCase(char c) {
  return Character.isUpperCase(c);
}
// action can be Runnable or a function name
static JButton newButton(final String text, final Object action) {
  return swing(new F0<JButton>() { JButton get() { try { 
    JButton btn = new JButton(text);
    if (action != null)
      btn.addActionListener(actionListener(action));
    return btn;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "JButton btn = new JButton(text);\r\n    if (action != null)\r\n      btn.addActio..."; }});
}
static BufferedImage toBufferedImageOpt(Object o) {
  if (o instanceof BufferedImage) return (BufferedImage) o;
  String c = getClassName(o);
  if (eqOneOf(c, "main$BWImage", "main$RGBImage"))
    return (BufferedImage) call(o, "getBufferedImage");
  if (eq(c, "main$PNGFile"))
    return (BufferedImage) call(o, "getImage");
  return null;
}
static String strUnnull(Object o) {
  return o == null ? "" : str(o);
}
static Map<String, Integer> newFindBot2_cache = synchroHashMap();
static boolean newFindBot2_verbose;

static DialogIO newFindBot2(String name) {
  Integer port = newFindBot2_cache.get(name);
  if (port != null) {
    if (newFindBot2_verbose)
      print("newFindBot2: testing " + name + " => " + port);
    DialogIO io = talkTo(port);
    String q = format("has bot *", name);
    String s = io.ask(q);
    if (match("yes", s)) {
      io = talkToSubBot(name, io);
      call(io, "pushback", "?"); // put some hello string in (yes, this should be improved.)
      return io;
    }
    // bot not there anymore - remove cache entry
    newFindBot2_cache.remove(name);
    if (newFindBot2_verbose)
      print("newFindBot2: dropping " + name + " => " + port);
  }
  
  DialogIO io = findBot(name);
  if (io != null) {
    newFindBot2_cache.put(name, io.getPort());
    if (newFindBot2_verbose)
      print("newFindBot2: remembering " + name + " => " + port);
  }
  return io;
}
static Font typeWriterFont() {
  return typeWriterFont(14);
}
  
static Font typeWriterFont(int size) {
  return new Font("Courier", Font.PLAIN, size);
}
static <A extends Window> A disposeWindowAfter(int delay, final A w) {
  if (w != null)
    swingLater(delay, new Runnable() { public void run() { try { 
      w.dispose();
    
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "w.dispose();"; }});
  return w;
}
static TreeMap<String,Class> hotwireCached_cache = new TreeMap();
static Lock hotwireCached_lock = lock();

static Class hotwireCached(String programID) {
  return hotwireCached(programID, true);
}

static Class hotwireCached(String programID, boolean runMain) {
  Lock _lock_939 = hotwireCached_lock; lock(_lock_939); try {
  
  programID = formatSnippetID(programID);
  Class c = hotwireCached_cache.get(programID);
  if (c == null) {
    c = hotwire(programID);
    if (runMain)
      callMain(c);
    hotwireCached_cache.put(programID, c);
  }
  return c;
} finally { unlock(_lock_939); } }
static JWindow showInTopRightCorner(Component c) {
  JWindow w = new JWindow();
  w.add(c);
  w.pack();
  moveToTopRightCorner(w);
  w.setVisible(true);
  return w;
}
static ActionListener actionListener(final Object runnable) {
  if (runnable instanceof ActionListener) return (ActionListener) runnable;
  return new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent _evt) { try { callF(runnable); } catch (Throwable __e) { messageBox(__e); }}};
}
static Graphics2D antiAliasGraphics(BufferedImage img) {
  Graphics2D g = createGraphics(img);
  g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  return g;
}
static class ScannedBot {
  String helloString;
  String address;
  
  ScannedBot(String helloString, String address) {
  this.address = address;
  this.helloString = helloString;}
  ScannedBot() {}
}

static List<ScannedBot> fullBotScan() {
  return fullBotScan("");
}

static List<ScannedBot> fullBotScan(String searchPattern) {
  List<ScannedBot> bots = new ArrayList();
  for (ProgramScan.Program p : quickBotScan()) {
    String botName = firstPartOfHelloString(p.helloString);
    boolean isVM = startsWithIgnoreCase(p.helloString, "This is a JavaX VM.");
    boolean shouldRecurse = swic(botName, "Multi-Port") || isVM;
    
    if (swic(botName, searchPattern)) bots.add(new ScannedBot(botName, "" + p.port));

    if (shouldRecurse) try {
      Map<Number, String> subBots = (Map) unstructure(sendToLocalBotQuietly(p.port, "list bots"));
      for (Number vport : subBots.keySet()) {
        botName = subBots.get(vport);
        if (swic(botName, searchPattern)) 
          bots.add(new ScannedBot(botName, p.port + "/" + vport));
      }
    } catch (Exception e) { e.printStackTrace(); }
  }
  return bots;
}
static int isqrt(double x) {
  return iround(Math.sqrt(x));
}
static JFrame consoleFrame() {
  return (JFrame) getOpt(get(getJavaX(), "console"), "frame");
}
static void cancelTimer(javax.swing.Timer timer) {
  if (timer != null) timer.stop();
}
static String programTitle() {
  return getProgramName();
}
static void onClick(JComponent c, final Object runnable) {
  c.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
      callF(runnable, e);
    }
  });
}

// re-interpreted for buttons
static void onClick(JButton btn, final Object runnable) {
  onEnter(btn, runnable);
}


static <A extends JTextComponent> A jenableUndoRedo(A textcomp) {
  final UndoManager undo = new UndoManager();
  textcomp.getDocument().addUndoableEditListener(new UndoableEditListener() {
    public void undoableEditHappened(UndoableEditEvent evt) {
      undo.addEdit(evt.getEdit());
    }
  });
  
  textcomp.getActionMap().put("Undo", abstractAction("Undo", new Runnable() { public void run() { try { 
    if (undo.canUndo()) undo.undo()
  ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (undo.canUndo()) undo.undo()"; }}));
  textcomp.getActionMap().put("Redo", abstractAction("Redo", new Runnable() { public void run() { try { 
    if (undo.canRedo()) undo.redo()
  ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (undo.canRedo()) undo.redo()"; }}));
  textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");
  textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo");
  return textcomp;
}
static String quoteUnlessIdentifierOrInteger(String s) {
  return quoteIfNotIdentifierOrInteger(s);
}
static JButton setButtonImage(BufferedImage img, JButton btn) {
  btn.setIcon(imageIcon(img));
  return btn;
}
static JWindow showWindow(Component c) {
  JWindow w = new JWindow();
  w.add(wrap(c));
  return w;
}
static volatile Android3 dbBot_instance;

static Android3 dbBot() {
  return dbBot(dbBotStandardName());
}

static Android3 dbBot(String name) {
  ensureDBNotRunning(name);
  assertNotNull(mainConcepts);
  return dbBot_instance = methodsBot2(name, mainConcepts, exposedDBMethods, mainConcepts.lock);
}
public static String rtrim(String s) {
  int i = s.length();
  while (i > 0 && " \t\r\n".indexOf(s.charAt(i-1)) >= 0)
    --i;
  return i < s.length() ? s.substring(0, i) : s;
}
static double pi() {
  return Math.PI;
}
static Object[] asArray(List l) {
  return toObjectArray(l);
}

static <A> A[] asArray(Class<A> type, List l) {
  return (A[]) l.toArray((Object[]) Array.newInstance(type, l.size()));
}
static JPanel smartAdd(JPanel panel, List parts) {
  for (Object o : parts)
    addToContainer(panel, wrapForSmartAdd(o));
  return panel;
}

static JPanel smartAdd(JPanel panel, Object... parts) {
  return smartAdd(panel, asList(parts));
}

static BufferedImage createBufferedImage(int w, int h, Color color) {
  return newBufferedImage(w, h, color);
}
static JScrollPane jscroll(final Component c) {
  return swing(new F0<JScrollPane>() { JScrollPane get() { try { return  new JScrollPane(c) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "new JScrollPane(c)"; }});
}
static Set<String> aggressivelyCollectPossibleGlobalIDs(String s) {
  LinkedHashSet<String> ids = new LinkedHashSet();
  if (s == null) return ids;
  for (int i = 0; i < l(s); i++) {
    int j = i;
    while (j < l(s) && Character.isLowerCase(s.charAt(j))) ++j;
    if (j-i == 16)
      ids.add(substring(s, i, j));
    i = j;
  }
  return ids;
}
  static byte[] base64decode(String s) {
    byte[] alphaToInt = base64decode_base64toint;
    int sLen = s.length();
    int numGroups = sLen/4;
    if (4*numGroups != sLen)
      throw new IllegalArgumentException(
        "String length must be a multiple of four.");
    int missingBytesInLastGroup = 0;
    int numFullGroups = numGroups;
    if (sLen != 0) {
      if (s.charAt(sLen-1) == '=') {
        missingBytesInLastGroup++;
        numFullGroups--;
      }
      if (s.charAt(sLen-2) == '=')
        missingBytesInLastGroup++;
    }
    byte[] result = new byte[3*numGroups - missingBytesInLastGroup];

    // Translate all full groups from base64 to byte array elements
    int inCursor = 0, outCursor = 0;
    for (int i=0; i<numFullGroups; i++) {
      int ch0 = base64decode_base64toint(s.charAt(inCursor++), alphaToInt);
      int ch1 = base64decode_base64toint(s.charAt(inCursor++), alphaToInt);
      int ch2 = base64decode_base64toint(s.charAt(inCursor++), alphaToInt);
      int ch3 = base64decode_base64toint(s.charAt(inCursor++), alphaToInt);
      result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
      result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
      result[outCursor++] = (byte) ((ch2 << 6) | ch3);
    }

    // Translate partial group, if present
    if (missingBytesInLastGroup != 0) {
      int ch0 = base64decode_base64toint(s.charAt(inCursor++), alphaToInt);
      int ch1 = base64decode_base64toint(s.charAt(inCursor++), alphaToInt);
      result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));

      if (missingBytesInLastGroup == 1) {
        int ch2 = base64decode_base64toint(s.charAt(inCursor++), alphaToInt);
        result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
      }
    }
    // assert inCursor == s.length()-missingBytesInLastGroup;
    // assert outCursor == result.length;
    return result;
  }

  static int base64decode_base64toint(char c, byte[] alphaToInt) {
    int result = alphaToInt[c];
    if (result < 0)
      throw new IllegalArgumentException("Illegal character " + c);
    return result;
  }

  static final byte base64decode_base64toint[] = {
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54,
    55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4,
    5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
    24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34,
    35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
  };


static void standardTitlePopupMenu(final JFrame frame) {
  // standard right-click behavior on titles
  if (isSubstanceLAF())
    titlePopupMenu(frame,
      new Object() { void get(JPopupMenu menu) { try { 
        boolean alwaysOnTop = frame.isAlwaysOnTop();
        menu.add(jmenuItem("Restart Program", "restart"));
        menu.add(jmenuItem("Duplicate Program", "duplicateThisProgram"));
        menu.add(jmenuItem("Show Console", "showConsole"));
        menu.add(jCheckBoxMenuItem("Always On Top", alwaysOnTop, new Runnable() { public void run() { try { 
          toggleAlwaysOnTop(frame) ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "toggleAlwaysOnTop(frame)"; }}));
        menu.add(jMenuItem("Shoot Window", new Runnable() { public void run() { try {  shootWindowGUI_external(frame, 500) ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "shootWindowGUI_external(frame, 500)"; }}));
       } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "bool alwaysOnTop = frame.isAlwaysOnTop();\r\n        menu.add(jmenuItem(\"Restar..."; }});
}

static int packFrame_minw = 150, packFrame_minh = 50;

static <A extends Component> A packFrame(final A c) {
  { swing(new Runnable() { public void run() { try { 
    JFrame frame = getFrame(c);
    if (frame != null) {
      frame.pack();
      int maxW = getScreenWidth()-50, maxH = getScreenHeight()-50;
      frame.setSize(
        min(maxW, max(frame.getWidth(), packFrame_minw)),
        min(maxH, max(frame.getHeight(), packFrame_minh)));
    }
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "JFrame frame = getFrame(c);\r\n    if (frame != null) {\r\n      frame.pack();\r\n ..."; }}); }
  return c;
}

static JFrame packFrame(ButtonGroup g) {
  return packFrame(getFrame(g));
}
static BufferedImage img_getCenterPortion(BufferedImage img, int w, int h) {
  int iw = img.getWidth(), ih = img.getHeight();
  if (iw < w || ih < h) throw fail("Too small");
  int x = (iw-w)/2, y = (ih-h)/2;
  return clipBufferedImage(img, x, y, w, h);
}
static void ensureDBNotRunning(String name) {
  if (hasBot(name)) {
    try {
      String id = fsI(dropAfterSpace(name));
      String framesBot = id + " Frames";
      print("Trying to activate frames of running DB: " + id);
      if (isOK(sendOpt(framesBot, "activate frames")) && isMainProgram())
        cleanKill();
    } catch (Throwable __e) { printStackTrace2(__e); }
    throw fail("Already running: " + name);
  }
}
static AbstractAction abstractAction(String name, final Object runnable) {
  return new AbstractAction(name) {
    public void actionPerformed(ActionEvent evt) {
      pcallF(runnable);
    }
  };
}
static ImageIcon imageIcon(String imageID) { try {
  return new ImageIcon(loadBinarySnippet(imageID).toURI().toURL());
} catch (Exception __e) { throw rethrow(__e); } }

static ImageIcon imageIcon(BufferedImage img) {
  return new ImageIcon(img);
}


  static ImageIcon imageIcon(RGBImage img) {
    return imageIcon(img.getBufferedImage());
  }

static boolean isLocalhost(String ip) {
  return isLoopbackIP(ip) || eqic(ip, "localhost");
}


// menuMaker = voidfunc(JPopupMenu)
static void titlePopupMenu(final Component c, final Object menuMaker) {
  swingNowOrLater(new Runnable() { public void run() { try { 
    if (!isSubstanceLAF())
      print("Can't add title right click!");
    else {
      JComponent titleBar = getTitlePaneComponent(getFrame(c));
      componentPopupMenu(titleBar, menuMaker);
    }
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (!isSubstanceLAF())\r\n      print(\"Can't add title right click!\");\r\n    els..."; }});
}
static String thisVMGreeting() {
  List record_list = (List) ( get(getJavaX(), "record_list"));
  Object android = first(record_list); // Should be of class Android3
  return getString(android, "greeting");
}
  static Method findMethod(Object o, String method, Object... args) {
    try {
      if (o == null) return null;
      if (o instanceof Class) {
        Method m = findMethod_static((Class) o, method, args, false);
        if (m == null) return null;
        m.setAccessible(true);
        return m;
      } else {
        Method m = findMethod_instance(o, method, args, false);
        if (m == null) return null;
        m.setAccessible(true);
        return m;
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  static Method findMethod_static(Class c, String method, Object[] args, boolean debug) {
    Class _c = c;
    while (c != null) {
      for (Method m : c.getDeclaredMethods()) {
        if (debug)
          System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
        if (!m.getName().equals(method)) {
          if (debug) System.out.println("Method name mismatch: " + method);
          continue;
        }

        if ((m.getModifiers() & Modifier.STATIC) == 0 || !findMethod_checkArgs(m, args, debug))
          continue;

        return m;
      }
      c = c.getSuperclass();
    }
    return null;
  }

  static Method findMethod_instance(Object o, String method, Object[] args, boolean debug) {
    Class c = o.getClass();
    while (c != null) {
      for (Method m : c.getDeclaredMethods()) {
        if (debug)
          System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
        if (m.getName().equals(method) && findMethod_checkArgs(m, args, debug))
          return m;
      }
      c = c.getSuperclass();
    }
    return null;
  }

  static boolean findMethod_checkArgs(Method m, Object[] args, boolean debug) {
    Class<?>[] types = m.getParameterTypes();
    if (types.length != args.length) {
      if (debug)
        System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
      return false;
    }
    for (int i = 0; i < types.length; i++)
      if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
        if (debug)
          System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
        return false;
      }
    return true;
  }


static int getScreenWidth() {
  return getScreenSize().width;
}
static void setFrameContents(final Component c, final Object contents) {
  { swing(new Runnable() { public void run() { try { 
    JFrame frame = getFrame(c);
    frame.getContentPane().removeAll();
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(wrap(contents));
    revalidate(frame);
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "JFrame frame = getFrame(c);\r\n    frame.getContentPane().removeAll();\r\n    fra..."; }}); }
}
static int myVMPort() {
  List records = (List) ( get(getJavaX(), "record_list"));
  Object android = records.get(records.size()-1);
  return (Integer) get(android, "port");
}
static boolean frameTooSmall(JFrame frame) {
  return frame.getWidth() < 100 || frame.getHeight() < 50;
}
static JMenuItem jMenuItem(String text, Object r) {
  return jmenuItem(text, r);
}
static Rectangle screenRectangle() {
  return new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
}

static boolean hasBot(String searchPattern) {
  DialogIO io = findBot(searchPattern);
  if (io != null) {
    io.close();
    return true;
  } else
    return false;
}
static void toggleAlwaysOnTop(JFrame frame) {
  frame.setAlwaysOnTop(!frame.isAlwaysOnTop());
}
static Object[] toObjectArray(Collection c) {
  List l = asList(c);
  return l.toArray(new Object[l.size()]);
}

static Dimension getScreenSize() {
  return Toolkit.getDefaultToolkit().getScreenSize();
}
static Android3 methodsBot2(String name, final Object receiver, final List<String> exposedMethods) {
  return methodsBot2(name, receiver, exposedMethods, null);
}

static Android3 methodsBot2(String name, final Object receiver, final List<String> exposedMethods, final Lock lock) {
  Android3 android = new Android3();
  android.greeting = name;
  android.console = false;
  android.responder = new Responder() {
    String answer(String s, List<String> history) {
      return exposeMethods2(receiver, s, exposedMethods, lock);
    }
  };
  return makeBot(android);
}
static String sendOpt(String bot, String text, Object... args) {
  return sendToLocalBotOpt(bot, text, args);
}
static void duplicateThisProgram() {
  nohupJavax(trim(programID() + " " + smartJoin((String[]) get(getJavaX(), "fullArgs"))));
}
static boolean isOK(String s) {
  s = trim(s);
  return swic(s, "ok ") || eqic(s, "ok") || matchStart("ok", s);
}
static void frameStandardSize(JFrame frame) {
  frame.setBounds(300, 100, 500, 400);
}
static boolean isSubstanceLAF() {
  return substanceLookAndFeelEnabled();
}
static JTextArea jtextarea() {
  return jTextArea();
}

static JTextArea jtextarea(String text) {
  return jTextArea(text);
}
static void cleanKill() {
  cleanKillVM();
}
static JCheckBoxMenuItem jCheckBoxMenuItem(String text, boolean checked, final Object r) {
  JCheckBoxMenuItem mi = new JCheckBoxMenuItem(text, checked);
  mi.addActionListener(actionListener(r));
  return mi;
}
static String repeat(char c, int n) {
  n = Math.max(n, 0);
  char[] chars = new char[n];
  for (int i = 0; i < n; i++)
    chars[i] = c;
  return new String(chars);
}

static <A> List<A> repeat(A a, int n) {
  List<A> l = new ArrayList(n);
  for (int i = 0; i < n; i++)
    l.add(a);
  return l;
}

static <A> List<A> repeat(int n, A a) {
  return repeat(a, n);
}
static void shootWindowGUI_external(JFrame frame) {
  call(hotwireOnce("#1007178"), "shootWindowGUI", frame);
}

static void shootWindowGUI_external(final JFrame frame, int delay) {
  call(hotwireOnce("#1007178"), "shootWindowGUI", frame, delay);
}

static String dropAfterSpace(String s) {
  return s == null ? null : substring(s, 0, smartIndexOf(s, ' '));
}
static File loadBinarySnippet(String snippetID) { try {
  long id = parseSnippetID(snippetID);
  File f = DiskSnippetCache_getLibrary(id);
  if (fileSize(f) == 0)
    f = loadDataSnippetToFile(snippetID);
  return f;
} catch (Exception __e) { throw rethrow(__e); } }
static int getScreenHeight() {
  return getScreenSize().height;
}
static boolean isLoopbackIP(String ip) {
  return eq(ip, "127.0.0.1");
}
static String or2(String a, String b) {
  return nempty(a) ? a : b;
}
static String sendToThisVM_newThread(String s, Object... args) {
  final String _s = format(s, args);
  try {
    return (String) evalInNewThread(new Object() { Object get() { try { return 
      callStaticAnswerMethod(getJavaX(), _s)
    ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "callStaticAnswerMethod(getJavaX(), _s)"; }});
  } catch (Throwable e) {
    e = getInnerException(e);
    printStackTrace(e);
    return str(e);
  }
}
static Component wrapForSmartAdd(Object o) {
  if (o == null) return new JPanel();
  if (o instanceof String) return jlabel((String) o);
  return wrap(o);
}
static void addToContainer(Container a, Component b) {
  if (a != null && b != null) a.add(b);
}
static boolean isMainProgram() {
  return creator() == null;
}
static String getProgramName_cache;

static synchronized String getProgramName() {
  if (getProgramName_cache == null)
    getProgramName_cache = getSnippetTitleOpt(programID());
  return getProgramName_cache;
}


static int makeBot(String greeting) {
  return makeAndroid3(greeting).port;
}

static Android3 makeBot(Android3 a) {
  makeAndroid3(a);
  return a;
}

static Android3 makeBot(String greeting, Object responder) {
  Android3 a = new Android3(greeting);
  a.responder = makeResponder(responder);
  makeBot(a);
  return a;
}

static Android3 makeBot() {
  return makeAndroid3(getProgramTitle() + ".");
}
static JTextArea jTextArea() {
  return jTextArea("");
}

static JTextArea jTextArea(final String text) {
  return swing(new F0<JTextArea>() { JTextArea get() { try { return  new JTextArea(text) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "new JTextArea(text)"; }});
}
static boolean exposeMethods2_debug;

static String exposeMethods2(Object receiver, String s, List<String> methodNames) {
  return exposeMethods2(receiver, s, methodNames, null);
}

static String exposeMethods2(Object receiver, String s, List<String> methodNames,
  Lock lock) {
  Matches m = new Matches();
  if (exposeMethods2_debug) print("Received: " + s);
  if (match("call *", s, m)) {
    List l;
    if (isIdentifier(m.unq(0)))
      l = ll(m.unq(0));
    else
      l = (List) unstructure(m.unq(0)); // we used to have safeUnstructure here
    String method = getString(l, 0);
    if (!contains(methodNames, method))
      throw fail("Method not allowed: " + method);
    if (lock != null) lock.lock();
    try {
      if (exposeMethods2_debug) print("Calling: " + method);
      Object o = call(receiver, method, asObjectArray(subList(l, 1)));
      if (exposeMethods2_debug) print("Got: " + getClassName(o));
      return ok2(structure(o));
    } finally {
      if (lock != null) lock.unlock();
    }
  }
  if (match("list methods", s))
    return ok2(structure(methodNames));
  return null;
}
static File loadDataSnippetToFile(String snippetID) { try {
  snippetID = fsI(snippetID);
  File f = DiskSnippetCache_file(parseSnippetID(snippetID));
  try {
    URL url = new URL(dataSnippetLink(snippetID));
    print("Loading library: " + hideCredentials(url));
    try {
      loadBinaryPageToFile(openConnection(url), f);
      if (fileSize(f) == 0) throw fail();
    } catch (Throwable _e) {
      url = new URL("http://data.tinybrain.de/blobs/"
        + psI(snippetID));
      print("Trying other server: " + hideCredentials(url));
      loadBinaryPageToFile(openConnection(url), f);
      print("Got bytes: " + fileSize(f));
    }
    // TODO: check if we hit the "LOADING" message
    if (fileSize(f) == 0) throw fail();
    System.err.println("Bytes loaded: " + fileSize(f));
  } catch (Throwable e) {
    printStackTrace(e);
    throw fail("Binary snippet " + snippetID + " not found or not public");
  }
  return f;
} catch (Exception __e) { throw rethrow(__e); } }
  static boolean matchStart(String pat, String s) {
    return matchStart(pat, s, null);
  }
  
  // matches are as you expect, plus an extra item for the rest string
  static boolean matchStart(String pat, String s, Matches matches) {
    if (s == null) return false;
    List<String> tokpat = parse3(pat), toks = parse3(s);
    if (toks.size() < tokpat.size()) return false;
    String[] m = match2(tokpat, toks.subList(0, tokpat.size()));
    //print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m));
    if (m == null)
      return false;
    else {
      if (matches != null) {
        matches.m = new String[m.length+1];
        arraycopy(m, matches.m);
        matches.m[m.length] = join(toks.subList(tokpat.size(), toks.size())); // for Matches.rest()
      }
      return true;
    }
  }
static boolean substanceLookAndFeelEnabled() {
  return startsWith(getLookAndFeel(), "org.pushingpixels.");
}
// Try to get the quoting right...

static String smartJoin(String[] args) {
  if (args.length == 1) return args[0];
  
  String[] a = new String[args.length];
  for (int i = 0; i < a.length; i++)
    a[i] = !isJavaIdentifier(args[i]) && !isQuoted(args[i]) ? quote(args[i]) : args[i];
  return join(" ", a);
}

static String smartJoin(List<String> args) {
  return smartJoin(toStringArray(args));
}
static JComponent getTitlePaneComponent(Window window) {
  if (!substanceLookAndFeelEnabled()) return null;
  
	JRootPane rootPane = null;
	if (window instanceof JFrame)
		rootPane = ((JFrame) window).getRootPane();
	if (window instanceof JDialog)
		rootPane = ((JDialog) window).getRootPane();
	if (rootPane != null) {
		Object /*SubstanceRootPaneUI*/ ui = rootPane.getUI();
		return (JComponent) call(ui, "getTitlePane");
	}
	return null;
}

static WeakReference<Class> creator_class;

static Class creator() {
  return creator_class == null ? null : creator_class.get();
}
static Object evalInNewThread(final Object f) {
  final Flag flag = new Flag();
  final Var var = new Var();
  final Var<Throwable> exception = new Var();
  { Thread _t_0 = new Thread() {
public void run() { try {
    try {
      var.set(callF(f));
    } catch (Throwable e) {
      exception.set(e);
    }
    flag.raise();
  } catch (Throwable __e) { printStackTrace2(__e); } }
};
startThread(_t_0); }
  flag.waitUntilUp();
  if (exception.has()) throw rethrow(exception.get());
  return var.get();
}
static 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<String> history) {
  return callStaticAnswerMethod(mc(), s, history);
}

static String callStaticAnswerMethod(Object c, String s, List<String> history) {
  String answer = (String) callOpt(c, "answer", s, history);
  if (answer == null)
    answer = (String) callOpt(c, "answer", s);
  return emptyToNull(answer);
}
static String getSnippetTitleOpt(String s) {
  return isSnippetID(s) ? getSnippetTitle(s) : s;
}
static File DiskSnippetCache_file(long snippetID) {
  return new File(getGlobalCache(), "data_" + snippetID + ".jar");
}
  
  // Data files are immutable, use centralized cache
public static File DiskSnippetCache_getLibrary(long snippetID) throws IOException {
  File file = DiskSnippetCache_file(snippetID);
  return file.exists() ? file : null;
}

public static void DiskSnippetCache_putLibrary(long snippetID, byte[] data) throws IOException {
  saveBinaryFile(DiskSnippetCache_file(snippetID), data);
}

static byte[] loadDataSnippetImpl(String snippetID) throws IOException {
  byte[] data;
  try {
    URL url = new URL("http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_"
      + parseSnippetID(snippetID) + "&contentType=application/binary");
    System.err.println("Loading library: " + url);
    try {
      data = loadBinaryPage(url.openConnection());
    } catch (RuntimeException e) {
      data = null;
    }
    
    if (data == null || data.length == 0) {
      url = new URL("http://data.tinybrain.de/blobs/"
        + parseSnippetID(snippetID));
      System.err.println("Loading library: " + url);
      data = loadBinaryPage(url.openConnection());
    }
    System.err.println("Bytes loaded: " + data.length);
  } catch (FileNotFoundException e) {
    throw new IOException("Binary snippet #" + snippetID + " not found or not public");
  }
  return data;
}
static String sendToLocalBotOpt(String bot, String text, Object... args) {
  if (bot == null) return null;
  text = format(text, args);
  DialogIO channel = findBot(bot);
  if (channel == null) {
    print(quote(bot) + " not found, skipping send: " + quote(text));
    return null;
  }
  try {
    channel.readLine();
    print(shorten(bot + "> " + text, 200));
    channel.sendLine(text);
    String s = channel.readLine();
    print(shorten(bot + "< " + s, 200));
    return s;
  } catch (Throwable e) {
    e.printStackTrace();
    return null;
  } finally {
    channel.close();
  }
}


  /** writes safely (to temp file, then rename) */
  public static void saveBinaryFile(String fileName, byte[] contents) throws IOException {
    File file = new File(fileName);
    File parentFile = file.getParentFile();
    if (parentFile != null)
      parentFile.mkdirs();
    String tempFileName = fileName + "_temp";
    FileOutputStream fileOutputStream = newFileOutputStream(tempFileName);
    fileOutputStream.write(contents);
    fileOutputStream.close();
    if (file.exists() && !file.delete())
      throw new IOException("Can't delete " + fileName);

    if (!new File(tempFileName).renameTo(file))
      throw new IOException("Can't rename " + tempFileName + " to " + fileName);
  }

  static void saveBinaryFile(File fileName, byte[] contents) {
    try {
      saveBinaryFile(fileName.getPath(), contents);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
static String getSnippetTitle(String id) { try {
  if (id == null) return null;
  if (!isSnippetID(id)) return "?";
  long parsedID = parseSnippetID(id);
  String url;
  if (isImageServerSnippet(parsedID))
    url = "http://ai1.lol/images/raw/title/" + parsedID;
  else
    url = tb_mainServer() + "/tb-int/getfield.php?id=" + parsedID + "&field=title" + standardCredentials();
  return trim(loadPageSilently(url));
} catch (Exception __e) { throw rethrow(__e); } }

static String getSnippetTitle(long id) {
  return getSnippetTitle(fsI(id));
}

static String emptyToNull(String s) {
  return eq(s, "") ? null : s;
}
static byte[] loadBinaryPage(String url) { try {
  print("Loading " + url);
  return loadBinaryPage(loadPage_openConnection(new URL(url)));
} catch (Exception __e) { throw rethrow(__e); } }

static byte[] loadBinaryPage(URLConnection con) { try {
  setHeaders(con);
  return loadBinaryPage_noHeaders(con);
} catch (Exception __e) { throw rethrow(__e); } }

static byte[] loadBinaryPage_noHeaders(URLConnection con) { try {
  ByteArrayOutputStream buf = new ByteArrayOutputStream();
  InputStream inputStream = con.getInputStream();
  long len = 0;
  try { len = con.getContentLengthLong(); } catch (Throwable e) { printStackTrace(e); }
  int n = 0;
  while (true) {
    int ch = inputStream.read();
    if (ch < 0)
      break;
    buf.write(ch);
    if (++n % 100000 == 0)
      println("  " + n + (len != 0 ? "/" + len : "") + " bytes loaded.");
  }
  inputStream.close();
  return buf.toByteArray();
} catch (Exception __e) { throw rethrow(__e); } }

// An "Android" is a program that accepts text questions (on console or TCP) and outputs one response text per question

static boolean makeAndroid3_disable; // disable all android making

static class Android3 {
  String greeting;
  boolean publicOverride; // optionally set this in client
  int startPort = 5000; // optionally set this in client
  Responder responder;
  boolean console = true;
  boolean quiet; // no messages on console
  boolean daemon = false;
  boolean incomingSilent = false;
  int incomingPrintLimit = 200;
  boolean useMultiPort = true;
  boolean recordHistory;
  boolean verbose;
  int answerPrintLimit = 500;
  boolean newLineAboveAnswer, newLineBelowAnswer;
  
  // set by system
  int port;
  long vport;
  DialogHandler handler;
  ServerSocket server;
  
  Android3(String greeting) {
  this.greeting = greeting;}
  Android3() {}
  
  synchronized void dispose() {
    if (server != null) {
      try {
        server.close();
      } catch (IOException e) {
        print("[internal] " + e);
      }
      server = null;
    }
    if (vport != 0) try {
      print("Disposing " + this);
      removeFromMultiPort(vport);
      vport = 0;
    } catch (Throwable __e) { printStackTrace2(__e); }
  }
  
  public String toString() { return "Bot: " + greeting + " [vport " + vport + "]"; }
}

static abstract class Responder {
  abstract String answer(String s, List<String> 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<String> history) {
        return callStaticAnswerMethod(s, history);
      }
    };
    
  if (!a.quiet)
    print("[bot] " + a.greeting);
  
  if (a.console && (readLine_noReadLine || makeAndroid3_consoleInUse()))
    a.console = false;
  
  record(a);
  
  if (a.useMultiPort)
    a.vport = addToMultiPort(a.greeting,
      makeAndroid3_verboseResponder(a));
      
  if (a.console)
    makeAndroid3_handleConsole(a);

  if (a.useMultiPort) return a;

  a.handler = makeAndroid3_makeDialogHandler(a);
  if (a.quiet) startDialogServer_quiet.set(true);
  try {
    a.port = a.daemon
      ? startDialogServerOnPortAboveDaemon(a.startPort, a.handler)
      : startDialogServerOnPortAbove(a.startPort, a.handler);
  } finally {
    startDialogServer_quiet.set(null);
  }
  a.server = startDialogServer_serverSocket;

  return a;
}

static void makeAndroid3_handleConsole(final Android3 a) {
  // Console handling stuff
  if (!a.quiet)
    print("You may also type on this console.");
  { Thread _t_0 = new Thread() {
public void run() { try {
    List<String> history = new ArrayList();
    while (licensed()) {
      String line;
      try {
        line = readLine();
      } catch (Throwable e) {
        print(getInnerMessage(e));
        break;
      }
      if (line == null) break;
      /*if (eq(line, "bye")) {
        print("> bye stranger");
        history = new ArrayList<S>();
      } else*/ {
        history.add(line);
        history.add(makeAndroid3_getAnswer(line, history, a)); // prints answer on console too
      }
    }
  } catch (Throwable __e) { printStackTrace2(__e); } }
};
startThread(_t_0); }
}

static DialogHandler makeAndroid3_makeDialogHandler(final Android3 a) {
  return new DialogHandler() {
public void run(final DialogIO io) {
    if (!a.publicOverride && !(publicCommOn() || io.isLocalConnection())) {
      io.sendLine("Sorry, not allowed");
      return;
    }
    
    String dialogID = randomID(8);
    
    io.sendLine(a.greeting + " / Your ID: " + dialogID);
    
    List<String> 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<String> history, Android3 a) {
  String answer, originalAnswer;
  try {
    originalAnswer = a.responder.answer(line, history);
    answer = makeAndroid3_fallback(line, history, originalAnswer);
  } catch (Throwable e) {
    e = getInnerException(e);
    printStackTrace(e);
    originalAnswer = answer = e.toString();
  }
  if (!a.incomingSilent) {
    if (originalAnswer == null) originalAnswer = "?";
    if (a.newLineAboveAnswer) print();
    print(">" + dropFirst(indentx(2, shorten(rtrim(originalAnswer), a.answerPrintLimit))));
    if (a.newLineBelowAnswer) print();
  }
  return answer;
}

static String makeAndroid3_fallback(String s, List<String> 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<String> history) {
      if (a.verbose)
        print("> " + shorten(s, a.incomingPrintLimit));
      String answer = a.responder.answer(s, history);
      if (a.verbose)
        print("< " + shorten(answer, a.incomingPrintLimit));
      return answer;
    }
  };
}

static ThreadLocal<DialogIO> makeAndroid3_io = new ThreadLocal();

static Android3 makeAndroid3() {
  return makeAndroid3(getProgramTitle() + ".");
}
static String getProgramTitle() {
  return getProgramName();
}
static Object[] asObjectArray(List l) {
  return toObjectArray(l);
}
static String dataSnippetLink(String snippetID) {
  long id = parseSnippetID(snippetID);
  if (id >= 1200000 && id < 1300000) {
    String pw = muricaPassword();
    if (empty(pw)) throw fail("Please set 'murica password by running #1008829");
    return "http://ai1.lol/1008823/raw/" + id + "?_pass=" + pw; // XXX, although it typically gets hidden when printing
  } else
    return "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_"
      + id + "&contentType=application/binary";
}
static void loadBinaryPageToFile(String url, File file) { try {
  print("Loading " + url);
  loadBinaryPageToFile(openConnection(new URL(url)), file);
} catch (Exception __e) { throw rethrow(__e); } }

static void loadBinaryPageToFile(URLConnection con, File file) { try {
  setHeaders(con);
  loadBinaryPageToFile_noHeaders(con, file);
} catch (Exception __e) { throw rethrow(__e); } }

static void loadBinaryPageToFile_noHeaders(URLConnection con, File file) { try {
  File ftemp = new File(f2s(file) + "_temp");
  FileOutputStream buf = newFileOutputStream(mkdirsFor(ftemp));
  InputStream inputStream = con.getInputStream();
  try {
    long len = 0;
    try { len = con.getContentLengthLong(); } catch (Throwable e) { printStackTrace(e); }
    int n = 0;
    while (true) {
      int ch = inputStream.read();
      if (ch < 0)
        break;
      buf.write(ch);
      if (++n % 100000 == 0)
        println("  " + n + (len != 0 ? "/" + len : "") + " bytes loaded.");
    }
    buf.close();
    buf = null;
    file.delete();
    ftemp.renameTo(file);
    inputStream.close();
  } finally {
    if (buf != null) buf.close();
  }
} catch (Exception __e) { throw rethrow(__e); } }

static String getLookAndFeel() {
  return getClassName(UIManager.getLookAndFeel());
}
static String ok2(String s) {
  return "ok " + s;
}
static void arraycopy(Object[] a, Object[] b) {
  int n = min(a.length, b.length);
  for (int i = 0; i < n; i++)
    b[i] = a[i];
}

static void arraycopy(Object src, int srcPos, Object dest, int destPos, int n) {
  System.arraycopy(src, srcPos, dest, destPos, n);
}
static String makeResponder_callAnswerMethod(Object bot, String s, List<String> history) {
  String answer = (String) callOpt(bot, "answer", s, history);
  if (answer == null)
    answer = (String) callOpt(bot, "answer", s);
  return answer;
}

static Responder makeResponder(final Object bot) {
  if (bot instanceof Responder) return (Responder) bot;
  
  if (bot instanceof String) {
    String f = (String) ( bot);
    return new Responder() {
      String answer(String s, List<String> history) {
        String answer = (String) callOptMC((String) bot, s, history);
        if (answer == null)
          answer = (String) callOptMC((String) bot, s);
        return answer;
      }
    };
  }
  
  return new Responder() {
    String answer(String s, List<String> history) {
      return makeResponder_callAnswerMethod(bot, s, history);
    }
  };
}


static AtomicInteger dialogServer_clients = new AtomicInteger();
static boolean dialogServer_printConnects;
static ThreadLocal<Boolean> startDialogServer_quiet = new ThreadLocal();

static Set<String> dialogServer_knownClients = synchroTreeSet();

static int startDialogServerOnPortAbove(int port, DialogHandler handler) {
  while (!forbiddenPort(port) && !startDialogServerIfPortAvailable(port, handler))
    ++port;
  return port;
}

static int startDialogServerOnPortAboveDaemon(int port, DialogHandler handler) {
  while (!forbiddenPort(port) && !startDialogServerIfPortAvailable(port, handler, true))
    ++port;
  return port;
}

static void startDialogServer(int port, DialogHandler handler) {
  if (!startDialogServerIfPortAvailable(port, handler))
    throw fail("Can't start dialog server on port " + port);
}

static boolean startDialogServerIfPortAvailable(int port, final DialogHandler handler) {
  return startDialogServerIfPortAvailable(port, handler, false);
}

static ServerSocket startDialogServer_serverSocket;
  
static boolean startDialogServerIfPortAvailable(int port, final DialogHandler handler, boolean daemon) {
  ServerSocket serverSocket = null;
  try {
    serverSocket = new ServerSocket(port);
  } catch (IOException e) {
    // probably the port number is used - let's assume there already is a chat server.
    return false;
  }
  final ServerSocket _serverSocket = serverSocket;
  startDialogServer_serverSocket = serverSocket;

  Thread thread = new Thread("Socket accept port " + port) { public void run() {
   try {
    while (true) {
      try {
        final Socket s = _serverSocket.accept();
        
        String client = s.getInetAddress().toString();
        if (!dialogServer_knownClients.contains(client) && neq(client, "/127.0.0.1")) {
          print("connect from " + client + " - clients: " + dialogServer_clients.incrementAndGet());
          dialogServer_knownClients.add(client);
        }
        
        String threadName = "Handling client " + s.getInetAddress();

        Thread t2 = new Thread(threadName) {
         public void run() {
          try {
            final Writer w = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
            final BufferedReader in = new BufferedReader(
              new InputStreamReader(s.getInputStream(), "UTF-8"));
              
            DialogIO io = new DialogIO() {
            
              // This should be the same as #1001076 (talkTo)
            
  boolean isLocalConnection() {
    return s.getInetAddress().isLoopbackAddress();
  }
  
  boolean isStillConnected() {
    return !(eos || s.isClosed());
  }
  
  void sendLine(String line) { try {
    w.write(line + "\n");
    w.flush();
  } catch (Exception __e) { throw rethrow(__e); } }
  
  String readLineImpl() { try {
    return in.readLine();
  } catch (Exception __e) { throw rethrow(__e); } }
  
  void close() {
    try {
      s.close();
    } catch (IOException e) {
      // whatever
    }
  }
  
  Socket getSocket() {
    return s;
  }
  };
            
            try {
              handler.run(io);
            } finally {
              s.close();
            }
          } catch (IOException e) {
            print("[internal] " + e);
          } finally {
            //print("client disconnect - " + dialogServer_clients.decrementAndGet() + " remaining");
          }
         }
        }; // Thread t2
        t2.setDaemon(true); // ?
        t2.start();
      } catch (SocketTimeoutException e) {
      }
    }   
   } catch (IOException e) {
     print("[internal] " + e);
   }
  }};
  if (daemon) thread.setDaemon(true);
  thread.start();
 
  if (!isTrue(getAndClearThreadLocal(startDialogServer_quiet)))
    print("Dialog server on port " + port + " started."); 
  return true;
}
static void removeFromMultiPort(long vport) {
  if (vport == 0) return;
  for (Object port : getMultiPorts())
    call(port, "removePort", vport);
}
static String getInjectionID() {
  return (String) call(getJavaX(), "getInjectionID", getMainClass());
}
static String muricaPassword() {
  return trim(loadTextFile(muricaPasswordFile()));
}
static String processID_cached;

// try to get our current process ID
static String getPID() {
  if (processID_cached == null) {
    String name = ManagementFactory.getRuntimeMXBean().getName();
    processID_cached = name.replaceAll("@.*", "");
  }
  return processID_cached;
}
static List<Object> record_list = synchroList();

static void record(Object o) {
  record_list.add(o);
}
static String myJavaSource_code;

static String myJavaSource() {
  return myJavaSource_code;
}
static boolean publicCommOn() {
  return "1".equals(loadTextFile(new File(userHome(), ".javax/public-communication")));
}
static <A> A println(A a) {
  return print(a);
}
static Object addToMultiPort_responder;

static long addToMultiPort(final String botName) {
  return addToMultiPort(botName, new Object() {
    public String answer(String s, List<String> 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<String> 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 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 Object callOptMC(String method, Object... args) {
  return callOpt(mc(), method, args);
}
static String getInnerMessage(Throwable e) {
  if (e == null) return null;
  return getInnerException(e).getMessage();
}


static File muricaPasswordFile() {
  return new File(javaxSecretDir(), "murica/muricaPasswordFile");
}
static List<Object> getMultiPorts() {
  return (List) callOpt(getJavaX(), "getMultiPorts");
}
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<String> indent(String indent, List<String> lines) {
  List<String> l = new ArrayList();
  for (String s : lines)
    l.add(indent + s);
  return l;
}
static boolean forbiddenPort(int port) {
  return port == 5037; // adb
}

// start multi-port if none exists in current VM.
static void startMultiPort() {
  List mp = getMultiPorts();
  if (mp != null && mp.isEmpty()) {
    nohupJavax("#1001639");
    throw fail("Upgrading JavaX, please restart this program afterwards.");
    //callMain(hotwire("#1001672"));
  }
}


static File javaxSecretDir_dir; // can be set to work on different base dir

static File javaxSecretDir() {
  return javaxSecretDir_dir != null ? javaxSecretDir_dir : new File(userHome(), "JavaX-Secret");
}


static class BetterLabel extends JLabel {
  BetterLabel() {
    componentPopupMenu(this, new Object() { void get(JPopupMenu menu) { try { 
      addMenuItem(menu, "Copy text to clipboard", new Runnable() { public void run() { try { 
        copyTextToClipboard(getText());
      
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "copyTextToClipboard(getText());"; }});
     } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "addMenuItem(menu, \"Copy text to clipboard\", r {\r\n        copyTextToClipboard(..."; }});
  }
  
  BetterLabel(String text) {
    this();
    setText(text);
  }
  
  public void setText(String text) {
    super.setText(text);
    setToolTipText(text);
  }
}static class Str extends Concept {
  String name;
  List<String> otherNames = new ArrayList();
  
  Str() {}
  Str(String name) {
  this.name = name;}
  
  public String toString() { return name; }
}



static class TransferableImage implements Transferable {
  Image i;

  TransferableImage(Image i) {
  this.i = i;}

  public Object getTransferData( DataFlavor flavor )
  throws UnsupportedFlavorException, IOException {
      if ( flavor.equals( DataFlavor.imageFlavor ) && i != null ) {
          return i;
      }
      else {
          throw new UnsupportedFlavorException( flavor );
      }
  }

  public DataFlavor[] getTransferDataFlavors() {
      DataFlavor[] flavors = new DataFlavor[ 1 ];
      flavors[ 0 ] = DataFlavor.imageFlavor;
      return flavors;
  }

  public boolean isDataFlavorSupported( DataFlavor flavor ) {
      DataFlavor[] flavors = getTransferDataFlavors();
      for ( int i = 0; i < flavors.length; i++ ) {
          if ( flavor.equals( flavors[ i ] ) ) {
              return true;
          }
      }

      return false;
  }
}static class MapInConcept<K, V> implements Map<K, V> {
  Map<K, V> m;  // Backing Map
  Concept cc;

  MapInConcept() {} // for persistence
  MapInConcept(Map<K, V> m, Concept cc) {
  this.cc = cc;
  this.m = m;}

  public int size() {
      synchronized (this) {return m.size();}
  }
  public boolean isEmpty() {
      synchronized (this) {return m.isEmpty();}
  }
  public boolean containsKey(Object key) {
      synchronized (this) {return m.containsKey(key);}
  }
  public boolean containsValue(Object value) {
      synchronized (this) {return m.containsValue(value);}
  }
  public V get(Object key) {
      synchronized (this) {return m.get(key);}
  }

  public V put(K key, V value) {
      try { synchronized (this) {return m.put(key, value);} } finally { cc.change(); }
  }
  public V remove(Object key) {
      try { synchronized (this) {return m.remove(key);} } finally { cc.change(); }
  }
  public void putAll(Map<? extends K, ? extends V> map) {
      synchronized (this) {m.putAll(map);} cc.change();
  }
  public void clear() {
      synchronized (this) {m.clear();} cc.change();
  }

  private transient Set<K> keySet;
  private transient Set<Map.Entry<K,V>> entrySet;
  private transient Collection<V> values;

  public Set<K> keySet() {
      synchronized (this) {
          if (keySet==null)
              keySet = new SynchronizedSet(m.keySet(), this);
          return keySet;
      }
  }

  public Set<Map.Entry<K,V>> entrySet() {
      synchronized (this) {
          if (entrySet==null)
              entrySet = new SynchronizedSet(m.entrySet(), this);
          return entrySet;
      }
  }

  public Collection<V> values() {
      synchronized (this) {
          if (values==null)
              values = new SynchronizedCollection(m.values(), this);
          return values;
      }
  }

  public boolean equals(Object o) {
      if (this == o)
          return true;
      synchronized (this) {return m.equals(o);} // TODO: deadlock?
  }
  public int hashCode() {
      synchronized (this) {return m.hashCode();}
  }
  public String toString() {
      synchronized (this) {return m.toString();}
  }

  // 1.8 stuff omitted
  
  Map<K, V> unwrap() { return m; }
}static class AutoComboBox extends JComboBox<Object> {
  String keyWord[] = {"item1", "item2", "item3"};
  Vector myVector = new Vector();
  
  AutoComboBox() {
    setModel(new DefaultComboBoxModel(myVector));
    setSelectedIndex(-1);
    setEditable(true);
    JTextField text = (JTextField) this.getEditor().getEditorComponent();
    text.setFocusable(true);
    text.setText("");
    text.addKeyListener(new ComboListener(this, myVector));
    setMyVector();
  }
  
  /**
   * set the item list of the AutoComboBox
   * @param keyWord an String array
   */
  void setKeyWord(String[] keyWord) {
    this.keyWord = keyWord;
    setMyVector();
  }
  
  void setKeyWord(Collection<String> keyWord) {
    setKeyWord(toStringArray(keyWord));
  }
  
  private void setMyVector() {
    copyArrayToVector(keyWord, myVector);
  }
}

static class ComboListener extends KeyAdapter {
  JComboBox cbListener;
  Vector vector;
  
  public ComboListener(JComboBox cbListenerParam, Vector vectorParam) {
      cbListener = cbListenerParam;
      vector = vectorParam;
  }
  
  public void keyReleased(KeyEvent key) {
    if (key.getKeyCode() == KeyEvent.VK_ENTER) return;
    
    JTextField tf = (JTextField) ( cbListener.getEditor().getEditorComponent());
    
    if (tf.getCaretPosition() != l(tf.getText())) return;
    
    String text = ((JTextField) key.getSource()).getText();

    Vector list = getFilteredList(text);
    if (nempty(list)) {
      cbListener.setModel(new DefaultComboBoxModel(list));
      cbListener.setSelectedIndex(-1);
      tf.setText(text); // necessary?
      cbListener.showPopup();
    } else
      cbListener.hidePopup();
  }

  public Vector getFilteredList(String text) {
    return new Vector(scoredSearch(text, vector));
  }
}static class ProgramScan {
  static int threads = isWindows() ? 500 : 10;
  static int timeout = 5000; // hmm...
  static String ip = "127.0.0.1";
  
  // This range is not used anymore anyway
  static int quickScanFrom = 10000, quickScanTo = 10999;

  static int maxNumberOfVMs_android = 4; // Android will always only have one if we don't screw up
  static int maxNumberOfVMs_nonAndroid = 50; // 100;
  static int maxNumberOfVMs;
  
  static boolean verbose;
  
  static class Program {
    int port;
    String helloString;
    
    Program(int port, String helloString) {
  this.helloString = helloString;
  this.port = port;}
  }
  
  static List<Program> scan() { try {
    return scan(1, 65535);
  } catch (Exception __e) { throw rethrow(__e); } }
  
  static List<Program> scan(int fromPort, int toPort) {
    return scan(fromPort, toPort, new int[0]);
  }
  
  static List<Program> scan(int fromPort, int toPort, int[] preferredPorts) { try {
    Set<Integer> preferredPortsSet = new HashSet<Integer>(asList(preferredPorts));
    int scanSize = toPort-fromPort+1;
    String name = toPort < 10000 ? "bot" : "program";
    int threads = isWindows() ? min(500, scanSize) : min(scanSize, 10);
    final ExecutorService es = Executors.newFixedThreadPool(threads);
    if (verbose) print(firstToUpper(name) + "-scanning " + ip + " with timeout " + timeout + " ms in " + threads + " threads.");
    startTiming();
    List < Future < Program > > futures = new ArrayList();
    List<Integer> ports = new ArrayList();
    for (int port : preferredPorts) {
      futures.add(checkPort(es, ip, port, timeout));
      ports.add(port);
    }
    for (int port = fromPort; port <= toPort; port++)
      if (!preferredPortsSet.contains(port) && !forbiddenPort(port)) {
        futures.add(checkPort(es, ip, port, timeout));
        ports.add(port);
      }
    es.shutdown();
    List<Program> programs = new ArrayList();
    long time = now();
    int i = 0;
    for (final Future<Program> f : futures) {
      if (verbose) print("Waiting for port " + get(ports, i++) + " at time " + (now()-time));
      Program p = f.get();
      if (p != null)
        programs.add(p);
    }
    //stopTiming("Port Scan " + scanSize + ", " + n(threads, "threads") + ": ", 250);
    if (verbose) print("Found " + programs.size() + " " + name + "(s) on " + ip);
    return programs;
  } catch (Exception __e) { throw rethrow(__e); } }

  static Future<Program> checkPort(final ExecutorService es, final String ip, final int port, final int timeout) {
    return es.submit(new Callable<Program>() {
        @Override public Program call() {
          try {
            Socket socket = new Socket();
            try {
              socket.setSoTimeout(timeout);
              socket.connect(new InetSocketAddress(ip, port), timeout);
              //if (verbose) print("Connected to " + ip + ":" + port);
              BufferedReader in = new BufferedReader(
                new InputStreamReader(socket.getInputStream(), "UTF-8"));
              String hello = or(in.readLine(), "?");
              return new Program(port, hello);
            } finally {
              socket.close();
            }
          } catch (Exception ex) {
            return null;
          }
        }
     });
  }
  
  static List<Program> quickScan() {
    return scan(quickScanFrom, quickScanTo);
  }
  
  static List<Program> quickBotScan() {
    return quickBotScan(new int[0]);
  }
  
  static List<Program> quickBotScan(int[] preferredPorts) {
    if (maxNumberOfVMs == 0)
      maxNumberOfVMs = isAndroid() ? maxNumberOfVMs_android : maxNumberOfVMs_nonAndroid;
    return scan(4999, 5000+maxNumberOfVMs-1, preferredPorts);
  }
}

static class SynchronizedSet<E> extends SynchronizedCollection<E> implements Set<E> {
  SynchronizedSet() {} // for persistence
  SynchronizedSet(Set<E> set) { super(set); }
  SynchronizedSet(Set<E> set, Object mutex) { super(set, mutex); }
  
  public boolean equals(Object o) {
      if (this == o)
          return true;
      synchronized (this) {return c.equals(o);}
  }
  
  public int hashCode() {
      synchronized (this) {return c.hashCode();}
  }
}static class SynchronizedCollection<E> implements Collection<E> {
  Collection<E> c;  // Backing Collection
  Object mutex;     // Object on which to synchronize

  SynchronizedCollection() {} // for persistence
  SynchronizedCollection(Collection<E> c) {
      this.c = Objects.requireNonNull(c);
      mutex = this;
  }

  SynchronizedCollection(Collection<E> c, Object mutex) {
      this.c = Objects.requireNonNull(c);
      this.mutex = Objects.requireNonNull(mutex);
  }

  public int size() {
      synchronized (mutex) {return c.size();}
  }
  public boolean isEmpty() {
      synchronized (mutex) {return c.isEmpty();}
  }
  public boolean contains(Object o) {
      synchronized (mutex) {return c.contains(o);}
  }
  public Object[] toArray() {
      synchronized (mutex) {return c.toArray();}
  }
  public <T> T[] toArray(T[] a) {
      synchronized (mutex) {return c.toArray(a);}
  }

  public Iterator<E> iterator() {
      return c.iterator(); // Must be manually synched by user!
  }

  public boolean add(E e) {
      synchronized (mutex) {return c.add(e);}
  }
  public boolean remove(Object o) {
      synchronized (mutex) {return c.remove(o);}
  }

  public boolean containsAll(Collection<?> coll) {
      synchronized (mutex) {return c.containsAll(coll);}
  }
  public boolean addAll(Collection<? extends E> coll) {
      synchronized (mutex) {return c.addAll(coll);}
  }
  public boolean removeAll(Collection<?> coll) {
      synchronized (mutex) {return c.removeAll(coll);}
  }
  public boolean retainAll(Collection<?> coll) {
      synchronized (mutex) {return c.retainAll(coll);}
  }
  public void clear() {
      synchronized (mutex) {c.clear();}
  }
  public String toString() {
      synchronized (mutex) {return c.toString();}
  }
  
  /*public void forEach(Consumer<? super E> consumer) {
      synchronized (mutex) {c.forEach(consumer);}
  }*/
 
  /*public boolean removeIf(Predicate<? super E> filter) {
      synchronized (mutex) {return c.removeIf(filter);}
  }*/
  @Override
  public Spliterator<E> spliterator() {
      return c.spliterator(); // Must be manually synched by user!
  }
  
  /*public Stream<E> stream() {
      return c.stream(); // Must be manually synched by user!
  }*/
  
  /*public Stream<E> parallelStream() {
      return c.parallelStream(); // Must be manually synched by user!
  }*/
}

// union-type entry
static class E {
  String q, a, state;
  boolean test; // not used anymore probably
  String comment; // not checked for comparison
  
  E() {}
  E(String text, String type) {
    if (eq(type, "q")) q = text;
    else if (eq(type, "a")) a = text;
    else state = text;
  }
  
  // does not compare the "test" bit
  boolean eqTo(E y) {
    E x = this;
    return eq/*ic*/(x.q, y.q) && eq/*ic*/(x.a, y.a) && eq/*ic*/(x.state, y.state);
  }
  
  public boolean equals(Object o) {
    return o instanceof E && eqTo((E) o);
  }
  
  public int hashCode() {
    return stdHash(this, "q", "a", "state");
  }
  
  static E state(String state) {
    if (state == null) return null; E e = new E(); e.state = state; return e;
  }
  
  static E q(String q) {
    if (q == null) return null; E e = new E(); e.q = q; return e;
  }
  
  static E a(String a) {
    if (a == null) return null; E e = new E(); e.a = a; return e;
  }
  
  public String toString() {
    return unparsePoemLine(this);
  }
  
  boolean q() { return q != null; }
  boolean a() { return a != null; }
  boolean state() { return state != null; }
  String type() { return q() ? "q" : a() ? "a" : "state"; }
  String text() { return q() ? q : a() ? a : state; }
}

static String unparsePoemLine(E e) {
  return trim(unparsePoem(litlist(e)));
}
static void copyArrayToVector(Object[] array, Vector v) {
  v.clear();
  v.addAll(toList(array));
}
static String firstToUpper(String s) {
  if (empty(s)) return s;
  return Character.toUpperCase(s.charAt(0)) + s.substring(1);
}
public static boolean isWindows() {
  return System.getProperty("os.name").contains("Windows");
}
static <A extends JComponent> A setToolTipText(final A c, final Object toolTip) {
  if (c == null) return null;
  { swing(new Runnable() { public void run() { try { 
    String s = nullIfEmpty(str(toolTip));
    if (neq(s, c.getToolTipText()))
      c.setToolTipText(s);
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "String s = nullIfEmpty(str(toolTip));\r\n    if (neq(s, c.getToolTipText()))\r\n ..."; }}); }
  return c;
}
static List<String> scoredSearch(String query, Collection<String> data) {
  Map<String,Integer> scores = new HashMap();
  for (String s : data) {
    int score = scoredSearch_score(s, query);
    if (score != 0)
      scores.put(s, score);
  }
  return keysSortedByValuesDesc(scores);
}
static long stopTiming_defaultMin = 10;

static long startTiming_startTime;
static void startTiming() {
  startTiming_startTime = now();
}

static void stopTiming() {
  stopTiming(null);
}

static void stopTiming(String text) {
  stopTiming(text, stopTiming_defaultMin);
}

static void stopTiming(String text, long minToPrint) {
  long time = now()-startTiming_startTime;
  if (time >= minToPrint) {
    text = or2(text, "Time: ");
    print(text + time + " ms");
  }
}
static <A> boolean containsAll(Collection<? extends A> a, Collection<A> b) {
  for (A o : b)
    if (!a.contains(o))
      return false;
  return true;
}
static JComponent unwrap(JComponent c) {
  return c instanceof JScrollPane ? unwrapScrollPane((JScrollPane) c) : c;
}


static int scoredSearch_score(String s, String query) {
  int score = 0;
  if (swic(s, query)) score += l(s) == l(query) ? 3 : 2;
  else if (containsIgnoreCase(s, query)) ++score;
  return score;
}
static String nullIfEmpty(String s) {
  return isEmpty(s) ? null : s;
}
static <A, B> List<A> keysSortedByValuesDesc(final Map<A, B> map) {
  List<A> l = new ArrayList(map.keySet());
  sort(l, mapComparatorDesc(map));
  return l;
}
static String unparsePoem(List<E> poem) {
  List<String> l = new ArrayList();
  for (E e : poem) {
    String s;
    if (e.q != null)
      s = "Usr: " + trim(e.q);
    else if (e.a != null)
      s = "Bot: " + trim(e.a);
    else if (e.state != null)
      s = "[" + trim(e.state) + "]";
    else
      s = "?";
    if (e.comment != null)
      s += " // " + e.comment;
    l.add(s);
  }
  return joinLines(l);
}
static JComponent unwrapScrollPane(JScrollPane sp) {
  return sp == null ? null : (JComponent) sp.getViewport().getView();
}


static <A, B> Comparator<A> mapComparatorDesc(final Map<A, B> map) {
  return new Comparator<A>() {
    public int compare(A a, A b) {
      return cmp(map.get(b), map.get(a));
    }
  };
}

}