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