]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/queries/GetComponentLocation.java
Fix GetComponentLocation to work with procedural UC instances
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / queries / GetComponentLocation.java
1 /*******************************************************************************
2  * Copyright (c) 2014 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  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.structural2.queries;
13
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.common.request.UnaryRead;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.layer0.variable.RVI;
19 import org.simantics.db.layer0.variable.Variable;
20 import org.simantics.db.layer0.variable.Variables;
21 import org.simantics.layer0.Layer0;
22 import org.simantics.structural.stubs.StructuralResource2;
23
24 /**
25  * Retrieves a simple description of the location of a structural component
26  * Variable within a context, such as a model or shared ontology.
27  * 
28  * <p>
29  * The location of a component can be:
30  * <ol>
31  * <li>within a model configuration or outside of one (
32  * {@link ComponentLocation#insideModelConfiguration})</li>
33  * <li>within a user component's configuration or not (
34  * {@link ComponentLocation#insideStructure})</li>
35  * <li>associated with a database representation or not (
36  * {@link ComponentLocation#hasRepresentation})</li>
37  * </ol>
38  * 
39  * These three traits are mostly orthogonal. Together they tell where in the
40  * model hierarchy a specified component is located. For example, a component
41  * within a user component configuration will be outside model configuration,
42  * inside structure and have a representation. A component within a user
43  * component instance will be inside model configuration, inside structure and
44  * have a representation. A (procedurally) generated component will be inside
45  * model configuration and have no representation and either inside or outside
46  * structure.
47  * 
48  * @author Tuukka Lehtonen
49  * @see ComponentLocation
50  */
51 public class GetComponentLocation extends UnaryRead<Variable, ComponentLocation> {
52
53     public GetComponentLocation(Variable component) {
54         super(component);
55     }
56
57     @Override
58     public ComponentLocation perform(ReadGraph graph) throws DatabaseException {
59         Layer0 L0 = Layer0.getInstance(graph);
60         StructuralResource2 STR = StructuralResource2.getInstance(graph);
61
62         boolean isInsideStructure = false;
63         boolean isInsideModelConfiguration = false;
64         boolean hasRepresentation = parameter.getPossibleRepresents(graph) != null;
65
66         Variable firstRepresentedParent = findFirstParentWithRepresentation(graph, parameter, STR);
67         if (firstRepresentedParent == null)
68             return null;
69         Resource representedParent = firstRepresentedParent.getRepresents(graph);
70         Resource representedParentType = graph.getPossibleType(representedParent, STR.Component);
71         if (representedParentType != null && graph.isInstanceOf(representedParentType, STR.ProceduralComponentType)) {
72             isInsideStructure = !parameter.equals(firstRepresentedParent);
73         } else {
74             Resource realParentComposite = graph.getPossibleObject(representedParent, L0.PartOf);
75             if (realParentComposite == null)
76                 return null;
77             isInsideStructure = graph.hasStatement(realParentComposite, STR.Defines);
78         }
79
80         Variable firstParentComposite = findFirstParentComposite(graph, firstRepresentedParent, STR);
81         if (firstParentComposite != null) {
82             Variable configurationContext = Variables.getPossibleConfigurationContext(graph, firstParentComposite);
83             RVI rvi = firstParentComposite.getPossibleRVI(graph);
84             if (rvi != null) {
85                 Variable firstConfigurationParentComposite = rvi.resolvePossible(graph, configurationContext);
86                 isInsideModelConfiguration = firstConfigurationParentComposite != null;
87             }
88         }
89
90         return new ComponentLocation(isInsideStructure, isInsideModelConfiguration, hasRepresentation);
91     }
92
93     private static Variable findFirstParentWithRepresentation(ReadGraph graph, Variable var, StructuralResource2 STR) throws DatabaseException {
94         while (var != null) {
95             Resource represents = var.getPossibleRepresents(graph);
96             if (represents != null)
97                 return var;
98             var = var.getParent(graph);
99         }
100         return null;
101     }
102
103     private static Variable findFirstParentComposite(ReadGraph graph, Variable var, StructuralResource2 STR) throws DatabaseException {
104         while (var != null) {
105             Resource represents = var.getPossibleRepresents(graph);
106             if (represents != null && graph.isInstanceOf(represents, STR.Composite))
107                 return var;
108             var = var.getParent(graph);
109         }
110         return null;
111     }
112
113 }