rewrite.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import functools
  2. from typing import List, Type
  3. from jinja2 import Template
  4. from api.od import ODAPI
  5. from .exec_node import ExecNode
  6. from .data_node import DataNode
  7. from .funcs import not_visited, generate_dot_node
  8. from ..rule_executor import RuleExecutor
  9. class Rewrite(ExecNode, DataNode):
  10. def __init__(self, label: str) -> None:
  11. super().__init__()
  12. self.label = label
  13. self.rule = None
  14. self.rule_executor: RuleExecutor | None = None
  15. def init_rule(self, rule, rule_executer):
  16. self.rule = rule
  17. self.rule_executor = rule_executer
  18. def execute(self, port: str, exec_id: int, od: ODAPI) -> tuple[int, any] | None:
  19. pivot = {}
  20. if self.data_in is not None:
  21. pivot = self.get_input_data("in", exec_id)[0]
  22. # TODO: remove print
  23. print(f"rewrite: {self.label}\n\tpivot: {pivot}")
  24. self.store_data( exec_id,
  25. self.rule_executor.rewrite_rule(od, self.rule, pivot=pivot), "out", 1
  26. )
  27. return None
  28. @not_visited
  29. def generate_dot(
  30. self, nodes: List[str], edges: List[str], visited: set[int], template: Template
  31. ) -> None:
  32. generate_dot_node(
  33. self,
  34. nodes,
  35. template,
  36. **{
  37. "label": f"rewrite\n{self.label}",
  38. "ports_exec": (
  39. self.get_exec_input_gates(),
  40. self.get_exec_output_gates(),
  41. ),
  42. "ports_data": (
  43. self.get_data_input_gates(),
  44. self.get_data_output_gates(),
  45. ),
  46. },
  47. )
  48. ExecNode.generate_dot(self, nodes, edges, visited, template)
  49. DataNode.generate_dot(self, nodes, edges, visited, template)