7.4.8. Attributes

Attributes can be attached to all types of Graph elements, including the Graph itself, Views, Nodes, and Edges. As this is a fundamental concept common to all elements, it is explained here first.

Together with Node and Edge types, attributes form the Graph schema.

An attribute is characterized by:

  • its name (key),

  • the type of element it can be bound to,

  • the type of values it can bind,

  • and the values it binds to elements.

Note

Attribute values are typed, differing from the Python principle of duck typing.

Attributes that are valid after source analysis is complete are listed in Language Schema. You can also use Gravis to get an overview of existing attributes for nodes and edges: load an RFG file and open the Mark by query... dialog. The <attribute> drop-down menu in the Nodes and Edges tabs shows all available attributes for nodes and edges, respectively. Alternatively, open a Graph, View, Node, or Edge information window and select Add attribute... from the context menu in the attribute section. The Key drop-down menu in the Add attribute dialog also displays all available attributes.

7.4.8.1. Attribute Names

An attribute’s name indicates its position within the attribute hierarchy. The parts of an attribute name are separated by dots. It is recommended that these parts contain only ASCII letters, digits, and underscores, although this is not enforced.

The Gravis UI presents attributes hierarchically in a pop-up menu structure within its Query dialog.

If no dot is used in the attribute name, the attribute is located at the top level of the attribute namespace.

Note

Using a hierarchy is generally recommended to improve interactive access and reduce namespace pollution.

Caution

Some attributes are used by Gravis and other Axivion Suite tools. If such attributes are removed from an RFG, these tools may no longer operate correctly. An example is the node and edge attribute Source.Name.

7.4.8.2. Attributable Elements

There are four separate namespaces for attribute names, one for each element type: Graph, View, Node, and Edge. Attributes with the same name but different value types can be created for different element types.

The following operations can be used to manipulate the attribute schema information for a Graph instance:

element_attribute_names()
element_attribute(attr_name)
is_element_attribute_name(attr_name)
create_element_attribute(new_attr_name, attr_type)
rename_element_attribute(attr_name, new_name)
destroy_element_attribute(attr_name)

Replace “element” with “graph”, “view”, “node”, or “edge” to manipulate the respective namespace.

The element_attribute_names method returns a list of all attribute names (as strings) for that element type.

The element_attribute method converts a string into an attribute descriptor, which can then be used to query or set an attribute’s value. The operations discussed in this section all use the attribute name rather than the descriptor.

Note

Descriptors are not required but may improve attribute access speed if necessary. Generally, an attribute name can be used whenever a descriptor is accepted.

The is_element_attribute_name method checks if a string is a valid element attribute name.

A new attribute can be created using create_element_attribute, where the second parameter specifies the type of values to be stored. The value type must be one of the following string literals: “toggle”, “int”, “float”, or “string”.

7.4.8.3. Attribute Types

Attributes have defined types; therefore, Python’s duck typing does not apply. You must ensure the value type matches the attribute’s defined type. The following value types are available:

toggle

A Toggle is either attached to an element (node, edge, etc.) or not. It does not carry a value.

int

Integers are in the range of -2147483648 and 2147483647.

float

Floats are in the range of -3.40282E+38 and 3.40282E+38.

string

Strings are of unbounded length and are UTF-8 encoded.

Note

Toggle attributes are either set or unset for an element and do not carry a value, distinguishing them from pure booleans. A toggle attribute has two states: set and unset.

7.4.8.4. Attribute Values

Attributes are used similarly to Python dictionaries.

has_key(key)
__contains__(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 can be used to query and manipulate attribute values for a graph, view, node, or edge.
  • has_key and __contains__ check whether the attribute key is set.

    Caution

    __contains__ and has_key return False instead of raising an exception if the specified attribute name is invalid.

    __getitem__ retrieves the stored value for attribute key.

    This method returns the attribute’s set value in the appropriate type. If no value exists for the queried attribute name, a KeyError exception is raised. If attribute is a toggle attribute, it returns True if the attribute is set. If the attribute is not set, a KeyError is raised. To check if a toggle is attached, use has_key (typically via the in operator). If key is not a valid attribute name, an InvalidAttribute exception is raised.

  • __setitem__ sets a new value for the attribute. The object passed must be of the appropriate type; for example, an integer cannot be passed to a string attribute. Providing a value of an incorrect type raises an AttributeTypeMismatch exception. Setting a toggle attribute to False unsets it.

  • __delitem__ removes an attribute value.

  • __iter__ iterates through set attributes as name-value pairs.

  • keys returns a list of all set attribute names.

  • items returns a list of all set name/key (as strings) and value pairs.

  • get_string_list interprets a string attribute value as a comma-separated list (using the backslash “\” as an escape character) and returns it.
    Both unset attributes and empty strings are interpreted as empty lists. This interpretation applies only to get_string_list and append_to_string_list. The has_key method distinguishes between an unset attribute and a set attribute (including an empty string representing an empty list).
    A trailing comma is ignored.
    A trailing escape character at the end of the string will raise a RuntimeError.
  • set_string_list creates a string representation of the elements of the iterable value and stores it in the attribute. Elements are escaped accordingly.

  • append_to_string_list properly escapes the string value and appends it to the list currently stored in the attribute. The current value (if available) is verified as a valid list; otherwise, a RuntimeError is thrown. append_to_string_list can be called on an element where the attribute has not yet been set, which is equivalent to appending to an empty list.

The type of an attribute can be checked using the attribute’s type method.

Listing Handling of graph attributes illustrates the handling of graph attributes.

Handling of graph attributes
#!/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

from bauhaus import rfg

my_graph = rfg.Graph()

my_graph.create_graph_attribute("MyOwnAttributes.AnAttribute", "string")
remembered_attribute = my_graph.create_graph_attribute(
    "MyOwnAttributes.AnotherAttribute", "string"
)

# one can use the attribute's name...
my_graph["MyOwnAttributes.AnAttribute"] = "some value here"

# ...and one can also use the attribute descriptor
my_graph[remembered_attribute] = "some other value here"

# make sure the value has been attached to the graph's attribute
if "MyOwnAttributes.AnAttribute" in my_graph:
    print("Yes, there is a value set")
else:
    print("Oh no, this can't be")

print("The set value is:", my_graph["MyOwnAttributes.AnAttribute"])

# iterate all set attributes
for attribute_name in my_graph.keys():
    print("The value for %s is %s" % (attribute_name, my_graph[attribute_name]))

# show a list of all bound pairs (key, value)
print(my_graph.items())

# get rid of the attribute value
del my_graph["MyOwnAttributes.AnAttribute"]
if "MyOwnAttributes.AnAttribute" not in my_graph:
    print("Deleting an attribute value works fine")
else:
    print("Oh no, this can't be")

Note

Toggle-valued attributes are either set or not set for an element and do not hold a value. In this regard, they differ from pure booleans. A toggle-valued attribute can be in one of two states: set or unset.

Listing Handling of attributes of type toggle illustrates the handling of toggle-valued attributes.

Handling of attributes of type toggle
#!/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


from bauhaus import rfg

g = rfg.Graph()
g.create_graph_attribute('My Attribute', 'toggle')

# use the in operator for testing if a toggle valued attribute
# is attached to an element
if 'My Attribute' in g:
    print('unexpected')

# when accessing the value of a toggle valued attribute, either
# guard using the above if-statement or a try-statement
try:
    print(g['My Attribute'])
except KeyError:
    print('intended flow of things')

# attach the toggle to an element by assigning an expression
# evaluating to True
g['My Attribute'] = (2 - 1) == 1
g['My Attribute'] = True

# assigning other types will result in an exception
try:
    g['My Attribute'] = 'yes'
except rfg.AttributeTypeMismatch:
    print('intended flow of things, too')

try:
    print(g['My Attribute'])
except KeyError:
    print('unexpected')

# use del to get rid of a toggle value
del g['My Attribute']

try:
    print(g['My Attribute'])
except KeyError:
    print('intended flow of things')

# assigning False also gets rid of a toggle value
g['My Attribute'] = True
g['My Attribute'] = False
if 'My Attribute' not in g:
    print('intended flow of things')

7.4.8.5. Attribute Classes: GraphAttribute, ViewAttribute, NodeAttribute, and EdgeAttribute

All four attribute classes share the same interface:

class GraphAttribute (graph, attr_name)
class ViewAttribute (graph, attr_name)
class NodeAttribute (graph, attr_name)
class EdgeAttribute (graph, attr_name)

The constructor takes a graph and the name of an attribute. The name must refer to an existing attribute of the corresponding type in the graph. Attribute names are case-sensitive.

Adding new attributes, as well as removing and querying existing ones, must be performed using the instance methods of the Graph class.

type()

The type instance method returns the type of the attribute. Valid return values are “toggle”, “int”, “float”, and “string”.

graph()

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

name()

The name instance method returns the name of the attribute.

__str__()

The __str__ instance method returns a textual representation of the attribute (for debugging purposes only).