1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Visualisation of Nit models
21 # A simple specialisation of OrderedTree to display projects, groups and modules
22 # FIXME do not use Object, but a better common interface of MModule and MGroup
24 super OrderedTree[Object]
26 # The model where to look for information
29 init(model
: Model) do self.model
= model
31 redef fun display
(a
) do
33 if a
.parent
== null then return "{a.mproject.name} ({a.filepath.to_s})"
34 return a
.name
+ " (group)"
35 else if a
isa MModule then
42 private var linex_comparator
: nullable LinexComparator = null
44 # Sort modules and groups with their names
47 sort_with
(alpha_comparator
)
50 # Sort modules and groups with a loosely adaptation of the linearization of modules
53 var c
= linex_comparator
55 c
= new LinexComparator(self)
62 # Compare modules and groups using the
63 # FIXME do not use Object, but a better common interface of MModule and MGroup
64 private class LinexComparator
67 redef type COMPARED: Object
69 var mins
= new HashMap [MGroup, nullable MModule]
70 var maxs
= new HashMap [MGroup, nullable MModule]
71 fun mini
(o
: Object): nullable MModule do
72 if o
isa MModule then return o
74 if not mins
.has_key
(o
) then computeminmax
(o
)
77 fun maxi
(o
: Object): nullable MModule do
78 if o
isa MModule then return o
80 if not maxs
.has_key
(o
) then computeminmax
(o
)
83 fun computeminmax
(o
: MGroup) do
84 if not tree
.sub
.has_key
(o
) then
89 var subs
= tree
.sub
[o
]
90 var minres
= mini
(subs
.first
)
91 var maxres
= maxi
(subs
.first
)
92 var order
= minres
.model
.mmodule_importation_hierarchy
95 if c
== null then continue
96 if minres
== null or order
.compare
(minres
, c
) > 0 then minres
= c
98 if c
== null then continue
99 if maxres
== null or order
.compare
(maxres
, c
) < 0 then maxres
= c
103 #if minres != maxres then print "* {o} {minres}..{maxres}"
105 redef fun compare
(a
,b
) do
109 if mb
== null then return 0 else return -1
110 else if mb
== null then
113 var order
= ma
.model
.mmodule_importation_hierarchy
114 return order
.compare
(ma
, mb
)
116 var tree
: OrderedTree[Object]
120 # Generate a MProjectTree based on the projects, groups and modules known in the model
121 fun to_mproject_tree
: MProjectTree
123 var res
= new MProjectTree(self)
124 for p
in mprojects
do
125 for g
in p
.mgroups
do
127 for m
in g
.mmodules
do
136 # Generate graphiz files based on projects, groups and modules
138 # Interesting elements must be selected. See `mmodules`, ``
139 # Display configuration can be set. See `cluster_group`, `project_group`
143 # The model where to look for information
146 # Set of projects to expand fully (ie all groups and modules are displayed)
147 # Initially empty, projects can be added
148 var mprojects
= new HashSet[MProject]
150 # Set of modules to display
151 # Initially empty, modules can be added
152 var mmodules
= new HashSet[MModule]
154 private fun node_for
(mmodule
: MModule): String
156 return "m_{mmodule.object_id}"
159 # Should groups be shown as clusters?
160 var cluster_group
= true is writable
162 # Should projects be shown as clusters?
163 var project_group
= true is writable
165 # Recursively generate node and clusters for a mgroup
166 private fun dot_cluster
(o
: OStream, mgroup
: MGroup)
168 # Open the cluster, if required
169 if mgroup
.parent
== null then
170 # is is a root group, so display the project
171 if project_group
then
172 o
.write
("subgraph cluster_{mgroup.object_id} \{\nlabel=\"{mgroup.mproject.name}\\n({mgroup.filepath.to_s})\"\ncolor
=black\nstyle
=dotted\n
")
175 if cluster_group then
176 o.write("subgraph cluster_
{mgroup.object_id} \
{\nlabel=\"{mgroup.name}\
"\ncolor=blue\nstyle=dotted\n")
180 # outputs the mmodules to show
181 for mmodule
in mgroup
.mmodules
do
182 if not mmodules
.has
(mmodule
) then continue
183 o
.write
("\t{node_for(mmodule)} [label=\"{mmodule.name}\
",color=green]\n")
186 # recursively progress on sub-clusters
187 for d
in mgroup
.in_nesting
.direct_smallers
do
191 # close the cluster if required
192 if mgroup
.parent
== null then
193 if project_group
then o
.write
("\}\n")
195 if cluster_group
then o
.write
("\}\n")
199 # Extends the set of `mmodules` by recursively adding the most specific imported modules of foreign projects
200 fun collect_important_importation
202 var todo
= new List[MModule]
203 todo
.add_all
(mmodules
)
204 while not todo
.is_empty
do
207 for psm
in m
.in_importation
.greaters
do
208 if m
.mgroup
.mproject
!= psm
.mgroup
.mproject
then continue
209 for ssm
in psm
.in_importation
.direct_greaters
do
210 if psm
.mgroup
.mproject
== ssm
.mgroup
.mproject
then continue
218 # Generate the dot content with the current configuration
219 redef fun write_to
(stream
)
221 # Collect interesting nodes
222 for m
in model
.mmodules
do
223 # filter out modules outside wanted projects
224 if m
.mgroup
== null then continue
225 if not mprojects
.has
(m
.mgroup
.mproject
) then continue
230 collect_important_importation
232 # Collect interesting edges
233 var sub_hierarchy
= new POSet[MModule]
235 sub_hierarchy
.add_node
(m
)
236 for sm
in m
.in_importation
.greaters
do
237 if sm
== m
then continue
238 if not mmodules
.has
(sm
) then continue
239 sub_hierarchy
.add_edge
(m
, sm
)
243 stream
.write
("digraph g \{\n")
244 stream
.write
("rankdir=BT;node[shape=box];\n")
246 for p
in model
.mprojects
do
247 dot_cluster
(stream
, p
.root
.as(not null))
251 for sm
in sub_hierarchy
[m
].direct_greaters
do
253 var nsm
= node_for
(sm
)
254 if m
.in_importation
.direct_greaters
.has
(sm
) then
255 stream
.write
("\t{nm} -> {nsm}[style=bold]\n")
257 stream
.write
("\t{nm} -> {nsm}[style=solid]\n")