MATSIM
NetworkFilterManager.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2008 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package org.matsim.core.network.filter;
21 
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map.Entry;
25 
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
28 import org.matsim.api.core.v01.Id;
34 
45 public final class NetworkFilterManager {
46 
47  private static final Logger log = LogManager.getLogger(NetworkFilterManager.class);
48 
49  private final Network network;
50 
51  private final List<NetworkLinkFilter> linkFilters;
52 
53  private final List<NetworkNodeFilter> nodeFilters;
54 
56 
57  public NetworkFilterManager(final Network net, NetworkConfigGroup networkConfigGroup) {
58  this.network = net;
59  this.networkConfigGroup = networkConfigGroup;
60  this.linkFilters = new ArrayList<>();
61  this.nodeFilters = new ArrayList<>();
62  }
63 
65  this.linkFilters.add(f);
66  }
67 
69  this.nodeFilters.add(f);
70  }
71 
72  private Node addNode(Network net, Node n){
73  Node newNode = net.getFactory().createNode(n.getId(), n.getCoord());
74  net.addNode(newNode);
75  return newNode;
76  }
77 
78  private void addLink(Network net, Link l){
79  Id<Node> fromId = l.getFromNode().getId();
80  Id<Node> toId = l.getToNode().getId();
81  Node from;
82  Node to;
83  Node nn;
84  //check if from node already exists
85  if (! net.getNodes().containsKey(fromId)) {
86  nn = this.network.getNodes().get(fromId);
87  from = this.addNode(net, nn);
88  }
89  else {
90  from = net.getNodes().get(fromId);
91  }
92  //check if to node already exists
93  if (! net.getNodes().containsKey(toId)){
94  nn = this.network.getNodes().get(toId);
95  to = this.addNode(net, nn);
96  }
97  else {
98  to = net.getNodes().get(toId);
99  }
100  Link ll = net.getFactory().createLink(l.getId(), from, to);
102  ll.setCapacity(l.getCapacity());
103  ll.setFreespeed(l.getFreespeed());
104  ll.setLength(l.getLength());
106  for(Entry<String, Object> entry : l.getAttributes().getAsMap().entrySet()) {
107  ll.getAttributes().putAttribute(entry.getKey(), entry.getValue());
108  }
109  net.addLink(ll);
110  }
111 
117  log.info("applying filters to network with " + network.getNodes().size() + " nodes and "
118  + network.getLinks().size() + " links...");
119  Network net = NetworkUtils.createNetwork(networkConfigGroup);
120  if (!this.nodeFilters.isEmpty()) {
121  for (Node n : this.network.getNodes().values()) {
122  if (nodeFilters.stream().allMatch(f -> f.judgeNode(n))) {
123  this.addNode(net, n);
124  }
125  }
126  }
127  if (!this.linkFilters.isEmpty()) {
128  for (Link l : this.network.getLinks().values()) {
129  if (linkFilters.stream().allMatch(f -> f.judgeLink(l))) {
130  this.addLink(net, l);
131  }
132  }
133  }
134  log.info("filtered network contains " + net.getNodes().size() + " nodes and " + net.getLinks().size() + " links.");
135  return net;
136  }
137 
138 }
Map< Id< Node >, ? extends Node > getNodes()
Link createLink(final Id< Link > id, final Node fromNode, final Node toNode)
Object putAttribute(final String attribute, final Object value)
Map< Id< Link >, ? extends Link > getLinks()
Node createNode(final Id< Node > id, final Coord coord)
NetworkFilterManager(final Network net, NetworkConfigGroup networkConfigGroup)