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
10 * Semantum Oy - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.structural2.queries;
\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
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
29 * The location of a component can be:
\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
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
48 * @author Tuukka Lehtonen
\r
49 * @see ComponentLocation
\r
51 public class GetComponentLocation extends UnaryRead<Variable, ComponentLocation> {
\r
53 public GetComponentLocation(Variable component) {
\r
58 public ComponentLocation perform(ReadGraph graph) throws DatabaseException {
\r
59 Layer0 L0 = Layer0.getInstance(graph);
\r
60 StructuralResource2 STR = StructuralResource2.getInstance(graph);
\r
62 boolean isInsideStructure = false;
\r
63 boolean isInsideModelConfiguration = false;
\r
64 boolean hasRepresentation = parameter.getPossibleRepresents(graph) != null;
\r
66 Variable firstRepresentedParent = findFirstParentWithRepresentation(graph, parameter, STR);
\r
67 if (firstRepresentedParent == null)
\r
69 Resource realParentComposite = graph.getPossibleObject(firstRepresentedParent.getRepresents(graph), L0.PartOf);
\r
70 if (realParentComposite == null)
\r
72 isInsideStructure = graph.hasStatement(realParentComposite, STR.Defines);
\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
79 Variable firstConfigurationParentComposite = rvi.resolvePossible(graph, configurationContext);
\r
80 isInsideModelConfiguration = firstConfigurationParentComposite != null;
\r
84 return new ComponentLocation(isInsideStructure, isInsideModelConfiguration, hasRepresentation);
\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
92 var = var.getParent(graph);
\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
102 var = var.getParent(graph);
\r