Microscopic Traffic Simulator
GeneratorsManager.cs
Go to the documentation of this file.
4 using System;
5 using System.Collections.Generic;
6 
7 namespace Microscopic_Traffic_Simulator___Model.CellularTopologyObjects
8 {
12  public class GeneratorsManager
13  {
19  private HashSet<Generator> generatorsWithTicket = new HashSet<Generator>();
20 
24  private List<Generator> generators = new List<Generator>();
25 
29  private CarsManager carsManager;
30 
34  private TransitionFunctionParameters transitionFunctionParameters;
35 
39  private GpsRecordsManager gpsRecordsManager;
40 
47  internal GeneratorsManager(CarsManager carsManager, TransitionFunctionParameters transitionFunctionParameters,
48  GpsRecordsManager gpsRecordsManager)
49  {
50  this.carsManager = carsManager;
51  this.transitionFunctionParameters = transitionFunctionParameters;
52  this.gpsRecordsManager = gpsRecordsManager;
53  }
54 
59  internal void ProcessGenerators(TimeSpan currentModelTime)
60  {
61  //initialize list of generators in which after getting their ticket
62  //the number of tickets will be dropped to zero.
63  List<Generator> generatorsNowWithoutTicket = new List<Generator>();
64  //for each the generators with ticket get new car. If the generator has free connected
65  //cell it will return the car. If the number of tickets in the generator will drop to
66  //zero then the generator will be added to generatorNowWithoutTicket list.
67  foreach (Generator generator in generatorsWithTicket)
68  {
69  bool generatorNowWithoutTicket;
70  Car newCar = generator.GetNewCar(out generatorNowWithoutTicket, transitionFunctionParameters);
71  if (newCar != null)
72  {
73  carsManager.AddNewCar(newCar, generator.ConnectedCell);
74  gpsRecordsManager.AddOutputRecord(new GpsRecordsManager.Record
75  {
76  Point = generator.ConnectedCell.Location,
77  DirectionNormalizedVector = generator.ConnectedCell.NormalizedDirectionVector,
78  Time = currentModelTime,
79  CarHashCode = newCar.GetHashCode()
80  });
81  }
82  if (generatorNowWithoutTicket)
83  generatorsNowWithoutTicket.Add(generator);
84  }
85  //remove generators which are now without ticket from generatorsWithTicket list
86  generatorsNowWithoutTicket.ForEach(generatorNowWithoutTicket =>
87  generatorsWithTicket.Remove(generatorNowWithoutTicket));
88  }
89 
96  internal void InitializeGeneratorForSimulation(Generator generator,
97  List<ISimulationEventsGenerator> simulationEventsGenerators)
98  {
99  simulationEventsGenerators.Add(generator);
100  generators.Add(generator);
101  generator.WasZeroTicketsNowOneTicketIsAvailable += ZeroToOneTicketsInGenerator;
102  }
103 
108  {
109  foreach (Generator generator in generators)
110  {
111  generator.WasZeroTicketsNowOneTicketIsAvailable -= ZeroToOneTicketsInGenerator;
112  }
113  }
114 
122  private void ZeroToOneTicketsInGenerator(object sender, EventArgs e)
123  {
124  generatorsWithTicket.Add(sender as Generator);
125  }
126 
130  internal void InitializeGenerators()
131  {
132  foreach (Generator generator in generators)
133  {
134  generator.InitializeTickets();
135  if (generator is Sensor)
136  {
137  (generator as Sensor).LoadInputRecords();
138  }
139  }
140  }
141 
145  internal void FinalizeGeneratorsWork()
146  {
147  generatorsWithTicket.Clear();
148  foreach (Generator generator in generators)
149  {
150  if (generator is Sensor)
151  {
152  (generator as Sensor).SaveInputRecords();
153  }
154  }
155  }
156  }
157 }
void DetachFromGeneratorEvents()
Detaches event handler ZeroToOneTicketsInGenerator from its publisher in generator.