src/share/classes/com/sun/tools/javac/util/GraphUtils.java

Thu, 24 May 2018 16:48:51 +0800

author
aoqi
date
Thu, 24 May 2018 16:48:51 +0800
changeset 3295
859dc787b52b
parent 3080
75296d8d5125
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

Merge

     1 /*
     2  * Copyright (c) 1999, 2013, 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.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.tools.javac.util;
    28 /** <p><b>This is NOT part of any supported API.
    29  *  If you write code that depends on this, you do so at your own risk.
    30  *  This code and its internal interfaces are subject to change or
    31  *  deletion without notice.</b>
    32  */
    33 public class GraphUtils {
    35     /**
    36      * Basic interface for defining various dependency kinds. All dependency kinds
    37      * must at least support basic capabilities to tell the DOT engine how to render them.
    38      */
    39     public interface DependencyKind {
    40         /**
    41          * Returns the DOT representation (to be used in a {@code style} attribute
    42          * that's most suited for this dependency kind.
    43          */
    44         String getDotStyle();
    45     }
    47     /**
    48      * This class is a basic abstract class for representing a node.
    49      * A node is associated with a given data.
    50      */
    51     public static abstract class Node<D> {
    52         public final D data;
    54         public Node(D data) {
    55             this.data = data;
    56         }
    58         /**
    59          * Get an array of the dependency kinds supported by this node.
    60          */
    61         public abstract DependencyKind[] getSupportedDependencyKinds();
    63         /**
    64          * Get all dependencies, regardless of their kind.
    65          */
    66         public abstract Iterable<? extends Node<D>> getAllDependencies();
    68         /**
    69          * Get a name for the dependency (of given kind) linking this node to a given node
    70          */
    71         public abstract String getDependencyName(Node<D> to, DependencyKind dk);
    73         @Override
    74         public String toString() {
    75             return data.toString();
    76         }
    77     }
    79     /**
    80      * This class specialized Node, by adding elements that are required in order
    81      * to perform Tarjan computation of strongly connected components.
    82      */
    83     public static abstract class TarjanNode<D> extends Node<D> implements Comparable<TarjanNode<D>> {
    84         int index = -1;
    85         int lowlink;
    86         boolean active;
    88         public TarjanNode(D data) {
    89             super(data);
    90         }
    92         public abstract Iterable<? extends TarjanNode<D>> getAllDependencies();
    94         public abstract Iterable<? extends TarjanNode<D>> getDependenciesByKind(DependencyKind dk);
    96         public int compareTo(TarjanNode<D> o) {
    97             return (index < o.index) ? -1 : (index == o.index) ? 0 : 1;
    98         }
    99     }
   101     /**
   102      * Tarjan's algorithm to determine strongly connected components of a
   103      * directed graph in linear time. Works on TarjanNode.
   104      */
   105     public static <D, N extends TarjanNode<D>> List<? extends List<? extends N>> tarjan(Iterable<? extends N> nodes) {
   106         Tarjan<D, N> tarjan = new Tarjan<>();
   107         return tarjan.findSCC(nodes);
   108     }
   110     //where
   111     private static class Tarjan<D, N extends TarjanNode<D>> {
   113         /** Unique node identifier. */
   114         int index = 0;
   116         /** List of SCCs found so far. */
   117         ListBuffer<List<N>> sccs = new ListBuffer<>();
   119         /** Stack of all reacheable nodes from given root. */
   120         ListBuffer<N> stack = new ListBuffer<>();
   122         private List<? extends List<? extends N>> findSCC(Iterable<? extends N> nodes) {
   123             for (N node : nodes) {
   124                 if (node.index == -1) {
   125                     findSCC(node);
   126                 }
   127             }
   128             return sccs.toList();
   129         }
   131         private void findSCC(N v) {
   132             visitNode(v);
   133             for (TarjanNode<D> tn : v.getAllDependencies()) {
   134                 @SuppressWarnings("unchecked")
   135                 N n = (N)tn;
   136                 if (n.index == -1) {
   137                     //it's the first time we see this node
   138                     findSCC(n);
   139                     v.lowlink = Math.min(v.lowlink, n.lowlink);
   140                 } else if (stack.contains(n)) {
   141                     //this node is already reachable from current root
   142                     v.lowlink = Math.min(v.lowlink, n.index);
   143                 }
   144             }
   145             if (v.lowlink == v.index) {
   146                 //v is the root of a SCC
   147                 addSCC(v);
   148             }
   149         }
   151         private void visitNode(N n) {
   152             n.index = index;
   153             n.lowlink = index;
   154             index++;
   155             stack.prepend(n);
   156             n.active = true;
   157         }
   159         private void addSCC(N v) {
   160             N n;
   161             ListBuffer<N> cycle = new ListBuffer<>();
   162             do {
   163                 n = stack.remove();
   164                 n.active = false;
   165                 cycle.add(n);
   166             } while (n != v);
   167             sccs.add(cycle.toList());
   168         }
   169     }
   171     /**
   172      * Debugging: dot representation of a set of connected nodes. The resulting
   173      * dot representation will use {@code Node.toString} to display node labels
   174      * and {@code Node.printDependency} to display edge labels. The resulting
   175      * representation is also customizable with a graph name and a header.
   176      */
   177     public static <D> String toDot(Iterable<? extends TarjanNode<D>> nodes, String name, String header) {
   178         StringBuilder buf = new StringBuilder();
   179         buf.append(String.format("digraph %s {\n", name));
   180         buf.append(String.format("label = \"%s\";\n", header));
   181         //dump nodes
   182         for (TarjanNode<D> n : nodes) {
   183             buf.append(String.format("%s [label = \"%s\"];\n", n.hashCode(), n.toString()));
   184         }
   185         //dump arcs
   186         for (TarjanNode<D> from : nodes) {
   187             for (DependencyKind dk : from.getSupportedDependencyKinds()) {
   188                 for (TarjanNode<D> to : from.getDependenciesByKind(dk)) {
   189                     buf.append(String.format("%s -> %s [label = \" %s \" style = %s ];\n",
   190                             from.hashCode(), to.hashCode(), from.getDependencyName(to, dk), dk.getDotStyle()));
   191                 }
   192             }
   193         }
   194         buf.append("}\n");
   195         return buf.toString();
   196     }
   197 }

mercurial