langtools/src/jdk.jdeps/share/classes/com/sun/tools/jdeps/ModuleDotGraph.java
author mchung
Wed, 29 Mar 2017 09:41:36 -0700
changeset 44452 93f6470b1045
parent 43873 705d732d3715
permissions -rw-r--r--
8173303: Add module-subgraph images to main platform documentation Reviewed-by: alanb, chegar, erikj, ihse, lancea Contributed-by: jonathan.gibbons@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
     1
/*
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
     4
 *
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    10
 *
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    15
 * accompanied this code).
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    16
 *
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    20
 *
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    23
 * questions.
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    24
 */
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    25
package com.sun.tools.jdeps;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    26
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    27
import static java.lang.module.ModuleDescriptor.Requires.Modifier.*;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    28
import static java.util.stream.Collectors.*;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    29
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    30
import java.io.BufferedWriter;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    31
import java.io.IOException;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    32
import java.io.PrintWriter;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    33
import java.lang.module.Configuration;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    34
import java.lang.module.ModuleDescriptor;
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
    35
import java.lang.module.ModuleDescriptor.*;
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    36
import java.lang.module.ModuleFinder;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    37
import java.lang.module.ModuleReference;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    38
import java.lang.module.ResolvedModule;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    39
import java.nio.file.Files;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    40
import java.nio.file.Path;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    41
import java.util.ArrayDeque;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    42
import java.util.ArrayList;
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
    43
import java.util.Collections;
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    44
import java.util.Deque;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    45
import java.util.HashSet;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    46
import java.util.List;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    47
import java.util.Map;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    48
import java.util.Objects;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    49
import java.util.Set;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    50
import java.util.TreeSet;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    51
import java.util.function.Function;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    52
import java.util.stream.Collectors;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    53
import java.util.stream.Stream;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    54
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    55
/**
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    56
 * Generate dot graph for modules
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    57
 */
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    58
public class ModuleDotGraph {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    59
    private final Map<String, Configuration> configurations;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    60
    private final boolean apiOnly;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    61
    public ModuleDotGraph(JdepsConfiguration config, boolean apiOnly) {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    62
        this(config.rootModules().stream()
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    63
                   .map(Module::name)
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    64
                   .sorted()
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    65
                   .collect(toMap(Function.identity(), mn -> config.resolve(Set.of(mn)))),
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    66
             apiOnly);
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    67
    }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    68
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    69
    public ModuleDotGraph(Map<String, Configuration> configurations, boolean apiOnly) {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    70
        this.configurations = configurations;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    71
        this.apiOnly = apiOnly;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    72
    }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    73
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    74
    /**
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    75
     * Generate dotfile for all modules
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    76
     *
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    77
     * @param dir output directory
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    78
     */
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    79
    public boolean genDotFiles(Path dir) throws IOException {
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
    80
        return genDotFiles(dir, DotGraphAttributes.DEFAULT);
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
    81
    }
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
    82
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
    83
    public boolean genDotFiles(Path dir, Attributes attributes)
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
    84
        throws IOException
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
    85
    {
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    86
        Files.createDirectories(dir);
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    87
        for (String mn : configurations.keySet()) {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    88
            Path path = dir.resolve(mn + ".dot");
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
    89
            genDotFile(path, mn, configurations.get(mn), attributes);
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    90
        }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    91
        return true;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    92
    }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    93
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    94
    /**
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    95
     * Generate dotfile of the given path
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
    96
     */
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
    97
    public void genDotFile(Path path, String name,
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
    98
                           Configuration configuration,
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
    99
                           Attributes attributes)
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   100
        throws IOException
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   101
    {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   102
        // transitive reduction
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   103
        Graph<String> graph = apiOnly
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   104
                ? requiresTransitiveGraph(configuration, Set.of(name))
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   105
                : gengraph(configuration);
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   106
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   107
        DotGraphBuilder builder = new DotGraphBuilder(name, graph, attributes);
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   108
        builder.subgraph("se", "java", attributes.javaSubgraphColor(),
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   109
                         DotGraphBuilder.JAVA_SE_SUBGRAPH)
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   110
               .subgraph("jdk", "jdk", attributes.jdkSubgraphColor(),
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   111
                         DotGraphBuilder.JDK_SUBGRAPH)
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   112
               .modules(graph.nodes().stream()
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   113
                                 .map(mn -> configuration.findModule(mn).get()
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   114
                                                .reference().descriptor()));
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   115
        // build dot file
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   116
        builder.build(path);
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   117
    }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   118
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   119
    /**
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   120
     * Returns a Graph of the given Configuration after transitive reduction.
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   121
     *
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   122
     * Transitive reduction of requires transitive edge and requires edge have
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   123
     * to be applied separately to prevent the requires transitive edges
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   124
     * (e.g. U -> V) from being reduced by a path (U -> X -> Y -> V)
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   125
     * in which  V would not be re-exported from U.
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   126
     */
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   127
    private Graph<String> gengraph(Configuration cf) {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   128
        Graph.Builder<String> builder = new Graph.Builder<>();
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   129
        cf.modules().stream()
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   130
            .forEach(rm -> {
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   131
                String mn = rm.name();
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   132
                builder.addNode(mn);
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   133
                rm.reads().stream()
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   134
                  .map(ResolvedModule::name)
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   135
                  .forEach(target -> builder.addEdge(mn, target));
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   136
            });
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   137
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   138
        Graph<String> rpg = requiresTransitiveGraph(cf, builder.nodes);
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   139
        return builder.build().reduce(rpg);
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   140
    }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   141
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   142
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   143
    /**
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   144
     * Returns a Graph containing only requires transitive edges
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   145
     * with transitive reduction.
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   146
     */
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   147
    public Graph<String> requiresTransitiveGraph(Configuration cf,
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   148
                                                 Set<String> roots)
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   149
    {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   150
        Deque<String> deque = new ArrayDeque<>(roots);
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   151
        Set<String> visited = new HashSet<>();
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   152
        Graph.Builder<String> builder = new Graph.Builder<>();
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   153
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   154
        while (deque.peek() != null) {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   155
            String mn = deque.pop();
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   156
            if (visited.contains(mn))
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   157
                continue;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   158
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   159
            visited.add(mn);
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   160
            builder.addNode(mn);
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   161
            cf.findModule(mn).get()
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   162
              .reference().descriptor().requires().stream()
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   163
              .filter(d -> d.modifiers().contains(TRANSITIVE)
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   164
                                || d.name().equals("java.base"))
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   165
              .map(Requires::name)
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   166
              .forEach(d -> {
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   167
                  deque.add(d);
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   168
                  builder.addEdge(mn, d);
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   169
              });
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   170
        }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   171
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   172
        return builder.build().reduce();
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   173
    }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   174
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   175
    public interface Attributes {
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   176
        static final String ORANGE = "#e76f00";
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   177
        static final String BLUE = "#437291";
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   178
        static final String BLACK = "#000000";
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   179
        static final String DARK_GRAY = "#999999";
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   180
        static final String LIGHT_GRAY = "#dddddd";
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   181
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   182
        int fontSize();
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   183
        String fontName();
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   184
        String fontColor();
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   185
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   186
        int arrowSize();
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   187
        int arrowWidth();
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   188
        String arrowColor();
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   189
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   190
        default double rankSep() {
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   191
            return 1;
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   192
        }
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   193
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   194
        default List<Set<String>> ranks() {
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   195
            return Collections.emptyList();
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   196
        }
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   197
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   198
        default int weightOf(String s, String t) {
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   199
            return 1;
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   200
        }
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   201
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   202
        default String requiresMandatedColor() {
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   203
            return LIGHT_GRAY;
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   204
        }
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   205
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   206
        default String javaSubgraphColor() {
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   207
            return ORANGE;
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   208
        }
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   209
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   210
        default String jdkSubgraphColor() {
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   211
            return BLUE;
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   212
        }
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   213
    }
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   214
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   215
    static class DotGraphAttributes implements Attributes {
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   216
        static final DotGraphAttributes DEFAULT = new DotGraphAttributes();
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   217
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   218
        static final String FONT_NAME = "DejaVuSans";
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   219
        static final int FONT_SIZE = 12;
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   220
        static final int ARROW_SIZE = 1;
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   221
        static final int ARROW_WIDTH = 2;
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   222
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   223
        @Override
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   224
        public int fontSize() {
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   225
            return FONT_SIZE;
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   226
        }
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   227
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   228
        @Override
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   229
        public String fontName() {
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   230
            return FONT_NAME;
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   231
        }
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   232
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   233
        @Override
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   234
        public String fontColor() {
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   235
            return BLACK;
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   236
        }
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   237
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   238
        @Override
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   239
        public int arrowSize() {
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   240
            return ARROW_SIZE;
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   241
        }
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   242
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   243
        @Override
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   244
        public int arrowWidth() {
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   245
            return ARROW_WIDTH;
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   246
        }
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   247
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   248
        @Override
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   249
        public String arrowColor() {
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   250
            return DARK_GRAY;
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   251
        }
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   252
    }
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   253
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   254
    private static class DotGraphBuilder {
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   255
        static final String REEXPORTS = "";
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   256
        static final String REQUIRES = "style=\"dashed\"";
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   257
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   258
        static final Set<String> JAVA_SE_SUBGRAPH = javaSE();
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   259
        static final Set<String> JDK_SUBGRAPH = jdk();
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   260
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   261
        private static Set<String> javaSE() {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   262
            String root = "java.se.ee";
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   263
            ModuleFinder system = ModuleFinder.ofSystem();
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   264
            if (system.find(root).isPresent()) {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   265
                return Stream.concat(Stream.of(root),
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   266
                                     Configuration.empty().resolve(system,
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   267
                                                                   ModuleFinder.of(),
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   268
                                                                   Set.of(root))
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   269
                                                  .findModule(root).get()
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   270
                                                  .reads().stream()
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   271
                                                  .map(ResolvedModule::name))
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   272
                             .collect(toSet());
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   273
            } else {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   274
                // approximation
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   275
                return system.findAll().stream()
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   276
                    .map(ModuleReference::descriptor)
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   277
                    .map(ModuleDescriptor::name)
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   278
                    .filter(name -> name.startsWith("java.") &&
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   279
                                        !name.equals("java.smartcardio"))
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   280
                    .collect(Collectors.toSet());
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   281
            }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   282
        }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   283
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   284
        private static Set<String> jdk() {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   285
            return ModuleFinder.ofSystem().findAll().stream()
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   286
                    .map(ModuleReference::descriptor)
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   287
                    .map(ModuleDescriptor::name)
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   288
                    .filter(name -> !JAVA_SE_SUBGRAPH.contains(name) &&
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   289
                                        (name.startsWith("java.") ||
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   290
                                            name.startsWith("jdk.") ||
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   291
                                            name.startsWith("javafx.")))
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   292
                    .collect(Collectors.toSet());
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   293
        }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   294
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   295
        static class SubGraph {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   296
            final String name;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   297
            final String group;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   298
            final String color;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   299
            final Set<String> nodes;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   300
            SubGraph(String name, String group, String color, Set<String> nodes) {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   301
                this.name = Objects.requireNonNull(name);
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   302
                this.group = Objects.requireNonNull(group);
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   303
                this.color = Objects.requireNonNull(color);
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   304
                this.nodes = Objects.requireNonNull(nodes);
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   305
            }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   306
        }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   307
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   308
        private final String name;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   309
        private final Graph<String> graph;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   310
        private final Set<ModuleDescriptor> descriptors = new TreeSet<>();
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   311
        private final List<SubGraph> subgraphs = new ArrayList<>();
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   312
        private final Attributes attributes;
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   313
        public DotGraphBuilder(String name,
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   314
                               Graph<String> graph,
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   315
                               Attributes attributes) {
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   316
            this.name = name;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   317
            this.graph = graph;
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   318
            this.attributes = attributes;
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   319
        }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   320
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   321
        public DotGraphBuilder modules(Stream<ModuleDescriptor> descriptors) {
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   322
            descriptors.forEach(this.descriptors::add);
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   323
            return this;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   324
        }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   325
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   326
        public void build(Path filename) throws IOException {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   327
            try (BufferedWriter writer = Files.newBufferedWriter(filename);
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   328
                 PrintWriter out = new PrintWriter(writer)) {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   329
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   330
                out.format("digraph \"%s\" {%n", name);
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   331
                out.format("  nodesep=.5;%n");
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   332
                out.format("  ranksep=%f;%n", attributes.rankSep());
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   333
                out.format("  pencolor=transparent;%n");
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   334
                out.format("  node [shape=plaintext, fontcolor=\"%s\", fontname=\"%s\","
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   335
                                + " fontsize=%d, margin=\".2,.2\"];%n",
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   336
                           attributes.fontColor(),
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   337
                           attributes.fontName(),
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   338
                           attributes.fontSize());
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   339
                out.format("  edge [penwidth=%d, color=\"%s\", arrowhead=open, arrowsize=%d];%n",
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   340
                           attributes.arrowWidth(),
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   341
                           attributes.arrowColor(),
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   342
                           attributes.arrowSize());
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   343
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   344
                // same RANKS
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   345
                attributes.ranks().stream()
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   346
                    .map(nodes -> descriptors.stream()
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   347
                                        .map(ModuleDescriptor::name)
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   348
                                        .filter(nodes::contains)
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   349
                                        .map(mn -> "\"" + mn + "\"")
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   350
                                        .collect(joining(",")))
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   351
                    .filter(group -> group.length() > 0)
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   352
                    .forEach(group -> out.format("  {rank=same %s}%n", group));
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   353
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   354
                subgraphs.forEach(subgraph -> {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   355
                    out.format("  subgraph %s {%n", subgraph.name);
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   356
                    descriptors.stream()
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   357
                        .map(ModuleDescriptor::name)
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   358
                        .filter(subgraph.nodes::contains)
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   359
                        .forEach(mn -> printNode(out, mn, subgraph.color, subgraph.group));
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   360
                    out.format("  }%n");
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   361
                });
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   362
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   363
                descriptors.stream()
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   364
                    .filter(md -> graph.contains(md.name()) &&
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   365
                                    !graph.adjacentNodes(md.name()).isEmpty())
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   366
                    .forEach(md -> printNode(out, md, graph.adjacentNodes(md.name())));
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   367
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   368
                out.println("}");
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   369
            }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   370
        }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   371
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   372
        public DotGraphBuilder subgraph(String name, String group, String color,
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   373
                                 Set<String> nodes) {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   374
            subgraphs.add(new SubGraph(name, group, color, nodes));
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   375
            return this;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   376
        }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   377
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   378
        public void printNode(PrintWriter out, String node, String color, String group) {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   379
            out.format("  \"%s\" [fontcolor=\"%s\", group=%s];%n",
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   380
                       node, color, group);
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   381
        }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   382
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   383
        public void printNode(PrintWriter out, ModuleDescriptor md, Set<String> edges) {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   384
            Set<String> requiresTransitive = md.requires().stream()
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   385
                .filter(d -> d.modifiers().contains(TRANSITIVE))
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   386
                .map(d -> d.name())
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   387
                .collect(toSet());
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   388
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   389
            String mn = md.name();
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   390
            edges.stream().forEach(dn -> {
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   391
                String attr;
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   392
                if (dn.equals("java.base")) {
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   393
                    attr = "color=\"" + attributes.requiresMandatedColor() + "\"";
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   394
                } else {
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   395
                    attr = (requiresTransitive.contains(dn) ? REEXPORTS : REQUIRES);
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   396
                }
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   397
44452
93f6470b1045 8173303: Add module-subgraph images to main platform documentation
mchung
parents: 43873
diff changeset
   398
                int w = attributes.weightOf(mn, dn);
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   399
                if (w > 1) {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   400
                    if (!attr.isEmpty())
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   401
                        attr += ", ";
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   402
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   403
                    attr += "weight=" + w;
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   404
                }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   405
                out.format("  \"%s\" -> \"%s\" [%s];%n", mn, dn, attr);
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   406
            });
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   407
        }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   408
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   409
    }
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
diff changeset
   410
}