Class: Evoc::Scenario
- Inherits:
-
Object
- Object
- Evoc::Scenario
- Includes:
- Comparable, Logging
- Defined in:
- lib/evoc/scenario.rb
Instance Attribute Summary collapse
-
#aggregator ⇒ Object
Returns the value of attribute aggregator.
-
#algorithm ⇒ Object
Returns the value of attribute algorithm.
-
#case_id ⇒ Object
Returns the value of attribute case_id.
-
#granularity ⇒ Object
Returns the value of attribute granularity.
-
#max_size ⇒ Object
Returns the value of attribute max_size.
-
#measures ⇒ Object
Returns the value of attribute measures.
-
#model_age ⇒ Object
Returns the value of attribute model_age.
-
#model_size ⇒ Object
Returns the value of attribute model_size.
-
#opts ⇒ Object
Returns the value of attribute opts.
-
#permutation ⇒ Object
Returns the value of attribute permutation.
-
#query ⇒ Object
Returns the value of attribute query.
-
#scenario_id ⇒ Object
Returns the value of attribute scenario_id.
-
#stats ⇒ Object
Returns the value of attribute stats.
-
#tx ⇒ Evoc::Tx
The transaction of this scenario.
-
#tx_id ⇒ Object
Returns the value of attribute tx_id.
-
#tx_index ⇒ String
The history index of this scenarios transaction.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
<=> defines how to compare two Query objects.
-
#expected_outcome ⇒ Array
The list of expected items.
-
#expected_outcome_size ⇒ Integer
The size of the expected outcome.
-
#initialize(opts = Hash.new) ⇒ Scenario
constructor
A new instance of Scenario.
-
#model_end ⇒ Integer
The index in the history where the model ends.
-
#model_hours ⇒ Integer
The time between the first and last transaction in the model.
-
#model_percentage ⇒ Float
The percentage of the whole history that is used in the model.
-
#model_start ⇒ Integer
The index in the history where the model starts.
-
#query_percentage ⇒ Object
query_percentage.
-
#query_size ⇒ Integer
The size of the query.
- #recommendation? ⇒ Boolean
- #to_h ⇒ Object
- #to_s ⇒ Object
-
#tx_size ⇒ Integer
The size of this scenarios transaction.
Methods included from Logging
configure_logger_for, #logger, logger_for, set_level
Constructor Details
#initialize(opts = Hash.new) ⇒ Scenario
Returns a new instance of Scenario.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/evoc/scenario.rb', line 22 def initialize(opts = Hash.new) logger.debug "Initialized new scenario with configuration: #{opts}" self.opts = opts self.scenario_id = opts.hash self.tx_id = opts[:tx_id] self.model_age = opts[:model_age] opts.each do |attribute,value| self.send("#{attribute}=", value) end end |
Instance Attribute Details
#aggregator ⇒ Object
Returns the value of attribute aggregator.
5 6 7 |
# File 'lib/evoc/scenario.rb', line 5 def aggregator @aggregator end |
#algorithm ⇒ Object
Returns the value of attribute algorithm.
5 6 7 |
# File 'lib/evoc/scenario.rb', line 5 def algorithm @algorithm end |
#case_id ⇒ Object
Returns the value of attribute case_id.
5 6 7 |
# File 'lib/evoc/scenario.rb', line 5 def case_id @case_id end |
#granularity ⇒ Object
Returns the value of attribute granularity.
5 6 7 |
# File 'lib/evoc/scenario.rb', line 5 def granularity @granularity end |
#max_size ⇒ Object
Returns the value of attribute max_size.
5 6 7 |
# File 'lib/evoc/scenario.rb', line 5 def max_size @max_size end |
#measures ⇒ Object
Returns the value of attribute measures.
5 6 7 |
# File 'lib/evoc/scenario.rb', line 5 def measures @measures end |
#model_age ⇒ Object
Returns the value of attribute model_age.
5 6 7 |
# File 'lib/evoc/scenario.rb', line 5 def model_age @model_age end |
#model_size ⇒ Object
Returns the value of attribute model_size.
5 6 7 |
# File 'lib/evoc/scenario.rb', line 5 def model_size @model_size end |
#opts ⇒ Object
Returns the value of attribute opts.
5 6 7 |
# File 'lib/evoc/scenario.rb', line 5 def opts @opts end |
#permutation ⇒ Object
Returns the value of attribute permutation.
5 6 7 |
# File 'lib/evoc/scenario.rb', line 5 def permutation @permutation end |
#query ⇒ Object
Returns the value of attribute query.
5 6 7 |
# File 'lib/evoc/scenario.rb', line 5 def query @query end |
#scenario_id ⇒ Object
Returns the value of attribute scenario_id.
5 6 7 |
# File 'lib/evoc/scenario.rb', line 5 def scenario_id @scenario_id end |
#stats ⇒ Object
Returns the value of attribute stats.
5 6 7 |
# File 'lib/evoc/scenario.rb', line 5 def stats @stats end |
#tx ⇒ Evoc::Tx
Returns the transaction of this scenario.
93 94 95 |
# File 'lib/evoc/scenario.rb', line 93 def tx @tx end |
#tx_id ⇒ Object
Returns the value of attribute tx_id.
5 6 7 |
# File 'lib/evoc/scenario.rb', line 5 def tx_id @tx_id end |
#tx_index ⇒ String
Returns the history index of this scenarios transaction.
229 230 231 |
# File 'lib/evoc/scenario.rb', line 229 def tx_index @tx_index end |
Instance Method Details
#<=>(other) ⇒ Object
<=> defines how to compare two Query objects
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/evoc/scenario.rb', line 37 def <=> other return nil unless other.is_a?(Query) comparison = 0 # first we compare the tx id if (self.tx_id <=> other.tx_id) == 0 # if we also have the same query if (self.query.sort <=> other.query.sort) == 0 # use history size as comparator comparison = (self.model_size <=> other.model_size) else # use the query comparison = (self.query.sort <=> other.query.sort) end else # use the tx id comparison = (self.tx_id <=> other.tx_id) end comparison end |
#expected_outcome ⇒ Array
Returns the list of expected items.
213 214 215 216 217 218 219 |
# File 'lib/evoc/scenario.rb', line 213 def expected_outcome expected_outcome = (self.tx.items - self.query) if expected_outcome.empty? logger.warn "The expected outcome was empty" end return expected_outcome end |
#expected_outcome_size ⇒ Integer
Returns the size of the expected outcome.
223 224 225 |
# File 'lib/evoc/scenario.rb', line 223 def expected_outcome_size self.expected_outcome.size end |
#model_end ⇒ Integer
Returns the index in the history where the model ends.
140 141 142 143 144 145 146 147 148 |
# File 'lib/evoc/scenario.rb', line 140 def model_end value = (self.tx_index - 1 - self.model_age.to_i) if value < 0 raise ArgumentError, "The model end index was negative (model_size:#{self.model_size}, tx_index:#{self.tx_index})" elsif value < self.model_start raise ArgumentError, "The model end was before the model start (start: #{self.model_start}, end: #{value})" end return value end |
#model_hours ⇒ Integer
Returns the time between the first and last transaction in the model.
152 153 154 155 156 |
# File 'lib/evoc/scenario.rb', line 152 def model_hours model_end_tx = Evoc::HistoryStore.base_history.get_tx(id: self.model_end) model_start_tx = Evoc::HistoryStore.base_history.get_tx(id: self.model_start) return TimeDifference.between(model_start_tx.date,model_end_tx.date).in_hours end |
#model_percentage ⇒ Float
Returns the percentage of the whole history that is used in the model.
109 110 111 |
# File 'lib/evoc/scenario.rb', line 109 def model_percentage self.model_size.to_i == 0 ? 100 : ((self.model_size.to_f/self.tx_index)*100).round(2) end |
#model_start ⇒ Integer
Returns the index in the history where the model starts.
130 131 132 133 134 135 136 |
# File 'lib/evoc/scenario.rb', line 130 def model_start value = self.tx_index - self.model_size.to_i - self.model_age.to_i if value < 0 raise ArgumentError, "Model out of bound" end return value end |
#query_percentage ⇒ Object
query_percentage
160 161 162 163 |
# File 'lib/evoc/scenario.rb', line 160 def query_percentage total_items = self.query.size.to_i + self.expected_outcome.size (self.query.size.to_f/total_items*100).round end |
#query_size ⇒ Integer
Returns the size of the query.
103 104 105 |
# File 'lib/evoc/scenario.rb', line 103 def query_size self.query.size end |
#recommendation? ⇒ Boolean
74 75 76 77 78 79 80 |
# File 'lib/evoc/scenario.rb', line 74 def recommendation? Evoc::RecommendationCache.recommendation_cached?(algorithm: self.algorithm, query: self.query, model_start: self.model_start, model_end: self.model_end, max_size: self.max_size) end |
#to_h ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/evoc/scenario.rb', line 58 def to_h fields = %w(case_id granularity scenario_id tx_id tx_index tx_size query_size query_percentage expected_outcome_size model_size model_hours model_age max_size algorithm aggregator measures stats) hash = Hash.new fields.each do |key| value = self.method(key).call if value.is_a?(Array) hash[key] = value.join(',') elsif value.is_a?(Hash) hash.merge!(value) else hash[key] = value end end return hash end |
#to_s ⇒ Object
82 83 84 |
# File 'lib/evoc/scenario.rb', line 82 def to_s self.opts end |
#tx_size ⇒ Integer
Returns the size of this scenarios transaction.
235 236 237 |
# File 'lib/evoc/scenario.rb', line 235 def tx_size self.tx.size end |