Skip to content

Commit

Permalink
Delete nodeCount attribute and related methods in IIDM node/breaker v…
Browse files Browse the repository at this point in the history
…oltage levels

Signed-off-by: RALAMBOTIANA MIORA <miora.ralambotiana@rte-france.com>

 correct code smells

Signed-off-by: RALAMBOTIANA MIORA <miora.ralambotiana@rte-france.com>

Update javadoc on VoltageLevel.cleanTopology()

Signed-off-by: RALAMBOTIANA MIORA <miora.ralambotiana@rte-france.com>

Add removeInternalConnection + Change mechanism (delete cleanTopology and auto-clean each time a connectable, a switch or an internal connection is removed)

Signed-off-by: RALAMBOTIANA MIORA <miora.ralambotiana@rte-france.com>
  • Loading branch information
miovd committed Feb 28, 2020
1 parent c84b8c6 commit df86197
Show file tree
Hide file tree
Showing 34 changed files with 230 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ private int newNode(VoltageLevel vl) {
// If a previous value was stored, new value is computed with "sum" 1
// We want a zero-based index, so -1 + ...
int numNodes = voltageLevelNumNodes.merge(vl, 1, Integer::sum);
if (vl.getNodeBreakerView().getNodeCount() < numNodes) {
// +10 to avoid calling setNodeCount too many times
vl.getNodeBreakerView().setNodeCount(numNodes + 10);
}
return numNodes - 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,20 +358,28 @@ interface InternalConnection {
}

/**
* Get the number of node.
* Get the count of used nodes (nodes attached to an equipment, a switch or an internal connection).
*
* @deprecated Depending on your use case, use {@link #getMaximumNodeIndex()} or {@link #getNodeCapacity()}.
*/
int getNodeCount();
@Deprecated
default int getNodeCount() {
throw new UnsupportedOperationException();
}

default int getMaximumNodeIndex() {
throw new UnsupportedOperationException();
}

default int getNodeCapacity() {
throw new UnsupportedOperationException();
}

/**
* Get the list of nodes.
*/
int[] getNodes();

/**
* Set the number of node.
*/
NodeBreakerView setNodeCount(int count);

/**
* Get a builder to create a new switch.
*/
Expand All @@ -397,6 +405,13 @@ interface InternalConnection {
*/
Stream<InternalConnection> getInternalConnectionStream();

/**
* Remove the internal connection between node1 and node2 if it exists.
*/
default void removeInternalConnection(int node1, int node2) {
throw new UnsupportedOperationException();
}

/**
* Get a builder to create a new breaker.
*/
Expand Down Expand Up @@ -1035,5 +1050,4 @@ default void remove() {
* @param writer a writer
*/
void exportTopology(Writer writer) throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public void remove() {
for (TerminalExt terminal : terminals) {
VoltageLevelExt vl = terminal.getVoltageLevel();
vl.detach(terminal);
vl.clean();
}
network.getListeners().notifyRemoval(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,17 +356,17 @@ static PowsyblException createNotSupportedBusBreakerTopologyException() {
private final NodeBreakerViewExt nodeBreakerView = new NodeBreakerViewExt() {

@Override
public int getNodeCount() {
public int getMaximumNodeIndex() {
throw createNotSupportedBusBreakerTopologyException();
}

@Override
public int[] getNodes() {
public int getNodeCapacity() {
throw createNotSupportedBusBreakerTopologyException();
}

@Override
public NodeBreakerView setNodeCount(int count) {
public int[] getNodes() {
throw createNotSupportedBusBreakerTopologyException();
}

Expand Down Expand Up @@ -756,11 +756,6 @@ public void detach(final TerminalExt terminal) {
terminal.setVoltageLevel(null);
}

@Override
public void clean() {
// nothing to do
}

@Override
public boolean connect(TerminalExt terminal) {
assert terminal instanceof BusTerminal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ public Switch add() {
}
SwitchImpl aSwitch = new SwitchImpl(NodeBreakerVoltageLevel.this, id, getName(), kind, open, retained, fictitious);
getNetwork().getIndex().checkAndAdd(aSwitch);
graph.addVertexIfNotPresent(node1);
graph.addVertexIfNotPresent(node2);
int e = graph.addEdge(node1, node2, aSwitch);
switches.put(id, e);
invalidateCache();
Expand Down Expand Up @@ -204,7 +206,8 @@ public void add() {
if (node2 == null) {
throw new ValidationException(NodeBreakerVoltageLevel.this, "second connection node is not set");
}

graph.addVertexIfNotPresent(node1);
graph.addVertexIfNotPresent(node2);
graph.addEdge(node1, node2, null);
invalidateCache();
}
Expand Down Expand Up @@ -562,25 +565,28 @@ CalculatedBusTopology getCalculatedBusTopology() {

private final NodeBreakerViewExt nodeBreakerView = new NodeBreakerViewExt() {

/**
* @deprecated Depending on your use case, use {@link #getMaximumNodeIndex()} or {@link #getNodeCapacity()}.
*/
@Override
@Deprecated
public int getNodeCount() {
return graph.getVertexCount();
}

@Override
public int[] getNodes() {
return graph.getVertices();
public int getMaximumNodeIndex() {
return graph.getMaximumVertexIndex();
}

@Override
public NodeBreakerView setNodeCount(int count) {
int oldCount = graph.getVertexCount();
if (count > oldCount) {
for (int i = oldCount; i < count; i++) {
graph.addVertex();
}
}
return this;
public int getNodeCapacity() {
return graph.getVertexCapacity();
}

@Override
public int[] getNodes() {
return graph.getVertices();
}

@Override
Expand Down Expand Up @@ -647,6 +653,21 @@ public int getNode2() {
});
}

@Override
public void removeInternalConnection(int node1, int node2) {
int[] internalConnectionsToBeRemoved = Arrays.stream(graph.getEdges())
.filter(e -> graph.getEdgeObject(e) == null)
.filter(e -> graph.getEdgeVertex1(e) == node1 && graph.getEdgeVertex2(e) == node2)
.toArray();
if (internalConnectionsToBeRemoved.length == 0) {
throw new PowsyblException("Internal connection not found between " + node1 + " and " + node2);
}
for (int ic : internalConnectionsToBeRemoved) {
graph.removeEdge(ic);
}
clean();
}

@Override
public SwitchAdder newBreaker() {
return new SwitchAdderImpl(SwitchKind.BREAKER);
Expand Down Expand Up @@ -879,6 +900,7 @@ public void attach(TerminalExt terminal, boolean test) {
return;
}
int node = ((NodeTerminal) terminal).getNode();
graph.addVertexIfNotPresent(node);
if (graph.getVertexObject(node) != null) {
throw new ValidationException(terminal.getConnectable(),
"an equipment (" + graph.getVertexObject(node).getConnectable().getId()
Expand Down Expand Up @@ -906,10 +928,10 @@ public void detach(TerminalExt terminal) {

// remove the link terminal -> voltage level
terminal.setVoltageLevel(null);
clean();
}

@Override
public void clean() {
private void clean() {
GraphUtil.removeIsolatedVertices(graph);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ interface BusViewExt extends BusView {
*/
void detach(TerminalExt terminal);

void clean();

boolean connect(TerminalExt terminal);

boolean disconnect(TerminalExt terminal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*/
package com.powsybl.iidm.network.impl;

import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.VoltageLevel;
import com.powsybl.iidm.network.test.NetworkTest1Factory;
import org.junit.Test;

Expand All @@ -15,47 +16,43 @@
public class NodeBreakerCleanTest {

@Test
public void cleanTopology() {
public void removeSwitchAndCleanTopology() {
Network network = NetworkTest1Factory.create();

VoltageLevelExt vl = (VoltageLevelExt) network.getVoltageLevel("voltageLevel1");
VoltageLevel vl = network.getVoltageLevel("voltageLevel1");
assertNotNull(vl);

VoltageLevel.NodeBreakerView topo = vl.getNodeBreakerView();
assertEquals(10, topo.getNodeCount());
vl.clean();

// Check useless nodes have been removed from the topology
assertEquals(6, topo.getNodeCount());
}

@Test
public void removeSwitch() {
Network network = NetworkTest1Factory.create();

VoltageLevelExt vl = (VoltageLevelExt) network.getVoltageLevel("voltageLevel1");
assertNotNull(vl);

VoltageLevel.NodeBreakerView topo = vl.getNodeBreakerView();
vl.clean();

// Check useless nodes have been removed from the topology
assertEquals(6, topo.getNodeCount());
assertEquals(6, topo.getMaximumNodeIndex());
assertEquals(7, topo.getNodeCapacity());

assertNotNull(topo.getSwitch("load1Disconnector1"));
assertNotNull(topo.getSwitch("load1Breaker1"));

// Remove the switches linked to an intermediate node (3)
topo.removeSwitch("load1Disconnector1");
topo.removeSwitch("load1Breaker1");

// Check the 2 switches and the intermediate node have been removed from the topology.
// Check the 2 switches and that the node capacity and maximum node index have not changed
assertNull(topo.getSwitch("load1Disconnector1"));
assertNull(topo.getSwitch("load1Breaker1"));
assertEquals(5, topo.getNodeCount());
assertEquals(6, topo.getMaximumNodeIndex());
assertEquals(7, topo.getNodeCapacity());

assertNull(network.getSwitch("load1Disconnector1"));
assertNull(network.getSwitch("load1Breaker1"));
assertNull(network.getIdentifiable("load1Disconnector1"));
assertNull(network.getIdentifiable("load1Breaker1"));

// Remove the switches linked to the maximum node (6)
topo.removeSwitch("generator1Disconnector1");
topo.removeSwitch("generator1Breaker1");

// Check the 2 switches and that the node capacity and maximum node index have changed
assertNull(topo.getSwitch("generator1Disconnector1"));
assertNull(topo.getSwitch("generator1Breaker1"));
assertEquals(5, topo.getMaximumNodeIndex());
assertEquals(6, topo.getNodeCapacity());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -282,19 +282,18 @@ public VoltageLevel.NodeBreakerView.SwitchAdder newDisconnector() {
// Simple delegated methods ------
// -------------------------------
@Override
public int getNodeCount() {
return getDelegate().getNodeCount();
public int getMaximumNodeIndex() {
return getDelegate().getMaximumNodeIndex();
}

@Override
public int[] getNodes() {
return getDelegate().getNodes();
public int getNodeCapacity() {
return getDelegate().getNodeCapacity();
}

@Override
public VoltageLevel.NodeBreakerView setNodeCount(final int count) {
getDelegate().setNodeCount(count);
return this;
public int[] getNodes() {
return getDelegate().getNodes();
}

@Override
Expand Down Expand Up @@ -425,7 +424,7 @@ public VscConverterStationAdder newVscConverterStation() {
@Override
public Iterable<VscConverterStation> getVscConverterStations() {
return Iterables.transform(getDelegate().getVscConverterStations(),
getIndex()::getVscConverterStation);
getIndex()::getVscConverterStation);
}

@Override
Expand All @@ -436,7 +435,7 @@ public Stream<VscConverterStation> getVscConverterStationStream() {
@Override
public Iterable<Battery> getBatteries() {
return Iterables.transform(getDelegate().getBatteries(),
getIndex()::getBattery);
getIndex()::getBattery);
}

@Override
Expand All @@ -452,7 +451,7 @@ public GeneratorAdder newGenerator() {
@Override
public Iterable<Generator> getGenerators() {
return Iterables.transform(getDelegate().getGenerators(),
getIndex()::getGenerator);
getIndex()::getGenerator);
}

@Override
Expand All @@ -468,7 +467,7 @@ public LoadAdder newLoad() {
@Override
public Iterable<Load> getLoads() {
return Iterables.transform(getDelegate().getLoads(),
getIndex()::getLoad);
getIndex()::getLoad);
}

@Override
Expand All @@ -484,7 +483,7 @@ public ShuntCompensatorAdder newShuntCompensator() {
@Override
public Iterable<ShuntCompensator> getShuntCompensators() {
return Iterables.transform(getDelegate().getShuntCompensators(),
getIndex()::getShuntCompensator);
getIndex()::getShuntCompensator);
}

@Override
Expand All @@ -500,7 +499,7 @@ public StaticVarCompensatorAdder newStaticVarCompensator() {
@Override
public Iterable<StaticVarCompensator> getStaticVarCompensators() {
return Iterables.transform(getDelegate().getStaticVarCompensators(),
getIndex()::getStaticVarCompensator);
getIndex()::getStaticVarCompensator);
}

@Override
Expand Down Expand Up @@ -541,8 +540,8 @@ public Iterable<DanglingLine> getDanglingLines() {
@Override
public Stream<DanglingLine> getDanglingLineStream() {
return getDelegate().getDanglingLineStream()
.filter(getIndex()::isMerged)
.map(getIndex()::getDanglingLine);
.filter(getIndex()::isMerged)
.map(getIndex()::getDanglingLine);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,6 @@ public void nodeBreakerViewTests() {

final VoltageLevel.NodeBreakerView nbv = voltageLevelNB.getNodeBreakerView();
assertTrue(nbv instanceof VoltageLevelAdapter.NodeBreakerViewAdapter);
nbv.setNodeCount(2);
assertEquals(2, nbv.getNodeCount());
assertEquals(2, nbv.getNodes().length);

nbv.newInternalConnection()
.setNode1(0)
Expand Down
Loading

0 comments on commit df86197

Please sign in to comment.