7.4.4. Class Node

A node represents an entity in the source code (e. g., a function, a variable, a file) or a concept defined by the analyst or an analysis (e. g., a cluster, an architectural entity).

7.4.4.1. Obtaining a Node

class Node(node_type)
class Node(graph, node_type_name)
class Node(graph, node_id)

The Node class constructor returns either a new node of type node_type or a new node of the type named node_type_name within graph. A node type can be acquired from the graph using the node_type instance method (see Section Node and Edge Types). The third constructor variant returns the existing node in graph with the id node_id. It raises a ValueError if no such node exists.

The new node is not contained in any view, but it is already part of the graph. It must be inserted into a view. A node in the RFG graph is automatically garbage collected when it is removed from the last view containing it.

Caution

If a node is removed from the last view that contains it, the node is automatically garbage collected immediately. Therefore, ensure the node is added to a target view before removing it from a source view if you intend to transfer the node between two views.

Note

If a new node was created and should be discarded, it must first be inserted into a view and then removed from that view. The node will then be garbage collected.

Caution

Nodes contained in sets are not automatically removed from those sets once they are garbage collected from an RFG.

All methods of the RFG interface that return nodes return values of class Node.

7.4.4.2. General Operations

graph()

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

id()

The id instance method returns the number identifying the node within its graph (see the Node class constructor).

__str__()

The __str__ instance method returns a textual representation of the node (for debugging purposes only). Use the general attribute handling methods for user-facing output.

__eq__(other)
__ne__(other)

The __eq__ and __ne__ instance methods check for the equality and inequality of two nodes, respectively.

Typically, these methods are called using the == and != operators.

7.4.4.3. Hashing of Nodes

__hash__()

The hash function is used for dictionary keys. Hash values may change between different script executions and cannot be used for persistence.

The code fragment in Listing Mapping the linkage name onto the nodes hashes the Linkage.Name attribute for all nodes in a view.

Mapping the linkage name onto the nodes
#!/usr/bin/env rfgscript
# Copyright (C) 2025 Axivion GmbH
# Copyright (C) 2025 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)

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

my_view = my_graph.view(rfg.code_facts_view_name())

# an empty dictionary
get_node_of_name = {}

# let us fill the table
for n in my_view.xnodes():
    get_node_of_name[n["Linkage.Name"]] = n

# from here on, we have random access to the nodes
# of the code facts view by link name
# example:
# somenode = get_node_of_name["somename"]

7.4.4.4. Type of a Node

node_type()
is_of_type(descriptor)
is_of_subtype(descriptor)
mutate(descriptor)

The node_type instance method returns the node’s type as a NodeType object. The is_of_type instance method takes a type descriptor or a string representing the type name as an argument and checks whether the node is of the given type (excluding subtypes). The is_of_subtype instance method includes subtypes (children) of the given type in the check. The mutate instance method changes the type of a node.

The script shown in Listing Counting how many nodes are present in a view per node type counts the number of nodes of each type present in an RFG view. The RFG file and the name of the view are provided as command-line parameters.

Counting how many nodes are present in a view per node type
#!/usr/bin/env rfgscript
# Copyright (C) 2025 Axivion GmbH
# Copyright (C) 2025 The Qt Company GmbH, a subsidiary of The Qt Group
# https://www.qt.io

import sys
import typing

from bauhaus import rfg

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

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

# summary contains the count of each node type contained in myrfg
summary: typing.Dict[str, int] = {}
if myrfg.is_view_name(sys.argv[2]):
    for node in myrfg.view(sys.argv[2]).xnodes():
        type_of_node = node.node_type().name()
        summary[type_of_node] = summary.get(type_of_node, 0) + 1

    # printout the result table
    print("\n".join("%s %s" % (atype, summary[atype]) for atype in summary))
else:
    print("Argument %s is not a view in the loaded RFG." % sys.argv[2])

7.4.4.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 attributes of a node. 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 names for all set attributes. items returns a list of all set key-value pairs (with keys as strings). get_string_list interprets the string attribute as a list and returns it. set_string_list stores a string representation of the provided string list. append_to_string_list appends the specified string to the string list stored in the attribute.

7.4.4.6. Membership in the View

insert(view)
add(view)
remove(view)

The insert and add instance methods ensure the node is contained in the specified view. The remove instance method removes the node from a view.

Caution

If a node is removed from the last view that contains it, the node is automatically garbage collected immediately. Therefore, ensure the node is added to a target view before removing it from a source view if you intend to transfer the node between two views.

The View class features a __contains__ method to check whether a node is contained in a view.

7.4.4.7. Predicates for the Neighborhood of Nodes

is_neighbor(view, node)
is_predecessor(view, node)
is_successor(view, node)
The is_neighbor instance method checks whether the current node is a neighbor of node in view. The is_predecessor instance method checks whether the current node is a predecessor of node in view. The is_successor instance method checks whether the current node is a successor of node in view.

7.4.4.8. Querying the Neighborhood of Nodes

These instance methods perform the operations described in Section Querying the Neighborhood of Nodes and Edges.

incomings(view[, edge_matcher])
outgoings(view[, edge_matcher])
attached(view[, edge_matcher])

The incomings, outgoings, and attached instance methods return edge sets.

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

The successors, predecessors, and neighbors instance methods return node sets.

Note

Because edges may be present in one view but not another, the view argument is crucial for determining neighborhood relations.