MATSIM
EventsReaderTXT.java
Go to the documentation of this file.
1 package org.matsim.core.events;
2 /* *********************************************************************** *
3  * project: org.matsim.*
4  *
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 import org.matsim.api.core.v01.Id;
23 import org.matsim.api.core.v01.events.*;
26 import org.matsim.core.utils.io.IOUtils;
28 import org.matsim.vehicles.Vehicle;
29 
30 import java.io.BufferedReader;
31 import java.io.IOException;
32 
34 
38 public final class EventsReaderTXT {
39 
40  private final EventsManager em;
41 
43  this.em = em;
44  }
45 
46  public void runEventsFile(String file) throws IOException {
47 
48 
49  BufferedReader br = IOUtils.getBufferedReader(file);
50  String l = br.readLine();
51  l = br.readLine();//skip first line
52  while (l != null) {
53  String[] expl = StringUtils.explode(l, '\t');
54  double time = Double.parseDouble(expl[0]);
55  Id<Vehicle> vehicleId = Id.create(expl[1], Vehicle.class);
56  Id<Link> linkId = Id.createLinkId(expl[3]);
57  String type = expl[5];
58 
59 
60  Event e = null;
61  int typeIndex = Integer.parseInt(type);
62  // (switch does not work with integer input. kai, mar'18)
63  if (typeIndex == ActivityEnd.ordinal()) {
64  e = new ActivityEndEvent(time, Id.createPersonId(vehicleId), linkId, null, null, null);
65  } else if (typeIndex == PersonDeparture.ordinal()) {
66  e = new PersonDepartureEvent(time, Id.createPersonId(vehicleId), linkId, "car", "car");
67  } else if (typeIndex == VehicleEntersTraffic.ordinal()) {
68  e = new VehicleEntersTrafficEvent(time, Id.createPersonId(vehicleId), linkId, vehicleId, "car", 0);
69  } else if (typeIndex == LinkLeave.ordinal()) {
70  e = new LinkLeaveEvent(time, vehicleId, linkId);
71  } else if (typeIndex == LinkEnter.ordinal()) {
72  e = new LinkEnterEvent(time, vehicleId, linkId);
73  } else if (typeIndex == PersonArrival.ordinal()) {
74  e = new PersonArrivalEvent(time, Id.createPersonId(vehicleId), linkId, "car");
75  } else if (typeIndex == ActivityStart.ordinal()) {
76  e = new ActivityStartEvent(time, Id.createPersonId(vehicleId), linkId, null, null, null );
77  } else {
78  throw new RuntimeException("Unsupported event type:" + l);
79  }
80  this.em.processEvent(e);
81  l = br.readLine();
82  }
83  br.close();
84  }
85 }
static Id< Link > createLinkId(final long key)
Definition: Id.java:202
static BufferedReader getBufferedReader(URL url, Charset charset)
Definition: IOUtils.java:321
static String [] explode(final String str, final char delimiter, final int limit)
static< T > Id< T > create(final long key, final Class< T > type)
Definition: Id.java:68
static Id< Person > createPersonId(final long key)
Definition: Id.java:193