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