Skip to content

Commit

Permalink
core - python : delete track_links from core parser and railjson_gene…
Browse files Browse the repository at this point in the history
…rator
  • Loading branch information
younesschrifi committed Oct 6, 2023
1 parent 1bd551d commit 93a660a
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 Expand Up @@ -75,7 +72,6 @@ public RJSInfra(
List<RJSTrainDetector> detectors
) {
this.trackSections = trackSections;
this.trackSectionLinks = new ArrayList<>();
this.switches = new ArrayList<>();
this.operationalPoints = new ArrayList<>();
this.routes = routes;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +0,0 @@
package fr.sncf.osrd.railjson.schema.infra;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import fr.sncf.osrd.railjson.schema.common.Identified;

/** This class represents a link between two track sections */
@SuppressFBWarnings({"URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD"})
public class RJSTrackSectionLink implements Identified {
public String id;
public RJSTrackEndpoint src;
public RJSTrackEndpoint dst;

/**
* Create a serialized track section link
* @param src the beginning of the link
* @param dst end end of the link
*/
public RJSTrackSectionLink(
String id,
RJSTrackEndpoint src,
RJSTrackEndpoint dst
) {
this.id = id;
this.src = src;
this.dst = dst;
}

@Override
public String getID() {
return id;
}
}
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
1 change: 0 additions & 1 deletion editoast/src/views/rolling_stocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,6 @@ pub mod tests {
.to_request(),
)
.await;

let power_restrictions = rolling_stock
.model
.power_restrictions
Expand Down
11 changes: 6 additions & 5 deletions python/railjson_generator/railjson_generator/infra_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
DoubleCrossSwitch,
PointSwitch,
SwitchGroup,
TrackSectionLink
)
from .schema.infra.track_section import TrackSection
from .utils import generate_routes
Expand Down Expand Up @@ -99,11 +100,11 @@ def add_double_cross_switch(
self.infra.switches.append(switch)
return switch

def add_link(self, *args, **kwargs):
link = Link(*args, **kwargs)
self.infra.links.append(link)
_register_connection(link.begin, link.end, None)
return link
def add_link(self, start: TrackEndpoint, end: TrackEndpoint, **kwargs):
switch = TrackSectionLink(start=start, end=end, **kwargs)
self.infra.switches.append(switch)
_register_connection(start, end, switch.group("SRC_DST"))
return switch

def add_operational_point(self, *args, **kwargs):
self.infra.operational_points.append(OperationalPoint(*args, **kwargs))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,20 @@
}
]
}
},
{
"id": "track_section_link",
"ports": [
"start",
"end"
],
"groups": {
"SRC_DST": [
{
"src": "start",
"dst": "end"
}
]
}
}
]
]
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ def to_rjs(self):
extensions={"sncf": {"label": self.label}},
)

@dataclass
class TrackSectionLink(Switch):
start: TrackEndpoint = None
end: TrackEndpoint = None

PORT_NAMES = ["start", "end"]
SWITCH_TYPE = "link"


@dataclass
class PointSwitch(Switch):
Expand Down

0 comments on commit 93a660a

Please sign in to comment.