lib/core: `is_numeric` returns false on empty strings
[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 t = new Template
51 t.add "subgraph cluster{name} \{\n"
52 t.add "label = \"{name}\"\n"
53 for i in mclassdefs do
54 if not model.view.accept_mentity(i) then continue
55 t.add i.tpl_module(model)
56 end
57 t.add "\}\n"
58 return t
59 end
60 end
61
62 redef class MClassDef
63
64 # Colour for the border of a class when first introduced
65 #
66 # Defaults to a shade of green
67 var intro_colour = "#58B26A"
68
69 # Colour for the border of a class when refined
70 #
71 # Defaults to a shade of red
72 var redef_colour = "#B24758"
73
74 redef fun tpl_module(model) do
75 var t = new Template
76 t.add "{mmodule}{name} [\n\tlabel = \"\{"
77 if mclass.kind == abstract_kind then
78 t.add "abstract\\n{name}"
79 else if mclass.kind == interface_kind then
80 t.add "interface\\n{name}"
81 else
82 t.add "{name}"
83 end
84 if mclass.arity > 0 then
85 t.add "["
86 var mparameters = mclass.mparameters
87 t.add mparameters.first.name
88 for i in [1 .. mparameters.length[ do
89 t.add ", "
90 t.add mparameters[i].name
91 end
92 t.add "]"
93 end
94 t.add "|"
95 for i in mpropdefs do
96 if not i isa MAttributeDef then continue
97 if not model.view.accept_mentity(i) then continue
98 t.add i.tpl_module(model)
99 t.add "\\l"
100 end
101 t.add "|"
102 for i in mpropdefs do
103 if not i isa MMethodDef then continue
104 if not model.view.accept_mentity(i) then continue
105 t.add i.tpl_module(model)
106 t.add "\\l"
107 end
108 t.add "\}\""
109 if is_intro then
110 t.add "color=\"{intro_colour}\""
111 else
112 t.add "color=\"{redef_colour}\""
113 end
114 t.add "\n]\n"
115 var supers = in_hierarchy.direct_greaters
116 for i in supers do
117 if i.mmodule != mmodule then continue
118 t.add "{i.mmodule}{i.name} -> {mmodule}{name} [dir=back"
119 if i.mclass.kind == interface_kind then
120 t.add " arrowtail=open style=dashed"
121 else
122 t.add " arrowtail=empty"
123 end
124 t.add "]\n"
125 end
126 return t
127 end
128 end
129
130 redef class MMethodDef
131 redef fun tpl_module(model) do
132 var t = new Template
133 t.add mproperty.visibility.tpl_class
134 t.add " "
135 t.add name.escape_to_dot
136 t.add msignature.tpl_class(model)
137 return t
138 end
139 end
140
141 redef class MAttributeDef
142 redef fun tpl_module(model) do
143 var t = new Template
144 t.add mproperty.visibility.tpl_class
145 t.add " "
146 t.add name
147 t.add ": "
148 t.add static_mtype.tpl_class(model)
149 return t
150 end
151 end