switch.py


Overview

The switch.py file defines a Switch component within the InfiniFlow system, designed to direct workflow execution based on conditional logic akin to a multi-branch switch statement. It evaluates a set of conditions against inputs obtained from other components and determines the next component(s) to execute. This component is critical in controlling branching logic in process automation, enabling dynamic routing depending on the data or state evaluated at runtime.

The file contains two primary classes:


Classes and Functions

Class: SwitchParam

class SwitchParam(ComponentParamBase):

Purpose

Defines and validates the parameters that configure the behavior of the Switch component. It inherits from ComponentParamBase and primarily manages the conditions that the Switch evaluates.

Attributes

Methods

Usage Example

param = SwitchParam()
param.conditions = [
    {
        "logical_operator": "and",
        "items": [
            {"cpn_id": "categorize:0", "operator": "contains", "value": "example"},
            {"cpn_id": "categorize:1", "operator": "=", "value": "test"}
        ],
        "to": ["next_component_id"]
    }
]
param.end_cpn_ids = ["default_component_id"]
param.check()  # Raises error if invalid

Class: Switch

class Switch(ComponentBase, ABC):

Purpose

Implements the Switch component logic for evaluating multiple conditional branches and determining the next step(s) in the workflow. It inherits from ComponentBase and is abstract (ABC).

Class Attributes

Methods

Parameters

Usage Example

switch_component = Switch()
switch_component._param = param  # instance of SwitchParam configured earlier
# _canvas is set by the workflow engine/environment
switch_component._invoke()
# Check outputs via switch_component.get_output("next") etc.

Important Implementation Details


Interaction with Other System Components


Visual Diagram

classDiagram
    class SwitchParam {
        -conditions: list
        -end_cpn_ids: list
        -operators: list[str]
        +__init__()
        +check()
        +get_input_form() dict
    }

    class Switch {
        -component_name: str = "Switch"
        +_invoke(**kwargs)
        +process_operator(input: Any, operator: str, value: Any) bool
        +thoughts() str
    }

    SwitchParam <|-- Switch : uses/configures
    Switch ..> ComponentBase : inherits
    SwitchParam ..> ComponentParamBase : inherits

Summary

The switch.py file implements a configurable conditional branching component for the InfiniFlow system. It allows workflows to dynamically select the next execution path based on composite conditions evaluated against other components' output values. The component is robust, supporting various string and numeric comparisons, logical operators, and fallback routes. It integrates closely with the workflow engine's canvas to fetch live data and route execution accordingly, making it a pivotal element for complex workflow orchestration.


If you need usage examples for integration in the overall workflow or further explanation of related components, please ask!