OSC2 expressions examples
Example: predefined identifiers
OSC2 code: predefined identifiers
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.C:
c_val: int
scenario top.B:
b_val: int
do c1: C()
scenario top.A:
a_val: int
do b1: B()
# Example1:
extend top.A:
in1: in b1.c1 with:
keep(outer.a_val == self.b_val)
keep(self.b_val == it.c_val)
# In the above example:
# outer: is the A scenario
# self: is the B scenario
# it: is the c1 scenario inside the B scenario
# Example2:
extend top:
top_val: int
extend top.A:
in2: in b1 with:
keep(it.actor.top_val == it.b_val)
keep(self.a_val == it.b_val)
# In the above example:
# actor: is top
# self: is the A scenario
# it: is the b1 scenario
Example: 'self' field shadowed by 'it' field
OSC2 code: 'self' field shadowed by 'it' field
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 vehicle.a:
x: int
scenario vehicle.b:
x: int
scenario vehicle.x:
do serial():
a()
b()
with:
keep(it.duration == self.duration)
Example: pass keyword
OSC2 code: pass keyword
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.another_scenario:
speed: speed
actor my_actor:
pass
scenario top.some_scenario:
do serial:
pass
another_scenario() with:
pass
Example: boolean literals
OSC2 code: Boolean literals
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.my_scenario2:
true_value: bool with:
keep(it == true)
Example: enumerated type literals
OSC2 code: enumerated type literals
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"
extend test_config:
set map = "$FTX_PACKAGES/maps/hooder.xodr"
set min_test_time = 0second
enum rgb_color: [red, green, blue]
enum cmyk_color: [cyan = 1, magenta = 2, yellow, black]
extend top.main:
#Example enumeration fields
var my_rgb_color: rgb_color = green
var my_cmyk_color: cmyk_color = black
# Conversion to integer
var x: int = my_rgb_color.as(int) # x == 1
var y: uint = my_cmyk_color.as(uint) # y == 4
# Conversion from integer
var my_car_color: cmyk_color = 3.as(cmyk_color) # my_car_color == yellow
do call logger.log_info("x: $(x), y: $(y), my_car_color: $(my_car_color)")
Example: rise operator
OSC2 code: rise operator
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:
do serial:
accelerate: sut.car.drive(duration: 10s) with:
speed(20kph, at:start)
speed(50kph, at:end)
decelerate: sut.car.drive(duration: 10s) with:
speed(50kph, at:start)
speed(20kph, at:end)
accelerate_again: sut.car.drive(duration: 10s) with:
speed(20kph, at:start)
speed(50kph, at:end)
on rise(sut.car.state.speed > 30kph):
logger.log_info("SUT accelerated beyond 30kph")
Example: fall operator
OSC2 code: fall operator
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:
do serial:
accelerate: sut.car.drive(duration: 10s) with:
speed(20kph, at:start)
speed(50kph, at:end)
decelerate: sut.car.drive(duration: 10s) with:
speed(50kph, at:start)
speed(20kph, at:end)
accelerate_again: sut.car.drive(duration: 10s) with:
speed(20kph, at:start)
speed(50kph, at:end)
on fall(sut.car.state.speed > 30kph):
logger.log_info("SUT speed dropped below 30kph")
Example: casting struct types
OSC2 code: casting struct types
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)
enum color: [red, blue, green]
struct color_container:
the_color: color
extend top.main:
red: color_container
c: color with:
keep (it == self.red.the_color) # the first step is resolved to the field 'red'
Example: casting scalar types
OSC2 code: casting scalar types
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:
var x: string
set x = 5.as(string)
Example: list declaration
OSC2 code: list declaration
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.my_scenario2:
convoy: list of vehicle
distances: list of length
Example: a list constraint
OSC2 code: list constraint
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.my_scenario2:
distances: list of length with:
keep(it == [12km, 13.5km, 70km])
Example: string constraint
OSC2 code: string constraint
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 data:
text: string with:
keep(it == "Absolute speed of ego at start (in km/h)")
Example: string interpolation
OSC2 code: string interpolation
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.time_check:
my_time: time
cars: list of vehicle
do serial:
log_info("There are $(cars.size()) cars.")
Example: watcher operators
OSC code: watcher operators
import "$FTX/env/basic/exe_platforms/model_ssp/config/model_sumo_config.osc"
extend test_config:
set map = "$FTX_PACKAGES/maps/M75_FTX_highway_long_single_road.xodr"
global modifier sut.indicators_monitor:
# Create watcher to emit interval as long as left turn indicator is on
watcher left_turn_monitor is while_w(sut.car.state.vehicle_indicators.turn_signal_state == left_on)
# Create watcher to emit interval as long as right turn indicator is on
watcher right_turn_monitor is while_w(sut.car.state.vehicle_indicators.turn_signal_state == right_on)
# Create watcher to emit interval as long as vehicle is moving to the left
watcher left_move_monitor is while_w(sut.car.state.road_speed.lat > 0.1mps)
# Create watcher to emit interval as long as vehicle is moving to the right
watcher right_move_monitor is while_w(sut.car.state.road_speed.lat < -0.1mps)
# Create watcher to emit interval as long as no left turn indicator
watcher no_left_turn_monitor is not_w(left_turn_monitor)
# Create watcher to emit interval as long as no right turn indicator
watcher no_right_turn_monitor is not_w(right_turn_monitor)
# Create watcher to emit interval when vehicle is moving left without indication
watcher left_move_no_indication_monitor is and_w(left_move_monitor, no_left_turn_monitor)
# Create watcher to emit interval when vehicle is moving right without indication
watcher right_move_no_indication_monitor is and_w(right_move_monitor, no_right_turn_monitor)
# Create watcher to emit interval when vehicle is moving to any direction without indication
watcher move_no_indication_monitor is or_w(left_move_no_indication_monitor, right_move_no_indication_monitor)
# Create watcher to emit interval between start of lane switch and end of lane switch
watcher switch_lane_monitor is between_w(x: @sut.car.switch_lane_start, y: @sut.car.switch_lane_end)
# Create watcher to emit zero-time intervals when vehicle is starting switching lanes
watcher switch_lane_start_monitor is upon_w(ev: @sut.car.switch_lane_start)
scenario sut.drive_with_indicators:
do serial:
# Start driving at lane 1
sut.car.drive() with:
lane(1, at: start)
duration(3s, run_mode: best_effort)
# Turn on right turn signal
sut.car.turn_signal_lights_state(right_on)
# Swicth to lane 2
sut.car.drive() with:
lane(2, at: end)
duration([2..5]s, run_mode: best_effort)
# Turn off turn signal
sut.car.turn_signal_lights_state(off)
# Switch to lane 1
sut.car.drive() with:
lane(1, at: end)
duration([2..5]s, run_mode: best_effort)
extend top.main:
do a: sut.drive_with_indicators()
Example: watcher operator above_w
OSC code: watcher operator above_w
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:
my_car: vehicle
do serial(duration: [10..30]sec):
drv1: my_car.drive() with:
speed(40kph, at: start)
drv2: my_car.drive() with:
speed(60kph, at: start)
speed(40kph, at: end)
watcher above_50 is above_w(sample_type: speed, sample_expression: my_car.state.speed, threshold: 50kph, tolerance: 5kph)
on @above_50.i_start:
logger.log_info("Interval started. Car speed: $(my_car.state.speed)")
on @above_50.i_end:
logger.log_info("Interval ended. Car speed: $(my_car.state.speed)")
Example: watcher operator above_w record
OSC code: watcher operator above_w
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:
my_car: vehicle
do serial(duration: [10..30]sec):
drv1: my_car.drive() with:
speed(40kph, at: start)
drv2: my_car.drive() with:
speed(60kph, at: start)
speed(40kph, at: end)
extend top.main:
watcher above_50(my_interval_data) is above_w(sample_type: speed, sample_expression: my_car.state.speed, threshold: 50kph, tolerance: 5kph)
struct my_interval_data inherits above_w_speed_data:
record(max, expression: max_value, unit: kph)
record(max_time, expression: max_value_time, unit: second)
Example: watcher operator above_w dynamically changing the threshold
OSC code: watcher operator above_w dynamically changing the threshold
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:
my_car: vehicle
do serial(duration: [10..50]sec):
drive1: my_car.drive() with: # 1st period of acceleration
speed(40kph, at: start)
drive2: my_car.drive() with:
speed(60kph, at: start)
drive3: my_car.drive() with: # 2nd period of acceleration
speed(40kph, at: start)
drive4: my_car.drive() with:
speed(60kph, at: start)
speed(40kph, at: end)
var speed_threshold := 50kph
watcher my_watcher is above_w(sample_type: speed, sample_expression: my_car.state.speed, threshold: speed_threshold, tolerance: 5kph)
on @drive3.start:
speed_threshold = 100kph
on @my_watcher.i_start:
logger.log_info("Interval started. Car speed: $(my_car.state.speed)")
on @my_watcher.i_end:
logger.log_info("Interval ended. Car speed: $(my_car.state.speed)")
Example: watcher operator below_w
OSC code: watcher operator below_w
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:
my_car: vehicle
do serial(duration: [10..30]sec):
drv1: my_car.drive() with:
speed(60kph, at: start)
drv2: my_car.drive() with:
speed(40kph, at: start)
speed(60kph, at: end)
watcher below_50 is below_w(sample_type: speed, sample_expression: my_car.state.speed, threshold: 50kph, tolerance: 5kph)
on @below_50.i_start:
logger.log_info("Interval started. Car speed: $(my_car.state.speed)")
on @below_50.i_end:
logger.log_info("Interval ended. Car speed: $(my_car.state.speed)")
Example: watcher operator below_w record
OSC code: watcher operator below_w
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:
my_car: vehicle
do serial(duration: [10..30]sec):
drv1: my_car.drive() with:
speed(60kph, at: start)
drv2: my_car.drive() with:
speed(40kph, at: start)
speed(60kph, at: end)
extend top.main:
watcher below_50 is below_w(sample_type: speed, sample_expression: my_car.state.speed, threshold: 50kph, tolerance: 5kph)
struct my_interval_data inherits below_w_speed_data:
record(min, expression: min_value, unit: kph)
record(min_time, expression: min_value_time, unit: second)
Example: watcher operator below_w dynamically changing the threshold
OSC code: watcher operator below_w dynamically changing the threshold
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:
my_car: vehicle
do serial(duration: [10..50]sec):
drive1: my_car.drive() with:
speed(60kph, at: start) # 1st period of deceleration
drive2: my_car.drive() with:
speed(40kph, at: start)
speed(60kph, at: end)
drive3: my_car.drive() with:
speed(60kph, at: start) # 2nd period of deceleration
drive4: my_car.drive() with:
speed(40kph, at: start)
speed(60kph, at: end)
var speed_threshold := 50kph
watcher my_watcher is below_w(sample_type: speed, sample_expression: my_car.state.speed, threshold: speed_threshold, tolerance: 5kph)
on @drive3.start:
speed_threshold = 0kph
on @my_watcher.i_start:
logger.log_info("Interval started. Car speed: $(my_car.state.speed)")
on @my_watcher.i_end:
logger.log_info("Interval ended. Car speed: $(my_car.state.speed)")
Example: represent scenario libraries as global actors
OSC2 code: represent scenario libraries as global actors
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"
extend test_config:
set map = "$FTX_PACKAGES/maps/hooder.xodr"
actor my_sim:
field1: int
extend top:
my_sim: my_sim
extend top.main:
do sut.car.drive(duration: 5s)
Example: path resolution using the implicit variable
OSC2 code: path resolution using the implicit variable
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)
actor z:
x: int
scenario z.y:
keep(x == 2) # resolves to z.x
Example: resolving field and constant with same name
OSC2 code: resolving field and constant with same name
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"
extend test_config:
set map = "$FTX_PACKAGES/maps/hooder.xodr"
set min_test_time = 0second
enum color: [red, green, blue]
extend top.main:
red: color with: # field has same name as an enum constant 'red'
keep (it == blue)
c: color with:
keep (it == red) # 'c' is type color, so 'red' is resolved
# as the enum constant 'red', not the value of the field 'red'
def foo(c: color) is empty
do call foo(red) # foo's parameter 'c' is type 'color', so 'red' is resolved
# as the enum constant 'red'
def foo(c: color) is also:
call logger.log_info("Called foo() method. c: $(c)")
on @start:
call logger.log_info("Value of 'red' field: $(red). Value of 'c' field: $(c)")
Example: refer explicitly to a field
OSC2 code: refer explicitly to a field
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)
enum color: [red, green, blue]
extend top.main:
red: color with:
keep (it == blue)
c: color with:
keep (it == self.red) # right-hand side is resolved to the field 'red'
Example: unambiguous field reference
OSC2 code: unambiguous field reference
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)
enum color: [red, green, blue]
struct color_container:
the_color: color
extend top.main:
red: color_container
c: color with:
keep (it == self.red.the_color) # the first step is resolved to the field 'red'
Example: user-defined labels
OSC2 code: user-defined labels
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"
extend test_config:
set map = "$FTX_PACKAGES/maps/hooder.xodr"
extend top.main:
in cut_in_and_stop.label(start_behind_sut.sut) with: speed([30..70]kph)
in cut_in_and_stop.cut_in with: keep(it.duration in [5..7]second)
in cut_in_and_stop.cut_in.label(change_lane.car1) with:
speed([50..70]kph)
lane(1)
do cut_in_and_stop: sut.cut_in_and_stop() # cut_in_and_stop
scenario sut.cut_in_and_stop:
car1: vehicle
side: av_side
do serial: # cut_in_and_stop
start_behind_sut: parallel(overlap:equal): # cut_in_and_stop.start_behind_sut
sut.car.drive() # cut_in_and_stop.label(start_behind_sut.sut)
car1.drive() # cut_in_and_stop.label(start_behind_sut.car1)
cut_in: cut_in(car1: car1, side: side) # cut_in_and_stop.cut_in
scenario sut.cut_in:
car1: vehicle
side: av_side
do serial: # cut_in_and_stop.cut_in
change_lane: parallel(overlap:equal): # cut_in_and_stop.cut_in.change_lane
sut.car.drive() # cut_in_and_stop.cut_in.label(change_lane.sut)
car1.drive() # cut_in_and_stop.cut_in.label(change_lane.car1)
Example: automatic labels"
OSC2 code: automatic labels
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"
extend test_config:
set map = "$FTX_PACKAGES/maps/hooder.xodr"
extend top.main:
in label(sut).label(serial.parallel.sut) with: speed([30..70]kph)
in label(sut).label(serial.cut_in).label(serial) with:
keep(it.duration in [5..7]second)
in label(sut).label(serial.cut_in).label(serial.parallel.car1) with:
speed([50..70]kph)
lane(1)
do sut.cut_in_and_stop() # label(sut)
scenario sut.cut_in_and_stop:
car1: vehicle
side: av_side
do serial: # label(sut).label(serial)
parallel(overlap:equal): # label(sut).label(serial.parallel)
sut.car.drive() # label(sut).label(serial.parallel.sut)
car1.drive() # label(sut).label(serial.parallel.car1)
cut_in(car1: car1, side: side) # label(sut).label(serial.cut_in)
scenario sut.cut_in:
car1: vehicle
side: av_side
# label(sut).label(serial.cut_in).label(serial)
do serial:
# label(sut).label(serial.cut_in).label(serial.parallel)
parallel(overlap:equal):
# label(sut).label(serial.cut_in).label(serial.parallel.sut)
sut.car.drive()
# label(sut).label(serial.cut_in).label(serial.parallel.car1)
car1.drive()
Example: referencing scenario modifiers
OSC2 code: referencing scenario modifiers
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.scen1:
car1: vehicle
do serial: # label(serial)
car1.drive() with: keep_lane() # label(serial.car1)
car1.drive() with: # label(serial.car1(2))
lane(1) # label(serial.car1(2).lane)
speed(speed: [30..120]kph) # label(serial.car1(2).speed)
with:
keep(label(serial.car1(2).speed).speed == 45kph)