You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

switch.py 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from abc import ABC
  17. import pandas as pd
  18. from agent.component.base import ComponentBase, ComponentParamBase
  19. class SwitchParam(ComponentParamBase):
  20. """
  21. Define the Switch component parameters.
  22. """
  23. def __init__(self):
  24. super().__init__()
  25. """
  26. {
  27. "cpn_id": "categorize:0",
  28. "not": False,
  29. "operator": "gt/gte/lt/lte/eq/in",
  30. "value": "",
  31. "to": ""
  32. }
  33. """
  34. self.conditions = []
  35. self.default = ""
  36. def check(self):
  37. self.check_empty(self.conditions, "[Switch] conditions")
  38. self.check_empty(self.default, "[Switch] Default path")
  39. for cond in self.conditions:
  40. if not cond["to"]: raise ValueError(f"[Switch] 'To' can not be empty!")
  41. def operators(self, field, op, value):
  42. if op == "gt":
  43. return float(field) > float(value)
  44. if op == "gte":
  45. return float(field) >= float(value)
  46. if op == "lt":
  47. return float(field) < float(value)
  48. if op == "lte":
  49. return float(field) <= float(value)
  50. if op == "eq":
  51. return str(field) == str(value)
  52. if op == "in":
  53. return str(field).find(str(value)) >= 0
  54. return False
  55. class Switch(ComponentBase, ABC):
  56. component_name = "Switch"
  57. def _run(self, history, **kwargs):
  58. for cond in self._param.conditions:
  59. input = self._canvas.get_component(cond["cpn_id"])["obj"].output()[1]
  60. if self._param.operators(input.iloc[0, 0], cond["operator"], cond["value"]):
  61. if not cond["not"]:
  62. return pd.DataFrame([{"content": cond["to"]}])
  63. return pd.DataFrame([{"content": self._param.default}])