71db364d749ec2250e16acb3874825e067363f02
[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 # --all
25 var opt_all = new OptionBool("Compute all metrics", "--all")
26
27 # --inheritance
28 var opt_inheritance = new OptionBool("Compute metrics about inheritance usage", "--inheritance")
29 # --refinement
30 var opt_refinement = new OptionBool("Compute metrics about refinement usage", "--refinement")
31 # --self
32 var opt_self = new OptionBool("Compute metrics about the usage of explicit and implicit self", "--self")
33 # --nullables
34 var opt_nullables = new OptionBool("Compute metrics on nullables send", "--nullables")
35 # --static-types
36 var opt_static_types = new OptionBool("Compute explicit static types metrics", "--static-types")
37 # --tables
38 var opt_tables = new OptionBool("Compute tables metrics", "--tables")
39 # --rta
40 var opt_rta = new OptionBool("Compute RTA metrics", "--rta")
41 # --generate_hyperdoc
42 var opt_generate_hyperdoc = new OptionBool("Generate Hyperdoc", "--generate_hyperdoc")
43
44 var opt_dir = new OptionString("Directory where some statistics files are generated", "-d", "--dir")
45 var output_dir: String = "."
46
47 redef init
48 do
49 super
50 self.option_context.add_option(opt_all)
51 self.option_context.add_option(opt_inheritance)
52 self.option_context.add_option(opt_refinement)
53 self.option_context.add_option(opt_self)
54 self.option_context.add_option(opt_nullables)
55 self.option_context.add_option(opt_static_types)
56 self.option_context.add_option(opt_tables)
57 self.option_context.add_option(opt_rta)
58 self.option_context.add_option(opt_generate_hyperdoc)
59 self.option_context.add_option(opt_dir)
60 end
61
62 redef fun process_options
63 do
64 super
65 var val = self.opt_dir.value
66 if val != null then
67 val = val.simplify_path
68 val.mkdir
69 self.output_dir = val
70 end
71 end
72 end
73
74 # A counter counts occurence of things
75 # Use this instead of a HashMap[E, Int]
76 class Counter[E: Object]
77 # Total number of counted occurences
78 var total: Int = 0
79
80 private var map = new HashMap[E, Int]
81
82 # The number of counted occurences of `e'
83 fun [](e: E): Int
84 do
85 var map = self.map
86 if map.has_key(e) then return map[e]
87 return 0
88 end
89
90 # Count one more occurence of `e'
91 fun inc(e: E)
92 do
93 self.map[e] = self[e] + 1
94 total += 1
95 end
96
97 # Return an array of elements sorted by occurences
98 fun sort: Array[E]
99 do
100 var res = map.keys.to_a
101 var sorter = new CounterSorter[E](self)
102 sorter.sort(res)
103 #res.sort !cmp a, b = map[a] <=> map[b]
104 return res
105 end
106 end
107
108 private class CounterSorter[E: Object]
109 super AbstractSorter[E]
110 var counter: Counter[E]
111 redef fun compare(a,b) do return self.counter.map[a] <=> self.counter.map[b]
112 end
113
114 # Helper function to display n/d and handle division by 0
115 fun div(n: Int, d: Int): String
116 do
117 if d == 0 then return "na"
118 return ((100*n/d).to_f/100.0).to_precision(2)
119 end