class Roby::PlanService

A plan service represents a “place” in the plan. I.e. it initially is attached to a given task instance, but its attachment will move when the task is replaced by another one, thus allowing to track a task that performs a given service for the system.

It forwards method calls to the underlying task

Attributes

event_handlers[R]

The set of event handlers that have been defined for this service

It is a mapping from a symbol (that represents the event name) to a set of procs that represent the handlers themselves

@see on

finalization_handlers[R]

Set of blocks that will be called when the service itself is finalized

@see when_finalized

plan_status_handlers[R]

The set of handlers for mission/permanent status change

replacement_handlers[R]

The set of handlers for replacments

@see on_replacement

task[R]

The underlying task

@return [Roby::Task]

Public Class Methods

get(task) click to toggle source

Returns a plan service for task. If a service is already defined for task, it will return it.

# File lib/roby/plan_service.rb, line 168
def self.get(task)
    if service = task.plan.find_plan_service(task)
        service
    else
        new(task)
    end
end
new(task) click to toggle source
# File lib/roby/plan_service.rb, line 33
def initialize(task)
    @event_handlers = Hash.new
    @finalization_handlers = Array.new
    @replacement_handlers = Array.new
    @plan_status_handlers = Array.new
    self.task = task
    task.plan.add_plan_service(self)
end

Public Instance Methods

create_transaction_proxy(transaction) click to toggle source
# File lib/roby/plan_service.rb, line 196
def create_transaction_proxy(transaction)
    transaction.create_and_register_proxy_plan_service(self)
end
finalized!() click to toggle source

Called by the plan when the service is finalized

# File lib/roby/plan_service.rb, line 124
def finalized!
    if task.plan.executable?
        finalization_handlers.each do |h|
            h.call
        end
    end
end
initialize_copy(source) click to toggle source
Calls superclass method
# File lib/roby/plan_service.rb, line 42
def initialize_copy(source)
    super

    @event_handlers = source.event_handlers.dup
    @finalization_handlers = source.finalization_handlers.dup
    @replacement_handlers = source.replacement_handlers.dup
    @plan_status_handlers = source.plan_status_handlers.dup
end
kind_of?(*args) click to toggle source
Calls superclass method
# File lib/roby/plan_service.rb, line 192
def kind_of?(*args)
    super || task.kind_of?(*args)
end
notify_task_status_change(new_status) click to toggle source

Called to notify about a plan status change for the underlying task

@see on_plan_status_change

# File lib/roby/plan_service.rb, line 91
def notify_task_status_change(new_status)
    plan_status_handlers.each do |h|
        h.call(new_status)
    end
end
on(event, &block) click to toggle source

Defines an event handler for this service

This event handler will only be called if symbol is emitted by the task that currently provides this service.

For instance, if you do

service = PlanService.get(t)
service.on(:success) do
    STDERR.puts "message"
end
plan.replace(t, t2)

Then, before the replacement, 'message' is displayed if t emits :success. After the replacement, it will be displayed if t2 emits :success, and will not be displayed if t does.

# File lib/roby/plan_service.rb, line 155
def on(event, &block)
    check_arity(block, 1)
    event = event.to_sym
    if event_handlers.has_key?(event)
        event_handlers[event] << block
    else
        task.event(event).on(on_replace: :drop, &method(:__handle_event__))
        (event_handlers[event] = Array.new) << block
    end
end
on_plan_status_change(initial: true, &block) click to toggle source

Registers a callback that is called when the task's mission/permanent status changes

@yieldparam [Symbol] status one of :mission, :permanent or :normal

# File lib/roby/plan_service.rb, line 74
def on_plan_status_change(initial: true, &block)
    plan_status_handlers << block
    if initial
        current_status =
            if task.plan.mission_task?(task)
                :mission
            elsif task.plan.permanent_task?(task)
                :permanent
            else :normal
            end
        block.call(current_status)
    end
end
on_replacement(&block) click to toggle source

Register a callback that should be called when the underlying task is replaced

@yield old, new

# File lib/roby/plan_service.rb, line 66
def on_replacement(&block)
    replacement_handlers << block
end
respond_to_missing?(m, include_private) click to toggle source
# File lib/roby/plan_service.rb, line 176
def respond_to_missing?(m, include_private)
    task.respond_to?(m)
end
task=(new_task) click to toggle source

Change the underlying task

# File lib/roby/plan_service.rb, line 98
def task=(new_task)
    replacement_handlers.each do |h|
        h.call(task, new_task)
    end
    @task = new_task

    # Register event handlers for all events that have a definition
    event_handlers.each_key do |event|
        new_task.event(event).on(on_replace: :drop, &method(:__handle_event__))
    end
end
to_task() click to toggle source

Returns the underlying task

@see task

# File lib/roby/plan_service.rb, line 188
def to_task
    task
end
transaction_proxy?() click to toggle source

True if this plan service instance is a transaction proxy, i.e. modifies an already existing service in the frame of a transaction

# File lib/roby/plan_service.rb, line 53
def transaction_proxy?
    false
end
when_finalized(&block) click to toggle source

Defines a finalization handler for this service

This handler will be called when the service itself is finalized

# File lib/roby/plan_service.rb, line 135
def when_finalized(&block)
    finalization_handlers << block
end