├── .gitignore ├── LICENSE ├── README.md └── invesdwin-context-python-parent ├── .gitignore ├── invesdwin-context-python-runtime-contract ├── .gitignore ├── pom.xml └── src │ ├── main │ └── java │ │ ├── META-INF │ │ └── logback │ │ │ └── context-python-runtime-contract.logback.xml │ │ └── de │ │ └── invesdwin │ │ └── context │ │ └── python │ │ └── runtime │ │ └── contract │ │ ├── AScriptTaskInputsPythonToExpression.java │ │ ├── AScriptTaskPython.java │ │ ├── AScriptTaskResultsPythonFromJson.java │ │ ├── IScriptTaskInputsPython.java │ │ ├── IScriptTaskResultsPython.java │ │ ├── IScriptTaskRunnerPython.java │ │ ├── ProvidedScriptTaskRunnerPython.java │ │ ├── callback │ │ ├── AScriptTaskParametersPythonFromJson.java │ │ ├── AScriptTaskReturnsPythonToExpression.java │ │ ├── ScriptTaskParametersPythonFromJson.java │ │ ├── ScriptTaskParametersPythonFromJsonPool.java │ │ ├── ScriptTaskReturnsPythonToExpression.java │ │ ├── ScriptTaskReturnsPythonToExpressionPool.java │ │ └── socket │ │ │ ├── SocketScriptTaskCallbackContext.java │ │ │ ├── SocketScriptTaskCallbackContext.py │ │ │ ├── SocketScriptTaskCallbackServer.java │ │ │ ├── SocketScriptTaskCallbackServerHandler.java │ │ │ └── SocketScriptTaskCallbackServerPool.java │ │ └── internal │ │ └── ProvidedScriptTaskRunnerPythonStub.java │ └── test │ └── java │ ├── META-INF │ └── logback │ │ └── context-python-runtime-contract.logback-test.xml │ └── de │ └── invesdwin │ └── context │ └── python │ └── runtime │ └── contract │ ├── InputsAndResultsTestBoolean.java │ ├── InputsAndResultsTestBoolean.py │ ├── InputsAndResultsTestByte.java │ ├── InputsAndResultsTestByte.py │ ├── InputsAndResultsTestCharacter.java │ ├── InputsAndResultsTestCharacter.py │ ├── InputsAndResultsTestDecimal.java │ ├── InputsAndResultsTestDecimal.py │ ├── InputsAndResultsTestDouble.java │ ├── InputsAndResultsTestDouble.py │ ├── InputsAndResultsTestDoubleNan.java │ ├── InputsAndResultsTestDoubleNan.py │ ├── InputsAndResultsTestEmpty.java │ ├── InputsAndResultsTestEmpty.py │ ├── InputsAndResultsTestEmptyMatrixValue.java │ ├── InputsAndResultsTestEmptyMatrixValue.py │ ├── InputsAndResultsTestFloat.java │ ├── InputsAndResultsTestFloat.py │ ├── InputsAndResultsTestInteger.java │ ├── InputsAndResultsTestInteger.py │ ├── InputsAndResultsTestLong.java │ ├── InputsAndResultsTestLong.py │ ├── InputsAndResultsTestNull.java │ ├── InputsAndResultsTestNullPutGet.java │ ├── InputsAndResultsTestNullPutGet.py │ ├── InputsAndResultsTestPercent.java │ ├── InputsAndResultsTestPercent.py │ ├── InputsAndResultsTestShort.java │ ├── InputsAndResultsTestShort.py │ ├── InputsAndResultsTestString.java │ ├── InputsAndResultsTestString.py │ ├── InputsAndResultsTests.java │ ├── callback │ ├── ParametersAndReturnsTestBoolean.java │ ├── ParametersAndReturnsTestBoolean.py │ ├── ParametersAndReturnsTestByte.java │ ├── ParametersAndReturnsTestByte.py │ ├── ParametersAndReturnsTestCharacter.java │ ├── ParametersAndReturnsTestCharacter.py │ ├── ParametersAndReturnsTestDecimal.java │ ├── ParametersAndReturnsTestDecimal.py │ ├── ParametersAndReturnsTestDouble.java │ ├── ParametersAndReturnsTestDouble.py │ ├── ParametersAndReturnsTestDoubleNan.java │ ├── ParametersAndReturnsTestDoubleNan.py │ ├── ParametersAndReturnsTestEmpty.java │ ├── ParametersAndReturnsTestEmpty.py │ ├── ParametersAndReturnsTestEmptyMatrixValue.java │ ├── ParametersAndReturnsTestEmptyMatrixValue.py │ ├── ParametersAndReturnsTestFloat.java │ ├── ParametersAndReturnsTestFloat.py │ ├── ParametersAndReturnsTestInteger.java │ ├── ParametersAndReturnsTestInteger.py │ ├── ParametersAndReturnsTestLong.java │ ├── ParametersAndReturnsTestLong.py │ ├── ParametersAndReturnsTestNullPutGet.java │ ├── ParametersAndReturnsTestNullPutGet.py │ ├── ParametersAndReturnsTestPercent.java │ ├── ParametersAndReturnsTestPercent.py │ ├── ParametersAndReturnsTestShort.java │ ├── ParametersAndReturnsTestShort.py │ ├── ParametersAndReturnsTestString.java │ ├── ParametersAndReturnsTestString.py │ └── ParametersAndReturnsTests.java │ └── hello │ ├── HelloWorldScript.java │ └── HelloWorldScript.py ├── invesdwin-context-python-runtime-graalpy ├── .gitignore ├── pom.xml └── src │ ├── main │ └── java │ │ ├── META-INF │ │ ├── de.invesdwin.context.python.runtime.graalpy.properties │ │ └── services │ │ │ ├── de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython │ │ │ └── javax.script.ScriptEngineFactory │ │ └── de │ │ └── invesdwin │ │ └── context │ │ └── python │ │ └── runtime │ │ └── graalpy │ │ ├── GraalpyProperties.java │ │ ├── GraalpyScriptTaskCallbackContext.java │ │ ├── GraalpyScriptTaskCallbackContext.py │ │ ├── GraalpyScriptTaskEnginePython.java │ │ ├── GraalpyScriptTaskInputsPython.java │ │ ├── GraalpyScriptTaskResultsPython.java │ │ ├── GraalpyScriptTaskRunnerPython.java │ │ ├── jsr223 │ │ └── GraalpyScriptEngineFactory.java │ │ └── pool │ │ └── GraalpyScriptEngineObjectPool.java │ └── test │ └── java │ └── de │ └── invesdwin │ └── context │ └── python │ └── runtime │ └── graalpy │ ├── GraalpyScriptTaskRunnerPythonTest.java │ ├── SimpleCallbackTest.java │ └── SimpleCallbackTest.py ├── invesdwin-context-python-runtime-japyb ├── .gitignore ├── pom.xml └── src │ ├── main │ └── java │ │ ├── META-INF │ │ ├── de.invesdwin.context.python.runtime.japyb.properties │ │ └── services │ │ │ └── de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython │ │ └── de │ │ └── invesdwin │ │ └── context │ │ └── python │ │ └── runtime │ │ └── japyb │ │ ├── JapybProperties.java │ │ ├── JapybScriptTaskEnginePython.java │ │ ├── JapybScriptTaskInputsPython.java │ │ ├── JapybScriptTaskResultsPython.java │ │ ├── JapybScriptTaskRunnerPython.java │ │ └── pool │ │ ├── ExtendedPythonBridge.java │ │ ├── JapybObjectPool.java │ │ ├── ModifiedPythonBridge.java │ │ ├── ModifiedPythonErrorConsoleWatcher.java │ │ ├── PythonResetContext.java │ │ └── PythonResetContext.py │ └── test │ └── java │ └── de │ └── invesdwin │ └── context │ └── python │ └── runtime │ └── japyb │ ├── JapybScriptTaskRunnerPythonTest.java │ ├── SimpleCallbackTest.java │ └── SimpleCallbackTest.py ├── invesdwin-context-python-runtime-jep ├── .gitignore ├── pom.xml └── src │ ├── main │ └── java │ │ ├── META-INF │ │ ├── de.invesdwin.context.python.runtime.jep.properties │ │ └── services │ │ │ └── de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython │ │ └── de │ │ └── invesdwin │ │ └── context │ │ └── python │ │ └── runtime │ │ └── jep │ │ ├── JepProperties.java │ │ ├── JepScriptTaskCallbackContext.java │ │ ├── JepScriptTaskEnginePython.java │ │ ├── JepScriptTaskInputsPython.java │ │ ├── JepScriptTaskResultsPython.java │ │ ├── JepScriptTaskRunnerPython.java │ │ └── internal │ │ ├── JepWrapper.java │ │ └── JepWrapper.py │ └── test │ └── java │ └── de │ └── invesdwin │ └── context │ └── python │ └── runtime │ └── jep │ ├── JepScriptTaskRunnerPythonTest.java │ ├── SimpleCallbackTest.java │ └── SimpleCallbackTest.py ├── invesdwin-context-python-runtime-jython ├── .gitignore ├── pom.xml └── src │ ├── main │ └── java │ │ ├── META-INF │ │ └── services │ │ │ └── de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython │ │ └── de │ │ └── invesdwin │ │ └── context │ │ └── python │ │ └── runtime │ │ └── jython │ │ ├── JythonScriptTaskCallbackContext.java │ │ ├── JythonScriptTaskCallbackContext.py │ │ ├── JythonScriptTaskEnginePython.java │ │ ├── JythonScriptTaskInputsPython.java │ │ ├── JythonScriptTaskResultsPython.java │ │ ├── JythonScriptTaskRunnerPython.java │ │ └── pool │ │ └── PyScriptEngineObjectPool.java │ └── test │ └── java │ └── de │ └── invesdwin │ └── context │ └── python │ └── runtime │ └── jython │ ├── JythonScriptTaskRunnerPythonTest.java │ ├── SimpleCallbackTest.java │ └── SimpleCallbackTest.py ├── invesdwin-context-python-runtime-libpythonclj ├── .gitignore ├── pom.xml └── src │ ├── main │ └── java │ │ ├── META-INF │ │ ├── de.invesdwin.context.python.runtime.libpythonclj.properties │ │ └── services │ │ │ └── de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython │ │ └── de │ │ └── invesdwin │ │ └── context │ │ └── python │ │ └── runtime │ │ └── libpythonclj │ │ ├── LibpythoncljProperties.java │ │ ├── LibpythoncljScriptTaskEnginePython.java │ │ ├── LibpythoncljScriptTaskInputsPython.java │ │ ├── LibpythoncljScriptTaskResultsPython.java │ │ ├── LibpythoncljScriptTaskRunnerPython.java │ │ └── internal │ │ ├── GilLock.java │ │ ├── IPythonEngineWrapper.java │ │ ├── InitializingPythonEngineWrapper.java │ │ ├── UncheckedPythonEngineWrapper.java │ │ └── UncheckedPythonEngineWrapper.py │ └── test │ └── java │ └── de │ └── invesdwin │ └── context │ └── python │ └── runtime │ └── libpythonclj │ ├── LibpythoncljScriptTaskRunnerPythonTest.java │ ├── SimpleCallbackTest.java │ └── SimpleCallbackTest.py ├── invesdwin-context-python-runtime-py4j ├── .gitignore ├── pom.xml └── src │ ├── main │ └── java │ │ ├── META-INF │ │ ├── de.invesdwin.context.python.runtime.py4j.properties │ │ ├── logback │ │ │ └── context-python-runtime-py4j.logback.xml │ │ └── services │ │ │ └── de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython │ │ └── de │ │ └── invesdwin │ │ └── context │ │ └── python │ │ └── runtime │ │ └── py4j │ │ ├── Py4jProperties.java │ │ ├── Py4jScriptTaskCallbackContext.java │ │ ├── Py4jScriptTaskEnginePython.java │ │ ├── Py4jScriptTaskInputsPython.java │ │ ├── Py4jScriptTaskResultsPython.java │ │ ├── Py4jScriptTaskRunnerPython.java │ │ └── pool │ │ ├── Py4jInterpreterObjectPool.java │ │ └── internal │ │ ├── IPy4jInterpreter.java │ │ ├── Py4jInterpreter.java │ │ ├── Py4jInterpreter.py │ │ └── ReadyApplication.java │ └── test │ └── java │ └── de │ └── invesdwin │ └── context │ └── python │ └── runtime │ └── py4j │ ├── Py4jScriptTaskRunnerPythonTest.java │ ├── SimpleCallbackTest.java │ └── SimpleCallbackTest.py ├── invesdwin-context-python-runtime-python4j ├── .gitignore ├── pom.xml └── src │ ├── main │ └── java │ │ ├── META-INF │ │ └── services │ │ │ └── de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython │ │ └── de │ │ └── invesdwin │ │ └── context │ │ └── python │ │ └── runtime │ │ └── python4j │ │ ├── Python4jScriptTaskEnginePython.java │ │ ├── Python4jScriptTaskInputsPython.java │ │ ├── Python4jScriptTaskResultsPython.java │ │ ├── Python4jScriptTaskRunnerPython.java │ │ └── internal │ │ ├── GilLock.java │ │ ├── IPythonEngineWrapper.java │ │ ├── InitializingPythonEngineWrapper.java │ │ ├── ModifiedPythonTypes.java │ │ ├── NonePythonType.java │ │ ├── UncheckedPythonEngineWrapper.java │ │ └── UncheckedPythonEngineWrapper.py │ └── test │ └── java │ └── de │ └── invesdwin │ └── context │ └── python │ └── runtime │ └── python4j │ ├── Python4jScriptTaskRunnerPythonTest.java │ ├── SimpleCallbackTest.java │ └── SimpleCallbackTest.py └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | /.project 2 | **/.DS_Store 3 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/.gitignore: -------------------------------------------------------------------------------- 1 | /.checkstyle 2 | /.classpath 3 | /log 4 | /target 5 | /activemq-data 6 | /.fbprefs 7 | /cache 8 | /.settings 9 | /.project 10 | /*.hprof 11 | /.jrctemp 12 | /.invesdwin 13 | /nbproject 14 | /bin 15 | /.springBeans 16 | /.factorypath 17 | /.attach_pid* 18 | /**/.DS_Store 19 | /.idea 20 | /**/*.iml 21 | /.sessions 22 | /**/.~lock.* 23 | /.pydevproject 24 | /Report_*.java 25 | /octave-workspace 26 | /hs_err_pid*.log 27 | /*.Rout 28 | /.apt* 29 | /ajcore.*.txt 30 | /.temp-*.txt 31 | /dependency-reduced-pom.xml 32 | /META-INF 33 | /.Rproj.user 34 | /.Rhistory 35 | /.RData 36 | /.Ruserdata 37 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/.gitignore: -------------------------------------------------------------------------------- 1 | /.checkstyle 2 | /.classpath 3 | /log 4 | /target 5 | /activemq-data 6 | /.fbprefs 7 | /cache 8 | /.settings 9 | /.project 10 | /*.hprof 11 | /.jrctemp 12 | /.invesdwin 13 | /nbproject 14 | /bin 15 | /.springBeans 16 | /.factorypath 17 | /.attach_pid* 18 | /**/.DS_Store 19 | /.idea 20 | /**/*.iml 21 | /.sessions 22 | /**/.~lock.* 23 | /.pydevproject 24 | /Report_*.java 25 | /octave-workspace 26 | /hs_err_pid*.log 27 | /*.Rout 28 | /.apt* 29 | /ajcore.*.txt 30 | /.temp-*.txt 31 | /dependency-reduced-pom.xml 32 | /META-INF 33 | /.Rproj.user 34 | /.Rhistory 35 | /.RData 36 | /.Ruserdata 37 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | de.invesdwin 5 | invesdwin-context-python-parent 6 | 1.0.4-SNAPSHOT 7 | 8 | invesdwin-context-python-runtime-contract 9 | jar 10 | 11 | 12 | 13 | de.invesdwin 14 | invesdwin-context-integration 15 | 16 | 17 | de.invesdwin 18 | invesdwin-context-integration-channel 19 | provided 20 | 21 | 22 | io.netty 23 | netty-handler 24 | provided 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/main/java/META-INF/logback/context-python-runtime-contract.logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/main/java/de/invesdwin/context/python/runtime/contract/AScriptTaskPython.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.contract; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import de.invesdwin.context.integration.script.AScriptTask; 6 | 7 | @NotThreadSafe 8 | public abstract class AScriptTaskPython extends AScriptTask { 9 | 10 | @Override 11 | public V run(final IScriptTaskRunnerPython runner) { 12 | return runner.run(this); 13 | } 14 | 15 | @Override 16 | public V run() { 17 | return run(ProvidedScriptTaskRunnerPython.INSTANCE); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/main/java/de/invesdwin/context/python/runtime/contract/AScriptTaskResultsPythonFromJson.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.contract; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import com.fasterxml.jackson.databind.JsonNode; 6 | 7 | import de.invesdwin.context.integration.script.AScriptTaskResultsFromString; 8 | import de.invesdwin.util.lang.string.Strings; 9 | 10 | @NotThreadSafe 11 | public abstract class AScriptTaskResultsPythonFromJson extends AScriptTaskResultsFromString 12 | implements IScriptTaskResultsPython { 13 | 14 | protected abstract JsonNode getAsJsonNode(String variable); 15 | 16 | @Override 17 | public String getString(final String variable) { 18 | final JsonNode node = getAsJsonNode(variable); 19 | if (node == null) { 20 | return null; 21 | } 22 | final String str = node.asText(); 23 | if (Strings.isBlankOrNullText(str)) { 24 | return null; 25 | } else { 26 | return str; 27 | } 28 | } 29 | 30 | @Override 31 | public String[] getStringVector(final String variable) { 32 | JsonNode strs = getAsJsonNode(variable); 33 | if (strs == null) { 34 | return null; 35 | } 36 | //unwrap array 37 | while (strs.size() == 1 && strs.get(0).size() > 1) { 38 | strs = strs.get(0); 39 | } 40 | final String[] values = new String[strs.size()]; 41 | for (int i = 0; i < values.length; i++) { 42 | final String str = strs.get(i).asText(); 43 | if (Strings.isBlankOrNullText(str)) { 44 | values[i] = null; 45 | } else { 46 | values[i] = str; 47 | } 48 | } 49 | return values; 50 | } 51 | 52 | @Override 53 | public String[][] getStringMatrix(final String variable) { 54 | final JsonNode strsMatrix = getAsJsonNode(variable); 55 | if (strsMatrix == null) { 56 | return null; 57 | } 58 | if (strsMatrix.size() == 0) { 59 | final String[][] emptyMatrix = new String[0][]; 60 | return emptyMatrix; 61 | } 62 | final int rows = strsMatrix.size(); 63 | final int columns = strsMatrix.get(0).size(); 64 | final String[][] valuesMatrix = new String[rows][]; 65 | for (int r = 0; r < rows; r++) { 66 | final String[] values = new String[columns]; 67 | valuesMatrix[r] = values; 68 | final JsonNode nodeRow = strsMatrix.get(r); 69 | for (int c = 0; c < columns; c++) { 70 | final String str = nodeRow.get(c).asText(); 71 | if (Strings.isBlankOrNullText(str)) { 72 | values[c] = null; 73 | } else { 74 | values[c] = str; 75 | } 76 | } 77 | } 78 | return valuesMatrix; 79 | } 80 | 81 | } -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/main/java/de/invesdwin/context/python/runtime/contract/IScriptTaskInputsPython.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.contract; 2 | 3 | import de.invesdwin.context.integration.script.IScriptTaskInputs; 4 | 5 | public interface IScriptTaskInputsPython extends IScriptTaskInputs { 6 | 7 | @Override 8 | default void putExpression(final String variable, final String expression) { 9 | getEngine().eval(variable + " = " + expression); 10 | } 11 | 12 | @Override 13 | default void putNull(final String variable) { 14 | putExpression(variable, "None"); 15 | } 16 | 17 | @Override 18 | default void remove(final String variable) { 19 | getEngine().eval("del " + variable); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/main/java/de/invesdwin/context/python/runtime/contract/IScriptTaskResultsPython.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.contract; 2 | 3 | import de.invesdwin.context.integration.script.IScriptTaskResults; 4 | 5 | public interface IScriptTaskResultsPython extends IScriptTaskResults { 6 | 7 | @Override 8 | default boolean isDefined(final String variable) { 9 | return getBoolean("'" + variable + "' in locals() or '" + variable + "' in globals()"); 10 | } 11 | 12 | @Override 13 | default boolean isNull(final String variable) { 14 | return getBoolean(variable + " is None"); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/main/java/de/invesdwin/context/python/runtime/contract/IScriptTaskRunnerPython.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.contract; 2 | 3 | import de.invesdwin.context.log.Log; 4 | 5 | public interface IScriptTaskRunnerPython { 6 | 7 | String CLEANUP_SCRIPT = "for var in list(globals()):\n" // 8 | + " if var[0] == '_': continue\n" // 9 | + " del globals()[var]\n"; 10 | 11 | Log LOG = new Log(IScriptTaskRunnerPython.class); 12 | 13 | T run(AScriptTaskPython scriptTask); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/main/java/de/invesdwin/context/python/runtime/contract/callback/AScriptTaskParametersPythonFromJson.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.contract.callback; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import com.fasterxml.jackson.databind.JsonNode; 6 | import com.fasterxml.jackson.databind.node.NullNode; 7 | 8 | import de.invesdwin.context.integration.script.callback.AScriptTaskParametersFromString; 9 | import de.invesdwin.util.lang.string.Strings; 10 | 11 | @NotThreadSafe 12 | public abstract class AScriptTaskParametersPythonFromJson extends AScriptTaskParametersFromString { 13 | 14 | protected abstract JsonNode getAsJsonNode(int index); 15 | 16 | @Override 17 | public boolean isNull(final int index) { 18 | final JsonNode node = getAsJsonNode(index); 19 | return node == null || node instanceof NullNode; 20 | } 21 | 22 | @Override 23 | public String getString(final int index) { 24 | final JsonNode node = getAsJsonNode(index); 25 | if (node == null) { 26 | return null; 27 | } 28 | final String str = node.asText(); 29 | if (Strings.isBlankOrNullText(str)) { 30 | return null; 31 | } else { 32 | return str; 33 | } 34 | } 35 | 36 | @Override 37 | public String[] getStringVector(final int index) { 38 | JsonNode strs = getAsJsonNode(index); 39 | if (strs == null) { 40 | return null; 41 | } 42 | //unwrap array 43 | while (strs.size() == 1 && strs.get(0).size() > 1) { 44 | strs = strs.get(0); 45 | } 46 | final String[] values = new String[strs.size()]; 47 | for (int i = 0; i < values.length; i++) { 48 | final String str = strs.get(i).asText(); 49 | if (Strings.isBlankOrNullText(str)) { 50 | values[i] = null; 51 | } else { 52 | values[i] = str; 53 | } 54 | } 55 | return values; 56 | } 57 | 58 | @Override 59 | public String[][] getStringMatrix(final int index) { 60 | final JsonNode strsMatrix = getAsJsonNode(index); 61 | if (strsMatrix == null) { 62 | return null; 63 | } 64 | if (strsMatrix.size() == 0) { 65 | final String[][] emptyMatrix = new String[0][]; 66 | return emptyMatrix; 67 | } 68 | final int rows = strsMatrix.size(); 69 | final int columns = strsMatrix.get(0).size(); 70 | final String[][] valuesMatrix = new String[rows][]; 71 | for (int r = 0; r < rows; r++) { 72 | final String[] values = new String[columns]; 73 | valuesMatrix[r] = values; 74 | final JsonNode nodeRow = strsMatrix.get(r); 75 | for (int c = 0; c < columns; c++) { 76 | final String str = nodeRow.get(c).asText(); 77 | if (Strings.isBlankOrNullText(str)) { 78 | values[c] = null; 79 | } else { 80 | values[c] = str; 81 | } 82 | } 83 | } 84 | return valuesMatrix; 85 | } 86 | 87 | } -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/main/java/de/invesdwin/context/python/runtime/contract/callback/ScriptTaskParametersPythonFromJson.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.contract.callback; 2 | 3 | import java.io.Closeable; 4 | 5 | import javax.annotation.concurrent.NotThreadSafe; 6 | 7 | import com.fasterxml.jackson.databind.JsonNode; 8 | 9 | import de.invesdwin.util.lang.string.Strings; 10 | 11 | @NotThreadSafe 12 | public class ScriptTaskParametersPythonFromJson extends AScriptTaskParametersPythonFromJson implements Closeable { 13 | 14 | private JsonNode parameters; 15 | 16 | public void setParameters(final JsonNode parameters) { 17 | this.parameters = parameters; 18 | } 19 | 20 | @Override 21 | public int size() { 22 | return parameters.size(); 23 | } 24 | 25 | @Override 26 | protected JsonNode getAsJsonNode(final int index) { 27 | return parameters.get(index); 28 | } 29 | 30 | @Override 31 | public void close() { 32 | parameters = null; 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return Strings.asString(parameters); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/main/java/de/invesdwin/context/python/runtime/contract/callback/ScriptTaskParametersPythonFromJsonPool.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.contract.callback; 2 | 3 | import javax.annotation.concurrent.ThreadSafe; 4 | 5 | import de.invesdwin.util.concurrent.pool.AAgronaObjectPool; 6 | 7 | @ThreadSafe 8 | public final class ScriptTaskParametersPythonFromJsonPool 9 | extends AAgronaObjectPool { 10 | 11 | public static final ScriptTaskParametersPythonFromJsonPool INSTANCE = new ScriptTaskParametersPythonFromJsonPool(); 12 | 13 | private ScriptTaskParametersPythonFromJsonPool() {} 14 | 15 | @Override 16 | protected ScriptTaskParametersPythonFromJson newObject() { 17 | return new ScriptTaskParametersPythonFromJson(); 18 | } 19 | 20 | @Override 21 | protected boolean passivateObject(final ScriptTaskParametersPythonFromJson element) { 22 | element.close(); 23 | return true; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/main/java/de/invesdwin/context/python/runtime/contract/callback/ScriptTaskReturnsPythonToExpression.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.contract.callback; 2 | 3 | import java.io.Closeable; 4 | 5 | import javax.annotation.concurrent.NotThreadSafe; 6 | 7 | @NotThreadSafe 8 | public class ScriptTaskReturnsPythonToExpression extends AScriptTaskReturnsPythonToExpression implements Closeable { 9 | 10 | private String returnExpression; 11 | 12 | public String getReturnExpression() { 13 | return returnExpression; 14 | } 15 | 16 | @Override 17 | public void returnExpression(final String expression) { 18 | assert returnExpression == null; 19 | this.returnExpression = expression; 20 | } 21 | 22 | @Override 23 | public void close() { 24 | returnExpression = null; 25 | } 26 | 27 | @Override 28 | public String toString() { 29 | return returnExpression; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/main/java/de/invesdwin/context/python/runtime/contract/callback/ScriptTaskReturnsPythonToExpressionPool.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.contract.callback; 2 | 3 | import javax.annotation.concurrent.ThreadSafe; 4 | 5 | import de.invesdwin.util.concurrent.pool.AAgronaObjectPool; 6 | 7 | @ThreadSafe 8 | public final class ScriptTaskReturnsPythonToExpressionPool 9 | extends AAgronaObjectPool { 10 | 11 | public static final ScriptTaskReturnsPythonToExpressionPool INSTANCE = new ScriptTaskReturnsPythonToExpressionPool(); 12 | 13 | private ScriptTaskReturnsPythonToExpressionPool() {} 14 | 15 | @Override 16 | protected ScriptTaskReturnsPythonToExpression newObject() { 17 | return new ScriptTaskReturnsPythonToExpression(); 18 | } 19 | 20 | @Override 21 | protected boolean passivateObject(final ScriptTaskReturnsPythonToExpression element) { 22 | element.close(); 23 | return true; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/main/java/de/invesdwin/context/python/runtime/contract/callback/socket/SocketScriptTaskCallbackServer.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.contract.callback.socket; 2 | 3 | import java.io.IOException; 4 | import java.net.InetSocketAddress; 5 | 6 | import javax.annotation.concurrent.NotThreadSafe; 7 | 8 | import de.invesdwin.context.integration.channel.async.AsynchronousHandlerFactorySupport; 9 | import de.invesdwin.context.integration.channel.async.IAsynchronousHandler; 10 | import de.invesdwin.context.integration.channel.async.netty.tcp.StringNettySocketAsynchronousChannel; 11 | import de.invesdwin.context.integration.channel.sync.ISynchronousChannel; 12 | import de.invesdwin.context.integration.channel.sync.netty.tcp.NettySocketSynchronousChannel; 13 | import de.invesdwin.context.integration.channel.sync.netty.tcp.type.NioNettySocketChannelType; 14 | import de.invesdwin.context.integration.compression.lz4.LZ4Streams; 15 | import de.invesdwin.context.integration.network.NetworkUtil; 16 | import de.invesdwin.util.assertions.Assertions; 17 | 18 | @NotThreadSafe 19 | public class SocketScriptTaskCallbackServer implements ISynchronousChannel { 20 | 21 | private StringNettySocketAsynchronousChannel server; 22 | private final String host; 23 | private final int configuredPort; 24 | private int port; 25 | 26 | public SocketScriptTaskCallbackServer(final String host, final int port) { 27 | this.host = host; 28 | this.configuredPort = port; 29 | this.port = port; 30 | } 31 | 32 | @Override 33 | public void open() { 34 | Assertions.checkNull(server); 35 | if (configuredPort <= 0) { 36 | port = NetworkUtil.findAvailableTcpPort(); 37 | } else { 38 | port = configuredPort; 39 | } 40 | final NettySocketSynchronousChannel serverChannel = new NettySocketSynchronousChannel( 41 | NioNettySocketChannelType.INSTANCE, new InetSocketAddress(host, port), true, 42 | LZ4Streams.DEFAULT_BLOCK_SIZE_BYTES); 43 | server = new StringNettySocketAsynchronousChannel(serverChannel, 44 | new AsynchronousHandlerFactorySupport() { 45 | @Override 46 | public IAsynchronousHandler newHandler() { 47 | return new SocketScriptTaskCallbackServerHandler(); 48 | } 49 | }, true); 50 | try { 51 | server.open(); 52 | } catch (final IOException e) { 53 | throw new RuntimeException(e); 54 | } 55 | } 56 | 57 | public String getHost() { 58 | return host; 59 | } 60 | 61 | public int getPort() { 62 | return port; 63 | } 64 | 65 | @Override 66 | public void close() { 67 | if (server != null) { 68 | server.close(); 69 | server = null; 70 | port = configuredPort; 71 | } 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/main/java/de/invesdwin/context/python/runtime/contract/callback/socket/SocketScriptTaskCallbackServerHandler.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.contract.callback.socket; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.annotation.concurrent.NotThreadSafe; 6 | 7 | import de.invesdwin.context.integration.channel.async.IAsynchronousHandler; 8 | import de.invesdwin.context.integration.channel.async.IAsynchronousHandlerContext; 9 | import de.invesdwin.util.concurrent.Executors; 10 | import de.invesdwin.util.concurrent.WrappedExecutorService; 11 | import de.invesdwin.util.lang.string.Strings; 12 | 13 | @NotThreadSafe 14 | public class SocketScriptTaskCallbackServerHandler implements IAsynchronousHandler { 15 | 16 | private static final WrappedExecutorService EXECUTOR = Executors 17 | .newCachedThreadPool(SocketScriptTaskCallbackServerHandler.class.getSimpleName()); 18 | private SocketScriptTaskCallbackContext callbackContext; 19 | 20 | @Override 21 | public String open(final IAsynchronousHandlerContext context) throws IOException { 22 | return null; 23 | } 24 | 25 | @Override 26 | public String idle(final IAsynchronousHandlerContext context) throws IOException { 27 | return null; 28 | } 29 | 30 | @Override 31 | public String handle(final IAsynchronousHandlerContext context, final String input) throws IOException { 32 | if (callbackContext == null) { 33 | callbackContext = SocketScriptTaskCallbackContext.getContext(input); 34 | if (callbackContext == null) { 35 | throw new IllegalArgumentException( 36 | SocketScriptTaskCallbackContext.class.getSimpleName() + " not found for uuid: " + input); 37 | } 38 | return null; 39 | } 40 | final int indexOfSeparator = Strings.indexOf(input, ";"); 41 | if (indexOfSeparator <= 0) { 42 | throw new IllegalArgumentException("requiring request format [;]: " + input); 43 | } 44 | final String methodName = input.substring(0, indexOfSeparator); 45 | final String args = input.substring(indexOfSeparator + 1, input.length()); 46 | //use executor to prevent blocking requests in a shared netty handler thread 47 | EXECUTOR.execute(new Runnable() { 48 | @Override 49 | public void run() { 50 | final String result = callbackContext.invoke(methodName, args); 51 | final String resultReplaced = Strings.replace(result, "\n", "__##P@NL@C##__"); 52 | context.write(resultReplaced); 53 | } 54 | }); 55 | return null; 56 | } 57 | 58 | @Override 59 | public void outputFinished(final IAsynchronousHandlerContext context) throws IOException {} 60 | 61 | @Override 62 | public void close() throws IOException {} 63 | 64 | } 65 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/main/java/de/invesdwin/context/python/runtime/contract/callback/socket/SocketScriptTaskCallbackServerPool.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.contract.callback.socket; 2 | 3 | import javax.annotation.concurrent.Immutable; 4 | 5 | import de.invesdwin.util.concurrent.pool.timeout.ASingletonTimeoutObjectPool; 6 | import de.invesdwin.util.lang.Closeables; 7 | import de.invesdwin.util.time.date.FTimeUnit; 8 | import de.invesdwin.util.time.duration.Duration; 9 | 10 | @Immutable 11 | public final class SocketScriptTaskCallbackServerPool 12 | extends ASingletonTimeoutObjectPool { 13 | 14 | public static final SocketScriptTaskCallbackServerPool INSTANCE = new SocketScriptTaskCallbackServerPool(); 15 | 16 | private SocketScriptTaskCallbackServerPool() { 17 | super(Duration.ONE_MINUTE, new Duration(10, FTimeUnit.SECONDS)); 18 | } 19 | 20 | @Override 21 | protected SocketScriptTaskCallbackServer newObject() { 22 | final SocketScriptTaskCallbackServer server = new SocketScriptTaskCallbackServer("localhost", 0); 23 | server.open(); 24 | return server; 25 | } 26 | 27 | @Override 28 | protected boolean passivateObject(final SocketScriptTaskCallbackServer element) { 29 | return true; 30 | } 31 | 32 | @Override 33 | public void invalidateObject(final SocketScriptTaskCallbackServer element) { 34 | Closeables.closeQuietly(element); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/main/java/de/invesdwin/context/python/runtime/contract/internal/ProvidedScriptTaskRunnerPythonStub.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.contract.internal; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | import jakarta.inject.Named; 5 | 6 | import de.invesdwin.context.python.runtime.contract.ProvidedScriptTaskRunnerPython; 7 | import de.invesdwin.context.test.ATest; 8 | import de.invesdwin.context.test.stub.StubSupport; 9 | 10 | @Named 11 | @NotThreadSafe 12 | public class ProvidedScriptTaskRunnerPythonStub extends StubSupport { 13 | 14 | @Override 15 | public void tearDownOnce(final ATest test) throws Exception { 16 | ProvidedScriptTaskRunnerPython.setProvidedInstance(null); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/META-INF/logback/context-python-runtime-contract.logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/InputsAndResultsTestBoolean.py: -------------------------------------------------------------------------------- 1 | print("getBoolean") 2 | if 'getBoolean' in locals(): 3 | raise Exception("getBoolean already defined!") 4 | getBoolean = putBoolean 5 | getBooleanType = type(getBoolean) 6 | print(getBooleanType) 7 | print(getBoolean) 8 | if(getBooleanType is not bool): 9 | raise Exception("getBoolean not bool!") 10 | 11 | print("getBooleanVector") 12 | if 'getBooleanVector' in locals(): 13 | raise Exception("getBooleanVector already defined!") 14 | getBooleanVector = putBooleanVector 15 | getBooleanVectorType = type(getBooleanVector[0]) 16 | print(getBooleanVectorType) 17 | print(getBooleanVector) 18 | if(getBooleanVectorType is not bool): 19 | raise Exception("getBooleanVector not bool!") 20 | 21 | print("getBooleanVectorAsList") 22 | if 'getBooleanVectorAsList' in locals(): 23 | raise Exception("getBooleanVectorAsList already defined!") 24 | getBooleanVectorAsList = putBooleanVectorAsList 25 | getBooleanVectorAsListType = type(getBooleanVectorAsList[0]) 26 | print(getBooleanVectorAsListType) 27 | print(getBooleanVectorAsList) 28 | if(getBooleanVectorAsListType is not bool): 29 | raise Exception("getBooleanVectorAsList not bool!") 30 | 31 | print("getBooleanMatrix") 32 | if 'getBooleanMatrix' in locals(): 33 | raise Exception("getBooleanMatrix already defined!") 34 | getBooleanMatrix = putBooleanMatrix 35 | getBooleanMatrixType = type(getBooleanMatrix[0][0]) 36 | print(getBooleanMatrixType) 37 | print(getBooleanMatrix) 38 | if(getBooleanMatrixType is not bool): 39 | raise Exception("getBooleanMatrix not bool!") 40 | 41 | print("getBooleanMatrixAsList") 42 | if 'getBooleanMatrixAsList' in locals(): 43 | raise Exception("getBooleanMatrixAsList already defined!") 44 | getBooleanMatrixAsList = putBooleanMatrixAsList 45 | getBooleanMatrixAsListType = type(getBooleanMatrixAsList[0][0]) 46 | print(getBooleanMatrixAsListType) 47 | print(getBooleanMatrixAsList) 48 | if(getBooleanMatrixAsListType is not bool): 49 | raise Exception("getBooleanMatrixAsList not bool!") 50 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/InputsAndResultsTestByte.py: -------------------------------------------------------------------------------- 1 | print("getByte") 2 | if 'getByte' in locals(): 3 | raise Exception("getByte already defined!") 4 | getByte = putByte 5 | getByteType = type(getByte) 6 | print(getByteType) 7 | print(getByte) 8 | if(getByteType is not int): 9 | raise Exception("getByte not int!") 10 | 11 | print("getByteVector") 12 | if 'getByteVector' in locals(): 13 | raise Exception("getByteVector already defined!") 14 | getByteVector = putByteVector 15 | getByteVectorType = type(getByteVector[0]) 16 | print(getByteVectorType) 17 | print(getByteVector) 18 | if(getByteVectorType is not int): 19 | raise Exception("getByteVector not int!") 20 | 21 | print("getByteVectorAsList") 22 | if 'getByteVectorAsList' in locals(): 23 | raise Exception("getByteVectorAsList already defined!") 24 | getByteVectorAsList = putByteVectorAsList 25 | getByteVectorAsListType = type(getByteVectorAsList[0]) 26 | print(getByteVectorAsListType) 27 | print(getByteVectorAsList) 28 | if(getByteVectorAsListType is not int): 29 | raise Exception("getByteVectorAsList not int!") 30 | 31 | print("getByteMatrix") 32 | if 'getByteMatrix' in locals(): 33 | raise Exception("getByteMatrix already defined!") 34 | getByteMatrix = putByteMatrix 35 | getByteMatrixType = type(getByteMatrix[0][0]) 36 | print(getByteMatrixType) 37 | print(getByteMatrix) 38 | if(getByteMatrixType is not int): 39 | raise Exception("getByteMatrix not int!") 40 | 41 | print("getByteMatrixAsList") 42 | if 'getByteMatrixAsList' in locals(): 43 | raise Exception("getByteMatrixAsList already defined!") 44 | getByteMatrixAsList = putByteMatrixAsList 45 | getByteMatrixAsListType = type(getByteMatrixAsList[0][0]) 46 | print(getByteMatrixAsListType) 47 | print(getByteMatrixAsList) 48 | if(getByteMatrixAsListType is not int): 49 | raise Exception("getByteMatrixAsList not int!") 50 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/InputsAndResultsTestCharacter.py: -------------------------------------------------------------------------------- 1 | print("getCharacter") 2 | if 'getCharacter' in locals(): 3 | raise Exception("getCharacter already defined!") 4 | getCharacter = putCharacter 5 | getCharacterType = type(getCharacter) 6 | print(getCharacterType) 7 | print(getCharacter) 8 | if(not isinstance(getCharacter, (unicode, str))): 9 | raise Exception("getCharacter not unicode or str!") 10 | 11 | print("getCharacterVector") 12 | if 'getCharacterVector' in locals(): 13 | raise Exception("getCharacterVector already defined!") 14 | getCharacterVector = putCharacterVector 15 | getCharacterVectorType = type(getCharacterVector[0]) 16 | print(getCharacterVectorType) 17 | print(getCharacterVector) 18 | if(not isinstance(getCharacterVector[0], (unicode, str))): 19 | raise Exception("getCharacterVector not unicode or str!") 20 | 21 | print("getCharacterVectorAsList") 22 | if 'getCharacterVectorAsList' in locals(): 23 | raise Exception("getCharacterVectorAsList already defined!") 24 | getCharacterVectorAsList = putCharacterVectorAsList 25 | getCharacterVectorAsListType = type(getCharacterVectorAsList[0]) 26 | print(getCharacterVectorAsListType) 27 | print(getCharacterVectorAsList) 28 | if(not isinstance(getCharacterVectorAsList[0], (unicode, str))): 29 | raise Exception("getCharacterVectorAsList not unicode or str!") 30 | 31 | print("getCharacterMatrix") 32 | if 'getCharacterMatrix' in locals(): 33 | raise Exception("getCharacterMatrix already defined!") 34 | getCharacterMatrix = putCharacterMatrix 35 | getCharacterMatrixType = type(getCharacterMatrix[0][0]) 36 | print(getCharacterMatrixType) 37 | print(getCharacterMatrix) 38 | if(not isinstance(getCharacterMatrix[0][0], (unicode, str))): 39 | raise Exception("getCharacterMatrix not unicode or str!") 40 | 41 | print("getCharacterMatrixAsList") 42 | if 'getCharacterMatrixAsList' in locals(): 43 | raise Exception("getCharacterMatrixAsList already defined!") 44 | getCharacterMatrixAsList = putCharacterMatrixAsList 45 | getCharacterMatrixAsListType = type(getCharacterMatrixAsList[0][0]) 46 | print(getCharacterMatrixAsListType) 47 | print(getCharacterMatrixAsList) 48 | if(not isinstance(getCharacterMatrixAsList[0][0], (unicode, str))): 49 | raise Exception("getCharacterMatrixAsList not unicode or str!") -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/InputsAndResultsTestDecimal.py: -------------------------------------------------------------------------------- 1 | print("getDecimal") 2 | if 'getDecimal' in locals(): 3 | raise Exception("getDecimal already defined!") 4 | getDecimal = putDecimal 5 | getDecimalType = type(getDecimal) 6 | print(getDecimalType) 7 | print(getDecimal) 8 | if(getDecimalType is not float): 9 | raise Exception("getDecimal not float!") 10 | 11 | print("getDecimalVector") 12 | if 'getDecimalVector' in locals(): 13 | raise Exception("getDecimalVector already defined!") 14 | getDecimalVector = putDecimalVector 15 | getDecimalVectorType = type(getDecimalVector[0]) 16 | print(getDecimalVectorType) 17 | print(getDecimalVector) 18 | if(getDecimalVectorType is not float): 19 | raise Exception("getDecimalVector not float!") 20 | 21 | print("getDecimalVectorAsList") 22 | if 'getDecimalVectorAsList' in locals(): 23 | raise Exception("getDecimalVectorAsList already defined!") 24 | getDecimalVectorAsList = putDecimalVectorAsList 25 | getDecimalVectorAsListType = type(getDecimalVectorAsList[0]) 26 | print(getDecimalVectorAsListType) 27 | print(getDecimalVectorAsList) 28 | if(getDecimalVectorAsListType is not float): 29 | raise Exception("getDecimalVectorAsList not float!") 30 | 31 | print("getDecimalMatrix") 32 | if 'getDecimalMatrix' in locals(): 33 | raise Exception("getDecimalMatrix already defined!") 34 | getDecimalMatrix = putDecimalMatrix 35 | getDecimalMatrixType = type(getDecimalMatrix[0][0]) 36 | print(getDecimalMatrixType) 37 | print(getDecimalMatrix) 38 | if(getDecimalMatrixType is not float): 39 | raise Exception("getDecimalMatrix not float!") 40 | 41 | print("getDecimalMatrixAsList") 42 | if 'getDecimalMatrixAsList' in locals(): 43 | raise Exception("getDecimalMatrixAsList already defined!") 44 | getDecimalMatrixAsList = putDecimalMatrixAsList 45 | getDecimalMatrixAsListType = type(getDecimalMatrixAsList[0][0]) 46 | print(getDecimalMatrixAsListType) 47 | print(getDecimalMatrixAsList) 48 | if(getDecimalMatrixAsListType is not float): 49 | raise Exception("getDecimalMatrixAsList not float!") 50 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/InputsAndResultsTestDouble.py: -------------------------------------------------------------------------------- 1 | print("getDouble") 2 | if 'getDouble' in locals(): 3 | raise Exception("getDouble already defined!") 4 | getDouble = putDouble 5 | getDoubleType = type(getDouble) 6 | print(getDoubleType) 7 | print(getDouble) 8 | if(getDoubleType is not float): 9 | raise Exception("getDouble not float!") 10 | 11 | print("getDoubleVector") 12 | if 'getDoubleVector' in locals(): 13 | raise Exception("getDoubleVector already defined!") 14 | getDoubleVector = putDoubleVector 15 | getDoubleVectorType = type(getDoubleVector[0]) 16 | print(getDoubleVectorType) 17 | print(getDoubleVector) 18 | if(getDoubleVectorType is not float): 19 | raise Exception("getDoubleVector not float!") 20 | 21 | print("getDoubleVectorAsList") 22 | if 'getDoubleVectorAsList' in locals(): 23 | raise Exception("getDoubleVectorAsList already defined!") 24 | getDoubleVectorAsList = putDoubleVectorAsList 25 | getDoubleVectorAsListType = type(getDoubleVectorAsList[0]) 26 | print(getDoubleVectorAsListType) 27 | print(getDoubleVectorAsList) 28 | if(getDoubleVectorAsListType is not float): 29 | raise Exception("getDoubleVectorAsList not float!") 30 | 31 | print("getDoubleMatrix") 32 | if 'getDoubleMatrix' in locals(): 33 | raise Exception("getDoubleMatrix already defined!") 34 | getDoubleMatrix = putDoubleMatrix 35 | getDoubleMatrixType = type(getDoubleMatrix[0][0]) 36 | print(getDoubleMatrixType) 37 | print(getDoubleMatrix) 38 | if(getDoubleMatrixType is not float): 39 | raise Exception("getDoubleMatrix not float!") 40 | 41 | print("getDoubleMatrixAsList") 42 | if 'getDoubleMatrixAsList' in locals(): 43 | raise Exception("getDoubleMatrixAsList already defined!") 44 | getDoubleMatrixAsList = putDoubleMatrixAsList 45 | getDoubleMatrixAsListType = type(getDoubleMatrixAsList[0][0]) 46 | print(getDoubleMatrixAsListType) 47 | print(getDoubleMatrixAsList) 48 | if(getDoubleMatrixAsListType is not float): 49 | raise Exception("getDoubleMatrixAsList not float!") 50 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/InputsAndResultsTestDoubleNan.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | print("getDouble") 4 | if 'getDouble' in locals(): 5 | raise Exception("getDouble already defined!") 6 | getDouble = putDouble 7 | getDoubleType = type(getDouble) 8 | print(getDoubleType) 9 | print(getDouble) 10 | if(getDoubleType is not float): 11 | raise Exception("getDouble not float!") 12 | if(not math.isnan(getDouble)): 13 | raise Exception("getDouble not NaN!") 14 | 15 | print("getDoubleVector") 16 | if 'getDoubleVector' in locals(): 17 | raise Exception("getDoubleVector already defined!") 18 | getDoubleVector = putDoubleVector 19 | getDoubleVectorType = type(getDoubleVector[0]) 20 | print(getDoubleVectorType) 21 | print(getDoubleVector) 22 | if(getDoubleVectorType is not float): 23 | raise Exception("getDoubleVector not float!") 24 | if(not math.isnan(getDoubleVector[1])): 25 | raise Exception("getDoubleVector[1] not NaN!") 26 | 27 | print("getDoubleVectorAsList") 28 | if 'getDoubleVectorAsList' in locals(): 29 | raise Exception("getDoubleVectorAsList already defined!") 30 | getDoubleVectorAsList = putDoubleVectorAsList 31 | getDoubleVectorAsListType = type(getDoubleVectorAsList[0]) 32 | print(getDoubleVectorAsListType) 33 | print(getDoubleVectorAsList) 34 | if(getDoubleVectorAsListType is not float): 35 | raise Exception("getDoubleVectorAsList not float!") 36 | if(not math.isnan(getDoubleVectorAsList[1])): 37 | raise Exception("getDoubleVectorAsList[1] not NaN!") 38 | 39 | print("getDoubleMatrix") 40 | if 'getDoubleMatrix' in locals(): 41 | raise Exception("getDoubleMatrix already defined!") 42 | getDoubleMatrix = putDoubleMatrix 43 | getDoubleMatrixType = type(getDoubleMatrix[0][0]) 44 | print(getDoubleMatrixType) 45 | print(getDoubleMatrix) 46 | if(getDoubleMatrixType is not float): 47 | raise Exception("getDoubleMatrix not float!") 48 | if(not math.isnan(getDoubleMatrix[0][0])): 49 | raise Exception("getDoubleMatrix[0][0] not NaN!") 50 | if(not math.isnan(getDoubleMatrix[1][1])): 51 | raise Exception("getDoubleMatrix[1][1] not NaN!") 52 | if(not math.isnan(getDoubleMatrix[2][2])): 53 | raise Exception("getDoubleMatrix[2][2] not NaN!") 54 | 55 | print("getDoubleMatrixAsList") 56 | if 'getDoubleMatrixAsList' in locals(): 57 | raise Exception("getDoubleMatrixAsList already defined!") 58 | getDoubleMatrixAsList = putDoubleMatrixAsList 59 | getDoubleMatrixAsListType = type(getDoubleMatrixAsList[0][0]) 60 | print(getDoubleMatrixAsListType) 61 | print(getDoubleMatrixAsList) 62 | if(getDoubleMatrixAsListType is not float): 63 | raise Exception("getDoubleMatrixAsList not float!") 64 | if(not math.isnan(getDoubleMatrixAsList[0][0])): 65 | raise Exception("getDoubleMatrixAsList[0][0] not NaN!") 66 | if(not math.isnan(getDoubleMatrixAsList[1][1])): 67 | raise Exception("getDoubleMatrixAsList[1][1] not NaN!") 68 | if(not math.isnan(getDoubleMatrixAsList[2][2])): 69 | raise Exception("getDoubleMatrixAsList[2][2] not NaN!") 70 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/InputsAndResultsTestEmptyMatrixValue.py: -------------------------------------------------------------------------------- 1 | getBooleanMatrix = putBooleanMatrix 2 | if len(getBooleanMatrix) != 2: 3 | raise Exception("getBooleanMatrix empty!") 4 | 5 | getBooleanMatrixAsList = putBooleanMatrixAsList 6 | if len(getBooleanMatrixAsList) != 2: 7 | raise Exception("getBooleanMatrixAsList empty!") 8 | 9 | 10 | getByteMatrix = putByteMatrix 11 | if len(getByteMatrix) != 2: 12 | raise Exception("getByteMatrix empty!") 13 | 14 | getByteMatrixAsList = putByteMatrixAsList 15 | if len(getByteMatrixAsList) != 2: 16 | raise Exception("getByteMatrixAsList empty!") 17 | 18 | 19 | getCharacterMatrix = putCharacterMatrix 20 | if len(getCharacterMatrix) != 2: 21 | raise Exception("getCharacterMatrix empty!") 22 | 23 | getCharacterMatrixAsList = putCharacterMatrixAsList 24 | if len(getCharacterMatrixAsList) != 2: 25 | raise Exception("getCharacterMatrixAsList empty!") 26 | 27 | 28 | getDecimalMatrix = putDecimalMatrix 29 | if len(getDecimalMatrix) != 2: 30 | raise Exception("getDecimalMatrix empty!") 31 | 32 | getDecimalMatrixAsList = putDecimalMatrixAsList 33 | if len(getDecimalMatrixAsList) != 2: 34 | raise Exception("getDecimalMatrixAsList empty!") 35 | 36 | 37 | getDoubleMatrix = putDoubleMatrix 38 | if len(getDoubleMatrix) != 2: 39 | raise Exception("getDoubleMatrix empty!") 40 | 41 | getDoubleMatrixAsList = putDoubleMatrixAsList 42 | if len(getDoubleMatrixAsList) != 2: 43 | raise Exception("getDoubleMatrixAsList empty!") 44 | 45 | 46 | getFloatMatrix = putFloatMatrix 47 | if len(getFloatMatrix) != 2: 48 | raise Exception("getFloatMatrix empty!") 49 | 50 | getFloatMatrixAsList = putFloatMatrixAsList 51 | if len(getFloatMatrixAsList) != 2: 52 | raise Exception("getFloatMatrixAsList empty!") 53 | 54 | 55 | getIntegerMatrix = putIntegerMatrix 56 | if len(getIntegerMatrix) != 2: 57 | raise Exception("getIntegerMatrix empty!") 58 | 59 | getIntegerMatrixAsList = putIntegerMatrixAsList 60 | if len(getIntegerMatrixAsList) != 2: 61 | raise Exception("getIntegerMatrixAsList empty!") 62 | 63 | 64 | getLongMatrix = putLongMatrix 65 | if len(getLongMatrix) != 2: 66 | raise Exception("getLongMatrix empty!") 67 | 68 | getLongMatrixAsList = putLongMatrixAsList 69 | if len(getLongMatrixAsList) != 2: 70 | raise Exception("getLongMatrixAsList empty!") 71 | 72 | 73 | getPercentMatrix = putPercentMatrix 74 | if len(getPercentMatrix) != 2: 75 | raise Exception("getPercentMatrix empty!") 76 | 77 | getPercentMatrixAsList = putPercentMatrixAsList 78 | if len(getPercentMatrixAsList) != 2: 79 | raise Exception("getPercentMatrixAsList empty!") 80 | 81 | 82 | getShortMatrix = putShortMatrix 83 | if len(getShortMatrix) != 2: 84 | raise Exception("getShortMatrix empty!") 85 | 86 | getShortMatrixAsList = putShortMatrixAsList 87 | if len(getShortMatrixAsList) != 2: 88 | raise Exception("getShortMatrixAsList empty!") 89 | 90 | 91 | getStringMatrix = putStringMatrix 92 | if len(getStringMatrix) != 2: 93 | raise Exception("getStringMatrix empty!") 94 | 95 | getStringMatrixAsList = putStringMatrixAsList 96 | if len(getStringMatrixAsList) != 2: 97 | raise Exception("getStringMatrixAsList empty!") 98 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/InputsAndResultsTestFloat.py: -------------------------------------------------------------------------------- 1 | print("getFloat") 2 | if 'getFloat' in locals(): 3 | raise Exception("getFloat already defined!") 4 | getFloat = putFloat 5 | getFloatType = type(getFloat) 6 | print(getFloatType) 7 | print(getFloat) 8 | if(getFloatType is not float): 9 | raise Exception("getFloat not float!") 10 | 11 | print("getFloatVector") 12 | if 'getFloatVector' in locals(): 13 | raise Exception("getFloatVector already defined!") 14 | getFloatVector = putFloatVector 15 | getFloatVectorType = type(getFloatVector[0]) 16 | print(getFloatVectorType) 17 | print(getFloatVector) 18 | if(getFloatVectorType is not float): 19 | raise Exception("getFloatVector not float!") 20 | 21 | print("getFloatVectorAsList") 22 | if 'getFloatVectorAsList' in locals(): 23 | raise Exception("getFloatVectorAsList already defined!") 24 | getFloatVectorAsList = putFloatVectorAsList 25 | getFloatVectorAsListType = type(getFloatVectorAsList[0]) 26 | print(getFloatVectorAsListType) 27 | print(getFloatVectorAsList) 28 | if(getFloatVectorAsListType is not float): 29 | raise Exception("getFloatVectorAsList not float!") 30 | 31 | print("getFloatMatrix") 32 | if 'getFloatMatrix' in locals(): 33 | raise Exception("getFloatMatrix already defined!") 34 | getFloatMatrix = putFloatMatrix 35 | getFloatMatrixType = type(getFloatMatrix[0][0]) 36 | print(getFloatMatrixType) 37 | print(getFloatMatrix) 38 | if(getFloatMatrixType is not float): 39 | raise Exception("getFloatMatrix not float!") 40 | 41 | print("getFloatMatrixAsList") 42 | if 'getFloatMatrixAsList' in locals(): 43 | raise Exception("getFloatMatrixAsList already defined!") 44 | getFloatMatrixAsList = putFloatMatrixAsList 45 | getFloatMatrixAsListType = type(getFloatMatrixAsList[0][0]) 46 | print(getFloatMatrixAsListType) 47 | print(getFloatMatrixAsList) 48 | if(getFloatMatrixAsListType is not float): 49 | raise Exception("getFloatMatrixAsList not float!") 50 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/InputsAndResultsTestInteger.py: -------------------------------------------------------------------------------- 1 | print("getInteger") 2 | if 'getInteger' in locals(): 3 | raise Exception("getInteger already defined!") 4 | getInteger = putInteger 5 | getIntegerType = type(getInteger) 6 | print(getIntegerType) 7 | print(getInteger) 8 | if(getIntegerType is not int): 9 | raise Exception("getInteger not int!") 10 | 11 | print("getIntegerVector") 12 | if 'getIntegerVector' in locals(): 13 | raise Exception("getIntegerVector already defined!") 14 | getIntegerVector = putIntegerVector 15 | getIntegerVectorType = type(getIntegerVector[0]) 16 | print(getIntegerVectorType) 17 | print(getIntegerVector) 18 | if(getIntegerVectorType is not int): 19 | raise Exception("getIntegerVector not int!") 20 | 21 | print("getIntegerVectorAsList") 22 | if 'getIntegerVectorAsList' in locals(): 23 | raise Exception("getIntegerVectorAsList already defined!") 24 | getIntegerVectorAsList = putIntegerVectorAsList 25 | getIntegerVectorAsListType = type(getIntegerVectorAsList[0]) 26 | print(getIntegerVectorAsListType) 27 | print(getIntegerVectorAsList) 28 | if(getIntegerVectorAsListType is not int): 29 | raise Exception("getIntegerVectorAsList not int!") 30 | 31 | print("getIntegerMatrix") 32 | if 'getIntegerMatrix' in locals(): 33 | raise Exception("getIntegerMatrix already defined!") 34 | getIntegerMatrix = putIntegerMatrix 35 | getIntegerMatrixType = type(getIntegerMatrix[0][0]) 36 | print(getIntegerMatrixType) 37 | print(getIntegerMatrix) 38 | if(getIntegerMatrixType is not int): 39 | raise Exception("getIntegerMatrix not int!") 40 | 41 | print("getIntegerMatrixAsList") 42 | if 'getIntegerMatrixAsList' in locals(): 43 | raise Exception("getIntegerMatrixAsList already defined!") 44 | getIntegerMatrixAsList = putIntegerMatrixAsList 45 | getIntegerMatrixAsListType = type(getIntegerMatrixAsList[0][0]) 46 | print(getIntegerMatrixAsListType) 47 | print(getIntegerMatrixAsList) 48 | if(getIntegerMatrixAsListType is not int): 49 | raise Exception("getIntegerMatrixAsList not int!") 50 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/InputsAndResultsTestLong.py: -------------------------------------------------------------------------------- 1 | print("getLong") 2 | if 'getLong' in locals(): 3 | raise Exception("getLong already defined!") 4 | getLong = putLong 5 | getLongType = type(getLong) 6 | print(getLongType) 7 | print(getLong) 8 | if(getLongType is not long): 9 | raise Exception("getLong not long!") 10 | 11 | print("getLongVector") 12 | if 'getLongVector' in locals(): 13 | raise Exception("getLongVector already defined!") 14 | getLongVector = putLongVector 15 | getLongVectorType = type(getLongVector[0]) 16 | print(getLongVectorType) 17 | print(getLongVector) 18 | if(getLongVectorType is not long): 19 | raise Exception("getLongVector not long!") 20 | 21 | print("getLongVectorAsList") 22 | if 'getLongVectorAsList' in locals(): 23 | raise Exception("getLongVectorAsList already defined!") 24 | getLongVectorAsList = putLongVectorAsList 25 | getLongVectorAsListType = type(getLongVectorAsList[0]) 26 | print(getLongVectorAsListType) 27 | print(getLongVectorAsList) 28 | if(getLongVectorAsListType is not long): 29 | raise Exception("getLongVectorAsList not long!") 30 | 31 | print("getLongMatrix") 32 | if 'getLongMatrix' in locals(): 33 | raise Exception("getLongMatrix already defined!") 34 | getLongMatrix = putLongMatrix 35 | getLongMatrixType = type(getLongMatrix[0][0]) 36 | print(getLongMatrixType) 37 | print(getLongMatrix) 38 | if(getLongMatrixType is not long): 39 | raise Exception("getLongMatrix not long!") 40 | 41 | print("getLongMatrixAsList") 42 | if 'getLongMatrixAsList' in locals(): 43 | raise Exception("getLongMatrixAsList already defined!") 44 | getLongMatrixAsList = putLongMatrixAsList 45 | getLongMatrixAsListType = type(getLongMatrixAsList[0][0]) 46 | print(getLongMatrixAsListType) 47 | print(getLongMatrixAsList) 48 | if(getLongMatrixAsListType is not long): 49 | raise Exception("getLongMatrixAsList not long!") 50 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/InputsAndResultsTestNull.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.contract; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import de.invesdwin.context.integration.script.IScriptTaskEngine; 6 | import de.invesdwin.context.integration.script.IScriptTaskInputs; 7 | import de.invesdwin.context.integration.script.IScriptTaskResults; 8 | import de.invesdwin.util.assertions.Assertions; 9 | 10 | @NotThreadSafe 11 | public class InputsAndResultsTestNull { 12 | 13 | private final IScriptTaskRunnerPython runner; 14 | 15 | public InputsAndResultsTestNull(final IScriptTaskRunnerPython runner) { 16 | this.runner = runner; 17 | } 18 | 19 | public void testNull() { 20 | new AScriptTaskPython() { 21 | 22 | @Override 23 | public void populateInputs(final IScriptTaskInputs inputs) {} 24 | 25 | @Override 26 | public void executeScript(final IScriptTaskEngine engine) { 27 | Assertions.checkTrue(engine.getResults().isNotDefined("testVariable")); 28 | Assertions.checkFalse(engine.getResults().isDefined("testVariable")); 29 | engine.getInputs().putNull("testVariable"); 30 | Assertions.checkFalse(engine.getResults().isNotDefined("testVariable")); 31 | Assertions.checkTrue(engine.getResults().isDefined("testVariable")); 32 | Assertions.checkTrue(engine.getResults().isNull("testVariable")); 33 | Assertions.checkFalse(engine.getResults().isNotNull("testVariable")); 34 | engine.getInputs().putString("testVariable", "value"); 35 | Assertions.checkFalse(engine.getResults().isNotDefined("testVariable")); 36 | Assertions.checkTrue(engine.getResults().isDefined("testVariable")); 37 | Assertions.checkFalse(engine.getResults().isNull("testVariable")); 38 | Assertions.checkTrue(engine.getResults().isNotNull("testVariable")); 39 | engine.getInputs().remove("testVariable"); 40 | Assertions.checkTrue(engine.getResults().isNotDefined("testVariable")); 41 | Assertions.checkFalse(engine.getResults().isDefined("testVariable")); 42 | 43 | } 44 | 45 | @Override 46 | public Void extractResults(final IScriptTaskResults results) { 47 | return null; 48 | } 49 | }.run(runner); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/InputsAndResultsTestPercent.py: -------------------------------------------------------------------------------- 1 | print("getPercent") 2 | if 'getPercent' in locals(): 3 | raise Exception("getPercent already defined!") 4 | getPercent = putPercent 5 | getPercentType = type(getPercent) 6 | print(getPercentType) 7 | print(getPercent) 8 | if(getPercentType is not float): 9 | raise Exception("getPercent not float!") 10 | 11 | print("getPercentVector") 12 | if 'getPercentVector' in locals(): 13 | raise Exception("getPercentVector already defined!") 14 | getPercentVector = putPercentVector 15 | getPercentVectorType = type(getPercentVector[0]) 16 | print(getPercentVectorType) 17 | print(getPercentVector) 18 | if(getPercentVectorType is not float): 19 | raise Exception("getPercentVector not float!") 20 | 21 | print("getPercentVectorAsList") 22 | if 'getPercentVectorAsList' in locals(): 23 | raise Exception("getPercentVectorAsList already defined!") 24 | getPercentVectorAsList = putPercentVectorAsList 25 | getPercentVectorAsListType = type(getPercentVectorAsList[0]) 26 | print(getPercentVectorAsListType) 27 | print(getPercentVectorAsList) 28 | if(getPercentVectorAsListType is not float): 29 | raise Exception("getPercentVectorAsList not float!") 30 | 31 | print("getPercentMatrix") 32 | if 'getPercentMatrix' in locals(): 33 | raise Exception("getPercentMatrix already defined!") 34 | getPercentMatrix = putPercentMatrix 35 | getPercentMatrixType = type(getPercentMatrix[0][0]) 36 | print(getPercentMatrixType) 37 | print(getPercentMatrix) 38 | if(getPercentMatrixType is not float): 39 | raise Exception("getPercentMatrix not float!") 40 | 41 | print("getPercentMatrixAsList") 42 | if 'getPercentMatrixAsList' in locals(): 43 | raise Exception("getPercentMatrixAsList already defined!") 44 | getPercentMatrixAsList = putPercentMatrixAsList 45 | getPercentMatrixAsListType = type(getPercentMatrixAsList[0][0]) 46 | print(getPercentMatrixAsListType) 47 | print(getPercentMatrixAsList) 48 | if(getPercentMatrixAsListType is not float): 49 | raise Exception("getPercentMatrixAsList not float!") 50 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/InputsAndResultsTestShort.py: -------------------------------------------------------------------------------- 1 | print("getShort") 2 | if 'getShort' in locals(): 3 | raise Exception("getShort already defined!") 4 | getShort = putShort 5 | getShortType = type(getShort) 6 | print(getShortType) 7 | print(getShort) 8 | if(getShortType is not int): 9 | raise Exception("getShort not int!") 10 | 11 | print("getShortVector") 12 | if 'getShortVector' in locals(): 13 | raise Exception("getShortVector already defined!") 14 | getShortVector = putShortVector 15 | getShortVectorType = type(getShortVector[0]) 16 | print(getShortVectorType) 17 | print(getShortVector) 18 | if(getShortVectorType is not int): 19 | raise Exception("getShortVector not int!") 20 | 21 | print("getShortVectorAsList") 22 | if 'getShortVectorAsList' in locals(): 23 | raise Exception("getShortVectorAsList already defined!") 24 | getShortVectorAsList = putShortVectorAsList 25 | getShortVectorAsListType = type(getShortVectorAsList[0]) 26 | print(getShortVectorAsListType) 27 | print(getShortVectorAsList) 28 | if(getShortVectorAsListType is not int): 29 | raise Exception("getShortVectorAsList not int!") 30 | 31 | print("getShortMatrix") 32 | if 'getShortMatrix' in locals(): 33 | raise Exception("getShortMatrix already defined!") 34 | getShortMatrix = putShortMatrix 35 | getShortMatrixType = type(getShortMatrix[0][0]) 36 | print(getShortMatrixType) 37 | print(getShortMatrix) 38 | if(getShortMatrixType is not int): 39 | raise Exception("getShortMatrix not int!") 40 | 41 | print("getShortMatrixAsList") 42 | if 'getShortMatrixAsList' in locals(): 43 | raise Exception("getShortMatrixAsList already defined!") 44 | getShortMatrixAsList = putShortMatrixAsList 45 | getShortMatrixAsListType = type(getShortMatrixAsList[0][0]) 46 | print(getShortMatrixAsListType) 47 | print(getShortMatrixAsList) 48 | if(getShortMatrixAsListType is not int): 49 | raise Exception("getShortMatrixAsList not int!") 50 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/InputsAndResultsTests.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.contract; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import javax.annotation.concurrent.NotThreadSafe; 7 | 8 | import de.invesdwin.context.python.runtime.contract.hello.HelloWorldScript; 9 | import de.invesdwin.util.concurrent.Executors; 10 | import de.invesdwin.util.concurrent.WrappedExecutorService; 11 | import de.invesdwin.util.concurrent.future.Futures; 12 | 13 | @NotThreadSafe 14 | public class InputsAndResultsTests { 15 | 16 | private final IScriptTaskRunnerPython runner; 17 | 18 | public InputsAndResultsTests(final IScriptTaskRunnerPython runner) { 19 | this.runner = runner; 20 | } 21 | 22 | public void test() { 23 | new HelloWorldScript(runner).testHelloWorld(); 24 | new InputsAndResultsTestByte(runner).testByte(); 25 | new InputsAndResultsTestFloat(runner).testFloat(); 26 | new InputsAndResultsTestDouble(runner).testDouble(); 27 | new InputsAndResultsTestDecimal(runner).testDecimal(); 28 | new InputsAndResultsTestPercent(runner).testPercent(); 29 | new InputsAndResultsTestShort(runner).testShort(); 30 | new InputsAndResultsTestInteger(runner).testInteger(); 31 | new InputsAndResultsTestLong(runner).testLong(); 32 | new InputsAndResultsTestCharacter(runner).testCharacter(); 33 | new InputsAndResultsTestString(runner).testString(); 34 | new InputsAndResultsTestBoolean(runner).testBoolean(); 35 | new InputsAndResultsTestNull(runner).testNull(); 36 | new InputsAndResultsTestNullPutGet(runner).testNullPutGet(); 37 | new InputsAndResultsTestEmpty(runner).testEmpty(); 38 | new InputsAndResultsTestEmptyMatrixValue(runner).testEmptyMatrixValue(); 39 | new InputsAndResultsTestDoubleNan(runner).testDoubleNan(); 40 | } 41 | 42 | public void testParallel() { 43 | final List tasks = new ArrayList(); 44 | for (int i = 0; i < 10; i++) { 45 | tasks.add(new Runnable() { 46 | @Override 47 | public void run() { 48 | test(); 49 | } 50 | }); 51 | } 52 | final WrappedExecutorService executor = Executors.newFixedThreadPool( 53 | InputsAndResultsTests.class.getSimpleName() + "_testParallel", 54 | Runtime.getRuntime().availableProcessors()); 55 | try { 56 | Futures.submitAndWait(executor, tasks); 57 | } catch (final InterruptedException e) { 58 | throw new RuntimeException(e); 59 | } finally { 60 | executor.shutdown(); 61 | } 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/callback/ParametersAndReturnsTestBoolean.py: -------------------------------------------------------------------------------- 1 | print("getBoolean") 2 | if 'getBoolean' in locals(): 3 | raise Exception("getBoolean already defined!") 4 | getBoolean = callback("getBoolean") 5 | getBooleanType = type(getBoolean) 6 | print(getBooleanType) 7 | print(getBoolean) 8 | if(getBooleanType is not bool): 9 | raise Exception("getBoolean not bool!") 10 | callback("setBoolean", getBoolean) 11 | 12 | print("getBooleanVector") 13 | if 'getBooleanVector' in locals(): 14 | raise Exception("getBooleanVector already defined!") 15 | getBooleanVector = callback("getBooleanVector") 16 | getBooleanVectorType = type(getBooleanVector[0]) 17 | print(getBooleanVectorType) 18 | print(getBooleanVector) 19 | if(getBooleanVectorType is not bool): 20 | raise Exception("getBooleanVector not bool!") 21 | callback("setBooleanVector", getBooleanVector) 22 | 23 | print("getBooleanVectorAsList") 24 | if 'getBooleanVectorAsList' in locals(): 25 | raise Exception("getBooleanVectorAsList already defined!") 26 | getBooleanVectorAsList = callback("getBooleanVectorAsList") 27 | getBooleanVectorAsListType = type(getBooleanVectorAsList[0]) 28 | print(getBooleanVectorAsListType) 29 | print(getBooleanVectorAsList) 30 | if(getBooleanVectorAsListType is not bool): 31 | raise Exception("getBooleanVectorAsList not bool!") 32 | callback("setBooleanVectorAsList", getBooleanVectorAsList) 33 | 34 | print("getBooleanMatrix") 35 | if 'getBooleanMatrix' in locals(): 36 | raise Exception("getBooleanMatrix already defined!") 37 | getBooleanMatrix = callback("getBooleanMatrix") 38 | getBooleanMatrixType = type(getBooleanMatrix[0][0]) 39 | print(getBooleanMatrixType) 40 | print(getBooleanMatrix) 41 | if(getBooleanMatrixType is not bool): 42 | raise Exception("getBooleanMatrix not bool!") 43 | callback("setBooleanMatrix", getBooleanMatrix) 44 | 45 | print("getBooleanMatrixAsList") 46 | if 'getBooleanMatrixAsList' in locals(): 47 | raise Exception("getBooleanMatrixAsList already defined!") 48 | getBooleanMatrixAsList = callback("getBooleanMatrixAsList") 49 | getBooleanMatrixAsListType = type(getBooleanMatrixAsList[0][0]) 50 | print(getBooleanMatrixAsListType) 51 | print(getBooleanMatrixAsList) 52 | if(getBooleanMatrixAsListType is not bool): 53 | raise Exception("getBooleanMatrixAsList not bool!") 54 | callback("setBooleanMatrixAsList", getBooleanMatrixAsList) 55 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/callback/ParametersAndReturnsTestByte.py: -------------------------------------------------------------------------------- 1 | print("getByte") 2 | if 'getByte' in locals(): 3 | raise Exception("getByte already defined!") 4 | getByte = callback("getByte") 5 | getByteType = type(getByte) 6 | print(getByteType) 7 | print(getByte) 8 | if(getByteType is not int): 9 | raise Exception("getByte not int!") 10 | callback("setByte", getByte) 11 | 12 | print("getByteVector") 13 | if 'getByteVector' in locals(): 14 | raise Exception("getByteVector already defined!") 15 | getByteVector = callback("getByteVector") 16 | getByteVectorType = type(getByteVector[0]) 17 | print(getByteVectorType) 18 | print(getByteVector) 19 | if(getByteVectorType is not int): 20 | raise Exception("getByteVector not int!") 21 | callback("setByteVector", getByteVector) 22 | 23 | print("getByteVectorAsList") 24 | if 'getByteVectorAsList' in locals(): 25 | raise Exception("getByteVectorAsList already defined!") 26 | getByteVectorAsList = callback("getByteVectorAsList") 27 | getByteVectorAsListType = type(getByteVectorAsList[0]) 28 | print(getByteVectorAsListType) 29 | print(getByteVectorAsList) 30 | if(getByteVectorAsListType is not int): 31 | raise Exception("getByteVectorAsList not int!") 32 | callback("setByteVectorAsList", getByteVectorAsList) 33 | 34 | print("getByteMatrix") 35 | if 'getByteMatrix' in locals(): 36 | raise Exception("getByteMatrix already defined!") 37 | getByteMatrix = callback("getByteMatrix") 38 | getByteMatrixType = type(getByteMatrix[0][0]) 39 | print(getByteMatrixType) 40 | print(getByteMatrix) 41 | if(getByteMatrixType is not int): 42 | raise Exception("getByteMatrix not int!") 43 | callback("setByteMatrix", getByteMatrix) 44 | 45 | print("getByteMatrixAsList") 46 | if 'getByteMatrixAsList' in locals(): 47 | raise Exception("getByteMatrixAsList already defined!") 48 | getByteMatrixAsList = callback("getByteMatrixAsList") 49 | getByteMatrixAsListType = type(getByteMatrixAsList[0][0]) 50 | print(getByteMatrixAsListType) 51 | print(getByteMatrixAsList) 52 | if(getByteMatrixAsListType is not int): 53 | raise Exception("getByteMatrixAsList not int!") 54 | callback("setByteMatrixAsList", getByteMatrixAsList) 55 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/callback/ParametersAndReturnsTestCharacter.py: -------------------------------------------------------------------------------- 1 | print("getCharacter") 2 | if 'getCharacter' in locals(): 3 | raise Exception("getCharacter already defined!") 4 | getCharacter = callback("getCharacter") 5 | getCharacterType = type(getCharacter) 6 | print(getCharacterType) 7 | print(getCharacter) 8 | if(not isinstance(getCharacter, (unicode, str))): 9 | raise Exception("getCharacter not unicode or str!") 10 | callback("setCharacter", getCharacter) 11 | 12 | print("getCharacterVector") 13 | if 'getCharacterVector' in locals(): 14 | raise Exception("getCharacterVector already defined!") 15 | getCharacterVector = callback("getCharacterVector") 16 | getCharacterVectorType = type(getCharacterVector[0]) 17 | print(getCharacterVectorType) 18 | print(getCharacterVector) 19 | if(not isinstance(getCharacterVector[0], (unicode, str))): 20 | raise Exception("getCharacterVector not unicode or str!") 21 | callback("setCharacterVector", getCharacterVector) 22 | 23 | print("getCharacterVectorAsList") 24 | if 'getCharacterVectorAsList' in locals(): 25 | raise Exception("getCharacterVectorAsList already defined!") 26 | getCharacterVectorAsList = callback("getCharacterVectorAsList") 27 | getCharacterVectorAsListType = type(getCharacterVectorAsList[0]) 28 | print(getCharacterVectorAsListType) 29 | print(getCharacterVectorAsList) 30 | if(not isinstance(getCharacterVectorAsList[0], (unicode, str))): 31 | raise Exception("getCharacterVectorAsList not unicode or str!") 32 | callback("setCharacterVectorAsList", getCharacterVectorAsList) 33 | 34 | print("getCharacterMatrix") 35 | if 'getCharacterMatrix' in locals(): 36 | raise Exception("getCharacterMatrix already defined!") 37 | getCharacterMatrix = callback("getCharacterMatrix") 38 | getCharacterMatrixType = type(getCharacterMatrix[0][0]) 39 | print(getCharacterMatrixType) 40 | print(getCharacterMatrix) 41 | if(not isinstance(getCharacterMatrix[0][0], (unicode, str))): 42 | raise Exception("getCharacterMatrix not unicode or str!") 43 | callback("setCharacterMatrix", getCharacterMatrix) 44 | 45 | print("getCharacterMatrixAsList") 46 | if 'getCharacterMatrixAsList' in locals(): 47 | raise Exception("getCharacterMatrixAsList already defined!") 48 | getCharacterMatrixAsList = callback("getCharacterMatrixAsList") 49 | getCharacterMatrixAsListType = type(getCharacterMatrixAsList[0][0]) 50 | print(getCharacterMatrixAsListType) 51 | print(getCharacterMatrixAsList) 52 | if(not isinstance(getCharacterMatrixAsList[0][0], (unicode, str))): 53 | raise Exception("getCharacterMatrixAsList not unicode or str!") 54 | callback("setCharacterMatrixAsList", getCharacterMatrixAsList) 55 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/callback/ParametersAndReturnsTestDecimal.py: -------------------------------------------------------------------------------- 1 | print("getDecimal") 2 | if 'getDecimal' in locals(): 3 | raise Exception("getDecimal already defined!") 4 | getDecimal = callback("getDecimal") 5 | getDecimalType = type(getDecimal) 6 | print(getDecimalType) 7 | print(getDecimal) 8 | if(getDecimalType is not float): 9 | raise Exception("getDecimal not float!") 10 | callback("setDecimal", getDecimal) 11 | 12 | print("getDecimalVector") 13 | if 'getDecimalVector' in locals(): 14 | raise Exception("getDecimalVector already defined!") 15 | getDecimalVector = callback("getDecimalVector") 16 | getDecimalVectorType = type(getDecimalVector[0]) 17 | print(getDecimalVectorType) 18 | print(getDecimalVector) 19 | if(getDecimalVectorType is not float): 20 | raise Exception("getDecimalVector not float!") 21 | callback("setDecimalVector", getDecimalVector) 22 | 23 | print("getDecimalVectorAsList") 24 | if 'getDecimalVectorAsList' in locals(): 25 | raise Exception("getDecimalVectorAsList already defined!") 26 | getDecimalVectorAsList = callback("getDecimalVectorAsList") 27 | getDecimalVectorAsListType = type(getDecimalVectorAsList[0]) 28 | print(getDecimalVectorAsListType) 29 | print(getDecimalVectorAsList) 30 | if(getDecimalVectorAsListType is not float): 31 | raise Exception("getDecimalVectorAsList not float!") 32 | callback("setDecimalVectorAsList", getDecimalVectorAsList) 33 | 34 | print("getDecimalMatrix") 35 | if 'getDecimalMatrix' in locals(): 36 | raise Exception("getDecimalMatrix already defined!") 37 | getDecimalMatrix = callback("getDecimalMatrix") 38 | getDecimalMatrixType = type(getDecimalMatrix[0][0]) 39 | print(getDecimalMatrixType) 40 | print(getDecimalMatrix) 41 | if(getDecimalMatrixType is not float): 42 | raise Exception("getDecimalMatrix not float!") 43 | callback("setDecimalMatrix", getDecimalMatrix) 44 | 45 | print("getDecimalMatrixAsList") 46 | if 'getDecimalMatrixAsList' in locals(): 47 | raise Exception("getDecimalMatrixAsList already defined!") 48 | getDecimalMatrixAsList = callback("getDecimalMatrixAsList") 49 | getDecimalMatrixAsListType = type(getDecimalMatrixAsList[0][0]) 50 | print(getDecimalMatrixAsListType) 51 | print(getDecimalMatrixAsList) 52 | if(getDecimalMatrixAsListType is not float): 53 | raise Exception("getDecimalMatrixAsList not float!") 54 | callback("setDecimalMatrixAsList", getDecimalMatrixAsList) 55 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/callback/ParametersAndReturnsTestDouble.py: -------------------------------------------------------------------------------- 1 | print("getDouble") 2 | if 'getDouble' in locals(): 3 | raise Exception("getDouble already defined!") 4 | getDouble = callback("getDouble") 5 | getDoubleType = type(getDouble) 6 | print(getDoubleType) 7 | print(getDouble) 8 | if(getDoubleType is not float): 9 | raise Exception("getDouble not float!") 10 | callback("setDouble", getDouble) 11 | 12 | print("getDoubleVector") 13 | if 'getDoubleVector' in locals(): 14 | raise Exception("getDoubleVector already defined!") 15 | getDoubleVector = callback("getDoubleVector") 16 | getDoubleVectorType = type(getDoubleVector[0]) 17 | print(getDoubleVectorType) 18 | print(getDoubleVector) 19 | if(getDoubleVectorType is not float): 20 | raise Exception("getDoubleVector not float!") 21 | callback("setDoubleVector", getDoubleVector) 22 | 23 | print("getDoubleVectorAsList") 24 | if 'getDoubleVectorAsList' in locals(): 25 | raise Exception("getDoubleVectorAsList already defined!") 26 | getDoubleVectorAsList = callback("getDoubleVectorAsList") 27 | getDoubleVectorAsListType = type(getDoubleVectorAsList[0]) 28 | print(getDoubleVectorAsListType) 29 | print(getDoubleVectorAsList) 30 | if(getDoubleVectorAsListType is not float): 31 | raise Exception("getDoubleVectorAsList not float!") 32 | callback("setDoubleVectorAsList", getDoubleVectorAsList) 33 | 34 | print("getDoubleMatrix") 35 | if 'getDoubleMatrix' in locals(): 36 | raise Exception("getDoubleMatrix already defined!") 37 | getDoubleMatrix = callback("getDoubleMatrix") 38 | getDoubleMatrixType = type(getDoubleMatrix[0][0]) 39 | print(getDoubleMatrixType) 40 | print(getDoubleMatrix) 41 | if(getDoubleMatrixType is not float): 42 | raise Exception("getDoubleMatrix not float!") 43 | callback("setDoubleMatrix", getDoubleMatrix) 44 | 45 | print("getDoubleMatrixAsList") 46 | if 'getDoubleMatrixAsList' in locals(): 47 | raise Exception("getDoubleMatrixAsList already defined!") 48 | getDoubleMatrixAsList = callback("getDoubleMatrixAsList") 49 | getDoubleMatrixAsListType = type(getDoubleMatrixAsList[0][0]) 50 | print(getDoubleMatrixAsListType) 51 | print(getDoubleMatrixAsList) 52 | if(getDoubleMatrixAsListType is not float): 53 | raise Exception("getDoubleMatrixAsList not float!") 54 | callback("setDoubleMatrixAsList", getDoubleMatrixAsList) 55 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/callback/ParametersAndReturnsTestDoubleNan.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | print("getDouble") 4 | if 'getDouble' in locals(): 5 | raise Exception("getDouble already defined!") 6 | getDouble = callback("getDouble") 7 | getDoubleType = type(getDouble) 8 | print(getDoubleType) 9 | print(getDouble) 10 | if(getDoubleType is not float): 11 | raise Exception("getDouble not float!") 12 | if(not math.isnan(getDouble)): 13 | raise Exception("getDouble not NaN!") 14 | callback("setDouble", getDouble) 15 | 16 | print("getDoubleVector") 17 | if 'getDoubleVector' in locals(): 18 | raise Exception("getDoubleVector already defined!") 19 | getDoubleVector = callback("getDoubleVector") 20 | getDoubleVectorType = type(getDoubleVector[0]) 21 | print(getDoubleVectorType) 22 | print(getDoubleVector) 23 | if(getDoubleVectorType is not float): 24 | raise Exception("getDoubleVector not float!") 25 | if(not math.isnan(getDoubleVector[1])): 26 | raise Exception("getDoubleVector[1] not NaN!") 27 | callback("setDoubleVector", getDoubleVector) 28 | 29 | print("getDoubleVectorAsList") 30 | if 'getDoubleVectorAsList' in locals(): 31 | raise Exception("getDoubleVectorAsList already defined!") 32 | getDoubleVectorAsList = callback("getDoubleVectorAsList") 33 | getDoubleVectorAsListType = type(getDoubleVectorAsList[0]) 34 | print(getDoubleVectorAsListType) 35 | print(getDoubleVectorAsList) 36 | if(getDoubleVectorAsListType is not float): 37 | raise Exception("getDoubleVectorAsList not float!") 38 | if(not math.isnan(getDoubleVectorAsList[1])): 39 | raise Exception("getDoubleVectorAsList[1] not NaN!") 40 | callback("setDoubleVectorAsList", getDoubleVectorAsList) 41 | 42 | print("getDoubleMatrix") 43 | if 'getDoubleMatrix' in locals(): 44 | raise Exception("getDoubleMatrix already defined!") 45 | getDoubleMatrix = callback("getDoubleMatrix") 46 | getDoubleMatrixType = type(getDoubleMatrix[0][0]) 47 | print(getDoubleMatrixType) 48 | print(getDoubleMatrix) 49 | if(getDoubleMatrixType is not float): 50 | raise Exception("getDoubleMatrix not float!") 51 | if(not math.isnan(getDoubleMatrix[0][0])): 52 | raise Exception("getDoubleMatrix[0][0] not NaN!") 53 | if(not math.isnan(getDoubleMatrix[1][1])): 54 | raise Exception("getDoubleMatrix[1][1] not NaN!") 55 | if(not math.isnan(getDoubleMatrix[2][2])): 56 | raise Exception("getDoubleMatrix[2][2] not NaN!") 57 | callback("setDoubleMatrix", getDoubleMatrix) 58 | 59 | print("getDoubleMatrixAsList") 60 | if 'getDoubleMatrixAsList' in locals(): 61 | raise Exception("getDoubleMatrixAsList already defined!") 62 | getDoubleMatrixAsList = callback("getDoubleMatrixAsList") 63 | getDoubleMatrixAsListType = type(getDoubleMatrixAsList[0][0]) 64 | print(getDoubleMatrixAsListType) 65 | print(getDoubleMatrixAsList) 66 | if(getDoubleMatrixAsListType is not float): 67 | raise Exception("getDoubleMatrixAsList not float!") 68 | if(not math.isnan(getDoubleMatrixAsList[0][0])): 69 | raise Exception("getDoubleMatrixAsList[0][0] not NaN!") 70 | if(not math.isnan(getDoubleMatrixAsList[1][1])): 71 | raise Exception("getDoubleMatrixAsList[1][1] not NaN!") 72 | if(not math.isnan(getDoubleMatrixAsList[2][2])): 73 | raise Exception("getDoubleMatrixAsList[2][2] not NaN!") 74 | callback("setDoubleMatrixAsList", getDoubleMatrixAsList) 75 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/callback/ParametersAndReturnsTestFloat.py: -------------------------------------------------------------------------------- 1 | print("getFloat") 2 | if 'getFloat' in locals(): 3 | raise Exception("getFloat already defined!") 4 | getFloat = callback("getFloat") 5 | getFloatType = type(getFloat) 6 | print(getFloatType) 7 | print(getFloat) 8 | if(getFloatType is not float): 9 | raise Exception("getFloat not float!") 10 | callback("setFloat", getFloat) 11 | 12 | print("getFloatVector") 13 | if 'getFloatVector' in locals(): 14 | raise Exception("getFloatVector already defined!") 15 | getFloatVector = callback("getFloatVector") 16 | getFloatVectorType = type(getFloatVector[0]) 17 | print(getFloatVectorType) 18 | print(getFloatVector) 19 | if(getFloatVectorType is not float): 20 | raise Exception("getFloatVector not float!") 21 | callback("setFloatVector", getFloatVector) 22 | 23 | print("getFloatVectorAsList") 24 | if 'getFloatVectorAsList' in locals(): 25 | raise Exception("getFloatVectorAsList already defined!") 26 | getFloatVectorAsList = callback("getFloatVectorAsList") 27 | getFloatVectorAsListType = type(getFloatVectorAsList[0]) 28 | print(getFloatVectorAsListType) 29 | print(getFloatVectorAsList) 30 | if(getFloatVectorAsListType is not float): 31 | raise Exception("getFloatVectorAsList not float!") 32 | callback("setFloatVectorAsList", getFloatVectorAsList) 33 | 34 | print("getFloatMatrix") 35 | if 'getFloatMatrix' in locals(): 36 | raise Exception("getFloatMatrix already defined!") 37 | getFloatMatrix = callback("getFloatMatrix") 38 | getFloatMatrixType = type(getFloatMatrix[0][0]) 39 | print(getFloatMatrixType) 40 | print(getFloatMatrix) 41 | if(getFloatMatrixType is not float): 42 | raise Exception("getFloatMatrix not float!") 43 | callback("setFloatMatrix", getFloatMatrix) 44 | 45 | print("getFloatMatrixAsList") 46 | if 'getFloatMatrixAsList' in locals(): 47 | raise Exception("getFloatMatrixAsList already defined!") 48 | getFloatMatrixAsList = callback("getFloatMatrixAsList") 49 | getFloatMatrixAsListType = type(getFloatMatrixAsList[0][0]) 50 | print(getFloatMatrixAsListType) 51 | print(getFloatMatrixAsList) 52 | if(getFloatMatrixAsListType is not float): 53 | raise Exception("getFloatMatrixAsList not float!") 54 | callback("setFloatMatrixAsList", getFloatMatrixAsList) 55 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/callback/ParametersAndReturnsTestInteger.py: -------------------------------------------------------------------------------- 1 | print("getInteger") 2 | if 'getInteger' in locals(): 3 | raise Exception("getInteger already defined!") 4 | getInteger = callback("getInteger") 5 | getIntegerType = type(getInteger) 6 | print(getIntegerType) 7 | print(getInteger) 8 | if(getIntegerType is not int): 9 | raise Exception("getInteger not int!") 10 | callback("setInteger", getInteger) 11 | 12 | print("getIntegerVector") 13 | if 'getIntegerVector' in locals(): 14 | raise Exception("getIntegerVector already defined!") 15 | getIntegerVector = callback("getIntegerVector") 16 | getIntegerVectorType = type(getIntegerVector[0]) 17 | print(getIntegerVectorType) 18 | print(getIntegerVector) 19 | if(getIntegerVectorType is not int): 20 | raise Exception("getIntegerVector not int!") 21 | callback("setIntegerVector", getIntegerVector) 22 | 23 | print("getIntegerVectorAsList") 24 | if 'getIntegerVectorAsList' in locals(): 25 | raise Exception("getIntegerVectorAsList already defined!") 26 | getIntegerVectorAsList = callback("getIntegerVectorAsList") 27 | getIntegerVectorAsListType = type(getIntegerVectorAsList[0]) 28 | print(getIntegerVectorAsListType) 29 | print(getIntegerVectorAsList) 30 | if(getIntegerVectorAsListType is not int): 31 | raise Exception("getIntegerVectorAsList not int!") 32 | callback("setIntegerVectorAsList", getIntegerVectorAsList) 33 | 34 | print("getIntegerMatrix") 35 | if 'getIntegerMatrix' in locals(): 36 | raise Exception("getIntegerMatrix already defined!") 37 | getIntegerMatrix = callback("getIntegerMatrix") 38 | getIntegerMatrixType = type(getIntegerMatrix[0][0]) 39 | print(getIntegerMatrixType) 40 | print(getIntegerMatrix) 41 | if(getIntegerMatrixType is not int): 42 | raise Exception("getIntegerMatrix not int!") 43 | callback("setIntegerMatrix", getIntegerMatrix) 44 | 45 | print("getIntegerMatrixAsList") 46 | if 'getIntegerMatrixAsList' in locals(): 47 | raise Exception("getIntegerMatrixAsList already defined!") 48 | getIntegerMatrixAsList = callback("getIntegerMatrixAsList") 49 | getIntegerMatrixAsListType = type(getIntegerMatrixAsList[0][0]) 50 | print(getIntegerMatrixAsListType) 51 | print(getIntegerMatrixAsList) 52 | if(getIntegerMatrixAsListType is not int): 53 | raise Exception("getIntegerMatrixAsList not int!") 54 | callback("setIntegerMatrixAsList", getIntegerMatrixAsList) 55 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/callback/ParametersAndReturnsTestLong.py: -------------------------------------------------------------------------------- 1 | print("getLong") 2 | if 'getLong' in locals(): 3 | raise Exception("getLong already defined!") 4 | getLong = callback("getLong") 5 | getLongType = type(getLong) 6 | print(getLongType) 7 | print(getLong) 8 | if(getLongType is not long): 9 | raise Exception("getLong not long!") 10 | callback("setLong", getLong) 11 | 12 | print("getLongVector") 13 | if 'getLongVector' in locals(): 14 | raise Exception("getLongVector already defined!") 15 | getLongVector = callback("getLongVector") 16 | getLongVectorType = type(getLongVector[0]) 17 | print(getLongVectorType) 18 | print(getLongVector) 19 | if(getLongVectorType is not long): 20 | raise Exception("getLongVector not long!") 21 | callback("setLongVector", getLongVector) 22 | 23 | print("getLongVectorAsList") 24 | if 'getLongVectorAsList' in locals(): 25 | raise Exception("getLongVectorAsList already defined!") 26 | getLongVectorAsList = callback("getLongVectorAsList") 27 | getLongVectorAsListType = type(getLongVectorAsList[0]) 28 | print(getLongVectorAsListType) 29 | print(getLongVectorAsList) 30 | if(getLongVectorAsListType is not long): 31 | raise Exception("getLongVectorAsList not long!") 32 | callback("setLongVectorAsList", getLongVectorAsList) 33 | 34 | print("getLongMatrix") 35 | if 'getLongMatrix' in locals(): 36 | raise Exception("getLongMatrix already defined!") 37 | getLongMatrix = callback("getLongMatrix") 38 | getLongMatrixType = type(getLongMatrix[0][0]) 39 | print(getLongMatrixType) 40 | print(getLongMatrix) 41 | if(getLongMatrixType is not long): 42 | raise Exception("getLongMatrix not long!") 43 | callback("setLongMatrix", getLongMatrix) 44 | 45 | print("getLongMatrixAsList") 46 | if 'getLongMatrixAsList' in locals(): 47 | raise Exception("getLongMatrixAsList already defined!") 48 | getLongMatrixAsList = callback("getLongMatrixAsList") 49 | getLongMatrixAsListType = type(getLongMatrixAsList[0][0]) 50 | print(getLongMatrixAsListType) 51 | print(getLongMatrixAsList) 52 | if(getLongMatrixAsListType is not long): 53 | raise Exception("getLongMatrixAsList not long!") 54 | callback("setLongMatrixAsList", getLongMatrixAsList) 55 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/callback/ParametersAndReturnsTestPercent.py: -------------------------------------------------------------------------------- 1 | print("getPercent") 2 | if 'getPercent' in locals(): 3 | raise Exception("getPercent already defined!") 4 | getPercent = callback("getPercent") 5 | getPercentType = type(getPercent) 6 | print(getPercentType) 7 | print(getPercent) 8 | if(getPercentType is not float): 9 | raise Exception("getPercent not float!") 10 | callback("setPercent", getPercent) 11 | 12 | print("getPercentVector") 13 | if 'getPercentVector' in locals(): 14 | raise Exception("getPercentVector already defined!") 15 | getPercentVector = callback("getPercentVector") 16 | getPercentVectorType = type(getPercentVector[0]) 17 | print(getPercentVectorType) 18 | print(getPercentVector) 19 | if(getPercentVectorType is not float): 20 | raise Exception("getPercentVector not float!") 21 | callback("setPercentVector", getPercentVector) 22 | 23 | print("getPercentVectorAsList") 24 | if 'getPercentVectorAsList' in locals(): 25 | raise Exception("getPercentVectorAsList already defined!") 26 | getPercentVectorAsList = callback("getPercentVectorAsList") 27 | getPercentVectorAsListType = type(getPercentVectorAsList[0]) 28 | print(getPercentVectorAsListType) 29 | print(getPercentVectorAsList) 30 | if(getPercentVectorAsListType is not float): 31 | raise Exception("getPercentVectorAsList not float!") 32 | callback("setPercentVectorAsList", getPercentVectorAsList) 33 | 34 | print("getPercentMatrix") 35 | if 'getPercentMatrix' in locals(): 36 | raise Exception("getPercentMatrix already defined!") 37 | getPercentMatrix = callback("getPercentMatrix") 38 | getPercentMatrixType = type(getPercentMatrix[0][0]) 39 | print(getPercentMatrixType) 40 | print(getPercentMatrix) 41 | if(getPercentMatrixType is not float): 42 | raise Exception("getPercentMatrix not float!") 43 | callback("setPercentMatrix", getPercentMatrix) 44 | 45 | print("getPercentMatrixAsList") 46 | if 'getPercentMatrixAsList' in locals(): 47 | raise Exception("getPercentMatrixAsList already defined!") 48 | getPercentMatrixAsList = callback("getPercentMatrixAsList") 49 | getPercentMatrixAsListType = type(getPercentMatrixAsList[0][0]) 50 | print(getPercentMatrixAsListType) 51 | print(getPercentMatrixAsList) 52 | if(getPercentMatrixAsListType is not float): 53 | raise Exception("getPercentMatrixAsList not float!") 54 | callback("setPercentMatrixAsList", getPercentMatrixAsList) 55 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/callback/ParametersAndReturnsTestShort.py: -------------------------------------------------------------------------------- 1 | print("getShort") 2 | if 'getShort' in locals(): 3 | raise Exception("getShort already defined!") 4 | getShort = callback("getShort") 5 | getShortType = type(getShort) 6 | print(getShortType) 7 | print(getShort) 8 | if(getShortType is not int): 9 | raise Exception("getShort not int!") 10 | callback("setShort", getShort) 11 | 12 | print("getShortVector") 13 | if 'getShortVector' in locals(): 14 | raise Exception("getShortVector already defined!") 15 | getShortVector = callback("getShortVector") 16 | getShortVectorType = type(getShortVector[0]) 17 | print(getShortVectorType) 18 | print(getShortVector) 19 | if(getShortVectorType is not int): 20 | raise Exception("getShortVector not int!") 21 | callback("setShortVector", getShortVector) 22 | 23 | print("getShortVectorAsList") 24 | if 'getShortVectorAsList' in locals(): 25 | raise Exception("getShortVectorAsList already defined!") 26 | getShortVectorAsList = callback("getShortVectorAsList") 27 | getShortVectorAsListType = type(getShortVectorAsList[0]) 28 | print(getShortVectorAsListType) 29 | print(getShortVectorAsList) 30 | if(getShortVectorAsListType is not int): 31 | raise Exception("getShortVectorAsList not int!") 32 | callback("setShortVectorAsList", getShortVectorAsList) 33 | 34 | print("getShortMatrix") 35 | if 'getShortMatrix' in locals(): 36 | raise Exception("getShortMatrix already defined!") 37 | getShortMatrix = callback("getShortMatrix") 38 | getShortMatrixType = type(getShortMatrix[0][0]) 39 | print(getShortMatrixType) 40 | print(getShortMatrix) 41 | if(getShortMatrixType is not int): 42 | raise Exception("getShortMatrix not int!") 43 | callback("setShortMatrix", getShortMatrix) 44 | 45 | print("getShortMatrixAsList") 46 | if 'getShortMatrixAsList' in locals(): 47 | raise Exception("getShortMatrixAsList already defined!") 48 | getShortMatrixAsList = callback("getShortMatrixAsList") 49 | getShortMatrixAsListType = type(getShortMatrixAsList[0][0]) 50 | print(getShortMatrixAsListType) 51 | print(getShortMatrixAsList) 52 | if(getShortMatrixAsListType is not int): 53 | raise Exception("getShortMatrixAsList not int!") 54 | callback("setShortMatrixAsList", getShortMatrixAsList) 55 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/callback/ParametersAndReturnsTests.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.contract.callback; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import javax.annotation.concurrent.NotThreadSafe; 7 | 8 | import de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython; 9 | import de.invesdwin.util.concurrent.Executors; 10 | import de.invesdwin.util.concurrent.WrappedExecutorService; 11 | import de.invesdwin.util.concurrent.future.Futures; 12 | 13 | @NotThreadSafe 14 | public class ParametersAndReturnsTests { 15 | 16 | private final IScriptTaskRunnerPython runner; 17 | 18 | public ParametersAndReturnsTests(final IScriptTaskRunnerPython runner) { 19 | this.runner = runner; 20 | } 21 | 22 | public void test() { 23 | new ParametersAndReturnsTestByte(runner).testByte(); 24 | new ParametersAndReturnsTestFloat(runner).testFloat(); 25 | new ParametersAndReturnsTestDouble(runner).testDouble(); 26 | new ParametersAndReturnsTestDecimal(runner).testDecimal(); 27 | new ParametersAndReturnsTestPercent(runner).testPercent(); 28 | new ParametersAndReturnsTestShort(runner).testShort(); 29 | new ParametersAndReturnsTestInteger(runner).testInteger(); 30 | new ParametersAndReturnsTestLong(runner).testLong(); 31 | new ParametersAndReturnsTestCharacter(runner).testCharacter(); 32 | new ParametersAndReturnsTestString(runner).testString(); 33 | new ParametersAndReturnsTestBoolean(runner).testBoolean(); 34 | new ParametersAndReturnsTestNullPutGet(runner).testNullPutGet(); 35 | new ParametersAndReturnsTestEmpty(runner).testEmpty(); 36 | new ParametersAndReturnsTestEmptyMatrixValue(runner).testEmptyMatrixValue(); 37 | new ParametersAndReturnsTestDoubleNan(runner).testDoubleNan(); 38 | } 39 | 40 | public void testParallel() { 41 | final List tasks = new ArrayList(); 42 | for (int i = 0; i < 10; i++) { 43 | tasks.add(new Runnable() { 44 | @Override 45 | public void run() { 46 | test(); 47 | } 48 | }); 49 | } 50 | final WrappedExecutorService executor = Executors.newFixedThreadPool( 51 | ParametersAndReturnsTests.class.getSimpleName() + "_testParallel", 52 | Runtime.getRuntime().availableProcessors()); 53 | try { 54 | Futures.submitAndWait(executor, tasks); 55 | } catch (final InterruptedException e) { 56 | throw new RuntimeException(e); 57 | } finally { 58 | executor.shutdown(); 59 | } 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/hello/HelloWorldScript.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.contract.hello; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import org.springframework.core.io.ClassPathResource; 6 | 7 | import de.invesdwin.context.integration.script.IScriptTaskEngine; 8 | import de.invesdwin.context.integration.script.IScriptTaskInputs; 9 | import de.invesdwin.context.integration.script.IScriptTaskResults; 10 | import de.invesdwin.context.python.runtime.contract.AScriptTaskPython; 11 | import de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython; 12 | import de.invesdwin.util.assertions.Assertions; 13 | 14 | @NotThreadSafe 15 | public class HelloWorldScript { 16 | 17 | private final IScriptTaskRunnerPython runner; 18 | 19 | public HelloWorldScript(final IScriptTaskRunnerPython runner) { 20 | this.runner = runner; 21 | } 22 | 23 | public void testHelloWorld() { 24 | final AScriptTaskPython script = new AScriptTaskPython() { 25 | 26 | @Override 27 | public void populateInputs(final IScriptTaskInputs inputs) { 28 | inputs.putString("hello", "World"); 29 | } 30 | 31 | @Override 32 | public void executeScript(final IScriptTaskEngine engine) { 33 | //execute this script inline: 34 | // engine.eval("world = \"Hello \" + hello + \"!\""); 35 | //or run it from a file: 36 | engine.eval(new ClassPathResource(HelloWorldScript.class.getSimpleName() + ".py", getClass())); 37 | } 38 | 39 | @Override 40 | public String extractResults(final IScriptTaskResults results) { 41 | return results.getString("world"); 42 | } 43 | }; 44 | final String result = script.run(runner); 45 | Assertions.assertThat(result).isEqualTo("Hello World!"); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-contract/src/test/java/de/invesdwin/context/python/runtime/contract/hello/HelloWorldScript.py: -------------------------------------------------------------------------------- 1 | world = "Hello " + hello + "!" -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-graalpy/.gitignore: -------------------------------------------------------------------------------- 1 | /.checkstyle 2 | /.classpath 3 | /log 4 | /target 5 | /activemq-data 6 | /.fbprefs 7 | /cache 8 | /.settings 9 | /.project 10 | /*.hprof 11 | /.jrctemp 12 | /.invesdwin 13 | /nbproject 14 | /bin 15 | /.springBeans 16 | /.factorypath 17 | /.attach_pid* 18 | /**/.DS_Store 19 | /.idea 20 | /**/*.iml 21 | /.sessions 22 | /**/.~lock.* 23 | /.pydevproject 24 | /Report_*.java 25 | /octave-workspace 26 | /hs_err_pid*.log 27 | /*.Rout 28 | /.apt* 29 | /ajcore.*.txt 30 | /.temp-*.txt 31 | /dependency-reduced-pom.xml 32 | /META-INF 33 | /.Rproj.user 34 | /.Rhistory 35 | /.RData 36 | /.Ruserdata 37 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-graalpy/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | 6 | de.invesdwin 7 | invesdwin-context-python-parent 8 | 1.0.4-SNAPSHOT 9 | 10 | invesdwin-context-python-runtime-graalpy 11 | jar 12 | 13 | 14 | 15 | de.invesdwin 16 | invesdwin-context-python-runtime-contract 17 | 18 | 19 | de.invesdwin 20 | invesdwin-context-python-runtime-contract 21 | test-jar 22 | test 23 | 24 | 25 | 26 | de.invesdwin 27 | invesdwin-context-graalvm 28 | 29 | 30 | org.graalvm.polyglot 31 | python 32 | pom 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-graalpy/src/main/java/META-INF/de.invesdwin.context.python.runtime.graalpy.properties: -------------------------------------------------------------------------------- 1 | # you can uncomment the following line and point it to a GraalPy virtual environment to add custom packages to the runtime 2 | #de.invesdwin.context.python.runtime.graalpy.GraalpyProperties.PYTHON_COMMAND=/bin/python -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-graalpy/src/main/java/META-INF/services/de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython: -------------------------------------------------------------------------------- 1 | de.invesdwin.context.python.runtime.graalpy.GraalpyScriptTaskRunnerPython -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-graalpy/src/main/java/META-INF/services/javax.script.ScriptEngineFactory: -------------------------------------------------------------------------------- 1 | de.invesdwin.context.python.runtime.graalpy.jsr223.GraalpyScriptEngineFactory -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-graalpy/src/main/java/de/invesdwin/context/python/runtime/graalpy/GraalpyProperties.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.graalpy; 2 | 3 | import javax.annotation.concurrent.ThreadSafe; 4 | 5 | import de.invesdwin.context.system.properties.SystemProperties; 6 | 7 | @ThreadSafe 8 | public final class GraalpyProperties { 9 | 10 | public static final String PYTHON_COMMAND; 11 | 12 | static { 13 | 14 | final SystemProperties systemProperties = new SystemProperties(GraalpyProperties.class); 15 | if (systemProperties.containsValue("PYTHON_COMMAND")) { 16 | PYTHON_COMMAND = systemProperties.getString("PYTHON_COMMAND"); 17 | } else { 18 | PYTHON_COMMAND = null; 19 | } 20 | } 21 | 22 | private GraalpyProperties() {} 23 | 24 | } 25 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-graalpy/src/main/java/de/invesdwin/context/python/runtime/graalpy/GraalpyScriptTaskCallbackContext.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.graalpy; 2 | 3 | import java.io.Closeable; 4 | import java.util.Map; 5 | import java.util.concurrent.ConcurrentHashMap; 6 | 7 | import javax.annotation.concurrent.ThreadSafe; 8 | 9 | import org.springframework.core.io.ClassPathResource; 10 | 11 | import de.invesdwin.context.integration.script.callback.IScriptTaskCallback; 12 | import de.invesdwin.context.integration.script.callback.ObjectScriptTaskParameters; 13 | import de.invesdwin.context.integration.script.callback.ObjectScriptTaskParametersPool; 14 | import de.invesdwin.context.integration.script.callback.ObjectScriptTaskReturnValue; 15 | import de.invesdwin.context.integration.script.callback.ObjectScriptTaskReturns; 16 | import de.invesdwin.context.integration.script.callback.ObjectScriptTaskReturnsPool; 17 | import de.invesdwin.util.lang.UUIDs; 18 | 19 | @ThreadSafe 20 | public class GraalpyScriptTaskCallbackContext implements Closeable { 21 | 22 | private static final Map UUID_CONTEXT = new ConcurrentHashMap<>(); 23 | 24 | private final String uuid; 25 | private final IScriptTaskCallback callback; 26 | 27 | public GraalpyScriptTaskCallbackContext(final IScriptTaskCallback callback) { 28 | this.uuid = UUIDs.newPseudoRandomUUID(); 29 | this.callback = callback; 30 | UUID_CONTEXT.put(uuid, this); 31 | } 32 | 33 | public static GraalpyScriptTaskCallbackContext getContext(final String uuid) { 34 | return UUID_CONTEXT.get(uuid); 35 | } 36 | 37 | public void init(final GraalpyScriptTaskEnginePython engine) { 38 | engine.getInputs().putString("graalpyScriptTaskCallbackContextUuid", getUuid()); 39 | engine.eval(new ClassPathResource(GraalpyScriptTaskCallbackContext.class.getSimpleName() + ".py", 40 | GraalpyScriptTaskCallbackContext.class)); 41 | } 42 | 43 | public String getUuid() { 44 | return uuid; 45 | } 46 | 47 | public ObjectScriptTaskReturnValue invoke(final String methodName, final Object... args) { 48 | final ObjectScriptTaskParameters parameters = ObjectScriptTaskParametersPool.INSTANCE.borrowObject(); 49 | final ObjectScriptTaskReturns returns = ObjectScriptTaskReturnsPool.INSTANCE.borrowObject(); 50 | try { 51 | parameters.setParameters(args); 52 | callback.invoke(methodName, parameters, returns); 53 | return returns.newReturn(); 54 | } finally { 55 | ObjectScriptTaskReturnsPool.INSTANCE.returnObject(returns); 56 | ObjectScriptTaskParametersPool.INSTANCE.returnObject(parameters); 57 | } 58 | } 59 | 60 | @Override 61 | public void close() { 62 | UUID_CONTEXT.remove(uuid); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-graalpy/src/main/java/de/invesdwin/context/python/runtime/graalpy/GraalpyScriptTaskCallbackContext.py: -------------------------------------------------------------------------------- 1 | from java.de.invesdwin.context.python.runtime.graalpy import GraalpyScriptTaskCallbackContext 2 | 3 | def callback(methodName, *parameters): 4 | if 'graalpyScriptTaskCallbackContext' not in globals(): 5 | if 'graalpyScriptTaskCallbackContextUuid' in locals() or 'graalpyScriptTaskCallbackContextUuid' in globals(): 6 | global graalpyScriptTaskCallbackContext 7 | graalpyScriptTaskCallbackContext = GraalpyScriptTaskCallbackContext.getContext(graalpyScriptTaskCallbackContextUuid) 8 | else: 9 | raise Exception("IScriptTaskCallback not available") 10 | returnValue = graalpyScriptTaskCallbackContext.invoke(methodName, parameters) 11 | if returnValue.isReturnExpression(): 12 | returnExpression = returnValue.getReturnValue() 13 | if returnExpression.startswith("raise "): 14 | exec(returnExpression, globals()) 15 | # fallback 16 | raise Exception(returnExpression) 17 | else: 18 | returnExpressionLines = returnExpression.splitlines() 19 | returnExpressionLinesLength = len(returnExpressionLines) 20 | if(returnExpressionLinesLength > 1): 21 | returnExpressionLinesLastElement = returnExpressionLinesLength-1 22 | returnExpressionExec = "\n".join(returnExpressionLines[:returnExpressionLinesLastElement]) 23 | returnExpressionEval = returnExpressionLines[returnExpressionLinesLastElement] 24 | loc = {} 25 | exec(returnExpressionExec, globals(), loc) 26 | return eval(returnExpressionEval, globals(), loc) 27 | else: 28 | return eval(returnExpression, globals()) 29 | else: 30 | return returnValue.getReturnValue() 31 | 32 | 33 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-graalpy/src/main/java/de/invesdwin/context/python/runtime/graalpy/GraalpyScriptTaskEnginePython.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.graalpy; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | import javax.script.ScriptException; 5 | 6 | import de.invesdwin.context.graalvm.jsr223.PolyglotScriptEngine; 7 | import de.invesdwin.context.integration.script.IScriptTaskEngine; 8 | import de.invesdwin.context.python.runtime.graalpy.pool.GraalpyScriptEngineObjectPool; 9 | import de.invesdwin.util.concurrent.WrappedExecutorService; 10 | import de.invesdwin.util.concurrent.lock.ILock; 11 | import de.invesdwin.util.concurrent.lock.disabled.DisabledLock; 12 | 13 | @NotThreadSafe 14 | public class GraalpyScriptTaskEnginePython implements IScriptTaskEngine { 15 | 16 | private PolyglotScriptEngine pyScriptEngine; 17 | private final GraalpyScriptTaskInputsPython inputs; 18 | private final GraalpyScriptTaskResultsPython results; 19 | 20 | public GraalpyScriptTaskEnginePython(final PolyglotScriptEngine pyScriptEngine) { 21 | this.pyScriptEngine = pyScriptEngine; 22 | this.inputs = new GraalpyScriptTaskInputsPython(this); 23 | this.results = new GraalpyScriptTaskResultsPython(this); 24 | } 25 | 26 | /** 27 | * https://github.com/mrj0/pyScriptEngine/issues/55 28 | */ 29 | @Override 30 | public void eval(final String expression) { 31 | try { 32 | pyScriptEngine.eval(expression); 33 | } catch (final ScriptException e) { 34 | throw new RuntimeException(e); 35 | } 36 | } 37 | 38 | @Override 39 | public GraalpyScriptTaskInputsPython getInputs() { 40 | return inputs; 41 | } 42 | 43 | @Override 44 | public GraalpyScriptTaskResultsPython getResults() { 45 | return results; 46 | } 47 | 48 | @Override 49 | public void close() { 50 | pyScriptEngine = null; 51 | } 52 | 53 | @Override 54 | public PolyglotScriptEngine unwrap() { 55 | return pyScriptEngine; 56 | } 57 | 58 | /** 59 | * Each instance has its own engine, so no shared locking required. 60 | */ 61 | @Override 62 | public ILock getSharedLock() { 63 | return DisabledLock.INSTANCE; 64 | } 65 | 66 | @Override 67 | public WrappedExecutorService getSharedExecutor() { 68 | return null; 69 | } 70 | 71 | public static GraalpyScriptTaskEnginePython newInstance() { 72 | return new GraalpyScriptTaskEnginePython(GraalpyScriptEngineObjectPool.INSTANCE.borrowObject()) { 73 | @Override 74 | public void close() { 75 | final PolyglotScriptEngine unwrap = unwrap(); 76 | if (unwrap != null) { 77 | GraalpyScriptEngineObjectPool.INSTANCE.returnObject(unwrap); 78 | } 79 | super.close(); 80 | } 81 | }; 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-graalpy/src/main/java/de/invesdwin/context/python/runtime/graalpy/GraalpyScriptTaskRunnerPython.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.graalpy; 2 | 3 | import javax.annotation.concurrent.Immutable; 4 | 5 | import org.springframework.beans.factory.FactoryBean; 6 | 7 | import de.invesdwin.context.graalvm.jsr223.PolyglotScriptEngine; 8 | import de.invesdwin.context.integration.script.callback.IScriptTaskCallback; 9 | import de.invesdwin.context.integration.script.callback.LoggingDelegateScriptTaskCallback; 10 | import de.invesdwin.context.python.runtime.contract.AScriptTaskPython; 11 | import de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython; 12 | import de.invesdwin.context.python.runtime.graalpy.pool.GraalpyScriptEngineObjectPool; 13 | import de.invesdwin.util.error.Throwables; 14 | import jakarta.inject.Named; 15 | 16 | @Immutable 17 | @Named 18 | public final class GraalpyScriptTaskRunnerPython 19 | implements IScriptTaskRunnerPython, FactoryBean { 20 | 21 | public static final String INTERNAL_RESULT_VARIABLE = GraalpyScriptTaskRunnerPython.class.getSimpleName() 22 | + "_result"; 23 | public static final GraalpyScriptTaskRunnerPython INSTANCE = new GraalpyScriptTaskRunnerPython(); 24 | 25 | /** 26 | * public for ServiceLoader support 27 | */ 28 | public GraalpyScriptTaskRunnerPython() {} 29 | 30 | @Override 31 | public T run(final AScriptTaskPython scriptTask) { 32 | //get session 33 | final PolyglotScriptEngine pyScriptEngine = GraalpyScriptEngineObjectPool.INSTANCE.borrowObject(); 34 | final IScriptTaskCallback callback = scriptTask.getCallback(); 35 | final GraalpyScriptTaskCallbackContext context; 36 | if (callback != null) { 37 | context = new GraalpyScriptTaskCallbackContext(LoggingDelegateScriptTaskCallback.maybeWrap(LOG, callback)); 38 | } else { 39 | context = null; 40 | } 41 | try { 42 | //inputs 43 | final GraalpyScriptTaskEnginePython engine = new GraalpyScriptTaskEnginePython(pyScriptEngine); 44 | if (context != null) { 45 | context.init(engine); 46 | } 47 | scriptTask.populateInputs(engine.getInputs()); 48 | 49 | //execute 50 | scriptTask.executeScript(engine); 51 | 52 | //results 53 | final T result = scriptTask.extractResults(engine.getResults()); 54 | engine.close(); 55 | 56 | //return 57 | GraalpyScriptEngineObjectPool.INSTANCE.returnObject(pyScriptEngine); 58 | return result; 59 | } catch (final Throwable t) { 60 | GraalpyScriptEngineObjectPool.INSTANCE.invalidateObject(pyScriptEngine); 61 | throw Throwables.propagate(t); 62 | } finally { 63 | if (context != null) { 64 | context.close(); 65 | } 66 | } 67 | } 68 | 69 | @Override 70 | public GraalpyScriptTaskRunnerPython getObject() throws Exception { 71 | return INSTANCE; 72 | } 73 | 74 | @Override 75 | public Class getObjectType() { 76 | return GraalpyScriptTaskRunnerPython.class; 77 | } 78 | 79 | @Override 80 | public boolean isSingleton() { 81 | return true; 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-graalpy/src/main/java/de/invesdwin/context/python/runtime/graalpy/jsr223/GraalpyScriptEngineFactory.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.graalpy.jsr223; 2 | 3 | import javax.annotation.concurrent.Immutable; 4 | 5 | import org.graalvm.polyglot.Context.Builder; 6 | 7 | import de.invesdwin.context.graalvm.jsr223.PolyglotScriptEngineFactory; 8 | import de.invesdwin.context.python.runtime.graalpy.GraalpyProperties; 9 | 10 | @Immutable 11 | public final class GraalpyScriptEngineFactory extends PolyglotScriptEngineFactory { 12 | 13 | public static final GraalpyScriptEngineFactory INSTANCE = new GraalpyScriptEngineFactory(); 14 | 15 | public GraalpyScriptEngineFactory() { 16 | super("python"); 17 | } 18 | 19 | /** 20 | * Support for using GraalPy venv: 21 | * https://github.com/graalvm/graal-languages-demos/blob/main/graalpy/graalpy-custom-venv-guide/README.md 22 | * 23 | * The venv could be packaged inside the jar and extracted on application launch, then the PYTHON_COMMAND system 24 | * property can be set automatically to point to the venv. This should suffice for embedding python modules and 25 | * matches the approach of python4j that also allows 26 | * 27 | * We currently do not support python-embedding with graalpy-maven-plugin but could do so in the future based on: 28 | * https://github.com/graalvm/graal-languages-demos/tree/main/graalpy/graalpy-javase-guide 29 | * 30 | * For now zipping a venv as described above should suffice. 31 | */ 32 | @Override 33 | public Builder customizeContextBuilder(final Builder builder) { 34 | if (GraalpyProperties.PYTHON_COMMAND != null) { 35 | builder.option("python.Executable", GraalpyProperties.PYTHON_COMMAND); 36 | builder.option("python.ForceImportSite", "true"); 37 | } 38 | return super.customizeContextBuilder(builder); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-graalpy/src/main/java/de/invesdwin/context/python/runtime/graalpy/pool/GraalpyScriptEngineObjectPool.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.graalpy.pool; 2 | 3 | import java.io.OutputStreamWriter; 4 | 5 | import javax.annotation.concurrent.ThreadSafe; 6 | import javax.script.ScriptException; 7 | 8 | import org.springframework.beans.factory.FactoryBean; 9 | import org.zeroturnaround.exec.stream.slf4j.Slf4jDebugOutputStream; 10 | import org.zeroturnaround.exec.stream.slf4j.Slf4jWarnOutputStream; 11 | 12 | import de.invesdwin.context.graalvm.jsr223.PolyglotScriptEngine; 13 | import de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython; 14 | import de.invesdwin.context.python.runtime.graalpy.jsr223.GraalpyScriptEngineFactory; 15 | import de.invesdwin.util.concurrent.pool.timeout.ATimeoutObjectPool; 16 | import de.invesdwin.util.time.date.FTimeUnit; 17 | import de.invesdwin.util.time.duration.Duration; 18 | import jakarta.inject.Named; 19 | 20 | @ThreadSafe 21 | @Named 22 | public final class GraalpyScriptEngineObjectPool extends ATimeoutObjectPool 23 | implements FactoryBean { 24 | 25 | public static final GraalpyScriptEngineObjectPool INSTANCE = new GraalpyScriptEngineObjectPool(); 26 | 27 | private GraalpyScriptEngineObjectPool() { 28 | super(Duration.ONE_MINUTE, new Duration(10, FTimeUnit.SECONDS)); 29 | } 30 | 31 | @Override 32 | public void invalidateObject(final PolyglotScriptEngine element) { 33 | element.close(); 34 | } 35 | 36 | @Override 37 | protected PolyglotScriptEngine newObject() { 38 | final PolyglotScriptEngine engine = GraalpyScriptEngineFactory.INSTANCE.getScriptEngine(); 39 | engine.getContext().setWriter(new OutputStreamWriter(new Slf4jDebugOutputStream(IScriptTaskRunnerPython.LOG))); 40 | engine.getContext() 41 | .setErrorWriter(new OutputStreamWriter(new Slf4jWarnOutputStream(IScriptTaskRunnerPython.LOG))); 42 | return engine; 43 | } 44 | 45 | /** 46 | * https://github.com/mrj0/jep/wiki/Performance-Considerations 47 | * 48 | * https://github.com/spyder-ide/spyder/issues/2563 49 | * 50 | * http://stackoverflow.com/questions/3543833/how-do-i-clear-all-variables-in-the-middle-of-a-python-script 51 | */ 52 | @Override 53 | protected boolean passivateObject(final PolyglotScriptEngine element) { 54 | try { 55 | element.eval(IScriptTaskRunnerPython.CLEANUP_SCRIPT); 56 | return true; 57 | } catch (final ScriptException e) { 58 | throw new RuntimeException(e); 59 | } 60 | } 61 | 62 | @Override 63 | public GraalpyScriptEngineObjectPool getObject() { 64 | return INSTANCE; 65 | } 66 | 67 | @Override 68 | public Class getObjectType() { 69 | return GraalpyScriptEngineObjectPool.class; 70 | } 71 | 72 | @Override 73 | public boolean isSingleton() { 74 | return true; 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-graalpy/src/test/java/de/invesdwin/context/python/runtime/graalpy/GraalpyScriptTaskRunnerPythonTest.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.graalpy; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | import de.invesdwin.context.python.runtime.contract.InputsAndResultsTests; 8 | import de.invesdwin.context.python.runtime.contract.callback.ParametersAndReturnsTests; 9 | import de.invesdwin.context.test.ATest; 10 | import jakarta.inject.Inject; 11 | 12 | @NotThreadSafe 13 | public class GraalpyScriptTaskRunnerPythonTest extends ATest { 14 | 15 | @Inject 16 | private GraalpyScriptTaskRunnerPython runner; 17 | 18 | @Test 19 | public void test() { 20 | new InputsAndResultsTests(runner).test(); 21 | } 22 | 23 | @Test 24 | public void testParallel() { 25 | new InputsAndResultsTests(runner).testParallel(); 26 | } 27 | 28 | @Test 29 | public void testCallback() { 30 | new ParametersAndReturnsTests(runner).test(); 31 | } 32 | 33 | @Test 34 | public void testCallbackParallel() { 35 | new ParametersAndReturnsTests(runner).testParallel(); 36 | } 37 | 38 | @Test 39 | public void testSimpleCallback() { 40 | new SimpleCallbackTest(runner).testSimpleCallback(); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-graalpy/src/test/java/de/invesdwin/context/python/runtime/graalpy/SimpleCallbackTest.py: -------------------------------------------------------------------------------- 1 | print("putUuid") 2 | print(putUuid) 3 | 4 | from java.de.invesdwin.context.python.runtime.graalpy import SimpleCallbackTest 5 | getSecretStaticImport = SimpleCallbackTest.getSecretStatic(putUuid) 6 | print("getSecretStaticImport") 7 | print(getSecretStaticImport) 8 | 9 | getSecretStaticCallback = callback("getSecretStatic", putUuid) 10 | print("getSecretStaticCallback") 11 | print(getSecretStaticCallback) 12 | 13 | getSecretCallback = callback("getSecret", putUuid) 14 | print("getSecretCallback") 15 | print(getSecretCallback) 16 | 17 | getSecretExpressionCallback = callback("getSecretExpression", putUuid) 18 | print("getSecretExpressionCallback") 19 | print(getSecretExpressionCallback) 20 | 21 | callback("voidMethod") 22 | 23 | callManyParams = callback("callManyParams", True, 2, 3, '4', 5, 6, 7.0, 8.0, "123456789", 10.0) 24 | if(callManyParams != 55): 25 | raise Exception("callManyParams unexpected result: "+callManyParams) 26 | callManyParamsExpression = callback("callManyParamsExpression", True, 2, 3, '4', 5, 6, 7.0, 8.0, "123456789", 10.0) 27 | if(callManyParamsExpression != 55): 28 | raise Exception("callManyParamsExpression unexpected result: "+callManyParamsExpression) 29 | callManyParamsExpressionMultiline = callback("callManyParamsExpressionMultiline", True, 2, 3, '4', 5, 6, 7.0, 8.0, "123456789", 10.0) 30 | if(callManyParamsExpressionMultiline != 55): 31 | raise Exception("callManyParamsExpressionMultiline unexpected result: "+callManyParamsExpressionMultiline) 32 | 33 | getManyParamsExpression = putManyParamsExpression 34 | print("getManyParamsExpression") 35 | print(getManyParamsExpression) 36 | getManyParamsExpressionMultilineWrong = putManyParamsExpressionMultilineWrong 37 | print("getManyParamsExpressionMultilineWrong") 38 | print(getManyParamsExpressionMultilineWrong) 39 | getManyParamsExpressionMultiline = putManyParamsExpressionMultiline 40 | print("getManyParamsExpressionMultiline") 41 | print(getManyParamsExpressionMultiline) 42 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-japyb/.gitignore: -------------------------------------------------------------------------------- 1 | /.checkstyle 2 | /.classpath 3 | /log 4 | /target 5 | /activemq-data 6 | /.fbprefs 7 | /cache 8 | /.settings 9 | /.project 10 | /*.hprof 11 | /.jrctemp 12 | /.invesdwin 13 | /nbproject 14 | /bin 15 | /.springBeans 16 | /.factorypath 17 | /.attach_pid* 18 | /**/.DS_Store 19 | /.idea 20 | /**/*.iml 21 | /.sessions 22 | /**/.~lock.* 23 | /.pydevproject 24 | /Report_*.java 25 | /octave-workspace 26 | /hs_err_pid*.log 27 | /*.Rout 28 | /.apt* 29 | /ajcore.*.txt 30 | /.temp-*.txt 31 | /dependency-reduced-pom.xml 32 | /META-INF 33 | /.Rproj.user 34 | /.Rhistory 35 | /.RData 36 | /.Ruserdata 37 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-japyb/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | 6 | de.invesdwin 7 | invesdwin-context-python-parent 8 | 1.0.4-SNAPSHOT 9 | 10 | invesdwin-context-python-runtime-japyb 11 | jar 12 | 13 | 14 | 15 | de.invesdwin 16 | invesdwin-context-python-runtime-contract 17 | 18 | 19 | de.invesdwin 20 | invesdwin-context-python-runtime-contract 21 | test-jar 22 | test 23 | 24 | 25 | de.invesdwin 26 | invesdwin-context-integration-channel 27 | 28 | 29 | io.netty 30 | netty-handler 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-japyb/src/main/java/META-INF/de.invesdwin.context.python.runtime.japyb.properties: -------------------------------------------------------------------------------- 1 | # you can switch to "python3" here for example or provide a different command entirely (e.g. "pypy") 2 | de.invesdwin.context.python.runtime.graalpy.GraalpyProperties.PYTHON_COMMAND=python3 -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-japyb/src/main/java/META-INF/services/de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython: -------------------------------------------------------------------------------- 1 | de.invesdwin.context.python.runtime.japyb.JapybScriptTaskRunnerPython -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-japyb/src/main/java/de/invesdwin/context/python/runtime/japyb/JapybProperties.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.japyb; 2 | 3 | import javax.annotation.concurrent.ThreadSafe; 4 | 5 | import de.invesdwin.context.system.properties.SystemProperties; 6 | 7 | @ThreadSafe 8 | public final class JapybProperties { 9 | 10 | public static final String PYTHON_COMMAND; 11 | 12 | static { 13 | 14 | final SystemProperties systemProperties = new SystemProperties(JapybProperties.class); 15 | if (systemProperties.containsValue("PYTHON_COMMAND")) { 16 | PYTHON_COMMAND = systemProperties.getString("PYTHON_COMMAND"); 17 | } else { 18 | PYTHON_COMMAND = null; 19 | } 20 | } 21 | 22 | private JapybProperties() {} 23 | 24 | } 25 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-japyb/src/main/java/de/invesdwin/context/python/runtime/japyb/JapybScriptTaskEnginePython.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.japyb; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import de.invesdwin.context.integration.script.IScriptTaskEngine; 6 | import de.invesdwin.context.python.runtime.japyb.pool.ExtendedPythonBridge; 7 | import de.invesdwin.context.python.runtime.japyb.pool.JapybObjectPool; 8 | import de.invesdwin.util.concurrent.WrappedExecutorService; 9 | import de.invesdwin.util.concurrent.lock.ILock; 10 | import de.invesdwin.util.concurrent.lock.disabled.DisabledLock; 11 | 12 | @NotThreadSafe 13 | public class JapybScriptTaskEnginePython implements IScriptTaskEngine { 14 | 15 | private ExtendedPythonBridge bridge; 16 | private final JapybScriptTaskInputsPython inputs; 17 | private final JapybScriptTaskResultsPython results; 18 | 19 | public JapybScriptTaskEnginePython(final ExtendedPythonBridge bridge) { 20 | this.bridge = bridge; 21 | this.inputs = new JapybScriptTaskInputsPython(this); 22 | this.results = new JapybScriptTaskResultsPython(this); 23 | } 24 | 25 | @Override 26 | public void eval(final String expression) { 27 | bridge.eval(expression); 28 | } 29 | 30 | @Override 31 | public JapybScriptTaskInputsPython getInputs() { 32 | return inputs; 33 | } 34 | 35 | @Override 36 | public JapybScriptTaskResultsPython getResults() { 37 | return results; 38 | } 39 | 40 | @Override 41 | public void close() { 42 | bridge = null; 43 | } 44 | 45 | @Override 46 | public ExtendedPythonBridge unwrap() { 47 | return bridge; 48 | } 49 | 50 | /** 51 | * Each instance has its own engine, so no shared locking required. 52 | */ 53 | @Override 54 | public ILock getSharedLock() { 55 | return DisabledLock.INSTANCE; 56 | } 57 | 58 | /** 59 | * No executor needed. 60 | */ 61 | @Override 62 | public WrappedExecutorService getSharedExecutor() { 63 | return null; 64 | } 65 | 66 | public static JapybScriptTaskEnginePython newInstance() { 67 | return new JapybScriptTaskEnginePython(JapybObjectPool.INSTANCE.borrowObject()) { 68 | @Override 69 | public void close() { 70 | final ExtendedPythonBridge unwrap = unwrap(); 71 | if (unwrap != null) { 72 | JapybObjectPool.INSTANCE.returnObject(unwrap); 73 | } 74 | super.close(); 75 | } 76 | }; 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-japyb/src/main/java/de/invesdwin/context/python/runtime/japyb/JapybScriptTaskInputsPython.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.japyb; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import de.invesdwin.context.python.runtime.contract.AScriptTaskInputsPythonToExpression; 6 | 7 | @NotThreadSafe 8 | public class JapybScriptTaskInputsPython extends AScriptTaskInputsPythonToExpression { 9 | 10 | private final JapybScriptTaskEnginePython engine; 11 | 12 | public JapybScriptTaskInputsPython(final JapybScriptTaskEnginePython engine) { 13 | this.engine = engine; 14 | } 15 | 16 | @Override 17 | public JapybScriptTaskEnginePython getEngine() { 18 | return engine; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-japyb/src/main/java/de/invesdwin/context/python/runtime/japyb/JapybScriptTaskResultsPython.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.japyb; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import com.fasterxml.jackson.databind.JsonNode; 6 | 7 | import de.invesdwin.context.python.runtime.contract.AScriptTaskResultsPythonFromJson; 8 | 9 | @NotThreadSafe 10 | public class JapybScriptTaskResultsPython extends AScriptTaskResultsPythonFromJson { 11 | 12 | private final JapybScriptTaskEnginePython engine; 13 | 14 | public JapybScriptTaskResultsPython(final JapybScriptTaskEnginePython engine) { 15 | this.engine = engine; 16 | } 17 | 18 | @Override 19 | public JapybScriptTaskEnginePython getEngine() { 20 | return engine; 21 | } 22 | 23 | @Override 24 | protected JsonNode getAsJsonNode(final String variable) { 25 | return engine.unwrap().getAsJsonNode(variable); 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-japyb/src/main/java/de/invesdwin/context/python/runtime/japyb/pool/ExtendedPythonBridge.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.japyb.pool; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.annotation.concurrent.NotThreadSafe; 6 | 7 | import de.invesdwin.context.python.runtime.japyb.JapybScriptTaskEnginePython; 8 | 9 | @NotThreadSafe 10 | public class ExtendedPythonBridge extends ModifiedPythonBridge { 11 | 12 | private final PythonResetContext resetContext; 13 | 14 | public ExtendedPythonBridge() { 15 | super(); 16 | this.resetContext = new PythonResetContext(new JapybScriptTaskEnginePython(this)); 17 | } 18 | 19 | @Override 20 | public void open() throws IOException { 21 | super.open(); 22 | resetContext.init(); 23 | } 24 | 25 | public void reset() throws IOException { 26 | getErrWatcher().clearLog(); 27 | resetContext.reset(); 28 | getErrWatcher().clearLog(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-japyb/src/main/java/de/invesdwin/context/python/runtime/japyb/pool/JapybObjectPool.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.japyb.pool; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.annotation.concurrent.ThreadSafe; 6 | 7 | import org.springframework.beans.factory.FactoryBean; 8 | 9 | import de.invesdwin.util.concurrent.pool.timeout.ATimeoutObjectPool; 10 | import de.invesdwin.util.time.date.FTimeUnit; 11 | import de.invesdwin.util.time.duration.Duration; 12 | import jakarta.inject.Named; 13 | 14 | @ThreadSafe 15 | @Named 16 | public final class JapybObjectPool extends ATimeoutObjectPool 17 | implements FactoryBean { 18 | 19 | public static final JapybObjectPool INSTANCE = new JapybObjectPool(); 20 | 21 | private JapybObjectPool() { 22 | //julia compilation is a lot of overhead, thus keep instances open longer 23 | super(new Duration(10, FTimeUnit.MINUTES), new Duration(10, FTimeUnit.SECONDS)); 24 | } 25 | 26 | @Override 27 | public void invalidateObject(final ExtendedPythonBridge element) { 28 | element.close(); 29 | } 30 | 31 | @Override 32 | protected ExtendedPythonBridge newObject() { 33 | final ExtendedPythonBridge session = new ExtendedPythonBridge(); 34 | try { 35 | session.open(); 36 | } catch (final IOException e) { 37 | throw new RuntimeException(e); 38 | } 39 | return session; 40 | } 41 | 42 | @Override 43 | protected boolean passivateObject(final ExtendedPythonBridge element) { 44 | try { 45 | element.reset(); 46 | return true; 47 | } catch (final IOException e) { 48 | throw new RuntimeException(e); 49 | } 50 | } 51 | 52 | @Override 53 | public JapybObjectPool getObject() throws Exception { 54 | return INSTANCE; 55 | } 56 | 57 | @Override 58 | public Class getObjectType() { 59 | return JapybObjectPool.class; 60 | } 61 | 62 | @Override 63 | public boolean isSingleton() { 64 | return true; 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-japyb/src/main/java/de/invesdwin/context/python/runtime/japyb/pool/PythonResetContext.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.japyb.pool; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import org.springframework.core.io.ClassPathResource; 6 | 7 | import de.invesdwin.context.integration.script.IScriptTaskEngine; 8 | 9 | @NotThreadSafe 10 | public class PythonResetContext { 11 | 12 | private final IScriptTaskEngine engine; 13 | 14 | public PythonResetContext(final IScriptTaskEngine engine) { 15 | this.engine = engine; 16 | } 17 | 18 | public void init() { 19 | engine.eval(new ClassPathResource(PythonResetContext.class.getSimpleName() + ".py", PythonResetContext.class)); 20 | } 21 | 22 | public void reset() { 23 | engine.eval("restoreContext()"); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-japyb/src/main/java/de/invesdwin/context/python/runtime/japyb/pool/PythonResetContext.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | # http://stackoverflow.com/questions/3543833/how-do-i-clear-all-variables-in-the-middle-of-a-python-script 4 | __saved_context__ = {} 5 | 6 | def saveContext(): 7 | __saved_context__.update(sys.modules[__name__].__dict__) 8 | 9 | def restoreContext(): 10 | names = list(sys.modules[__name__].__dict__.keys()) 11 | for n in names: 12 | if n not in __saved_context__: 13 | del sys.modules[__name__].__dict__[n] 14 | 15 | saveContext() -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-japyb/src/test/java/de/invesdwin/context/python/runtime/japyb/JapybScriptTaskRunnerPythonTest.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.japyb; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | import de.invesdwin.context.python.runtime.contract.InputsAndResultsTests; 8 | import de.invesdwin.context.python.runtime.contract.callback.ParametersAndReturnsTests; 9 | import de.invesdwin.context.test.ATest; 10 | import jakarta.inject.Inject; 11 | 12 | @NotThreadSafe 13 | public class JapybScriptTaskRunnerPythonTest extends ATest { 14 | 15 | @Inject 16 | private JapybScriptTaskRunnerPython runner; 17 | 18 | @Test 19 | public void test() { 20 | new InputsAndResultsTests(runner).test(); 21 | } 22 | 23 | @Test 24 | public void testParallel() { 25 | new InputsAndResultsTests(runner).testParallel(); 26 | } 27 | 28 | @Test 29 | public void testCallback() { 30 | new ParametersAndReturnsTests(runner).test(); 31 | } 32 | 33 | @Test 34 | public void testCallbackParallel() { 35 | new ParametersAndReturnsTests(runner).testParallel(); 36 | } 37 | 38 | @Test 39 | public void testSimpleCallback() { 40 | new SimpleCallbackTest(runner).testSimpleCallback(); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-japyb/src/test/java/de/invesdwin/context/python/runtime/japyb/SimpleCallbackTest.py: -------------------------------------------------------------------------------- 1 | print("putUuid") 2 | print(putUuid) 3 | 4 | getSecretStaticCallback = callback("getSecretStatic", putUuid) 5 | print("getSecretStaticCallback") 6 | print(getSecretStaticCallback) 7 | 8 | getSecretCallback = callback("getSecret", putUuid) 9 | print("getSecretCallback") 10 | print(getSecretCallback) 11 | 12 | getSecretExpressionCallback = callback("getSecretExpression", putUuid) 13 | print("getSecretExpressionCallback") 14 | print(getSecretExpressionCallback) 15 | 16 | callback("voidMethod") 17 | 18 | callManyParams = callback("callManyParams", True, 2, 3, '4', 5, 6, 7.0, 8.0, "123456789", 10.0) 19 | if(callManyParams != 55): 20 | raise Exception("callManyParams unexpected result: "+callManyParams) 21 | callManyParamsExpression = callback("callManyParamsExpression", True, 2, 3, '4', 5, 6, 7.0, 8.0, "123456789", 10.0) 22 | if(callManyParamsExpression != 55): 23 | raise Exception("callManyParamsExpression unexpected result: "+callManyParamsExpression) 24 | callManyParamsExpressionMultiline = callback("callManyParamsExpressionMultiline", True, 2, 3, '4', 5, 6, 7.0, 8.0, "123456789", 10.0) 25 | if(callManyParamsExpressionMultiline != 55): 26 | raise Exception("callManyParamsExpressionMultiline unexpected result: "+callManyParamsExpressionMultiline) 27 | 28 | getManyParamsExpression = putManyParamsExpression 29 | print("getManyParamsExpression") 30 | print(getManyParamsExpression) 31 | getManyParamsExpressionMultilineWrong = putManyParamsExpressionMultilineWrong 32 | print("getManyParamsExpressionMultilineWrong") 33 | print(getManyParamsExpressionMultilineWrong) 34 | getManyParamsExpressionMultiline = putManyParamsExpressionMultiline 35 | print("getManyParamsExpressionMultiline") 36 | print(getManyParamsExpressionMultiline) 37 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-jep/.gitignore: -------------------------------------------------------------------------------- 1 | /.checkstyle 2 | /.classpath 3 | /log 4 | /target 5 | /activemq-data 6 | /.fbprefs 7 | /cache 8 | /.settings 9 | /.project 10 | /*.hprof 11 | /.jrctemp 12 | /.invesdwin 13 | /nbproject 14 | /bin 15 | /.springBeans 16 | /.factorypath 17 | /.attach_pid* 18 | /**/.DS_Store 19 | /.idea 20 | /**/*.iml 21 | /.sessions 22 | /**/.~lock.* 23 | /.pydevproject 24 | /Report_*.java 25 | /octave-workspace 26 | /hs_err_pid*.log 27 | /*.Rout 28 | /.apt* 29 | /ajcore.*.txt 30 | /.temp-*.txt 31 | /dependency-reduced-pom.xml 32 | /META-INF 33 | /.Rproj.user 34 | /.Rhistory 35 | /.RData 36 | /.Ruserdata 37 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-jep/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | de.invesdwin 5 | invesdwin-context-python-parent 6 | 1.0.4-SNAPSHOT 7 | 8 | invesdwin-context-python-runtime-jep 9 | jar 10 | 11 | 12 | 13 | 14 | org.apache.maven.plugins 15 | maven-surefire-plugin 16 | 17 | 18 | 19 | **/* 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | de.invesdwin 34 | invesdwin-context-python-runtime-contract 35 | 36 | 37 | de.invesdwin 38 | invesdwin-context-python-runtime-contract 39 | test-jar 40 | test 41 | 42 | 43 | 44 | black.ninia 45 | jep 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-jep/src/main/java/META-INF/de.invesdwin.context.python.runtime.jep.properties: -------------------------------------------------------------------------------- 1 | de.invesdwin.context.python.runtime.jep.JepProperties.THREAD_POOL_COUNT=${de.invesdwin.context.ContextProperties.CPU_THREAD_POOL_COUNT} 2 | # specify where the libjep.so resides on your computer (which you might normally add to java.library.path manually) 3 | de.invesdwin.context.python.runtime.jep.JepProperties.JEP_LIBRARY_PATH=/usr/local/lib/python3.12/dist-packages/jep/ -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-jep/src/main/java/META-INF/services/de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython: -------------------------------------------------------------------------------- 1 | de.invesdwin.context.python.runtime.jep.JepScriptTaskRunnerPython -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-jep/src/main/java/de/invesdwin/context/python/runtime/jep/JepProperties.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.jep; 2 | 3 | import java.io.File; 4 | import java.io.PrintStream; 5 | import java.util.LinkedHashSet; 6 | import java.util.Set; 7 | 8 | import javax.annotation.concurrent.ThreadSafe; 9 | 10 | import org.zeroturnaround.exec.stream.slf4j.Slf4jDebugOutputStream; 11 | import org.zeroturnaround.exec.stream.slf4j.Slf4jWarnOutputStream; 12 | 13 | import de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython; 14 | import de.invesdwin.context.system.properties.SystemProperties; 15 | import de.invesdwin.util.collections.Collections; 16 | import de.invesdwin.util.concurrent.Executors; 17 | 18 | @ThreadSafe 19 | public final class JepProperties { 20 | 21 | public static final int THREAD_POOL_COUNT; 22 | public static final File JEP_LIBRARY_PATH; 23 | 24 | public static final PrintStream REDIRECTED_OUT = new PrintStream( 25 | new Slf4jDebugOutputStream(IScriptTaskRunnerPython.LOG)); 26 | public static final PrintStream REDIRECTED_ERR = new PrintStream( 27 | new Slf4jWarnOutputStream(IScriptTaskRunnerPython.LOG)); 28 | 29 | private static final Set SHARED_MODULES = Collections.synchronizedSet(new LinkedHashSet()); 30 | 31 | static { 32 | SHARED_MODULES.add("numpy"); 33 | 34 | final SystemProperties systemProperties = new SystemProperties(JepProperties.class); 35 | if (systemProperties.containsValue("THREAD_POOL_COUNT")) { 36 | THREAD_POOL_COUNT = systemProperties.getInteger("THREAD_POOL_COUNT"); 37 | } else { 38 | THREAD_POOL_COUNT = Executors.getCpuThreadPoolCount(); 39 | } 40 | if (systemProperties.containsValue("JEP_LIBRARY_PATH")) { 41 | JEP_LIBRARY_PATH = systemProperties.getFile("JEP_LIBRARY_PATH"); 42 | } else { 43 | JEP_LIBRARY_PATH = null; 44 | } 45 | } 46 | 47 | private JepProperties() { 48 | } 49 | 50 | public static Set getSharedModules() { 51 | return Collections.unmodifiableSet(SHARED_MODULES); 52 | } 53 | 54 | /** 55 | * You need to define all modules that cannot be unloaded once loaded here 56 | */ 57 | public static boolean addSharedModule(final String groupId) { 58 | final boolean changed = SHARED_MODULES.add(groupId); 59 | if (changed) { 60 | JepScriptTaskRunnerPython.resetExecutor(); 61 | } 62 | return changed; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-jep/src/main/java/de/invesdwin/context/python/runtime/jep/JepScriptTaskCallbackContext.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.jep; 2 | 3 | import java.io.Closeable; 4 | import java.util.Map; 5 | import java.util.concurrent.ConcurrentHashMap; 6 | 7 | import javax.annotation.concurrent.ThreadSafe; 8 | 9 | import de.invesdwin.context.integration.script.callback.IScriptTaskCallback; 10 | import de.invesdwin.context.integration.script.callback.ObjectScriptTaskParameters; 11 | import de.invesdwin.context.integration.script.callback.ObjectScriptTaskParametersPool; 12 | import de.invesdwin.context.integration.script.callback.ObjectScriptTaskReturnValue; 13 | import de.invesdwin.context.integration.script.callback.ObjectScriptTaskReturns; 14 | import de.invesdwin.context.integration.script.callback.ObjectScriptTaskReturnsPool; 15 | import de.invesdwin.util.lang.UUIDs; 16 | 17 | @ThreadSafe 18 | public class JepScriptTaskCallbackContext implements Closeable { 19 | 20 | private static final Map UUID_CONTEXT = new ConcurrentHashMap<>(); 21 | 22 | private final String uuid; 23 | private final IScriptTaskCallback callback; 24 | 25 | public JepScriptTaskCallbackContext(final IScriptTaskCallback callback) { 26 | this.uuid = UUIDs.newPseudoRandomUUID(); 27 | this.callback = callback; 28 | UUID_CONTEXT.put(uuid, this); 29 | } 30 | 31 | public static JepScriptTaskCallbackContext getContext(final String uuid) { 32 | return UUID_CONTEXT.get(uuid); 33 | } 34 | 35 | public void init(final JepScriptTaskEnginePython engine) { 36 | engine.getInputs().putString("jepScriptTaskCallbackContextUuid", getUuid()); 37 | } 38 | 39 | public String getUuid() { 40 | return uuid; 41 | } 42 | 43 | public ObjectScriptTaskReturnValue invoke(final String methodName, final Object... args) { 44 | final ObjectScriptTaskParameters parameters = ObjectScriptTaskParametersPool.INSTANCE.borrowObject(); 45 | final ObjectScriptTaskReturns returns = ObjectScriptTaskReturnsPool.INSTANCE.borrowObject(); 46 | try { 47 | parameters.setParameters(args); 48 | callback.invoke(methodName, parameters, returns); 49 | return returns.newReturn(); 50 | } finally { 51 | ObjectScriptTaskReturnsPool.INSTANCE.returnObject(returns); 52 | ObjectScriptTaskParametersPool.INSTANCE.returnObject(parameters); 53 | } 54 | } 55 | 56 | @Override 57 | public void close() { 58 | UUID_CONTEXT.remove(uuid); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-jep/src/main/java/de/invesdwin/context/python/runtime/jep/JepScriptTaskEnginePython.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.jep; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import de.invesdwin.context.integration.script.IScriptTaskEngine; 6 | import de.invesdwin.context.python.runtime.jep.internal.JepWrapper; 7 | import de.invesdwin.util.concurrent.WrappedExecutorService; 8 | import de.invesdwin.util.concurrent.lock.ILock; 9 | import de.invesdwin.util.concurrent.lock.disabled.DisabledLock; 10 | import jep.Jep; 11 | 12 | @NotThreadSafe 13 | public class JepScriptTaskEnginePython implements IScriptTaskEngine { 14 | 15 | private Jep jep; 16 | private final JepScriptTaskInputsPython inputs; 17 | private final JepScriptTaskResultsPython results; 18 | 19 | public JepScriptTaskEnginePython(final Jep jep) { 20 | this.jep = jep; 21 | this.inputs = new JepScriptTaskInputsPython(this); 22 | this.results = new JepScriptTaskResultsPython(this); 23 | } 24 | 25 | @Override 26 | public void eval(final String expression) { 27 | jep.exec(expression); 28 | } 29 | 30 | @Override 31 | public JepScriptTaskInputsPython getInputs() { 32 | return inputs; 33 | } 34 | 35 | @Override 36 | public JepScriptTaskResultsPython getResults() { 37 | return results; 38 | } 39 | 40 | @Override 41 | public void close() { 42 | eval("restoreContext()"); 43 | jep = null; 44 | } 45 | 46 | @Override 47 | public Jep unwrap() { 48 | return jep; 49 | } 50 | 51 | /** 52 | * Jep can only allows access within the same thread. Thus not lock needed. Though be careful about not trying to 53 | * access the instance from other threads. This will lead to exceptions. 54 | */ 55 | @Override 56 | public ILock getSharedLock() { 57 | return DisabledLock.INSTANCE; 58 | } 59 | 60 | @Override 61 | public WrappedExecutorService getSharedExecutor() { 62 | return JepScriptTaskRunnerPython.getExecutor(); 63 | } 64 | 65 | public static JepScriptTaskEnginePython newInstance() { 66 | return new JepScriptTaskEnginePython(JepWrapper.get().getJep()); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-jep/src/main/java/de/invesdwin/context/python/runtime/jep/internal/JepWrapper.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.jep.internal; 2 | 3 | import java.io.Closeable; 4 | 5 | import javax.annotation.concurrent.NotThreadSafe; 6 | 7 | import org.springframework.core.io.ClassPathResource; 8 | 9 | import de.invesdwin.context.python.runtime.jep.JepProperties; 10 | import de.invesdwin.context.python.runtime.jep.JepScriptTaskEnginePython; 11 | import de.invesdwin.instrument.DynamicInstrumentationReflections; 12 | import de.invesdwin.util.lang.finalizer.AFinalizer; 13 | import io.netty.util.concurrent.FastThreadLocal; 14 | import jep.Jep; 15 | import jep.JepConfig; 16 | import jep.JepException; 17 | import jep.SubInterpreter; 18 | 19 | @NotThreadSafe 20 | public final class JepWrapper implements Closeable { 21 | 22 | private static final FastThreadLocal JEP = new FastThreadLocal() { 23 | @Override 24 | protected JepWrapper initialValue() { 25 | return new JepWrapper(); 26 | } 27 | 28 | @Override 29 | protected void onRemoval(final JepWrapper value) throws Exception { 30 | value.close(); 31 | } 32 | }; 33 | 34 | static { 35 | DynamicInstrumentationReflections.addPathToJavaLibraryPath(JepProperties.JEP_LIBRARY_PATH); 36 | } 37 | 38 | private final JepWrapperFinalizer finalizer; 39 | 40 | private JepWrapper() { 41 | try { 42 | this.finalizer = new JepWrapperFinalizer(); 43 | this.finalizer.register(this); 44 | final JepScriptTaskEnginePython engine = new JepScriptTaskEnginePython(finalizer.jep); 45 | engine.eval(new ClassPathResource(JepWrapper.class.getSimpleName() + ".py", JepWrapper.class)); 46 | engine.close(); 47 | } catch (final JepException e) { 48 | throw new RuntimeException( 49 | "Maybe you are mixing the python version with a different one for which jep was compiled for?", e); 50 | } 51 | } 52 | 53 | public Jep getJep() { 54 | return finalizer.jep; 55 | } 56 | 57 | @Override 58 | public void close() { 59 | finalizer.close(); 60 | } 61 | 62 | private static final class JepWrapperFinalizer extends AFinalizer { 63 | 64 | private SubInterpreter jep; 65 | 66 | private JepWrapperFinalizer() throws JepException { 67 | this.jep = new SubInterpreter(new JepConfig().setSharedModules(JepProperties.getSharedModules())); 68 | } 69 | 70 | @Override 71 | protected void clean() { 72 | try { 73 | jep.close(); 74 | jep = null; 75 | } catch (final JepException e) { 76 | throw new RuntimeException(e); 77 | } 78 | } 79 | 80 | @Override 81 | protected boolean isCleaned() { 82 | return jep == null; 83 | } 84 | 85 | @Override 86 | public boolean isThreadLocal() { 87 | return false; 88 | } 89 | 90 | } 91 | 92 | public static JepWrapper get() { 93 | return JEP.get(); 94 | } 95 | } -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-jep/src/main/java/de/invesdwin/context/python/runtime/jep/internal/JepWrapper.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from de.invesdwin.context.python.runtime.jep import JepScriptTaskCallbackContext 3 | 4 | class StdOutToJava(object): 5 | "Redirects Python's sys.stdout to Slf4j" 6 | 7 | def __init__(self): 8 | from de.invesdwin.context.python.runtime.jep import JepProperties 9 | self.oldout = sys.stdout 10 | self.printmethod = getattr(JepProperties.REDIRECTED_OUT, 'print') 11 | self.flushmethod = getattr(JepProperties.REDIRECTED_OUT, 'flush') 12 | 13 | def write(self, msg): 14 | self.printmethod(msg) 15 | 16 | def flush(self): 17 | self.flushmethod() 18 | 19 | 20 | class StdErrToJava(object): 21 | "Redirects Python's sys.stderr to Slf4j" 22 | 23 | def __init__(self): 24 | from de.invesdwin.context.python.runtime.jep import JepProperties 25 | self.oldout = sys.stdout 26 | self.printmethod = getattr(JepProperties.REDIRECTED_ERR, 'print') 27 | self.flushmethod = getattr(JepProperties.REDIRECTED_ERR, 'flush') 28 | 29 | def write(self, msg): 30 | self.printmethod(msg) 31 | 32 | def flush(self): 33 | self.flushmethod() 34 | 35 | sys.stdout = StdOutToJava() 36 | sys.stderr = StdErrToJava() 37 | 38 | # http://stackoverflow.com/questions/3543833/how-do-i-clear-all-variables-in-the-middle-of-a-python-script 39 | __saved_context__ = {} 40 | 41 | def saveContext(): 42 | __saved_context__.update(sys.modules[__name__].__dict__) 43 | 44 | def restoreContext(): 45 | names = list(sys.modules[__name__].__dict__.keys()) 46 | for n in names: 47 | if n not in __saved_context__: 48 | del sys.modules[__name__].__dict__[n] 49 | 50 | def callback(methodName, *parameters): 51 | if 'jepScriptTaskCallbackContext' not in globals(): 52 | if 'jepScriptTaskCallbackContextUuid' in locals() or 'jepScriptTaskCallbackContextUuid' in globals(): 53 | global jepScriptTaskCallbackContext 54 | jepScriptTaskCallbackContext = JepScriptTaskCallbackContext.getContext(jepScriptTaskCallbackContextUuid) 55 | else: 56 | raise Exception("IScriptTaskCallback not available") 57 | returnValue = jepScriptTaskCallbackContext.invoke(methodName, parameters) 58 | if returnValue.isReturnExpression(): 59 | returnExpression = returnValue.getReturnValue() 60 | if returnExpression.startswith("raise "): 61 | exec(returnExpression, globals()) 62 | # fallback 63 | raise Exception(returnExpression) 64 | else: 65 | returnExpressionLines = returnExpression.splitlines() 66 | returnExpressionLinesLength = len(returnExpressionLines) 67 | if(returnExpressionLinesLength > 1): 68 | returnExpressionLinesLastElement = returnExpressionLinesLength-1 69 | returnExpressionExec = "\n".join(returnExpressionLines[:returnExpressionLinesLastElement]) 70 | returnExpressionEval = returnExpressionLines[returnExpressionLinesLastElement] 71 | loc = {} 72 | exec(returnExpressionExec, globals(), loc) 73 | return eval(returnExpressionEval, globals(), loc) 74 | else: 75 | return eval(returnExpression, globals()) 76 | else: 77 | return returnValue.getReturnValue() 78 | 79 | saveContext() 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-jep/src/test/java/de/invesdwin/context/python/runtime/jep/JepScriptTaskRunnerPythonTest.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.jep; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | import de.invesdwin.context.python.runtime.contract.InputsAndResultsTests; 8 | import de.invesdwin.context.python.runtime.contract.callback.ParametersAndReturnsTests; 9 | import de.invesdwin.context.test.ATest; 10 | import jakarta.inject.Inject; 11 | 12 | @NotThreadSafe 13 | public class JepScriptTaskRunnerPythonTest extends ATest { 14 | 15 | @Inject 16 | private JepScriptTaskRunnerPython runner; 17 | 18 | @Test 19 | public void test() { 20 | new InputsAndResultsTests(runner).test(); 21 | } 22 | 23 | @Test 24 | public void testParallel() { 25 | new InputsAndResultsTests(runner).testParallel(); 26 | } 27 | 28 | @Test 29 | public void testCallback() { 30 | new ParametersAndReturnsTests(runner).test(); 31 | } 32 | 33 | @Test 34 | public void testCallbackParallel() { 35 | new ParametersAndReturnsTests(runner).testParallel(); 36 | } 37 | 38 | @Test 39 | public void testSimpleCallback() { 40 | new SimpleCallbackTest(runner).testSimpleCallback(); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-jep/src/test/java/de/invesdwin/context/python/runtime/jep/SimpleCallbackTest.py: -------------------------------------------------------------------------------- 1 | print("putUuid") 2 | print(putUuid) 3 | 4 | from de.invesdwin.context.python.runtime.jep import SimpleCallbackTest 5 | getSecretStaticImport = SimpleCallbackTest.getSecretStatic(putUuid) 6 | print("getSecretStaticImport") 7 | print(getSecretStaticImport) 8 | 9 | getSecretStaticCallback = callback("getSecretStatic", putUuid) 10 | print("getSecretStaticCallback") 11 | print(getSecretStaticCallback) 12 | 13 | getSecretCallback = callback("getSecret", putUuid) 14 | print("getSecretCallback") 15 | print(getSecretCallback) 16 | 17 | getSecretExpressionCallback = callback("getSecretExpression", putUuid) 18 | print("getSecretExpressionCallback") 19 | print(getSecretExpressionCallback) 20 | 21 | callback("voidMethod") 22 | 23 | callManyParams = callback("callManyParams", True, 2, 3, '4', 5, 6, 7.0, 8.0, "123456789", 10.0) 24 | if(callManyParams != 55): 25 | raise Exception("callManyParams unexpected result: "+callManyParams) 26 | callManyParamsExpression = callback("callManyParamsExpression", True, 2, 3, '4', 5, 6, 7.0, 8.0, "123456789", 10.0) 27 | if(callManyParamsExpression != 55): 28 | raise Exception("callManyParamsExpression unexpected result: "+callManyParamsExpression) 29 | callManyParamsExpressionMultiline = callback("callManyParamsExpressionMultiline", True, 2, 3, '4', 5, 6, 7.0, 8.0, "123456789", 10.0) 30 | if(callManyParamsExpressionMultiline != 55): 31 | raise Exception("callManyParamsExpressionMultiline unexpected result: "+callManyParamsExpressionMultiline) 32 | 33 | getManyParamsExpression = putManyParamsExpression 34 | print("getManyParamsExpression") 35 | print(getManyParamsExpression) 36 | getManyParamsExpressionMultilineWrong = putManyParamsExpressionMultilineWrong 37 | print("getManyParamsExpressionMultilineWrong") 38 | print(getManyParamsExpressionMultilineWrong) 39 | getManyParamsExpressionMultiline = putManyParamsExpressionMultiline 40 | print("getManyParamsExpressionMultiline") 41 | print(getManyParamsExpressionMultiline) 42 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-jython/.gitignore: -------------------------------------------------------------------------------- 1 | /.checkstyle 2 | /.classpath 3 | /log 4 | /target 5 | /activemq-data 6 | /.fbprefs 7 | /cache 8 | /.settings 9 | /.project 10 | /*.hprof 11 | /.jrctemp 12 | /.invesdwin 13 | /nbproject 14 | /bin 15 | /.springBeans 16 | /.factorypath 17 | /.attach_pid* 18 | /**/.DS_Store 19 | /.idea 20 | /**/*.iml 21 | /.sessions 22 | /**/.~lock.* 23 | /.pydevproject 24 | /Report_*.java 25 | /octave-workspace 26 | /hs_err_pid*.log 27 | /*.Rout 28 | /.apt* 29 | /ajcore.*.txt 30 | /.temp-*.txt 31 | /dependency-reduced-pom.xml 32 | /META-INF 33 | /.Rproj.user 34 | /.Rhistory 35 | /.RData 36 | /.Ruserdata 37 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-jython/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | 6 | de.invesdwin 7 | invesdwin-context-python-parent 8 | 1.0.4-SNAPSHOT 9 | 10 | invesdwin-context-python-runtime-jython 11 | jar 12 | 13 | 14 | 15 | de.invesdwin 16 | invesdwin-context-python-runtime-contract 17 | 18 | 19 | de.invesdwin 20 | invesdwin-context-python-runtime-contract 21 | test-jar 22 | test 23 | 24 | 25 | 26 | org.python 27 | jython-slim 28 | 29 | 30 | org.bouncycastle 31 | bcprov-jdk18on 32 | 33 | 34 | org.bouncycastle 35 | bcpkix-jdk18on 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-jython/src/main/java/META-INF/services/de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython: -------------------------------------------------------------------------------- 1 | de.invesdwin.context.python.runtime.jython.JythonScriptTaskRunnerPython -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-jython/src/main/java/de/invesdwin/context/python/runtime/jython/JythonScriptTaskCallbackContext.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.jython; 2 | 3 | import java.io.Closeable; 4 | import java.util.Map; 5 | import java.util.concurrent.ConcurrentHashMap; 6 | 7 | import javax.annotation.concurrent.ThreadSafe; 8 | 9 | import org.springframework.core.io.ClassPathResource; 10 | 11 | import de.invesdwin.context.integration.script.callback.IScriptTaskCallback; 12 | import de.invesdwin.context.integration.script.callback.ObjectScriptTaskParameters; 13 | import de.invesdwin.context.integration.script.callback.ObjectScriptTaskParametersPool; 14 | import de.invesdwin.context.integration.script.callback.ObjectScriptTaskReturnValue; 15 | import de.invesdwin.context.integration.script.callback.ObjectScriptTaskReturns; 16 | import de.invesdwin.context.integration.script.callback.ObjectScriptTaskReturnsPool; 17 | import de.invesdwin.util.lang.UUIDs; 18 | 19 | @ThreadSafe 20 | public class JythonScriptTaskCallbackContext implements Closeable { 21 | 22 | private static final Map UUID_CONTEXT = new ConcurrentHashMap<>(); 23 | 24 | private final String uuid; 25 | private final IScriptTaskCallback callback; 26 | 27 | public JythonScriptTaskCallbackContext(final IScriptTaskCallback callback) { 28 | this.uuid = UUIDs.newPseudoRandomUUID(); 29 | this.callback = callback; 30 | UUID_CONTEXT.put(uuid, this); 31 | } 32 | 33 | public static JythonScriptTaskCallbackContext getContext(final String uuid) { 34 | return UUID_CONTEXT.get(uuid); 35 | } 36 | 37 | public void init(final JythonScriptTaskEnginePython engine) { 38 | engine.getInputs().putString("jythonScriptTaskCallbackContextUuid", getUuid()); 39 | engine.eval(new ClassPathResource(JythonScriptTaskCallbackContext.class.getSimpleName() + ".py", 40 | JythonScriptTaskCallbackContext.class)); 41 | } 42 | 43 | public String getUuid() { 44 | return uuid; 45 | } 46 | 47 | public ObjectScriptTaskReturnValue invoke(final String methodName, final Object... args) { 48 | final ObjectScriptTaskParameters parameters = ObjectScriptTaskParametersPool.INSTANCE.borrowObject(); 49 | final ObjectScriptTaskReturns returns = ObjectScriptTaskReturnsPool.INSTANCE.borrowObject(); 50 | try { 51 | parameters.setParameters(args); 52 | callback.invoke(methodName, parameters, returns); 53 | return returns.newReturn(); 54 | } finally { 55 | ObjectScriptTaskReturnsPool.INSTANCE.returnObject(returns); 56 | ObjectScriptTaskParametersPool.INSTANCE.returnObject(parameters); 57 | } 58 | } 59 | 60 | @Override 61 | public void close() { 62 | UUID_CONTEXT.remove(uuid); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-jython/src/main/java/de/invesdwin/context/python/runtime/jython/JythonScriptTaskCallbackContext.py: -------------------------------------------------------------------------------- 1 | from de.invesdwin.context.python.runtime.jython import JythonScriptTaskCallbackContext 2 | 3 | def callback(methodName, *parameters): 4 | if 'jythonScriptTaskCallbackContext' not in globals(): 5 | if 'jythonScriptTaskCallbackContextUuid' in locals() or 'jythonScriptTaskCallbackContextUuid' in globals(): 6 | global jythonScriptTaskCallbackContext 7 | jythonScriptTaskCallbackContext = JythonScriptTaskCallbackContext.getContext(jythonScriptTaskCallbackContextUuid) 8 | else: 9 | raise Exception("IScriptTaskCallback not available") 10 | returnValue = jythonScriptTaskCallbackContext.invoke(methodName, parameters) 11 | if returnValue.isReturnExpression(): 12 | returnExpression = returnValue.getReturnValue() 13 | if returnExpression.startswith("raise "): 14 | exec(returnExpression, globals()) 15 | # fallback 16 | raise Exception(returnExpression) 17 | else: 18 | returnExpressionLines = returnExpression.splitlines() 19 | returnExpressionLinesLength = len(returnExpressionLines) 20 | if(returnExpressionLinesLength > 1): 21 | returnExpressionLinesLastElement = returnExpressionLinesLength-1 22 | returnExpressionExec = "\n".join(returnExpressionLines[:returnExpressionLinesLastElement]) 23 | returnExpressionEval = returnExpressionLines[returnExpressionLinesLastElement] 24 | loc = {} 25 | exec(returnExpressionExec, globals(), loc) 26 | return eval(returnExpressionEval, globals(), loc) 27 | else: 28 | return eval(returnExpression, globals()) 29 | else: 30 | return returnValue.getReturnValue() 31 | 32 | 33 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-jython/src/main/java/de/invesdwin/context/python/runtime/jython/JythonScriptTaskEnginePython.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.jython; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | import javax.script.ScriptException; 5 | 6 | import org.python.jsr223.PyScriptEngine; 7 | 8 | import de.invesdwin.context.integration.script.IScriptTaskEngine; 9 | import de.invesdwin.context.python.runtime.jython.pool.PyScriptEngineObjectPool; 10 | import de.invesdwin.util.concurrent.WrappedExecutorService; 11 | import de.invesdwin.util.concurrent.lock.ILock; 12 | import de.invesdwin.util.concurrent.lock.disabled.DisabledLock; 13 | 14 | @NotThreadSafe 15 | public class JythonScriptTaskEnginePython implements IScriptTaskEngine { 16 | 17 | private PyScriptEngine pyScriptEngine; 18 | private final JythonScriptTaskInputsPython inputs; 19 | private final JythonScriptTaskResultsPython results; 20 | 21 | public JythonScriptTaskEnginePython(final PyScriptEngine pyScriptEngine) { 22 | this.pyScriptEngine = pyScriptEngine; 23 | this.inputs = new JythonScriptTaskInputsPython(this); 24 | this.results = new JythonScriptTaskResultsPython(this); 25 | } 26 | 27 | /** 28 | * https://github.com/mrj0/pyScriptEngine/issues/55 29 | */ 30 | @Override 31 | public void eval(final String expression) { 32 | try { 33 | pyScriptEngine.eval(expression); 34 | } catch (final ScriptException e) { 35 | throw new RuntimeException(e); 36 | } 37 | } 38 | 39 | @Override 40 | public JythonScriptTaskInputsPython getInputs() { 41 | return inputs; 42 | } 43 | 44 | @Override 45 | public JythonScriptTaskResultsPython getResults() { 46 | return results; 47 | } 48 | 49 | @Override 50 | public void close() { 51 | pyScriptEngine = null; 52 | } 53 | 54 | @Override 55 | public PyScriptEngine unwrap() { 56 | return pyScriptEngine; 57 | } 58 | 59 | /** 60 | * Each instance has its own engine, so no shared locking required. 61 | */ 62 | @Override 63 | public ILock getSharedLock() { 64 | return DisabledLock.INSTANCE; 65 | } 66 | 67 | @Override 68 | public WrappedExecutorService getSharedExecutor() { 69 | return null; 70 | } 71 | 72 | public static JythonScriptTaskEnginePython newInstance() { 73 | return new JythonScriptTaskEnginePython(PyScriptEngineObjectPool.INSTANCE.borrowObject()) { 74 | @Override 75 | public void close() { 76 | final PyScriptEngine unwrap = unwrap(); 77 | if (unwrap != null) { 78 | PyScriptEngineObjectPool.INSTANCE.returnObject(unwrap); 79 | } 80 | super.close(); 81 | } 82 | }; 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-jython/src/main/java/de/invesdwin/context/python/runtime/jython/JythonScriptTaskRunnerPython.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.jython; 2 | 3 | import javax.annotation.concurrent.Immutable; 4 | 5 | import org.python.jsr223.PyScriptEngine; 6 | import org.springframework.beans.factory.FactoryBean; 7 | 8 | import de.invesdwin.context.integration.script.callback.IScriptTaskCallback; 9 | import de.invesdwin.context.integration.script.callback.LoggingDelegateScriptTaskCallback; 10 | import de.invesdwin.context.python.runtime.contract.AScriptTaskPython; 11 | import de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython; 12 | import de.invesdwin.context.python.runtime.jython.pool.PyScriptEngineObjectPool; 13 | import de.invesdwin.util.error.Throwables; 14 | import jakarta.inject.Named; 15 | 16 | @Immutable 17 | @Named 18 | public final class JythonScriptTaskRunnerPython 19 | implements IScriptTaskRunnerPython, FactoryBean { 20 | 21 | public static final String INTERNAL_RESULT_VARIABLE = JythonScriptTaskRunnerPython.class.getSimpleName() 22 | + "_result"; 23 | public static final JythonScriptTaskRunnerPython INSTANCE = new JythonScriptTaskRunnerPython(); 24 | 25 | /** 26 | * public for ServiceLoader support 27 | */ 28 | public JythonScriptTaskRunnerPython() {} 29 | 30 | @Override 31 | public T run(final AScriptTaskPython scriptTask) { 32 | //get session 33 | final PyScriptEngine pyScriptEngine = PyScriptEngineObjectPool.INSTANCE.borrowObject(); 34 | final IScriptTaskCallback callback = scriptTask.getCallback(); 35 | final JythonScriptTaskCallbackContext context; 36 | if (callback != null) { 37 | context = new JythonScriptTaskCallbackContext(LoggingDelegateScriptTaskCallback.maybeWrap(LOG, callback)); 38 | } else { 39 | context = null; 40 | } 41 | try { 42 | //inputs 43 | final JythonScriptTaskEnginePython engine = new JythonScriptTaskEnginePython(pyScriptEngine); 44 | if (context != null) { 45 | context.init(engine); 46 | } 47 | scriptTask.populateInputs(engine.getInputs()); 48 | 49 | //execute 50 | scriptTask.executeScript(engine); 51 | 52 | //results 53 | final T result = scriptTask.extractResults(engine.getResults()); 54 | engine.close(); 55 | 56 | //return 57 | PyScriptEngineObjectPool.INSTANCE.returnObject(pyScriptEngine); 58 | return result; 59 | } catch (final Throwable t) { 60 | PyScriptEngineObjectPool.INSTANCE.invalidateObject(pyScriptEngine); 61 | throw Throwables.propagate(t); 62 | } finally { 63 | if (context != null) { 64 | context.close(); 65 | } 66 | } 67 | } 68 | 69 | @Override 70 | public JythonScriptTaskRunnerPython getObject() throws Exception { 71 | return INSTANCE; 72 | } 73 | 74 | @Override 75 | public Class getObjectType() { 76 | return JythonScriptTaskRunnerPython.class; 77 | } 78 | 79 | @Override 80 | public boolean isSingleton() { 81 | return true; 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-jython/src/main/java/de/invesdwin/context/python/runtime/jython/pool/PyScriptEngineObjectPool.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.jython.pool; 2 | 3 | import java.io.OutputStreamWriter; 4 | 5 | import javax.annotation.concurrent.ThreadSafe; 6 | import javax.script.ScriptException; 7 | 8 | import org.python.jsr223.PyScriptEngine; 9 | import org.python.jsr223.PyScriptEngineFactory; 10 | import org.springframework.beans.factory.FactoryBean; 11 | import org.zeroturnaround.exec.stream.slf4j.Slf4jDebugOutputStream; 12 | import org.zeroturnaround.exec.stream.slf4j.Slf4jWarnOutputStream; 13 | 14 | import de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython; 15 | import de.invesdwin.util.concurrent.pool.timeout.ATimeoutObjectPool; 16 | import de.invesdwin.util.time.date.FTimeUnit; 17 | import de.invesdwin.util.time.duration.Duration; 18 | import jakarta.inject.Named; 19 | 20 | @ThreadSafe 21 | @Named 22 | public final class PyScriptEngineObjectPool extends ATimeoutObjectPool 23 | implements FactoryBean { 24 | 25 | public static final PyScriptEngineObjectPool INSTANCE = new PyScriptEngineObjectPool(); 26 | private static final PyScriptEngineFactory FACTORY = new PyScriptEngineFactory(); 27 | 28 | private PyScriptEngineObjectPool() { 29 | super(Duration.ONE_MINUTE, new Duration(10, FTimeUnit.SECONDS)); 30 | } 31 | 32 | @Override 33 | public void invalidateObject(final PyScriptEngine element) { 34 | element.close(); 35 | } 36 | 37 | @Override 38 | protected PyScriptEngine newObject() { 39 | final PyScriptEngine engine = (PyScriptEngine) FACTORY.getScriptEngine(); 40 | engine.getContext().setWriter(new OutputStreamWriter(new Slf4jDebugOutputStream(IScriptTaskRunnerPython.LOG))); 41 | engine.getContext() 42 | .setErrorWriter(new OutputStreamWriter(new Slf4jWarnOutputStream(IScriptTaskRunnerPython.LOG))); 43 | return engine; 44 | } 45 | 46 | /** 47 | * https://github.com/mrj0/jep/wiki/Performance-Considerations 48 | * 49 | * https://github.com/spyder-ide/spyder/issues/2563 50 | * 51 | * http://stackoverflow.com/questions/3543833/how-do-i-clear-all-variables-in-the-middle-of-a-python-script 52 | */ 53 | @Override 54 | protected boolean passivateObject(final PyScriptEngine element) { 55 | try { 56 | element.eval(IScriptTaskRunnerPython.CLEANUP_SCRIPT); 57 | return true; 58 | } catch (final ScriptException e) { 59 | throw new RuntimeException(e); 60 | } 61 | } 62 | 63 | @Override 64 | public PyScriptEngineObjectPool getObject() { 65 | return INSTANCE; 66 | } 67 | 68 | @Override 69 | public Class getObjectType() { 70 | return PyScriptEngineObjectPool.class; 71 | } 72 | 73 | @Override 74 | public boolean isSingleton() { 75 | return true; 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-jython/src/test/java/de/invesdwin/context/python/runtime/jython/JythonScriptTaskRunnerPythonTest.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.jython; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | import de.invesdwin.context.python.runtime.contract.InputsAndResultsTests; 8 | import de.invesdwin.context.python.runtime.contract.callback.ParametersAndReturnsTests; 9 | import de.invesdwin.context.test.ATest; 10 | import jakarta.inject.Inject; 11 | 12 | @NotThreadSafe 13 | public class JythonScriptTaskRunnerPythonTest extends ATest { 14 | 15 | @Inject 16 | private JythonScriptTaskRunnerPython runner; 17 | 18 | @Test 19 | public void test() { 20 | new InputsAndResultsTests(runner).test(); 21 | } 22 | 23 | @Test 24 | public void testParallel() { 25 | new InputsAndResultsTests(runner).testParallel(); 26 | } 27 | 28 | @Test 29 | public void testCallback() { 30 | new ParametersAndReturnsTests(runner).test(); 31 | } 32 | 33 | @Test 34 | public void testCallbackParallel() { 35 | new ParametersAndReturnsTests(runner).testParallel(); 36 | } 37 | 38 | @Test 39 | public void testSimpleCallback() { 40 | new SimpleCallbackTest(runner).testSimpleCallback(); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-jython/src/test/java/de/invesdwin/context/python/runtime/jython/SimpleCallbackTest.py: -------------------------------------------------------------------------------- 1 | print("putUuid") 2 | print(putUuid) 3 | 4 | from de.invesdwin.context.python.runtime.jython import SimpleCallbackTest 5 | getSecretStaticImport = SimpleCallbackTest.getSecretStatic(putUuid) 6 | print("getSecretStaticImport") 7 | print(getSecretStaticImport) 8 | 9 | getSecretStaticCallback = callback("getSecretStatic", putUuid) 10 | print("getSecretStaticCallback") 11 | print(getSecretStaticCallback) 12 | 13 | getSecretCallback = callback("getSecret", putUuid) 14 | print("getSecretCallback") 15 | print(getSecretCallback) 16 | 17 | getSecretExpressionCallback = callback("getSecretExpression", putUuid) 18 | print("getSecretExpressionCallback") 19 | print(getSecretExpressionCallback) 20 | 21 | callback("voidMethod") 22 | 23 | callManyParams = callback("callManyParams", True, 2, 3, '4', 5, 6, 7.0, 8.0, "123456789", 10.0) 24 | if(callManyParams != 55): 25 | raise Exception("callManyParams unexpected result: "+callManyParams) 26 | callManyParamsExpression = callback("callManyParamsExpression", True, 2, 3, '4', 5, 6, 7.0, 8.0, "123456789", 10.0) 27 | if(callManyParamsExpression != 55): 28 | raise Exception("callManyParamsExpression unexpected result: "+callManyParamsExpression) 29 | callManyParamsExpressionMultiline = callback("callManyParamsExpressionMultiline", True, 2, 3, '4', 5, 6, 7.0, 8.0, "123456789", 10.0) 30 | if(callManyParamsExpressionMultiline != 55): 31 | raise Exception("callManyParamsExpressionMultiline unexpected result: "+callManyParamsExpressionMultiline) 32 | 33 | getManyParamsExpression = putManyParamsExpression 34 | print("getManyParamsExpression") 35 | print(getManyParamsExpression) 36 | getManyParamsExpressionMultilineWrong = putManyParamsExpressionMultilineWrong 37 | print("getManyParamsExpressionMultilineWrong") 38 | print(getManyParamsExpressionMultilineWrong) 39 | getManyParamsExpressionMultiline = putManyParamsExpressionMultiline 40 | print("getManyParamsExpressionMultiline") 41 | print(getManyParamsExpressionMultiline) 42 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-libpythonclj/.gitignore: -------------------------------------------------------------------------------- 1 | /.checkstyle 2 | /.classpath 3 | /log 4 | /target 5 | /activemq-data 6 | /.fbprefs 7 | /cache 8 | /.settings 9 | /.project 10 | /*.hprof 11 | /.jrctemp 12 | /.invesdwin 13 | /nbproject 14 | /bin 15 | /.springBeans 16 | /.factorypath 17 | /.attach_pid* 18 | /**/.DS_Store 19 | /.idea 20 | /**/*.iml 21 | /.sessions 22 | /**/.~lock.* 23 | /.pydevproject 24 | /Report_*.java 25 | /octave-workspace 26 | /hs_err_pid*.log 27 | /*.Rout 28 | /.apt* 29 | /ajcore.*.txt 30 | /.temp-*.txt 31 | /dependency-reduced-pom.xml 32 | /META-INF 33 | /.Rproj.user 34 | /.Rhistory 35 | /.RData 36 | /.Ruserdata 37 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-libpythonclj/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | 6 | de.invesdwin 7 | invesdwin-context-python-parent 8 | 1.0.4-SNAPSHOT 9 | 10 | invesdwin-context-python-runtime-libpythonclj 11 | jar 12 | 13 | 14 | 15 | 16 | org.apache.maven.plugins 17 | maven-surefire-plugin 18 | 19 | 20 | 21 | **/* 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | de.invesdwin 31 | invesdwin-context-python-runtime-contract 32 | 33 | 34 | de.invesdwin 35 | invesdwin-context-python-runtime-contract 36 | test-jar 37 | test 38 | 39 | 40 | clj-python 41 | libpython-clj 42 | 43 | 44 | de.invesdwin 45 | invesdwin-context-integration-channel 46 | 47 | 48 | io.netty 49 | netty-handler 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-libpythonclj/src/main/java/META-INF/de.invesdwin.context.python.runtime.libpythonclj.properties: -------------------------------------------------------------------------------- 1 | # you can switch to a different python installation by defining an absolute path here 2 | de.invesdwin.context.python.runtime.libpythonclj.LibpythoncljProperties.PYTHON_COMMAND=python3 3 | # disable automatic checks for the global interpreter lock 4 | libpython_clj.manual_gil=true -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-libpythonclj/src/main/java/META-INF/services/de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython: -------------------------------------------------------------------------------- 1 | de.invesdwin.context.python.runtime.libpythonclj.LibpythoncljScriptTaskRunnerPython -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-libpythonclj/src/main/java/de/invesdwin/context/python/runtime/libpythonclj/LibpythoncljProperties.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.libpythonclj; 2 | 3 | import javax.annotation.concurrent.ThreadSafe; 4 | 5 | import de.invesdwin.context.system.properties.SystemProperties; 6 | 7 | @ThreadSafe 8 | public final class LibpythoncljProperties { 9 | 10 | public static final String PYTHON_COMMAND; 11 | 12 | static { 13 | 14 | final SystemProperties systemProperties = new SystemProperties(LibpythoncljProperties.class); 15 | if (systemProperties.containsValue("PYTHON_COMMAND")) { 16 | PYTHON_COMMAND = systemProperties.getString("PYTHON_COMMAND"); 17 | } else { 18 | PYTHON_COMMAND = null; 19 | } 20 | } 21 | 22 | private LibpythoncljProperties() { 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-libpythonclj/src/main/java/de/invesdwin/context/python/runtime/libpythonclj/LibpythoncljScriptTaskEnginePython.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.libpythonclj; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import de.invesdwin.context.integration.script.IScriptTaskEngine; 6 | import de.invesdwin.context.python.runtime.libpythonclj.internal.IPythonEngineWrapper; 7 | import de.invesdwin.context.python.runtime.libpythonclj.internal.InitializingPythonEngineWrapper; 8 | import de.invesdwin.util.concurrent.WrappedExecutorService; 9 | import de.invesdwin.util.concurrent.lock.ILock; 10 | import de.invesdwin.util.concurrent.lock.disabled.DisabledLock; 11 | 12 | @NotThreadSafe 13 | public class LibpythoncljScriptTaskEnginePython implements IScriptTaskEngine { 14 | 15 | private IPythonEngineWrapper pythonEngine; 16 | private final LibpythoncljScriptTaskInputsPython inputs; 17 | private final LibpythoncljScriptTaskResultsPython results; 18 | 19 | public LibpythoncljScriptTaskEnginePython(final IPythonEngineWrapper pythonEngine) { 20 | this.pythonEngine = pythonEngine; 21 | this.inputs = new LibpythoncljScriptTaskInputsPython(this); 22 | this.results = new LibpythoncljScriptTaskResultsPython(this); 23 | } 24 | 25 | @Override 26 | public void eval(final String expression) { 27 | try { 28 | pythonEngine.exec(expression); 29 | } catch (final Exception e) { 30 | throw new RuntimeException(e); 31 | } 32 | } 33 | 34 | @Override 35 | public LibpythoncljScriptTaskInputsPython getInputs() { 36 | return inputs; 37 | } 38 | 39 | @Override 40 | public LibpythoncljScriptTaskResultsPython getResults() { 41 | return results; 42 | } 43 | 44 | @Override 45 | public void close() { 46 | if (pythonEngine != null) { 47 | eval("restoreContext()"); 48 | pythonEngine = null; 49 | } 50 | } 51 | 52 | @Override 53 | public IPythonEngineWrapper unwrap() { 54 | return pythonEngine; 55 | } 56 | 57 | @Override 58 | public ILock getSharedLock() { 59 | if (pythonEngine == null) { 60 | return DisabledLock.INSTANCE; 61 | } else { 62 | return pythonEngine.getLock(); 63 | } 64 | } 65 | 66 | @Override 67 | public WrappedExecutorService getSharedExecutor() { 68 | return null; 69 | } 70 | 71 | /** 72 | * WARNING: Don't share instances of this class between threads, or else deadlocks or jvm crashes might occur due to 73 | * GIL lock mismanagement. 74 | */ 75 | public static LibpythoncljScriptTaskEnginePython newInstance() { 76 | return new LibpythoncljScriptTaskEnginePython(InitializingPythonEngineWrapper.getInstance()); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-libpythonclj/src/main/java/de/invesdwin/context/python/runtime/libpythonclj/internal/GilLock.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.libpythonclj.internal; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import org.apache.commons.lang3.mutable.MutableInt; 6 | 7 | import de.invesdwin.util.concurrent.lock.ASimpleLock; 8 | import de.invesdwin.util.concurrent.lock.IReentrantLock; 9 | import de.invesdwin.util.concurrent.lock.Locks; 10 | 11 | /** 12 | * WARNING: Don't share instances of this class between threads, or else deadlocks or jvm crashes might occur due to GIL 13 | * lock mismanagement. 14 | */ 15 | @NotThreadSafe 16 | public final class GilLock extends ASimpleLock { 17 | 18 | private static final IReentrantLock MAIN_LOCK = Locks.newReentrantLock("libpython-clj_MAIN"); 19 | private static final String GIL_LOCK_NAME = "libpython-clj_GIL"; 20 | 21 | private final MutableInt lockedCount = new MutableInt(); 22 | private long key; 23 | 24 | public GilLock() {} 25 | 26 | @Override 27 | public boolean isLocked() { 28 | return lockedCount.intValue() > 0; 29 | } 30 | 31 | @Override 32 | public boolean isLockedByCurrentThread() { 33 | //we don't share this lock instance between threads anyhow 34 | return isLocked(); 35 | } 36 | 37 | @Override 38 | public void lock() { 39 | if (lockedCount.incrementAndGet() == 1) { 40 | MAIN_LOCK.lock(); 41 | key = libpython_clj2.java_api.lockGIL(); 42 | } 43 | } 44 | 45 | @Override 46 | public void unlock() { 47 | if (lockedCount.decrementAndGet() == 0) { 48 | libpython_clj2.java_api.unlockGIL(key); 49 | MAIN_LOCK.unlock(); 50 | } 51 | } 52 | 53 | @Override 54 | public String getName() { 55 | return GIL_LOCK_NAME; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-libpythonclj/src/main/java/de/invesdwin/context/python/runtime/libpythonclj/internal/IPythonEngineWrapper.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.libpythonclj.internal; 2 | 3 | import de.invesdwin.util.concurrent.lock.ILock; 4 | 5 | public interface IPythonEngineWrapper { 6 | 7 | ILock getLock(); 8 | 9 | void exec(String expression); 10 | 11 | Object get(String variable); 12 | 13 | void set(String variable, Object value); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-libpythonclj/src/main/java/de/invesdwin/context/python/runtime/libpythonclj/internal/InitializingPythonEngineWrapper.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.libpythonclj.internal; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import de.invesdwin.util.concurrent.lock.ILock; 6 | 7 | @NotThreadSafe 8 | public final class InitializingPythonEngineWrapper implements IPythonEngineWrapper { 9 | 10 | private static boolean initialized = false; 11 | private final IPythonEngineWrapper delegate; 12 | 13 | public InitializingPythonEngineWrapper() { 14 | this.delegate = InitializingPythonEngineWrapper.getInstance(); 15 | } 16 | 17 | public static void maybeInit() { 18 | if (initialized) { 19 | return; 20 | } 21 | synchronized (InitializingPythonEngineWrapper.class) { 22 | if (initialized) { 23 | return; 24 | } 25 | UncheckedPythonEngineWrapper.getInstance().init(); 26 | initialized = true; 27 | } 28 | } 29 | 30 | @Override 31 | public ILock getLock() { 32 | maybeInit(); 33 | return delegate.getLock(); 34 | } 35 | 36 | @Override 37 | public void exec(final String expression) { 38 | maybeInit(); 39 | delegate.exec(expression); 40 | } 41 | 42 | @Override 43 | public Object get(final String variable) { 44 | maybeInit(); 45 | return delegate.get(variable); 46 | } 47 | 48 | @Override 49 | public void set(final String variable, final Object value) { 50 | maybeInit(); 51 | delegate.set(variable, value); 52 | } 53 | 54 | public static IPythonEngineWrapper getInstance() { 55 | InitializingPythonEngineWrapper.maybeInit(); 56 | return UncheckedPythonEngineWrapper.getInstance(); 57 | } 58 | 59 | } -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-libpythonclj/src/main/java/de/invesdwin/context/python/runtime/libpythonclj/internal/UncheckedPythonEngineWrapper.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | # http://stackoverflow.com/questions/3543833/how-do-i-clear-all-variables-in-the-middle-of-a-python-script 4 | __saved_context__ = {} 5 | 6 | def saveContext(): 7 | __saved_context__.update(sys.modules[__name__].__dict__) 8 | 9 | def restoreContext(): 10 | names = list(sys.modules[__name__].__dict__.keys()) 11 | for n in names: 12 | if n not in __saved_context__: 13 | del sys.modules[__name__].__dict__[n] 14 | 15 | saveContext() -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-libpythonclj/src/test/java/de/invesdwin/context/python/runtime/libpythonclj/LibpythoncljScriptTaskRunnerPythonTest.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.libpythonclj; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | import de.invesdwin.context.python.runtime.contract.InputsAndResultsTests; 8 | import de.invesdwin.context.python.runtime.contract.callback.ParametersAndReturnsTests; 9 | import de.invesdwin.context.test.ATest; 10 | import jakarta.inject.Inject; 11 | 12 | @NotThreadSafe 13 | public class LibpythoncljScriptTaskRunnerPythonTest extends ATest { 14 | 15 | @Inject 16 | private LibpythoncljScriptTaskRunnerPython runner; 17 | 18 | @Test 19 | public void test() { 20 | new InputsAndResultsTests(runner).test(); 21 | } 22 | 23 | @Test 24 | public void testParallel() { 25 | new InputsAndResultsTests(runner).testParallel(); 26 | } 27 | 28 | @Test 29 | public void testCallback() { 30 | new ParametersAndReturnsTests(runner).test(); 31 | } 32 | 33 | @Test 34 | public void testCallbackParallel() { 35 | new ParametersAndReturnsTests(runner).testParallel(); 36 | } 37 | 38 | @Test 39 | public void testSimpleCallback() { 40 | new SimpleCallbackTest(runner).testSimpleCallback(); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-libpythonclj/src/test/java/de/invesdwin/context/python/runtime/libpythonclj/SimpleCallbackTest.py: -------------------------------------------------------------------------------- 1 | print("putUuid") 2 | print(putUuid) 3 | 4 | getSecretStaticCallback = callback("getSecretStatic", putUuid) 5 | print("getSecretStaticCallback") 6 | print(getSecretStaticCallback) 7 | 8 | getSecretCallback = callback("getSecret", putUuid) 9 | print("getSecretCallback") 10 | print(getSecretCallback) 11 | 12 | getSecretExpressionCallback = callback("getSecretExpression", putUuid) 13 | print("getSecretExpressionCallback") 14 | print(getSecretExpressionCallback) 15 | 16 | callback("voidMethod") 17 | 18 | callManyParams = callback("callManyParams", True, 2, 3, '4', 5, 6, 7.0, 8.0, "123456789", 10.0) 19 | if(callManyParams != 55): 20 | raise Exception("callManyParams unexpected result: "+callManyParams) 21 | callManyParamsExpression = callback("callManyParamsExpression", True, 2, 3, '4', 5, 6, 7.0, 8.0, "123456789", 10.0) 22 | if(callManyParamsExpression != 55): 23 | raise Exception("callManyParamsExpression unexpected result: "+callManyParamsExpression) 24 | callManyParamsExpressionMultiline = callback("callManyParamsExpressionMultiline", True, 2, 3, '4', 5, 6, 7.0, 8.0, "123456789", 10.0) 25 | if(callManyParamsExpressionMultiline != 55): 26 | raise Exception("callManyParamsExpressionMultiline unexpected result: "+callManyParamsExpressionMultiline) 27 | 28 | getManyParamsExpression = putManyParamsExpression 29 | print("getManyParamsExpression") 30 | print(getManyParamsExpression) 31 | getManyParamsExpressionMultilineWrong = putManyParamsExpressionMultilineWrong 32 | print("getManyParamsExpressionMultilineWrong") 33 | print(getManyParamsExpressionMultilineWrong) 34 | getManyParamsExpressionMultiline = putManyParamsExpressionMultiline 35 | print("getManyParamsExpressionMultiline") 36 | print(getManyParamsExpressionMultiline) 37 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-py4j/.gitignore: -------------------------------------------------------------------------------- 1 | /.checkstyle 2 | /.classpath 3 | /log 4 | /target 5 | /activemq-data 6 | /.fbprefs 7 | /cache 8 | /.settings 9 | /.project 10 | /*.hprof 11 | /.jrctemp 12 | /.invesdwin 13 | /nbproject 14 | /bin 15 | /.springBeans 16 | /.factorypath 17 | /.attach_pid* 18 | /**/.DS_Store 19 | /.idea 20 | /**/*.iml 21 | /.sessions 22 | /**/.~lock.* 23 | /.pydevproject 24 | /Report_*.java 25 | /octave-workspace 26 | /hs_err_pid*.log 27 | /*.Rout 28 | /.apt* 29 | /ajcore.*.txt 30 | /.temp-*.txt 31 | /dependency-reduced-pom.xml 32 | /META-INF 33 | /.Rproj.user 34 | /.Rhistory 35 | /.RData 36 | /.Ruserdata 37 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-py4j/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | de.invesdwin 5 | invesdwin-context-python-parent 6 | 1.0.4-SNAPSHOT 7 | 8 | invesdwin-context-python-runtime-py4j 9 | jar 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | de.invesdwin 18 | invesdwin-context-python-runtime-contract 19 | 20 | 21 | de.invesdwin 22 | invesdwin-context-python-runtime-contract 23 | test-jar 24 | test 25 | 26 | 27 | 28 | net.sf.py4j 29 | py4j 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-py4j/src/main/java/META-INF/de.invesdwin.context.python.runtime.py4j.properties: -------------------------------------------------------------------------------- 1 | # you can switch to "python3" here for example or provide a different command entirely (e.g. "pypy") 2 | de.invesdwin.context.python.runtime.py4j.Py4jProperties.PYTHON_COMMAND=python3 -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-py4j/src/main/java/META-INF/logback/context-python-runtime-py4j.logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-py4j/src/main/java/META-INF/services/de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython: -------------------------------------------------------------------------------- 1 | de.invesdwin.context.python.runtime.py4j.Py4jScriptTaskRunnerPython -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-py4j/src/main/java/de/invesdwin/context/python/runtime/py4j/Py4jProperties.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.py4j; 2 | 3 | import javax.annotation.concurrent.Immutable; 4 | 5 | import de.invesdwin.context.system.properties.SystemProperties; 6 | 7 | @Immutable 8 | public final class Py4jProperties { 9 | 10 | public static final String PYTHON_COMMAND; 11 | 12 | static { 13 | final SystemProperties systemProperties = new SystemProperties(Py4jProperties.class); 14 | if (systemProperties.containsValue("PYTHON_COMMAND")) { 15 | PYTHON_COMMAND = systemProperties.getString("PYTHON_COMMAND"); 16 | } else { 17 | PYTHON_COMMAND = "python"; 18 | } 19 | } 20 | 21 | private Py4jProperties() {} 22 | 23 | } 24 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-py4j/src/main/java/de/invesdwin/context/python/runtime/py4j/Py4jScriptTaskEnginePython.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.py4j; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import de.invesdwin.context.integration.script.IScriptTaskEngine; 6 | import de.invesdwin.context.python.runtime.py4j.pool.Py4jInterpreterObjectPool; 7 | import de.invesdwin.context.python.runtime.py4j.pool.internal.Py4jInterpreter; 8 | import de.invesdwin.util.concurrent.WrappedExecutorService; 9 | import de.invesdwin.util.concurrent.lock.ILock; 10 | import de.invesdwin.util.concurrent.lock.disabled.DisabledLock; 11 | 12 | @NotThreadSafe 13 | public class Py4jScriptTaskEnginePython implements IScriptTaskEngine { 14 | 15 | private Py4jInterpreter py4jInterpreter; 16 | private final Py4jScriptTaskInputsPython inputs; 17 | private final Py4jScriptTaskResultsPython results; 18 | 19 | public Py4jScriptTaskEnginePython(final Py4jInterpreter py4jInterpreter) { 20 | this.py4jInterpreter = py4jInterpreter; 21 | this.inputs = new Py4jScriptTaskInputsPython(this); 22 | this.results = new Py4jScriptTaskResultsPython(this); 23 | } 24 | 25 | @Override 26 | public void eval(final String expression) { 27 | py4jInterpreter.eval(expression); 28 | } 29 | 30 | @Override 31 | public Py4jScriptTaskInputsPython getInputs() { 32 | return inputs; 33 | } 34 | 35 | @Override 36 | public Py4jScriptTaskResultsPython getResults() { 37 | return results; 38 | } 39 | 40 | @Override 41 | public void close() { 42 | py4jInterpreter = null; 43 | } 44 | 45 | @Override 46 | public Py4jInterpreter unwrap() { 47 | return py4jInterpreter; 48 | } 49 | 50 | /** 51 | * Each instance has its own engine, so no shared locking required. 52 | */ 53 | @Override 54 | public ILock getSharedLock() { 55 | return DisabledLock.INSTANCE; 56 | } 57 | 58 | @Override 59 | public WrappedExecutorService getSharedExecutor() { 60 | return null; 61 | } 62 | 63 | public static Py4jScriptTaskEnginePython newInstance() { 64 | return new Py4jScriptTaskEnginePython(Py4jInterpreterObjectPool.INSTANCE.borrowObject()) { 65 | @Override 66 | public void close() { 67 | final Py4jInterpreter unwrap = unwrap(); 68 | if (unwrap != null) { 69 | Py4jInterpreterObjectPool.INSTANCE.returnObject(unwrap); 70 | } 71 | super.close(); 72 | } 73 | }; 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-py4j/src/main/java/de/invesdwin/context/python/runtime/py4j/Py4jScriptTaskRunnerPython.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.py4j; 2 | 3 | import javax.annotation.concurrent.Immutable; 4 | 5 | import org.springframework.beans.factory.FactoryBean; 6 | 7 | import de.invesdwin.context.integration.script.callback.IScriptTaskCallback; 8 | import de.invesdwin.context.integration.script.callback.LoggingDelegateScriptTaskCallback; 9 | import de.invesdwin.context.python.runtime.contract.AScriptTaskPython; 10 | import de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython; 11 | import de.invesdwin.context.python.runtime.py4j.pool.Py4jInterpreterObjectPool; 12 | import de.invesdwin.context.python.runtime.py4j.pool.internal.Py4jInterpreter; 13 | import de.invesdwin.util.error.Throwables; 14 | import jakarta.inject.Named; 15 | 16 | @Immutable 17 | @Named 18 | public final class Py4jScriptTaskRunnerPython 19 | implements IScriptTaskRunnerPython, FactoryBean { 20 | 21 | public static final String INTERNAL_RESULT_VARIABLE = Py4jScriptTaskRunnerPython.class.getSimpleName() + "_result"; 22 | public static final Py4jScriptTaskRunnerPython INSTANCE = new Py4jScriptTaskRunnerPython(); 23 | 24 | /** 25 | * public for ServiceLoader support 26 | */ 27 | public Py4jScriptTaskRunnerPython() {} 28 | 29 | @Override 30 | public T run(final AScriptTaskPython scriptTask) { 31 | //get session 32 | final Py4jInterpreter pyScriptEngine = Py4jInterpreterObjectPool.INSTANCE.borrowObject(); 33 | final IScriptTaskCallback callback = scriptTask.getCallback(); 34 | final Py4jScriptTaskCallbackContext context; 35 | if (callback != null) { 36 | context = new Py4jScriptTaskCallbackContext(LoggingDelegateScriptTaskCallback.maybeWrap(LOG, callback)); 37 | } else { 38 | context = null; 39 | } 40 | try { 41 | //inputs 42 | final Py4jScriptTaskEnginePython engine = new Py4jScriptTaskEnginePython(pyScriptEngine); 43 | if (context != null) { 44 | context.init(engine); 45 | } 46 | scriptTask.populateInputs(engine.getInputs()); 47 | 48 | //execute 49 | scriptTask.executeScript(engine); 50 | 51 | //results 52 | final T result = scriptTask.extractResults(engine.getResults()); 53 | engine.close(); 54 | 55 | //return 56 | Py4jInterpreterObjectPool.INSTANCE.returnObject(pyScriptEngine); 57 | return result; 58 | } catch (final Throwable t) { 59 | Py4jInterpreterObjectPool.INSTANCE.invalidateObject(pyScriptEngine); 60 | throw Throwables.propagate(t); 61 | } finally { 62 | if (context != null) { 63 | context.close(); 64 | } 65 | } 66 | } 67 | 68 | @Override 69 | public Py4jScriptTaskRunnerPython getObject() throws Exception { 70 | return INSTANCE; 71 | } 72 | 73 | @Override 74 | public Class getObjectType() { 75 | return Py4jScriptTaskRunnerPython.class; 76 | } 77 | 78 | @Override 79 | public boolean isSingleton() { 80 | return true; 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-py4j/src/main/java/de/invesdwin/context/python/runtime/py4j/pool/Py4jInterpreterObjectPool.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.py4j.pool; 2 | 3 | import javax.annotation.concurrent.ThreadSafe; 4 | 5 | import org.springframework.beans.factory.FactoryBean; 6 | 7 | import de.invesdwin.context.python.runtime.py4j.pool.internal.Py4jInterpreter; 8 | import de.invesdwin.util.concurrent.pool.timeout.ATimeoutObjectPool; 9 | import de.invesdwin.util.time.date.FTimeUnit; 10 | import de.invesdwin.util.time.duration.Duration; 11 | import jakarta.inject.Named; 12 | 13 | @ThreadSafe 14 | @Named 15 | public final class Py4jInterpreterObjectPool extends ATimeoutObjectPool 16 | implements FactoryBean { 17 | 18 | public static final Py4jInterpreterObjectPool INSTANCE = new Py4jInterpreterObjectPool(); 19 | 20 | private Py4jInterpreterObjectPool() { 21 | super(Duration.ONE_MINUTE, new Duration(10, FTimeUnit.SECONDS)); 22 | } 23 | 24 | @Override 25 | public void invalidateObject(final Py4jInterpreter element) { 26 | element.close(); 27 | } 28 | 29 | @Override 30 | protected Py4jInterpreter newObject() { 31 | return new Py4jInterpreter(); 32 | } 33 | 34 | @Override 35 | protected boolean passivateObject(final Py4jInterpreter element) { 36 | element.cleanup(); 37 | return true; 38 | } 39 | 40 | @Override 41 | public Py4jInterpreterObjectPool getObject() { 42 | return INSTANCE; 43 | } 44 | 45 | @Override 46 | public Class getObjectType() { 47 | return Py4jInterpreterObjectPool.class; 48 | } 49 | 50 | @Override 51 | public boolean isSingleton() { 52 | return true; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-py4j/src/main/java/de/invesdwin/context/python/runtime/py4j/pool/internal/IPy4jInterpreter.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.py4j.pool.internal; 2 | 3 | public interface IPy4jInterpreter { 4 | 5 | Object get(String variable); 6 | 7 | void eval(String expression); 8 | 9 | void put(String variable, Object value); 10 | 11 | void cleanup(); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-py4j/src/main/java/de/invesdwin/context/python/runtime/py4j/pool/internal/ReadyApplication.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.py4j.pool.internal; 2 | 3 | import java.util.concurrent.atomic.AtomicBoolean; 4 | 5 | import javax.annotation.concurrent.ThreadSafe; 6 | 7 | @ThreadSafe 8 | public class ReadyApplication { 9 | 10 | private final AtomicBoolean ready = new AtomicBoolean(false); 11 | 12 | public void ready() { 13 | ready.set(true); 14 | } 15 | 16 | public boolean isReady() { 17 | return ready.get(); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-py4j/src/test/java/de/invesdwin/context/python/runtime/py4j/Py4jScriptTaskRunnerPythonTest.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.py4j; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | import de.invesdwin.context.python.runtime.contract.InputsAndResultsTests; 8 | import de.invesdwin.context.python.runtime.contract.callback.ParametersAndReturnsTests; 9 | import de.invesdwin.context.test.ATest; 10 | import jakarta.inject.Inject; 11 | 12 | @NotThreadSafe 13 | public class Py4jScriptTaskRunnerPythonTest extends ATest { 14 | 15 | @Inject 16 | private Py4jScriptTaskRunnerPython runner; 17 | 18 | @Test 19 | public void test() { 20 | new InputsAndResultsTests(runner).test(); 21 | } 22 | 23 | @Test 24 | public void testParallel() { 25 | new InputsAndResultsTests(runner).testParallel(); 26 | } 27 | 28 | @Test 29 | public void testCallback() { 30 | new ParametersAndReturnsTests(runner).test(); 31 | } 32 | 33 | @Test 34 | public void testCallbackParallel() { 35 | new ParametersAndReturnsTests(runner).testParallel(); 36 | } 37 | 38 | @Test 39 | public void testSimpleCallback() { 40 | new SimpleCallbackTest(runner).testSimpleCallback(); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-py4j/src/test/java/de/invesdwin/context/python/runtime/py4j/SimpleCallbackTest.py: -------------------------------------------------------------------------------- 1 | print("putUuid") 2 | print(putUuid) 3 | 4 | getSecretStaticGateway = gateway.jvm.de.invesdwin.context.python.runtime.py4j.SimpleCallbackTest.getSecretStatic(putUuid) 5 | print("getSecretStaticGateway") 6 | print(getSecretStaticGateway) 7 | 8 | java_import(gateway.jvm,'de.invesdwin.context.python.runtime.py4j.SimpleCallbackTest') 9 | getSecretStaticImport = gateway.jvm.SimpleCallbackTest.getSecretStatic(putUuid) 10 | print("getSecretStaticImport") 11 | print(getSecretStaticImport) 12 | 13 | getSecretStaticCallback = callback("getSecretStatic", putUuid) 14 | print("getSecretStaticCallback") 15 | print(getSecretStaticCallback) 16 | 17 | getSecretCallback = callback("getSecret", putUuid) 18 | print("getSecretCallback") 19 | print(getSecretCallback) 20 | 21 | getSecretExpressionCallback = callback("getSecretExpression", putUuid) 22 | print("getSecretExpressionCallback") 23 | print(getSecretExpressionCallback) 24 | 25 | callback("voidMethod") 26 | 27 | callManyParams = callback("callManyParams", True, 2, 3, '4', 5, 6, 7.0, 8.0, "123456789", 10.0) 28 | if(callManyParams != 55): 29 | raise Exception("callManyParams unexpected result: "+callManyParams) 30 | callManyParamsExpression = callback("callManyParamsExpression", True, 2, 3, '4', 5, 6, 7.0, 8.0, "123456789", 10.0) 31 | if(callManyParamsExpression != 55): 32 | raise Exception("callManyParamsExpression unexpected result: "+callManyParamsExpression) 33 | callManyParamsExpressionMultiline = callback("callManyParamsExpressionMultiline", True, 2, 3, '4', 5, 6, 7.0, 8.0, "123456789", 10.0) 34 | if(callManyParamsExpressionMultiline != 55): 35 | raise Exception("callManyParamsExpressionMultiline unexpected result: "+callManyParamsExpressionMultiline) 36 | 37 | getManyParamsExpression = putManyParamsExpression 38 | print("getManyParamsExpression") 39 | print(getManyParamsExpression) 40 | getManyParamsExpressionMultilineWrong = putManyParamsExpressionMultilineWrong 41 | print("getManyParamsExpressionMultilineWrong") 42 | print(getManyParamsExpressionMultilineWrong) 43 | getManyParamsExpressionMultiline = putManyParamsExpressionMultiline 44 | print("getManyParamsExpressionMultiline") 45 | print(getManyParamsExpressionMultiline) 46 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-python4j/.gitignore: -------------------------------------------------------------------------------- 1 | /.checkstyle 2 | /.classpath 3 | /log 4 | /target 5 | /activemq-data 6 | /.fbprefs 7 | /cache 8 | /.settings 9 | /.project 10 | /*.hprof 11 | /.jrctemp 12 | /.invesdwin 13 | /nbproject 14 | /bin 15 | /.springBeans 16 | /.factorypath 17 | /.attach_pid* 18 | /**/.DS_Store 19 | /.idea 20 | /**/*.iml 21 | /.sessions 22 | /**/.~lock.* 23 | /.pydevproject 24 | /Report_*.java 25 | /octave-workspace 26 | /hs_err_pid*.log 27 | /*.Rout 28 | /.apt* 29 | /ajcore.*.txt 30 | /.temp-*.txt 31 | /dependency-reduced-pom.xml 32 | /META-INF 33 | /.Rproj.user 34 | /.Rhistory 35 | /.RData 36 | /.Ruserdata 37 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-python4j/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | 6 | de.invesdwin 7 | invesdwin-context-python-parent 8 | 1.0.4-SNAPSHOT 9 | 10 | invesdwin-context-python-runtime-python4j 11 | jar 12 | 13 | 14 | 15 | 16 | org.apache.maven.plugins 17 | maven-surefire-plugin 18 | 19 | 20 | 21 | **/* 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | de.invesdwin 31 | invesdwin-context-python-runtime-contract 32 | 33 | 34 | de.invesdwin 35 | invesdwin-context-python-runtime-contract 36 | test-jar 37 | test 38 | 39 | 40 | org.nd4j 41 | python4j-numpy 42 | 43 | 44 | org.nd4j 45 | python4j-core 46 | 47 | 48 | de.invesdwin 49 | invesdwin-context-integration-channel 50 | 51 | 52 | io.netty 53 | netty-handler 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-python4j/src/main/java/META-INF/services/de.invesdwin.context.python.runtime.contract.IScriptTaskRunnerPython: -------------------------------------------------------------------------------- 1 | de.invesdwin.context.python.runtime.python4j.Python4jScriptTaskRunnerPython -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-python4j/src/main/java/de/invesdwin/context/python/runtime/python4j/Python4jScriptTaskEnginePython.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.python4j; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import de.invesdwin.context.integration.script.IScriptTaskEngine; 6 | import de.invesdwin.context.python.runtime.python4j.internal.IPythonEngineWrapper; 7 | import de.invesdwin.context.python.runtime.python4j.internal.InitializingPythonEngineWrapper; 8 | import de.invesdwin.util.concurrent.WrappedExecutorService; 9 | import de.invesdwin.util.concurrent.lock.ILock; 10 | import de.invesdwin.util.concurrent.lock.disabled.DisabledLock; 11 | 12 | @NotThreadSafe 13 | public class Python4jScriptTaskEnginePython implements IScriptTaskEngine { 14 | 15 | private IPythonEngineWrapper pythonEngine; 16 | private final Python4jScriptTaskInputsPython inputs; 17 | private final Python4jScriptTaskResultsPython results; 18 | 19 | public Python4jScriptTaskEnginePython(final IPythonEngineWrapper pythonEngine) { 20 | this.pythonEngine = pythonEngine; 21 | this.inputs = new Python4jScriptTaskInputsPython(this); 22 | this.results = new Python4jScriptTaskResultsPython(this); 23 | } 24 | 25 | @Override 26 | public void eval(final String expression) { 27 | try { 28 | pythonEngine.exec(expression); 29 | } catch (final Exception e) { 30 | throw new RuntimeException(e); 31 | } 32 | } 33 | 34 | @Override 35 | public Python4jScriptTaskInputsPython getInputs() { 36 | return inputs; 37 | } 38 | 39 | @Override 40 | public Python4jScriptTaskResultsPython getResults() { 41 | return results; 42 | } 43 | 44 | @Override 45 | public void close() { 46 | if (pythonEngine != null) { 47 | eval("restoreContext()"); 48 | pythonEngine = null; 49 | } 50 | } 51 | 52 | @Override 53 | public IPythonEngineWrapper unwrap() { 54 | return pythonEngine; 55 | } 56 | 57 | @Override 58 | public ILock getSharedLock() { 59 | if (pythonEngine == null) { 60 | return DisabledLock.INSTANCE; 61 | } else { 62 | return pythonEngine.getLock(); 63 | } 64 | } 65 | 66 | @Override 67 | public WrappedExecutorService getSharedExecutor() { 68 | return null; 69 | } 70 | 71 | /** 72 | * WARNING: Don't share instances of this class between threads, or else deadlocks or jvm crashes might occur due to 73 | * GIL lock mismanagement. 74 | */ 75 | public static Python4jScriptTaskEnginePython newInstance() { 76 | return new Python4jScriptTaskEnginePython(InitializingPythonEngineWrapper.getInstance()); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-python4j/src/main/java/de/invesdwin/context/python/runtime/python4j/internal/GilLock.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.python4j.internal; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import org.apache.commons.lang3.mutable.MutableInt; 6 | import org.nd4j.python4j.PythonGIL; 7 | 8 | import de.invesdwin.util.concurrent.lock.ASimpleLock; 9 | import de.invesdwin.util.concurrent.lock.IReentrantLock; 10 | import de.invesdwin.util.concurrent.lock.Locks; 11 | 12 | /** 13 | * WARNING: Don't share instances of this class between threads, or else deadlocks or jvm crashes might occur due to GIL 14 | * lock mismanagement. 15 | * 16 | * TODO: migrate to upstream as soon as possible: https://github.com/eclipse/deeplearning4j/issues/9595 17 | */ 18 | @NotThreadSafe 19 | public final class GilLock extends ASimpleLock { 20 | 21 | private static final IReentrantLock MAIN_LOCK = Locks.newReentrantLock("python4j_MAIN"); 22 | private static final String GIL_LOCK_NAME = "python4j_GIL"; 23 | 24 | private final MutableInt lockedCount = new MutableInt(); 25 | private PythonGIL key; 26 | 27 | public GilLock() {} 28 | 29 | @Override 30 | public boolean isLocked() { 31 | return lockedCount.intValue() > 0; 32 | } 33 | 34 | @Override 35 | public boolean isLockedByCurrentThread() { 36 | //we don't share this lock instance between threads anyhow 37 | return isLocked(); 38 | } 39 | 40 | @Override 41 | public void lock() { 42 | if (lockedCount.incrementAndGet() == 1) { 43 | MAIN_LOCK.lock(); 44 | key = PythonGIL.lock(); 45 | } 46 | } 47 | 48 | @Override 49 | public void unlock() { 50 | if (lockedCount.decrementAndGet() == 0) { 51 | key.close(); 52 | key = null; 53 | MAIN_LOCK.unlock(); 54 | } 55 | } 56 | 57 | @Override 58 | public String getName() { 59 | return GIL_LOCK_NAME; 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-python4j/src/main/java/de/invesdwin/context/python/runtime/python4j/internal/IPythonEngineWrapper.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.python4j.internal; 2 | 3 | import de.invesdwin.util.concurrent.lock.ILock; 4 | 5 | public interface IPythonEngineWrapper { 6 | 7 | ILock getLock(); 8 | 9 | void exec(String expression); 10 | 11 | Object get(String variable); 12 | 13 | void set(String variable, Object value); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-python4j/src/main/java/de/invesdwin/context/python/runtime/python4j/internal/InitializingPythonEngineWrapper.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.python4j.internal; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import de.invesdwin.util.concurrent.lock.ILock; 6 | 7 | @NotThreadSafe 8 | public final class InitializingPythonEngineWrapper implements IPythonEngineWrapper { 9 | 10 | private static boolean initialized = false; 11 | private final IPythonEngineWrapper delegate; 12 | 13 | public InitializingPythonEngineWrapper() { 14 | this.delegate = InitializingPythonEngineWrapper.getInstance(); 15 | } 16 | 17 | public static void maybeInit() { 18 | if (initialized) { 19 | return; 20 | } 21 | synchronized (InitializingPythonEngineWrapper.class) { 22 | if (initialized) { 23 | return; 24 | } 25 | UncheckedPythonEngineWrapper.getInstance().init(); 26 | initialized = true; 27 | } 28 | } 29 | 30 | @Override 31 | public ILock getLock() { 32 | maybeInit(); 33 | return delegate.getLock(); 34 | } 35 | 36 | @Override 37 | public void exec(final String expression) { 38 | maybeInit(); 39 | delegate.exec(expression); 40 | } 41 | 42 | @Override 43 | public Object get(final String variable) { 44 | maybeInit(); 45 | return delegate.get(variable); 46 | } 47 | 48 | @Override 49 | public void set(final String variable, final Object value) { 50 | maybeInit(); 51 | delegate.set(variable, value); 52 | } 53 | 54 | public static IPythonEngineWrapper getInstance() { 55 | InitializingPythonEngineWrapper.maybeInit(); 56 | return UncheckedPythonEngineWrapper.getInstance(); 57 | } 58 | 59 | } -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-python4j/src/main/java/de/invesdwin/context/python/runtime/python4j/internal/NonePythonType.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.python4j.internal; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import org.nd4j.python4j.PythonObject; 6 | import org.nd4j.python4j.PythonType; 7 | 8 | @NotThreadSafe 9 | public class NonePythonType extends PythonType { 10 | 11 | public NonePythonType() { 12 | super("NoneType", Void.class); 13 | } 14 | 15 | @Override 16 | public boolean accepts(final Object javaObject) { 17 | return javaObject == null; 18 | } 19 | 20 | @Override 21 | public Void toJava(final PythonObject pythonObject) { 22 | return null; 23 | } 24 | 25 | @Override 26 | public PythonObject toPython(final Void javaObject) { 27 | return UncheckedPythonEngineWrapper.getInstance().newNone(); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-python4j/src/main/java/de/invesdwin/context/python/runtime/python4j/internal/UncheckedPythonEngineWrapper.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | # http://stackoverflow.com/questions/3543833/how-do-i-clear-all-variables-in-the-middle-of-a-python-script 4 | __saved_context__ = {} 5 | 6 | def saveContext(): 7 | __saved_context__.update(sys.modules[__name__].__dict__) 8 | 9 | def restoreContext(): 10 | names = list(sys.modules[__name__].__dict__.keys()) 11 | for n in names: 12 | if n not in __saved_context__: 13 | del sys.modules[__name__].__dict__[n] 14 | 15 | saveContext() -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-python4j/src/test/java/de/invesdwin/context/python/runtime/python4j/Python4jScriptTaskRunnerPythonTest.java: -------------------------------------------------------------------------------- 1 | package de.invesdwin.context.python.runtime.python4j; 2 | 3 | import javax.annotation.concurrent.NotThreadSafe; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | import de.invesdwin.context.python.runtime.contract.InputsAndResultsTests; 8 | import de.invesdwin.context.python.runtime.contract.callback.ParametersAndReturnsTests; 9 | import de.invesdwin.context.test.ATest; 10 | import jakarta.inject.Inject; 11 | 12 | @NotThreadSafe 13 | public class Python4jScriptTaskRunnerPythonTest extends ATest { 14 | 15 | @Inject 16 | private Python4jScriptTaskRunnerPython runner; 17 | 18 | @Test 19 | public void test() { 20 | new InputsAndResultsTests(runner).test(); 21 | } 22 | 23 | @Test 24 | public void testParallel() { 25 | new InputsAndResultsTests(runner).testParallel(); 26 | } 27 | 28 | @Test 29 | public void testCallback() { 30 | new ParametersAndReturnsTests(runner).test(); 31 | } 32 | 33 | @Test 34 | public void testCallbackParallel() { 35 | new ParametersAndReturnsTests(runner).testParallel(); 36 | } 37 | 38 | @Test 39 | public void testSimpleCallback() { 40 | new SimpleCallbackTest(runner).testSimpleCallback(); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /invesdwin-context-python-parent/invesdwin-context-python-runtime-python4j/src/test/java/de/invesdwin/context/python/runtime/python4j/SimpleCallbackTest.py: -------------------------------------------------------------------------------- 1 | print("putUuid") 2 | print(putUuid) 3 | 4 | getSecretStaticCallback = callback("getSecretStatic", putUuid) 5 | print("getSecretStaticCallback") 6 | print(getSecretStaticCallback) 7 | 8 | getSecretCallback = callback("getSecret", putUuid) 9 | print("getSecretCallback") 10 | print(getSecretCallback) 11 | 12 | getSecretExpressionCallback = callback("getSecretExpression", putUuid) 13 | print("getSecretExpressionCallback") 14 | print(getSecretExpressionCallback) 15 | 16 | callback("voidMethod") 17 | 18 | callManyParams = callback("callManyParams", True, 2, 3, '4', 5, 6, 7.0, 8.0, "123456789", 10.0) 19 | if(callManyParams != 55): 20 | raise Exception("callManyParams unexpected result: "+callManyParams) 21 | callManyParamsExpression = callback("callManyParamsExpression", True, 2, 3, '4', 5, 6, 7.0, 8.0, "123456789", 10.0) 22 | if(callManyParamsExpression != 55): 23 | raise Exception("callManyParamsExpression unexpected result: "+callManyParamsExpression) 24 | callManyParamsExpressionMultiline = callback("callManyParamsExpressionMultiline", True, 2, 3, '4', 5, 6, 7.0, 8.0, "123456789", 10.0) 25 | if(callManyParamsExpressionMultiline != 55): 26 | raise Exception("callManyParamsExpressionMultiline unexpected result: "+callManyParamsExpressionMultiline) 27 | 28 | getManyParamsExpression = putManyParamsExpression 29 | print("getManyParamsExpression") 30 | print(getManyParamsExpression) 31 | getManyParamsExpressionMultilineWrong = putManyParamsExpressionMultilineWrong 32 | print("getManyParamsExpressionMultilineWrong") 33 | print(getManyParamsExpressionMultilineWrong) 34 | getManyParamsExpressionMultiline = putManyParamsExpressionMultiline 35 | print("getManyParamsExpressionMultiline") 36 | print(getManyParamsExpressionMultiline) 37 | --------------------------------------------------------------------------------