Class: Roby::Coordination::Actions

Inherits:
Base show all
Extended by:
Models::Actions
Defined in:
lib/roby/coordination/actions.rb

Overview

Common functionality of coordination models that manipulate actions (ActionStateMachine, ActionScript)

Direct Known Subclasses

ActionScript, ActionStateMachine

Defined Under Namespace

Classes: TaskInfo

Instance Attribute Summary collapse

Attributes inherited from Base

#arguments, #instances, #parent

Instance Method Summary collapse

Methods inherited from Base

#attach_fault_response_tables_to, #bind_coordination_task_to_instance, #instance_for, #model, #plan, #root_task

Constructor Details

#initialize(root_task, arguments = {}) ⇒ Actions

Returns a new instance of Actions.



26
27
28
29
30
# File 'lib/roby/coordination/actions.rb', line 26

def initialize(root_task, arguments = {})
    super(root_task, arguments)
    @task_info = resolve_task_info
    @resolved_captures = {}
end

Instance Attribute Details

#current_taskCoordination::Task (readonly)

Returns the currently active toplevel task.

Returns:



11
12
13
# File 'lib/roby/coordination/actions.rb', line 11

def current_task
  @current_task
end

#resolved_capturesHash<Models::Capture, Object> (readonly)

Resolved captures

This is currently only used by the Roby::Coordination::ActionStateMachine

Returns:



24
25
26
# File 'lib/roby/coordination/actions.rb', line 24

def resolved_captures
  @resolved_captures
end

#task_infoObject (readonly)

Mapping from a Coordination::Models::Task object to the set of forwards that are defined for it



17
18
19
# File 'lib/roby/coordination/actions.rb', line 17

def task_info
  @task_info
end

Instance Method Details

#action_interface_modelObject



32
33
34
# File 'lib/roby/coordination/actions.rb', line 32

def action_interface_model
    model.action_interface
end

#dependency_options_for(toplevel, task, roles) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/roby/coordination/actions.rb', line 60

def dependency_options_for(toplevel, task, roles)
    roles = roles.dup
    if task.name
        roles << task.name
    end
    Hash[roles: roles,
         failure: :stop.or(:start.never),
         remove_when_done: true]
end

#remove_current_taskObject



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/roby/coordination/actions.rb', line 95

def remove_current_task
    current_task_child = root_task.find_child_from_role("current_task")
    task_info[current_task].required_tasks.each do |task, roles|
        if state_name = task.name
            roles = [state_name, *roles]
        end
        if !roles.empty? && (child_task = root_task.find_child_from_role(roles.first))
            root_task.remove_roles(child_task, *roles)
        end
    end
end

#resolve_task_infoObject



52
53
54
55
56
57
58
# File 'lib/roby/coordination/actions.rb', line 52

def resolve_task_info
    result = {}
    model.each_task do |task|
        result[instance_for(task)] = task_info_for(task)
    end
    result
end

#start_task(toplevel, explicit_start: false) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/roby/coordination/actions.rb', line 70

def start_task(toplevel, explicit_start: false)
    task_info = self.task_info[toplevel]
    tasks, forwards = task_info.required_tasks, task_info.forwards
    variables = arguments.merge(resolved_captures)

    instanciated_tasks = tasks.map do |task, roles|
        action_task = task.model.instanciate(root_task.plan, variables)
        root_task.depends_on(action_task, dependency_options_for(toplevel, task, roles))
        bind_coordination_task_to_instance(task, action_task, on_replace: :copy)
        task.model.setup_instanciated_task(self, action_task, variables)
        action_task
    end

    @current_task = toplevel
    forwards.each do |source, target|
        source.resolve.on do |event|
            if target.resolve.task.running?
                target.resolve.emit(*event.context)
            end
        end
    end

    instanciated_tasks
end

#task_info_for(task) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/roby/coordination/actions.rb', line 36

def task_info_for(task)
    required_tasks = model.required_tasks_for(task).map do |t, roles|
        [instance_for(t), roles]
    end

    forwards = Set.new
    model.each_forward do |in_task, event, target|
        if in_task == task
            event  = instance_for(event)
            target = instance_for(target)
            forwards << [event, target]
        end
    end
    TaskInfo.new(required_tasks, forwards)
end