Class: OmfEc::Prototype
- Inherits:
-
Object
- Object
- OmfEc::Prototype
- Defined in:
- lib/omf_ec/prototype.rb
Overview
This class describes a prototype which can be used to access applications within an Experiment.
Constant Summary collapse
- @@prototypes =
Hash.new
- @@bindStruct =
Struct.new(:name)
Instance Attribute Summary collapse
-
#applications ⇒ Object
readonly
Applications used on the prototype.
-
#description ⇒ Object
writeonly
Description of the prototype.
-
#name ⇒ Object
writeonly
Name of prototype.
-
#parameters ⇒ Object
readonly
Parameters of the prototype.
-
#uri ⇒ Object
writeonly
Global reference.
-
#version ⇒ Object
readonly
Version of prototype.
Class Method Summary collapse
-
.[](uri) ⇒ Object
Return a known Prototype instance.
-
.create(uri, name = uri) ⇒ Object
Create a new Prototype instance.
-
.reset ⇒ Object
Reset all class state.
Instance Method Summary collapse
-
#addApplication(name, location = nil, &block) ⇒ Object
Add an Application which should be installed on this prototype.
-
#addPrototype(name, param) ⇒ Object
Add a nested Prototype which should be instantiated when this Prototype is instantiated.
-
#bindProperty(name) ⇒ Object
Returns an object which maintains the connection to a a local property of this Prototype.
-
#defProperty(id, description, default = nil) ⇒ Object
Define a property for this prototype.
-
#initialize(uri, name = uri) ⇒ Prototype
constructor
Create a new Prototype instance.
-
#instantiate(group, bindings) ⇒ Object
Instantiate this prototype for a particular node set.
-
#setVersion(major = 0, minor = 0, revision = 0) ⇒ Object
Set the version number for this Prototype.
Constructor Details
#initialize(uri, name = uri) ⇒ Prototype
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 '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
#applications ⇒ Object (readonly)
Applications used on the prototype
74 75 76 |
# File 'lib/omf_ec/prototype.rb', line 74 def applications @applications end |
#description=(value) ⇒ Object (writeonly)
Description of the prototype
68 69 70 |
# File 'lib/omf_ec/prototype.rb', line 68 def description=(value) @description = value end |
#name=(value) ⇒ Object (writeonly)
Name of prototype
62 63 64 |
# File 'lib/omf_ec/prototype.rb', line 62 def name=(value) @name = value end |
#parameters ⇒ Object (readonly)
Parameters of the prototype
71 72 73 |
# File 'lib/omf_ec/prototype.rb', line 71 def parameters @parameters end |
#uri=(value) ⇒ Object (writeonly)
Global reference
59 60 61 |
# File 'lib/omf_ec/prototype.rb', line 59 def uri=(value) @uri = value end |
#version ⇒ Object (readonly)
Version of prototype
65 66 67 |
# File 'lib/omf_ec/prototype.rb', line 65 def version @version end |
Class Method Details
.[](uri) ⇒ Object
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 '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 |
.create(uri, name = uri) ⇒ Object
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 'lib/omf_ec/prototype.rb', line 46 def self.create(uri, name = uri) return Prototype.new(uri, name) end |
.reset ⇒ Object
Reset all class state. Specifically forget all prototype declarations. This is primarily used by the test suite.
53 54 55 56 |
# File 'lib/omf_ec/prototype.rb', line 53 def self.reset() @@prototypes = Hash.new @@bindStruct = Struct.new(:name) end |
Instance Method Details
#addApplication(name, location = nil, &block) ⇒ Object
Add an Application which should be installed on this prototype.
228 229 230 |
# File 'lib/omf_ec/prototype.rb', line 228 def addApplication(name, location = nil, &block) @applications << [name, location, block] end |
#addPrototype(name, param) ⇒ Object
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 '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 |
#bindProperty(name) ⇒ Object
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 'lib/omf_ec/prototype.rb', line 198 def bindProperty(name) @@bindStruct.new(name) end |
#defProperty(id, description, default = nil) ⇒ Object
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 '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 |
#instantiate(group, bindings) ⇒ Object
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 '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 |
#setVersion(major = 0, minor = 0, revision = 0) ⇒ Object
Set the version number for this Prototype
-
major = major version number
-
minor = minor version number
-
revision = revision version number
208 209 210 211 |
# File '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 |