MATSIM
EventsReaderXMLv1.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * EventsReaderXMLv1.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.events;
22 
23 import org.matsim.api.core.v01.Coord;
24 import org.matsim.api.core.v01.Id;
53 import org.matsim.vehicles.Vehicle;
54 import org.xml.sax.Attributes;
55 import org.xml.sax.SAXException;
56 
57 import java.util.HashMap;
58 import java.util.Map;
59 import java.util.Stack;
60 
61 public final class EventsReaderXMLv1 extends MatsimXmlEventsParser {
62 
63  static public final String EVENT = "event";
64 
65  private final EventsManager events;
66  private final Map<String, MatsimEventsReader.CustomEventMapper> customEventMappers = new HashMap<>();
67 
68  public EventsReaderXMLv1(final EventsManager events) {
69  this.events = events;
70  this.setValidating(false);// events-files have no DTD, thus they cannot validate
71  }
72  @Override
73  public void addCustomEventMapper(String eventType, MatsimEventsReader.CustomEventMapper cem) {
74  customEventMappers.put(eventType, cem);
75  }
76 
77  @Override
78  public void startTag(final String name, final Attributes atts, final Stack<String> context) {
79  if (EVENT.equals(name)) {
80  startEvent(atts);
81  }
82  }
83 
84  @Override
85  public void characters(char[] ch, int start, int length) throws SAXException {
86  // ignore characters to prevent OutOfMemoryExceptions
87  /* the events-file only contains empty tags with attributes,
88  * but without the dtd or schema, all whitespace between tags is handled
89  * by characters and added up by super.characters, consuming huge
90  * amount of memory when large events-files are read in.
91  */
92  }
93 
94  @Override
95  public void endTag(final String name, final String content, final Stack<String> context) {
96  }
97 
98  private void startEvent(final Attributes atts) {
99  double time = Double.parseDouble(atts.getValue("time"));
100  String eventType = atts.getValue("type");
101 
102  // === material related to wait2link below here ===
103  if (LinkLeaveEvent.EVENT_TYPE.equals(eventType)) {
104  this.events.processEvent(new LinkLeaveEvent(time,
105  Id.create(atts.getValue(LinkLeaveEvent.ATTRIBUTE_VEHICLE), Vehicle.class),
106  Id.create(atts.getValue(LinkLeaveEvent.ATTRIBUTE_LINK), Link.class)
107  // had driver id in previous version
108  ));
109  } else if (LinkEnterEvent.EVENT_TYPE.equals(eventType)) {
110  this.events.processEvent(new LinkEnterEvent(time,
111  Id.create(atts.getValue(LinkEnterEvent.ATTRIBUTE_VEHICLE), Vehicle.class),
112  Id.create(atts.getValue(LinkEnterEvent.ATTRIBUTE_LINK), Link.class)
113  // had driver id in previous version
114  ));
115  } else if (VehicleEntersTrafficEvent.EVENT_TYPE.equals(eventType) ) {
116  // (this is the new version, marked by the new events name)
117 
118  this.events.processEvent(new VehicleEntersTrafficEvent(time,
119  Id.create(atts.getValue(HasPersonId.ATTRIBUTE_PERSON), Person.class),
120  Id.create(atts.getValue(VehicleEntersTrafficEvent.ATTRIBUTE_LINK), Link.class),
123  Double.parseDouble( atts.getValue( VehicleEntersTrafficEvent.ATTRIBUTE_POSITION) )
124  ));
125  } else if ( "wait2link".equals(eventType) ) {
126  // (this is the old version, marked by the old events name)
127 
128  // retrofit vehicle Id:
129  Id<Vehicle> vehicleId ;
130  if ( atts.getValue(VehicleEntersTrafficEvent.ATTRIBUTE_VEHICLE) != null ) {
131  vehicleId = Id.create( atts.getValue(VehicleEntersTrafficEvent.ATTRIBUTE_VEHICLE), Vehicle.class ) ;
132  } else {
133  // for the old events type, we set the vehicle id to the driver id if the vehicle id does not exist:
134  vehicleId = Id.create(atts.getValue(HasPersonId.ATTRIBUTE_PERSON), Vehicle.class);
135  }
136  // retrofit position:
137  double position ;
138  if ( atts.getValue( VehicleEntersTrafficEvent.ATTRIBUTE_POSITION)!=null ) {
139  position = Double.parseDouble( atts.getValue( VehicleEntersTrafficEvent.ATTRIBUTE_POSITION) ) ;
140  } else {
141  position = 1.0 ;
142  }
143  this.events.processEvent(new VehicleEntersTrafficEvent(time,
144  Id.create(atts.getValue(HasPersonId.ATTRIBUTE_PERSON), Person.class),
145  Id.create(atts.getValue(VehicleEntersTrafficEvent.ATTRIBUTE_LINK), Link.class),
146  vehicleId,
148  position
149  ));
150  } else if (VehicleLeavesTrafficEvent.EVENT_TYPE.equals(eventType)) {
151  this.events.processEvent(new VehicleLeavesTrafficEvent(time,
153  Id.create(atts.getValue(VehicleLeavesTrafficEvent.ATTRIBUTE_LINK), Link.class),
154  atts.getValue(VehicleLeavesTrafficEvent.ATTRIBUTE_VEHICLE) == null ? null : Id.create(atts.getValue(VehicleLeavesTrafficEvent.ATTRIBUTE_VEHICLE), Vehicle.class),
156  Double.parseDouble( atts.getValue( VehicleLeavesTrafficEvent.ATTRIBUTE_POSITION) )
157  ));
158  }
159  // === material related to wait2link above here
160  else if (ActivityEndEvent.EVENT_TYPE.equals(eventType)) {
161  Coord coord = null ;
162  if ( atts.getValue( Event.ATTRIBUTE_X )!=null ) {
163  double xx = Double.parseDouble( atts.getValue( Event.ATTRIBUTE_X ) ) ;
164  double yy = Double.parseDouble( atts.getValue( Event.ATTRIBUTE_Y ) ) ;
165  coord = new Coord( xx, yy ) ;
166  }
167  this.events.processEvent(new ActivityEndEvent(
168  time,
169  Id.create(atts.getValue(HasPersonId.ATTRIBUTE_PERSON), Person.class),
170  Id.create(atts.getValue(HasLinkId.ATTRIBUTE_LINK), Link.class),
171  atts.getValue(HasFacilityId.ATTRIBUTE_FACILITY) == null ? null : Id.create(atts.getValue(HasFacilityId.ATTRIBUTE_FACILITY),
172  ActivityFacility.class),
173  atts.getValue(ActivityEndEvent.ATTRIBUTE_ACTTYPE),
174  coord));
175  } else if (ActivityStartEvent.EVENT_TYPE.equals(eventType)) {
176  Coord coord = null ;
177  if ( atts.getValue( Event.ATTRIBUTE_X )!=null ) {
178  double xx = Double.parseDouble( atts.getValue( Event.ATTRIBUTE_X ) ) ;
179  double yy = Double.parseDouble( atts.getValue( Event.ATTRIBUTE_Y ) ) ;
180  coord = new Coord( xx, yy ) ;
181  }
182  this.events.processEvent(new ActivityStartEvent(
183  time,
184  Id.create(atts.getValue( HasPersonId.ATTRIBUTE_PERSON ), Person.class ),
185  Id.create(atts.getValue( HasLinkId.ATTRIBUTE_LINK ), Link.class ),
186  atts.getValue( HasFacilityId.ATTRIBUTE_FACILITY ) == null ? null : Id.create(atts.getValue(
188  atts.getValue(ActivityStartEvent.ATTRIBUTE_ACTTYPE ),
189  coord ) ) ;
190  } else if (PersonArrivalEvent.EVENT_TYPE.equals(eventType)) {
191  String legMode = atts.getValue(PersonArrivalEvent.ATTRIBUTE_LEGMODE);
192  String mode = legMode == null ? null : legMode.intern();
193  this.events.processEvent(new PersonArrivalEvent(time, Id.create(atts.getValue(PersonArrivalEvent.ATTRIBUTE_PERSON), Person.class), Id.create(atts.getValue(PersonArrivalEvent.ATTRIBUTE_LINK), Link.class), mode));
194  } else if (PersonDepartureEvent.EVENT_TYPE.equals(eventType)) {
195  String legMode = atts.getValue(PersonDepartureEvent.ATTRIBUTE_LEGMODE);
196  String canonicalLegMode = legMode == null ? null : legMode.intern();
197  String routingMode = atts.getValue(PersonDepartureEvent.ATTRIBUTE_ROUTING_MODE);
198  String canonicalRoutingMode = routingMode == null ? null : routingMode.intern();
199  this.events.processEvent(new PersonDepartureEvent(time, Id.create(atts.getValue(PersonDepartureEvent.ATTRIBUTE_PERSON), Person.class), Id.create(atts.getValue(PersonDepartureEvent.ATTRIBUTE_LINK), Link.class), canonicalLegMode, canonicalRoutingMode));
200  } else if (PersonStuckEvent.EVENT_TYPE.equals(eventType)) {
201  String legMode = atts.getValue(PersonStuckEvent.ATTRIBUTE_LEGMODE);
202  String mode = legMode == null ? null : legMode.intern();
203  String linkIdString = atts.getValue(PersonStuckEvent.ATTRIBUTE_LINK);
204  Id<Link> linkId = linkIdString == null ? null : Id.create(linkIdString, Link.class); // linkId is optional
205  this.events.processEvent(new PersonStuckEvent(time, Id.create(atts.getValue(PersonStuckEvent.ATTRIBUTE_PERSON), Person.class), linkId, mode));
206  } else if (VehicleAbortsEvent.EVENT_TYPE.equals(eventType)) {
207  String linkIdString = atts.getValue(VehicleAbortsEvent.ATTRIBUTE_LINK);
208  Id<Link> linkId = linkIdString == null ? null : Id.create(linkIdString, Link.class);
209  this.events.processEvent(new VehicleAbortsEvent(time, Id.create(atts.getValue(VehicleAbortsEvent.ATTRIBUTE_VEHICLE), Vehicle.class), linkId));
210  } else if (PersonMoneyEvent.EVENT_TYPE.equals(eventType) || "agentMoney".equals(eventType)) {
211  this.events.processEvent(new PersonMoneyEvent(time, Id.create(atts.getValue(PersonMoneyEvent.ATTRIBUTE_PERSON), Person.class), Double.parseDouble(atts.getValue(PersonMoneyEvent.ATTRIBUTE_AMOUNT)), atts.getValue(PersonMoneyEvent.ATTRIBUTE_PURPOSE), atts.getValue(PersonMoneyEvent.ATTRIBUTE_TRANSACTION_PARTNER), atts.getValue(PersonMoneyEvent.ATTRIBUTE_REFERENCE)));
212  } else if (PersonScoreEvent.EVENT_TYPE.equals(eventType) || "personScore".equals(eventType)) {
213  this.events.processEvent(new PersonScoreEvent(time, Id.create(atts.getValue(PersonScoreEvent.ATTRIBUTE_PERSON), Person.class), Double.parseDouble(atts.getValue(PersonScoreEvent.ATTRIBUTE_AMOUNT)), atts.getValue(PersonScoreEvent.ATTRIBUTE_KIND)));
214  } else if (PersonEntersVehicleEvent.EVENT_TYPE.equals(eventType)) {
215  String personString = atts.getValue(PersonEntersVehicleEvent.ATTRIBUTE_PERSON);
216  String vehicleString = atts.getValue(PersonEntersVehicleEvent.ATTRIBUTE_VEHICLE);
217  this.events.processEvent(new PersonEntersVehicleEvent(time, Id.create(personString, Person.class), Id.create(vehicleString, Vehicle.class)));
218  } else if (PersonLeavesVehicleEvent.EVENT_TYPE.equals(eventType)) {
221  this.events.processEvent(new PersonLeavesVehicleEvent(time, pId, vId));
222  } else if (TeleportationArrivalEvent.EVENT_TYPE.equals(eventType)) {
223  this.events.processEvent(new TeleportationArrivalEvent(
224  time,
226  Double.parseDouble(atts.getValue(TeleportationArrivalEvent.ATTRIBUTE_DISTANCE)), atts.getValue(TeleportationArrivalEvent.ATTRIBUTE_MODE)));
227  } else if (VehicleArrivesAtFacilityEvent.EVENT_TYPE.equals(eventType)) {
228  String delay = atts.getValue(VehicleArrivesAtFacilityEvent.ATTRIBUTE_DELAY);
229  this.events.processEvent(new VehicleArrivesAtFacilityEvent(time, Id.create(atts.getValue(VehicleArrivesAtFacilityEvent.ATTRIBUTE_VEHICLE), Vehicle.class), Id.create(atts.getValue(VehicleArrivesAtFacilityEvent.ATTRIBUTE_FACILITY), TransitStopFacility.class), delay == null ? 0.0 : Double.parseDouble(delay)));
230  } else if (VehicleDepartsAtFacilityEvent.EVENT_TYPE.equals(eventType)) {
231  String delay = atts.getValue(VehicleDepartsAtFacilityEvent.ATTRIBUTE_DELAY);
232  this.events.processEvent(new VehicleDepartsAtFacilityEvent(time, Id.create(atts.getValue(VehicleArrivesAtFacilityEvent.ATTRIBUTE_VEHICLE), Vehicle.class), Id.create(atts.getValue(VehicleArrivesAtFacilityEvent.ATTRIBUTE_FACILITY), TransitStopFacility.class), delay == null ? 0.0 : Double.parseDouble(delay)));
233  } else if (TransitDriverStartsEvent.EVENT_TYPE.equals(eventType)) {
235  } else if (BoardingDeniedEvent.EVENT_TYPE.equals(eventType)){
236  Id<Person> personId = Id.create(atts.getValue(BoardingDeniedEvent.ATTRIBUTE_PERSON_ID), Person.class);
237  Id<Vehicle> vehicleId = Id.create(atts.getValue(BoardingDeniedEvent.ATTRIBUTE_VEHICLE_ID), Vehicle.class);
238  this.events.processEvent(new BoardingDeniedEvent(time, personId, vehicleId));
239  } else if (AgentWaitingForPtEvent.EVENT_TYPE.equals(eventType)){
240  Id<Person> agentId = Id.create(atts.getValue(AgentWaitingForPtEvent.ATTRIBUTE_AGENT), Person.class);
243  this.events.processEvent(new AgentWaitingForPtEvent(time, agentId, waitStopId, destinationStopId));
244  } else {
245  GenericEvent event = new GenericEvent(eventType, time);
246  for ( int ii=0; ii<atts.getLength(); ii++ ) {
247  String key = atts.getLocalName(ii);
248  if ( key.equals("time") || key.equals("type") ) {
249  continue;
250  }
251  String value = atts.getValue(ii);
252  event.getAttributes().put(key, value);
253  }
255  if (cem != null) {
256  this.events.processEvent(cem.apply(event));
257  } else {
258  this.events.processEvent(event);
259  }
260  }
261  }
262 
263 }
void endTag(final String name, final String content, final Stack< String > context)
final void setValidating(final boolean validateXml)
void addCustomEventMapper(String eventType, MatsimEventsReader.CustomEventMapper cem)
final Map< String, MatsimEventsReader.CustomEventMapper > customEventMappers
static final String ATTRIBUTE_X
Definition: Event.java:35
void characters(char[] ch, int start, int length)
static< T > Id< T > create(final long key, final Class< T > type)
Definition: Id.java:68
EventsReaderXMLv1(final EventsManager events)
void startTag(final String name, final Attributes atts, final Stack< String > context)
static final String ATTRIBUTE_Y
Definition: Event.java:36