exec_node.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from abc import abstractmethod
  2. from typing import override
  3. from jinja2 import Template
  4. from api.od import ODAPI
  5. from .funcs import generate_dot_edge
  6. from .node import Node
  7. class ExecNode(Node):
  8. def __init__(self) -> None:
  9. super().__init__()
  10. from .null_node import NullNode
  11. self.next_node: dict[str, tuple[ExecNode, str]] = {}
  12. for port in self.get_exec_output_gates():
  13. self.next_node[port] = (NullNode(), "in")
  14. def nextState(self, exec_id: int) -> tuple["ExecNode", str]:
  15. return self.next_node["out"]
  16. @staticmethod
  17. def get_exec_input_gates():
  18. return ["in"]
  19. @staticmethod
  20. def get_exec_output_gates():
  21. return ["out"]
  22. def connect(self, next_state: "ExecNode", from_gate: str, to_gate: str) -> None:
  23. if from_gate not in self.get_exec_output_gates():
  24. raise Exception(f"from_gate {from_gate} is not a valid port")
  25. if to_gate not in next_state.get_exec_input_gates():
  26. raise Exception(f"to_gate {to_gate} is not a valid port")
  27. self.next_node[from_gate] = (next_state, to_gate)
  28. @abstractmethod
  29. def execute(self, port: str, exec_id: int, od: ODAPI) -> tuple[int, any] | None:
  30. return None
  31. @override
  32. def generate_dot(
  33. self, nodes: list[str], edges: list[str], visited: set[int], template: Template
  34. ) -> None:
  35. for out_port, edge in self.next_node.items():
  36. template.render()
  37. generate_dot_edge(
  38. self,
  39. edge[0],
  40. edges,
  41. template,
  42. kwargs={
  43. "prefix": "e",
  44. "from_gate": out_port,
  45. "to_gate": edge[1],
  46. "color": "darkblue",
  47. },
  48. )
  49. for edge in self.next_node.values():
  50. edge[0].generate_dot(nodes, edges, visited, template)