1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 * Semantum Oy - issue #4384
12 *******************************************************************************/
13 package org.simantics.diagram.runtime;
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;
28 * Extract a triple <Model URI, Variable URI, RVI> from the input arguments if
31 * Returns <code>null</code> if it cannot find:
33 * <li>model resource by its URI</li>
34 * <li>active realization of the model or base realization as a fallback</li>
37 * @author Tuukka Lehtonen
39 class RuntimeVariable extends TernaryRead<Resource, RVI, Resource, RuntimeDiagramDesc> {
41 public RuntimeVariable(Resource model, RVI rvi, Resource diagramResource) {
42 super(model, rvi, diagramResource);
46 public RuntimeDiagramDesc perform(ReadGraph graph) throws DatabaseException {
48 Resource model = parameter;
50 Resource diagram = parameter3;
52 Variable variable = null;
53 String runtimeProfileURI = null;
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);
65 Resource composite = graph.getPossibleObject(diagram, ModelingResources.getInstance(graph).DiagramToComposite);
66 if (composite != null) {
67 variable = Variables.getPossibleVariable(graph, composite);
73 Resource runtimeProfile = getActiveProfile(graph, model, diagram);
74 if (runtimeProfile != null) {
75 runtimeProfileURI = graph.getPossibleURI(runtimeProfile);
79 return new RuntimeDiagramDesc(makeModelURI(graph, variable, model), makeVariableURI(graph, variable), makeRVIString(graph, variable, rvi), runtimeProfileURI);
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;
88 return model != null ? graph.getPossibleURI(model) : null;
91 String makeVariableURI(ReadGraph graph, Variable variable) throws DatabaseException {
92 return variable != null ? variable.getURI(graph) : null;
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();
100 return rvi != null ? rvi.toString() : null;
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);