Collect common metrics about modules

Introduced classes

class MDIT

nitc :: MDIT

Module Metric: Depth in Tree
abstract class MModuleMetric

nitc :: MModuleMetric

A metric about MModule
private class MModulesMetricsPhase

nitc :: MModulesMetricsPhase

Extract metrics about modules from the model.
class MNBAC

nitc :: MNBAC

Module Metric: Number of Abstract Class in module (intro + redef)
class MNBCC

nitc :: MNBCC

Module Metric: Number of Concrete Class in module (intro + redef)
class MNBD

nitc :: MNBD

Module Metric: Number of Accessible Definitions (of all kind)
class MNBEC

nitc :: MNBEC

Module Metric: Number of Enum in module (intro + redef)
class MNBI

nitc :: MNBI

Module Metric: Number of Introduction (of all kind)
class MNBIC

nitc :: MNBIC

Module Metric: Number of Interface in module (intro + redef)
class MNBR

nitc :: MNBR

Module Metric: Number of Refinement
class MNOA

nitc :: MNOA

Module Metric: Number of Ancestors
class MNOC

nitc :: MNOC

Module Metric: Number of Children
class MNOD

nitc :: MNOD

Module Metric: Number of Descendants
class MNOP

nitc :: MNOP

Module Metric: Number of Parents

Redefined classes

redef class ToolContext

nitc :: mmodules_metrics $ ToolContext

Global context for tools

All class definitions

class MDIT

nitc $ MDIT

Module Metric: Depth in Tree
abstract class MModuleMetric

nitc $ MModuleMetric

A metric about MModule
private class MModulesMetricsPhase

nitc $ MModulesMetricsPhase

Extract metrics about modules from the model.
class MNBAC

nitc $ MNBAC

Module Metric: Number of Abstract Class in module (intro + redef)
class MNBCC

nitc $ MNBCC

Module Metric: Number of Concrete Class in module (intro + redef)
class MNBD

nitc $ MNBD

Module Metric: Number of Accessible Definitions (of all kind)
class MNBEC

nitc $ MNBEC

Module Metric: Number of Enum in module (intro + redef)
class MNBI

nitc $ MNBI

Module Metric: Number of Introduction (of all kind)
class MNBIC

nitc $ MNBIC

Module Metric: Number of Interface in module (intro + redef)
class MNBR

nitc $ MNBR

Module Metric: Number of Refinement
class MNOA

nitc $ MNOA

Module Metric: Number of Ancestors
class MNOC

nitc $ MNOC

Module Metric: Number of Children
class MNOD

nitc $ MNOD

Module Metric: Number of Descendants
class MNOP

nitc $ MNOP

Module Metric: Number of Parents
redef class ToolContext

nitc :: mmodules_metrics $ ToolContext

Global context for tools
package_diagram nitc::mmodules_metrics mmodules_metrics nitc::metrics_base metrics_base nitc::mmodules_metrics->nitc::metrics_base nitc::model_collect model_collect nitc::mmodules_metrics->nitc::model_collect nitc::modelbuilder modelbuilder nitc::metrics_base->nitc::modelbuilder csv csv nitc::metrics_base->csv counter counter nitc::metrics_base->counter nitc::model_filters model_filters nitc::model_collect->nitc::model_filters ...nitc::modelbuilder ... ...nitc::modelbuilder->nitc::modelbuilder ...csv ... ...csv->csv ...counter ... ...counter->counter ...nitc::model_filters ... ...nitc::model_filters->nitc::model_filters nitc::mendel_metrics mendel_metrics nitc::mendel_metrics->nitc::mmodules_metrics nitc::inheritance_metrics inheritance_metrics nitc::inheritance_metrics->nitc::mmodules_metrics nitc::rta_metrics rta_metrics nitc::rta_metrics->nitc::mmodules_metrics nitc::metrics metrics nitc::metrics->nitc::mendel_metrics nitc::metrics->nitc::inheritance_metrics nitc::metrics->nitc::rta_metrics nitc::metrics... ... nitc::metrics...->nitc::metrics

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module annotation

nitc :: annotation

Management and utilities on annotations
module array

core :: array

This module introduces the standard array structure.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module caching

serialization :: caching

Services for caching serialization engines
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module console

console :: console

Defines some ANSI Terminal Control Escape Sequences.
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module counter

counter :: counter

Simple numerical statistical analysis and presentation
module csv

csv :: csv

CSV document handling.
module digraph

graph :: digraph

Implementation of directed graphs, also called digraphs.
module engine_tools

serialization :: engine_tools

Advanced services for serialization engines
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module ini

ini :: ini

Read and write INI configuration files
module inspect

serialization :: inspect

Refine Serializable::inspect to show more useful information
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module lexer

nitc :: lexer

Lexer and its tokens.
module lexer_work

nitc :: lexer_work

Internal algorithm and data structures for the Nit lexer
module list

core :: list

This module handle double linked lists
module literal

nitc :: literal

Parsing of literal values in the abstract syntax tree.
module loader

nitc :: loader

Loading of Nit source files
module location

nitc :: location

Nit source-file and locations in source-file
module math

core :: math

Mathematical operations
module mdoc

nitc :: mdoc

Documentation of model entities
module meta

meta :: meta

Simple user-defined meta-level to manipulate types of instances as object.
module mmodule

nitc :: mmodule

modules and module hierarchies in the metamodel
module mmodule_data

nitc :: mmodule_data

Define and retrieve data in modules
module model

nitc :: model

Classes, types and properties
module model_base

nitc :: model_base

The abstract concept of model and related common things
module model_examples

nitc :: model_examples

Examples for Model entities
module modelbuilder_base

nitc :: modelbuilder_base

Load nit source files and build the associated model
module modelize_class

nitc :: modelize_class

Analysis and verification of class definitions to instantiate model element
module modelize_property

nitc :: modelize_property

Analysis and verification of property definitions to instantiate model element
module more_collections

more_collections :: more_collections

Highly specific, but useful, collections-related classes.
module mpackage

nitc :: mpackage

Modelisation of a Nit package
module native

core :: native

Native structures for text and bytes
module nitpm_shared

nitc :: nitpm_shared

Services related to the Nit package manager
module numeric

core :: numeric

Advanced services for Numeric types
module opts

opts :: opts

Management of options on the command line
module ordered_tree

ordered_tree :: ordered_tree

Manipulation and presentation of ordered trees.
module parse_annotations

nitc :: parse_annotations

Simple annotation parsing
module parser

nitc :: parser

Parser.
module parser_nodes

nitc :: parser_nodes

AST nodes of the Nit language
module parser_prod

nitc :: parser_prod

Production AST nodes full definition.
module parser_work

nitc :: parser_work

Internal algorithm and data structures for the Nit parser
module phase

nitc :: phase

Phases of the processing of nit programs
module poset

poset :: poset

Pre order sets and partial order set (ie hierarchies)
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module serialization

serialization :: serialization

General serialization services
module serialization_core

serialization :: serialization_core

Abstract services to serialize Nit objects to different formats
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module tables

nitc :: tables

Module that interfaces the parsing tables.
module template

template :: template

Basic template system
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module toolcontext

nitc :: toolcontext

Common command-line tool infrastructure than handle options and error messages
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O
module version

nitc :: version

This file was generated by git-gen-version.sh

Parents

module metrics_base

nitc :: metrics_base

Helpers for various statistics tools.
module model_collect

nitc :: model_collect

Collect things from the model.

Children

module inheritance_metrics

nitc :: inheritance_metrics

Collect metrics about inheritance usage
module mendel_metrics

nitc :: mendel_metrics

The Mendel model helps to understand class hierarchies.
module rta_metrics

nitc :: rta_metrics

Metrics from RTA

Descendants

module a_star-m

a_star-m

module api

nitc :: api

Components required to build a web server about the nit model.
module metrics

nitc :: metrics

Various statistics about Nit models and programs
module nitmetrics

nitc :: nitmetrics

A program that collects various metrics on nit programs and libraries
module nitweb

nitc :: nitweb

Runs a webserver based on nitcorn that render things from model.
# Collect common metrics about modules
module mmodules_metrics

import metrics_base
import model::model_collect

redef class ToolContext

	# 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, 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 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 mmodules = new HashSet[MModule]
		for mpackage in model.mpackages 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.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.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 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
class MNOA
	super MModuleMetric
	super IntMetric
	redef fun name do return "mnoa"
	redef fun desc do return "number of ancestor modules"

	redef fun collect(mmodules) do
		for mmodule in mmodules do
			values[mmodule] = mmodule.in_importation.greaters.length - 1
		end
	end
end

# Module Metric: Number of Parents
class MNOP
	super MModuleMetric
	super IntMetric
	redef fun name do return "mnop"
	redef fun desc do return "number of parent modules"

	redef fun collect(mmodules) do
		for mmodule in mmodules do
			values[mmodule] = mmodule.in_importation.direct_greaters.length
		end
	end
end

# Module Metric: Number of Children
class MNOC
	super MModuleMetric
	super IntMetric
	redef fun name do return "mnoc"
	redef fun desc do return "number of child modules"

	redef fun collect(mmodules) do
		for mmodule in mmodules do
			values[mmodule] = mmodule.in_importation.direct_smallers.length
		end
	end
end

# Module Metric: Number of Descendants
class MNOD
	super MModuleMetric
	super IntMetric
	redef fun name do return "mnod"
	redef fun desc do return "number of descendant modules"

	redef fun collect(mmodules) do
		for mmodule in mmodules do
			values[mmodule] = mmodule.in_importation.smallers.length - 1
		end
	end
end

# Module Metric: Depth in Tree
class MDIT
	super MModuleMetric
	super IntMetric
	redef fun name do return "mdit"
	redef fun desc do return "depth in module tree"

	redef fun collect(mmodules) do
		for mmodule in mmodules do
			values[mmodule] = mmodule.in_importation.depth
		end
	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
class MNBI
	super MModuleMetric
	super IntMetric
	redef fun name do return "mnbi"
	redef fun desc do return "number of introduction in module"

	redef fun collect(mmodules) do
		for mmodule in mmodules do
			values[mmodule] = mmodule.intro_mclasses.length
		end
	end
end

# Module Metric: Number of Refinement
#
# count all mclasses refined in the module
class MNBR
	super MModuleMetric
	super IntMetric
	redef fun name do return "mnbr"
	redef fun desc do return "number of refinement in module"

	redef fun collect(mmodules) do
		for mmodule in mmodules do
			var value = 0
			for mclassdef in mmodule.mclassdefs do
				if not mclassdef.is_intro then value += 1
			end
			values[mmodule] = value
		end
	end
end

# Module Metric: Number of Concrete Class in module (intro + redef)
class MNBCC
	super MModuleMetric
	super IntMetric
	redef fun name do return "mnbcc"
	redef fun desc do return "number of concrete class in module (intro + redef)"

	redef fun collect(mmodules) do
		for mmodule in mmodules do
			var value = 0
			for mclassdef in mmodule.mclassdefs do
				if mclassdef.mclass.kind == concrete_kind then value += 1
			end
			values[mmodule] = value
		end
	end
end

# Module Metric: Number of Abstract Class in module (intro + redef)
class MNBAC
	super MModuleMetric
	super IntMetric
	redef fun name do return "mnbac"
	redef fun desc do return "number of abstract class in module (intro + redef)"

	redef fun collect(mmodules) do
		for mmodule in mmodules do
			var value = 0
			for mclassdef in mmodule.mclassdefs do
				if mclassdef.mclass.kind == abstract_kind then value += 1
			end
			values[mmodule] = value
		end
	end
end

# Module Metric: Number of Interface in module (intro + redef)
class MNBIC
	super MModuleMetric
	super IntMetric
	redef fun name do return "mnbic"
	redef fun desc do return "number of interface in module (intro + redef)"

	redef fun collect(mmodules) do
		for mmodule in mmodules do
			var value = 0
			for mclassdef in mmodule.mclassdefs do
				if mclassdef.mclass.kind == interface_kind then value += 1
			end
			values[mmodule] = value
		end
	end
end

# Module Metric: Number of Enum in module (intro + redef)
class MNBEC
	super MModuleMetric
	super IntMetric
	redef fun name do return "mnbec"
	redef fun desc do return "number of enum in module (intro + redef)"

	redef fun collect(mmodules) do
		for mmodule in mmodules do
			var value = 0
			for mclassdef in mmodule.mclassdefs do
				if mclassdef.mclass.kind == enum_kind then value += 1
			end
			values[mmodule] = value
		end
	end
end
src/metrics/mmodules_metrics.nit:17,1--292,3