mcimadamore@1562: /*
mcimadamore@1562: * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
mcimadamore@1562: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@1562: *
mcimadamore@1562: * This code is free software; you can redistribute it and/or modify it
mcimadamore@1562: * under the terms of the GNU General Public License version 2 only, as
mcimadamore@1562: * published by the Free Software Foundation. Oracle designates this
mcimadamore@1562: * particular file as subject to the "Classpath" exception as provided
mcimadamore@1562: * by Oracle in the LICENSE file that accompanied this code.
mcimadamore@1562: *
mcimadamore@1562: * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@1562: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@1562: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@1562: * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@1562: * accompanied this code).
mcimadamore@1562: *
mcimadamore@1562: * You should have received a copy of the GNU General Public License version
mcimadamore@1562: * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@1562: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@1562: *
mcimadamore@1562: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
mcimadamore@1562: * or visit www.oracle.com if you need additional information or have any
mcimadamore@1562: * questions.
mcimadamore@1562: */
mcimadamore@1562:
mcimadamore@1562: package com.sun.tools.javac.util;
mcimadamore@1562:
mcimadamore@1562: /**
This is NOT part of any supported API.
mcimadamore@1562: * If you write code that depends on this, you do so at your own risk.
mcimadamore@1562: * This code and its internal interfaces are subject to change or
mcimadamore@1562: * deletion without notice.
mcimadamore@1562: */
mcimadamore@1562: public class GraphUtils {
mcimadamore@1562:
mcimadamore@1562: /**
vromero@2000: * Basic interface for defining various dependency kinds. All dependency kinds
vromero@2000: * must at least support basic capabilities to tell the DOT engine how to render them.
vromero@2000: */
vromero@2000: public interface DependencyKind {
vromero@2000: /**
vromero@2000: * Returns the DOT representation (to be used in a {@code style} attribute
vromero@2000: * that's most suited for this dependency kind.
vromero@2000: */
vromero@2000: String getDotStyle();
vromero@2000: }
vromero@2000:
vromero@2000: /**
mcimadamore@1562: * This class is a basic abstract class for representing a node.
mcimadamore@1562: * A node is associated with a given data.
mcimadamore@1562: */
mcimadamore@1562: public static abstract class Node {
mcimadamore@1562: public final D data;
mcimadamore@1562:
mcimadamore@1562: public Node(D data) {
mcimadamore@1562: this.data = data;
mcimadamore@1562: }
mcimadamore@1562:
vromero@2000: /**
vromero@2000: * Get an array of the dependency kinds supported by this node.
vromero@2000: */
vromero@2000: public abstract DependencyKind[] getSupportedDependencyKinds();
mcimadamore@1562:
vromero@2000: /**
vromero@2000: * Get all dependencies, regardless of their kind.
vromero@2000: */
vromero@2000: public abstract Iterable extends Node> getAllDependencies();
vromero@2000:
vromero@2000: /**
vromero@2000: * Get a name for the dependency (of given kind) linking this node to a given node
vromero@2000: */
vromero@2000: public abstract String getDependencyName(Node to, DependencyKind dk);
mcimadamore@1562:
mcimadamore@1562: @Override
mcimadamore@1562: public String toString() {
mcimadamore@1562: return data.toString();
mcimadamore@1562: }
mcimadamore@1562: }
mcimadamore@1562:
mcimadamore@1562: /**
mcimadamore@1562: * This class specialized Node, by adding elements that are required in order
mcimadamore@1562: * to perform Tarjan computation of strongly connected components.
mcimadamore@1562: */
mcimadamore@1562: public static abstract class TarjanNode extends Node implements Comparable> {
mcimadamore@1562: int index = -1;
mcimadamore@1562: int lowlink;
mcimadamore@1562: boolean active;
mcimadamore@1562:
mcimadamore@1562: public TarjanNode(D data) {
mcimadamore@1562: super(data);
mcimadamore@1562: }
mcimadamore@1562:
vromero@2000: public abstract Iterable extends TarjanNode> getAllDependencies();
vromero@2000:
vromero@2000: public abstract Iterable extends TarjanNode> getDependenciesByKind(DependencyKind dk);
mcimadamore@1562:
mcimadamore@1562: public int compareTo(TarjanNode o) {
mcimadamore@1562: return (index < o.index) ? -1 : (index == o.index) ? 0 : 1;
mcimadamore@1562: }
mcimadamore@1562: }
mcimadamore@1562:
mcimadamore@1562: /**
mcimadamore@1562: * Tarjan's algorithm to determine strongly connected components of a
mcimadamore@1562: * directed graph in linear time. Works on TarjanNode.
mcimadamore@1562: */
mcimadamore@1562: public static > List extends List extends N>> tarjan(Iterable extends N> nodes) {
mcimadamore@3080: Tarjan tarjan = new Tarjan<>();
mcimadamore@3080: return tarjan.findSCC(nodes);
mcimadamore@3080: }
mcimadamore@3080:
mcimadamore@3080: //where
mcimadamore@3080: private static class Tarjan> {
mcimadamore@3080:
mcimadamore@3080: /** Unique node identifier. */
mcimadamore@3080: int index = 0;
mcimadamore@3080:
mcimadamore@3080: /** List of SCCs found so far. */
mcimadamore@3080: ListBuffer> sccs = new ListBuffer<>();
mcimadamore@3080:
mcimadamore@3080: /** Stack of all reacheable nodes from given root. */
alundblad@2047: ListBuffer stack = new ListBuffer<>();
mcimadamore@3080:
mcimadamore@3080: private List extends List extends N>> findSCC(Iterable extends N> nodes) {
mcimadamore@3080: for (N node : nodes) {
mcimadamore@3080: if (node.index == -1) {
mcimadamore@3080: findSCC(node);
mcimadamore@3080: }
mcimadamore@3080: }
mcimadamore@3080: return sccs.toList();
mcimadamore@3080: }
mcimadamore@3080:
mcimadamore@3080: private void findSCC(N v) {
mcimadamore@3080: visitNode(v);
mcimadamore@3080: for (TarjanNode tn : v.getAllDependencies()) {
mcimadamore@3080: @SuppressWarnings("unchecked")
mcimadamore@3080: N n = (N)tn;
mcimadamore@3080: if (n.index == -1) {
mcimadamore@3080: //it's the first time we see this node
mcimadamore@3080: findSCC(n);
mcimadamore@3080: v.lowlink = Math.min(v.lowlink, n.lowlink);
mcimadamore@3080: } else if (stack.contains(n)) {
mcimadamore@3080: //this node is already reachable from current root
mcimadamore@3080: v.lowlink = Math.min(v.lowlink, n.index);
mcimadamore@3080: }
mcimadamore@3080: }
mcimadamore@3080: if (v.lowlink == v.index) {
mcimadamore@3080: //v is the root of a SCC
mcimadamore@3080: addSCC(v);
mcimadamore@1562: }
mcimadamore@1562: }
mcimadamore@1562:
mcimadamore@3080: private void visitNode(N n) {
mcimadamore@3080: n.index = index;
mcimadamore@3080: n.lowlink = index;
mcimadamore@3080: index++;
mcimadamore@3080: stack.prepend(n);
mcimadamore@3080: n.active = true;
mcimadamore@1562: }
mcimadamore@3080:
mcimadamore@3080: private void addSCC(N v) {
mcimadamore@1562: N n;
alundblad@2047: ListBuffer cycle = new ListBuffer<>();
mcimadamore@1562: do {
mcimadamore@1562: n = stack.remove();
mcimadamore@1562: n.active = false;
mcimadamore@1562: cycle.add(n);
mcimadamore@1562: } while (n != v);
mcimadamore@3080: sccs.add(cycle.toList());
mcimadamore@1562: }
mcimadamore@1562: }
mcimadamore@1562:
mcimadamore@1562: /**
mcimadamore@1562: * Debugging: dot representation of a set of connected nodes. The resulting
mcimadamore@1562: * dot representation will use {@code Node.toString} to display node labels
mcimadamore@1562: * and {@code Node.printDependency} to display edge labels. The resulting
mcimadamore@1562: * representation is also customizable with a graph name and a header.
mcimadamore@1562: */
mcimadamore@1562: public static String toDot(Iterable extends TarjanNode> nodes, String name, String header) {
mcimadamore@1562: StringBuilder buf = new StringBuilder();
mcimadamore@1562: buf.append(String.format("digraph %s {\n", name));
mcimadamore@1562: buf.append(String.format("label = \"%s\";\n", header));
mcimadamore@1562: //dump nodes
mcimadamore@1562: for (TarjanNode n : nodes) {
mcimadamore@1562: buf.append(String.format("%s [label = \"%s\"];\n", n.hashCode(), n.toString()));
mcimadamore@1562: }
mcimadamore@1562: //dump arcs
mcimadamore@1562: for (TarjanNode from : nodes) {
vromero@2000: for (DependencyKind dk : from.getSupportedDependencyKinds()) {
vromero@2000: for (TarjanNode to : from.getDependenciesByKind(dk)) {
vromero@2000: buf.append(String.format("%s -> %s [label = \" %s \" style = %s ];\n",
vromero@2000: from.hashCode(), to.hashCode(), from.getDependencyName(to, dk), dk.getDotStyle()));
vromero@2000: }
mcimadamore@1562: }
mcimadamore@1562: }
mcimadamore@1562: buf.append("}\n");
mcimadamore@1562: return buf.toString();
mcimadamore@1562: }
mcimadamore@1562: }