--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2012 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.ui.diagram.monitor;\r
+\r
+import org.simantics.common.format.Formatter;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.datatypes.literal.DecimalFormatFormatter;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.BinaryRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.exception.VariableException;\r
+import org.simantics.db.layer0.variable.RVI;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.operation.Layer0X;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ResolveMonitorVariable extends BinaryRead<Resource, Resource, MonitorVariable> {\r
+\r
+ public ResolveMonitorVariable(Resource runtimeDiagram, Resource monitorElement) {\r
+ super(runtimeDiagram, monitorElement);\r
+ }\r
+\r
+ @Override\r
+ public MonitorVariable perform(ReadGraph graph) throws DatabaseException {\r
+ Boolean external = null;\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+\r
+ Resource monitorComponent = graph.getPossibleObject(parameter2, DIA.HasMonitorComponent);\r
+ if (monitorComponent == null)\r
+ return null;\r
+\r
+ String suffix = graph.getPossibleRelatedValue(parameter2, DIA.HasMonitorSuffix, Bindings.STRING);\r
+ if (suffix == null)\r
+ return null;\r
+\r
+ String diagramVariable = graph.getPossibleRelatedValue(parameter, DIA.RuntimeDiagram_HasVariable);\r
+ if (diagramVariable == null)\r
+ return null;\r
+\r
+ Variable var = Variables.getPossibleVariable(graph, diagramVariable);\r
+ if (var == null)\r
+ return null;\r
+\r
+ Variable component = var.browsePossible(graph, monitorComponent);\r
+ if (component == null) {\r
+ // The monitored component is not in this diagram.\r
+ component = Variables.getPossibleVariable(graph, monitorComponent);\r
+ if (component == null)\r
+ return null;\r
+\r
+ String componentRVI = Variables.getPossibleRVI(graph, component);\r
+ if (componentRVI == null)\r
+ return null;\r
+\r
+ Resource realization = Variables.getRealization(graph, var);\r
+ String diagramRealizationURI = graph.getPossibleURI(realization);\r
+ if (diagramRealizationURI == null)\r
+ return null;\r
+\r
+ String componentURI = diagramRealizationURI + componentRVI;\r
+ var = Variables.getPossibleVariable(graph, componentURI);\r
+ if (var == null)\r
+ return null;\r
+\r
+ external = Boolean.TRUE;\r
+ } else {\r
+ var = component;\r
+ }\r
+\r
+ var = var.browsePossible(graph, suffix);\r
+ if (var == null)\r
+ return null;\r
+\r
+ Formatter formatter = graph.getPossibleRelatedAdapter(parameter2, DIA.HasFormatter, Formatter.class);\r
+ if (formatter == null)\r
+ formatter = new DecimalFormatFormatter();\r
+\r
+ Layer0X L0X = Layer0X.getInstance(graph);\r
+ String _expression = graph.getPossibleRelatedAdapter(parameter2, L0X.HasExpression, String.class);\r
+ if(_expression == null) _expression = "value";\r
+\r
+ RVI rvi = null;\r
+ try {\r
+ rvi = Variables.getRVI2(graph, var);\r
+ } catch (VariableException e) {\r
+ // No RVI, which is fine for contents that do not originate from the model configuration.\r
+ }\r
+\r
+ return new MonitorVariable(monitorComponent, formatter, _expression, var, rvi, external);\r
+ }\r
+\r
+}
\ No newline at end of file