1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.query;
14 import java.util.ArrayList;
15 import java.util.Collection;
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;
31 * @author Hannu Niemistö
32 * @author Tuukka Lehtonen
34 public class FlagTextQuery extends ResourceRead<String[]> {
36 private static final boolean DEBUG = false;
38 static final String[] NO_RESULT = new String[] { "?", "?" };
40 public FlagTextQuery(Resource flag) {
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);
51 System.out.println(getClass().getSimpleName() + "(" + NameUtils.getSafeName(g, resource) + ")");
53 Resource connectionJoin = g.getPossibleObject(resource, dr.FlagIsJoinedBy);
55 System.out.println(getClass().getSimpleName() + " connectionJoin: " + NameUtils.getSafeName(g, connectionJoin));
56 if (connectionJoin == null)
59 Resource diagram = getFirstOwnerDiagram(g, resource);
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);
68 System.out.println(getClass().getSimpleName() + " composite: " + NameUtils.getSafeName(g, composite));
69 if (composite == null)
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);
76 for (Resource connection : StructuralUtils.getRelatedConnectionsOfConnectionJoin(g, connectionJoin, connectionsOnFlagDiagram)) {
78 System.out.println(getClass().getSimpleName() + " connection: " + NameUtils.getSafeName(g, connection));
79 for (Resource component : g.getObjects(connection, sr.Connects)) {
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 ?
85 getSafeLabel(g, g.getSingleObject(component, l0.PartOf)),
86 getSafeLabel(g, component)
94 private Resource getFirstOwnerDiagram(ReadGraph graph, Resource diagramPart) throws DatabaseException {
95 Resource diagram = null;
96 for (Resource temp : OrderedSetElementsPredicate.INSTANCE.getSubjects(graph, resource)) {
101 System.out.println(getClass().getSimpleName() + " getFirstOwnerDiagram(" + NameUtils.getSafeName(graph, diagramPart) + "): " + NameUtils.getSafeName(graph, diagram));
106 * Find the connections that are related to the specified connection join
107 * and on the specified composite.
111 * @param connectionJoin
113 * @throws DatabaseException
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)) {
120 System.out.println(getClass().getSimpleName() + " joins connection: " + NameUtils.getSafeName(graph, connection));
121 if (StructuralUtils.isConnectionInComposite(graph, connection, composite))
122 result.add(connection);
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);
131 String label = NameUtils.getSafeLabel(graph, r);
132 if (!label.isEmpty())
134 //if (!stm.isAsserted(r))
137 return NameUtils.getSafeName(graph, r);