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