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