]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/runtime/RuntimeVariable.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / runtime / RuntimeVariable.java
diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/runtime/RuntimeVariable.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/runtime/RuntimeVariable.java
new file mode 100644 (file)
index 0000000..3d34466
--- /dev/null
@@ -0,0 +1,111 @@
+/*******************************************************************************\r
+ * Copyright (c) 2010, 2013 Association for Decentralized Information Management\r
+ * in 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
+ *     Semantum Oy - issue #4384\r
+ *******************************************************************************/\r
+package org.simantics.diagram.runtime;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.TernaryRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.request.PossibleActiveRun;\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.layer0.Layer0;\r
+import org.simantics.modeling.ModelingResources;\r
+\r
+/**\r
+ * Extract a triple <Model URI, Variable URI, RVI> from the input arguments if\r
+ * possible.\r
+ * \r
+ * Returns <code>null</code> if it cannot find:\r
+ * <ol>\r
+ * <li>model resource by its URI</li>\r
+ * <li>active realization of the model or base realization as a fallback</li>\r
+ * </ol>\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+class RuntimeVariable extends TernaryRead<Resource, RVI, Resource, RuntimeDiagramDesc> {\r
+\r
+    public RuntimeVariable(Resource model, RVI rvi, Resource diagramResource) {\r
+        super(model, rvi, diagramResource);\r
+    }\r
+\r
+    @Override\r
+    public RuntimeDiagramDesc perform(ReadGraph graph) throws DatabaseException {\r
+\r
+        Resource model = parameter;\r
+        RVI rvi = parameter2;\r
+        Resource diagram = parameter3;\r
+\r
+        Variable variable = null;\r
+        String runtimeProfileURI = null;\r
+\r
+        if (model != null && rvi != null) {\r
+               Layer0 L0 = Layer0.getInstance(graph);\r
+               if (graph.isInstanceOf(model, L0.IndexRoot)) {\r
+                       Variable activeRun = graph.syncRequest(new PossibleActiveRun(model));\r
+                       Variable context = activeRun != null ? activeRun : Variables.getPossibleConfigurationContext(graph, model); \r
+                       if (context != null) variable = rvi.resolvePossible(graph, context);\r
+               }\r
+\r
+        } else {\r
+\r
+               Resource composite = graph.getPossibleObject(diagram, ModelingResources.getInstance(graph).DiagramToComposite);\r
+               if (composite != null) {\r
+                       variable = Variables.getPossibleVariable(graph, composite);\r
+               }\r
+\r
+        }\r
+\r
+        if (model != null) {\r
+            Resource runtimeProfile = getActiveProfile(graph, model, diagram);\r
+            if (runtimeProfile != null) {\r
+                runtimeProfileURI = graph.getPossibleURI(runtimeProfile);\r
+            }\r
+        }\r
+\r
+        return new RuntimeDiagramDesc(makeModelURI(graph, variable, model), makeVariableURI(graph, variable), makeRVIString(graph, variable, rvi), runtimeProfileURI);\r
+\r
+    }\r
+\r
+    String makeModelURI(ReadGraph graph, Variable variable, Resource model) throws DatabaseException {\r
+        if (variable != null) {\r
+            Resource m = Variables.getPossibleIndexRoot(graph, variable);\r
+            return m != null ? graph.getPossibleURI(m) : null;\r
+        }\r
+        return model != null ? graph.getPossibleURI(model) : null;\r
+    }\r
+\r
+    String makeVariableURI(ReadGraph graph, Variable variable) throws DatabaseException {\r
+        return variable != null ? variable.getURI(graph) : null;\r
+    }\r
+\r
+    String makeRVIString(ReadGraph graph, Variable variable, RVI rvi) throws DatabaseException {\r
+        if(variable != null) {\r
+               RVI r = variable.getPossibleRVI(graph); \r
+               if(r != null) return r.toString();\r
+        }\r
+        return rvi != null ? rvi.toString() : null;\r
+    }\r
+\r
+    private static Resource getActiveProfile(ReadGraph graph, Resource model, Resource diagram) throws DatabaseException {\r
+        DiagramResource DIA = DiagramResource.getInstance(graph);\r
+        // This has been disabled to make diagram profile selection model-specific instead of diagram-specific.\r
+//        Resource activeProfile = graph.getPossibleObject(diagram, DIA.HasActiveProfile);\r
+//        if(activeProfile != null) return activeProfile;\r
+        return graph.getPossibleObject(model, DIA.HasActiveProfile);\r
+    }\r
+\r
+}
\ No newline at end of file