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

Mutualize RaUsageLimits serializers/deserializers and support max-elementary-actions-per-tso #1150

Merged
merged 3 commits into from
Oct 2, 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 @@ -9,23 +9,12 @@

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.powsybl.openrao.commons.OpenRaoException;
import com.powsybl.openrao.data.cracapi.Crac;
import com.powsybl.openrao.data.cracapi.RaUsageLimits;
import com.powsybl.openrao.data.cracapi.parameters.JsonCracCreationParametersConstants;
import org.apache.commons.lang3.tuple.Pair;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.powsybl.openrao.data.cracio.json.JsonSerializationConstants.INSTANT;
import static com.powsybl.openrao.data.cracio.json.JsonSerializationConstants.MAX_ELEMENTARY_ACTIONS_PER_TSO;
import static com.powsybl.openrao.data.cracio.json.JsonSerializationConstants.MAX_RA;
import static com.powsybl.openrao.data.cracio.json.JsonSerializationConstants.MAX_TSO;
import static com.powsybl.openrao.data.cracio.json.JsonSerializationConstants.MAX_TOPO_PER_TSO;
import static com.powsybl.openrao.data.cracio.json.JsonSerializationConstants.MAX_PST_PER_TSO;
import static com.powsybl.openrao.data.cracio.json.JsonSerializationConstants.MAX_RA_PER_TSO;
import static com.powsybl.openrao.data.cracio.json.JsonSerializationConstants.RA_USAGE_LIMITS_PER_INSTANT;

/**
* @author Martin Belthle {@literal <martin.belthle at rte-france.com>}
Expand All @@ -37,7 +26,7 @@ private RaUsageLimitsDeserializer() {

public static void deserialize(JsonParser jsonParser, Crac crac) throws IOException {
while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
Pair<String, RaUsageLimits> raUsageLimitsPair = deserializeRaUsageLimits(jsonParser);
Pair<String, RaUsageLimits> raUsageLimitsPair = JsonCracCreationParametersConstants.deserializeRaUsageLimits(jsonParser);
RaUsageLimits raUsageLimits = raUsageLimitsPair.getRight();
crac.newRaUsageLimits(raUsageLimitsPair.getLeft())
.withMaxRa(raUsageLimits.getMaxRa())
Expand All @@ -49,58 +38,4 @@ public static void deserialize(JsonParser jsonParser, Crac crac) throws IOExcept
.add();
}
}

private static Map<String, Integer> readStringToPositiveIntMap(JsonParser jsonParser) throws IOException {
HashMap<String, Integer> map = jsonParser.readValueAs(HashMap.class);
// Check types
map.forEach((Object o, Object o2) -> {
if (!(o instanceof String) || !(o2 instanceof Integer)) {
throw new OpenRaoException("Unexpected key or value type in a Map<String, Integer> parameter!");
}
if ((int) o2 < 0) {
throw new OpenRaoException("Unexpected negative integer!");
}
});
return map;
}

public static Pair<String, RaUsageLimits> deserializeRaUsageLimits(JsonParser jsonParser) throws IOException {
RaUsageLimits raUsageLimits = new RaUsageLimits();
String instant = null;
while (!jsonParser.nextToken().isStructEnd()) {
switch (jsonParser.getCurrentName()) {
case INSTANT -> {
jsonParser.nextToken();
instant = jsonParser.getValueAsString();
}
case MAX_RA -> {
jsonParser.nextToken();
raUsageLimits.setMaxRa(jsonParser.getIntValue());
}
case MAX_TSO -> {
jsonParser.nextToken();
raUsageLimits.setMaxTso(jsonParser.getIntValue());
}
case MAX_TOPO_PER_TSO -> {
jsonParser.nextToken();
raUsageLimits.setMaxTopoPerTso(readStringToPositiveIntMap(jsonParser));
}
case MAX_PST_PER_TSO -> {
jsonParser.nextToken();
raUsageLimits.setMaxPstPerTso(readStringToPositiveIntMap(jsonParser));
}
case MAX_RA_PER_TSO -> {
jsonParser.nextToken();
raUsageLimits.setMaxRaPerTso(readStringToPositiveIntMap(jsonParser));
}
case MAX_ELEMENTARY_ACTIONS_PER_TSO -> {
jsonParser.nextToken();
raUsageLimits.setMaxElementaryActionsPerTso(readStringToPositiveIntMap(jsonParser));
}
default ->
throw new OpenRaoException(String.format("Cannot deserialize ra-usage-limits-per-instant parameters: unexpected field in %s (%s)", RA_USAGE_LIMITS_PER_INSTANT, jsonParser.getCurrentName()));
}
}
return Pair.of(instant, raUsageLimits);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.powsybl.openrao.data.cracapi.cnec.FlowCnec;
import com.powsybl.openrao.data.cracapi.cnec.VoltageCnec;
import com.powsybl.openrao.data.cracapi.networkaction.NetworkAction;
import com.powsybl.openrao.data.cracapi.parameters.JsonCracCreationParametersConstants;
import com.powsybl.openrao.data.cracapi.rangeaction.CounterTradeRangeAction;
import com.powsybl.openrao.data.cracapi.rangeaction.HvdcRangeAction;
import com.powsybl.openrao.data.cracapi.rangeaction.InjectionRangeAction;
Expand All @@ -29,7 +30,6 @@
import java.util.stream.Collectors;

import static com.powsybl.openrao.data.cracio.json.JsonSerializationConstants.*;
import static com.powsybl.openrao.data.cracio.json.JsonSerializationConstants.INSTANT;

/**
* @author Alexandre Montigny {@literal <alexandre.montigny at rte-france.com>}
Expand Down Expand Up @@ -67,16 +67,7 @@ public void serialize(Crac crac, JsonGenerator gen, SerializerProvider serialize
private void serializeRaUsageLimits(Crac crac, JsonGenerator gen) throws IOException {
gen.writeArrayFieldStart(RA_USAGE_LIMITS_PER_INSTANT);
for (Map.Entry<Instant, RaUsageLimits> entry : crac.getRaUsageLimitsPerInstant().entrySet()) {
RaUsageLimits raUsageLimits = entry.getValue();
gen.writeStartObject();
gen.writeStringField(INSTANT, entry.getKey().getId());
gen.writeNumberField(MAX_RA, raUsageLimits.getMaxRa());
gen.writeNumberField(MAX_TSO, raUsageLimits.getMaxTso());
gen.writeObjectField(MAX_TOPO_PER_TSO, new TreeMap<>(raUsageLimits.getMaxTopoPerTso()));
gen.writeObjectField(MAX_PST_PER_TSO, new TreeMap<>(raUsageLimits.getMaxPstPerTso()));
gen.writeObjectField(MAX_RA_PER_TSO, new TreeMap<>(raUsageLimits.getMaxRaPerTso()));
gen.writeObjectField(MAX_ELEMENTARY_ACTIONS_PER_TSO, new TreeMap<>(raUsageLimits.getMaxElementaryActionsPerTso()));
gen.writeEndObject();
JsonCracCreationParametersConstants.serializeRaUsageLimitForOneInstant(gen, Map.entry(entry.getKey().getId(), entry.getValue()));
}
gen.writeEndArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public final class JsonCracCreationParametersConstants {
public static final String MAX_TOPO_PER_TSO = "max-topo-per-tso";
public static final String MAX_PST_PER_TSO = "max-pst-per-tso";
public static final String MAX_RA_PER_TSO = "max-ra-per-tso";
public static final String MAX_ELEMENTARY_ACTIONS_PER_TSO = "max-elementary-actions-per-tso";

private JsonCracCreationParametersConstants() {
// should not be instantiated
Expand Down Expand Up @@ -67,19 +68,24 @@ static void serializeRaUsageLimits(CracCreationParameters parameters, JsonGenera
jsonGenerator.writeFieldName(RA_USAGE_LIMITS_PER_INSTANT);
jsonGenerator.writeStartArray();
for (Map.Entry<String, RaUsageLimits> entry : parameters.getRaUsageLimitsPerInstant().entrySet()) {
RaUsageLimits raUsageLimits = entry.getValue();
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField(INSTANT, entry.getKey());
jsonGenerator.writeNumberField(MAX_RA, raUsageLimits.getMaxRa());
jsonGenerator.writeNumberField(MAX_TSO, raUsageLimits.getMaxTso());
jsonGenerator.writeObjectField(MAX_TOPO_PER_TSO, new TreeMap<>(raUsageLimits.getMaxTopoPerTso()));
jsonGenerator.writeObjectField(MAX_PST_PER_TSO, new TreeMap<>(raUsageLimits.getMaxPstPerTso()));
jsonGenerator.writeObjectField(MAX_RA_PER_TSO, new TreeMap<>(raUsageLimits.getMaxRaPerTso()));
jsonGenerator.writeEndObject();
serializeRaUsageLimitForOneInstant(jsonGenerator, entry);
}
jsonGenerator.writeEndArray();
}

public static void serializeRaUsageLimitForOneInstant(JsonGenerator jsonGenerator, Map.Entry<String, RaUsageLimits> entry) throws IOException {
RaUsageLimits raUsageLimits = entry.getValue();
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField(INSTANT, entry.getKey());
jsonGenerator.writeNumberField(MAX_RA, raUsageLimits.getMaxRa());
jsonGenerator.writeNumberField(MAX_TSO, raUsageLimits.getMaxTso());
jsonGenerator.writeObjectField(MAX_TOPO_PER_TSO, new TreeMap<>(raUsageLimits.getMaxTopoPerTso()));
jsonGenerator.writeObjectField(MAX_PST_PER_TSO, new TreeMap<>(raUsageLimits.getMaxPstPerTso()));
jsonGenerator.writeObjectField(MAX_RA_PER_TSO, new TreeMap<>(raUsageLimits.getMaxRaPerTso()));
jsonGenerator.writeObjectField(MAX_ELEMENTARY_ACTIONS_PER_TSO, new TreeMap<>(raUsageLimits.getMaxElementaryActionsPerTso()));
jsonGenerator.writeEndObject();
}

static void deserializeRaUsageLimitsAndUpdateParameters(JsonParser jsonParser, CracCreationParameters parameters) throws IOException {
while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
Pair<String, RaUsageLimits> pairOfInstantAndItsRaUsageLimits = deserializeRaUsageLimits(jsonParser);
Expand All @@ -101,7 +107,7 @@ private static Map<String, Integer> readStringToPositiveIntMap(JsonParser jsonPa
return map;
}

private static Pair<String, RaUsageLimits> deserializeRaUsageLimits(JsonParser jsonParser) throws IOException {
public static Pair<String, RaUsageLimits> deserializeRaUsageLimits(JsonParser jsonParser) throws IOException {
RaUsageLimits raUsageLimits = new RaUsageLimits();
String instant = null;
while (!jsonParser.nextToken().isStructEnd()) {
Expand Down Expand Up @@ -130,6 +136,10 @@ private static Pair<String, RaUsageLimits> deserializeRaUsageLimits(JsonParser j
jsonParser.nextToken();
raUsageLimits.setMaxRaPerTso(readStringToPositiveIntMap(jsonParser));
break;
case MAX_ELEMENTARY_ACTIONS_PER_TSO:
jsonParser.nextToken();
raUsageLimits.setMaxElementaryActionsPerTso(readStringToPositiveIntMap(jsonParser));
break;
default:
throw new OpenRaoException(String.format("Cannot deserialize ra-usage-limits-per-instant parameters: unexpected field in %s (%s)", RA_USAGE_LIMITS_PER_INSTANT, jsonParser.getCurrentName()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ void importFromFileWithRaLimits() {
expectedLimitsPreventive.setMaxRaPerTso(Map.of("FR", 4));
expectedLimitsPreventive.setMaxTopoPerTso(Map.of("FR", 2));
expectedLimitsPreventive.setMaxPstPerTso(Map.of("FR", 3));
expectedLimitsPreventive.setMaxElementaryActionsPerTso(Map.of("FR", 10));
RaUsageLimits expectedLimitsCurative = new RaUsageLimits();
expectedLimitsCurative.setMaxRa(7);
expectedLimitsCurative.setMaxTso(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
},
"max-ra-per-tso" : {
"FR" : 7
},
"max-elementary-actions-per-tso" : {
"FR" : 10
}
} ]
}
Loading