Skip to content

Commit 2d9fdcc

Browse files
feat: DSL standard library — action hierarchy & environment (p7-s5) (#99)
Adds §8.8.1's action hierarchy and the whole of §8.10/§8.11 to osc.standard.domain: osc_action with action_for_environment and action_for_movable_object under it; the environment actor with its local_to_unix_time convenience method; the weather, air, precipitation, wind, fog, clouds and celestial_light_source structs; and the seven environment actions. An action's declared name is a qualified behavior name (§7.2.2.2.5), which is why the standard can have both a struct 'air' and an action 'environment.air' without a collision, and why an action's 'inherits' names the base by its qualified behavior name too. Checking these is in scope for v0.0.1; executing them is not — the DSL environment actions stay Post in the execution matrix. Refs #43
1 parent 8fb78d8 commit 2d9fdcc

3 files changed

Lines changed: 212 additions & 2 deletions

File tree

‎docs/roadmap/coverage/osc-dsl-coverage.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ included, so a conforming implementation and Scena agree.
111111
| `std` — physical-object actors, structs and enums | §8.7 | In | p7-s5 | **Landed** (`dsl_stdlib_test.cpp`): the `osc_actor` → `physical_object` → `movable_object` → `traffic_participant` → `vehicle` → `trailer` chain with `stationary_object`, `person` and `animal`; `bounding_box`, `axle`, `hitch_receiver`, `hitch_coupler`; all 11 enums including the backward-compatibility spellings (`truck = heavy_truck`, `fire = fire_brigade`, …) sharing their replacement's value; the §8.7.6.1 measurement methods. §8.7.26 (traffic-participant groups) is non-normative and excluded |
112112
| `std` — road abstraction classes | §8.12.3–§8.12.41 | In | p7-s5 | **Landed** (`dsl_stdlib_test.cpp`): the `route` → `route_element` hierarchy with `road`, `lane_section`, `lane`, `crossing`, the four point structs, `path`/`trajectory` and their relative variants, `compound_route`/`compound_lane`, `junction`, and 15 enums. The road-dependent `physical_object` and `traffic_participant` methods arrive here as `extend` blocks, which is how the standard prints their prototypes |
113113
| `std` — the `map` actor | §8.12.2 | In | p7-s5 | Lands with the next slice of #43; §8.12.2's method and modifier surface is larger than the rest of §8.12 combined |
114-
| `std` — actions, modifiers, environment, traffic lights | §8.3, §8.5, §8.8–§8.11, §8.15 | In | p7-s5 | Lands with the remaining slices of #43 |
114+
| `std` — action hierarchy and the environment | §8.8.1, §8.10, §8.11 | In | p7-s5 | **Landed** (`dsl_stdlib_test.cpp`): `osc_action` with `action_for_environment` and `action_for_movable_object`; the `environment` actor with `local_to_unix_time`; the `weather`/`air`/`precipitation`/`wind`/`fog`/`clouds`/`celestial_light_source` structs; the seven §8.11 environment actions. Their *execution* stays Post (see the actor table) — the DSL environment actions are checked, not run, in v0.0.1 |
115+
| `std` — movement actions and modifiers, traffic lights, the `map` actor | §8.3, §8.5, §8.8.2–§8.9, §8.12.2, §8.15 | In | p7-s5 | Lands with the remaining slices of #43 |
115116

116117
Coverage of the individual §8 declarations: all physical types and units
117118
(§8.14.1), compound structs (§8.14.2), string methods (§8.13), all actors

‎frontends/dsl/src/stdlib.cpp‎

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,13 +601,111 @@ extend traffic_participant:
601601
def distance_along_route(route: route, from: route_distance_enum = from_start) -> length is undefined
602602
)OSC";
603603

604+
// §8.8.1 the action hierarchy, and §8.10/§8.11 the environment actor with its
605+
// structs and its actions. Transcribed from the parameter tables of
606+
// §8.8.1.1, §8.10.2–§8.10.9 and §8.11.2–§8.11.8.
607+
constexpr std::string_view kDomainEnvironment = R"OSC(
608+
# --- §8.8.1.1 action osc_action ----------------------------------------------
609+
# "The parent action `osc_action` is the base class for all actions in the ASAM
610+
# OpenSCENARIO domain model and it is associated with the parent actor
611+
# `osc_actor`", with `action_for_environment` and `action_for_movable_object`
612+
# as its two children.
613+
action osc_actor.osc_action
614+
615+
action environment.action_for_environment inherits osc_actor.osc_action
616+
617+
action movable_object.action_for_movable_object inherits osc_actor.osc_action
618+
619+
# --- §8.10.4 struct air ------------------------------------------------------
620+
struct air:
621+
temperature: temperature
622+
pressure: pressure
623+
relative_humidity: float
624+
625+
# --- §8.10.5 struct precipitation --------------------------------------------
626+
# §8.10.5: volumetric flux reduces to the same dimension as speed, and the
627+
# language cannot declare two physical types over one unit, so the standard
628+
# uses `speed` here too.
629+
struct precipitation:
630+
intensity: speed
631+
632+
# --- §8.10.6 struct wind -----------------------------------------------------
633+
struct wind:
634+
speed: speed
635+
direction: angle
636+
637+
# --- §8.10.7 struct fog ------------------------------------------------------
638+
struct fog:
639+
visual_range: length
640+
641+
# --- §8.10.8 struct clouds ---------------------------------------------------
642+
struct clouds:
643+
cloudiness: uint
644+
645+
# --- §8.10.9 struct celestial_light_source -----------------------------------
646+
struct celestial_light_source:
647+
position: celestial_position_2d
648+
649+
# --- §8.10.3 struct weather --------------------------------------------------
650+
struct weather:
651+
air: air
652+
rain: precipitation
653+
snow: precipitation
654+
wind: wind
655+
fog: fog
656+
clouds: clouds
657+
658+
# --- §8.10.2 actor environment -----------------------------------------------
659+
actor environment inherits osc_actor:
660+
geodetic_position: geodetic_position_2d
661+
datetime: time
662+
sun: celestial_light_source
663+
moon: celestial_light_source
664+
weather: weather
665+
def local_to_unix_time(year: uint, month: uint, day: uint, hour: uint, minute: uint, second: uint, time_zone: float) -> time is undefined
666+
667+
# --- §8.11.2 action air ------------------------------------------------------
668+
action environment.air inherits environment.action_for_environment:
669+
temperature: temperature
670+
pressure: pressure
671+
relative_humidity: float
672+
673+
# --- §8.11.3 action rain -----------------------------------------------------
674+
action environment.rain inherits environment.action_for_environment:
675+
intensity: speed
676+
677+
# --- §8.11.4 action snow -----------------------------------------------------
678+
action environment.snow inherits environment.action_for_environment:
679+
intensity: speed
680+
681+
# --- §8.11.5 action wind -----------------------------------------------------
682+
action environment.wind inherits environment.action_for_environment:
683+
speed: speed
684+
direction: angle
685+
686+
# --- §8.11.6 action fog ------------------------------------------------------
687+
action environment.fog inherits environment.action_for_environment:
688+
visual_range: length
689+
690+
# --- §8.11.7 action clouds ---------------------------------------------------
691+
action environment.clouds inherits environment.action_for_environment:
692+
cloudiness: uint
693+
694+
# --- §8.11.8 action assign_celestial_position --------------------------------
695+
action environment.assign_celestial_position inherits environment.action_for_environment:
696+
light_source: celestial_light_source
697+
azimuth: angle
698+
elevation: angle
699+
)OSC";
700+
604701
const std::string& types_module() {
605702
static const std::string source = std::string(kTypesScalars) + std::string(kTypesCompound);
606703
return source;
607704
}
608705

609706
const std::string& domain_module() {
610-
static const std::string source = std::string(kDomainEntities) + std::string(kDomainRoads);
707+
static const std::string source =
708+
std::string(kDomainEntities) + std::string(kDomainRoads) + std::string(kDomainEnvironment);
611709
return source;
612710
}
613711

‎frontends/dsl/tests/dsl_stdlib_test.cpp‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,117 @@ TEST(DslStdlibTest, AnEnumLiteralSharedAcrossTheLibraryNeedsItsEnumName) {
756756
EXPECT_TRUE(explained);
757757
}
758758

759+
// --- §8.8.1 the action hierarchy, §8.10/§8.11 the environment ---------------
760+
761+
TEST(DslStdlibTest, TheActionHierarchyIsRootedInOscAction) {
762+
// §8.8.1.1: `osc_action` is "the base class for all actions ... associated
763+
// with the parent actor `osc_actor`", with two children.
764+
Library library;
765+
check_full_library(library);
766+
const auto base = library.program.types_by_name.find("std::osc_actor.osc_action");
767+
ASSERT_NE(base, library.program.types_by_name.end());
768+
EXPECT_EQ(library.program.types[base->second].kind, TypeKind::Action);
769+
for (const char* child : {"std::environment.action_for_environment",
770+
"std::movable_object.action_for_movable_object"}) {
771+
const auto id = library.program.types_by_name.find(child);
772+
ASSERT_NE(id, library.program.types_by_name.end()) << child;
773+
EXPECT_TRUE(library.program.is_derived_from(id->second, base->second)) << child;
774+
}
775+
}
776+
777+
TEST(DslStdlibTest, TheEnvironmentActorCarriesItsStructsAndItsMethod) {
778+
Library library;
779+
check_full_library(library);
780+
const auto environment = library.program.types_by_name.find("std::environment");
781+
ASSERT_NE(environment, library.program.types_by_name.end());
782+
EXPECT_EQ(library.program.types[environment->second].kind, TypeKind::Actor);
783+
for (const char* field : {"geodetic_position", "datetime", "sun", "moon", "weather"}) {
784+
EXPECT_NE(library.program.find_field(environment->second, field), nullptr) << field;
785+
}
786+
// §8.10.2.1.1's seven-argument convenience method.
787+
const scena::dsl::MethodInfo* method =
788+
library.program.find_method(environment->second, "local_to_unix_time");
789+
ASSERT_NE(method, nullptr);
790+
EXPECT_EQ(method->parameters.size(), 7U);
791+
ASSERT_NE(method->return_type, scena::dsl::kInvalidType);
792+
EXPECT_EQ(library.program.types[method->return_type].name, "stdtypes::time");
793+
}
794+
795+
TEST(DslStdlibTest, TheWeatherStructsAreDeclaredWithTheirFields) {
796+
Library library;
797+
check_full_library(library);
798+
struct Case {
799+
const char* name;
800+
std::vector<const char*> fields;
801+
};
802+
for (const Case& expected :
803+
{Case{"std::weather", {"air", "rain", "snow", "wind", "fog", "clouds"}},
804+
Case{"std::air", {"temperature", "pressure", "relative_humidity"}},
805+
Case{"std::precipitation", {"intensity"}}, Case{"std::wind", {"speed", "direction"}},
806+
Case{"std::fog", {"visual_range"}}, Case{"std::clouds", {"cloudiness"}},
807+
Case{"std::celestial_light_source", {"position"}}}) {
808+
const auto id = library.program.types_by_name.find(expected.name);
809+
ASSERT_NE(id, library.program.types_by_name.end()) << expected.name;
810+
EXPECT_EQ(library.program.types[id->second].kind, TypeKind::Struct) << expected.name;
811+
for (const char* field : expected.fields) {
812+
EXPECT_NE(library.program.find_field(id->second, field), nullptr)
813+
<< expected.name << "." << field;
814+
}
815+
}
816+
// §8.10.5: volumetric flux reduces to the dimension of speed, and the
817+
// language cannot declare two physical types over one unit, so the
818+
// standard types precipitation intensity as a speed.
819+
const auto precipitation = library.program.types_by_name.find("std::precipitation");
820+
ASSERT_NE(precipitation, library.program.types_by_name.end());
821+
const scena::dsl::FieldInfo* intensity =
822+
library.program.find_field(precipitation->second, "intensity");
823+
ASSERT_NE(intensity, nullptr);
824+
EXPECT_EQ(library.program.types[intensity->type].name, "stdtypes::speed");
825+
}
826+
827+
TEST(DslStdlibTest, TheEnvironmentActionsAreDeclaredOnTheEnvironmentActor) {
828+
Library library;
829+
check_full_library(library);
830+
const auto parent =
831+
library.program.types_by_name.find("std::environment.action_for_environment");
832+
const auto actor = library.program.types_by_name.find("std::environment");
833+
ASSERT_NE(parent, library.program.types_by_name.end());
834+
ASSERT_NE(actor, library.program.types_by_name.end());
835+
struct Case {
836+
const char* name;
837+
const char* field;
838+
};
839+
for (const Case& expected :
840+
{Case{"std::environment.air", "temperature"}, Case{"std::environment.rain", "intensity"},
841+
Case{"std::environment.snow", "intensity"}, Case{"std::environment.wind", "direction"},
842+
Case{"std::environment.fog", "visual_range"},
843+
Case{"std::environment.clouds", "cloudiness"},
844+
Case{"std::environment.assign_celestial_position", "light_source"}}) {
845+
const auto id = library.program.types_by_name.find(expected.name);
846+
ASSERT_NE(id, library.program.types_by_name.end()) << expected.name;
847+
const TypeInfo& action = library.program.types[id->second];
848+
EXPECT_EQ(action.kind, TypeKind::Action) << expected.name;
849+
EXPECT_EQ(action.actor_type, actor->second) << expected.name;
850+
EXPECT_TRUE(library.program.is_derived_from(id->second, parent->second)) << expected.name;
851+
EXPECT_NE(library.program.find_field(id->second, expected.field), nullptr)
852+
<< expected.name << "." << expected.field;
853+
}
854+
}
855+
856+
TEST(DslStdlibTest, AnActionAndAStructMayShareASimpleName) {
857+
// §8.10.4 declares a struct `air` and §8.11.2 an action `environment.air`.
858+
// The action's name is a qualified behavior name (§7.2.2.2.5), so the two
859+
// never collide — which is why the standard can reuse the word.
860+
Library library;
861+
check_full_library(library);
862+
const TypeInfo* structure = library.program.find("std::air");
863+
const TypeInfo* action = library.program.find("std::environment.air");
864+
ASSERT_NE(structure, nullptr);
865+
ASSERT_NE(action, nullptr);
866+
EXPECT_EQ(structure->kind, TypeKind::Struct);
867+
EXPECT_EQ(action->kind, TypeKind::Action);
868+
}
869+
759870
TEST(DslStdlibTest, CheckingTheLibraryIsDeterministic) {
760871
// Load time is inside the determinism contract: the same sources must give
761872
// the same program and the same diagnostics, in the same order.

0 commit comments

Comments
 (0)