Class: Lutaml::Xsd::Validation::RuleEngine
- Inherits:
-
Object
- Object
- Lutaml::Xsd::Validation::RuleEngine
- Defined in:
- lib/lutaml/xsd/validation/rule_engine.rb
Overview
RuleEngine executes validation rules by category
This class coordinates the execution of validation rules in a specific order, managing the validation workflow and collecting results. It implements the Strategy pattern, delegating actual validation to individual rules.
Instance Attribute Summary collapse
-
#collector ⇒ ResultCollector
readonly
The result collector.
-
#registry ⇒ RuleRegistry
readonly
The rule registry.
Instance Method Summary collapse
-
#execute_all(document, repository) ⇒ void
Execute all registered rules.
-
#execute_category(category, document, repository) ⇒ void
Execute all rules in a specific category.
-
#execute_enabled(document, repository) ⇒ void
Execute only enabled rules.
-
#execute_rule(rule, document, repository) ⇒ void
Execute a single rule.
-
#execute_with_order(categories, document, repository) ⇒ void
Execute rules with a custom order.
-
#initialize(registry, collector) ⇒ RuleEngine
constructor
Initialize a new RuleEngine.
-
#inspect ⇒ String
Detailed string representation.
-
#statistics ⇒ Hash
Get execution statistics.
-
#to_h ⇒ Hash
Convert to hash representation.
-
#to_s ⇒ String
String representation.
Constructor Details
#initialize(registry, collector) ⇒ RuleEngine
Initialize a new RuleEngine
33 34 35 36 |
# File 'lib/lutaml/xsd/validation/rule_engine.rb', line 33 def initialize(registry, collector) @registry = registry @collector = collector end |
Instance Attribute Details
#collector ⇒ ResultCollector (readonly)
Returns The result collector.
27 28 29 |
# File 'lib/lutaml/xsd/validation/rule_engine.rb', line 27 def collector @collector end |
#registry ⇒ RuleRegistry (readonly)
Returns The rule registry.
24 25 26 |
# File 'lib/lutaml/xsd/validation/rule_engine.rb', line 24 def registry @registry end |
Instance Method Details
#execute_all(document, repository) ⇒ void
This method returns an undefined value.
Execute all registered rules
Executes rules in category order: structure, type, constraint, facet, identity.
65 66 67 68 69 70 71 72 73 |
# File 'lib/lutaml/xsd/validation/rule_engine.rb', line 65 def execute_all(document, repository) execute_category(:structure, document, repository) execute_category(:type, document, repository) execute_category(:constraint, document, repository) execute_category(:facet, document, repository) execute_category(:identity, document, repository) rescue ResultCollector::StopValidationError # Stop validation early if configured end |
#execute_category(category, document, repository) ⇒ void
This method returns an undefined value.
Execute all rules in a specific category
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/lutaml/xsd/validation/rule_engine.rb', line 46 def execute_category(category, document, repository) rules = @registry.rules_for_category(category) rules.each do |rule| execute_rule(rule, document, repository) end rescue ResultCollector::StopValidationError # Stop validation early if configured raise end |
#execute_enabled(document, repository) ⇒ void
This method returns an undefined value.
Execute only enabled rules
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/lutaml/xsd/validation/rule_engine.rb', line 108 def execute_enabled(document, repository) enabled_rules = @registry.all_rules(enabled_only: true) # Group by category and execute in order categories = ValidationRule::CATEGORIES categories.each do |category| category_rules = enabled_rules.select { |r| r.category == category } category_rules.each do |rule| execute_rule(rule, document, repository) end end rescue ResultCollector::StopValidationError # Stop validation early if configured end |
#execute_rule(rule, document, repository) ⇒ void
This method returns an undefined value.
Execute a single rule
81 82 83 84 85 86 87 |
# File 'lib/lutaml/xsd/validation/rule_engine.rb', line 81 def execute_rule(rule, document, repository) return unless rule.enabled? rule.validate(document, repository, @collector) rescue StandardError => e handle_rule_error(rule, e) end |
#execute_with_order(categories, document, repository) ⇒ void
This method returns an undefined value.
Execute rules with a custom order
95 96 97 98 99 100 101 |
# File 'lib/lutaml/xsd/validation/rule_engine.rb', line 95 def execute_with_order(categories, document, repository) categories.each do |category| execute_category(category, document, repository) end rescue ResultCollector::StopValidationError # Stop validation early if configured end |
#inspect ⇒ String
Detailed string representation
159 160 161 162 163 |
# File 'lib/lutaml/xsd/validation/rule_engine.rb', line 159 def inspect "#<#{self.class.name} " \ "registry=#{@registry.inspect} " \ "collector=#{@collector.class.name}>" end |
#statistics ⇒ Hash
Get execution statistics
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/lutaml/xsd/validation/rule_engine.rb', line 127 def statistics { total_rules: @registry.all_rules(enabled_only: false).size, enabled_rules: @registry.all_rules(enabled_only: true).size, categories: @registry.categories.size, errors_collected: @collector.errors.size, warnings_collected: @collector.warnings.size, infos_collected: @collector.infos.size, } end |
#to_h ⇒ Hash
Convert to hash representation
141 142 143 144 145 146 |
# File 'lib/lutaml/xsd/validation/rule_engine.rb', line 141 def to_h { registry: @registry.to_h, statistics: statistics, } end |
#to_s ⇒ String
String representation
151 152 153 154 |
# File 'lib/lutaml/xsd/validation/rule_engine.rb', line 151 def to_s stats = statistics "RuleEngine(#{stats[:enabled_rules]}/#{stats[:total_rules]} rules)" end |