Class: OmfRcShm::App::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/omf_rc_shm/app/definition.rb

Overview

Application Definition used in experiment script

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Definition

Returns a new instance of Definition.

Parameters:

  • name (String)

    name of the application to define



15
16
17
18
# File 'lib/omf_rc_shm/app/definition.rb', line 15

def initialize(name)
  self.name = name
  self.properties = Hashie::Mash.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/omf_rc_shm/app/definition.rb', line 57

def method_missing(method_name, *args)
  k = method_name.to_sym
  return @properties[k] if @properties.key?(k)
  m = method_name.to_s.match(/(.*?)([=]?)$/)
  if m[2] == '='
    @properties[m[1].to_sym] = args.first
  else
    super
  end
end

Instance Attribute Details

#nameObject

TODO: eventually this call would mirror all the properties of the App Proxy right now we just have name, binary_path, parameters



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
59
60
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/omf_rc_shm/app/definition.rb', line 8

class Definition

  # TODO: eventually this call would mirror all the properties of the App Proxy
  # right now we just have name, binary_path, parameters
  attr_accessor :name, :properties

  # @param [String] name name of the application to define
  def initialize(name)
    self.name = name
    self.properties = Hashie::Mash.new
  end

  # Add new parameter(s) to this Application Definition
  #
  # @param [Hash] params a hash with the parameters to add
  #
  def define_parameter(params)
    @properties[:parameters] = Hashie::Mash.new unless @properties.key?(:parameters)
    if params.kind_of? Hash
      @properties[:parameters].merge!(params)
    else
      error "Cannot define parameter for app '#{self.name}'! Parameter "+
        "not passed as a Hash ('#{params.inspect}')"
    end
  end

  def define_measurement_point(mp)
    @properties[:oml] = Hashie::Mash.new unless @properties.key?(:oml)
    if mp.kind_of? Hash
      @properties[:oml][:available_mps] = Array.new unless @properties[:oml].key?(:available_mps)
      @properties[:oml][:available_mps] << mp
    else
      error "Cannot define Measurement Point for app '#{self.name}'! MP "+
        "not passed as a Hash ('#{mp.inspect}')"
    end
  end

  warn_removed :version

  def path=(arg)
    @properties[:binary_path] = arg
    warn_deprecation :path=, :binary_path=
  end

  def shortDescription=(arg)
    @properties[:description] = arg
    warn_deprecation :shortDescription=, :description=
  end

  def method_missing(method_name, *args)
    k = method_name.to_sym
    return @properties[k] if @properties.key?(k)
    m = method_name.to_s.match(/(.*?)([=]?)$/)
    if m[2] == '='
      @properties[m[1].to_sym] = args.first
    else
      super
    end
  end

  # The following are OEDL 5 methods

  # Add a new parameter to this Application Definition.
  # This method is for backward compatibility with previous OEDL 5.
  #
  # @param [String] name name of the property to define (mandatory)
  # @param [String] description description of this property; oml2-scaffold uses this for the help message (popt: descrip)
  # @param [String] parameter command-line parameter to introduce this property, including dashes if needed (can be nil)
  # @param [Hash] options list of options associated with this property
  # @option options [String] :type type of the property: :integer, :string and :boolean are supported; oml2-scaffold extends this with :int and :double (popt: argInfo)
  # @option options [Boolean] :dynamic true if the property can be changed at run-time
  # @option options [Fixnum] :order used to order properties when creating the command line
  #
  # The OML code-generation tool, oml2-scaffold extends the range of
  # options supported in the options hash to support generation of
  # popt(3) command line parsing code. As for the parameters, depending
  # on the number of dashes (two/one) in parameter, it is used as the
  # longName/shortName for popt(3), otherwise the former defaults to
  # name, and the latter defaults to either :mnemonic or nothing.
  #
  # @option options [String] :mnemonic one-letter mnemonic for the option (also returned by poptGetNextOpt as val)
  # @option options [String] :unit unit in which this property is expressed; oml2-scaffold uses this for the help message (popt: argDescrip)
  # @option options [String] :default default value if argument unspecified (optional; defaults to something sane for the :type)
  # @option options [String] :var_name name of the C variable for popt(3) to store the property value into (optional; popt: arg; defaults to name, after sanitisation)
  #
  # @see http://oml.mytestbed.net/doc/oml/latest/oml2-scaffold.1.html
  # @see http://linux.die.net/man/3/popt
  #
  def defProperty(name = :mandatory, description = nil, parameter = nil, options = {})
    opts = {:description => description, :cmd => parameter}
    # Map old OMF5 types to OMF6
    options[:type] = 'Numeric' if options[:type] == :integer
    options[:type] = 'String' if options[:type] == :string
    options[:type] = 'Boolean' if options[:type] == :boolean
    opts = opts.merge(options)
    define_parameter(Hash[name,opts])
  end

  # Define metrics to measure
  #
  # @param [String] name of the metric
  # @param [Symbol] type of the metric data. For all supporting metric data types, refers to http://oml.mytestbed.net/doc/oml/latest/oml2-scaffold.1.html#_mp_defmetric_name_type
  # @param [Hash] opts additional options
  #
  # @option opts [String] :unit unit of measure of the metric
  # @option opts [String] :description of the metric
  # @option opts [Float] :precision precision of the metric value
  # @option opts [Range] :range value range of the metric
  #
  # @example OEDL
  #   app.defMeasurement("power") do |mp|
  #     mp.defMetric('power', :double, :unit => "W", :precision => 0.1, :description => 'Power')
  #   end
  def defMetric(name,type, opts = {})
    # the third parameter used to be a description string
    opts = {:description => opts} if opts.class!=Hash
    @fields << {:field => name, :type => type}.merge(opts)
  end

  # XXX: This should be provided by the omf-oml glue.
  def defMeasurement(name,&block)
    mp = {:mp => name, :fields => []}
    @fields = []
    # call the block with ourserlves to process its 'defMetric' statements
    block.call(self) if block
    @fields.each { |f| mp[:fields] << f }
    define_measurement_point(mp)
  end
end

#propertiesObject

TODO: eventually this call would mirror all the properties of the App Proxy right now we just have name, binary_path, parameters



12
13
14
# File 'lib/omf_rc_shm/app/definition.rb', line 12

def properties
  @properties
end

Instance Method Details

#define_measurement_point(mp) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/omf_rc_shm/app/definition.rb', line 34

def define_measurement_point(mp)
  @properties[:oml] = Hashie::Mash.new unless @properties.key?(:oml)
  if mp.kind_of? Hash
    @properties[:oml][:available_mps] = Array.new unless @properties[:oml].key?(:available_mps)
    @properties[:oml][:available_mps] << mp
  else
    error "Cannot define Measurement Point for app '#{self.name}'! MP "+
      "not passed as a Hash ('#{mp.inspect}')"
  end
end

#define_parameter(params) ⇒ Object

Add new parameter(s) to this Application Definition

Parameters:

  • params (Hash)

    a hash with the parameters to add



24
25
26
27
28
29
30
31
32
# File 'lib/omf_rc_shm/app/definition.rb', line 24

def define_parameter(params)
  @properties[:parameters] = Hashie::Mash.new unless @properties.key?(:parameters)
  if params.kind_of? Hash
    @properties[:parameters].merge!(params)
  else
    error "Cannot define parameter for app '#{self.name}'! Parameter "+
      "not passed as a Hash ('#{params.inspect}')"
  end
end

#defMeasurement(name, &block) ⇒ Object

XXX: This should be provided by the omf-oml glue.



128
129
130
131
132
133
134
135
# File 'lib/omf_rc_shm/app/definition.rb', line 128

def defMeasurement(name,&block)
  mp = {:mp => name, :fields => []}
  @fields = []
  # call the block with ourserlves to process its 'defMetric' statements
  block.call(self) if block
  @fields.each { |f| mp[:fields] << f }
  define_measurement_point(mp)
end

#defMetric(name, type, opts = {}) ⇒ Object

Define metrics to measure

Examples:

OEDL

app.defMeasurement("power") do |mp|
  mp.defMetric('power', :double, :unit => "W", :precision => 0.1, :description => 'Power')
end

Parameters:

Options Hash (opts):

  • :unit (String)

    unit of measure of the metric

  • :description (String)

    of the metric

  • :precision (Float)

    precision of the metric value

  • :range (Range)

    value range of the metric



121
122
123
124
125
# File 'lib/omf_rc_shm/app/definition.rb', line 121

def defMetric(name,type, opts = {})
  # the third parameter used to be a description string
  opts = {:description => opts} if opts.class!=Hash
  @fields << {:field => name, :type => type}.merge(opts)
end

#defProperty(name = :mandatory, description = nil, parameter = nil, options = {}) ⇒ Object

Add a new parameter to this Application Definition. This method is for backward compatibility with previous OEDL 5.

The OML code-generation tool, oml2-scaffold extends the range of options supported in the options hash to support generation of popt(3) command line parsing code. As for the parameters, depending on the number of dashes (two/one) in parameter, it is used as the longName/shortName for popt(3), otherwise the former defaults to name, and the latter defaults to either :mnemonic or nothing.

Parameters:

  • name (String) (defaults to: :mandatory)

    name of the property to define (mandatory)

  • description (String) (defaults to: nil)

    description of this property; oml2-scaffold uses this for the help message (popt: descrip)

  • parameter (String) (defaults to: nil)

    command-line parameter to introduce this property, including dashes if needed (can be nil)

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

    list of options associated with this property

Options Hash (options):

  • :type (String)

    type of the property: :integer, :string and :boolean are supported; oml2-scaffold extends this with :int and :double (popt: argInfo)

  • :dynamic (Boolean)

    true if the property can be changed at run-time

  • :order (Fixnum)

    used to order properties when creating the command line

  • :mnemonic (String)

    one-letter mnemonic for the option (also returned by poptGetNextOpt as val)

  • :unit (String)

    unit in which this property is expressed; oml2-scaffold uses this for the help message (popt: argDescrip)

  • :default (String)

    default value if argument unspecified (optional; defaults to something sane for the :type)

  • :var_name (String)

    name of the C variable for popt(3) to store the property value into (optional; popt: arg; defaults to name, after sanitisation)

See Also:



96
97
98
99
100
101
102
103
104
# File 'lib/omf_rc_shm/app/definition.rb', line 96

def defProperty(name = :mandatory, description = nil, parameter = nil, options = {})
  opts = {:description => description, :cmd => parameter}
  # Map old OMF5 types to OMF6
  options[:type] = 'Numeric' if options[:type] == :integer
  options[:type] = 'String' if options[:type] == :string
  options[:type] = 'Boolean' if options[:type] == :boolean
  opts = opts.merge(options)
  define_parameter(Hash[name,opts])
end

#path=(arg) ⇒ Object



47
48
49
50
# File 'lib/omf_rc_shm/app/definition.rb', line 47

def path=(arg)
  @properties[:binary_path] = arg
  warn_deprecation :path=, :binary_path=
end

#shortDescription=(arg) ⇒ Object



52
53
54
55
# File 'lib/omf_rc_shm/app/definition.rb', line 52

def shortDescription=(arg)
  @properties[:description] = arg
  warn_deprecation :shortDescription=, :description=
end