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 for mclass
in view
.mclasses
do
39 tpl
.add mclass
.tpl_class
(self)
48 # Generates a dot-compatible `Writable` UML Class diagram from `self`
49 fun tpl_class
(model
: UMLModel): Writable is abstract
54 redef fun tpl_class
(model
) do
56 t
.add
"{name} [\n label = \"\
{"
57 if kind == abstract_kind then
58 t.add "abstract\\n
{name}"
59 else if kind == interface_kind then
60 t.add "interface\\n
{name}"
66 t.add mparameters.first.name
67 for i in [1 .. mparameters.length[ do
69 t.add mparameters[i].name
74 var props = collect_intro_mproperties(model.view)
76 if not i isa MAttribute then continue
77 t.add i.tpl_class(model)
82 if not i isa MMethod then continue
83 t.add i.tpl_class(model)
87 var g
= in_hierarchy
(model
.mainmodule
).direct_greaters
89 if not model
.view
.accept_mentity
(i
) then continue
90 t
.add
"{i.name} -> {name} [dir=back"
91 if i
.kind
== interface_kind
then
92 t
.add
" arrowtail=open style=dashed"
94 t
.add
" arrowtail=empty"
104 redef fun tpl_class
(model
) do
105 var tpl
= new Template
106 tpl
.add visibility
.tpl_class
108 tpl
.add name
.escape_to_dot
109 tpl
.add intro
.msignature
.tpl_class
(model
)
114 redef class MSignature
116 redef fun tpl_class
(model
) do
119 var params
= new Array[MParameter]
120 for i
in mparameters
do
123 if params
.length
> 0 then
124 t
.add params
.first
.name
126 t
.add params
.first
.mtype
.tpl_class
(model
)
127 for i
in [1 .. params
.length
[ do
131 t
.add params
[i
].mtype
.tpl_class
(model
)
135 if return_mtype
!= null then
137 t
.add return_mtype
.tpl_class
(model
)
143 redef class MAttribute
144 redef fun tpl_class
(model
) do
145 var tpl
= new Template
146 tpl
.add visibility
.tpl_class
150 tpl
.add intro
.static_mtype
.tpl_class
(model
)
155 redef class MVisibility
156 # Returns the visibility as a UML token
158 # assert public_visibility.tpl_class == "+"
159 # assert private_visibility.tpl_class == "-"
160 fun tpl_class
: Writable do
161 if self == private_visibility
then
163 else if self == protected_visibility
then
165 else if self == public_visibility
then
173 redef class MClassType
174 redef fun tpl_class
(model
) do
179 redef class MGenericType
180 redef fun tpl_class
(model
) do
182 t
.add name
.substring
(0, name
.index_of
('['))
184 t
.add arguments
.first
.tpl_class
(model
)
185 for i
in [1 .. arguments
.length
[ do
187 t
.add arguments
[i
].tpl_class
(model
)
194 redef class MParameterType
195 redef fun tpl_class
(model
) do
200 redef class MVirtualType
201 redef fun tpl_class
(model
) do
206 redef class MNullableType
207 redef fun tpl_class
(model
) do
210 t
.add mtype
.tpl_class
(model
)