Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove track links object #5203

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class RJSInfra {
.build()
.adapter(RJSInfra.class);

public static final transient String CURRENT_VERSION = "3.4.3";
public static final transient String CURRENT_VERSION = "3.4.4";

/** The version of the infra format used */
public String version;
Expand All @@ -31,9 +31,6 @@ public class RJSInfra {
@Json(name = "track_sections")
public Collection<RJSTrackSection> trackSections;

@Json(name = "track_section_links")
public Collection<RJSTrackSectionLink> trackSectionLinks;

/** Switches are at the ends of track sections, and link those together. */
public Collection<RJSSwitch> switches;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,6 @@ private TrackInfra parse(RJSInfra infra) {
switches.put(s.id, parseSwitch(s, switchTypeMap));
}

addRemainingLinks(infra);

var trackSectionsByID = new HashMap<String, TrackSectionImpl>();
for (var track : infra.trackSections) {
var newTrack = makeTrackSection(track);
Expand Down Expand Up @@ -227,37 +225,6 @@ private void loadNeutralRanges(boolean announcement, RJSNeutralSection neutralSe
}
}

/** Creates all the track section links that haven't already been created by switches */
private void addRemainingLinks(RJSInfra infra) {
int generatedID = 0;
for (var link : infra.trackSectionLinks) {
var srcID = link.src.track;
var dstID = link.dst.track;
var oldSrcNode = getNode(srcID, link.src.endpoint);
var oldDstNode = getNode(dstID, link.dst.endpoint);
if (oldSrcNode != null || oldDstNode != null) {
// At least one of the node already exists:
// either both are the same switch node, or there is an error in the infra
if (oldSrcNode instanceof SwitchPort srcSwitchPort
&& oldDstNode instanceof SwitchPort dstSwitchPort
&& srcSwitchPort.getSwitch().getID().equals(dstSwitchPort.getSwitch().getID()))
continue;
throw newEndpointAlreadyLinkedError(
link.id,
oldSrcNode,
oldDstNode
);
}
if (link.id == null || link.id.equals("")) {
// Forcing a unique ID avoids node equality troubles, and makes debugging easier
link.id = String.format("generated_%d", generatedID++);
}
var newNode = new TrackNodeImpl.Joint(link.id);
addNode(srcID, link.src.endpoint, newNode);
addNode(dstID, link.dst.endpoint, newNode);
}
}

/** Adds all the speed sections to track attributes */
private void addSpeedSections(
List<RJSSpeedSection> speedSections,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import fr.sncf.osrd.railjson.schema.common.graph.ApplicableDirection;
import fr.sncf.osrd.railjson.schema.common.graph.EdgeEndpoint;
import fr.sncf.osrd.railjson.schema.infra.RJSTrackEndpoint;
import fr.sncf.osrd.railjson.schema.infra.RJSTrackSectionLink;
import fr.sncf.osrd.railjson.schema.infra.trackranges.RJSApplicableDirectionsTrackRange;
import fr.sncf.osrd.railjson.schema.infra.trackranges.RJSSpeedSection;
import fr.sncf.osrd.reporting.exceptions.ErrorType;
Expand Down Expand Up @@ -55,23 +54,6 @@ public void testDuplicatedSwitch() throws Exception {
assertEquals(thrown.osrdErrorType, ErrorType.StrictWarningError);
}

@Test
public void testLinkOnSwitch() throws Exception {
var rjsInfra = Helpers.getExampleInfra("tiny_infra/infra.json");
var s = rjsInfra.switches.iterator().next();
var ports = new ArrayList<>(s.ports.values());
rjsInfra.trackSectionLinks.add(new RJSTrackSectionLink(
"broken",
ports.get(0),
new RJSTrackEndpoint("ne.micro.bar_a", EdgeEndpoint.END)
));
var thrown = assertThrows(
OSRDError.class,
() -> UndirectedInfraBuilder.parseInfra(rjsInfra, new DiagnosticRecorderImpl(true))
);
assertEquals(thrown.osrdErrorType, ErrorType.InvalidInfraEndpointAlreadyLinked);
}

@Test
public void testDuplicateDetector() throws Exception {
var rjsInfra = Helpers.getExampleInfra("tiny_infra/infra.json");
Expand All @@ -83,15 +65,6 @@ public void testDuplicateDetector() throws Exception {
assertEquals(thrown.osrdErrorType, ErrorType.StrictWarningError);
}

@Test
public void testUnlabeledLinks() throws Exception {
var rjsInfra = Helpers.getExampleInfra("one_line/infra.json");
for (var link : rjsInfra.trackSectionLinks)
link.id = null;
// We check that no warning or assertion is raised when importing the infra
UndirectedInfraBuilder.parseInfra(rjsInfra, new DiagnosticRecorderImpl(true));
}

@Test
public void testOverlappingSpeedSections() throws Exception {
var rjsInfra = Helpers.getExampleInfra("one_line/infra.json");
Expand Down
17 changes: 0 additions & 17 deletions editoast/map_layers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,6 @@ layers:
where:
- speed_section.data @? '$.extensions.psl_sncf.z'

track_section_links:
table_name: infra_layer_track_section_link
id_field: id
views:
geo:
on_field: geographic
cache_duration: 3600
data_expr: track_section_link.data
joins:
- inner join infra_object_track_section_link track_section_link on track_section_link.obj_id = layer.obj_id and track_section_link.infra_id = layer.infra_id
sch:
on_field: schematic
cache_duration: 3600
data_expr: track_section_link.data
joins:
- inner join infra_object_track_section_link track_section_link on track_section_link.obj_id = layer.obj_id and track_section_link.infra_id = layer.infra_id

switches:
table_name: infra_layer_switch
id_field: id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- This file should undo anything in `up.sql`

CREATE TABLE infra_layer_track_section_link (
id int8 PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
obj_id varchar(255) NOT NULL,
geographic geometry(point, 3857) NOT NULL,
schematic geometry(point, 3857) NOT NULL,
infra_id int8 NOT NULL REFERENCES infra(id) ON DELETE CASCADE,
UNIQUE (infra_id, obj_id)
);
CREATE INDEX infra_layer_track_section_link_geographic ON infra_layer_track_section_link USING gist (geographic);
CREATE INDEX infra_layer_track_section_link_schematic ON infra_layer_track_section_link USING gist (schematic);

CREATE TABLE infra_object_track_section_link (
id int8 PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
obj_id varchar(255) NOT NULL,
data jsonb NOT NULL,
infra_id int8 NOT NULL REFERENCES infra(id) ON DELETE CASCADE,
UNIQUE (infra_id, obj_id)
);
10 changes: 10 additions & 0 deletions editoast/migrations/2023-10-11-222214_delete_track_links/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- Your SQL goes here
DROP TABLE infra_layer_track_section_link;
DROP TABLE infra_object_track_section_link;
younesschrifi marked this conversation as resolved.
Show resolved Hide resolved
DELETE FROM infra_layer_error
WHERE information->>'obj_type' = 'TrackSectionLink';
ALTER TABLE infra
ALTER column railjson_version
SET DEFAULT '3.4.4';
UPDATE infra
SET railjson_version = '3.4.4';
4 changes: 0 additions & 4 deletions editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,6 @@ components:
- out_of_range
- overlapping_speed_sections
- overlapping_switches
- overlapping_track_links
- overlapping_catenaries
- unknown_port_name
- unused_port
Expand Down Expand Up @@ -659,7 +658,6 @@ components:
- Signal
- SpeedSection
- Detector
- TrackSectionLink
- Switch
- SwitchType
- BufferStop
Expand Down Expand Up @@ -1132,8 +1130,6 @@ components:
type: array
switches:
type: array
track_section_links:
type: array
track_sections:
type: array
version:
Expand Down
4 changes: 0 additions & 4 deletions editoast/openapi_legacy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2484,7 +2484,6 @@ components:
- Signal
- SpeedSection
- Detector
- TrackSectionLink
- Switch
- SwitchType
- BufferStop
Expand All @@ -2504,8 +2503,6 @@ components:
type: array
switches:
type: array
track_section_links:
type: array
track_sections:
type: array
signals:
Expand Down Expand Up @@ -2708,7 +2705,6 @@ components:
- out_of_range
- overlapping_speed_sections
- overlapping_switches
- overlapping_track_links
- overlapping_catenaries
- unknown_port_name
- unused_port
Expand Down
14 changes: 2 additions & 12 deletions editoast/src/converters/generate_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ impl Node {
}

/// An edge connects two nodes
/// This connection can be between two tracks (switch and section link)
/// This connection can be between two tracks (switch)
/// Or traversing a whole track
/// Or along a track (detector and buffer stops)
#[derive(Clone, Debug)]
enum EdgeType {
Switch { id: Identifier, port: Identifier },
SectionLink,
Track,
Buffer(Direction),
ToDetector,
Expand All @@ -49,10 +48,9 @@ struct Graph {
}

impl Graph {
/* Part 2: build the graph from track sections, track sections links, switches, buffers and detectors */
/* Part 2: build the graph from track sections, switches, buffers and detectors */
fn load(&mut self, railjson: &RailJson) {
self.edges_from_track_sections(railjson);
self.edges_from_track_sections_links(railjson);
self.edges_from_switches(railjson);
}

Expand Down Expand Up @@ -139,14 +137,6 @@ impl Graph {
}
}

fn edges_from_track_sections_links(&mut self, railjson: &RailJson) {
for link in &railjson.track_section_links {
let u = Node::TrackEndpoint(link.src.clone());
let v = Node::TrackEndpoint(link.dst.clone());
self.add_symmetrical_edge(u, v, EdgeType::SectionLink);
}
}

fn edges_from_switches(&mut self, railjson: &RailJson) {
for switch in &railjson.switches {
let switch_type = railjson
Expand Down
14 changes: 7 additions & 7 deletions editoast/src/converters/osm_to_railjson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ pub fn parse_osm(osm_pbf_in: PathBuf) -> Result<RailJson, Box<dyn Error + Send +
.buffer_stops
.push(edge_to_buffer(&node, adj.edges[1], 1));
}
(2, 1) => railjson.track_section_links.push(TrackSectionLink {
id: node.0.to_string().into(),
src: adj.branches[0].0.clone(),
dst: adj.branches[0].1.clone(),
}),
(2, 1) => railjson.switches.push(link_switch(node, &adj.branches)),
(3, 2) => railjson.switches.push(point_switch(node, &adj.branches)),
(4, 2) => railjson.switches.push(cross_switch(node, &adj.branches)),
(4, 4) => railjson
Expand Down Expand Up @@ -152,8 +148,8 @@ mod tests {
ports.get(&name.into()).unwrap().track.0 == expected
}
let mut railjson = parse_osm("src/tests/switches.osm.pbf".into()).unwrap();
assert_eq!(3, railjson.switch_types.len());
assert_eq!(3, railjson.switches.len());
assert_eq!(4, railjson.switch_types.len());
assert_eq!(4, railjson.switches.len());
assert_eq!(18, railjson.buffer_stops.len());

// Switches can be in a random order, we sort them to be sure to extract the expected ones
Expand All @@ -162,6 +158,10 @@ mod tests {
.sort_by(|a, b| a.switch_type.as_str().cmp(b.switch_type.as_str()));

let switch = &railjson.switches[2];
assert_eq!("link", switch.switch_type.as_str());
assert_eq!(2, switch.ports.len());

let switch = &railjson.switches[3];
assert_eq!("point", switch.switch_type.as_str());
assert_eq!(3, switch.ports.len());
assert!(port_eq(&switch.ports, "BASE", "-103478-0"));
Expand Down
29 changes: 28 additions & 1 deletion editoast/src/converters/utils.rs
Tguisnet marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ use osmpbfreader::Node;
use std::str::FromStr;

pub fn default_switch_types() -> Vec<SwitchType> {
let mut link_group = std::collections::HashMap::new();
link_group.insert(
"LINK".into(),
vec![SwitchPortConnection {
src: "SOURCE".into(),
dst: "DESTINATION".into(),
}],
);

let mut point_groups = std::collections::HashMap::new();
point_groups.insert(
"LEFT".into(),
Expand Down Expand Up @@ -70,6 +79,11 @@ pub fn default_switch_types() -> Vec<SwitchType> {
);

vec![
SwitchType {
id: "link".into(),
ports: vec!["SOURCE".into(), "DESTINATION".into()],
groups: link_group,
},
SwitchType {
id: "point".into(),
ports: vec!["BASE".into(), "LEFT".into(), "RIGHT".into()],
Expand Down Expand Up @@ -158,14 +172,27 @@ fn track_section(n: NodeId, edge: &Edge) -> TrackEndpoint {
}

// When building the network topology, most things happen around a Node (in the OpenStreetMap sense)
// That’s where buffer stops, section links and switches happen
// That’s where buffer stops, and switches happen
// To do that, we count how many edges are adjacent to that node and how many branches go through that node
#[derive(Default)]
pub struct NodeAdjacencies<'a> {
pub edges: Vec<&'a Edge>,
pub branches: Vec<Branch>,
}

pub fn link_switch(node: NodeId, branches: &[Branch]) -> Switch {
let mut ports = HashMap::new();
ports.insert("SOURCE".into(), branches[0].0.clone());
ports.insert("DESTINATION".into(), branches[0].1.clone());
Switch {
id: node.0.to_string().into(),
switch_type: "link".into(),
ports,
group_change_delay: 0.,
..Default::default()
}
}

pub fn point_switch(node: NodeId, branches: &[Branch]) -> Switch {
let mut endpoint_count = HashMap::<&TrackEndpoint, u64>::new();
for (src, dst) in branches {
Expand Down
Loading