104 std::chrono::milliseconds max_execution_time { 0 };
107 std::type_index target_operation_type = std::type_index(
typeid(
void));
123 std::string rule_name = rule.
name;
126 auto insert_pos = std::ranges::upper_bound(m_rules, rule,
128 m_rules.insert(insert_pos, std::move(rule));
130 m_context_index[rule_context].push_back(rule_name);
148 for (
const auto& rule : m_rules) {
149 if (rule.matcher(input, context)) {
171 const std::any& input,
174 auto it = std::ranges::find_if(m_rules,
175 [&rule_name](
const Rule& rule) {
return rule.
name == rule_name; });
177 if (it != m_rules.end() && it->matcher(input, context)) {
178 return it->executor(input, context);
194 auto it = m_context_index.find(context);
195 return it != m_context_index.end() ? it->second : std::vector<std::string> {};
213 template <
typename OperationType>
216 std::vector<std::string> matching_rules;
219 for (
const auto& rule : m_rules) {
220 if (rule.target_operation_type == target_type) {
221 matching_rules.push_back(rule.name);
224 return matching_rules;
260 template <
typename ConcreteOpType,
typename... OpArgs>
264 const std::unordered_map<std::string, std::any>& op_parameters = {},
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));
275 auto captured_args = std::make_tuple(std::forward<OpArgs>(op_args)...);
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)...);
283 apply_context_parameters(operation, ctx);
285 auto typed_input = safe_any_cast_or_throw<DataIO>(input);
286 return operation->apply_operation(typed_input);
289 add_rule(std::move(rule));
330 m_rule.
name = std::move(name);
375 template <ComputeData DataType>
378 m_rule.
matcher = UniversalMatcher::create_type_matcher<DataType>();
393 m_rule.
matcher = std::move(matcher);
407 template <
typename Func>
410 m_rule.
executor = [func = std::forward<Func>(executor)](
const std::any& input,
const ExecutionContext& ctx) -> std::any {
411 return func(input, ctx);
424 template <
typename OperationType>
442 m_rule.
tags = std::move(tags);
457 m_grammar->
add_rule(std::move(m_rule));
496 std::vector<std::string> names;
497 names.reserve(m_rules.size());
498 std::ranges::transform(m_rules, std::back_inserter(names),
499 [](
const Rule& rule) {
return rule.
name; });
508 [[nodiscard]]
bool has_rule(
const std::string& rule_name)
const
510 return std::ranges::any_of(m_rules,
511 [&rule_name](
const Rule& rule) {
return rule.
name == rule_name; });
524 auto it = std::ranges::find_if(m_rules,
525 [&rule_name](
const Rule& rule) {
return rule.
name == rule_name; });
527 if (it != m_rules.end()) {
531 auto& context_rules = m_context_index[context];
532 std::erase_if(context_rules,
533 [&](
const std::string& name) {
534 return name == rule_name;
551 m_context_index.clear();
void build()
Finalizes and adds the rule to the grammar.
RuleBuilder(ComputationGrammar *grammar, std::string name)
Constructs a RuleBuilder for the specified grammar.
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.
ComputationGrammar * m_grammar
Reference to parent grammar.
RuleBuilder & executes(Func &&executor)
Sets the executor function for this rule.
Rule m_rule
Rule being constructed.
RuleBuilder & with_tags(std::vector< std::string > tags)
Sets arbitrary tags for this rule.
RuleBuilder & matches_custom(UniversalMatcher::MatcherFunc matcher)
Sets a custom matcher function.
RuleBuilder & with_priority(int priority)
Sets the execution priority for this rule.
RuleBuilder & with_description(std::string description)
Sets a human-readable description for this rule.
RuleBuilder & targets_operation()
Sets the target operation type for this rule.
Fluent interface for building rules with method chaining.
bool remove_rule(const std::string &rule_name)
Remove a rule by name.
RuleBuilder create_rule(const std::string &name)
Create a rule builder for fluent rule construction.
std::unordered_map< ComputationContext, std::vector< std::string > > m_context_index
Index of rule names by context for fast lookup.
std::optional< Rule > find_best_match(const std::any &input, const ExecutionContext &context) const
Find the best matching rule for the given input.
std::vector< std::string > get_rules_for_operation_type() const
Get rules that target a specific operation type.
size_t get_rule_count() const
Get the total number of rules in the grammar.
void clear_all_rules()
Clear all rules from the grammar.
std::vector< std::string > get_all_rule_names() const
Get all rule names in the grammar.
std::vector< std::string > get_rules_by_context(ComputationContext context) const
Get all rule names for a specific computation context.
bool has_rule(const std::string &rule_name) const
Check if a rule with the given name exists.
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.
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.
void add_rule(Rule rule)
Add a rule to the grammar.
std::vector< Rule > m_rules
All rules sorted by priority (highest first)
Core grammar system for rule-based computation in Maya Flux.
std::function< bool(const std::any &, const ExecutionContext &)> MatcherFunc
ComputationContext
Defines the computational contexts in which rules can be applied.
OperationType
Operation categories for organization and discovery.
ExecutionMode
Execution paradigms for operations.
ComputationContext context
Computational context this rule operates in.
std::type_index target_operation_type
Type of operation this rule creates (for type-based queries)
int priority
Execution priority (higher values evaluated first)
std::unordered_map< std::string, std::any > default_parameters
Default parameters for the rule's operation.
Executor executor
Function that performs the computation.
std::string description
Human-readable description of what the rule does.
std::string name
Unique identifier for this rule.
std::vector< std::string > tags
Arbitrary tags for categorization and search.
std::vector< std::string > dependencies
Names of rules that must execute before this one.
UniversalMatcher::MatcherFunc matcher
Function that determines if rule applies.
std::function< std::any(const std::any &, const ExecutionContext &)> Executor
Type alias for matcher functions used in computation rules.
Represents a computation rule with matching and execution logic.
Context information controlling how a compute operation executes.