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 java.util.function.*;
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 java.awt.geom.*;
import javax.imageio.*;
import java.math.*;
// script that recognizes images or performs some kind of analysis on an image
import static x30_pkg.x30_util.DynamicObject;
import java.text.*;
import java.nio.file.Path;
import java.util.jar.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import javax.swing.border.*;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;
import javax.swing.Timer;
import java.awt.geom.*;
import java.text.NumberFormat;
import javax.imageio.metadata.*;
import javax.imageio.stream.*;
import java.awt.datatransfer.StringSelection;
import java.nio.charset.Charset;
import javax.swing.undo.UndoManager;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
import java.awt.datatransfer.UnsupportedFlavorException;
import javax.swing.Icon;
class main {
static class G22RecognizerScript extends G22LeftArrowScript {
@Override GazelleV_LeftArrowScriptParser makeParser() {
var parser = super.makeParser();
parser.addVar("image", BufferedImage.class, false);
parser.addVar("context", G22AnalysisContext.class, false);
return parser;
}
class CompiledAnalyzer extends LASCompileResult {
final public CompiledAnalyzer setTimeout(double timeout){ return timeout(timeout); }
public CompiledAnalyzer timeout(double timeout) { this.timeout = timeout; return this; } final public double getTimeout(){ return timeout(); }
public double timeout() { return timeout; }
double timeout = 10;
Object get(BufferedImage image) {
G22AnalysisContext context = new G22AnalysisContext(g22utils(_concepts()), image);
return get(context);
}
Object get(G22AnalysisContext context) {
if (context.analyzerResults.containsKey(analyzer())) {
Object value = context.analyzerResults.get(analyzer());
if (value == G22AnalysisContext.calculating)
throw fail("Recursive call to analyzer " + this);
return ((OKOrError) value).getMandatory();
}
context.analyzerResults.put(analyzer(), G22AnalysisContext.calculating);
var result = okOrError(() -> {
if (parsedScript == null) rethrow(compileError);
var image = context.image;
if (image == null) throw fail("Need image");
return evalWithTimeoutOrFail(timeout, () -> {
VarContext ctx = new VarContext();
ctx.set("image", image);
ctx.set("context", context);
return parsedScript.get(ctx);
});
});
context.analyzerResults.put(analyzer(), result);
return result.getMandatory();
}
}
G22RecognizerScript analyzer() { return this; }
LASCompileResult newCompileResult() { return new CompiledAnalyzer(); }
CompiledAnalyzer compileForAutoRun() {
return (CompiledAnalyzer) super.compileForAutoRun();
}
}
static G22Utils g22utils(Concepts cc) {
return (G22Utils) cc.miscMapGet(G22Utils.class);
}
// get purpose 1: access a list/array/map (safer version of x.get(y))
static A get(List l, int idx) {
return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null;
}
// seems to conflict with other signatures
/*static B get(Map map, A key) {
ret map != null ? map.get(key) : null;
}*/
static A get(A[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : null;
}
// default to false
static boolean get(boolean[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : false;
}
// get purpose 2: access a field by reflection or a map
static Object get(Object o, String field) {
try {
if (o == null) return null;
if (o instanceof Class) return get((Class) o, field);
if (o instanceof Map)
return ((Map) o).get(field);
Field f = getOpt_findField(o.getClass(), field);
if (f != null) {
makeAccessible(f);
return f.get(o);
}
if (o instanceof DynamicObject)
return getOptDynOnly(((DynamicObject) o), field);
} catch (Exception e) {
throw asRuntimeException(e);
}
throw new RuntimeException("Field '" + field + "' not found in " + o.getClass().getName());
}
static Object get_raw(String field, Object o) {
return get_raw(o, field);
}
static Object get_raw(Object o, String field) { try {
if (o == null) return null;
Field f = get_findField(o.getClass(), field);
makeAccessible(f);
return f.get(o);
} catch (Exception __e) { throw rethrow(__e); } }
static Object get(Class c, String field) {
try {
Field f = get_findStaticField(c, field);
makeAccessible(f);
return f.get(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Field get_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
}
static Field get_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
}
static Object get(String field, Object o) {
return get(o, field);
}
static boolean get(BitSet bs, int idx) {
return bs != null && bs.get(idx);
}
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(Object... objects) { throw new Fail(objects); }
static RuntimeException fail(String msg) { throw new RuntimeException(msg == null ? "" : msg); }
static RuntimeException fail(String msg, Throwable innerException) { throw new RuntimeException(msg, innerException); }
static OKOrError okOrError(IF0 f) {
try {
return OKOrError_ok(f.get());
} catch (Throwable e) {
printStackTrace(e);
return OKOrError_error(e);
}
}
static OKOrError okOrError(Runnable r) {
return okOrError(runnableToIF0(r));
}
static RuntimeException rethrow(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static RuntimeException rethrow(String msg, Throwable t) {
throw new RuntimeException(msg, t);
}
static A evalWithTimeoutOrFail(int timeoutMS, F0 f) {
return evalWithTimeoutOrException(timeoutMS, f);
}
static A evalWithTimeoutOrFail(double timeoutSeconds, F0 f) {
return evalWithTimeoutOrException(timeoutSeconds, f);
}
static A evalWithTimeoutOrFail(int timeoutMS, IF0 f) {
return evalWithTimeoutOrException(timeoutMS, f);
}
static A evalWithTimeoutOrFail(double timeoutSeconds, IF0 f) {
return evalWithTimeoutOrException(timeoutSeconds, f);
}
static void evalWithTimeoutOrFail(int timeoutMS, Runnable r) {
evalWithTimeoutOrException(timeoutMS, runnableToF0(r));
}
static void evalWithTimeoutOrFail(double timeoutSeconds, Runnable r) {
evalWithTimeoutOrException(timeoutSeconds, runnableToF0(r));
}
static int l(Object[] a) { return a == null ? 0 : a.length; }
static int l(boolean[] a) { return a == null ? 0 : a.length; }
static int l(byte[] a) { return a == null ? 0 : a.length; }
static int l(short[] a) { return a == null ? 0 : a.length; }
static int l(long[] a) { return a == null ? 0 : a.length; }
static int l(int[] a) { return a == null ? 0 : a.length; }
static int l(float[] a) { return a == null ? 0 : a.length; }
static int l(double[] a) { return a == null ? 0 : a.length; }
static int l(char[] a) { return a == null ? 0 : a.length; }
static int l(Collection c) { return c == null ? 0 : c.size(); }
static int l(Iterator i) { return iteratorCount_int_close(i); } // consumes the iterator && closes it if possible
static int l(Map m) { return m == null ? 0 : m.size(); }
static int l(CharSequence s) { return s == null ? 0 : s.length(); }
static long l(File f) { return f == null ? 0 : f.length(); }
static int l(Object o) {
return o == null ? 0
: o instanceof String ? l((String) o)
: o instanceof Map ? l((Map) o)
: o instanceof Collection ? l((Collection) o)
: o instanceof Object[] ? l((Object[]) o)
: o instanceof boolean[] ? l((boolean[]) o)
: o instanceof byte[] ? l((byte[]) o)
: o instanceof char[] ? l((char[]) o)
: o instanceof short[] ? l((short[]) o)
: o instanceof int[] ? l((int[]) o)
: o instanceof float[] ? l((float[]) o)
: o instanceof double[] ? l((double[]) o)
: o instanceof long[] ? l((long[]) o)
: (Integer) call(o, "size");
}
static int l(MultiSet ms) { return ms == null ? 0 : ms.size(); }
static int l(IntRange r) { return r == null ? 0 : r.length(); }
static int l(AppendableChain a) { return a == null ? 0 : a.size; }
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 Field makeAccessible(Field f) {
try {
f.setAccessible(true);
} catch (Throwable e) {
// Note: The error reporting only works with Java VM option --illegal-access=deny
vmBus_send("makeAccessible_error", e, f);
}
return f;
}
static Method makeAccessible(Method m) {
try {
m.setAccessible(true);
} catch (Throwable e) {
vmBus_send("makeAccessible_error", e, m);
}
return m;
}
static Constructor makeAccessible(Constructor c) {
try {
c.setAccessible(true);
} catch (Throwable e) {
vmBus_send("makeAccessible_error", e, c);
}
return c;
}
static Object getOptDynOnly(DynamicObject o, String field) {
if (o == null || o.fieldValues == null) return null;
return o.fieldValues.get(field);
}
static RuntimeException asRuntimeException(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static OKOrError OKOrError_ok(A a) {
return new OKOrError(a);
}
static A printStackTrace(A e) {
// we go to system.out now - system.err is nonsense
if (e != null) print(getStackTrace(e));
return e;
}
static void printStackTrace() {
printStackTrace(new Throwable());
}
static void printStackTrace(String msg) {
printStackTrace(new Throwable(msg));
}
static void printStackTrace(String msg, Throwable e) {
printStackTrace(new Throwable(msg, e));
}
static OKOrError OKOrError_error(Throwable e) {
return new OKOrError(false, e);
}
static IF0 runnableToIF0(Runnable r) {
return r == null ? null : () -> { r.run(); return null; };
}
static void _handleError(Error e) {
call(javax(), "_handleError", e);
}
// Note: these first 3 syntaxes seem to be wrong
// (not failing on timeout)
static Object evalWithTimeoutOrException(final Object f, int timeoutMS) {
return evalWithTimeoutOrException(timeoutMS, f);
}
static Object evalWithTimeoutOrException(int timeoutMS, final Object f) {
return eitherAOpt(evalWithTimeout(timeoutMS, f));
}
static Object evalWithTimeoutOrException(double timeoutSeconds, final Object f) {
return eitherAOpt(evalWithTimeout(timeoutSeconds, f));
}
// the following 4 syntaxes are ok
static A evalWithTimeoutOrException(int timeoutMS, F0 f) {
Either