Microscopic Traffic Simulator

- Main Page
- Packages
- Classes
- Files

- File List

ParametersViewModel.cs
Go to the documentation of this file.
1 using Microscopic_Traffic_Simulator.ViewModels.Messages;
2 using Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneralParameters;
3 using System;
4 using System.ComponentModel;
5 using System.IO;
6 using System.Windows.Input;
7
8 namespace Microscopic_Traffic_Simulator.ViewModels
9 {
13 class ParametersViewModel : ViewModelBase, INotifyPropertyChanged
14 {
18 internal class ParametersMessage : ParameterizedMessage<Parameters> { }
19
23 private Parameters parameters;
24
28 private IInteractions interactions;
29
33 private ISettings settings;
34
38 public string LastUsedParametersFileName
39 {
40 get { return Path.GetFileNameWithoutExtension(PathToParameters); }
41 }
42
46 private string PathToParameters
47 {
48 get { return settings.PathToParameters; }
49 set
50 {
51 settings.PathToParameters = value;
52 settings.Save();
53 OnPropertyChanged("LastUsedParametersFileName");
54 }
55 }
56
60 public double P1_CellLength
61 {
62 get { return parameters.CellularTopologyParameters.P1_CellLength; }
63 set { parameters.CellularTopologyParameters.P1_CellLength = value; OnPropertyChanged("P1_CellLength"); }
64 }
65
69 public TimeSpan P2_SimulationStepInterval
70 {
71 get { return parameters.CellularTopologyParameters.P2_SimulationStepInterval; }
72 set
73 {
74 parameters.CellularTopologyParameters.P2_SimulationStepInterval = value;
75 OnPropertyChanged("P2_SimulationStepInterval");
76 }
77 }
78
82 public double P5_LowSpeedDecProbability
83 {
84 get { return parameters.TransitionFunctionParameters.P5_LowSpeedDecProbability; }
85 set
86 {
87 parameters.TransitionFunctionParameters.P5_LowSpeedDecProbability = value;
88 OnPropertyChanged("P5_LowSpeedDecProbability");
89 }
90 }
91
95 public int P6_DecProbabilitySpeedBound
96 {
97 get { return parameters.TransitionFunctionParameters.P6_DecProbabilitySpeedBound; }
98 set
99 {
100 parameters.TransitionFunctionParameters.P6_DecProbabilitySpeedBound = value;
101 OnPropertyChanged("P6_DecProbabilitySpeedBound");
102 }
103 }
104
108 public double P7_AccelerationProbability
109 {
110 get { return parameters.TransitionFunctionParameters.P7_AccelerationProbability; }
111 set
112 {
113 parameters.TransitionFunctionParameters.P7_AccelerationProbability = value;
114 OnPropertyChanged("P7_AccelerationProbability");
115 }
116 }
117
121 public double P8_HighSpeedDecProbability
122 {
123 get { return parameters.TransitionFunctionParameters.P8_HighSpeedDecProbability; }
124 set
125 {
126 parameters.TransitionFunctionParameters.P8_HighSpeedDecProbability = value;
127 OnPropertyChanged("P8_HighSpeedDecProbability");
128 }
129 }
130
134 public double P9_DecRateWhenLeadingAcc
135 {
136 get { return parameters.TransitionFunctionParameters.P9_DecRateWhenLeadingAcc; }
137 set
138 {
139 parameters.TransitionFunctionParameters.P9_DecRateWhenLeadingAcc = value;
140 OnPropertyChanged("P9_DecRateWhenLeadingAcc");
141 }
142 }
143
147 public double P10_DecRateWhenLeadingDec
148 {
149 get { return parameters.TransitionFunctionParameters.P10_DecRateWhenLeadingDec; }
150 set
151 {
152 parameters.TransitionFunctionParameters.P10_DecRateWhenLeadingDec = value;
153 OnPropertyChanged("P10_DecRateWhenLeadingDec");
154 }
155 }
156
160 private ICommand defaultCommand;
164 public ICommand DefaultCommand
165 {
166 get
167 {
168 if (defaultCommand == null)
169 {
170 defaultCommand = new RelayCommand(i => SetDefaults());
171 }
172 return defaultCommand;
173 }
174 }
175
179 private ICommand importCommand;
183 public ICommand ImportCommand
184 {
185 get
186 {
187 if (importCommand == null)
188 {
189 importCommand = new RelayCommand(i => Import());
190 }
191 return importCommand;
192 }
193 }
194
198 private ICommand exportCommand;
202 public ICommand ExportCommand
203 {
204 get
205 {
206 if (exportCommand == null)
207 {
208 exportCommand = new RelayCommand(i => Export());
209 }
210 return exportCommand;
211 }
212 }
213
217 private ICommand okCommand;
221 public ICommand OkCommand
222 {
223 get
224 {
225 if (okCommand == null)
226 {
227 okCommand = new RelayCommand(i => UseParameters());
228 }
229 return okCommand;
230 }
231 }
232
239 public ParametersViewModel(Messenger messenger, ISettings settings, IInteractions interactions)
240 {
241 this.messenger = messenger;
242 this.settings = settings;
243 this.interactions = interactions;
244 }
245
249 public void Initialize()
250 {
251 if (PathToParameters == string.Empty)
252 {
253 SetDefaults();
254 }
255 else
256 {
257 try
258 {
259 Import(PathToParameters);
260 }
261 catch
262 {
263 PathToParameters = string.Empty;
264 SetDefaults();
265 }
266 }
267 }
268
272 private void SetDefaults()
273 {
274 parameters = new Parameters();
275 P1_CellLength = 1.0;
276 P2_SimulationStepInterval = TimeSpan.FromSeconds(1.0);
277 P5_LowSpeedDecProbability = 0.2;
278 P6_DecProbabilitySpeedBound = 15;
279 P7_AccelerationProbability = 0.8;
280 P8_HighSpeedDecProbability = 0.1;
281 P9_DecRateWhenLeadingAcc = 2.0;
282 P10_DecRateWhenLeadingDec = 3.0;
283 PathToParameters = string.Empty;
284 OnPropertyChanged(string.Empty);
285 }
286
290 private void Import()
291 {
292 string pathToOpen = interactions.GetPathToOpenParametersFile();
293 if (!string.IsNullOrEmpty(pathToOpen))
294 {
295 Import(pathToOpen);
296 }
297 }
298
303 private void Import(string path)
304 {
305 try
306 {
307 parameters = Parameters.Deserialize(path);
308 PathToParameters = path;
309 OnPropertyChanged(string.Empty);
310 }
311 catch (Exception ex)
312 {
313 interactions.ScreamErrorMessage(ex.Message);
314 }
315 }
316
317
321 private void Export()
322 {
323 string pathToSave = interactions.GetPathToSaveParametersFile();
324 if (!string.IsNullOrEmpty(pathToSave))
325 {
326 try
327 {
328 parameters.Serialize(pathToSave);
329 PathToParameters = pathToSave;
330 }
331 catch (Exception ex)
332 {
333 interactions.ScreamErrorMessage(ex.Message);
334 }
335 }
336 }
337
341 private void UseParameters()
342 {
343 messenger.GetEvent<ParametersMessage>().Publish(parameters);
344 }
345 }
346 }
Microscopic_Traffic_Simulator.ViewModels.Messages
Definition: Message.cs:4
Microscopic_Traffic_Simulator.ViewModels.IInteractions.ScreamErrorMessage
void ScreamErrorMessage(string errorMessage)
Screaming error message.
Microscopic_Traffic_Simulator.ViewModels.IInteractions.GetPathToOpenParametersFile
string GetPathToOpenParametersFile()
Asking which parameters file to open for loading.
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneralParameters.Parameters
Class containing all simulator parameters.
Definition: Parameters.cs:12
System
Microscopic_Traffic_Simulator.ViewModels.Messages.ParameterizedMessage
Class representing message with payload.
Definition: ParameterizedMessage.cs:9
Microscopic_Traffic_Simulator.ViewModels.ISettings
Interface of application settings.
Definition: ISettings.cs:6
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneralParameters.CellularTopologyParameters.P2_SimulationStepInterval
TimeSpan P2_SimulationStepInterval
Cell length parameter.
Definition: CellularTopologyParameters.cs:19
Microscopic_Traffic_Simulator.ViewModels.Messages.Messenger
Class representing messenger for communicating between viewmodels.
Definition: Messenger.cs:9
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneralParameters.Parameters.Serialize
void Serialize(string path)
Save parameters to file.
Definition: Parameters.cs:52
Microscopic_Traffic_Simulator.ViewModels.IInteractions.GetPathToSaveParametersFile
string GetPathToSaveParametersFile()
Asking which paramters file the data to be saved into.
Microscopic_Traffic_Simulator.ViewModels
Definition: CanvasViewModel.cs:8
Microscopic_Traffic_Simulator.RelayCommand
Command class for binding UI commands with view models.
Definition: RelayCommand.cs:10
Microscopic_Traffic_Simulator.ViewModels.ParametersViewModel.ParametersViewModel
ParametersViewModel(Messenger messenger, ISettings settings, IInteractions interactions)
Constructor to parameters view model.
Definition: ParametersViewModel.cs:239
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneralParameters.Parameters.CellularTopologyParameters
CellularTopologyParameters CellularTopologyParameters
Reference to cellular topology parameters.
Definition: Parameters.cs:21
Microscopic_Traffic_Simulator.ViewModels.ViewModelBase
Definition: ViewModelBase.cs:8
Microscopic_Traffic_Simulator.ViewModels.IInteractions
Interface for interactions with user.
Definition: IInteractions.cs:6
Microscopic_Traffic_Simulator.ViewModels.ParametersViewModel.Initialize
void Initialize()
Initialize parameters view model by getting last used or default parameters.
Definition: ParametersViewModel.cs:249
Microscopic_Traffic_Simulator.ViewModels.ISettings.PathToParameters
string PathToParameters
Path to last used file with parameters.
Definition: ISettings.cs:36
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneralParameters.Parameters.TransitionFunctionParameters
TransitionFunctionParameters TransitionFunctionParameters
Reference to transition function parameters.
Definition: Parameters.cs:31
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneralParameters
Definition: CellularTopologyParameters.cs:3
Microscopic_Traffic_Simulator
Definition: App.xaml.cs:12
Microscopic_Traffic_Simulator.ViewModels.ParametersViewModel
Class representing view model of parameters view.
Definition: ParametersViewModel.cs:13
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneralParameters.CellularTopologyParameters.P1_CellLength
double P1_CellLength
Cell length parameter.
Definition: CellularTopologyParameters.cs:14
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects
Definition: Car.cs:6
Microscopic_Traffic_Simulator___Model.CellularTopologyObjects.GeneralParameters.Parameters.Deserialize
static Parameters Deserialize(string path)
Opens parameters file to import.
Definition: Parameters.cs:39
Microscopic_Traffic_Simulator.ViewModels.ISettings.Save
void Save()
Saves application settings.
Microscopic_Traffic_Simulator___Model