--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2014 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.structural2.queries;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.UnaryRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.RVI;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+\r
+/**\r
+ * Retrieves a simple description of the location of a structural component\r
+ * Variable within a context, such as a model or shared ontology.\r
+ * \r
+ * <p>\r
+ * The location of a component can be:\r
+ * <ol>\r
+ * <li>within a model configuration or outside of one (\r
+ * {@link ComponentLocation#insideModelConfiguration})</li>\r
+ * <li>within a user component's configuration or not (\r
+ * {@link ComponentLocation#insideStructure})</li>\r
+ * <li>associated with a database representation or not (\r
+ * {@link ComponentLocation#hasRepresentation})</li>\r
+ * </ol>\r
+ * \r
+ * These three traits are mostly orthogonal. Together they tell where in the\r
+ * model hierarchy a specified component is located. For example, a component\r
+ * within a user component configuration will be outside model configuration,\r
+ * inside structure and have a representation. A component within a user\r
+ * component instance will be inside model configuration, inside structure and\r
+ * have a representation. A (procedurally) generated component will be inside\r
+ * model configuration and have no representation and either inside or outside\r
+ * structure.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ * @see ComponentLocation\r
+ */\r
+public class GetComponentLocation extends UnaryRead<Variable, ComponentLocation> {\r
+\r
+ public GetComponentLocation(Variable component) {\r
+ super(component);\r
+ }\r
+\r
+ @Override\r
+ public ComponentLocation perform(ReadGraph graph) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
+\r
+ boolean isInsideStructure = false;\r
+ boolean isInsideModelConfiguration = false;\r
+ boolean hasRepresentation = parameter.getPossibleRepresents(graph) != null;\r
+\r
+ Variable firstRepresentedParent = findFirstParentWithRepresentation(graph, parameter, STR);\r
+ if (firstRepresentedParent == null)\r
+ return null;\r
+ Resource realParentComposite = graph.getPossibleObject(firstRepresentedParent.getRepresents(graph), L0.PartOf);\r
+ if (realParentComposite == null)\r
+ return null;\r
+ isInsideStructure = graph.hasStatement(realParentComposite, STR.Defines);\r
+\r
+ Variable firstParentComposite = findFirstParentComposite(graph, firstRepresentedParent, STR);\r
+ if (firstParentComposite != null) {\r
+ Variable configurationContext = Variables.getPossibleConfigurationContext(graph, firstParentComposite);\r
+ RVI rvi = firstParentComposite.getPossibleRVI(graph);\r
+ if (rvi != null) {\r
+ Variable firstConfigurationParentComposite = rvi.resolvePossible(graph, configurationContext);\r
+ isInsideModelConfiguration = firstConfigurationParentComposite != null;\r
+ }\r
+ }\r
+\r
+ return new ComponentLocation(isInsideStructure, isInsideModelConfiguration, hasRepresentation);\r
+ }\r
+\r
+ private static Variable findFirstParentWithRepresentation(ReadGraph graph, Variable var, StructuralResource2 STR) throws DatabaseException {\r
+ while (var != null) {\r
+ Resource represents = var.getPossibleRepresents(graph);\r
+ if (represents != null)\r
+ return var;\r
+ var = var.getParent(graph);\r
+ }\r
+ return null;\r
+ }\r
+\r
+ private static Variable findFirstParentComposite(ReadGraph graph, Variable var, StructuralResource2 STR) throws DatabaseException {\r
+ while (var != null) {\r
+ Resource represents = var.getPossibleRepresents(graph);\r
+ if (represents != null && graph.isInstanceOf(represents, STR.Composite))\r
+ return var;\r
+ var = var.getParent(graph);\r
+ }\r
+ return null;\r
+ }\r
+\r
+}
\ No newline at end of file