Skip to content

Commit

Permalink
new eurostag name strategy based on CutEurostagNamingStrategy and csv…
Browse files Browse the repository at this point in the history
… iidm/eurostag mappings file
  • Loading branch information
Christian Biasuzzi committed Nov 25, 2016
1 parent 87ce7c5 commit 6e4d6bc
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@ public class CutEurostagNamingStrategy implements EurostagNamingStrategy {
public void fillDictionary(EurostagDictionary dictionary, NameType nameType, Set<String> iidmIds) {
iidmIds.forEach(iidmId -> {
if (!dictionary.iidmIdExists(iidmId)) {
String esgId = iidmId.length() > nameType.getLength() ? iidmId.substring(0, nameType.getLength())
: Strings.padEnd(iidmId, nameType.getLength(), ' ');
int counter = 0;
while (dictionary.esgIdExists(esgId)) {
String counterStr = Integer.toString(counter++);
if (counterStr.length() > nameType.getLength()) {
throw new RuntimeException("Renaming fatal error " + iidmId + " -> " + esgId);
}
esgId = esgId.substring(0, nameType.getLength() - counterStr.length()) + counterStr;
}
String esgId = getEsgId(dictionary, nameType, iidmId);
dictionary.add(iidmId, esgId);
}
});
}

protected String getEsgId(EurostagDictionary dictionary, NameType nameType, String iidmId) {
String esgId = iidmId.length() > nameType.getLength() ? iidmId.substring(0, nameType.getLength())
: Strings.padEnd(iidmId, nameType.getLength(), ' ');
int counter = 0;
while (dictionary.esgIdExists(esgId)) {
String counterStr = Integer.toString(counter++);
if (counterStr.length() > nameType.getLength()) {
throw new RuntimeException("Renaming fatal error " + iidmId + " -> " + esgId);
}
esgId = esgId.substring(0, nameType.getLength() - counterStr.length()) + counterStr;
}
return esgId;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* Copyright (c) 2016, All partners of the iTesla project (http://www.itesla-project.eu/consortium)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package eu.itesla_project.iidm.eurostag.export;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author Christian Biasuzzi <christian.biasuzzi@techrain.it>
*/
public class DicoEurostagNamingStrategy implements EurostagNamingStrategy {

private static final Logger LOGGER = LoggerFactory.getLogger(DicoEurostagNamingStrategy.class);

private BiMap<String, String> dicoMap = HashBiMap.create(100);

private CutEurostagNamingStrategy defaultStrategy = new CutEurostagNamingStrategy();

class DicoCsvReader {

private static final String SEPARATOR = ";";

private final Reader source;

DicoCsvReader(Reader source) {
this.source = source;
}

List<List<String>> readDicoMappings() throws IOException {
try (BufferedReader reader = new BufferedReader(source)) {
return reader.lines()
.skip(1)
.map(line -> Arrays.asList(line.split(SEPARATOR)))
.collect(Collectors.toList());
}
}
}

public DicoEurostagNamingStrategy(Path dicoFile) {
if ((dicoFile == null) || (!Files.isRegularFile(dicoFile))) {
String errMsg = "csv file does not exist or is not valid: " + dicoFile;
LOGGER.error(errMsg);
throw new RuntimeException(errMsg);
} else {
LOGGER.debug("reading iidm-esgid mapping from csv file " + dicoFile);
try {
Reader reader = Files.newBufferedReader(dicoFile, Charset.forName("UTF-8"));
DicoCsvReader dicoReader = new DicoCsvReader(reader);
List<List<String>> dicoMappings = dicoReader.readDicoMappings();
int count = 1;
for (List<String> row : dicoMappings) {
count++;
String iidmId = row.get(0).trim();
String esgId = row.get(1).trim();
if (esgId.length() > NameType.GENERATOR.getLength()) {
String errMsg = "esgId: " + esgId + " 's length > " + NameType.GENERATOR.getLength() + ". Line " + count;
throw new RuntimeException(errMsg);
}
if ("".equals(iidmId) || "".equals(esgId)) {
String errMsg = "either iidmId or esgId or both are empty strings. Line " + count;
LOGGER.error(errMsg);
throw new RuntimeException(errMsg);
}
if (dicoMap.containsKey(esgId)) {
String errMsg = "esgId: " + esgId + " already mapped.";
LOGGER.error(errMsg);
throw new RuntimeException(errMsg);
}
dicoMap.put(iidmId, esgId);
}
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
}

@Override
public void fillDictionary(EurostagDictionary dictionary, NameType nameType, Set<String> iidmIds) {
iidmIds.forEach(iidmId -> {
if (!dictionary.iidmIdExists(iidmId)) {
String esgId;
if ((nameType == NameType.GENERATOR) && (dicoMap.containsKey(iidmId))) {
esgId = dicoMap.get(iidmId);
} else {
esgId = defaultStrategy.getEsgId(dictionary, nameType, iidmId);
if (nameType == NameType.GENERATOR) {
LOGGER.warn(" dico mapping not found for iidmId: '{}', esgId: '{}' generated using CutName strategy", iidmId, esgId);
}
}
dictionary.add(iidmId, esgId);
LOGGER.debug("iidmId: '{}' ; esgId: '{}'", iidmId, esgId);
}
});
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package eu.itesla_project.iidm.eurostag.export;

import eu.itesla_project.commons.config.ModuleConfig;
import eu.itesla_project.commons.config.PlatformConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Path;

/**
* @author Christian Biasuzzi <christian.biasuzzi@techrain.it>
*/
public class DicoEurostagNamingStrategyFactory implements EurostagNamingStrategyFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(DicoEurostagNamingStrategyFactory.class);

@Override
public EurostagNamingStrategy create() {
ModuleConfig config = PlatformConfig.defaultConfig().getModuleConfigIfExists("eurostag-naming-strategy-dico");
if (config != null) {
Path dicoFile = config.getPathProperty("dicoFile", null);
LOGGER.info("property dicoFile: {}", dicoFile);
return new DicoEurostagNamingStrategy(dicoFile);
} else {
return new CutEurostagNamingStrategy();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@
import java.util.stream.Collectors;

/**
*
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public class EurostagDictionary {

private static final EurostagNamingStrategy NAMING_STRATEGY = new CutEurostagNamingStrategy();
private static final EurostagNamingStrategy NAMING_STRATEGY = new DicoEurostagNamingStrategyFactory().create();

private final BiMap<String, String> iidmId2esgId;

Expand Down Expand Up @@ -100,11 +99,11 @@ public static EurostagDictionary create(Network network, BranchParallelIndexes p
return dictionary;
}

public EurostagDictionary() {
private EurostagDictionary() {
this(new HashMap<>());
}

public EurostagDictionary(Map<String, String> iidmId2esgId) {
private EurostagDictionary(Map<String, String> iidmId2esgId) {
this.iidmId2esgId = HashBiMap.create(iidmId2esgId);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package eu.itesla_project.iidm.eurostag.export;
/**
* Copyright (c) 2016, RTE
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

/**
*
* @author Christian Biasuzzi <christian.biasuzzi@techrain.it>
*/
public interface EurostagNamingStrategyFactory {

EurostagNamingStrategy create();

}

0 comments on commit 6e4d6bc

Please sign in to comment.