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})"
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 loosly adaptation of the linerarisation 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
65 super Comparator[Object]
66 var mins
= new HashMap [MGroup, nullable MModule]
67 var maxs
= new HashMap [MGroup, nullable MModule]
68 fun min
(o
: Object): nullable MModule do
69 if o
isa MModule then return o
71 if not mins
.has_key
(o
) then computeminmax
(o
)
74 fun max
(o
: Object): nullable MModule do
75 if o
isa MModule then return o
77 if not maxs
.has_key
(o
) then computeminmax
(o
)
80 fun computeminmax
(o
: MGroup) do
81 if not tree
.sub
.has_key
(o
) then
86 var subs
= tree
.sub
[o
]
87 var minres
= min
(subs
.first
)
88 var maxres
= max
(subs
.first
)
89 var order
= minres
.model
.mmodule_importation_hierarchy
92 if c
== null then continue
93 if minres
== null or order
.compare
(minres
, c
) > 0 then minres
= c
95 if c
== null then continue
96 if maxres
== null or order
.compare
(maxres
, c
) < 0 then maxres
= c
100 #if minres != maxres then print "* {o} {minres}..{maxres}"
102 redef fun compare
(a
,b
) do
106 if mb
== null then return 0 else return -1
107 else if mb
== null then
110 var order
= ma
.model
.mmodule_importation_hierarchy
111 return order
.compare
(ma
, mb
)
113 var tree
: OrderedTree[Object]
117 # Generate a MProjectTree based on the projects, groups and modules known in the model
118 fun to_mproject_tree
: MProjectTree
120 var res
= new MProjectTree(self)
121 for p
in mprojects
do
122 for g
in p
.mgroups
do
124 for m
in g
.mmodules
do
133 # Generate graphiz files based on projects, groups and modules
135 # Interessing elements must be selected. See `mmodules`, ``
136 # Display configuration can be set. See `cluster_group`, `project_group`
140 # The model where to look for information
143 # Set of projects to expand fully (ie all groups and modules are displayed)
144 # Initially empty, projects can be added
145 var mprojects
= new HashSet[MProject]
147 # Set of modules to display
148 # Initially empty, modules can be added
149 var mmodules
= new HashSet[MModule]
151 private fun node_for
(mmodule
: MModule): nullable String
153 return "m_{mmodule.object_id}"
156 # Should groups be shown as clusters?
157 var cluster_group
writable = true
159 # Should projects be shown as clusters?
160 var project_group
writable = true
162 # Recursively generate noed ans clusters for a mroup
163 private fun dot_cluster
(o
: OStream, mgroup
: MGroup)
165 # Open the cluster, if required
166 if mgroup
.parent
== null then
167 # is is a root group, so display the project
168 if project_group
then
169 o
.write
("subgraph cluster_{mgroup.object_id} \{\nlabel=\"{mgroup.mproject.name}\\n({mgroup.filepath})\"\ncolor
=black\nstyle
=dotted\n
")
172 if cluster_group then
173 o.write("subgraph cluster_
{mgroup.object_id} \
{\nlabel=\"{mgroup.name}\
"\ncolor=blue\nstyle=dotted\n")
177 # outputs the mmodules to show
178 for mmodule
in mgroup
.mmodules
do
179 if not mmodules
.has
(mmodule
) then continue
180 o
.write
("\t{node_for(mmodule)} [label=\"{mmodule.name}\
",color=green]\n")
183 # recusively progress on sub-clusters
184 for d
in mgroup
.in_nesting
.direct_smallers
do
188 # close the cluster if required
189 if mgroup
.parent
== null then
190 if project_group
then o
.write
("\}\n")
192 if cluster_group
then o
.write
("\}\n")
196 # Extends the set of `mmodules` by recurdively adding the most specific imported modules of foreign projects
197 fun collect_important_importation
199 var todo
= new List[MModule]
200 todo
.add_all
(mmodules
)
201 while not todo
.is_empty
do
204 for psm
in m
.in_importation
.greaters
do
205 if m
.mgroup
.mproject
!= psm
.mgroup
.mproject
then continue
206 for ssm
in psm
.in_importation
.direct_greaters
do
207 if psm
.mgroup
.mproject
== ssm
.mgroup
.mproject
then continue
215 # Generate the dot content with the current configuration
216 redef fun write_to
(stream
)
218 # Collect interessing nodes
219 for m
in model
.mmodules
do
220 # filter out modules outside wanted projects
221 if m
.mgroup
== null then continue
222 if not mprojects
.has
(m
.mgroup
.mproject
) then continue
227 collect_important_importation
229 # Collect interessing edges
230 var sub_hierarchy
= new POSet[MModule]
232 sub_hierarchy
.add_node
(m
)
233 for sm
in m
.in_importation
.greaters
do
234 if sm
== m
then continue
235 if not mmodules
.has
(sm
) then continue
236 sub_hierarchy
.add_edge
(m
, sm
)
240 stream
.write
("digraph g \{\n")
241 stream
.write
("rankdir=BT;node[shape=box];\n")
243 for p
in model
.mprojects
do
244 dot_cluster
(stream
, p
.root
.as(not null))
248 for sm
in sub_hierarchy
[m
].direct_greaters
do
250 var nsm
= node_for
(sm
)
251 if m
.in_importation
.direct_greaters
.has
(sm
) then
252 stream
.write
("\t{nm} -> {nsm}[style=bold]\n")
254 stream
.write
("\t{nm} -> {nsm}[style=solid]\n")