-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dynamic and event models suppliers
Signed-off-by: lisrte <[email protected]>
- Loading branch information
Showing
16 changed files
with
888 additions
and
1 deletion.
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
14 changes: 14 additions & 0 deletions
14
dynawaltz/src/main/java/com/powsybl/dynawaltz/suppliers/Property.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,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) { | ||
} |
49 changes: 49 additions & 0 deletions
49
dynawaltz/src/main/java/com/powsybl/dynawaltz/suppliers/PropertyBuilder.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,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; | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
dynawaltz/src/main/java/com/powsybl/dynawaltz/suppliers/PropertyParserUtils.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,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); | ||
} | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
dynawaltz/src/main/java/com/powsybl/dynawaltz/suppliers/PropertyType.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,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); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
dynawaltz/src/main/java/com/powsybl/dynawaltz/suppliers/SetGroupType.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,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 | ||
} |
69 changes: 69 additions & 0 deletions
69
...waltz/src/main/java/com/powsybl/dynawaltz/suppliers/dynamicmodels/DynamicModelConfig.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,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")); | ||
} | ||
} | ||
|
71 changes: 71 additions & 0 deletions
71
...va/com/powsybl/dynawaltz/suppliers/dynamicmodels/DynamicModelConfigsJsonDeserializer.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) 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); | ||
} | ||
} |
Oops, something went wrong.