Skip to content

Commit 276e553

Browse files
Add missing notifications on extensions update and creation (#511)
Signed-off-by: Franck LECUYER <franck.lecuyer@rte-france.com>
1 parent 49f657d commit 276e553

File tree

79 files changed

+783
-394
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+783
-394
lines changed

network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/AbstractIdentifiableImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ public void updateResource(Consumer<Resource<D>> modifier, AttributeFilter attri
5353
index.updateResource(resource, attributeFilter);
5454
}
5555

56+
public void notifyExtensionCreation(Extension<?> extension) {
57+
getIndex().notifyExtensionCreation(extension);
58+
}
59+
60+
public void updateResourceExtension(Extension<?> extension, Consumer<Resource<D>> modifier, String attribute, Object oldValue, Object newValue) {
61+
modifier.accept(resource);
62+
index.updateResource(resource, null);
63+
String variantId = getNetwork().getVariantManager().getWorkingVariantId();
64+
getIndex().notifyExtensionUpdate(extension, attribute, variantId, oldValue, newValue);
65+
}
66+
5667
public Resource<D> getNullableResource() {
5768
return resource;
5869
}

network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/BusbarSectionPositionAdderImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
package com.powsybl.network.store.iidm.impl;
88

9-
import com.powsybl.commons.extensions.AbstractExtensionAdder;
9+
import com.powsybl.network.store.iidm.impl.extensions.AbstractIidmExtensionAdder;
1010
import com.powsybl.iidm.network.BusbarSection;
1111
import com.powsybl.iidm.network.extensions.BusbarSectionPosition;
1212
import com.powsybl.iidm.network.extensions.BusbarSectionPositionAdder;
@@ -15,7 +15,7 @@
1515
/**
1616
* @author Jon Harper <jon.harper at rte-france.com>
1717
*/
18-
public class BusbarSectionPositionAdderImpl extends AbstractExtensionAdder<BusbarSection, BusbarSectionPosition>
18+
public class BusbarSectionPositionAdderImpl extends AbstractIidmExtensionAdder<BusbarSection, BusbarSectionPosition>
1919
implements BusbarSectionPositionAdder {
2020

2121
private int busbarIndex = -1;

network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/BusbarSectionPositionImpl.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ private BusbarSectionPositionAttributes getPositionAttributes(Resource<BusbarSec
5252

5353
@Override
5454
public BusbarSectionPosition setBusbarIndex(int busbarIndex) {
55-
getBusbarSection().updateResource(res -> getPositionAttributes(res).setBusbarIndex(checkIndex(getBusbarSection(), busbarIndex, "Busbar")));
55+
int oldValue = getBusbarIndex();
56+
if (oldValue != busbarIndex) {
57+
getBusbarSection().updateResourceExtension(this, res -> getPositionAttributes(res).setBusbarIndex(checkIndex(getBusbarSection(), busbarIndex, "Busbar")), "Busbar index", oldValue, busbarIndex);
58+
}
5659
return this;
5760
}
5861

@@ -63,7 +66,10 @@ public int getSectionIndex() {
6366

6467
@Override
6568
public BusbarSectionPosition setSectionIndex(int sectionIndex) {
66-
getBusbarSection().updateResource(res -> getPositionAttributes(res).setSectionIndex(checkIndex(getBusbarSection(), sectionIndex, "Section")));
69+
int oldValue = getSectionIndex();
70+
if (oldValue != sectionIndex) {
71+
getBusbarSection().updateResourceExtension(this, res -> getPositionAttributes(res).setSectionIndex(checkIndex(getBusbarSection(), sectionIndex, "Section")), "Section index", oldValue, sectionIndex);
72+
}
6773
return this;
6874
}
6975
}

network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/ConnectablePositionAdderImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
*/
77
package com.powsybl.network.store.iidm.impl;
88

9-
import com.powsybl.commons.extensions.AbstractExtensionAdder;
109
import com.powsybl.iidm.network.Connectable;
1110
import com.powsybl.iidm.network.extensions.ConnectablePosition;
1211
import com.powsybl.iidm.network.extensions.ConnectablePositionAdder;
12+
import com.powsybl.network.store.iidm.impl.extensions.AbstractIidmExtensionAdder;
1313
import com.powsybl.network.store.model.ConnectablePositionAttributes;
1414

1515
/**
1616
* @author Jon Harper <jon.harper at rte-france.com>
1717
*/
1818
public class ConnectablePositionAdderImpl<C extends Connectable<C>>
19-
extends AbstractExtensionAdder<C, ConnectablePosition<C>> implements ConnectablePositionAdder<C> {
19+
extends AbstractIidmExtensionAdder<C, ConnectablePosition<C>> implements ConnectablePositionAdder<C> {
2020

2121
private FeederAdderImpl feederAdder;
2222
private FeederAdderImpl feederAdder1;

network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/ConnectablePositionImpl.java

Lines changed: 62 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import com.powsybl.commons.PowsyblException;
1010
import com.powsybl.commons.extensions.AbstractExtension;
11+
import com.powsybl.commons.extensions.Extension;
1112
import com.powsybl.iidm.network.Connectable;
1213
import com.powsybl.iidm.network.ThreeSides;
1314
import com.powsybl.iidm.network.extensions.ConnectablePosition;
@@ -32,8 +33,10 @@ public class FeederImpl implements Feeder {
3233
private static final String UNEXPECTED_EXTENDABLE = "Unexpected extendable instance: ";
3334
private final ThreeSides side;
3435
private final Function<Connectable<C>, ConnectablePositionAttributes> getter;
36+
private final ConnectablePosition<C> connectablePosition;
3537

36-
public FeederImpl(Function<Connectable<C>, ConnectablePositionAttributes> getter, ThreeSides side) {
38+
public FeederImpl(ConnectablePosition<C> connectablePosition, Function<Connectable<C>, ConnectablePositionAttributes> getter, ThreeSides side) {
39+
this.connectablePosition = connectablePosition;
3740
this.getter = Objects.requireNonNull(getter);
3841
this.side = side;
3942
}
@@ -56,14 +59,11 @@ public Feeder setName(String name) {
5659
String oldValue = getAttributes().getLabel();
5760
if (!Objects.equals(oldValue, name)) {
5861
if (getExtendable() instanceof AbstractInjectionImpl<?, ?> injection) {
59-
updateInjectionLabelResource(injection, name);
60-
notifyUpdate(injection, LABEL, oldValue, name);
62+
updateInjectionLabelResource(connectablePosition, injection, name, oldValue, name);
6163
} else if (getExtendable() instanceof AbstractBranchImpl<?, ?> branch) {
62-
updateBranchLabelResource(branch, name);
63-
notifyUpdate(branch, LABEL, oldValue, name);
64+
updateBranchLabelResource(connectablePosition, branch, name, oldValue, name);
6465
} else if (getExtendable() instanceof ThreeWindingsTransformerImpl windingsTransformer) {
65-
updateTransformerLabelResource(windingsTransformer, name);
66-
notifyUpdate(windingsTransformer, LABEL, oldValue, name);
66+
updateTransformerLabelResource(connectablePosition, windingsTransformer, name, oldValue, name);
6767
} else {
6868
throw new AssertionError(UNEXPECTED_EXTENDABLE + getExtendable().getClass());
6969
}
@@ -87,15 +87,11 @@ public Feeder setOrder(int order) {
8787
Integer oldValue = getAttributes().getOrder();
8888
if (!Objects.equals(oldValue, order)) {
8989
if (getExtendable() instanceof AbstractInjectionImpl<?, ?> injection) {
90-
updateInjectionOrderResource(injection, order);
91-
notifyUpdate(injection, ORDER, oldValue, order);
92-
90+
updateInjectionOrderResource(connectablePosition, injection, order, oldValue, order);
9391
} else if (getExtendable() instanceof AbstractBranchImpl<?, ?> branch) {
94-
updateBranchOrderResource(branch, order);
95-
notifyUpdate(branch, ORDER, oldValue, order);
92+
updateBranchOrderResource(connectablePosition, branch, order, oldValue, order);
9693
} else if (getExtendable() instanceof ThreeWindingsTransformerImpl windingsTransformer) {
97-
updateTransformerOrderResource(windingsTransformer, order);
98-
notifyUpdate(windingsTransformer, ORDER, oldValue, order);
94+
updateTransformerOrderResource(connectablePosition, windingsTransformer, order, oldValue, order);
9995
} else {
10096
throw new AssertionError(UNEXPECTED_EXTENDABLE + getExtendable().getClass());
10197
}
@@ -113,43 +109,42 @@ public Feeder setDirection(Direction direction) {
113109
Direction oldValue = getAttributes().getDirection();
114110
if (!Objects.equals(oldValue, direction)) {
115111
if (getExtendable() instanceof AbstractInjectionImpl<?, ?> injection) {
116-
updateInjectionDirectionResource(injection, direction);
117-
notifyUpdate(injection, DIRECTION, oldValue, direction);
112+
updateInjectionDirectionResource(connectablePosition, injection, direction, oldValue, direction);
118113
} else if (getExtendable() instanceof AbstractBranchImpl<?, ?> branch) {
119-
updateBranchDirectionResource(branch, direction);
120-
notifyUpdate(branch, DIRECTION, oldValue, direction);
114+
updateBranchDirectionResource(connectablePosition, branch, direction, oldValue, direction);
121115
} else if (getExtendable() instanceof ThreeWindingsTransformerImpl windingsTransformer) {
122-
updateTransformerDirectionResource(windingsTransformer, direction);
123-
notifyUpdate(windingsTransformer, DIRECTION, oldValue, direction);
116+
updateTransformerDirectionResource(connectablePosition, windingsTransformer, direction, oldValue, direction);
124117
} else {
125118
throw new AssertionError(UNEXPECTED_EXTENDABLE + getExtendable().getClass());
126119
}
127120
}
128121
return this;
129122
}
130123

131-
private void updateInjectionLabelResource(AbstractInjectionImpl<?, ?> injection, String name) {
132-
injection.updateResource(res -> res.getAttributes().getPosition().setLabel(Objects.requireNonNull(name)));
124+
private void updateInjectionLabelResource(Extension<?> extension, AbstractInjectionImpl<?, ?> injection, String name, Object oldValue, Object newValue) {
125+
injection.updateResourceExtension(extension,
126+
res -> res.getAttributes().getPosition().setLabel(Objects.requireNonNull(name)),
127+
LABEL, oldValue, newValue);
133128
}
134129

135-
private void updateBranchLabelResource(AbstractBranchImpl<?, ?> branch, String name) {
130+
private void updateBranchLabelResource(Extension<?> extension, AbstractBranchImpl<?, ?> branch, String name, Object oldValue, Object newValue) {
136131
Objects.requireNonNull(name);
137132
ThreeSides sides = Objects.requireNonNull(getSide());
138-
branch.updateResource(res -> {
133+
branch.updateResourceExtension(extension, res -> {
139134
if (sides == ThreeSides.ONE) {
140135
res.getAttributes().getPosition1().setLabel(name);
141136
} else if (sides == ThreeSides.TWO) {
142137
res.getAttributes().getPosition2().setLabel(name);
143138
} else {
144139
throw new PowsyblException(NOT_SUPPORTED);
145140
}
146-
});
141+
}, LABEL, oldValue, newValue);
147142
}
148143

149-
private void updateTransformerLabelResource(ThreeWindingsTransformerImpl windingsTransformer, String name) {
144+
private void updateTransformerLabelResource(Extension<?> extension, ThreeWindingsTransformerImpl windingsTransformer, String name, Object oldValue, Object newValue) {
150145
Objects.requireNonNull(name);
151146
ThreeSides sides = Objects.requireNonNull(getSide());
152-
windingsTransformer.updateResource(res -> {
147+
windingsTransformer.updateResourceExtension(extension, res -> {
153148
if (sides == ThreeSides.ONE) {
154149
res.getAttributes().getPosition1().setLabel(name);
155150
} else if (sides == ThreeSides.TWO) {
@@ -159,64 +154,69 @@ private void updateTransformerLabelResource(ThreeWindingsTransformerImpl winding
159154
} else {
160155
throw new PowsyblException(NOT_SUPPORTED);
161156
}
162-
});
157+
}, LABEL, oldValue, newValue);
163158
}
164159

165-
private void updateInjectionOrderResource(AbstractInjectionImpl<?, ?> injection, int order) {
166-
injection.updateResource(res -> res.getAttributes().getPosition().setOrder(order));
160+
private void updateInjectionOrderResource(Extension<?> extension, AbstractInjectionImpl<?, ?> injection, int order, Object oldValue, Object newValue) {
161+
injection.updateResourceExtension(extension,
162+
res -> res.getAttributes().getPosition().setOrder(order),
163+
ORDER, oldValue, newValue);
167164
}
168165

169-
private void updateBranchOrderResource(AbstractBranchImpl<?, ?> branch, int order) {
166+
private void updateBranchOrderResource(Extension<?> extension, AbstractBranchImpl<?, ?> branch, int order, Object oldValue, Object newValue) {
170167
ThreeSides sides = Objects.requireNonNull(getSide());
171-
branch.updateResource(res -> {
172-
if (sides == ThreeSides.ONE) {
173-
res.getAttributes().getPosition1().setOrder(order);
174-
} else if (sides == ThreeSides.TWO) {
175-
res.getAttributes().getPosition2().setOrder(order);
176-
} else {
177-
throw new PowsyblException(NOT_SUPPORTED);
178-
}
179-
});
168+
branch.updateResourceExtension(extension,
169+
res -> {
170+
if (sides == ThreeSides.ONE) {
171+
res.getAttributes().getPosition1().setOrder(order);
172+
} else if (sides == ThreeSides.TWO) {
173+
res.getAttributes().getPosition2().setOrder(order);
174+
} else {
175+
throw new PowsyblException(NOT_SUPPORTED);
176+
}
177+
}, ORDER, oldValue, newValue);
180178
}
181179

182-
private void updateTransformerOrderResource(ThreeWindingsTransformerImpl windingsTransformer, int order) {
180+
private void updateTransformerOrderResource(Extension<?> extension, ThreeWindingsTransformerImpl windingsTransformer, int order, Object oldValue, Object newValue) {
183181
ThreeSides sides = Objects.requireNonNull(getSide());
184-
windingsTransformer.updateResource(res -> {
185-
if (sides == ThreeSides.ONE) {
186-
res.getAttributes().getPosition1().setOrder(order);
187-
} else if (sides == ThreeSides.TWO) {
188-
res.getAttributes().getPosition2().setOrder(order);
189-
} else if (sides == ThreeSides.THREE) {
190-
res.getAttributes().getPosition3().setOrder(order);
191-
} else {
192-
throw new PowsyblException(NOT_SUPPORTED);
193-
}
194-
});
182+
windingsTransformer.updateResourceExtension(extension,
183+
res -> {
184+
if (sides == ThreeSides.ONE) {
185+
res.getAttributes().getPosition1().setOrder(order);
186+
} else if (sides == ThreeSides.TWO) {
187+
res.getAttributes().getPosition2().setOrder(order);
188+
} else if (sides == ThreeSides.THREE) {
189+
res.getAttributes().getPosition3().setOrder(order);
190+
} else {
191+
throw new PowsyblException(NOT_SUPPORTED);
192+
}
193+
}, ORDER, oldValue, newValue);
195194
}
196195

197-
private void updateInjectionDirectionResource(AbstractInjectionImpl<?, ?> injection, Direction direction) {
198-
injection.updateResource(res -> res.getAttributes().getPosition()
199-
.setDirection(Direction.valueOf(Objects.requireNonNull(direction).name())));
196+
private void updateInjectionDirectionResource(Extension<?> extension, AbstractInjectionImpl<?, ?> injection, Direction direction, Object oldValue, Object newValue) {
197+
injection.updateResourceExtension(extension, res -> res.getAttributes().getPosition()
198+
.setDirection(Direction.valueOf(Objects.requireNonNull(direction).name())),
199+
DIRECTION, oldValue, newValue);
200200
}
201201

202-
private void updateBranchDirectionResource(AbstractBranchImpl<?, ?> branch, Direction direction) {
202+
private void updateBranchDirectionResource(Extension<?> extension, AbstractBranchImpl<?, ?> branch, Direction direction, Object oldValue, Object newValue) {
203203
Objects.requireNonNull(direction);
204204
ThreeSides sides = Objects.requireNonNull(getSide());
205-
branch.updateResource(res -> {
205+
branch.updateResourceExtension(extension, res -> {
206206
if (sides == ThreeSides.ONE) {
207207
res.getAttributes().getPosition1().setDirection(direction);
208208
} else if (sides == ThreeSides.TWO) {
209209
res.getAttributes().getPosition2().setDirection(direction);
210210
} else {
211211
throw new PowsyblException(NOT_SUPPORTED);
212212
}
213-
});
213+
}, DIRECTION, oldValue, newValue);
214214
}
215215

216-
private void updateTransformerDirectionResource(ThreeWindingsTransformerImpl windingsTransformer, Direction direction) {
216+
private void updateTransformerDirectionResource(Extension<?> extension, ThreeWindingsTransformerImpl windingsTransformer, Direction direction, Object oldValue, Object newValue) {
217217
Objects.requireNonNull(direction);
218218
ThreeSides sides = Objects.requireNonNull(getSide());
219-
windingsTransformer.updateResource(res -> {
219+
windingsTransformer.updateResourceExtension(extension, res -> {
220220
if (sides == ThreeSides.ONE) {
221221
res.getAttributes().getPosition1().setDirection(direction);
222222
} else if (sides == ThreeSides.TWO) {
@@ -226,12 +226,7 @@ private void updateTransformerDirectionResource(ThreeWindingsTransformerImpl win
226226
} else {
227227
throw new PowsyblException(NOT_SUPPORTED);
228228
}
229-
});
230-
}
231-
232-
private void notifyUpdate(AbstractConnectableImpl<?, ?> connectable, String attribute, Object oldValue, Object newValue) {
233-
String variantId = connectable.getNetwork().getVariantManager().getWorkingVariantId();
234-
connectable.getNetwork().getIndex().notifyUpdate(getExtendable(), attribute, variantId, oldValue, newValue);
229+
}, DIRECTION, oldValue, newValue);
235230
}
236231
}
237232

@@ -254,7 +249,7 @@ public ConnectablePositionImpl(C connectable,
254249

255250
private FeederImpl getFeeder(Function<Connectable<C>, ConnectablePositionAttributes> positionAttributesGetter, ThreeSides side) {
256251
return (positionAttributesGetter != null && positionAttributesGetter.apply(getExtendable()) != null) ?
257-
new FeederImpl(positionAttributesGetter, side) : null;
252+
new FeederImpl(this, positionAttributesGetter, side) : null;
258253
}
259254

260255
@Override

network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/NetworkObjectIndex.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,16 @@ public void notifyExtensionUpdate(Extension<?> extension, String attribute, Stri
527527
}
528528
}
529529

530+
public void notifyExtensionCreation(Extension<?> extension) {
531+
for (NetworkListener listener : network.getListeners()) {
532+
try {
533+
listener.onExtensionCreation(extension);
534+
} catch (Exception t) {
535+
LOGGER.error(t.toString(), t);
536+
}
537+
}
538+
}
539+
530540
public void notifyUpdate(Identifiable<?> identifiable, String attribute, String variantId, Object oldValue, Object newValue) {
531541
if (!Objects.equals(oldValue, newValue)) {
532542
for (NetworkListener listener : network.getListeners()) {

network-store-iidm-impl/src/main/java/com/powsybl/network/store/iidm/impl/extensions/AbstractIidmExtensionAdder.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
import com.powsybl.commons.extensions.AbstractExtensionAdder;
1111
import com.powsybl.commons.extensions.Extension;
1212
import com.powsybl.iidm.network.Identifiable;
13-
import com.powsybl.iidm.network.NetworkListener;
14-
import com.powsybl.network.store.iidm.impl.NetworkImpl;
15-
16-
import java.util.List;
13+
import com.powsybl.network.store.iidm.impl.AbstractIdentifiableImpl;
1714

1815
/**
1916
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
@@ -27,10 +24,7 @@ protected AbstractIidmExtensionAdder(I identifiable) {
2724
@Override
2825
public E add() {
2926
E extension = super.add();
30-
List<NetworkListener> listeners = ((NetworkImpl) extendable.getNetwork()).getListeners();
31-
for (NetworkListener listener : listeners) {
32-
listener.onExtensionCreation(extension);
33-
}
27+
((AbstractIdentifiableImpl<?, ?>) extendable).notifyExtensionCreation(extension);
3428
return extension;
3529
}
3630
}

0 commit comments

Comments
 (0)