1 /*******************************************************************************
2 * Copyright (c) 2007, 2017 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 * Semantum Oy - #7178 Use NameLabelUtil
12 *******************************************************************************/
13 package org.simantics.diagram.query;
15 import java.util.ArrayList;
16 import java.util.Collection;
18 import org.simantics.NameLabelUtil;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.common.request.ResourceRead;
22 import org.simantics.db.common.utils.NameUtils;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.diagram.stubs.DiagramResource;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.layer0.utils.binaryPredicates.OrderedSetElementsPredicate;
27 import org.simantics.modeling.ModelingResources;
28 import org.simantics.structural.stubs.StructuralResource2;
29 import org.simantics.structural2.utils.StructuralUtils;
32 * @author Hannu Niemistö
33 * @author Tuukka Lehtonen
35 public class FlagTextQuery extends ResourceRead<String[]> {
37 private static final boolean DEBUG = false;
39 static final String[] NO_RESULT = new String[] { "?", "?" };
41 public FlagTextQuery(Resource flag) {
46 public String[] perform(ReadGraph g) throws DatabaseException {
47 Layer0 l0 = Layer0.getInstance(g);
48 StructuralResource2 sr = StructuralResource2.getInstance(g);
49 DiagramResource dr = DiagramResource.getInstance(g);
52 System.out.println(getClass().getSimpleName() + "(" + NameUtils.getSafeName(g, resource) + ")");
54 Resource connectionJoin = g.getPossibleObject(resource, dr.FlagIsJoinedBy);
56 System.out.println(getClass().getSimpleName() + " connectionJoin: " + NameUtils.getSafeName(g, connectionJoin));
57 if (connectionJoin == null)
60 Resource diagram = getFirstOwnerDiagram(g, resource);
64 // Get structural composite of flag's diagram.
65 String DiagramToCompositeURI = ModelingResources.URIs.DiagramToComposite;
66 Resource DiagramToComposite = g.getResource(DiagramToCompositeURI);
67 Resource composite = g.getPossibleObject(diagram, DiagramToComposite);
69 System.out.println(getClass().getSimpleName() + " composite: " + NameUtils.getSafeName(g, composite));
70 if (composite == null)
73 // Find the connection that is on this diagram's composite and exclude
74 // it from browsing of connections related to the connection join.
75 Collection<Resource> connectionsOnFlagDiagram = findConnectionOnCompositeFromJoin(g, composite, connectionJoin);
77 for (Resource connection : StructuralUtils.getRelatedConnectionsOfConnectionJoin(g, connectionJoin, connectionsOnFlagDiagram)) {
79 System.out.println(getClass().getSimpleName() + " connection: " + NameUtils.getSafeName(g, connection));
80 for (Resource component : g.getObjects(connection, sr.Connects)) {
82 System.out.println(getClass().getSimpleName() + " connects: " + NameUtils.getSafeName(g, component));
83 // TODO how to correctly resolve flag text in cases where
84 // the other diagram's connection has many attachments ?
86 getSafeLabel(g, g.getSingleObject(component, l0.PartOf)),
87 getSafeLabel(g, component)
95 private Resource getFirstOwnerDiagram(ReadGraph graph, Resource diagramPart) throws DatabaseException {
96 Resource diagram = null;
97 for (Resource temp : OrderedSetElementsPredicate.INSTANCE.getSubjects(graph, resource)) {
102 System.out.println(getClass().getSimpleName() + " getFirstOwnerDiagram(" + NameUtils.getSafeName(graph, diagramPart) + "): " + NameUtils.getSafeName(graph, diagram));
107 * Find the connections that are related to the specified connection join
108 * and on the specified composite.
112 * @param connectionJoin
114 * @throws DatabaseException
116 private Collection<Resource> findConnectionOnCompositeFromJoin(ReadGraph graph, Resource composite, Resource connectionJoin) throws DatabaseException {
117 StructuralResource2 sr = StructuralResource2.getInstance(graph);
118 Collection<Resource> result = new ArrayList<Resource>(1);
119 for (Resource connection : graph.getObjects(connectionJoin, sr.Joins)) {
121 System.out.println(getClass().getSimpleName() + " joins connection: " + NameUtils.getSafeName(graph, connection));
122 if (StructuralUtils.isConnectionInComposite(graph, connection, composite))
123 result.add(connection);
128 public static String getSafeLabel(ReadGraph graph, Resource r) throws DatabaseException {
129 return NameLabelUtil.modalName(graph, r);