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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.structural.ui.compositeViewer;
\r
14 import java.util.HashMap;
\r
16 import org.simantics.db.ReadGraph;
\r
17 import org.simantics.db.Resource;
\r
18 import org.simantics.db.Statement;
\r
19 import org.simantics.db.common.utils.NameUtils;
\r
20 import org.simantics.db.exception.DatabaseException;
\r
21 import org.simantics.db.request.Read;
\r
22 import org.simantics.graphviz.Edge;
\r
23 import org.simantics.graphviz.Graph;
\r
24 import org.simantics.graphviz.Node;
\r
25 import org.simantics.layer0.Layer0;
\r
26 import org.simantics.structural.stubs.StructuralResource2;
\r
28 public class CreateCompositeGraph implements Read<Graph> {
\r
32 public CreateCompositeGraph(Resource composite) {
\r
33 this.composite = composite;
\r
36 static class Connection {
\r
39 public Connection(Node parent, String label) {
\r
40 this.parent = parent;
\r
45 private static String getName(ReadGraph g, Resource r) throws DatabaseException {
\r
46 String s = g.getPossibleRelatedValue(r, Layer0.getInstance(g).HasName);
\r
48 s = NameUtils.getSafeName(g, r);
\r
53 HashMap<Resource, Node> connections = new HashMap<Resource, Node>();
\r
55 private void connect(Node parent, String label, Resource connection) {
\r
56 Node cn = connections.get(connection);
\r
58 cn = new Node(graph);
\r
59 cn.setShape("diamond");
\r
60 cn.setFixedSize(true);
\r
63 connections.put(connection, cn);
\r
65 Edge edge = new Edge(parent, cn);
\r
66 edge.setArrowhead("none");
\r
67 if(label.contains("Self"))
\r
68 edge.setColor("red");
\r
70 edge.setTailLabel(label);
\r
71 edge.set("fontsize", "8");
\r
75 public Graph perform(ReadGraph g) throws DatabaseException {
\r
76 Layer0 L0 = Layer0.getInstance(g);
\r
77 StructuralResource2 STR = StructuralResource2.getInstance(g);
\r
79 graph = new Graph();
\r
80 graph.set("overlap", "false");
\r
81 graph.set("splines", "true");
\r
83 for(Resource child : g.getObjects(composite, L0.ConsistsOf)) {
\r
84 Node node = new Node(graph, getName(g, child));
\r
85 node.setShape("rect");
\r
86 if(g.isInstanceOf(child, STR.Composite)) {
\r
87 for(Resource c : g.getObjects(child, STR.HasConnectionJoin))
\r
88 connect(node, "", c);
\r
91 for(Statement stat : g.getStatements(child, STR.IsConnectedTo)) {
\r
92 String label = getName(g, stat.getPredicate());
\r
93 connect(node, label, stat.getObject());
\r
98 Graph rgraph = graph;
\r
100 connections.clear();
\r