package org.simantics.pythonlink; import java.io.Closeable; public class PythonContext implements Closeable { private long contextID; PythonContext() { contextID = createContextImpl(); } @Override public void close() { long id = contextID; contextID = 0; 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); } public void setPythonDoubleVariable(String variableName, double value) { setPythonDoubleVariableImpl(contextID, variableName, value); } public void setPythonStringVariable(String variableName, String value) { setPythonStringVariableImpl(contextID, variableName, value); } public void setPythonIntegerArrayVariable(String variableName, int[] value) { setPythonIntegerArrayVariableImpl(contextID, variableName, value); } public void setPythonDoubleArrayVariable(String variableName, double[] value) { setPythonDoubleArrayVariableImpl(contextID, variableName, value); } public void setPythonStringArrayVariable(String variableName, String[] value) { setPythonStringArrayVariableImpl(contextID, variableName, value); } // Getters public int getPythonIntegerVariable(String variableName) { return getPythonIntegerVariableImpl(contextID, variableName); } public double getPythonDoubleVariable(String variableName) { return getPythonDoubleVariableImpl(contextID, variableName); } public String getPythonStringVariable(String variableName) { return getPythonStringVariableImpl(contextID, variableName); } public int[] getPythonIntegerArrayVariable(String variableName) { return getPythonIntegerArrayVariableImpl(contextID, variableName); } public double[] getPythonDoubleArrayVariable(String variableName) { return getPythonDoubleArrayVariableImpl(contextID, variableName); } public String[] getPythonStringArrayVariable(String variableName) { return getPythonStringArrayVariableImpl(contextID, variableName); } public void setPythonNDArrayVariable(String variableName, NDArray value) { setPythonNDArrayVariableImpl(contextID, variableName, value); } public NDArray getPythonNDArrayVariable(String variableName) { return getPythonNDArrayVariableImpl(contextID, variableName); } // Native function declarations private static native long createContextImpl(); private static native void deleteContextImpl(long contextID); 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); private static native void setPythonStringVariableImpl(long contextID, String variableName, String value); private static native void setPythonIntegerArrayVariableImpl(long contextID, String variableName, int[] value); private static native void setPythonDoubleArrayVariableImpl(long contextID, String variableName, double[] value); private static native void setPythonStringArrayVariableImpl(long contextID, String variableName, String[] value); private static native int getPythonIntegerVariableImpl(long contextID, String variableName); private static native double getPythonDoubleVariableImpl(long contextID, String variableName); private static native String getPythonStringVariableImpl(long contextID, String variableName); private static native int[] getPythonIntegerArrayVariableImpl(long contextID, String variableName); private static native double[] getPythonDoubleArrayVariableImpl(long contextID, String variableName); private static native String[] getPythonStringArrayVariableImpl(long contextID, String variableName); private static native void setPythonNDArrayVariableImpl(long contextID, String variableName, NDArray value); private static native NDArray getPythonNDArrayVariableImpl(long contextID, String variableName); }