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