Skip to content

Commit

Permalink
Add deserialization in DynawoModelsSupplier and DynawoEventModelsSupp…
Browse files Browse the repository at this point in the history
…lier

Signed-off-by: lisrte <[email protected]>
  • Loading branch information
Lisrte committed Jun 13, 2024
1 parent 62865d6 commit a8d96f1
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.powsybl.dynawaltz.suppliers;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.powsybl.commons.PowsyblException;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
public class SupplierJsonDeserializer<T> {

private final StdDeserializer<List<T>> deserializer;

public SupplierJsonDeserializer(StdDeserializer<List<T>> deserializer) {
this.deserializer = deserializer;
}

public List<T> deserialize(Path path) {
try {
Reader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
return setupObjectMapper().readValue(reader, new TypeReference<>() {
});
} catch (IOException e) {
throw new PowsyblException("JSON input cannot be read", e);
}
}

public List<T> deserialize(InputStream is) {
try {
return setupObjectMapper().readValue(is, new TypeReference<>() {
});
} catch (IOException e) {
throw new PowsyblException("JSON input cannot be read", e);
}
}

private ObjectMapper setupObjectMapper() {
return new ObjectMapper().registerModule(new SimpleModule().addDeserializer(List.class, deserializer));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@
import com.powsybl.commons.report.ReportNode;
import com.powsybl.dynamicsimulation.DynamicModel;
import com.powsybl.dynamicsimulation.DynamicModelsSupplier;
import com.powsybl.dynawaltz.DynaWaltzProvider;
import com.powsybl.dynawaltz.builders.ModelBuilder;
import com.powsybl.dynawaltz.builders.ModelConfigsHandler;
import com.powsybl.dynawaltz.suppliers.Property;
import com.powsybl.dynawaltz.suppliers.SupplierJsonDeserializer;
import com.powsybl.iidm.network.Network;

import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;

/**
* Instantiates an {@link DynamicModelConfig} list from {@link DynamicModelConfig}
* Instantiates an {@link DynamicModelConfig} list from {@link DynamicModelConfig} or JSON input
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
public class DynawoModelsSupplier implements DynamicModelsSupplier {
Expand All @@ -34,6 +38,19 @@ public DynawoModelsSupplier(List<DynamicModelConfig> dynamicModelConfigs) {
this.dynamicModelConfigs = dynamicModelConfigs;
}

public DynawoModelsSupplier(InputStream is) {
this.dynamicModelConfigs = new SupplierJsonDeserializer<>(new DynamicModelConfigsJsonDeserializer()).deserialize(is);
}

public DynawoModelsSupplier(Path path) {
this.dynamicModelConfigs = new SupplierJsonDeserializer<>(new DynamicModelConfigsJsonDeserializer()).deserialize(path);
}

@Override
public String getName() {
return DynaWaltzProvider.NAME;
}

@Override
public List<DynamicModel> get(Network network, ReportNode reportNode) {
return dynamicModelConfigs.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@
import com.powsybl.commons.report.ReportNode;
import com.powsybl.dynamicsimulation.EventModel;
import com.powsybl.dynamicsimulation.EventModelsSupplier;
import com.powsybl.dynawaltz.DynaWaltzProvider;
import com.powsybl.dynawaltz.builders.ModelBuilder;
import com.powsybl.dynawaltz.builders.ModelConfigsHandler;
import com.powsybl.dynawaltz.suppliers.Property;
import com.powsybl.dynawaltz.suppliers.SupplierJsonDeserializer;
import com.powsybl.iidm.network.Network;

import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;

/**
* Instantiates an {@link EventModel} list from {@link EventModelConfig}
* Instantiates an {@link EventModel} list from {@link EventModelConfig} or JSON input
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
public class DynawoEventModelsSupplier implements EventModelsSupplier {
Expand All @@ -32,6 +36,19 @@ public DynawoEventModelsSupplier(List<EventModelConfig> eventModelConfigs) {
this.eventModelConfigs = eventModelConfigs;
}

public DynawoEventModelsSupplier(InputStream is) {
this.eventModelConfigs = new SupplierJsonDeserializer<>(new EventModelConfigsJsonDeserializer()).deserialize(is);
}

public DynawoEventModelsSupplier(Path path) {
this.eventModelConfigs = new SupplierJsonDeserializer<>(new EventModelConfigsJsonDeserializer()).deserialize(path);
}

@Override
public String getName() {
return DynaWaltzProvider.NAME;
}

@Override
public List<EventModel> get(Network network, ReportNode reportNode) {
return eventModelConfigs.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
class DynawoEventModelsSuppliersTest {
class DynawoEventModelsSupplierTest {

@Test
void testEventSupplier() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
class DynawoModelsSuppliersTest {
class DynawoModelsSupplierTest {

@Test
void testDynamicModelSupplier() {
Expand Down

0 comments on commit a8d96f1

Please sign in to comment.