nitmetrics: refactor self usage metrics computation
[nit.git] / src / metrics / metrics_base.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Helpers for various statistics tools.
18 module metrics_base
19
20 import modelbuilder
21
22 redef class ToolContext
23
24 # --self
25 var opt_self = new OptionBool("Compute metrics about the usage of explicit and implicit self", "--self")
26 # --nullables
27 var opt_nullables = new OptionBool("Compute metrics on nullables send", "--nullables")
28 # --static-types
29 var opt_static_types = new OptionBool("Compute explicit static types metrics", "--static-types")
30 # --tables
31 var opt_tables = new OptionBool("Compute tables metrics", "--tables")
32 # --rta
33 var opt_rta = new OptionBool("Compute RTA metrics", "--rta")
34 # --generate_hyperdoc
35 var opt_generate_hyperdoc = new OptionBool("Generate Hyperdoc", "--generate_hyperdoc")
36
37 var opt_dir = new OptionString("Directory where some statistics files are generated", "-d", "--dir")
38 var output_dir: String = "."
39
40 redef init
41 do
42 super
43 self.option_context.add_option(opt_self)
44 self.option_context.add_option(opt_nullables)
45 self.option_context.add_option(opt_static_types)
46 self.option_context.add_option(opt_tables)
47 self.option_context.add_option(opt_rta)
48 self.option_context.add_option(opt_generate_hyperdoc)
49 self.option_context.add_option(opt_dir)
50 end
51
52 redef fun process_options
53 do
54 super
55 var val = self.opt_dir.value
56 if val != null then
57 val = val.simplify_path
58 val.mkdir
59 self.output_dir = val
60 end
61 end
62 end
63
64 # A counter counts occurence of things
65 # Use this instead of a HashMap[E, Int]
66 class Counter[E: Object]
67 # Total number of counted occurences
68 var total: Int = 0
69
70 private var map = new HashMap[E, Int]
71
72 # The number of counted occurences of `e'
73 fun [](e: E): Int
74 do
75 var map = self.map
76 if map.has_key(e) then return map[e]
77 return 0
78 end
79
80 # Count one more occurence of `e'
81 fun inc(e: E)
82 do
83 self.map[e] = self[e] + 1
84 total += 1
85 end
86
87 # Return an array of elements sorted by occurences
88 fun sort: Array[E]
89 do
90 var res = map.keys.to_a
91 var sorter = new CounterSorter[E](self)
92 sorter.sort(res)
93 #res.sort !cmp a, b = map[a] <=> map[b]
94 return res
95 end
96 end
97
98 private class CounterSorter[E: Object]
99 super AbstractSorter[E]
100 var counter: Counter[E]
101 redef fun compare(a,b) do return self.counter.map[a] <=> self.counter.map[b]
102 end
103
104 # Helper function to display n/d and handle division by 0
105 fun div(n: Int, d: Int): String
106 do
107 if d == 0 then return "na"
108 return ((100*n/d).to_f/100.0).to_precision(2)
109 end