1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
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 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.charts.query;
14 import java.util.UUID;
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;
40 * This query returns TrendItem description
42 * @author toni.kalajainen
44 public class TrendItemQuery extends ResourceRead<TrendItem> {
46 public TrendItemQuery(Resource resource) {
50 @SuppressWarnings("unused")
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);
58 TrendItem item = new TrendItem();
60 item.label = ChartItemLabelRule.INSTANCE.getLabel_(graph, resource);
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;
76 item.scale = new Scale.Auto();
81 Integer index = graph.getPossibleRelatedValue(resource, CHART.Chart_Item_Index);
82 item.index = index != null ? index : 1;
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;
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;
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;
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) );
133 if (item.simpleLabel==null) item.simpleLabel = "";
134 if (subscriptionItem != null)
138 item.groupItemId = graph.getPossibleRelatedValue(subscriptionItem, L0.HasName, Bindings.STRING);
139 if (item.groupItemId == null) break SubscriptionItem;
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();
146 Variable configuration = Variables.getPossibleConfigurationContext(graph, subscription);
147 if (configuration == null) break SubscriptionItem;
149 Resource activeExperiment = graph.syncRequest(new PossibleActiveExperiment(subscription));
150 if (activeExperiment != null) {
151 Variable experimentVariable = null;
153 experimentVariable = Variables.switchRealization(graph, configuration, activeExperiment);
154 } catch (DatabaseException e) {
155 experimentVariable = Variables.switchPossibleContext(graph, configuration, activeExperiment);
157 if (experimentVariable != null) {
158 configuration = experimentVariable;
162 item.variableId = item.variableReference = rvi.toPossibleString(graph, configuration);
163 if (item.variableId == null) break SubscriptionItem;
165 if (item.simpleLabel.isEmpty()) {
166 item.simpleLabel = SubscriptionItemLabel.removeVariablePrefixPath( item.variableId );
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);
174 item.label2 = rvi.asString(graph, base);
175 if (!item.label2.isEmpty() && item.label2.charAt(0)=='/') item.label2 = item.label2.substring(1);
177 item.label2 = rvi.toString();
182 if (subscription!=null) {
183 item.groupId = graph.getPossibleRelatedValue(subscription, L0.HasName, Bindings.STRING);
185 item.groupId = UUID.randomUUID().toString();
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);
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 + "]";
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 = "";