Events Structure
When you look at an event file, you will notice that there are different types of events. Each event type is characterized by the agents' action:

Reading Events
In order to read events, you will need to implement certain interfaces from this package:
org.matsim.core.api.experimental.events.handler
An EventHandler is a Java class which implements one or more of these interfaces. The following example shows an EventHandler which handles LinkEnterEvents and LinkLeaveEvents. That means it can follow all vehicle movements over links:
public class MyEventHandler1 implements LinkEnterEventHandler, LinkLeaveEventHandler {
@Override
public void reset(int iteration) {
}
@Override
public void handleEvent(LinkEnterEvent event) {
}
@Override
public void handleEvent(LinkLeaveEvent event) {
}
}
The code in the handleEvent-Methods is executed every time a vehicle enters (or leaves) a link. Fill in anything you feel like doing. For a start, just have it write something to the console.
You will need a simple main routine
public static void main(String[] args) {
// The filename of the input file. Point this to the location where your
// event file is.
String inputFile = "../examples/output/ITERS/it.10/10.events.xml.gz";
// Create an EventsManager instance. This is MATSim infrastructure.
EventsManager events = new EventsUtils.createEventsManager();
// Create an instance of the custom EventHandler which you just wrote.
// Add it to the EventsManager.
MyEventHandler1 handler = new MyEventHandler1();
events.addHandler(handler);
// Connect a file reader to the EventsManager and read in the event file.
MatsimEventsReader reader = new MatsimEventsReader(events);
reader.readFile(inputFile);
System.out.println("Events file read!");
}
Now, try to create handlers which:
write the attributes of the LinkEnter- and LinkLeaveEvents to the console
calculates total time agents spend on the road and the average travel time per agent under the assumption that the size of the population will be given as a parameter.
You will find the solutions in the classes MyEventHandler1 and MyEventHandler2 which are part of the MATSim source distribution. Just open them in Eclipse. But try it yourself first!
Finally, try and write yet another EventHandler which sums up the traveled kilometers per agent. This requires a little more programming (you will have to create a Map in which you keep the current sum and the last link enter time per agent).