]> gerrit.simantics Code Review - simantics/python.git/blob - org.simantics.pythonlink/scl/Python.scl
Add missing PythonCompatible instances.
[simantics/python.git] / org.simantics.pythonlink / scl / Python.scl
1 effect Python
2     "Simantics/Python/Python"
3     "org.simantics.pythonlink.PythonContext"
4
5 class PythonCompatible a where
6     setPythonVariable :: String -> a -> <Python> ()
7     getPythonVariable :: String -> <Python> a
8
9 importJava "org.simantics.pythonlink.NDArray" where
10     data NDArray
11     
12     @JavaName "<init>"
13     ndarray :: Vector Double -> NDArray
14     @JavaName "<init>"
15     ndarrayM :: Integer -> Integer -> Vector Double -> NDArray
16     @JavaName "<init>"
17     ndarrayN :: Vector Integer -> Vector Double -> NDArray
18     
19     @JavaName size
20     ndarraySize :: NDArray -> Integer
21     @JavaName dims
22     ndarrayDims :: NDArray -> Vector Integer
23     @JavaName getValues
24     ndarrayValues :: NDArray -> Vector Double
25     @JavaName getValue
26     ndarrayElement :: NDArray -> Integer -> Double
27     @JavaName getValue
28     ndarrayElementM :: NDArray -> Integer -> Integer -> Double
29     @JavaName getValue
30     ndarrayElementN :: NDArray -> Vector Integer -> Double
31     
32     @JavaName toString
33     ndarrayToString :: NDArray -> String
34     @JavaName equals
35     ndarrayEquals :: NDArray -> NDArray -> Boolean
36
37 class NDArrayInput a where
38     ndarrayInput :: a -> ([Integer], [Double])
39     
40 instance NDArrayInput [Double] where
41     ndarrayInput v = ([length v], v)
42     
43 instance (NDArrayInput [a]) => NDArrayInput [[a]] where
44     ndarrayInput v = let
45         inp = map ndarrayInput v
46         dims = map fst inp
47         vals = join $ map snd inp
48       in
49         if not (all (== (dims!0)) dims) then
50             fail "List of lists not of uniform dimension"
51           else
52             ([length v] + (dims!0), vals)
53
54 ndarrayFromList :: NDArrayInput a => a -> NDArray
55 ndarrayFromList v = ndarrayN (vector dims) (vector vals) where
56     (dims, vals) = ndarrayInput v
57
58 instance Show NDArray where
59     show = ndarrayToString
60
61 instance Eq NDArray where
62     (==) = ndarrayEquals
63
64 importJava "org.simantics.pythonlink.PythonContext" where
65     data PythonContext
66     
67     @JavaName close
68     closePythonContext :: PythonContext -> <Proc> ()
69     
70     executePythonStatement :: String -> <Python> ()
71     
72     setPythonBooleanVariable :: String -> Boolean -> <Python> ()
73     setPythonIntegerVariable :: String -> Integer -> <Python> ()
74     setPythonLongVariable :: String -> Long -> <Python> ()
75     setPythonDoubleVariable :: String -> Double -> <Python> ()
76     setPythonStringVariable :: String -> String -> <Python> ()
77     
78     setPythonBooleanArrayVariable :: String -> Vector Boolean -> <Python> ()
79     setPythonIntegerArrayVariable :: String -> Vector Integer -> <Python> ()
80     setPythonLongArrayVariable :: String -> Vector Long -> <Python> ()
81     setPythonDoubleArrayVariable :: String -> Vector Double -> <Python> ()
82     setPythonStringArrayVariable :: String -> Vector String -> <Python> ()
83     setPythonNDArrayVariable :: String -> NDArray -> <Python> ()
84     
85     getPythonBooleanVariable :: String -> <Python> Boolean
86     getPythonIntegerVariable :: String -> <Python> Integer
87     getPythonLongVariable :: String -> <Python> Long
88     getPythonDoubleVariable :: String -> <Python> Double
89     getPythonStringVariable :: String -> <Python> String
90     
91     getPythonBooleanArrayVariable :: String -> <Python> Vector Boolean
92     getPythonIntegerArrayVariable :: String -> <Python> Vector Integer
93     getPythonLongArrayVariable :: String -> <Python> Vector Long
94     getPythonDoubleArrayVariable :: String -> <Python> Vector Double
95     getPythonStringArrayVariable  :: String -> <Python> Vector String
96     getPythonNDArrayVariable :: String -> <Python> NDArray
97     
98     setPythonVariantVariable :: String -> Variant -> <Python> ()
99     getPythonVariantVariable :: String -> <Python> Variant
100
101 importJava "org.simantics.pythonlink.Python" where
102     openPythonContext :: <Proc> PythonContext
103     
104     runPythonF :: (() -> <Python, e> a) -> <Proc, e> a
105     runWithPythonContextF :: PythonContext -> (() -> <Python, e> a) -> <Proc, e> a
106
107 runPython :: (<Python, e> a) -> <Proc, e> a
108 runPython v = runPythonF (\_ -> v)
109
110 runWithPythonContext :: PythonContext -> (<Python, e> a) -> <Proc, e> a
111 runWithPythonContext python v = runWithPythonContextF python (\_ -> v) 
112
113 instance (VecComp a, PythonCompatible (Vector a)) => PythonCompatible [a] where
114     setPythonVariable n v = setPythonVariable n (vector v)
115     getPythonVariable = vectorToList . getPythonVariable
116
117 instance PythonCompatible Boolean where
118     getPythonVariable = getPythonBooleanVariable
119     setPythonVariable = setPythonBooleanVariable
120
121 instance PythonCompatible (Vector Boolean) where
122     getPythonVariable = getPythonBooleanArrayVariable
123     setPythonVariable = setPythonBooleanArrayVariable    
124
125 instance PythonCompatible Integer where
126     setPythonVariable = setPythonIntegerVariable
127     getPythonVariable = getPythonIntegerVariable
128     
129 instance PythonCompatible (Vector Integer) where
130     setPythonVariable = setPythonIntegerArrayVariable
131     getPythonVariable = getPythonIntegerArrayVariable
132
133 instance PythonCompatible Long where
134     setPythonVariable = setPythonLongVariable
135     getPythonVariable = getPythonLongVariable
136     
137 instance PythonCompatible (Vector Long) where
138     setPythonVariable = setPythonLongArrayVariable
139     getPythonVariable = getPythonLongArrayVariable
140
141 instance PythonCompatible Double where
142     setPythonVariable = setPythonDoubleVariable
143     getPythonVariable = getPythonDoubleVariable
144     
145 instance PythonCompatible (Vector Double) where
146     setPythonVariable = setPythonDoubleArrayVariable
147     getPythonVariable = getPythonDoubleArrayVariable
148
149 instance PythonCompatible String where
150     setPythonVariable = setPythonStringVariable
151     getPythonVariable = getPythonStringVariable
152     
153 instance PythonCompatible (Vector String) where
154     setPythonVariable = setPythonStringArrayVariable
155     getPythonVariable = getPythonStringArrayVariable
156     
157 instance PythonCompatible NDArray where
158     setPythonVariable = setPythonNDArrayVariable
159     getPythonVariable = getPythonNDArrayVariable