From 41e86809c6f381612bdc8ffd4f4013aba39d1db1 Mon Sep 17 00:00:00 2001 From: tuorjr Date: Mon, 26 Sep 2016 15:44:59 +0000 Subject: [PATCH] Some fixes to Python integration. git-svn-id: https://www.simantics.org/svn/simantics-incubator/reino@1690 e36c2e66-7d30-0410-bdb2-d9e1f5a6d952 --- .../scl/Simantics/Python.scl | 8 ++++---- .../src/org/simantics/pythonlink/Python.java | 4 ++-- .../simantics/pythonlink/PythonContext.java | 20 ++++++++++++++----- .../pythonlink/test/ScriptTestBase.java | 5 ----- .../pythonlink/test/ScriptTests.java | 6 +----- .../pythonlink/test/scripts/Python.sts | 6 +++--- 6 files changed, 25 insertions(+), 24 deletions(-) diff --git a/org.simantics.pythonlink/scl/Simantics/Python.scl b/org.simantics.pythonlink/scl/Simantics/Python.scl index 899f577..375c549 100644 --- a/org.simantics.pythonlink/scl/Simantics/Python.scl +++ b/org.simantics.pythonlink/scl/Simantics/Python.scl @@ -45,7 +45,7 @@ importJava "org.simantics.pythonlink.PythonContext" where @JavaName close closePythonContext :: PythonContext -> () - executePythonStatement :: String -> Integer + executePythonStatement :: String -> () setPythonIntegerVariable :: String -> Integer -> () setPythonIntegerArrayVariable :: String -> Vector Integer -> () @@ -63,12 +63,12 @@ importJava "org.simantics.pythonlink.PythonContext" where getPythonStringArrayVariable :: String -> Vector String getPythonNDArrayVariable :: String -> NDArray - runPythonF :: (() -> a) -> a - runWithPythonContextF :: PythonContext -> (() -> a) -> a - importJava "org.simantics.pythonlink.Python" where openPythonContext :: () -> PythonContext + runPythonF :: (() -> a) -> a + runWithPythonContextF :: PythonContext -> (() -> a) -> a + runPython :: ( a) -> a runPython v = runPythonF (\_ -> v) diff --git a/org.simantics.pythonlink/src/org/simantics/pythonlink/Python.java b/org.simantics.pythonlink/src/org/simantics/pythonlink/Python.java index af5c9e2..07921fd 100644 --- a/org.simantics.pythonlink/src/org/simantics/pythonlink/Python.java +++ b/org.simantics.pythonlink/src/org/simantics/pythonlink/Python.java @@ -12,7 +12,7 @@ public class Python { } @SuppressWarnings( { "unchecked", "rawtypes" } ) - public static Object runPython(Function f) { + public static Object runPythonF(Function f) { SCLContext sclContext = SCLContext.getCurrent(); Object oldContext = sclContext.get(PYTHON_CONTEXT); try (PythonContext newContext = openPythonContext()) { @@ -25,7 +25,7 @@ public class Python { } @SuppressWarnings( { "unchecked", "rawtypes" } ) - public static Object runWithPythonContext(PythonContext context, Function f) { + public static Object runWithPythonContextF(PythonContext context, Function f) { SCLContext sclContext = SCLContext.getCurrent(); Object oldContext = sclContext.get(PYTHON_CONTEXT); try { diff --git a/org.simantics.pythonlink/src/org/simantics/pythonlink/PythonContext.java b/org.simantics.pythonlink/src/org/simantics/pythonlink/PythonContext.java index d237c83..5341cc2 100644 --- a/org.simantics.pythonlink/src/org/simantics/pythonlink/PythonContext.java +++ b/org.simantics.pythonlink/src/org/simantics/pythonlink/PythonContext.java @@ -6,19 +6,27 @@ public class PythonContext implements Closeable { private long contextID; PythonContext() { - contextID = openPythonContextImpl(); + contextID = createContextImpl(); } @Override public void close() { long id = contextID; contextID = 0; - closePythonContextImpl(id); + if (id != 0) deleteContextImpl(id); + } + + @Override + protected void finalize() throws Throwable { + super.finalize(); + close(); } public void executePythonStatement(String statement) { executePythonStatementImpl( contextID, statement ); } + + // Setters public void setPythonIntegerVariable(String variableName, int value) { setPythonIntegerVariableImpl(contextID, variableName, value); @@ -39,6 +47,8 @@ public class PythonContext implements Closeable { public void setPythonStringArrayVariable(String variableName, String[] value) { setPythonStringArrayVariableImpl(contextID, variableName, value); } + + // Getters public int getPythonIntegerVariable(String variableName) { return getPythonIntegerVariableImpl(contextID, variableName); @@ -68,10 +78,10 @@ public class PythonContext implements Closeable { } // Native function declarations - private static native long openPythonContextImpl(); - private static native void closePythonContextImpl(long contextID); + private static native long createContextImpl(); + private static native void deleteContextImpl(long contextID); - private static native void executePythonStatementImpl(long contextID, String statement); + private static native int executePythonStatementImpl(long contextID, String statement); private static native void setPythonIntegerVariableImpl(long contextID, String variableName, int value); private static native void setPythonDoubleVariableImpl(long contextID, String variableName, double value); diff --git a/org.simantics.pythonlink/test/org/simantics/pythonlink/test/ScriptTestBase.java b/org.simantics.pythonlink/test/org/simantics/pythonlink/test/ScriptTestBase.java index 6328fec..b6238d6 100644 --- a/org.simantics.pythonlink/test/org/simantics/pythonlink/test/ScriptTestBase.java +++ b/org.simantics.pythonlink/test/org/simantics/pythonlink/test/ScriptTestBase.java @@ -4,14 +4,9 @@ import java.io.BufferedReader; import java.io.InputStreamReader; import java.nio.charset.Charset; -import org.junit.Before; import org.simantics.scl.compiler.commands.CommandSession; import org.simantics.scl.compiler.commands.TestScriptExecutor; -import org.simantics.scl.compiler.module.repository.ModuleRepository; -import org.simantics.scl.compiler.source.repository.CompositeModuleSourceRepository; -import org.simantics.scl.compiler.source.repository.SourceRepositories; import org.simantics.scl.osgi.SCLOsgi; -import org.simantics.scl.osgi.internal.ServiceBasedModuleSourceRepository; public class ScriptTestBase { diff --git a/org.simantics.pythonlink/test/org/simantics/pythonlink/test/ScriptTests.java b/org.simantics.pythonlink/test/org/simantics/pythonlink/test/ScriptTests.java index 4c5318a..6a8bdae 100644 --- a/org.simantics.pythonlink/test/org/simantics/pythonlink/test/ScriptTests.java +++ b/org.simantics.pythonlink/test/org/simantics/pythonlink/test/ScriptTests.java @@ -8,9 +8,5 @@ public class ScriptTests extends ScriptTestBase { super("scripts"); } - @Test public void Arithmetic() throws Exception { test(); } - @Test public void Functions() throws Exception { test(); } - @Test public void Functions2() throws Exception { test(); } - @Test public void Lists() throws Exception { test(); } - + @Test public void Python() throws Exception { test(); } } diff --git a/org.simantics.pythonlink/test/org/simantics/pythonlink/test/scripts/Python.sts b/org.simantics.pythonlink/test/org/simantics/pythonlink/test/scripts/Python.sts index 409d9b2..7464d73 100644 --- a/org.simantics.pythonlink/test/org/simantics/pythonlink/test/scripts/Python.sts +++ b/org.simantics.pythonlink/test/org/simantics/pythonlink/test/scripts/Python.sts @@ -29,10 +29,10 @@ vector [1, 2, 3, 4, 10] > ndarray (vector [1, 2, 3]) ndarray(3) [1.0, 2.0, 3.0] > ndarrayM 2 2 (vector [1, 2, 3, 4]) -ndarray(2x2) [1.0, 2.0, 3.0, 4.0] +ndarray(2x2) [[1.0, 2.0], [3.0, 4.0]] > a = ndarrayN (vector [2, 2, 3]) (vector [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) > a -ndarray(2x2x3) [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, ...] +ndarray(2x2x3) [[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], [[7.0, 8.0, 9.0], [10.0, 11.0, 12.0]]] > ndarrayDims a vector [2, 2, 3] > ndarrayValues a @@ -45,4 +45,4 @@ vector [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0] > setPythonNDArrayVariable "foo" (ndarrayM 2 3 (vector [1, 2, 3, 4, 5, 6])) > executePythonStatement "bar = foo.cumsum(1)" > getPythonNDArrayVariable "bar" -ndarray(2x3) [1.0, 3.0, 6.0, 4.0, 9.0, 15.0] +ndarray(2x3) [[1.0, 3.0, 6.0], [4.0, 9.0, 15.0]] -- 2.47.1