MATSIM
NetworkSegmentDoubleLinks.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * NetworkSegmentDoubleLinks.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2008 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.network.algorithms;
22 
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25 import org.matsim.api.core.v01.Id;
30 
31 import java.util.*;
32 
40 public final class NetworkSegmentDoubleLinks implements NetworkRunnable {
41  private static final Logger log = LogManager.getLogger(NetworkSegmentDoubleLinks.class);
42 
43  private Network network = null;
44 
45  private int dblLinks = 0;
46  private int trblLinks = 0; // what does trbl stand for?? please document! TODO [GL] documentation
47  private int qtblLinks = 0; // what does qtbl stand for?? please document!
48  private int ntblLinks = 0; // what does ntbl stand for?? pleaes document!
49 
51  // run methods
53 
54  @Override
55  public void run(final Network network) {
56  this.network = network;
57  log.info(" running " + this.getClass().getName() + " algorithm...");
58 
59  Queue<Node> nodes = new LinkedList<>(network.getNodes().values());
60  while (nodes.peek() != null) {
61  Node n = nodes.poll();
62  HashMap<Id<Node>, List<Link>> toNodesMap = new HashMap<>();
63  for (Link l : n.getOutLinks().values()) {
64  List<Link> links = toNodesMap.get(l.getToNode().getId());
65  if (links != null) {
66  links.add(l);
67  } else {
68  links = new ArrayList<>();
69  links.add(l);
70  toNodesMap.put(l.getToNode().getId(), links);
71  }
72  }
73  if (toNodesMap.size() > 0) {
74  handleDblLinks(toNodesMap);
75  }
76  }
77 
78  log.info("handled: ");
79  log.info("\t" + this.dblLinks + " dblLinks.");
80  log.info("\t" + this.trblLinks + " trblLinks."); // what does trbl stand for?? please document! TODO [GL] documentation
81  log.info("\t" + this.qtblLinks + " qtblLinks.");
82  log.info("\t" + this.ntblLinks + " ntblLinks.");
83  log.info("done.");
84  }
85 
86  private void handleDblLinks(HashMap<Id<Node>, List<Link>> toNodesMap) {
87  for (List<Link> vec : toNodesMap.values()) {
88  switch (vec.size()) {
89  case 1:
90  break;
91  case 2:
92  this.dblLinks++;
93  break;
94  case 3:
95  this.trblLinks++;
96  break;
97  case 4:
98  this.qtblLinks++;
99  break;
100  default:
101  this.ntblLinks++;
102  }
103 
104  for (int i = 1; i < vec.size(); i++) {
105  splitLink(vec.get(i));
106  }
107  }
108  }
109 
110  private void splitLink(Link link) {
111  this.network.removeLink(link.getId());
112  double length = link.getLength()/2.0;
113  double freespeed = link.getFreespeed();
114  double capacity = link.getCapacity();
115  double permlanes = link.getNumberOfLanes();
116 
117  Node medianNode = this.network.getFactory().createNode(getNewNodeId(), link.getCoord());
118  this.network.addNode(medianNode);
119 
120  Link tmpLink = this.network.getFactory().createLink(link.getId(), link.getFromNode(), medianNode);
121  tmpLink.setLength(length);
122  tmpLink.setFreespeed(freespeed);
123  tmpLink.setCapacity(capacity);
124  tmpLink.setNumberOfLanes(permlanes);
125  this.network.addLink(tmpLink);
126 
127  tmpLink = this.network.getFactory().createLink(getNewLinkId(), medianNode, link.getToNode());
128  tmpLink.setLength(length);
129  tmpLink.setFreespeed(freespeed);
130  tmpLink.setCapacity(capacity);
131  tmpLink.setNumberOfLanes(permlanes);
132  this.network.addLink(tmpLink);
133  }
134 
135  private Id<Link> getNewLinkId() {
136  Random r = new Random();
137  Id<Link> id = Id.create(r.nextInt(Integer.MAX_VALUE), Link.class);
138  while (this.network.getLinks().get(id) != null) {
139  id = Id.create(r.nextInt(Integer.MAX_VALUE), Link.class);
140  }
141  return id;
142  }
143 
144  private Id<Node> getNewNodeId() {
145  Random r = new Random();
146  Id<Node> id = Id.create(r.nextInt(Integer.MAX_VALUE), Node.class);
147  while (this.network.getNodes().get(id) != null) {
148  id = Id.create(r.nextInt(Integer.MAX_VALUE), Node.class);
149  }
150  return id;
151  }
152 
153 }
Map< Id< Node >, ? extends Node > getNodes()
Link removeLink(final Id< Link > linkId)
static< T > Id< T > create(final long key, final Class< T > type)
Definition: Id.java:68
Link createLink(final Id< Link > id, final Node fromNode, final Node toNode)
Map< Id< Link >, ? extends Link > getLinks()
Node createNode(final Id< Node > id, final Coord coord)
Map< Id< Link >, ? extends Link > getOutLinks()