1 /*******************************************************************************
2 * Copyright (c) 2013 Association for Decentralized Information Management
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
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.subscription;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
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;
28 * @author Jani Simomaa <jani.simomaa@semantum.fi>
30 public class SCLSubscription {
33 * Creates new folder under given models subscription folder
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
40 public static Resource addSubscriptionFolder (WriteGraph graph, Resource model, String name) throws DatabaseException {
42 NewSubscription folder = new NewSubscription(model, name);
43 folder.perform(graph);
44 return folder.subscription;
48 * Adds subscription item with default plotting values to the given subscription resource (folder).
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
55 public static Resource addSubscriptionItems (WriteGraph graph, Resource subscriptionresource, Variable variable) throws DatabaseException {
57 VariableReference ref = new VariableReference(variable.getRVI(graph), variable.getDatatype(graph), null);
58 List<VariableReference> references = new ArrayList<VariableReference>(1);
60 AddSubscriptionItems subitems = new AddSubscriptionItems(subscriptionresource, references);
62 subitems.perform(graph);
63 return subitems.result.get(subitems.result.size() -1);
67 * Adds subscription item to the given subscription resource (folder) with advanced plotting values that user can define.
69 * @param subscriptionresource resource of the subscription folder
70 * @param variable variable of the item to be created
77 * @return resource of the subscription item
78 * @throws DatabaseException on database failures
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 {
82 NewSubscriptionItem ns = new NewSubscriptionItem(
90 variable.getRVI(graph),
91 variable.getDatatype(graph)
96 return ns.subscriptionItem;
100 * Returns the default subscription folder of the given model. If folder with label Default is not found, returns some other subscription folder
102 * @param model resource of the target model
103 * @return resource of the subscription folder
104 * @throws DatabaseException on database failures
106 public static Resource defaultSubscriptionFolder (ReadGraph graph, Resource model) throws DatabaseException {
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) {
114 if (graph.isInstanceOf(children, MR.Subscription)) {
116 String label = graph.getRelatedValue2(folder, l0.HasLabel);
117 if (label.equals("Default")) {
125 public static String getLabel(ReadGraph graph, Resource subscription) throws DatabaseException {
127 return SubscriptionItemLabel.resolveLabel(graph, subscription, true);