MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
MayaFlux::Yantra::ComputationGrammar Class Reference

Core grammar system for rule-based computation in Maya Flux. More...

#include <ComputeGrammar.hpp>

+ Collaboration diagram for MayaFlux::Yantra::ComputationGrammar:

Classes

struct  Rule
 Represents a computation rule with matching and execution logic. More...
 
class  RuleBuilder
 Fluent interface for building rules with method chaining. More...
 

Public Member Functions

void add_rule (Rule rule)
 Add a rule to the grammar.
 
std::optional< Rulefind_best_match (const std::any &input, const ExecutionContext &context) const
 Find the best matching rule for the given input.
 
std::optional< std::any > execute_rule (const std::string &rule_name, const std::any &input, const ExecutionContext &context) const
 Execute a specific rule by name.
 
std::vector< std::string > get_rules_by_context (ComputationContext context) const
 Get all rule names for a specific computation context.
 
template<typename OperationType >
std::vector< std::string > get_rules_for_operation_type () const
 Get rules that target a specific operation type.
 
template<typename ConcreteOpType , typename... OpArgs>
void 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)
 Helper to add concrete operation rules with automatic executor generation.
 
RuleBuilder create_rule (const std::string &name)
 Create a rule builder for fluent rule construction.
 
size_t get_rule_count () const
 Get the total number of rules in the grammar.
 
std::vector< std::string > get_all_rule_names () const
 Get all rule names in the grammar.
 
bool has_rule (const std::string &rule_name) const
 Check if a rule with the given name exists.
 
bool remove_rule (const std::string &rule_name)
 Remove a rule by name.
 
void clear_all_rules ()
 Clear all rules from the grammar.
 

Private Attributes

std::vector< Rulem_rules
 All rules sorted by priority (highest first)
 
std::unordered_map< ComputationContext, std::vector< std::string > > m_context_index
 Index of rule names by context for fast lookup.
 

Detailed Description

Core grammar system for rule-based computation in Maya Flux.

The ComputationGrammar provides a powerful, declarative system for defining how computational operations should be applied based on input data characteristics, execution context, and user-defined rules. This enables intelligent, adaptive computation that can select appropriate operations dynamically.

Core Concepts

Rules: Define when and how operations should be applied. Each rule contains:

  • Matching logic to determine if the rule applies to given input
  • Execution logic that performs the actual computation
  • Metadata for organization, prioritization, and optimization

Contexts: Categorize rules by computational domain (temporal, spectral, etc.) for efficient lookup and logical organization.

Priority System: Higher priority rules are evaluated first, allowing for hierarchical decision making and exception handling.

Usage Patterns

Simple Rule Creation

// Add a rule for mathematical transformations on double vectors
grammar.create_rule("gain_amplification")
.matches_type<std::vector<double>>()
.executes([](const std::any& input, const ExecutionContext& ctx) {
// Custom transformation logic
return input; // simplified
})
.build();
RuleBuilder & with_context(ComputationContext context)
Sets the computation context for this rule.
RuleBuilder & matches_type()
Sets the matcher to check for a specific data type.
RuleBuilder & with_priority(int priority)
Sets the execution priority for this rule.
RuleBuilder create_rule(const std::string &name)
Create a rule builder for fluent rule construction.
Core grammar system for rule-based computation in Maya Flux.
@ TEMPORAL
Time-domain operations.
Context information for operation execution.

Complex Matcher Combinations

auto complex_matcher = UniversalMatcher::combine_and({
UniversalMatcher::create_type_matcher<std::vector<Kakshya::DataVariant>>(),
UniversalMatcher::create_parameter_matcher("frequency_range", std::string("audio"))
});
grammar.create_rule("spectral_filter")
.matches_custom(complex_matcher)
.executes([](const std::any& input, const ExecutionContext& ctx) {
// Spectral filtering logic
return input;
})
.build();
RuleBuilder & executes(Func &&executor)
Sets the executor function for this rule.
RuleBuilder & matches_custom(UniversalMatcher::MatcherFunc matcher)
Sets a custom matcher function.
static MatcherFunc create_context_matcher(ComputationContext required_context)
Creates a matcher that checks for specific computation contexts.
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.
@ SPECTRAL
Frequency-domain operations.

Operation Integration

// Create rule that automatically applies MathematicalTransformer
"auto_normalize",
ComputationContext::MATHEMATICAL,
UniversalMatcher::create_type_matcher<std::vector<Kakshya::DataVariant>>(),
{{"operation", std::string("normalize")}, {"target_peak", 1.0}},
75 // priority
);
void 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)
Helper to add concrete operation rules with automatic executor generation.
Concrete transformer for mathematical operations.

Definition at line 76 of file ComputeGrammar.hpp.


The documentation for this class was generated from the following file: