Microscopic Traffic Simulator

- Main Page
- Packages
- Classes
- Files

- File List

Generator.cs
Go to the documentation of this file.
1 using Microscopic_Traffic_Simulator___Model.CellularTopologyObjects;
2 using Microscopic_Traffic_Simulator___Model.GeneralParameters;
3 using Microscopic_Traffic_Simulator___Model.SimulationControl;
4 using System;
5 using System.Collections.Generic;
6
7 namespace Microscopic_Traffic_Simulator___Model.TrafficObjects
8 {
12 [Serializable]
13 public class Generator : ISimulationEventsGenerator
14 {
18 public bool IsPauseScheduled { get { return false; } }
19
23 [NonSerialized]
24 private Cell connectedCell = null;
28 internal Cell ConnectedCell
29 {
30 get { return connectedCell; }
31 set { connectedCell = value; }
32 }
33
37 private const int GeneratorSimulationPriority = 0;
38
42 public int Priority { get { return GeneratorSimulationPriority; } }
43
48 [NonSerialized]
49 private Queue<int> tickets = new Queue<int>();
50
54 internal event EventHandler WasZeroTicketsNowOneTicketIsAvailable;
55
63 internal Car GetNewCar(out bool lastTicketUsed, TransitionFunctionParameters transitionFunctionParameters)
64 {
65 //initialize lastTicketUsed parameter to false
66 lastTicketUsed = false;
67 //if no tickets available return
68 if (tickets.Count == 0)
69 return null;
70 //check if connected cell is free
71 if (connectedCell.Car == null)
72 {
73 //remove one ticket
74 int speed = tickets.Dequeue();
75 //check if it was the last ticket
76 if (tickets.Count == 0)
77 lastTicketUsed = true;
78 //create new car instance
79 Car newCar = new Car(connectedCell, speed, transitionFunctionParameters);
80 //insert car to connected cell
81 connectedCell.Car = newCar;
82 //return car
83 return newCar;
84 }
85 else
86 return null;
87 }
88
95 public virtual TimeSpan GetTimeToNextAction(Random random)
96 {
97 return TimeSpan.FromSeconds(5.0);
98 }
99
104 public virtual void PerformAction(Random random)
105 {
106 GenerateTicket(0);
107 }
108
114 protected void GenerateTicket(int initialSpeed)
115 {
116 tickets.Enqueue(initialSpeed);
117 if (tickets.Count == 1)
118 OnWasZeroTicketsNowOneTicketWasGenerated();
119 }
120
124 internal void InitializeTickets()
125 {
126 tickets = new Queue<int>();
127 }
128
132 private void OnWasZeroTicketsNowOneTicketWasGenerated()
133 {
134 if (WasZeroTicketsNowOneTicketIsAvailable != null)
135 WasZeroTicketsNowOneTicketIsAvailable(this, EventArgs.Empty);
136 }
137 }
138 }
Microscopic_Traffic_Simulator___Model.TrafficObjects.Generator.GetTimeToNextAction
virtual TimeSpan GetTimeToNextAction(Random random)
Returns time to generation of new ticket.
Definition: Generator.cs:95
Microscopic_Traffic_Simulator___Model.TrafficObjects.Generator.PerformAction
virtual void PerformAction(Random random)
Generate new car.
Definition: Generator.cs:104
Microscopic_Traffic_Simulator___Model.SimulationControl
Definition: ISimulationEventsGenerator.cs:3
System
Microscopic_Traffic_Simulator___Model.GeneralParameters
Definition: TransitionFunctionParameters.cs:3
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.Cell
Class representing cell.
Definition: Cell.cs:9
Microscopic_Traffic_Simulator___Model.TrafficObjects.Generator
Represents generator of cars.
Definition: Generator.cs:13
Microscopic_Traffic_Simulator___Model.GeneralParameters.TransitionFunctionParameters
Class containing parameters of transition function.
Definition: TransitionFunctionParameters.cs:9
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.Car
Class representing car
Definition: Car.cs:11
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects
Definition: Car.cs:6
Microscopic_Traffic_Simulator___Model.TrafficObjects.Generator.GenerateTicket
void GenerateTicket(int initialSpeed)
Generate new ticket. In case that current number of tickets is zero add generator to set of generator...
Definition: Generator.cs:114
Microscopic_Traffic_Simulator___Model.SimulationControl.ISimulationEventsGenerator
Every class which performs some repeating action should implement this interface
Definition: ISimulationEventsGenerator.cs:8
Microscopic_Traffic_Simulator___Model