Skip to content

Commit

Permalink
Code changes after review comments
Browse files Browse the repository at this point in the history
Signed-off-by: BOUHOURS Antoine <[email protected]>
  • Loading branch information
antoinebhs committed Dec 12, 2023
1 parent 6d5d096 commit 3762133
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,26 @@ public void apply(Network network, Reporter subReporter) {
.filter(distinctByKey(FilterInfos::getId))
.collect(Collectors.toMap(FilterInfos::getId, FilterInfos::getName));

Map<UUID, FilterEquipments> exportFilters = ModificationUtils.getInstance().getUuidFilterEquipmentsMap(filterService, network, subReporter, filters, scalingInfos);
if (exportFilters == null) {
return;
Map<UUID, FilterEquipments> exportFilters = ModificationUtils.getUuidFilterEquipmentsMap(filterService, network, subReporter, filters, scalingInfos.getErrorType());
if (exportFilters != null) {
Map<UUID, FilterEquipments> filtersWithWrongEquipmentIds = ModificationUtils.getUuidFilterWrongEquipmentsIdsMap(subReporter, exportFilters, filters);

// apply variations
scalingInfos.getVariations().forEach(variation -> {
List<IdentifiableAttributes> identifiableAttributes = ModificationUtils.getIdentifiableAttributes(exportFilters, filtersWithWrongEquipmentIds, variation.getFilters(), subReporter);

if (CollectionUtils.isEmpty(identifiableAttributes)) {
String filterNames = variation.getFilters().stream().map(FilterInfos::getName).collect(Collectors.joining(", "));
createReport(subReporter,
"allFiltersWrong",
String.format("All of the following variation's filters have equipments with wrong id : %s", filterNames),
TypedValue.WARN_SEVERITY);
} else {
applyVariation(network, subReporter, identifiableAttributes, variation);
}
});
createReport(subReporter, "scalingCreated", "new scaling created", TypedValue.INFO_SEVERITY);
}
Map<UUID, FilterEquipments> filtersWithWrongEquipmentIds = ModificationUtils.getInstance().getUuidFilterWrongEquipmentsIdsMap(subReporter, exportFilters, filters);

// apply variations
scalingInfos.getVariations().forEach(variation -> {
List<IdentifiableAttributes> identifiableAttributes = ModificationUtils.getIdentifiableAttributes(exportFilters, filtersWithWrongEquipmentIds, variation.getFilters(), subReporter);

if (CollectionUtils.isEmpty(identifiableAttributes)) {
String filterNames = variation.getFilters().stream().map(FilterInfos::getName).collect(Collectors.joining(", "));
createReport(subReporter,
"allFiltersWrong",
String.format("All of the following variation's filters have equipments with wrong id : %s", filterNames),
TypedValue.WARN_SEVERITY);
} else {
applyVariation(network, subReporter, identifiableAttributes, variation);
}
});
createReport(subReporter, "scalingCreated", "new scaling created", TypedValue.INFO_SEVERITY);
}

private void applyVariation(Network network,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void apply(Network network, Reporter subReporter) {
.filter(distinctByKey(FilterInfos::getId))
.collect(Collectors.toMap(FilterInfos::getId, FilterInfos::getName));

Map<UUID, FilterEquipments> exportFilters = ModificationUtils.getInstance().getUuidFilterEquipmentsMap(filterService, network, subReporter, filters, modificationInfos);
Map<UUID, FilterEquipments> exportFilters = ModificationUtils.getUuidFilterEquipmentsMap(filterService, network, subReporter, filters, modificationInfos.getErrorType());

if (exportFilters != null) {
long equipmentCount = exportFilters.values()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1045,19 +1045,18 @@ public static List<IdentifiableAttributes> getIdentifiableAttributes(Map<UUID, F
.flatMap(f -> exportFilters.get(f.getId())
.getIdentifiableAttributes()
.stream())
.filter(distinctByKey(IdentifiableAttributes::getId))
.toList();
}

@Nullable
public Map<UUID, FilterEquipments> getUuidFilterEquipmentsMap(FilterService filterService, Network network, Reporter subReporter, Map<UUID, String> filters, ModificationInfos modificationInfos) {
public static Map<UUID, FilterEquipments> getUuidFilterEquipmentsMap(FilterService filterService, Network network, Reporter subReporter, Map<UUID, String> filters, NetworkModificationException.Type errorType) {
Map<UUID, FilterEquipments> exportFilters = filterService.getUuidFilterEquipmentsMap(network, filters);

boolean isValidFilter = ModificationUtils.getInstance().isValidFilter(subReporter, modificationInfos.getErrorType(), exportFilters);
boolean isValidFilter = ModificationUtils.getInstance().isValidFilter(subReporter, errorType, exportFilters);
return isValidFilter ? exportFilters : null;
}

public Map<UUID, FilterEquipments> getUuidFilterWrongEquipmentsIdsMap(Reporter subReporter, Map<UUID, FilterEquipments> exportFilters, Map<UUID, String> filters) {
public static Map<UUID, FilterEquipments> getUuidFilterWrongEquipmentsIdsMap(Reporter subReporter, Map<UUID, FilterEquipments> exportFilters, Map<UUID, String> filters) {
// collect all filters with wrong equipments ids
Map<UUID, FilterEquipments> filterWithWrongEquipmentsIds = exportFilters.entrySet().stream()
.filter(e -> !CollectionUtils.isEmpty(e.getValue().getNotFoundEquipments()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private void createGenerators() {
getNetwork().getGenerator(GENERATOR_ID_10).setTargetP(100).setMaxP(500);
}

private List<FilterEquipments> getTestFilters(boolean testDuplicatedElements) {
private List<FilterEquipments> getTestFilters() {
IdentifiableAttributes gen1 = getIdentifiableAttributes(GENERATOR_ID_1, 1.0);
IdentifiableAttributes gen2 = getIdentifiableAttributes(GENERATOR_ID_2, 2.0);
IdentifiableAttributes gen3 = getIdentifiableAttributes(GENERATOR_ID_3, 2.0);
Expand All @@ -96,8 +96,7 @@ private List<FilterEquipments> getTestFilters(boolean testDuplicatedElements) {
IdentifiableAttributes gen9 = getIdentifiableAttributes(GENERATOR_ID_9, 0.0);
IdentifiableAttributes gen10 = getIdentifiableAttributes(GENERATOR_ID_10, 9.0);

List<IdentifiableAttributes> listFilter1 = testDuplicatedElements ? List.of(gen1, gen2, gen2) : List.of(gen1, gen2);
FilterEquipments filter1 = getFilterEquipments(FILTER_ID_1, "filter1", listFilter1, List.of());
FilterEquipments filter1 = getFilterEquipments(FILTER_ID_1, "filter1", List.of(gen1, gen2), List.of());
FilterEquipments filter2 = getFilterEquipments(FILTER_ID_2, "filter2", List.of(gen3, gen4), List.of());
FilterEquipments filter3 = getFilterEquipments(FILTER_ID_3, "filter3", List.of(gen5, gen6), List.of());
FilterEquipments filter4 = getFilterEquipments(FILTER_ID_4, "filter4", List.of(gen7, gen8), List.of());
Expand All @@ -109,7 +108,7 @@ private List<FilterEquipments> getTestFilters(boolean testDuplicatedElements) {
@Test
@Override
public void testCreate() throws Exception {
List<FilterEquipments> filters = getTestFilters(false);
List<FilterEquipments> filters = getTestFilters();
UUID stubId = wireMockServer.stubFor(WireMock.get(WireMock.urlMatching(getPath(getNetworkUuid(), true) + "(.+,){4}.*"))
.willReturn(WireMock.ok()
.withBody(mapper.writeValueAsString(filters))
Expand All @@ -129,7 +128,7 @@ public void testCreate() throws Exception {
@Test
@Override
public void testCopy() throws Exception {
List<FilterEquipments> filters = getTestFilters(false);
List<FilterEquipments> filters = getTestFilters();
UUID stubId = wireMockServer.stubFor(WireMock.get(WireMock.urlMatching(getPath(getNetworkUuid(), true) + "(.+,){4}.*"))
.willReturn(WireMock.ok()
.withBody(mapper.writeValueAsString(filters))
Expand Down Expand Up @@ -262,25 +261,6 @@ public void testScalingCreationWithWarning() throws Exception {
wireMockUtils.verifyGetRequest(subFilter, PATH, Map.of("networkUuid", WireMock.equalTo(String.valueOf(getNetworkUuid())), "variantId", WireMock.equalTo("variant_1"), "ids", WireMock.matching(".*")), false);
}

@Test
public void testCreateWithDuplicatedElements() throws Exception {
List<FilterEquipments> filters = getTestFilters(true);
UUID stubId = wireMockServer.stubFor(WireMock.get(WireMock.urlMatching(getPath(getNetworkUuid(), true) + "(.+,){4}.*"))
.willReturn(WireMock.ok()
.withBody(mapper.writeValueAsString(filters))
.withHeader("Content-Type", "application/json"))).getId();

super.testCreate();

wireMockUtils.verifyGetRequest(stubId, PATH, handleQueryParams(getNetworkUuid(), filters.stream().map(FilterEquipments::getFilterId).collect(Collectors.toList())), false);

assertEquals(
String.format("ScalingInfos(super=ModificationInfos(uuid=null, type=GENERATOR_SCALING, date=null, stashed=false, messageType=null, messageValues=null), variations=[ScalingVariationInfos(id=null, filters=[FilterInfos(id=%s, name=filter1)], variationMode=PROPORTIONAL_TO_PMAX, variationValue=50.0, reactiveVariationMode=null), ScalingVariationInfos(id=null, filters=[FilterInfos(id=%s, name=filter2)], variationMode=REGULAR_DISTRIBUTION, variationValue=50.0, reactiveVariationMode=null), ScalingVariationInfos(id=null, filters=[FilterInfos(id=%s, name=filter3)], variationMode=STACKING_UP, variationValue=50.0, reactiveVariationMode=null), ScalingVariationInfos(id=null, filters=[FilterInfos(id=%s, name=filter4)], variationMode=VENTILATION, variationValue=50.0, reactiveVariationMode=null), ScalingVariationInfos(id=null, filters=[FilterInfos(id=%s, name=filter1), FilterInfos(id=%s, name=filter5)], variationMode=PROPORTIONAL, variationValue=50.0, reactiveVariationMode=null)], variationType=DELTA_P)",
FILTER_ID_1, FILTER_ID_2, FILTER_ID_3, FILTER_ID_4, FILTER_ID_1, FILTER_ID_5),
buildModification().toString()
);
}

@Override
protected Network createNetwork(UUID networkUuid) {
return NetworkCreation.createGeneratorsNetwork(networkUuid, new NetworkFactoryImpl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private void createLoads() {
getNetwork().getLoad(LOAD_ID_10).setP0(100).setQ0(1.0);
}

private List<FilterEquipments> getTestFilters(boolean testDuplicatedEquipements) {
private List<FilterEquipments> getTestFilters() {
IdentifiableAttributes load1 = getIdentifiableAttributes(LOAD_ID_1, 1.0);
IdentifiableAttributes load2 = getIdentifiableAttributes(LOAD_ID_2, 2.0);
IdentifiableAttributes load3 = getIdentifiableAttributes(LOAD_ID_3, 2.0);
Expand All @@ -118,8 +118,7 @@ private List<FilterEquipments> getTestFilters(boolean testDuplicatedEquipements)
IdentifiableAttributes load9 = getIdentifiableAttributes(LOAD_ID_9, 0.0);
IdentifiableAttributes load10 = getIdentifiableAttributes(LOAD_ID_10, 9.0);

List<IdentifiableAttributes> listFilter1 = testDuplicatedEquipements ? List.of(load1, load2, load2) : List.of(load1, load2);
FilterEquipments filter1 = getFilterEquipments(FILTER_ID_1, "filter1", listFilter1, List.of());
FilterEquipments filter1 = getFilterEquipments(FILTER_ID_1, "filter1", List.of(load1, load2), List.of());
FilterEquipments filter2 = getFilterEquipments(FILTER_ID_2, "filter2", List.of(load3, load4), List.of());
FilterEquipments filter3 = getFilterEquipments(FILTER_ID_3, "filter3", List.of(load5, load6), List.of());
FilterEquipments filter4 = getFilterEquipments(FILTER_ID_4, "filter4", List.of(load7, load8), List.of());
Expand All @@ -131,7 +130,7 @@ private List<FilterEquipments> getTestFilters(boolean testDuplicatedEquipements)
@Test
@Override
public void testCreate() throws Exception {
List<FilterEquipments> filters = getTestFilters(false);
List<FilterEquipments> filters = getTestFilters();
UUID stubId = wireMockServer.stubFor(WireMock.get(WireMock.urlMatching(getPath(getNetworkUuid(), true) + "(.+,){4}.*"))
.willReturn(WireMock.ok()
.withBody(mapper.writeValueAsString(filters))
Expand All @@ -145,7 +144,7 @@ public void testCreate() throws Exception {
@Test
@Override
public void testCopy() throws Exception {
List<FilterEquipments> filters = getTestFilters(false);
List<FilterEquipments> filters = getTestFilters();
UUID stubId = wireMockServer.stubFor(WireMock.get(WireMock.urlMatching(getPath(getNetworkUuid(), true) + "(.+,){4}.*"))
.willReturn(WireMock.ok()
.withBody(mapper.writeValueAsString(filters))
Expand Down Expand Up @@ -280,19 +279,6 @@ public void testScalingCreationWithWarning() throws Exception {
assertEquals(300, getNetwork().getLoad(LOAD_ID_10).getP0(), 0.01D);
}

@Test
public void testCreateWithDuplicatedLoads() throws Exception {
List<FilterEquipments> filters = getTestFilters(true);
UUID stubId = wireMockServer.stubFor(WireMock.get(WireMock.urlMatching(getPath(getNetworkUuid(), true) + "(.+,){4}.*"))
.willReturn(WireMock.ok()
.withBody(mapper.writeValueAsString(filters))
.withHeader("Content-Type", "application/json"))).getId();

super.testCreate();

wireMockUtils.verifyGetRequest(stubId, PATH, handleQueryParams(getNetworkUuid(), filters.stream().map(FilterEquipments::getFilterId).collect(Collectors.toList())), false);
}

@Override
protected Network createNetwork(UUID networkUuid) {
return NetworkCreation.createLoadNetwork(networkUuid, new NetworkFactoryImpl());
Expand Down

0 comments on commit 3762133

Please sign in to comment.