Skip to content

Commit

Permalink
#98 working on refactoring output configuration and output formatter …
Browse files Browse the repository at this point in the history
…to make consolidation in single file possible in a nice way. not done yet.

- added option to planit output formatter flag to signal this (does not do anything yet)
- continue with refactoring writing csvs to be able to feed in file name based on iteration inclusion or not, adding in writing to file or string and to choose whether a header is to be included or not (when consolidating, only the initial header is to be included, not subsequent ones)
- then for simulation data print to string with single header, track in list per iteration and then print final result in one go using the same general approach
  • Loading branch information
MarkRaadsen committed Jul 21, 2024
1 parent d059ac4 commit 3bc4326
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,6 @@ public class SimulationOutputTypeConfiguration extends OutputTypeConfiguration {
/** the logger */
private static final Logger LOGGER = Logger.getLogger(MacroscopicLinkSegmentImpl.class.getCanonicalName());

/**
* Constructor
*
* Define the default output properties here.
*/
public SimulationOutputTypeConfiguration(){
super(OutputType.SIMULATION);
initialiseDefaultOutputProperties();

/* simulation info by default is of most use when recorded each iteration as usually we have just a
single value for each property per iteration */
setPersistOnlyFinalIteration(false);
}

/**
* Set the default output properties for the path output configuration
*/
Expand All @@ -65,6 +51,20 @@ private void initialiseDefaultOutputProperties() {
addProperty(OutputPropertyType.ROUTE_CHOICE_CONVERGENCE_GAP);
}

/**
* Constructor
*
* Define the default output properties here.
*/
public SimulationOutputTypeConfiguration(){
super(OutputType.SIMULATION);
initialiseDefaultOutputProperties();

/* simulation info by default is of most use when recorded each iteration as usually we have just a
single value for each property per iteration, so persist all iterations as the default */
setPersistOnlyFinalIteration(false);
}

/**
* Validate whether the specified list of keys is valid, and if it is return only the keys which will be used
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,11 @@ public BaseOutputFormatter(IdGroupingToken groupId) {
*/
@Override
public void persist(
TimePeriod timePeriod, Set<Mode> modes, OutputConfiguration outputConfiguration, OutputTypeConfiguration outputTypeConfiguration, OutputAdapter outputAdapter) {
TimePeriod timePeriod,
Set<Mode> modes,
OutputConfiguration outputConfiguration,
OutputTypeConfiguration outputTypeConfiguration,
OutputAdapter outputAdapter) {

OutputType outputType = outputTypeConfiguration.getOutputType();
OutputProperty[] outputValuePropertyArray = outputTypeConfiguration.getOutputValueProperties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ public abstract class CsvFileOutputFormatter extends FileOutputFormatter {
*/
protected final Map<OutputTypeEnum, List<String>> csvFileNameMap;

/**
* Based on the output type configuration generate the CSV header
* @param outputTypeConfiguration to use
* @return CSV header as list of strings
*/
protected static List<String> generateCsvHeader(final OutputTypeConfiguration outputTypeConfiguration){
return outputTypeConfiguration.getOutputProperties().stream().map(
OutputProperty::getName).collect(Collectors.toList());
}

/**
* Constructor
*
Expand Down Expand Up @@ -302,15 +312,17 @@ protected PlanItException writeLinkResultsForCurrentTimePeriodToCsvPrinter(Outpu
/**
* Open the CSV output file and write the headers to it
*
* @param outputTypeConfiguration the current output type configuration
* @param csvFileName the name of the CSV output file
* @return the CSVPrinter object (output values will be written to this in subsequent rows)
* @throws Exception thrown if there is an error opening the file
*/
protected CSVPrinter openCsvFileAndWriteHeaders(OutputTypeConfiguration outputTypeConfiguration, String csvFileName) throws Exception {
CSVPrinter csvPrinter = new CSVPrinter(new FileWriter(csvFileName), CSVFormat.Builder.create(CSVFormat.DEFAULT).setIgnoreSurroundingSpaces(true).build());
List<String> headerValues = outputTypeConfiguration.getOutputProperties().stream().map(OutputProperty::getName).collect(Collectors.toList());
csvPrinter.printRecord(headerValues);
protected CSVPrinter createCsvPrinter(String csvFileName) throws Exception {

CSVPrinter csvPrinter =
new CSVPrinter(
new FileWriter(csvFileName),
CSVFormat.Builder.create(CSVFormat.DEFAULT).setIgnoreSurroundingSpaces(true).build());

return csvPrinter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,15 @@ protected FileOutputFormatter(IdGroupingToken groupId) {
* @return the name of the output file in absolute form
* @throws PlanItException thrown if the output directory cannot be opened
*/
protected String generateAbsoluteOutputFileName(String outputDirectory, String nameRoot, String nameExtension, TimePeriod timePeriod, OutputType outputType, long runId, int iteration)
throws PlanItException {
protected static String generateAbsoluteOutputFileName(
String outputDirectory,
String nameRoot,
String nameExtension,
TimePeriod timePeriod,
OutputType outputType,
long runId,
int iteration) throws PlanItException {

try {
Path outputDirPath = Path.of(outputDirectory).toAbsolutePath();
if (!Files.isDirectory(outputDirPath)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,9 @@ public void initialiseBeforeSimulation(OutputConfiguration outputConfiguration,
*
* @param outputConfiguration OutputConfiguration of the assignment
* @param outputAdapter the outputAdapter
* @throws PlanItException thrown if there is an error
*/
@Override
public void finaliseAfterSimulation(OutputConfiguration outputConfiguration, OutputAdapter outputAdapter) throws PlanItException {
public void finaliseAfterSimulation(OutputConfiguration outputConfiguration, OutputAdapter outputAdapter){
// do nothing
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ public interface OutputFormatter {
*
* @param outputConfiguration OutputTypeConfiguration for the assignment to be saved
* @param outputAdapter the outputAdapter
* @throws PlanItException thrown if there is an error
*/
public void finaliseAfterSimulation(OutputConfiguration outputConfiguration, OutputAdapter outputAdapter) throws PlanItException;
public void finaliseAfterSimulation(OutputConfiguration outputConfiguration, OutputAdapter outputAdapter);

/**
* Flag to indicate whether an implementation can handle multiple iterations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void setKappaStep(double kappaStep) {
/**
* Get the lambda flag
*
* @return kappaStep the kappa step
* @return activateLambda flag
*/
public Boolean isActivateLambda() {
return (Boolean) getFirstParameterOfDelayedMethodCall(SET_ACTIVATE_LAMBDA);
Expand Down

0 comments on commit 3bc4326

Please sign in to comment.