Skip to content

Commit

Permalink
Add an equipment setter in AbstractEquipmentModelBuilder
Browse files Browse the repository at this point in the history
Signed-off-by: lisrte <[email protected]>
  • Loading branch information
Lisrte authored and flo-dup committed Jan 29, 2024
1 parent 022a193 commit 7d4b0d2
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.powsybl.iidm.network.IdentifiableType;
import com.powsybl.iidm.network.Network;

import java.util.Objects;

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
Expand All @@ -33,6 +35,11 @@ public R staticId(String staticId) {
return self();
}

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

public R dynamicModelId(String dynamicModelId) {
this.dynamicModelId = dynamicModelId;
return self();
Expand All @@ -58,6 +65,10 @@ protected void checkData() {

protected abstract T findEquipment(String staticId);

protected boolean checkEquipment(T equipment) {
return Objects.equals(network.getId(), equipment.getNetwork().getId());
}

public T getEquipment() {
return builderEquipment.getEquipment();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.powsybl.iidm.network.IdentifiableType;

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

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

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;
Expand All @@ -37,24 +43,37 @@ public BuilderEquipment(IdentifiableType identifiableType, String fieldName) {
}

public BuilderEquipment(IdentifiableType identifiableType) {
this(identifiableType, "staticId");
this(identifiableType, DEFAULT_FIELD_NAME);
}

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

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) {
fromStaticId = false;
staticId = equipment.getId();
if (equipmentChecker.test(equipment)) {
this.equipment = equipment;
}
}

public boolean checkEquipmentData(Reporter reporter) {
if (!hasStaticId()) {
Reporters.reportFieldNotSet(reporter, fieldName);
return false;
} else if (equipment == null) {
Reporters.reportStaticIdUnknown(reporter, fieldName, staticId, equipmentType);
if (fromStaticId) {
Reporters.reportStaticIdUnknown(reporter, fieldName, staticId, equipmentType);
} else {
Reporters.reportDifferentNetwork(reporter, EQUIPMENT_FIELD_NAME, staticId, equipmentType);
}
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,19 @@ public static void reportStaticIdUnknown(Reporter reporter, String fieldName, St
.withKey("staticIdUnknown")
.withDefaultMessage("'${fieldName}' field value '${staticId}' not found for equipment type(s) ${equipmentType}")
.withValue("equipmentType", equipmentType)
.withValue(FIELD_NAME, fieldName).withValue("staticId", staticId)
.withValue(FIELD_NAME, fieldName)
.withValue("staticId", staticId)
.withSeverity(TypedValue.WARN_SEVERITY)
.build());
}

public static void reportDifferentNetwork(Reporter reporter, String fieldName, String staticId, String equipmentType) {
reporter.report(Report.builder()
.withKey("wrongNetwork")
.withDefaultMessage("'${fieldName}' field value ${equipmentType} ${staticId} does not belong to the builder network")
.withValue("equipmentType", equipmentType)
.withValue(FIELD_NAME, fieldName)
.withValue("staticId", staticId)
.withSeverity(TypedValue.WARN_SEVERITY)
.build());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* 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.models;

import com.powsybl.commons.reporter.ReporterModel;
import com.powsybl.commons.test.TestUtil;
import com.powsybl.dynawaltz.models.lines.LineBuilder;
import com.powsybl.iidm.network.Line;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.test.PhaseShifterTestCaseFactory;
import com.powsybl.iidm.network.test.SvcTestCaseFactory;
import org.junit.jupiter.api.Test;

import java.io.StringWriter;

import static org.junit.jupiter.api.Assertions.*;

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

@Test
void addEquipmentFromAnotherNetwork() {

Network network = SvcTestCaseFactory.create();
Line ln1 = network.getLine("L1");
Network network2 = PhaseShifterTestCaseFactory.create();
Line ln2 = network2.getLine("L1");
ReporterModel reporter = new ReporterModel("builderTests", "Builder tests");

BlackBoxModel bbm1 = LineBuilder.of(network)
.dynamicModelId("BBM_LINE_NETWORK_1")
.equipment(ln1)
.parameterSetId("sl")
.build();
BlackBoxModel bbm2 = LineBuilder.of(network, reporter)
.dynamicModelId("BBM_LINE_NETWORK_2")
.equipment(ln2)
.parameterSetId("sl")
.build();

assertNotNull(bbm1);
assertNull(bbm2);
StringWriter sw = new StringWriter();
reporter.export(sw);
assertEquals("""
+ Builder tests
'equipment' field value LINE L1 does not belong to the builder network
Model BBM_LINE_NETWORK_2 cannot be instantiated
""",
TestUtil.normalizeLineSeparator(sw.toString()));
}
}

0 comments on commit 7d4b0d2

Please sign in to comment.