Skip to content

Commit

Permalink
Fix identifier contingency list deserialization (#2807)
Browse files Browse the repository at this point in the history
Signed-off-by: Etienne LESOT <[email protected]>
  • Loading branch information
EtienneLt authored Dec 5, 2023
1 parent 1884fe9 commit c5f1293
Show file tree
Hide file tree
Showing 33 changed files with 238 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
*/
public class DefaultContingencyList implements ContingencyList {

public static final String TYPE = "default";
private final String name;

private final List<Contingency> contingencies;

public DefaultContingencyList(Contingency... contingencies) {
Expand All @@ -43,7 +43,7 @@ public String getName() {

@Override
public String getType() {
return "default";
return TYPE;
}

public List<Contingency> getContingencies() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
public class HvdcLineCriterionContingencyList extends AbstractLineCriterionContingencyList {

public static final String TYPE = "hvdcCriterion";

public HvdcLineCriterionContingencyList(String name, TwoCountriesCriterion twoCountriesCriterion,
TwoNominalVoltageCriterion twoNominalVoltageCriterion,
List<PropertyCriterion> propertyCriteria, RegexCriterion regexCriterion) {
Expand All @@ -27,6 +29,6 @@ public HvdcLineCriterionContingencyList(String name, TwoCountriesCriterion twoCo

@Override
public String getType() {
return "hvdcCriterion";
return TYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
public class IdentifierContingencyList implements ContingencyList {

private static final String VERSION = "1.2";
public static final String TYPE = "identifier";
private final String name;
private final List<NetworkElementIdentifier> networkElementIdentifiers;

Expand All @@ -41,7 +42,7 @@ public String getName() {

@Override
public String getType() {
return "identifier";
return TYPE;
}

public List<NetworkElementIdentifier> getIdentifiants() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
public class InjectionCriterionContingencyList extends AbstractEquipmentCriterionContingencyList {

public static final String TYPE = "injectionCriterion";
private final SingleCountryCriterion singleCountryCriterion;
private final SingleNominalVoltageCriterion singleNominalVoltageCriterion;

Expand All @@ -38,7 +39,7 @@ public InjectionCriterionContingencyList(String name, IdentifiableType identifia

@Override
public String getType() {
return "injectionCriterion";
return TYPE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
public class LineCriterionContingencyList extends AbstractLineCriterionContingencyList {

public static final String TYPE = "lineCriterion";

public LineCriterionContingencyList(String name,
TwoCountriesCriterion twoCountriesCriterion,
TwoNominalVoltageCriterion twoNominalVoltageCriterion,
Expand All @@ -25,6 +27,6 @@ public LineCriterionContingencyList(String name,

@Override
public String getType() {
return "lineCriterion";
return TYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* @author Etienne Lesot {@literal <[email protected]>}
*/
public class ListOfContingencyLists implements ContingencyList {

public static final String TYPE = "list";
private final String name;
private final List<ContingencyList> contingencyLists;

Expand All @@ -33,7 +35,7 @@ public String getName() {

@Override
public String getType() {
return "list";
return TYPE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
public class ThreeWindingsTransformerCriterionContingencyList extends AbstractEquipmentCriterionContingencyList {

public static final String TYPE = "threeWindingsTransformerCriterion";
private final SingleCountryCriterion singleCountryCriterion;
private final ThreeNominalVoltageCriterion threeNominalVoltageCriterion;

Expand All @@ -33,7 +34,7 @@ public ThreeWindingsTransformerCriterionContingencyList(String name,

@Override
public String getType() {
return "threeWindingsTransformerCriterion";
return TYPE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* @author Florian Dupuy {@literal <florian.dupuy at rte-france.com>}
*/
public class TieLineCriterionContingencyList extends AbstractEquipmentCriterionContingencyList {

public static final String TYPE = "tieLineCriterion";
private final TwoCountriesCriterion twoCountriesCriterion;
private final SingleNominalVoltageCriterion singleNominalVoltageCriterion;

Expand All @@ -33,7 +35,7 @@ public TieLineCriterionContingencyList(String name,

@Override
public String getType() {
return "tieLineCriterion";
return TYPE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* @author Etienne Lesot {@literal <[email protected]>}
*/
public class TwoWindingsTransformerCriterionContingencyList extends AbstractEquipmentCriterionContingencyList {

public static final String TYPE = "twoWindingsTransformerCriterion";
private final SingleCountryCriterion singleCountryCriterion;
private final TwoNominalVoltageCriterion twoNominalVoltageCriterion;

Expand All @@ -32,7 +34,7 @@ public TwoWindingsTransformerCriterionContingencyList(String name,

@Override
public String getType() {
return "twoWindingsTransformerCriterion";
return TYPE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,45 @@ protected static class ParsingContext {
}

protected boolean deserializeCommonAttributes(JsonParser parser, DeserializationContext ctx,
ParsingContext parsingCtx, String name) throws IOException {
ParsingContext parsingCtx, String name, String expectedType) throws IOException {
switch (name) {
case "name":
case "name" -> {
parsingCtx.name = parser.nextTextValue();
return true;
case "countryCriterion":
}
case "countryCriterion" -> {
parser.nextToken();
parsingCtx.countryCriterion = JsonUtil.readValue(ctx, parser, Criterion.class);
return true;
case "nominalVoltageCriterion":
}
case "nominalVoltageCriterion" -> {
parser.nextToken();
parsingCtx.nominalVoltageCriterion = JsonUtil.readValue(ctx, parser, Criterion.class);
return true;
case "propertyCriteria":
}
case "propertyCriteria" -> {
parser.nextToken();
parsingCtx.propertyCriteria = JsonUtil.readList(ctx, parser, Criterion.class);
return true;
case "regexCriterion":
}
case "regexCriterion" -> {
parser.nextToken();
parsingCtx.regexCriterion = JsonUtil.readValue(ctx, parser, Criterion.class);
return true;
case "version":
case "type":
}
case "version" -> {
parser.nextToken();
return true;
default:
}
case "type" -> {
if (!parser.nextTextValue().equals(expectedType)) {
throw new IllegalStateException("type should be: " + expectedType);
}
return true;
}
default -> {
return false;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,16 @@ public Contingency deserialize(JsonParser parser, DeserializationContext deseria

while (parser.nextToken() != JsonToken.END_OBJECT) {
switch (parser.getCurrentName()) {
case "id":
id = parser.nextTextValue();
break;

case "elements":
case "id" -> id = parser.nextTextValue();
case "elements" -> {
parser.nextToken();
elements = JsonUtil.readList(deserializationContext, parser, ContingencyElement.class);
break;

case "extensions":
}
case "extensions" -> {
parser.nextToken();
extensions = JsonUtil.readExtensions(parser, deserializationContext, SUPPLIER.get());
break;

default:
throw new IllegalStateException("Unexpected field: " + parser.getCurrentName());
}
default -> throw new IllegalStateException("Unexpected field: " + parser.getCurrentName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,68 +32,33 @@ public ContingencyElement deserialize(JsonParser parser, DeserializationContext

while (parser.nextToken() != JsonToken.END_OBJECT) {
switch (parser.getCurrentName()) {
case "id":
id = parser.nextTextValue();
break;

case "voltageLevelId":
voltageLevelId = parser.nextTextValue();
break;

case "type":
case "id" -> id = parser.nextTextValue();
case "voltageLevelId" -> voltageLevelId = parser.nextTextValue();
case "type" -> {
parser.nextToken();
type = JsonUtil.readValue(ctx, parser, ContingencyElementType.class);
break;

default:
throw new IllegalStateException("Unexpected field: " + parser.getCurrentName());
}
default -> throw new IllegalStateException("Unexpected field: " + parser.getCurrentName());
}
}

if (type != null) {
switch (type) {
case BRANCH:
return new BranchContingency(id, voltageLevelId);

case GENERATOR:
return new GeneratorContingency(id);

case STATIC_VAR_COMPENSATOR:
return new StaticVarCompensatorContingency(id);

case SHUNT_COMPENSATOR:
return new ShuntCompensatorContingency(id);

case HVDC_LINE:
return new HvdcLineContingency(id, voltageLevelId);

case BUSBAR_SECTION:
return new BusbarSectionContingency(id);

case DANGLING_LINE:
return new DanglingLineContingency(id);

case LINE:
return new LineContingency(id, voltageLevelId);

case TWO_WINDINGS_TRANSFORMER:
return new TwoWindingsTransformerContingency(id, voltageLevelId);

case THREE_WINDINGS_TRANSFORMER:
return new ThreeWindingsTransformerContingency(id);

case LOAD:
return new LoadContingency(id);

case BUS:
return new BusContingency(id);

case TIE_LINE:
return new TieLineContingency(id, voltageLevelId);

default:
throw new IllegalStateException("Unexpected ContingencyElementType value: " + type);
}
return switch (type) {
case BRANCH -> new BranchContingency(id, voltageLevelId);
case GENERATOR -> new GeneratorContingency(id);
case STATIC_VAR_COMPENSATOR -> new StaticVarCompensatorContingency(id);
case SHUNT_COMPENSATOR -> new ShuntCompensatorContingency(id);
case HVDC_LINE -> new HvdcLineContingency(id, voltageLevelId);
case BUSBAR_SECTION -> new BusbarSectionContingency(id);
case DANGLING_LINE -> new DanglingLineContingency(id);
case LINE -> new LineContingency(id, voltageLevelId);
case TWO_WINDINGS_TRANSFORMER -> new TwoWindingsTransformerContingency(id, voltageLevelId);
case THREE_WINDINGS_TRANSFORMER -> new ThreeWindingsTransformerContingency(id);
case LOAD -> new LoadContingency(id);
case BUS -> new BusContingency(id);
case TIE_LINE -> new TieLineContingency(id, voltageLevelId);
default -> throw new IllegalStateException("Unexpected ContingencyElementType value: " + type);
};
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,39 @@ public ContingencyList deserialize(JsonParser parser, DeserializationContext des
while (parser.getCurrentName() != null) {
if ("type".equals(parser.getCurrentName())) {
switch (parser.nextTextValue()) {
case "default":
case "default" -> {
DefaultContingencyListDeserializer defaultContingencyListDeserializer = new DefaultContingencyListDeserializer();
return defaultContingencyListDeserializer.deserialize(parser, deserializationContext);
case "injectionCriterion":
}
case "injectionCriterion" -> {
InjectionCriterionContingencyListDeserializer injectionCriterionContingencyListDeserializer = new InjectionCriterionContingencyListDeserializer();
return injectionCriterionContingencyListDeserializer.deserialize(parser, deserializationContext);
case "hvdcCriterion":
}
case "hvdcCriterion" -> {
HvdcLineCriterionContingencyListDeserializer hvdcLineCriterionContingencyListDeserializer = new HvdcLineCriterionContingencyListDeserializer();
return hvdcLineCriterionContingencyListDeserializer.deserialize(parser, deserializationContext);
case "lineCriterion":
}
case "lineCriterion" -> {
LineCriterionContingencyListDeserializer lineCriterionContingencyListDeserializer = new LineCriterionContingencyListDeserializer();
return lineCriterionContingencyListDeserializer.deserialize(parser, deserializationContext);
case "twoWindingsTransformerCriterion":
}
case "twoWindingsTransformerCriterion" -> {
TwoWindingsTransformerCriterionContingencyListDeserializer twoWindingsTransformerCriterionContingencyListDeserializer = new TwoWindingsTransformerCriterionContingencyListDeserializer();
return twoWindingsTransformerCriterionContingencyListDeserializer.deserialize(parser, deserializationContext);
case "threeWindingsTransformerCriterion":
}
case "threeWindingsTransformerCriterion" -> {
ThreeWindingsTransformerCriterionContingencyListDeserializer threeWindingsTransformerCriterionContingencyListDeserializer = new ThreeWindingsTransformerCriterionContingencyListDeserializer();
return threeWindingsTransformerCriterionContingencyListDeserializer.deserialize(parser, deserializationContext);
case "list":
}
case "list" -> {
ListOfContingencyListsDeserializer listOfContingencyListsDeserializer = new ListOfContingencyListsDeserializer();
return listOfContingencyListsDeserializer.deserialize(parser, deserializationContext);
default:
throw new IllegalStateException("Unexpected field: " + parser.getCurrentName());
}
case "identifier" -> {
IdentifierContingencyListDeserializer identifierContingencyListDeserializer = new IdentifierContingencyListDeserializer();
return identifierContingencyListDeserializer.deserialize(parser, deserializationContext);
}
default -> throw new IllegalStateException("Unexpected field: " + parser.getCurrentName());
}
}
parser.nextToken();
Expand Down
Loading

0 comments on commit c5f1293

Please sign in to comment.