Skip to content
Snippets Groups Projects
Nloc.java 4.58 KiB
Newer Older
package nloc;

import java.util.List;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Arrays;
public class Nloc {
  public static final int MIN_TIMEDIFF = 2;
  private List<Channel> chanlist;
  private List<Droplet> dropletList;

  public Nloc (List<Channel> chanlist) {
    this.chanlist = chanlist;
    this.dropletList = new ArrayList<Droplet>();
  }

Peter Wagenhuber's avatar
Peter Wagenhuber committed
//  public void addChannel(Channel chan) {
//    chanlist.add(chan);
//  }
//
  public void addChannel(Channel chan) {
    chanlist.add(chan);
  }

  public Sink getSink() {
    Sink sink = null;
    for (Channel chan : chanlist) {
      if (chan instanceof Sink) sink = (Sink)chan;
    }
    return sink;
  }

  public Pump getPump() {
    Pump pump = null;
    for (Channel chan : chanlist) {
      if (chan instanceof Pump) pump = (Pump)chan;
    }
    return pump;
  }

  public boolean simulate() {
    boolean works = true;
    while (!allDropletsInSink()) {
      try {
        this.moveDroplets();
      } catch (CoalescedDropletException e) {
        works = false;
        System.out.println(e.getDroplet());
  public List<Droplet> getDropletList() {
    return dropletList;
  }

  public void moveDroplets() throws CoalescedDropletException {
    for (Droplet dr : dropletList) {
      dr.move();
    }
    for (Droplet dr: dropletList) {
      if (dr.coalesce()) throw new CoalescedDropletException(dr);
  //public Pump generateDropletSequence(List<String> modules) 
  //  throws NlocStructureException {

  //  Pump pump = this.getPump();

  //  List<Channel> modPath = this.getModulesByName(modules);
  //  List<List<Channel>> pathlist = this.getAllPaths();
  //  List<Channel> desiredPath = getDesiredPath(modPath, pathlist);

  //  List<BFTableEntry> bftable = calcBFTable(desiredPath);
  //  BFTableEntry startConfiguration = bftable.get(0);

  //  pump = createDropletsequenceInPump(pump, startConfiguration);

  //  return pump;
  //}

  //private Pump createDropletsequenceInPump(Pump pump, 
  //    BFTableEntry startConfiguration) {
  //  int noOfDroplets = startConfiguration.getNoOfDroplets();
  //  int minTimediff = startConfiguration.getMinTimediff();

  //  for (int i = 0; i < noOfDroplets; i++){
  //    if (!pump.containsDroplets()) {
  //      dropletList.add(new Droplet(DropletType.PAYLOAD, 
  //          new Position(pump,1)));
  //    } else {
  //      dropletList.add(new Droplet(DropletType.HEADER, 
  //          new Position(pump,i + i * minTimediff + 1)));
  //    }
  //  }
  //  return pump;
  //}
  public int getPayloadPathlength(List<Channel> path) {
    int len = 0;
    for (Channel ch: path) {
      len += ch.getPSteps(); 
    }
    return len;
  }

  public int getHeaderPathlength(List<Channel> path) {
    int len = 0;
    for (Channel ch: path) {
      len += ch.getHSteps(); 
    }
    return len;
  }

  public boolean allDropletsInSink() {
    boolean allInSink = true;
    for (Droplet dr : dropletList) {
      allInSink &= dr.isInSink();
    }
    return allInSink;
  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;
  }

  public Channel getModuleByName(String name) {
    Channel found = null;
    for (Channel ch: chanlist) {
      if (ch.getName().equals(name)) {
        found = ch;
      } 
    }
    return found;
  }

  public List<Channel> getModulesByName(List<String> names) {
    List<Channel> ret = new ArrayList<Channel>();
    for (String name : names) {
      Channel ch = getModuleByName(name);
      if (name != null) {
        ret.add(ch);
  public List<List<Channel>> getAllPathsFromTo(Channel from, Channel to) {
    List<List<Channel>> pl = new ArrayList<List<Channel>>();
    List<Channel> path = new ArrayList<Channel>();
    getAllPathsRecursive(from, to, path, pl);
    return pl;
  }

  public List<List<Channel>> getAllPaths() {
    List<List<Channel>> pl = new ArrayList<List<Channel>>();
    List<Channel> path = new ArrayList<Channel>();
    getAllPathsRecursive(this.getPump(), this.getSink(), path, pl);
  private void getAllPathsRecursive(Channel chan, Channel end, List<Channel> path, List<List<Channel>> pathlist) {
      pathlist.add(path);
    } else {
      for (Channel ch : chan.getChildrenList()) {
        getAllPathsRecursive(ch, end, new ArrayList<Channel>(path), pathlist);