Feature #6707

Python integration

Added by Reino Ruusu over 1 year ago. Updated over 1 year ago.

Status:In ProgressStart date:
Priority:4Due date:
Assignee:Reino Ruusu% Done:

0%

Category:InteroperabilitySpent time:-
Target version:-
Release notes:
Story points-
Velocity based estimate-

Description

As a modeller, I want to be able to use existing Python language scripts on the Simantics platform.

This features provides Java and SCL APIs for executing Python scripts and moving data to/from the Python interpreter. Python is executed in embedded mode via a DLL, which provides JNI routines for interfacing to Java.

History

#1 Updated by Reino Ruusu over 1 year ago

Supported functionalities:
  • Create a Python context (in-memory module)
  • Set variable value
  • Get variable value
  • Execute python statement (no return of evaluated value)

Access to contexts provided via a <Python> side effect in SCL.

Execute in a temporary context:

runPython :: <Python, Proc> a -> <Proc> a

Execute using an explicitly created context.

openPythonContext :: () -> <Proc> PythonContext
closePytonContext :: PythonContext -> <Proc> ()
runWithPythonContext :: PythonContext -> <Python, Proc> a -> <Proc> a

Example use:

> runPython do
>     setPythonVariable "foo" 1.0
>     executePythonStatement "bar = foo + 1" 
>     getPythonVariable "bar" :: Double
2.0

At first stage, support is provided for transferring data to/from:
  • Basic Python numeric datatypes
    • int, long, float
  • Homogenous lists of basic datatypes
  • Numpy ndarrays of basic datatypes
Later steps:
  • Access to untyped variables as Variant objects
  • Non-homogenous lists as [Variant]
  • Access to Python dictionaries as java.util.Map objects
  • Access to Python objects as java.util.Map objects (with attribute values)
  • Variable interface to the Python context

#2 Updated by Reino Ruusu over 1 year ago

SCL routines:

import "Vector" 

effect Python
    "Simantics/Python/Python" 
    "org.simantics.pythonlink.PythonContext" 

class PythonCompatible a where
    setPythonVariable :: String -> a -> <Python, Proc> ()
    getPythonVariable :: String -> <Python, Proc> a

importJava "org.simantics.pythonlink.NDArray" where
    data NDArray

    @JavaName "<init>" 
    ndarray :: Vector Double -> NDArray
    @JavaName "<init>" 
    ndarrayM :: Integer -> Integer -> Vector Double -> NDArray
    @JavaName "<init>" 
    ndarrayN :: Vector Integer -> Vector Double -> NDArray

    @JavaName size
    ndarraySize :: NDArray -> Integer
    @JavaName dims
    ndarrayDims :: NDArray -> Vector Integer
    @JavaName getValues
    ndarrayValues :: NDArray -> Vector Double
    @JavaName getValue
    ndarrayElement :: NDArray -> Integer -> Double
    @JavaName getValue
    ndarrayElementM :: NDArray -> Integer -> Integer -> Double
    @JavaName getValue
    ndarrayElementN :: NDArray -> Vector Integer -> Double

    @JavaName toString
    ndarrayToString :: NDArray -> String
    @JavaName equals
    ndarrayEquals :: NDArray -> NDArray -> Boolean

instance Show NDArray where
    show = ndarrayToString

importJava "org.simantics.pythonlink.PythonContext" where
    data PythonContext

    @JavaName close
    closePythonContext :: PythonContext -> <Proc> ()

    executePythonStatement :: String -> <Python, Proc> Integer

    setPythonIntegerVariable :: String -> Integer -> <Python, Proc> ()
    setPythonIntegerArrayVariable :: String -> Vector Integer -> <Python, Proc> ()
    setPythonDoubleVariable :: String -> Double -> <Python, Proc> ()
    setPythonDoubleArrayVariable :: String -> Vector Double -> <Python, Proc> ()
    setPythonStringVariable :: String -> String -> <Python, Proc> ()
    setPythonStringArrayVariable :: String -> Vector String -> <Python, Proc> ()
    setPythonNDArrayVariable :: String -> NDArray -> <Python, Proc> ()

    getPythonIntegerVariable :: String -> <Python, Proc> Integer
    getPythonIntegerArrayVariable :: String -> <Python, Proc> Vector Integer
    getPythonDoubleVariable :: String -> <Python, Proc> Double
    getPythonDoubleArrayVariable :: String -> <Python, Proc> Vector Double
    getPythonStringVariable :: String -> <Python, Proc> String
    getPythonStringArrayVariable  :: String -> <Python, Proc> Vector String
    getPythonNDArrayVariable :: String -> <Python, Proc> NDArray

importJava "org.simantics.pythonlink.Python" where
    openPythonContext :: () -> <Proc> PythonContext

    runPythonF :: (() -> <Python, Proc> a) -> <Proc> a
    runWithPythonContextF :: PythonContext -> (() -> <Python, Proc> a) -> <Proc> a

runPython :: (<Python, Proc> a) -> <Proc> a
runPython v = runPythonF (\_ -> v)

runWithPythonContext :: PythonContext -> (<Python, Proc> a) -> <Proc> a
runWithPythonContext python v = runWithPythonContextF python (\_ -> v) 

instance PythonCompatible Double where
    setPythonVariable = setPythonDoubleVariable
    getPythonVariable = getPythonDoubleVariable

instance PythonCompatible (Vector Double) where
    setPythonVariable = setPythonDoubleArrayVariable
    getPythonVariable = getPythonDoubleArrayVariable

instance PythonCompatible Integer where
    setPythonVariable = setPythonIntegerVariable
    getPythonVariable = getPythonIntegerVariable

instance PythonCompatible (Vector Integer) where
    setPythonVariable = setPythonIntegerArrayVariable
    getPythonVariable = getPythonIntegerArrayVariable

instance PythonCompatible String where
    setPythonVariable = setPythonStringVariable
    getPythonVariable = getPythonStringVariable

instance PythonCompatible (Vector String) where
    setPythonVariable = setPythonStringArrayVariable
    getPythonVariable = getPythonStringArrayVariable

instance PythonCompatible NDArray where
    setPythonVariable = setPythonNDArrayVariable
    getPythonVariable = getPythonNDArrayVariable

#3 Updated by Reino Ruusu over 1 year ago

Initial implementation at https://www.simantics.org/svn/simantics-incubator/reino/
  • org.simantics.pythonlink
  • org.simantics.pythonlink.win32.x86_64
  • org.simantics.pythonlink.feature

#4 Updated by Tuukka Lehtonen over 1 year ago

  • Due date deleted (2016-09-26)
  • Category set to Interoperability
  • Status changed from New to In Progress
  • Target version deleted (1.25.0)
  • Start date deleted (2016-09-26)
  • Release set to 43

Since this is still in incubation, it's not a part of the 1.25.0 release. Moving elsewhere.

#5 Updated by Antti Villberg over 1 year ago

  • Release deleted (43)

#6 Updated by Antti Villberg over 1 year ago

Would you be interested in moving this development to a separate gerrit repository?

#7 Updated by Reino Ruusu over 1 year ago

Antti Villberg wrote:

Would you be interested in moving this development to a separate gerrit repository?

I can do that later today or tomorrow.

#8 Updated by Reino Ruusu over 1 year ago

Was committed yesterday to Gerrit project simantics/python.

https://www.simantics.org:8088/r/#/admin/projects/simantics/python

Also available in: Atom PDF