Class: OmfEc::Context::AppContext

Inherits:
Object
  • Object
show all
Defined in:
omf_ec/lib/omf_ec/context/app_context.rb

Overview

Holds application configuration

Constant Summary

@@context_count =

Keep track of contexts for each app, i.e. multiple contexts can share the same app def. This happens for example when a group can have the same applications added to it many times, but with different parameter values for each. Thus we need to distinguish these different context

Hash.new

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (AppContext) initialize(name, location = nil, group)

Returns a new instance of AppContext



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'omf_ec/lib/omf_ec/context/app_context.rb', line 17

def initialize(name, location = nil, group)
  load_oedl(location) unless location.nil?
  if OmfEc.experiment.app_definitions.key?(name)
    self.app_def = OmfEc.experiment.app_definitions[name]
    self.param_values = Hash.new
    self.oml_collections = Array.new
    @@context_count[name] = 0 unless @@context_count.key?(name)
    id = @@context_count[name]
    @@context_count[name] += 1
    self.name = "#{name}_cxt_#{id}"
    @group = group
    self
  else
    raise RuntimeError, "Cannot create context for unknown application '#{name}'"
  end
end

Instance Attribute Details

- (Object) app_def

Returns the value of attribute app_def



9
10
11
# File 'omf_ec/lib/omf_ec/context/app_context.rb', line 9

def app_def
  @app_def
end

- (Object) name

Returns the value of attribute name



9
10
11
# File 'omf_ec/lib/omf_ec/context/app_context.rb', line 9

def name
  @name
end

- (Object) oml_collections

Returns the value of attribute oml_collections



9
10
11
# File 'omf_ec/lib/omf_ec/context/app_context.rb', line 9

def oml_collections
  @oml_collections
end

- (Object) param_values

Returns the value of attribute param_values



9
10
11
# File 'omf_ec/lib/omf_ec/context/app_context.rb', line 9

def param_values
  @param_values
end

- (Object) proto_props

Returns the value of attribute proto_props



9
10
11
# File 'omf_ec/lib/omf_ec/context/app_context.rb', line 9

def proto_props
  @proto_props
end

Instance Method Details

- (Object) bindProperty(prop_name, prop_ref = prop_name)



60
61
62
63
# File 'omf_ec/lib/omf_ec/context/app_context.rb', line 60

def bindProperty(prop_name, prop_ref = prop_name)
  @proto_props ||= Hashie::Mash.new
  @proto_props[prop_name] = prop_ref
end

- (Object) measure(mp, opts, &block)

For now this follows v5.4 syntax… We have not yet finalised an OML syntax inside OEDL for v6 TODO: v6 currently does not support OML filters. Formerly in v5.x, these filters were defined in an optional block.



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'omf_ec/lib/omf_ec/context/app_context.rb', line 69

def measure(mp, opts, &block)
  collect_point = opts.delete(:collect)
  collect_point ||= OmfEc.experiment.oml_uri
  if collect_point.nil?
    warn "No OML URI configured for measurement collection! "+
         "(see option 'oml_uri'). Disabling OML Collection for '#{mp}'."
    return
  end
  stream = { :mp => mp , :filters => [] }.merge(opts)
  index = @oml_collections.find_index { |c| c[:url] == collect_point }
  @oml_collections << {:url => collect_point, :streams => [stream] } if index.nil?
  @oml_collections[index][:streams] << stream unless index.nil?
end

- (Object) mp_table_names



106
107
108
109
110
111
112
113
# File 'omf_ec/lib/omf_ec/context/app_context.rb', line 106

def mp_table_names
  {}.tap do |m_t_n|
    @oml_collections.map { |v| v[:streams] }.flatten.each do |s|
      mp = s[:mp].to_s
      m_t_n[mp] = "#{self.app_def.name}_#{mp}"
    end
  end
end

- (Object) properties



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'omf_ec/lib/omf_ec/context/app_context.rb', line 83

def properties
  # deep copy the properties from the app definition
  original = Marshal.load(Marshal.dump(app_def.properties))
  # now build the properties for this context
  # - use the properties from app definition as the base
  # - if this context's param_values has a property which also exists in
  #   the app def and if that property has an assigned value, then
  #   use that value for the properties of this context
  p = original.merge({type: 'application', state: 'created'})
  @param_values.each do |k,v|
    if p[:parameters].key?(k)
      p[:parameters][k][:value] = v.kind_of?(OmfEc::ExperimentProperty) ? v.value : v
    end
  end
  if @oml_collections.size > 0
    p[:use_oml] = true
    p[:oml][:id] = @name
    p[:oml][:experiment] = OmfEc.experiment.id
    p[:oml][:collection] = @oml_collections
  end
  p
end

- (Object) setProperty(key, property_value)



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'omf_ec/lib/omf_ec/context/app_context.rb', line 34

def setProperty(key, property_value)
  app_def_param = app_def.properties.parameters
  raise OEDLUnknownProperty.new(key, "Unknown parameter '#{key}' for application "+
    "definition '#{app_def.name}'") if app_def_param.nil? || !app_def_param.key?(key)
  if property_value.kind_of?(OmfEc::ExperimentProperty)
    @param_values[key] = property_value.value
    # if this app parameter has its dynamic attribute set to true, then
    # we register a callback block to the ExperimentProperty, which will
    # be called each time the property changes its value
    if app_def_param[key][:dynamic]
      info "Binding dynamic parameter '#{key}' to the property '#{property_value.name}'"
      property_value.on_change do |new_value|
        info "Updating dynamic app parameter '#{key}' with value: '#{new_value}'"
        OmfEc.subscribe_and_monitor(@group.resource_group(:application)) do |topic|
          p = properties
          p[:parameters][key.to_sym][:value] = property_value.value
          topic.configure(p, { guard: { hrn: @name}, assert: OmfEc.experiment.assertion } )
        end
      end
    end
  else
    @param_values[key] = property_value
  end
  self
end