MATSIM
PlanInheritanceRecordReader.java
Go to the documentation of this file.
1 package org.matsim.core.replanning.inheritance;
2 
3 import java.io.BufferedReader;
4 
5 /* *********************************************************************** *
6  * project: org.matsim.*
7  * ParallelPopulationReaderMatsimV6.java
8  * *
9  * *********************************************************************** *
10  * *
11  * copyright : (C) 2023 by the members listed in the COPYING, *
12  * LICENSE and WARRANTY file. *
13  * email : info at matsim dot org *
14  * *
15  * *********************************************************************** *
16  * *
17  * This program is free software; you can redistribute it and/or modify *
18  * it under the terms of the GNU General Public License as published by *
19  * the Free Software Foundation; either version 2 of the License, or *
20  * (at your option) any later version. *
21  * See also COPYING, LICENSE and WARRANTY file *
22  * *
23  * *********************************************************************** */
24 
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.stream.Collectors;
32 
33 import org.matsim.api.core.v01.Id;
35 import org.matsim.core.utils.io.IOUtils;
36 
43 
44 
45  private final String DELIMITER = "\t";
46  private final BufferedReader reader;
47 
48  public PlanInheritanceRecordReader(String filename) {
49  this.reader = IOUtils.getBufferedReader(filename);
50 
51 
52 
53  }
54  public Map<String, Integer> buildIdx(String[] header) {
55  Map<String, Integer> lookup = new HashMap<String,Integer>();
56  for (int i=0; i<header.length; i++) {
57  lookup.put(header[i],i);
58  }
59  return lookup;
60  }
61 
62  public List<PlanInheritanceRecord> read() {
63  List<PlanInheritanceRecord> records = new ArrayList<PlanInheritanceRecord>();
64  try {
65  Map<String,Integer> lookUp = buildIdx(reader.readLine().split(DELIMITER));
66  String lineString = reader.readLine();
67  while(lineString !=null) {
68  String[] line = lineString.split(DELIMITER);
69  PlanInheritanceRecord planInheritanceRecord = new PlanInheritanceRecord();
70  planInheritanceRecord.setAgentId(Id.createPersonId(line[lookUp.get(PlanInheritanceRecordWriter.AGENT_ID)]));
71  planInheritanceRecord.setPlanId(Id.create(line[lookUp.get(PlanInheritanceRecordWriter.PLAN_ID)], Plan.class));
72  planInheritanceRecord.setAncestorId(Id.create(line[lookUp.get(PlanInheritanceRecordWriter.ANCESTOR_ID)], Plan.class));
73  planInheritanceRecord.setMutatedBy(line[lookUp.get(PlanInheritanceRecordWriter.MUTATED_BY)]);
74  planInheritanceRecord.setIterationCreated(Integer.parseInt(line[lookUp.get(PlanInheritanceRecordWriter.ITERATION_CREATED)]));
75  planInheritanceRecord.setIterationRemoved(Integer.parseInt(line[lookUp.get(PlanInheritanceRecordWriter.ITERATION_REMOVED)]));
76  String iterationsSelected = line[lookUp.get(PlanInheritanceRecordWriter.ITERATIONS_SELECTED)];
77  planInheritanceRecord.setIterationsSelected(Arrays.asList(iterationsSelected.substring(1, iterationsSelected.length()-1).split(", ")).stream()
78  .map(Integer::parseInt)
79  .collect(Collectors.toList()));
80  records.add(planInheritanceRecord);
81  lineString = reader.readLine();
82  }
83  return records;
84 
85  } catch (IOException e) {
86  throw new RuntimeException("Could not read the plan inheritance records!", e);
87  }
88 
89 
90 
91  }
92 
93 
94 }
static BufferedReader getBufferedReader(URL url, Charset charset)
Definition: IOUtils.java:321
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