Skip to content

Commit

Permalink
Further testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan-CS committed Jun 15, 2021
1 parent f5b2746 commit 03b30ce
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/io/github/ethankelly/Tuple.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private boolean isValidTuple(List<Vertex> toAdd, boolean closures) {
Graph g = this.getGraph();
return Vertex.areStatesDifferent(toAdd, closures)
&& Vertex.areLocationsDifferent(toAdd)
&& Vertex.areAllConnected(toAdd, g);
&& g.areAllConnected(toAdd);
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/io/github/ethankelly/graph/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,24 @@ private static void spliceUtil(List<Graph> subGraphs, Vertex cutVertex, Graph cl
}
}

/**
* Given some list of vertices (tuple), this method verifies that all of the vertices in the graph given are in fact
* connected. That is, we are only interested in a tuple of vertices that constitute some manner of path (cycle or
* such like) - if not, then we do not have to consider the associated equation in the final system of equations
* that describes our compartmental model and we can discount the given tuple.
*
* @param toCheck the tuple we wish to check constitutes some kind of path.
* @return true if the tuple forms a path in some way, false otherwise.
*/
public boolean areAllConnected(List<Vertex> toCheck) {
// If there's only one vertex in the list, required in the system of equations - ensure this returns true.
// If more than one vertex, return whether they all have some path between them.
return toCheck.size() == 1 || IntStream.range(0, toCheck.size() - 1).allMatch(
i -> hasEdge(new Vertex(toCheck.get(i).getLocation()),
new Vertex(toCheck.get(i + 1).getLocation()))
);
}

/**
* Finds the cut-vertices of the graph, deletes edges attached to them, creates sub-graphs for each of the remaining
* connected components and replaces the cut-vertex in each of them and returns the list of graphs.
Expand Down
26 changes: 3 additions & 23 deletions src/io/github/ethankelly/graph/Vertex.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import java.util.HashMap;
import java.util.List;
import java.util.stream.IntStream;

/**
* The {@code VertexState} class represents an instance of a vertex being in a particular state in a compartmental
Expand Down Expand Up @@ -86,25 +85,6 @@ public static boolean areLocationsDifferent(List<Vertex> vertices) {
return locationsDifferent;
}

/**
* Given some list of vertices (tuple), this method verifies that all of the vertices in the graph given are in fact
* connected. That is, we are only interested in a tuple of vertices that constitute some manner of path (cycle or
* such like) - if not, then we do not have to consider the associated equation in the final system of equations
* that describes our compartmental model and we can discount the given tuple.
*
* @param toCheck the tuple we wish to check constitutes some kind of path.
* @param g the graph in which the tuple exists.
* @return true if the tuple forms a path in some way, false otherwise.
*/
public static boolean areAllConnected(List<Vertex> toCheck, Graph g) {
// If there's only one vertex in the list, required in the system of equations - ensure this returns true.
// If more than one vertex, return whether they all have some path between them.
return toCheck.size() == 1 || IntStream.range(0, toCheck.size() - 1).allMatch(
i -> g.hasEdge(new Vertex(toCheck.get(i).getLocation()),
new Vertex(toCheck.get(i + 1).getLocation()))
);
}

/**
* @return the state of the vertex.
*/
Expand Down Expand Up @@ -186,16 +166,16 @@ public int compareTo(Vertex that) {
int SAME = 0;
int AFTER = 1;

int states = 0;
int locations = 0;
int states = SAME;
int locations = SAME;

if (this.getState() < that.getState()) states = BEFORE;
else if (this.getState() > that.getState()) states = AFTER;

if (this.getLocation() < that.getLocation()) locations = BEFORE;
else if (this.getState() > that.getLocation()) locations = AFTER;

if (states == 0) return locations;
if (states == SAME) return locations;
else return states;
}
}
65 changes: 38 additions & 27 deletions test/io/github/ethankelly/TupleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;

public class TupleTest {
Expand All @@ -29,27 +29,37 @@ public class TupleTest {
@BeforeAll
static void setUp() {
// Set up expected singles and tuples for SIR triangle
for (int i = 0; i < 3; i++) {
expectedTriangleSIRSingles.add(new Vertex('S', i));
expectedTriangleSIRSingles.add(new Vertex('I', i));
expectedTriangleSIRSingles.add(new Vertex('S', 0));
expectedTriangleSIRSingles.add(new Vertex('S', 1));
expectedTriangleSIRSingles.add(new Vertex('S', 2));
expectedTriangleSIRSingles.add(new Vertex('I', 0));
expectedTriangleSIRSingles.add(new Vertex('I', 1));
expectedTriangleSIRSingles.add(new Vertex('I', 2));

expectedTriangleSIRPSingles.addAll(expectedTriangleSIRSingles);
expectedTriangleSIRPSingles.add(new Vertex('P', 0));
expectedTriangleSIRPSingles.add(new Vertex('P', 1));
expectedTriangleSIRPSingles.add(new Vertex('P', 2));

expectedTriangleSIRTuples.add(Arrays.asList(new Vertex('S', 0),
new Vertex('S', 1),
new Vertex('I', 2)));
expectedTriangleSIRTuples.add(Arrays.asList(new Vertex('S', 0),
new Vertex('I', 1),
new Vertex('S', 2)));
expectedTriangleSIRTuples.add(Arrays.asList(new Vertex('S', 0),
new Vertex('I', 1),
new Vertex('I', 2)));
expectedTriangleSIRTuples.add(Arrays.asList(new Vertex('I', 0),
new Vertex('S', 1),
new Vertex('S', 2)));
expectedTriangleSIRTuples.add(Arrays.asList(new Vertex('I', 0),
new Vertex('S', 1),
new Vertex('I', 2)));
expectedTriangleSIRTuples.add(Arrays.asList(new Vertex('I', 0),
new Vertex('S', 1),
new Vertex('S', 2)));

expectedTriangleSIRTuples.add(Collections.singletonList(new Vertex('S', i)));
expectedTriangleSIRTuples.add(Collections.singletonList(new Vertex('I', i)));
List<Vertex> newVerticesSI = new ArrayList<>();
List<Vertex> newVerticesIS = new ArrayList<>();
for (int j = 0; j < 3; j++) {
newVerticesSI.clear();
newVerticesIS.clear();
if (i != j) {
newVerticesSI.add(new Vertex('S', i));
newVerticesSI.add(new Vertex('I', j));
newVerticesIS.add(new Vertex('I', i));
newVerticesIS.add(new Vertex('S', j));
expectedTriangleSIRTuples.add(newVerticesSI);
expectedTriangleSIRTuples.add(newVerticesIS);
}
}
}
System.out.println(expectedTriangleSIRTuples);
System.out.println(expectedTriangleSIRTuples.size());
expectedTriangleSIRSingles.sort(null);
Expand Down Expand Up @@ -94,11 +104,12 @@ void testTriangleGenerateSingles() {

@Test
void testTriangleTuples() {
Assertions.assertTrue(expectedTriangleSIRTuples.containsAll(triangleTuplesNoClosuresSIR.getTuples()) &&
triangleTuplesNoClosuresSIR.getTuples().containsAll(expectedTriangleSIRTuples),
"Triangle tuples without closures not as expected");
Assertions.assertTrue(expectedTriangleSIRTuples.containsAll(triangleTuplesClosuresSIR.getTuples()) &&
triangleTuplesClosuresSIR.getTuples().containsAll(expectedTriangleSIRTuples),
"Triangle tuples with closures not as expected");
Assertions.assertTrue(expectedTriangleSIRTuples.containsAll(triangleTuplesNoClosuresSIR.getTuples()),
"Triangle tuples without closures contains too many tuples");
Assertions.assertTrue(triangleTuplesNoClosuresSIR.getTuples().containsAll(expectedTriangleSIRTuples),
"Triangle tuples without closures is missing tuples");
// Assertions.assertTrue(expectedTriangleSIRTuples.containsAll(triangleTuplesClosuresSIR.getTuples()) &&
// triangleTuplesClosuresSIR.getTuples().containsAll(expectedTriangleSIRTuples),
// "Triangle tuples with closures not as expected");
}
}
20 changes: 7 additions & 13 deletions test/io/github/ethankelly/graph/VertexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,17 @@ void areLocationsDifferent() {
"Two of same location in list should return false.");
}

@Test
void areAllConnected() {
}

@Test
void testEquals() {
}
Vertex v = new Vertex('S', 1);
Vertex w = new Vertex('S', 1);

@Test
void testHashCode() {
}
Assertions.assertEquals(v, w, "Equivalent vertices did not return true");

@Test
void testClone() {
}
Vertex x = new Vertex('I', 1);
Vertex y = new Vertex('S', 2);

@Test
void compareTo() {
Assertions.assertNotEquals(v, x, "Non-equivalent vertices returned equal");
Assertions.assertNotEquals(v, y, "Non-equivalent vertices returned equal");
}
}

0 comments on commit 03b30ce

Please sign in to comment.