Advertisement
Guest User

CifData

a guest
Jun 17th, 2014
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 21.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using CIFProcessing.Dictionaries;
  7. using CIFProcessing.Exceptions;
  8.  
  9. namespace CIFProcessing.Classes
  10. {
  11.     /// <summary>
  12.     /// Stores the initial cif data and process it so a schedule can be formed
  13.     /// </summary>
  14.     class CifData
  15.     {
  16.         private readonly List<string> _filelines;
  17.  
  18.         /// <summary>
  19.         /// Initializes a new instance of the <see cref="CifData"/> class.
  20.         /// </summary>
  21.         /// <param name="filePath">string containing full path to the cif</param>
  22.         public CifData(string filePath)
  23.         {
  24.             _filelines = new List<string>();
  25.            
  26.             // Read lines into list, only if relevant
  27.             try
  28.             {
  29.                 string[] validTypes = { "BS", "BX", "LO", "LI", "LT" };
  30.                 using (var cifReader = new StreamReader(filePath))
  31.                 {
  32.                     while (cifReader.Peek() != -1)
  33.                     {
  34.                         string line = cifReader.ReadLine();
  35.                         if (line != null && validTypes.Contains(line.Substring(0, 2)))
  36.                         {
  37.                             _filelines.Add(line);
  38.                         }
  39.                     }
  40.                 }
  41.             }
  42.             catch (Exception e)
  43.             {
  44.                 throw new LoadException("Error: " +
  45.                                         e.Message +
  46.                                         Environment.NewLine +
  47.                                         "Please ensure a valid CIF is selected");
  48.             }
  49.         }
  50.  
  51.         /// <summary>
  52.         /// Constructs the schedule
  53.         /// </summary>
  54.         /// <returns> A schedule comprising train classes</returns>
  55.         public Schedule BuildSchedule()
  56.         {
  57.             var cifSchedule = new Schedule();
  58.             try
  59.             {
  60.                 Tiploc tiploc = Tiploc.GetInstance();
  61.  
  62.                 // loop through file            
  63.                 for (int i = 0; i < _filelines.Count; i++)
  64.                 {
  65.                     string stopLocation;
  66.                     switch (_filelines[i].Substring(0, 2))
  67.                     {
  68.                         case "BS":
  69.                             // Each new train starts with a BS line so will start here
  70.                             {
  71.                                 // Check the train is a valid passenger train(Headcode starting 1 or 2)
  72.                                 // and is a new entry.If not skip to next BS
  73.                                 string[] passStops = { "1", "2" };
  74.                                 if (passStops.Contains(_filelines[i].Substring(32, 1)) == false ||
  75.                                     _filelines[i].Substring(2, 1) == "D")
  76.                                 {
  77.                                     // Finds next "BS" line and sets the counter so on the next iteration it starts there
  78.                                     int stop = _filelines.Count();
  79.                                     int newi = 0;
  80.                                     for (int j = i + 1; j < stop; j++)
  81.                                     {
  82.                                         if (_filelines[j].Substring(0, 2) == "BS")
  83.                                         {
  84.                                             stop = j;
  85.                                             newi = j - 1;
  86.                                         }
  87.                                     }
  88.  
  89.                                     i = newi;
  90.                                     break;
  91.                                 }
  92.                                 BuildTrain(cifSchedule, i);
  93.                                 break;
  94.                             }
  95.  
  96.                         case "LO":
  97.  
  98.                             // Each train will have locations with it. Add origin location to train instance
  99.                             {
  100.                                 const string stopType = "Origin";
  101.                                 stopLocation = _filelines[i].Substring(2, 8).Trim();
  102.                                 stopLocation = tiploc.GetFullName(stopLocation);
  103.                                 DateTime publicDeparture = DateTime.ParseExact(
  104.                                     _filelines[i].Substring(15, 4),
  105.                                     "HHmm",
  106.                                     CultureInfo.InvariantCulture);
  107.                                 DateTime workingDeparture = DateTime.ParseExact(
  108.                                     _filelines[i].Substring(10, 4),
  109.                                     "HHmm",
  110.                                     CultureInfo.InvariantCulture);
  111.                                 string platform = _filelines[i].Substring(19, 3);
  112.                                 string line = _filelines[i].Substring(22, 3);
  113.  
  114.                                 // Counting up the allowances
  115.                                 double allowances = 0.0;
  116.  
  117.                                 // Engineering allowances
  118.                                 if (_filelines[i].Substring(25, 2) == "  ")
  119.                                 {
  120.                                     allowances += 0.0;
  121.                                 }
  122.                                 else if (_filelines[i].Substring(26, 1) == "H" &
  123.                                          _filelines[i].Substring(25, 1) != " ")
  124.                                 {
  125.                                     allowances += 0.5 + double.Parse(_filelines[i].Substring(25, 1));
  126.                                 }
  127.                                 else if (_filelines[i].Substring(26, 1) == "H" &
  128.                                          _filelines[i].Substring(25, 1) == " ")
  129.                                 {
  130.                                     allowances += 0.5;
  131.                                 }
  132.                                 else
  133.                                 {
  134.                                     allowances += double.Parse(_filelines[i].Substring(25, 2));
  135.                                 }
  136.  
  137.                                 // Pathing Allowance
  138.                                 if (_filelines[i].Substring(27, 2) == "  ")
  139.                                 {
  140.                                     allowances += 0.0;
  141.                                 }
  142.                                 else if (_filelines[i].Substring(28, 1) == "H" &
  143.                                          _filelines[i].Substring(27, 1) != " ")
  144.                                 {
  145.                                     allowances += 0.5 + double.Parse(_filelines[i].Substring(27, 1));
  146.                                 }
  147.                                 else if (_filelines[i].Substring(28, 1) == "H" &
  148.                                          _filelines[i].Substring(27, 1) == " ")
  149.                                 {
  150.                                     allowances += 0.5;
  151.                                 }
  152.                                 else
  153.                                 {
  154.                                     allowances += double.Parse(_filelines[i].Substring(27, 2));
  155.                                 }
  156.  
  157.                                 // Performance Allowance
  158.                                 if (_filelines[i].Substring(41, 2) == "  ")
  159.                                 {
  160.                                     allowances += 0.0;
  161.                                 }
  162.                                 else if (_filelines[i].Substring(42, 1) == "H" &
  163.                                          _filelines[i].Substring(41, 1) != " ")
  164.                                 {
  165.                                     allowances += 0.5 + double.Parse(_filelines[i].Substring(41, 1));
  166.                                 }
  167.                                 else if (_filelines[i].Substring(42, 1) == "H" &
  168.                                          _filelines[i].Substring(41, 1) == " ")
  169.                                 {
  170.                                     allowances += 0.5;
  171.                                 }
  172.                                 else
  173.                                 {
  174.                                     allowances += double.Parse(_filelines[i].Substring(41, 2));
  175.                                 }
  176.  
  177.                                 // Creating the class instance of location and adding it to the last train instance
  178.                                 int last = cifSchedule.TrainArray.Count() - 1;
  179.                                 cifSchedule.TrainArray[last].AddLocation(new Location(
  180.                                     stopType,
  181.                                     stopLocation,
  182.                                     publicDeparture,
  183.                                     workingDeparture,
  184.                                     platform,
  185.                                     line,
  186.                                     allowances));
  187.                                 break;
  188.                             }
  189.  
  190.                         case "LI":
  191.                             // Each train will have locations with it. Add intermediate location to train instance
  192.                             {
  193.                                 // Check the train stops to pick up and/or set down - marked in posistion 42 as T,U or D
  194.                                 string[] validStops = { "T", "U", "D" };
  195.                                 if (validStops.Contains(_filelines[i].Substring(42, 1)) == false)
  196.                                 {
  197.                                 }
  198.                                 else
  199.                                 {
  200.                                     const string stopType = "Intermediate";
  201.                                     stopLocation = _filelines[i].Substring(2, 8).Trim();
  202.                                     stopLocation = tiploc.GetFullName(stopLocation);
  203.                                     DateTime publicArrival = DateTime.ParseExact(
  204.                                         _filelines[i].Substring(25, 4),
  205.                                         "HHmm",
  206.                                         CultureInfo.InvariantCulture);
  207.                                     DateTime publicDeparture = DateTime.ParseExact(
  208.                                         _filelines[i].Substring(29, 4),
  209.                                         "HHmm",
  210.                                         CultureInfo.InvariantCulture);
  211.                                     DateTime workingArrival = DateTime.ParseExact(
  212.                                         _filelines[i].Substring(10, 4),
  213.                                         "HHmm",
  214.                                         CultureInfo.InvariantCulture);
  215.                                     DateTime workingDeparture = DateTime.ParseExact(
  216.                                         _filelines[i].Substring(15, 4),
  217.                                         "HHmm",
  218.                                         CultureInfo.InvariantCulture);
  219.                                     string platform = _filelines[i].Substring(33, 3);
  220.                                     string line = _filelines[i].Substring(36, 3);
  221.  
  222.                                     // Counting up the allowances
  223.                                     double allowances = 0.0;
  224.  
  225.                                     // Engineering allowances
  226.                                     if (_filelines[i].Substring(54, 2) == "  ")
  227.                                     {
  228.                                         allowances += 0.0;
  229.                                     }
  230.                                     else if (_filelines[i].Substring(55, 1) == "H" &
  231.                                              _filelines[i].Substring(54, 1) != " ")
  232.                                     {
  233.                                         allowances += 0.5 + double.Parse(_filelines[i].Substring(54, 1));
  234.                                     }
  235.                                     else if (_filelines[i].Substring(55, 1) == "H" &
  236.                                              _filelines[i].Substring(54, 1) == " ")
  237.                                     {
  238.                                         allowances += 0.5;
  239.                                     }
  240.                                     else
  241.                                     {
  242.                                         allowances += double.Parse(_filelines[i].Substring(54, 2));
  243.                                     }
  244.  
  245.                                     // Pathing Allowance
  246.                                     if (_filelines[i].Substring(56, 2) == "  ")
  247.                                     {
  248.                                         allowances += 0.0;
  249.                                     }
  250.                                     else if (_filelines[i].Substring(57, 1) == "H" &
  251.                                              _filelines[i].Substring(56, 1) != " ")
  252.                                     {
  253.                                         allowances += 0.5 + double.Parse(_filelines[i].Substring(56, 1));
  254.                                     }
  255.                                     else if (_filelines[i].Substring(57, 1) == "H" &
  256.                                              _filelines[i].Substring(56, 1) == " ")
  257.                                     {
  258.                                         allowances += 0.5;
  259.                                     }
  260.                                     else
  261.                                     {
  262.                                         allowances += double.Parse(_filelines[i].Substring(56, 2));
  263.                                     }
  264.  
  265.                                     // Performance Allowance
  266.                                     if (_filelines[i].Substring(58, 2) == "  ")
  267.                                     {
  268.                                         allowances += 0.0;
  269.                                     }
  270.                                     else if (_filelines[i].Substring(59, 1) == "H" &
  271.                                              _filelines[i].Substring(58, 1) != " ")
  272.                                     {
  273.                                         allowances += 0.5 + double.Parse(_filelines[i].Substring(58, 1));
  274.                                     }
  275.                                     else if (_filelines[i].Substring(59, 1) == "H" &
  276.                                              _filelines[i].Substring(58, 1) == " ")
  277.                                     {
  278.                                         allowances += 0.5;
  279.                                     }
  280.                                     else
  281.                                     {
  282.                                         allowances += double.Parse(_filelines[i].Substring(58, 2));
  283.                                     }
  284.  
  285.                                     // Creating the class instance of location and adding it to the last train instance
  286.                                     int last = cifSchedule.TrainArray.Count() - 1;
  287.                                     cifSchedule.TrainArray[last].AddLocation(new Location(
  288.                                         stopType,
  289.                                         stopLocation,
  290.                                         publicArrival,
  291.                                         publicDeparture,
  292.                                         workingArrival,
  293.                                         workingDeparture,
  294.                                         platform,
  295.                                         line,
  296.                                         allowances));
  297.                                 }
  298.  
  299.                                 break;
  300.                             }
  301.  
  302.                         case "LT":
  303.  
  304.                             // Each train will have locations with it. Add terminus location to train instance
  305.                             {
  306.                                 const string stopType = "Terminus";
  307.                                 stopLocation = _filelines[i].Substring(2, 8).Trim();
  308.                                 stopLocation = tiploc.GetFullName(stopLocation);
  309.                                 DateTime publicArrival = DateTime.ParseExact(
  310.                                     _filelines[i].Substring(15, 4),
  311.                                     "HHmm",
  312.                                     CultureInfo.InvariantCulture);
  313.                                 DateTime workingArrival = DateTime.ParseExact(
  314.                                     _filelines[i].Substring(10, 4),
  315.                                     "HHmm",
  316.                                     CultureInfo.InvariantCulture);
  317.                                 string platform = _filelines[i].Substring(19, 3);
  318.  
  319.                                 // Creating the class instance of location and adding it to the last train instance
  320.                                 int last = cifSchedule.TrainArray.Count() - 1;
  321.                                 cifSchedule.TrainArray[last].AddLocation(new Location(
  322.                                     stopType,
  323.                                     stopLocation,
  324.                                     publicArrival,
  325.                                     workingArrival,
  326.                                     platform));
  327.                                 cifSchedule.TrainArray[last].LocationList.Sort();
  328.                                 break;
  329.                             }
  330.                     }
  331.                 }
  332.             }
  333.             catch (KeyNotFoundException kex)
  334.             {
  335.                 throw new LoadException(kex.Message);
  336.             }
  337.             catch (Exception ex)
  338.             {
  339.                 throw new LoadException(
  340.                     "Error" +
  341.                     ex.Message +
  342.                     Environment.NewLine +
  343.                     "Please ensure the CIF is not corrupt");
  344.             }
  345.  
  346.             // Throw exception if not been able to build any trains
  347.             if (cifSchedule.TrainArray.Count == 0)
  348.             {
  349.                 throw new LoadException("Error : Invalid CIF" + Environment.NewLine + "Please select a valid CIF");
  350.             }
  351.  
  352.             cifSchedule.TrainArray.Sort();
  353.             return cifSchedule;
  354.         }
  355.  
  356.         /// <summary>
  357.         /// constructs train classes and adds to schedule
  358.         /// </summary>
  359.         /// <param name="schedule">the schedule to which the train is added to</param>
  360.         /// <param name="i">The start of the file lines for that train</param>
  361.         private void BuildTrain(Schedule schedule, int i)
  362.         {
  363.             // Get the lookup classes
  364.             Tiploc tiploc = Tiploc.GetInstance();
  365.             Toc tocDict = Toc.GetInstance();
  366.  
  367.             // Getting the values from the BS line
  368.             DateTime startDate = DateTime.ParseExact(
  369.                 _filelines[i].Substring(9, 6),
  370.                 "yyMMdd",
  371.                 CultureInfo.InvariantCulture);
  372.             DateTime endDate;
  373.             if (_filelines[i].Substring(15, 6) == "999999")
  374.             {
  375.                 endDate = DateTime.MaxValue;
  376.             }
  377.             else
  378.             {
  379.                 endDate = DateTime.ParseExact(
  380.                     _filelines[i].Substring(15, 6),
  381.                     "yyMMdd",
  382.                     CultureInfo.InvariantCulture);
  383.             }
  384.  
  385.             string daysRun = _filelines[i].Substring(21, 7);
  386.             string headCode = _filelines[i].Substring(32, 4);
  387.             string serviceCode = _filelines[i].Substring(41, 8);
  388.  
  389.             // getting the value from the BX line
  390.             string toc = _filelines[i + 1].Substring(11, 2);
  391.             toc = tocDict.GetFullName(toc);
  392.             if (schedule.GetTocs().Contains(toc) == false)
  393.             {
  394.                 schedule.AddToc(toc);
  395.             }
  396.  
  397.             // Getting the data from the location lines
  398.             int endLoop = _filelines.Count();
  399.             bool midnightTrain = false;
  400.             string originLocation = string.Empty;
  401.             DateTime originTime = DateTime.Now;
  402.             string destinationLocation = string.Empty;
  403.             DateTime destinationTime = DateTime.Now;
  404.  
  405.             // Loop through lines starting at first Location line
  406.             for (int j = i + 2; j < endLoop; j++)
  407.             {
  408.                 switch (_filelines[j].Substring(0, 2))
  409.                 {
  410.                     case "LO":
  411.                         {
  412.                             originLocation = tiploc.GetFullName(_filelines[j].Substring(2, 8).Trim());
  413.                             originTime = DateTime.ParseExact(
  414.                                 _filelines[j].Substring(15, 4),
  415.                                 "HHmm",
  416.                                 CultureInfo.InvariantCulture);
  417.  
  418.                             // Set train as after midnight if departs between midnight and 4am
  419.                             if (originTime.TimeOfDay >= new TimeSpan(0, 0, 0) &
  420.                                 originTime.TimeOfDay < new TimeSpan(4, 0, 0))
  421.                             {
  422.                                 midnightTrain = true;
  423.                             }
  424.  
  425.                             break;
  426.                         }
  427.  
  428.                     case "LT":
  429.                         {
  430.                             destinationLocation = tiploc.GetFullName(_filelines[j].Substring(2, 8).Trim());
  431.                             destinationTime = DateTime.ParseExact(
  432.                                 _filelines[j].Substring(15, 4),
  433.                                 "HHmm",
  434.                                 CultureInfo.InvariantCulture);
  435.                             endLoop = j;
  436.                             break;
  437.                         }
  438.                 }
  439.             }
  440.  
  441.             // Finally constructing the instance of Train and adding it to the schedule
  442.             schedule.AddTrain(
  443.                 new Train(
  444.                 midnightTrain,
  445.                 daysRun,
  446.                 toc,
  447.                 originTime,
  448.                 originLocation,
  449.                 destinationTime,
  450.                 destinationLocation,
  451.                 headCode,
  452.                 "unknown",
  453.                 serviceCode,
  454.                 startDate,
  455.                 endDate));
  456.         }
  457.     }
  458. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement