]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.db/scl/Simantics/Variables.scl
Merge "(refs #7259) Added Iterator.filter and MSet.filterInPlace"
[simantics/platform.git] / bundles / org.simantics.scl.db / scl / Simantics / Variables.scl
1 include "Simantics/Model"
2 include "Simantics/DB"
3 import "Simantics/DB" as DB
4
5 importJava "org.simantics.db.layer0.variable.Variables" where
6     @JavaName getVariable
7     """
8 Function **variable** converts a variable URI to a variable.
9
10 Example:
11
12     import "Simantics/Variables"
13     import "Apros/Module"
14     model_id = model "Model"
15     my_component = getComponent model_id "PO01"
16     my_uri = uriOf my_component
17     my_variable = variable my_uri
18     my_variable
19     
20     > <variable>
21     """
22     variable :: String -> <ReadGraph> Variable
23     
24     @JavaName getVariable
25     """
26 Function **resourceVariable** converts a resource to a corresponding variable.
27
28 Example:
29     
30     import "Simantics/Variables"
31     import "Apros/Module"
32     model_id = model "Model"
33     my_resource = getComponent model_id "PO01"
34     my_variable = resourceVariable my_resource
35     my_variable
36     
37     > <variable>
38     """
39     resourceVariable :: Resource -> <ReadGraph> Variable
40     
41     @JavaName getModel
42     """
43 Function **modelOfVariable** returns the model, in which the given input variable is located, as **Resource**
44
45 Example:
46
47     import "Simantics/Variables"
48     import "Apros/Module"
49     my_variable = moduleVariable "PO01"
50     my_model = modelOfVariable my_variable
51     my_model
52     
53     > #376833
54     
55     // use function nameOf, which works for resources, to print out the name of the model
56     nameOf(my_model)
57     
58     > "Model"
59
60     """
61     modelOfVariable :: Variable -> <ReadGraph> Model
62
63     @JavaName getModel
64     modelResourceOfVariable :: Variable -> <ReadGraph> Resource
65     
66     @JavaName possibleActiveVariable
67     possibleActiveVariable :: Variable -> <ReadGraph> Maybe Variable
68
69     @JavaName switchPossibleContext
70     switchPossibleContext :: Variable -> Resource -> <ReadGraph> Maybe Variable
71     
72     @JavaName getIndexRoot
73     instanceIndexRoot :: Variable -> <ReadGraph> Resource
74     
75     createValueAccessor :: (Variable -> <ReadGraph> a) -> (Variable -> Binding b -> <ReadGraph> b) -> (Variable -> c -> <WriteGraph> ()) -> (Variable -> d -> Binding d -> <WriteGraph> ()) -> (Variable -> <ReadGraph> Datatype) -> ValueAccessor
76
77     @JavaName getConfigurationContext
78     possibleConfigurationContext :: Resource -> <ReadGraph> Variable
79
80     setRVIProperty :: Variable -> RVI -> <WriteGraph> ()
81     
82 importJava "org.simantics.db.layer0.function.All" where
83
84     standardGetValue1 :: Variable -> <ReadGraph> a
85     standardGetValue2 :: Variable -> Binding a -> <ReadGraph> a
86     standardSetValue2 :: Variable -> a -> <WriteGraph> ()
87     standardSetValue3 :: Variable -> a -> Binding a -> <WriteGraph> ()
88     standardGetDatatype :: Variable -> <ReadGraph> Datatype
89
90 importJava "org.simantics.db.layer0.variable.VariableMap" where
91     data VariableMap
92
93 importJava "org.simantics.db.layer0.variable.ResourceCollectionVariableMap" where
94     @JavaName "<init>"
95     createVariableMap :: [Resource] -> VariableMap
96
97 importJava "org.simantics.db.layer0.variable.ValueAccessor" where
98     data ValueAccessor
99
100 importJava "org.simantics.db.layer0.variable.Variable" where
101     data Variable
102
103     @JavaName getRVI
104     rviOf :: Variable -> <ReadGraph> RVI
105     
106     @JavaName getProperties
107     properties_ :: Variable -> <ReadGraph> Collection Variable
108     
109     @JavaName getProperties
110     propertiesClassified_ :: Variable -> Resource -> <ReadGraph> Collection Variable
111     
112     @JavaName getChildren
113     children_ :: Variable -> <ReadGraph> Collection Variable
114     
115     @JavaName getValue
116     value_ :: Variable -> Binding a -> <ReadGraph> a
117     
118     @JavaName getDatatype
119     datatype :: Variable -> <ReadGraph> Datatype
120     
121     @JavaName getValue
122     untypedValue :: Variable -> <ReadGraph> a
123     
124     @JavaName getPossibleValue
125     possibleVariableValue_ :: Variable -> Binding a -> <ReadGraph> Maybe a
126     
127     @JavaName getPossibleValue
128     untypedPossibleVariableValue :: Variable -> <ReadGraph> Maybe a
129     
130     @private
131     @JavaName getChild
132     child_ :: Variable -> String -> <ReadGraph> Variable
133     
134     @private
135     @JavaName getPossibleChild
136     possibleChild_ :: Variable -> String -> <ReadGraph> Maybe Variable
137
138     browse :: Variable -> String -> <ReadGraph> Variable
139     browsePossible :: Variable -> String -> <ReadGraph> Maybe Variable
140     
141     @JavaName getProperty
142     """
143 Function **property** return the wanted property as **Variable**
144
145 **Input 1:** Module which property we want to obtain as **Variable**
146
147 **Input 2:** Name of the property as **String**
148
149 **Output:** wanted property as **Variable**
150
151 Example
152
153     import "Simantics/Variables"
154     import "Apros/Module"
155     my_property_variable = property (moduleVariable "PO01") "PO11_PRESSURE"
156     my_property_variable
157     
158     > <variable>
159     """
160     property :: Variable -> String -> <ReadGraph> Variable
161     
162     @JavaName getPossibleProperty
163     possibleProperty :: Variable -> String -> <ReadGraph> Maybe Variable
164     
165     @JavaName getPropertyValue
166     propertyValue_ :: Variable -> String -> Binding a -> <ReadGraph> a
167
168     @JavaName getPropertyValue
169     untypedPropertyValue :: Variable -> String -> <ReadGraph> a
170
171     @JavaName getPossiblePropertyValue
172     possiblePropertyValue_ :: Variable -> String -> Binding a -> <ReadGraph> Maybe a
173     
174     @JavaName getPossiblePropertyValue
175     untypedPossiblePropertyValue :: Variable -> String -> <ReadGraph> Maybe a
176     
177     @JavaName getName
178
179     """
180 Function **name** return the name of the input variable as string
181
182 Example:
183     
184     import "Simantics/Variables"
185     import "Apros/Module"
186     name (moduleVariable "PO01")
187     
188     > "PO01"
189     """
190     
191     name :: Variable -> <ReadGraph> String
192     @JavaName getURI
193     """
194 Function **uri** return the uri of given variable. The uri is fetched from the active **Experiment**
195
196 **Input 1:** wanted variable which uri is needed as **Variable**
197
198 **Output:** uri of the variable as **String**
199
200 Example:
201
202     import "Simantics/Variables"
203     import "Apros/Module"
204     uri (moduleVariable "PO01")
205
206     >"http://Projects/Development%20Project/Model/Experiment/8ee6b693-891b-438e-a597-9e15a2634e8b/NewGenericDiagram/PO01"
207
208     """
209     uri :: Variable -> <ReadGraph> String
210     
211     @JavaName getParent
212     """
213 Function **variableParent** returns the name of the parent variable as **Variable**
214
215 Example:
216
217     import "Simantics/Variables"
218     import "Apros/Module"
219     my_variable = moduleVariable "PO01"
220     variableParent my_variable
221     
222     > <variable>
223     
224     name (variableParent my_variable)
225
226     > "NewGenericDiagram"
227     
228     """
229     variableParent :: Variable -> <ReadGraph> Variable
230
231     @JavaName getRepresents
232     
233     """
234 Function **represents** returns the resource of the given input variable
235
236 **Input 1:** **Variable** which resource is wanted to be obtainend
237
238 **Output:** **Resource** of the given input variable.
239
240 Example: Find out the resource of given variable
241
242     import "Simantics/Variables"
243     import "Apros/Module"
244     represents (moduleVariable "PO01")
245
246         > #426013
247
248     """
249     represents :: Variable -> <ReadGraph> Resource
250     
251     @JavaName getPossibleRepresents
252     possibleRepresents :: Variable -> <ReadGraph> Maybe Resource
253     
254     @JavaName setValue
255     setValue :: Variable -> a -> <WriteGraph> ()
256     
257     @JavaName setPropertyValue
258     setPropertyValue_ :: Variable -> String -> a -> Binding a -> <WriteGraph> ()
259     
260     @JavaName getType
261     """
262 Function **getType** returns the type of the input variable as resource
263
264 Example:
265
266     import "Simantics/Variables"
267     import "Apros/Module"
268     getType (moduleVariable "PO01")
269     
270     > #275837
271     
272     nameOf(getType (moduleVariable "PO01"))
273     
274     > "POINT_QF"
275     """
276     getType :: Variable -> <ReadGraph> Resource
277     
278     @JavaName getPossibleType
279     getPossibleType :: Variable -> <ReadGraph> Maybe Resource
280         
281     getPredicateResource :: Variable -> <ReadGraph> Resource
282     
283     getIndexRoot :: Variable -> <ReadGraph> Resource
284
285 value :: Serializable a => Typeable a => Variable -> <ReadGraph> a
286 value var = value_ var binding 
287
288 possibleVariableValue :: Serializable a => Typeable a => Variable -> <ReadGraph> Maybe a
289 possibleVariableValue var = possibleVariableValue_ var binding  
290
291 """
292 Function **propertyValue** finds the value of given property.
293
294 Example: Find out the value of point PO01 attribute PO11_PRESSURE
295
296     import "Simantics/Variables"
297     import "Apros/Module"
298     propertyValue (moduleVariable "PO01") "PO11_PRESSURE" :: Double
299
300         > 0.75
301
302 """
303
304 propertyValue :: Serializable a => Typeable a => Variable -> String -> <ReadGraph> a
305 propertyValue var prop = propertyValue_ var prop binding
306
307 possiblePropertyValue :: Serializable a => Typeable a => Variable -> String -> <ReadGraph> Maybe a
308 possiblePropertyValue var prop = possiblePropertyValue_ var prop binding
309
310 """
311 Function **setPropertyValue** sets wanted to value to given Variable property.
312
313 **Input 1:** Wanted module as **Variable**
314
315 **Input 2:** Wanted property name as **String**
316
317 **Output:** No output, given value is inserted to property 
318
319 Example:
320
321     import "Simantics/Variables"
322     import "Apros/Module"
323     setPropertyValue (moduleVariable "PO01") "PO11_PRESSURE" 0.5
324 """
325
326 setPropertyValue :: Serializable a => Variable -> String -> a -> <WriteGraph> ()
327 setPropertyValue subject prediate value = setPropertyValue_ subject prediate value binding
328
329 """
330 Function **modelVariableOfVariable** returns the model, in which the given input Variable is located, as **Variable**
331
332 Example:
333
334     import "Simantics/Variables"
335     import "Apros/Module"
336     my_model_variable = modelVariableOfVariable (moduleVariable "PO01")
337     my_model_variable
338
339     > <variable>
340     
341     name my_model_variable
342     
343     > "Model"
344 """
345
346 modelVariableOfVariable :: Variable -> <ReadGraph> Variable     
347 modelVariableOfVariable var = variable $ uriOf $ modelOfVariable var
348
349 uniqueChild :: Model -> Resource -> String -> <ReadGraph> Variable
350 uniqueChild model typet childName = do
351     typeName = DB.nameOf typet
352     query = "Types: " + typeName + " AND Name: " + childName
353     moduleResources = searchByQuery model query
354     variable $ uriOf $ moduleResources ! 0
355
356 """
357 Function **properties** returns a list, which contains of the properties of the input variable as **Variable**
358
359 Example 1: print out all the properties of certain point as they are shown in Variable Debugger.
360     
361     import "Simantics/Variables"
362     import "Apros/Module"
363     point_properties_list = properties (moduleVariable "PO01")
364     //print out the names of the properties
365     for point_properties_list (\x -> print(name x) )
366     
367     > IncludedInSimulation
368     > PO11_PRESSURE
369     > IsDesynchronized
370     > PO11_PROPERTY_CALC
371     > PO11_EPM_ROU
372     > PO11_ELEV_FROM_BOT
373     ...
374
375 Example 2: print out only the properties of a point, which name starts as "PO11". Else do nothing.
376
377     import "Simantics/Variables"
378     import "Apros/Module"
379     point_properties_list = properties (moduleVariable "PO01")
380     for point_properties_list (\x -> do
381         //print out the names of the properties, which name start as "PO11"
382         if (take 4 (name x) == "PO11") then do
383             print(name x)
384             ()
385         else do
386             ()
387     )
388     
389     > PO11_PRESSURE
390     > PO11_PROPERTY_CALC
391     > PO11_EPM_ROU
392     > PO11_ELEV_FROM_BOT
393     > PO11_NODE_VELOCITY_CALC
394     ...
395
396 """
397 properties = collectionToList . properties_
398
399 instance Show Variable where
400     show _ = "<variable>"
401     
402 instance Browsable Variable where
403     fromUri = variable
404     uriOf = uri
405     nameOf = name
406     possibleNameOf v = Just (name v)
407     children v = collectionToList (children_ v)
408     parent = variableParent
409     possibleParent v = Just (variableParent v)
410     valueOf v = untypedValue v
411     variantValueOf v = createVariant (datatype v) (untypedValue v :: Dynamic)
412     child = child_
413     possibleChild = possibleChild_
414
415 propertiesClassified :: Variable -> Resource -> <ReadGraph> [Variable]
416 propertiesClassified parent classified = do
417     collectionToList $ propertiesClassified_ parent classified
418
419 importJava "org.simantics.db.layer0.variable.RVI" where
420     data RVI
421     
422     resolvePossible :: RVI -> Variable -> <ReadGraph> Maybe Variable
423
424 instantiateUnder :: Resource -> Resource -> <WriteGraph> Resource
425 instantiateUnder container typeToInstantiate = do
426     fn = (resourceVariable typeToInstantiate)#methods#instantiateUnder :: Resource -> Resource -> <WriteGraph> Resource  
427     fn container typeToInstantiate