This repository has been archived by the owner on May 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added JSON importer/exporter for SecurityAnalysisResult (#72)
- Loading branch information
1 parent
fc10ab1
commit d48a371
Showing
23 changed files
with
792 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...ingency-api/src/main/java/eu/itesla_project/contingency/json/ContingencyDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...-api/src/main/java/eu/itesla_project/contingency/json/ContingencyElementDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
contingency-api/src/test/java/eu/itesla_project/contingency/json/ContingencyJsonTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.