match.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from typing import List, override, Type
  2. from jinja2 import Template
  3. from api.od import ODAPI
  4. from transformation.schedule.rule_executor import RuleExecutor
  5. from .exec_node import ExecNode
  6. from .data_node import DataNode
  7. from .funcs import not_visited, generate_dot_node
  8. class Match(ExecNode, DataNode):
  9. def input_event(self, gate: str, exec_id: int) -> None:
  10. pass
  11. def __init__(self, label: str, n: int | float) -> None:
  12. super().__init__()
  13. self.label: str = label
  14. self.n: int = n
  15. self.rule = None
  16. self.rule_executer: RuleExecutor | None = None
  17. @override
  18. def nextState(self, exec_id: int) -> tuple[ExecNode, str]:
  19. return self.next_node["fail" if self.data_out["out"].empty(exec_id) else "success"]
  20. @staticmethod
  21. @override
  22. def get_exec_output_gates():
  23. return ["success", "fail"]
  24. def execute(self, port: str, exec_id: int, od: ODAPI) -> tuple[int, any] | None:
  25. pivot = {}
  26. if self.data_in is not None:
  27. pivot = self.get_input_data("in", exec_id)[0]
  28. # TODO: remove this print
  29. print(f"matching: {self.label}\n\tpivot: {pivot}")
  30. self.store_data( exec_id,
  31. self.rule_executer.match_rule(od.m, self.rule, pivot=pivot), "out", self.n
  32. )
  33. return None
  34. def init_rule(self, rule, rule_executer):
  35. self.rule = rule
  36. self.rule_executer = rule_executer
  37. @not_visited
  38. def generate_dot(
  39. self, nodes: List[str], edges: List[str], visited: set[int], template: Template
  40. ) -> None:
  41. generate_dot_node(
  42. self,
  43. nodes,
  44. template,
  45. **{
  46. "label": f"match\n{self.label}\nn = {self.n}",
  47. "ports_exec": (
  48. self.get_exec_input_gates(),
  49. self.get_exec_output_gates(),
  50. ),
  51. "ports_data": (
  52. self.get_data_input_gates(),
  53. self.get_data_output_gates(),
  54. ),
  55. },
  56. )
  57. ExecNode.generate_dot(self, nodes, edges, visited, template)
  58. DataNode.generate_dot(self, nodes, edges, visited, template)