Newer
Older
import java.util.List;
import java.util.ArrayList;
private static final int MIN_TIMEDIFF = 6;
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
private List<Channel> chanlist;
private List<Droplet> dropletList;
public Nloc (List<Channel> chanlist) {
this.chanlist = chanlist;
this.dropletList = new ArrayList<Droplet>();
}
public void addChannel(Channel chan) {
chanlist.add(chan);
}
public Pump getPump() {
Pump pump = null;
for (Channel chan : chanlist) {
if (chan instanceof Pump) pump = (Pump)chan;
}
return pump;
}
public Pump generateDropletSequence(List<String> modules) {
Pump pump = this.getPump();
int timediff = MIN_TIMEDIFF + 1;
List<Channel> modPath = this.getModulesByName(modules);
List<List<Channel>> pathlist = this.getAllPaths();
List<Channel> desiredPath = getDesiredPath(modPath, pathlist);
int noOfDroplets = this.getNumberOfDroplets(desiredPath);
pump.setSteps(noOfDroplets + (noOfDroplets - 1) * timediff);
for (int i = 0; i < noOfDroplets; i++){
if (!pump.containsDroplets()) {
Droplet droplet = new Droplet(DropletType.PAYLOAD,
new Position(pump,0));
} else {
Droplet droplet = new Droplet(DropletType.HEADER,
new Position(pump,i + i * timediff));
}
dropletList.add(droplet);
}
return pump;
}
public boolean allDropletsInSink() {
boolean allInSink = true;
for (Droplet dr : dropletList) {
allInSink &= dr.isInSink();
}
return allInSink;
}
public int getNumberOfDroplets(List<Channel> desiredPath) {
int numOfDroplets = 1;
ListIterator<Channel> iter = desiredPath.listIterator();
while(iter.hasNext()) {
Channel current = iter.next();
if (current.isBifurcation()) {
numOfDroplets *= (current.getChildrenList().indexOf(iter.next()) + 1);
iter.previous();
}
}
return numOfDroplets;
}
public List<Channel> getDesiredPath(List<Channel> modules,
List<List<Channel>> pathlist) {
List<Channel> found = new ArrayList<Channel>();
for (List<Channel> path : pathlist) {
if (path.containsAll(modules)) {
found = path;
}
}
return found;
}
private List<Channel> getModulesByName(List<String> names) {
List<Channel> ret = new ArrayList<Channel>();
for (String name : names) {
for (Channel ch : chanlist) {
if (ch instanceof Module && ((Module)ch).getModuleName().equals(name)) {
ret.add(ch);
}
}
}
return ret;
}
public List<List<Channel>> getAllPaths() {
List<List<Channel>> pl = new ArrayList<List<Channel>>();
List<Channel> path = new ArrayList<Channel>();
getAllPathsRecursive(this.getPump(), path, pl);
private void getAllPathsRecursive(Channel chan, List<Channel> path, List<List<Channel>> pathlist) {
if (chan.getChildrenList().isEmpty()) {
pathlist.add(path);
} else {
for (Channel ch : chan.getChildrenList()) {
getAllPathsRecursive(ch, new ArrayList<Channel>(path), pathlist);
}
}
}