Class: Roby::Coordination::Base

Inherits:
Object
  • Object
show all
Extended by:
Models::Base
Defined in:
lib/roby/coordination/base.rb

Overview

Context for all the execution objects that can be attached to the action interface and/or tasks, such as state machines and scripts

Direct Known Subclasses

Actions, TaskScript

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_task = nil, arguments = {}, options = {}) ⇒ Base

Create a new coordination instance. The model is represented by this object’s class

Parameters:

  • root_task (Roby::Task) (defaults to: nil)

    the task instance that should be bound to Models::Base#root

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

    parametrization of this coordination object. The list of known arguments can be accessed with model.arguments (defined by Models::Arguments.

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

Options Hash (options):

  • :on_replace (:drop, :copy) — default: :drop

    defines what should be done if the root task gets replaced. :drop means that the state machine should not be passed to the new task. :copy means that it should. Note that it only affects the root task. All other tasks that are referred to inside the coordination model are tracked for replacements.

  • :parent (nil, Base) — default: nil

    the parent coordination model. This is used so that the coordination tasks can be shared across instances



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/roby/coordination/base.rb', line 61

def initialize(root_task = nil, arguments = {}, options = {})
    options = Kernel.validate_options options, on_replace: :drop, parent: nil

    @arguments = model.validate_arguments(arguments)
    @parent = options[:parent]
    @instances = {}
    if root_task
        bind_coordination_task_to_instance(
            instance_for(model.root),
            root_task,
            on_replace: options[:on_replace])
        root_task.add_coordination_object(self)

        attach_fault_response_tables_to(root_task)

        if options[:on_replace] == :copy
            root_task.as_service.on_replacement do |old_task, new_task|
                old_task.remove_coordination_object(self)
                new_task.add_coordination_object(self)
                attach_fault_response_tables_to(new_task)
            end
        end
    end
end

Instance Attribute Details

#argumentsHash (readonly)

The set of arguments given to this execution context

Returns:

  • (Hash)


29
30
31
# File 'lib/roby/coordination/base.rb', line 29

def arguments
  @arguments
end

#instancesObject (readonly)

A mapping from the model-level description of the context to the instance-level description

See Also:

  • instanciate


35
36
37
# File 'lib/roby/coordination/base.rb', line 35

def instances
  @instances
end

#parentnil, Base (readonly)

The parent coordination object

Returns:



13
14
15
# File 'lib/roby/coordination/base.rb', line 13

def parent
  @parent
end

Instance Method Details

#attach_fault_response_tables_to(_task) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/roby/coordination/base.rb', line 86

def attach_fault_response_tables_to(_task)
    model.each_used_fault_response_table do |table, arguments|
        arguments = arguments.transform_values do |val|
            if val.kind_of?(Models::Variable)
                self.arguments[val.name]
            else
                val
            end
        end
        root_task.use_fault_response_table(table, arguments)
    end
end

#bind_coordination_task_to_instance(coordination_task, instance, options = {}) ⇒ void

This method returns an undefined value.

Binds a task instance to the coordination task

This method binds a task instance to the coordination task it represents, and optionally installs the handlers necessary to track replacement

Parameters:

  • coordination_task (Coordination::Task)

    the coordination task

  • instance (Roby::Task)

    the task

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

    a customizable set of options

Options Hash (options):

  • :on_replace (Symbol) — default: :drop

    what should be done if the task instance is replaced by another task. If :drop, the coordination task will be reset to nil. If :copy, it will track the new task



112
113
114
115
116
117
118
119
120
121
# File 'lib/roby/coordination/base.rb', line 112

def bind_coordination_task_to_instance(coordination_task, instance, options = {})
    options = Kernel.validate_options options, on_replace: :drop

    coordination_task.bind(instance)
    if options[:on_replace] == :copy
        instance.as_service.on_replacement do |old_task, new_task|
            coordination_task.bind(new_task)
        end
    end
end

#instance_for(object) ⇒ Coordination::Task

Returns the instance-level coordination task that is used to represent a model-level coordination task

Coordination models are built using instances of Models::Task (or its subclasses). When they get instanciated into actual coordination objects, these are uniquely associated with instances of Task (or its subclasses).

This method can be used to retrieve the unique object associated with a given model-level coordination task

Parameters:

Returns:



137
138
139
140
141
142
143
144
145
# File 'lib/roby/coordination/base.rb', line 137

def instance_for(object)
    if (ins = instances[object])
        ins
    elsif parent && (ins = parent.instances[object])
        ins
    else
        instances[object] = object.new(self)
    end
end

#modelModel<Base>

The execution context model

Returns:

  • (Model<Base>)

    a subclass of Base



39
40
41
# File 'lib/roby/coordination/base.rb', line 39

def model
    self.class
end

#planObject

The plan this coordination object is part of



23
24
25
# File 'lib/roby/coordination/base.rb', line 23

def plan
    root_task.plan
end

#root_taskModel<Roby::Task>

The task on which this execution context is being executed. It must fullfill model.task

Returns:



18
19
20
# File 'lib/roby/coordination/base.rb', line 18

def root_task
    instance_for(model.root).task
end