Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor builders specific errors #380

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ private static Stream<Arguments> provideWarningsModel() {
+ DSL tests
+ Groovy Dynamic Models Supplier
+ DSL model builder for HvdcVSC
'staticId' field value 'L' not found for equipment type(s) VSC HVDC_LINE
'staticId' field value 'L' should be an HVDC VSC
Model BBM_HVDC cannot be instantiated
""")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
package com.powsybl.dynawo.builders;

import com.powsybl.commons.report.ReportNode;
import com.powsybl.iidm.network.Identifiable;
import com.powsybl.iidm.network.IdentifiableType;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.*;

import java.util.Objects;
import java.util.function.Predicate;

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
Expand All @@ -24,12 +21,11 @@ public abstract class AbstractEquipmentModelBuilder<T extends Identifiable<?>, R
protected String parameterSetId;
protected final ModelConfig modelConfig;
protected final BuilderEquipment<T> builderEquipment;
private Predicate<T> equipmentPredicates = eq -> Objects.equals(network, eq.getNetwork());

protected AbstractEquipmentModelBuilder(Network network, ModelConfig modelConfig, IdentifiableType equipmentType, ReportNode reportNode) {
super(network, reportNode);
this.modelConfig = Objects.requireNonNull(modelConfig);
this.builderEquipment = new BuilderEquipment<>(equipmentType);
this.builderEquipment = new BuilderEquipment<>(equipmentType.toString());
}

protected AbstractEquipmentModelBuilder(Network network, ModelConfig modelConfig, String equipmentType, ReportNode reportNode) {
Expand All @@ -38,6 +34,13 @@ protected AbstractEquipmentModelBuilder(Network network, ModelConfig modelConfig
this.builderEquipment = new BuilderEquipment<>(equipmentType);
}

protected AbstractEquipmentModelBuilder(Network network, ModelConfig modelConfig, String equipmentType,
BuilderEquipment.EquipmentPredicate<T> equipmentPredicate, ReportNode reportNode) {
super(network, reportNode);
this.modelConfig = modelConfig;
this.builderEquipment = new BuilderEquipment<>(equipmentType, equipmentPredicate);
}

@Override
public R staticId(String staticId) {
builderEquipment.addEquipment(staticId, this::findEquipment);
Expand All @@ -46,7 +49,7 @@ public R staticId(String staticId) {

@Override
public R equipment(T equipment) {
builderEquipment.addEquipment(equipment, this::checkEquipment);
builderEquipment.addEquipment(equipment, network);
return self();
}

Expand Down Expand Up @@ -77,14 +80,6 @@ protected void checkData() {

protected abstract T findEquipment(String staticId);

protected boolean checkEquipment(T equipment) {
return equipmentPredicates.test(equipment);
}

protected void addEquipmentPredicate(Predicate<T> predicate) {
equipmentPredicates = equipmentPredicates.and(predicate);
}

public T getEquipment() {
return builderEquipment.getEquipment();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
import com.powsybl.commons.report.ReportNode;
import com.powsybl.iidm.network.Identifiable;
import com.powsybl.iidm.network.IdentifiableType;
import com.powsybl.iidm.network.Network;

import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;

/**
* Represents an equipment field identified by a static ID in a builder
Expand All @@ -22,46 +23,57 @@
*/
public class BuilderEquipment<T extends Identifiable<?>> {

private static final String DEFAULT_FIELD_NAME = "staticId";
@FunctionalInterface
public interface EquipmentPredicate<T> {
boolean test(T equipment, String fieldName, ReportNode reportNode);
}

private static final String DEFAULT_FIELD_NAME = "staticId";
private static final String EQUIPMENT_FIELD_NAME = "equipment";

protected boolean fromStaticId;
protected String staticId;
protected T equipment;
private final String equipmentType;
private final String fieldName;
private final EquipmentPredicate<T> equipmentPredicate;

public BuilderEquipment(String equipmentType, String fieldName) {
public BuilderEquipment(String equipmentType, String fieldName, EquipmentPredicate<T> equipmentPredicate) {
this.equipmentType = equipmentType;
this.fieldName = fieldName;
this.equipmentPredicate = equipmentPredicate;
}

public BuilderEquipment(IdentifiableType identifiableType, String fieldName) {
this.equipmentType = identifiableType.toString();
this.fieldName = fieldName;
public BuilderEquipment(String equipmentType, EquipmentPredicate<T> equipmentPredicate) {
this(equipmentType, DEFAULT_FIELD_NAME, equipmentPredicate);
}

public BuilderEquipment(IdentifiableType identifiableType) {
this(identifiableType, DEFAULT_FIELD_NAME);
public BuilderEquipment(String equipmentType, String fieldName) {
this(equipmentType, fieldName, (eq, f, r) -> true);
}

public BuilderEquipment(String equipmentType) {
this(equipmentType, DEFAULT_FIELD_NAME);
}

public BuilderEquipment(IdentifiableType identifiableType) {
this(identifiableType.toString());
}

public BuilderEquipment(IdentifiableType identifiableType, String fieldName) {
this(identifiableType.toString(), fieldName);
}

public void addEquipment(String equipmentId, Function<String, T> equipmentSupplier) {
fromStaticId = true;
staticId = equipmentId;
equipment = equipmentSupplier.apply(staticId);
}

public void addEquipment(T equipment, Predicate<T> equipmentChecker) {
public void addEquipment(T equipment, Network network) {
fromStaticId = false;
staticId = equipment.getId();
if (equipmentChecker.test(equipment)) {
this.equipment = equipment;
}
this.equipment = Objects.equals(network, equipment.getNetwork()) ? equipment : null;
}

public boolean checkEquipmentData(ReportNode reportNode) {
Expand All @@ -76,7 +88,7 @@ public boolean checkEquipmentData(ReportNode reportNode) {
}
return false;
}
return true;
return equipmentPredicate.test(equipment, fieldName, reportNode);
}

public String getStaticId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public class BuilderEquipmentsList<T extends Identifiable<?>> {
// when set to true equipment ids not found in the network are seen as dynamic ids for automatons and reported as such
private final boolean missingIdsHasDynamicIds;
protected List<String> missingEquipmentIds = new ArrayList<>();
protected final List<T> equipments = new ArrayList<>();
protected List<T> equipments = new ArrayList<>();
private final BuilderEquipment.EquipmentPredicate<T> equipmentPredicate;

public BuilderEquipmentsList(IdentifiableType identifiableType, String fieldName) {
this(identifiableType.toString(), fieldName, false);
Expand All @@ -37,9 +38,15 @@ public BuilderEquipmentsList(String equipmentType, String fieldName) {
}

public BuilderEquipmentsList(String equipmentType, String fieldName, boolean missingIdsHasDynamicIds) {
this(equipmentType, fieldName, missingIdsHasDynamicIds, null);
}

public BuilderEquipmentsList(String equipmentType, String fieldName, boolean missingIdsHasDynamicIds,
BuilderEquipment.EquipmentPredicate<T> equipmentPredicate) {
this.equipmentType = equipmentType;
this.fieldName = fieldName;
this.missingIdsHasDynamicIds = missingIdsHasDynamicIds;
this.equipmentPredicate = equipmentPredicate;
}

public void addEquipments(String[] staticIds, Function<String, T> equipmentsSupplier) {
Expand Down Expand Up @@ -69,8 +76,10 @@ public boolean checkEquipmentData(ReportNode reportNode) {
BuilderReports.reportStaticIdUnknown(reportNode, fieldName, missingId, equipmentType));
if (emptyList) {
BuilderReports.reportEmptyList(reportNode, fieldName);
} else if (equipmentPredicate != null) {
equipments = equipments.stream().filter(eq -> equipmentPredicate.test(eq, fieldName, reportNode)).toList();
}
return !emptyList;
return !equipments.isEmpty();
} else {
missingEquipmentIds.forEach(missingId ->
BuilderReports.reportUnknownStaticIdHandling(reportNode, fieldName, missingId, equipmentType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
*/
public class BuilderIdListEquipmentList<T extends Identifiable<?>> extends BuilderEquipmentsList<T> {

public BuilderIdListEquipmentList(String equipmentType, String fieldName, BuilderEquipment.EquipmentPredicate<T> equipmentPredicate) {
super(equipmentType, fieldName, false, equipmentPredicate);
}

public BuilderIdListEquipmentList(String equipmentType, String fieldName) {
super(equipmentType, fieldName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.powsybl.commons.report.ReportNode;
import com.powsybl.commons.report.TypedValue;
import com.powsybl.iidm.network.HvdcConverterStation;
import com.powsybl.iidm.network.IdentifiableType;

/**
Expand All @@ -17,6 +18,7 @@
public final class BuilderReports {

private static final String FIELD_NAME = "fieldName";
private static final String STATIC_ID = "staticId";
private static final String EQUIPMENT_TYPE_FIELD = "equipmentType";

private BuilderReports() {
Expand Down Expand Up @@ -86,7 +88,7 @@ public static void reportStaticIdUnknown(ReportNode reportNode, String fieldName
.withMessageTemplate("unknownStaticIdToDynamic", "'${fieldName}' field value '${staticId}' not found for equipment type(s) ${equipmentType}")
.withUntypedValue(EQUIPMENT_TYPE_FIELD, equipmentType)
.withUntypedValue(FIELD_NAME, fieldName)
.withUntypedValue("staticId", staticId)
.withUntypedValue(STATIC_ID, staticId)
.withSeverity(TypedValue.WARN_SEVERITY)
.add();
}
Expand All @@ -96,7 +98,7 @@ public static void reportDifferentNetwork(ReportNode reportNode, String fieldNam
.withMessageTemplate("wrongNetwork", "'${fieldName}' field value ${equipmentType} ${staticId} does not belong to the builder network")
.withUntypedValue(EQUIPMENT_TYPE_FIELD, equipmentType)
.withUntypedValue(FIELD_NAME, fieldName)
.withUntypedValue("staticId", staticId)
.withUntypedValue(STATIC_ID, staticId)
.withSeverity(TypedValue.WARN_SEVERITY)
.add();
}
Expand All @@ -105,7 +107,7 @@ public static void reportUnknownStaticIdHandling(ReportNode reportNode, String f
reportNode.newReportNode()
.withMessageTemplate("staticIdUnknown", "'${fieldName}' field value '${staticId}' not found for equipment type(s) ${equipmentType}, id will be used as pure dynamic model id")
.withUntypedValue(EQUIPMENT_TYPE_FIELD, equipmentType)
.withUntypedValue(FIELD_NAME, fieldName).withUntypedValue("staticId", staticId)
.withUntypedValue(FIELD_NAME, fieldName).withUntypedValue(STATIC_ID, staticId)
.withSeverity(TypedValue.INFO_SEVERITY)
.add();
}
Expand Down Expand Up @@ -146,4 +148,32 @@ public static void reportFieldConflict(ReportNode reportNode, String firstFieldN
.withSeverity(TypedValue.TRACE_SEVERITY)
.add();
}

public static void reportWrongHvdcType(ReportNode reportNode, String fieldName, String staticId, HvdcConverterStation.HvdcType hvdcType) {
reportNode.newReportNode()
.withMessageTemplate("wrongHvdcType", "'${fieldName}' field value '${staticId}' should be an HVDC ${type}")
.withUntypedValue("type", hvdcType.toString())
.withUntypedValue(FIELD_NAME, fieldName)
.withUntypedValue(STATIC_ID, staticId)
.withSeverity(TypedValue.WARN_SEVERITY)
.add();
}

public static void reportFictitiousEquipment(ReportNode reportNode, String fieldName, String staticId) {
reportNode.newReportNode()
.withMessageTemplate("fictitiousEquipment", "'${fieldName}' field value '${staticId}' should not be fictitious")
.withUntypedValue(FIELD_NAME, fieldName)
.withUntypedValue(STATIC_ID, staticId)
.withSeverity(TypedValue.WARN_SEVERITY)
.add();
}

public static void reportNotEnergized(ReportNode reportNode, String fieldName, String staticId) {
reportNode.newReportNode()
.withMessageTemplate("notEnergized", "'${fieldName}' field value '${staticId}' should be energized")
.withUntypedValue(FIELD_NAME, fieldName)
.withUntypedValue(STATIC_ID, staticId)
.withSeverity(TypedValue.WARN_SEVERITY)
.add();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,46 @@

import com.powsybl.iidm.network.*;

import static com.powsybl.iidm.network.IdentifiableType.BUS;
import static com.powsybl.iidm.network.IdentifiableType.BUSBAR_SECTION;

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

public static final String MEASUREMENT_POINT_TYPE = IdentifiableType.BUS + "/" + IdentifiableType.BUSBAR_SECTION;
public static final String MEASUREMENT_POINT_TYPE = BUS + "/" + BUSBAR_SECTION;

private BuildersUtil() {
}

/**
* Returns the ActionConnectionPoint (bus or busbar section) identified by the staticId parameter
* Verifies the point is energized and in main connected component, if not returns null
* @param network the network containing the ActionConnectionPoint
* @param staticId the identifiable id
* @return the energized action connection point if found, <code>null</code> instead
*/
public static Identifiable<?> getActionConnectionPoint(Network network, String staticId) {
BusbarSection busbarSection = network.getBusbarSection(staticId);
if (busbarSection != null) {
return isEnergizedBus(busbarSection.getTerminal().getBusBreakerView().getBus()) ? busbarSection : null;
}
Bus bus = network.getBusBreakerView().getBus(staticId);
return isEnergizedBus(bus) ? bus : null;
return busbarSection != null ? busbarSection : network.getBusBreakerView().getBus(staticId);
}

/**
* Verifies the point is energized and in main connected component
*/
public static final BuilderEquipment.EquipmentPredicate<Identifiable<?>> IS_ENERGIZED = (eq, f, r) -> {
boolean isEnergized = switch (eq.getType()) {
case BUS -> isEnergizedBus((Bus) eq);
case BUSBAR_SECTION -> isEnergizedBus(((Injection<?>) eq).getTerminal().getBusBreakerView().getBus());
//TODO
default -> throw new UnsupportedOperationException("Only bus and bus bar section are supported");
};
if (!isEnergized) {
BuilderReports.reportNotEnergized(r, f, eq.getId());
}
return isEnergized;
};

/**
* Verifies a bus is energized and in main connected component
* @param bus the reviewed bus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.util.*;

import static com.powsybl.dynawo.builders.BuildersUtil.IS_ENERGIZED;
import static com.powsybl.dynawo.builders.BuildersUtil.MEASUREMENT_POINT_TYPE;

/**
Expand Down Expand Up @@ -57,7 +58,7 @@ public static Collection<ModelInfo> getSupportedModelInfos() {
protected TapChangerBlockingAutomationSystemBuilder(Network network, ModelConfig modelConfig, ReportNode reportNode) {
super(network, modelConfig, reportNode);
tapChangerEquipments = new BuilderEquipmentsList<>(TAP_CHANGER_TYPE, TRANSFORMER_FIELD, true);
uMeasurementPoints = new BuilderIdListEquipmentList<>(MEASUREMENT_POINT_TYPE, U_MEASUREMENTS_FIELD);
uMeasurementPoints = new BuilderIdListEquipmentList<>(MEASUREMENT_POINT_TYPE, U_MEASUREMENTS_FIELD, IS_ENERGIZED);
}

public TapChangerBlockingAutomationSystemBuilder transformers(String staticId) {
Expand Down
Loading
Loading