+
+ public static Function resolveFunction(RuntimeModule rm, String function) throws ValueNotFound {
+ return (Function)rm.getValue(function);
+ }
+
+ private static SCLValue resolveSCLValue(RuntimeModule rm, String function) throws ValueNotFound {
+ return rm.getModule().getValue(function);
+ }
+
+ private static RuntimeModule resolveRuntimeModule(String module) throws ValueNotFound {
+ Failable<RuntimeModule> f = SCLOsgi.MODULE_REPOSITORY.getRuntimeModule(module);
+ if(f.didSucceed())
+ return f.getResult();
+ else if(f == DoesNotExist.INSTANCE)
+ throw new ValueNotFound("Didn't find module " + module);
+ else
+ throw new ValueNotFound(((Failure)f).toString());
+ }
+
+ private static List<TCon> getEffects(SCLValue value) throws ValueNotFoundException, ValueNotFound, MatchException {
+
+ Type type = value.getType();
+ MultiFunction mfun = Types.matchFunction(type, 1);
+ ArrayList<TCon> concreteEffects = new ArrayList<>();
+ mfun.effect.collectConcreteEffects(concreteEffects);
+ return concreteEffects;
+
+ }
+
+ public static List<TCon> getEffects(RuntimeModule rm, String function) throws ValueNotFoundException, ValueNotFound, MatchException {
+ return getEffects(resolveSCLValue(rm, function));
+ }
+
+ public static List<TCon> getEffects(String module, String function) throws ValueNotFoundException, ValueNotFound, MatchException {
+ return getEffects(resolveSCLValue(resolveRuntimeModule(module), function));
+ }
+
+ 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);
+ }
+ }