Class: OmfEc::Prototype

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

Overview

This class describes a prototype which can be used to access applications within an Experiment.

Constant Summary

@@prototypes =
Hash.new
@@bindStruct =
Struct.new(:name)

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Prototype) initialize(uri, name = uri)

Create a new Prototype instance.

  • uri = an URI identifying the new Prototype

  • name = an optional name for this Prototype (default = 'uri')



81
82
83
84
85
86
87
88
89
90
91
92
# File 'omf_ec/lib/omf_ec/prototype.rb', line 81

def initialize(uri, name = uri)
  if @@prototypes.has_key? uri
    raise StandardError, "Prototype with name '#{uri}' already exists."
  end
  @@prototypes[uri] = self

  @uri = uri
  @name = name
  @properties = Hashie::Mash.new
  @incPrototypes = Hash.new
  @applications = Array.new
end

Instance Attribute Details

- (Object) applications (readonly)

Applications used on the prototype



74
75
76
# File 'omf_ec/lib/omf_ec/prototype.rb', line 74

def applications
  @applications
end

- (Object) description=(value) (writeonly)

Description of the prototype



68
69
70
# File 'omf_ec/lib/omf_ec/prototype.rb', line 68

def description=(value)
  @description = value
end

- (Object) name=(value) (writeonly)

Name of prototype



62
63
64
# File 'omf_ec/lib/omf_ec/prototype.rb', line 62

def name=(value)
  @name = value
end

- (Object) parameters (readonly)

Parameters of the prototype



71
72
73
# File 'omf_ec/lib/omf_ec/prototype.rb', line 71

def parameters
  @parameters
end

- (Object) uri=(value) (writeonly)

Global reference



59
60
61
# File 'omf_ec/lib/omf_ec/prototype.rb', line 59

def uri=(value)
  @uri = value
end

- (Object) version (readonly)

Version of prototype



65
66
67
# File 'omf_ec/lib/omf_ec/prototype.rb', line 65

def version
  @version
end

Class Method Details

+ (Object) [](uri)

Return a known Prototype instance.

Return

the uri 'URI' identifying the Prototype



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'omf_ec/lib/omf_ec/prototype.rb', line 21

def self.[](uri)
  proto = @@prototypes[uri]
  if proto == nil
    debug "Loading prototype '#{uri}'"
    str, type = OConfig.load(uri, true)
    #MObject.debug('Prototype: ', 'str: "', str, '".')
    if type == "text/xml"
      # proto = Prototype.from_xml(str.to_xml???)
    elsif type == "text/ruby"
      # 'str' has already been evaluated
      proto = @@prototypes[uri]
    end
    if proto == nil
      raise "Unknown prototype '#{uri}'."
    end
  end

  proto
end

+ (Object) create(uri, name = uri)

Create a new Prototype instance.

  • uri = an URI identifying the new Prototype

  • name = an optional name for this Prototype (default = 'uri')



46
47
48
# File 'omf_ec/lib/omf_ec/prototype.rb', line 46

def self.create(uri, name = uri)
  return Prototype.new(uri, name)
end

+ (Object) reset

Reset all class state. Specifically forget all prototype declarations. This is primarily used by the test suite.



53
54
55
56
# File 'omf_ec/lib/omf_ec/prototype.rb', line 53

def self.reset()
  @@prototypes = Hash.new
  @@bindStruct = Struct.new(:name)
end

Instance Method Details

- (Object) addApplication(name, location = nil, &block)

Add an Application which should be installed on this prototype.



228
229
230
# File 'omf_ec/lib/omf_ec/prototype.rb', line 228

def addApplication(name, location = nil, &block)
  @applications << [name, location, block]
end

- (Object) addPrototype(name, param)

Add a nested Prototype which should be instantiated when this Prototype is instantiated.

  • name = Name used for reference

  • param = Hash of parameter bindings



219
220
221
222
223
224
# File 'omf_ec/lib/omf_ec/prototype.rb', line 219

def addPrototype(name, param)
  if @incPrototypes.has_key? name
    raise "Prototype already has a prototype '" + name + "'."
  end
  @incPrototypes[name] = param
end

- (Object) bindProperty(name)

Returns an object which maintains the connection to a a local property of this Prototype.

  • name = name of the local property

Return

a structure with connection info to the local property



198
199
200
# File 'omf_ec/lib/omf_ec/prototype.rb', line 198

def bindProperty(name)
  @@bindStruct.new(name)
end

- (Object) defProperty(id, description, default = nil)

Define a property for this prototype

  • id = ID of parameter, also used as name

  • description = Description of parameter's purpose

  • default = Default value if not set, makes parameter optional



183
184
185
186
187
188
189
# File 'omf_ec/lib/omf_ec/prototype.rb', line 183

def defProperty(id, description, default = nil)
  if @properties[id] != nil
    raise "Property '" + id + "' already defined."
  end
  param = OmfEc::Parameter.new(id, id, description, default)
  @properties[id] = param
end

- (Object) getBoundValue(name, bindings) (private)

Return the value of a given property 'name' within the context of 'bindings'.

  • name = name of the property to get the value from

  • bindings = context for this property

Return

The value of the property



164
165
166
167
168
169
170
171
172
173
174
# File 'omf_ec/lib/omf_ec/prototype.rb', line 164

def getBoundValue(name, bindings)
  if (bindings.has_key? name)
    return bindings[name]
  else
    # use default
    if (@properties[name] == nil)
      raise "Unknown property #{name}"
    end
    return @properties[name].defaultValue
  end
end

- (Object) instantiate(group, bindings)

Instantiate this prototype for a particular node set.

  • group = Group to configure according to this prototype

  • bindings = a Hash with the bindings for local parameters



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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'omf_ec/lib/omf_ec/prototype.rb', line 99

def instantiate(group, bindings)
  bindings = Hashie::Mash.new(bindings)
  # check if bindings contain unknown properties
  if (diff = bindings.keys - @properties.keys) != []
    raise "Unknown parameters '#{diff.join(', ')}'" \
      + " not in '#{@properties.keys.join(', ')}'."
  end
  # merge bindings with properties declaration
  context = Hash.new
  @properties.each do |name, param|
    #puts "A>> #{name}"
    value = getBoundValue(name, bindings)
    if value != nil
      context[name] = getBoundValue(name, bindings)
    else
      warn "No specific or default value found for Property '#{name}'. Prototype '#{@name}' will not use it!"
    end
  end

  @incPrototypes.each do |name, params|
    proto = Prototype[name]
    p = params.clone
    p.each do |key, val|
      if val.kind_of?(@@bindStruct)
        #puts "B>> #{val.name}:#{key}"
        value = getBoundValue(name, bindings)
        if value != nil
          p[key] = val = value
        else
          warn "No specific or default value found for Property '#{name}'. Prototype '#{@name}' will not use it!"
        end
      end
      #debug "recursive bindings: #{key}=>#{val}"
    end
    proto.instantiate(group, p)
  end

  @applications.each do |app|
    name, location, block = *app

    if block
      block_with_binding_props = proc do |app_ctx|
        block.call(app_ctx)
        app_ctx.proto_props.each do |p_name, p_ref|
          app_ctx.setProperty(p_name, context[p_ref]) if context[p_ref]
        end
      end
    end

    if block_with_binding_props
      group.addApplication(name, location, &block_with_binding_props)
    else
      group.addApplication(name, location)
    end
  end
end

- (Object) setVersion(major = 0, minor = 0, revision = 0)

Set the version number for this Prototype

  • major = major version number

  • minor = minor version number

  • revision = revision version number



208
209
210
211
# File 'omf_ec/lib/omf_ec/prototype.rb', line 208

def setVersion(major = 0, minor = 0, revision = 0)
  #TODO Needs new implementation
  #@currentVersion = MutableVersion.new(major, minor, revision)
end