MATSIM
RunPopulationStreamingExample.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.* *
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2008 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 package tutorial.programming.streaming;
20 
21 import java.util.ArrayList;
22 import java.util.Collection;
23 
24 import org.matsim.api.core.v01.Scenario;
32 
41 final class RunPopulationStreamingExample {
42 
43  private static class RemoveUnselectedPlans implements PersonAlgorithm {
44  @Override public void run(Person person) {
45  Plan selectedPlan = person.getSelectedPlan() ;
46 
47  Collection<Plan> toBeRemoved = new ArrayList<>() ;
48  for ( Plan plan : person.getPlans() ) {
49  if ( plan != selectedPlan ) {
50  toBeRemoved.add( plan ) ;
51  }
52  }
53  for ( Plan plan : toBeRemoved ) {
54  person.removePlan(plan) ;
55  }
56  }
57  }
58 
62  public static void main(String[] args) {
63  final String inputPopFile="inputPop.xml.gz" ;
64  final String outputPopFile="outputPop.xml.gz" ;
65 
67  StreamingPopulationReader reader = new StreamingPopulationReader( scenario ) ;
68 
69  // --- add an algorithm:
70  reader.addAlgorithm( new RemoveUnselectedPlans() ) ;
71 
72  // --- add writing the population needs to be explicitly added!:
73  final StreamingPopulationWriter writer = new StreamingPopulationWriter( null );
74  // with current design, PopulationWriter(...) minimally demands a population. Which can, however, be null when used for streaming.
75 
76  writer.startStreaming(outputPopFile);
77  // (write the header of the population file)
78 
79  reader.addAlgorithm( writer ) ;
80 
81  // --- run everything:
82  reader.readFile( inputPopFile );
83 
84  writer.closeStreaming();
85  // (write the footer of the population file)
86 
87 
88  }
89 
90 }
static Config createConfig(final String filename)
static Scenario createScenario(final Config config)