66template <ComputeData InputType = std::vector<Kakshya::DataVariant>, ComputeData OutputType = InputType>
97 template <
typename ConcreteOpType>
100 static_assert(std::is_base_of_v<ComputeOperation<InputType, OutputType>, ConcreteOpType>,
101 "Operation must derive from ComputeOperation");
126 template <
typename ConcreteOpType,
typename... Args>
129 auto operation = std::make_shared<ConcreteOpType>(std::forward<Args>(args)...);
130 return add_operation(operation, name);
157 input_type current_data = input;
160 if (
auto best_rule = m_grammar->find_best_match(current_data, current_context)) {
161 if (
auto rule_result = m_grammar->execute_rule(best_rule->name, current_data, current_context)) {
162 auto cast_result = safe_any_cast<input_type>(*rule_result);
165 current_data = *cast_result.value;
168 Journal::Component::Yantra,
169 Journal::Context::Runtime,
170 "Grammar rule '{}' returned incompatible type: {}",
177 for (
const auto& [operation, name] : m_operations) {
179 auto result = operation->apply_operation(current_data);
180 current_data = result;
181 }
catch (
const std::exception& e) {
183 Journal::Component::Yantra, Journal::Context::Runtime, std::source_location::current(),
184 "Pipeline operation '{}' failed: {}", name, e.what());
188 if constexpr (std::is_same_v<InputType, OutputType>) {
203 [[nodiscard]] std::shared_ptr<ComputationGrammar>
get_grammar()
const
217 m_grammar = std::move(grammar);
238 template <
typename ConcreteOpType>
239 std::shared_ptr<ConcreteOpType>
get_operation(
const std::string& name)
const
241 for (
const auto& [operation, op_name] : m_operations) {
242 if (op_name == name) {
243 return std::dynamic_pointer_cast<ConcreteOpType>(operation);
269 template <
typename ConcreteOpType>
271 const std::function<
void(std::shared_ptr<ConcreteOpType>)>& configurator)
273 for (
auto& [operation, op_name] : m_operations) {
274 if (op_name == name) {
275 if (
auto concrete_op = std::dynamic_pointer_cast<ConcreteOpType>(operation)) {
276 configurator(concrete_op);
290 return m_operations.size();
301 m_operations.clear();
313 std::vector<std::string> names;
314 names.reserve(m_operations.size());
315 std::ranges::transform(m_operations, std::back_inserter(names),
316 [](
const auto& op_pair) {
return op_pair.second; });
330 auto it = std::ranges::find_if(m_operations,
331 [&name](
const auto& op_pair) {
return op_pair.second == name; });
333 if (it != m_operations.end()) {
334 m_operations.erase(it);
342 std::vector<std::pair<std::shared_ptr<ComputeOperation<InputType, OutputType>>, std::string>>
m_operations;
352namespace PipelineFactory {
369 template <ComputeData DataType = std::vector<Kakshya::DataVariant>>
372 auto pipeline = std::make_shared<ComputationPipeline<DataType>>();
397 template <ComputeData DataType = std::vector<Kakshya::DataVariant>>
400 auto pipeline = std::make_shared<ComputationPipeline<DataType>>();
483 template <ComputeData InputType>
489 if (
auto best_rule = m_grammar->find_best_match(current, context)) {
490 if (
auto rule_result = m_grammar->execute_rule(best_rule->name, current, context)) {
491 auto cast_result = safe_any_cast<Datum<InputType>>(*rule_result);
493 current = *cast_result.value;
496 Journal::Component::Yantra,
497 Journal::Context::Runtime,
498 "Grammar rule '{}' returned incompatible type: {}",
517 template <ComputeData InputType>
519 const std::string& rule_name,
524 if (
auto rule_result = m_grammar->execute_rule(rule_name, current, context)) {
525 auto cast_result = safe_any_cast<Datum<InputType>>(*rule_result);
527 current = *cast_result.value;
530 Journal::Component::Yantra,
531 Journal::Context::Runtime,
532 "Grammar rule '{}' returned incompatible type: {}",
538 Journal::Component::Yantra,
539 Journal::Context::Runtime,
540 "Grammar rule '{}' not found",
567 m_grammar = std::move(grammar);
580 m_grammar->add_rule(std::move(rule));
602 return m_grammar->create_rule(name);
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
Fluent interface for building rules with method chaining.
Core grammar system for rule-based computation in Maya Flux.
std::shared_ptr< ConcreteOpType > get_operation(const std::string &name) const
Get operation by name.
ComputationPipeline & create_operation(const std::string &name="", Args &&... args)
Create and add operation by type.
bool remove_operation(const std::string &name)
Remove operation by name.
std::shared_ptr< ComputationGrammar > get_grammar() const
Get the grammar instance.
void clear_operations()
Clear all operations.
size_t operation_count() const
Get number of operations in pipeline.
void set_grammar(std::shared_ptr< ComputationGrammar > grammar)
Set grammar instance.
output_type process(const input_type &input, const ExecutionContext &context={})
Execute the pipeline with grammar rule application.
std::vector< std::pair< std::shared_ptr< ComputeOperation< InputType, OutputType > >, std::string > > m_operations
Operations and their names in execution order.
std::shared_ptr< ComputationGrammar > m_grammar
Grammar instance for rule-based operation selection.
ComputationPipeline(std::shared_ptr< ComputationGrammar > grammar=nullptr)
Constructor with optional grammar.
std::vector< std::string > get_operation_names() const
Get all operation names in the pipeline.
bool configure_operation(const std::string &name, const std::function< void(std::shared_ptr< ConcreteOpType >)> &configurator)
Configure operation by name.
ComputationPipeline & add_operation(std::shared_ptr< ConcreteOpType > operation, const std::string &name="")
Add a concrete operation instance to the pipeline.
Pipeline that uses grammar rules for operation composition.
Local execution orchestrator for computational operations.
Base interface for all computational operations in the processing pipeline.
Datum< InputType > execute_with_grammar(const std::string &rule_name, const Datum< InputType > &input, const ExecutionContext &context={})
Execute a named grammar rule explicitly by name.
std::shared_ptr< ComputationGrammar > m_grammar
Grammar instance for rule-based operation selection.
void add_grammar_rule(ComputationGrammar::Rule rule)
Add a grammar rule directly to the matrix.
std::shared_ptr< ComputationGrammar > get_grammar() const
Get the grammar instance.
GrammarAwareComputeMatrix(std::shared_ptr< ComputationGrammar > grammar=nullptr)
Constructor with optional grammar.
void set_grammar(std::shared_ptr< ComputationGrammar > grammar)
Set grammar instance.
Datum< InputType > execute_with_grammar(const Datum< InputType > &input, const ExecutionContext &context={})
Execute operations with grammar rule pre-processing.
ComputationGrammar::RuleBuilder create_grammar_rule(const std::string &name)
Create a rule builder for this matrix's grammar.
ComputeMatrix extension that integrates grammar-based operation selection.
void error_rethrow(Component component, Context context, std::source_location location=std::source_location::current(), std::string_view additional_context="")
Catch and log an exception, then rethrow it.
std::shared_ptr< ComputationPipeline< DataType > > create_audio_pipeline()
Create an audio processing pipeline.
std::shared_ptr< ComputationPipeline< DataType > > create_analysis_pipeline()
Create an analysis pipeline.
Represents a computation rule with matching and execution logic.
Input/Output container for computation pipeline data flow with structure preservation.
Context information controlling how a compute operation executes.