Microscopic Traffic Simulator

- Main Page
- Packages
- Classes
- Files

- File List

GeneratorsManager.cs
Go to the documentation of this file.
1 using Microscopic_Traffic_Simulator___Model.GeneralParameters;
2 using Microscopic_Traffic_Simulator___Model.SimulationControl;
3 using Microscopic_Traffic_Simulator___Model.TrafficObjects;
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
107 public void DetachFromGeneratorEvents()
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 }
Microscopic_Traffic_Simulator___Model.SimulationControl
Definition: ISimulationEventsGenerator.cs:3
System
Microscopic_Traffic_Simulator___Model.GeneralParameters
Definition: TransitionFunctionParameters.cs:3
Microscopic_Traffic_Simulator___Model.TrafficObjects.Generator
Represents generator of cars.
Definition: Generator.cs:13
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GpsRecordsManager
Manages GPS records for cellular topology.
Definition: GPSRecordsManager.cs:15
Microscopic_Traffic_Simulator___Model.TrafficObjects.Sensor
Class representing sensor.
Definition: Sensor.cs:14
Microscopic_Traffic_Simulator___Model.GeneralParameters.TransitionFunctionParameters
Class containing parameters of transition function.
Definition: TransitionFunctionParameters.cs:9
Microscopic_Traffic_Simulator___Model.TrafficObjects
Definition: Generator.cs:7
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.Car
Class representing car
Definition: Car.cs:11
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneratorsManager.DetachFromGeneratorEvents
void DetachFromGeneratorEvents()
Detaches event handler ZeroToOneTicketsInGenerator from its publisher in generator.
Definition: GeneratorsManager.cs:107
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneratorsManager
Manages generators and sensors in topology.
Definition: GeneratorsManager.cs:12
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GpsRecordsManager.Record
Structure representing car&#39;s location in time.
Definition: GpsRecordsManager.Record.cs:14
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.CarsManager
Manages cars in cellular topology.
Definition: CarsManager.cs:11
Microscopic_Traffic_Simulator___Model