]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2010, 2013 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *     Semantum Oy - issue #4384\r
12  *******************************************************************************/\r
13 package org.simantics.diagram.runtime;\r
14 \r
15 import org.simantics.db.ReadGraph;\r
16 import org.simantics.db.Resource;\r
17 import org.simantics.db.common.request.TernaryRead;\r
18 import org.simantics.db.exception.DatabaseException;\r
19 import org.simantics.db.layer0.request.PossibleActiveRun;\r
20 import org.simantics.db.layer0.variable.RVI;\r
21 import org.simantics.db.layer0.variable.Variable;\r
22 import org.simantics.db.layer0.variable.Variables;\r
23 import org.simantics.diagram.stubs.DiagramResource;\r
24 import org.simantics.layer0.Layer0;\r
25 import org.simantics.modeling.ModelingResources;\r
26 \r
27 /**\r
28  * Extract a triple <Model URI, Variable URI, RVI> from the input arguments if\r
29  * possible.\r
30  * \r
31  * Returns <code>null</code> if it cannot find:\r
32  * <ol>\r
33  * <li>model resource by its URI</li>\r
34  * <li>active realization of the model or base realization as a fallback</li>\r
35  * </ol>\r
36  * \r
37  * @author Tuukka Lehtonen\r
38  */\r
39 class RuntimeVariable extends TernaryRead<Resource, RVI, Resource, RuntimeDiagramDesc> {\r
40 \r
41     public RuntimeVariable(Resource model, RVI rvi, Resource diagramResource) {\r
42         super(model, rvi, diagramResource);\r
43     }\r
44 \r
45     @Override\r
46     public RuntimeDiagramDesc perform(ReadGraph graph) throws DatabaseException {\r
47 \r
48         Resource model = parameter;\r
49         RVI rvi = parameter2;\r
50         Resource diagram = parameter3;\r
51 \r
52         Variable variable = null;\r
53         String runtimeProfileURI = null;\r
54 \r
55         if (model != null && rvi != null) {\r
56                 Layer0 L0 = Layer0.getInstance(graph);\r
57                 if (graph.isInstanceOf(model, L0.IndexRoot)) {\r
58                         Variable activeRun = graph.syncRequest(new PossibleActiveRun(model));\r
59                         Variable context = activeRun != null ? activeRun : Variables.getPossibleConfigurationContext(graph, model); \r
60                         if (context != null) variable = rvi.resolvePossible(graph, context);\r
61                 }\r
62 \r
63         } else {\r
64 \r
65                 Resource composite = graph.getPossibleObject(diagram, ModelingResources.getInstance(graph).DiagramToComposite);\r
66                 if (composite != null) {\r
67                         variable = Variables.getPossibleVariable(graph, composite);\r
68                 }\r
69 \r
70         }\r
71 \r
72         if (model != null) {\r
73             Resource runtimeProfile = getActiveProfile(graph, model, diagram);\r
74             if (runtimeProfile != null) {\r
75                 runtimeProfileURI = graph.getPossibleURI(runtimeProfile);\r
76             }\r
77         }\r
78 \r
79         return new RuntimeDiagramDesc(makeModelURI(graph, variable, model), makeVariableURI(graph, variable), makeRVIString(graph, variable, rvi), runtimeProfileURI);\r
80 \r
81     }\r
82 \r
83     String makeModelURI(ReadGraph graph, Variable variable, Resource model) throws DatabaseException {\r
84         if (variable != null) {\r
85             Resource m = Variables.getPossibleIndexRoot(graph, variable);\r
86             return m != null ? graph.getPossibleURI(m) : null;\r
87         }\r
88         return model != null ? graph.getPossibleURI(model) : null;\r
89     }\r
90 \r
91     String makeVariableURI(ReadGraph graph, Variable variable) throws DatabaseException {\r
92         return variable != null ? variable.getURI(graph) : null;\r
93     }\r
94 \r
95     String makeRVIString(ReadGraph graph, Variable variable, RVI rvi) throws DatabaseException {\r
96         if(variable != null) {\r
97                 RVI r = variable.getPossibleRVI(graph); \r
98                 if(r != null) return r.toString();\r
99         }\r
100         return rvi != null ? rvi.toString() : null;\r
101     }\r
102 \r
103     private static Resource getActiveProfile(ReadGraph graph, Resource model, Resource diagram) throws DatabaseException {\r
104         DiagramResource DIA = DiagramResource.getInstance(graph);\r
105         // This has been disabled to make diagram profile selection model-specific instead of diagram-specific.\r
106 //        Resource activeProfile = graph.getPossibleObject(diagram, DIA.HasActiveProfile);\r
107 //        if(activeProfile != null) return activeProfile;\r
108         return graph.getPossibleObject(model, DIA.HasActiveProfile);\r
109     }\r
110 \r
111 }