1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Helpers for various statistics tools.
23 redef class ToolContext
26 var opt_all
= new OptionBool("Compute all metrics", "--all")
29 var opt_inheritance
= new OptionBool("Compute metrics about inheritance usage", "--inheritance")
31 var opt_refinement
= new OptionBool("Compute metrics about refinement usage", "--refinement")
33 var opt_self
= new OptionBool("Compute metrics about the usage of explicit and implicit self", "--self")
35 var opt_nullables
= new OptionBool("Compute metrics on nullables send", "--nullables")
37 var opt_static_types
= new OptionBool("Compute explicit static types metrics", "--static-types")
39 var opt_tables
= new OptionBool("Compute tables metrics", "--tables")
41 var opt_rta
= new OptionBool("Compute RTA metrics", "--rta")
43 var opt_generate_csv
= new OptionBool("Generate CVS format metrics", "--generate-csv")
45 var opt_generate_hyperdoc
= new OptionBool("Generate Hyperdoc", "--generate_hyperdoc")
47 var opt_dir
= new OptionString("Directory where some statistics files are generated", "-d", "--dir")
48 var output_dir
: String = "."
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
)
66 redef fun process_options
69 var val
= self.opt_dir
.value
71 val
= val
.simplify_path
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")
105 return self.std_modules_cache
.as(not null)
107 private var std_modules_cache
: nullable Set[String]
111 fun is_class
: Bool do
112 return self.kind
== concrete_kind
or self.kind
== abstract_kind
115 fun is_interface
: Bool do
116 return self.kind
== interface_kind
120 return self.kind
== enum_kind
123 fun is_abstract
: Bool do
124 return self.kind
== abstract_kind
127 fun is_user_defined
: Bool do
128 return self.intro_mmodule
.is_user_defined
133 fun is_user_defined
: Bool do
134 return not self.model
.std_modules
.has
(self.name
)
138 # A counter counts occurence of things
139 # Use this instead of a HashMap[E, Int]
140 class Counter[E
: Object]
141 # Total number of counted occurences
144 private var map
= new HashMap[E
, Int]
146 # The number of counted occurences of `e'
150 if map
.has_key
(e
) then return map
[e
]
154 # Count one more occurence of `e'
157 self.map
[e
] = self[e
] + 1
161 # Return an array of elements sorted by occurences
164 var res
= map
.keys
.to_a
165 var sorter
= new CounterSorter[E
](self)
167 #res.sort !cmp a, b = map[a] <=> map[b]
172 private class CounterSorter[E
: Object]
173 super AbstractSorter[E
]
174 var counter
: Counter[E
]
175 redef fun compare
(a
,b
) do return self.counter
.map
[a
] <=> self.counter
.map
[b
]
178 # Helper function to display n/d and handle division by 0
179 fun div
(n
: Int, d
: Int): String
181 if d
== 0 then return "na"
182 return ((100*n
/d
).to_f
/100.0).to_precision
(2)