MATSIM
StreamingPopulationReader.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * Plans.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007, 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.population.io;
22 
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25 import org.matsim.api.core.v01.Id;
26 import org.matsim.api.core.v01.Scenario;
31 import org.matsim.core.config.Config;
37 
38 import java.io.InputStream;
39 import java.net.URL;
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.Map;
43 
44 public final class StreamingPopulationReader implements MatsimReader {
45  private static final Logger log = LogManager.getLogger(StreamingPopulationReader.class);
46 
48  private final StreamingPopulation pop ;
49  private int cnt;
50  private final Map<Class<?>, AttributeConverter<?>> attributeConverters = new HashMap<>();
51 
52  // algorithms over plans
53  private final ArrayList<PersonAlgorithm> personAlgos = new ArrayList<>();
54 
55  public StreamingPopulationReader(Scenario scenario ) {
56  // should we convert to global by default or not? Optimal seems to depend on usecase...
57  this( null, null, scenario ) ;
58  }
59 
60  public StreamingPopulationReader(String inputCRS, String targetCRS, Scenario scenario ) {
61  if ( scenario instanceof MutableScenario ) {
62  pop = new StreamingPopulation( scenario.getConfig() ) ;
63  ((MutableScenario) scenario).setPopulation(pop);
64  reader = new PopulationReader( inputCRS, targetCRS, scenario, true) ;
65  } else {
66  throw new RuntimeException("scenario given into this class needs to be an instance of MutableScenario.") ;
67  }
68  }
69 
70  public void putAttributeConverter(final Class<?> clazz, AttributeConverter<?> converter) {
71  this.attributeConverters.put(clazz, converter);
72  }
73 
74  public void putAttributeConverters(final Map<Class<?>, AttributeConverter<?>> converters) {
75  this.attributeConverters.putAll(converters);
76  }
77 
78  Population getStreamingPopulation() {
79  return pop ;
80  }
81  @Override public void readFile(String filename) {
82  reader.putAttributeConverters(this.attributeConverters);
83  reader.readFile(filename);
84  }
85 
86  @Override
87  public void readURL( URL url ) {
88  reader.putAttributeConverters(this.attributeConverters);
89  reader.parse( url ) ;
90  }
91 
92  public void parse(InputStream is) {
93  reader.parse(is);
94  }
95 
96  public void parse(URL url) {
97  reader.parse( url );
98  }
99 
101  // algorithms
103 
104  public final void clearAlgorithms() {
105  this.personAlgos.clear();
106  }
107  public final void addAlgorithm(final PersonAlgorithm algo) {
108  this.personAlgos.add(algo);
109  }
110 
111 
113  // get methods
115 
116 
117  @Deprecated // dec'16
118  public final boolean isStreaming() {
119  return true ;
120  }
121 
122  @Deprecated // dec'16
123  final void setIsStreaming(final boolean isStreaming) {
124  if ( isStreaming==false ) {
125  log.error( "streaming == false currently not supported with this approach; please program it yourself; something like" ) ;
126  log.error( "for ( Person person : pop.getPersons().values() ) {" ) ;
127  log.error( " personAlgo.run( person ); " ) ;
128  log.error( "}" );
129  throw new RuntimeException("streaming == false currently not supported with this approach; see log statements" ) ;
130  }
131  }
132  @Override public final String toString() {
133  return "[nof_plansalgos=" + this.personAlgos.size() + "]";
134  }
135 
136  final class StreamingPopulation implements Population {
137  private Population delegate ;
138 
139  StreamingPopulation(Config config) {
140  delegate = PopulationUtils.createPopulation(config) ;
141  }
142 
143  @Override
144  public final void addPerson(final Person p) {
145 
146 
147  cnt++ ;
148 
149 // if (!this.isStreaming) {
150 // // streaming is off, just add the person to our list
151 // pop.addPerson(p);
152 // } else {
153  // streaming is on, run algorithm on the person and write it to file.
154 
155  /* Add Person to map, for algorithms might reference to the person
156  * with "agent = population.getPersons().get(personId);"
157  * remove it after running the algorithms! */
158  delegate.addPerson(p);
159  // (yyyyyy do we really need this? Also, does it make a lot of sense?
160  // pop.getPerson( current ) will then work, but pop.getPerson( other ) will not work. Might be better to just get rid of this
161  // completely, no? kai, jul'16)
162 
163  // run algos
164  for (PersonAlgorithm algo : personAlgos) {
165  algo.run(p);
166  }
167 
168  // remove again as we are streaming
169  delegate.removePerson(p.getId());
170 // }
171 
172  }
173  @Override public PopulationFactory getFactory() {
174  return delegate.getFactory();
175  }
176  @Override public String getName() {
177  return delegate.getName();
178  }
179  @Override public void setName(String name) {
180  delegate.setName(name);
181  }
182  @Override public Map<Id<Person>, ? extends Person> getPersons() {
183  return delegate.getPersons();
184  }
185  @Override public Person removePerson(Id<Person> personId) {
186  return delegate.removePerson(personId);
187  }
188 
189  @Override
190  public Attributes getAttributes() {
191  return delegate.getAttributes();
192  }
193  }
194 
195  @Deprecated // do not use from outside; will be removed as public functionality eventually yy
196  public void printPlansCount() {
197  log.info("processed " + cnt + " persons.");
198  }
199 
200 }
void putAttributeConverters(final Map< Class<?>, AttributeConverter<?>> converters)
StreamingPopulationReader(String inputCRS, String targetCRS, Scenario scenario)
void putAttributeConverter(final Class<?> clazz, AttributeConverter<?> converter)
Map< Id< Person >,? extends Person > getPersons()
static Population createPopulation(Config config)
final void readFile(final String filename)
Person removePerson(final Id< Person > personId)
final Map< Class<?>, AttributeConverter<?> > attributeConverters
void putAttributeConverters(final Map< Class<?>, AttributeConverter<?>> converters)