--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 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
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.diagram.query;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Statement;\r
+import org.simantics.db.common.request.ResourceRead;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.layer0.utils.binaryPredicates.OrderedSetElementsPredicate;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+import org.simantics.structural2.utils.StructuralUtils;\r
+\r
+/**\r
+ * @author Hannu Niemistö\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class FlagTextQuery extends ResourceRead<String[]> {\r
+\r
+ private static final boolean DEBUG = false;\r
+\r
+ static final String[] NO_RESULT = new String[] { "?", "?" };\r
+\r
+ public FlagTextQuery(Resource flag) {\r
+ super(flag);\r
+ }\r
+\r
+ @Override\r
+ public String[] perform(ReadGraph g) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(g);\r
+ StructuralResource2 sr = StructuralResource2.getInstance(g);\r
+ DiagramResource dr = DiagramResource.getInstance(g);\r
+\r
+ if (DEBUG)\r
+ System.out.println(getClass().getSimpleName() + "(" + NameUtils.getSafeName(g, resource) + ")");\r
+\r
+ Resource connectionJoin = g.getPossibleObject(resource, dr.FlagIsJoinedBy);\r
+ if (DEBUG)\r
+ System.out.println(getClass().getSimpleName() + " connectionJoin: " + NameUtils.getSafeName(g, connectionJoin));\r
+ if (connectionJoin == null)\r
+ return NO_RESULT;\r
+\r
+ Resource diagram = getFirstOwnerDiagram(g, resource);\r
+ if (diagram == null)\r
+ return NO_RESULT;\r
+\r
+ // Get structural composite of flag's diagram.\r
+ String DiagramToCompositeURI = ModelingResources.URIs.DiagramToComposite;\r
+ Resource DiagramToComposite = g.getResource(DiagramToCompositeURI);\r
+ Resource composite = g.getPossibleObject(diagram, DiagramToComposite);\r
+ if (DEBUG)\r
+ System.out.println(getClass().getSimpleName() + " composite: " + NameUtils.getSafeName(g, composite));\r
+ if (composite == null)\r
+ return NO_RESULT;\r
+\r
+ // Find the connection that is on this diagram's composite and exclude\r
+ // it from browsing of connections related to the connection join.\r
+ Collection<Resource> connectionsOnFlagDiagram = findConnectionOnCompositeFromJoin(g, composite, connectionJoin);\r
+\r
+ for (Resource connection : StructuralUtils.getRelatedConnectionsOfConnectionJoin(g, connectionJoin, connectionsOnFlagDiagram)) {\r
+ if (DEBUG)\r
+ System.out.println(getClass().getSimpleName() + " connection: " + NameUtils.getSafeName(g, connection));\r
+ for (Resource component : g.getObjects(connection, sr.Connects)) {\r
+ if (DEBUG)\r
+ System.out.println(getClass().getSimpleName() + " connects: " + NameUtils.getSafeName(g, component));\r
+ // TODO how to correctly resolve flag text in cases where\r
+ // the other diagram's connection has many attachments ?\r
+ return new String[] {\r
+ getSafeLabel(g, g.getSingleObject(component, l0.PartOf)),\r
+ getSafeLabel(g, component)\r
+ };\r
+ }\r
+ }\r
+\r
+ return NO_RESULT;\r
+ }\r
+\r
+ private Resource getFirstOwnerDiagram(ReadGraph graph, Resource diagramPart) throws DatabaseException {\r
+ Resource diagram = null;\r
+ for (Resource temp : OrderedSetElementsPredicate.INSTANCE.getSubjects(graph, resource)) {\r
+ diagram = temp;\r
+ break;\r
+ }\r
+ if (DEBUG)\r
+ System.out.println(getClass().getSimpleName() + " getFirstOwnerDiagram(" + NameUtils.getSafeName(graph, diagramPart) + "): " + NameUtils.getSafeName(graph, diagram));\r
+ return diagram;\r
+ }\r
+\r
+ /**\r
+ * Find the connections that are related to the specified connection join\r
+ * and on the specified composite.\r
+ * \r
+ * @param graph\r
+ * @param composite\r
+ * @param connectionJoin\r
+ * @return\r
+ * @throws DatabaseException\r
+ */\r
+ private Collection<Resource> findConnectionOnCompositeFromJoin(ReadGraph graph, Resource composite, Resource connectionJoin) throws DatabaseException {\r
+ StructuralResource2 sr = StructuralResource2.getInstance(graph);\r
+ Collection<Resource> result = new ArrayList<Resource>(1);\r
+ for (Resource connection : graph.getObjects(connectionJoin, sr.Joins)) {\r
+ if (DEBUG)\r
+ System.out.println(getClass().getSimpleName() + " joins connection: " + NameUtils.getSafeName(graph, connection));\r
+ if (StructuralUtils.isConnectionInComposite(graph, connection, composite))\r
+ result.add(connection);\r
+ }\r
+ return result;\r
+ }\r
+\r
+ public static String getSafeLabel(ReadGraph graph, Resource r) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ Statement stm = graph.getPossibleStatement(r, l0.HasLabel);\r
+ if (stm != null) {\r
+ String label = NameUtils.getSafeLabel(graph, r);\r
+ if (!label.isEmpty())\r
+ return label;\r
+ //if (!stm.isAsserted(r))\r
+ // return label;\r
+ }\r
+ return NameUtils.getSafeName(graph, r);\r
+ }\r
+\r
+}\r