Skip to content
This repository has been archived by the owner on May 16, 2018. It is now read-only.

Commit

Permalink
Added JSON importer/exporter for SecurityAnalysisResult (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
massimo-ferraro authored and mathbagu committed Apr 19, 2017
1 parent fc10ab1 commit d48a371
Show file tree
Hide file tree
Showing 23 changed files with 792 additions and 36 deletions.
16 changes: 16 additions & 0 deletions contingency-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

<dependencies>
<!-- Compilation dependencies -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>computation</artifactId>
Expand All @@ -35,6 +39,11 @@
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand All @@ -45,6 +54,13 @@
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>commons</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>iidm-impl</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright (c) 2017, 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 eu.itesla_project.contingency.json;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import eu.itesla_project.contingency.Contingency;
import eu.itesla_project.contingency.ContingencyElement;
import eu.itesla_project.contingency.ContingencyImpl;

/**
*
* @author Massimo Ferraro <[email protected]>
*/
public class ContingencyDeserializer extends StdDeserializer<Contingency> {

public ContingencyDeserializer() {
super(Contingency.class);
}

@Override
public Contingency deserialize(JsonParser parser, DeserializationContext ctx) throws IOException {
String id = null;
List<ContingencyElement> elements = Collections.emptyList();

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

case "elements":
parser.nextToken();
elements = parser.readValueAs(new TypeReference<ArrayList<ContingencyElement>>() {});
break;

default:
throw new AssertionError("Unexpected field: " + parser.getCurrentName());
}
}

return new ContingencyImpl(id, elements);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Copyright (c) 2017, 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 eu.itesla_project.contingency.json;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import eu.itesla_project.contingency.BranchContingency;
import eu.itesla_project.contingency.ContingencyElement;
import eu.itesla_project.contingency.ContingencyElementType;
import eu.itesla_project.contingency.GeneratorContingency;

import java.io.IOException;

/**
* @author Mathieu Bague <mathieu.bague at rte-france.com>
*/
public class ContingencyElementDeserializer extends StdDeserializer<ContingencyElement> {

public ContingencyElementDeserializer() {
super(ContingencyElement.class);
}

@Override
public ContingencyElement deserialize(JsonParser parser, DeserializationContext ctx) throws IOException {
String id = null;
String substationId = null;
ContingencyElementType type = null;

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

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

case "type":
parser.nextToken();
type = parser.readValueAs(ContingencyElementType.class);
break;

default:
throw new AssertionError("Unexpected field: " + parser.getCurrentName());
}
}

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

case GENERATOR:
return new GeneratorContingency(id);

default:
throw new AssertionError();
}
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package eu.itesla_project.contingency.json;

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

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.module.SimpleModule;
import eu.itesla_project.contingency.*;
import org.junit.Test;

import eu.itesla_project.commons.ConverterBaseTest;

public class ContingencyJsonTest extends ConverterBaseTest {

private static Contingency create() {
List<ContingencyElement> elements = new ArrayList<>();
elements.add(new BranchContingency("NHV1_NHV2_2", "P1"));
elements.add(new BranchContingency("NHV1_NHV2_1"));
elements.add(new GeneratorContingency("GEN"));

return new ContingencyImpl("contingency", elements);
}

private static Contingency read(Path jsonFile) {
Objects.requireNonNull(jsonFile);

try (InputStream is = Files.newInputStream(jsonFile)) {
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(Contingency.class, new ContingencyDeserializer());
module.addDeserializer(ContingencyElement.class, new ContingencyElementDeserializer());
objectMapper.registerModule(module);

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

private static void write(Contingency object, Path jsonFile) {
Objects.requireNonNull(object);
Objects.requireNonNull(jsonFile);

try (OutputStream os = Files.newOutputStream(jsonFile)) {
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();

writer.writeValue(os, object);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Test
public void roundTripTest() throws IOException {
roundTripTest(create(), ContingencyJsonTest::write, ContingencyJsonTest::read, "/contingency.json");
}
}
15 changes: 15 additions & 0 deletions contingency-api/src/test/resources/contingency.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"id" : "contingency",
"elements" : [ {
"id" : "NHV1_NHV2_2",
"substationId" : "P1",
"type" : "BRANCH"
}, {
"id" : "NHV1_NHV2_1",
"substationId" : null,
"type" : "BRANCH"
}, {
"id" : "GEN",
"type" : "GENERATOR"
} ]
}
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<graphvizapi.version>1.1</graphvizapi.version>
<groovy.version>2.4.7</groovy.version>
<guava.version>20.0</guava.version>
<jackson.version>2.8.7</jackson.version>
<jama.version>1.0.3</jama.version>
<jimfs.version>1.1</jimfs.version>
<jodatime.version>2.9.7</jodatime.version>
Expand Down Expand Up @@ -291,6 +292,11 @@
<dependencyManagement>
<dependencies>
<!-- Compile dependencies -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
Expand Down
11 changes: 11 additions & 0 deletions security-analysis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
<name>Security analysis</name>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>contingency-api</artifactId>
Expand Down Expand Up @@ -69,6 +73,13 @@
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>commons</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>iidm-impl</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public class LimitViolation {
private final Country country;

private final float baseVoltage;

public LimitViolation(String subjectId, LimitViolationType limitType, float limit, String limitName, float limitReduction, float value, Country country, float baseVoltage) {

public LimitViolation(String subjectId, LimitViolationType limitType, float limit, String limitName,
float limitReduction, float value, Country country, float baseVoltage) {
this.subjectId = Objects.requireNonNull(subjectId);
this.limitType = Objects.requireNonNull(limitType);
this.limit = limit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,25 @@
*/
package eu.itesla_project.security;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
* @author Mathieu Bague <mathieu.bague at rte-france.com>
*/
abstract class LimitViolationsResult {
public class LimitViolationsResult {

private final boolean computationOk;

private final List<LimitViolation> limitViolations;

private final List<String> actionsTaken;

public LimitViolationsResult(boolean computationOk, List<LimitViolation> limitViolations) {
this(computationOk, limitViolations, Collections.emptyList());
}

public LimitViolationsResult(boolean computationOk, List<LimitViolation> limitViolations, List<String> actionsTaken) {
this.computationOk = computationOk;
this.limitViolations = Objects.requireNonNull(limitViolations);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2016, RTE (http://www.rte-france.com)
* Copyright (c) 2016-2017, 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/.
Expand All @@ -16,20 +16,45 @@
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian@ at rte-france.com>
* @author Mathieu Bague <mathieu.bague at rte-france.com>
*/
public class PostContingencyResult extends LimitViolationsResult {
public class PostContingencyResult {

private final Contingency contingency;

private final LimitViolationsResult limitViolationsResult;

public PostContingencyResult(Contingency contingency, LimitViolationsResult limitViolationsResult) {
this.contingency = Objects.requireNonNull(contingency);
this.limitViolationsResult = Objects.requireNonNull(limitViolationsResult);
}

public PostContingencyResult(Contingency contingency, boolean computationOk, List<LimitViolation> limitViolations) {
this(contingency, computationOk, limitViolations, Collections.emptyList());
this(contingency, new LimitViolationsResult(computationOk, limitViolations, Collections.emptyList()));
}

public PostContingencyResult(Contingency contingency, boolean computationOk, List<LimitViolation> limitViolations, List<String> actionsTaken) {
super(computationOk, limitViolations, actionsTaken);
this.contingency = Objects.requireNonNull(contingency);
this(contingency, new LimitViolationsResult(computationOk, limitViolations, actionsTaken));
}

public Contingency getContingency() {
return contingency;
}

public LimitViolationsResult getLimitViolationsResult() {
return limitViolationsResult;
}

@Deprecated
public boolean isComputationOk() {
return limitViolationsResult.isComputationOk();
}

@Deprecated
public List<LimitViolation> getLimitViolations() {
return limitViolationsResult.getLimitViolations();
}

@Deprecated
public List<String> getActionsTaken() {
return limitViolationsResult.getActionsTaken();
}
}
Loading

0 comments on commit d48a371

Please sign in to comment.