-effect Python\r
- "Simantics/Python/Python"\r
- "org.simantics.pythonlink.PythonContext"\r
-\r
-class PythonCompatible a where\r
- setPythonVariable :: String -> a -> <Python> ()\r
- getPythonVariable :: String -> <Python> a\r
-\r
-importJava "org.simantics.pythonlink.NDArray" where\r
- data NDArray\r
- \r
- @JavaName "<init>"\r
- ndarray :: Vector Double -> NDArray\r
- @JavaName "<init>"\r
- ndarrayM :: Integer -> Integer -> Vector Double -> NDArray\r
- @JavaName "<init>"\r
- ndarrayN :: Vector Integer -> Vector Double -> NDArray\r
- \r
- @JavaName size\r
- ndarraySize :: NDArray -> Integer\r
- @JavaName dims\r
- ndarrayDims :: NDArray -> Vector Integer\r
- @JavaName getValues\r
- ndarrayValues :: NDArray -> Vector Double\r
- @JavaName getValue\r
- ndarrayElement :: NDArray -> Integer -> Double\r
- @JavaName getValue\r
- ndarrayElementM :: NDArray -> Integer -> Integer -> Double\r
- @JavaName getValue\r
- ndarrayElementN :: NDArray -> Vector Integer -> Double\r
- \r
- @JavaName toString\r
- ndarrayToString :: NDArray -> String\r
- @JavaName equals\r
- ndarrayEquals :: NDArray -> NDArray -> Boolean\r
-\r
-class NDArrayInput a where\r
- ndarrayInput :: a -> ([Integer], [Double])\r
- \r
-instance NDArrayInput [Double] where\r
- ndarrayInput v = ([length v], v)\r
- \r
-instance (NDArrayInput [a]) => NDArrayInput [[a]] where\r
- ndarrayInput v = let\r
- inp = map ndarrayInput v\r
- dims = map fst inp\r
- vals = join $ map snd inp\r
- in\r
- if not (all (== (dims!0)) dims) then\r
- fail "List of lists not of uniform dimension"\r
- else\r
- ([length v] + (dims!0), vals)\r
-\r
-ndarrayFromList :: NDArrayInput a => a -> NDArray\r
-ndarrayFromList v = ndarrayN (vector dims) (vector vals) where\r
- (dims, vals) = ndarrayInput v\r
-\r
-instance Show NDArray where\r
- show = ndarrayToString\r
-\r
-instance Eq NDArray where\r
- (==) = ndarrayEquals\r
-\r
-importJava "org.simantics.pythonlink.PythonContext" where\r
- data PythonContext\r
- \r
- @JavaName close\r
- closePythonContext :: PythonContext -> <Proc> ()\r
- \r
- executePythonStatement :: String -> <Python> ()\r
- \r
- setPythonBooleanVariable :: String -> Boolean -> <Python> ()\r
- setPythonIntegerVariable :: String -> Integer -> <Python> ()\r
- setPythonLongVariable :: String -> Long -> <Python> ()\r
- setPythonDoubleVariable :: String -> Double -> <Python> ()\r
- setPythonStringVariable :: String -> String -> <Python> ()\r
- \r
- setPythonBooleanArrayVariable :: String -> Vector Boolean -> <Python> ()\r
- setPythonIntegerArrayVariable :: String -> Vector Integer -> <Python> ()\r
- setPythonLongArrayVariable :: String -> Vector Long -> <Python> ()\r
- setPythonDoubleArrayVariable :: String -> Vector Double -> <Python> ()\r
- setPythonStringArrayVariable :: String -> Vector String -> <Python> ()\r
- setPythonNDArrayVariable :: String -> NDArray -> <Python> ()\r
- \r
- getPythonBooleanVariable :: String -> <Python> Boolean\r
- getPythonIntegerVariable :: String -> <Python> Integer\r
- getPythonLongVariable :: String -> <Python> Long\r
- getPythonDoubleVariable :: String -> <Python> Double\r
- getPythonStringVariable :: String -> <Python> String\r
- \r
- getPythonBooleanArrayVariable :: String -> <Python> Vector Boolean\r
- getPythonIntegerArrayVariable :: String -> <Python> Vector Integer\r
- getPythonLongArrayVariable :: String -> <Python> Vector Long\r
- getPythonDoubleArrayVariable :: String -> <Python> Vector Double\r
- getPythonStringArrayVariable :: String -> <Python> Vector String\r
- getPythonNDArrayVariable :: String -> <Python> NDArray\r
- \r
- setPythonVariantVariable :: String -> Variant -> <Python> ()\r
- getPythonVariantVariable :: String -> <Python> Variant\r
-\r
-importJava "org.simantics.pythonlink.Python" where\r
- openPythonContext :: <Proc> PythonContext\r
- \r
- runPythonF :: (() -> <Python, e> a) -> <Proc, e> a\r
- runWithPythonContextF :: PythonContext -> (() -> <Python, e> a) -> <Proc, e> a\r
-\r
-runPython :: (<Python, e> a) -> <Proc, e> a\r
-runPython v = runPythonF (\_ -> v)\r
-\r
-runWithPythonContext :: PythonContext -> (<Python, e> a) -> <Proc, e> a\r
-runWithPythonContext python v = runWithPythonContextF python (\_ -> v) \r
-\r
-instance (VecComp a, PythonCompatible (Vector a)) => PythonCompatible [a] where\r
- setPythonVariable n v = setPythonVariable n (vector v)\r
- getPythonVariable = vectorToList . getPythonVariable\r
- \r
-instance PythonCompatible Double where\r
- setPythonVariable = setPythonDoubleVariable\r
- getPythonVariable = getPythonDoubleVariable\r
- \r
-instance PythonCompatible (Vector Double) where\r
- setPythonVariable = setPythonDoubleArrayVariable\r
- getPythonVariable = getPythonDoubleArrayVariable\r
-\r
-instance PythonCompatible Integer where\r
- setPythonVariable = setPythonIntegerVariable\r
- getPythonVariable = getPythonIntegerVariable\r
- \r
-instance PythonCompatible (Vector Integer) where\r
- setPythonVariable = setPythonIntegerArrayVariable\r
- getPythonVariable = getPythonIntegerArrayVariable\r
- \r
-instance PythonCompatible String where\r
- setPythonVariable = setPythonStringVariable\r
- getPythonVariable = getPythonStringVariable\r
- \r
-instance PythonCompatible (Vector String) where\r
- setPythonVariable = setPythonStringArrayVariable\r
- getPythonVariable = getPythonStringArrayVariable\r
- \r
-instance PythonCompatible NDArray where\r
- setPythonVariable = setPythonNDArrayVariable\r
- getPythonVariable = getPythonNDArrayVariable\r
+effect Python
+ "Simantics/Python/Python"
+ "org.simantics.pythonlink.PythonContext"
+
+class PythonCompatible a where
+ setPythonVariable :: String -> a -> <Python> ()
+ getPythonVariable :: String -> <Python> 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
+
+class NDArrayInput a where
+ ndarrayInput :: a -> ([Integer], [Double])
+
+instance NDArrayInput [Double] where
+ ndarrayInput v = ([length v], v)
+
+instance (NDArrayInput [a]) => NDArrayInput [[a]] where
+ ndarrayInput v = let
+ inp = map ndarrayInput v
+ dims = map fst inp
+ vals = join $ map snd inp
+ in
+ if not (all (== (dims!0)) dims) then
+ fail "List of lists not of uniform dimension"
+ else
+ ([length v] + (dims!0), vals)
+
+ndarrayFromList :: NDArrayInput a => a -> NDArray
+ndarrayFromList v = ndarrayN (vector dims) (vector vals) where
+ (dims, vals) = ndarrayInput v
+
+instance Show NDArray where
+ show = ndarrayToString
+
+instance Eq NDArray where
+ (==) = ndarrayEquals
+
+importJava "org.simantics.pythonlink.PythonContext" where
+ data PythonContext
+
+ @JavaName close
+ closePythonContext :: PythonContext -> <Proc> ()
+
+ executePythonStatement :: String -> <Python> ()
+
+ setPythonBooleanVariable :: String -> Boolean -> <Python> ()
+ setPythonIntegerVariable :: String -> Integer -> <Python> ()
+ setPythonLongVariable :: String -> Long -> <Python> ()
+ setPythonDoubleVariable :: String -> Double -> <Python> ()
+ setPythonStringVariable :: String -> String -> <Python> ()
+
+ setPythonBooleanArrayVariable :: String -> Vector Boolean -> <Python> ()
+ setPythonIntegerArrayVariable :: String -> Vector Integer -> <Python> ()
+ setPythonLongArrayVariable :: String -> Vector Long -> <Python> ()
+ setPythonDoubleArrayVariable :: String -> Vector Double -> <Python> ()
+ setPythonStringArrayVariable :: String -> Vector String -> <Python> ()
+ setPythonNDArrayVariable :: String -> NDArray -> <Python> ()
+
+ getPythonBooleanVariable :: String -> <Python> Boolean
+ getPythonIntegerVariable :: String -> <Python> Integer
+ getPythonLongVariable :: String -> <Python> Long
+ getPythonDoubleVariable :: String -> <Python> Double
+ getPythonStringVariable :: String -> <Python> String
+
+ getPythonBooleanArrayVariable :: String -> <Python> Vector Boolean
+ getPythonIntegerArrayVariable :: String -> <Python> Vector Integer
+ getPythonLongArrayVariable :: String -> <Python> Vector Long
+ getPythonDoubleArrayVariable :: String -> <Python> Vector Double
+ getPythonStringArrayVariable :: String -> <Python> Vector String
+ getPythonNDArrayVariable :: String -> <Python> NDArray
+
+ setPythonVariantVariable :: String -> Variant -> <Python> ()
+ getPythonVariantVariable :: String -> <Python> Variant
+
+importJava "org.simantics.pythonlink.Python" where
+ openPythonContext :: <Proc> PythonContext
+
+ runPythonF :: (() -> <Python, e> a) -> <Proc, e> a
+ runWithPythonContextF :: PythonContext -> (() -> <Python, e> a) -> <Proc, e> a
+
+runPython :: (<Python, e> a) -> <Proc, e> a
+runPython v = runPythonF (\_ -> v)
+
+runWithPythonContext :: PythonContext -> (<Python, e> a) -> <Proc, e> a
+runWithPythonContext python v = runWithPythonContextF python (\_ -> v)
+
+instance (VecComp a, PythonCompatible (Vector a)) => PythonCompatible [a] where
+ setPythonVariable n v = setPythonVariable n (vector v)
+ getPythonVariable = vectorToList . getPythonVariable
+
+instance PythonCompatible Boolean where
+ getPythonVariable = getPythonBooleanVariable
+ setPythonVariable = setPythonBooleanVariable
+
+instance PythonCompatible (Vector Boolean) where
+ getPythonVariable = getPythonBooleanArrayVariable
+ setPythonVariable = setPythonBooleanArrayVariable
+
+instance PythonCompatible Integer where
+ setPythonVariable = setPythonIntegerVariable
+ getPythonVariable = getPythonIntegerVariable
+
+instance PythonCompatible (Vector Integer) where
+ setPythonVariable = setPythonIntegerArrayVariable
+ getPythonVariable = getPythonIntegerArrayVariable
+
+instance PythonCompatible Long where
+ setPythonVariable = setPythonLongVariable
+ getPythonVariable = getPythonLongVariable
+
+instance PythonCompatible (Vector Long) where
+ setPythonVariable = setPythonLongArrayVariable
+ getPythonVariable = getPythonLongArrayVariable
+
+instance PythonCompatible Double where
+ setPythonVariable = setPythonDoubleVariable
+ getPythonVariable = getPythonDoubleVariable
+
+instance PythonCompatible (Vector Double) where
+ setPythonVariable = setPythonDoubleArrayVariable
+ getPythonVariable = getPythonDoubleArrayVariable
+
+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