csv: Allow output customization.
[nit.git] / src / metrics / metrics_base.nit
index 9824179..c51e656 100644 (file)
@@ -19,6 +19,7 @@
 module metrics_base
 
 import model_utils
+import modelbuilder
 import csv
 import counter
 import console
@@ -32,7 +33,8 @@ redef class ToolContext
        var opt_mmodules = new OptionBool("Compute metrics about mmodules", "--mmodules")
        # --mclassses
        var opt_mclasses = new OptionBool("Compute metrics about mclasses", "--mclasses")
-
+       # --mendel
+       var opt_mendel = new OptionBool("Compute mendel metrics", "--mendel")
        # --inheritance
        var opt_inheritance = new OptionBool("Compute metrics about inheritance usage", "--inheritance")
        # --genericity
@@ -69,6 +71,7 @@ redef class ToolContext
                self.option_context.add_option(opt_all)
                self.option_context.add_option(opt_mmodules)
                self.option_context.add_option(opt_mclasses)
+               self.option_context.add_option(opt_mendel)
                self.option_context.add_option(opt_inheritance)
                self.option_context.add_option(opt_refinement)
                self.option_context.add_option(opt_self)
@@ -84,7 +87,7 @@ redef class ToolContext
                self.option_context.add_option(opt_nocolors)
        end
 
-       redef fun process_options
+       redef fun process_options(args)
        do
                super
                var val = self.opt_dir.value
@@ -168,6 +171,15 @@ interface Metric
 
        # Pretty print the metric results in console
        fun to_console(indent: Int, colors: Bool) do
+               if values.is_empty then
+                       if colors then
+                               print "{"\t" * indent}{name}: {desc} -- nothing".green
+                       else
+                               print "{"\t" * indent}{name}: {desc} -- nothing"
+                       end
+                       return
+               end
+
                var max = self.max
                var min = self.min
                if colors then
@@ -193,6 +205,12 @@ interface Metric
 
        # The element with the lowest value
        fun min: ELM is abstract
+
+       # The value threshold above what elements are considered as 'interesting'
+       fun threshold: Float do return avg + std_dev
+
+       # The set of element above the threshold
+       fun above_threshold: Set[ELM] is abstract
 end
 
 # A Metric that collects integer data
@@ -225,6 +243,24 @@ class IntMetric
        redef fun avg: Float do return values_cache.avg
 
        redef fun std_dev: Float do return values_cache.std_dev
+
+       redef fun above_threshold do
+               var above = new HashSet[ELM]
+               var threshold = threshold
+               for element, value in values do
+                       if value.to_f > threshold then above.add(element)
+               end
+               return above
+       end
+
+       redef fun to_console(indent, colors) do
+               super
+               if colors then
+                       print "{"\t" * indent}  sum: {sum}".light_gray
+               else
+                       print "{"\t" * indent}  sum: {sum}"
+               end
+       end
 end
 
 # A Metric that collects float datas
@@ -284,6 +320,25 @@ class FloatMetric
                end
                return (sum / values.length.to_f).sqrt
        end
+
+       redef fun above_threshold do
+               var above = new HashSet[ELM]
+               var threshold = threshold
+               for element, value in values do
+                       if value > threshold then above.add(element)
+               end
+               return above
+       end
+
+       redef fun to_console(indent, colors) do
+               super
+               if colors then
+                       print "{"\t" * indent}  sum: {sum}".light_gray
+               else
+                       print "{"\t" * indent}  sum: {sum}"
+               end
+       end
+
 end
 
 # A MetricSet is a metric holder
@@ -312,8 +367,10 @@ class MetricSet
        end
 
        # Export the metric set in CSV format
-       fun to_csv: CSVDocument do
-               var csv = new CSVDocument
+       fun to_csv: CsvDocument do
+               var csv = new CsvDocument
+
+               csv.format = new CsvFormat('"', ';', "\n")
 
                # set csv headers
                csv.header.add("entry")
@@ -335,7 +392,7 @@ class MetricSet
                                        line.add("n/a")
                                end
                        end
-                       csv.lines.add(line)
+                       csv.records.add(line)
                end
                return csv
        end