Class: Evoc::Scenario

Inherits:
Object
  • Object
show all
Includes:
Comparable, Logging
Defined in:
lib/evoc/scenario.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#aggregatorObject

Returns the value of attribute aggregator.



5
6
7
# File 'lib/evoc/scenario.rb', line 5

def aggregator
  @aggregator
end

#algorithmObject

Returns the value of attribute algorithm.



5
6
7
# File 'lib/evoc/scenario.rb', line 5

def algorithm
  @algorithm
end

#case_idObject

Returns the value of attribute case_id.



5
6
7
# File 'lib/evoc/scenario.rb', line 5

def case_id
  @case_id
end

#granularityObject

Returns the value of attribute granularity.



5
6
7
# File 'lib/evoc/scenario.rb', line 5

def granularity
  @granularity
end

#max_sizeObject

Returns the value of attribute max_size.



5
6
7
# File 'lib/evoc/scenario.rb', line 5

def max_size
  @max_size
end

#measuresObject

Returns the value of attribute measures.



5
6
7
# File 'lib/evoc/scenario.rb', line 5

def measures
  @measures
end

#model_ageObject

Returns the value of attribute model_age.



5
6
7
# File 'lib/evoc/scenario.rb', line 5

def model_age
  @model_age
end

#model_sizeObject

Returns the value of attribute model_size.



5
6
7
# File 'lib/evoc/scenario.rb', line 5

def model_size
  @model_size
end

#optsObject

Returns the value of attribute opts.



5
6
7
# File 'lib/evoc/scenario.rb', line 5

def opts
  @opts
end

#permutationObject

Returns the value of attribute permutation.



5
6
7
# File 'lib/evoc/scenario.rb', line 5

def permutation
  @permutation
end

#queryObject

Returns the value of attribute query.



5
6
7
# File 'lib/evoc/scenario.rb', line 5

def query
  @query
end

#scenario_idObject

Returns the value of attribute scenario_id.



5
6
7
# File 'lib/evoc/scenario.rb', line 5

def scenario_id
  @scenario_id
end

#statsObject

Returns the value of attribute stats.



5
6
7
# File 'lib/evoc/scenario.rb', line 5

def stats
  @stats
end

#txEvoc::Tx

Returns the transaction of this scenario.

Returns:

  • (Evoc::Tx)

    the transaction of this scenario



93
94
95
# File 'lib/evoc/scenario.rb', line 93

def tx
  @tx
end

#tx_idObject

Returns the value of attribute tx_id.



5
6
7
# File 'lib/evoc/scenario.rb', line 5

def tx_id
  @tx_id
end

#tx_indexString

Returns the history index of this scenarios transaction.

Returns:

  • (String)

    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_outcomeArray

Returns the list of expected items.

Returns:

  • (Array)

    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_sizeInteger

Returns the size of the expected outcome.

Returns:

  • (Integer)

    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_endInteger

Returns the index in the history where the model ends.

Returns:

  • (Integer)

    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_hoursInteger

Returns the time between the first and last transaction in the model.

Returns:

  • (Integer)

    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_percentageFloat

Returns the percentage of the whole history that is used in the model.

Returns:

  • (Float)

    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_startInteger

Returns the index in the history where the model starts.

Returns:

  • (Integer)

    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_percentageObject

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_sizeInteger

Returns the size of the query.

Returns:

  • (Integer)

    the size of the query



103
104
105
# File 'lib/evoc/scenario.rb', line 103

def query_size
  self.query.size
end

#recommendation?Boolean

Returns:

  • (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_hObject



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_sObject



82
83
84
# File 'lib/evoc/scenario.rb', line 82

def to_s
  self.opts
end

#tx_sizeInteger

Returns the size of this scenarios transaction.

Returns:

  • (Integer)

    the size of this scenarios transaction



235
236
237
# File 'lib/evoc/scenario.rb', line 235

def tx_size
  self.tx.size
end