Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
Signed-off-by: vmouradian <valentin.mouradian@artelys.com>
  • Loading branch information
vmouradian committed Dec 17, 2024
1 parent 0c3d84f commit d47e484
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.openloadflow.sa;
package com.powsybl.openloadflow.sa.extensions;

import com.powsybl.commons.extensions.AbstractExtension;
import com.powsybl.contingency.Contingency;
Expand All @@ -14,15 +14,15 @@
/**
* @author Valentin Mouradian {@literal <valentin.mouradian at artelys.com>}
*/
public class ContingencyParameters extends AbstractExtension<Contingency> {
public class ContingencyLoadFlowParameters extends AbstractExtension<Contingency> {

private boolean distributedSlack;

private boolean areaInterchangeControl;

private LoadFlowParameters.BalanceType balanceType;

public ContingencyParameters(boolean distributedSlack, boolean areaInterchangeControl, LoadFlowParameters.BalanceType balanceType) {
public ContingencyLoadFlowParameters(boolean distributedSlack, boolean areaInterchangeControl, LoadFlowParameters.BalanceType balanceType) {
this.distributedSlack = distributedSlack;
this.areaInterchangeControl = areaInterchangeControl;
this.balanceType = balanceType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.powsybl.openloadflow.sa;
package com.powsybl.openloadflow.sa.extensions;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonGenerator;
Expand All @@ -14,7 +14,7 @@
import java.io.IOException;

@AutoService(ExtensionJsonSerializer.class)
public class ContingencyParametersJsonSerializer implements ExtensionJsonSerializer<Contingency, ContingencyParameters> {
public class ContingencyLoadFlowParametersJsonSerializer implements ExtensionJsonSerializer<Contingency, ContingencyLoadFlowParameters> {

@Override
public String getExtensionName() {
Expand All @@ -27,8 +27,8 @@ public String getCategoryName() {
}

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

/**
Expand All @@ -45,16 +45,16 @@ private interface SerializationSpec {

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

@Override
public void serialize(ContingencyParameters extension, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
public void serialize(ContingencyLoadFlowParameters extension, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
createMapper().writeValue(jsonGenerator, extension);
}

@Override
public ContingencyParameters deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
return createMapper().readValue(jsonParser, ContingencyParameters.class);
public ContingencyLoadFlowParameters deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
return createMapper().readValue(jsonParser, ContingencyLoadFlowParameters.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
import com.powsybl.commons.config.InMemoryPlatformConfig;
import com.powsybl.commons.config.MapModuleConfig;
import com.powsybl.commons.test.AbstractSerDeTest;
import com.powsybl.contingency.BranchContingency;
import com.powsybl.contingency.Contingency;
import com.powsybl.loadflow.LoadFlowParameters;
import com.powsybl.openloadflow.util.JsonSerializer;
import com.powsybl.openloadflow.util.PowsyblOpenLoadFlowVersion;
import com.powsybl.openloadflow.util.ProviderConstants;
import com.powsybl.security.SecurityAnalysisParameters;
Expand Down Expand Up @@ -109,25 +105,4 @@ void jsonTest() throws IOException {
parameters.addExtension(OpenSecurityAnalysisParameters.class, parametersExt);
roundTripTest(parameters, JsonSecurityAnalysisParameters::write, JsonSecurityAnalysisParameters::read, "/sa-params.json");
}

@Test
void testContingencyParametersExtension() {
Contingency contingency = new Contingency("L2", new BranchContingency("L2"));
contingency.addExtension(ContingencyParameters.class, new ContingencyParameters(false, true, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD));

ContingencyParameters contingencyParameters = contingency.getExtension(ContingencyParameters.class);

assertEquals(contingencyParameters, contingency.getExtensionByName("contingency-parameters"));
assertFalse(contingencyParameters.isDistributedSlack());
assertTrue(contingencyParameters.isAreaInterchangeControl());
assertEquals(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD, contingencyParameters.getBalanceType());
}

@Test
void testContingencyParametersExtensionJson() throws IOException {
Contingency contingency = new Contingency("L2", new BranchContingency("L2"));
contingency.addExtension(ContingencyParameters.class, new ContingencyParameters(false, true, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD));
assertEquals(ContingencyParameters.class, new ContingencyParametersJsonSerializer().getExtensionClass());
roundTripTest(contingency, JsonSerializer::write, JsonSerializer::readContingency, "/contingencies.json");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* Copyright (c) 2024, Coreso SA (https://www.coreso.eu/) and TSCNET Services GmbH (https://www.tscnet.eu/)
* 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.openloadflow.sa.extensions;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.powsybl.commons.json.JsonUtil;
import com.powsybl.commons.test.AbstractSerDeTest;
import com.powsybl.contingency.BranchContingency;
import com.powsybl.contingency.Contingency;
import com.powsybl.contingency.json.ContingencyJsonModule;
import com.powsybl.loadflow.LoadFlowParameters;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;

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

/**
* @author Valentin Mouradian {@literal <valentin.mouradian at artelys.com>}
*/
class OpenSecurityAnalysisExtensionsTest extends AbstractSerDeTest {

@Test
void testContingencyParametersExtension() {
Contingency contingency = new Contingency("L2", new BranchContingency("L2"));
contingency.addExtension(ContingencyLoadFlowParameters.class, new ContingencyLoadFlowParameters(false, true, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD));

ContingencyLoadFlowParameters contingencyLoadFlowParameters = contingency.getExtension(ContingencyLoadFlowParameters.class);

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

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

public static Contingency readContingency(Path jsonFile) {
Objects.requireNonNull(jsonFile);

try (InputStream is = Files.newInputStream(jsonFile)) {
ObjectMapper objectMapper = JsonUtil.createObjectMapper();
ContingencyJsonModule module = new ContingencyJsonModule();
objectMapper.registerModule(module);

return objectMapper.readValue(is, Contingency.class);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public static void writeContingency(Contingency contingency, Path jsonFile) {
Objects.requireNonNull(contingency);
Objects.requireNonNull(jsonFile);

try (OutputStream os = Files.newOutputStream(jsonFile)) {
ObjectMapper mapper = JsonUtil.createObjectMapper();
ContingencyJsonModule module = new ContingencyJsonModule();
mapper.registerModule(module);

ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
writer.writeValue(os, contingency);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

}
65 changes: 0 additions & 65 deletions src/test/java/com/powsybl/openloadflow/util/JsonSerializer.java

This file was deleted.

0 comments on commit d47e484

Please sign in to comment.