Merge: introduce nit_env.sh to setup the shell environement
[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 model.tpl_module(ctx, mainmodule)
38 tpl.add "\}"
39 return tpl
40 end
41 end
42
43 redef class Model
44 # Returns a UML package diagram of `main`
45 redef fun tpl_module(ctx, main) do
46 return main.tpl_module(ctx, main)
47 end
48 end
49
50 redef class MModule
51 redef fun tpl_module(ctx, main) do
52 var t = new Template
53 t.add "subgraph cluster{name} \{\n"
54 t.add "label = \"{name}\"\n"
55 for i in mclassdefs do
56 if not ctx.private_gen and i.mclass.visibility != public_visibility then continue
57 t.add i.tpl_module(ctx, main)
58 end
59 t.add "\}\n"
60 return t
61 end
62 end
63
64 redef class MEntity
65 # Builds a dot UML package diagram entity from `self`
66 fun tpl_module(ctx: ToolContext, main: MModule): Writable is abstract
67 end
68
69 redef class MClassDef
70
71 # Colour for the border of a class when first introduced
72 #
73 # Defaults to a shade of green
74 var intro_colour = "#58B26A"
75
76 # Colour for the border of a class when refined
77 #
78 # Defaults to a shade of red
79 var redef_colour = "#B24758"
80
81 redef fun tpl_module(ctx, main) do
82 var t = new Template
83 t.add "{mmodule}{name} [\n\tlabel = \"\{"
84 if mclass.kind == abstract_kind then
85 t.add "abstract\\n{name}"
86 else if mclass.kind == interface_kind then
87 t.add "interface\\n{name}"
88 else
89 t.add "{name}"
90 end
91 if mclass.arity > 0 then
92 t.add "["
93 var mparameters = mclass.mparameters
94 t.add mparameters.first.name
95 for i in [1 .. mparameters.length[ do
96 t.add ", "
97 t.add mparameters[i].name
98 end
99 t.add "]"
100 end
101 t.add "|"
102 for i in mpropdefs do
103 if not i isa MAttributeDef then continue
104 if not ctx.private_gen and i.mproperty.visibility != public_visibility then continue
105 t.add i.tpl_module(ctx, main)
106 t.add "\\l"
107 end
108 t.add "|"
109 for i in mpropdefs do
110 if not i isa MMethodDef then continue
111 if not ctx.private_gen and i.mproperty.visibility != public_visibility then continue
112 t.add i.tpl_module(ctx, main)
113 t.add "\\l"
114 end
115 t.add "\}\""
116 if is_intro then
117 t.add "color=\"{intro_colour}\""
118 else
119 t.add "color=\"{redef_colour}\""
120 end
121 t.add "\n]\n"
122 var supers = in_hierarchy.direct_greaters
123 for i in supers do
124 if i.mmodule != mmodule then continue
125 t.add "{i.mmodule}{i.name} -> {mmodule}{name} [dir=back"
126 if i.mclass.kind == interface_kind then
127 t.add " arrowtail=open style=dashed"
128 else
129 t.add " arrowtail=empty"
130 end
131 t.add "]\n"
132 end
133 return t
134 end
135 end
136
137 redef class MMethodDef
138 redef fun tpl_module(ctx, main) do
139 var t = new Template
140 t.add mproperty.visibility.tpl_class
141 t.add " "
142 t.add name.escape_to_dot
143 t.add msignature.tpl_class(ctx, main)
144 return t
145 end
146 end
147
148 redef class MAttributeDef
149 redef fun tpl_module(ctx, main) do
150 var t = new Template
151 t.add mproperty.visibility.tpl_class
152 t.add " "
153 t.add name
154 t.add ": "
155 t.add static_mtype.tpl_class(ctx, main)
156 return t
157 end
158 end