Merge: doc: fixed some typos and other misc. corrections
[nit.git] / src / uml / uml_class.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Provides facilities of exporting a `Model` to a UML class diagram
16 module uml_class
17
18 import uml_base
19 import model::model_collect
20
21 redef class UMLModel
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"
27 fontsize = 8
28 node [
29 fontname = "Bitstream Vera Sans"
30 fontsize = 8
31 shape = "record"
32 ]
33
34 edge [
35 fontname = "Bitstream Vera Sans"
36 fontsize = 8
37 ]\n"""
38 for mclass in model.collect_mclasses(filter) do
39 tpl.add mclass.tpl_class(self)
40 tpl.add "\n"
41 end
42 tpl.add "\}"
43 return tpl
44 end
45 end
46
47 redef class MEntity
48 # Generates a dot-compatible `Writable` UML Class diagram from `self`
49 fun tpl_class(model: UMLModel): Writable is abstract
50 end
51
52 redef class MClass
53
54 redef fun tpl_class(model) do
55 var name = name.escape_to_dot
56 var t = new Template
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}"
62 else
63 t.add "{name}"
64 end
65 if arity > 0 then
66 t.add "["
67 t.add mparameters.first.name
68 for i in [1 .. mparameters.length[ do
69 t.add ", "
70 t.add mparameters[i].name
71 end
72 t.add "]"
73 end
74 t.add "|"
75 var props = collect_intro_mproperties(model.filter)
76 for i in props do
77 if not i isa MAttribute then continue
78 t.add i.tpl_class(model)
79 t.add "\\l"
80 end
81 t.add "|"
82 for i in props do
83 if not i isa MMethod then continue
84 t.add i.tpl_class(model)
85 t.add "\\l"
86 end
87 t.add "\}\"\n]\n"
88 var g = in_hierarchy(model.mainmodule).direct_greaters
89 for i in g do
90 if not model.filter.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"
94 else
95 t.add " arrowtail=empty"
96 end
97 t.add "];\n"
98 end
99 return t
100 end
101
102 end
103
104 redef class MMethod
105 redef fun tpl_class(model) do
106 var tpl = new Template
107 tpl.add visibility.tpl_class
108 tpl.add " "
109 tpl.add name.escape_to_dot
110 tpl.add intro.msignature.tpl_class(model)
111 return tpl
112 end
113 end
114
115 redef class MSignature
116
117 redef fun tpl_class(model) do
118 var t = new Template
119 t.add "("
120 var params = new Array[MParameter]
121 for i in mparameters do
122 params.add i
123 end
124 if params.length > 0 then
125 t.add params.first.name.escape_to_dot
126 t.add ": "
127 t.add params.first.mtype.tpl_class(model)
128 for i in [1 .. params.length [ do
129 t.add ", "
130 t.add params[i].name.escape_to_dot
131 t.add ": "
132 t.add params[i].mtype.tpl_class(model)
133 end
134 end
135 t.add ")"
136 if return_mtype != null then
137 t.add ": "
138 t.add return_mtype.tpl_class(model)
139 end
140 return t
141 end
142 end
143
144 redef class MAttribute
145 redef fun tpl_class(model) do
146 var tpl = new Template
147 tpl.add visibility.tpl_class
148 tpl.add " "
149 tpl.add name.escape_to_dot
150 tpl.add ": "
151 tpl.add intro.static_mtype.tpl_class(model)
152 return tpl
153 end
154 end
155
156 redef class MVisibility
157 # Returns the visibility as a UML token
158 #
159 # assert public_visibility.tpl_class == "+"
160 # assert private_visibility.tpl_class == "-"
161 fun tpl_class: Writable do
162 if self == private_visibility then
163 return "-"
164 else if self == protected_visibility then
165 return "#"
166 else if self == public_visibility then
167 return "+"
168 else
169 return "+"
170 end
171 end
172 end
173
174 redef class MClassType
175 redef fun tpl_class(model) do
176 return name
177 end
178 end
179
180 redef class MGenericType
181 redef fun tpl_class(model) do
182 var t = new Template
183 t.add name.substring(0, name.index_of('['))
184 t.add "["
185 t.add arguments.first.tpl_class(model)
186 for i in [1 .. arguments.length[ do
187 t.add ", "
188 t.add arguments[i].tpl_class(model)
189 end
190 t.add "]"
191 return t
192 end
193 end
194
195 redef class MParameterType
196 redef fun tpl_class(model) do
197 return name
198 end
199 end
200
201 redef class MVirtualType
202 redef fun tpl_class(model) do
203 return name
204 end
205 end
206
207 redef class MNullableType
208 redef fun tpl_class(model) do
209 var t = new Template
210 t.add "nullable "
211 t.add mtype.tpl_class(model)
212 return t
213 end
214 end