Module: Roby::Coordination::Models::Base
- Includes:
- MetaRuby::ModelAsClass, Arguments
- Included in:
- Actions
- Defined in:
- lib/roby/coordination/models/base.rb
Overview
Model part of Base
Instance Attribute Summary collapse
-
#name ⇒ String?
Name of this coordination object.
-
#root(*new_root) ⇒ Root
Gets or sets the root task model.
Instance Method Summary collapse
-
#find_event(name) ⇒ Object
Gives direct access to the root’s events.
-
#find_task_by_name(name) ⇒ Object
Returns the task for the given name, if found, nil otherwise.
-
#map_tasks(mapping) ⇒ Object
private
Transform the model by exchanging tasks.
- #method_missing(m, *args, **kw, &block) ⇒ Object
-
#parse_names(suffixes) ⇒ Object
Assigns names to tasks based on the name of the local variables they are assigned to.
- #respond_to_missing?(m, include_private) ⇒ Boolean
-
#setup_submodel(subclass, root: Roby::Task, **options) ⇒ Model<StateMachine>
Creates a new execution context model as a submodel of self.
-
#task(object, task_model = Roby::Task) ⇒ Object
Creates a state from an object.
-
#task_model ⇒ Model<Roby::Task>
The task model this execution context is attached to.
-
#use_fault_response_table(table_model, arguments = {}) ⇒ Object
Declare that this fault response table should be active as long as this coordination model is.
-
#used_fault_response_table ⇒ Array<(FaultResponseTable,Hash)>
The set of fault response tables that should be active when this coordination model is.
- #validate_event(object) ⇒ Object
- #validate_or_create_task(task) ⇒ Object
- #validate_task(object) ⇒ Object
Methods included from Arguments
#argument, #validate_arguments
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, **kw, &block) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/roby/coordination/models/base.rb', line 136 def method_missing(m, *args, **kw, &block) if has_argument?(m) unless args.empty? && kw.empty? raise ArgumentError, "expected zero arguments to #{m}, "\ "got #{args.size} positional arguments (#{args}) and "\ "#{kw.size} keyword arguments (#{kw})" end Variable.new(m) elsif m =~ /(.*)(?:_event$|_child$)/ root.send(m, *args, &block) else super end end |
Instance Attribute Details
#name ⇒ String?
Name of this coordination object
For debugging purposes. It is usually set by the enclosing context
16 17 18 |
# File 'lib/roby/coordination/models/base.rb', line 16 def name @name end |
#root(*new_root) ⇒ Root
Gets or sets the root task model
22 23 24 25 26 27 28 29 |
# File 'lib/roby/coordination/models/base.rb', line 22 def root(*new_root) if !new_root.empty? @root = Root.new(new_root.first, self) elsif @root then @root elsif superclass.respond_to?(:root) @root = superclass.root.rebind(self) end end |
Instance Method Details
#find_event(name) ⇒ Object
Gives direct access to the root’s events
This is needed to be able to use a coordination model as model for a coordination task, which in turn gives access to e.g. states in an action state machine
45 46 47 |
# File 'lib/roby/coordination/models/base.rb', line 45 def find_event(name) root.find_event(name) end |
#find_task_by_name(name) ⇒ Object
Returns the task for the given name, if found, nil otherwise
52 53 54 55 56 |
# File 'lib/roby/coordination/models/base.rb', line 52 def find_task_by_name(name) tasks.find do |m| m.name == name.to_s end end |
#map_tasks(mapping) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Transform the model by exchanging tasks
99 100 101 102 103 104 105 106 |
# File 'lib/roby/coordination/models/base.rb', line 99 def map_tasks(mapping) @root = mapping.fetch(root) @tasks = tasks.map do |t| new_task = mapping.fetch(t) new_task.map_tasks(mapping) new_task end end |
#parse_names(suffixes) ⇒ Object
Assigns names to tasks based on the name of the local variables they are assigned to
This must be called by methods that are themselves called during parsing
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/roby/coordination/models/base.rb', line 115 def parse_names(suffixes) definition_context = binding.callers.find { |b| b.frame_type == :block } return unless definition_context # Assign names to tasks using the local variables definition_context.local_variables.each do |var| object = definition_context.local_variable_get(var) suffixes.each do |klass, suffix| if object.kind_of?(klass) object.name ||= "#{var}#{suffix}" end end end end |
#respond_to_missing?(m, include_private) ⇒ Boolean
130 131 132 133 134 |
# File 'lib/roby/coordination/models/base.rb', line 130 def respond_to_missing?(m, include_private) has_argument?(m) || (m =~ /_event$|_child$/ && root.respond_to?(m)) || super end |
#setup_submodel(subclass, root: Roby::Task, **options) ⇒ Model<StateMachine>
Creates a new execution context model as a submodel of self
73 74 75 76 |
# File 'lib/roby/coordination/models/base.rb', line 73 def setup_submodel(subclass, root: Roby::Task, **) subclass.root(root) super end |
#task(object, task_model = Roby::Task) ⇒ Object
Creates a state from an object
60 |
# File 'lib/roby/coordination/models/base.rb', line 60 inherited_attribute(:task, :tasks) { [] } |
#task_model ⇒ Model<Roby::Task>
Returns the task model this execution context is attached to.
36 37 38 |
# File 'lib/roby/coordination/models/base.rb', line 36 def task_model root.model end |
#use_fault_response_table(table_model, arguments = {}) ⇒ Object
Declare that this fault response table should be active as long as this coordination model is
185 186 187 188 |
# File 'lib/roby/coordination/models/base.rb', line 185 def use_fault_response_table(table_model, arguments = {}) arguments = table_model.validate_arguments(arguments) used_fault_response_tables << [table_model, arguments] end |
#used_fault_response_table ⇒ Array<(FaultResponseTable,Hash)>
The set of fault response tables that should be active when this coordination model is
65 |
# File 'lib/roby/coordination/models/base.rb', line 65 inherited_attribute(:used_fault_response_table, :used_fault_response_tables) { [] } |
#validate_event(object) ⇒ Object
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/roby/coordination/models/base.rb', line 172 def validate_event(object) unless object.kind_of?(Coordination::Models::Event) raise ArgumentError, "expected an action-event object, got #{object}. "\ "Acceptable events need to be created from e.g. "\ "actions by calling #task(action).my_event" end object end |
#validate_or_create_task(task) ⇒ Object
164 165 166 167 168 169 170 |
# File 'lib/roby/coordination/models/base.rb', line 164 def validate_or_create_task(task) if !task.kind_of?(Coordination::Models::Task) task(task) else task end end |
#validate_task(object) ⇒ Object
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/roby/coordination/models/base.rb', line 153 def validate_task(object) unless object.kind_of?(Coordination::Models::Task) raise ArgumentError, "expected a state object, got #{object}. States "\ "need to be created from e.g. actions by calling "\ "#state before they can be used in the state machine" end object end |