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

Log duplicate static id #270

Merged
merged 5 commits into from
Sep 28, 2023
Merged
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 @@ -57,12 +57,12 @@ public DynaWaltzContext(Network network, String workingVariantId, List<BlackBoxM
List<Curve> curves, DynamicSimulationParameters parameters, DynaWaltzParameters dynaWaltzParameters) {
this.network = Objects.requireNonNull(network);
this.workingVariantId = Objects.requireNonNull(workingVariantId);
this.dynamicModels = Objects.requireNonNull(dynamicModels);
this.dynamicModels = checkDuplicateStaticId(Objects.requireNonNull(dynamicModels));
this.eventModels = checkEventModelIdUniqueness(Objects.requireNonNull(eventModels));
this.staticIdBlackBoxModelMap = getInputBlackBoxDynamicModelStream()
.filter(EquipmentBlackBoxModel.class::isInstance)
.map(EquipmentBlackBoxModel.class::cast)
.collect(Collectors.toMap(EquipmentBlackBoxModel::getStaticId, Function.identity(), this::mergeDuplicateStaticId, LinkedHashMap::new));
.collect(Collectors.toMap(EquipmentBlackBoxModel::getStaticId, Function.identity()));
this.curves = Objects.requireNonNull(curves);
this.parameters = Objects.requireNonNull(parameters);
this.dynaWaltzParameters = Objects.requireNonNull(dynaWaltzParameters);
Expand Down Expand Up @@ -163,8 +163,17 @@ public <T extends Model> T getPureDynamicModel(String dynamicId, Class<T> connec
}
}

private EquipmentBlackBoxModel mergeDuplicateStaticId(EquipmentBlackBoxModel bbm1, EquipmentBlackBoxModel bbm2) {
throw new PowsyblException("Duplicate staticId: " + bbm1.getStaticId());
private static List<BlackBoxModel> checkDuplicateStaticId(List<BlackBoxModel> dynamicModels) {
Set<String> staticIds = new HashSet<>();
return dynamicModels.stream()
.filter(bbm -> {
if (bbm instanceof EquipmentBlackBoxModel eBbm && !staticIds.add(eBbm.getStaticId())) {
LOGGER.warn("Duplicate static id found: {} -> dynamic model {} {} will be skipped", eBbm.getStaticId(), eBbm.getLib(), eBbm.getDynamicModelId());
return false;
}
return true;
})
.collect(Collectors.toList());
}

private static List<BlackBoxModel> checkEventModelIdUniqueness(List<BlackBoxModel> eventModels) {
Expand Down Expand Up @@ -230,7 +239,7 @@ public boolean isWithoutBlackBoxDynamicModel(Identifiable<?> equipment) {
}

private Stream<BlackBoxModel> getInputBlackBoxDynamicModelStream() {
//Doesn't include the OmegaRef, it only concerns the DynamicModels provided by the user
// Doesn't include the OmegaRef, it only concerns the DynamicModels provided by the user
return dynamicModels.stream();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ void duplicateStaticId() {
}
});
String workingVariantId = network.getVariantManager().getWorkingVariantId();
Exception e = assertThrows(PowsyblException.class, () -> new DynaWaltzContext(network, workingVariantId, dynamicModels, eventModels, curves, null, null));
assertEquals("Duplicate staticId: GEN5", e.getMessage());
DynaWaltzContext context = new DynaWaltzContext(network, workingVariantId, dynamicModels, eventModels, curves, DynamicSimulationParameters.load(), DynaWaltzParameters.load());
assertEquals(1, context.getBlackBoxDynamicModels().size());
}

@Test
Expand Down
Loading