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.



12
13
14
15
# File 'lib/roby/event.rb', line 12

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

Instance Attribute Details

#contextObject

Returns the value of attribute context.



21
22
23
# File 'lib/roby/event.rb', line 21

def context
  @context
end

#generatorObject (readonly)

The generator which emitted this event



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

def generator
  @generator
end

#propagation_idObject

Returns the value of attribute propagation_id.



21
22
23
# File 'lib/roby/event.rb', line 21

def propagation_id
  @propagation_id
end

#timeObject

Returns the value of attribute time.



21
22
23
# File 'lib/roby/event.rb', line 21

def time
  @time
end

Instance Method Details

#add_sources(new_sources) ⇒ Object



72
73
74
75
76
# File 'lib/roby/event.rb', line 72

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.



113
114
115
# File 'lib/roby/event.rb', line 113

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

#all_sourcesObject

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



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

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

#inspectObject

:nodoc:



107
108
109
# File 'lib/roby/event.rb', line 107

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

#modelObject



103
104
105
# File 'lib/roby/event.rb', line 103

def model
    self.class
end

#nameObject



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

def name
    model.name
end

#planObject



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

def plan
    generator.plan
end

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

:nodoc:



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/roby/event.rb', line 121

def pretty_print(pp, context: true, context_task: nil) # :nodoc:
    pp.text "[#{Roby.format_time(time)} @#{propagation_id}] #{self.class}"
    if context && !self.context.empty?
        pp.breakable
        pp.text "with context"
        pp.nest(2) do
            pp.text "  "
            pp.seplist(self.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



62
63
64
# File 'lib/roby/event.rb', line 62

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



55
56
57
# File 'lib/roby/event.rb', line 55

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



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

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



78
79
80
81
82
83
# File 'lib/roby/event.rb', line 78

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.



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

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



67
68
69
70
# File 'lib/roby/event.rb', line 67

def sources=(new_sources) # :nodoc:
    @sources = []
    add_sources(new_sources)
end

#to_execution_exceptionObject



135
136
137
# File 'lib/roby/event.rb', line 135

def to_execution_exception
    generator.to_execution_exception
end

#to_execution_exception_matcherObject



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

def to_execution_exception_matcher
    generator.to_execution_exception_matcher
end

#to_sObject

:nodoc:



117
118
119
# File 'lib/roby/event.rb', line 117

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