Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Changed class name in FlexOptionsResult.toString [#693](https://github.com/ie3-institute/PowerSystemDataModel/issues/693)
- Deleted parameter decimalPlaces and changed naming of serialization method [#710](https://github.com/ie3-institute/PowerSystemDataModel/issues/710)
- Changed switch result documentation according to the implementation [#757](https://github.com/ie3-institute/PowerSystemDataModel/issues/757)
- Deleted parameter initFiles, set parameter append to false by default [#791](https://github.com/ie3-institute/PowerSystemDataModel/issues/791)

## [2.0.1] - 2021-07-08

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,12 @@ private BufferedCsvWriter initWriter(String baseDirectory, CsvFileDefinition fil
if (!directories.exists() && !directories.mkdirs())
throw new IOException("Unable to create directory tree '" + directories + "'");

File pathFile = new File(fullPath);
boolean append = pathFile.exists();
boolean append = false;
BufferedCsvWriter writer =
new BufferedCsvWriter(
fullPath, fileDefinition.headLineElements(), fileDefinition.csvSep(), append);
if (!append) {
writer.writeFileHeader();
} else {
log.warn(
"File '{}' already exist. Will append new content WITHOUT new header! Full path: {}",
fileDefinition.fileName(),
pathFile.getAbsolutePath());
}
writer.writeFileHeader();

return writer;
}

Expand Down
53 changes: 3 additions & 50 deletions src/main/java/edu/ie3/datamodel/io/sink/CsvFileSink.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class CsvFileSink implements InputDataSink, OutputDataSink {
private final String csvSep;

public CsvFileSink(String baseFolderPath) {
this(baseFolderPath, new FileNamingStrategy(), false, ",");
this(baseFolderPath, new FileNamingStrategy(), ",");
}

/**
Expand All @@ -69,17 +69,10 @@ public CsvFileSink(String baseFolderPath) {
*
* @param baseFolderPath the base folder path where the files should be put into
* @param fileNamingStrategy the data sink file naming strategy that should be used
* @param initFiles true if the files should be created during initialization (might create files,
* that only consist of a headline, because no data will be written into them), false
* otherwise
* @param csvSep the csv file separator that should be use
*/
public CsvFileSink(
String baseFolderPath,
FileNamingStrategy fileNamingStrategy,
boolean initFiles,
String csvSep) {
this(baseFolderPath, new ProcessorProvider(), fileNamingStrategy, initFiles, csvSep);
public CsvFileSink(String baseFolderPath, FileNamingStrategy fileNamingStrategy, String csvSep) {
this(baseFolderPath, new ProcessorProvider(), fileNamingStrategy, csvSep);
}

/**
Expand All @@ -94,22 +87,16 @@ public CsvFileSink(
* @param baseFolderPath the base folder path where the files should be put into
* @param processorProvider the processor provided that should be used for entity serialization
* @param fileNamingStrategy the data sink file naming strategy that should be used
* @param initFiles true if the files should be created during initialization (might create files,
* that only consist of a headline, because no data will be written into them), false
* otherwise
* @param csvSep the csv file separator that should be use
*/
public CsvFileSink(
String baseFolderPath,
ProcessorProvider processorProvider,
FileNamingStrategy fileNamingStrategy,
boolean initFiles,
String csvSep) {
this.csvSep = csvSep;
this.processorProvider = processorProvider;
this.connector = new CsvFileConnector(baseFolderPath, fileNamingStrategy);

if (initFiles) initFiles(processorProvider, connector);
}

@Override
Expand Down Expand Up @@ -352,40 +339,6 @@ private <C extends UniqueEntity> void write(C entity) {
}
}

/**
* Initialize files, hence create a file for each expected class that will be processed in the
* future. Please note, that files for time series can only be create on presence of a concrete
* time series, as their file name depends on the individual uuid of the time series.
*
* @param processorProvider the processor provider all files that will be processed is derived
* from
* @param connector the connector to the files
*/
private void initFiles(
final ProcessorProvider processorProvider, final CsvFileConnector connector) {
processorProvider
.getRegisteredClasses()
.forEach(
clz -> {
try {
String[] headerElements =
csvHeaderElements(processorProvider.getHeaderElements(clz));

connector.getOrInitWriter(clz, headerElements, csvSep);
} catch (ProcessorProviderException e) {
log.error(
"Error during receiving of head line elements. Cannot prepare writer for class {}.",
clz,
e);
} catch (ConnectorException e) {
log.error(
"Error during instantiation files. Cannot get or init writer for class {}.",
clz,
e);
}
});
}

/**
* Transforms a provided array of strings to valid csv formatted strings (according to csv
* specification RFC 4180)
Expand Down
115 changes: 0 additions & 115 deletions src/test/groovy/edu/ie3/datamodel/io/sink/CsvFileSinkTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -72,33 +72,6 @@ class CsvFileSinkTest extends Specification implements TimeSeriesTestData {
}
}

def "A valid CsvFileSink called by simple constructor should not initialize files by default and consist of several default values"() {
given:
CsvFileSink csvFileSink = new CsvFileSink(testBaseFolderPath)
csvFileSink.shutdown()

expect:
!new File(testBaseFolderPath).exists()
csvFileSink.csvSep == ","
}

def "A valid CsvFileSink with 'initFiles' enabled should create files as expected"() {
given:
CsvFileSink csvFileSink = new CsvFileSink(testBaseFolderPath,
new ProcessorProvider([
new ResultEntityProcessor(PvResult),
new ResultEntityProcessor(EvResult)
], [] as Map),
new FileNamingStrategy(),
true,
",")
csvFileSink.shutdown()

expect:
new File(testBaseFolderPath).exists()
new File(testBaseFolderPath + File.separator + "ev_res.csv").exists()
new File(testBaseFolderPath + File.separator + "pv_res.csv").exists()
}

def "A valid CsvFileSink is able to convert an entity data map correctly to RFC 4180 compliant strings"() {
given:
Expand Down Expand Up @@ -140,91 +113,6 @@ class CsvFileSinkTest extends Specification implements TimeSeriesTestData {
csvFileSink.shutdown()
}

def "A valid CsvFileSink without 'initFiles' should only persist provided elements correctly but not init all files"() {
given:
CsvFileSink csvFileSink = new CsvFileSink(testBaseFolderPath,
new ProcessorProvider([
new ResultEntityProcessor(PvResult),
new ResultEntityProcessor(WecResult),
new ResultEntityProcessor(EvResult),
new ResultEntityProcessor(EvcsResult),
new ResultEntityProcessor(EmResult),
new ResultEntityProcessor(FlexOptionsResult),
new InputEntityProcessor(Transformer2WInput),
new InputEntityProcessor(NodeInput),
new InputEntityProcessor(EvcsInput),
new InputEntityProcessor(Transformer2WTypeInput),
new InputEntityProcessor(LineGraphicInput),
new InputEntityProcessor(NodeGraphicInput),
new InputEntityProcessor(CylindricalStorageInput),
new InputEntityProcessor(ThermalHouseInput),
new InputEntityProcessor(OperatorInput),
new InputEntityProcessor(LineInput),
new InputEntityProcessor(ThermalBusInput),
new InputEntityProcessor(LineTypeInput),
new InputEntityProcessor(LoadInput),
new InputEntityProcessor(EmInput)
], [] as Map),
new FileNamingStrategy(),
false,
",")

UUID uuid = UUID.fromString("22bea5fc-2cb2-4c61-beb9-b476e0107f52")
UUID inputModel = UUID.fromString("22bea5fc-2cb2-4c61-beb9-b476e0107f52")
Quantity<Power> p = Quantities.getQuantity(10, StandardUnits.ACTIVE_POWER_IN)
Quantity<Power> q = Quantities.getQuantity(10, StandardUnits.REACTIVE_POWER_IN)
PvResult pvResult = new PvResult(uuid, TimeUtil.withDefaults.toZonedDateTime("2020-01-30 17:26:44"), inputModel, p, q)
WecResult wecResult = new WecResult(uuid, TimeUtil.withDefaults.toZonedDateTime("2020-01-30 17:26:44"), inputModel, p, q)
EvcsResult evcsResult = new EvcsResult(uuid, TimeUtil.withDefaults.toZonedDateTime("2020-01-30 17:26:44"), inputModel, p, q)
EmResult emResult = new EmResult(uuid, TimeUtil.withDefaults.toZonedDateTime("2020-01-30 17:26:44"), inputModel, p, q)

Quantity<Power> pRef = Quantities.getQuantity(5.1, StandardUnits.ACTIVE_POWER_RESULT)
Quantity<Power> pMin = Quantities.getQuantity(-6, StandardUnits.ACTIVE_POWER_RESULT)
Quantity<Power> pMax = Quantities.getQuantity(6, StandardUnits.ACTIVE_POWER_RESULT)
FlexOptionsResult flexOptionsResult = new FlexOptionsResult(uuid, TimeUtil.withDefaults.toZonedDateTime("2020-01-30 17:26:44"), inputModel, pRef, pMin, pMax)

when:
csvFileSink.persistAll([
pvResult,
wecResult,
evcsResult,
emResult,
flexOptionsResult,
GridTestData.transformerCtoG,
GridTestData.lineGraphicCtoD,
GridTestData.nodeGraphicC,
ThermalUnitInputTestData.cylindricStorageInput,
ThermalUnitInputTestData.thermalHouseInput,
SystemParticipantTestData.evcsInput,
SystemParticipantTestData.loadInput,
SystemParticipantTestData.emInput
])
csvFileSink.shutdown()

then:
new File(testBaseFolderPath).exists()
new File(testBaseFolderPath + File.separator + "wec_res.csv").exists()
new File(testBaseFolderPath + File.separator + "pv_res.csv").exists()
new File(testBaseFolderPath + File.separator + "evcs_res.csv").exists()
new File(testBaseFolderPath + File.separator + "em_res.csv").exists()
new File(testBaseFolderPath + File.separator + "flex_options_res.csv").exists()
new File(testBaseFolderPath + File.separator + "transformer_2_w_type_input.csv").exists()
new File(testBaseFolderPath + File.separator + "node_input.csv").exists()
new File(testBaseFolderPath + File.separator + "transformer_2_w_input.csv").exists()
new File(testBaseFolderPath + File.separator + "operator_input.csv").exists()
new File(testBaseFolderPath + File.separator + "cylindrical_storage_input.csv").exists()
new File(testBaseFolderPath + File.separator + "line_graphic_input.csv").exists()
new File(testBaseFolderPath + File.separator + "line_input.csv").exists()
new File(testBaseFolderPath + File.separator + "operator_input.csv").exists()
new File(testBaseFolderPath + File.separator + "node_graphic_input.csv").exists()
new File(testBaseFolderPath + File.separator + "thermal_bus_input.csv").exists()
new File(testBaseFolderPath + File.separator + "thermal_house_input.csv").exists()
new File(testBaseFolderPath + File.separator + "load_input.csv").exists()
new File(testBaseFolderPath + File.separator + "em_input.csv").exists()

!new File(testBaseFolderPath + File.separator + "ev_res.csv").exists()
}

def "A valid CsvFileSink should persist a time series correctly"() {
given:
TimeSeriesProcessor<IndividualTimeSeries, TimeBasedValue, EnergyPriceValue> timeSeriesProcessor = new TimeSeriesProcessor<>(IndividualTimeSeries, TimeBasedValue, EnergyPriceValue)
Expand All @@ -237,7 +125,6 @@ class CsvFileSinkTest extends Specification implements TimeSeriesTestData {
CsvFileSink csvFileSink = new CsvFileSink(testBaseFolderPath,
new ProcessorProvider([], timeSeriesProcessorMap),
new FileNamingStrategy(),
false,
",")

when:
Expand Down Expand Up @@ -311,7 +198,6 @@ class CsvFileSinkTest extends Specification implements TimeSeriesTestData {
ProcessorProvider.allEntityProcessors(),
new HashMap<TimeSeriesProcessorKey, TimeSeriesProcessor<TimeSeries<TimeSeriesEntry<Value>, Value>, TimeSeriesEntry<Value>, Value>>()),
new FileNamingStrategy(),
false,
",")

when:
Expand All @@ -331,7 +217,6 @@ class CsvFileSinkTest extends Specification implements TimeSeriesTestData {
testBaseFolderPath,
new ProcessorProvider(),
new FileNamingStrategy(),
false,
",")

when:
Expand Down