Class: Roby::Event

Inherits:
Object show all
Includes:
DRoby::V5::EventDumper
Defined in:
lib/roby/event.rb,
lib/roby/droby/enable.rb

Overview

Event objects are the objects representing a particular emission in the event propagation process. They represent the common propagation information (time, generator, sources, …) and provide some common functionalities related to propagation as well.

Direct Known Subclasses

TaskEvent

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DRoby::V5::EventDumper

#droby_dump

Constructor Details

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

Returns a new instance of Event.



10
11
12
13
# File 'lib/roby/event.rb', line 10

def initialize(generator, propagation_id, context, time = Time.now)
    @generator, @propagation_id, @context, @time = generator, propagation_id, context.freeze, time
    @sources = Set.new
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



19
20
21
# File 'lib/roby/event.rb', line 19

def context
  @context
end

#generatorObject (readonly)

The generator which emitted this event



8
9
10
# File 'lib/roby/event.rb', line 8

def generator
  @generator
end

#propagation_idObject

Returns the value of attribute propagation_id.



19
20
21
# File 'lib/roby/event.rb', line 19

def propagation_id
  @propagation_id
end

#timeObject

Returns the value of attribute time.



19
20
21
# File 'lib/roby/event.rb', line 19

def time
  @time
end

Instance Method Details

#add_sources(new_sources) ⇒ Object



70
71
72
73
74
# File 'lib/roby/event.rb', line 70

def add_sources(new_sources)
    for new_s in new_sources
        @sources << WeakRef.new(new_s)
    end
end

#after(time) ⇒ Object

Returns an event generator which will be emitted once time seconds after this event has been emitted.



105
106
107
# File 'lib/roby/event.rb', line 105

def after(time)
    State.at t: (self.time + time)
end

#all_sourcesObject

Recursively computes the source event that led to the emission of self



42
43
44
45
46
47
48
49
# File 'lib/roby/event.rb', line 42

def all_sources
    result = Set.new
    sources.each do |ev|
        result << ev
        result.merge(ev.all_sources)
    end
    result
end

#inspectObject

:nodoc:



99
100
101
# File 'lib/roby/event.rb', line 99

def inspect # :nodoc:
    "#<#{model.to_s}:0x#{address.to_s(16)} generator=#{generator} model=#{model}"
end

#modelObject



98
# File 'lib/roby/event.rb', line 98

def model; self.class end

#nameObject



97
# File 'lib/roby/event.rb', line 97

def name; model.name end

#planObject



15
16
17
# File 'lib/roby/event.rb', line 15

def plan
    generator.plan
end

#pretty_print(pp, with_context = true) ⇒ Object

:nodoc:



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/roby/event.rb', line 113

def pretty_print(pp, with_context = true) # :nodoc:
    pp.text "[#{Roby.format_time(time)} @#{propagation_id}] #{self.class}"
    if with_context && context
        pp.breakable
        pp.text "with context"
        pp.nest(2) do
            pp.text "  "
            pp.seplist(context) do |v|
                v.pretty_print(pp)
            end
        end
    end
end

#protect_all_sourcesObject

Call to recursively protect this event’s sources from Ruby’s garbage collection. Call this if you want to store the propagation history for this event



60
61
62
# File 'lib/roby/event.rb', line 60

def protect_all_sources
    @protected_all_sources = all_sources
end

#protect_sourcesObject

Call to protect this event’s source from Ruby’s garbage collection. Call this if you want to store the propagation history for this event



53
54
55
# File 'lib/roby/event.rb', line 53

def protect_sources
    @protected_sources = sources
end

#reemit(new_id, new_context = nil) ⇒ Object

To be used in the event generators ::new methods, when we need to reemit an event while changing its



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/roby/event.rb', line 85

def reemit(new_id, new_context = nil)
    if propagation_id != new_id || (new_context && new_context != context)
        new_event = self.dup
        new_event.propagation_id = new_id
        new_event.context = new_context
        new_event.time = Time.now
        new_event
    else
        self
    end
end

#root_sourcesObject



76
77
78
79
80
81
# File 'lib/roby/event.rb', line 76

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

#sourcesObject

The events whose emission directly triggered this event during the propagation. The events in this set are subject to Ruby’s own garbage collection, which means that if a source event is garbage collected (i.e. if all references to the associated task/event generator are removed), it will be removed from this set as well.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/roby/event.rb', line 27

def sources
    result = Set.new
    @sources.delete_if do |ref|
        begin 
            result << ref.__getobj__
            false
        rescue WeakRef::RefError
            true
        end
    end
    result
end

#sources=(new_sources) ⇒ Object

Sets the sources. See #sources



65
66
67
68
# File 'lib/roby/event.rb', line 65

def sources=(new_sources) # :nodoc:
    @sources = Set.new
    add_sources(new_sources)
end

#to_execution_exceptionObject



127
128
129
# File 'lib/roby/event.rb', line 127

def to_execution_exception
    generator.to_execution_exception
end

#to_execution_exception_matcherObject



131
132
133
# File 'lib/roby/event.rb', line 131

def to_execution_exception_matcher
    generator.to_execution_exception_matcher
end

#to_sObject

:nodoc:



109
110
111
# File 'lib/roby/event.rb', line 109

def to_s # :nodoc:
    "[#{Roby.format_time(time)} @#{propagation_id}] #{self.class.to_s}: #{context}"
end