Skip to content

Commit 44d24d9

Browse files
authored
Merge pull request #191 from ProgrammingLife2017/caching
Caching improved
2 parents 03d083a + 94c3dbb commit 44d24d9

File tree

8 files changed

+203
-77
lines changed

8 files changed

+203
-77
lines changed

src/main/java/programminglife/gui/controller/GraphController.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ private Set<Segment> drawDFS(Segment origin, Segment node,
102102
drawnNodes.add(node);
103103

104104
int childCount = 0;
105-
for (Segment child : this.getGraph().getChildren(node)) {
105+
for (int child : this.getGraph().getChildren(node)) {
106106
XYCoordinate newOffset = offset.add(HORIZONTAL_OFFSET)
107107
.add(node.getWidthCoordinate())
108108
.setY(INITIAL_OFFSET.getY() + (int) (CHILD_OFFSET * childCount * node.getHeight()));
109-
this.drawDFS(node, child, newOffset, maxDepth - 1, drawnNodes);
109+
this.drawDFS(node, new Segment(this.graph, child), newOffset, maxDepth - 1, drawnNodes);
110110
childCount++;
111111
}
112112
}

src/main/java/programminglife/gui/controller/GuiController.java

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package programminglife.gui.controller;
22

3+
import javafx.animation.PauseTransition;
4+
import javafx.application.Platform;
35
import javafx.beans.value.ChangeListener;
46
import javafx.beans.value.ObservableValue;
57
import javafx.event.ActionEvent;
@@ -134,6 +136,7 @@ public void update(Observable o, Object arg) {
134136
this.setGraph(graph);
135137
} else if (arg instanceof Exception) {
136138
Exception e = (Exception) arg;
139+
e.printStackTrace();
137140
Alerts.error(e.getMessage());
138141
}
139142
} else if (o instanceof FileProgressCounter) {
@@ -153,6 +156,7 @@ public void update(Observable o, Object arg) {
153156
public void setGraph(GenomeGraph graph) {
154157
this.graphController.setGraph(graph);
155158
disableGraphUIElements(graph == null);
159+
Platform.runLater(() -> ProgrammingLife.getStage().setTitle(graph.getID()));
156160

157161
if (graph != null) {
158162
Console.println("[%s] Graph was set to %s.", Thread.currentThread().getName(), graph.getID());

src/main/java/programminglife/model/GenomeGraph.java

+86-37
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package programminglife.model;
22

3+
import org.apache.commons.lang3.ArrayUtils;
34
import org.apache.commons.lang3.NotImplementedException;
45
import org.jetbrains.annotations.NotNull;
56
import programminglife.model.exception.NodeExistsException;
67
import programminglife.parser.Cache;
78

89
import java.io.IOException;
910
import java.util.*;
10-
import java.util.stream.Collectors;
1111

1212
/**
1313
* Created by marti_000 on 25-4-2017.
@@ -16,10 +16,6 @@ public class GenomeGraph implements Graph {
1616
private String id;
1717
private Cache cache;
1818

19-
// TODO cache graph structure
20-
private Map<Integer, Set<Integer>> children;
21-
private Map<Integer, Set<Integer>> parents;
22-
2319
// TODO cache genomes
2420
private Map<String, Genome> genomes;
2521

@@ -28,19 +24,7 @@ public class GenomeGraph implements Graph {
2824
* @param id id of the graph
2925
*/
3026
public GenomeGraph(String id) {
31-
this(id, new HashMap<>(), new HashMap<>());
32-
}
33-
34-
/**
35-
* The constructor for a GenomeGraph.
36-
* @param id String.
37-
* @param children {@link Set} which contains the children.
38-
* @param parents {@link Set} which contains the parents.
39-
*/
40-
public GenomeGraph(String id, Map<Integer, Set<Integer>> children, Map<Integer, Set<Integer>> parents) {
4127
this.id = id;
42-
this.children = children;
43-
this.parents = parents;
4428
this.genomes = new HashMap<>();
4529
this.cache = new Cache(id);
4630
}
@@ -55,11 +39,11 @@ public String getID() {
5539

5640
@Override
5741
public void addNode(Node node) {
58-
this.addNode(node, new HashSet<>(), new HashSet<>());
42+
this.addNode(node, new int[0], new int[0]);
5943
}
6044

6145
@Override
62-
public void addNode(Node node, Set<Node> children, Set<Node> parents) {
46+
public void addNode(Node node, int[] children, int[] parents) {
6347
if (this.contains(node)) {
6448
throw new NodeExistsException(String.format("%s already exists in graph %s",
6549
node.toString(), this.getID()));
@@ -70,48 +54,46 @@ public void addNode(Node node, Set<Node> children, Set<Node> parents) {
7054

7155
@Override
7256
public void replaceNode(Node node) {
73-
this.replaceNode(node, new HashSet<>(), new HashSet<>());
57+
this.replaceNode(node, new int[0], new int[0]);
7458
}
7559

7660
@Override
77-
public void replaceNode(Node node, Set<Node> children, Set<Node> parents) {
78-
this.children.put(node.getIdentifier(), children.stream().map(c ->
79-
c.getIdentifier()).collect(Collectors.toSet()));
80-
this.parents.put(node.getIdentifier(), parents.stream().map(p ->
81-
p.getIdentifier()).collect(Collectors.toSet()));
61+
public void replaceNode(Node node, int[] children, int[] parents) {
62+
this.cache.getChildrenAdjacencyMap().put(node.getIdentifier(), children);
63+
this.cache.getParentsAdjacencyMap().put(node.getIdentifier(), parents);
8264
}
8365

8466
/**
8567
* Get the number of nodes in the {@link GenomeGraph}.
8668
* @return the number of nodes
8769
*/
8870
public int size() {
89-
assert (children.size() == parents.size());
90-
return this.children.size();
71+
assert (this.cache.getChildrenAdjacencyMap().size() == this.cache.getParentsAdjacencyMap().size());
72+
return this.cache.getChildrenAdjacencyMap().size();
9173
}
9274

9375
@Override
94-
public Set<Segment> getChildren(Node node) {
76+
public int[] getChildren(Node node) {
9577
return this.getChildren(node.getIdentifier());
9678
}
9779

9880
@Override
99-
public Set<Segment> getChildren(int nodeID) {
100-
return this.children.get(nodeID).stream().map(id -> new Segment(this, id)).collect(Collectors.toSet());
81+
public int[] getChildren(int nodeID) {
82+
return this.cache.getChildrenAdjacencyMap().get(nodeID);
10183
}
10284

10385
@Override
104-
public Set<Segment> getParents(Node node) {
86+
public int[] getParents(Node node) {
10587
return this.getParents(node.getIdentifier());
10688
}
10789

10890
@Override
109-
public Set<Segment> getParents(int nodeID) {
110-
return this.parents.get(nodeID).stream().map(id -> new Segment(this, id)).collect(Collectors.toSet());
91+
public int[] getParents(int nodeID) {
92+
return this.cache.getParentsAdjacencyMap().get(nodeID);
11193
}
11294

11395
@Override
114-
public Set<Genome> getGenomes(Node node) {
96+
public int[] getGenomes(Node node) {
11597
throw new NotImplementedException("GenomeGraph#getGenomes(Node) is not yet implemented");
11698
}
11799

@@ -122,7 +104,7 @@ public boolean contains(Node node) {
122104

123105
@Override
124106
public boolean contains(int nodeID) {
125-
return this.children.containsKey(nodeID);
107+
return this.cache.getChildrenAdjacencyMap().containsKey(nodeID);
126108
}
127109

128110
@Override
@@ -137,7 +119,26 @@ public void addEdge(Node source, Node destination) {
137119
* @param child Node of the child to be added.
138120
*/
139121
private void addChild(Node node, Node child) {
140-
this.children.get(node.getIdentifier()).add(child.getIdentifier());
122+
if (this.cache.getCurrentParentID() == -1) {
123+
this.cache.setCurrentParentID(node.getIdentifier());
124+
}
125+
126+
if (node.getIdentifier() == this.cache.getCurrentParentID()) {
127+
// if same parent as previous link || if first link of graph,
128+
// just add the child
129+
this.cache.getCurrentParentChildren().add(child.getIdentifier());
130+
} else {
131+
// write previous list to cache
132+
int[] oldChildren = this.getChildren(this.cache.getCurrentParentID());
133+
int[] allChildren = this.append(oldChildren, this.cache.getCurrentParentChildren());
134+
this.cache.getChildrenAdjacencyMap().put(this.cache.getCurrentParentID(), allChildren);
135+
136+
// reset node id
137+
this.cache.setCurrentParentID(node.getIdentifier());
138+
// reset children list
139+
this.cache.setCurrentParentChildren(new LinkedList<>());
140+
this.cache.getCurrentParentChildren().add(child.getIdentifier());
141+
}
141142
}
142143

143144
/**
@@ -146,7 +147,11 @@ private void addChild(Node node, Node child) {
146147
* @param parent Node of the parent to be added.
147148
*/
148149
private void addParent(Node node, Node parent) {
149-
this.parents.get(node.getIdentifier()).add(parent.getIdentifier());
150+
int[] oldParents = this.getParents(node.getIdentifier());
151+
//TODO find a way to do this more efficient
152+
int[] newParents = Arrays.copyOf(oldParents, oldParents.length + 1);
153+
newParents[newParents.length - 1] = parent.getIdentifier();
154+
this.cache.getParentsAdjacencyMap().put(node.getIdentifier(), newParents);
150155
}
151156

152157
/**
@@ -230,6 +235,22 @@ public int getSequenceLength(int nodeID) {
230235
return this.cache.getSequenceLength(nodeID);
231236
}
232237

238+
/**
239+
* Get the number of lines in the GFA file.
240+
* @return # of lines
241+
*/
242+
public int getNumberOfLines() {
243+
return this.cache.getNumberOfLines();
244+
}
245+
246+
/**
247+
* Set the number of lines in the GFA file.
248+
* @param numberOfLines # of lines
249+
*/
250+
public void setNumberOfLines(int numberOfLines) {
251+
this.cache.setNumberOfLines(numberOfLines);
252+
}
253+
233254
/**
234255
* Roll back the latest changes to the cache.
235256
* @throws IOException when something strange happens during deletion
@@ -263,4 +284,32 @@ public void close() throws IOException {
263284
this.cache = null;
264285
}
265286
}
287+
288+
/**
289+
* Append an {@link List<Integer>} to a int[].
290+
* @param oldArray the int[] to go first
291+
* @param newList the {@link List<Integer>} to be appended
292+
* @return a int[] consisting of all elements
293+
*/
294+
private int[] append(int[] oldArray, List<Integer> newList) {
295+
int[] newArray;
296+
if (oldArray == null) {
297+
newArray = oldArray;
298+
} else {
299+
newArray = ArrayUtils.addAll(oldArray, newList.stream().mapToInt(i -> i).toArray());
300+
}
301+
302+
return newArray;
303+
}
304+
305+
/**
306+
* Cache the group of edges from the last parent.
307+
*
308+
* Necessary because these are skipped during parsing.
309+
*/
310+
public void cacheLastEdges() {
311+
int[] oldChildren = this.getChildren(this.cache.getCurrentParentID());
312+
int[] allChildren = this.append(oldChildren, this.cache.getCurrentParentChildren());
313+
this.cache.getChildrenAdjacencyMap().put(this.cache.getCurrentParentID(), allChildren);
314+
}
266315
}

src/main/java/programminglife/model/Graph.java

+16-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package programminglife.model;
22

3-
import java.util.Set;
4-
53
/**
64
* Interface for the graph.
75
*/
@@ -21,10 +19,10 @@ public interface Graph {
2119
/**
2220
* Add a node to the graph.
2321
* @param node Node.
24-
* @param children {@link Set} which contains the children.
25-
* @param parents {@link Set} which contains the parents.
22+
* @param children int[] which contains the children.
23+
* @param parents int[] which contains the parents.
2624
*/
27-
void addNode(Node node, Set<Node> children, Set<Node> parents);
25+
void addNode(Node node, int[] children, int[] parents);
2826

2927
/**
3028
* Replaces a node.
@@ -35,10 +33,10 @@ public interface Graph {
3533
/**
3634
* Replaces a node.
3735
* @param node Node.
38-
* @param children {@link Set} of the children.
39-
* @param parents {@link Set} of the parents.
36+
* @param children int[] of the children.
37+
* @param parents int[] of the parents.
4038
*/
41-
void replaceNode(Node node, Set<Node> children, Set<Node> parents);
39+
void replaceNode(Node node, int[] children, int[] parents);
4240

4341
/**
4442
* Gives the size of a graph.
@@ -49,37 +47,37 @@ public interface Graph {
4947
/**
5048
* getter for the Children of a node.
5149
* @param node the {@link Node} to get the children from.
52-
* @return {@link Set} of the children.
50+
* @return int[] of the children.
5351
*/
54-
Set<Segment> getChildren(Node node);
52+
int[] getChildren(Node node);
5553

5654
/**
5755
* getter for the Children of a node.
5856
* @param nodeID the node ID to get children from
59-
* @return {@link Set} of the children.
57+
* @return int[] of the children.
6058
*/
61-
Set<Segment> getChildren(int nodeID);
59+
int[] getChildren(int nodeID);
6260

6361
/**
6462
* getter for the Parents of a node.
6563
* @param node Node.
66-
* @return {@link Set} of the parents.
64+
* @return int[] of the parents.
6765
*/
68-
Set<Segment> getParents(Node node);
66+
int[] getParents(Node node);
6967

7068
/**
7169
* getter for the Parents of a node.
7270
* @param nodeID int.
73-
* @return {@link Set} of the parents.
71+
* @return int[] of the parents.
7472
*/
75-
Set<Segment> getParents(int nodeID);
73+
int[] getParents(int nodeID);
7674

7775
/**
7876
* getter for the Genomes of a node.
7977
* @param node Node.
80-
* @return {@link Set}.
78+
* @return int[].
8179
*/
82-
Set<Genome> getGenomes(Node node);
80+
int[] getGenomes(Node node);
8381

8482
/**
8583
* Checks to see if the graph contains a certain node.

0 commit comments

Comments
 (0)