metrics: move up collect in MetricSet
authorAlexandre Terrasa <alexandre@moz-code.org>
Thu, 6 Mar 2014 15:36:54 +0000 (10:36 -0500)
committerAlexandre Terrasa <alexandre@moz-code.org>
Fri, 7 Mar 2014 06:10:18 +0000 (01:10 -0500)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/metrics/inheritance_metrics.nit
src/metrics/mclasses_metrics.nit
src/metrics/metrics_base.nit
src/metrics/mmodules_metrics.nit

index 56ae196..8c799ac 100644 (file)
@@ -36,7 +36,7 @@ private class InheritanceMetricsPhase
 
                print toolcontext.format_h1("\n# Inheritance metrics")
 
-               var hmetrics = new InheritanceMetricSet
+               var hmetrics = new MetricSet
                hmetrics.register(new MDUI(mainmodule))
                hmetrics.register(new MDUIC(mainmodule))
                hmetrics.register(new MDUII(mainmodule))
@@ -44,7 +44,7 @@ private class InheritanceMetricsPhase
                hmetrics.register(new MIFC(mainmodule))
                hmetrics.register(new MIFI(mainmodule))
 
-               var cmetrics = new MClassMetricSet
+               var cmetrics = new MetricSet
                cmetrics.register(new CNOAC(mainmodule))
                cmetrics.register(new CNOPC(mainmodule))
                cmetrics.register(new CNOCC(mainmodule))
@@ -73,10 +73,10 @@ private class InheritanceMetricsPhase
                                if mod_mclasses.is_empty then continue
                                mmodules.add_all(mgroup.mmodules)
                                mclasses.add_all(mod_mclasses)
-                               cmetrics.collect(new HashSet[MClass].from(mod_mclasses), mainmodule)
-                               for name, metric in cmetrics.metrics do
+                               cmetrics.collect(new HashSet[MClass].from(mod_mclasses))
+                               for metric in cmetrics.metrics do
                                        if metric isa IntMetric then
-                                               print toolcontext.format_h4("\t{name}: {metric.desc}")
+                                               print toolcontext.format_h4("\t{metric.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})")
@@ -84,10 +84,10 @@ private class InheritanceMetricsPhase
                                                print toolcontext.format_p("\t    min: {min.first} ({min.second})")
                                        end
                                end
-                               hmetrics.collect(new HashSet[MModule].from(mgroup.mmodules), mainmodule)
-                               for name, metric in hmetrics.metrics do
+                               hmetrics.collect(new HashSet[MModule].from(mgroup.mmodules))
+                               for metric in hmetrics.metrics do
                                        if metric isa FloatMetric then
-                                               print toolcontext.format_h4("\t{name}: {metric.desc}")
+                                               print toolcontext.format_h4("\t{metric.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})")
@@ -100,10 +100,11 @@ private class InheritanceMetricsPhase
                if not mclasses.is_empty then
                        # Global metrics
                        print toolcontext.format_h2("\n ## global metrics")
-                       cmetrics.collect(mclasses, mainmodule)
-                       for name, metric in cmetrics.metrics do
+                       cmetrics.clear
+                       cmetrics.collect(mclasses)
+                       for metric in cmetrics.metrics do
                                if metric isa IntMetric then
-                                       print toolcontext.format_h4("\t{name}: {metric.desc}")
+                                       print toolcontext.format_h4("\t{metric.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})")
@@ -111,10 +112,11 @@ private class InheritanceMetricsPhase
                                        print toolcontext.format_p("\t    min: {min.first} ({min.second})")
                                end
                        end
-                       hmetrics.collect(mmodules, mainmodule)
-                       for name, metric in hmetrics.metrics do
+                       hmetrics.clear
+                       hmetrics.collect(mmodules)
+                       for metric in hmetrics.metrics do
                                if metric isa FloatMetric then
-                                       print toolcontext.format_h4("\t{name}: {metric.desc}")
+                                       print toolcontext.format_h4("\t{metric.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})")
@@ -126,18 +128,6 @@ private class InheritanceMetricsPhase
        end
 end
 
-# Metric Set used to collect data about inheritance in each module
-class InheritanceMetricSet
-       super MetricSet
-       redef type METRIC: MModuleMetric
-       fun collect(mmodules: Set[MModule], mainmodule: MModule) do
-               clear
-               for metric in metrics.values do
-                       metric.collect(mmodules)
-               end
-       end
-end
-
 # Module metric: proportion of MClasses Defined Using Inheritance
 #
 # Count MClasses that have another parents than Object
index 9b7ddb1..58b0200 100644 (file)
@@ -35,7 +35,7 @@ private class MClassesMetricsPhase
 
                print toolcontext.format_h1("\n# MClasses metrics")
 
-               var metrics = new MClassMetricSet
+               var metrics = new MetricSet
                var min_vis = private_visibility
                metrics.register(new CNOA(mainmodule))
                metrics.register(new CNOP(mainmodule))
@@ -66,10 +66,10 @@ private class MClassesMetricsPhase
                                for mmodule in mgroup.mmodules do mod_mclasses.add_all(mmodule.intro_mclasses)
                                if mod_mclasses.is_empty then continue
                                mclasses.add_all(mod_mclasses)
-                               metrics.collect(new HashSet[MClass].from(mod_mclasses), mainmodule)
-                               for name, metric in metrics.metrics do
+                               metrics.collect(new HashSet[MClass].from(mod_mclasses))
+                               for metric in metrics.metrics do
                                        if metric isa IntMetric then
-                                               print toolcontext.format_h4("\t{name}: {metric.desc}")
+                                               print toolcontext.format_h4("\t{metric.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})")
@@ -82,11 +82,10 @@ private class MClassesMetricsPhase
                if not mclasses.is_empty then
                        # Global metrics
                        print toolcontext.format_h2("\n ## global metrics")
-
-                       metrics.collect(mclasses, mainmodule)
-                       for name, metric in metrics.metrics do
+                       metrics.collect(mclasses)
+                       for metric in metrics.metrics do
                                if metric isa IntMetric then
-                                       print toolcontext.format_h4("\t{name}: {metric.desc}")
+                                       print toolcontext.format_h4("\t{metric.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})")
@@ -98,20 +97,6 @@ private class MClassesMetricsPhase
        end
 end
 
-# An MetricSet for MClasses metrics
-class MClassMetricSet
-       super MetricSet
-       redef type METRIC: MClassMetric
-
-       # Collect all the metrics on the set of MClasses
-       fun collect(mclasses: Set[MClass], mainmodule: MModule) do
-               clear
-               for metric in metrics.values do
-                       metric.collect(mclasses)
-               end
-       end
-end
-
 # A metric about MClass
 interface MClassMetric
        super Metric
index c1fcb51..c5c03aa 100644 (file)
@@ -256,14 +256,20 @@ end
 #
 # It purpose is to be extended with a metric collect service
 class MetricSet
-       type METRIC: Metric
+       type ELM: Object
 
        # Metrics to compute
-       var metrics: Map[String, METRIC] = new HashMap[String, METRIC]
+       var metrics: Set[Metric] = new HashSet[Metric]
 
        # Add a metric to the set
-       fun register(metrics: METRIC...) do for metric in metrics do self.metrics[metric.name] = metric
+       fun register(metrics: Metric...) do for metric in metrics do self.metrics.add(metric)
 
        # Clear all results for all metrics
-       fun clear do for metric in metrics.values do metric.clear
+       fun clear do for metric in metrics do metric.clear
+
+       # Collect all metrics for this set of class
+       fun collect(elements: Set[ELM]) do
+               for metric in metrics do metric.collect(elements)
+       end
+
 end
index 4af3424..f6a169a 100644 (file)
@@ -35,7 +35,7 @@ private class MModulesMetricsPhase
 
                print toolcontext.format_h1("\n# MModules metrics")
 
-               var metrics = new MModuleMetricSet
+               var metrics = new MetricSet
                metrics.register(new MNOA, new MNOP, new MNOC, new MNOD, new MDIT)
                metrics.register(new MNBI, new MNBR, new MNBCC, new MNBAC, new MNBIC)
 
@@ -52,10 +52,10 @@ private class MModulesMetricsPhase
                                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
+                               metrics.collect(new HashSet[MModule].from(mgroup.mmodules))
+                               for metric in metrics.metrics do
                                        if metric isa IntMetric then
-                                               print toolcontext.format_h4("\t{name}: {metric.desc}")
+                                               print toolcontext.format_h4("\t{metric.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})")
@@ -68,11 +68,11 @@ private class MModulesMetricsPhase
                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
+                       metrics.clear
+                       metrics.collect(mmodules)
+                       for metric in metrics.metrics do
                                if metric isa IntMetric then
-                                       print toolcontext.format_h4( "\t{name}: {metric.desc}")
+                                       print toolcontext.format_h4( "\t{metric.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})")
@@ -84,22 +84,8 @@ private class MModulesMetricsPhase
        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
-abstract class MModuleMetric
+# A metric about MModule
+interface MModuleMetric
        super Metric
        redef type ELM: MModule
 end