Commit 89e2b3ee authored by Sebastian Vollbrecht's avatar Sebastian Vollbrecht
Browse files

JavaDoc fixes and changes regarding the way the generation process is logged.

Also some other minimal changes.
parent 152176dd
...@@ -64,32 +64,17 @@ public class Pair<T1, T2> { ...@@ -64,32 +64,17 @@ public class Pair<T1, T2> {
} }
@Override @Override
public int hashCode() { public boolean equals(Object o) {
final int prime = 31; if (this == o)
int result = 1;
result = prime * result + ((first == null) ? 0 : first.hashCode());
result = prime * result + ((second == null) ? 0 : second.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true; return true;
if (obj == null) if (o == null || getClass() != o.getClass())
return false;
if (!(obj instanceof Pair))
return false; return false;
Pair<?, ?> other = (Pair<?, ?>) obj; Pair<?, ?> pair = (Pair<?, ?>) o;
if (first == null) { return Objects.equals(first, pair.first) && Objects.equals(second, pair.second);
if (other.first != null)
return false;
} else if (!first.equals(other.first))
return false;
if (second == null) {
return other.second == null;
} else
return second.equals(other.second);
} }
@Override
public int hashCode() {
return Objects.hash(first, second);
}
} }
...@@ -84,6 +84,17 @@ public class SeededRandom { ...@@ -84,6 +84,17 @@ public class SeededRandom {
return rng.nextDouble(); return rng.nextDouble();
} }
/**
* Returns a random long value.
*
* @return the random long value
* @throws UnsupportedOperationException if this instance has not yet been seeded
*/
public long nextLong() {
checkSeededStatus();
return rng.nextLong();
}
/** /**
* Returns the wrapped random instance. * Returns the wrapped random instance.
* *
......
...@@ -19,6 +19,7 @@ package graphgen.generator; ...@@ -19,6 +19,7 @@ package graphgen.generator;
import graphgen.datastructures.Pair; import graphgen.datastructures.Pair;
import graphgen.datastructures.SeededRandom; import graphgen.datastructures.SeededRandom;
import graphgen.enums.ResourceType;
import graphgen.generator.components.StrictComponent; import graphgen.generator.components.StrictComponent;
import graphgen.generator.components.edges.EdgeCreator; import graphgen.generator.components.edges.EdgeCreator;
import graphgen.generator.components.edges.includers.EdgeIncluder; import graphgen.generator.components.edges.includers.EdgeIncluder;
...@@ -36,6 +37,7 @@ import graphgen.util.GraphUtils; ...@@ -36,6 +37,7 @@ import graphgen.util.GraphUtils;
import graphgen.util.JavaUtils; import graphgen.util.JavaUtils;
import modsched.Edge; import modsched.Edge;
import java.io.OutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
...@@ -84,7 +86,6 @@ public final class GraphGenerator implements StrictComponent { ...@@ -84,7 +86,6 @@ public final class GraphGenerator implements StrictComponent {
private final SeededRandom rng; private final SeededRandom rng;
private final EdgeInclusionDuplicateChecker edgeChecker; private final EdgeInclusionDuplicateChecker edgeChecker;
private boolean verbose = false;
private PrintStream out = System.out; private PrintStream out = System.out;
/** /**
...@@ -165,9 +166,7 @@ public final class GraphGenerator implements StrictComponent { ...@@ -165,9 +166,7 @@ public final class GraphGenerator implements StrictComponent {
*/ */
public Graph<ResourceNode> createGraph(long seed) { public Graph<ResourceNode> createGraph(long seed) {
if (verbose) { out.println("Creating graph with seed " + seed);
out.println("Creating graph with seed " + seed);
}
// Reset and clear generation objects. // Reset and clear generation objects.
layerCreator.reset(); layerCreator.reset();
...@@ -186,9 +185,7 @@ public final class GraphGenerator implements StrictComponent { ...@@ -186,9 +185,7 @@ public final class GraphGenerator implements StrictComponent {
GeneratedLayerStructure layers = layerCreator.createLayerStructure(); GeneratedLayerStructure layers = layerCreator.createLayerStructure();
if (verbose) { out.println("\tGenerated layer structure: " + layers.toString());
out.println("\tGenerated layer structure: " + layers.toString());
}
edgeCreator.addObserver(layers); edgeCreator.addObserver(layers);
edgeCreator.addObserver(property); edgeCreator.addObserver(property);
...@@ -201,9 +198,17 @@ public final class GraphGenerator implements StrictComponent { ...@@ -201,9 +198,17 @@ public final class GraphGenerator implements StrictComponent {
Set<ResourceNode> nodes = nodeCreator.createNodes(layers); Set<ResourceNode> nodes = nodeCreator.createNodes(layers);
if (verbose) { out.print("\tResources in use: [");
out.println("\tGenerated nodes: " + nodes.toString()); List<Resource> resourcesUsed = new ArrayList<>(GraphUtils.getResources(nodes, ResourceType.ALL));
for (int i = 0; i < resourcesUsed.size(); i++) {
if (i > 0) {
out.print(", ");
}
Resource r = resourcesUsed.get(i);
out.print(String.format("(%s, %d, %d)", r.name, r.delay, r.limit));
} }
out.println("]");
out.println("\tGenerated nodes: " + nodes.toString());
Map<Integer, ResourceNode> nodeTable = new HashMap<>(); Map<Integer, ResourceNode> nodeTable = new HashMap<>();
nodes.forEach(n -> nodeTable.put(n.getId(), n)); nodes.forEach(n -> nodeTable.put(n.getId(), n));
...@@ -218,11 +223,9 @@ public final class GraphGenerator implements StrictComponent { ...@@ -218,11 +223,9 @@ public final class GraphGenerator implements StrictComponent {
edgeCreator edgeCreator
); );
if (verbose) { out.println("\tLayer structure post property generation: " + layers.toString());
out.println("\tLayer structure post property generation: " + layers.toString()); out.println("\tGenerated edges post property generation: " + edgeCreator.edgeView());
out.println("\tGenerated edges post property generation: " + edgeCreator.edgeView()); out.println("\tGenerating incoming edges.");
out.println("\tGenerating incoming edges.");
}
nodes.add(SOURCE); nodes.add(SOURCE);
nodes.add(SINK); nodes.add(SINK);
...@@ -231,22 +234,16 @@ public final class GraphGenerator implements StrictComponent { ...@@ -231,22 +234,16 @@ public final class GraphGenerator implements StrictComponent {
generateIncomingEdges(layers, nodes, nodeTable, asapTimes); generateIncomingEdges(layers, nodes, nodeTable, asapTimes);
if (verbose) { out.println("\tGenerating outgoing edges.");
out.println("\tGenerating outgoing edges.");
}
generateOutgoingEdges(layers, nodes, nodeTable, asapTimes); generateOutgoingEdges(layers, nodes, nodeTable, asapTimes);
if (verbose) { out.println("\tGenerating optional edges.");
out.println("\tGenerating optional edges.");
}
generateOptionalEdges(layers, nodes, nodeTable, asapTimes); generateOptionalEdges(layers, nodes, nodeTable, asapTimes);
verifyASAPTimesMap(edgeCreator.edgeView(), asapTimes); verifyASAPTimesMap(edgeCreator.edgeView(), asapTimes);
if (verbose) { out.println("\tGenerating remaining backedges.");
out.println("\tGenerating remaining backedges.");
}
generateBackedges(asapTimes); generateBackedges(asapTimes);
...@@ -280,11 +277,11 @@ public final class GraphGenerator implements StrictComponent { ...@@ -280,11 +277,11 @@ public final class GraphGenerator implements StrictComponent {
for (int i = 0; i < amount; i++) { for (int i = 0; i < amount; i++) {
if (!graphs.add(createGraph(seed)) && verbose) { if (!graphs.add(createGraph(seed))) {
out.println("Duplicate detected."); out.println("Duplicate detected (seed: " + seed + ").");
} }
seed = rng.getRandom().nextLong(); seed = rng.nextLong();
} }
} else { } else {
...@@ -298,11 +295,11 @@ public final class GraphGenerator implements StrictComponent { ...@@ -298,11 +295,11 @@ public final class GraphGenerator implements StrictComponent {
if (!GraphUtils.isGraphIsomorphicToAny(graph, asapTimes, asapTimesMap)) if (!GraphUtils.isGraphIsomorphicToAny(graph, asapTimes, asapTimesMap))
graphs.add(graph); graphs.add(graph);
else if (verbose) { else {
out.println("Isomorphism detected."); out.println("Isomorphism detected (seed: " + seed + ").");
} }
seed = rng.getRandom().nextLong(); seed = rng.nextLong();
} }
} }
...@@ -321,25 +318,26 @@ public final class GraphGenerator implements StrictComponent { ...@@ -321,25 +318,26 @@ public final class GraphGenerator implements StrictComponent {
} }
/** /**
* Sets the generator to verbose mode, i.e. that the generation process will be written to the provided print * Toggles the generator's verbosity. If a print print stream is supplied, the generation process will be written to
* stream. * it. If <i>out</i> is null, the generation process won't be documented.
* <p> * <p>
* Use <code>new PrintStream(new FileOutputStream(new File(filename))</code> to immediately write to a file.<br> * Use<p>{@code new PrintStream(new FileOutputStream(new File(filename))}<p>to immediately write to a file.<br>
* <b>Warning</b>: The resulting file might be excessively large due to the * <b>Warning</b>: The resulting file might be excessively large due to the
* amount of edges possible in large graphs (O(2*V<sup>2</sup>)). * amount of edges possible in large graphs (O(2*V<sup>2</sup>)).
* *
* @param out the print stream * @param out the print stream used to document the generation process or null to disable any generation output
*/
public void setVerbose(PrintStream out) {
verbose = true;
this.out = out;
}
/**
* Sets the generator to non-verbose mode, i.e. that the generation process won't be documented.
*/ */
public void setNonVerbose() { public void setOut(PrintStream out) {
verbose = false; if (out == null) {
this.out = new PrintStream(new OutputStream() {
@Override
public void write(int b) {
// Do nothing.
}
});
} else {
this.out = out;
}
} }
/** /**
...@@ -375,9 +373,7 @@ public final class GraphGenerator implements StrictComponent { ...@@ -375,9 +373,7 @@ public final class GraphGenerator implements StrictComponent {
if (hasMandatoryIncomingEdge) if (hasMandatoryIncomingEdge)
continue; continue;
if (verbose) { out.println("\t\tTrying to generate an incoming edge for node " + dst);
out.println("\t\tTrying to generate an incoming edge for node " + dst);
}
/* /*
* Store source candidates with edge delays whose corresponding edges are * Store source candidates with edge delays whose corresponding edges are
...@@ -400,9 +396,7 @@ public final class GraphGenerator implements StrictComponent { ...@@ -400,9 +396,7 @@ public final class GraphGenerator implements StrictComponent {
int delay = edgeCreator.computeEdgeDelay(src, dst); int delay = edgeCreator.computeEdgeDelay(src, dst);
if (verbose) { out.print("\t\t\t" + src + "--" + delay + "-->" + dst + ": ");
out.print("\t\t\t" + src + "--" + delay + "-->" + dst + ": ");
}
if (isEdgePossible(src, dst, delay)) { if (isEdgePossible(src, dst, delay)) {
if (edgeIncluder.includeEdge(new Edge<>(src, dst, delay, 0))) { if (edgeIncluder.includeEdge(new Edge<>(src, dst, delay, 0))) {
...@@ -414,14 +408,11 @@ public final class GraphGenerator implements StrictComponent { ...@@ -414,14 +408,11 @@ public final class GraphGenerator implements StrictComponent {
possibleCandidates.add(Pair.of(src, delay)); possibleCandidates.add(Pair.of(src, delay));
if (verbose) { out.println("optimal.");
out.println("optimal.");
}
} else { } else {
if (verbose) { out.println("the edge includer denied the inclusion.");
out.println("the edge includer denied the inclusion.");
}
if (!optimalCandidatesFound) { if (!optimalCandidatesFound) {
possibleCandidates.add(Pair.of(src, delay)); possibleCandidates.add(Pair.of(src, delay));
...@@ -435,9 +426,7 @@ public final class GraphGenerator implements StrictComponent { ...@@ -435,9 +426,7 @@ public final class GraphGenerator implements StrictComponent {
int delay; int delay;
if (possibleCandidates.isEmpty()) { if (possibleCandidates.isEmpty()) {
if (verbose) { out.println("\t\t\tChosen incoming edge: " + SOURCE + "--" + 0 + "-->" + dst + ": ");
out.println("\t\t\tChosen incoming edge: " + SOURCE + "--" + 0 + "-->" + dst + ": ");
}
src = SOURCE; src = SOURCE;
delay = 0; delay = 0;
} else { } else {
...@@ -445,12 +434,10 @@ public final class GraphGenerator implements StrictComponent { ...@@ -445,12 +434,10 @@ public final class GraphGenerator implements StrictComponent {
src = srcAndDelay.first; src = srcAndDelay.first;
delay = srcAndDelay.second; delay = srcAndDelay.second;
if (verbose) { if (optimalCandidatesFound) {
if (optimalCandidatesFound) out.println("\t\t\tChosen incoming edge (optimal): " + src + "--" + delay + "-->" + dst + ": ");
out.println("\t\t\tChosen incoming edge (optimal): " + src + "--" + delay + "-->" + dst + ": "); } else {
else out.println("\t\t\tChosen incoming edge (not optimal): " + src + "--" + delay + "-->" + dst + ": ");
out.println(
"\t\t\tChosen incoming edge (not optimal): " + src + "--" + delay + "-->" + dst + ": ");
} }
} }
...@@ -470,9 +457,7 @@ public final class GraphGenerator implements StrictComponent { ...@@ -470,9 +457,7 @@ public final class GraphGenerator implements StrictComponent {
if (!edgeCreator.outgoingEdgesOf(src).isEmpty() || src == SINK) if (!edgeCreator.outgoingEdgesOf(src).isEmpty() || src == SINK)
continue; continue;
if (verbose) { out.println("\t\tTrying to generate an outgoing edge for node " + src);
out.println("\t\tTrying to generate an outgoing edge for node " + src);
}
int srcDepth = layers.getDepth(src); int srcDepth = layers.getDepth(src);
...@@ -495,9 +480,7 @@ public final class GraphGenerator implements StrictComponent { ...@@ -495,9 +480,7 @@ public final class GraphGenerator implements StrictComponent {
int delay = edgeCreator.computeEdgeDelay(src, dst); int delay = edgeCreator.computeEdgeDelay(src, dst);
if (verbose) { out.print("\t\t\t" + src + "--" + delay + "-->" + dst + ": ");
out.print("\t\t\t" + src + "--" + delay + "-->" + dst + ": ");
}
if (isEdgePossible(src, dst, delay)) { if (isEdgePossible(src, dst, delay)) {
if (edgeIncluder.includeEdge(new Edge<>(src, dst, delay, 0))) { if (edgeIncluder.includeEdge(new Edge<>(src, dst, delay, 0))) {
...@@ -509,13 +492,11 @@ public final class GraphGenerator implements StrictComponent { ...@@ -509,13 +492,11 @@ public final class GraphGenerator implements StrictComponent {
possibleCandidates.add(Pair.of(dst, delay)); possibleCandidates.add(Pair.of(dst, delay));
if (verbose) { out.println("optimal.");
out.println("optimal.");
}
} else { } else {
if (verbose) {
out.println("the edge includer denied the inclusion."); out.println("the edge includer denied the inclusion.");
}
if (!optimalCandidatesFound) { if (!optimalCandidatesFound) {
possibleCandidates.add(Pair.of(dst, delay)); possibleCandidates.add(Pair.of(dst, delay));
...@@ -528,9 +509,7 @@ public final class GraphGenerator implements StrictComponent { ...@@ -528,9 +509,7 @@ public final class GraphGenerator implements StrictComponent {
int delay; int delay;
if (possibleCandidates.isEmpty()) { if (possibleCandidates.isEmpty()) {
if (verbose) { out.println("\t\t\tChosen incoming edge: " + src + "--" + 0 + "-->" + SINK + ": ");
out.println("\t\t\tChosen incoming edge: " + src + "--" + 0 + "-->" + SINK + ": ");
}
dst = SINK; dst = SINK;
delay = 0; delay = 0;
} else { } else {
...@@ -538,12 +517,10 @@ public final class GraphGenerator implements StrictComponent { ...@@ -538,12 +517,10 @@ public final class GraphGenerator implements StrictComponent {
dst = dstAndDelay.first; dst = dstAndDelay.first;
delay = dstAndDelay.second; delay = dstAndDelay.second;
if (verbose) { if (optimalCandidatesFound) {
if (optimalCandidatesFound) out.println("\t\t\tChosen outgoing edge (optimal): " + src + "--" + delay + "-->" + dst + ": ");
out.println("\t\t\tChosen outgoing edge (optimal): " + src + "--" + delay + "-->" + dst + ": "); } else {
else out.println("\t\t\tChosen outgoing edge (not optimal): " + src + "--" + delay + "-->" + dst + ": ");
out.println(
"\t\t\tChosen outgoing edge (not optimal): " + src + "--" + delay + "-->" + dst + ": ");
} }
} }
...@@ -597,30 +574,22 @@ public final class GraphGenerator implements StrictComponent { ...@@ -597,30 +574,22 @@ public final class GraphGenerator implements StrictComponent {
int delay = edgeCreator.computeEdgeDelay(src, dst); int delay = edgeCreator.computeEdgeDelay(src, dst);
if (verbose) { out.print(
out.print( "\t\tChecking inclusion of edge " + ++currentEdgeCount + " of " + maxEdgeCount + ": " + src + "--" + delay + "-->" + dst + ": ");
"\t\tChecking inclusion of edge " + ++currentEdgeCount + " of " + maxEdgeCount + ": " + src + "--" + delay + "-->" + dst + ": ");
}
if (!edgeIncluder.includeEdge(new Edge<>(src, dst, delay, 0))) { if (!edgeIncluder.includeEdge(new Edge<>(src, dst, delay, 0))) {
if (verbose) { out.println("the edge includer denied the inclusion.");
out.println("the edge includer denied the inclusion.");
}
continue; continue;
} }
if (edgeChecker.hasEdgeBeenDeniedAlready(src, dst, delay) || !property.isEdgeValid(src, dst, delay)) { if (edgeChecker.hasEdgeBeenDeniedAlready(src, dst, delay) || !property.isEdgeValid(src, dst, delay)) {
if (verbose) { out.println("the property denied the inclusion.");
out.println("the property denied the inclusion.");
}
continue; continue;
} }
edgeCreator.createEdge(src, dst, delay); edgeCreator.createEdge(src, dst, delay);
if (verbose) { out.println("OK.");
out.println("OK.");
}
int newDstTime = asapTimes.get(src) + src.getDelay() + delay; int newDstTime = asapTimes.get(src) + src.getDelay() + delay;
...@@ -670,30 +639,22 @@ public final class GraphGenerator implements StrictComponent { ...@@ -670,30 +639,22 @@ public final class GraphGenerator implements StrictComponent {
int delay = edgeCreator.computeBackedgeDelay(src, dst); int delay = edgeCreator.computeBackedgeDelay(src, dst);
int distance = edgeCreator.computeBackedgeDistance(src, dst); int distance = edgeCreator.computeBackedgeDistance(src, dst);
if (verbose) { out.print(
out.print( "\t\tChecking inclusion of backedge " + ++currentBackedgeCount + " of " + maxBackedgeCount + ": " + src + "--(" + delay + ", " + distance + ")-->" + dst + ": ");
"\t\tChecking inclusion of backedge " + ++currentBackedgeCount + " of " + maxBackedgeCount + ": " + src + "--(" + delay + ", " + distance + ")-->" + dst + ": ");
}
if (!backedgeIncluder.includeEdge(new Edge<>(src, dst, delay, distance))) { if (!backedgeIncluder.includeEdge(new Edge<>(src, dst, delay, distance))) {
if (verbose) { out.println("the backedge includer denied the inclusion.");
out.println("the backedge includer denied the inclusion.");
}
continue; continue;
} }
if (!property.isBackedgeValid(src, dst, delay, distance)) { if (!property.isBackedgeValid(src, dst, delay, distance)) {
if (verbose) { out.println("the property denied the inclusion.");
out.println("the property denied the inclusion.");
}
continue; continue;
} }
edgeCreator.createBackedge(src, dst, delay, distance); edgeCreator.createBackedge(src, dst, delay, distance);
if (verbose) { out.println("OK.");
out.println("OK.");
}
} }
} }
...@@ -736,23 +697,17 @@ public final class GraphGenerator implements StrictComponent { ...@@ -736,23 +697,17 @@ public final class GraphGenerator implements StrictComponent {
private boolean isEdgePossible(ResourceNode src, ResourceNode dst, int delay) { private boolean isEdgePossible(ResourceNode src, ResourceNode dst, int delay) {
if (!edgeCreator.isEdgeValid(src, dst)) { if (!edgeCreator.isEdgeValid(src, dst)) {
if (verbose) { out.println("the nodes are connected already.");
out.println("the nodes are connected already.");
}
return false; return false;
} }