krrood.class_diagrams.class_diagram#

Attributes#

Exceptions#

ParseError

Error that will be raised when the parser encounters something that can/should not be parsed.

Classes#

ClassRelation

Abstract base class representing a relationship between two classes in a UML class diagram.

Inheritance

Represents an inheritance (generalization) relationship in UML.

Association

Represents a general association relationship between two classes.

HasRoleTaker

This is an association between a role and a role taker where the role class contains a role taker field.

WrappedClass

A node wrapper around a Python class used in the class diagram graph.

WrappedSpecializedGeneric

Specialization of WrappedClass for completely parameterized generic types, e.g. Generic[float].

ClassDiagram

A graph of classes and their relations discovered via attribute introspection.

Functions#

resolve_type(→ Any)

Resolve type variables and forward references in a type.

make_specialized_dataclass(→ Type)

Build a concrete dataclass for a fully specialized generic alias, e.g., GenericClass[float].

Module Contents#

krrood.class_diagrams.class_diagram.RWXNode = None#
class krrood.class_diagrams.class_diagram.ClassRelation#

Bases: abc.ABC

Abstract base class representing a relationship between two classes in a UML class diagram.

source: WrappedClass#

The source class in the relation.

target: WrappedClass#

The target class in the relation.

property color: str#

Default edge color used when visualizing the relation.

class krrood.class_diagrams.class_diagram.Inheritance#

Bases: ClassRelation

Represents an inheritance (generalization) relationship in UML.

This is an “is-a” relationship where the source class inherits from the target class. In UML notation, this is represented by a solid line with a hollow triangle pointing to the parent class.

class krrood.class_diagrams.class_diagram.Association#

Bases: ClassRelation

Represents a general association relationship between two classes.

This is the most general form of relationship, indicating that instances of one class are connected to instances of another class. In UML notation, this is shown as a solid line.

field: krrood.class_diagrams.wrapped_field.WrappedField#

The field in the source class that creates this association with the target class.

property one_to_many: bool#

Whether the association is one-to-many (True) or many-to-one (False).

get_key(include_field_name: bool = False) tuple#

A tuple representing the key of the association.

class krrood.class_diagrams.class_diagram.HasRoleTaker#

Bases: Association

This is an association between a role and a role taker where the role class contains a role taker field.

exception krrood.class_diagrams.class_diagram.ParseError#

Bases: TypeError

Error that will be raised when the parser encounters something that can/should not be parsed.

For instance, Union types

class krrood.class_diagrams.class_diagram.WrappedClass#

A node wrapper around a Python class used in the class diagram graph.

index: int | None = None#
clazz: Type#
property class_to_introspect: Type#
Returns:

The class where the introspector should be called on.

property fields: List[krrood.class_diagrams.wrapped_field.WrappedField]#

Return wrapped fields discovered by the diagram’s attribute introspector.

Public names from the introspector are used to index _wrapped_field_name_map_.

property name: str#
Returns:

The name of the class that is wrapped.

property name_with_entire_path: str#
class krrood.class_diagrams.class_diagram.WrappedSpecializedGeneric#

Bases: WrappedClass

Specialization of WrappedClass for completely parameterized generic types, e.g. Generic[float].

property name_with_entire_path: str#
property class_to_introspect#
Returns:

The class where the introspector should be called on.

property name#
Returns:

The name of the class that is wrapped.

class krrood.class_diagrams.class_diagram.ClassDiagram#

A graph of classes and their relations discovered via attribute introspection.

classes: dataclasses.InitVar[List[Type]]#
introspector: krrood.class_diagrams.attribute_introspector.AttributeIntrospector#
get_associations_with_condition(clazz: Type | WrappedClass, condition: Callable[[Association], bool]) Iterable[Association]#

Get all associations that match the condition.

Parameters:
  • clazz – The source class or wrapped class for which outgoing edges are to be retrieved.

  • condition – The condition to filter relations by.

get_outgoing_relations(clazz: Type | WrappedClass) Iterable[ClassRelation]#

Get all outgoing edge relations of the given class.

Parameters:

clazz – The source class or wrapped class for which outgoing edges are to be retrieved.

get_common_role_taker_associations(cls1: Type | WrappedClass, cls2: Type | WrappedClass) Tuple[HasRoleTaker | None, HasRoleTaker | None]#

Return pair of role-taker associations if both classes point to the same target.

The method checks whether both classes have a HasRoleTaker association to the same target class and returns the matching associations, otherwise (None, None).

get_role_taker_associations_of_cls(cls: Type | WrappedClass) HasRoleTaker | None#

Return the role-taker association of a class if present.

A role taker is a field that is a one-to-one relationship and is not optional.

get_neighbors_with_relation_type(cls: Type | WrappedClass, relation_type: Type[ClassRelation]) Tuple[WrappedClass, Ellipsis]#

Return all neighbors of a class whose connecting edge matches the relation type.

Parameters:
  • cls – The class or wrapped class for which neighbors are to be found.

  • relation_type – The type of the relation to filter edges by.

Returns:

A tuple containing the neighbors of the class, filtered by the specified relation type.

get_outgoing_neighbors_with_relation_type(cls: Type | WrappedClass, relation_type: Type[ClassRelation]) Tuple[WrappedClass, Ellipsis]#

Caches and retrieves the outgoing neighbors of a given class with a specific relation type using the dependency graph.

Parameters:

cls – The class or wrapped class for which outgoing neighbors are to be found. relation_type: The type of the relation to filter edges by.

Returns:

A tuple containing the outgoing neighbors of the class, filtered by the specified relation type.

Raises:

Any exceptions raised internally by find_successors_by_edge or during class wrapping.

get_incoming_neighbors_with_relation_type(cls: Type | WrappedClass, relation_type: Type[ClassRelation]) Tuple[WrappedClass, Ellipsis]#
get_out_edges(cls: Type | WrappedClass) Tuple[ClassRelation, Ellipsis]#

Caches and retrieves the outgoing edges (relations) for the provided class in a dependency graph.

Parameters:

cls – The class or wrapped class for which outgoing edges are to be retrieved.

Returns:

A tuple of outgoing edges (relations) associated with the provided class.

property parent_map#

Build parent map from inheritance edges: child_idx -> set(parent_idx)

all_ancestors(node_idx: int) set[int]#

DFS to compute all ancestors for each node index

get_assoc_keys_by_source(include_field_name: bool = False) dict[int, set[tuple]]#

Fetches association keys grouped by their source from the internal dependency graph.

This method traverses the edges of the dependency graph, identifies associations, and groups their keys by their source nodes. Optionally includes the field name of associations in the resulting keys.

Include_field_name:

Optional; If True, includes the field name in the association keys. Defaults to False.

Returns:

A dictionary where the keys are source node identifiers (int), and the values are sets of tuples representing association keys.

to_subdiagram_without_inherited_associations(include_field_name: bool = False) ClassDiagram#

Return a new class diagram where association edges that are present on any ancestor of the source class are removed from descendants.

Inheritance edges are preserved.

remove_edges(edges)#

Remove edges from the dependency graph

property wrapped_classes#

Return all wrapped classes present in the diagram.

property associations: List[Association]#

Return all association relations present in the diagram.

property inheritance_relations: List[Inheritance]#

Return all inheritance relations present in the diagram.

get_wrapped_class(clazz: Type) WrappedClass | None#

Gets the wrapped class corresponding to the provided class type.

If the class type is already a WrappedClass, it will be returned as is. Otherwise, the method checks if the class type has an associated WrappedClass in the internal mapping and returns it if found.

:param clazz : The class type to check or retrieve the associated WrappedClass. :return: The associated WrappedClass if it exists, None otherwise.

add_node(clazz: Type | WrappedClass)#

Adds a new node to the dependency graph for the specified wrapped class.

The method sets the position of the given wrapped class in the dependency graph, links it with the current class diagram, and updates the mapping of the underlying class to the wrapped class.

Parameters:

clazz – The wrapped class object to be added to the dependency graph.

add_relation(relation: ClassRelation)#

Adds a relation to the internal dependency graph.

The method establishes a directed edge in the graph between the source and target indices of the provided relation. This function is used to model dependencies among entities represented within the graph.

Relation:

The relation object that contains the source and target entities and

encapsulates the relationship between them.

visualize(filename: str = 'class_diagram.pdf', title: str = 'Class Diagram', figsize: tuple = (35, 30), node_size: int = 7000, font_size: int = 25, layout: str = 'layered', edge_style: str = 'straight', **kwargs)#

Visualize the class diagram using rustworkx_utils.

Creates a visual representation of the class diagram showing classes and their relationships. The diagram is saved as a PDF file.

Parameters:
  • filename – Output filename for the visualization

  • title – Title for the diagram

  • figsize – Figure size as (width, height) tuple

  • node_size – Size of the nodes in the visualization

  • font_size – Font size for labels

  • kwargs – Additional keyword arguments passed to RWXNode.visualize()

clear()#
krrood.class_diagrams.class_diagram.resolve_type(type_to_resolve: Any, substitution: Dict[TypeVar, Any], name_substitution: Dict[str, Any]) Any#

Resolve type variables and forward references in a type.

Parameters:
  • type_to_resolve – The type to resolve.

  • substitution – Mapping of TypeVar to concrete types.

  • name_substitution – Mapping of TypeVar names to concrete types.

Returns:

The resolved type.

krrood.class_diagrams.class_diagram.make_specialized_dataclass(alias: _GenericAlias) Type#

Build a concrete dataclass for a fully specialized generic alias, e.g., GenericClass[float].

The resulting class is intended for internal use only and should never be used directly.

Parameters:

alias – The fully specialized generic alias to build a dataclass for.

Returns:

A concrete dataclass corresponding to the provided alias.