]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLFunctions.java
Fixed argument passing in async/sync read/write SCL functions
[simantics/platform.git] / bundles / org.simantics.scl.db / src / org / simantics / scl / db / SCLFunctions.java
index e20dfc471ad9ea28831463f00bc9bfc220d8704a..bca7ba2481ff641b5a7d4c300ed80642cb751ac2 100644 (file)
@@ -1,3 +1,14 @@
+/*******************************************************************************
+ * Copyright (c) 2019 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Semantum Oy - initial API and implementation
+ *******************************************************************************/
 package org.simantics.scl.db;
 
 import java.io.IOException;
@@ -112,7 +123,7 @@ public class SCLFunctions {
     }
 
     private static <T> T evaluate(RuntimeModule rm, String function, Object ... args) throws ValueNotFound {
-        return evaluate(resolveFunction(rm, function));
+        return evaluate(resolveFunction(rm, function), args);
     }
 
     public static <T> T evaluate(String module, String function, Object ... args) throws ValueNotFound {
@@ -140,7 +151,7 @@ public class SCLFunctions {
         }
     }
     
-    public static <T> T evaluateGraph(String module, String function, Object graph, Object ... args) throws DatabaseException {
+    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);
@@ -152,9 +163,21 @@ public class SCLFunctions {
         }
     }
 
+    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 {    
+    public static <T> void asyncRead(final Function f) throws DatabaseException {
         asyncRead(f, NO_ARGS);
     }
 
@@ -166,7 +189,7 @@ public class SCLFunctions {
                 SCLContext.push(context);
                 context.put(GRAPH, graph);
                 try {
-                    f.apply(Tuple0.INSTANCE);
+                    f.applyArray(args);
                 } finally {
                     SCLContext.pop();
                 }
@@ -190,7 +213,7 @@ public class SCLFunctions {
                     SCLContext.push(context);
                     ReadGraph oldGraph = (ReadGraph)context.put(GRAPH, graph);
                     try {
-                        return (T)f.apply(Tuple0.INSTANCE);
+                        return (T)f.applyArray(args);
                     } finally {
                         context.put(GRAPH, oldGraph);
                         SCLContext.pop();
@@ -213,7 +236,7 @@ public class SCLFunctions {
                     SCLContext.push(context);
                     context.put(GRAPH, graph);
                     try {
-                        f.apply(args);
+                        f.applyArray(args);
                     } finally {
                         SCLContext.pop();
                     }
@@ -231,9 +254,14 @@ public class SCLFunctions {
     public static <T> T syncWrite(final Function f, final Object ... args) throws DatabaseException {
         final SCLContext context = SCLContext.getCurrent();
         Object graph = context.get(GRAPH);
-        if (graph != null) {
-            return (T)f.apply(Tuple0.INSTANCE);
+        if (graph != null && graph instanceof WriteGraph) {
+            return (T)f.applyArray(args);
         } else {
+            if (graph != null) {
+                LOGGER.error(
+                        "SCLContext {} for current thread {} contains an existing graph object but it is not WriteGraph - Somewhere is a function that forgets to remove the graph from the context!!",
+                        context, Thread.currentThread());
+            }
             final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
             return Simantics.getSession().syncRequest(new WriteResultRequest<T>() {
                 @Override
@@ -242,7 +270,7 @@ public class SCLFunctions {
                     SCLReportingHandler oldPrinter = (SCLReportingHandler)context.put(SCLReportingHandler.REPORTING_HANDLER, printer);
                     ReadGraph oldGraph = (ReadGraph)context.put(GRAPH, graph);
                     try {
-                        return (T)f.apply(args);
+                        return (T)f.applyArray(args);
                     } finally {
                         context.put(GRAPH, oldGraph);
                         context.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);