1 /*******************************************************************************
2 * Copyright (c) 2014 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 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.structural2.queries;
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;
25 * Retrieves a simple description of the location of a structural component
26 * Variable within a context, such as a model or shared ontology.
29 * The location of a component can be:
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>
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
48 * @author Tuukka Lehtonen
49 * @see ComponentLocation
51 public class GetComponentLocation extends UnaryRead<Variable, ComponentLocation> {
53 public GetComponentLocation(Variable component) {
58 public ComponentLocation perform(ReadGraph graph) throws DatabaseException {
59 Layer0 L0 = Layer0.getInstance(graph);
60 StructuralResource2 STR = StructuralResource2.getInstance(graph);
62 boolean isInsideStructure = false;
63 boolean isInsideModelConfiguration = false;
64 boolean hasRepresentation = parameter.getPossibleRepresents(graph) != null;
66 Variable firstRepresentedParent = findFirstParentWithRepresentation(graph, parameter, STR);
67 if (firstRepresentedParent == null)
69 Resource realParentComposite = graph.getPossibleObject(firstRepresentedParent.getRepresents(graph), L0.PartOf);
70 if (realParentComposite == null)
72 isInsideStructure = graph.hasStatement(realParentComposite, STR.Defines);
74 Variable firstParentComposite = findFirstParentComposite(graph, firstRepresentedParent, STR);
75 if (firstParentComposite != null) {
76 Variable configurationContext = Variables.getPossibleConfigurationContext(graph, firstParentComposite);
77 RVI rvi = firstParentComposite.getPossibleRVI(graph);
79 Variable firstConfigurationParentComposite = rvi.resolvePossible(graph, configurationContext);
80 isInsideModelConfiguration = firstConfigurationParentComposite != null;
84 return new ComponentLocation(isInsideStructure, isInsideModelConfiguration, hasRepresentation);
87 private static Variable findFirstParentWithRepresentation(ReadGraph graph, Variable var, StructuralResource2 STR) throws DatabaseException {
89 Resource represents = var.getPossibleRepresents(graph);
90 if (represents != null)
92 var = var.getParent(graph);
97 private static Variable findFirstParentComposite(ReadGraph graph, Variable var, StructuralResource2 STR) throws DatabaseException {
99 Resource represents = var.getPossibleRepresents(graph);
100 if (represents != null && graph.isInstanceOf(represents, STR.Composite))
102 var = var.getParent(graph);