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