MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ add_operation_rule()

template<typename ConcreteOpType , typename... OpArgs>
void MayaFlux::Yantra::ComputationGrammar::add_operation_rule ( const std::string &  rule_name,
ComputationContext  context,
UniversalMatcher::MatcherFunc  matcher,
const std::unordered_map< std::string, std::any > &  op_parameters = {},
int  priority = 50,
OpArgs &&...  op_args 
)
inline

Helper to add concrete operation rules with automatic executor generation.

Template Parameters
ConcreteOpTypeThe concrete operation type to instantiate
OpArgsConstructor argument types for the operation
Parameters
rule_nameUnique name for this rule
contextComputation context for the rule
matcherMatcher function to determine when rule applies
op_parametersParameters to configure the operation instance
priorityExecution priority (default: 50)
op_argsConstructor arguments for the operation

Creates a rule that automatically instantiates and configures a concrete operation type when matched. This is the preferred way to integrate existing operations into the grammar system, as it handles type safety and parameter application automatically.

The generated executor:

  1. Creates an instance of ConcreteOpType with the provided constructor arguments
  2. Applies the op_parameters using set_parameter()
  3. Applies additional parameters from the execution context
  4. Executes the operation on the input data

Example:

grammar.add_operation_rule<SpectralTransformer<>>(
"pitch_shift_rule",
UniversalMatcher::create_type_matcher<std::vector<Kakshya::DataVariant>>(),
{{"operation", std::string("pitch_shift")}, {"pitch_ratio", 1.5}},
80 // priority
);
Concrete transformer for frequency-domain operations.
@ SPECTRAL
Frequency-domain operations.

Definition at line 261 of file ComputeGrammar.hpp.

264 {},
265 int priority = 50,
266 OpArgs&&... op_args)
267 {
268 Rule rule;
269 rule.name = rule_name;
270 rule.context = context;
271 rule.priority = priority;
272 rule.matcher = std::move(matcher);
273 rule.target_operation_type = std::type_index(typeid(ConcreteOpType));
274
275 auto captured_args = std::make_tuple(std::forward<OpArgs>(op_args)...);
276
277 rule.executor = [op_parameters, captured_args = std::move(captured_args)](const std::any& input, const ExecutionContext& ctx) -> std::any {
278 auto operation = std::apply([&op_parameters](auto&&... args) {
279 return create_configured_operation<ConcreteOpType>(op_parameters, std::forward<decltype(args)>(args)...);
280 },
281 captured_args);
282
283 apply_context_parameters(operation, ctx);
284
285 auto typed_input = safe_any_cast_or_throw<DataIO>(input);
286 return operation->apply_operation(typed_input);
287 };
288
289 add_rule(std::move(rule));
290 }
void add_rule(Rule rule)
Add a rule to the grammar.
void apply_context_parameters(std::shared_ptr< OperationType > operation, const ExecutionContext &ctx)
Applies context parameters to an operation.