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 # Provides facilities of exporting a `Model` to a UML class diagram
19 import model
::model_collect
22 # Generates a UML class diagram from a `Model`
23 fun generate_class_uml
: Writable do
24 var tpl
= new Template
25 tpl
.add
"digraph G \{\n"
26 tpl
.add
""" fontname = "Bitstream Vera Sans"
29 fontname = "Bitstream Vera Sans"
35 fontname = "Bitstream Vera Sans"
38 tpl
.add model
.tpl_class
(ctx
, mainmodule
)
46 # Generates a UML Class diagram from the entities of a `Model`
47 redef fun tpl_class
(ctx
, main
) do
50 if not ctx
.private_gen
and i
.visibility
!= public_visibility
then continue
51 t
.add i
.tpl_class
(ctx
, main
)
60 # Generates a dot-compatible `Writable` UML Class diagram from `self`
61 fun tpl_class
(ctx
: ToolContext, main
: MModule): Writable is abstract
66 redef fun tpl_class
(ctx
, main
) do
68 t
.add
"{name} [\n label = \"\
{"
69 if kind == abstract_kind then
70 t.add "abstract\\n
{name}"
71 else if kind == interface_kind then
72 t.add "interface\\n
{name}"
78 t.add mparameters.first.name
79 for i in [1 .. mparameters.length[ do
81 t.add mparameters[i].name
86 var props: Collection[MProperty]
87 if ctx.private_gen then
88 props = collect_intro_mproperties(none_visibility)
90 props = collect_intro_mproperties(public_visibility)
93 if i isa MAttribute then
94 t.add i.tpl_class(ctx, main)
100 if ctx.private_gen then
101 meths = collect_intro_mmethods(none_visibility)
103 meths = collect_intro_mmethods(public_visibility)
106 t.add i.tpl_class(ctx, main)
110 var g
= in_hierarchy
(main
).direct_greaters
112 if not ctx
.private_gen
and i
.visibility
!= public_visibility
then continue
113 t
.add
"{i.name} -> {name} [dir=back"
114 if i
.kind
== interface_kind
then
115 t
.add
" arrowtail=open style=dashed"
117 t
.add
" arrowtail=empty"
127 redef fun tpl_class
(ctx
, main
) do
128 var tpl
= new Template
129 tpl
.add visibility
.tpl_class
131 tpl
.add name
.escape_to_dot
132 tpl
.add intro
.msignature
.tpl_class
(ctx
, main
)
137 redef class MSignature
139 redef fun tpl_class
(ctx
, main
) do
142 var params
= new Array[MParameter]
143 for i
in mparameters
do
146 if params
.length
> 0 then
147 t
.add params
.first
.name
149 t
.add params
.first
.mtype
.tpl_class
(ctx
, main
)
150 for i
in [1 .. params
.length
[ do
154 t
.add params
[i
].mtype
.tpl_class
(ctx
, main
)
158 if return_mtype
!= null then
160 t
.add return_mtype
.tpl_class
(ctx
, main
)
166 redef class MAttribute
167 redef fun tpl_class
(ctx
, main
) do
168 var tpl
= new Template
169 tpl
.add visibility
.tpl_class
173 tpl
.add intro
.static_mtype
.tpl_class
(ctx
, main
)
178 redef class MVisibility
179 # Returns the visibility as a UML token
181 # assert public_visibility.tpl_class == "+"
182 # assert private_visibility.tpl_class == "-"
183 fun tpl_class
: Writable do
184 if self == private_visibility
then
186 else if self == protected_visibility
then
188 else if self == public_visibility
then
196 redef class MClassType
197 redef fun tpl_class
(c
, m
) do
202 redef class MGenericType
203 redef fun tpl_class
(c
, m
) do
205 t
.add name
.substring
(0, name
.index_of
('['))
207 t
.add arguments
.first
.tpl_class
(c
, m
)
208 for i
in [1 .. arguments
.length
[ do
210 t
.add arguments
[i
].tpl_class
(c
, m
)
217 redef class MParameterType
218 redef fun tpl_class
(c
, m
) do
223 redef class MVirtualType
224 redef fun tpl_class
(c
, m
) do
229 redef class MNullableType
230 redef fun tpl_class
(c
, m
) do
233 t
.add mtype
.tpl_class
(c
, m
)