Skip to content

Commit

Permalink
chore: new mock data structure (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
mghilardelli authored Dec 12, 2024
1 parent 151f24e commit 6cdb7d7
Show file tree
Hide file tree
Showing 28 changed files with 58 additions and 21 deletions.
13 changes: 4 additions & 9 deletions sfera-mock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@
- architecture: BoardAdviceCalculation
- connectivity: Connected

| Train number | Result |
|--------------|--------------------------|
| 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
see journeys [src/main/resources/static_sfera_resources](src/main/resources/static_sfera_resources)

## 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`
1. add a new directory named `<train number>_<optional comment>` in `static_sfera_resources`
2. add a journey profile named `SFERA_JP_<train number>` to the directory
2. add corresponding segment profiles named `SFERA_SP_<train number>_<sp id>` to the directory

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ch.sbb.sferamock.messages;

import ch.sbb.sferamock.messages.services.JourneyProfileRepository;
import java.util.Set;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

@Component
@Endpoint(id = "trains")
public class TrainsActuator {

private final JourneyProfileRepository journeyProfileRepository;

public TrainsActuator(JourneyProfileRepository journeyProfileRepository) {
this.journeyProfileRepository = journeyProfileRepository;
}

@ReadOperation
public Set<String> getJps() {
return journeyProfileRepository.getAvailableJourneyProfiles();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.boot.ApplicationArguments;
Expand All @@ -20,8 +21,8 @@
@Service
public class JourneyProfileRepository implements ApplicationRunner {

private static final String XML_RESOURCES_CLASSPATH = "classpath:static_sfera_resources/jp/*.xml";
private static final String XML_REGEX = "SFERA_JP_(.+)\\.xml";
private static final String XML_RESOURCES_CLASSPATH = "classpath:static_sfera_resources/*/SFERA_JP_*.xml";
private static final String XML_REGEX = "/([a-zA-Z0-9]+)_\\w+/SFERA_JP_([a-zA-Z0-9]+)\\.xml";
private final XmlHelper xmlHelper;

Map<String, JourneyProfile> journeyProfiles = new HashMap<>();
Expand All @@ -39,12 +40,16 @@ public Optional<JourneyProfile> getJourneyProfile(TrainIdentification trainIdent
return Optional.ofNullable(journeyProfiles.get(trainIdentification.operationalNumber()));
}

public Set<String> getAvailableJourneyProfiles() {
return journeyProfiles.keySet();
}

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());
var operationalNumber = extractOperationalNumber(file.getPath());
try (InputStream in = new FileInputStream(file)) {
String xmlPayload = new String(in.readAllBytes());
var journeyProfile = xmlHelper.xmlToObject(xmlPayload);
Expand All @@ -53,12 +58,16 @@ private void importJps() throws IOException {
}
}

public static String extractOperationalNumber(String filename) {
private static String extractOperationalNumber(String filename) {
Pattern pattern = Pattern.compile(XML_REGEX);
Matcher matcher = pattern.matcher(filename);
if (matcher.find()) {
return matcher.group(1);
String directoryOperationalNumber = matcher.group(1);
String fileOperationalNumber = matcher.group(2);
if (directoryOperationalNumber != null && directoryOperationalNumber.equals(fileOperationalNumber)) {
return directoryOperationalNumber;
}
}
return null;
throw new RuntimeException("Operational number extraction in JP repository failed for file: " + filename);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
@Service
public class SegmentProfileRepository implements ApplicationRunner {

private static final String XML_RESOURCES_CLASSPATH = "classpath:static_sfera_resources/sp/*.xml";
private static final String XML_REGEX = "SFERA_SP_(.+_\\d+)\\.xml";
private static final String XML_RESOURCES_CLASSPATH = "classpath:static_sfera_resources/*/SFERA_SP_*.xml";
private static final String XML_REGEX = "/([a-zA-Z0-9]+)_\\w+/SFERA_SP_(([a-zA-Z0-9]+)_\\w+)\\.xml";

private final XmlHelper xmlHelper;

Expand All @@ -45,7 +45,7 @@ private void importSps() throws IOException {
var resources = resolver.getResources(XML_RESOURCES_CLASSPATH);
for (var resource : resources) {
File file = resource.getFile();
var segmentId = extractSpId(file.getName());
var segmentId = extractSpId(file.getPath());
try (InputStream in = new FileInputStream(file)) {
String xmlPayload = new String(in.readAllBytes());
var segmentProfile = xmlHelper.xmlToObject(xmlPayload);
Expand All @@ -58,8 +58,12 @@ public static String extractSpId(String filename) {
Pattern pattern = Pattern.compile(XML_REGEX);
Matcher matcher = pattern.matcher(filename);
if (matcher.find()) {
return matcher.group(1);
String directoryOperationalNumber = matcher.group(1);
String fileOperationalNumber = matcher.group(3);
if (directoryOperationalNumber != null && directoryOperationalNumber.equals(fileOperationalNumber)) {
return matcher.group(2);
}
}
return null;
throw new RuntimeException("SP id extraction failed for file: " + filename);
}
}
6 changes: 6 additions & 0 deletions sfera-mock/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ management:
health:
probes:
enabled: true
endpoints:
web:
exposure:
include: health,trains



spring:
profiles:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@
</KilometreReferencePoint>

</SP_ContextInformation>
</SegmentProfile>
</SegmentProfile>

0 comments on commit 6cdb7d7

Please sign in to comment.