]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/subscription/AddSubscription.java
Removed stderr logging from CompileSCLMonitorRequest.
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / subscription / AddSubscription.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * 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  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.subscription;
13
14 import java.util.Collection;
15 import java.util.UUID;
16
17 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
18 import org.eclipse.core.runtime.preferences.InstanceScope;
19 import org.simantics.databoard.Bindings;
20 import org.simantics.databoard.Databoard;
21 import org.simantics.databoard.annotations.Optional;
22 import org.simantics.databoard.binding.Binding;
23 import org.simantics.databoard.type.Datatype;
24 import org.simantics.databoard.util.Bean;
25 import org.simantics.db.Resource;
26 import org.simantics.db.WriteGraph;
27 import org.simantics.db.common.request.ObjectsWithType;
28 import org.simantics.db.common.request.WriteRequest;
29 import org.simantics.db.common.utils.NameUtils;
30 import org.simantics.db.exception.DatabaseException;
31 import org.simantics.db.layer0.util.RemoverUtil;
32 import org.simantics.db.layer0.variable.RVI;
33 import org.simantics.layer0.Layer0;
34 import org.simantics.modeling.ModelingResources;
35 import org.simantics.modeling.preferences.SubscriptionPreferences;
36
37
38 public class AddSubscription extends WriteRequest {
39
40     Resource         container;
41     String           subscriptionName;
42     Collection<Item> items;
43     boolean          overwrite;
44
45     public AddSubscription(Resource container, String subscriptionName, Collection<Item> items, boolean overwrite) {
46         this.container = container;
47         this.subscriptionName = subscriptionName; 
48         this.items = items;
49         this.overwrite = overwrite;
50     }
51
52     @Override
53     public void perform(WriteGraph graph) throws DatabaseException {
54         Layer0 l0 = Layer0.getInstance(graph);
55         ModelingResources mod = ModelingResources.getInstance(graph);
56
57         IEclipsePreferences pn = InstanceScope.INSTANCE.getNode( SubscriptionPreferences.P_NODE );
58         Double defaultSubscriptionItemInterval = pn.getDouble(SubscriptionPreferences.P_SUBSCRIPTION_INTERVAL, SubscriptionPreferences.DEFAULT_SUBSCRIPTION_INTERVAL);
59         Double defaultSubscriptionItemDeadband = pn.getDouble(SubscriptionPreferences.P_SUBSCRIPTION_DEADBAND, SubscriptionPreferences.DEFAULT_SUBSCRIPTION_DEADBAND); 
60
61         if (overwrite) {
62             for (Resource existing : graph.syncRequest(new ObjectsWithType(container, l0.ConsistsOf, mod.Subscription))) {
63                 String name = graph.getPossibleRelatedValue(existing, l0.HasName, Bindings.STRING);
64                 if (subscriptionName.equals(name)) {
65                     RemoverUtil.remove(graph, existing);
66                 }
67             }
68         }
69
70         Resource subscription = graph.newResource();
71         graph.claim(subscription, l0.InstanceOf, null, mod.Subscription);
72         String freshName = NameUtils.findFreshName(graph, subscriptionName, container);
73         graph.claimLiteral(subscription, l0.HasName, freshName);
74         graph.claimLiteral(subscription, l0.HasLabel, freshName);
75         graph.claim(container, l0.ConsistsOf, subscription);
76
77         Binding rviBinding = graph.getService(Databoard.class).getBindingUnchecked( RVI.class );
78         for (Item item : items) {
79             Resource subscriptionItem = graph.newResource();
80             graph.claim(subscriptionItem, l0.InstanceOf, null, mod.Subscription_Item);
81             graph.claim(subscription, l0.ConsistsOf, subscriptionItem);
82             graph.claimLiteral(subscriptionItem, l0.HasName, UUID.randomUUID().toString(), Bindings.STRING);
83             if ( item.label!= null) graph.claimLiteral(subscriptionItem, l0.HasLabel, item.label, Bindings.STRING);
84             
85             graph.claimLiteral(subscriptionItem, mod.Subscription_Item_VariableId, l0.RVI, item.resolvedName, rviBinding);
86
87             Double interval = item.interval!=null ? item.interval : defaultSubscriptionItemInterval;
88             if (interval != null) {
89                 graph.claimLiteral(subscriptionItem, mod.Subscription_Item_SamplingInterval, interval, Bindings.DOUBLE);
90             }
91             
92             Double deadband = item.deadband!=null ? item.deadband : defaultSubscriptionItemDeadband;
93             if (deadband != null) {
94                 graph.claimLiteral(subscriptionItem, mod.Subscription_Item_Deadband, deadband, Bindings.DOUBLE);
95             }
96
97             if (item.gain != null) {
98                 graph.claimLiteral(subscriptionItem, mod.Subscription_Item_Gain, item.gain, Bindings.DOUBLE);
99             }
100             
101             if (item.bias != null) {
102                 graph.claimLiteral(subscriptionItem, mod.Subscription_Item_Deadband, item.bias, Bindings.DOUBLE);
103             }                
104             
105             if (item.datatype != null)
106                 graph.claimLiteral(subscriptionItem, mod.Subscription_Item_Datatype,
107                         l0.DataType, item.datatype,
108                         Bindings.getBindingUnchecked(Datatype.class));
109         }
110     }
111
112     public static class Item extends Bean {
113         public RVI resolvedName;
114         public @Optional Double interval, deadband, gain, bias;
115         public @Optional String label;
116         public @Optional Datatype datatype;
117     }
118
119 }