@JavaName close\r
closePythonContext :: PythonContext -> <Proc> ()\r
\r
- executePythonStatement :: String -> <Python, Proc> Integer\r
+ executePythonStatement :: String -> <Python, Proc> ()\r
\r
setPythonIntegerVariable :: String -> Integer -> <Python, Proc> ()\r
setPythonIntegerArrayVariable :: String -> Vector Integer -> <Python, Proc> ()\r
getPythonStringArrayVariable :: String -> <Python, Proc> Vector String\r
getPythonNDArrayVariable :: String -> <Python, Proc> NDArray\r
\r
- runPythonF :: (() -> <Python, Proc> a) -> <Proc> a\r
- runWithPythonContextF :: PythonContext -> (() -> <Python, Proc> a) -> <Proc> a\r
-\r
importJava "org.simantics.pythonlink.Python" where\r
openPythonContext :: () -> <Proc> PythonContext\r
\r
+ runPythonF :: (() -> <Python, Proc> a) -> <Proc> a\r
+ runWithPythonContextF :: PythonContext -> (() -> <Python, Proc> a) -> <Proc> a\r
+\r
runPython :: (<Python, Proc> a) -> <Proc> a\r
runPython v = runPythonF (\_ -> v)\r
\r
}\r
\r
@SuppressWarnings( { "unchecked", "rawtypes" } )\r
- public static Object runPython(Function f) {\r
+ public static Object runPythonF(Function f) {\r
SCLContext sclContext = SCLContext.getCurrent();\r
Object oldContext = sclContext.get(PYTHON_CONTEXT);\r
try (PythonContext newContext = openPythonContext()) {\r
}\r
\r
@SuppressWarnings( { "unchecked", "rawtypes" } )\r
- public static Object runWithPythonContext(PythonContext context, Function f) {\r
+ public static Object runWithPythonContextF(PythonContext context, Function f) {\r
SCLContext sclContext = SCLContext.getCurrent();\r
Object oldContext = sclContext.get(PYTHON_CONTEXT);\r
try {\r
private long contextID;\r
\r
PythonContext() {\r
- contextID = openPythonContextImpl();\r
+ contextID = createContextImpl();\r
}\r
\r
@Override\r
public void close() {\r
long id = contextID;\r
contextID = 0;\r
- closePythonContextImpl(id);\r
+ if (id != 0) deleteContextImpl(id);\r
+ }\r
+\r
+ @Override\r
+ protected void finalize() throws Throwable {\r
+ super.finalize();\r
+ close();\r
}\r
\r
public void executePythonStatement(String statement) {\r
executePythonStatementImpl( contextID, statement );\r
}\r
+\r
+ // Setters\r
\r
public void setPythonIntegerVariable(String variableName, int value) {\r
setPythonIntegerVariableImpl(contextID, variableName, value);\r
public void setPythonStringArrayVariable(String variableName, String[] value) {\r
setPythonStringArrayVariableImpl(contextID, variableName, value);\r
}\r
+\r
+ // Getters\r
\r
public int getPythonIntegerVariable(String variableName) {\r
return getPythonIntegerVariableImpl(contextID, variableName);\r
}\r
\r
// Native function declarations\r
- private static native long openPythonContextImpl();\r
- private static native void closePythonContextImpl(long contextID);\r
+ private static native long createContextImpl();\r
+ private static native void deleteContextImpl(long contextID);\r
\r
- private static native void executePythonStatementImpl(long contextID, String statement);\r
+ private static native int executePythonStatementImpl(long contextID, String statement);\r
\r
private static native void setPythonIntegerVariableImpl(long contextID, String variableName, int value);\r
private static native void setPythonDoubleVariableImpl(long contextID, String variableName, double value);\r
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 {
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(); }
}
> ndarray (vector [1, 2, 3])\r
ndarray(3) [1.0, 2.0, 3.0]\r
> ndarrayM 2 2 (vector [1, 2, 3, 4])\r
-ndarray(2x2) [1.0, 2.0, 3.0, 4.0]\r
+ndarray(2x2) [[1.0, 2.0], [3.0, 4.0]]\r
> a = ndarrayN (vector [2, 2, 3]) (vector [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])\r
> a\r
-ndarray(2x2x3) [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, ...]\r
+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]]]\r
> ndarrayDims a\r
vector [2, 2, 3]\r
> ndarrayValues a\r
> setPythonNDArrayVariable "foo" (ndarrayM 2 3 (vector [1, 2, 3, 4, 5, 6]))\r
> executePythonStatement "bar = foo.cumsum(1)"\r
> getPythonNDArrayVariable "bar"\r
-ndarray(2x3) [1.0, 3.0, 6.0, 4.0, 9.0, 15.0]\r
+ndarray(2x3) [[1.0, 3.0, 6.0], [4.0, 9.0, 15.0]]\r