MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
GrammarHelper.hpp
Go to the documentation of this file.
1#pragma once
2
4
6
7namespace MayaFlux::Yantra {
8
9/**
10 * @enum ComputationContext
11 * @brief Defines the computational contexts in which rules can be applied
12 */
13enum class ComputationContext : uint8_t {
14 TEMPORAL, ///< Time-domain operations
15 SPECTRAL, ///< Frequency-domain operations
16 SPATIAL, ///< Spatial operations
17 SEMANTIC, ///< Semantic operations
18 STRUCTURAL, ///< Graph/tree operations
19 LOGICAL, ///< Boolean/conditional operations
20 PARAMETRIC, ///< Parameter transformation
21 REACTIVE, ///< Event-driven operations
22 CONCURRENT, ///< Parallel/async operations
23 RECURSIVE, ///< Self-referential operations
24 CONVOLUTION ///< Convolution-based operations
25};
26
27/**
28 * @class UniversalMatcher
29 * @brief Type-agnostic pattern matching for computation rules
30 *
31 * Provides factory methods for creating matcher functions that can be used
32 * to determine if operations should be applied in specific contexts.
33 */
34class MAYAFLUX_API UniversalMatcher {
35public:
36 using MatcherFunc = std::function<bool(const std::any&, const ExecutionContext&)>;
37
38 /**
39 * @brief Creates a matcher that checks for specific data types
40 * @tparam DataType The ComputeData type to match
41 * @return Matcher function that returns true if input matches DataType
42 */
43 template <ComputeData DataType>
45 {
46 return [](const std::any& input, const ExecutionContext& /*ctx*/) -> bool {
47 try {
48 std::any_cast<IO<DataType>>(input);
49 return true;
50 } catch (const std::bad_any_cast&) {
51 return false;
52 }
53 };
54 }
55
56 /**
57 * @brief Creates a matcher that checks for specific computation contexts
58 * @param required_context The context that must be present
59 * @return Matcher function that returns true if context matches
60 */
62 {
63 return [required_context](const std::any& /*input*/, const ExecutionContext& ctx) -> bool {
64 return ctx.execution_metadata.contains("computation_context") && std::any_cast<ComputationContext>(ctx.execution_metadata.at("computation_context")) == required_context;
65 };
66 }
67
68 /**
69 * @brief Creates a matcher that checks for specific parameter values
70 * @param param_name Name of the parameter to check
71 * @param expected_value Expected value (type-checked)
72 * @return Matcher function that returns true if parameter matches
73 */
74 static MatcherFunc create_parameter_matcher(const std::string& param_name, const std::any& expected_value)
75 {
76 return [param_name, expected_value](const std::any& /*input*/, const ExecutionContext& ctx) -> bool {
77 return ctx.execution_metadata.contains(param_name) && ctx.execution_metadata.at(param_name).type() == expected_value.type();
78 };
79 }
80
81 /**
82 * @brief Combines multiple matchers with AND logic
83 * @param matchers Vector of matcher functions to combine
84 * @return Matcher that returns true only if all matchers pass
85 */
86 static MatcherFunc combine_and(std::vector<MatcherFunc> matchers)
87 {
88 return [matchers = std::move(matchers)](const std::any& input, const ExecutionContext& ctx) -> bool {
89 return std::ranges::all_of(matchers, [&](const auto& matcher) {
90 return matcher(input, ctx);
91 });
92 };
93 }
94
95 /**
96 * @brief Combines multiple matchers with OR logic
97 * @param matchers Vector of matcher functions to combine
98 * @return Matcher that returns true if any matcher passes
99 */
100 static MatcherFunc combine_or(std::vector<MatcherFunc> matchers)
101 {
102 return [matchers = std::move(matchers)](const std::any& input, const ExecutionContext& ctx) -> bool {
103 return std::ranges::any_of(matchers, [&](const auto& matcher) {
104 return matcher(input, ctx);
105 });
106 };
107 }
108};
109
110/**
111 * @brief Creates an operation instance with parameters using safe_any_cast system
112 * @tparam OperationType Type of operation to create
113 * @tparam Args Constructor argument types
114 * @param parameters Map of parameter names to values
115 * @param args Constructor arguments
116 * @return Configured operation instance
117 */
118template <typename OperationType, typename... Args>
119std::shared_ptr<OperationType> create_configured_operation(
120 const std::unordered_map<std::string, std::any>& parameters,
121 Args&&... args)
122{
123 auto operation = std::make_shared<OperationType>(std::forward<Args>(args)...);
124
125 for (const auto& [param_name, param_value] : parameters) {
126 try {
127 operation->set_parameter(param_name, param_value);
128 } catch (...) {
129 // Ignore parameters that don't apply to this operation
130 }
131 }
132
133 return operation;
134}
135
136/**
137 * @brief Applies context parameters to an operation
138 * @tparam OperationType Type of operation to configure
139 * @param operation Operation instance to configure
140 * @param ctx Execution context containing parameters
141 */
142template <typename OperationType>
143void apply_context_parameters(std::shared_ptr<OperationType> operation, const ExecutionContext& ctx)
144{
145 for (const auto& [ctx_name, ctx_value] : ctx.execution_metadata) {
146 try {
147 operation->set_parameter(ctx_name, ctx_value);
148 } catch (...) {
149 // Ignore parameters that don't apply to this operation
150 }
151 }
152}
153
154} // namespace MayaFlux::Yantra
static MatcherFunc combine_or(std::vector< MatcherFunc > matchers)
Combines multiple matchers with OR logic.
static MatcherFunc create_context_matcher(ComputationContext required_context)
Creates a matcher that checks for specific computation contexts.
static MatcherFunc create_type_matcher()
Creates a matcher that checks for specific data types.
std::function< bool(const std::any &, const ExecutionContext &)> MatcherFunc
static MatcherFunc combine_and(std::vector< MatcherFunc > matchers)
Combines multiple matchers with AND logic.
static MatcherFunc create_parameter_matcher(const std::string &param_name, const std::any &expected_value)
Creates a matcher that checks for specific parameter values.
Type-agnostic pattern matching for computation rules.
std::shared_ptr< OperationType > create_configured_operation(const std::unordered_map< std::string, std::any > &parameters, Args &&... args)
Creates an operation instance with parameters using safe_any_cast system.
@ TEMPORAL
Time-based patterns, onset detection.
@ SPATIAL
Multi-dimensional geometric analysis.
@ SPECTRAL
Spectral energy (FFT-based)
@ RECURSIVE
Recursive/nested extraction.
void apply_context_parameters(std::shared_ptr< OperationType > operation, const ExecutionContext &ctx)
Applies context parameters to an operation.
ComputationContext
Defines the computational contexts in which rules can be applied.
@ STRUCTURAL
Graph/tree operations.
@ LOGICAL
Boolean/conditional operations.
@ CONCURRENT
Parallel/async operations.
@ PARAMETRIC
Parameter transformation.
@ REACTIVE
Event-driven operations.
@ CONVOLUTION
Convolution-based operations.
OperationType
Operation categories for organization and discovery.
std::unordered_map< std::string, std::any > execution_metadata
Context information for operation execution.