langtools/src/jdk.jdeps/share/classes/com/sun/tools/jdeps/Graph.java
author mchung
Thu, 19 May 2016 10:55:33 -0700
changeset 38524 badd925c1d2f
child 42407 f3702cff2933
permissions -rw-r--r--
8156680: jdeps implementation refresh Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
     1
/*
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
     4
 *
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    10
 *
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    15
 * accompanied this code).
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    16
 *
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    20
 *
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    23
 * questions.
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    24
 */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    25
package com.sun.tools.jdeps;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    26
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    27
import java.io.PrintWriter;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    28
import java.lang.module.ModuleDescriptor;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    29
import java.lang.module.ModuleFinder;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    30
import java.lang.module.ModuleReference;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    31
import java.util.Collections;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    32
import java.util.Deque;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    33
import java.util.HashMap;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    34
import java.util.HashSet;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    35
import java.util.LinkedList;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    36
import java.util.Map;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    37
import java.util.Set;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    38
import java.util.function.Consumer;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    39
import java.util.function.Predicate;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    40
import java.util.stream.Collectors;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    41
import java.util.stream.Stream;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    42
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    43
public final class Graph<T> {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    44
    private final Set<T> nodes;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    45
    private final Map<T, Set<T>> edges;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    46
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    47
    public Graph(Set<T> nodes, Map<T, Set<T>> edges) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    48
        this.nodes = Collections.unmodifiableSet(nodes);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    49
        this.edges = Collections.unmodifiableMap(edges);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    50
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    51
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    52
    public Set<T> nodes() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    53
        return nodes;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    54
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    55
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    56
    public Map<T, Set<T>> edges() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    57
        return edges;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    58
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    59
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    60
    public Set<T> adjacentNodes(T u) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    61
        return edges.get(u);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    62
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    63
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    64
    public boolean contains(T u) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    65
        return nodes.contains(u);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    66
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    67
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    68
    public Set<Edge<T>> edgesFrom(T u) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    69
        return edges.get(u).stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    70
                    .map(v -> new Edge<T>(u, v))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    71
                    .collect(Collectors.toSet());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    72
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    73
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    74
    /**
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    75
     * Returns a new Graph after transitive reduction
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    76
     */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    77
    public Graph<T> reduce() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    78
        Builder<T> builder = new Builder<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    79
        nodes.stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    80
                .forEach(u -> {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    81
                    builder.addNode(u);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    82
                    edges.get(u).stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    83
                         .filter(v -> !pathExists(u, v, false))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    84
                         .forEach(v -> builder.addEdge(u, v));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    85
                });
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    86
        return builder.build();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    87
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    88
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    89
    /**
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    90
     * Returns a new Graph after transitive reduction.  All edges in
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    91
     * the given g takes precedence over this graph.
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    92
     *
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    93
     * @throw IllegalArgumentException g must be a subgraph this graph
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    94
     */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    95
    public Graph<T> reduce(Graph<T> g) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    96
        boolean subgraph = nodes.containsAll(g.nodes) &&
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    97
                g.edges.keySet().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    98
                       .allMatch(u -> adjacentNodes(u).containsAll(g.adjacentNodes(u)));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
    99
        if (!subgraph) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   100
            throw new IllegalArgumentException(g + " is not a subgraph of " + this);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   101
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   102
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   103
        Builder<T> builder = new Builder<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   104
        nodes.stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   105
                .forEach(u -> {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   106
                    builder.addNode(u);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   107
                    // filter the edge if there exists a path from u to v in the given g
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   108
                    // or there exists another path from u to v in this graph
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   109
                    edges.get(u).stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   110
                         .filter(v -> !g.pathExists(u, v) && !pathExists(u, v, false))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   111
                         .forEach(v -> builder.addEdge(u, v));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   112
                });
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   113
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   114
        // add the overlapped edges from this graph and the given g
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   115
        g.edges().keySet().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   116
                .forEach(u -> g.adjacentNodes(u).stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   117
                                .filter(v -> isAdjacent(u, v))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   118
                                .forEach(v -> builder.addEdge(u, v)));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   119
        return builder.build();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   120
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   121
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   122
    /**
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   123
     * Returns nodes sorted in topological order.
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   124
     */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   125
    public Stream<T> orderedNodes() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   126
        TopoSorter<T> sorter = new TopoSorter<>(this);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   127
        return sorter.result.stream();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   128
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   129
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   130
    /**
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   131
     * Traverse this graph and performs the given action in topological order
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   132
     */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   133
    public void ordered(Consumer<T> action) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   134
        TopoSorter<T> sorter = new TopoSorter<>(this);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   135
        sorter.ordered(action);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   136
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   137
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   138
    /**
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   139
     * Traverses this graph and performs the given action in reverse topological order
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   140
     */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   141
    public void reverse(Consumer<T> action) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   142
        TopoSorter<T> sorter = new TopoSorter<>(this);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   143
        sorter.reverse(action);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   144
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   145
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   146
    /**
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   147
     * Returns a transposed graph from this graph
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   148
     */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   149
    public Graph<T> transpose() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   150
        Builder<T> builder = new Builder<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   151
        builder.addNodes(nodes);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   152
        // reverse edges
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   153
        edges.keySet().forEach(u -> {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   154
            edges.get(u).stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   155
                .forEach(v -> builder.addEdge(v, u));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   156
        });
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   157
        return builder.build();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   158
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   159
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   160
    /**
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   161
     * Returns all nodes reachable from the given set of roots.
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   162
     */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   163
    public Set<T> dfs(Set<T> roots) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   164
        Deque<T> deque = new LinkedList<>(roots);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   165
        Set<T> visited = new HashSet<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   166
        while (!deque.isEmpty()) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   167
            T u = deque.pop();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   168
            if (!visited.contains(u)) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   169
                visited.add(u);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   170
                if (contains(u)) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   171
                    adjacentNodes(u).stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   172
                        .filter(v -> !visited.contains(v))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   173
                        .forEach(deque::push);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   174
                }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   175
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   176
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   177
        return visited;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   178
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   179
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   180
    private boolean isAdjacent(T u, T v) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   181
        return edges.containsKey(u) && edges.get(u).contains(v);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   182
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   183
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   184
    private boolean pathExists(T u, T v) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   185
        return pathExists(u, v, true);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   186
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   187
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   188
    /**
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   189
     * Returns true if there exists a path from u to v in this graph.
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   190
     * If includeAdjacent is false, it returns true if there exists
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   191
     * another path from u to v of distance > 1
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   192
     */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   193
    private boolean pathExists(T u, T v, boolean includeAdjacent) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   194
        if (!nodes.contains(u) || !nodes.contains(v)) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   195
            return false;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   196
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   197
        if (includeAdjacent && isAdjacent(u, v)) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   198
            return true;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   199
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   200
        Deque<T> stack = new LinkedList<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   201
        Set<T> visited = new HashSet<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   202
        stack.push(u);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   203
        while (!stack.isEmpty()) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   204
            T node = stack.pop();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   205
            if (node.equals(v)) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   206
                return true;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   207
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   208
            if (!visited.contains(node)) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   209
                visited.add(node);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   210
                edges.get(node).stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   211
                     .filter(e -> includeAdjacent || !node.equals(u) || !e.equals(v))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   212
                     .forEach(e -> stack.push(e));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   213
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   214
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   215
        assert !visited.contains(v);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   216
        return false;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   217
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   218
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   219
    public void printGraph(PrintWriter out) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   220
        out.println("graph for " + nodes);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   221
        nodes.stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   222
             .forEach(u -> adjacentNodes(u).stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   223
                               .forEach(v -> out.format("  %s -> %s%n", u, v)));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   224
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   225
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   226
    @Override
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   227
    public String toString() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   228
        return nodes.toString();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   229
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   230
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   231
    static class Edge<T> {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   232
        final T u;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   233
        final T v;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   234
        Edge(T u, T v) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   235
            this.u = u;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   236
            this.v = v;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   237
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   238
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   239
        @Override
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   240
        public String toString() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   241
            return String.format("%s -> %s", u, v);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   242
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   243
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   244
        @Override
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   245
        public boolean equals(Object o) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   246
            if (this == o) return true;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   247
            if (o == null || !(o instanceof Edge))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   248
                return false;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   249
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   250
            @SuppressWarnings("unchecked")
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   251
            Edge<T> edge = (Edge<T>) o;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   252
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   253
            return u.equals(edge.u) && v.equals(edge.v);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   254
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   255
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   256
        @Override
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   257
        public int hashCode() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   258
            int result = u.hashCode();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   259
            result = 31 * result + v.hashCode();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   260
            return result;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   261
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   262
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   263
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   264
    static class Builder<T> {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   265
        final Set<T> nodes = new HashSet<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   266
        final Map<T, Set<T>> edges = new HashMap<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   267
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   268
        public void addNode(T node) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   269
            if (nodes.contains(node)) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   270
                return;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   271
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   272
            nodes.add(node);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   273
            edges.computeIfAbsent(node, _e -> new HashSet<>());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   274
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   275
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   276
        public void addNodes(Set<T> nodes) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   277
            nodes.addAll(nodes);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   278
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   279
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   280
        public void addEdge(T u, T v) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   281
            addNode(u);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   282
            addNode(v);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   283
            edges.get(u).add(v);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   284
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   285
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   286
        public Graph<T> build() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   287
            return new Graph<T>(nodes, edges);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   288
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   289
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   290
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   291
    /**
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   292
     * Topological sort
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   293
     */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   294
    static class TopoSorter<T> {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   295
        final Deque<T> result = new LinkedList<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   296
        final Deque<T> nodes;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   297
        final Graph<T> graph;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   298
        TopoSorter(Graph<T> graph) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   299
            this.graph = graph;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   300
            this.nodes = new LinkedList<>(graph.nodes);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   301
            sort();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   302
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   303
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   304
        public void ordered(Consumer<T> action) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   305
            result.iterator().forEachRemaining(action);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   306
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   307
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   308
        public void reverse(Consumer<T> action) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   309
            result.descendingIterator().forEachRemaining(action);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   310
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   311
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   312
        private void sort() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   313
            Deque<T> visited = new LinkedList<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   314
            Deque<T> done = new LinkedList<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   315
            T node;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   316
            while ((node = nodes.poll()) != null) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   317
                if (!visited.contains(node)) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   318
                    visit(node, visited, done);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   319
                }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   320
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   321
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   322
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   323
        private void visit(T node, Deque<T> visited, Deque<T> done) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   324
            if (visited.contains(node)) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   325
                if (!done.contains(node)) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   326
                    throw new IllegalArgumentException("Cyclic detected: " +
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   327
                        node + " " + graph.edges().get(node));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   328
                }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   329
                return;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   330
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   331
            visited.add(node);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   332
            graph.edges().get(node).stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   333
                .forEach(x -> visit(x, visited, done));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   334
            done.add(node);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   335
            result.addLast(node);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   336
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   337
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   338
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   339
    public static class DotGraph {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   340
        static final String ORANGE = "#e76f00";
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   341
        static final String BLUE = "#437291";
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   342
        static final String GRAY = "#dddddd";
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   343
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   344
        static final String REEXPORTS = "";
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   345
        static final String REQUIRES = "style=\"dashed\"";
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   346
        static final String REQUIRES_BASE = "color=\"" + GRAY + "\"";
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   347
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   348
        static final Set<String> javaModules = modules(name ->
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   349
            (name.startsWith("java.") && !name.equals("java.smartcardio")));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   350
        static final Set<String> jdkModules = modules(name ->
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   351
            (name.startsWith("java.") ||
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   352
                name.startsWith("jdk.") ||
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   353
                name.startsWith("javafx.")) && !javaModules.contains(name));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   354
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   355
        private static Set<String> modules(Predicate<String> predicate) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   356
            return ModuleFinder.ofSystem().findAll()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   357
                               .stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   358
                               .map(ModuleReference::descriptor)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   359
                               .map(ModuleDescriptor::name)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   360
                               .filter(predicate)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   361
                               .collect(Collectors.toSet());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   362
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   363
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   364
        static void printAttributes(PrintWriter out) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   365
            out.format("  size=\"25,25\";%n");
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   366
            out.format("  nodesep=.5;%n");
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   367
            out.format("  ranksep=1.5;%n");
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   368
            out.format("  pencolor=transparent;%n");
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   369
            out.format("  node [shape=plaintext, fontname=\"DejaVuSans\", fontsize=36, margin=\".2,.2\"];%n");
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   370
            out.format("  edge [penwidth=4, color=\"#999999\", arrowhead=open, arrowsize=2];%n");
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   371
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   372
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   373
        static void printNodes(PrintWriter out, Graph<String> graph) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   374
            out.format("  subgraph se {%n");
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   375
            graph.nodes().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   376
                 .filter(javaModules::contains)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   377
                 .forEach(mn -> out.format("  \"%s\" [fontcolor=\"%s\", group=%s];%n",
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   378
                                           mn, ORANGE, "java"));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   379
            out.format("  }%n");
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   380
            graph.nodes().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   381
                 .filter(jdkModules::contains)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   382
                 .forEach(mn -> out.format("    \"%s\" [fontcolor=\"%s\", group=%s];%n",
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   383
                                           mn, BLUE, "jdk"));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   384
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   385
            graph.nodes().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   386
                 .filter(mn -> !javaModules.contains(mn) && !jdkModules.contains(mn))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   387
                 .forEach(mn -> out.format("  \"%s\";%n", mn));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   388
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   389
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   390
        static void printEdges(PrintWriter out, Graph<String> graph,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   391
                               String node, Set<String> requiresPublic) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   392
            graph.adjacentNodes(node).forEach(dn -> {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   393
                String attr = dn.equals("java.base") ? REQUIRES_BASE
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   394
                        : (requiresPublic.contains(dn) ? REEXPORTS : REQUIRES);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   395
                out.format("  \"%s\" -> \"%s\" [%s];%n", node, dn, attr);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   396
            });
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   397
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   398
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   399
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   400
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents:
diff changeset
   401
}