nitmetrics: now use model_utils
[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 model_utils
21 import csv
22
23 redef class ToolContext
24
25 # --all
26 var opt_all = new OptionBool("Compute all metrics", "--all")
27
28 # --inheritance
29 var opt_inheritance = new OptionBool("Compute metrics about inheritance usage", "--inheritance")
30 # --genericity
31 var opt_refinement = new OptionBool("Compute metrics about refinement usage", "--refinement")
32 # --self
33 var opt_self = new OptionBool("Compute metrics about the usage of explicit and implicit self", "--self")
34 # --nullables
35 var opt_nullables = new OptionBool("Compute metrics on nullables send", "--nullables")
36 # --static-types
37 var opt_static_types = new OptionBool("Compute explicit static types metrics", "--static-types")
38 # --tables
39 var opt_tables = new OptionBool("Compute tables metrics", "--tables")
40 # --rta
41 var opt_rta = new OptionBool("Compute RTA metrics", "--rta")
42 # --generate-csv
43 var opt_generate_csv = new OptionBool("Generate CVS format metrics", "--generate-csv")
44 # --generate_hyperdoc
45 var opt_generate_hyperdoc = new OptionBool("Generate Hyperdoc", "--generate_hyperdoc")
46
47 var opt_dir = new OptionString("Directory where some statistics files are generated", "-d", "--dir")
48 var output_dir: String = "."
49
50 redef init
51 do
52 super
53 self.option_context.add_option(opt_all)
54 self.option_context.add_option(opt_inheritance)
55 self.option_context.add_option(opt_refinement)
56 self.option_context.add_option(opt_self)
57 self.option_context.add_option(opt_nullables)
58 self.option_context.add_option(opt_static_types)
59 self.option_context.add_option(opt_tables)
60 self.option_context.add_option(opt_rta)
61 self.option_context.add_option(opt_generate_csv)
62 self.option_context.add_option(opt_generate_hyperdoc)
63 self.option_context.add_option(opt_dir)
64 end
65
66 redef fun process_options
67 do
68 super
69 var val = self.opt_dir.value
70 if val != null then
71 val = val.simplify_path
72 val.mkdir
73 self.output_dir = val
74 end
75 end
76 end
77
78 redef class Model
79
80 # List of modules in std lib
81 # FIXME this is quite ugly, find a dynamic way...
82 fun std_modules: Set[String] do
83 if self.std_modules_cache == null then
84 self.std_modules_cache = new HashSet[String]
85 self.std_modules_cache.add("collection")
86 self.std_modules_cache.add("abstract_collection")
87 self.std_modules_cache.add("array")
88 self.std_modules_cache.add("hash_collection")
89 self.std_modules_cache.add("list")
90 self.std_modules_cache.add("range")
91 self.std_modules_cache.add("sorter")
92 self.std_modules_cache.add("environ")
93 self.std_modules_cache.add("exec")
94 self.std_modules_cache.add("file")
95 self.std_modules_cache.add("gc")
96 self.std_modules_cache.add("hash")
97 self.std_modules_cache.add("kernel")
98 self.std_modules_cache.add("math")
99 self.std_modules_cache.add("standard")
100 self.std_modules_cache.add("stream")
101 self.std_modules_cache.add("string")
102 self.std_modules_cache.add("string_search")
103 self.std_modules_cache.add("time")
104 end
105 return self.std_modules_cache.as(not null)
106 end
107 private var std_modules_cache: nullable Set[String]
108 end
109
110 redef class MClass
111 fun is_user_defined: Bool do
112 return self.intro_mmodule.is_user_defined
113 end
114 end
115
116 redef class MModule
117 fun is_user_defined: Bool do
118 return not self.model.std_modules.has(self.name)
119 end
120 end
121
122 # A counter counts occurence of things
123 # Use this instead of a HashMap[E, Int]
124 class Counter[E: Object]
125 # Total number of counted occurences
126 var total: Int = 0
127
128 private var map = new HashMap[E, Int]
129
130 # The number of counted occurences of `e'
131 fun [](e: E): Int
132 do
133 var map = self.map
134 if map.has_key(e) then return map[e]
135 return 0
136 end
137
138 # Count one more occurence of `e'
139 fun inc(e: E)
140 do
141 self.map[e] = self[e] + 1
142 total += 1
143 end
144
145 # Return an array of elements sorted by occurences
146 fun sort: Array[E]
147 do
148 var res = map.keys.to_a
149 var sorter = new CounterSorter[E](self)
150 sorter.sort(res)
151 #res.sort !cmp a, b = map[a] <=> map[b]
152 return res
153 end
154 end
155
156 private class CounterSorter[E: Object]
157 super AbstractSorter[E]
158 var counter: Counter[E]
159 redef fun compare(a,b) do return self.counter.map[a] <=> self.counter.map[b]
160 end
161
162 # Helper function to display n/d and handle division by 0
163 fun div(n: Int, d: Int): String
164 do
165 if d == 0 then return "na"
166 return ((100*n/d).to_f/100.0).to_precision(2)
167 end