null_node.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. """
  2. null_node.py
  3. Defines the NullNode class, a no-op singleton execution node used for open execution pins
  4. in the object diagram execution graph.
  5. """
  6. from abc import ABC
  7. from typing import List, Type
  8. from jinja2 import Template
  9. from api.od import ODAPI
  10. from .funcs import generate_dot_node
  11. from .singleton import Singleton
  12. from .exec_node import ExecNode
  13. class NullNode(ExecNode, metaclass=Singleton):
  14. """
  15. A no-op execution node representing a null operation.
  16. This node is typically used to represent a placeholder or open execution pin.
  17. It always returns a fixed result and does not perform any operation.
  18. """
  19. def __init__(self):
  20. """
  21. Initializes the NullNode instance.
  22. Inherits unique ID and state behavior from ExecNode.
  23. """
  24. super().__init__()
  25. def execute(self, port: str, exec_id: int, od: ODAPI) -> tuple[int, any] | None:
  26. """
  27. Simulates execution by returning a static result indicating an open pin.
  28. Args:
  29. port (str): The name of the input port.
  30. exec_id (int): The current execution ID.
  31. od (ODAPI): The Object Diagram API instance providing execution context.
  32. Returns:
  33. tuple[int, str] | None: A tuple (-1, "open pin reached") indicating a no-op.
  34. """
  35. return -1, "open pin reached"
  36. @staticmethod
  37. def get_exec_output_gates():
  38. """
  39. Returns the list of output gates for execution.
  40. Returns:
  41. list: An empty list, as NullNode has no output gates.
  42. """
  43. return []
  44. def generate_dot(
  45. self, nodes: List[str], edges: List[str], visited: set[int], template: Template
  46. ) -> None:
  47. """
  48. Generates DOT graph representation for this node if it hasn't been visited.
  49. Args:
  50. nodes (List[str]): A list to accumulate DOT node definitions.
  51. edges (List[str]): A list to accumulate DOT edge definitions.
  52. visited (set[int]): Set of already visited node IDs to avoid cycles.
  53. template (Template): A Jinja2 template used to render the node's DOT representation.
  54. """
  55. if self.id in visited:
  56. return
  57. generate_dot_node(
  58. self,
  59. nodes,
  60. template,
  61. **{
  62. "label": "null",
  63. "ports_exec": (
  64. self.get_exec_input_gates(),
  65. self.get_exec_output_gates(),
  66. ),
  67. }
  68. )