Class: OmfEc::Context::GroupContext

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

Overview

Holds group configuration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ GroupContext

Returns a new instance of GroupContext.



13
14
15
16
17
# File 'lib/omf_ec/context/group_context.rb', line 13

def initialize(opts)
  self.group = opts.delete(:group)
  self.guard = opts
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Calling standard methods or assignments will simply trigger sending a FRCP message

Examples:

OEDL

# Will send FRCP CONFIGURE message
g.resources[type: 'engine'].throttle = 0

# Will send FRCP REQUEST message
g.resources[type: 'engine'].rpm

# Will send FRCP RELEASE message
g.resources[type: 'engine'].release


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/omf_ec/context/group_context.rb', line 45

def method_missing(name, *args, &block)
  if name =~ /(.+)=/
    self.operation = :configure
    name = $1
  elsif name =~ /release/
    self.operation = :release
  else
    self.operation = :request
  end
  send_message(name, *args, &block)
end

Instance Attribute Details

#groupObject

Returns the value of attribute group.



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

def group
  @group
end

#guardObject

Returns the value of attribute guard.



10
11
12
# File 'lib/omf_ec/context/group_context.rb', line 10

def guard
  @guard
end

#operationObject

Returns the value of attribute operation.



11
12
13
# File 'lib/omf_ec/context/group_context.rb', line 11

def operation
  @operation
end

Instance Method Details

#[](opts = {}) ⇒ Object

Supports OEDL 6 syntax [] for setting FRCP guard

Examples:

Reduce throttle to zero for all resources of type ‘engine’ from group ‘A’


group('A') do |g|
  g.resources[type: 'engine'].throttle = 0
end

Parameters:

  • opts (Hash) (defaults to: {})

    option hash which sets constraints



29
30
31
32
# File 'lib/omf_ec/context/group_context.rb', line 29

def [](opts = {})
  self.guard.merge!(opts)
  self
end

#send_message(name, value = nil, &block) ⇒ Object

Send FRCP message

Parameters:

  • name (String)

    of the property

  • value (Object) (defaults to: nil)

    of the property, for configuring



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/omf_ec/context/group_context.rb', line 61

def send_message(name, value = nil, &block)
  if self.guard[:type]
    topic = self.group.resource_topic(self.guard[:type])
  else
    topic = self.group.topic
  end

  if topic.nil?
    if self.guard[:type]
      warn "Group '#{self.group.name}' has NO resources of type '#{self.guard[:type]}' ready. Could not send message."
    else
      warn "Group topic '#{self.group.name}' NOT subscribed. Could not send message."
    end
    return
  end


  case self.operation
  when :configure
    topic.configure({ name => value },
                    { guard: self.guard, assert: OmfEc.experiment.assertion })
  when :request
    topic.request([:uid, :hrn, name],
                  { guard: self.guard, assert: OmfEc.experiment.assertion })
  when :release
    topics_to_release = OmfEc.experiment.state.find_all do |res_state|
      all_equal(self.guard.keys) do |k|
        res_state[k] == self.guard[k]
      end
    end

    topics_to_release.each do |res_state|
      OmfEc.subscribe_and_monitor(res_state.uid) do |child_topic|
        OmfEc.subscribe_and_monitor(self.group.id) do |group_topic|
          group_topic.release(child_topic, { assert: OmfEc.experiment.assertion }) if child_topic
        end
      end
    end
  end
end