Skip to content

Behavior specification examples

Example: behavior specification with do

OSC2 code: behavior specification with do
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 top.turn:
    speed: speed

scenario top.yield:
    speed: speed


scenario vehicle.zip:
    do serial():
        t: turn()
        y: yield()

Example: extend top.main with do

OSC2 code: extend top.main with do
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

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

scenario vehicle.zip:
    speed: speed

    do actor.drive() with:
        duration([5..10]second)
        change_lane()


extend top.main:
    car1: vehicle

    do z: car1.zip()

Example: extend scenario with do

OSC2 code: extend scenario with do
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 top.intercept:
    speed: speed

scenario vehicle.zip:
    speed: speed


extend vehicle.zip:
    do top.intercept()

Example: explicit label for do

OSC2 code: explicit label for do
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

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


extend top.main:
    car1: vehicle

    do a: serial():                # explicit label "a"
        car1.drive() with:
            s1: speed(0kph, at: start)
            s2: speed(10kph, at: end)

Example: behavior specification with previous_do()

OSC2 code: behavior specification with previous_do()
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

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


scenario top.scenario1:
  flag: bool

  do log("Scenario 1 is executing")

extend top.scenario1:

  do serial:
    previous_do()
    log("Extending scenario 1 behavior")

extend top.main:

  do serial:
    scenario1()

Example: behavior specification with scenario invocation

OSC2 code: behavior specification with scenario invocation
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 traffic.two_phases:   # Scenario name
    # Define the cars with specific attributes
    car1: vehicle with:
        keep(it.color == green)
        keep(it.vehicle_category == box_truck)

    # Define the behavior
    do serial():
        phase1: car1.drive() with:
            spd1: speed(0kph, at: start)
            spd2: speed(10kph, at: end)
        phase2: car1.drive() with:
            speed([10..15]kph)

Example: behavior specification with call

OSC2 code: behavior specification with call
import "$FTX_PACKAGES/base_scenarios/scenarios/vehicle_cut_in/vehicle_cut_in_and_slow/vehicle_cut_in_and_slow_top.osc"

import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

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

extend vehicle:
    def calculate_dist_to_other_car(other_car: vehicle) is undefined


extend top.main:
    on @c.phase_essence_slow.start:
        call sut.car.calculate_dist_to_other_car(c.cut_in_vehicle)

    do c: sut.vehicle_cut_in_and_slow()

Example: behavior specification with emit

OSC2 code: behavior specification with emit
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"
import "$FTX_PACKAGES/base_scenarios/scenarios/oncoming_vehicles/oncoming/oncoming_top.osc"

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


extend top.main:
    event second_after_pass_by
    do parallel(start_to_start:[0..0]s, overlap:any):
        onc: sut.oncoming()
        smp: serial():
            wait @onc.oncoming_vehicle.car_passing_by
            wait elapsed(1s)
            log_info("Sampling dut speed: $(sut.car.state.speed)")
            emit second_after_pass_by
    var dut_speed:=sample(sut.car.state.speed,
     @second_after_pass_by) with:
        cover(dut_speed_second_after_pass_by, expression: it,
        unit:kph,range:[0..200], every:10)

Example: behavior specification with 'on'

OSC2 code: behavior specification with 'on'
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 top.scenario1:
    car1: vehicle

    event near_collision

    var near_collisions_count := 0

    on @near_collision:
        near_collisions_count = near_collisions_count + 1
        logger.log_info("Near collision occurred.")

    do serial:
        car1.drive()

Example: watcher that creates intervals that record maximal speed

OSC2 code: watcher that creates intervals that record maximal speed
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

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


# Define custom watcher data that records the maximum speed during
# an interval
struct speed_above_data inherits any_watcher_data:
    max_speed: speed
    record(max_speed, unit: kph)

# Define a watcher type that create intervals
# when the speed of a vehicle exceeds some speed limit,
# and records the maximal exceeding speed
watcher modifier speed_above(speed_above_data):
    v: vehicle
    limit: speed

    on @top.w_clk:
        if(data == null):
            if(v.state.speed > limit):
                start_interval()
        else:
            if(v.state.speed <= limit):
                end_interval()
    on @i_clock:
        if(data.max_speed < v.state.speed):
            data.max_speed = v.state.speed

global modifier vehicle.speed_watchers:
    # Instantiate the watcher for all the vehicle actors
    watcher speed_above_w is speed_above(actor, 30kph)


extend top.main:
    on @sut.car.speed_watchers.speed_above_w.i_clock:
        logger.log_info("$(sut.car.state.speed)")
    on @sut.car.speed_watchers.speed_above_w.i_end:
        logger.log_info("MAX: $(sut.car.speed_watchers.speed_above_w.data.max_speed)")

    do serial:
        sut.car.drive(duration: [5sec..10sec]) with:
            speed([20kph..25kph], at: start)
            speed([35kph..40kph], at: end)
        sut.car.drive(duration: [5sec..10sec]) with:
            speed([20kph..25kph], at: end)

Example: on-the-fly watcher that creates intervals that record maximal speed

OSC2 code: watcher that creates intervals that record maximal speed
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

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


# Define watcher data that records a speed
struct speed_watcher_data inherits any_watcher_data:
    speed: speed
    record(speed, unit: kph)

extend top.main:
    watcher above_speed(speed_watcher_data) is any_watcher_behaviour() # Instantiate the watcher

    on @top.clk:
        if(above_speed.data == null):
            if(sut.car.state.speed > 30kph):     # when the speed of the vehicle exceeds 30kph start the interval
                above_speed.start_interval()
        else:
            if(sut.car.state.speed <= 30kph):    # when the speed of the vehicle is less than 30kph end the interval
                above_speed.end_interval()

    on @above_speed.i_clock:               # during the interval record the maximal speed
        if(above_speed.data.speed < sut.car.state.speed): above_speed.data.speed = sut.car.state.speed



    on @above_speed.i_clock:
        logger.log_info("$(sut.car.state.speed)")
    on @above_speed.i_end:
        logger.log_info("MAX: $(above_speed.data.speed)")

    do serial:
        sut.car.drive(duration: [5sec..10sec]) with:
            speed([20kph..25kph], at: start)
            speed([35kph..40kph], at: end)
        sut.car.drive(duration: [5sec..10sec]) with:
            speed([20kph..25kph], at: end)

Example: checker that creates issues when speed limit exceeds

OSC2 code: checker that creates issues when speed limit exceeds
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 issue_kind: [too_fast] #define a new issue kind

# Define a watcher type that create intervals
# when the speed of a vehicle exceeds some speed limit
watcher modifier speed_above:
    v: vehicle
    limit: speed
    var max_speed: speed

    on @top.w_clk:
        if(data == null):
            if(v.state.speed > limit):
                start_interval()
        else:
            if(v.state.speed <= limit):
                end_interval()
    on @i_clock:
        if(max_speed < v.state.speed):
            max_speed = v.state.speed

global modifier sut_vehicle.speed_checkers:
    checker check_speed_above is speed_above(actor, 30kph) with:
        it.sut_error(kind: too_fast,
            details: "Vehicle $(it.v) exceeded maximal speed $(it.limit) reaching up to $(it.max_speed)")


extend top.main:
    do serial:
        sut.car.drive(duration: [5sec..10sec]) with:
            speed([20kph..25kph], at: start)
            speed([35kph..40kph], at: end)
        sut.car.drive(duration: [5sec..10sec]) with:
            speed([20kph..25kph], at: end)

Example: checker with set_issue to issue an informative interval

OSC2 code: checker with set_issue to issue an informative interval
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

extend issue_kind: [too_fast] #define a new issue kind

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

# Define a watcher type that create intervals
# when the speed of a vehicle exceeds some speed limit
watcher modifier speed_above:
    v: vehicle
    limit: speed
    var max_speed: speed

    on @top.w_clk:
        if(data == null):
            if(v.state.speed > limit):
                start_interval()
        else:
            if(v.state.speed <= limit):
                end_interval()
    on @i_clock:
        if(max_speed < v.state.speed):
            max_speed = v.state.speed

global modifier vehicle.speed_checkers:
    checker check_speed_above is speed_above(actor, 30kph) with:
        it.sut_error(kind: too_fast, 
            details: "Vehicle $(it.v) exceeded maximal speed $(it.limit) reaching up to $(it.max_speed)")

extend top.main:
    do serial:
        sut.car.drive(duration: [5sec..10sec]) with:
            speed([20kph..25kph], at: start)
            speed([35kph..40kph], at: end)
        sut.car.drive(duration: [5sec..10sec]) with:
            speed([20kph..25kph], at: end)


extend top.main:
    set_issue(target_checker: sut.car.speed_checkers.check_speed_above, severity: info)

Example: behavior specification with until

OSC2 code: behavior specification with until
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 top.scenario1:
    car1: vehicle

    event e1


    # Example 1

    do serial:
        phase1: car1.drive() with:
            speed(40kph)
            until(@e1)
        phase2: car1.drive() with:
            speed(80kph)
            until(@e1)

Example: behavior specification with wait

OSC2 code: behavior specification with wait
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:
    event my_event

scenario top.scenario1:
    a: uint
    b: uint


    do serial:
        w1: wait @top.my_event
        w2: wait @top.my_event if a > b
        w3: wait (a > b)

Example: behavior specification with wait elapsed

OSC2 code: behavior specification with wait elapsed
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

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

scenario top.scenario1:
    max: time with:
        keep(it <= 10s)


    do serial:
        w1: wait elapsed([3..5]second)

        # max is a field defined with type time and constrained to a value
        w2: wait elapsed(max)


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

Example: behavior specification with copy structs

OSC2 code: behavior specification with copy structs
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

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

struct other_struct

struct my_struct:
    var x: int
    var s: other_struct

extend top.main:

    def test() is:

        var original := new my_struct
        original.x = 123
        original.s = new other_struct
        var copy := original.copy()
        logger.log_info("$(copy != original), $(copy.x), $(copy.s == original.s)")


    do serial:
        call test()
        sut.car.drive(duration: 5s)

Example: behavior specification to retrieve environmental variables

OSC2 code: behavior specification to retrieve environmental variables
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

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

extend top.main:

    do serial:
        call logger.log_info("$FTX is $(os.getenv("FTX"))")
        sut.car.drive() with: speed([30..60]kph)
    with:
        duration([3..7]s)

Example: call method example

OSC2 code: call method example
import "$FTX_PACKAGES/base_scenarios/scenarios/vehicle_cut_in/vehicle_cut_in_and_slow/vehicle_cut_in_and_slow_top.osc"

import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

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

extend vehicle:
    def calculate_dist_to_other_car(other_car: vehicle) is undefined


extend top.main:
    on @c.phase_essence_slow.start:
        call sut.car.calculate_dist_to_other_car(c.cut_in_vehicle)

    do c: sut.vehicle_cut_in_and_slow()

Example: simple concatenation example

OSC2 code: simple concatenation example
import "$FTX_QA/regression/config/av_fake_ssp.osc"

extend top.main:

    ###### Example starts here

    def test_join_to_string() is:
        var strings: list of string = ["one", "two", "three"]
        logger.log_info(strings.join_to_string("-","{","}")) # prints "{one-two-three}"
        logger.log_info(strings.join_to_string()) # prints "one, two, three"

    ###### Example ends here

    do call test_join_to_string()

Example: print vehicle states

OSC2 code: print vehicle states
import "$FTX/env/basic/exe_platforms/sumo_ssp/config/sumo_config.osc"

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

extend top.main:

    v1: vehicle with:
        keep(it.color == green)
    v2: vehicle with:
        keep(it.color == blue)

    do parallel:
        d1: sut.car.drive()
        d2: v1.drive() with:
            speed(10kph, at: start)
        d3: v2.drive() with:
            speed(15kph, at: start)
    with:
        duration([3..5]second)

    def print_vehicles_state() is:
        var all_vehicles := [sut.car, v1, v2]
        var state_str := all_vehicles.map("$(it.color): $(it.state.speed)").join_to_string(prefix: "Vehicles state: ")
        logger.log_info(state_str)

    on @start:
        print_vehicles_state()

Example: string method length()

OSC2 code: string method length()
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

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


extend top.main:

    def test() is:

        var str := "hello world"
        logger.log_info("Length is: $(str.length())")


    do serial:
        call test()
        sut.car.drive(duration: 5s)

Example: string method contains()

OSC2 code: string method contains()
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

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


extend top.main:

    def test() is:

        var str := "red green blue"
        logger.log_info("$(str.contains("green")), $(str.contains("yellow"))")


    do serial:
        call test()
        sut.car.drive(duration: 5s)

Example: string method substring()

OSC2 code: string method substring()
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

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


extend top.main:

    def test() is:

        var str := "red green blue"
        logger.log_info("$(str.substring(4,9))")


    do serial:
        call test()
        sut.car.drive(duration: 5s)

Example: string method split()

OSC2 code: string method split()
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

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


extend top.main:

    def test() is:

        var str := "red,green,blue"
        var colors := str.split(",")
        logger.log_info("Color #2: $(colors[1])")


    do serial:
        call test()
        sut.car.drive(duration: 5s)

Example: string method replace()

OSC2 code: string method replace()
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

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


extend top.main:

    def test() is:

        var str := "red,green,blue"
        logger.log_info("$(str.replace("re", "-"))")


    do serial:
        call test()
        sut.car.drive(duration: 5s)

Example: ftx_provided() method

OSC2 code: ftx_provided() method
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

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


scenario my_scenario:
    speed_param: speed

    # Example: Using ftx_provided() with keep
    keep(not ftx_provided("speed_param") => speed_param == 60kph)

    on @start:
       if (top.time == 5s):
            # Example: Using ftx_provided() with assertion
            call assertion_error_if(not ftx_provided("speed_param"), "speed_param should not be provided")

# Example: Using ftx_provided() with external_match_data and sample_if
@external_match(name: "cut_in")
scenario sut.test_ftx_provided_external:
    @external_match_data()
    var speed_at_cut_in: speed

    # Only collect coverage when field was provided from external data
    cover(speed_at_cut_in, unit: kph, sample_if: ftx_provided("speed_at_cut_in"))

Example: string method trim()

OSC2 code: string method trim()
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

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


extend top.main:

    def test() is:

        var str := "   hello world   "
        logger.log_info("*$(str.trim())*")


    do serial:
        call test()
        sut.car.drive(duration: 5s)