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