Microscopic Traffic Simulator
Generator.cs
Go to the documentation of this file.
4 using System;
5 using System.Collections.Generic;
6 
7 namespace Microscopic_Traffic_Simulator___Model.TrafficObjects
8 {
12  [Serializable]
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 }
virtual TimeSpan GetTimeToNextAction(Random random)
Returns time to generation of new ticket.
Definition: Generator.cs:95
virtual void PerformAction(Random random)
Generate new car.
Definition: Generator.cs:104
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
Every class which performs some repeating action should implement this interface