Skip to content

Commit

Permalink
Merge pull request #62 from itesla/node_breaker_connect
Browse files Browse the repository at this point in the history
Fix TODO exceptions + switch status change refactoring
  • Loading branch information
geofjamg committed Sep 21, 2016
2 parents 0f33c00 + ad874aa commit 5278cca
Show file tree
Hide file tree
Showing 15 changed files with 368 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
package eu.itesla_project.graph;

import com.google.common.base.Function;
import gnu.trove.list.array.TIntArrayList;

import java.io.PrintStream;
import java.util.List;

Expand Down Expand Up @@ -56,6 +58,8 @@ public interface UndirectedGraph<V, E> {

void traverse(int v, Traverser<E> traverser);

List<TIntArrayList> findAllPaths(int from, Function<V, Boolean> pathComplete, Function<E, Boolean> pathCanceled);

void addListener(UndirectedGraphListener l);

void removeListener(UndirectedGraphListener l);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
import gnu.trove.list.linked.TIntLinkedList;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
Expand Down Expand Up @@ -353,6 +350,66 @@ public void traverse(int v, Traverser<E> traverser) {
traverse(v, traverser, encountered);
}

@Override
public List<TIntArrayList> findAllPaths(int from, Function<V, Boolean> pathComplete, Function<E, Boolean> pathCanceled) {
Objects.requireNonNull(pathComplete);
Objects.requireNonNull(pathCanceled);
List<TIntArrayList> paths = new ArrayList<>();
BitSet encountered = new BitSet(vertices.size());
TIntArrayList path = new TIntArrayList(1);
findAllPaths(from, pathComplete, pathCanceled, path, encountered, paths);
// sort paths by size
paths.sort((o1, o2) -> o1.size() - o2.size());
return paths;
}

private boolean findAllPaths(int e, int v1or2, Function<V, Boolean> pathComplete, Function<E, Boolean> pathCanceled,
TIntArrayList adjacentEdges, int i, TIntArrayList path, BitSet encountered,
List<TIntArrayList> paths) {
Vertex<V> obj1or2 = vertices.get(v1or2);
if (pathComplete.apply(obj1or2.getObject())) {
paths.add(path);
return true;
} else {
path.add(e);
if (i < adjacentEdges.size () - 1){
TIntArrayList path2 = new TIntArrayList(path);
BitSet encountered2 = new BitSet(vertices.size());
encountered2.or(encountered);
findAllPaths(v1or2, pathComplete, pathCanceled, path2, encountered2, paths);
} else{
findAllPaths(v1or2, pathComplete, pathCanceled, path, encountered, paths);
}
return false;
}
}

private void findAllPaths(int v, Function<V, Boolean> pathComplete, Function<E, Boolean> pathCanceled,
TIntArrayList path, BitSet encountered, List<TIntArrayList> paths) {
checkVertex(v);
encountered.set(v, true);
TIntArrayList[] adjacencyList = getAdjacencyList();
TIntArrayList adjacentEdges = adjacencyList[v];
for (int i = 0; i < adjacentEdges.size(); i++) {
int e = adjacentEdges.getQuick(i);
Edge<E> edge = edges.get(e);
if (pathCanceled.apply(edge.getObject())) {
return;
}
int v1 = edge.getV1();
int v2 = edge.getV2();
if (!encountered.get(v1)) {
if (findAllPaths(e, v1, pathComplete, pathCanceled, adjacentEdges, i, path, encountered, paths)) {
return;
}
} else if (!encountered.get(v2)) {
if (findAllPaths(e, v2, pathComplete, pathCanceled, adjacentEdges, i, path, encountered, paths)) {
return;
}
}
}
}

@Override
public void addListener(UndirectedGraphListener l) {
listeners.add(l);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ public interface Switch extends Identifiable<Switch> {
*/
boolean isOpen();

/**
* Change the switch status.
* <p>
* Depends on the working state.
* @param open the new switch status
* @see StateManager
*/
void setOpen(boolean open);

/**
* Get the retain status of the switch. A retained switch is a switch that
* will be part of the bus/breaker topology.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,18 @@ public static interface BusView {
/**
* Try to connect the terminal.
* <p>Depends on the working state.
* @return true if terminal has been connected, false otherwise
* @see StateManager
*/
void connect();
boolean connect();

/**
* Disconnect the terminal.
* <p>Depends on the working state.
* @return true if terminal has been disconnected, false otherwise
* @see StateManager
*/
void disconnect();
boolean disconnect();

/**
* Test if the terminal is connected.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,26 +376,6 @@ public interface InternalConnectionAdder extends IdentifiableAdder<InternalConne
*/
SwitchAdder newDisconnector();

/**
* Open a switch.
* <p>
* Depends on the working state.
* @param switchId the id of the switch to open
* @throws ITeslaException if switch is not found
* @see StateManager
*/
NodeBreakerView openSwitch(String switchId);

/**
* Close a switch.
* <p>
* Depends on the working state.
* @param switchId the id of the switch to close
* @throws ITeslaException if switch is not found
* @see StateManager
*/
NodeBreakerView closeSwitch(String switchId);

/**
* Get the first node to which a switch is connected.
*
Expand Down Expand Up @@ -506,26 +486,6 @@ public interface SwitchAdder extends IdentifiableAdder<SwitchAdder> {
*/
void removeAllBuses();

/**
* Open a switch.
* <p>
* Depends on the working state.
* @param switchId the id of the switch to open
* @throws ITeslaException if switch is not found
* @see StateManager
*/
BusBreakerView openSwitch(String switchId);

/**
* Close a switch.
* <p>
* Depends on the working state.
* @param switchId the id of the switch to close
* @throws ITeslaException if switch is not found
* @see StateManager
*/
BusBreakerView closeSwitch(String switchId);

/**
* Get switches.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ public float getI() {
}

@Override
public void connect() {
voltageLevel.connect(this);
public boolean connect() {
return voltageLevel.connect(this);
}

@Override
public void disconnect() {
voltageLevel.disconnect(this);
public boolean disconnect() {
return voltageLevel.disconnect(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ public Switch add() {
throw new ValidationException(this, "second connection bus is not set");
}

SwitchImpl _switch = new SwitchImpl(getNetwork().getRef(),
id, getName(), SwitchKind.BREAKER, open, true);
SwitchImpl _switch = new SwitchImpl(BusBreakerVoltageLevel.this, id, getName(), SwitchKind.BREAKER, open, true);
addSwitch(_switch, busId1, busId2);
getNetwork().getListeners().notifyCreation(_switch);
return _switch;
Expand Down Expand Up @@ -314,7 +313,8 @@ public StateImpl copy() {
});
}

private void invalidateCache() {
@Override
public void invalidateCache() {
calculatedBusTopology.invalidateCache();
getNetwork().getConnectedComponentsManager().invalidate();
}
Expand Down Expand Up @@ -383,16 +383,6 @@ public SwitchAdder newDisconnector() {
throw createNotSupportedBusBreakerTopologyException();
}

@Override
public NodeBreakerView openSwitch(String switchId) {
throw createNotSupportedBusBreakerTopologyException();
}

@Override
public NodeBreakerView closeSwitch(String switchId) {
throw createNotSupportedBusBreakerTopologyException();
}

@Override
public Switch getSwitch(String switchId) {
throw createNotSupportedBusBreakerTopologyException();
Expand Down Expand Up @@ -462,26 +452,6 @@ public void removeAllBuses() {
BusBreakerVoltageLevel.this.removeAllBuses();
}

@Override
public BusBreakerView openSwitch(String switchId) {
SwitchImpl _switch = BusBreakerVoltageLevel.this.getSwitch(switchId, true);
if (!_switch.isOpen()) {
_switch.setOpen(true);
invalidateCache();
}
return this;
}

@Override
public BusBreakerView closeSwitch(String switchId) {
SwitchImpl _switch = BusBreakerVoltageLevel.this.getSwitch(switchId, true);
if (_switch.isOpen()) {
_switch.setOpen(false);
invalidateCache();
}
return this;
}

@Override
public Iterable<Switch> getSwitches() {
return Iterables.unmodifiableIterable(Iterables.transform(graph.getEdgesObject(), new Function<SwitchImpl, Switch>() {
Expand Down Expand Up @@ -693,33 +663,37 @@ public void clean() {
}

@Override
public void connect(TerminalExt terminal) {
public boolean connect(TerminalExt terminal) {
assert terminal instanceof BusTerminal;

// already connected?
if (((BusTerminal) terminal).isConnected()) {
return;
return false;
}

((BusTerminal) terminal).setConnected(true);

// invalidate connected components
invalidateCache();

return true;
}

@Override
public void disconnect(TerminalExt terminal) {
public boolean disconnect(TerminalExt terminal) {
assert terminal instanceof BusTerminal;

// already connected?
if (!terminal.isConnected()) {
return;
return false;
}

((BusTerminal) terminal).setConnected(false);

// invalidate connected components
invalidateCache();

return true;
}

@Override
Expand Down

0 comments on commit 5278cca

Please sign in to comment.