Message Boards Message Boards

Call graph generation for context functions

Posted 6 years ago

In brief

This document describes the package CallGraph.m for making call graphs between the functions that belong to specified contexts.

The main function is CallGraph that gives a graph with vertices that are functions names and edges that show which functions call which other functions. With the default option values the graph vertices are labeled and have tooltips with function usage messages.

General design

The call graphs produced by the main package function CallGraph are assumed to be used for studying or refactoring of large code bases written with Mathematica / Wolfram Language.

The argument of CallGraph is a context string or a list of context strings.

With the default values of its options CallGraph produces a graph with labeled nodes and the labels have tooltips that show the usage messages of the functions from the specified contexts. It is assumed that this would be the most useful call graph type for studying the codes of different sets of packages.

We can make simple, non-label, non-tooltip call graph using CallGraph[ ... , "UsageTooltips" -> False ].

The simple call graph can be modified with the functions:

CallGraphAddUsageMessages, CallGraphAddPrintDefinitionsButtons, CallGraphBiColorCircularEmbedding

Each of those functions is decorating the simple graph in a particular way.

Package load

This loads the package CallGraph.m :

Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/Misc/CallGraph.m"]

The following packages are used in the examples below.

Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/MonadicProgramming/MonadicQuantileRegression.m"]

Get["https://raw.githubusercontent.com/szhorvat/IGraphM/master/IGInstaller.m"];
Needs["IGraphM`"]

Usage examples

Generate a call graph with usage tooltips

CallGraph["IGraphM`", GraphLayout -> "SpringElectricalEmbedding", ImageSize -> Large]

IGraphM-call-graph-with-usage-tooltips

Generate a call graph by excluding symbols

gr = CallGraph["IGraphM`", Exclusions -> Map[ToExpression, Names["IG*Q"]], ImageSize -> 900]

IGraphM-call-graph-with-exclusions

Generate call graph with buttons to print definitions

gr0 = CallGraph["IGraphM`", "UsageTooltips" -> False];
gr1 = CallGraphAddPrintDefinitionsButtons[gr0, GraphLayout -> "StarEmbedding", ImageSize -> 900]

IGraphM-call-graph-with-definition-buttons

Generate circular embedding graph color

cols = RandomSample[ ColorData["Rainbow"] /@ Rescale[Range[VertexCount[gr1]]]];

CallGraphBiColorCircularEmbedding[ gr1, "VertexColors" -> cols, ImageSize -> 900 ]

IGraphM-call-graph-bicolor-Bezie

(The core functions used for the implementation of CallGraphBiColorCircularEmbedding were taken from kglr's Mathematica Stack Exchange answer: https://mathematica.stackexchange.com/a/188390/34008 . Those functions were modified to take additional arguments.)

Options

The package functions "CallGraph*" take all of the options of the function Graph. Below are described the additional options of CallGraph.

  • "PrivateContexts"
    Should the functions of the private contexts be included in the call graph.

  • "SelfReferencing"
    Should the self referencing edges be excluded or not.

  • "AtomicSymbols"
    Should atomic symbols be included in the call graph.

  • Exclusions
    Symbols to be excluded from the call graph.

  • "UsageTooltips"
    Should vertex labels with the usage tooltips be added.

  • "UsageTooltipsStyle"
    The style of the usage tooltips.

Possible issues

  • With large context (e.g. "System`") the call graph generation might take long time. (See the TODOs below.)

  • With "PrivateContexts"->False the call graph will be empty if the public functions do not depend on each other.

  • For certain packages the scanning of the down values would produce (multiple) error messages or warnings.

Future plans

The following is my TODO list for this project.

  1. Special handling for the "System`" context.

  2. Use the symbols up-values to make the call graph.

  3. Consider/implement call graph making with specified patterns and list of symbols.

    • Instead of just using contexts and exclusions. (The current approach/implementation.)
  4. Provide special functions for "call sequence" tracing for a specified symbol.

POSTED BY: Anton Antonov
4 Replies

I made a corresponding paclet, "AntonAntonov/CallGraph", (this post is a tech note in it):

enter image description here

POSTED BY: Anton Antonov

enter image description here - Congratulations! This post is now a Staff Pick as distinguished by a badge on your profile! Thank you, keep it coming, and consider contributing your work to the The Notebook Archive!

POSTED BY: EDITORIAL BOARD

Thanks, Szabolcs! I was planning to see are there other implementations of the CallGraph.m functionality. (Also, I was looking for your email in order to send you the link to this post because I used IGraphM.m ...)

POSTED BY: Anton Antonov

I was just about to try it on IGraph/M. Then I saw that you already did :-)

Very cool package. You might want to post an answer here: https://mathematica.stackexchange.com/questions/4343/automatically-generating-a-dependency-graph-of-an-arbitrary-mathematica-function

POSTED BY: Szabolcs Horvát
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract