Today I will show how to make use of a very powerful, yet underutilized capability of Sonargraph-Architect. By writing simple Groovy scripts you are able to create your own code checkers or define your own metrics. Many of our most useful scripts are just about 50 lines of code and therefore not a big effort to create. As an example we will develop a script that finds packages (Java) or name spaces (C#, C++) that occur in more than one module.
The scripting engine of Sonargraph is based on our scripting API. Most scripts are based on the visitor pattern. Using this pattern a script can traverse specific elements of Sonargraph’s software system model, which is basically a very big tree data structure. At the root there is the software system node, which is accessible by a globally available instance of class CoreAccess, called “coreAccess”. This specific instance is language agnostic, i.e. it can be used for scripts that support all programming languages supported by Sonargraph. When creating a script you decide wether it will be language specific or language agnostic. Language specific scripts have access to more detailed language specific data and will use different root objects like “javaAccess” or “csharpAccess”.
Depending on the task to be solved the script will use one of the three available main branches of Sonargraph’s model tree. The parser model, which is basically representing the physical aspects of the software system like source files, root directories and programming elements rooted in their physical home, which usually is a source file. The two other branches are based on a logical model. The logical model skips physical aspects and only contains logical elements like namespaces/packages and programming elements like classes. The first variant also still retains modules while the second variant only makes a difference between internal and external elements. Your own code base defines all the “internal” elements, while all elements that referenced by your code based without being defined in your code base are considered external.
Parser Model (physical) versus Logical Model (with Modules)
Since we want to find packages/namespaces spread over more than one module we will use the logical branch with modules. To solve our problem we will create a map that maps namespace/package names to the modules where they occur in. Each namespace that belongs to more than one module after we have visited all namespaces will get a warning marker.
So let’s begin creating a script. Navigate to the “Files” tab (top left section) in Sonargraph-Architect and right click on “Scripts”. A popup menu will appear where you can select “New Script File…”. We give a name to our script and are happy with the fact that only “Core” is selected for languages. Our script will be language agnostic. The editor will show some example code which you can delete, we won’t need it for our purposes.
// Find all packages/namespaces that occur in more than one module
ICoreVisitor v = coreAccess.createVisitor()
def namespaceMap = [:]
def distributedPackages = [:]
LogicalNamespaceAccess ns ->
// A 'part' namespace does not contain classes
def name = ns.name
def entry = namespaceMap[name]
if (entry == null)
// seeing this namespace name for the first time
entry = [ ns ]
namespaceMap[name] = entry
// append namespace to list
entry << ns
// use map as a set
distributedPackages[name] = 1
// very important, since namespaces can be nested
In line 2 we create the all important visitor object by utilizing the global variable “coreAccess” which represents our software system. From line 7 on we define a closure that is executed for each instance of “LogicalModuleNamespace” in our software system model. The visitited instance is passed as a parameter named “ns” (line 9). Before defining the closure we declare to global maps, “namespaceMap” and “distributesPackages”. The first map will map namespace names to a list of modules, while the second one is just use as as set to remember the names of all distributes namespaces.
In line 17 we are testing for “part” namespaces. Those are namespaces that do not contain any classes or other programming elements, but only nested namespaces. We skip those because they would just lead to irrelevant false positives. The remaining code should be pretty self explanatory. Just notice the call to “visitChildren” in line 32. This is very important, otherwise we would only visit top-level namespaces and never reach nested namespaces.
// skip externals
// Traverse the model and build a data model to be used below
This next section adds another closure that allows us to skip all external elements by not calling “visitChildren” inside of the closure. Line 41 starts the tree visitation process. After this call returns our two global maps will be filled with the needed data.
// Now do the work and create issues and a result tree
// 'it' represents the name of a distributed package
distributedNamespaces = namespaceMap[it]
// distributedNamespaces is a list of LogicalNamespaceAccessobjects
moduleNames = distributedNamespaces.collect
msg = "Namespace occurs in more than one module: $moduleNames"
// Under each name node add the modules where this package name occurs
node = result.addNode(it)
// Add an issue to the logical namespace
result.addWarningIssue(it, "Distributed Package", msg)
// Add the module as a child node in the result tree
The purpose of this loop is to make use of our results. For the script view we will create a result tree that will associate distributed package names with all the modules they occur in. Moreover we will add issues to the distributed logical namespace objects.
Screenshot of the Sonargraph-Architect script result view
At the end we will also add a metric to Sonargraph that counts the number of distributes package names in a software system. The “good” range will be zero to zero, we consider distributed packages as a bad thing.
// Now define a metric where every value above zero will create a warning
def metricId = coreAccess.getOrCreateMetricId("DistributedPackageNames", "Number of distributed package names", "Number of package names that occur in more than one module", false, 0, 0)
// Add the metric value to the result
result.addMetricValue(metricId, coreAccess, distributedPackages.size())
A good practice to develop scripts is to use “println” statements in your code. Those will go to the console view when you run the script. When everything works as expected you can remove the “println” statements.The editor also supports code completion by pressing Ctrl-Space.
After our script is polished we can add it to the automated script runner of Sonargraph-Architect, i.e. the script will run automatically after each refresh and will contribute issues to the Sonargraph issues view and metrics to the metrics view. The script runner configuration can be accessed via the “System/Configure…” menu. Once it is added to the script runner the script will also run during automated builds with Sonargraph-Build or in our plugins for Eclipse and IntelliJ.
Screenshot of Sonargraph-Architect showing the script results
Now we have come to the end of our litte example. Please leave feedback or questions in the comment section below. If you want to try our scripting engine yourself you can obtain a free evaluation license from our website and try it on your own project. The script above is part of our core quality model and can be used out of the box with Sonargraph-Architect.