X-Git-Url: http://nitlanguage.org diff --git a/src/metrics/metrics_base.nit b/src/metrics/metrics_base.nit index 080ceec..959041b 100644 --- a/src/metrics/metrics_base.nit +++ b/src/metrics/metrics_base.nit @@ -18,7 +18,6 @@ # Helpers for various statistics tools. module metrics_base -import model_utils import modelbuilder import csv import counter @@ -51,18 +50,20 @@ redef class ToolContext var opt_tables = new OptionBool("Compute tables metrics", "--tables") # --rta var opt_rta = new OptionBool("Compute RTA metrics", "--rta") + # --readme + var opt_readme = new OptionBool("Compute ReadMe metrics", "--readme") # --generate-csv - var opt_csv = new OptionBool("Export metrics in CSV format", "--csv") + var opt_csv = new OptionBool("Also export metrics in CSV format", "--csv") # --generate_hyperdoc var opt_generate_hyperdoc = new OptionBool("Generate Hyperdoc", "--generate_hyperdoc") # --poset var opt_poset = new OptionBool("Complete metrics on posets", "--poset") - # --no-colors var opt_nocolors = new OptionBool("Disable colors in console outputs", "--no-colors") - - + # --dir var opt_dir = new OptionString("Directory where some statistics files are generated", "-d", "--dir") + + # Output directory for metrics files. var output_dir: String = "." redef init @@ -80,6 +81,7 @@ redef class ToolContext 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_readme) self.option_context.add_option(opt_csv) self.option_context.add_option(opt_generate_hyperdoc) self.option_context.add_option(opt_poset) @@ -98,27 +100,41 @@ redef class ToolContext end end - # colorize heading 1 for console output + # Format and colorize a string heading of level 1 for console output. + # + # Default style is yellow and bold. fun format_h1(str: String): String do if opt_nocolors.value then return str return str.yellow.bold end + # Format and colorize a string heading of level 2 for console output. + # + # Default style is white and bold. fun format_h2(str: String): String do if opt_nocolors.value then return str return str.bold end + # Format and colorize a string heading of level 3 for console output. + # + # Default style is white and nobold. fun format_h3(str: String): String do if opt_nocolors.value then return str return str end + # Format and colorize a string heading of level 4 for console output. + # + # Default style is green. fun format_h4(str: String): String do if opt_nocolors.value then return str return str.green end + # Format and colorize a string heading of level 5 for console output. + # + # Default style is light gray. fun format_p(str: String): String do if opt_nocolors.value then return str return str.light_gray @@ -126,29 +142,24 @@ redef class ToolContext end -redef class MClass - # is the class imported from standard lib? - fun is_standard: Bool do - return self.intro_mmodule.mgroup.mproject.name == "standard" - end -end - -redef class MModule - # is the module imported from standard lib? - fun is_standard: Bool do - return self.mgroup.mproject.name == "standard" - end -end - # A Metric is used to collect data about things # # The concept is reified here for a better organization and documentation interface Metric + + # Type of elements measured by this metric. type ELM: Object + + # Type of values used to measure elements. type VAL: Object + + # Type of data representation used to associate elements and values. type RES: Map[ELM, VAL] + # The name of this metric (generally an acronym about the metric). fun name: String is abstract + + # A long and understandable description about what is measured by this metric. fun desc: String is abstract # Clear all results for this metric @@ -158,7 +169,7 @@ interface Metric fun values: RES is abstract # Collect metric values on elements - fun collect(elements: Set[ELM]) is abstract + fun collect(elements: Collection[ELM]) is abstract # The value calculated for the element fun [](element: ELM): VAL do return values[element] @@ -227,10 +238,12 @@ end class IntMetric super Metric - redef type VAL: Int + redef type VAL: Int is fixed redef type RES: Counter[ELM] + # `IntMetric` uses a Counter to store values in intern. protected var values_cache = new Counter[ELM] + redef fun values do return values_cache redef fun clear do values_cache.clear @@ -248,9 +261,9 @@ class IntMetric end # Values average - redef fun avg: Float do return values_cache.avg + redef fun avg do return values_cache.avg - redef fun std_dev: Float do return values_cache.std_dev + redef fun std_dev do return values_cache.std_dev redef fun above_threshold do var above = new HashSet[ELM] @@ -279,7 +292,9 @@ class FloatMetric redef type VAL: Float + # `FloatMetric` uses a Map to store values in intern. protected var values_cache = new HashMap[ELM, VAL] + redef fun values do return values_cache redef fun clear do values_cache.clear @@ -287,7 +302,10 @@ class FloatMetric redef fun sum do var sum = 0.0 - for v in values.values do sum += v + for v in values.values do + if v.is_nan then continue + sum += v + end return sum end @@ -322,9 +340,10 @@ class FloatMetric return sum / values.length.to_f end - redef fun std_dev: Float do + redef fun std_dev do var sum = 0.0 for value in values.values do + if value.is_nan then continue sum += (value - avg).pow(2.to_f) end return (sum / values.length.to_f).sqrt @@ -353,6 +372,8 @@ end # # It purpose is to be extended with a metric collect service class MetricSet + + # Type of element measured by this `MetricSet`. type ELM: Object # Metrics to compute @@ -377,8 +398,7 @@ class MetricSet # Export the metric set in CSV format fun to_csv: CsvDocument do var csv = new CsvDocument - - csv.format = new CsvFormat('"', ';', "\n") + csv.separator = ';' # set csv headers csv.header.add("entry")