@@ -511,27 +511,39 @@ public int getCalculatedBusNum() {
511
511
}
512
512
513
513
private void updateBusesAttributes (double value , ObjDoubleConsumer <CalculatedBusAttributes > setValue ) {
514
- // busnum of this bus -> nodes in this bus -> busnums in the other view -> buses of the other view
514
+ // Use the busnum of this bus to get the nodes in this bus to get the
515
+ // busnums in the other view to get the buses of the other view to
516
+ // update them all. For the busbreakerview, there is only one matching bus in the busview so return early.
517
+ // We only update when isCalculatedBusesValid is true, there is no point in updating stale bus objects and
518
+ // in when isCalculatedBusesValid is not true, we may even update the wrong buses (but not much of a problem
519
+ // because they are stale objects).
520
+ // TODO add tests for updates with isCalculatedBusesValid=false
521
+ // NOTE: we don't maintain a mapping from busnum to nodes so we iterate
522
+ // all the nodes and filter but it should be ok, the number is small. TODO, is this really ok ?
515
523
VoltageLevelAttributes vlAttributes = ((VoltageLevelImpl ) getVoltageLevel ()).getResource ().getAttributes ();
516
524
Map <Integer , Integer > nodesToCalculatedBuses = isBusView
517
525
? vlAttributes .getNodeToCalculatedBusForBusView ()
518
526
: vlAttributes .getNodeToCalculatedBusForBusBreakerView ();
519
527
Map <Integer , Integer > nodesToCalculatedBusesInOtherView = isBusView
520
528
? vlAttributes .getNodeToCalculatedBusForBusBreakerView ()
521
529
: vlAttributes .getNodeToCalculatedBusForBusView ();
522
- if (!MapUtils .isEmpty (nodesToCalculatedBuses ) && !MapUtils .isEmpty (nodesToCalculatedBusesInOtherView )) {
530
+ List <CalculatedBusAttributes > calculatedBusAttributes = isBusView
531
+ ? vlAttributes .getCalculatedBusesForBusBreakerView ()
532
+ : vlAttributes .getCalculatedBusesForBusView ();
533
+ if (vlAttributes .isCalculatedBusesValid () && !CollectionUtils .isEmpty (calculatedBusAttributes )
534
+ && !MapUtils .isEmpty (nodesToCalculatedBuses ) && !MapUtils .isEmpty (nodesToCalculatedBusesInOtherView )) {
535
+ Set <Integer > seen = new HashSet <>();
523
536
for (Entry <Integer , Integer > entry : nodesToCalculatedBuses .entrySet ()) {
524
537
if (getCalculatedBusNum () == entry .getValue ()) {
525
538
int node = entry .getKey ();
526
- if (nodesToCalculatedBusesInOtherView .containsKey (node )) {
527
- int busNumInOtherView = nodesToCalculatedBusesInOtherView .get (node );
528
- List <CalculatedBusAttributes > calculatedBusAttributes = isBusView
529
- ? vlAttributes .getCalculatedBusesForBusBreakerView ()
530
- : vlAttributes .getCalculatedBusesForBusView ();
531
- if (!CollectionUtils .isEmpty (calculatedBusAttributes )) {
532
- setValue .accept (calculatedBusAttributes .get (busNumInOtherView ), value );
533
- index .updateVoltageLevelResource (voltageLevelResource , AttributeFilter .SV );
534
- }
539
+ Integer busNumInOtherView = nodesToCalculatedBusesInOtherView .get (node );
540
+ if (busNumInOtherView != null && !seen .contains (busNumInOtherView )) {
541
+ setValue .accept (calculatedBusAttributes .get (busNumInOtherView ), value );
542
+ index .updateVoltageLevelResource (voltageLevelResource , AttributeFilter .SV );
543
+ seen .add (busNumInOtherView );
544
+ }
545
+ if (!isBusView ) {
546
+ return ;
535
547
}
536
548
}
537
549
}
@@ -540,11 +552,18 @@ private void updateBusesAttributes(double value, ObjDoubleConsumer<CalculatedBus
540
552
541
553
private void updateConfiguredBuses (double newValue ,
542
554
ObjDoubleConsumer <ConfiguredBusImpl > setValue ) {
555
+ // update all the configured buses
556
+ // NOTE: we don't maintain a mapping from busnum to bus so we iterate
557
+ // all the buses and filter but it should be ok, the number is small. TODO, is this really ok ?
558
+ // We only update when isCalculatedBusesValid is true, otherwise we may update the wrong configured bus
559
+ // TODO add tests for updates with isCalculatedBusesValid=false
543
560
VoltageLevelAttributes vlAttributes = ((VoltageLevelImpl ) getVoltageLevel ()).getResource ().getAttributes ();
544
- for (Entry <String , Integer > entry : vlAttributes .getBusToCalculatedBusForBusView ().entrySet ()) {
545
- if (getCalculatedBusNum () == entry .getValue ()) {
546
- Bus bus = getVoltageLevel ().getBusBreakerView ().getBus (entry .getKey ());
547
- setValue .accept ((ConfiguredBusImpl ) bus , newValue );
561
+ if (vlAttributes .isCalculatedBusesValid ()) {
562
+ for (Entry <String , Integer > entry : vlAttributes .getBusToCalculatedBusForBusView ().entrySet ()) {
563
+ if (getCalculatedBusNum () == entry .getValue ()) {
564
+ ConfiguredBusImpl bus = index .getConfiguredBus (entry .getKey ()).orElseThrow (IllegalStateException ::new );
565
+ setValue .accept (bus , newValue );
566
+ }
548
567
}
549
568
}
550
569
}
0 commit comments