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
55 var name
= name
.escape_to_dot
57 t
.add
"{name} [\n label = \"\
{"
58 if kind == abstract_kind then
59 t.add "abstract\\n
{name}"
60 else if kind == interface_kind then
61 t.add "interface\\n
{name}"
67 t.add mparameters.first.name
68 for i in [1 .. mparameters.length[ do
70 t.add mparameters[i].name
75 var props = collect_intro_mproperties(model.view)
77 if not i isa MAttribute then continue
78 t.add i.tpl_class(model)
83 if not i isa MMethod then continue
84 t.add i.tpl_class(model)
88 var g
= in_hierarchy
(model
.mainmodule
).direct_greaters
90 if not model
.view
.accept_mentity
(i
) then continue
91 t
.add
"{i.name} -> {name} [dir=back"
92 if i
.kind
== interface_kind
then
93 t
.add
" arrowtail=open style=dashed"
95 t
.add
" arrowtail=empty"
105 redef fun tpl_class
(model
) do
106 var tpl
= new Template
107 tpl
.add visibility
.tpl_class
109 tpl
.add name
.escape_to_dot
110 tpl
.add intro
.msignature
.tpl_class
(model
)
115 redef class MSignature
117 redef fun tpl_class
(model
) do
120 var params
= new Array[MParameter]
121 for i
in mparameters
do
124 if params
.length
> 0 then
125 t
.add params
.first
.name
.escape_to_dot
127 t
.add params
.first
.mtype
.tpl_class
(model
)
128 for i
in [1 .. params
.length
[ do
130 t
.add params
[i
].name
.escape_to_dot
132 t
.add params
[i
].mtype
.tpl_class
(model
)
136 if return_mtype
!= null then
138 t
.add return_mtype
.tpl_class
(model
)
144 redef class MAttribute
145 redef fun tpl_class
(model
) do
146 var tpl
= new Template
147 tpl
.add visibility
.tpl_class
149 tpl
.add name
.escape_to_dot
151 tpl
.add intro
.static_mtype
.tpl_class
(model
)
156 redef class MVisibility
157 # Returns the visibility as a UML token
159 # assert public_visibility.tpl_class == "+"
160 # assert private_visibility.tpl_class == "-"
161 fun tpl_class
: Writable do
162 if self == private_visibility
then
164 else if self == protected_visibility
then
166 else if self == public_visibility
then
174 redef class MClassType
175 redef fun tpl_class
(model
) do
180 redef class MGenericType
181 redef fun tpl_class
(model
) do
183 t
.add name
.substring
(0, name
.index_of
('['))
185 t
.add arguments
.first
.tpl_class
(model
)
186 for i
in [1 .. arguments
.length
[ do
188 t
.add arguments
[i
].tpl_class
(model
)
195 redef class MParameterType
196 redef fun tpl_class
(model
) do
201 redef class MVirtualType
202 redef fun tpl_class
(model
) do
207 redef class MNullableType
208 redef fun tpl_class
(model
) do
211 t
.add mtype
.tpl_class
(model
)