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
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
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
28 * Extract a triple <Model URI, Variable URI, RVI> from the input arguments if
\r
31 * Returns <code>null</code> if it cannot find:
\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
37 * @author Tuukka Lehtonen
\r
39 class RuntimeVariable extends TernaryRead<Resource, RVI, Resource, RuntimeDiagramDesc> {
\r
41 public RuntimeVariable(Resource model, RVI rvi, Resource diagramResource) {
\r
42 super(model, rvi, diagramResource);
\r
46 public RuntimeDiagramDesc perform(ReadGraph graph) throws DatabaseException {
\r
48 Resource model = parameter;
\r
49 RVI rvi = parameter2;
\r
50 Resource diagram = parameter3;
\r
52 Variable variable = null;
\r
53 String runtimeProfileURI = null;
\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
65 Resource composite = graph.getPossibleObject(diagram, ModelingResources.getInstance(graph).DiagramToComposite);
\r
66 if (composite != null) {
\r
67 variable = Variables.getPossibleVariable(graph, composite);
\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
79 return new RuntimeDiagramDesc(makeModelURI(graph, variable, model), makeVariableURI(graph, variable), makeRVIString(graph, variable, rvi), runtimeProfileURI);
\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
88 return model != null ? graph.getPossibleURI(model) : null;
\r
91 String makeVariableURI(ReadGraph graph, Variable variable) throws DatabaseException {
\r
92 return variable != null ? variable.getURI(graph) : null;
\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
100 return rvi != null ? rvi.toString() : null;
\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