99 std::chrono::milliseconds max_execution_time { 0 };
102 std::type_index target_operation_type = std::type_index(
typeid(
void));
118 std::string rule_name = rule.
name;
121 auto insert_pos = std::ranges::upper_bound(m_rules, rule,
123 m_rules.insert(insert_pos, std::move(rule));
125 m_context_index[rule_context].push_back(rule_name);
143 for (
const auto& rule : m_rules) {
144 if (rule.matcher(input, context)) {
166 const std::any& input,
169 auto it = std::ranges::find_if(m_rules,
170 [&rule_name](
const Rule& rule) {
return rule.
name == rule_name; });
172 if (it != m_rules.end() && it->matcher(input, context)) {
173 return it->executor(input, context);
189 auto it = m_context_index.find(context);
190 return it != m_context_index.end() ? it->second : std::vector<std::string> {};
208 template <
typename OperationType>
211 std::vector<std::string> matching_rules;
214 for (
const auto& rule : m_rules) {
215 if (rule.target_operation_type == target_type) {
216 matching_rules.push_back(rule.name);
219 return matching_rules;
255 template <
typename ConcreteOpType,
typename... OpArgs>
259 const std::unordered_map<std::string, std::any>& op_parameters = {},
264 rule.name = rule_name;
265 rule.context = context;
266 rule.priority = priority;
267 rule.matcher = std::move(matcher);
268 rule.target_operation_type = std::type_index(
typeid(ConcreteOpType));
271 auto captured_args = std::make_tuple(std::forward<OpArgs>(op_args)...);
273 rule.executor = [op_parameters, captured_args = std::move(captured_args)](
const std::any& input,
const ExecutionContext& ctx) -> std::any {
274 auto operation = std::apply([&op_parameters](
auto&&... args) {
275 return create_configured_operation<ConcreteOpType>(op_parameters, std::forward<
decltype(args)>(args)...);
279 apply_context_parameters(operation, ctx);
281 auto typed_input = std::any_cast<IO<std::vector<Kakshya::DataVariant>>>(input);
282 return operation->apply_operation(typed_input);
285 add_rule(std::move(rule));
326 m_rule.
name = std::move(name);
371 template <ComputeData DataType>
374 m_rule.
matcher = UniversalMatcher::create_type_matcher<DataType>();
389 m_rule.
matcher = std::move(matcher);
403 template <
typename Func>
406 m_rule.
executor = [func = std::forward<Func>(executor)](
const std::any& input,
const ExecutionContext& ctx) -> std::any {
407 return func(input, ctx);
420 template <
typename OperationType>
438 m_rule.
tags = std::move(tags);
453 m_grammar->
add_rule(std::move(m_rule));
492 std::vector<std::string> names;
493 names.reserve(m_rules.size());
494 std::ranges::transform(m_rules, std::back_inserter(names),
495 [](
const Rule& rule) {
return rule.
name; });
504 [[nodiscard]]
bool has_rule(
const std::string& rule_name)
const
506 return std::ranges::any_of(m_rules,
507 [&rule_name](
const Rule& rule) {
return rule.
name == rule_name; });
520 auto it = std::ranges::find_if(m_rules,
521 [&rule_name](
const Rule& rule) {
return rule.
name == rule_name; });
523 if (it != m_rules.end()) {
527 auto& context_rules = m_context_index[context];
529 std::remove(context_rules.begin(), context_rules.end(), rule_name),
530 context_rules.end());
546 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.
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.
std::function< std::any(const std::any &, const ExecutionContext &)> executor
Function that performs the computation.
UniversalMatcher::MatcherFunc matcher
Function that determines if rule applies.
Represents a computation rule with matching and execution logic.
Context information for operation execution.