Skip to content
SciTools Blog
Menu
  • Home
  • Blog
  • Support
  • Contact
  • Login
  • Pricing
  • Free Trial
Menu
robot playing piano

Automating A Custom Architecture

Posted on May 16, 2022

Abstract: Build a custom architecture using the Perl or Python API to virtually reorganize your code however you wish.

“Hey Natasha, you should write an article about architectures.” It’s an important feature. I have used architectures. BUT, there is no way I’d take the time to build an architecture by hand using the Architecture Designer. I’m much too lazy. Although, making the Architecture Graph edit architectures by drag and drop was fun:

Using the Architecture Designer (or right click, Graph->Graph Architecture) you can edit an architecture by dragging entities.

Still, for anything bigger than Fastgrep, I wouldn’t use it. After all, isn’t the point of being a programmer to automate these things? That’s why my biggest architecture usage, described in this article, used a script.

However, that script isn’t an example of best practice. It only works for files and it exports an old format no longer used. In order to write this article, we needed a better way to make architectures using the Perl and Python APIs. 

Now, just like you can add an annotation to an entity with ent.annotate(), you can add an entity to an architecture with ent.add_to_arch(). The function can take a string or an understand.Arch object. If you use a string, then then the architecture doesn’t have to exist. Here’s an example in Perl:

use Understand;
$db = Understand::open("fastgrep.und");

foreach $file ($db->lookup("regsub.c","file")) {
  $file->add_to_arch("perl/test"); # perl/test doesn't exist yet
}

$arch = $db->lookup_arch("perl/test"); # $file->add_to_arch created it, so now it can be looked up
print $arch->name() . "\n";

foreach $file ($db->lookup("regexp.c","file")) {
  $file->add_to_arch($arch); # and you can add to the arch object directly instead of by a longname string
}

Now that I have this cool functionality, how can I use it? Well, I’ve been working with graphs a lot lately, and it would be useful to know what the maximum call level for a particular function would be. For example, I might want a really small graph as a test case for a Visio export. I want a deep graph for testing the level option. So, I’ll create an architecture with functions that groups the functions by calls depth. 

import understand
import sys

def depth(ent):
  level = -1
  cur = [ent]
  next = []
  visited = set()
  while len(cur):
    level += 1
    for curEnt in cur:
      if curEnt in visited:
        continue
      visited.add(curEnt)
      for ref in curEnt.refs("call"):
        next.append(ref.ent())
    cur = next
    next = []
  return level

db = understand.open(sys.argv[1]);
cache = dict()
for ent in db.ents("function"):
  ent.add_to_arch("calllevels/"+str(depth(ent)))

The script is obviously lacking in optimization and it’s probably a bad idea to run it on any large projects, but it worked great for Fastgrep. At the very least, I can learn if there is anything with a call depth greater than 6 (which is the call depth of chimaera, the go-to function for testing). The generated architecture shows several functions with a greater depth:

The CallLevels architecture, created using the script above for Fastgrep.

With this capability, you can quickly automate your own architecture creation however you wish!

  • Instagram
  • Facebook
  • LinkedIn
  • Twitter
  • YouTube

Learn more about Understand's Features

  • Dependency Graph
    View Dependency Graphs
  • Comply with Standards
  • View Useful Metrics
  • Team Annotations
  • Browse Depenedencies
  • Edit and Refactor

Related

  • API
  • Architectures
  • Business
  • Code Comparison
  • Code Comprehension
  • Code Navigation
  • Code Visualization
  • Coding Standards
  • Dependencies
  • Developer Notes
  • DevOps
  • Getting Started
  • Legacy Code
  • Licensing
  • Metrics
  • Platform
  • Plugins
  • Power User Tips
  • Programming Practices
  • Uncategorized
  • Useful Scripts
  • User Stories
  • May 2025
  • January 2025
  • December 2024
  • November 2024
  • August 2024
  • June 2024
  • May 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • April 2023
  • January 2023
  • December 2022
  • November 2022
  • September 2022
  • August 2022
  • May 2022
  • April 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

©2025 SciTools Blog | Design: Newspaperly WordPress Theme