Skip to content

Commit f1189b8

Browse files
feat(core): a route model and writer for OpenSCENARIO (p8-s3) (#247) (#541)
The first of four PRs for lane-anchored routes: the model and the writer. The reader, the resolver and the editor tool follow. <RoutingAction> gains its AssignRouteAction arm, carrying an inline <Route> of <Waypoint>s. Waypoints hold the same osc::Position variant a teleport does, so a route RoadMaker authors names lanes rather than points in space — which is what makes it follow the roads beneath it (GW-6 step 7) instead of staying behind where it was drawn. ★ The struct is RouteWaypoint, never Waypoint. roadmaker::Waypoint is the road-authoring node type, and an unqualified Waypoint declared inside roadmaker::osc would shadow it in exactly the translation unit that needs both — the resolver, which walks a route across a network whose roads are shaped by authoring waypoints. Third name this header's surrounding tree has already taken, after `signals` (a Qt macro) and RoadNetwork; the rule those established is that the element keeps its schema name and the struct gets an unambiguous one. The inline route is an OPTIONAL arm. An <AssignRouteAction> whose content is a <CatalogReference> keeps it on the preserved tier, and no empty <Route> is invented — that would replace a legal document with an invalid one. Refusals mirror the schema: fewer than two waypoints, a nameless route, a duplicate route name (uniqueness is document-wide, since routes are siblings once flattened and a simulator resolves one by name), a waypoint with no routeStrategy. Waypoint positions go through the SAME check_road_relative_position an init teleport does, so a route cannot smuggle past it a reference the placement layer would have refused. A <WorldPosition> waypoint warns, citing routing.ambiguous_route_waypoints, and does not block the write. It is legal; it merely fails to say which lane the route takes where several meet. routing.route_waypoints_locations is deliberately NOT declared yet. Whether a road belongs to a junction is a fact about the .xodr, and a Scenario holds only the roadId string — so it belongs with the network-aware checker in the next PR. Declaring it early would make it the decoration rules.hpp's own header rules out, which EveryRuleConstantIsCitedBySomeFinding enforces. Twelve new writer tests, all six sabotages caught. One of them was incoherent first: the routing arm's own-element test built three separate PrivateActions, which produce three elements no matter what the writer does with the arms. Rebuilt around ONE action carrying all three, it fails as it should. Refs #247.
1 parent cd1043a commit f1189b8

5 files changed

Lines changed: 544 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,27 @@ Current version on `main`: **0.0.1**.
239239
a scenario that gained a `<LongitudinalAction>` from merely being looked at
240240
would no longer re-save byte-identically. A speed defined relative to
241241
another entity is shown disabled rather than silently overwritten.
242+
- **Routes in the scenario model**
243+
([#247](https://github.com/Robomous/RoadMaker/issues/247), model + writer): the
244+
kernel can hold and serialize `<RoutingAction><AssignRouteAction><Route>` with
245+
its `<Waypoint>` list, so an actor can be given a path through the network
246+
rather than only a starting pose. RoadMaker authors `<LanePosition>`
247+
waypoints, which is what makes a route follow the roads beneath it through
248+
later edits instead of staying behind in world space.
249+
- **A `<CatalogReference>` route is kept, not replaced.** The inline `<Route>`
250+
is an optional arm; an `<AssignRouteAction>` whose content this version does
251+
not model rides the preserved tier whole, and no empty `<Route>` is invented
252+
for it.
253+
- **Refusals mirror the schema**: fewer than two waypoints ("at least two
254+
waypoints are needed to define a route"), a nameless route, a duplicate
255+
route name (citing `naming.unique_element_names_on_same_level`), a waypoint
256+
with no `routeStrategy`. A waypoint's position is held to the **same**
257+
road-relative checks an init teleport is, so a route cannot smuggle past
258+
them a reference the placement layer would have refused.
259+
- **A world-position waypoint warns rather than blocks**, citing
260+
`routing.ambiguous_route_waypoints`: it is legal, and it does not say which
261+
lane the route takes where several meet — so the same file routes
262+
differently in two simulators.
242263
- **The esmini CI pin is honoured by a bump**
243264
([#506](https://github.com/Robomous/RoadMaker/issues/506)): the cache key
244265
repeated the version as a literal instead of deriving it from

‎core/include/roadmaker/osc/rules.hpp‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,27 @@ inline constexpr std::string_view kRoadLaneExists =
203203
inline constexpr std::string_view kRoadLaneOffsetInBounds =
204204
"asam.net:xosc:1.0.0:positioning.road_lane_offset_in_bounds";
205205

206+
/// "When a `Waypoint` of a `Route` is ambiguous then the `Waypoint` can be
207+
/// defined in `Road` or `Lane` position types to be unambiguous."
208+
///
209+
/// Cited as a WARNING on a `<WorldPosition>` route waypoint (p8-s3, issue
210+
/// #247). Legal, and reported anyway: a world point where several lanes meet
211+
/// does not say which lane the route takes, so the same file routes differently
212+
/// in two simulators. RoadMaker itself only ever authors `<LanePosition>`
213+
/// waypoints, which is what makes a route survive an edit to the road beneath
214+
/// it — so this fires on foreign input, never on our own output.
215+
inline constexpr std::string_view kAmbiguousRouteWaypoints =
216+
"asam.net:xosc:1.0.0:routing.ambiguous_route_waypoints";
217+
218+
// `asam.net:xosc:1.0.0:routing.route_waypoints_locations` ("route waypoints
219+
// should not be located in junctions to avoid ambiguity") is deliberately NOT
220+
// declared here yet. It is not reachable from `validate_scenario` at all —
221+
// whether a road belongs to a junction is a fact about the `.xodr`, and a
222+
// `Scenario` holds only the roadId STRING (ADR-0014 §5) — so it belongs with
223+
// the network-aware checker in `osc/route.hpp`. Declaring it before something
224+
// cites it is exactly the decoration this file's header rules out, and
225+
// `XoscRules.EveryRuleConstantIsCitedBySomeFinding` enforces that.
226+
206227
/// "Based on the determined version of the checked file (Element `FileHeader`,
207228
/// attributes `revMajor` and `revMinor`), it shall comply with the schema of
208229
/// the detected version."

‎core/include/roadmaker/osc/scenario.hpp‎

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -543,9 +543,94 @@ struct LongitudinalAction {
543543
RawXml preserved;
544544
};
545545

546-
/// `<PrivateAction>` — §7.5, a ten-way choice of which this version models two
547-
/// arms: the teleport that places an entity and the longitudinal action that
548-
/// gives it an initial speed.
546+
/// `<Waypoint>` — §7.7, "reference position used to form a route" (p8-s3,
547+
/// issue #247).
548+
///
549+
/// ★ THE STRUCT IS `RouteWaypoint`, NEVER `Waypoint`, and that is forced rather
550+
/// than stylistic. `roadmaker::Waypoint` is the road-authoring node type
551+
/// (`road/authoring.hpp`), and an unqualified `Waypoint` declared inside
552+
/// `roadmaker::osc` would shadow it in exactly the translation unit that needs
553+
/// both — `osc/route.cpp`, which walks a route's waypoints across a network
554+
/// whose roads are shaped by authoring waypoints. This is the third name in
555+
/// this header the surrounding tree has already taken, after `signals` (a Qt
556+
/// macro) and `RoadNetwork` (the kernel's most central type); the rule those
557+
/// two established is that the ELEMENT keeps its schema name and the STRUCT
558+
/// gets an unambiguous one.
559+
struct RouteWaypoint {
560+
/// `@routeStrategy` — REQUIRED (`use="required"`, no default), so this is a
561+
/// plain value and not an optional. A free string for the reason
562+
/// `Vehicle::category` and `TrafficSignalState::state` are: the enumeration is
563+
/// {fastest, shortest, random, leastIntersections} today, and an unmodeled
564+
/// spelling must round-trip rather than be rewritten into the nearest one this
565+
/// build knows — the class of corruption #476 found in the OpenDRIVE writer.
566+
///
567+
/// `kDefaultRouteStrategy` is what RoadMaker authors; nothing here narrows
568+
/// what can be read.
569+
std::string route_strategy;
570+
571+
/// `<Position>` — 1..1. The waypoints RoadMaker authors are
572+
/// `<LanePosition>`s, which is what makes a route lane-anchored: the roads
573+
/// beneath it can move and the route follows, because it names the lane rather
574+
/// than a point in space (GW-6 step 7).
575+
Position position;
576+
577+
RawXml preserved;
578+
};
579+
580+
/// The `@routeStrategy` RoadMaker authors.
581+
///
582+
/// "shortest" rather than "fastest": a RoadMaker route names every lane it
583+
/// passes through, so there is nothing for a simulator to optimize between
584+
/// consecutive waypoints and the cheapest reading is the least surprising one.
585+
inline constexpr std::string_view kDefaultRouteStrategy = "shortest";
586+
587+
/// `<Route>` — §7.7, "a continuous path throughout the road network, defined by
588+
/// a series of waypoints".
589+
struct Route {
590+
/// `@name` — REQUIRED. "Required in catalogs", and required by the schema
591+
/// everywhere else too (`use="required"`).
592+
std::string name;
593+
594+
/// `@closed` — REQUIRED. "In a closed route, the last waypoint is followed by
595+
/// the first waypoint to create a closed route."
596+
bool closed = false;
597+
598+
std::vector<ParameterDeclaration> parameter_declarations;
599+
600+
/// `minOccurs="2"`: "at least two waypoints are needed to define a route."
601+
/// The writer refuses a shorter one rather than emitting a `<Route>` no
602+
/// schema-aware reader will accept.
603+
std::vector<RouteWaypoint> waypoints;
604+
605+
RawXml preserved;
606+
};
607+
608+
/// `<AssignRouteAction>` — §7.7, "controls an entity to follow a route using
609+
/// waypoints on the road network".
610+
///
611+
/// A choice of `<Route>` XOR `<CatalogReference>`. Only the inline route is
612+
/// modeled; a catalog reference rides `preserved.children` whole, exactly as an
613+
/// unmodeled entity object does. `route` is therefore an OPTIONAL rather than a
614+
/// value: unset means the action's content is entirely preserved, and the
615+
/// writer emits no `<Route>` for it — emitting an empty one would turn a legal
616+
/// catalog reference into an invalid document.
617+
struct AssignRouteAction {
618+
std::optional<Route> route;
619+
RawXml preserved;
620+
};
621+
622+
/// `<RoutingAction>` — §7.7, a five-way choice of which this version models one
623+
/// arm: `<AssignRouteAction>`. `<FollowTrajectoryAction>`,
624+
/// `<AcquirePositionAction>`, `<RandomRouteAction>` and
625+
/// `<PreferredLaneLayerAction>` ride `preserved.children`.
626+
struct RoutingAction {
627+
std::optional<AssignRouteAction> assign_route;
628+
RawXml preserved;
629+
};
630+
631+
/// `<PrivateAction>` — §7.5, a ten-way choice of which this version models three
632+
/// arms: the teleport that places an entity, the longitudinal action that gives
633+
/// it an initial speed, and the routing action that gives it a route.
549634
///
550635
/// ★ ONE ELEMENT PER SET ARM. The schema's choice is per-`<PrivateAction>`, so
551636
/// the writer emits one `<PrivateAction>` per arm that is set: teleport-only and
@@ -562,6 +647,11 @@ struct LongitudinalAction {
562647
struct PrivateAction {
563648
std::optional<TeleportAction> teleport;
564649
std::optional<LongitudinalAction> longitudinal;
650+
651+
/// The routing arm (p8-s3, issue #247). Emitted AFTER the other two, matching
652+
/// the order an actor is authored: placed, given a speed, then given a route.
653+
std::optional<RoutingAction> routing;
654+
565655
RawXml preserved;
566656
};
567657

‎core/src/osc/writer.cpp‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,58 @@ void write_longitudinal(pugi::xml_node action_node, const LongitudinalAction& lo
458458
write_preserved_children(node, longitudinal.preserved);
459459
}
460460

461+
/// `<RoutingAction>` — p8-s3, issue #247.
462+
///
463+
/// The `<Route>` child order is the XSD sequence exactly:
464+
/// `<ParameterDeclarations>` (0..1) then `<Waypoint>` (2..*). Unlike the
465+
/// scenario-level `<ParameterDeclarations>`, which is part of the always-present
466+
/// skeleton, a route's is OPTIONAL — so it is emitted only when it has content.
467+
/// An empty one would be a child a foreign input did not have, and this writer's
468+
/// round trip is a fixed point measured in bytes.
469+
void write_routing(pugi::xml_node action_node, const RoutingAction& routing) {
470+
pugi::xml_node node = action_node.append_child("RoutingAction");
471+
write_preserved_attributes(node, routing.preserved);
472+
473+
if (routing.assign_route.has_value()) {
474+
const AssignRouteAction& assign = *routing.assign_route;
475+
pugi::xml_node assign_node = node.append_child("AssignRouteAction");
476+
write_preserved_attributes(assign_node, assign.preserved);
477+
// Unset `route` means the action's content is a <CatalogReference> riding
478+
// the preserved tier. Emitting an empty <Route> here would replace a legal
479+
// catalog reference with an invalid element.
480+
if (assign.route.has_value()) {
481+
const Route& route = *assign.route;
482+
pugi::xml_node route_node = assign_node.append_child("Route");
483+
route_node.append_attribute("name").set_value(route.name.c_str());
484+
route_node.append_attribute("closed").set_value(route.closed);
485+
write_preserved_attributes(route_node, route.preserved);
486+
if (!route.parameter_declarations.empty()) {
487+
pugi::xml_node parameters = route_node.append_child("ParameterDeclarations");
488+
for (const ParameterDeclaration& parameter : route.parameter_declarations) {
489+
pugi::xml_node parameter_node = parameters.append_child("ParameterDeclaration");
490+
parameter_node.append_attribute("name").set_value(parameter.name.c_str());
491+
parameter_node.append_attribute("parameterType")
492+
.set_value(parameter.parameter_type.c_str());
493+
parameter_node.append_attribute("value").set_value(parameter.value.c_str());
494+
write_preserved_attributes(parameter_node, parameter.preserved);
495+
write_preserved_children(parameter_node, parameter.preserved);
496+
}
497+
}
498+
for (const RouteWaypoint& waypoint : route.waypoints) {
499+
pugi::xml_node waypoint_node = route_node.append_child("Waypoint");
500+
waypoint_node.append_attribute("routeStrategy").set_value(waypoint.route_strategy.c_str());
501+
write_preserved_attributes(waypoint_node, waypoint.preserved);
502+
write_position(waypoint_node, waypoint.position);
503+
write_preserved_children(waypoint_node, waypoint.preserved);
504+
}
505+
write_preserved_children(route_node, route.preserved);
506+
}
507+
write_preserved_children(assign_node, assign.preserved);
508+
}
509+
510+
write_preserved_children(node, routing.preserved);
511+
}
512+
461513
/// One `PrivateAction` — ONE `<PrivateAction>` ELEMENT PER SET ARM.
462514
///
463515
/// The schema's choice is per-element, so an action carrying both a teleport
@@ -492,6 +544,12 @@ void write_private_action(pugi::xml_node private_node, const PrivateAction& acti
492544
write_longitudinal(node, *action.longitudinal);
493545
close(node);
494546
}
547+
if (action.routing.has_value()) {
548+
pugi::xml_node node;
549+
open(node);
550+
write_routing(node, *action.routing);
551+
close(node);
552+
}
495553
if (!preserved_written) {
496554
pugi::xml_node node;
497555
open(node);
@@ -756,13 +814,71 @@ void check_road_relative_position(std::vector<Diagnostic>& findings,
756814
}
757815
}
758816

817+
/// One `<Route>` (p8-s3, issue #247), and the route names already seen in this
818+
/// document — `@name` must be unique among siblings, and every route this
819+
/// version writes is a sibling of every other because they all hang off
820+
/// `<Init>`.
821+
void check_route(std::vector<Diagnostic>& findings,
822+
std::set<std::string>& route_names,
823+
const Route& route,
824+
bool has_logic_file,
825+
const std::string& location) {
826+
// No rule id for the missing name: `use="required"` is a schema constraint,
827+
// not one of Annex C's checker rules, and citing a UID that does not cover it
828+
// would be worse than citing none.
829+
if (route.name.empty()) {
830+
findings.push_back(error(location, "<Route> has no name, which the schema requires"));
831+
} else if (!route_names.insert(route.name).second) {
832+
findings.push_back(error(location,
833+
fmt::format("a route named '{}' is already declared", route.name),
834+
rules::kUniqueElementNames));
835+
}
836+
if (route.waypoints.size() < 2) {
837+
// minOccurs="2": "at least two waypoints are needed to define a route".
838+
// Refused rather than padded — a one-waypoint route names a destination and
839+
// no origin, and inventing the missing end is not this writer's call.
840+
findings.push_back(
841+
error(location,
842+
fmt::format("<Route> has {} waypoint(s); at least two are needed to define one",
843+
route.waypoints.size())));
844+
}
845+
for (std::size_t index = 0; index < route.waypoints.size(); ++index) {
846+
const RouteWaypoint& waypoint = route.waypoints[index];
847+
const std::string waypoint_location = fmt::format("{}/Waypoint[{}]", location, index);
848+
if (waypoint.route_strategy.empty()) {
849+
findings.push_back(
850+
error(waypoint_location, "<Waypoint> has no routeStrategy, which the schema requires"));
851+
}
852+
// "When a Waypoint of a Route is ambiguous then the Waypoint can be defined
853+
// in Road or Lane position types to be unambiguous." A <WorldPosition>
854+
// waypoint is the ambiguous case the rule names — a WARNING, not an error:
855+
// it is legal, it is merely how a route stops being reproducible.
856+
if (std::holds_alternative<WorldPosition>(waypoint.position)) {
857+
findings.push_back(
858+
Diagnostic{.severity = Severity::Warning,
859+
.location = waypoint_location + "/Position/WorldPosition",
860+
.message = "a route waypoint given as a world position is ambiguous where "
861+
"several lanes meet; a <RoadPosition> or <LanePosition> names "
862+
"which one",
863+
.rule_id = std::string(rules::kAmbiguousRouteWaypoints),
864+
.road = {},
865+
.lane = {}});
866+
}
867+
check_road_relative_position(
868+
findings, waypoint.position, has_logic_file, waypoint_location + "/Position");
869+
}
870+
}
871+
759872
void check_storyboard(std::vector<Diagnostic>& findings, const Scenario& scenario) {
760873
std::set<std::string> entity_names;
761874
for (const ScenarioObject& object : scenario.entities.scenario_objects) {
762875
entity_names.insert(object.name);
763876
}
764877

765878
const bool has_logic_file = scenario.road_network.logic_file.has_value();
879+
// Route names are unique across the whole init block, not per entity: they
880+
// are siblings once flattened, and a simulator resolves a route by name.
881+
std::set<std::string> route_names;
766882
const auto& privates = scenario.storyboard.init.actions.privates;
767883
for (std::size_t index = 0; index < privates.size(); ++index) {
768884
const std::string location = fmt::format("Storyboard/Init/Actions/Private[{}]", index);
@@ -792,6 +908,14 @@ void check_storyboard(std::vector<Diagnostic>& findings, const Scenario& scenari
792908
fmt::format("target speed {} is negative",
793909
num(action.longitudinal->speed->absolute_target->value))));
794910
}
911+
if (action.routing.has_value() && action.routing->assign_route.has_value() &&
912+
action.routing->assign_route->route.has_value()) {
913+
check_route(findings,
914+
route_names,
915+
*action.routing->assign_route->route,
916+
has_logic_file,
917+
action_location + "/RoutingAction/AssignRouteAction/Route");
918+
}
795919
}
796920

797921
if (entity_ref.empty()) {

0 commit comments

Comments
 (0)