1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 # Copyright 2014 Alexandre Terrasa <alexandre@moz-code.org>
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 # Helpers for various statistics tools.
26 redef class ToolContext
29 var opt_all
= new OptionBool("Compute all metrics", "--all")
32 var opt_mmodules
= new OptionBool("Compute metrics about mmodules", "--mmodules")
34 var opt_mclasses
= new OptionBool("Compute metrics about mclasses", "--mclasses")
37 var opt_inheritance
= new OptionBool("Compute metrics about inheritance usage", "--inheritance")
39 var opt_refinement
= new OptionBool("Compute metrics about refinement usage", "--refinement")
41 var opt_self
= new OptionBool("Compute metrics about the usage of explicit and implicit self", "--self")
43 var opt_ast
= new OptionBool("Compute metrics about the usage of nodes and identifiers in the AST", "--ast")
45 var opt_nullables
= new OptionBool("Compute metrics on nullables send", "--nullables")
47 var opt_static_types
= new OptionBool("Compute explicit static types metrics", "--static-types")
49 var opt_tables
= new OptionBool("Compute tables metrics", "--tables")
51 var opt_rta
= new OptionBool("Compute RTA metrics", "--rta")
53 var opt_generate_csv
= new OptionBool("Generate CVS format metrics", "--generate-csv")
55 var opt_generate_hyperdoc
= new OptionBool("Generate Hyperdoc", "--generate_hyperdoc")
57 var opt_poset
= new OptionBool("Complete metrics on posets", "--poset")
60 var opt_nocolors
= new OptionBool("Disable colors in console outputs", "--no-colors")
63 var opt_dir
= new OptionString("Directory where some statistics files are generated", "-d", "--dir")
64 var output_dir
: String = "."
69 self.option_context
.add_option
(opt_all
)
70 self.option_context
.add_option
(opt_mmodules
)
71 self.option_context
.add_option
(opt_mclasses
)
72 self.option_context
.add_option
(opt_inheritance
)
73 self.option_context
.add_option
(opt_refinement
)
74 self.option_context
.add_option
(opt_self
)
75 self.option_context
.add_option
(opt_ast
)
76 self.option_context
.add_option
(opt_nullables
)
77 self.option_context
.add_option
(opt_static_types
)
78 self.option_context
.add_option
(opt_tables
)
79 self.option_context
.add_option
(opt_rta
)
80 self.option_context
.add_option
(opt_generate_csv
)
81 self.option_context
.add_option
(opt_generate_hyperdoc
)
82 self.option_context
.add_option
(opt_poset
)
83 self.option_context
.add_option
(opt_dir
)
84 self.option_context
.add_option
(opt_nocolors
)
87 redef fun process_options
90 var val
= self.opt_dir
.value
92 val
= val
.simplify_path
98 # colorize heading 1 for console output
99 fun format_h1
(str
: String): String do
100 if opt_nocolors
.value
then return str
101 return str
.yellow
.bold
104 fun format_h2
(str
: String): String do
105 if opt_nocolors
.value
then return str
109 fun format_h3
(str
: String): String do
110 if opt_nocolors
.value
then return str
114 fun format_h4
(str
: String): String do
115 if opt_nocolors
.value
then return str
119 fun format_p
(str
: String): String do
120 if opt_nocolors
.value
then return str
121 return str
.light_gray
127 # is the class imported from standard lib?
128 fun is_standard
: Bool do
129 return self.intro_mmodule
.mgroup
.mproject
.name
== "standard"
134 # is the module imported from standard lib?
135 fun is_standard
: Bool do
136 return self.mgroup
.mproject
.name
== "standard"
140 # A Metric is used to collect data about things
142 # The concept is reified here for a better organization and documentation
144 fun name
: String is abstract
145 fun desc
: String is abstract
146 # clear all results for this metric
147 fun clear
is abstract
150 # A Metric that collects integer data
152 # Used to count things
153 class IntMetric[E
: Object]
156 var values
= new Counter[E
]
158 redef fun clear
do values
.clear
160 # Return the couple with the highest value
161 fun max
: Couple[E
, Int] do
162 assert not values
.is_empty
163 var elem
= values
.max
.as(not null)
164 var value
= values
[elem
]
165 return new Couple[E
, Int](elem
, value
)
168 # Return the couple with the lowest value
169 fun min
: Couple[E
, Int] do
170 assert not values
.is_empty
171 var elem
= values
.min
.as(not null)
172 var value
= values
[elem
]
173 return new Couple[E
, Int](elem
, value
)
177 fun avg
: Float do return values
.avg
180 # A Metric that collects float datas
182 # Used sor summarization
183 class FloatMetric[E
: Object]
186 var values
: Map[E
, Float] = new HashMap[E
, Float]
188 redef fun clear
do values
.clear
190 # Return the couple with the highest value
191 fun max
: Couple[E
, Float] do
192 assert not values
.is_empty
193 var max
: nullable Float = null
194 var elem
: nullable E
= null
195 for e
, v
in values
do
196 if max
== null or v
> max
then
201 return new Couple[E
, Float](elem
.as(not null), max
.as(not null))
204 # Return the couple with the lowest value
205 fun min
: Couple[E
, Float] do
206 assert not values
.is_empty
207 var min
: nullable Float = null
208 var elem
: nullable E
= null
209 for e
, v
in values
do
210 if min
== null or v
< min
then
215 return new Couple[E
, Float](elem
.as(not null), min
.as(not null))
220 if values
.is_empty
then return 0.0
222 for value
in values
.values
do
225 return sum
/ values
.length
.to_f
229 # A MetricSet is a metric holder
231 # It purpose is to be extended with a metric collect service
236 var metrics
: Map[String, METRIC] = new HashMap[String, METRIC]
238 # Add a metric to the set
239 fun register
(metrics
: METRIC...) do for metric
in metrics
do self.metrics
[metric
.name
] = metric
241 # Clear all results for all metrics
242 fun clear
do for metric
in metrics
.values
do metric
.clear