Skip to content

Commit

Permalink
Cleanups and StringBuffers
Browse files Browse the repository at this point in the history
  • Loading branch information
sbondorf committed Aug 19, 2018
1 parent 21f660d commit d53da91
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 38 deletions.
Expand Up @@ -45,18 +45,18 @@
*/
public class Flow implements Serializable, Displayable {
private static final long serialVersionUID = 7989846738211040015L;

private int ID;

private List<Integer> vertices;
private List<Arrival> arrivals;
private List<Integer> priorities;

private int ID;
private String alias;
private Network nw;

private int established_arrivals;

private Network nw;

/**
* Constructs a flow, with the complete route through the network and
* priorities at the vertices given.
Expand All @@ -79,6 +79,7 @@ public class Flow implements Serializable, Displayable {
*/
public Flow(int flow_ID, List<Integer> vertices, List<Arrival> arrivals,
List<Integer> priorities, String alias, Network nw) {
this.ID = flow_ID;
this.vertices = vertices;
this.arrivals = arrivals;
this.priorities = priorities;
Expand All @@ -88,12 +89,14 @@ public Flow(int flow_ID, List<Integer> vertices, List<Arrival> arrivals,
if(vertices.size() != priorities.size() || arrivals.size() > vertices.size()) {
throw new NetworkActionException("Flow cannot be constructed. route, priorities and arrivals do not match.");
}

// TODO: Make this safer
established_arrivals = 1;
this.ID = flow_ID;
this.alias = alias == null ? "" : alias;

// TODO: Why?
arrivals.get(0).addArrivalDependency(flow_ID);

this.nw = nw;
}

Expand All @@ -106,8 +109,8 @@ public Flow(int flow_ID, List<Integer> vertices, List<Arrival> arrivals,
*/
public void addNodetoPath(int vertex_ID, int priority) {
vertices.add(vertex_ID);
arrivals.add(new Arrival(nw));
priorities.add(priority);
arrivals.add(new Arrival(nw));
}

/**
Expand Down
Expand Up @@ -63,28 +63,30 @@
*/
public class Network implements Serializable {
private static final long serialVersionUID = 695224731594099768L;

private Map<Integer, Vertex> vertices;
private Map<Integer, Flow> flows;
private Map<Integer, Hoelder> hoelders;

private int FLOW_ID;
private int VERTEX_ID;
private int HOELDER_ID;

private Map<Integer, Flow> flows;
private Map<Integer, Vertex> vertices;
private Map<Integer, Hoelder> hoelders;

private List<NetworkListener> listeners;

public Network() {
this(null, null, null);
}

public Network(Map<Integer, Vertex> vertices, Map<Integer, Flow> flows, Map<Integer, Hoelder> hoelders) {
this.vertices = (vertices != null) ? vertices : new HashMap<Integer, Vertex>();
this.flows = (flows != null) ? flows : new HashMap<Integer, Flow>();
this.vertices = (vertices != null) ? vertices : new HashMap<Integer, Vertex>();
this.hoelders = (hoelders != null) ? hoelders : new HashMap<Integer, Hoelder>();

FLOW_ID = this.flows.size() + 1;
VERTEX_ID = this.vertices.size() + 1;
HOELDER_ID = this.hoelders.size() + 1;

this.listeners = new ArrayList<NetworkListener>();
}

Expand Down Expand Up @@ -316,26 +318,28 @@ public int addFlow(Arrival initial_arrival, List<Integer> route, List<Integer> p
arrivals.add(new Arrival(this));
}

//Adds the flow into the flow list
// Adds the flow into the flow list
Flow flow = new Flow(FLOW_ID, route, arrivals, priorities, alias, this);
// Check whether every vertex exists
for (int i = 0; i < route.size(); i++) {
if (vertices.get(route.get(i)) == null) {
throw new NetworkActionException("Error while adding flow " + alias + ". No node with ID " + i);
}
}
//Initializes the first arrival at the first vertex
// Initializes the first arrival at the first vertex
Vertex first_vertex = vertices.get(route.get(0));

flows.put(FLOW_ID, flow);
//Writes the flow in its corresponding vertices

// Writes the flow in its corresponding vertices
for (int i = 0; i < route.size(); i++) {
Vertex vertex;
vertex = vertices.get(route.get(i));
vertex.addUnknownArrival(priorities.get(i), FLOW_ID);
}
first_vertex.learnArrival(FLOW_ID, initial_arrival);
//Increments the flow count

// Increments the flow count
incrementFLOW_ID();

// Notify the listeners
Expand All @@ -354,11 +358,10 @@ public int addFlow(Arrival initial_arrival, List<Integer> route, List<Integer> p
* @param priority the priority the flow has at the appended vertex.
*/
public void appendNode(int flow_id, int vertex_id, int priority) {

//Adds the vertex to the path of the flow
// Adds the vertex to the path of the flow
flows.get(flow_id).addNodetoPath(vertex_id, priority);

//Adds a non-established arrival to the appended vertex
// Adds a non-established arrival to the appended vertex
vertices.get(vertex_id).addUnknownArrival(priority, flow_id);
}

Expand All @@ -371,11 +374,10 @@ public void appendNode(int flow_id, int vertex_id, int priority) {
* @throws ArrivalNotAvailableException
*/
public void setInitialArrival(int flow_id, Arrival arrival) throws ArrivalNotAvailableException {

//initializes the arrival at the flow
// Initializes the arrival at the flow
flows.get(flow_id).setInitialArrival(arrival);

//the arrival is established at the vertex
// The arrival is established at the vertex
Vertex vertex = vertices.get(flows.get(flow_id).getFirstVertexID());
vertex.learnArrival(flow_id, arrival);
}
Expand Down Expand Up @@ -437,21 +439,38 @@ public void resetHOELDER_ID(int reset) {
* @return
*/
public String getStringRepresentation() {
String result_string = "List of vertices:\n";
for (Map.Entry<Integer, Vertex> entry : vertices.entrySet()) {
result_string = result_string + "Vertex-ID: " + entry.getValue().getID()
+ "\t Vertex-Alias: " + entry.getValue().getAlias() + "\n";
StringBuffer network_str = new StringBuffer();

network_str.append("List of vertices:");
network_str.append("\n");
for (Map.Entry<Integer,Vertex> entry : vertices.entrySet()) {
network_str.append("Vertex-ID: ");
network_str.append(Integer.toString(entry.getValue().getID()));
network_str.append("\t");
network_str.append(" Vertex-Alias: ");
network_str.append(entry.getValue().getAlias());
network_str.append("\n");
}
result_string = result_string + "List of flows:\n";
for (Map.Entry<Integer, Flow> entry : flows.entrySet()) {
result_string = result_string + "Flow-ID: " + entry.getValue().getID()
+ "\t Flow-Alias: " + entry.getValue().getAlias() + "\t route:\n" + entry.getValue().getVerticeIDs().toString() + "\n";

network_str.append("List of flows:");
network_str.append("\n");
for (Map.Entry<Integer,Flow> entry : flows.entrySet()) {
network_str.append("Flow-ID: ");
network_str.append(Integer.toString(entry.getValue().getID()));
network_str.append("\t");
network_str.append(" Flow-Alias: ");
network_str.append(entry.getValue().getAlias());
network_str.append("\t");
network_str.append(" route:");
network_str.append("\n");
network_str.append(entry.getValue().getVerticeIDs().toString());
}

return result_string + "Number of Hoelder parameters: " + (HOELDER_ID - 1);
}
network_str.append("Number of Hoelder parameters: ");
network_str.append(Integer.toString(HOELDER_ID - 1));

//Getter and Setter
return network_str.toString();
}

public Vertex getVertex(int id) {
return vertices.get(id);
Expand Down Expand Up @@ -488,7 +507,6 @@ public Map<Integer, Vertex> getVertices() {
}

public Network deepCopy() {

Map<Integer, Vertex> newVertices = new HashMap<Integer, Vertex>(this.vertices.size());
Map<Integer, Flow> newFlows = new HashMap<Integer, Flow>(this.flows.size());
Map<Integer, Hoelder> newHoelders = new HashMap<Integer, Hoelder>(this.hoelders.size());
Expand Down Expand Up @@ -523,7 +541,7 @@ public Network deepCopy() {
* @return
*/
public static Network load(File profile_path, boolean redirectListeners) {
//will read profile.txt line by line
// will read profile.txt line by line
Network nw = new Network();
// Kind of a hack
if (redirectListeners) {
Expand All @@ -536,9 +554,9 @@ public static Network load(File profile_path, boolean redirectListeners) {
try (BufferedReader br = new BufferedReader(new FileReader(profile_path))) {
String sCurrentLine;

//reads all lines
// reads all lines
while ((sCurrentLine = br.readLine()) != null) {
//if a line starts with "I" an interface (i.e. service element) is added to the network in form of a Vertex.
// If a line starts with "I" an interface (i.e. service element) is added to the network in form of a Vertex.
if (sCurrentLine.startsWith("I")) {
try {
nw.handleVertexLine(sCurrentLine);
Expand All @@ -549,7 +567,7 @@ public static Network load(File profile_path, boolean redirectListeners) {
}
}

//if a line starts with "F" a flow is added to the network in form of a Flow-object.
// If a line starts with "F" a flow is added to the network in form of a Flow-object.
if (sCurrentLine.startsWith("F")) {
try {
nw.handleFlowLine(sCurrentLine);
Expand Down Expand Up @@ -596,6 +614,7 @@ private int handleVertexLine(String line) throws BadInitializationException {

private void handleFlowLine(String line) throws NumberFormatException, BadInitializationException, ArrivalNotAvailableException {
final int pathOffset = 2; // There are 2 entries before the route

// Removes the first character, we do not need that anoymore
line = line.substring(1).trim();
String[] lineParts = line.split(",");
Expand Down Expand Up @@ -648,7 +667,6 @@ private void handleFlowLine(String line) throws NumberFormatException, BadInitia
maxTheta = Double.parseDouble(lineParts[pathOffset + pathLength + 3].trim());
arrival = ArrivalFactory.buildStationaryTB(rate, bucket, maxTheta);
}

} else {
throw new FileOperationException("No arrival with type " + arrivalType + " known.", line);
}
Expand All @@ -671,7 +689,6 @@ public void save(File file) {
}

try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {

// Write out the nodes
for (Vertex v : this.getVertices().values()) {
bw.write("I " + v.getAlias() + ", " + "FIFO" + ", " + "CR" + ", " + v.getService().getRho().toString().substring(1));
Expand Down

0 comments on commit d53da91

Please sign in to comment.