jdk/make/src/classes/build/tools/jigsaw/Graph.java
author alanb
Thu, 17 Mar 2016 19:04:16 +0000
changeset 36511 9d0388c6b336
permissions -rw-r--r--
8142968: Module System implementation Summary: Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, chris.hegarty@oracle.com, alexandr.scherbatiy@oracle.com, amy.lu@oracle.com, calvin.cheung@oracle.com, daniel.fuchs@oracle.com, erik.joelsson@oracle.com, harold.seigel@oracle.com, jaroslav.bachorik@oracle.com, jean-francois.denise@oracle.com, jan.lahoda@oracle.com, james.laskey@oracle.com, lois.foltan@oracle.com, miroslav.kos@oracle.com, huaming.li@oracle.com, sean.mullan@oracle.com, naoto.sato@oracle.com, masayoshi.okutsu@oracle.com, peter.levart@gmail.com, philip.race@oracle.com, claes.redestad@oracle.com, sergey.bylokhov@oracle.com, alexandre.iline@oracle.com, volker.simonis@gmail.com, staffan.larsen@oracle.com, stuart.marks@oracle.com, semyon.sadetsky@oracle.com, serguei.spitsyn@oracle.com, sundararajan.athijegannathan@oracle.com, valerie.peng@oracle.com, vincent.x.ryan@oracle.com, weijun.wang@oracle.com, yuri.nesterenko@oracle.com, yekaterina.kantserova@oracle.com, alexander.kulyakhtin@oracle.com, felix.yang@oracle.com, andrei.eremeev@oracle.com, frank.yuan@oracle.com, sergei.pikalev@oracle.com, sibabrata.sahoo@oracle.com, tiantian.du@oracle.com, sha.jiang@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
package build.tools.jigsaw;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.PrintStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.util.Deque;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.util.HashSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.LinkedList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
public class Graph<T> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
    private static boolean traceOn = Boolean.getBoolean("build.tools.module.trace");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
    private final Set<T> nodes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
    private final Map<T, Set<T>> edges;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
    private Graph(Set<T> nodes, Map<T, Set<T>> edges) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
        this.nodes = nodes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
        this.edges = edges;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
    public Set<T> nodes() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
        return nodes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
    public Map<T, Set<T>> edges() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
        return edges;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
    public Set<T> adjacentNodes(T u) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
        return edges.get(u);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
     * Returns a new Graph after transitive reduction
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
    public Graph<T> reduce() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
        Graph.Builder<T> builder = new Builder<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
        nodes.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
             .forEach(u -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
                 builder.addNode(u);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
                 edges.get(u).stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
                         .filter(v -> !pathExists(u, v, false))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
                         .forEach(v -> builder.addEdge(u, v));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
             });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        return builder.build();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
     * Returns a new Graph after transitive reduction.  All edges in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
     * the given g takes precedence over this graph.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
     * @throw IllegalArgumentException g must be a subgraph this graph
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
    public Graph<T> reduce(Graph<T> g) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        boolean subgraph = nodes.containsAll(g.nodes) && g.edges.keySet().stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
               .allMatch(u -> adjacentNodes(u).containsAll(g.adjacentNodes(u)));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        if (!subgraph) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
            throw new IllegalArgumentException("the given argument is not a subgraph of this graph");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
        Graph.Builder<T> builder = new Builder<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        nodes.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
             .forEach(u -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
                 builder.addNode(u);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
                 // filter the edge if there exists a path from u to v in the given g
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
                 // or there exists another path from u to v in this graph
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
                 edges.get(u).stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
                      .filter(v -> !g.pathExists(u, v) && !pathExists(u, v, false))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
                      .forEach(v -> builder.addEdge(u, v));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
             });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        // add the overlapped edges from this graph and the given g
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        g.edges().keySet().stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
                 .forEach(u -> g.adjacentNodes(u).stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
                         .filter(v -> isAdjacent(u, v))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
                         .forEach(v -> builder.addEdge(u, v)));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        return builder.build();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
    private boolean isAdjacent(T u, T v) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        return edges.containsKey(u) && edges.get(u).contains(v);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
    private boolean pathExists(T u, T v) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        return pathExists(u, v, true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
     * Returns true if there exists a path from u to v in this graph.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
     * If includeAdjacent is false, it returns true if there exists
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
     * another path from u to v of distance > 1
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
    private boolean pathExists(T u, T v, boolean includeAdjacent) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        if (!nodes.contains(u) || !nodes.contains(v)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
            return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
        if (includeAdjacent && isAdjacent(u, v)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
            return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        Deque<T> stack = new LinkedList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
        Set<T> visited = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        stack.push(u);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        while (!stack.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
            T node = stack.pop();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
            if (node.equals(v)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
                if (traceOn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
                    System.out.format("Edge %s -> %s removed%n", u, v);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
                return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
            if (!visited.contains(node)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
                visited.add(node);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
                edges.get(node).stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
                     .filter(e -> includeAdjacent || !node.equals(u) || !e.equals(v))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
                     .forEach(e -> stack.push(e));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
        assert !visited.contains(v);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
        return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
    void printGraph(PrintStream out) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
        nodes.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
             .forEach(u -> adjacentNodes(u).stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
                     .forEach(v -> out.format("%s -> %s%n", u, v)));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
    public static class Builder<T> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
        final Set<T> nodes = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
        final Map<T, Set<T>> edges = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        public void addNode(T node) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
            if (nodes.contains(node)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
                return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
            nodes.add(node);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
            edges.computeIfAbsent(node, _e -> new HashSet<>());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
        public void addEdge(T u, T v) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
            addNode(u);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
            addNode(v);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
            edges.get(u).add(v);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
        public Graph<T> build() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
            return new Graph<>(nodes, edges);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
}