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

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

Defined Under Namespace

Classes: FailedOccurenceConstraint, FailedTemporalConstraint

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.



543
544
545
546
547
# File 'lib/roby/event_structure/temporal_constraints.rb', line 543

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



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/roby/event_structure/temporal_constraints.rb', line 513

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

#each_failed_occurence_constraint(use_last_event:) ⇒ Object

Enumerate the occurence constraints that would be failed because of next_event

Parameters:

  • next_event (Boolean)

    consider the last event (true) or one-from-last (false)



576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
# File 'lib/roby/event_structure/temporal_constraints.rb', line 576

def each_failed_occurence_constraint(use_last_event:)
    unless block_given?
        return enum_for(
            :each_failed_occurence_constraint,
            use_last_event: use_last_event
        )
    end

    base_event =
        if use_last_event
            last
        else
            history[-2]
        end

    base_time = base_event&.time

    each_backward_temporal_constraint do |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]
            next unless count < min_count || count > max_count

            failure = FailedOccurenceConstraint.new(
                parent: parent,
                parent_history_size: parent.history.size,
                required_min_count: min_count,
                required_max_count: max_count
            )
            failure.base_time = base_time if recurrent
            yield(failure)
        end
    end
    nil
end

#each_failed_temporal_constraint(time) {|constraint| ... } ⇒ Object

Yield each temporal constraint that is currently unmet

Yield Parameters:



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/roby/event_structure/temporal_constraints.rb', line 475

def each_failed_temporal_constraint(time)
    unless block_given?
        return enum_for(:each_failed_temporal_constraint, time)
    end

    each_backward_temporal_constraint do |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)
                failure = FailedTemporalConstraint.new(
                    parent: parent, interval: disjoint_set,
                    time: time
                )
                yield(failure)
            end

            disjoint_set.include?(diff)
        end
    end
end

#find_failed_occurence_constraint(next_event) ⇒ Object



550
551
552
553
554
555
556
557
558
559
560
561
562
563
# File 'lib/roby/event_structure/temporal_constraints.rb', line 550

def find_failed_occurence_constraint(next_event)
    each_failed_occurence_constraint(use_last_event: next_event) do |f|
        next if block_given? && !yield(f.parent)

        result = [
            f.parent, f.parent_history_size,
            [f.required_min_count, f.required_max_count]
        ]

        result << f.base_time if f.base_time
        return result
    end
    nil
end

#find_failed_temporal_constraint(time) ⇒ Object



460
461
462
463
464
465
466
467
# File 'lib/roby/event_structure/temporal_constraints.rb', line 460

def find_failed_temporal_constraint(time)
    each_failed_temporal_constraint(time) do |failure|
        next if block_given? && !yield(failure.parent)

        return failure.parent, failure.interval
    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)


506
507
508
509
# File 'lib/roby/event_structure/temporal_constraints.rb', line 506

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