Helpers for various statistics tools.

Introduced classes

class FloatMetric

nitc :: FloatMetric

A Metric that collects float datas
class IntMetric

nitc :: IntMetric

A Metric that collects integer data
interface Metric

nitc :: Metric

A Metric is used to collect data about things
class MetricSet

nitc :: MetricSet

A MetricSet is a metric holder

Redefined classes

redef class ToolContext

nitc :: metrics_base $ ToolContext

Global context for tools

All class definitions

class FloatMetric

nitc $ FloatMetric

A Metric that collects float datas
class IntMetric

nitc $ IntMetric

A Metric that collects integer data
interface Metric

nitc $ Metric

A Metric is used to collect data about things
class MetricSet

nitc $ MetricSet

A MetricSet is a metric holder
redef class ToolContext

nitc :: metrics_base $ ToolContext

Global context for tools
package_diagram nitc::metrics_base metrics_base nitc::modelbuilder modelbuilder nitc::metrics_base->nitc::modelbuilder csv csv nitc::metrics_base->csv counter counter nitc::metrics_base->counter nitc::loader loader nitc::modelbuilder->nitc::loader nitc::phase phase nitc::modelbuilder->nitc::phase core core csv->core poset poset counter->poset ...nitc::loader ... ...nitc::loader->nitc::loader ...nitc::phase ... ...nitc::phase->nitc::phase ...core ... ...core->core ...poset ... ...poset->poset nitc::metrics_base... ... nitc::metrics_base...->nitc::metrics_base

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 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 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 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 model

nitc :: model

Classes, types and properties
module model_base

nitc :: model_base

The abstract concept of model and related common things
module modelbuilder_base

nitc :: modelbuilder_base

Load nit source files and build the associated model
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 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 counter

counter :: counter

Simple numerical statistical analysis and presentation
module csv

csv :: csv

CSV document handling.

Children

module ast_metrics

nitc :: ast_metrics

Metrics about the nodes and identifiers in the AST
module detect_covariance

nitc :: detect_covariance

Detect the static usage of covariance in the code.
module detect_variance_constraints

nitc :: detect_variance_constraints

Collect metrics about detected variances constraints on formal types.
module generate_hierarchies

nitc :: generate_hierarchies

Create dot files for various hierarchies of a model.
module mclasses_metrics

nitc :: mclasses_metrics

Collect common metrics about mclasses
module mmodules_metrics

nitc :: mmodules_metrics

Collect common metrics about modules
module model_hyperdoc

nitc :: model_hyperdoc

Dump of Nit model into hypertext human-readable format.
module poset_metrics

nitc :: poset_metrics

Metrics about the various posets of the model of a Nit program
module readme_metrics

nitc :: readme_metrics

Collect common metrics about README files
module refinement_metrics

nitc :: refinement_metrics

Collect metrics about refinement usage
module self_metrics

nitc :: self_metrics

Metrics about the usage of explicit and implicit self
module static_types_metrics

nitc :: static_types_metrics

Metrics on the usage of explicit static types.
module tables_metrics

nitc :: tables_metrics

Metrics on table generation

Descendants

module a_star-m

a_star-m

module api

nitc :: api

Components required to build a web server about the nit model.
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 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.
module nullables_metrics

nitc :: nullables_metrics

Statistics about the usage of nullables
module rta_metrics

nitc :: rta_metrics

Metrics from RTA
# Helpers for various statistics tools.
module metrics_base

import modelbuilder
import csv
import counter
import console

redef class ToolContext

	# --all
	var opt_all = new OptionBool("Compute all metrics", "--all")

	# --mmodules
	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
	var opt_refinement = new OptionBool("Compute metrics about refinement usage", "--refinement")
	# --self
	var opt_self = new OptionBool("Compute metrics about the usage of explicit and implicit self", "--self")
	# --ast
	var opt_ast = new OptionBool("Compute metrics about the usage of nodes and identifiers in the AST", "--ast")
	# --nullables
	var opt_nullables = new OptionBool("Compute metrics on nullables send", "--nullables")
	# --static-types
	var opt_static_types = new OptionBool("Compute explicit static types metrics", "--static-types")
	# --tables
	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("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
	do
		super
		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)
		self.option_context.add_option(opt_ast)
		self.option_context.add_option(opt_nullables)
		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)
		self.option_context.add_option(opt_dir)
		self.option_context.add_option(opt_nocolors)
	end

	redef fun process_options(args)
	do
		super
		var val = self.opt_dir.value
		if val != null then
			val = val.simplify_path
			val.mkdir
			self.output_dir = val
		end
	end

	# 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
	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
	fun clear is abstract

	# Values for each element
	fun values: RES is abstract

	# Collect metric values on elements
	fun collect(elements: Collection[ELM]) is abstract

	# The value calculated for the element
	fun [](element: ELM): VAL do return values[element]

	# Does the element have a value for this metric?
	fun has_element(element: ELM): Bool do return values.has_key(element)

	# The values average
	fun avg: Float is abstract

	# 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
			print "{"\t" * indent}{name}: {desc}".green
			print "{"\t" * indent}  avg: {avg}".light_gray
			print "{"\t" * indent}  max: {max} ({self[max]})".light_gray
			print "{"\t" * indent}  min: {min} ({self[min]})".light_gray
			print "{"\t" * indent}  std: {std_dev}".light_gray
		else
			print "{"\t" * indent}{name}: {desc}"
			print "{"\t" * indent}  avg: {avg}"
			print "{"\t" * indent}  max: {max} ({self[max]})"
			print "{"\t" * indent}  min: {min} ({self[min]})"
			print "{"\t" * indent}  std: {std_dev}"
		end
	end

	# The sum of all the values.
	fun sum: VAL is abstract

	# The values standard derivation
	fun std_dev: Float is abstract

	# The element with the highest value
	fun max: ELM is abstract

	# 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

	# Sort the metric keys by values
	fun sort: Array[ELM] do
		return values.keys_sorted_by_values(default_reverse_comparator)
	end
end

# A Metric that collects integer data
#
# Used to count things
class IntMetric
	super Metric

	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

	redef fun sum do return values_cache.sum

	redef fun max do
		assert not values_cache.is_empty
		return values_cache.max.as(not null)
	end

	redef fun min do
		assert not values_cache.is_empty
		return values_cache.min.as(not null)
	end

	# Values average
	redef fun avg do return values_cache.avg

	redef fun std_dev 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
#
# Used sor summarization
class FloatMetric
	super Metric

	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


	redef fun sum do
		var sum = 0.0
		for v in values.values do
			if v.is_nan then continue
			sum += v
		end
		return sum
	end

	redef fun max do
		assert not values.is_empty
		var max: nullable Float = null
		var elem: nullable ELM = null
		for e, v in values do
			if max == null or v > max then
				max = v
				elem = e
			end
		end
		return elem.as(not null)
	end

	redef fun min do
		assert not values.is_empty
		var min: nullable Float = null
		var elem: nullable ELM = null
		for e, v in values do
			if min == null or v < min then
				min = v
				elem = e
			end
		end
		return elem.as(not null)
	end

	redef fun avg do
		if values.is_empty then return 0.0
		return sum / values.length.to_f
	end

	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
	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
#
# 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
	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.add(metric)

	# Clear all results for all metrics
	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

	# Pretty print the resuls in console
	fun to_console(indent: Int, colors: Bool) do
		for metric in metrics do metric.to_console(indent, colors)
	end

	# Export the metric set in CSV format
	fun to_csv: CsvDocument do
		var csv = new CsvDocument
		csv.separator = ';'

		# set csv headers
		csv.header.add("entry")
		for metric in metrics do csv.header.add(metric.name)

		# collect all entries to merge metric results
		var entries = new HashSet[ELM]
		for metric in metrics do
			for entry in metric.values.keys do entries.add(entry)
		end

		# collect results
		for entry in entries do
			var line = [entry.to_s]
			for metric in metrics do
				if metric.has_element(entry) then
					line.add(metric[entry].to_s)
				else
					line.add("n/a")
				end
			end
			csv.records.add(line)
		end
		return csv
	end
end
src/metrics/metrics_base.nit:18,1--427,3