1 package org.matsim.analysis;
3 import jakarta.inject.Inject;
4 import org.apache.commons.csv.CSVFormat;
5 import org.apache.commons.csv.CSVPrinter;
6 import org.apache.logging.log4j.LogManager;
7 import org.apache.logging.log4j.Logger;
16 import java.io.BufferedWriter;
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.stream.Collectors;
43 CSVPrinter csvPrinter =
new CSVPrinter(bufferedWriter, CSVFormat.Builder.create()
44 .setDelimiter(config.global().getDefaultDelimiter().charAt(0))
45 .setHeader(header).build());
46 for (
Person p : scenario.getPopulation().getPersons().values()) {
51 }
catch (IOException e) {
57 private void writePerson(
Person p, List<String> attributes, CSVPrinter csvPrinter)
throws IOException {
58 if (p.getSelectedPlan() == null) {
59 log.error(
"Found person without a selected plan: " + p.getId().toString() +
" will not be added to output_persons.csv");
62 List<String> line =
new ArrayList<>();
63 line.add(p.getId().toString());
64 line.add(p.getSelectedPlan().getScore() == null ?
"null" : p.getSelectedPlan().getScore().toString());
68 if (!p.getSelectedPlan().getPlanElements().isEmpty()) {
79 for (String attribute : attributes) {
80 Object value = p.getAttributes().getAttribute(attribute);
81 String result = value != null ? String.valueOf(value) :
"";
84 csvPrinter.printRecord(line);
88 List<String> header =
new ArrayList<>();
90 header.add(
"executed_score");
91 header.add(
"first_act_x");
92 header.add(
"first_act_y");
93 header.add(
"first_act_type");
94 header.addAll(attributes);
95 return header.toArray(String[]::
new);
99 List<String> attributes = scenario.getPopulation().getPersons().values().parallelStream()
100 .flatMap(p -> p.getAttributes().getAsMap().keySet().stream()).distinct()
101 .collect(Collectors.toList());
102 attributes.remove(
"vehicles");
void writePerson(Person p, List< String > attributes, CSVPrinter csvPrinter)
static BufferedWriter getBufferedWriter(URL url, Charset charset, boolean append)
List< String > prepareAttributes()
String [] prepareHeader(List< String > attributes)