Graph commands

Commands that return graphical representations about a Model or a MEntity.

Introduced classes

abstract class CmdGraph

nitc :: CmdGraph

An abstract command that returns a dot graph
class CmdInheritanceGraph

nitc :: CmdInheritanceGraph

Render a hierarchy graph for mentity if any.
class CmdUML

nitc :: CmdUML

UML command
class ErrorBadGraphFormat

nitc :: ErrorBadGraphFormat

Bad graph format requested
class InheritanceGraph

nitc :: InheritanceGraph

Graph for mentity hierarchies
class WarningNoUML

nitc :: WarningNoUML

No UML model for mentity

Redefined classes

redef class MClass

nitc :: commands_graph $ MClass

A named class
redef abstract class MEntity

nitc :: commands_graph $ MEntity

A named and possibly documented entity in the model.
redef class MGroup

nitc :: commands_graph $ MGroup

A group of modules in a package
redef class MModule

nitc :: commands_graph $ MModule

A Nit module is usually associated with a Nit source file.
redef class MPackage

nitc :: commands_graph $ MPackage

A Nit package, that encompass a product

All class definitions

abstract class CmdGraph

nitc $ CmdGraph

An abstract command that returns a dot graph
class CmdInheritanceGraph

nitc $ CmdInheritanceGraph

Render a hierarchy graph for mentity if any.
class CmdUML

nitc $ CmdUML

UML command
class ErrorBadGraphFormat

nitc $ ErrorBadGraphFormat

Bad graph format requested
class InheritanceGraph

nitc $ InheritanceGraph

Graph for mentity hierarchies
redef class MClass

nitc :: commands_graph $ MClass

A named class
redef abstract class MEntity

nitc :: commands_graph $ MEntity

A named and possibly documented entity in the model.
redef class MGroup

nitc :: commands_graph $ MGroup

A group of modules in a package
redef class MModule

nitc :: commands_graph $ MModule

A Nit module is usually associated with a Nit source file.
redef class MPackage

nitc :: commands_graph $ MPackage

A Nit package, that encompass a product
class WarningNoUML

nitc $ WarningNoUML

No UML model for mentity
package_diagram nitc::commands_graph commands_graph nitc::commands_model commands_model nitc::commands_graph->nitc::commands_model nitc\>uml\> uml nitc::commands_graph->nitc\>uml\> dot dot nitc::commands_graph->dot nitc::commands_base commands_base nitc::commands_model->nitc::commands_base nitc\>modelize\> modelize nitc::commands_model->nitc\>modelize\> nitc\>model\> model nitc\>uml\>->nitc\>model\> core core dot->core ...nitc::commands_base ... ...nitc::commands_base->nitc::commands_base ...nitc\>modelize\> ... ...nitc\>modelize\>->nitc\>modelize\> ...nitc\>model\> ... ...nitc\>model\>->nitc\>model\> ...core ... ...core->core nitc::md_commands md_commands nitc::md_commands->nitc::commands_graph nitc::commands_parser commands_parser nitc::commands_parser->nitc::commands_graph nitc::commands_http commands_http nitc::commands_http->nitc::commands_graph nitc::json_commands json_commands nitc::json_commands->nitc::commands_graph nitc::commands_docdown commands_docdown nitc::commands_docdown->nitc::md_commands nitc::term term nitc::term->nitc::md_commands nitc::term->nitc::commands_parser nitc::commands_docdown... ... nitc::commands_docdown...->nitc::commands_docdown nitc::term... ... nitc::term...->nitc::term nitc::html_commands html_commands nitc::html_commands->nitc::commands_parser nitc::html_commands... ... nitc::html_commands...->nitc::html_commands nitc::api_base api_base nitc::api_base->nitc::commands_http nitc::api_base->nitc::json_commands nitc::api_base... ... nitc::api_base...->nitc::api_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 abstract_tree

trees :: abstract_tree

Introduce tree structures abstraction
module annotation

nitc :: annotation

Management and utilities on annotations
module array

core :: array

This module introduces the standard array structure.
module bintree

trees :: bintree

Binary Tree data-structure
module bitset

core :: bitset

Services to handle BitSet
module bktree

trees :: bktree

Implementation of BKTree
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 commands_base

nitc :: commands_base

Documentation commands
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 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_collect

nitc :: model_collect

Collect things from the model.
module model_examples

nitc :: model_examples

Examples for Model entities
module model_index

nitc :: model_index

Search things from the Model
module modelbuilder_base

nitc :: modelbuilder_base

Load nit source files and build the associated model
module modelize

nitc :: modelize

Create a model from nit source files
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 rbtree

trees :: rbtree

A red–black tree is a data structure which is a type of self-balancing binary search tree.
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 trees

trees :: trees

General module for tree data structures
module trie

trees :: trie

A trie (or prefix tree) is a datastructure used to perform prefix searches.
module uml_base

nitc :: uml_base

Exposes the base class for UML generation of a Model
module uml_class

nitc :: uml_class

Provides facilities of exporting a Model to a UML class diagram
module uml_module

nitc :: uml_module

Services for generation of a UML package diagram based on a Model
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 commands_model

nitc :: commands_model

Doc commands about a Model or a MEntity
module dot

dot :: dot

Dot rendering library
module uml

nitc :: uml

Group head module for UML generation services

Children

module commands_http

nitc :: commands_http

Initialize commands from HTTP requests
module commands_parser

nitc :: commands_parser

A parser that create DocCommand from a string
module json_commands

nitc :: json_commands

Translate command results to json
module md_commands

nitc :: md_commands

Render commands results as Markdown

Descendants

module a_star-m

a_star-m

module api

nitc :: api

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

nitc :: api_auth

module api_base

nitc :: api_base

Base classes used by nitweb.
module api_docdown

nitc :: api_docdown

Nitdoc specific Markdown format handling for Nitweb
module api_feedback

nitc :: api_feedback

Feedback related features
module api_light

nitc :: api_light

Highlight and collect messages from a piece of code
module api_model

nitc :: api_model

module commands_docdown

nitc :: commands_docdown

Doc down related queries
module html_commands

nitc :: html_commands

Render commands results as HTML
module nitdoc

nitc :: nitdoc

Generator of static API documentation for the Nit language
module nitweb

nitc :: nitweb

Runs a webserver based on nitcorn that render things from model.
module nitx

nitc :: nitx

nitx, a command tool that displays useful data about Nit code
module static

nitc :: static

Nitdoc generation framework
module static_base

nitc :: static_base

Base entities shared by all the nitdoc code
module static_cards

nitc :: static_cards

Cards templates for the static documentation
module static_html

nitc :: static_html

Render documentation pages as HTML
module static_index

nitc :: static_index

Manage indexing of Nit model for Nitdoc QuickSearch.
module static_structure

nitc :: static_structure

Composes the pages of the static documentation
module term

nitc :: term

# Graph commands
#
# Commands that return graphical representations about a Model or a MEntity.
module commands_graph

import commands_model

import uml
import dot

# An abstract command that returns a dot graph
abstract class CmdGraph
	super DocCommand

	# Mainmodule for linearization
	var mainmodule: MModule

	# Rendering format
	#
	# Default is `dot`.
	# See `allowed_formats`.
	var format = "dot" is optional, writable

	# Allowed rendering formats.
	#
	# Can be `dot` or `svg`.
	var allowed_formats: Array[String] = ["dot", "svg"]

	# Dot to render
	var dot: nullable Writable = null is optional, writable

	# Render `dot` depending on `format`
	fun render: nullable Writable do
		var dot = self.dot
		if dot == null then return null
		if format == "svg" then
			var proc = new ProcessDuplex("dot", "-Tsvg")
			var svg = proc.write_and_read(dot.write_to_string)
			proc.close
			proc.wait
			return svg
		end
		return dot
	end

	redef fun init_command do
		if not allowed_formats.has(format) then
			return new ErrorBadGraphFormat(format, allowed_formats)
		end
		return super
	end
end

# Bad graph format requested
class ErrorBadGraphFormat
	super CmdError

	# Provided format
	var format: String

	# Allowed formats
	var allowed_formats: Array[String]

	redef fun to_s do
		var allowed_values = new Buffer
		for allowed in allowed_formats do
			allowed_values.append "`{allowed}`"
			if allowed != allowed_formats.last then
				allowed_values.append ", "
			end
		end
		return "Bad format `{format}`. Allowed values are {allowed_values.write_to_string}."
	end
end

# UML command
#
# Return an UML diagram about a `mentity`.
class CmdUML
	super CmdEntity
	super CmdGraph

	autoinit(model, mainmodule, filter, mentity, mentity_name, format, uml)

	# UML model to return
	var uml: nullable UMLModel = null is optional, writable

	redef fun init_command do
		if uml != null then return new CmdSuccess

		var res = super
		if not res isa CmdSuccess then return res
		var mentity = self.mentity.as(not null)

		if mentity isa MClassDef then mentity = mentity.mclass
		if mentity isa MClass or mentity isa MModule then
			uml = new UMLModel(model, mainmodule, filter)
		else
			return new WarningNoUML(mentity)
		end
		return res
	end

	redef fun render do
		var uml = self.uml
		if uml == null then return null
		if mentity isa MClass then
			dot = uml.generate_class_uml.write_to_string
		else if mentity isa MModule then
			dot = uml.generate_package_uml.write_to_string
		end
		return super
	end
end

# No UML model for `mentity`
class WarningNoUML
	super CmdWarning

	# MEntity provided
	var mentity: MEntity

	redef fun to_s do return "No UML for `{mentity.full_name}`"
end

# Render a hierarchy graph for `mentity` if any.
class CmdInheritanceGraph
	super CmdEntity
	super CmdGraph

	autoinit(model, mainmodule, filter, mentity, mentity_name, pdepth, cdepth, format, graph)

	# Parents depth to display
	var pdepth: nullable Int = null is optional, writable

	# Children depth to display
	var cdepth: nullable Int = null is optional, writable

	# Inheritance graph to return
	var graph: nullable InheritanceGraph = null is optional, writable

	redef fun init_command do
		if graph != null then return new CmdSuccess

		var res = super
		if not res isa CmdSuccess then return res
		var mentity = self.mentity.as(not null)

		graph = new InheritanceGraph(mentity, model, mainmodule, filter)
		return res
	end

	redef fun render do
		var graph = self.graph
		if graph == null then return ""
		self.dot = graph.draw(pdepth, cdepth).to_dot
		return super
	end
end

# Graph for mentity hierarchies
#
# Recursively build parents and children list from a `center`.
class InheritanceGraph

	# MEntity at the center of this graph
	var center: MEntity

	# Model used to build graph from
	var model: Model

	# Mainmodule for class linearization
	var mainmodule: MModule

	# Filter to apply on model if any
	var filter: nullable ModelFilter

	# Graph generated
	var graph: DotGraph is lazy do
		var graph = new DotGraph("package_diagram", "digraph")

		graph["compound"] = "true"
		graph["rankdir"] = "BT"
		graph["ranksep"] = 0.3
		graph["nodesep"] = 0.3

		graph.nodes_attrs["margin"] = 0.1
		graph.nodes_attrs["width"] = 0
		graph.nodes_attrs["height"] = 0
		graph.nodes_attrs["fontsize"] = 10
		graph.nodes_attrs["fontname"] = "helvetica"

		graph.edges_attrs["dir"] = "none"
		graph.edges_attrs["color"] = "gray"

		return graph
	end

	# Build the graph
	fun draw(parents_depth, children_depth: nullable Int): DotGraph do
		draw_node center
		draw_parents(center, parents_depth)
		draw_children(center, children_depth)
		return graph
	end

	private var nodes = new HashMap[MEntity, DotElement]
	private var done_parents = new HashSet[MEntity]
	private var done_children = new HashSet[MEntity]

	# Recursively draw parents of mentity
	fun draw_parents(mentity: MEntity, max_depth: nullable Int, current_depth: nullable Int) do
		if done_parents.has(mentity) then return
		done_parents.add mentity
		current_depth = current_depth or else 0
		if max_depth != null and current_depth >= max_depth then
			from_dotdotdot(mentity)
			return
		end
		var parents = mentity.collect_parents(mainmodule, filter)
		if parents.length > 10 then
			from_dotdotdot(mentity)
			return
		end
		for parent in parents do
			if parent isa MModule then
				var mgroup = parent.mgroup
				if mgroup != null and mgroup.default_mmodule == parent then parent = mgroup
			end
			if parent isa MGroup then
				if parent.mpackage.mgroups.first == parent then parent = parent.mpackage
			end
			draw_edge(mentity, parent)
		end
		for parent in parents do
			if parent isa MModule then
				var mgroup = parent.mgroup
				if mgroup != null and mgroup.default_mmodule == parent then parent = mgroup
			end
			if parent isa MGroup then
				if parent.mpackage.mgroups.first == parent then parent = parent.mpackage
			end
			draw_parents(parent, max_depth, current_depth + 1)
		end
	end

	# Recursively draw children of mentity
	fun draw_children(mentity: MEntity, max_depth: nullable Int, current_depth: nullable Int) do
		if done_children.has(mentity) then return
		done_children.add mentity
		current_depth = current_depth or else 0
		if max_depth != null and current_depth >= max_depth then
			to_dotdotdot(mentity)
			return
		end
		var children = mentity.collect_children(mainmodule, filter)
		if children.length > 10 then
			to_dotdotdot(mentity)
			return
		end
		for child in children do
			if child isa MGroup then
				if child.mpackage.mgroups.first == child then child = child.mpackage
			end
			draw_edge(child, mentity)
		end
		for child in children do
			if child isa MGroup then
				if child.mpackage.mgroups.first == child then child = child.mpackage
			end
			draw_children(child, max_depth, current_depth + 1)
		end
	end

	# Draw a node from a `mentity`
	fun draw_node(mentity: MEntity): DotElement do
		if nodes.has_key(mentity) then return nodes[mentity]
		var node: DotElement = mentity.to_dot_node
		if mentity == center then node = highlight(node)
		nodes[mentity] = node
		graph.add node
		return node
	end

	private var edges = new HashMap2[MEntity, MEntity, DotEdge]

	# Draw a edges between two mentities
	fun draw_edge(from, to: MEntity): DotEdge do
		if edges.has(from, to) then return edges[from, to].as(not null)
		if edges.has(to, from) then return edges[to, from].as(not null)
		var nfrom = draw_node(from)
		var nto = draw_node(to)
		var edge = new DotEdge(nfrom, nto)
		edges[from, to] = edge
		graph.add edge
		return edge
	end

	private var to_dots = new HashMap[MEntity, DotElement]

	# Create a link from `mentity` to a `...` node
	fun to_dotdotdot(mentity: MEntity): DotEdge do
		var nto = draw_node(mentity)
		var dots = to_dots.get_or_null(mentity)
		if dots == null then
			dots = dotdotdot("{nto.id}...")
			to_dots[mentity] = dots
		end
		graph.add dots
		var edge = new DotEdge(dots, nto)
		graph.add edge
		return edge
	end

	private var from_dots = new HashMap[MEntity, DotElement]

	# Create a link from a `...` node to a `mentity`
	fun from_dotdotdot(mentity: MEntity): DotEdge do
		var nfrom = draw_node(mentity)
		var dots = to_dots.get_or_null(mentity)
		if dots == null then
			dots = dotdotdot("...{nfrom.id}")
			from_dots[mentity] = dots
		end
		graph.add dots
		var edge = new DotEdge(dots, nfrom)
		graph.add edge
		return edge
	end

	# Change the border color of the node
	fun highlight(dot: DotElement): DotElement do
		dot["color"] = "#1E9431"
		return dot
	end

	# Generate a `...` node
	fun dotdotdot(id: String): DotNode do
		var node = new DotNode(id)
		node["label"] = "..."
		node["shape"] = "none"
		return node
	end
end

redef class MEntity
	# Return `self` as a DotNode
	fun to_dot_node: DotNode do
		var node = new DotNode(full_name)
		node["label"] = name
		return node
	end
end

redef class MPackage
	redef fun to_dot_node do
		var node = super
		node["shape"] = "tab"
		return node
	end
end

redef class MGroup
	redef fun to_dot_node do
		var node = super
		node["shape"] = "folder"
		return node
	end
end

redef class MModule
	redef fun to_dot_node do
		var node = super
		node["shape"] = "note"
		return node
	end
end

redef class MClass
	redef fun to_dot_node do
		var node = super
		node["shape"] = "box"
		return node
	end
end
src/doc/commands/commands_graph.nit:15,1--399,3