MATSIM
EventsConverterXML.java
Go to the documentation of this file.
1 /*
2  * *********************************************************************** *
3  * * project: org.matsim.*
4  * * DefaultControlerModules.java
5  * * *
6  * * *********************************************************************** *
7  * * *
8  * * copyright : (C) 2014 by the members listed in the COPYING, *
9  * * LICENSE and WARRANTY file. *
10  * * email : info at matsim dot org *
11  * * *
12  * * *********************************************************************** *
13  * * *
14  * * This program is free software; you can redistribute it and/or modify *
15  * * it under the terms of the GNU General Public License as published by *
16  * * the Free Software Foundation; either version 2 of the License, or *
17  * * (at your option) any later version. *
18  * * See also COPYING, LICENSE and WARRANTY file *
19  * * *
20  * * ***********************************************************************
21  */
22 package org.matsim.core.events;
23 
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Stack;
27 
28 import org.matsim.api.core.v01.Id;
41 import org.matsim.vehicles.Vehicle;
42 import org.xml.sax.Attributes;
43 
55 public final class EventsConverterXML extends MatsimXmlParser{
56 
57  private static final String EVENT = "event";
58  private static final String ATTRIBUTE_PERSON = "person";
59 
60  private final EventsManager events;
62  private boolean containsVehicleLeavesTrafficEvents = false;
63  private Map<Id<Person>, Id<Vehicle>> driverToVeh = new HashMap<>();
64  private Map<Id<Person>, String> personToLegMode = new HashMap<>();
65 
66  public EventsConverterXML(final EventsManager events) {
68  this.events = events;
69  this.setValidating(false);// events-files have no DTD, thus they cannot validate
70  this.basicEventsReader = new EventsReaderXMLv1(events);
71  }
72 
73 
74  @Override
75  public void startTag(String name, Attributes atts, Stack<String> context) {
76  if ( EVENT.equals(name)) {
77  // (we are essentially ignoring the xml header)
78 
79  double time = Double.parseDouble(atts.getValue("time"));
80  String eventType = atts.getValue("type");
81 
82  switch (eventType){
84  // remember person to (leg) mode relation
85  personToLegMode.put(Id.createPersonId(atts.getValue(PersonDepartureEvent.ATTRIBUTE_PERSON)), atts.getValue(PersonDepartureEvent.ATTRIBUTE_LEGMODE));
86  this.basicEventsReader.startTag(name, atts, context);
87  break;
89  case "wait2link":
90  // (assumes that the reader has already converted the wait2link events)
91  Id<Person> driverId = Id.createPersonId(atts.getValue( HasPersonId.ATTRIBUTE_PERSON ) );
92  Id<Vehicle> vehicleId;
93  if (atts.getValue(VehicleEntersTrafficEvent.ATTRIBUTE_VEHICLE) == null || atts.getValue(VehicleEntersTrafficEvent.ATTRIBUTE_VEHICLE).equals("null")){
94  // use the person id as vehicle id
95  vehicleId = Id.create(driverId, Vehicle.class);
96  } else {
97  vehicleId = Id.create(atts.getValue(VehicleEntersTrafficEvent.ATTRIBUTE_VEHICLE), Vehicle.class);
98  }
99  assert vehicleId != null;
100 
101  String networkMode = atts.getValue(VehicleEntersTrafficEvent.ATTRIBUTE_NETWORKMODE);
102  // use the (leg) mode from the departure event if this event does not contain a (network) mode
103  if (networkMode == null){
104  networkMode = personToLegMode.get(driverId);
105  }
106 
107  // remember driver to vehicle relation
108  driverToVeh.put(driverId, vehicleId);
109  // process event with correct vehicleId
110  this.events.processEvent(new VehicleEntersTrafficEvent(time, driverId, Id.createLinkId(atts.getValue(VehicleEntersTrafficEvent.ATTRIBUTE_LINK)),
111  vehicleId, networkMode, 1.0));
112  break;
114  if (atts.getValue(LinkEnterEvent.ATTRIBUTE_VEHICLE) == null || atts.getValue(LinkEnterEvent.ATTRIBUTE_VEHICLE).equals("null")){
115  // create an link enter event with the correct vehicle id
116  this.events.processEvent(new LinkEnterEvent(time, driverToVeh.get(Id.createPersonId(atts.getValue(ATTRIBUTE_PERSON))),
117  Id.create(atts.getValue(LinkEnterEvent.ATTRIBUTE_LINK), Link.class)));
118  } else {
119  // the event already contains the vehicle id and can be processed normally
120  this.basicEventsReader.startTag(name, atts, context);
121  }
122  break;
124  if (atts.getValue(LinkLeaveEvent.ATTRIBUTE_VEHICLE) == null || atts.getValue(LinkLeaveEvent.ATTRIBUTE_VEHICLE).equals("null")){
125  // create an link leave event with the correct vehicle id
126  final Id<Person> personId = Id.createPersonId(atts.getValue(ATTRIBUTE_PERSON));
127  assert personId != null;
128  final Id<Link> linkId = Id.create(atts.getValue(LinkLeaveEvent.ATTRIBUTE_LINK), Link.class);
129  assert linkId != null;
130  vehicleId = driverToVeh.get(personId);
131  assert vehicleId != null;
132  this.events.processEvent(new LinkLeaveEvent(time, vehicleId, linkId));
133  } else {
134  // the event already contains the vehicle id and can be processed normally
135  this.basicEventsReader.startTag(name, atts, context);
136  }
137  break;
139  this.containsVehicleLeavesTrafficEvents = true;
140  this.basicEventsReader.startTag(name, atts, context);
141  break;
143  if (driverToVeh.containsKey(Id.createPersonId(atts.getValue(PersonLeavesVehicleEvent.ATTRIBUTE_PERSON))) && !this.containsVehicleLeavesTrafficEvents){
144  /* if the person is a driver and the vehicle leaves traffic event is missing ...
145  * ... process the event later (during person arrival) to be able to collect
146  * some information about the missing vehicle leaves traffic event before */
147  } else {
148  // process normally
149  this.basicEventsReader.startTag(name, atts, context);
150  }
151  break;
154  Id<Link> linkId = Id.createLinkId(atts.getValue(PersonArrivalEvent.ATTRIBUTE_LINK));
155  String mode = atts.getValue(PersonArrivalEvent.ATTRIBUTE_LEGMODE);
156 
157  // create a vehicle leaves traffic event if it is missing
158  if (driverToVeh.containsKey(personId) && !this.containsVehicleLeavesTrafficEvents){
159 // Id<Vehicle> vehicleIdOfDriver = driverToVeh.get(personId);
160  // remove the person-car-mapping, otherwise every following (even non-vehicle) leg produces leave traffic/vehicle events
161  Id<Vehicle> vehicleIdOfDriver = driverToVeh.remove(personId);
162 
163  this.events.processEvent(new VehicleLeavesTrafficEvent(time, personId, linkId, vehicleIdOfDriver, mode, 1.0));
164  this.events.processEvent(new PersonLeavesVehicleEvent(time, personId, vehicleIdOfDriver));
165  }
166  // process the PersonArrivalEvent normally, independently of missing VehicleLeavesTrafficEvents
167  this.basicEventsReader.startTag(name, atts, context);
168  break;
169  default:
170  this.basicEventsReader.startTag(name, atts, context);
171  break;
172  }
173  }
174  }
175 
176  @Override
177  public void endTag(String name, String content, Stack<String> context) {
178  }
179 
180 }
static< T > Id< T > get(int index, final Class< T > type)
Definition: Id.java:112
final void setValidating(final boolean validateXml)
static Id< Link > createLinkId(final long key)
Definition: Id.java:202
static< T > Id< T > create(final long key, final Class< T > type)
Definition: Id.java:68
Map< Id< Person >, Id< Vehicle > > driverToVeh
EventsConverterXML(final EventsManager events)
void startTag(final String name, final Attributes atts, final Stack< String > context)
static Id< Person > createPersonId(final long key)
Definition: Id.java:193
void endTag(String name, String content, Stack< String > context)
void startTag(String name, Attributes atts, Stack< String > context)