From d60f7b8960a754eab1491d15f0f16390fdf44ea6 Mon Sep 17 00:00:00 2001 From: GregorRyb Date: Wed, 10 Jul 2024 19:06:26 +0200 Subject: [PATCH] add money event handler --- .../analysis/MyPersonMoneyEventHandler.java | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/main/java/org/matsim/analysis/MyPersonMoneyEventHandler.java diff --git a/src/main/java/org/matsim/analysis/MyPersonMoneyEventHandler.java b/src/main/java/org/matsim/analysis/MyPersonMoneyEventHandler.java new file mode 100644 index 00000000..b9824ee7 --- /dev/null +++ b/src/main/java/org/matsim/analysis/MyPersonMoneyEventHandler.java @@ -0,0 +1,73 @@ +package org.matsim.analysis; + +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVPrinter; +import org.apache.logging.log4j.LogManager; +import org.matsim.api.core.v01.events.PersonMoneyEvent; +import org.matsim.core.api.experimental.events.EventsManager; +import org.matsim.core.events.EventsUtils; +import org.matsim.core.utils.io.IOUtils; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + + +/** + * Quick person money analysis handler + */ +public class MyPersonMoneyEventHandler { + + public static void main (String[] args) { + String inputPath = args[0]; + String outputPath = args[1]; + EventsManager myEventsmanager = EventsUtils.createEventsManager(); + TestPersonMoneyEventHandler myEventHandler = new TestPersonMoneyEventHandler(new ArrayList()); + myEventsmanager.addHandler(myEventHandler); + EventsUtils.readEvents(myEventsmanager, inputPath); + writeAllPersonMoneyEvents(myEventHandler.personMoneyEventList, outputPath); + } + + static void writeAllPersonMoneyEvents(List personMoneyEventList, String outputFilename) { + try { + CSVPrinter csvPrinter = new CSVPrinter(IOUtils.getBufferedWriter(outputFilename), CSVFormat.DEFAULT); + + try { + csvPrinter.printRecord("time", "person", "amount", "purpose", "transactionPartner", "reference"); + Iterator var3 = personMoneyEventList.iterator(); + + while (var3.hasNext()) { + PersonMoneyEvent personMoneyEvent = (PersonMoneyEvent) var3.next(); + csvPrinter.printRecord(personMoneyEvent.getTime(), personMoneyEvent.getPersonId(), personMoneyEvent.getAmount(), personMoneyEvent.getPurpose(), personMoneyEvent.getTransactionPartner(), personMoneyEvent.getReference()); + } + } catch (Throwable var6) { + try { + csvPrinter.close(); + } catch (Throwable var5) { + var6.addSuppressed(var5); + } + throw var6; + } + csvPrinter.close(); + } catch (IOException var7) { + LogManager.getLogger("Could not write " + outputFilename + "."); + } + } + +} + +class TestPersonMoneyEventHandler implements org.matsim.api.core.v01.events.handler.PersonMoneyEventHandler { + + List personMoneyEventList; + TestPersonMoneyEventHandler( List personMoneyEventList) { + this.personMoneyEventList = personMoneyEventList; + } + + @Override + public void handleEvent(PersonMoneyEvent personMoneyEvent) { + personMoneyEventList.add(personMoneyEvent); + } +} + +