Skip to content

Commit

Permalink
state id in helper
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Bouquet <[email protected]>
  • Loading branch information
bqth29 committed Feb 12, 2025
1 parent c097f03 commit 7b47772
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public final Set<State> getStates() {

@Override
public State getPreventiveState() {
return states.get("preventive");
return states.get(StateIdHelper.getStateId(getInstant(InstantKind.PREVENTIVE), timestamp));
}

@Override
Expand Down Expand Up @@ -359,7 +359,7 @@ public Set<State> getStates(Instant instant) {
@Override
public State getState(Contingency contingency, Instant instant) {
Objects.requireNonNull(contingency, "Contingency must not be null when getting a state.");
return states.get(contingency.getId() + " - " + instant.getId());
return states.get(StateIdHelper.getStateId(contingency, instant, timestamp));
}

State addPreventiveState() {
Expand All @@ -383,7 +383,7 @@ State addState(Contingency contingency, Instant instant) {
if (!contingencies.containsKey(contingency.getId())) {
throw new OpenRaoException(format(ADD_ELEMENT_TO_CRAC_ERROR_MESSAGE, contingency.getId()));
}
State state = new PostContingencyState(getContingency(contingency.getId()), instant, null);
State state = new PostContingencyState(getContingency(contingency.getId()), instant, timestamp);
states.put(state.getId(), state);
return state;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public class PostContingencyState implements State {
if (instant.isPreventive()) {
throw new OpenRaoException("Instant cannot be preventive");
}
String baseId = contingency.getId() + " - " + instant.getId();
this.id = timestamp == null ? baseId : baseId + " - " + timestamp.format(DATE_TIME_FORMATTER);
this.id = StateIdHelper.getStateId(contingency, instant, timestamp);
this.contingency = contingency;
this.instant = instant;
this.timestamp = timestamp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ public class PreventiveState implements State {

private final Instant instant;
private final OffsetDateTime timestamp;
private final String id;

PreventiveState(Instant instant, OffsetDateTime timestamp) {
if (!instant.isPreventive()) {
throw new OpenRaoException("Instant must be preventive");
}
this.instant = instant;
this.timestamp = timestamp;
this.id = StateIdHelper.getStateId(instant, timestamp);
}

@Override
public String getId() {
return instant.getId();
return id;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2025, RTE (http://www.rte-france.com)
* 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 com.powsybl.openrao.data.crac.impl;

import com.powsybl.contingency.Contingency;
import com.powsybl.openrao.data.crac.api.Instant;

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

/**
* @author Thomas Bouquet {@literal <thomas.bouquet at rte-france.com>}
*/
public final class StateIdHelper {
private StateIdHelper() {
}

private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmm");

public static String getStateId(Contingency contingency, Instant instant, OffsetDateTime timestamp) {
String idWithContingency = contingency == null ? instant.getId() : contingency.getId() + " - " + instant.getId();
return timestamp == null ? idWithContingency : idWithContingency + " - " + timestamp.format(DATE_TIME_FORMATTER);
}

public static String getStateId(Instant instant, OffsetDateTime timestamp) {
return getStateId(null, instant, timestamp);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2025, RTE (http://www.rte-france.com)
* 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 com.powsybl.openrao.data.crac.impl;

import com.powsybl.contingency.Contingency;
import com.powsybl.openrao.data.crac.api.Instant;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* @author Thomas Bouquet {@literal <thomas.bouquet at rte-france.com>}
*/
class StateIdHelperTest {
private Contingency contingency;
private Instant instant;
private OffsetDateTime timestamp;

@BeforeEach
void setUp() {
contingency = Mockito.mock(Contingency.class);
Mockito.when(contingency.getId()).thenReturn("contingency");
instant = Mockito.mock(Instant.class);
Mockito.when(instant.getId()).thenReturn("instant");
timestamp = OffsetDateTime.of(2025, 2, 12, 9, 18, 0, 0, ZoneOffset.UTC);
}

@Test
void testIdWithContingencyAndTimestamp() {
assertEquals("contingency - instant - 202502120918", StateIdHelper.getStateId(contingency, instant, timestamp));
}

@Test
void testIdWithContingency() {
assertEquals("contingency - instant", StateIdHelper.getStateId(contingency, instant, null));
}

@Test
void testIdWithTimestamp() {
assertEquals("instant - 202502120918", StateIdHelper.getStateId(instant, timestamp));
}

@Test
void testBasicId() {
assertEquals("instant", StateIdHelper.getStateId(instant, null));
}
}

0 comments on commit 7b47772

Please sign in to comment.