nitmetrics: now use model_utils
[nit.git] / src / metrics / metrics_base.nit
index 56641ec..627af78 100644 (file)
 # Helpers for various statistics tools.
 module metrics_base
 
-import modelbuilder
+import model_utils
+import csv
 
 redef class ToolContext
 
+       # --all
+       var opt_all = new OptionBool("Compute all metrics", "--all")
+
+       # --inheritance
+       var opt_inheritance = new OptionBool("Compute metrics about inheritance usage", "--inheritance")
+       # --genericity
+       var opt_refinement = new OptionBool("Compute metrics about refinement usage", "--refinement")
        # --self
        var opt_self = new OptionBool("Compute metrics about the usage of explicit and implicit self", "--self")
        # --nullables
@@ -31,6 +39,8 @@ redef class ToolContext
        var opt_tables = new OptionBool("Compute tables metrics", "--tables")
        # --rta
        var opt_rta = new OptionBool("Compute RTA metrics", "--rta")
+       # --generate-csv
+       var opt_generate_csv = new OptionBool("Generate CVS format metrics", "--generate-csv")
        # --generate_hyperdoc
        var opt_generate_hyperdoc = new OptionBool("Generate Hyperdoc", "--generate_hyperdoc")
 
@@ -40,11 +50,15 @@ redef class ToolContext
        redef init
        do
                super
+               self.option_context.add_option(opt_all)
+               self.option_context.add_option(opt_inheritance)
+               self.option_context.add_option(opt_refinement)
                self.option_context.add_option(opt_self)
                self.option_context.add_option(opt_nullables)
                self.option_context.add_option(opt_static_types)
                self.option_context.add_option(opt_tables)
                self.option_context.add_option(opt_rta)
+               self.option_context.add_option(opt_generate_csv)
                self.option_context.add_option(opt_generate_hyperdoc)
                self.option_context.add_option(opt_dir)
        end
@@ -61,6 +75,50 @@ redef class ToolContext
        end
 end
 
+redef class Model
+
+       # List of modules in std lib
+       # FIXME this is quite ugly, find a dynamic way...
+       fun std_modules: Set[String] do
+               if self.std_modules_cache == null then
+                       self.std_modules_cache = new HashSet[String]
+                       self.std_modules_cache.add("collection")
+                       self.std_modules_cache.add("abstract_collection")
+                       self.std_modules_cache.add("array")
+                       self.std_modules_cache.add("hash_collection")
+                       self.std_modules_cache.add("list")
+                       self.std_modules_cache.add("range")
+                       self.std_modules_cache.add("sorter")
+                       self.std_modules_cache.add("environ")
+                       self.std_modules_cache.add("exec")
+                       self.std_modules_cache.add("file")
+                       self.std_modules_cache.add("gc")
+                       self.std_modules_cache.add("hash")
+                       self.std_modules_cache.add("kernel")
+                       self.std_modules_cache.add("math")
+                       self.std_modules_cache.add("standard")
+                       self.std_modules_cache.add("stream")
+                       self.std_modules_cache.add("string")
+                       self.std_modules_cache.add("string_search")
+                       self.std_modules_cache.add("time")
+               end
+               return self.std_modules_cache.as(not null)
+       end
+       private var std_modules_cache: nullable Set[String]
+end
+
+redef class MClass
+       fun is_user_defined: Bool do
+               return self.intro_mmodule.is_user_defined
+       end
+end
+
+redef class MModule
+       fun is_user_defined: Bool do
+               return not self.model.std_modules.has(self.name)
+       end
+end
+
 # A counter counts occurence of things
 # Use this instead of a HashMap[E, Int]
 class Counter[E: Object]