Skip to main content

src/TransformsV2/detail/PipelineStep.hpp

src/TransformsV2/detail/PipelineStep.hpp

Namespaces

Name
Neuralyzer
Neuralyzer::Transforms
Neuralyzer::Transforms::V2

Classes

Name
struct Neuralyzer::Transforms::V2::PipelineStep
Represents a single step in a transform pipeline.

Source code

#ifndef PIPELINE_STEP_HPP
#define PIPELINE_STEP_HPP

#include "core/ElementRegistry.hpp"     // ElementRegistry
#include "PipelineValueStore/PipelineValueStore.hpp"  // PipelineValueStore

#include "TransformTypes/TransformTypes.hpp" // ElementVariant, BatchVariant

#include <any>       //std::any
#include <functional>// std::function
#include <map>       // std::map
#include <string>    // std::string

namespace Neuralyzer::Transforms::V2 {


// ============================================================================
// Pipeline Step Definition
// ============================================================================

struct PipelineStep {
    std::string transform_name;
    mutable std::any params;// Type-erased parameters (mutable for preprocessing/caching)

    std::map<std::string, std::string> param_bindings;

    // Type-erased executors that know the correct parameter type
    // These are set when the step is added to the pipeline
    std::function<ElementVariant(ElementVariant const &, std::any const &)> element_executor;
    std::function<BatchVariant(BatchVariant const &, std::any const &)> time_grouped_executor;

    template<typename Params>
    PipelineStep(std::string name, Params p)
        : transform_name(std::move(name)),
          params(std::move(p)) {
        // Create type-erased executors that capture the parameter type
        auto & registry = ElementRegistry::instance();
        auto const * meta = registry.getMetadata(transform_name);

        if (meta && !meta->is_time_grouped) {
            // Element transform executor
            element_executor = [name = transform_name, p = this->params](
                                       ElementVariant const & input, std::any const &) -> ElementVariant {
                // This will be called with the correct types by the pipeline
                // The actual execution is deferred until we know InputElement and OutputElement
                return ElementVariant{};// Placeholder - will be replaced by specialized executor
            };
        } else if (meta && meta->is_time_grouped) {
            // Time-grouped transform executor
            time_grouped_executor = [name = transform_name, p = this->params](
                                            BatchVariant const & input, std::any const &) -> BatchVariant {
                return BatchVariant{};// Placeholder
            };
        }
    }

    PipelineStep(std::string name)
        : PipelineStep(std::move(name), NoParams{}) {}

    // ========================================================================
    // Value Store Bindings (V2 pattern)
    // ========================================================================

    void applyBindings(PipelineValueStore const & store) const;

    [[nodiscard]] bool hasBindings() const noexcept;

private:
public:
    template<typename InputElement, typename OutputElement, typename Params>
    void createElementExecutor() {
        auto & registry = ElementRegistry::instance();
        element_executor = [&registry, name = transform_name, p = std::any_cast<Params>(params)](
                                   ElementVariant const & input_variant, std::any const &) -> ElementVariant {
            // Extract input from variant
            if (auto const * input = std::get_if<InputElement>(&input_variant)) {
                OutputElement result = registry.execute<InputElement, OutputElement, Params>(
                        name, *input, p);
                return ElementVariant{result};
            }
            throw std::runtime_error("Input variant does not hold expected type: " + std::string(typeid(InputElement).name()));
        };
    }

    template<typename InputElement, typename OutputElement, typename Params>
    void createTimeGroupedExecutor() {
        auto & registry = ElementRegistry::instance();
        time_grouped_executor = [&registry, name = transform_name, p = std::any_cast<Params>(params)](
                                        BatchVariant const & input_batch, std::any const &) -> BatchVariant {
            if (auto const * input_vec = std::get_if<std::vector<InputElement>>(&input_batch)) {
                std::span<InputElement const> input_span(*input_vec);
                std::vector<OutputElement> result = registry.executeTimeGrouped<InputElement, OutputElement, Params>(
                        name, input_span, p);
                return BatchVariant{result};
            }
            throw std::runtime_error("Input batch does not hold expected type: " + std::string(typeid(std::vector<InputElement>).name()));
        };
    }
};

}// namespace Neuralyzer::Transforms::V2

#endif// PIPELINE_STEP_HPP

Updated on 2026-11-13 at 20:25:44 +0000