]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/query/FlagTextQuery.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / query / FlagTextQuery.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.diagram.query;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 \r
17 import org.simantics.db.ReadGraph;\r
18 import org.simantics.db.Resource;\r
19 import org.simantics.db.Statement;\r
20 import org.simantics.db.common.request.ResourceRead;\r
21 import org.simantics.db.common.utils.NameUtils;\r
22 import org.simantics.db.exception.DatabaseException;\r
23 import org.simantics.diagram.stubs.DiagramResource;\r
24 import org.simantics.layer0.Layer0;\r
25 import org.simantics.layer0.utils.binaryPredicates.OrderedSetElementsPredicate;\r
26 import org.simantics.modeling.ModelingResources;\r
27 import org.simantics.structural.stubs.StructuralResource2;\r
28 import org.simantics.structural2.utils.StructuralUtils;\r
29 \r
30 /**\r
31  * @author Hannu Niemistö\r
32  * @author Tuukka Lehtonen\r
33  */\r
34 public class FlagTextQuery extends ResourceRead<String[]> {\r
35 \r
36     private static final boolean DEBUG = false;\r
37 \r
38     static final String[] NO_RESULT = new String[] { "?", "?" };\r
39 \r
40     public FlagTextQuery(Resource flag) {\r
41         super(flag);\r
42     }\r
43 \r
44     @Override\r
45     public String[] perform(ReadGraph g) throws DatabaseException {\r
46         Layer0 l0 = Layer0.getInstance(g);\r
47         StructuralResource2 sr = StructuralResource2.getInstance(g);\r
48         DiagramResource dr = DiagramResource.getInstance(g);\r
49 \r
50         if (DEBUG)\r
51             System.out.println(getClass().getSimpleName() + "(" + NameUtils.getSafeName(g, resource) + ")");\r
52 \r
53         Resource connectionJoin = g.getPossibleObject(resource, dr.FlagIsJoinedBy);\r
54         if (DEBUG)\r
55             System.out.println(getClass().getSimpleName() + " connectionJoin: " + NameUtils.getSafeName(g, connectionJoin));\r
56         if (connectionJoin == null)\r
57             return NO_RESULT;\r
58 \r
59         Resource diagram = getFirstOwnerDiagram(g, resource);\r
60         if (diagram == null)\r
61             return NO_RESULT;\r
62 \r
63         // Get structural composite of flag's diagram.\r
64         String DiagramToCompositeURI = ModelingResources.URIs.DiagramToComposite;\r
65         Resource DiagramToComposite = g.getResource(DiagramToCompositeURI);\r
66         Resource composite = g.getPossibleObject(diagram, DiagramToComposite);\r
67         if (DEBUG)\r
68             System.out.println(getClass().getSimpleName() + " composite: " + NameUtils.getSafeName(g, composite));\r
69         if (composite == null)\r
70             return NO_RESULT;\r
71 \r
72         // Find the connection that is on this diagram's composite and exclude\r
73         // it from browsing of connections related to the connection join.\r
74         Collection<Resource> connectionsOnFlagDiagram = findConnectionOnCompositeFromJoin(g, composite, connectionJoin);\r
75 \r
76         for (Resource connection : StructuralUtils.getRelatedConnectionsOfConnectionJoin(g, connectionJoin, connectionsOnFlagDiagram)) {\r
77             if (DEBUG)\r
78                 System.out.println(getClass().getSimpleName() + " connection: " + NameUtils.getSafeName(g, connection));\r
79             for (Resource component : g.getObjects(connection, sr.Connects)) {\r
80                 if (DEBUG)\r
81                     System.out.println(getClass().getSimpleName() + " connects: " + NameUtils.getSafeName(g, component));\r
82                 // TODO how to correctly resolve flag text in cases where\r
83                 // the other diagram's connection has many attachments ?\r
84                 return new String[] {\r
85                         getSafeLabel(g, g.getSingleObject(component, l0.PartOf)),\r
86                         getSafeLabel(g, component)\r
87                 };\r
88             }\r
89         }\r
90 \r
91         return NO_RESULT;\r
92     }\r
93 \r
94     private Resource getFirstOwnerDiagram(ReadGraph graph, Resource diagramPart) throws DatabaseException {\r
95         Resource diagram = null;\r
96         for (Resource temp : OrderedSetElementsPredicate.INSTANCE.getSubjects(graph, resource)) {\r
97             diagram = temp;\r
98             break;\r
99         }\r
100         if (DEBUG)\r
101             System.out.println(getClass().getSimpleName() + " getFirstOwnerDiagram(" + NameUtils.getSafeName(graph, diagramPart) + "): " + NameUtils.getSafeName(graph, diagram));\r
102         return diagram;\r
103     }\r
104 \r
105     /**\r
106      * Find the connections that are related to the specified connection join\r
107      * and on the specified composite.\r
108      * \r
109      * @param graph\r
110      * @param composite\r
111      * @param connectionJoin\r
112      * @return\r
113      * @throws DatabaseException\r
114      */\r
115     private Collection<Resource> findConnectionOnCompositeFromJoin(ReadGraph graph, Resource composite, Resource connectionJoin) throws DatabaseException {\r
116         StructuralResource2 sr = StructuralResource2.getInstance(graph);\r
117         Collection<Resource> result = new ArrayList<Resource>(1);\r
118         for (Resource connection : graph.getObjects(connectionJoin, sr.Joins)) {\r
119             if (DEBUG)\r
120                 System.out.println(getClass().getSimpleName() + " joins connection: " + NameUtils.getSafeName(graph, connection));\r
121             if (StructuralUtils.isConnectionInComposite(graph, connection, composite))\r
122                 result.add(connection);\r
123         }\r
124         return result;\r
125     }\r
126 \r
127     public static String getSafeLabel(ReadGraph graph, Resource r) throws DatabaseException {\r
128         Layer0 l0 = Layer0.getInstance(graph);\r
129         Statement stm = graph.getPossibleStatement(r, l0.HasLabel);\r
130         if (stm != null) {\r
131             String label = NameUtils.getSafeLabel(graph, r);\r
132             if (!label.isEmpty())\r
133                 return label;\r
134             //if (!stm.isAsserted(r))\r
135             //    return label;\r
136         }\r
137         return NameUtils.getSafeName(graph, r);\r
138     }\r
139 \r
140 }\r