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