Module: Roby::EventStructure::TemporalConstraints::Extension

Defined in:
lib/roby/event_structure/temporal_constraints.rb

Instance Method Summary collapse

Instance Method Details

#add_occurence_constraint(other_event, min, max = Infinity, recurrent = false) ⇒ Object

Adds a constraint on the allowed emission of other_event based on the existing emissions of self

min and max specify the minimum (resp. maximum) of times self should be emitted before other_event has the right to be emitted.

If recurrent is true, then the min/max values are computed using the emissions of self since the last emission of other_event. Otherwise, all emissions since the creation of self are taken into account.



524
525
526
527
528
# File 'lib/roby/event_structure/temporal_constraints.rb', line 524

def add_occurence_constraint(other_event, min, max = Infinity, recurrent = false)
    set = TemporalConstraintSet.new
    set.add_occurence_constraint(min, max, recurrent)
    add_forward_temporal_constraint(other_event, set)
end

#add_temporal_constraint(other_event, min, max) ⇒ Object

Creates a temporal constraint between self and other_event. min is the minimum time



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/roby/event_structure/temporal_constraints.rb', line 494

def add_temporal_constraint(other_event, min, max)
    if min > max
        raise ArgumentError, "min should be lower than max (min == #{min} and max == #{max})"
    end

    if max < 0
        return other_event.add_temporal_constraint(self, -max, -min)
    elsif min < 0
        set = TemporalConstraintSet.new
        set.add(-max, -min)
        other_event.add_forward_temporal_constraint(self, set)
    end

    set = TemporalConstraintSet.new
    set.add(min, max)
    add_forward_temporal_constraint(other_event, set)
    set
end

#find_failed_occurence_constraint(next_event) ⇒ Object



530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/roby/event_structure/temporal_constraints.rb', line 530

def find_failed_occurence_constraint(next_event)
    base_event = if next_event then last
                 else
                     history[-2]
                 end
    if base_event
        base_time = base_event.time
    end
    each_backward_temporal_constraint do |parent|
        next if block_given? && !yield(parent)

        constraints = parent[self, TemporalConstraints]
        counts = { false => parent.history.size }
        if base_time
            negative_count = parent.history.inject(0) do |count, ev|
                break(count) if ev.time > base_time

                count + 1
            end
        else
            negative_count = 0
        end
        counts[true] = counts[false] - negative_count
        counts.each do |recurrent, count|
            min_count, max_count = constraints.occurence_constraints[recurrent]
            if count < min_count || count > max_count
                if recurrent && base_time
                    return [parent, parent.history.size, [min_count, max_count], base_time]
                else
                    return [parent, parent.history.size, [min_count, max_count]]
                end
            end
        end
    end
    nil
end

#find_failed_temporal_constraint(time) ⇒ Object

Returns a [parent, intervals] pair that represents a temporal constraint the given time fails to meet



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/roby/event_structure/temporal_constraints.rb', line 461

def find_failed_temporal_constraint(time)
    each_backward_temporal_constraint do |parent|
        next if block_given? && !yield(parent)

        disjoint_set = parent[self, TemporalConstraints]
        next if disjoint_set.intervals.empty?

        if disjoint_set.boundaries[0] < 0
            # It might be fullfilled in the future
            next
        end

        max_diff = disjoint_set.boundaries[1]
        parent.history.each do |parent_event|
            diff = time - parent_event.time
            if diff > max_diff || !disjoint_set.include?(diff)
                return parent, disjoint_set
            end

            disjoint_set.include?(diff)
        end
    end
    nil
end

#has_temporal_constraints?Boolean

True if this event is constrained by the TemporalConstraints relation in any way

Returns:

  • (Boolean)


452
453
454
455
456
457
# File 'lib/roby/event_structure/temporal_constraints.rb', line 452

def has_temporal_constraints?
    each_backward_temporal_constraint do |parent| # rubocop:disable Lint/UnreachableLoop
        return true
    end
    false
end

#meets_temporal_constraints?(time, &block) ⇒ Boolean

Returns true if this event meets its temporal constraints

Returns:

  • (Boolean)


487
488
489
490
# File 'lib/roby/event_structure/temporal_constraints.rb', line 487

def meets_temporal_constraints?(time, &block)
    !find_failed_temporal_constraint(time, &block) &&
        !find_failed_occurence_constraint(true, &block)
end

#should_emit_after(other_event, options = nil) ⇒ Object

Shortcut to specify that self should be emitted after other_event



430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/roby/event_structure/temporal_constraints.rb', line 430

def should_emit_after(other_event, options = nil)
    if options
        options = Kernel.validate_options options,
                                          min_t: nil, max_t: nil, recurrent: false
        recurrent = options[:recurrent]
    end
    other_event.add_occurence_constraint(self, 1, Infinity, recurrent)
    if options && (options[:min_t] || options[:max_t])
        other_event.add_temporal_constraint(self,
                                            options[:min_t] || 0, options[:max_t] || Infinity)
    end
end

#should_emit_after?(generator) ⇒ Boolean

Returns true if there is a constraint that specifies that self and generator are ordered in time

Returns:

  • (Boolean)


445
446
447
448
# File 'lib/roby/event_structure/temporal_constraints.rb', line 445

def should_emit_after?(generator)
    graph = plan.event_relation_graph_for(TemporalConstraints)
    graph.has_edge?(generator, self)
end