Skip to content

Commit

Permalink
feat: prepare sp with mock signals, track equipment, station speeds a…
Browse files Browse the repository at this point in the history
…nd curves (#353)
  • Loading branch information
mghilardelli authored Nov 5, 2024
1 parent 3e434c0 commit d53ca8c
Show file tree
Hide file tree
Showing 17 changed files with 490 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ sfera-mock/src/main/java/generated/**
sfera-mock/src/main/java/META-INF/JAXB/**
sfera-mock/src/main/java/org/w3/_2001/xmlschema/**
*.p12
*.env

### STS ###
.apt_generated
Expand Down
8 changes: 7 additions & 1 deletion sfera-mock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@

| Train number | Result |
|--------------|--------------------------|
| 4816 | Zürich HB - Aarau <br/> |
| 4816 | Zürich HB - Aarau |
| 7839 | Solothurn - Oberdorf SO |
| 9999 | Fictional Bahnhof A - D1 |

TODO: 29137 Lenzburg - Luzern https://miro.com/app/board/uXjVKK4zJFk=/?moveToWidget=3458764596975113381&cot=14

## Add new Scenario
To create a new scenario some resources need to be added
1. add journey profile named `SFERA_JP_<train number>` to `static_sfera_resources/jp`
2. add corresponding segment profiles named `SFERA_SP_<train number>_<sp id>` to `static_sfera_resources/sp`

Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package ch.sbb.sferamock.messages.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.transform.stream.StreamResult;
Expand All @@ -12,7 +8,6 @@
import org.slf4j.LoggerFactory;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

@Component
public class XmlHelper {
Expand All @@ -35,10 +30,7 @@ public String toString(Object object) {
}
}

public Object xmlToObject(String filePath) throws IOException {
File file = ResourceUtils.getFile(filePath);
InputStream in = new FileInputStream(file);
String xmlPayload = new String(in.readAllBytes());
return jaxb2Marshaller.unmarshal(new StreamSource(new StringReader(xmlPayload)));
public Object xmlToObject(String xml) {
return jaxb2Marshaller.unmarshal(new StreamSource(new StringReader(xml)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@
import ch.sbb.sferamock.adapters.sfera.model.v0201.JourneyProfile;
import ch.sbb.sferamock.messages.common.XmlHelper;
import ch.sbb.sferamock.messages.model.TrainIdentification;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Service;

@Service
public class JourneyProfileRepository implements ApplicationRunner {

private static final String XML_RESOURCES_CLASSPATH = "classpath:sfera_example_messages/";
private static final String XML_RESOURCES_CLASSPATH = "classpath:static_sfera_resources/jp/*.xml";
private static final String XML_REGEX = "SFERA_JP_(\\d+)\\.xml";
private final XmlHelper xmlHelper;

Map<String, JourneyProfile> journeyProfiles = new HashMap<>();
Expand All @@ -25,16 +32,33 @@ public JourneyProfileRepository(XmlHelper xmlHelper) {

@Override
public void run(ApplicationArguments args) throws Exception {
importJp("4816", "SFERA_JP_4816.xml");
importJp("7839", "SFERA_JP_7839.xml");
importJps();
}

public Optional<JourneyProfile> getJourneyProfile(TrainIdentification trainIdentification) {
return Optional.ofNullable(journeyProfiles.get(trainIdentification.operationalNumber()));
}

private void importJp(String operationalNumber, String path) throws IOException {
var journeyProfile = xmlHelper.xmlToObject(XML_RESOURCES_CLASSPATH + path);
journeyProfiles.put(operationalNumber, (JourneyProfile) journeyProfile);
private void importJps() throws IOException {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
var resources = resolver.getResources(XML_RESOURCES_CLASSPATH);
for (var resource : resources) {
File file = resource.getFile();
var operationalNumber = extractOperationalNumber(file.getName());
try (InputStream in = new FileInputStream(file)) {
String xmlPayload = new String(in.readAllBytes());
var journeyProfile = xmlHelper.xmlToObject(xmlPayload);
journeyProfiles.put(operationalNumber, (JourneyProfile) journeyProfile);
}
}
}

public static String extractOperationalNumber(String filename) {
Pattern pattern = Pattern.compile(XML_REGEX);
Matcher matcher = pattern.matcher(filename);
if (matcher.find()) {
return matcher.group(1);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@
import ch.sbb.sferamock.adapters.sfera.model.v0201.SegmentProfile;
import ch.sbb.sferamock.messages.common.XmlHelper;
import ch.sbb.sferamock.messages.model.SegmentIdentification;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Service;

@Service
public class SegmentProfileRepository implements ApplicationRunner {

private static final String XML_RESOURCES_CLASSPATH = "classpath:sfera_example_messages/";
private static final String XML_RESOURCES_CLASSPATH = "classpath:static_sfera_resources/sp/*.xml";
private static final String XML_REGEX = "SFERA_SP_(\\d+_\\d+)\\.xml";

private final XmlHelper xmlHelper;

Map<String, SegmentProfile> segmentProfiles = new HashMap<>();
Expand All @@ -25,17 +33,33 @@ public SegmentProfileRepository(XmlHelper xmlHelper) {

@Override
public void run(ApplicationArguments args) throws Exception {
importSp("1", "SFERA_SP_1.xml");
importSp("2", "SFERA_SP_2.xml");
importSp("3", "SFERA_SP_3.xml");
importSps();
}

public Optional<SegmentProfile> getSegmentProfile(SegmentIdentification spId) {
return Optional.ofNullable(segmentProfiles.get(spId.id()));
}

private void importSp(String operationalNumber, String path) throws IOException {
var segmentProfile = xmlHelper.xmlToObject(XML_RESOURCES_CLASSPATH + path);
segmentProfiles.put(operationalNumber, (SegmentProfile) segmentProfile);
private void importSps() throws IOException {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
var resources = resolver.getResources(XML_RESOURCES_CLASSPATH);
for (var resource : resources) {
File file = resource.getFile();
var segmentId = extractSpId(file.getName());
try (InputStream in = new FileInputStream(file)) {
String xmlPayload = new String(in.readAllBytes());
var segmentProfile = xmlHelper.xmlToObject(xmlPayload);
segmentProfiles.put(segmentId, (SegmentProfile) segmentProfile);
}
}
}

public static String extractSpId(String filename) {
Pattern pattern = Pattern.compile(XML_REGEX);
Matcher matcher = pattern.matcher(filename);
if (matcher.find()) {
return matcher.group(1);
}
return null;
}
}
8 changes: 8 additions & 0 deletions sfera-mock/src/main/resources/SFERA_3.0_custom.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ SFERA v3 custom
- TAF_TAP_location_type
- TimingPoint
- TAF_TAP_LocationReference
- SignalPhysicalCharacteristics
- visualIdentifier
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" xmlns:altova="http://www.altova.com/xml-schema-extensions" elementFormDefault="qualified" attributeFormDefault="unqualified" version="2.01" vc:minVersion="1.1">
<xs:element name="SFERA_B2G_EventMessage">
Expand Down Expand Up @@ -2083,6 +2085,7 @@ SF12 SFERA_G2B_ReplyMessage</xs:documentation>
<xs:complexContent>
<xs:extension base="PhysicalCharacteristics_ComplexType">
<xs:attribute ref="signalType"/>
<xs:attribute ref="visualIdentifier"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
Expand Down Expand Up @@ -6023,6 +6026,11 @@ Resolution: 15 min
<xs:documentation>Vertical offset (in m) from the top of the track to the middle of the object.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="visualIdentifier" type="xs:string">
<xs:annotation>
<xs:documentation>Visual identifier of the signal. Typically a sign placed over or under the signal itself facilitates the visual identification by the driver.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="voltageValue" type="voltageType">
<xs:annotation>
<xs:documentation>Voltage value (V)</xs:documentation>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?xml version="1.0"?>
<JourneyProfile JP_Status="Valid" JP_Version="1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../SFERA_3.0_custom.xsd">
xsi:noNamespaceSchemaLocation="../../SFERA_3.0_custom.xsd">
<TrainIdentification>
<OTN_ID>
<Company>1085</Company>
<OperationalTrainNumber>4816</OperationalTrainNumber>
<StartDate>2022-01-04</StartDate>
</OTN_ID>
</TrainIdentification>
<SegmentProfileList SP_ID="1" SP_VersionMajor="1" SP_VersionMinor="1" SP_Direction="Nominal">
<SegmentProfileList SP_ID="4816_1" SP_VersionMajor="1" SP_VersionMinor="1" SP_Direction="Nominal">
<SP_Zone>
<IM_ID>0085</IM_ID>
</SP_Zone>
Expand All @@ -24,7 +24,7 @@
</TimingPointReference>
</TimingPointConstraints>
</SegmentProfileList>
<SegmentProfileList SP_ID="2" SP_VersionMajor="1" SP_VersionMinor="1" SP_Direction="Nominal">
<SegmentProfileList SP_ID="4816_2" SP_VersionMajor="1" SP_VersionMinor="1" SP_Direction="Nominal">
<SP_Zone>
<IM_ID>0085</IM_ID>
</SP_Zone>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?xml version="1.0"?>
<JourneyProfile JP_Status="Valid" JP_Version="1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../SFERA_3.0_custom.xsd">
xsi:noNamespaceSchemaLocation="../../SFERA_3.0_custom.xsd">
<TrainIdentification>
<OTN_ID>
<Company>1085</Company>
<OperationalTrainNumber>7839</OperationalTrainNumber>
<StartDate>2022-01-04</StartDate>
</OTN_ID>
</TrainIdentification>
<SegmentProfileList SP_ID="3" SP_VersionMajor="1" SP_VersionMinor="1" SP_Direction="Nominal">
<SegmentProfileList SP_ID="7839_1" SP_VersionMajor="1" SP_VersionMinor="1" SP_Direction="Nominal">
<SP_Zone>
<IM_ID>0085</IM_ID>
</SP_Zone>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0"?>
<JourneyProfile JP_Status="Valid" JP_Version="1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../SFERA_3.0_custom.xsd">
<TrainIdentification>
<OTN_ID>
<Company>1085</Company>
<OperationalTrainNumber>9999</OperationalTrainNumber>
<StartDate>2022-01-04</StartDate>
</OTN_ID>
</TrainIdentification>
<SegmentProfileList SP_ID="9999_1" SP_VersionMajor="1" SP_VersionMinor="0" SP_Direction="Nominal">
<SP_Zone>
<IM_ID>0085</IM_ID>
</SP_Zone>
<TimingPointConstraints>
<TimingPointReference>
<TP_ID_Reference TP_ID="Bahnhof_A"/>
</TimingPointReference>
</TimingPointConstraints>
</SegmentProfileList>
<SegmentProfileList SP_ID="9999_2" SP_VersionMajor="1" SP_VersionMinor="0" SP_Direction="Nominal">
<SP_Zone>
<IM_ID>0085</IM_ID>
</SP_Zone>
<TimingPointConstraints>
<TimingPointReference>
<TP_ID_Reference TP_ID="Haltestelle_B"/>
</TimingPointReference>
</TimingPointConstraints>
</SegmentProfileList>
<SegmentProfileList SP_ID="9999_3" SP_VersionMajor="1" SP_VersionMinor="0" SP_Direction="Nominal">
<SP_Zone>
<IM_ID>0085</IM_ID>
</SP_Zone>
<TimingPointConstraints>
<TimingPointReference>
<TP_ID_Reference TP_ID="Halt_auf_Verlangen_C"/>
</TimingPointReference>
<StoppingPointInformation departureTime="2022-05-17T07:52:00Z">
<StopType stopTypePurpose="Commercial" mandatoryStop="false"/>
</StoppingPointInformation>
</TimingPointConstraints>
</SegmentProfileList>
<SegmentProfileList SP_ID="9999_4" SP_VersionMajor="1" SP_VersionMinor="0" SP_Direction="Nominal">
<SP_Zone>
<IM_ID>0085</IM_ID>
</SP_Zone>
<TimingPointConstraints>
<TimingPointReference>
<TP_ID_Reference TP_ID="Klammerbahnhof_D"/>
</TimingPointReference>
</TimingPointConstraints>
</SegmentProfileList>
<SegmentProfileList SP_ID="9999_5" SP_VersionMajor="1" SP_VersionMinor="0" SP_Direction="Nominal">
<SP_Zone>
<IM_ID>0085</IM_ID>
</SP_Zone>
<TimingPointConstraints>
<TimingPointReference>
<TP_ID_Reference TP_ID="Klammerbahnhof_D1"/>
</TimingPointReference>
</TimingPointConstraints>
</SegmentProfileList>
</JourneyProfile>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<SegmentProfile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../SFERA_3.0_custom.xsd"
SP_ID="1" SP_VersionMajor="1" SP_VersionMinor="1" SP_Length="41500" SP_Status="Valid">
xsi:noNamespaceSchemaLocation="../../SFERA_3.0_custom.xsd"
SP_ID="4816_1" SP_VersionMajor="1" SP_VersionMinor="0" SP_Length="41500" SP_Status="Valid">
<SP_Zone>
<IM_ID>0085</IM_ID>
</SP_Zone>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<SegmentProfile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../SFERA_3.0_custom.xsd"
SP_ID="2" SP_VersionMajor="1" SP_VersionMinor="1" SP_Length="41500" SP_Status="Valid">
xsi:noNamespaceSchemaLocation="../../SFERA_3.0_custom.xsd"
SP_ID="4816_2" SP_VersionMajor="1" SP_VersionMinor="0" SP_Length="41500" SP_Status="Valid">
<SP_Zone>
<IM_ID>0085</IM_ID>
</SP_Zone>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<SegmentProfile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../SFERA_3.0_custom.xsd"
SP_ID="3" SP_VersionMajor="1" SP_VersionMinor="1" SP_Length="41500" SP_Status="Valid">
xsi:noNamespaceSchemaLocation="../../SFERA_3.0_custom.xsd"
SP_ID="7839_1" SP_VersionMajor="1" SP_VersionMinor="0" SP_Length="41500" SP_Status="Valid">
<SP_Zone>
<IM_ID>0085</IM_ID>
</SP_Zone>
Expand Down
Loading

0 comments on commit d53ca8c

Please sign in to comment.