]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/queries/GetComponentLocation.java
Replace scheduleAtFixedRate with scheduleWithFixedDelay
[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 realParentComposite = graph.getPossibleObject(firstRepresentedParent.getRepresents(graph), L0.PartOf);
70         if (realParentComposite == null)
71             return null;
72         isInsideStructure = graph.hasStatement(realParentComposite, STR.Defines);
73
74         Variable firstParentComposite = findFirstParentComposite(graph, firstRepresentedParent, STR);
75         if (firstParentComposite != null) {
76             Variable configurationContext = Variables.getPossibleConfigurationContext(graph, firstParentComposite);
77             RVI rvi = firstParentComposite.getPossibleRVI(graph);
78             if (rvi != null) {
79                 Variable firstConfigurationParentComposite = rvi.resolvePossible(graph, configurationContext);
80                 isInsideModelConfiguration = firstConfigurationParentComposite != null;
81             }
82         }
83
84         return new ComponentLocation(isInsideStructure, isInsideModelConfiguration, hasRepresentation);
85     }
86
87     private static Variable findFirstParentWithRepresentation(ReadGraph graph, Variable var, StructuralResource2 STR) throws DatabaseException {
88         while (var != null) {
89             Resource represents = var.getPossibleRepresents(graph);
90             if (represents != null)
91                 return var;
92             var = var.getParent(graph);
93         }
94         return null;
95     }
96
97     private static Variable findFirstParentComposite(ReadGraph graph, Variable var, StructuralResource2 STR) throws DatabaseException {
98         while (var != null) {
99             Resource represents = var.getPossibleRepresents(graph);
100             if (represents != null && graph.isInstanceOf(represents, STR.Composite))
101                 return var;
102             var = var.getParent(graph);
103         }
104         return null;
105     }
106
107 }