Microscopic Traffic Simulator
Sensor.cs
Go to the documentation of this file.
3 using System;
4 using System.Collections.Generic;
5 using System.Globalization;
6 using System.IO;
7 
8 namespace Microscopic_Traffic_Simulator___Model.TrafficObjects
9 {
13  [Serializable]
14  public partial class Sensor : Generator
15  {
19  [NonSerialized]
20  private List<Record> inputRecords;
21 
25  [NonSerialized]
26  private List<Record> outputRecords;
27 
31  private string pathToInputRecords;
32 
36  private string pathToOutputRecords;
37 
41  private int actionToGetTimeTo;
42 
46  private int actionToPerform;
47 
56  {
57  set { cellularTopologyParameters = value; }
58  }
59 
65  public Sensor(string pathToInputRecords, string pathToOutputRecords)
66  {
67  this.pathToInputRecords = pathToInputRecords;
68  this.pathToOutputRecords = pathToOutputRecords;
69  }
70 
74  internal void LoadInputRecords()
75  {
76  inputRecords = new List<Record>();
77  outputRecords = new List<Record>();
78  actionToGetTimeTo = 0;
79  actionToPerform = 0;
80  foreach (string[] lineItems in new FileLineDataLoader(pathToInputRecords).GetLinesWithData())
81  {
82  inputRecords.Add(new Record()
83  {
84  Time = TimeSpan.Parse(lineItems[0]),
85  Speed = double.Parse(lineItems[1])
86  });
87  }
88  }
89 
93  internal void SaveInputRecords()
94  {
95  using (StreamWriter sw = new FileDataStore(pathToOutputRecords).GetStreamWriter())
96  {
97  foreach (Record record in outputRecords)
98  {
99  sw.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0} {1}", record.Time, record.Speed));
100  }
101  }
102  }
103 
109  public override TimeSpan GetTimeToNextAction(Random random)
110  {
111  if (actionToGetTimeTo == inputRecords.Count)
112  {
113  return TimeSpan.MaxValue;
114  }
115  else if (actionToGetTimeTo == 0)
116  {
117  return inputRecords[actionToGetTimeTo++].Time;
118  }
119  else
120  {
121  return inputRecords[actionToGetTimeTo].Time - inputRecords[actionToGetTimeTo++ - 1].Time;
122  }
123  }
124 
129  public override void PerformAction(Random random)
130  {
131  GenerateTicket(cellularTopologyParameters.FromKphSpeedToSimSpeed(inputRecords[actionToPerform++].Speed));
132  }
133 
139  internal void AddOutputRecord(TimeSpan time, int speed)
140  {
141  outputRecords.Add(new Record()
142  {
143  Time = time,
144  Speed = cellularTopologyParameters.FromSimSpeedToKphSpeed(speed)
145  });
146  }
147  }
148 }
CellularTopologyParameters cellularTopologyParameters
Reference to simulation parameters.
Definition: Sensor.cs:51
override void PerformAction(Random random)
Generate new ticket from input records.
Definition: Sensor.cs:129
Class for loading lines of data from text file.
override TimeSpan GetTimeToNextAction(Random random)
Returns time to generation of new ticket by getting time to the next input record.
Definition: Sensor.cs:109
Class for storing line data to text files.
Definition: FileDataStore.cs:8
Sensor(string pathToInputRecords, string pathToOutputRecords)
Initialize sensor with paths to files with input and output records.
Definition: Sensor.cs:65