Class: Roby::Coordination::Models::Script::Wait

Inherits:
ScriptInstruction show all
Defined in:
lib/roby/coordination/models/script.rb

Overview

Script element that implements #wait

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ScriptInstruction

#cancel

Constructor Details

#initialize(event, after: nil) ⇒ Wait

Returns a new instance of Wait.

Parameters:

  • options (Hash)

    a customizable set of options



66
67
68
69
70
71
72
# File 'lib/roby/coordination/models/script.rb', line 66

def initialize(event, after: nil)
    super()

    @event = event
    @done = false
    @time_barrier = after
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



46
47
48
# File 'lib/roby/coordination/models/script.rb', line 46

def event
  @event
end

#time_barrierTime? (readonly)

Returns time after which an emission is valid. ‘nil’ means that only emissions that have happened after the script reached this instruction are considered.

Returns:

  • (Time, nil)

    time after which an emission is valid. ‘nil’ means that only emissions that have happened after the script reached this instruction are considered



51
52
53
# File 'lib/roby/coordination/models/script.rb', line 51

def time_barrier
  @time_barrier
end

#timeoutFloat (readonly)

Returns number of seconds after which the wait instruction should generate an error.

Returns:

  • (Float)

    number of seconds after which the wait instruction should generate an error



55
56
57
# File 'lib/roby/coordination/models/script.rb', line 55

def timeout
  @timeout
end

Instance Method Details

#done?Boolean

Returns true if the watched event got emitted.

Returns:

  • (Boolean)

    true if the watched event got emitted



61
# File 'lib/roby/coordination/models/script.rb', line 61

attr_predicate :done?

#execute(script) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/roby/coordination/models/script.rb', line 78

def execute(script)
    event     = self.event.resolve
    plan      = script.plan
    root_task = script.root_task

    if time_barrier
        last_event = event.history.last
        if last_event && last_event.time > time_barrier
            return true
        end
    end

    if event.unreachable?
        plan.add_error(DeadInstruction.new(script.root_task))
        return false
    end

    if event.task != root_task
        role_name = "wait_#{self.object_id}"
        current_roles =
            root_task.depends_on?(event.task) &&
            root_task.roles_of(event.task)

        root_task.depends_on(
            event.task, success: nil, role: role_name
        )
    end

    event.if_unreachable(
        cancel_at_emission: true
    ) do |reason, generator|
        unless disabled?
            generator.plan.add_error(
                DeadInstruction.new(script.root_task)
            )
        end
    end

    event.on on_replace: :copy do |event|
        if event.generator == self.event.resolve && !disabled?
            handle_event(script, role_name, current_roles, event)
        end
    end

    false
end

#handle_event(script, role_name, current_roles, event) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Helper to handle events



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/roby/coordination/models/script.rb', line 128

def handle_event(script, role_name, current_roles, event)
    return if time_barrier && event.time < time_barrier

    child = role_name &&
            script.root_task.find_child_from_role(role_name)
    if child
        script.root_task.remove_roles(
            child, role_name,
            remove_child_when_empty:
                !current_roles || !current_roles.empty?
        )
    end

    cancel
    script.step
end

#initialized?Boolean

Returns true if #execute has been called once.

Returns:

  • (Boolean)

    true if #execute has been called once



58
# File 'lib/roby/coordination/models/script.rb', line 58

attr_predicate :initialized?

#new(script) ⇒ Object



74
75
76
# File 'lib/roby/coordination/models/script.rb', line 74

def new(script)
    Wait.new(script.instance_for(event), after: time_barrier)
end

#to_sObject



149
150
151
# File 'lib/roby/coordination/models/script.rb', line 149

def to_s
    "wait(#{event})"
end

#waited_task_roleObject



145
146
147
# File 'lib/roby/coordination/models/script.rb', line 145

def waited_task_role
    "wait_#{object_id}"
end