7.7. Writing your own analysis rules¶
The analysis framework shipped with the Axivion Suite allows you to write your own analysis rules. These additional rules can then be used as if they were part of the Axivion Suite: Their options can be edited in the graphical configuration interface, and if enabled, they are executed as part of an analysis run.
Alternatively, you can run an external tool and capture its output.
To get code completion and type checking for the analysis framework in your IDE, see Section Code Completion and Type Checking in an IDE.
7.7.1. Adding a Custom Rule¶
You can load additional rule sets by adding directories to the project configuration
via the graphical interface, the pseudo option _additional_rules in a
JSON configuration file, or the
add_additional_rules_directory() function in a Python configuration
file. This section briefly discusses the file structure of
additional rule sets.
A rule set consists of one or more Python files in a rule set directory:
ruleset-root/
rule1.py
rule2.py
In this case, the path to ruleset-root can be imported directly.
Ensure that all Python modules in that directory have unique names. This means that,
in the example above, neither rule1 nor rule2 may already exist in the
Python code supplied in Axivion Suite or in the Python environment.
To avoid naming conflicts between individual modules, it is recommended to group the entire rule set into a single Python package.
ruleset-root/
ruleset-package/
__init__.py
rule1.py
rule2.py
In this structure, only ruleset-package needs to have a unique
name.
You can import either the path to ruleset-root or the path
to
ruleset-root/ruleset-package directly.
The following example shows a custom rule in rule1.py.
# Axivion Suite
# Copyright (C) 2021-2025 Axivion GmbH
# Copyright (C) 2025-2026 The Qt Company GmbH, a subsidiary of The Qt Group
# https://www.qt.io
from axivion import analysis
from bauhaus import ir
@analysis.rule('MyOwnRules-MacroCheck')
class MyMacroCheckRule(analysis.AnalysisRule):
title = '''This is the first example rule.'''
_message_descriptions = {
'bad_macro_key': 'This kind of macro is forbidden',
}
"""The messages possibly emitted by this rule."""
report_concat: bool = False
"""Description of this option."""
def get_rulehtml_description(
self, config: analysis.RuleConfiguration | None = None
):
# Fill in your HTML description of the rule here (appears in the WebUI,
# the IDE plugins and in the configuration GUI).
return """This is an example of a custom rule.
"""
def execute(self, ir_graph: ir.Graph):
# This is the main entry point called from the framework - write your check here
for node in ir_graph.nodes_of_type(
ir.Physical.Source_Function_Macro_Definition
):
if self.is_interesting_macro(node):
# A stylecheck finding is reported with add_message
self.add_message(node, 'bad_macro_key', entity=node.Name)
def is_interesting_macro(self, node: ir.Physical.Source_Function_Macro_Definition):
# You can define your own Python methods to structure the code of your check
if node.Name.startswith('HAVE'):
return True
return bool(self.report_concat and '##' in node.Replacement)
Important notes:
The class name does not matter. The relevant name is the one passed to the
@analysis.ruledecorator. This name appears in the graphical configuration interface and is used to refer to the rule in a Python configuration file. For instance, you can configure the new example rule in a Python configuration file as follows:analysis = axivion.config.get_analysis() analysis['MyOwnRules-MacroCheck'].report_concat = True
See Section Python API for a description of the configuration objects.
Your rule class should be derived from
axivion.analysis.AnalysisRule, either directly or indirectly.The rule accesses the IR through the module
bauhaus.ir, described in Section IR Scripting.Class variables are treated as rule options (
report_concatin the example rule).Each option needs a default value. Its type comes from the type hint, or from the default value if there is no hint. A default value that does not match the type hint causes a run-time error when the rule is scheduled.
Follow these steps to add this rule to the set of available rules in your project:
Create a subdirectory for your custom rules (e.g.,
ruleset-packagein the example above).Store the file
rule1.pyin that directory.Also create an empty
__init__.pyfile in that directory.Open the graphical configuration interface and select the
Analysisitem. Right-click to open the context menu and select the menu itemAdditional rules.... In the directory chooser, select your rule directory (e.g.,ruleset-package).The rule
MyOwnRules-MacroCheckshould now be visible in theMyOwnRulesrule group under theAnalysisitem.By default, the rule is added to a rule group named after the part of the rule name preceding the last hyphen (in this case,
MyOwnRules). Rules without a hyphen in their name are added as immediate children of the top-level aspect node to which the directory is added (in this case,Analysis). To add your rule to a different rule group in the hierarchy (e.g., Stylechecks), pass the name of the parent rule group to the@analysis.ruledecorator:@analysis.rule('MyOwnRules-MacroCheck', rulegroup='Stylechecks')
If the named group does not exist, it is created automatically.
Rule names must be unique across the entire aspect (top-level node) – for example, the entire
Analysishierarchy. Even if rules are added to different rule groups, they must not share the same name.
7.7.2. Analysis framework API¶
The analysis framework API is defined by the following Python modules:
Base classes and types for writing analysis rules. |
|
Post-processing of the issues reported by rules. |
|
Access to the configuration from Python configuration files. |
|
Helpers for configuring the steps of the architecture analysis. |
|
Sets of Rhapsody API calls for configuring the Rhapsody import. |
|
Enums for code annotation formats in Python configuration files. |
|
Predicates for constructors, assignment operators and other special routines. |
|
Functions for stripping casts and implicit conversions from expressions. |
|
Predicates for const and volatile qualified types. |
|
Types shared by the Python APIs of the Axivion Suite. |
|
Base classes for metric rules. |