Module: Roby::Coordination::Models::Script

Extended by:
MetaRuby::Attributes
Included in:
ActionScript, FaultHandler, TaskScript
Defined in:
lib/roby/coordination/models/script.rb

Overview

The metamodel for all script-based coordination models

Defined Under Namespace

Classes: DeadInstruction, Emit, Start, TimeoutStart, TimeoutStop, Wait

Instance Method Summary collapse

Instance Method Details

#add(instruction) ⇒ Object



274
275
276
277
278
279
# File 'lib/roby/coordination/models/script.rb', line 274

def add(instruction)
    if terminal?
        raise ArgumentError, "a terminal command has been called on this script, cannot add anything further"
    end
    instructions << instruction
end

#call(script) ⇒ Object

Execute another script at this point in the execution



258
259
260
# File 'lib/roby/coordination/models/script.rb', line 258

def call(script)
    instructions.concat(script.instructions)
end

#emit(event) ⇒ Object

Emit the given event

Parameters:



252
253
254
255
# File 'lib/roby/coordination/models/script.rb', line 252

def emit(event)
    validate_event event
    add Emit.new(event)
end

#execute(task, options = Hash.new) ⇒ Object

Starts the given task, and waits for it to successfully finish

Parameters:



211
212
213
214
215
# File 'lib/roby/coordination/models/script.rb', line 211

def execute(task, options = Hash.new)
    task = validate_or_create_task task
    start(task, options)
    wait(task.success_event)
end

#instructionsArray

The list of instructions in this script

Returns:

  • (Array)


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

attribute(:instructions) { Array.new }

#sleep(time) ⇒ Object

Waits a certain amount of time before continuing

Parameters:

  • time (Float)

    the amount of time to wait, in seconds



220
221
222
223
224
# File 'lib/roby/coordination/models/script.rb', line 220

def sleep(time)
    task = self.task(ActionCoordination::TaskFromAsPlan.new(Tasks::Timeout.with_arguments(delay: time), Tasks::Timeout))
    start task, explicit_start: true
    wait task.stop_event
end

#start(task, explicit_start: false, **options) ⇒ Object

Starts the given task

Parameters:



199
200
201
202
203
# File 'lib/roby/coordination/models/script.rb', line 199

def start(task, explicit_start: false, **options)
    task = validate_or_create_task task
    add Start.new(task, explicit_start: explicit_start, **options)
    wait(task.start_event)
end

#terminalObject

Marks this script has being terminated, i.e. that no new instructions can be added to it

Once this is called, adding new instructions will raise ArgumentError



179
180
181
# File 'lib/roby/coordination/models/script.rb', line 179

def terminal
    __terminal(true)
end

#terminal?Boolean

Returns if true, this script cannot get new instructions (a terminal instruction has been added).

Returns:

  • (Boolean)

    if true, this script cannot get new instructions (a terminal instruction has been added)



185
186
187
# File 'lib/roby/coordination/models/script.rb', line 185

def terminal?
    __terminal
end

#timeout_start(delay, options = Hash.new) ⇒ Object



262
263
264
265
266
# File 'lib/roby/coordination/models/script.rb', line 262

def timeout_start(delay, options = Hash.new)
    ins = TimeoutStart.new(delay, options)
    add ins
    ins
end

#timeout_stop(timeout_start) ⇒ Object



268
269
270
271
272
# File 'lib/roby/coordination/models/script.rb', line 268

def timeout_stop(timeout_start)
    ins = TimeoutStop.new(timeout_start)
    add ins
    ins
end

#wait(event, timeout: nil, **wait_options) ⇒ Object

Waits until this event gets emitted

It will wait even if this event has already been emitted at this point in the script (i.e. waits for a “new” emission)

Parameters:

  • event (Event)

    the event to wait for

  • options (Hash)
  • timeout (Float) (defaults to: nil)

    a timeout (for backward compatibility, use timeout(seconds) do … end instead)



235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/roby/coordination/models/script.rb', line 235

def wait(event, timeout: nil, **wait_options)
    validate_event event

    wait = Wait.new(event, **wait_options)
    if timeout
        timeout(timeout) do
            add wait
        end
    else
        add wait
    end
    wait
end