Skip to content

Commit

Permalink
Add dynamic and event models suppliers
Browse files Browse the repository at this point in the history
Signed-off-by: lisrte <[email protected]>
  • Loading branch information
Lisrte committed May 2, 2024
1 parent 66ac224 commit f1d3fb0
Show file tree
Hide file tree
Showing 16 changed files with 888 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
public class StandardBusBuilder extends AbstractBusBuilder<StandardBusBuilder> {

public static final String CATEGORY = "INFINITE_BUS";
public static final String CATEGORY = "BASE_BUS";
private static final ModelConfigs MODEL_CONFIGS = ModelConfigsHandler.getInstance().getModelConfigs(CATEGORY);

public static StandardBusBuilder of(Network network) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) 2024, 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.dynawaltz.suppliers;

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
public record Property(String name, Object value, Class<?> propertyClass) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (c) 2024, 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.dynawaltz.suppliers;

import java.util.List;

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

private String name;
private Object value;
private PropertyType type;

public Property build() {
return new Property(name, type.isConversionFree() ? value : type.convertValue((String) value), type.getPropertyClass());
}

public PropertyBuilder name(String name) {
this.name = name;
return this;
}

public PropertyBuilder value(String value) {
this.value = value;
return this;
}

public PropertyBuilder values(List<String> values) {
this.value = values;
return this;
}

public PropertyBuilder arrays(List<List<String>> arrays) {
this.value = arrays.toArray(new List[0]);
return this;
}

public PropertyBuilder type(PropertyType type) {
this.type = type;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Copyright (c) 2024, 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.dynawaltz.suppliers;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.json.JsonUtil;

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

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

private PropertyParserUtils() {
}

public static Property parseProperty(JsonParser parser) {
PropertyBuilder builder = new PropertyBuilder();
JsonUtil.parseObject(parser, name -> switch (name) {
case "name" -> {
builder.name(parser.nextTextValue());
yield true;
}
case "value" -> {
builder.value(parser.nextTextValue());
yield true;
}
case "values" -> {
builder.values(JsonUtil.parseStringArray(parser));
yield true;
}
case "arrays" -> {
parseArrays(parser, builder);
yield true;
}
case "type" -> {
builder.type(PropertyType.valueOf(parser.nextTextValue()));
yield true;
}
default -> false;
});
return builder.build();
}

private static void parseArrays(JsonParser parser, PropertyBuilder builder) throws IOException {
parser.nextToken();
parser.nextToken();
JsonToken token;
List<List<String>> arrays = new ArrayList<>();
List<String> values = new ArrayList<>();
while ((token = parser.nextToken()) != null) {
if (token == JsonToken.VALUE_STRING) {
values.add(parser.getText());
} else if (token == JsonToken.END_ARRAY) {
arrays.add(values);
JsonToken next = parser.nextToken();
if (next == JsonToken.END_ARRAY) {
builder.arrays(arrays);
break;
} else if (next == JsonToken.START_ARRAY) {
values = new ArrayList<>();
} else {
throw new PowsyblException("Unexpected token " + next);
}
} else {
throw new PowsyblException("Unexpected token " + token);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright (c) 2024, 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.dynawaltz.suppliers;

import com.powsybl.iidm.network.TwoSides;

import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.function.Function;

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

TWO_SIDES(TwoSides.class, TwoSides::valueOf),
BOOLEAN(boolean.class, Boolean::parseBoolean),
INTEGER(int.class, Integer::parseInt),
DOUBLE(double.class, Double::parseDouble),
STRING(String.class, value -> value),
STRINGS(Collection.class, List::of),
STRINGS_ARRAYS(Collection[].class, s -> new Collection[]{List.of(s)});

private static final EnumSet<PropertyType> CONVERSION_FREE_TYPES = EnumSet.of(STRING, STRINGS, STRINGS_ARRAYS);

private final Class<?> propertyClass;
private final Function<String, Object> valueConvertor;

PropertyType(Class<?> propertyClass, Function<String, Object> valueConvertor) {
this.propertyClass = propertyClass;
this.valueConvertor = valueConvertor;
}

public Class<?> getPropertyClass() {
return propertyClass;
}

public Object convertValue(String value) {
return valueConvertor.apply(value);
}

public boolean isConversionFree() {
return CONVERSION_FREE_TYPES.contains(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) 2024, 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.dynawaltz.suppliers;

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
public enum SetGroupType {
FIXED,
PREFIX,
SUFFIX
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright (c) 2024, 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.dynawaltz.suppliers.dynamicmodels;

import com.powsybl.commons.PowsyblException;
import com.powsybl.dynawaltz.suppliers.Property;
import com.powsybl.dynawaltz.suppliers.SetGroupType;

import java.util.List;

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

private final String model;
private final String group;
private final List<Property> properties;

public DynamicModelConfig(String model, String group, SetGroupType groupType, List<Property> properties) {
this.model = model;
this.group = switch (groupType) {
case FIXED -> group;
case PREFIX -> group + getDynamicModelIdProperty(properties);
case SUFFIX -> getDynamicModelIdProperty(properties) + group;
};
this.properties = properties;
}

public DynamicModelConfig(String model, String group, List<Property> properties) {
this.model = model;
this.group = group;
this.properties = properties;
}

public String getModel() {
return model;
}

public String getGroup() {
return group;
}

public List<Property> getProperties() {
return properties;
}

private static String getDynamicModelIdProperty(List<Property> properties) {
return properties.stream()
.filter(p -> p.name().equalsIgnoreCase("dynamicModelId"))
.map(p -> (String) p.value())
.findFirst()
.orElseGet(() -> DynamicModelConfig.getStaticIdProperty(properties));
}

private static String getStaticIdProperty(List<Property> properties) {
return properties.stream()
.filter(p -> p.name().equalsIgnoreCase("staticId"))
.map(p -> (String) p.value())
.findFirst()
.orElseThrow(() -> new PowsyblException("No ID found for parameter set id"));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Copyright (c) 2024, 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.dynawaltz.suppliers.dynamicmodels;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.powsybl.commons.json.JsonUtil;
import com.powsybl.dynawaltz.suppliers.Property;
import com.powsybl.dynawaltz.suppliers.PropertyParserUtils;
import com.powsybl.dynawaltz.suppliers.SetGroupType;

import java.util.ArrayList;
import java.util.List;

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
public class DynamicModelConfigsJsonDeserializer extends StdDeserializer<List<DynamicModelConfig>> {

public DynamicModelConfigsJsonDeserializer() {
super(List.class);
}

@Override
public List<DynamicModelConfig> deserialize(JsonParser parser, DeserializationContext context) {
List<DynamicModelConfig> modelConfigList = new ArrayList<>();
JsonUtil.parseObject(parser, name -> {
if (name.equals("models")) {
JsonUtil.parseObjectArray(parser, modelConfigList::add, DynamicModelConfigsJsonDeserializer::parseModelConfig);
return true;
}
return false;
});
return modelConfigList;
}

private static DynamicModelConfig parseModelConfig(JsonParser parser) {
var parsingContext = new Object() {
String model = null;
String group = null;
SetGroupType groupType = null;
final List<Property> properties = new ArrayList<>();
};
JsonUtil.parseObject(parser, name -> switch (name) {
case "model" -> {
parsingContext.model = parser.nextTextValue();
yield true;
}
case "group" -> {
parsingContext.group = parser.nextTextValue();
yield true;
}
case "groupType" -> {
parsingContext.groupType = SetGroupType.valueOf(parser.nextTextValue());
yield true;
}
case "properties" -> {
JsonUtil.parseObjectArray(parser, parsingContext.properties::add, PropertyParserUtils::parseProperty);
yield true;
}
default -> false;
});
return new DynamicModelConfig(parsingContext.model, parsingContext.group, parsingContext.groupType, parsingContext.properties);
}
}
Loading

0 comments on commit f1d3fb0

Please sign in to comment.