Skip to content

Commit

Permalink
Factorize searching in exported CGMES file with regular expressions
Browse files Browse the repository at this point in the history
Signed-off-by: Romain Courtier <[email protected]>
  • Loading branch information
rcourtier committed Oct 18, 2024
1 parent 09dc49b commit 235961a
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
Expand Down Expand Up @@ -94,4 +96,21 @@ public static Network readCgmesResources(Properties properties, String dir, Stri
ReadOnlyDataSource ds = new ResourceDataSource("CGMES input file(s)", new ResourceSet(dir, files));
return Network.read(ds, properties);
}

public static String getFirstMatch(String text, Pattern pattern) {
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
return matcher.group(1);
}
return null;
}

public static Set<String> getUniqueMatches(String text, Pattern pattern) {
Set<String> matches = new HashSet<>();
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
matches.add(matcher.group(1));
}
return matches;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import java.io.IOException;
import java.nio.file.Files;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.powsybl.cgmes.conversion.test.ConversionUtil.getUniqueMatches;
import static org.junit.jupiter.api.Assertions.*;

/**
Expand Down Expand Up @@ -65,10 +65,10 @@ void exportSelectedLimitsGroupTest() throws IOException {
String exportSelectedLimitsGroupXml = Files.readString(tmpDir.resolve("ExportSelectedLimitsGroup_EQ.xml"));

// There is 1 set on side 1 which is selected, and there are 2 sets on side 2 but none of them is selected
assertEquals(1, getOccurrences(exportSelectedLimitsGroupXml, OPERATIONAL_LIMIT_SET).size());
assertEquals(3, getOccurrences(exportSelectedLimitsGroupXml, OPERATIONAL_LIMIT_TYPE).size());
assertEquals(0, getOccurrences(exportSelectedLimitsGroupXml, ACTIVE_POWER_LIMIT).size());
assertEquals(3, getOccurrences(exportSelectedLimitsGroupXml, CURRENT_LIMIT).size());
assertEquals(1, getUniqueMatches(exportSelectedLimitsGroupXml, OPERATIONAL_LIMIT_SET).size());
assertEquals(3, getUniqueMatches(exportSelectedLimitsGroupXml, OPERATIONAL_LIMIT_TYPE).size());
assertEquals(0, getUniqueMatches(exportSelectedLimitsGroupXml, ACTIVE_POWER_LIMIT).size());
assertEquals(3, getUniqueMatches(exportSelectedLimitsGroupXml, CURRENT_LIMIT).size());

// Manually select one of the limits group on side 2 and export again
Line line = network.getLine("Line");
Expand All @@ -77,10 +77,10 @@ void exportSelectedLimitsGroupTest() throws IOException {
exportSelectedLimitsGroupXml = Files.readString(tmpDir.resolve("ExportSelectedLimitsGroup_EQ.xml"));

// That makes 1 set selected on each side = 2 in total
assertEquals(2, getOccurrences(exportSelectedLimitsGroupXml, OPERATIONAL_LIMIT_SET).size());
assertEquals(3, getOccurrences(exportSelectedLimitsGroupXml, OPERATIONAL_LIMIT_TYPE).size());
assertEquals(0, getOccurrences(exportSelectedLimitsGroupXml, ACTIVE_POWER_LIMIT).size());
assertEquals(6, getOccurrences(exportSelectedLimitsGroupXml, CURRENT_LIMIT).size());
assertEquals(2, getUniqueMatches(exportSelectedLimitsGroupXml, OPERATIONAL_LIMIT_SET).size());
assertEquals(3, getUniqueMatches(exportSelectedLimitsGroupXml, OPERATIONAL_LIMIT_TYPE).size());
assertEquals(0, getUniqueMatches(exportSelectedLimitsGroupXml, ACTIVE_POWER_LIMIT).size());
assertEquals(6, getUniqueMatches(exportSelectedLimitsGroupXml, CURRENT_LIMIT).size());
}

@Test
Expand All @@ -95,19 +95,10 @@ void exportAllLimitsGroupTest() throws IOException {
String exportAllLimitsGroupXml = Files.readString(tmpDir.resolve("ExportAllLimitsGroup_EQ.xml"));

// All 3 OperationalLimitsGroup are exported, even though only 2 are selected
assertEquals(3, getOccurrences(exportAllLimitsGroupXml, OPERATIONAL_LIMIT_SET).size());
assertEquals(3, getOccurrences(exportAllLimitsGroupXml, OPERATIONAL_LIMIT_TYPE).size());
assertEquals(3, getOccurrences(exportAllLimitsGroupXml, ACTIVE_POWER_LIMIT).size());
assertEquals(9, getOccurrences(exportAllLimitsGroupXml, CURRENT_LIMIT).size());
}

private Set<String> getOccurrences(String xml, Pattern pattern) {
Set<String> matches = new HashSet<>();
Matcher matcher = pattern.matcher(xml);
while (matcher.find()) {
matches.add(matcher.group(1));
}
return matches;
assertEquals(3, getUniqueMatches(exportAllLimitsGroupXml, OPERATIONAL_LIMIT_SET).size());
assertEquals(3, getUniqueMatches(exportAllLimitsGroupXml, OPERATIONAL_LIMIT_TYPE).size());
assertEquals(3, getUniqueMatches(exportAllLimitsGroupXml, ACTIVE_POWER_LIMIT).size());
assertEquals(9, getUniqueMatches(exportAllLimitsGroupXml, CURRENT_LIMIT).size());
}

}
Loading

0 comments on commit 235961a

Please sign in to comment.