MATSIM
ParallelPopulationReaderMatsimV4Runner.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * ParallelPopulationReaderMatsimV4Runner.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2012 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.List;
24 import java.util.concurrent.BlockingQueue;
25 
26 import org.matsim.api.core.v01.Scenario;
34 import org.xml.sax.Attributes;
35 
43 /* deliberately package */ class ParallelPopulationReaderMatsimV4Runner extends PopulationReaderMatsimV4 implements Runnable {
44 
45  private final BlockingQueue<List<Tag>> queue;
46 
47  public ParallelPopulationReaderMatsimV4Runner(
48  final CoordinateTransformation coordinateTransformation,
49  final Scenario scenario,
50  final BlockingQueue<List<Tag>> queue) {
51  super(coordinateTransformation , scenario);
52  this.queue = queue;
53  }
54 
55  @Override
56  public void run() {
57  /*
58  * The thread will go on with the parsing until an EndProcessingTag is found,
59  * which calls "return".
60  */
61  while (true) {
62  try {
63  List<Tag> tags;
64  tags = queue.take();
65 
66  for (Tag tag : tags) {
67  if (tag instanceof PersonTag) {
68  this.currperson = ((PersonTag) tag).person;
69  } else if (tag instanceof StartTag) {
70  // if its is a person tag, we use the startPerson method from this class
71  if (PERSON.equals(tag.name)) {
72  startPerson(((StartTag) tag).atts);
73  }
74  // otherwise hand the tag over to the super class
75  else {
76  this.startTag(tag.name, ((StartTag) tag).atts, tag.context);
77  }
78  } else if (tag instanceof EndTag) {
79  /*
80  * If its is a person tag, we reset the current person. We do not hand the
81  * tag over to the superclass because the person has already been added
82  * to the population.
83  */
84  if (PERSON.equals(tag.name)) {
85  this.currperson = null;
86  }
87  // otherwise hand the tag over to the super class
88  else {
89  this.endTag(tag.name, ((EndTag) tag).content, tag.context);
90  }
91  } else if (tag instanceof EndProcessingTag) {
92  return;
93  }
94  }
95  } catch (InterruptedException e) {
96  throw new RuntimeException(e);
97  }
98  }
99  }
100 
101  private void startPerson(final Attributes atts) {
102  String ageString = atts.getValue("age");
103 // int age = Integer.MIN_VALUE;
104  Integer age = null ;
105  if (ageString != null) age = Integer.parseInt(ageString);
106  PersonUtils.setAge(this.currperson, age);
107  PersonUtils.setSex(this.currperson, atts.getValue("sex"));
108  PersonUtils.setLicence(this.currperson, atts.getValue("license"));
109  PersonUtils.setCarAvail(this.currperson, atts.getValue("car_avail"));
110  String employed = atts.getValue("employed");
111  if (employed == null) {
112  PersonUtils.setEmployed(this.currperson, null);
113  } else {
114  PersonUtils.setEmployed(this.currperson, "yes".equals(employed));
115  }
116  }
117 }
abstract void startTag(String name, Attributes atts, Stack< String > context)