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