1 /* 2 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 package com.sun.hotspot.igv.layout; 25 26 import java.util.HashSet; 27 import java.util.HashMap; 28 import java.util.Set; 29 import java.util.SortedSet; 30 import java.util.TreeSet; 31 32 /** 33 * 34 * @author Thomas Wuerthinger 35 */ 36 public class LayoutGraph { 37 38 private Set<? extends Link> links; 39 private SortedSet<Vertex> vertices; 40 private HashMap<Vertex, Set<Port>> inputPorts; 41 private HashMap<Vertex, Set<Port>> outputPorts; 42 private HashMap<Port, Set<Link>> portLinks; 43 LayoutGraph(Set<? extends Link> links)44 public LayoutGraph(Set<? extends Link> links) { 45 this(links, new HashSet<Vertex>()); 46 } 47 LayoutGraph(Set<? extends Link> links, Set<? extends Vertex> additionalVertices)48 public LayoutGraph(Set<? extends Link> links, Set<? extends Vertex> additionalVertices) { 49 this.links = links; 50 assert verify(); 51 52 vertices = new TreeSet<Vertex>(); 53 portLinks = new HashMap<Port, Set<Link>>(); 54 inputPorts = new HashMap<Vertex, Set<Port>>(); 55 outputPorts = new HashMap<Vertex, Set<Port>>(); 56 57 for (Link l : links) { 58 Port p = l.getFrom(); 59 Port p2 = l.getTo(); 60 Vertex v1 = p.getVertex(); 61 Vertex v2 = p2.getVertex(); 62 63 if (!vertices.contains(v1)) { 64 65 outputPorts.put(v1, new HashSet<Port>(1)); 66 inputPorts.put(v1, new HashSet<Port>(3)); 67 vertices.add(v1); 68 assert vertices.contains(v1); 69 } 70 71 if (!vertices.contains(v2)) { 72 vertices.add(v2); 73 assert vertices.contains(v2); 74 outputPorts.put(v2, new HashSet<Port>(1)); 75 inputPorts.put(v2, new HashSet<Port>(3)); 76 } 77 78 if (!portLinks.containsKey(p)) { 79 HashSet<Link> hashSet = new HashSet<Link>(3); 80 portLinks.put(p, hashSet); 81 } 82 83 if (!portLinks.containsKey(p2)) { 84 portLinks.put(p2, new HashSet<Link>(3)); 85 } 86 87 outputPorts.get(v1).add(p); 88 inputPorts.get(v2).add(p2); 89 90 portLinks.get(p).add(l); 91 portLinks.get(p2).add(l); 92 } 93 94 for (Vertex v : additionalVertices) { 95 if (!vertices.contains(v)) { 96 outputPorts.put(v, new HashSet<Port>(1)); 97 inputPorts.put(v, new HashSet<Port>(3)); 98 vertices.add(v); 99 vertices.contains(v); 100 } 101 } 102 } 103 getInputPorts(Vertex v)104 public Set<Port> getInputPorts(Vertex v) { 105 return this.inputPorts.get(v); 106 } 107 getOutputPorts(Vertex v)108 public Set<Port> getOutputPorts(Vertex v) { 109 return this.outputPorts.get(v); 110 } 111 getPortLinks(Port p)112 public Set<Link> getPortLinks(Port p) { 113 return portLinks.get(p); 114 } 115 getLinks()116 public Set<? extends Link> getLinks() { 117 return links; 118 } 119 verify()120 public boolean verify() { 121 return true; 122 } 123 getVertices()124 public SortedSet<Vertex> getVertices() { 125 return vertices; 126 } 127 markNotRoot(Set<Vertex> notRootSet, Vertex v, Vertex startingVertex)128 private void markNotRoot(Set<Vertex> notRootSet, Vertex v, Vertex startingVertex) { 129 130 if (notRootSet.contains(v)) { 131 return; 132 } 133 if (v != startingVertex) { 134 notRootSet.add(v); 135 } 136 Set<Port> outPorts = getOutputPorts(v); 137 for (Port p : outPorts) { 138 Set<Link> portLinks = getPortLinks(p); 139 for (Link l : portLinks) { 140 Port other = l.getTo(); 141 Vertex otherVertex = other.getVertex(); 142 if (otherVertex != startingVertex) { 143 markNotRoot(notRootSet, otherVertex, startingVertex); 144 } 145 } 146 } 147 } 148 149 // Returns a set of vertices with the following properties: 150 // - All Vertices in the set startingRoots are elements of the set. 151 // - When starting a DFS at every vertex in the set, every vertex of the 152 // whole graph is visited. findRootVertices(Set<Vertex> startingRoots)153 public Set<Vertex> findRootVertices(Set<Vertex> startingRoots) { 154 155 Set<Vertex> notRootSet = new HashSet<Vertex>(); 156 for (Vertex v : startingRoots) { 157 if (!notRootSet.contains(v)) { 158 markNotRoot(notRootSet, v, v); 159 } 160 } 161 162 Set<Vertex> tmpVertices = getVertices(); 163 for (Vertex v : tmpVertices) { 164 if (!notRootSet.contains(v)) { 165 if (this.getInputPorts(v).size() == 0) { 166 markNotRoot(notRootSet, v, v); 167 } 168 } 169 } 170 171 for (Vertex v : tmpVertices) { 172 if (!notRootSet.contains(v)) { 173 markNotRoot(notRootSet, v, v); 174 } 175 } 176 177 Set<Vertex> result = new HashSet<Vertex>(); 178 for (Vertex v : tmpVertices) { 179 if (!notRootSet.contains(v)) { 180 result.add(v); 181 } 182 } 183 assert tmpVertices.size() == 0 || result.size() > 0; 184 return result; 185 } 186 findRootVertices()187 public Set<Vertex> findRootVertices() { 188 return findRootVertices(new HashSet<Vertex>()); 189 } 190 getClusters()191 public SortedSet<Cluster> getClusters() { 192 193 SortedSet<Cluster> clusters = new TreeSet<Cluster>(); 194 for (Vertex v : getVertices()) { 195 if (v.getCluster() != null) { 196 clusters.add(v.getCluster()); 197 } 198 } 199 200 return clusters; 201 } 202 } 203