module Roby::Coordination::Models::Base

Model part of Base

Attributes

name[RW]

Name of this coordination object

For debugging purposes. It is usually set by the enclosing context

@return [String,nil]

root[W]

@deprecated use {#root} instead, as it is a more DSL-like API

Public Instance Methods

find_event(name) click to toggle source

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

# File lib/roby/coordination/models/base.rb, line 41
def find_event(name)
    root.find_event(name)
end
find_task_by_name(name) click to toggle source

Returns the task for the given name, if found, nil otherwise

@return Roby::Coordination::Models::TaskFromAction

# File lib/roby/coordination/models/base.rb, line 48
def find_task_by_name(name)
    tasks.find do |m|
        m.name == name.to_s
    end
end
map_tasks(mapping) click to toggle source

@api private

Transform the model by exchanging tasks

@param [#[]] mapping a mapping that replaces an existing task by a

new one. All tasks must be mapped
# File lib/roby/coordination/models/base.rb, line 95
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
method_missing(m, *args, &block) click to toggle source
Calls superclass method
# File lib/roby/coordination/models/base.rb, line 133
def method_missing(m, *args, &block)
    if has_argument?(m)
        if !args.empty?
            raise ArgumentError, "expected zero arguments to #{m}, got #{args.size}"
        end
        Variable.new(m)
    elsif m =~ /(.*)(?:_event$|_child$)/
        return root.send(m, *args, &block)
    else return super
    end
end
parse_names(suffixes) click to toggle source

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

@param [String] suffix that should be added to all the names

# File lib/roby/coordination/models/base.rb, line 111
def parse_names(suffixes)
    definition_context = binding.callers.find { |b| b.frame_type == :block }
    return if !definition_context

    # Assign names to tasks using the local variables
    vars = definition_context.eval "local_variables"
    values = definition_context.eval "[#{vars.map { |n| "#{n}" }.join(", ")}]"
    vars.zip(values).each do |name, object|
        suffixes.each do |klass, suffix|
            if object.kind_of?(klass)
                object.name = "#{name}#{suffix}"
            end
        end
    end
end
respond_to_missing?(m, include_private) click to toggle source
Calls superclass method
# File lib/roby/coordination/models/base.rb, line 127
def respond_to_missing?(m, include_private)
    has_argument?(m) ||
        (m =~ /_event$|_child$/ && root.respond_to?(m)) ||
        super
end
root(*new_root) click to toggle source

Gets or sets the root task model

@return [Root] the root task model, i.e. a representation of the

task this execution context is going to be run on
# File lib/roby/coordination/models/base.rb, line 20
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
setup_submodel(subclass, options = Hash.new) click to toggle source

Creates a new execution context model as a submodel of self

@param [Model<Roby::Task>] subclass the

task model that is going to be used as a toplevel task for the
state machine

@return [Model<StateMachine>] a subclass of StateMachine

Calls superclass method
# File lib/roby/coordination/models/base.rb, line 69
def setup_submodel(subclass, options = Hash.new)
    options = Kernel.validate_options options, root: Roby::Task
    subclass.root(options[:root])
    super
end
task(object, task_model = Roby::Task) click to toggle source

Creates a state from an object

# File lib/roby/coordination/models/base.rb, line 76
def task(object, task_model = Roby::Task)
    if object.respond_to?(:to_coordination_task)
        task = object.to_coordination_task(task_model)
        tasks << task
        task
    elsif object.respond_to?(:as_plan)
        task = TaskFromAsPlan.new(object, task_model)
        tasks << task
        task
    else raise ArgumentError, "cannot create a task from #{object}"
    end
end
task_model() click to toggle source

@return [Model<Roby::Task>] the task model this execution context

is attached to
# File lib/roby/coordination/models/base.rb, line 34
def task_model; root.model end
use_fault_response_table(table_model, arguments = Hash.new) click to toggle source

Declare that this fault response table should be active as long as this coordination model is

# File lib/roby/coordination/models/base.rb, line 168
def use_fault_response_table(table_model, arguments = Hash.new)
    arguments = table_model.validate_arguments(arguments)
    used_fault_response_tables << [table_model, arguments]
end
validate_event(object) click to toggle source
# File lib/roby/coordination/models/base.rb, line 159
def validate_event(object)
    if !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) click to toggle source
# File lib/roby/coordination/models/base.rb, line 152
def validate_or_create_task(task)
    if !task.kind_of?(Coordination::Models::Task)
        task(task)
    else task
    end
end
validate_task(object) click to toggle source
# File lib/roby/coordination/models/base.rb, line 145
def validate_task(object)
    if !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