7.4.3. Class View

A view is a subgraph of an RFG. You can load and save views as GXL files, query the attributes of the view, check whether nodes and edges are contained in a view, and add and remove nodes and edges. You can compute the union, difference, and intersection of views. You can lift edges in a hierarchical view. The view itself grants access to the neighborhood of nodes and edges; i. e., you can query nodes and edges based on the view. You can get a set of all nodes and edges contained in the view, or iterate through them using xnodes and xedges. Iterating the view itself, e. g., using a for loop, yields the set of attributes of the view; see Section Value Handling for Attributes.

code_facts_view_name()
declaration_facts_view_name()
include_view_name()
environment_view_name()
module_view_name()
lifted_module_view_name()
file_view_name()
lifted_file_view_name()
call_view_name()
user_view_name()
entries_view_name()
alive_view_name()
dead_view_name()
assembly_view_name()
mapping_view_name()
architecture_view_name()
reflexion_view_name()
causing_view_name()
known_problems_view_name()
clones_view_name()
cycles_view_name()
dominance_tree_view_name()

Some view names imply a special meaning. The name Code Facts, for example, is used for views that contain the elements that model the source code of the system under analysis.

Caution

These routines return the fixed default view names used for the given purposes, independent of the loaded RFG. The view names created by a specific analysis setup may differ.

Note

Gravis consults the view attribute Gravis.ROLE to find the default input views for architecture analysis. For instance, the following code finds the view with the Architecture role if one exists; otherwise, the result is None:

architecture_view = \
    next((v for v in graph.xviews() \
            if 'Gravis.ROLE' in v \
                and v['Gravis.ROLE'] == 'ARCHITECTURE_ROLE'), \
         None)

Apart from 'ARCHITECTURE_ROLE', other useful roles are 'BASE_ROLE' (identifying the Base view), 'HIERARCHY_ROLE' (Hierarchy view), and 'MAPPING_ROLE' (Mapping view). See Architecture Analysis Facilities for an explanation of these roles.

is_valid_view_name(name)
uniquify_view_name(graph, name)
parenthesize(name)

The function is_valid_view_name checks whether name can be used as a view name. A valid view name is not empty and contains none of the characters "'$%&?*#\/.:,;`@<>|. The function uniquify_view_name returns name if no view of that name exists in graph; otherwise, it returns name with a counter attached. The function parenthesize puts parentheses around name if it contains a space.

7.4.3.1. Obtaining a View

class View(graph, viewname[, filename[, format[, link_edges[, import_attributes[, merge_attributes]]]]])

The constructor of class View, when used with a graph object graph and a view name view_name as arguments, functions as follows: If no view named view_name exists in graph, it creates and returns a new view named view_name in graph. Otherwise, it returns the already existing view named view_name in graph.

The constructor can also be used to import a view from an external file into graph. To do this, you must supply at least the third argument filename, which denotes the location of the file to be imported. In this case, the parameter view_name specifies the name of the view into which the content of the file should be imported: if no view named view_name exists in the graph, a view named view_name is created, the file content is imported into this view, and the view is then returned. If a view named view_name already exists in the graph, a new view name is constructed and the file content is imported into this new view, which is then returned. In this case, view_name is not the name of the returned view.

  • The parameter format (default: 'GXL') specifies the format of the imported file. Currently, only the GXL format is supported, so the format specification (if given) must always be 'GXL'.

  • The parameter link_edges (default: True) specifies whether edges of the same type and the same source/target nodes should be linked together during import. If this option is set to False, edges are not linked; i. e., multiple edges of the same type in the file will result in multiple edges in the graph.

  • The parameter import_attributes (default: True) specifies whether attributes of nodes and edges should be imported.

  • The parameter merge_attributes (default: True) specifies whether imported attributes should be merged (i.e., existing values are preferred). If this option is set to False, they are overwritten (i.e., existing values are replaced by imported values).

7.4.3.2. Storing a View

save(filename[, format[, attributes]])

The instance method save saves the view to a file named filename. If the file exists, it is overwritten silently. If the location is not writable or is otherwise invalid, an exception is raised describing why the operation failed. The format specification must be “GXL”, which is also the default.

The optional parameter attributes controls whether attribute values should be stored. True (default) stores all attribute values; False prevents most attribute values from being stored. Only Linkage.Name, Source.Name, and attributes starting with Architecture. are stored in all cases.

The example in Listing Saving and loading a view stores the Code Facts view of an RFG as a GXL file and immediately loads the file, creating a new view named Copy of followed by the name of the Code Facts view.

Saving and loading a view
from bauhaus import rfg
my_graph = rfg.Graph(filename)
my_view = my_graph.view(rfg.code_facts_view_name())
my_view.save(filename+"-CODEFACTS.gxl", "GXL")
loaded_view = rfg.View(
    my_graph,
    "Copy of "+rfg.code_facts_view_name(),
    filename+"-CODEFACTS.gxl",
    "GXL")

7.4.3.3. Destroying a View

A view in an RFG graph is not subject to garbage collection on its own. You must take active measures to remove it.

destroy()

The method destroy removes the view from the graph.

Caution

Unlike removing every element (node, edge) from a view, completely destroying the view does not necessarily destroy all elements that are not contained in other views of the graph. Nonetheless, you must not use any node or edge obtained from a destroyed view.

7.4.3.4. General Operations

graph()

The instance method graph returns the graph to which the view belongs.

__str__()

The instance method __str__ returns a textual representation of the view (for debugging purposes only). For user output, use name and the attribute handling methods.

__copy__()
__deepcopy__()

Shallow copies are not permitted; therefore, __copy__ raises a TypeError exception. The instance method __deepcopy__ creates a deep copy of the view; see copy in Section View Operations.

__eq__(other)
__ne__(other)

The instance methods __eq__ and __ne__ check for equality and inequality, respectively, of two views. A view can also be compared with a string, in which case the name of the view is compared with the string.

Ordinarily, both functions are called via the == and != operators.

name()
rename(new_name, force=False)

The instance method name returns the name of the view as a string. The view name is not an ordinary attribute.

The instance method rename assigns a new name to the view. The new name must not be used by another view in the graph unless the force parameter evaluates to True, in which case the other view with that name is deleted before the current view is renamed.

7.4.3.5. Value Handling for Attributes

For a full explanation of attribute handling, see Section Attributes.

has_key(key)
__getitem__(key)
__setitem__(key, value)
__delitem__(key)
__iter__()
keys()
items()
get_string_list(key)
set_string_list(key, value)
append_to_string_list(key, value)

These instance methods query and manipulate the attributes of a view. has_key checks whether the attribute key is set; __getitem__ retrieves the stored value for attribute key; and __setitem__ sets a new value for the attribute. __iter__ iterates through the set attributes as name-value pairs. keys returns a list of the names of all set attributes. items returns a list of all set key-value pairs (where keys are strings). get_string_list interprets the string attribute as a list and returns that list. set_string_list stores a string representation of the given string list. append_to_string_list appends the given string to the string list stored in the attribute.

7.4.3.6. Membership in the View

Nodes and edges can be contained in views. The following methods manage and query membership.

__contains__(element)
insert(element[, source, target])
add(element[, source, target])
insert_uniquely(element, source, target)
add_uniquely(element, source, target)
remove(element)

The instance method __contains__ checks whether the element passed as a parameter (a node or an edge) is contained in the view. If a ViewAttribute or a string is passed as a parameter, __contains__ checks if the attribute has a value. __contains__ can also be used with the in operator.

Caution

__contains__ simply returns False instead of raising an exception if an attribute specified by name is not a valid view attribute.

The instance methods insert and add, when called with one parameter, insert the supplied element or set of elements into the view. You can call insert and add using a node, an edge, a set of nodes, or a set of edges.

Note

Inserting an edge into a view also inserts the source and target nodes of the edge if they are not already contained in the view.

The three-parameter versions of insert and add can only be used for single edges that have not yet been inserted into any view. A binding between the edge and its target and source nodes is established, and the edge is inserted into the view.

Note

The additional parameters source and target must be supplied as named parameters.

The instance methods insert_uniquely and add_uniquely check if an edge of the requested type already exists between the two nodes, source and target:

  • If not, these methods behave like insert.

  • If an edge of the same type as parameter element already exists, the parameter edge is implicitly freed and must not be used further; the existing edge is returned instead.

See Listing Using the result of insert_uniquely for an example.

Using the result of insert_uniquely
...
   new_edge = some_graph.edge('Call')
   new_edge = some_view.insert_uniquely(new_edge, source=some_node, target=other_node)
   new_edge['Some.Attribute'] = 42
...

Caution

When creating a new edge, you must insert it using the three-parameter version for the first time to establish the target and source nodes of the edge.

The instance method remove removes the element or set of elements from the view. If the parameter is a node or a set of nodes, any attached edges are also implicitly removed.

Caution

If an element is removed from the last view containing it, the element is automatically garbage collected immediately. Therefore, if you intend to transfer an element between two views, ensure you add the element to the target view before removing it from the source view.

Caution

Hierarchies are expressed by edges. These edges must form a tree for the hierarchy to work properly with many analyses within the Axivion Suite. Ensure that your view manipulations do not create non-tree structures if you plan to use a view as a hierarchy.

nodes([node_matcher])
edges([edge_matcher])

These methods return all nodes and edges, respectively, contained in the view. If a matcher is supplied, only those nodes or edges matched by the matcher function are included in the sets.

Note

Since nodes and edges return full sets of elements, it is recommended to use xnodes and xedges for iteration to optimize memory management.

7.4.3.7. Iterating the Elements Contained in a View

xnodes([node_matcher])
xedges([edge_matcher])

The instance method xnodes returns a generator for all nodes contained in the view. The instance method xedges returns a generator for all edges contained in the view.

An example of how to use these functions is shown in Listing Iterate the nodes in the Code Facts view.

Iterate the nodes in the Code Facts view
from bauhaus import rfg
my_graph = rfg.Graph(filename)
my_view = my_graph.view(rfg.code_facts_view_name())
for n in my_view.xnodes():
    ...  # perform your actions on the nodes

Note

This iteration scheme is more efficient than obtaining a set of all nodes or edges in the view.

7.4.3.8. View Operations

Views can be intersected, united, differenced, and copied. Edges of a view can be subtracted from a second view. Edges can be hierarchically lifted to get a view in which transitive dependencies are interactively browsable. Additionally, analyses for cycles, dead code, and architecture violations (reflection analysis) can be applied. Edge maps can be replaced in a view to obtain a view that is browsable by its architecture.

Unless stated otherwise, the result of the following instance methods is the resulting view. In the tables of default result names, parenthesize(...) refers to the parenthesize function.

The example code in Listing View operations example illustrates the usage of the view operation instance methods:

View operations example
#!/usr/bin/env rfgscript
# Copyright (C) 2025 Axivion GmbH
# Copyright (C) 2025-2026 The Qt Company GmbH, a subsidiary of The Qt Group
# https://www.qt.io


import sys

from bauhaus import rfg

if len(sys.argv) < 2:
    print("usage: %s <rfgfile>" % (sys.argv[0]))
    sys.exit(1)

g = rfg.Graph(sys.argv[1])

base_view = g.view(rfg.code_facts_view_name())
env_view = g.view(rfg.environment_view_name())
call_view = g.view(rfg.call_view_name())

base_view.cycles_view()
base_without_env = base_view.difference(env_view)

# we ignore the result of these operations
base_without_env.union(call_view)
# we rename the result view of the copy operation
call_view.copy().rename("This is a copy of the Call view")
call_view.intersection(env_view)

module_view = g.view(rfg.module_view_name())
module_view.lift_edges_totally(base_view)

g.save(sys.argv[1] + ".new.rfg")

Intersection, Union, Difference

intersection(other_view[, result_name])
union(other_view[, result_name])
difference(other_view[, result_name])

These instance methods take the view instance on which they are called and perform the operations indicated by their names with respect to the parameter other_view.

If the result_name parameter is passed, it is disambiguated and used as the name of the result view. If the result_name parameter is not passed, the result view is named according to the following table. If the name is already in use, a counter is attached to disambiguate the name.

operation

name of result

intersection

parenthesize(self.name()) + “ and “ + parenthesize(other_view.name())

union

parenthesize(self.name()) + “ or “ + parenthesize(other_view.name())

difference

parenthesize(self.name()) + “ minus “ + parenthesize(other_view.name())

Caution

Hierarchies are expressed by edges. Edges must form a tree for the hierarchy to work properly with many analyses within the Axivion Suite. Therefore, ensure that you do not create non-tree structures as a result of your view manipulations if you plan to use a view as a hierarchy.

The resulting view is returned.

Copy

copy([result_name])

The copy instance method creates a copy of the view.

If the result_name parameter is passed, it is disambiguated and used as the name of the result view. If the result_name parameter is not passed, the result view is named according to the following table. If the name is already in use, a counter is attached to disambiguate the name.

operation

name of result

copy

self.name()

The resulting view is returned.

Subtract Edges

subtract_edges(to_be_subtracted[, result_name])

The subtract_edges instance method creates a new view that contains the nodes from the current view and all edges that are not contained in the argument view to_be_subtracted.

If the result_name parameter is passed, it is disambiguated and used as the name of the result view. If the result_name parameter is not passed, the result view is named according to the following table.

operation

name of result

subtract_edges

self.name()+” minus edges from “+to_be_subtracted.name()

The resulting view is returned.

Lift Edges

lift_edges_totally(other_view[, result_name])

The lift_edges_totally instance method treats the view as a hierarchy and uses other_view as the source of edges to be lifted within that hierarchy.

If the result_name parameter is passed, it is disambiguated and used as the name of the result view. If the result_name parameter is not passed, the result view is named according to the following table. If the name is already in use, a counter is attached to disambiguate the name.

operation

name of result

lift_edges_totally

“Lifted totally in “+self.name() +” edges from “+other_view.name()

The resulting view is returned.

Cycles

cycles_view([result_name[, include_selfloops]])
cycles([include_selfloops[, ignored_edge_types]])
num_sccs([include_selfloops[, ignored_edge_types[, exclude_fully_artificial_cycles[, approximated_edge_types]]]])

The cycles_view instance method creates a new view containing the cycles of the nodes and edges within the current view. Since cycles may overlap, the result is technically a view of the strongly connected components of the original view. Edges from a node to itself are only considered if include_selfloops is True (default: False). Edges of type Belongs_To and its subtypes are ignored.

If the result_name parameter is passed, it is disambiguated and used as the name of the result view. If the result_name parameter is not passed, the result view is named according to the following table.

operation

name of result

cycles_view

“cycles in “+self.name()

The resulting view is returned.

The cycles instance method computes the same strongly connected components without modifying the RFG. It returns a list containing one EdgeSet per strongly connected component. Edges whose type name is included in the set ignored_edge_types are ignored.

The num_sccs instance method returns a pair consisting of the number of strongly connected components and the number of those that contain an edge whose type name is included in the set approximated_edge_types. If exclude_fully_artificial_cycles is True, components in which all edges start at nodes marked Element.Is_Artificial are not counted.

cycles_view and cycles require the Cycles license feature; num_sccs requires the Metrics license feature. Otherwise, they throw InvalidLicense.

Note

To compute cycles at the module level, first lift the dependencies from the base level to the modules in an intermediate view, remove all base nodes from that intermediate view, and then compute cycles on the resulting intermediate view.

Dead Code

dead_code_view(entries_view[, alive_result_name[, dead_result_name[, merge_copies]]])
dead_code(entries_view[, merge_copies])

The dead_code_view analysis creates two views containing the live and dead routines of a system. The base view is the instance on which dead_code_view was called. The entry points into the live set are the nodes contained in the entries_view. If merge_copies is True (default: False), all copies of a routine are live if at least one copy is live.

If values for the alive_result_name and dead_result_name arguments are not supplied, the result views are named according to the following table (these names are disambiguated to ensure no existing views are overwritten):

result name argument

default name of result

alive_result_name

“alive”

dead_result_name

“dead”

The result is a dictionary of views containing the keys “alive_view” and “dead_view”.

The dead_code instance method performs the same analysis without modifying the RFG and returns the dead nodes as a NodeSet.

Both methods require the Deadcode license feature. Otherwise, they throw InvalidLicense.

Reflexion: Architecture Analysis

reflexion(base_view, hierarchy_view, mapping_view
[, result_name
[, causing_elements_view_name
[, allow_dependencies_to_parents
[, root_edge_type_name
[, browsable_mapping_type
[, check_declaration_forwarding
[, declare_edge_type
]]]]]]])

The reflexion instance method checks the dependencies of the source entities against the architecture of the view on which it is called. base_view contains the source dependencies to be checked. hierarchy_view contains the Belongs_To hierarchy of the source entities; source entities without a mapping of their own inherit the mapping target of their closest mapped ancestor. mapping_view contains the Maps_To edges from source entities to architecture entities. If allow_dependencies_to_parents is True (default: False), a dependency whose target is mapped to an ancestor of the mapping target of its source is not reported as a divergence. The method requires the license feature Architecture. Without it, it throws InvalidLicense.

If the result_name parameter is passed, it is disambiguated and used as the name of the result view. If the result_name parameter is not passed, the result view is named according to the following table.

operation

name of result

reflexion

“Reflexion of “+base_view.name()+” “+hierarchy_view.name()+” architecture “+self.name()+” mapping “+mapping_view.name()+” ignored “

A causing elements view is always created. It is named causing_elements_view_name (disambiguated) or, if that parameter is not passed, “causing_elements_view”.

The root_edge_type_name parameter specifies the root type of the edge type hierarchy used for the architecture check. The default is Source_Dependency.

The result of the reflexion instance method is a dictionary containing the result view (as the value for the key “result_view”) and the causing elements view (as the value for the key “causing_elements_view”).

The browsable_mapping_type parameter indicates the kind of browsable mapping to be generated. It should be 'None' (or the None value) for no browsable mapping at all, 'Minimal' (only endpoints of issues are included), or 'Full' (all mapped nodes are included). If the check_declaration_forwarding parameter is True (the default), mapping targets for declarations are computed by considering the definition of the declaration and a subset of its other declarations (see Architecture Analysis). The edge type describing the relation between declarations and their definitions is specified by declare_edge_type (the default is 'Declare').

reflexion_analysis_result(
[include_divergences
[, include_absences
[, include_convergences
]]])

The reflexion_analysis_result function returns the result of the most recent call to reflexion within the same process. If there is no such call, it raises NoReflexionResultIterator. The parameters control which of the three reflexion results are included in the report. By default, only divergences and absences are included.

The return value is a list of pairs of edges (reflexion_edge, causing_edge).

  • If reflexion_edge is an absence edge, causing_edge is an edge from the architectural model that does not cover any edge from the source model.

  • If reflexion_edge is a divergence edge, causing_edge is an edge from the source model that is not covered by any edge from the architectural model.

  • If reflexion_edge is a convergence edge, causing_edge (more accurately: “conforming edge”) is an edge from the source model that is covered by the convergence edge in the result view (which is a duplicate of an existing source dependency in the architectural model).

Replace Maps_To Edges

replace_maps_to_edges(hierarchy_view, mapping_view[, result_name])

If the result_name parameter is passed, it is disambiguated and used as the name of the result view. If the result_name parameter is not passed, the result view is named according to the following table.

operation

name of result

replace_maps_to_edges

“Maps_To replaced for mapping “+ mapping_view.name()+ “ in architecture “+ self.name()+ “ with respect to hierarchy “+ hierarchy_view.name()

The replace_maps_to_edges instance method unites the view it operates on (treated as the architectural view), the mapping view, and the hierarchy view, and replaces all maps to edges in the union with hierarchical edges. Edges from the hierarchy view that interfere with the hierarchy (because the mapped entity is itself contained in the hierarchy) are removed from the result view. The result view contains a browsable architecture with attached concrete entities. Use lift_edges_totally to obtain a view that contains transitive relationships.

The resulting view is returned.

7.4.3.9. Querying the Neighborhood of Nodes and Edges

Instance methods that take an optional matcher argument use that argument as a matcher function to filter the edges considered when computing the resulting sets of nodes. Only edges that match the matcher are taken into account.

If a single element (i.e., a single node or edge) is provided as the first parameter, it is treated as if it were the only element in a set passed as the parameter. Exceptions include connectings, which only accepts a node set, and sources and targets, which only accept an edge set.

incomings(element[, edge_matcher])
outgoings(element[, edge_matcher])
attached(element[, edge_matcher])
connectings(element[, edge_matcher])

The incomings instance method returns the set of all edges whose target is contained in the node set.

The outgoings instance method returns the set of all edges whose source is contained in the node set.

If a node or node set is passed to the attached instance method, it returns the set of all edges whose sources or targets are contained in the node set. If an edge or edge set is passed to the attached instance method, it returns the set of all source and target nodes of the edge or edges, respectively. In this case, the matcher filters the resulting nodes instead of the edges.

The connectings instance method returns the set of all edges whose source and target are both contained in the node set.

successors(element[, edge_matcher])
predecessors(element[, edge_matcher])
neighbors(element[, edge_matcher])

The predecessors instance method returns the set of all nodes connected to the argument node set by edges that have a target in that node set.

The successors instance method returns the set of all nodes connected to the argument node set by edges that have a source in that node set.

The neighbors instance method returns the set of all nodes connected to the argument node set by edges that have a source or target in that node set.

Note

The edges forming a hierarchy are directed from the part to the container. Therefore, the direct children of a node X can be obtained by applying predecessors to X.

sources(edges[, node_matcher])
targets(edges[, node_matcher])
source(edge[, node_matcher])
target(edge[, node_matcher])

The instance method sources returns the set of all source nodes of the argument edge set.

The instance method targets returns the set of all target nodes of the argument edge set.

The instance method source returns the source node of the argument edge.

The instance method target returns the target node of the argument edge.

If the node_matcher parameter is used, only nodes matching the node matcher are returned. For source and target, None is returned if the source or target node does not match the node matcher.