]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/subscription/SCLSubscription.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / subscription / SCLSubscription.java
1 /*******************************************************************************\r
2  * Copyright (c) 2013 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     Semantum Oy - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.subscription;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 import java.util.List;\r
17 \r
18 import org.simantics.db.ReadGraph;\r
19 import org.simantics.db.Resource;\r
20 import org.simantics.db.WriteGraph;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 import org.simantics.db.layer0.variable.Variable;\r
23 import org.simantics.db.layer0.variable.VariableReference;\r
24 import org.simantics.layer0.Layer0;\r
25 import org.simantics.modeling.ModelingResources;\r
26 \r
27 /**\r
28  * @author Jani Simomaa <jani.simomaa@semantum.fi>\r
29  */\r
30 public class SCLSubscription {\r
31         \r
32         /**\r
33      * Creates new folder under given models subscription folder\r
34      * \r
35      * @param model resource of the target model\r
36      * @param name name of the folder\r
37      * @return resource of the folder\r
38      * @throws DatabaseException on database failures\r
39      */\r
40         public static Resource addSubscriptionFolder (WriteGraph graph, Resource model, String name) throws DatabaseException {\r
41                 \r
42                 NewSubscription folder = new NewSubscription(model, name);\r
43                 folder.perform(graph);\r
44                 return folder.subscription;\r
45         }\r
46         \r
47         /**\r
48      * Adds subscription item with default plotting values to the given subscription resource (folder). \r
49      * \r
50      * @param subscriptionresource resource of the subscription folder\r
51      * @param variable variable of the item to be created\r
52      * @return resource of the subscription item\r
53      * @throws DatabaseException on database failures\r
54      */\r
55         public static Resource addSubscriptionItems (WriteGraph graph, Resource subscriptionresource, Variable variable) throws DatabaseException {\r
56                                 \r
57                 VariableReference ref = new VariableReference(variable.getRVI(graph), variable.getDatatype(graph), null);\r
58                 List<VariableReference> references = new ArrayList<VariableReference>(1);\r
59                 references.add(ref);\r
60                 AddSubscriptionItems subitems = new AddSubscriptionItems(subscriptionresource, references);\r
61                 \r
62                 subitems.perform(graph);\r
63                 return subitems.result.get(subitems.result.size() -1);\r
64         }\r
65         \r
66         /**\r
67      * Adds subscription item to the given subscription resource (folder) with advanced plotting values that user can define.\r
68      * \r
69      * @param subscriptionresource resource of the subscription folder\r
70      * @param variable variable of the item to be created\r
71      * @param interval\r
72      * @param deadband\r
73      * @param gain\r
74      * @param bias\r
75      * @param unit\r
76      * @param label\r
77      * @return resource of the subscription item\r
78      * @throws DatabaseException on database failures\r
79      */\r
80         public static Resource newSubscriptionItem (WriteGraph graph, Resource subscriptionresource, Variable variable, double interval, double deadband, double gain, double bias, String unit, String label) throws DatabaseException {\r
81                 \r
82                 NewSubscriptionItem ns = new NewSubscriptionItem(\r
83                                 subscriptionresource, \r
84                                 interval, \r
85                                 deadband, \r
86                                 gain, \r
87                                 bias, \r
88                                 unit, \r
89                                 label, \r
90                                 variable.getRVI(graph),\r
91                                 variable.getDatatype(graph)\r
92                                 );\r
93                 \r
94                 ns.perform(graph);\r
95                 \r
96                 return ns.subscriptionItem;\r
97         }\r
98         \r
99         /**\r
100      * Returns the default subscription folder of the given model. If folder with label Default is not found, returns some other subscription folder\r
101      * \r
102      * @param model resource of the target model\r
103      * @return resource of the subscription folder\r
104      * @throws DatabaseException on database failures\r
105      */\r
106         public static Resource defaultSubscriptionFolder (ReadGraph graph, Resource model) throws DatabaseException {\r
107                 \r
108                 Layer0 l0 = Layer0.getInstance(graph);\r
109                 ModelingResources MR = ModelingResources.getInstance(graph);\r
110                 Collection<Resource> childrens = graph.getObjects(model, l0.ConsistsOf);\r
111                 Resource folder = null;\r
112                 for (final Resource children : childrens) {\r
113                         \r
114                         if (graph.isInstanceOf(children, MR.Subscription)) {\r
115                                 folder = children;\r
116                                 String label = graph.getRelatedValue2(folder, l0.HasLabel);\r
117                                 if (label.equals("Default")) {\r
118                                         return folder;\r
119                                 }\r
120                         }                       \r
121                 }\r
122                 return folder;\r
123         }\r
124         \r
125         public static String getLabel(ReadGraph graph, Resource subscription) throws DatabaseException {\r
126                 \r
127                 return SubscriptionItemLabel.resolveLabel(graph, subscription, true);           \r
128         }\r
129 }\r