Skip to content

Defining events examples

Example: print a message at the start of a scenario

OSC2 code: print a message at the start of a scenario
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"
import "$FTX_PACKAGES/base_scenarios/scenarios/crossing_vrus_family/crossing_person/crossing_person_top.osc"

extend test_config:
    set map = "$FTX_PACKAGES/maps/M77_FTX_highway_straight_long_road.xodr"



extend top.main:
    sut_v: sut_vehicle

    do serial:
        drv1: sut_v.drive() with:
           duration([10..20]sec)
        drv2: sut_v.drive() with:
           duration([10..20]sec)

    on @drv2.start:
        logger.log_info("SUT speed at start of drv2 is $(sut_v.state.speed)")

Example: record a value on finish event

OSC2 code: record a value on finish event
#
#Start_Header
#Incomplete scenario \(incomplete_scenario\): Test reached max allowed duration
#End_Header
#
import "$FTX/env/basic/exe_platforms/model_ssp/config/model_sumo_config.osc"

extend test_config:
    set map = "$FTX_PACKAGES/maps/M77_FTX_highway_straight_long_road.xodr"

extend test_config:
    set max_test_time = 50sec



extend top.main:
    sut_v: sut_vehicle
    npc: vehicle

    do parallel(overlap:equal):
        # Plan for SUT to drive with constant speed, without lane changes
        sut_v.drive() with:
            speed(30kph, run_mode: best_effort)
            keep_lane(run_mode: best_effort)
            duration([3..10]sec)
        # NPC will drive in the same lane as SUT, ahead of it,
        # and decrease its speed during the run. This will create
        # a collision course between the SUT and the NPC
        npc.drive() with:
            position(time: [3..5]second, ahead_of: sut.car, at: start)
            lane(same_as: sut.car, at: start)
            speed(25kph, at: start, run_mode: best_effort)
            change_speed([-10..-5]kph)
            avoid_collisions(false)
            keep_lane()

    # Calculate and record minimal time-to-collision

    var samples_count := 0
    var min_ttc : time
    on @top.clk:
        var curr_ttc := sut_v.get_ttc_to_object(npc)
        samples_count = samples_count + 1
        if curr_ttc < min_ttc or samples_count == 1:
            min_ttc = curr_ttc

    record(name: min_time_to_collision, expression: min_ttc, unit: millisecond, event: finish)

    # Simulate a scenario completion error.
    # min_time_to_collision will be recorded even though the scenario fails

    on elapsed(3sec):
        scenario_completion_error(incomplete_scenario, "Test reached max allowed duration")

Example: event declaration without event expression

OSC2 code: event declaration without event expression
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

extend test_config:
    set map = "$FTX_PACKAGES/maps/hooder.xodr"

extend top.main:
    do sut.car.drive(duration: 5s)


extend top.main:
    event arrived

Example: event declaration with event path

OSC2 code: event declaration with event path
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

extend test_config:
    set map = "$FTX_PACKAGES/maps/hooder.xodr"

extend top.main:
    do sut.car.drive(duration: 5s)

struct car_struct:
    event arrived

scenario traffic.scenario1:
    main_car: car_struct


    event arrived is @main_car.arrived

Example: event declaration with Boolean expression

OSC2 code: event declaration with Boolean expression
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

extend test_config:
    set map = "$FTX_PACKAGES/maps/hooder.xodr"

extend top.main:
    do sut.car.drive(duration: 5s)

scenario sut.scenario1:
    snow_depth: length


    event deep_snow is (snow_depth > 15cm)

Example: event declaration with condition

OSC2 code: event declaration with condition
import "$FTX/env/basic/exe_platforms/model_ssp/config/model_sumo_config.osc"

extend test_config:
    set map = "$FTX_PACKAGES/maps/hooder.xodr"


extend top.main:
    var sim_start_time := sample(top.time, @start)
    event sim_start is (top.time - sim_start_time == 0s)
    event sim_start_plus_1 is (top.time - sim_start_time == 1s) # 1 second after simulation start


    do wait elapsed(3s)

    on @sim_start:
        call logger.log_info("@sim_start emitted")
    on @sim_start_plus_1:
        call logger.log_info("@sim_start_plus_1 emitted")

Example: using event parameters

OSC2 code: using event parameters
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

extend test_config:
    set map = "$FTX_PACKAGES/maps/hooder.xodr"

extend top.main:
    encroaching_car: vehicle
    encroached_car: vehicle
    car1: vehicle

    event car_passing_by(info : pass_by_info)

    do sut.car.drive(duration: 5s)


extend top.main:

    event encroached_passed_by is @encroached_car.car_passing_by as dat  \
        if (dat.info.other_car == encroaching_car and dat.info.time_to_passing_by < config.test.step_time)

Example: use of event_occurrences() and event_occurred

OSC2 code: use of event_occurrences() and event_occurred
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

scenario sut.refill_fuel:
    nearest_station: length
    nearest_restaurant: length

    event passing_by_gas_station is (nearest_station < 2000m)
    event pick_up_food is (nearest_restaurant < 1000m)
    event interceptor_passed_by

    do serial:
       sut_drive: sut.car.drive() with:
           speed(speed: 30kph)
       emit interceptor_passed_by
       emit interceptor_passed_by
       emit interceptor_passed_by

extend test_config:
    set map = "$FTX_PACKAGES/maps/highway.xodr"

extend top.main:
    do serial(duration: [2..5]s):
       pt_to_event:sut.refill_fuel() with :
           keep(it.nearest_station == 1000m)
           keep(it.nearest_restaurant == 5000m)

    # Event occurs check
    on @pt_to_event.end if pt_to_event.passing_by_gas_station.event_occurrences() == 0:
        call other_error(assertion, "Event occurred $(pt_to_event.passing_by_gas_station.event_occurrences()) times, but not reported.")

    # Event does not occur check
    on @pt_to_event.end if pt_to_event.pick_up_food.event_occurred():
        call other_error(assertion, "Event pick_up_food reported but did not actually occur.")

    # Event occurred for the specified number of time
    on @pt_to_event.end if pt_to_event.interceptor_passed_by.event_occurrences() != 3 :
        call other_error(assertion, "Event occurrence count is wrong.")

Example: use of event_data()

OSC2 code: use of event_data()
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

scenario sut.refill_fuel:
    nearest_station: length

    event passing_by_gas_station is (nearest_station < 2000m)
    event check_fuel(fuel_empty: bool, some_info: string)

    do serial():
        sut_drive: sut.car.drive() with:
           speed(speed: 30kph)
        emit check_fuel(true, "Passed_by")

extend test_config:
    set map = "$FTX_PACKAGES/maps/highway.xodr"

extend top.main:
    do serial(duration: [2..5]s):
       pt_to_event: sut.refill_fuel() with :
           keep(it.nearest_station == 1000m)

    on @pt_to_event.end if pt_to_event.check_fuel.event_data().fuel_empty == false:
       call other_error(assertion, "fuel_empty: $(pt_to_event.check_fuel.event_data().fuel_empty)")

    on @pt_to_event.end if pt_to_event.check_fuel.event_data().some_info != "Passed_by":
       call other_error(assertion, "some_info: $(pt_to_event.check_fuel.event_data().some_info)")