-
Notifications
You must be signed in to change notification settings - Fork 54
Refactor graph traversal methods to use new computeTraversalPartitions API
#3753
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
988efdd
30cc260
10555c9
e522228
caa146e
b54a1c6
8eb3d6c
c562c12
c9cf18d
df1cf80
1b58640
c5a2ef6
7cd03d4
16a1dd0
e3e8b9e
037e516
b173fb9
201fb81
f73819e
583b71a
f85d061
dd565cb
9557084
8f96dea
b6b5556
6c094d8
e7cbba1
8f84c9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -259,41 +259,6 @@ protected BusChecker getBusChecker() { | |
| return CALCULATED_BUS_CHECKER; | ||
| } | ||
|
|
||
| private void traverse(int n, boolean[] encountered, Predicate<SwitchImpl> terminate, Map<String, CalculatedBus> id2bus, CalculatedBus[] node2bus) { | ||
| if (!encountered[n]) { | ||
| final TIntArrayList nodes = new TIntArrayList(1); | ||
| nodes.add(n); | ||
| Traverser traverser = (n1, e, n2) -> { | ||
| SwitchImpl aSwitch = graph.getEdgeObject(e); | ||
| if (aSwitch != null && terminate.test(aSwitch)) { | ||
| return TraverseResult.TERMINATE_PATH; | ||
| } | ||
|
|
||
| if (!encountered[n2]) { | ||
| // We need to check this as the traverser might be called twice with the same n2 but with different edges. | ||
| // Note that the "encountered" array is used and maintained inside graph::traverse method, hence we should not update it. | ||
| nodes.add(n2); | ||
| } | ||
| return TraverseResult.CONTINUE; | ||
| }; | ||
| graph.traverse(n, TraversalType.DEPTH_FIRST, traverser, encountered); | ||
|
|
||
| // check that the component is a bus | ||
| String busId = Identifiables.getUniqueId(NAMING_STRATEGY.getId(voltageLevel, nodes), getNetwork().getIndex()::contains); | ||
| CopyOnWriteArrayList<NodeTerminal> terminals = new CopyOnWriteArrayList<>(); | ||
| for (int i = 0; i < nodes.size(); i++) { | ||
| int n2 = nodes.getQuick(i); | ||
| NodeTerminal terminal2 = graph.getVertexObject(n2); | ||
| if (terminal2 != null) { | ||
| terminals.add(terminal2); | ||
| } | ||
| } | ||
| if (getBusChecker().isValid(graph, nodes, terminals)) { | ||
| addBus(nodes, id2bus, node2bus, busId, terminals); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void addBus(TIntArrayList nodes, Map<String, CalculatedBus> id2bus, CalculatedBus[] node2bus, | ||
| String busId, CopyOnWriteArrayList<NodeTerminal> terminals) { | ||
| String busName = NAMING_STRATEGY.getName(voltageLevel, nodes); | ||
|
|
@@ -312,10 +277,49 @@ protected void updateCache(final Predicate<SwitchImpl> terminate) { | |
| LOGGER.trace("Update bus topology of voltage level {}", voltageLevel.getId()); | ||
| Map<String, CalculatedBus> id2bus = new LinkedHashMap<>(); | ||
| CalculatedBus[] node2bus = new CalculatedBus[graph.getVertexCapacity()]; | ||
| boolean[] encountered = new boolean[graph.getVertexCapacity()]; | ||
| for (int v : graph.getVertices()) { | ||
| traverse(v, encountered, terminate, id2bus, node2bus); | ||
| } | ||
|
|
||
| graph.getConnectedComponents( | ||
| sw -> !terminate.test(sw), | ||
| new UndirectedGraph.ConnectedComponentCollector<TIntArrayList, NodeTerminal>() { | ||
| @Override | ||
| public TIntArrayList createComponent() { | ||
| return new TIntArrayList(1); | ||
| } | ||
|
|
||
| @Override | ||
| public void addVertex(TIntArrayList nodes, int nodeIndex, NodeTerminal terminal) { | ||
| nodes.add(nodeIndex); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isComponentValid(TIntArrayList nodes) { | ||
| CopyOnWriteArrayList<NodeTerminal> terminals = new CopyOnWriteArrayList<>(); | ||
| for (int i = 0; i < nodes.size(); i++) { | ||
| NodeTerminal terminal = graph.getVertexObject(nodes.getQuick(i)); | ||
| if (terminal != null) { | ||
| terminals.add(terminal); | ||
| } | ||
| } | ||
| return getBusChecker().isValid(graph, nodes, terminals); | ||
| } | ||
| } | ||
| ).forEach(nodes -> { | ||
| String busId = Identifiables.getUniqueId( | ||
| NAMING_STRATEGY.getId(voltageLevel, nodes), | ||
| getNetwork().getIndex()::contains | ||
| ); | ||
| CopyOnWriteArrayList<NodeTerminal> terminals = new CopyOnWriteArrayList<>(); | ||
|
||
| for (int i = 0; i < nodes.size(); i++) { | ||
| int nodeIndex = nodes.getQuick(i); | ||
| NodeTerminal terminal = graph.getVertexObject(nodeIndex); | ||
| if (terminal != null) { | ||
| terminals.add(terminal); | ||
| } | ||
| } | ||
|
|
||
| addBus(nodes, id2bus, node2bus, busId, terminals); | ||
| }); | ||
|
|
||
| busCache = new BusCache(node2bus, id2bus); | ||
| LOGGER.trace("Found buses {}", id2bus.values()); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -579,6 +579,43 @@ public boolean traverse(int v, TraversalType traversalType, Traverser traverser, | |
| return keepGoing; | ||
| } | ||
|
|
||
| @Override | ||
| public <C> List<C> getConnectedComponents(Predicate<E> isTraversable, ConnectedComponentCollector<C, V> collector) { | ||
| Objects.requireNonNull(isTraversable); | ||
| Objects.requireNonNull(collector); | ||
|
|
||
| List<C> components = new ArrayList<>(); | ||
| boolean[] encountered = new boolean[vertices.size()]; | ||
|
|
||
| for (int v = 0; v < vertices.size(); v++) { | ||
| Vertex<V> vertex = vertices.get(v); | ||
clementleclercRTE marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (vertex != null && !encountered[v]) { | ||
| C component = collector.createComponent(); | ||
|
|
||
| collector.addVertex(component, v, vertex.getObject()); | ||
|
|
||
| traverse(v, TraversalType.BREADTH_FIRST, (v1, e, v2) -> { | ||
flo-dup marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| E edgeObject = edges.get(e).getObject(); | ||
| if (edgeObject == null || isTraversable.test(edgeObject)) { | ||
clementleclercRTE marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!encountered[v2]) { | ||
| Vertex<V> destVertex = vertices.get(v2); | ||
| if (destVertex != null) { | ||
clementleclercRTE marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| collector.addVertex(component, v2, destVertex.getObject()); | ||
| } | ||
| } | ||
| return TraverseResult.CONTINUE; | ||
| } | ||
| return TraverseResult.TERMINATE_PATH; | ||
| }, encountered); | ||
|
|
||
| if (collector.isComponentValid(component)) { | ||
| components.add(component); | ||
| } | ||
| } | ||
| } | ||
| return components; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean traverse(int v, TraversalType traversalType, Traverser traverser) { | ||
| boolean[] encountered = new boolean[vertices.size()]; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.