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



58
59
60
61
62
# File 'lib/roby/coordination/models/script.rb', line 58

def initialize(event, after: nil)
    @event = event
    @done = false
    @time_barrier = after
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



38
39
40
# File 'lib/roby/coordination/models/script.rb', line 38

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



43
44
45
# File 'lib/roby/coordination/models/script.rb', line 43

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



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

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



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

attr_predicate :done?

#execute(script) ⇒ Object



68
69
70
71
72
73
74
75
76
77
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
# File 'lib/roby/coordination/models/script.rb', line 68

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|
        if !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?
            if !time_barrier || event.time > time_barrier
                if role_name && (child = script.root_task.find_child_from_role(role_name))
                    script.root_task.remove_roles(child, role_name, remove_child_when_empty: !current_roles || !current_roles.empty?)
                end
                cancel
                script.step
            end
        end
    end

    false
end

#initialized?Boolean

Returns true if #execute has been called once.

Returns:

  • (Boolean)

    true if #execute has been called once



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

attr_predicate :initialized?

#new(script) ⇒ Object



64
65
66
# File 'lib/roby/coordination/models/script.rb', line 64

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

#to_sObject



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

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

#waited_task_roleObject



112
113
114
# File 'lib/roby/coordination/models/script.rb', line 112

def waited_task_role
    "wait_#{object_id}"
end