composite.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # -*- coding: utf-8 -*-
  2. """
  3. Composite Conditions - Kombiniert mehrere Conditions.
  4. """
  5. from typing import Any
  6. from trixy_core.scheduler.condition.base import Condition
  7. class AndCondition(Condition):
  8. """
  9. Prüft ob ALLE Sub-Conditions erfüllt sind.
  10. Beispiel:
  11. - AndCondition([TimeRangeCondition(...), DayOfWeekCondition(...)])
  12. """
  13. def __init__(
  14. self,
  15. conditions: list[Condition],
  16. name: str | None = None,
  17. invert: bool = False,
  18. ):
  19. """
  20. Initialisiert die Condition.
  21. Args:
  22. conditions: Liste von Sub-Conditions
  23. name: Condition-Name
  24. invert: Ergebnis invertieren
  25. """
  26. super().__init__(name, invert)
  27. if not conditions:
  28. raise ValueError("AndCondition benötigt mindestens eine Condition")
  29. self._conditions = list(conditions)
  30. @property
  31. def conditions(self) -> list[Condition]:
  32. """Sub-Conditions."""
  33. return list(self._conditions)
  34. def add_condition(self, condition: Condition) -> None:
  35. """Fügt Condition hinzu."""
  36. self._conditions.append(condition)
  37. def remove_condition(self, condition: Condition) -> bool:
  38. """Entfernt Condition."""
  39. try:
  40. self._conditions.remove(condition)
  41. return True
  42. except ValueError:
  43. return False
  44. def _evaluate(self, context: dict) -> bool:
  45. """Prüft ob alle Conditions erfüllt sind."""
  46. if not self._conditions:
  47. return True
  48. return all(c.evaluate(context) for c in self._conditions)
  49. def to_dict(self) -> dict:
  50. """Konvertiert zu Dictionary."""
  51. return {
  52. **self._base_dict(),
  53. "conditions": [c.to_dict() for c in self._conditions],
  54. }
  55. class OrCondition(Condition):
  56. """
  57. Prüft ob MINDESTENS EINE Sub-Condition erfüllt ist.
  58. Beispiel:
  59. - OrCondition([StateCondition(...), BooleanCondition(...)])
  60. """
  61. def __init__(
  62. self,
  63. conditions: list[Condition],
  64. name: str | None = None,
  65. invert: bool = False,
  66. ):
  67. """
  68. Initialisiert die Condition.
  69. Args:
  70. conditions: Liste von Sub-Conditions
  71. name: Condition-Name
  72. invert: Ergebnis invertieren
  73. """
  74. super().__init__(name, invert)
  75. if not conditions:
  76. raise ValueError("OrCondition benötigt mindestens eine Condition")
  77. self._conditions = list(conditions)
  78. self._matched_condition: Condition | None = None
  79. @property
  80. def conditions(self) -> list[Condition]:
  81. """Sub-Conditions."""
  82. return list(self._conditions)
  83. @property
  84. def matched_condition(self) -> Condition | None:
  85. """Zuletzt gematchte Condition."""
  86. return self._matched_condition
  87. def add_condition(self, condition: Condition) -> None:
  88. """Fügt Condition hinzu."""
  89. self._conditions.append(condition)
  90. def remove_condition(self, condition: Condition) -> bool:
  91. """Entfernt Condition."""
  92. try:
  93. self._conditions.remove(condition)
  94. return True
  95. except ValueError:
  96. return False
  97. def _evaluate(self, context: dict) -> bool:
  98. """Prüft ob mindestens eine Condition erfüllt ist."""
  99. if not self._conditions:
  100. return False
  101. for condition in self._conditions:
  102. if condition.evaluate(context):
  103. self._matched_condition = condition
  104. return True
  105. self._matched_condition = None
  106. return False
  107. def to_dict(self) -> dict:
  108. """Konvertiert zu Dictionary."""
  109. return {
  110. **self._base_dict(),
  111. "conditions": [c.to_dict() for c in self._conditions],
  112. }
  113. class NotCondition(Condition):
  114. """
  115. Invertiert eine Condition.
  116. Beispiel:
  117. - NotCondition(StateCondition("system_mode", "maintenance"))
  118. → Nur wenn NICHT im Wartungsmodus
  119. Hinweis: Kann auch über invert=True bei anderen Conditions erreicht werden.
  120. """
  121. def __init__(
  122. self,
  123. condition: Condition,
  124. name: str | None = None,
  125. ):
  126. """
  127. Initialisiert die Condition.
  128. Args:
  129. condition: Zu invertierende Condition
  130. name: Condition-Name
  131. """
  132. # NOT Condition hat kein eigenes invert
  133. super().__init__(name, invert=False)
  134. self._condition = condition
  135. @property
  136. def condition(self) -> Condition:
  137. """Innere Condition."""
  138. return self._condition
  139. def _evaluate(self, context: dict) -> bool:
  140. """Invertiert das Ergebnis der inneren Condition."""
  141. return not self._condition.evaluate(context)
  142. def to_dict(self) -> dict:
  143. """Konvertiert zu Dictionary."""
  144. return {
  145. **self._base_dict(),
  146. "condition": self._condition.to_dict(),
  147. }
  148. class XorCondition(Condition):
  149. """
  150. Prüft ob GENAU EINE Sub-Condition erfüllt ist.
  151. Beispiel:
  152. - XorCondition([ConditionA, ConditionB])
  153. → Nur wenn A oder B, aber nicht beide
  154. """
  155. def __init__(
  156. self,
  157. conditions: list[Condition],
  158. name: str | None = None,
  159. invert: bool = False,
  160. ):
  161. """
  162. Initialisiert die Condition.
  163. Args:
  164. conditions: Liste von Sub-Conditions
  165. name: Condition-Name
  166. invert: Ergebnis invertieren
  167. """
  168. super().__init__(name, invert)
  169. if len(conditions) < 2:
  170. raise ValueError("XorCondition benötigt mindestens zwei Conditions")
  171. self._conditions = list(conditions)
  172. @property
  173. def conditions(self) -> list[Condition]:
  174. """Sub-Conditions."""
  175. return list(self._conditions)
  176. def _evaluate(self, context: dict) -> bool:
  177. """Prüft ob genau eine Condition erfüllt ist."""
  178. results = [c.evaluate(context) for c in self._conditions]
  179. return sum(results) == 1
  180. def to_dict(self) -> dict:
  181. """Konvertiert zu Dictionary."""
  182. return {
  183. **self._base_dict(),
  184. "conditions": [c.to_dict() for c in self._conditions],
  185. }