Merge: doc: fixed some typos and other misc. corrections
[nit.git] / src / metrics / mmodules_metrics.nit
index 4af3424..b740912 100644 (file)
 # Collect common metrics about modules
 module mmodules_metrics
 
-import model
 import metrics_base
-import phase
-import frontend
+import model::model_collect
 
 redef class ToolContext
-       var mmodules_metrics_phase = new MModulesMetricsPhase(self, null)
+
+       # MModules related metrics phase
+       var mmodules_metrics_phase: Phase = new MModulesMetricsPhase(self, null)
 end
 
 # Extract metrics about modules from the model.
 private class MModulesMetricsPhase
        super Phase
-       redef fun process_mainmodule(mainmodule)
+       redef fun process_mainmodule(mainmodule, given_mmodules)
        do
                if not toolcontext.opt_mmodules.value and not toolcontext.opt_all.value then return
+               var csv = toolcontext.opt_csv.value
+               var out = "{toolcontext.opt_dir.value or else "metrics"}/mmodules"
+               out.mkdir
+
+               var model = toolcontext.modelbuilder.model
 
                print toolcontext.format_h1("\n# MModules metrics")
 
-               var metrics = new MModuleMetricSet
-               metrics.register(new MNOA, new MNOP, new MNOC, new MNOD, new MDIT)
-               metrics.register(new MNBI, new MNBR, new MNBCC, new MNBAC, new MNBIC)
+               var metrics = new MetricSet
+               metrics.register(new MNOA(model, mainmodule))
+               metrics.register(new MNOP(model, mainmodule))
+               metrics.register(new MNOC(model, mainmodule))
+               metrics.register(new MNOD(model, mainmodule))
+               metrics.register(new MDIT(model, mainmodule))
+               metrics.register(new MNBI(model, mainmodule))
+               metrics.register(new MNBR(model, mainmodule))
+               metrics.register(new MNBCC(model, mainmodule))
+               metrics.register(new MNBAC(model, mainmodule))
+               metrics.register(new MNBIC(model, mainmodule))
 
-               var model = toolcontext.modelbuilder.model
                var mmodules = new HashSet[MModule]
-               for mproject in model.mprojects do
+               for mpackage in model.mpackages do
 
-                       print  toolcontext.format_h2("\n ## project {mproject}")
-
-                       for mgroup in mproject.mgroups do
+                       print  toolcontext.format_h2("\n ## package {mpackage}")
+                       for mgroup in mpackage.mgroups do
                                if mgroup.mmodules.is_empty then continue
 
                                # Scalar metrics
                                print  toolcontext.format_h3("  `- group {mgroup.full_name}")
-
                                mmodules.add_all(mgroup.mmodules)
-                               metrics.collect(new HashSet[MModule].from(mgroup.mmodules), mainmodule)
-                               for name, metric in metrics.metrics do
-                                       if metric isa IntMetric then
-                                               print toolcontext.format_h4("\t{name}: {metric.desc}")
-                                               print toolcontext.format_p("\t    avg: {metric.avg}")
-                                               var max = metric.max
-                                               print  toolcontext.format_p("\t    max: {max.first} ({max.second})")
-                                               var min = metric.min
-                                               print  toolcontext.format_p("\t    min: {min.first} ({min.second})")
-                                       end
-                               end
+                               metrics.clear
+                               metrics.collect(new HashSet[MModule].from(mgroup.mmodules))
+                               metrics.to_console(1, not toolcontext.opt_nocolors.value)
+                               if csv then metrics.to_csv.write_to_file("{out}/{mgroup}.csv")
                        end
                end
                if not mmodules.is_empty then
                        # Global metrics
                        print  toolcontext.format_h2("\n ## global metrics")
-
-                       metrics.collect(mmodules, mainmodule)
-                       for name, metric in metrics.metrics do
-                               if metric isa IntMetric then
-                                       print toolcontext.format_h4( "\t{name}: {metric.desc}")
-                                       print  toolcontext.format_p("\t    avg: {metric.avg}")
-                                       var max = metric.max
-                                               print  toolcontext.format_p("\t    max: {max.first} ({max.second})")
-                                       var min = metric.min
-                                       print  toolcontext.format_p("\t    min: {min.first} ({min.second})")
-                               end
-                       end
+                       metrics.clear
+                       metrics.collect(mmodules)
+                       metrics.to_console(1, not toolcontext.opt_nocolors.value)
+                       if csv then metrics.to_csv.write_to_file("{out}/summary.csv")
                end
        end
 end
 
-# A MetricSet for metrics about MModules
-class MModuleMetricSet
-       super MetricSet
-       redef type METRIC: MModuleMetric
-
-       # Collect all the metrics on the set of MModules
-       fun collect(mmodules: Set[MModule], mainmodule: MModule) do
-               clear
-               for metric in metrics.values do
-                       metric.collect(mmodules)
-               end
-       end
-end
-
-# An abstract Metric on MModules
+# A metric about MModule
 abstract class MModuleMetric
        super Metric
        redef type ELM: MModule
+
+       # Model view used to collect and filter entities
+       var model: Model
+
+       # Mainmodule used for linearization
+       var mainmodule: MModule
+
+       # Filter to apply on model if any
+       var filter: nullable ModelFilter
 end
 
 # Module Metric: Number of Ancestors
@@ -174,6 +164,25 @@ class MDIT
        end
 end
 
+# Module Metric: Number of Accessible Definitions (of all kind)
+#
+# count all mclasses accessible by the module
+class MNBD
+       super MModuleMetric
+       super IntMetric
+       redef fun name do return "mnbd"
+       redef fun desc do return "number of definition accessibles in module"
+
+       redef fun collect(mmodules) do
+               for mmodule in mmodules do
+                       values[mmodule] = 0
+                       for a in mmodule.collect_ancestors(mainmodule, filter) do
+                               values[mmodule] += a.intro_mclasses.length
+                       end
+               end
+       end
+end
+
 # Module Metric: Number of Introduction (of all kind)
 #
 # count all mclasses introduced by the module
@@ -281,5 +290,3 @@ class MNBEC
                end
        end
 end
-
-