]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.runtime/scl/Databoard.scl
Adding FileSystem and FileSystems to Files SCL-API
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / Databoard.scl
1 import "Prelude"
2 import "Random"
3
4 /// Datatype ///
5
6 "A data type component with component name and data type"
7 @JavaType "org.simantics.databoard.type.Component"
8 @FieldNames [name, "type"]
9 data DatatypeComponent = DatatypeComponent String Datatype
10
11 """A data type that represents the data types supported by the Simantics
12 Databoard plugin."""
13 @JavaType "org.simantics.databoard.type.Datatype"
14 data Datatype =
15     @JavaType "org.simantics.databoard.type.BooleanType"
16     BooleanType
17   | @JavaType "org.simantics.databoard.type.ByteType"
18     ByteType
19   | @JavaType "org.simantics.databoard.type.IntegerType"
20     IntegerType
21   | @JavaType "org.simantics.databoard.type.LongType"
22     LongType
23   | @JavaType "org.simantics.databoard.type.FloatType"
24     FloatType
25   | @JavaType "org.simantics.databoard.type.DoubleType"
26     DoubleType
27   | @JavaType "org.simantics.databoard.type.StringType"
28     StringType
29   | @JavaType "org.simantics.databoard.type.ArrayType"
30     @FieldNames [componentType]
31     ArrayType Datatype
32   | @JavaType "org.simantics.databoard.type.OptionalType"
33     @FieldNames [componentType]
34     OptionalType Datatype
35   | @JavaType "org.simantics.databoard.type.MapType"
36     @FieldNames [keyType, valueType]
37     MapType Datatype Datatype
38   | @JavaType "org.simantics.databoard.type.RecordType"
39     @FieldNames [components]
40     RecordType (Vector DatatypeComponent)
41   | @JavaType "org.simantics.databoard.type.UntionType"
42     @FieldNames [components]
43     UnionType (Vector DatatypeComponent)
44   | @JavaType "org.simantics.databoard.type.VariantType"
45     VariantType
46
47 importJava "org.simantics.databoard.type.Datatype" where
48     @private
49     @JavaName toString
50     showDatatype :: Datatype -> String
51     
52     "Get the number of type components in an data type"
53     @JavaName getComponentCount
54     datatypeCompnentCount :: Datatype -> Integer
55
56     "Get a component type of a composite data type"
57     @JavaName getComponentType
58     datatypeComponentType :: Datatype -> ChildReference -> Datatype
59
60
61 instance Show Datatype where
62     show = showDatatype
63
64 /// Binding ///
65
66 importJava "org.simantics.databoard.binding.Binding" where
67     "Check whether a dynamic object is an instance of a given binding"
68     @JavaName isInstance
69     isBindingInstance :: Binding Dynamic -> Dynamic -> Boolean
70
71     "Create a serializable object from a textual representation"
72     parseValueDefinition :: Serializable a => String -> a
73     
74     "Compare two serializable objects\n\nResult is -1, 0 or 1 depending the partial ordering of the objects."
75     @JavaName compare
76     compareObjects :: Serializable a => a -> a -> Integer
77
78     "The default value of a serializable type"
79     @JavaName createDefault
80     serializableDefaultValue :: Serializable a => a
81     
82     "Create a random value of a serializable type"
83     @JavaName createRandom
84     serializableRandomValue :: Serializable a => <Random> a
85     
86     "Get a textual representation of a serializable value"
87     @JavaName toString
88     showSerializable :: Serializable a => a -> String
89     
90     @private
91     @JavaName getComponent 
92     getSerializableComponent_ :: Serializable a => a -> ChildReference -> Binding b -> b 
93
94     "Get a component binding"
95     @JavaName getComponentBinding
96     getComponentBinding :: Binding a -> ChildReference -> Binding b
97     
98 "Get a child data component of a composite serializable value"
99 getSerializableComponent :: Serializable a => Serializable b => a -> ChildReference -> b
100 getSerializableComponent object ref = getSerializableComponent_ object ref binding
101
102 /// Serializer ///
103
104 importJava "org.simantics.databoard.serialization.Serializer" where
105     "A data serializer for SCL type a" 
106     data Serializer a
107
108     @private
109     @JavaName "serialize"    
110     serialize_ :: Serializer a -> a -> ByteArray
111     
112     @private
113     @JavaName "deserialize"
114     deserialize_ :: Serializer a -> ByteArray -> a
115
116 importJava "org.simantics.databoard.Bindings" where
117     @private
118     @JavaName "getSerializer"
119     serializerOf :: Binding a -> Serializer a
120     
121     @private
122     @JavaName toString
123     bindingToString :: Binding a -> String
124
125     "Adapt between types using explicitly provided binding objects: `adapt_ value from to`"
126     @JavaName adapt
127     adapt_ :: a -> Binding a -> Binding b -> b
128     
129 "Adapt value from one serializable type to another"
130 adapt :: Serializable a => Serializable b => a -> b
131 adapt x = adapt_ x binding binding
132     
133 instance Show (Binding a) where
134     show = bindingToString
135
136 "Serializes a value to a byte array using default serializer."
137 serialize :: Serializable a => a -> ByteArray
138 serialize v = serialize_ (serializerOf binding) v
139
140 "Deserializes a value from a byte array using default serializer."
141 deserialize :: Serializable a => ByteArray -> a
142 deserialize ba = deserialize_ (serializerOf binding) ba
143
144 importJava "org.simantics.databoard.Bindings" where
145     "Get a default binding for a given data type" 
146     @JavaName getBinding    
147     datatypeBinding :: Datatype -> Binding Dynamic
148
149 importJava "org.simantics.databoard.Datatypes" where
150     "Get a data type from a string representation"
151     @JavaName translate
152     translateDatatype :: String -> Datatype
153
154 importJava "org.simantics.databoard.binding.mutable.Variant" where
155     // data Variant (in Builtins)
156     "Create a variant using an explicitly provided binding value (unchecked cast)"
157     @JavaName "<init>"
158     createVariant_ :: Binding Dynamic -> Dynamic -> Variant
159     
160     "Get the data type of a variant object"
161     @JavaName "type"
162     variantDatatype :: Variant -> Datatype
163     
164     "Get raw value contained by a variant (unchecked cast)"
165     @JavaName getValue
166     rawVariantValue :: Variant -> a
167     
168     "Create a variant from a raw object (based on Java class)"
169     @JavaName ofInstance
170     variantOf :: a -> Variant
171     
172     "Create a variant with explicitly provided binding and value"
173     @JavaName "<init>"
174     variant_ :: Binding a -> a -> Variant
175     
176     "Get value from a variant using a given binding"
177     @JavaName getValue
178     variantValue_ :: Variant -> Binding a -> a
179     
180     @private
181     @JavaName toString
182     showVariant :: Variant -> String
183     
184     "Get a component of compound data value in a variant"
185     @JavaName getComponent
186     variantComponent :: Variant -> ChildReference -> Variant
187
188 "Create a variant of a given data type from an object in the default binding (unchecked, use with extreme caution)"
189 createVariant :: Datatype -> Dynamic -> Variant
190 createVariant dt v = createVariant_ (datatypeBinding dt) v
191
192 "Create a variant from a serializable value"
193 variant :: Serializable a => a -> Variant
194 variant v = variant_ binding v
195
196 "Get the value of a variant in a serializable type"
197 variantValue :: Serializable a => Variant -> a
198 variantValue v = variantValue_ v binding
199
200 instance Show Variant where
201     show = showVariant
202
203 "Get an element of a compound variant value using an index reference" 
204 variantElement :: Serializable a => Variant -> Integer -> a
205 variantElement v i = variantValue (variantComponent v (indexReference i))
206
207 importJava "org.simantics.databoard.accessor.reference.ChildReference" where
208     "A reference to a child element in a composite data type/binding or value"
209     data ChildReference
210
211     "Combine a list of child data object references into a single path reference"
212     @JavaName compile    
213     compileReference :: [ChildReference] -> ChildReference
214
215 importJava "org.simantics.databoard.accessor.reference.IndexReference" where
216     """Get a reference to a child data object using an index (zero-based)
217 * Element index of an array object
218 * Field index of a record or union type
219 * 0:
220   * Key component of a map type/binding
221   * Component of any single-component type/binding (optional, array)
222   * Contained value/type of any single-element object (optional, union, variant)
223 * 1:
224   * Value component of a map type/binding
225     """
226     @JavaName "<init>"
227     indexReference :: Integer -> ChildReference
228
229 importJava "org.simantics.databoard.accessor.reference.KeyReference" where
230     """Get a reference to a MapType child data object using a given key value
231 * Contained value of a map object for a given key value
232     """
233     @JavaName "<init>"
234     keyReference :: Variant -> ChildReference
235
236 importJava "org.simantics.databoard.accessor.reference.NameReference" where
237     """Get a reference to a child data object using a field name
238 * A component name of a record or union data type/binding
239 * "key": The key component of a map data type/binding
240 * "value": The value component of a map data type/binding
241     """
242     @JavaName "<init>"
243     nameReference :: String -> ChildReference
244     
245 importJava "org.simantics.databoard.accessor.reference.LabelReference" where
246     """Get a reference to a child data object using a label
247 * A component name of a record or union data type/binding
248 * A string representation of the index of a record or union data type/binding component
249 * "v": The component type of an array/optional data type/binding
250 * "0"/"key": The key component of a map data type/binding
251 * "1"/"value": The value component of a map data type/binding
252     """
253     @JavaName "<init>"
254     labelReference :: String -> ChildReference
255
256 importJava "org.simantics.databoard.accessor.reference.ComponentReference" where
257     """Get a reference to a component child data object
258 * Component of an array/optional data type/binding
259 * Contained value of an optional/variant/union object
260     """
261     @JavaName "<init>"
262     componentReference :: ChildReference