modelbuilder: add parameter `given_mmodules` to `Phase::process_mainmodule`
[nit.git] / src / metrics / generate_hierarchies.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Create dot files for various hierarchies of a model.
18 # See graphviz http://www.graphviz.org/
19 module generate_hierarchies
20
21 import model
22 private import metrics_base
23 import frontend
24 import model_viz
25
26 redef class ToolContext
27 var generate_hierarchies_phase: Phase = new GenerateHierarchyPhase(self, null)
28 end
29
30 private class GenerateHierarchyPhase
31 super Phase
32
33 redef fun process_mainmodule(mainmodule, given_mmodules)
34 do
35 if not toolcontext.opt_generate_hyperdoc.value and not toolcontext.opt_all.value then return
36 var model = toolcontext.modelbuilder.model
37 generate_module_hierarchy(toolcontext, model)
38 generate_classdef_hierarchy(toolcontext, model)
39 generate_class_hierarchy(toolcontext, mainmodule)
40 end
41 end
42
43 # Create a dot file representing the module hierarchy of a model.
44 # Importation relation is represented with arrow
45 # Nesting relation is represented with nested boxes
46 fun generate_module_hierarchy(toolcontext: ToolContext, model: Model)
47 do
48 var dot = new MProjectDot(model)
49
50 var projectpath = toolcontext.output_dir.join_path("project_hierarchy.dot")
51 dot.mprojects.add(model.mprojects.first)
52 print "generating {projectpath}"
53 dot.write_to_file(projectpath)
54
55 var modulepath = toolcontext.output_dir.join_path("module_hierarchy.dot")
56 dot.mprojects.add_all(model.mprojects)
57 print "generating {modulepath}"
58 dot.write_to_file(modulepath)
59 end
60
61 # Create a dot file representing the class hierarchy of a model.
62 fun generate_class_hierarchy(toolcontext: ToolContext, mmodule: MModule)
63 do
64 var buf = new Buffer
65 buf.append("digraph \{\n")
66 buf.append("node [shape=box];\n")
67 buf.append("rankdir=BT;\n")
68 var hierarchy = mmodule.flatten_mclass_hierarchy
69 for mclass in hierarchy do
70 buf.append("\"{mclass}\" [label=\"{mclass}\"];\n")
71 for s in hierarchy[mclass].direct_greaters do
72 buf.append("\"{mclass}\" -> \"{s}\";\n")
73 end
74 end
75 buf.append("\}\n")
76 var f = new OFStream.open(toolcontext.output_dir.join_path("class_hierarchy.dot"))
77 f.write(buf.to_s)
78 f.close
79 end
80
81 # Create a dot file representing the classdef hierarchy of a model.
82 # For a simple user of the model, the classdef hierarchy is not really usefull, it is more an internal thing.
83 fun generate_classdef_hierarchy(toolcontext: ToolContext, model: Model)
84 do
85 var buf = new Buffer
86 buf.append("digraph \{\n")
87 buf.append("node [shape=box];\n")
88 buf.append("rankdir=BT;\n")
89 for mmodule in model.mmodules do
90 for mclassdef in mmodule.mclassdefs do
91 buf.append("\"{mclassdef} {mclassdef.bound_mtype}\" [label=\"{mclassdef.mmodule}\\n{mclassdef.bound_mtype}\"];\n")
92 for s in mclassdef.in_hierarchy.direct_greaters do
93 buf.append("\"{mclassdef} {mclassdef.bound_mtype}\" -> \"{s} {s.bound_mtype}\";\n")
94 end
95 end
96 end
97 buf.append("\}\n")
98 var f = new OFStream.open(toolcontext.output_dir.join_path("classdef_hierarchy.dot"))
99 f.write(buf.to_s)
100 f.close
101 end