Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@
import com.powsybl.commons.datasource.DataSource;
import com.powsybl.iidm.network.DanglingLine;
import com.powsybl.iidm.network.Identifiable;
import com.univocity.parsers.csv.*;
import de.siegmar.fastcsv.writer.CsvWriter;
import de.siegmar.fastcsv.writer.LineDelimiter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.*;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UncheckedIOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static com.powsybl.cgmes.conversion.naming.CgmesObjectReference.*;
import static com.powsybl.cgmes.conversion.naming.CgmesObjectReference.combine;
import static com.powsybl.cgmes.conversion.naming.CgmesObjectReference.ref;
import static com.powsybl.cgmes.conversion.naming.CgmesObjectReference.refTyped;

/**
* @author Miora Vedelago {@literal <miora.ralambotiana at rte-france.com>}
Expand Down Expand Up @@ -117,37 +124,33 @@ public void debug(String baseName, DataSource ds) {
return;
}
String mappingFilename = baseName + "_debug_naming_strategy.csv";
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(ds.newOutputStream(mappingFilename, false)))) {
CsvWriterSettings settings = new CsvWriterSettings();
settings.getFormat().setLineSeparator(System.lineSeparator());
settings.getFormat().setDelimiter(';');
settings.getFormat().setQuoteEscape('"');
CsvWriter csvWriter = new CsvWriter(writer, settings);
try {
String[] nextLine = new String[3];
nextLine[0] = "CgmesUuid";
nextLine[1] = "IidmId";
nextLine[2] = "Seed";
csvWriter.writeRow(nextLine);

for (Map.Entry<String, String> e : idByUuid.entrySet()) {
String uuid = e.getKey();
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(ds.newOutputStream(mappingFilename, false)));
CsvWriter csvWriter = CsvWriter.builder()
.fieldSeparator(';')
.lineDelimiter(LineDelimiter.PLATFORM)
.quoteCharacter('"')
.build(writer)) {
String[] nextLine = new String[3];
nextLine[0] = "CgmesUuid";
nextLine[1] = "IidmId";
nextLine[2] = "Seed";
csvWriter.writeRecord(nextLine);

for (Map.Entry<String, String> e : idByUuid.entrySet()) {
String uuid = e.getKey();
nextLine[0] = uuid;
nextLine[1] = e.getValue();
nextLine[2] = uuidSeed.get(uuid);
csvWriter.writeRecord(nextLine);
}
for (Map.Entry<String, String> e : uuidSeed.entrySet()) {
String uuid = e.getKey();
if (!idByUuid.containsKey(uuid)) {
nextLine[0] = uuid;
nextLine[1] = e.getValue();
nextLine[1] = "unknown";
nextLine[2] = uuidSeed.get(uuid);
csvWriter.writeRow(nextLine);
}
for (Map.Entry<String, String> e : uuidSeed.entrySet()) {
String uuid = e.getKey();
if (!idByUuid.containsKey(uuid)) {
nextLine[0] = uuid;
nextLine[1] = "unknown";
nextLine[2] = uuidSeed.get(uuid);
csvWriter.writeRow(nextLine);
}
csvWriter.writeRecord(nextLine);
}
} finally {
csvWriter.close();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
Expand Down
8 changes: 4 additions & 4 deletions commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@
<groupId>com.google.re2j</groupId>
<artifactId>re2j</artifactId>
</dependency>
<dependency>
<groupId>com.univocity</groupId>
<artifactId>univocity-parsers</artifactId>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
Expand All @@ -72,6 +68,10 @@
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>de.siegmar</groupId>
<artifactId>fastcsv</artifactId>
</dependency>
<dependency>
<groupId>net.java.dev.stax-utils</groupId>
<artifactId>stax-utils</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.powsybl.commons.PowsyblException;
import com.univocity.parsers.csv.*;
import de.siegmar.fastcsv.reader.CsvReader;
import de.siegmar.fastcsv.reader.CsvRecord;
import de.siegmar.fastcsv.writer.CsvWriter;
import de.siegmar.fastcsv.writer.LineDelimiter;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.util.Arrays;
import java.util.Map;
import java.io.IOException;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
Expand Down Expand Up @@ -66,21 +68,19 @@ public void readCsv(BufferedReader reader) {
readCsv(reader, DEFAULT_SEPARATOR);
}

private static void setFormat(CsvFormat format, char separator) {
format.setLineSeparator(System.lineSeparator());
format.setDelimiter(separator);
format.setQuoteEscape('"');
}

public void readCsv(BufferedReader reader, char separator) {
CsvParserSettings settings = new CsvParserSettings();
setFormat(settings.getFormat(), separator);
CsvParser csvParser = new CsvParser(settings);
for (String[] nextLine : csvParser.iterate(reader)) {
if (nextLine.length != 2) {
throw new PowsyblException("Invalid line '" + Arrays.toString(nextLine) + "'");
}
mapping.put(nextLine[0], nextLine[1]);
try (CsvReader<CsvRecord> csvReader = CsvReader.builder()
.fieldSeparator(separator)
.quoteCharacter('"')
.ofCsvRecord(reader)) {
csvReader.forEach(csvRecord -> {
if (csvRecord.getFieldCount() != 2) {
throw new PowsyblException("Invalid line '" + csvRecord + "'");
}
mapping.put(csvRecord.getField(0), csvRecord.getField(1));
});
} catch (IOException e) {
throw new PowsyblException("Failed to read the CSV", e);
}
}

Expand All @@ -89,18 +89,14 @@ public void writeCsv(BufferedWriter writer) {
}

public void writeCsv(BufferedWriter writer, char separator) {
CsvWriterSettings settings = new CsvWriterSettings();
setFormat(settings.getFormat(), separator);
CsvWriter csvWriter = new CsvWriter(writer, settings);
try {
String[] nextLine = new String[2];
for (Map.Entry<String, String> e : mapping.entrySet()) {
nextLine[0] = e.getKey();
nextLine[1] = e.getValue();
csvWriter.writeRow(nextLine);
}
} finally {
csvWriter.close();
try (CsvWriter csvWriter1 = CsvWriter.builder()
.fieldSeparator(separator)
.quoteCharacter('"')
.lineDelimiter(LineDelimiter.PLATFORM)
.build(writer)) {
mapping.forEach(csvWriter1::writeRecord);
} catch (IOException e) {
throw new PowsyblException("Failed to write the CSV", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package com.powsybl.commons.util;

import com.powsybl.commons.PowsyblException;
import org.junit.jupiter.api.Test;

import java.io.*;
Expand All @@ -20,11 +21,8 @@ class StringAnonymizerTest {

private static String toCsv(StringAnonymizer anonymizer) throws IOException {
StringWriter stringWriter = new StringWriter();
BufferedWriter writer = new BufferedWriter(stringWriter);
try {
try (BufferedWriter writer = new BufferedWriter(stringWriter)) {
anonymizer.writeCsv(writer);
} finally {
writer.close();
}
return stringWriter.toString();
}
Expand All @@ -50,14 +48,11 @@ void test() throws IOException {
assertEquals("bar", anonymizer.deanonymize(anonymizedBar));
assertNull(anonymizer.anonymize(null));
assertNull(anonymizer.deanonymize(null));
try {
anonymizer.deanonymize("baz");
fail();
} catch (Exception ignored) {
}
PowsyblException exception = assertThrows(PowsyblException.class, () -> anonymizer.deanonymize("baz"));
assertEquals("Mapping not found for anonymized string 'baz'", exception.getMessage());
String csv = toCsv(anonymizer);
assertEquals(String.join(System.lineSeparator(), "foo;A", "bar;B") + System.lineSeparator(),
csv.toString());
csv);
StringAnonymizer anonymizer2 = fromCsv(csv);
assertEquals("foo", anonymizer2.deanonymize(anonymizedFoo));
assertEquals("bar", anonymizer2.deanonymize(anonymizedBar));
Expand All @@ -73,7 +68,7 @@ void invalidFileTest() throws IOException {
}

@Test
void invalidFileTest2() throws IOException {
void invalidFileTest2() {
String csv = String.join(System.lineSeparator(),
"C");
assertThrows(RuntimeException.class, () -> fromCsv(csv));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
import com.powsybl.ieeecdf.model.IeeeCdfTitle;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.NetworkFactory;
import com.univocity.parsers.csv.CsvParser;
import com.univocity.parsers.csv.CsvParserSettings;
import de.siegmar.fastcsv.reader.CsvReader;
import de.siegmar.fastcsv.reader.CsvRecord;

import java.io.IOException;
import java.time.LocalDate;
import java.util.Objects;
import java.util.Properties;
import java.util.function.ToDoubleFunction;

Expand Down Expand Up @@ -136,42 +138,48 @@ public static Network create9zeroimpedance() {
return create9zeroimpedance(NetworkFactory.findDefault());
}

private static void parseBuses(IeeeCdfModel model, CsvParserSettings settings, String fileName, double baseKv) {
CsvParser csvParser = new CsvParser(settings);
for (String[] nextLine : csvParser.iterate(IeeeCdfNetworkFactory.class.getResourceAsStream("/" + fileName))) {
int busNo = Integer.parseInt(nextLine[0]);
int busCode = Integer.parseInt(nextLine[1]);
double loadP = Double.parseDouble(nextLine[2]);
double loadQ = Double.parseDouble(nextLine[3]);
IeeeCdfBus bus = new IeeeCdfBus();
bus.setNumber(busNo);
bus.setName("bus-" + busNo);
bus.setBaseVoltage(baseKv);
bus.setActiveLoad(loadP / 1000);
bus.setReactiveLoad(loadQ / 1000);
if (busCode == 1) {
bus.setType(IeeeCdfBus.Type.HOLD_VOLTAGE_AND_ANGLE);
bus.setDesiredVoltage(1);
} else {
bus.setType(IeeeCdfBus.Type.UNREGULATED);
}
model.getBuses().add(bus);
private static void parseBuses(IeeeCdfModel model, CsvReader.CsvReaderBuilder builder, String fileName, double baseKv) {
try (CsvReader<CsvRecord> csvReader = builder.ofCsvRecord(Objects.requireNonNull(IeeeCdfNetworkFactory.class.getResourceAsStream("/" + fileName)))) {
csvReader.forEach(csvRecord -> {
int busNo = Integer.parseInt(csvRecord.getField(0));
int busCode = Integer.parseInt(csvRecord.getField(1));
double loadP = Double.parseDouble(csvRecord.getField(2));
double loadQ = Double.parseDouble(csvRecord.getField(3));
IeeeCdfBus bus = new IeeeCdfBus();
bus.setNumber(busNo);
bus.setName("bus-" + busNo);
bus.setBaseVoltage(baseKv);
bus.setActiveLoad(loadP / 1000);
bus.setReactiveLoad(loadQ / 1000);
if (busCode == 1) {
bus.setType(IeeeCdfBus.Type.HOLD_VOLTAGE_AND_ANGLE);
bus.setDesiredVoltage(1);
} else {
bus.setType(IeeeCdfBus.Type.UNREGULATED);
}
model.getBuses().add(bus);
});
} catch (IOException exception) {
throw new PowsyblException("Failed to read the CSV", exception);
}
}

private static void parseLines(IeeeCdfModel model, CsvParserSettings settings, String fileName) {
CsvParser csvParser = new CsvParser(settings);
for (String[] nextLine : csvParser.iterate(IeeeCdfNetworkFactory.class.getResourceAsStream("/" + fileName))) {
int sendingBus = Integer.parseInt(nextLine[0]);
int receivingBus = Integer.parseInt(nextLine[1]);
double r = Double.parseDouble(nextLine[2]);
double x = Double.parseDouble(nextLine[3]);
IeeeCdfBranch branch = new IeeeCdfBranch();
branch.setTapBusNumber(sendingBus);
branch.setzBusNumber(receivingBus);
branch.setResistance(r);
branch.setReactance(x);
model.getBranches().add(branch);
private static void parseLines(IeeeCdfModel model, CsvReader.CsvReaderBuilder builder, String fileName) {
try (CsvReader<CsvRecord> csvReader = builder.ofCsvRecord(Objects.requireNonNull(IeeeCdfNetworkFactory.class.getResourceAsStream("/" + fileName)))) {
csvReader.forEach(csvRecord -> {
int sendingBus = Integer.parseInt(csvRecord.getField(0));
int receivingBus = Integer.parseInt(csvRecord.getField(1));
double r = Double.parseDouble(csvRecord.getField(2));
double x = Double.parseDouble(csvRecord.getField(3));
IeeeCdfBranch branch = new IeeeCdfBranch();
branch.setTapBusNumber(sendingBus);
branch.setzBusNumber(receivingBus);
branch.setResistance(r);
branch.setReactance(x);
model.getBranches().add(branch);
});
} catch (IOException exception) {
throw new PowsyblException("Failed to read the CSV", exception);
}
}

Expand All @@ -183,13 +191,12 @@ private static Network createFromCsv(String name, NetworkFactory networkFactory,
title.setMvaBase(100);
title.setDate(LocalDate.parse("2022-09-23"));
IeeeCdfModel model = new IeeeCdfModel(title);
CsvParserSettings settings = new CsvParserSettings();
settings.getFormat().setLineSeparator(System.lineSeparator());
settings.getFormat().setDelimiter(" ");
parseBuses(model, settings, name + "-bus.csv", baseKv);
parseLines(model, settings, name + "-line.csv");
CsvReader.CsvReaderBuilder builder = CsvReader.builder()
.fieldSeparator(" ");
parseBuses(model, builder, name + "-bus.csv", baseKv);
parseLines(model, builder, name + "-line.csv");
if (meshed) {
parseLines(model, settings, name + "-mesh.csv");
parseLines(model, builder, name + "-mesh.csv");
}
return new IeeeCdfImporter().convert(model, networkFactory, name, false);
}
Expand Down
Loading