jdk/make/src/classes/build/tools/jigsaw/GenGraphs.java
author alanb
Thu, 01 Dec 2016 08:57:53 +0000
changeset 42338 a60f280f803c
parent 38457 3d019217e322
child 42693 6645de32a866
permissions -rw-r--r--
8169069: Module system implementation refresh (11/2016) Reviewed-by: plevart, chegar, psandoz, mchung, alanb, dfuchs, naoto, coffeys, weijun Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, mark.reinhold@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, 2016, 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.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.PrintStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.lang.module.Configuration;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.lang.module.ModuleDescriptor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.lang.module.ModuleFinder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.lang.module.ModuleReference;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.lang.module.ResolvedModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.HashSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.TreeSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.function.Function;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.util.stream.Collectors;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
    46
import static java.lang.module.ModuleDescriptor.Requires.Modifier.TRANSITIVE;
36511
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
 * Generate the DOT file for a module graph for each module in the JDK
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
 * after transitive reduction.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
public class GenGraphs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
    public static void main(String[] args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
        if (args.length != 1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
            System.err.println("ERROR: specify the output directory");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
            System.exit(1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
        Path dir = Paths.get(args[0]);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
        Files.createDirectories(dir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
        ModuleFinder finder = ModuleFinder.ofSystem();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
        Set<ModuleDescriptor> javaSEModules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
            = new TreeSet<>(finder.findAll().stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
                                  .map(ModuleReference::descriptor)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
                                  .filter(m -> (m.name().startsWith("java.") &&
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
                                               !m.name().equals("java.smartcardio")))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
                                  .collect(Collectors.toSet()));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        Set<ModuleDescriptor> jdkModules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
            = new TreeSet<>(finder.findAll().stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
                                  .map(ModuleReference::descriptor)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
                                  .filter(m -> !javaSEModules.contains(m))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
                                  .collect(Collectors.toSet()));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
        GenGraphs genGraphs = new GenGraphs(javaSEModules, jdkModules);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        Set<String> mods = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        for (ModuleReference mref: finder.findAll()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
            ModuleDescriptor descriptor = mref.descriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
            String name = descriptor.name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
            mods.add(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
            Configuration cf = Configuration.empty()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
                    .resolveRequires(finder,
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 36511
diff changeset
    85
                                     ModuleFinder.of(),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
                                     Set.of(name));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
            genGraphs.genDotFile(dir, name, cf);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        Configuration cf = Configuration.empty()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
                .resolveRequires(finder,
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 36511
diff changeset
    92
                                 ModuleFinder.of(),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
                                 mods);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        genGraphs.genDotFile(dir, "jdk", cf);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
    private final Set<ModuleDescriptor> javaGroup;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
    private final Set<ModuleDescriptor> jdkGroup;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
    GenGraphs(Set<ModuleDescriptor> javaGroup, Set<ModuleDescriptor> jdkGroup) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
        this.javaGroup = Collections.unmodifiableSet(javaGroup);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        this.jdkGroup = Collections.unmodifiableSet(jdkGroup);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
    private static final String ORANGE = "#e76f00";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
    private static final String BLUE = "#437291";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
    private static final String GRAY = "#dddddd";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
    private static final String REEXPORTS = "";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
    private static final String REQUIRES = "style=\"dashed\"";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
    private static final String REQUIRES_BASE = "color=\"" + GRAY + "\"";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
    private static final Map<String,Integer> weights = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
    private static void weight(String s, String t, int w) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        weights.put(s + ":" + t, w);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
    private static int weightOf(String s, String t) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
        int w = weights.getOrDefault(s + ":" + t, 1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
        if (w != 1)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
            return w;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        if (s.startsWith("java.") && t.startsWith("java."))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
            return 10;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        return 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
    static {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        int h = 1000;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        weight("java.se", "java.compact3", h * 10);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        weight("jdk.compact3", "java.compact3", h * 10);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
        weight("java.compact3", "java.compact2", h * 10);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
        weight("java.compact2", "java.compact1", h * 10);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
        weight("java.compact1", "java.logging", h * 10);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        weight("java.logging", "java.base", h * 10);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
    private void genDotFile(Path dir, String name, Configuration cf) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
        try (PrintStream out
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
                 = new PrintStream(Files.newOutputStream(dir.resolve(name + ".dot")))) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
            Map<String, ModuleDescriptor> nameToModule = cf.modules().stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
                    .map(ResolvedModule::reference)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
                    .map(ModuleReference::descriptor)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
                    .collect(Collectors.toMap(ModuleDescriptor::name, Function.identity()));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
            Set<ModuleDescriptor> descriptors = new TreeSet<>(nameToModule.values());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
            out.format("digraph \"%s\" {%n", name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
            out.format("size=\"25,25\";");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
            out.format("nodesep=.5;%n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
            out.format("ranksep=1.5;%n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
            out.format("pencolor=transparent;%n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
            out.format("node [shape=plaintext, fontname=\"DejaVuSans\", fontsize=36, margin=\".2,.2\"];%n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
            out.format("edge [penwidth=4, color=\"#999999\", arrowhead=open, arrowsize=2];%n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
            out.format("subgraph %sse {%n", name.equals("jdk") ? "cluster_" : "");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
            descriptors.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
                .filter(javaGroup::contains)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
                .map(ModuleDescriptor::name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
                .forEach(mn -> out.format("  \"%s\" [fontcolor=\"%s\", group=%s];%n",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
                                          mn, ORANGE, "java"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
            out.format("}%n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
            descriptors.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
                .filter(jdkGroup::contains)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
                .map(ModuleDescriptor::name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
                .forEach(mn -> out.format("  \"%s\" [fontcolor=\"%s\", group=%s];%n",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
                                          mn, BLUE, "jdk"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
            // transitive reduction
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
            Graph<String> graph = gengraph(cf);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
            descriptors.forEach(md -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
                String mn = md.name();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   175
                Set<String> requiresTransitive = md.requires().stream()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   176
                        .filter(d -> d.modifiers().contains(TRANSITIVE))
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
                        .map(d -> d.name())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
                        .collect(Collectors.toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
                graph.adjacentNodes(mn).forEach(dn -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
                    String attr = dn.equals("java.base") ? REQUIRES_BASE
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   182
                            : (requiresTransitive.contains(dn) ? REEXPORTS : REQUIRES);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
                    int w = weightOf(mn, dn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
                    if (w > 1)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
                        attr += "weight=" + w;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
                    out.format("  \"%s\" -> \"%s\" [%s];%n", mn, dn, attr);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
                });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
            });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
            out.println("}");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
     * Returns a Graph of the given Configuration after transitive reduction.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   197
     * Transitive reduction of requires transitive edge and requires edge have
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   198
     * to be applied separately to prevent the requires transitive edges
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
     * (e.g. U -> V) from being reduced by a path (U -> X -> Y -> V)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
     * in which  V would not be re-exported from U.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
    private Graph<String> gengraph(Configuration cf) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
        Graph.Builder<String> builder = new Graph.Builder<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
        for (ResolvedModule resolvedModule : cf.modules()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
            String mn = resolvedModule.reference().descriptor().name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
            builder.addNode(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
            resolvedModule.reads().stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
                    .map(ResolvedModule::name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
                    .forEach(target -> builder.addEdge(mn, target));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
        }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   211
        Graph<String> rpg = requiresTransitiveGraph(cf);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
        return builder.build().reduce(rpg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   216
     * Returns a Graph containing only requires transitive edges
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
     * with transitive reduction.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
     */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   219
    private Graph<String> requiresTransitiveGraph(Configuration cf) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
        Graph.Builder<String> builder = new Graph.Builder<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
        for (ResolvedModule resolvedModule : cf.modules()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
            ModuleDescriptor descriptor = resolvedModule.reference().descriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
            String mn = descriptor.name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
            descriptor.requires().stream()
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38457
diff changeset
   225
                    .filter(d -> d.modifiers().contains(TRANSITIVE))
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
                    .map(d -> d.name())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
                    .forEach(d -> builder.addEdge(mn, d));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
        return builder.build().reduce();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
}