Class: Roby::TaskEvent

Inherits:
Event show all
Extended by:
Models::TaskEvent
Defined in:
lib/roby/task_event.rb

Overview

Base class for events emitted by tasks.

When one creates a new event on a task, Roby creates a corresponding subclass of TaskEvent. The emitted event objects are then instances of that class.

For instance, there is a Roby::Task::StopEvent class which is used to represent the emitted :stop events of Roby::Task. However, if one overloads the stop command with

class TModel < Roby::Task
event :stop, controlable: true
end

Then TModel::StopEvent will be a subclass of StopEvent.

These models are meant to be extended when the emission carry information, i.e. to provide a robust access to the information contained in Event#context

Instance Attribute Summary collapse

Attributes included from Models::TaskEvent

#task_model

Attributes inherited from Event

#context, #generator, #propagation_id, #time

Instance Method Summary collapse

Methods included from Models::TaskEvent

generalized_match, match, setup_submodel

Methods inherited from Event

#add_sources, #after, #all_sources, #inspect, #name, #plan, #protect_all_sources, #protect_sources, #reemit, #root_sources, #sources, #sources=, #to_execution_exception, #to_execution_exception_matcher

Methods included from DRoby::V5::EventDumper

#droby_dump

Constructor Details

#initialize(task, generator, propagation_id, context, time = Time.now) ⇒ TaskEvent

Returns a new instance of TaskEvent.



31
32
33
34
35
36
# File 'lib/roby/task_event.rb', line 31

def initialize(task, generator, propagation_id, context, time = Time.now)
    @task = task
    @terminal_flag = generator.terminal_flag
    @model = self.class
    super(generator, propagation_id, context, time)
end

Instance Attribute Details

#modelObject (readonly)

The event model, usually its class



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

def model
  @model
end

#taskObject (readonly)

The task which fired this event



27
28
29
# File 'lib/roby/task_event.rb', line 27

def task
  @task
end

Instance Method Details

#all_task_sourcesObject

Recursively browses in the event sources, returning only those that come from this event's task



73
74
75
76
77
78
79
80
# File 'lib/roby/task_event.rb', line 73

def all_task_sources
    result = Set.new
    for ev in task_sources
        result << ev
        result.merge(ev.all_task_sources)
    end
    result
end

#controlable?Boolean

If the event is controlable

Returns:

  • (Boolean)


134
135
136
# File 'lib/roby/task_event.rb', line 134

def controlable?
    model.controlable?
end

#failure?Boolean

If this event is terminal

Returns:

  • (Boolean)


144
145
146
# File 'lib/roby/task_event.rb', line 144

def failure?
    @terminal_flag == :failure
end

#pp_context(pp) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/roby/task_event.rb', line 118

def pp_context(pp)
    if !context || context.empty?
        pp.text "No context"
        return
    end

    pp.text "context:"
    context.each do |obj|
        pp.nest(2) do
            pp.breakable
            Roby.format(obj, pp)
        end
    end
end

#pretty_print(pp, context: true, context_task: nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/roby/task_event.rb', line 100

def pretty_print(pp, context: true, context_task: nil)
    pp.text "event '#{symbol}' emitted at [#{Roby.format_time(time)} @#{propagation_id}]"
    if !context_task || context_task != task
        pp.text " from"
        pp.nest(2) do
            pp.breakable
            task.pretty_print(pp)
        end
    end

    return unless context

    pp.nest(2) do
        pp.breakable
        pp_context(pp)
    end
end

#root_task_sourcesObject

Recursively browses in the event sources, returning those (1) that come from this event's task and (2) have no parent from within the Forwarding relation in the task sources.



85
86
87
88
89
90
# File 'lib/roby/task_event.rb', line 85

def root_task_sources
    all = all_task_sources
    all.find_all do |event|
        all.none? { |ev| ev.generator.child_object?(event.generator, Roby::EventStructure::Forwarding) }
    end
end

#success?Boolean

If this event is terminal

Returns:

  • (Boolean)


139
140
141
# File 'lib/roby/task_event.rb', line 139

def success?
    @terminal_flag == :success
end

#symbolObject

The event symbol



154
155
156
# File 'lib/roby/task_event.rb', line 154

def symbol
    model.symbol
end

#task_sourcesObject

Returns the events that are the cause of this event, limiting itself to the task's events. The return value is a Set of TaskEvent instances.

For instance, for an interruptible task:

task.start!
task.stop!

Then task.stop_event.last.task_sources will return a Set instance which contains the failed event. I.e. in this particular situation, it behaves in the same way than Event#event_sources

However, with

event.add_signal task.failed_event
task.start!
event.call

Event#event_sources will return both event.last and task.failed_event.last while TaskEvent will only return task.failed_event.last.



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

def task_sources
    result = Set.new
    for ev in sources
        gen = ev.generator
        if gen.respond_to?(:task) && gen.task == task
            result << ev
        end
    end
    result
end

#terminal?Boolean

If this event is terminal

Returns:

  • (Boolean)


149
150
151
# File 'lib/roby/task_event.rb', line 149

def terminal?
    @terminal_flag
end

#to_sObject



92
93
94
95
96
97
98
# File 'lib/roby/task_event.rb', line 92

def to_s
    result = "[#{Roby.format_time(time)} @#{propagation_id}] #{task}/#{symbol}"
    if context
        result += ": #{context}"
    end
    result
end