Advertisement
Guest User

CifData

a guest
Jun 17th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 12.39 KB | None | 0 0
  1. Imports System
  2. Imports System.Collections.Generic
  3. Imports System.Globalization
  4. Imports System.IO
  5. Imports System.Linq
  6. Imports CIFReader.Dictionaries
  7. Imports CIFReader.Exceptions
  8.  
  9. Namespace CIFReader.Classes
  10.  
  11.     Class CifData
  12.  
  13.         Private ReadOnly _filelines As List(Of String)
  14.  
  15.         Public Sub New(filepath As String)
  16.             _filelines = New List(Of String)()
  17.  
  18.             Try
  19.                 Dim validTypes As String() = {"BS", "BX", "LO", "LI", "LT"}
  20.                 Using CIFReader = New StreamReader(filepath)
  21.                     While CIFReader.Peek() <> -1
  22.                         Dim line As String = CIFReader.ReadLine()
  23.                         If line IsNot Nothing AndAlso validTypes.Contains(line.Substring(0, 2)) Then
  24.                             _filelines.Add(line)
  25.                         End If
  26.                     End While
  27.                 End Using
  28.             Catch ex As Exception
  29.                 Throw New LoadException("Error: " & ex.Message & Environment.NewLine &
  30.                                         "Please ensure a valid CIF is selected")
  31.             End Try
  32.         End Sub
  33.  
  34.         Public Function BuildSchedule() As Schedule
  35.             Dim cifSchedule = New Schedule()
  36.             Try
  37.                 Dim tiploc_1 As Tiploc = Tiploc.GetInstance()
  38.                 For i As Integer = 0 To _filelines.Count - 1
  39.                     Dim stopLocation As String
  40.                     Select Case _filelines(i).Substring(0, 2)
  41.                         Case "BS"
  42.                             If True Then
  43.                                 Dim passStops As String() = {"1", "2"}
  44.                                 If passStops.Contains(_filelines(i).Substring(32, 1)) = False OrElse _filelines(i).Substring(2, 1) = "D" Then
  45.                                     Dim [stop] As Integer = _filelines.Count()
  46.                                     Dim newi As Integer = 0
  47.                                     For j As Integer = i + 1 To [stop] - 1
  48.                                         If _filelines(j).Substring(0, 2) = "BS" Then
  49.                                             [stop] = j
  50.                                             newi = j - 1
  51.                                         End If
  52.                                     Next
  53.  
  54.                                     i = newi
  55.                                     Exit Select
  56.                                 End If
  57.                                 BuildTrain(cifSchedule, i)
  58.                                 Exit Select
  59.                             End If
  60.  
  61.                         Case "LO"
  62.                             If True Then
  63.                                 Const stopType As String = "Origin"
  64.                                 stopLocation = _filelines(i).Substring(2, 7).Trim()
  65.                                 stopLocation = tiploc_1.getFullName(stopLocation)
  66.                                 Dim publicDeparture As DateTime = DateTime.ParseExact(_filelines(i).Substring(15, 4), "HHmm", CultureInfo.InvariantCulture)
  67.                                 Dim workingDeparture As DateTime = workingTimes(i, 10)
  68.                                 Dim platform As String = _filelines(i).Substring(19, 3).Trim()
  69.                                 Dim line As String = _filelines(i).Substring(22, 3).Trim()
  70.                                 Dim allowances As Double = 0.0
  71.                                 'Engineering Allowance
  72.                                 allowances = allowancesAdd(i, 25)
  73.                                 'Pathing Allowance
  74.                                 allowances = allowancesAdd(i, 27)
  75.                                 'Performance Allowance
  76.                                 allowances = allowancesAdd(i, 41)
  77.                                 Dim last As Integer = cifSchedule.TrainArray.Count() - 1
  78.                                 cifSchedule.TrainArray(last).AddLocation(New Location(stopType, stopLocation,
  79.                                                                                       publicDeparture, workingDeparture,
  80.                                                                                       platform, line, allowances))
  81.                                 Exit Select
  82.                             End If
  83.  
  84.                         Case "LI"
  85.                             If True Then
  86.                                 Dim validStops As String() = {"T", "U", "D"}
  87.                                 If validStops.Contains(_filelines(i).Substring(42, 1)) = False Then
  88.                                 Else
  89.                                     Const stopType As String = "Intermediate"
  90.                                     stopLocation = _filelines(i).Substring(2, 7).Trim
  91.                                     stopLocation = tiploc_1.getFullName(stopLocation)
  92.                                     Dim publicArrival As DateTime = DateTime.ParseExact(_filelines(i).Substring(25, 4), "HHmm", CultureInfo.InvariantCulture)
  93.                                     Dim publicDeparture As DateTime = DateTime.ParseExact(_filelines(i).Substring(29, 4), "HHmm", CultureInfo.InvariantCulture)
  94.                                     'Flag for inputting Half-minutes to working times, if possible.
  95.                                     Dim workingArrival As DateTime = workingTimes(i, 10)
  96.                                     Dim workingDeparture As DateTime = workingTimes(i, 15)
  97.                                     Dim platform As String = _filelines(i).Substring(33, 3).Trim
  98.                                     Dim line As String = _filelines(i).Substring(36, 3).Trim
  99.  
  100.                                     Dim allowances As Double = 0.0
  101.                                     'Engineering Allowance
  102.                                     allowances = allowancesAdd(i, 54)
  103.                                     'Pathing Allowance
  104.                                     allowances = allowancesAdd(i, 56)
  105.                                     'Performance Allowance
  106.                                     allowances = allowancesAdd(i, 58)
  107.  
  108.                                     Dim last As Integer = cifSchedule.TrainArray.Count() - 1
  109.                                     cifSchedule.TrainArray(last).AddLocation(New Location(stopType, stopLocation, publicArrival,
  110.                                                                                           publicDeparture, workingArrival,
  111.                                                                                           workingDeparture, platform, line,
  112.                                                                                           allowances))
  113.  
  114.                                 End If
  115.                                 Exit Select
  116.                             End If
  117.  
  118.                         Case "LT"
  119.                             If True Then
  120.                                 Const stopType As String = "Terminus"
  121.                                 stopLocation = _filelines(i).Substring(2, 7).Trim
  122.                                 stopLocation = tiploc_1.getFullName(stopLocation)
  123.                                 Dim publicArrival As DateTime = DateTime.ParseExact(_filelines(i).Substring(15, 4), "HHmm", CultureInfo.InvariantCulture)
  124.                                 Dim workingArrival As DateTime = workingTimes(i, 10)
  125.                                 Dim platform As String = _filelines(i).Substring(19, 3)
  126.  
  127.                                 Dim last As Integer = cifSchedule.TrainArray.Count() - 1
  128.                                 cifSchedule.TrainArray(last).AddLocation(New Location(stopType, stopLocation, publicArrival,
  129.                                                                                       workingArrival, platform))
  130.                                 cifSchedule.TrainArray(last).locationList.Sort()
  131.                                 Exit Select
  132.                             End If
  133.  
  134.  
  135.                     End Select
  136.                 Next
  137.  
  138.             Catch kex As KeyNotFoundException
  139.                 Throw New LoadException(kex.Message)
  140.             Catch ex As Exception
  141.                 Throw New LoadException("Error: " & ex.Message & Environment.NewLine & "Please ensure CIF is not corrupt")
  142.             End Try
  143.             If cifSchedule.TrainArray.Count = 0 Then
  144.                 Throw New LoadException("Error: Invalid CIF" & Environment.NewLine & "No trains found")
  145.             End If
  146.  
  147.             cifSchedule.TrainArray.Sort()
  148.             Return cifSchedule
  149.         End Function
  150.  
  151.         Private Sub BuildTrain(schedule As Schedule, i As Integer)
  152.  
  153.             Dim tiploc_1 As Tiploc = Tiploc.GetInstance()
  154.             Dim tocDict As Toc = Toc.GetInstance()
  155.  
  156.             Dim startDate As DateTime = DateTime.ParseExact(_filelines(i).Substring(9, 6), "yyMMdd", CultureInfo.InvariantCulture)
  157.             Dim endDate As DateTime
  158.             If _filelines(i).Substring(15, 6) = "999999" Then
  159.                 endDate = DateTime.MaxValue
  160.             Else
  161.                 endDate = DateTime.ParseExact(_filelines(i).Substring(15, 6), "yyMMdd", CultureInfo.InvariantCulture)
  162.             End If
  163.  
  164.             Dim daysRun As String = _filelines(i).Substring(21, 7)
  165.             Dim headCode As String = _filelines(i).Substring(32, 4)
  166.             Dim serviceCode As String = _filelines(i).Substring(41, 8)
  167.  
  168.             Dim toc_2 As String = _filelines(i + 1).Substring(11, 2)
  169.             toc_2 = tocDict.GetFullName(toc_2)
  170.             If schedule.GetTocs().Contains(toc_2) = False Then
  171.                 schedule.AddToc(toc_2)
  172.             End If
  173.  
  174.             Dim endLoop As Integer = _filelines.Count
  175.             Dim midnightTrain As Boolean = False
  176.             Dim originLocation As String = String.Empty
  177.             Dim originTime As DateTime = DateTime.Now
  178.             Dim destinationLocation As String = String.Empty
  179.             Dim destinationTime As DateTime = DateTime.Now
  180.  
  181.             For j As Integer = i + 2 To endLoop - 1
  182.                 Select Case _filelines(j).Substring(0, 2)
  183.                     Case "LO"
  184.                         If True Then
  185.                             originLocation = tiploc_1.getFullName(_filelines(j).Substring(2, 7).Trim)
  186.                             originTime = Date.ParseExact(_filelines(j).Substring(15, 4), "HHmm", CultureInfo.InvariantCulture)
  187.  
  188.                             If originTime.TimeOfDay >= New TimeSpan(0, 0, 0) And originTime.TimeOfDay < New TimeSpan(4, 0, 0) Then
  189.                                 midnightTrain = True
  190.                             End If
  191.                             Exit Select
  192.                         End If
  193.  
  194.                     Case "LT"
  195.                         If True Then
  196.                             destinationLocation = tiploc_1.getFullName(_filelines(j).Substring(2, 7).Trim)
  197.                             destinationTime = Date.ParseExact(_filelines(j).Substring(15, 4), "HHmm", CultureInfo.InvariantCulture)
  198.                             Exit For
  199.                         End If
  200.                 End Select
  201.             Next
  202.  
  203.  
  204.             schedule.AddTrain(New Train(midnightTrain, daysRun, toc_2, originTime, originLocation, destinationTime,
  205.                                         destinationLocation, headCode, "unknown", serviceCode, startDate, endDate))
  206.  
  207.         End Sub
  208.  
  209.  
  210.         Public Function allowancesAdd(row, column)
  211.             Dim allowances As Double = 0.0
  212.             If _filelines(row).Substring(column, 2) = "  " Then
  213.                 allowances += 0.0
  214.             ElseIf _filelines(row).Substring(column + 1, 1) = "H" And _filelines(row).Substring(column, 1) <> " " Then
  215.                 allowances += 0.5 + Double.Parse(_filelines(row).Substring(column, 1))
  216.             ElseIf _filelines(row).Substring(column + 1, 1) = "H" And _filelines(row).Substring(column, 1) = " " Then
  217.                 allowances += 0.5
  218.             Else
  219.                 allowances += Double.Parse(_filelines(row).Substring(column, 2).Trim)
  220.             End If
  221.             Return allowances
  222.         End Function
  223.  
  224.         Public Function workingTimes(row, column)
  225.             Dim workingTime As DateTime
  226.             Dim timeString As String = _filelines(row).Substring(column, 4)
  227.             If _filelines(row).Substring(column + 4, 1) = "H" Then
  228.                 workingTime = DateAdd(DateInterval.Second, 30, Date.ParseExact(timeString, "HHmm", CultureInfo.InvariantCulture))
  229.             Else
  230.                 workingTime = Date.ParseExact(timeString, "HHmm", CultureInfo.InvariantCulture)
  231.             End If
  232.  
  233.             Return workingTime
  234.         End Function
  235.  
  236.     End Class
  237.  
  238. End Namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement