Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using CIFProcessing.Dictionaries;
- using CIFProcessing.Exceptions;
- namespace CIFProcessing.Classes
- {
- /// <summary>
- /// Stores the initial cif data and process it so a schedule can be formed
- /// </summary>
- class CifData
- {
- private readonly List<string> _filelines;
- /// <summary>
- /// Initializes a new instance of the <see cref="CifData"/> class.
- /// </summary>
- /// <param name="filePath">string containing full path to the cif</param>
- public CifData(string filePath)
- {
- _filelines = new List<string>();
- // Read lines into list, only if relevant
- try
- {
- string[] validTypes = { "BS", "BX", "LO", "LI", "LT" };
- using (var cifReader = new StreamReader(filePath))
- {
- while (cifReader.Peek() != -1)
- {
- string line = cifReader.ReadLine();
- if (line != null && validTypes.Contains(line.Substring(0, 2)))
- {
- _filelines.Add(line);
- }
- }
- }
- }
- catch (Exception e)
- {
- throw new LoadException("Error: " +
- e.Message +
- Environment.NewLine +
- "Please ensure a valid CIF is selected");
- }
- }
- /// <summary>
- /// Constructs the schedule
- /// </summary>
- /// <returns> A schedule comprising train classes</returns>
- public Schedule BuildSchedule()
- {
- var cifSchedule = new Schedule();
- try
- {
- Tiploc tiploc = Tiploc.GetInstance();
- // loop through file
- for (int i = 0; i < _filelines.Count; i++)
- {
- string stopLocation;
- switch (_filelines[i].Substring(0, 2))
- {
- case "BS":
- // Each new train starts with a BS line so will start here
- {
- // Check the train is a valid passenger train(Headcode starting 1 or 2)
- // and is a new entry.If not skip to next BS
- string[] passStops = { "1", "2" };
- if (passStops.Contains(_filelines[i].Substring(32, 1)) == false ||
- _filelines[i].Substring(2, 1) == "D")
- {
- // Finds next "BS" line and sets the counter so on the next iteration it starts there
- int stop = _filelines.Count();
- int newi = 0;
- for (int j = i + 1; j < stop; j++)
- {
- if (_filelines[j].Substring(0, 2) == "BS")
- {
- stop = j;
- newi = j - 1;
- }
- }
- i = newi;
- break;
- }
- BuildTrain(cifSchedule, i);
- break;
- }
- case "LO":
- // Each train will have locations with it. Add origin location to train instance
- {
- const string stopType = "Origin";
- stopLocation = _filelines[i].Substring(2, 8).Trim();
- stopLocation = tiploc.GetFullName(stopLocation);
- DateTime publicDeparture = DateTime.ParseExact(
- _filelines[i].Substring(15, 4),
- "HHmm",
- CultureInfo.InvariantCulture);
- DateTime workingDeparture = DateTime.ParseExact(
- _filelines[i].Substring(10, 4),
- "HHmm",
- CultureInfo.InvariantCulture);
- string platform = _filelines[i].Substring(19, 3);
- string line = _filelines[i].Substring(22, 3);
- // Counting up the allowances
- double allowances = 0.0;
- // Engineering allowances
- if (_filelines[i].Substring(25, 2) == " ")
- {
- allowances += 0.0;
- }
- else if (_filelines[i].Substring(26, 1) == "H" &
- _filelines[i].Substring(25, 1) != " ")
- {
- allowances += 0.5 + double.Parse(_filelines[i].Substring(25, 1));
- }
- else if (_filelines[i].Substring(26, 1) == "H" &
- _filelines[i].Substring(25, 1) == " ")
- {
- allowances += 0.5;
- }
- else
- {
- allowances += double.Parse(_filelines[i].Substring(25, 2));
- }
- // Pathing Allowance
- if (_filelines[i].Substring(27, 2) == " ")
- {
- allowances += 0.0;
- }
- else if (_filelines[i].Substring(28, 1) == "H" &
- _filelines[i].Substring(27, 1) != " ")
- {
- allowances += 0.5 + double.Parse(_filelines[i].Substring(27, 1));
- }
- else if (_filelines[i].Substring(28, 1) == "H" &
- _filelines[i].Substring(27, 1) == " ")
- {
- allowances += 0.5;
- }
- else
- {
- allowances += double.Parse(_filelines[i].Substring(27, 2));
- }
- // Performance Allowance
- if (_filelines[i].Substring(41, 2) == " ")
- {
- allowances += 0.0;
- }
- else if (_filelines[i].Substring(42, 1) == "H" &
- _filelines[i].Substring(41, 1) != " ")
- {
- allowances += 0.5 + double.Parse(_filelines[i].Substring(41, 1));
- }
- else if (_filelines[i].Substring(42, 1) == "H" &
- _filelines[i].Substring(41, 1) == " ")
- {
- allowances += 0.5;
- }
- else
- {
- allowances += double.Parse(_filelines[i].Substring(41, 2));
- }
- // Creating the class instance of location and adding it to the last train instance
- int last = cifSchedule.TrainArray.Count() - 1;
- cifSchedule.TrainArray[last].AddLocation(new Location(
- stopType,
- stopLocation,
- publicDeparture,
- workingDeparture,
- platform,
- line,
- allowances));
- break;
- }
- case "LI":
- // Each train will have locations with it. Add intermediate location to train instance
- {
- // Check the train stops to pick up and/or set down - marked in posistion 42 as T,U or D
- string[] validStops = { "T", "U", "D" };
- if (validStops.Contains(_filelines[i].Substring(42, 1)) == false)
- {
- }
- else
- {
- const string stopType = "Intermediate";
- stopLocation = _filelines[i].Substring(2, 8).Trim();
- stopLocation = tiploc.GetFullName(stopLocation);
- DateTime publicArrival = DateTime.ParseExact(
- _filelines[i].Substring(25, 4),
- "HHmm",
- CultureInfo.InvariantCulture);
- DateTime publicDeparture = DateTime.ParseExact(
- _filelines[i].Substring(29, 4),
- "HHmm",
- CultureInfo.InvariantCulture);
- DateTime workingArrival = DateTime.ParseExact(
- _filelines[i].Substring(10, 4),
- "HHmm",
- CultureInfo.InvariantCulture);
- DateTime workingDeparture = DateTime.ParseExact(
- _filelines[i].Substring(15, 4),
- "HHmm",
- CultureInfo.InvariantCulture);
- string platform = _filelines[i].Substring(33, 3);
- string line = _filelines[i].Substring(36, 3);
- // Counting up the allowances
- double allowances = 0.0;
- // Engineering allowances
- if (_filelines[i].Substring(54, 2) == " ")
- {
- allowances += 0.0;
- }
- else if (_filelines[i].Substring(55, 1) == "H" &
- _filelines[i].Substring(54, 1) != " ")
- {
- allowances += 0.5 + double.Parse(_filelines[i].Substring(54, 1));
- }
- else if (_filelines[i].Substring(55, 1) == "H" &
- _filelines[i].Substring(54, 1) == " ")
- {
- allowances += 0.5;
- }
- else
- {
- allowances += double.Parse(_filelines[i].Substring(54, 2));
- }
- // Pathing Allowance
- if (_filelines[i].Substring(56, 2) == " ")
- {
- allowances += 0.0;
- }
- else if (_filelines[i].Substring(57, 1) == "H" &
- _filelines[i].Substring(56, 1) != " ")
- {
- allowances += 0.5 + double.Parse(_filelines[i].Substring(56, 1));
- }
- else if (_filelines[i].Substring(57, 1) == "H" &
- _filelines[i].Substring(56, 1) == " ")
- {
- allowances += 0.5;
- }
- else
- {
- allowances += double.Parse(_filelines[i].Substring(56, 2));
- }
- // Performance Allowance
- if (_filelines[i].Substring(58, 2) == " ")
- {
- allowances += 0.0;
- }
- else if (_filelines[i].Substring(59, 1) == "H" &
- _filelines[i].Substring(58, 1) != " ")
- {
- allowances += 0.5 + double.Parse(_filelines[i].Substring(58, 1));
- }
- else if (_filelines[i].Substring(59, 1) == "H" &
- _filelines[i].Substring(58, 1) == " ")
- {
- allowances += 0.5;
- }
- else
- {
- allowances += double.Parse(_filelines[i].Substring(58, 2));
- }
- // Creating the class instance of location and adding it to the last train instance
- int last = cifSchedule.TrainArray.Count() - 1;
- cifSchedule.TrainArray[last].AddLocation(new Location(
- stopType,
- stopLocation,
- publicArrival,
- publicDeparture,
- workingArrival,
- workingDeparture,
- platform,
- line,
- allowances));
- }
- break;
- }
- case "LT":
- // Each train will have locations with it. Add terminus location to train instance
- {
- const string stopType = "Terminus";
- stopLocation = _filelines[i].Substring(2, 8).Trim();
- stopLocation = tiploc.GetFullName(stopLocation);
- DateTime publicArrival = DateTime.ParseExact(
- _filelines[i].Substring(15, 4),
- "HHmm",
- CultureInfo.InvariantCulture);
- DateTime workingArrival = DateTime.ParseExact(
- _filelines[i].Substring(10, 4),
- "HHmm",
- CultureInfo.InvariantCulture);
- string platform = _filelines[i].Substring(19, 3);
- // Creating the class instance of location and adding it to the last train instance
- int last = cifSchedule.TrainArray.Count() - 1;
- cifSchedule.TrainArray[last].AddLocation(new Location(
- stopType,
- stopLocation,
- publicArrival,
- workingArrival,
- platform));
- cifSchedule.TrainArray[last].LocationList.Sort();
- break;
- }
- }
- }
- }
- catch (KeyNotFoundException kex)
- {
- throw new LoadException(kex.Message);
- }
- catch (Exception ex)
- {
- throw new LoadException(
- "Error" +
- ex.Message +
- Environment.NewLine +
- "Please ensure the CIF is not corrupt");
- }
- // Throw exception if not been able to build any trains
- if (cifSchedule.TrainArray.Count == 0)
- {
- throw new LoadException("Error : Invalid CIF" + Environment.NewLine + "Please select a valid CIF");
- }
- cifSchedule.TrainArray.Sort();
- return cifSchedule;
- }
- /// <summary>
- /// constructs train classes and adds to schedule
- /// </summary>
- /// <param name="schedule">the schedule to which the train is added to</param>
- /// <param name="i">The start of the file lines for that train</param>
- private void BuildTrain(Schedule schedule, int i)
- {
- // Get the lookup classes
- Tiploc tiploc = Tiploc.GetInstance();
- Toc tocDict = Toc.GetInstance();
- // Getting the values from the BS line
- DateTime startDate = DateTime.ParseExact(
- _filelines[i].Substring(9, 6),
- "yyMMdd",
- CultureInfo.InvariantCulture);
- DateTime endDate;
- if (_filelines[i].Substring(15, 6) == "999999")
- {
- endDate = DateTime.MaxValue;
- }
- else
- {
- endDate = DateTime.ParseExact(
- _filelines[i].Substring(15, 6),
- "yyMMdd",
- CultureInfo.InvariantCulture);
- }
- string daysRun = _filelines[i].Substring(21, 7);
- string headCode = _filelines[i].Substring(32, 4);
- string serviceCode = _filelines[i].Substring(41, 8);
- // getting the value from the BX line
- string toc = _filelines[i + 1].Substring(11, 2);
- toc = tocDict.GetFullName(toc);
- if (schedule.GetTocs().Contains(toc) == false)
- {
- schedule.AddToc(toc);
- }
- // Getting the data from the location lines
- int endLoop = _filelines.Count();
- bool midnightTrain = false;
- string originLocation = string.Empty;
- DateTime originTime = DateTime.Now;
- string destinationLocation = string.Empty;
- DateTime destinationTime = DateTime.Now;
- // Loop through lines starting at first Location line
- for (int j = i + 2; j < endLoop; j++)
- {
- switch (_filelines[j].Substring(0, 2))
- {
- case "LO":
- {
- originLocation = tiploc.GetFullName(_filelines[j].Substring(2, 8).Trim());
- originTime = DateTime.ParseExact(
- _filelines[j].Substring(15, 4),
- "HHmm",
- CultureInfo.InvariantCulture);
- // Set train as after midnight if departs between midnight and 4am
- if (originTime.TimeOfDay >= new TimeSpan(0, 0, 0) &
- originTime.TimeOfDay < new TimeSpan(4, 0, 0))
- {
- midnightTrain = true;
- }
- break;
- }
- case "LT":
- {
- destinationLocation = tiploc.GetFullName(_filelines[j].Substring(2, 8).Trim());
- destinationTime = DateTime.ParseExact(
- _filelines[j].Substring(15, 4),
- "HHmm",
- CultureInfo.InvariantCulture);
- endLoop = j;
- break;
- }
- }
- }
- // Finally constructing the instance of Train and adding it to the schedule
- schedule.AddTrain(
- new Train(
- midnightTrain,
- daysRun,
- toc,
- originTime,
- originLocation,
- destinationTime,
- destinationLocation,
- headCode,
- "unknown",
- serviceCode,
- startDate,
- endDate));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement