lib/standard/stream: Renamed streams for more explicit denomination
[nit.git] / src / metrics / generate_hierarchies.nit
index 9765a2e..6193b9c 100644 (file)
@@ -18,9 +18,8 @@
 # See graphviz http://www.graphviz.org/
 module generate_hierarchies
 
-import model
-private import metrics_base
-import frontend
+import metrics_base
+import model::model_viz
 
 redef class ToolContext
        var generate_hierarchies_phase: Phase = new GenerateHierarchyPhase(self, null)
@@ -29,11 +28,11 @@ end
 private class GenerateHierarchyPhase
        super Phase
 
-       redef fun process_mainmodule(mainmodule)
+       redef fun process_mainmodule(mainmodule, given_mmodules)
        do
                if not toolcontext.opt_generate_hyperdoc.value and not toolcontext.opt_all.value then return
                var model = toolcontext.modelbuilder.model
-               generate_module_hierarchy(toolcontext, model)
+               generate_module_hierarchy(toolcontext, given_mmodules)
                generate_classdef_hierarchy(toolcontext, model)
                generate_class_hierarchy(toolcontext, mainmodule)
        end
@@ -42,48 +41,31 @@ end
 # Create a dot file representing the module hierarchy of a model.
 # Importation relation is represented with arrow
 # Nesting relation is represented with nested boxes
-fun generate_module_hierarchy(toolcontext: ToolContext, model: Model)
+fun generate_module_hierarchy(toolcontext: ToolContext, given_mmodules: Collection[MModule])
 do
-       var buf = new Buffer
-       buf.append("digraph \{\n")
-       buf.append("node [shape=box];\n")
-       buf.append("rankdir=BT;\n")
-       for mmodule in model.mmodules do
-               if mmodule.direct_owner == null then
-                       generate_module_hierarchy_for(mmodule, buf)
-               end
-       end
-       for mmodule in model.mmodules do
-               for s in mmodule.in_importation.direct_greaters do
-                       buf.append("\"{mmodule}\" -> \"{s}\";\n")
-               end
-       end
-       buf.append("\}\n")
-       var f = new OFStream.open(toolcontext.output_dir.join_path("module_hierarchy.dot"))
-       f.write(buf.to_s)
-       f.close
-end
+       var model = given_mmodules.first.model
+       var dot = new MProjectDot(model)
 
-# Helper function for `generate_module_hierarchy`.
-# Create graphviz nodes for the module and recusrively for its nested modules
-private fun generate_module_hierarchy_for(mmodule: MModule, buf: Buffer)
-do
-       if mmodule.in_nesting.direct_greaters.is_empty then
-               buf.append("\"{mmodule.name}\";\n")
-       else
-               buf.append("subgraph \"cluster_{mmodule.name}\" \{label=\"\"\n")
-               buf.append("\"{mmodule.name}\";\n")
-               for s in mmodule.in_nesting.direct_greaters do
-                       generate_module_hierarchy_for(s, buf)
-               end
-               buf.append("\}\n")
+       # Collect requested projects
+       for m in given_mmodules do
+               var g = m.mgroup
+               if g == null then continue
+               dot.mprojects.add(g.mproject)
        end
+       var projectpath = toolcontext.output_dir.join_path("project_hierarchy.dot")
+       print "generating project_hierarchy.dot"
+       dot.write_to_file(projectpath)
+
+       var modulepath = toolcontext.output_dir.join_path("module_hierarchy.dot")
+       dot.mprojects.add_all(model.mprojects)
+       print "generating module_hierarchy.dot"
+       dot.write_to_file(modulepath)
 end
 
 # Create a dot file representing the class hierarchy of a model.
 fun generate_class_hierarchy(toolcontext: ToolContext, mmodule: MModule)
 do
-       var buf = new Buffer
+       var buf = new FlatBuffer
        buf.append("digraph \{\n")
        buf.append("node [shape=box];\n")
        buf.append("rankdir=BT;\n")
@@ -95,7 +77,7 @@ do
                end
        end
        buf.append("\}\n")
-       var f = new OFStream.open(toolcontext.output_dir.join_path("class_hierarchy.dot"))
+       var f = new FileWriter.open(toolcontext.output_dir.join_path("class_hierarchy.dot"))
        f.write(buf.to_s)
        f.close
 end
@@ -104,7 +86,7 @@ end
 # For a simple user of the model, the classdef hierarchy is not really usefull, it is more an internal thing.
 fun generate_classdef_hierarchy(toolcontext: ToolContext, model: Model)
 do
-       var buf = new Buffer
+       var buf = new FlatBuffer
        buf.append("digraph \{\n")
        buf.append("node [shape=box];\n")
        buf.append("rankdir=BT;\n")
@@ -117,7 +99,7 @@ do
                end
        end
        buf.append("\}\n")
-       var f = new OFStream.open(toolcontext.output_dir.join_path("classdef_hierarchy.dot"))
+       var f = new FileWriter.open(toolcontext.output_dir.join_path("classdef_hierarchy.dot"))
        f.write(buf.to_s)
        f.close
 end