Skip to content

CallGraphForest

Class for call graph forests.

A call graph forest represents a collection of call graph trees.

Attributes:

Name Type Description
graphs dict[NodeID, CallGraphNode]

The dictionary of call graph trees. The key is the name of the tree, the value is the root CallGraphNode of the tree.

Source code in src/library_analyzer/processing/api/purity_analysis/model/_call_graph.py
@dataclass
class CallGraphForest:
    """Class for call graph forests.

    A call graph forest represents a collection of call graph trees.

    Attributes
    ----------
    graphs :
        The dictionary of call graph trees.
        The key is the name of the tree, the value is the root CallGraphNode of the tree.
    """

    graphs: dict[NodeID, CallGraphNode] = field(default_factory=dict)

    def add_graph(self, graph_id: NodeID, graph: CallGraphNode) -> None:
        """Add a call graph tree to the forest.

        Parameters
        ----------
        graph_id :
            The NodeID of the tree node.
        graph :
            The root of the tree.
        """
        # if graph_id in self.forest:
        #     raise ValueError(f"Graph with id {graph_id} already exists inside the call graph.")
        self.graphs[graph_id] = graph

    def get_graph(self, graph_id: NodeID) -> CallGraphNode:
        """Get a call graph tree from the forest.

        Parameters
        ----------
        graph_id :
            The NodeID of the tree node to get.

        Raises
        ------
        KeyError
            If the graph_id is not in the forest.
        """
        result = self.graphs.get(graph_id)
        if result is None:
            raise KeyError(f"Graph with id {graph_id} not found inside the call graph.")
        return result

    def has_graph(self, graph_id: NodeID) -> bool:
        """Check if the forest contains a call graph tree with the given NodeID.

        Parameters
        ----------
        graph_id :
            The NodeID of the tree to check for.

        Returns
        -------
        bool
            True if the forest contains a tree with the given NodeID, False otherwise.
        """
        return graph_id in self.graphs

    def delete_graph(self, graph_id: NodeID) -> None:
        """Delete a call graph tree from the forest.

        Parameters
        ----------
        graph_id :
            The NodeID of the tree to delete.
        """
        del self.graphs[graph_id]

graphs: dict[NodeID, CallGraphNode] = field(default_factory=dict) class-attribute instance-attribute

__init__(graphs=dict())

add_graph(graph_id, graph)

Add a call graph tree to the forest.

Parameters:

Name Type Description Default
graph_id NodeID

The NodeID of the tree node.

required
graph CallGraphNode

The root of the tree.

required
Source code in src/library_analyzer/processing/api/purity_analysis/model/_call_graph.py
def add_graph(self, graph_id: NodeID, graph: CallGraphNode) -> None:
    """Add a call graph tree to the forest.

    Parameters
    ----------
    graph_id :
        The NodeID of the tree node.
    graph :
        The root of the tree.
    """
    # if graph_id in self.forest:
    #     raise ValueError(f"Graph with id {graph_id} already exists inside the call graph.")
    self.graphs[graph_id] = graph

delete_graph(graph_id)

Delete a call graph tree from the forest.

Parameters:

Name Type Description Default
graph_id NodeID

The NodeID of the tree to delete.

required
Source code in src/library_analyzer/processing/api/purity_analysis/model/_call_graph.py
def delete_graph(self, graph_id: NodeID) -> None:
    """Delete a call graph tree from the forest.

    Parameters
    ----------
    graph_id :
        The NodeID of the tree to delete.
    """
    del self.graphs[graph_id]

get_graph(graph_id)

Get a call graph tree from the forest.

Parameters:

Name Type Description Default
graph_id NodeID

The NodeID of the tree node to get.

required

Raises:

Type Description
KeyError

If the graph_id is not in the forest.

Source code in src/library_analyzer/processing/api/purity_analysis/model/_call_graph.py
def get_graph(self, graph_id: NodeID) -> CallGraphNode:
    """Get a call graph tree from the forest.

    Parameters
    ----------
    graph_id :
        The NodeID of the tree node to get.

    Raises
    ------
    KeyError
        If the graph_id is not in the forest.
    """
    result = self.graphs.get(graph_id)
    if result is None:
        raise KeyError(f"Graph with id {graph_id} not found inside the call graph.")
    return result

has_graph(graph_id)

Check if the forest contains a call graph tree with the given NodeID.

Parameters:

Name Type Description Default
graph_id NodeID

The NodeID of the tree to check for.

required

Returns:

Type Description
bool

True if the forest contains a tree with the given NodeID, False otherwise.

Source code in src/library_analyzer/processing/api/purity_analysis/model/_call_graph.py
def has_graph(self, graph_id: NodeID) -> bool:
    """Check if the forest contains a call graph tree with the given NodeID.

    Parameters
    ----------
    graph_id :
        The NodeID of the tree to check for.

    Returns
    -------
    bool
        True if the forest contains a tree with the given NodeID, False otherwise.
    """
    return graph_id in self.graphs