MATSIM
GenericPlanStrategyImpl.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 org.matsim.core.replanning;
20 
21 import org.apache.logging.log4j.LogManager;
22 import org.apache.logging.log4j.Logger;
29 
30 import java.util.ArrayList;
31 import java.util.List;
32 
37 public class GenericPlanStrategyImpl<T extends BasicPlan, I> implements GenericPlanStrategy<T, I> {
38 
41  private final List<GenericPlanStrategyModule<T>> modules = new ArrayList<>();
42  private final List<T> plans = new ArrayList<>();
43  private long counter = 0;
45  private final static Logger log = LogManager.getLogger(GenericPlanStrategyImpl.class);
46 
51  public GenericPlanStrategyImpl(final PlanSelector<T, I> planSelector) {
52  this.planSelector = planSelector;
53  }
54 
55  public void addStrategyModule(final GenericPlanStrategyModule<T> module) {
56  if (this.firstModule == null) {
57  this.firstModule = module;
58  } else {
59  this.modules.add(module);
60  }
61  }
62 
64  if (this.firstModule == null) {
65  return 0;
66  }
67  return this.modules.size() + 1; // we also have to count "firstModule", thus +1
68  }
69 
70  @Override
71  public void run(final HasPlansAndId<T, I> person) {
72  this.counter++;
73 
74  // if there is at least one unscored plan, find that one:
75  T plan = new RandomUnscoredPlanSelector<T, I>().selectPlan(person) ;
76 
77  // otherwise, find one according to selector (often defined in PlanStrategy ctor):
78  if (plan == null) {
79  plan = this.planSelector.selectPlan(person);
80  }
81 
82  // "select" that plan:
83  if ( plan != null ) {
84  person.setSelectedPlan(plan);
85  }
86  else {
87  log.error( planSelector+" returned no plan: not changing selected plan for person "+person );
88  }
89 
90  // if there is a "module" (i.e. "innovation"):
91  if (this.firstModule != null) {
92 
93  // set the working plan to a copy of the selected plan:
95 
96  //Id is only set inside planInheritance -> if null planInheritance is disabled
97  if (plan instanceof Plan && ((Plan) plan).getId() != null) {
98  // add plan inheritance flags
99  ((Plan) plan).setIterationCreated(this.replanningContext.getIteration());
100  ((Plan) plan).setPlanMutator(this.toString());
101  }
102 
103  // add new plan to container that contains the plans that are handled by this PlanStrategy:
104  this.plans.add(plan);
105 
106  // start working on this new plan:
107  this.firstModule.handlePlan(plan);
108  }
109 
110  }
111 
112  @Override
113  public void init(ReplanningContext replanningContext0) {
114  this.replanningContext = replanningContext0;
115  if (this.firstModule != null) {
116  this.firstModule.prepareReplanning(replanningContext0);
117  }
118  }
119 
120  @Override
121  public void finish() {
122  if (this.firstModule != null) {
123  // finish the first module
124  this.firstModule.finishReplanning();
125  // now work through the others
126  for (GenericPlanStrategyModule<T> module : this.modules) {
127  module.prepareReplanning(replanningContext);
128  for (T plan : this.plans) {
129  module.handlePlan(plan);
130  }
131  module.finishReplanning();
132  }
133  }
134  this.plans.clear();
135  log.info("Plan-Strategy finished, " + this.counter + " plans handled. Strategy: " + this.toString());
136  this.counter = 0;
137  }
138 
139  @Override
140  public String toString() {
141  StringBuilder name = new StringBuilder(20);
142  name.append(this.planSelector.getClass().getSimpleName());
143  if (this.firstModule != null) {
144  name.append('_');
145  name.append(this.firstModule.getClass().getSimpleName());
146  for (Object module : this.modules) {
147  name.append('_');
148  name.append(module.getClass().getSimpleName());
149  }
150  }
151  return name.toString();
152  }
153 
155  return planSelector;
156  }
157 
158 
159 }
final List< GenericPlanStrategyModule< T > > modules
void init(ReplanningContext replanningContext0)
T selectPlan(HasPlansAndId< T, I > member)
void addStrategyModule(final GenericPlanStrategyModule< T > module)
GenericPlanStrategyImpl(final PlanSelector< T, I > planSelector)
abstract void setSelectedPlan(T selectedPlan)
void prepareReplanning(ReplanningContext replanningContext)