MATSIM
PopulationReader.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * MatsimPopulationReader.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 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 java.util.HashMap;
24 import java.util.Map;
25 import java.util.Stack;
26 
27 import org.apache.logging.log4j.LogManager;
28 import org.apache.logging.log4j.Logger;
29 import org.matsim.api.core.v01.Scenario;
38 import org.xml.sax.Attributes;
39 import org.xml.sax.SAXException;
40 
47 public final class PopulationReader extends MatsimXmlParser {
48 
49  private final static String PLANS = "plans.dtd"; // a special, inofficial case, handle it like plans_v0
50  private final static String PLANS_V0 = "plans_v0.dtd";
51  private final static String PLANS_V1 = "plans_v1.dtd";
52  private final static String PLANS_V4 = "plans_v4.dtd";
53  private final static String POPULATION_V5 = "population_v5.dtd";
54  private final static String POPULATION_V6 = "population_v6.dtd";
55 
56  private final String inputCRS;
57  private final String targetCRS;
58 
59  private MatsimXmlParser delegate = null;
60  private final Scenario scenario;
61 
62  private final Map<Class<?>, AttributeConverter<?>> attributeConverters = new HashMap<>();
63 
64  private static final Logger log = LogManager.getLogger(PopulationReader.class);
65 
66  public PopulationReader(final Scenario scenario) {
67  this(null, null, scenario);
68  }
69 
70  public PopulationReader(final String inputCRS, final String targetCRS, final Scenario scenario) {
71  this(inputCRS, targetCRS, scenario, false);
72  }
73 
74  /*deliberately package*/ PopulationReader(
75  final String inputCRS,
76  final String targetCRS,
77  final Scenario scenario,
78  boolean streaming ) {
79  super(ValidationType.DTD_ONLY);
80  if ( !streaming && scenario.getPopulation() instanceof StreamingPopulation ) {
81  throw new RuntimeException("MatsimPopulationReader called directly with an instance of StreamingPopulation "
82  + "in scenario. Call via StreamingPopulationReader or ask for help. kai, jul'16") ;
83  }
84  this.inputCRS = inputCRS;
85  this.targetCRS = targetCRS;
86  this.scenario = scenario;
87  }
88 
89  public void putAttributeConverter( final Class<?> clazz , AttributeConverter<?> converter ) {
90  attributeConverters.put( clazz , converter );
91  }
92 
93  public void putAttributeConverters( final Map<Class<?>, AttributeConverter<?>> converters ) {
94  attributeConverters.putAll( converters );
95  }
96 
97  @Override
98  public void startTag(final String name, final Attributes atts, final Stack<String> context) {
99  this.delegate.startTag(name, atts, context);
100  }
101 
102  @Override
103  public void endTag(final String name, final String content, final Stack<String> context) {
104  this.delegate.endTag(name, content, context);
105  }
106 
107  @Override
108  protected void setDoctype(final String doctype) {
109  super.setDoctype(doctype);
110 
111  final CoordinateTransformation transformation =
112  inputCRS == null ?
113  new IdentityTransformation() :
115  inputCRS,
116  targetCRS );
117 
118  switch ( doctype ) {
119  case POPULATION_V6:
120  if (FeatureFlags.useParallelIO()) {
121  this.delegate =
122  new ParallelPopulationReaderMatsimV6(
123  inputCRS,
124  targetCRS,
125  this.scenario);
126  ((ParallelPopulationReaderMatsimV6) delegate).putAttributeConverters(attributeConverters);
127  } else {
128  this.delegate =
129  new PopulationReaderMatsimV6(
130  inputCRS,
131  targetCRS,
132  this.scenario);
133  ((PopulationReaderMatsimV6) delegate).putAttributeConverters(attributeConverters);
134  }
135  log.info("using population_v6-reader.");
136  break;
137  case POPULATION_V5:
138  this.delegate =
139  new PopulationReaderMatsimV5(
140  transformation,
141  this.scenario);
142  log.info("using population_v5-reader.");
143  break;
144  case PLANS_V4:
145  // Replaced non-parallel reader with parallel implementation. cdobler, mar'12.
146  this.delegate =
147  new ParallelPopulationReaderMatsimV4(
148  transformation,
149  this.scenario);
150  log.info("using plans_v4-reader.");
151  break;
152  case PLANS_V1:
153  this.delegate =
154  new PopulationReaderMatsimV1(
155  transformation,
156  this.scenario);
157  log.info("using plans_v1-reader.");
158  break;
159  case PLANS_V0:
160  case PLANS:
161  this.delegate =
162  new PopulationReaderMatsimV0(
163  transformation,
164  this.scenario);
165  log.info("using plans_v0-reader.");
166  break;
167  default:
168  throw new IllegalArgumentException("No population reader available for doctype \"" + doctype + "\".");
169  }
170  }
171 
172  @Override
173  public void endDocument() {
174  try {
175  this.delegate.endDocument();
176  } catch (SAXException e) {
177  throw new RuntimeException(e);
178  }
179  if (targetCRS != null) {
181  }
182  }
183 }
void startTag(final String name, final Attributes atts, final Stack< String > context)
void endTag(final String name, final String content, final Stack< String > context)
abstract void startTag(String name, Attributes atts, Stack< String > context)
static CoordinateTransformation getCoordinateTransformation(final String fromSystem, final String toSystem)
static< T extends MatsimToplevelContainer &Attributable > void putCRS(T container, String CRS)
final Map< Class<?>, AttributeConverter<?> > attributeConverters
PopulationReader(final String inputCRS, final String targetCRS, final Scenario scenario)
static boolean useParallelIO()
abstract void endTag(String name, String content, Stack< String > context)
void putAttributeConverter(final Class<?> clazz, AttributeConverter<?> converter)
void putAttributeConverters(final Map< Class<?>, AttributeConverter<?>> converters)