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