MayaFlux 0.4.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 return static_cast<bool>(safe_any_cast<Datum<DataType>>(input));
48 };
49 }
50
51 /**
52 * @brief Creates a matcher that checks for specific computation contexts
53 * @param required_context The context that must be present
54 * @return Matcher function that returns true if context matches
55 */
57 {
58 return [required_context](const std::any& /*input*/, const ExecutionContext& ctx) -> bool {
59 auto it = ctx.execution_metadata.find("computation_context");
60 if (it == ctx.execution_metadata.end())
61 return false;
62 auto result = safe_any_cast<ComputationContext>(it->second);
63 return result && *result.value == required_context;
64 };
65 }
66
67 /**
68 * @brief Creates a matcher that checks for specific parameter values
69 * @param param_name Name of the parameter to check
70 * @param expected_value Expected value (type-checked)
71 * @return Matcher function that returns true if parameter matches
72 */
73 static MatcherFunc create_parameter_matcher(const std::string& param_name, const std::any& expected_value)
74 {
75 return [param_name, expected_value](const std::any& /*input*/, const ExecutionContext& ctx) -> bool {
76 return ctx.execution_metadata.contains(param_name) && ctx.execution_metadata.at(param_name).type() == expected_value.type();
77 };
78 }
79
80 /**
81 * @brief Combines multiple matchers with AND logic
82 * @param matchers Vector of matcher functions to combine
83 * @return Matcher that returns true only if all matchers pass
84 */
85 static MatcherFunc combine_and(std::vector<MatcherFunc> matchers)
86 {
87 return [matchers = std::move(matchers)](const std::any& input, const ExecutionContext& ctx) -> bool {
88 return std::ranges::all_of(matchers, [&](const auto& matcher) {
89 return matcher(input, ctx);
90 });
91 };
92 }
93
94 /**
95 * @brief Combines multiple matchers with OR logic
96 * @param matchers Vector of matcher functions to combine
97 * @return Matcher that returns true if any matcher passes
98 */
99 static MatcherFunc combine_or(std::vector<MatcherFunc> matchers)
100 {
101 return [matchers = std::move(matchers)](const std::any& input, const ExecutionContext& ctx) -> bool {
102 return std::ranges::any_of(matchers, [&](const auto& matcher) {
103 return matcher(input, ctx);
104 });
105 };
106 }
107};
108
109/**
110 * @brief Creates an operation instance with parameters using safe_any_cast system
111 * @tparam OperationType Type of operation to create
112 * @tparam Args Constructor argument types
113 * @param parameters Map of parameter names to values
114 * @param args Constructor arguments
115 * @return Configured operation instance
116 */
117template <typename OperationType, typename... Args>
118std::shared_ptr<OperationType> create_configured_operation(
119 const std::unordered_map<std::string, std::any>& parameters,
120 Args&&... args)
121{
122 auto operation = std::make_shared<OperationType>(std::forward<Args>(args)...);
123
124 for (const auto& [param_name, param_value] : parameters) {
125 try {
126 operation->set_parameter(param_name, param_value);
127 } catch (...) {
128 MF_DEBUG(
131 "Parameter '{}' with type {} does not apply to operation '{}'",
132 param_name,
133 param_value.type().name(),
134 operation->get_name());
135 }
136 }
137
138 return operation;
139}
140
141/**
142 * @brief Applies context parameters to an operation
143 * @tparam OperationType Type of operation to configure
144 * @param operation Operation instance to configure
145 * @param ctx Execution context containing parameters
146 */
147template <typename OperationType>
148void apply_context_parameters(std::shared_ptr<OperationType> operation, const ExecutionContext& ctx)
149{
150 for (const auto& [ctx_name, ctx_value] : ctx.execution_metadata) {
151 try {
152 operation->set_parameter(ctx_name, ctx_value);
153 } catch (...) {
154 MF_DEBUG(
157 "Context parameter '{}' with type {} does not apply to operation '{}'",
158 ctx_name,
159 ctx_value.type().name(),
160 operation->get_name());
161 }
162 }
163}
164
165} // namespace MayaFlux::Yantra
#define MF_DEBUG(comp, ctx,...)
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.
@ Runtime
General runtime operations (default fallback)
@ Yantra
DSP algorithms, computational units, matrix operations, Grammar.
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
Arbitrary metadata parameters used by operations.
Context information controlling how a compute operation executes.