]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/query/TrendItemQuery.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / query / TrendItemQuery.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.charts.query;
13
14 import java.util.UUID;
15
16 import org.simantics.charts.ontology.ChartResource;
17 import org.simantics.charts.ui.ChartItemLabelRule;
18 import org.simantics.databoard.Bindings;
19 import org.simantics.databoard.Databoard;
20 import org.simantics.databoard.binding.Binding;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.common.request.PossibleTypedParent;
24 import org.simantics.db.common.request.ResourceRead;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.layer0.request.PossibleActiveExperiment;
27 import org.simantics.db.layer0.variable.RVI;
28 import org.simantics.db.layer0.variable.Variable;
29 import org.simantics.db.layer0.variable.Variables;
30 import org.simantics.diagram.stubs.G2DResource;
31 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
32 import org.simantics.layer0.Layer0;
33 import org.simantics.modeling.ModelingResources;
34 import org.simantics.modeling.subscription.SubscriptionItemLabel;
35 import org.simantics.trend.configuration.Scale;
36 import org.simantics.trend.configuration.TrendItem;
37 import org.simantics.trend.configuration.TrendItem.DrawMode;
38
39 /**
40  * This query returns TrendItem description
41  * 
42  * @author toni.kalajainen
43  */
44 public class TrendItemQuery extends ResourceRead<TrendItem> {
45
46         public TrendItemQuery(Resource resource) {
47                 super(resource);
48         }
49
50         @SuppressWarnings("unused")
51         @Override
52         public TrendItem perform(ReadGraph graph) throws DatabaseException {
53         ChartResource CHART = ChartResource.getInstance(graph);
54         G2DResource G2D = G2DResource.getInstance(graph);
55         ModelingResources MOD = ModelingResources.getInstance(graph);
56         Layer0 L0 = Layer0.getInstance(graph);
57
58         TrendItem item = new TrendItem();
59
60         item.label = ChartItemLabelRule.INSTANCE.getLabel_(graph, resource);            
61
62         scale: {
63             Double low = null;
64             Double high = null;
65             Resource sc = graph.getPossibleObject(resource, CHART.Chart_Item_ScaleMode);
66             if (CHART.ScaleMode_AutoScale.equals(sc)) {
67                 item.scale = new Scale.Auto();
68             } else if (CHART.ScaleMode_ManualScale.equals(sc)) {
69                 Scale.Manual manualScale = new Scale.Manual();
70                 item.scale = manualScale;
71                 low = graph.getPossibleRelatedValue(resource, CHART.Chart_Item_ScaleMode_Min, Bindings.DOUBLE);
72                 high = graph.getPossibleRelatedValue(resource, CHART.Chart_Item_ScaleMode_Max, Bindings.DOUBLE);
73                 manualScale.min = low == null ? 0 : low;
74                 manualScale.max = high == null ? 120 : high;
75             } else {
76                 item.scale = new Scale.Auto();
77             }
78         }
79         
80         index: {
81                 Integer index = graph.getPossibleRelatedValue(resource, CHART.Chart_Item_Index);
82             item.index = index != null ? index : 1;
83         }
84
85         renderer: {
86             item.renderer = TrendItem.Renderer.Analog;
87             Resource rend = graph.getPossibleObject(resource, CHART.Chart_Item_Renderer);
88             if (CHART.Renderer_Binary.equals(rend)) {
89                 item.renderer = TrendItem.Renderer.Binary;
90             }
91         }
92
93         drawmode: {
94             Resource dm = graph.getPossibleObject(resource, CHART.Chart_Item_DrawMode);
95             if (CHART.DrawMode_DeviationAndAverage.equals(dm)) {
96                 item.drawMode = DrawMode.DeviationAndAverage;
97             } else if (CHART.DrawMode_Average.equals(dm)) {
98                 item.drawMode = DrawMode.Average;
99 //            } else if (CHART.DrawMode_Median.equals(dm)) {
100 //              item.drawMode = DrawMode.Median;
101             } else if (CHART.DrawMode_Sample.equals(dm)) {
102                 item.drawMode = DrawMode.Sample;
103 //            } else if (CHART.DrawMode_DeviationAndMedian.equals(dm)) {
104 //              item.drawMode = DrawMode.DeviationAndMedian;
105             } else if (CHART.DrawMode_DeviationAndSample.equals(dm)) {
106                 item.drawMode = DrawMode.DeviationAndSample;
107             } else if (CHART.DrawMode_DeviationAndLine.equals(dm)) {
108                 item.drawMode = DrawMode.DeviationAndLine;
109             } else if (CHART.DrawMode_Line.equals(dm)) {
110                 item.drawMode = DrawMode.Line;
111             } else if (CHART.DrawMode_Deviation.equals(dm)) {
112                 item.drawMode = DrawMode.Deviation;
113             }
114         }
115
116         visualAttributes: {
117             Boolean hidden = graph.getPossibleRelatedValue(resource, CHART.Chart_Item_hidden, Bindings.BOOLEAN);
118             item.hidden = hidden != null ? hidden : false;
119             item.customStrokeWidth = DiagramGraphUtil.getPossibleRelatedValue(graph, resource, G2D.HasStrokeWidth, Float.class, null);
120             float[] color = graph.getPossibleRelatedValue(resource, G2D.HasColor, Bindings.FLOAT_ARRAY);
121             if (color != null && color.length >= 3)
122                 item.customColor = color;
123         }
124
125         item.variableId = "";
126         item.groupItemId = "";
127         Resource subscriptionItem = graph.getPossibleObject(resource, CHART.Chart_Item_HasSubscriptionItem);
128         Resource subscription = null;
129         if (subscriptionItem!=null) {
130                 item.simpleLabel = graph.getPossibleRelatedValue(subscriptionItem, L0.HasLabel, Bindings.STRING);
131             subscription = graph.syncRequest( new PossibleTypedParent(subscriptionItem, MOD.Subscription) );
132         }
133         if (item.simpleLabel==null) item.simpleLabel = "";
134         if (subscriptionItem != null) 
135                 SubscriptionItem: { 
136                 
137                         label2: {
138                                 item.groupItemId = graph.getPossibleRelatedValue(subscriptionItem, L0.HasName, Bindings.STRING);
139                                 if (item.groupItemId == null) break SubscriptionItem;
140
141                                 Binding rviBinding = graph.getService(Databoard.class).getBindingUnchecked( RVI.class );
142                             RVI rvi = graph.getPossibleRelatedValue(subscriptionItem, MOD.Subscription_Item_VariableId, rviBinding);
143                             if (rvi == null) break SubscriptionItem;        
144                             //variablePersistentId = rvi.toString();
145                             
146                                         Variable configuration = Variables.getPossibleConfigurationContext(graph, subscription);
147                                         if (configuration == null) break SubscriptionItem;
148                                         
149                                         Resource activeExperiment = graph.syncRequest(new PossibleActiveExperiment(subscription));
150                                         if (activeExperiment != null) {
151                                                 Variable experimentVariable = null;
152                                                 try {
153                                                         experimentVariable = Variables.switchRealization(graph, configuration, activeExperiment);
154                                                 } catch (DatabaseException e) {
155                                                         experimentVariable = Variables.switchPossibleContext(graph, configuration, activeExperiment);
156                                                 }
157                                                 if (experimentVariable != null) {
158                                                         configuration = experimentVariable;
159                                                 }
160                                         }
161
162                                         item.variableId = item.variableReference = rvi.toPossibleString(graph, configuration);
163                             if (item.variableId == null) break SubscriptionItem;
164
165                             if (item.simpleLabel.isEmpty()) {
166                                 item.simpleLabel = SubscriptionItemLabel.removeVariablePrefixPath( item.variableId );
167                             }
168                             /*
169                             SimulationResource SIM = SimulationResource.getInstance(graph);
170                             Resource model = graph.syncRequest( new PossibleTypedParent(resource, SIM.Model) );                     
171                             Variable base = null;
172                             if (model!=null) base = Variables.getConfigurationContext(graph, model);
173                                 if (base != null) {
174                                         item.label2 = rvi.asString(graph, base);
175                                         if (!item.label2.isEmpty() && item.label2.charAt(0)=='/') item.label2 = item.label2.substring(1);
176                                 } else {
177                                         item.label2 = rvi.toString();
178                                 }*/
179                         }
180                         
181                         subscription: {
182                             if (subscription!=null) {
183                                 item.groupId = graph.getPossibleRelatedValue(subscription, L0.HasName, Bindings.STRING);
184                             } else {
185                                 item.groupId = UUID.randomUUID().toString();
186                             }
187                             if (item.label == null || item.label.isEmpty()) {
188                                 item.label = item.simpleLabel;
189                                         if (!item.label.isEmpty() && item.label.charAt(0)=='/') item.label = item.label.substring(1);
190                             }
191                         }
192                         
193                         
194                         unit: {
195                             item.unit = graph.getPossibleRelatedValue(subscriptionItem, MOD.Subscription_Item_Unit, Bindings.STRING);
196                 //            if (item.unit != null && !item.unit.isEmpty())
197                 //                item.label += " [" + item.unit + "]";
198                         }
199         } 
200         
201         if (item.groupItemId==null) item.groupItemId = "";
202         if (item.variableId==null) item.variableId = "";
203         if (item.groupId==null) item.groupId = "";
204         if (item.unit==null) item.unit = "";
205         
206         return item;
207         }
208
209 }