]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/subscription/SCLSubscription.java
Layer0Utils.addL0Identifier to prevent possible differentiation of code
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / subscription / SCLSubscription.java
1 /*******************************************************************************
2  * Copyright (c) 2013 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 package org.simantics.modeling.subscription;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.layer0.variable.Variable;
23 import org.simantics.db.layer0.variable.VariableReference;
24 import org.simantics.layer0.Layer0;
25 import org.simantics.modeling.ModelingResources;
26
27 /**
28  * @author Jani Simomaa <jani.simomaa@semantum.fi>
29  */
30 public class SCLSubscription {
31         
32         /**
33      * Creates new folder under given models subscription folder
34      * 
35      * @param model resource of the target model
36      * @param name name of the folder
37      * @return resource of the folder
38      * @throws DatabaseException on database failures
39      */
40         public static Resource addSubscriptionFolder (WriteGraph graph, Resource model, String name) throws DatabaseException {
41                 
42                 NewSubscription folder = new NewSubscription(model, name);
43                 folder.perform(graph);
44                 return folder.subscription;
45         }
46         
47         /**
48      * Adds subscription item with default plotting values to the given subscription resource (folder). 
49      * 
50      * @param subscriptionresource resource of the subscription folder
51      * @param variable variable of the item to be created
52      * @return resource of the subscription item
53      * @throws DatabaseException on database failures
54      */
55         public static Resource addSubscriptionItems (WriteGraph graph, Resource subscriptionresource, Variable variable) throws DatabaseException {
56                                 
57                 VariableReference ref = new VariableReference(variable.getRVI(graph), variable.getDatatype(graph), null);
58                 List<VariableReference> references = new ArrayList<VariableReference>(1);
59                 references.add(ref);
60                 AddSubscriptionItems subitems = new AddSubscriptionItems(subscriptionresource, references);
61                 
62                 subitems.perform(graph);
63                 return subitems.result.get(subitems.result.size() -1);
64         }
65         
66         /**
67      * Adds subscription item to the given subscription resource (folder) with advanced plotting values that user can define.
68      * 
69      * @param subscriptionresource resource of the subscription folder
70      * @param variable variable of the item to be created
71      * @param interval
72      * @param deadband
73      * @param gain
74      * @param bias
75      * @param unit
76      * @param label
77      * @return resource of the subscription item
78      * @throws DatabaseException on database failures
79      */
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 {
81                 
82                 NewSubscriptionItem ns = new NewSubscriptionItem(
83                                 subscriptionresource, 
84                                 interval, 
85                                 deadband, 
86                                 gain, 
87                                 bias, 
88                                 unit, 
89                                 label, 
90                                 variable.getRVI(graph),
91                                 variable.getDatatype(graph)
92                                 );
93                 
94                 ns.perform(graph);
95                 
96                 return ns.subscriptionItem;
97         }
98         
99         /**
100      * Returns the default subscription folder of the given model. If folder with label Default is not found, returns some other subscription folder
101      * 
102      * @param model resource of the target model
103      * @return resource of the subscription folder
104      * @throws DatabaseException on database failures
105      */
106         public static Resource defaultSubscriptionFolder (ReadGraph graph, Resource model) throws DatabaseException {
107                 
108                 Layer0 l0 = Layer0.getInstance(graph);
109                 ModelingResources MR = ModelingResources.getInstance(graph);
110                 Collection<Resource> childrens = graph.getObjects(model, l0.ConsistsOf);
111                 Resource folder = null;
112                 for (final Resource children : childrens) {
113                         
114                         if (graph.isInstanceOf(children, MR.Subscription)) {
115                                 folder = children;
116                                 String label = graph.getRelatedValue2(folder, l0.HasLabel);
117                                 if (label.equals("Default")) {
118                                         return folder;
119                                 }
120                         }                       
121                 }
122                 return folder;
123         }
124         
125         public static String getLabel(ReadGraph graph, Resource subscription) throws DatabaseException {
126                 
127                 return SubscriptionItemLabel.resolveLabel(graph, subscription, true);           
128         }
129 }