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