node.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. """
  2. node.py
  3. Defines the abstract base Node class for graph-based structures. Each Node is assigned
  4. a unique identifier via an external IdGenerator. The class provides an interface for
  5. managing execution state and generating DOT graph representations.
  6. """
  7. from abc import abstractmethod
  8. from jinja2 import Template
  9. from .funcs import IdGenerator
  10. class Node:
  11. """
  12. Abstract base class for graph nodes. Each Node has a unique ID and supports
  13. context-dependent state management for execution scenarios. Subclasses must
  14. implement the DOT graph generation logic.
  15. """
  16. @abstractmethod
  17. def __init__(self) -> None:
  18. """
  19. Initializes the Node instance with a unique ID.
  20. Attributes:
  21. id (int): A unique identifier assigned by IdGenerator.
  22. """
  23. self.id: int = IdGenerator.generate_node_id()
  24. def get_id(self) -> int:
  25. """
  26. Retrieves the unique identifier of the node.
  27. Returns:
  28. int: The unique node ID.
  29. """
  30. return self.id
  31. def generate_stack_frame(self, exec_id: int) -> None:
  32. """
  33. Initializes a new state frame for a specific execution context.
  34. Designed to be overridden in subclasses that use execution state.
  35. Args:
  36. exec_id (int): The ID of the execution context.
  37. """
  38. def delete_stack_frame(self, exec_id: int) -> None:
  39. """
  40. Deletes the state frame for a specific execution context.
  41. Designed to be overridden in subclasses that use execution state.
  42. Args:
  43. exec_id (int): The ID of the execution context.
  44. """
  45. @abstractmethod
  46. def generate_dot(
  47. self, nodes: list[str], edges: list[str], visited: set[int], template: Template
  48. ) -> None:
  49. """
  50. Generates the DOT graph representation for this node and its relationships.
  51. Args:
  52. nodes (list[str]): A list to append DOT node definitions to.
  53. edges (list[str]): A list to append DOT edge definitions to.
  54. visited (set[int]): A set of already visited node IDs to avoid duplicates or recursion.
  55. template (Template): A Jinja2 template used to format the node's DOT representation.
  56. """