Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow timeToImplement pattern with 0Y and 0M #929

Merged
merged 2 commits into from
Mar 25, 2024
Merged
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 @@ -14,7 +14,9 @@
import com.powsybl.triplestore.api.PropertyBag;
import com.powsybl.triplestore.api.PropertyBags;

import java.time.DateTimeException;
import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -64,31 +66,20 @@ public static boolean isValidInterval(OffsetDateTime dateTime, String startTime,
}

public static int convertDurationToSeconds(String duration) {
Map<Character, Integer> durationFactors = new HashMap<>();
durationFactors.put('D', 86400);
durationFactors.put('H', 3600);
durationFactors.put('M', 60);
durationFactors.put('S', 1);

Pattern pattern = Pattern.compile("P(?:\\d+D)?(?:T(?:\\d+H)?(?:\\d+M)?(?:\\d+S)?)?");
Matcher matcher = pattern.matcher(duration);

if (!matcher.matches()) {
throw new OpenRaoException("Error occurred while converting time to implement to seconds, unknown pattern: " + duration);
Pattern removeYearAndMonthPattern = Pattern.compile("P(?:0Y)?(?:0M)?(\\d+D)?(T(?:\\d+H)?(?:\\d+M)?(?:\\d+S)?)?");
Matcher matcher = removeYearAndMonthPattern.matcher(duration);
String durationWithoutYearAndMonth;
if (matcher.matches()) {
durationWithoutYearAndMonth = "P" + (matcher.group(1) == null ? "" : matcher.group(1)) + (matcher.group(2) == null ? "" : matcher.group(2));
} else {
durationWithoutYearAndMonth = duration;
}

int seconds = 0;

for (Map.Entry<Character, Integer> entry : durationFactors.entrySet()) {
Pattern unitPattern = Pattern.compile("(\\d+)" + entry.getKey());
Matcher unitMatcher = unitPattern.matcher(duration);

if (unitMatcher.find()) {
int value = Integer.parseInt(unitMatcher.group(1));
seconds += value * entry.getValue();
}
try {
return (int) java.time.Duration.parse(durationWithoutYearAndMonth).get(ChronoUnit.SECONDS);
} catch (DateTimeException dateTimeException) {
throw new OpenRaoException("Error occurred while converting time to implement to seconds for duration: " + durationWithoutYearAndMonth);
}
return seconds;
}

public static void checkPropertyReference(PropertyBag propertyBag, String remedialActionId, String propertyBagKind, String expectedPropertyReference) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.powsybl.openrao.data.craccreation.creator.csaprofile.craccreator;

import com.powsybl.openrao.commons.OpenRaoException;
import com.powsybl.triplestore.api.PropertyBag;
import com.powsybl.triplestore.api.PropertyBags;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -58,7 +59,7 @@ void testGetLinkedPropertyBags() {
}

@Test
void testDurationConversion() {
void testDurationConversionNoYearNoMonth() {
assertEquals(90063, CsaProfileCracUtils.convertDurationToSeconds("P1DT1H1M3S"));
assertEquals(10920, CsaProfileCracUtils.convertDurationToSeconds("PT3H2M0S"));
assertEquals(315, CsaProfileCracUtils.convertDurationToSeconds("P0DT5M15S"));
Expand All @@ -74,6 +75,71 @@ void testDurationConversion() {
assertEquals(60, CsaProfileCracUtils.convertDurationToSeconds("PT1M"));
assertEquals(0, CsaProfileCracUtils.convertDurationToSeconds("PT0H"));
assertEquals(86400, CsaProfileCracUtils.convertDurationToSeconds("P1D"));
assertThrows(RuntimeException.class, () -> CsaProfileCracUtils.convertDurationToSeconds("P1R"));
}

@Test
void testDurationConversionNoMonth() {
assertEquals(90063, CsaProfileCracUtils.convertDurationToSeconds("P0Y1DT1H1M3S"));
assertEquals(10920, CsaProfileCracUtils.convertDurationToSeconds("P0YT3H2M0S"));
assertEquals(315, CsaProfileCracUtils.convertDurationToSeconds("P0Y0DT5M15S"));
assertEquals(172800, CsaProfileCracUtils.convertDurationToSeconds("P0Y2DT0H0S"));
assertEquals(3600, CsaProfileCracUtils.convertDurationToSeconds("P0Y0DT1H0M"));
assertEquals(62, CsaProfileCracUtils.convertDurationToSeconds("P0YT1M2S"));
assertEquals(2, CsaProfileCracUtils.convertDurationToSeconds("P0YT0H2S"));
assertEquals(2, CsaProfileCracUtils.convertDurationToSeconds("P0Y0DT2S"));
assertEquals(240, CsaProfileCracUtils.convertDurationToSeconds("P0YT0H4M"));
assertEquals(60, CsaProfileCracUtils.convertDurationToSeconds("P0Y0DT1M"));
assertEquals(3600, CsaProfileCracUtils.convertDurationToSeconds("P0Y0DT1H"));
assertEquals(5, CsaProfileCracUtils.convertDurationToSeconds("P0YT5S"));
assertEquals(60, CsaProfileCracUtils.convertDurationToSeconds("P0YT1M"));
assertEquals(0, CsaProfileCracUtils.convertDurationToSeconds("P0YT0H"));
assertEquals(86400, CsaProfileCracUtils.convertDurationToSeconds("P0Y1D"));
}

@Test
void testDurationConversionNoYear() {
assertEquals(90063, CsaProfileCracUtils.convertDurationToSeconds("P0M1DT1H1M3S"));
assertEquals(10920, CsaProfileCracUtils.convertDurationToSeconds("P0MT3H2M0S"));
assertEquals(315, CsaProfileCracUtils.convertDurationToSeconds("P0M0DT5M15S"));
assertEquals(172800, CsaProfileCracUtils.convertDurationToSeconds("P0M2DT0H0S"));
assertEquals(3600, CsaProfileCracUtils.convertDurationToSeconds("P0M0DT1H0M"));
assertEquals(62, CsaProfileCracUtils.convertDurationToSeconds("P0MT1M2S"));
assertEquals(2, CsaProfileCracUtils.convertDurationToSeconds("P0MT0H2S"));
assertEquals(2, CsaProfileCracUtils.convertDurationToSeconds("P0M0DT2S"));
assertEquals(240, CsaProfileCracUtils.convertDurationToSeconds("P0MT0H4M"));
assertEquals(60, CsaProfileCracUtils.convertDurationToSeconds("P0M0DT1M"));
assertEquals(3600, CsaProfileCracUtils.convertDurationToSeconds("P0M0DT1H"));
assertEquals(5, CsaProfileCracUtils.convertDurationToSeconds("P0MT5S"));
assertEquals(60, CsaProfileCracUtils.convertDurationToSeconds("P0MT1M"));
assertEquals(0, CsaProfileCracUtils.convertDurationToSeconds("P0MT0H"));
assertEquals(86400, CsaProfileCracUtils.convertDurationToSeconds("P0M1D"));
}

@Test
void testDurationConversionWithYearAndMonth() {
assertEquals(90063, CsaProfileCracUtils.convertDurationToSeconds("P0Y0M1DT1H1M3S"));
assertEquals(10920, CsaProfileCracUtils.convertDurationToSeconds("P0Y0MT3H2M0S"));
assertEquals(315, CsaProfileCracUtils.convertDurationToSeconds("P0Y0M0DT5M15S"));
assertEquals(172800, CsaProfileCracUtils.convertDurationToSeconds("P0Y0M2DT0H0S"));
assertEquals(3600, CsaProfileCracUtils.convertDurationToSeconds("P0Y0M0DT1H0M"));
assertEquals(62, CsaProfileCracUtils.convertDurationToSeconds("P0Y0MT1M2S"));
assertEquals(2, CsaProfileCracUtils.convertDurationToSeconds("P0Y0MT0H2S"));
assertEquals(2, CsaProfileCracUtils.convertDurationToSeconds("P0Y0M0DT2S"));
assertEquals(240, CsaProfileCracUtils.convertDurationToSeconds("P0Y0MT0H4M"));
assertEquals(60, CsaProfileCracUtils.convertDurationToSeconds("P0Y0M0DT1M"));
assertEquals(3600, CsaProfileCracUtils.convertDurationToSeconds("P0Y0M0DT1H"));
assertEquals(5, CsaProfileCracUtils.convertDurationToSeconds("P0Y0MT5S"));
assertEquals(60, CsaProfileCracUtils.convertDurationToSeconds("P0Y0MT1M"));
assertEquals(0, CsaProfileCracUtils.convertDurationToSeconds("P0Y0MT0H"));
assertEquals(86400, CsaProfileCracUtils.convertDurationToSeconds("P0Y0M1D"));
}

@Test
void testInvalidDurationPattern() {
assertThrows(OpenRaoException.class, () -> CsaProfileCracUtils.convertDurationToSeconds("P1R"));
assertThrows(OpenRaoException.class, () -> CsaProfileCracUtils.convertDurationToSeconds("P2Y"));
assertThrows(OpenRaoException.class, () -> CsaProfileCracUtils.convertDurationToSeconds("P1YT3S"));
assertThrows(OpenRaoException.class, () -> CsaProfileCracUtils.convertDurationToSeconds("P5Y4M"));
assertThrows(OpenRaoException.class, () -> CsaProfileCracUtils.convertDurationToSeconds("P0Y5MT3S"));
}
}
Loading