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
21 # Generates a UML class diagram from a `Model`
22 fun generate_class_uml
: Streamable do
23 var tpl
= new Template
24 tpl
.add
"digraph G \{\n"
25 tpl
.add
""" fontname = "Bitstream Vera Sans"
28 fontname = "Bitstream Vera Sans"
34 fontname = "Bitstream Vera Sans"
37 tpl
.add model
.tpl_class
(ctx
, mainmodule
)
45 # Generates a UML Class diagram from the entities of a `Model`
46 fun tpl_class
(ctx
: ToolContext, main
: MModule): Streamable do
49 if not ctx
.private_gen
and i
.visibility
!= public_visibility
then continue
50 t
.add i
.tpl_class
(ctx
, main
)
59 # Generates a dot-compatible `Streamable` UML Class diagram from `self`
60 fun tpl_class
(ctx
: ToolContext, main
: MModule): Streamable is abstract
65 redef fun tpl_class
(ctx
, main
): Streamable do
67 t
.add
"{name} [\n label = \"\
{"
68 if kind == abstract_kind then
69 t.add "abstract\\n
{name}"
70 else if kind == interface_kind then
71 t.add "interface\\n
{name}"
77 var formal = intro.parameter_names
79 for i in [1 .. formal.length[ do
86 var props: Collection[MProperty]
87 if ctx.private_gen then
88 props = intro_mproperties(none_visibility)
90 props = intro_mproperties(public_visibility)
93 if i isa MAttribute then
94 t.add i.tpl_class(ctx, main)
99 for i in intro_methods do
100 if not ctx.private_gen and i.visibility != public_visibility then continue
101 t.add i.tpl_class(ctx, main)
105 var g
= in_hierarchy
(main
).direct_greaters
107 if not ctx
.private_gen
and i
.visibility
!= public_visibility
then continue
108 t
.add
"{i.name} -> {name} [dir=back"
109 if i
.kind
== interface_kind
then
110 t
.add
" arrowtail=open style=dashed"
112 t
.add
" arrowtail=empty"
122 redef fun tpl_class
(ctx
, main
) do
123 var tpl
= new Template
124 tpl
.add visibility
.tpl_class
126 tpl
.add name
.escape_to_dot
127 tpl
.add intro
.msignature
.tpl_class
(ctx
, main
)
132 redef class MSignature
134 redef fun tpl_class
(ctx
, main
) do
137 var params
= new Array[MParameter]
138 for i
in mparameters
do
141 if params
.length
> 0 then
142 t
.add params
.first
.name
144 t
.add params
.first
.mtype
.tpl_class
(ctx
, main
)
145 for i
in [1 .. params
.length
[ do
149 t
.add params
[i
].mtype
.tpl_class
(ctx
, main
)
153 if return_mtype
!= null then
155 t
.add return_mtype
.tpl_class
(ctx
, main
)
161 redef class MAttribute
162 redef fun tpl_class
(ctx
, main
) do
163 var tpl
= new Template
164 tpl
.add visibility
.tpl_class
168 tpl
.add intro
.static_mtype
.tpl_class
(ctx
, main
)
173 redef class MVisibility
174 # Returns the visibility as a UML token
176 # assert public_visibility.tpl_class == "+"
177 # assert private_visibility.tpl_class == "-"
178 fun tpl_class
: Streamable do
179 if self == private_visibility
then
181 else if self == protected_visibility
then
183 else if self == public_visibility
then
191 redef class MClassType
192 redef fun tpl_class
(c
, m
) do
197 redef class MGenericType
198 redef fun tpl_class
(c
, m
) do
200 t
.add name
.substring
(0, name
.index_of
('['))
202 t
.add arguments
.first
.tpl_class
(c
, m
)
203 for i
in [1 .. arguments
.length
[ do
205 t
.add arguments
[i
].tpl_class
(c
, m
)
212 redef class MParameterType
213 redef fun tpl_class
(c
, m
) do
214 var n
= mclass
.intro
.parameter_names
219 redef class MVirtualType
220 redef fun tpl_class
(c
, m
) do
225 redef class MNullableType
226 redef fun tpl_class
(c
, m
) do
229 t
.add mtype
.tpl_class
(c
, m
)