Skip to content

Commit

Permalink
refactor to have optional values
Browse files Browse the repository at this point in the history
Signed-off-by: vmouradian <[email protected]>
  • Loading branch information
vmouradian committed Jan 6, 2025
1 parent 5bc558a commit 0463613
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,51 @@
import com.powsybl.contingency.Contingency;
import com.powsybl.loadflow.LoadFlowParameters;

import java.util.Optional;

/**
* @author Valentin Mouradian {@literal <valentin.mouradian at artelys.com>}
*/
public class ContingencyLoadFlowParameters extends AbstractExtension<Contingency> {

private boolean distributedSlack;
private Boolean distributedSlack = null;

private boolean areaInterchangeControl;
private Boolean areaInterchangeControl = null;

private LoadFlowParameters.BalanceType balanceType;
private LoadFlowParameters.BalanceType balanceType = null;

public ContingencyLoadFlowParameters(boolean distributedSlack, boolean areaInterchangeControl, LoadFlowParameters.BalanceType balanceType) {
this.distributedSlack = distributedSlack;
this.areaInterchangeControl = areaInterchangeControl;
this.balanceType = balanceType;
public ContingencyLoadFlowParameters() {
}

@Override
public String getName() {
return "contingency-load-flow-parameters";
}

public boolean isDistributedSlack() {
return distributedSlack;
public Optional<Boolean> isDistributedSlack() {
return Optional.ofNullable(distributedSlack);
}

public boolean isAreaInterchangeControl() {
return areaInterchangeControl;
public Optional<Boolean> isAreaInterchangeControl() {
return Optional.ofNullable(areaInterchangeControl);
}

public LoadFlowParameters.BalanceType getBalanceType() {
return balanceType;
public Optional<LoadFlowParameters.BalanceType> getBalanceType() {
return Optional.ofNullable(balanceType);
}

public ContingencyLoadFlowParameters setDistributedSlack(Boolean distributedSlack) {
this.distributedSlack = distributedSlack;
return this;
}

public ContingencyLoadFlowParameters setAreaInterchangeControl(Boolean areaInterchangeControl) {
this.areaInterchangeControl = areaInterchangeControl;
return this;
}

public ContingencyLoadFlowParameters setBalanceType(LoadFlowParameters.BalanceType balanceType) {
this.balanceType = balanceType;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
*/
package com.powsybl.openloadflow.sa.extensions;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.google.auto.service.AutoService;
import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.extensions.ExtensionJsonSerializer;
import com.powsybl.commons.json.JsonUtil;
import com.powsybl.contingency.Contingency;
import com.powsybl.loadflow.LoadFlowParameters;

import java.io.IOException;
import java.util.Optional;

/**
* @author Valentin Mouradian {@literal <valentin.mouradian at artelys.com>}
Expand All @@ -33,38 +34,53 @@ public String getExtensionName() {

@Override
public String getCategoryName() {
return "contingency";
return "security-analysis";
}

@Override
public Class<? super ContingencyLoadFlowParameters> getExtensionClass() {
return ContingencyLoadFlowParameters.class;
}

/**
* Specifies serialization for our extension: ignore name and extendable
*/
private interface SerializationSpec {

@JsonIgnore
String getName();

@JsonIgnore
Contingency getExtendable();
}

private static ObjectMapper createMapper() {
return JsonUtil.createObjectMapper()
.addMixIn(ContingencyLoadFlowParameters.class, SerializationSpec.class);
}

@Override
public void serialize(ContingencyLoadFlowParameters extension, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
createMapper().writeValue(jsonGenerator, extension);
Optional<Boolean> distributedSlack = extension.isDistributedSlack();
Optional<Boolean> areaInterchangeControl = extension.isAreaInterchangeControl();
Optional<LoadFlowParameters.BalanceType> balanceType = extension.getBalanceType();

jsonGenerator.writeStartObject();
if (distributedSlack.isPresent()) {
jsonGenerator.writeBooleanField("distributedSlack", distributedSlack.get());
}
if (areaInterchangeControl.isPresent()) {
jsonGenerator.writeBooleanField("areaInterchangeControl", areaInterchangeControl.get());
}
if (balanceType.isPresent()) {
jsonGenerator.writeStringField("balanceType", balanceType.get().name());
}
jsonGenerator.writeEndObject();
}

@Override
public ContingencyLoadFlowParameters deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
return createMapper().readValue(jsonParser, ContingencyLoadFlowParameters.class);
ContingencyLoadFlowParameters contingencyLoadFlowParameters = new ContingencyLoadFlowParameters();
while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
if (jsonParser.currentName().equals("distributedSlack")) {
jsonParser.nextToken();
boolean distributedSlack = jsonParser.readValueAs(Boolean.class);
contingencyLoadFlowParameters.setDistributedSlack(distributedSlack);
} else if (jsonParser.currentName().equals("areaInterchangeControl")) {
jsonParser.nextToken();
boolean areaInterchangeControl = jsonParser.readValueAs(Boolean.class);
contingencyLoadFlowParameters.setAreaInterchangeControl(areaInterchangeControl);
} else if (jsonParser.currentName().equals("balanceType")) {
jsonParser.nextToken();
LoadFlowParameters.BalanceType balanceType = LoadFlowParameters.BalanceType.valueOf(jsonParser.readValueAs(String.class));
contingencyLoadFlowParameters.setBalanceType(balanceType);
} else {
throw new PowsyblException("Unexpected field: " + jsonParser.currentName());
}
}
return contingencyLoadFlowParameters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,29 @@ class OpenSecurityAnalysisExtensionsTest extends AbstractSerDeTest {
@Test
void testContingencyLoadFlowParametersExtension() {
Contingency contingency = new Contingency("L2", new BranchContingency("L2"));
contingency.addExtension(ContingencyLoadFlowParameters.class, new ContingencyLoadFlowParameters(false, true, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD));
ContingencyLoadFlowParameters contingencyLoadFlowParameters = new ContingencyLoadFlowParameters()
.setAreaInterchangeControl(true)
.setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD);
contingency.addExtension(ContingencyLoadFlowParameters.class, contingencyLoadFlowParameters);

ContingencyLoadFlowParameters contingencyLoadFlowParameters = contingency.getExtension(ContingencyLoadFlowParameters.class);
ContingencyLoadFlowParameters extension = contingency.getExtension(ContingencyLoadFlowParameters.class);
assertEquals(extension, contingency.getExtensionByName("contingency-load-flow-parameters"));

assertEquals(contingencyLoadFlowParameters, contingency.getExtensionByName("contingency-load-flow-parameters"));
assertFalse(contingencyLoadFlowParameters.isDistributedSlack());
assertTrue(contingencyLoadFlowParameters.isAreaInterchangeControl());
assertEquals(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD, contingencyLoadFlowParameters.getBalanceType());
assertFalse(extension.isDistributedSlack().isPresent());
assertTrue(extension.isAreaInterchangeControl().isPresent());
assertTrue(extension.getBalanceType().isPresent());

assertTrue(extension.isAreaInterchangeControl().get());
assertEquals(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD, extension.getBalanceType().get());
}

@Test
void testContingencyLoadFlowParametersExtensionJson() throws IOException {
Contingency contingency = new Contingency("L2", new BranchContingency("L2"));
contingency.addExtension(ContingencyLoadFlowParameters.class, new ContingencyLoadFlowParameters(false, true, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD));
ContingencyLoadFlowParameters contingencyLoadFlowParameters = new ContingencyLoadFlowParameters().setDistributedSlack(false)
.setAreaInterchangeControl(true)
.setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD);
contingency.addExtension(ContingencyLoadFlowParameters.class, contingencyLoadFlowParameters);
assertEquals(ContingencyLoadFlowParameters.class, new ContingencyLoadFlowParametersJsonSerializer().getExtensionClass());
roundTripTest(contingency, OpenSecurityAnalysisExtensionsTest::writeContingency, OpenSecurityAnalysisExtensionsTest::readContingency, "/contingencies.json");
}
Expand Down

0 comments on commit 0463613

Please sign in to comment.