MATSIM
HouseholdsWriterV10.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * HouseholdsWriterV1
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 package org.matsim.households;
21 
22 import org.apache.logging.log4j.LogManager;
23 import org.apache.logging.log4j.Logger;
24 import org.matsim.api.core.v01.Id;
26 import org.matsim.core.gbl.Gbl;
33 import org.matsim.vehicles.Vehicle;
34 
35 import java.io.IOException;
36 import java.io.UncheckedIOException;
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41 
46 public class HouseholdsWriterV10 extends MatsimXmlWriter implements HouseholdAlgorithm{
47  private static final Logger log = LogManager.getLogger( HouseholdsWriterV10.class ) ;
48 
49  private List<Tuple<String, String>> atts = new ArrayList<>();
51  private final Map<Class<?>,AttributeConverter<?>> attributeConverters = new HashMap<>();
52 
53  public HouseholdsWriterV10(Households households) {
54  this.households = households;
55  }
56 
57  public <T> void putAttributeConverter(Class<T> clazz, AttributeConverter<T> converter) {
58  this.attributeConverters.put( clazz , converter );
59  }
60 
61  public void putAttributeConverters( final Map<Class<?>, AttributeConverter<?>> converters ) {
62  this.attributeConverters.putAll( converters );
63  }
64 
65  public void writeFile(String filename) throws UncheckedIOException {
66  log.info( Gbl.aboutToWrite( " households", filename ) ) ;
67  this.openFileAndWritePreamble(filename);
68  this.writeHouseholds(this.households);
69  this.writeEndAndCloseFile();
70  }
71 
72  /*package*/ void openFileAndWritePreamble(String filename){
73  this.openFile(filename);
74  this.writeXmlHead();
75  this.writeHeader();
76  }
77 
78  /*package*/ void writeEndAndCloseFile(){
79 
81  this.close();
82  }
83 
84 
85  private void writeHeader(){
86  atts.clear();
89  atts.add(createTuple("xsi:schemaLocation", MATSIM_NAMESPACE + " " + DEFAULT_DTD_LOCATION + "households_v1.0.xsd"));
91  }
92 
93  private void writeHouseholds(Households basicHouseholds) throws UncheckedIOException {
94  Counter counter = new Counter("[HouseholdsWriter] wrote household # ");
95  for (Household h : basicHouseholds.getHouseholds().values()) {
96  this.writeHousehold(h);
97  counter.incCounter();
98  }
99  counter.printCounter();
100  }
101 
102  /*package*/ void writeHousehold(Household h) throws UncheckedIOException {
103  this.atts.clear();
104  atts.add(createTuple(HouseholdsSchemaV10Names.ID, h.getId().toString()));
106  if ((h.getMemberIds() != null) && !h.getMemberIds().isEmpty()){
107  this.writeMembers(h.getMemberIds());
108  }
109  if ((h.getVehicleIds() != null) && !h.getVehicleIds().isEmpty()) {
111  for (Id<Vehicle> id : h.getVehicleIds()){
112  atts.clear();
113  atts.add(createTuple(HouseholdsSchemaV10Names.REFID, id.toString()));
115  }
117  }
118  if (h.getIncome() != null){
119  this.writeIncome(h.getIncome());
120  }
121 
123  attributesWriter.putAttributeConverters(this.attributeConverters);
124  try {
125  this.writer.write(NL);
126  } catch (IOException e) {
127  e.printStackTrace();
128  }
129  attributesWriter.writeAttributes( "\t\t" , this.writer , h.getAttributes() );
130 
132  }
133 
134  private void writeIncome(Income income) throws UncheckedIOException {
135  atts.clear();
136  if (income.getCurrency() != null) {
137  atts.add(createTuple(HouseholdsSchemaV10Names.CURRENCY,income.getCurrency()));
138  }
139  atts.add(createTuple(HouseholdsSchemaV10Names.PERIOD, income.getIncomePeriod().toString()));
141  this.writeContent(Double.toString(income.getIncome()), true);
143  }
144 
145  private void writeMembers(List<Id<Person>> memberIds) throws UncheckedIOException {
147  for (Id<Person> id : memberIds){
148  atts.clear();
149  atts.add(createTuple(HouseholdsSchemaV10Names.REFID, id.toString()));
151  }
153  }
154 
155  @Override
156  public void run(Household household) {
157  writeHousehold(household);
158  }
159 
160 
161 
162 
163 }
List< Id< Vehicle > > getVehicleIds()
void putAttributeConverters(final Map< Class<?>, AttributeConverter<?>> converters)
void writeHouseholds(Households basicHouseholds)
final void writeContent(String content, boolean allowWhitespaces)
final void writeStartTag(String tagname, List< Tuple< String, String >> attributes)
static String aboutToWrite(String what, String filename)
Definition: Gbl.java:254
static Tuple< String, String > createTuple(String one, String two)
final Map< Class<?>, AttributeConverter<?> > attributeConverters
List< Id< Person > > getMemberIds()
void writeMembers(List< Id< Person >> memberIds)