Skip to content

Commit

Permalink
Additionally export request configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
munterfi committed Jun 11, 2024
1 parent 00672e9 commit e3f3bce
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 5 deletions.
2 changes: 0 additions & 2 deletions src/main/java/ch/sbb/rssched/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public class Application {

// TODO: Export also RSSched configuration excel to output folder

// TODO: Use instance ID for file paths.

public static final String APP_CMD_SYNTAX = "rssched-matsim-client <config_file>";
public static final String DEFAULT_HOST = "http://localhost";
public static final String DEFAULT_PORT = "3000";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import ch.sbb.rssched.client.config.selection.FilterStrategy;
import ch.sbb.rssched.client.config.selection.NoFilterStrategy;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -39,6 +45,15 @@ public static Builder builder() {
return new Builder();
}

public String toJSON() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Jdk8Module());
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); // Ensure dates are written in ISO 8601 format
mapper.enable(SerializationFeature.INDENT_OUTPUT);
return mapper.writeValueAsString(this);
}

/**
* Builder class for constructing a RequestConfig instance with customized settings. Allows addition of depots,
* shunting locations, and maintenance slots.
Expand Down Expand Up @@ -138,10 +153,13 @@ public static class Global {
* Note: The transit vehicle type ids must match / exist in the matsim scenario.
*/
private final Set<VehicleType> vehicleTypes = new HashSet<>();

/**
* The filter strategy to filter transit lines of interest, default is no filter.
*/
@JsonIgnore
private FilterStrategy filterStrategy = new NoFilterStrategy();

/**
* The sample size of the run, needed to scale to 100% for the demand.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ch.sbb.rssched.client.pipeline.request;

import ch.sbb.rssched.client.config.RsschedRequestConfig;
import ch.sbb.rssched.client.pipeline.core.ResultSink;
import ch.sbb.rssched.client.pipeline.utils.io.OutputDirectoryManager;
import lombok.extern.log4j.Log4j2;

import java.io.FileWriter;
import java.io.IOException;


/**
* Request config JSON writer
* <p>
* Writes the scheduler request configuration to a JSON file in the specified output directory.
*
* @author munterfi
*/
@Log4j2
public class RequestConfigWriter implements ResultSink<RequestPipe> {
private static final String REQUEST_CONFIG_FILE_NAME = "scheduler_request_config.json";
private final RsschedRequestConfig config;

public RequestConfigWriter(RsschedRequestConfig config) {
this.config = config;
}

@Override
public void process(RequestPipe pipe) {
String filePath = new OutputDirectoryManager(config.getOutputDirectory(), pipe.getRunId(),
config.getInstanceId()).buildFilePath(REQUEST_CONFIG_FILE_NAME);
log.info("Exporting request config JSON to {}", filePath);
try (FileWriter fileWriter = new FileWriter(filePath)) {
fileWriter.write(config.toJSON());
} catch (IOException e) {
throw new RuntimeException("Error writing the JSON file: " + e.getMessage(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
* Request JSON writer
* <p>
* Writes the scheduler request to a JSON file in the specified output directory.
* <p>
* Note: Although this would be a sink,
*
* @author munterfi
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public RequestPipeline(RsschedRequestConfig config) {
// add filter
addFilter(new RequestComposer(config));
// add sink
addSink(new RequestConfigWriter(config));
addSink(new RequestJSONWriter(config.getOutputDirectory(), config.getInstanceId()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ void shouldReadConfigFile() throws IOException {
assertEquals(999, config.getDepot().getDefaultCapacity());
assertEquals("dpt_", config.getDepot().getDefaultIdPrefix());
assertFalse(config.getDepot().isCreateAtTerminalLocations());

}

}

0 comments on commit e3f3bce

Please sign in to comment.