35222456a53714becbddedba5f8f3998d3cc5f3e
[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 metrics_base
22 import model::model_viz
23
24 redef class ToolContext
25 var generate_hierarchies_phase: Phase = new GenerateHierarchyPhase(self, null)
26 end
27
28 private class GenerateHierarchyPhase
29 super Phase
30
31 redef fun process_mainmodule(mainmodule, given_mmodules)
32 do
33 if not toolcontext.opt_generate_hyperdoc.value and not toolcontext.opt_all.value then return
34 var model = toolcontext.modelbuilder.model
35 generate_module_hierarchy(toolcontext, given_mmodules)
36 generate_classdef_hierarchy(toolcontext, model)
37 generate_class_hierarchy(toolcontext, mainmodule)
38 end
39 end
40
41 # Create a dot file representing the module hierarchy of a model.
42 # Importation relation is represented with arrow
43 # Nesting relation is represented with nested boxes
44 fun generate_module_hierarchy(toolcontext: ToolContext, given_mmodules: Collection[MModule])
45 do
46 var model = given_mmodules.first.model
47 var dot = new MProjectDot(model)
48
49 # Collect requested projects
50 for m in given_mmodules do
51 var g = m.mgroup
52 if g == null then continue
53 dot.mprojects.add(g.mproject)
54 end
55 var projectpath = toolcontext.output_dir.join_path("project_hierarchy.dot")
56 print "generating project_hierarchy.dot"
57 dot.write_to_file(projectpath)
58
59 var modulepath = toolcontext.output_dir.join_path("module_hierarchy.dot")
60 dot.mprojects.add_all(model.mprojects)
61 print "generating module_hierarchy.dot"
62 dot.write_to_file(modulepath)
63 end
64
65 # Create a dot file representing the class hierarchy of a model.
66 fun generate_class_hierarchy(toolcontext: ToolContext, mmodule: MModule)
67 do
68 var buf = new FlatBuffer
69 buf.append("digraph \{\n")
70 buf.append("node [shape=box];\n")
71 buf.append("rankdir=BT;\n")
72 var hierarchy = mmodule.flatten_mclass_hierarchy
73 for mclass in hierarchy do
74 buf.append("\"{mclass}\" [label=\"{mclass}\"];\n")
75 for s in hierarchy[mclass].direct_greaters do
76 buf.append("\"{mclass}\" -> \"{s}\";\n")
77 end
78 end
79 buf.append("\}\n")
80 var f = new OFStream.open(toolcontext.output_dir.join_path("class_hierarchy.dot"))
81 f.write(buf.to_s)
82 f.close
83 end
84
85 # Create a dot file representing the classdef hierarchy of a model.
86 # For a simple user of the model, the classdef hierarchy is not really usefull, it is more an internal thing.
87 fun generate_classdef_hierarchy(toolcontext: ToolContext, model: Model)
88 do
89 var buf = new FlatBuffer
90 buf.append("digraph \{\n")
91 buf.append("node [shape=box];\n")
92 buf.append("rankdir=BT;\n")
93 for mmodule in model.mmodules do
94 for mclassdef in mmodule.mclassdefs do
95 buf.append("\"{mclassdef} {mclassdef.bound_mtype}\" [label=\"{mclassdef.mmodule}\\n{mclassdef.bound_mtype}\"];\n")
96 for s in mclassdef.in_hierarchy.direct_greaters do
97 buf.append("\"{mclassdef} {mclassdef.bound_mtype}\" -> \"{s} {s.bound_mtype}\";\n")
98 end
99 end
100 end
101 buf.append("\}\n")
102 var f = new OFStream.open(toolcontext.output_dir.join_path("classdef_hierarchy.dot"))
103 f.write(buf.to_s)
104 f.close
105 end