Class: CLabs::CaseGen::Rules
Instance Attribute Summary
Attributes inherited from Agent
Class Method Summary collapse
Instance Method Summary collapse
- #[](index) ⇒ Object
- #combinations ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(data, reference_agents = []) ⇒ Rules
constructor
A new instance of Rules.
- #length ⇒ Object
- #parse_data ⇒ Object
- #titles ⇒ Object
- #to_s ⇒ Object
- #validate_rules ⇒ Object
Constructor Details
#initialize(data, reference_agents = []) ⇒ Rules
Returns a new instance of Rules.
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/agents/sets.rb', line 161 def initialize(data, reference_agents=[]) @data = data @agents = reference_agents @rules = [] @rule_classes = [] ObjectSpace.each_object(Class) do |obj| @rule_classes << obj if obj.ancestors.include?(Rule) && obj != Rule end parse_data end |
Class Method Details
.agent_id ⇒ Object
157 158 159 |
# File 'lib/agents/sets.rb', line 157 def Rules.agent_id "casegen:rules" end |
Instance Method Details
#[](index) ⇒ Object
210 211 212 |
# File 'lib/agents/sets.rb', line 210 def [](index) return @rules[index] end |
#combinations ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/agents/sets.rb', line 218 def combinations return @combinations if !@combinations.nil? if @agents[0].class == Sets agent = @agents[0] @combinations = [] agent.combinations.each do |combo| delete = false combo_hash = {} i = 0 # combo is an array of values, in the same order of the set_titles. # combo_hash will have set names matched with set values agent.set_titles.each do |title| combo_hash[title] = combo[i] i += 1 end @rules.each do |rule| delete |= rule.criteria.match(combo_hash) end @combinations << combo if !delete end return @combinations end return [] end |
#each(&block) ⇒ Object
214 215 216 |
# File 'lib/agents/sets.rb', line 214 def each(&block) @rules.each(&block) end |
#length ⇒ Object
206 207 208 |
# File 'lib/agents/sets.rb', line 206 def length @rules.length end |
#parse_data ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/agents/sets.rb', line 172 def parse_data raw_rules = @data.split(/(?=^\S)/) raw_rules.each do |rule| @rule_classes.each do |rule_class| @rules << rule_class.create(rule.strip) end end @rules.compact! @rules.flatten! validate_rules end |
#titles ⇒ Object
243 244 245 |
# File 'lib/agents/sets.rb', line 243 def titles @agents[0].titles end |
#to_s ⇒ Object
247 248 249 250 251 |
# File 'lib/agents/sets.rb', line 247 def to_s puts @agents[0].combinations.inspect if !@agents[0].nil? puts puts @rules.inspect end |
#validate_rules ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/agents/sets.rb', line 185 def validate_rules @agents.each do |agent| if agent.class == Sets @rules.each do |rule| rule.criteria.equalities.each_pair do |set_name, set_value| set = agent.set_by_name(set_name) if set.nil? raise ParserException.new("Invalid set name <#{set_name}> " + "in rule <#{rule.criteria}>. Valid set names are <#{agent.set_names.join(', ')}>.") end if !set.values.include?(set_value) raise ParserException.new("Invalid set value <#{set_value}> " + "in rule <#{rule.criteria}>. Valid set values for <#{set.name}> " + "are <#{set.values.join(', ')}>.") end end end end end end |