+ private static <T> T evaluate(Function function, Object ... args) {
+ return (T)function.applyArray(args);
+ }
+
+ private static <T> T evaluate(RuntimeModule rm, String function, Object ... args) throws ValueNotFound {
+ return evaluate(resolveFunction(rm, function), args);
+ }
+
+ public static <T> T evaluate(String module, String function, Object ... args) throws ValueNotFound {
+ return evaluate(resolveRuntimeModule(module), function, args);
+ }
+
+ public static <T> T evaluateDB(String module, String function, Object ... args) throws DatabaseException {
+ try {
+ RuntimeModule rm = resolveRuntimeModule(module);
+ List<TCon> effects = getEffects(resolveSCLValue(rm, function));
+ Function f = resolveFunction(rm, function);
+ if(effects.contains(Types.WRITE_GRAPH)) {
+ return syncWrite(f, args);
+ } else if(effects.contains(Types.READ_GRAPH)) {
+ return syncRead(f, args);
+ } else {
+ return evaluate(f, args);
+ }
+ } catch (ValueNotFound e) {
+ throw new DatabaseException("SCL Value not found: " + e.name);
+ } catch (Throwable t) {
+ if (t instanceof DatabaseException)
+ throw (DatabaseException) t;
+ throw new DatabaseException(t);
+ }
+ }
+
+ public static <T> T evaluateGraph(String module, String function, ReadGraph graph, Object ... args) throws DatabaseException {
+ final SCLContext context = SCLContext.getCurrent();
+ SCLContext.push(context);
+ Object oldGraph = context.put(GRAPH, graph);
+ try {
+ return evaluateDB(module, function, args);
+ } finally {
+ context.put(GRAPH, oldGraph);
+ SCLContext.pop();
+ }
+ }
+
+ public static void runWithGraph(ReadGraph graph, Runnable r) {
+ final SCLContext context = SCLContext.getCurrent();
+ SCLContext.push(context);
+ Object oldGraph = context.put(GRAPH, graph);
+ try {
+ r.run();
+ } finally {
+ context.put(GRAPH, oldGraph);
+ SCLContext.pop();
+ }
+ }
+
+ private static Object[] NO_ARGS = new Object[] { Tuple0.INSTANCE };
+
+ public static <T> void asyncRead(final Function f) throws DatabaseException {
+ asyncRead(f, NO_ARGS);
+ }
+
+ public static void asyncRead(final Function f, final Object ... args) throws DatabaseException {