Merge: doc: fixed some typos and other misc. corrections
[nit.git] / src / uml / uml_module.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 # Services for generation of a UML package diagram based on a `Model`
16 module uml_module
17
18 import uml_base
19 import uml_class
20
21 redef class UMLModel
22 # Generates a UML package diagram from a `Model`
23 fun generate_package_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 edge [
34 fontname = "Bitstream Vera Sans"
35 fontsize = 8
36 ]\n"""
37 tpl.add mainmodule.tpl_module(self)
38 tpl.add "\}"
39 return tpl
40 end
41 end
42
43 redef class MEntity
44 # Builds a dot UML package diagram entity from `self`
45 fun tpl_module(model: UMLModel): Writable is abstract
46 end
47
48 redef class MModule
49 redef fun tpl_module(model) do
50 var name = self.name.escape_to_dot
51 var t = new Template
52 t.add "subgraph cluster{name} \{\n"
53 t.add "label = \"{name}\"\n"
54 for i in mclassdefs do
55 if not model.filter.accept_mentity(i) then continue
56 t.add i.tpl_module(model)
57 end
58 t.add "\}\n"
59 return t
60 end
61 end
62
63 redef class MClassDef
64
65 # Colour for the border of a class when first introduced
66 #
67 # Defaults to a shade of green
68 var intro_colour = "#58B26A"
69
70 # Colour for the border of a class when refined
71 #
72 # Defaults to a shade of red
73 var redef_colour = "#B24758"
74
75 redef fun tpl_module(model) do
76 var name = self.name.escape_to_dot
77 var t = new Template
78 t.add "{mmodule.name.escape_to_dot}{name} [\n\tlabel = \"\{"
79 if mclass.kind == abstract_kind then
80 t.add "abstract\\n{name}"
81 else if mclass.kind == interface_kind then
82 t.add "interface\\n{name}"
83 else
84 t.add "{name}"
85 end
86 if mclass.arity > 0 then
87 t.add "["
88 var mparameters = mclass.mparameters
89 t.add mparameters.first.name
90 for i in [1 .. mparameters.length[ do
91 t.add ", "
92 t.add mparameters[i].name
93 end
94 t.add "]"
95 end
96 t.add "|"
97 for i in mpropdefs do
98 if not i isa MAttributeDef then continue
99 if not model.filter.accept_mentity(i) then continue
100 t.add i.tpl_module(model)
101 t.add "\\l"
102 end
103 t.add "|"
104 for i in mpropdefs do
105 if not i isa MMethodDef then continue
106 if not model.filter.accept_mentity(i) then continue
107 t.add i.tpl_module(model)
108 t.add "\\l"
109 end
110 t.add "\}\""
111 if is_intro then
112 t.add "color=\"{intro_colour}\""
113 else
114 t.add "color=\"{redef_colour}\""
115 end
116 t.add "\n]\n"
117 var supers = in_hierarchy.direct_greaters
118 for i in supers do
119 if i.mmodule != mmodule then continue
120 t.add "{i.mmodule}{i.name} -> {mmodule}{name} [dir=back"
121 if i.mclass.kind == interface_kind then
122 t.add " arrowtail=open style=dashed"
123 else
124 t.add " arrowtail=empty"
125 end
126 t.add "]\n"
127 end
128 return t
129 end
130 end
131
132 redef class MMethodDef
133 redef fun tpl_module(model) do
134 var t = new Template
135 t.add mproperty.visibility.tpl_class
136 t.add " "
137 t.add name.escape_to_dot
138 t.add msignature.tpl_class(model)
139 return t
140 end
141 end
142
143 redef class MAttributeDef
144 redef fun tpl_module(model) do
145 var t = new Template
146 t.add mproperty.visibility.tpl_class
147 t.add " "
148 t.add name
149 t.add ": "
150 t.add static_mtype.tpl_class(model)
151 return t
152 end
153 end