Skip to content

Commit ddfe5f8

Browse files
committed
Merge pull request #47 from rutgervandenberg/dev
Implement Sequence class and controller for filtering in views. All looks good, can be merged.
2 parents f8434fa + 2f820f6 commit ddfe5f8

File tree

9 files changed

+348
-12
lines changed

9 files changed

+348
-12
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
<version>4.12</version>
1818
<scope>test</scope>
1919
</dependency>
20+
<dependency>
21+
<groupId>org.mockito</groupId>
22+
<artifactId>mockito-all</artifactId>
23+
<version>1.9.5</version>
24+
<scope>test</scope>
25+
</dependency>
2026
<dependency>
2127
<groupId>org.jgrapht</groupId>
2228
<artifactId>jgrapht-core</artifactId>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package nl.tudelft.lifetiles.controller;
2+
3+
import java.util.HashSet;
4+
import java.util.Map;
5+
import java.util.Observable;
6+
import java.util.Set;
7+
8+
import nl.tudelft.lifetiles.graph.Sequence;
9+
10+
/**
11+
* @author Rutger van den Berg
12+
* Controls what the view modules display.
13+
*/
14+
public class ViewController extends Observable {
15+
/**
16+
* Map of all sequences currently loaded.
17+
*/
18+
private Map<String, Sequence> sequenceMap;
19+
/**
20+
* Set containing all sequences currently loaded.
21+
*/
22+
private Set<Sequence> allSequences;
23+
/**
24+
* Set containing the currently visible sequences.
25+
*/
26+
private Set<Sequence> visibleSequences;
27+
28+
/**
29+
* Creates a new viewcontroller.
30+
*
31+
* @param sequences
32+
* The current sequences.
33+
*/
34+
public ViewController(final Map<String, Sequence> sequences) {
35+
sequenceMap = sequences;
36+
allSequences = new HashSet<>();
37+
38+
for (Map.Entry<String, Sequence> map : sequenceMap.entrySet()) {
39+
Sequence seq = map.getValue();
40+
allSequences.add(seq);
41+
}
42+
visibleSequences = new HashSet<>(allSequences);
43+
}
44+
45+
/**
46+
* Sets the visible sequences in all views to the provided sequences.
47+
*
48+
* @param sequences
49+
* The sequences to set to visible.
50+
*/
51+
public final void setVisible(final Set<Sequence> sequences) {
52+
visibleSequences = sequences;
53+
if (visibleSequences.retainAll(allSequences)) {
54+
throw new IllegalArgumentException(
55+
"Attempted to set a non-existant sequence to visible");
56+
}
57+
setChanged();
58+
notifyObservers();
59+
}
60+
61+
/**
62+
* @return A set containing all visible sequences.
63+
*/
64+
public final Set<Sequence> getVisible() {
65+
return visibleSequences;
66+
}
67+
68+
}

src/main/java/nl/tudelft/lifetiles/graph/DefaultGraphParser.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
import java.io.IOException;
55
import java.net.URISyntaxException;
66
import java.nio.file.Files;
7-
import java.util.Collections;
7+
import java.util.HashMap;
88
import java.util.HashSet;
99
import java.util.Iterator;
10+
import java.util.Map;
1011
import java.util.Set;
1112

1213
/**
@@ -73,12 +74,13 @@ private void parseEdges(final String filename,
7374
*/
7475
private void parseVertices(final String filename,
7576
final Graph<SequenceSegment> graph) {
77+
Map<String, Sequence> sequences = new HashMap<>();
7678
try {
7779
File file = new File(this.getClass()
7880
.getResource("/" + filename + ".node.graph").toURI());
7981
Iterator<String> it = Files.lines(file.toPath()).iterator();
8082
while (it.hasNext()) {
81-
graph.addVertex(createSegment(it.next(), it.next()));
83+
graph.addVertex(createSegment(it.next(), it.next(), sequences));
8284
}
8385
} catch (IOException e) {
8486
e.printStackTrace();
@@ -92,22 +94,28 @@ private void parseVertices(final String filename,
9294
* Description line in the vertex file.
9395
* @param content
9496
* Content line in the vertex file.
97+
* @param sequences
98+
* A map containing sequences that have already been seen.
9599
* @return a new SequenceSegment
96100
*/
97101
private SequenceSegment createSegment(final String descriptor,
98-
final String content) {
102+
final String content, final Map<String, Sequence> sequences) {
99103
if (!descriptor.startsWith(">")) {
100104
throw new IllegalArgumentException();
101105
}
102106
String[] desc = descriptor.split("\\|");
103107
String[] sources = desc[2].split(",");
104-
for (String s : sources) {
105-
s = s.trim();
108+
Set<Sequence> currentSequences = new HashSet<>();
109+
for (String sequencename : sources) {
110+
if (!sequences.containsKey(sequencename.trim())) {
111+
sequences.put(sequencename, new SequenceImplementation(
112+
sequencename));
113+
}
114+
currentSequences.add(sequences.get(sequencename));
106115
}
107-
Set<String> s = new HashSet<String>();
108-
Collections.addAll(s, sources);
109116

110-
return new SequenceSegment(s, Integer.parseInt(desc[START_POS].trim()),
117+
return new SequenceSegment(currentSequences,
118+
Integer.parseInt(desc[START_POS].trim()),
111119
Integer.parseInt(desc[END_POS].trim()), content.trim());
112120

113121
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package nl.tudelft.lifetiles.graph;
2+
3+
import java.util.List;
4+
5+
/**
6+
* @author Rutger van den Berg
7+
* Interface for complete sequences.
8+
*/
9+
public interface Sequence {
10+
/**
11+
* Return the segments related to this sequence.
12+
*
13+
* @return A list of sequence segments.
14+
*/
15+
List<SequenceSegment> getSegments();
16+
17+
/**
18+
* Add a segment to the sequence.
19+
*
20+
* @param segment
21+
* The segment to append.
22+
*/
23+
void appendSegment(SequenceSegment segment);
24+
25+
/**
26+
* @return The identifier for this sequence.
27+
*/
28+
String getIdentifier();
29+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package nl.tudelft.lifetiles.graph;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/**
9+
* @author Rutger van den Berg
10+
* Class that derives seperate sequences from a sequencegraph.
11+
*/
12+
public class SequenceGenerator {
13+
/**
14+
* The graph from which the sequences are derived.
15+
*/
16+
private Graph<SequenceSegment> sourceGraph;
17+
/**
18+
* keep track of which segments have been processed.
19+
*/
20+
private Map<SequenceSegment, Boolean> processed;
21+
/**
22+
* the generated sequences.
23+
*/
24+
private List<Sequence> sequences;
25+
26+
/**
27+
* @param graph
28+
* A DAG graph for which to generate sequences.
29+
*/
30+
public SequenceGenerator(final Graph<SequenceSegment> graph) {
31+
sourceGraph = graph;
32+
}
33+
34+
/**
35+
* @return A list of sequences
36+
*/
37+
public final List<Sequence> generateSequences() {
38+
processed = new HashMap<>();
39+
sequences = new ArrayList<>();
40+
for (SequenceSegment segment : sourceGraph.getSource()) {
41+
generateSequences(segment);
42+
}
43+
return sequences;
44+
}
45+
46+
/**
47+
* Helper for generating sequences.
48+
*
49+
* @param vertex
50+
* The vertex from which to start adding segments.
51+
*/
52+
private void generateSequences(final SequenceSegment vertex) {
53+
for (Sequence seq : vertex.getSources()) {
54+
seq.appendSegment(vertex);
55+
}
56+
for (Edge<SequenceSegment> edge : sourceGraph.getOutgoing(vertex)) {
57+
SequenceSegment candidate = sourceGraph.getDestination(edge);
58+
if (!processed.containsKey(candidate)) {
59+
processed.put(candidate, new Boolean(true));
60+
generateSequences(candidate);
61+
}
62+
}
63+
}
64+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package nl.tudelft.lifetiles.graph;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* @author Rutger van den Berg
8+
* Contains an entire sequence.
9+
*/
10+
public class SequenceImplementation implements Sequence {
11+
/**
12+
* List of sequence segments related to this sequence.
13+
*/
14+
private List<SequenceSegment> sequenceList;
15+
/**
16+
* Identifier for this sequence.
17+
*/
18+
private String ident;
19+
20+
/**
21+
* @param identifier
22+
* The identifier for this sequence.
23+
*/
24+
public SequenceImplementation(final String identifier) {
25+
ident = identifier;
26+
sequenceList = new ArrayList<>();
27+
}
28+
29+
@Override
30+
public final List<SequenceSegment> getSegments() {
31+
32+
return sequenceList;
33+
}
34+
35+
@Override
36+
public final void appendSegment(final SequenceSegment segment) {
37+
sequenceList.add(segment);
38+
39+
}
40+
41+
@Override
42+
public final String getIdentifier() {
43+
return ident;
44+
}
45+
46+
}

src/main/java/nl/tudelft/lifetiles/graph/SequenceSegment.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class SequenceSegment {
99
/**
1010
* Contains the sources containing this segment.
1111
*/
12-
private Set<String> sourcesVar;
12+
private Set<Sequence> sourcesVar;
1313

1414
/**
1515
* The start position for this segment.
@@ -34,8 +34,9 @@ public class SequenceSegment {
3434
* @param content
3535
* The content for this segment.
3636
*/
37-
public SequenceSegment(final Set<String> sources, final long startPosition,
38-
final long endPosition, final String content) {
37+
public SequenceSegment(final Set<Sequence> sources,
38+
final long startPosition, final long endPosition,
39+
final String content) {
3940
sourcesVar = sources;
4041
startVar = startPosition;
4142
endVar = endPosition;
@@ -45,7 +46,7 @@ public SequenceSegment(final Set<String> sources, final long startPosition,
4546
/**
4647
* @return the sources
4748
*/
48-
public final Set<String> getSources() {
49+
public final Set<Sequence> getSources() {
4950
return sourcesVar;
5051
}
5152

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package nl.tudelft.lifetiles.controller;
2+
3+
import java.util.HashMap;
4+
import java.util.HashSet;
5+
import java.util.Map;
6+
import java.util.Observable;
7+
import java.util.Observer;
8+
import java.util.Set;
9+
10+
import nl.tudelft.lifetiles.controller.ViewController;
11+
import nl.tudelft.lifetiles.graph.Sequence;
12+
import nl.tudelft.lifetiles.graph.SequenceImplementation;
13+
14+
import org.junit.Before;
15+
import org.junit.Rule;
16+
import org.junit.Test;
17+
import org.junit.rules.ExpectedException;
18+
import org.mockito.Mockito;
19+
20+
public class ViewControllerTest {
21+
ViewController controller;
22+
Observer view;
23+
Map<String, Sequence> sequences;
24+
Sequence s1, s2, s3, s4;
25+
26+
@Rule
27+
public ExpectedException thrown = ExpectedException.none();
28+
29+
@Before
30+
public void setUp() throws Exception {
31+
s1 = new SequenceImplementation("s1");
32+
s2 = new SequenceImplementation("s2");
33+
s3 = new SequenceImplementation("s3");
34+
s4 = new SequenceImplementation("s4");
35+
sequences = new HashMap<>();
36+
sequences.put("s1", s1);
37+
sequences.put("s2", s2);
38+
sequences.put("s3", s3);
39+
controller = new ViewController(sequences);
40+
}
41+
42+
@Test
43+
public void testSetWrongVisible() {
44+
Set<Sequence> newSequences = new HashSet<>();
45+
newSequences.add(s1);
46+
newSequences.add(s4);
47+
thrown.expect(IllegalArgumentException.class);
48+
controller.setVisible(newSequences);
49+
}
50+
51+
@Test
52+
public void testObserve() {
53+
view = Mockito.mock(Observer.class);
54+
controller.addObserver(view);
55+
Set<Sequence> newSequences = new HashSet<>();
56+
newSequences.add(s1);
57+
newSequences.add(s2);
58+
controller.setVisible(newSequences);
59+
Mockito.verify(view, Mockito.atLeastOnce()).update(
60+
Mockito.<Observable> any(), Mockito.<Object> any());
61+
}
62+
63+
}

0 commit comments

Comments
 (0)