From 1ea9a623d145875880b543e8eea62e801561fba9 Mon Sep 17 00:00:00 2001 From: tkorvola Date: Thu, 2 Feb 2017 13:01:37 +0200 Subject: [PATCH] Add missing PythonCompatible instances. Boolean and Long were missing, also Vectors of them. Change-Id: I8af943c78924721fb281a66b468f0dfbe530a7a2 --- org.simantics.pythonlink/scl/Python.scl | 302 +++++++++++++----------- 1 file changed, 159 insertions(+), 143 deletions(-) diff --git a/org.simantics.pythonlink/scl/Python.scl b/org.simantics.pythonlink/scl/Python.scl index 5fc7831..421ecc8 100644 --- a/org.simantics.pythonlink/scl/Python.scl +++ b/org.simantics.pythonlink/scl/Python.scl @@ -1,143 +1,159 @@ -effect Python - "Simantics/Python/Python" - "org.simantics.pythonlink.PythonContext" - -class PythonCompatible a where - setPythonVariable :: String -> a -> () - getPythonVariable :: String -> a - -importJava "org.simantics.pythonlink.NDArray" where - data NDArray - - @JavaName "" - ndarray :: Vector Double -> NDArray - @JavaName "" - ndarrayM :: Integer -> Integer -> Vector Double -> NDArray - @JavaName "" - 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 -> () - - executePythonStatement :: String -> () - - setPythonBooleanVariable :: String -> Boolean -> () - setPythonIntegerVariable :: String -> Integer -> () - setPythonLongVariable :: String -> Long -> () - setPythonDoubleVariable :: String -> Double -> () - setPythonStringVariable :: String -> String -> () - - setPythonBooleanArrayVariable :: String -> Vector Boolean -> () - setPythonIntegerArrayVariable :: String -> Vector Integer -> () - setPythonLongArrayVariable :: String -> Vector Long -> () - setPythonDoubleArrayVariable :: String -> Vector Double -> () - setPythonStringArrayVariable :: String -> Vector String -> () - setPythonNDArrayVariable :: String -> NDArray -> () - - getPythonBooleanVariable :: String -> Boolean - getPythonIntegerVariable :: String -> Integer - getPythonLongVariable :: String -> Long - getPythonDoubleVariable :: String -> Double - getPythonStringVariable :: String -> String - - getPythonBooleanArrayVariable :: String -> Vector Boolean - getPythonIntegerArrayVariable :: String -> Vector Integer - getPythonLongArrayVariable :: String -> Vector Long - getPythonDoubleArrayVariable :: String -> Vector Double - getPythonStringArrayVariable :: String -> Vector String - getPythonNDArrayVariable :: String -> NDArray - - setPythonVariantVariable :: String -> Variant -> () - getPythonVariantVariable :: String -> Variant - -importJava "org.simantics.pythonlink.Python" where - openPythonContext :: PythonContext - - runPythonF :: (() -> a) -> a - runWithPythonContextF :: PythonContext -> (() -> a) -> a - -runPython :: ( a) -> a -runPython v = runPythonF (\_ -> v) - -runWithPythonContext :: PythonContext -> ( a) -> 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 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 +effect Python + "Simantics/Python/Python" + "org.simantics.pythonlink.PythonContext" + +class PythonCompatible a where + setPythonVariable :: String -> a -> () + getPythonVariable :: String -> a + +importJava "org.simantics.pythonlink.NDArray" where + data NDArray + + @JavaName "" + ndarray :: Vector Double -> NDArray + @JavaName "" + ndarrayM :: Integer -> Integer -> Vector Double -> NDArray + @JavaName "" + 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 -> () + + executePythonStatement :: String -> () + + setPythonBooleanVariable :: String -> Boolean -> () + setPythonIntegerVariable :: String -> Integer -> () + setPythonLongVariable :: String -> Long -> () + setPythonDoubleVariable :: String -> Double -> () + setPythonStringVariable :: String -> String -> () + + setPythonBooleanArrayVariable :: String -> Vector Boolean -> () + setPythonIntegerArrayVariable :: String -> Vector Integer -> () + setPythonLongArrayVariable :: String -> Vector Long -> () + setPythonDoubleArrayVariable :: String -> Vector Double -> () + setPythonStringArrayVariable :: String -> Vector String -> () + setPythonNDArrayVariable :: String -> NDArray -> () + + getPythonBooleanVariable :: String -> Boolean + getPythonIntegerVariable :: String -> Integer + getPythonLongVariable :: String -> Long + getPythonDoubleVariable :: String -> Double + getPythonStringVariable :: String -> String + + getPythonBooleanArrayVariable :: String -> Vector Boolean + getPythonIntegerArrayVariable :: String -> Vector Integer + getPythonLongArrayVariable :: String -> Vector Long + getPythonDoubleArrayVariable :: String -> Vector Double + getPythonStringArrayVariable :: String -> Vector String + getPythonNDArrayVariable :: String -> NDArray + + setPythonVariantVariable :: String -> Variant -> () + getPythonVariantVariable :: String -> Variant + +importJava "org.simantics.pythonlink.Python" where + openPythonContext :: PythonContext + + runPythonF :: (() -> a) -> a + runWithPythonContextF :: PythonContext -> (() -> a) -> a + +runPython :: ( a) -> a +runPython v = runPythonF (\_ -> v) + +runWithPythonContext :: PythonContext -> ( a) -> 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 -- 2.47.1