internal Route(string path) { RouteFolder = FolderStructure.Route(path); string trkFilePath = RouteFolder.TrackFileName; try { var trkFile = new RouteFile(trkFilePath); Name = trkFile.Route.Name; RouteID = trkFile.Route.RouteID; Description = trkFile.Route.Description; } catch { Name = $"<{catalog.GetString("load error:")} {System.IO.Path.GetFileName(path)}>"; } if (string.IsNullOrEmpty(Name)) { Name = $"<{catalog.GetString("unnamed:")} {System.IO.Path.GetFileNameWithoutExtension(path)}>"; } if (string.IsNullOrEmpty(Description)) { Description = null; } Path = path; }
internal Route(string path) { RouteFolder = FolderStructure.Route(path); string trkFilePath = RouteFolder.TrackFileName; try { RouteFile trkFile = new RouteFile(trkFilePath); Name = trkFile.Route.Name; RouteID = trkFile.Route.RouteID; Description = trkFile.Route.Description; } #pragma warning disable CA1031 // Do not catch general exception types catch #pragma warning restore CA1031 // Do not catch general exception types { Name = $"<{catalog.GetString("load error:")} {System.IO.Path.GetFileName(path)}>"; } if (string.IsNullOrEmpty(Name)) { Name = $"<{catalog.GetString("unnamed:")} {System.IO.Path.GetFileNameWithoutExtension(path)}>"; } if (string.IsNullOrEmpty(Description)) { Description = null; } Path = path; }
public static async Task <IEnumerable <Route> > GetRoutes(Folder folder, CancellationToken token) { if (null == folder) { throw new ArgumentNullException(nameof(folder)); } using (SemaphoreSlim addItem = new SemaphoreSlim(1)) { List <Route> result = new List <Route>(); string routesDirectory = folder.ContentFolder.RoutesFolder; if (Directory.Exists(routesDirectory)) { ActionBlock <string> actionBlock = new ActionBlock <string> (async routeDirectory => { if (FolderStructure.Route(routeDirectory).IsValid) { try { Route route = new Route(routeDirectory); await addItem.WaitAsync(token).ConfigureAwait(false); result.Add(route); } finally { addItem.Release(); } } }, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = Environment.ProcessorCount, CancellationToken = token }); foreach (string routeDirectory in Directory.EnumerateDirectories(routesDirectory)) { await actionBlock.SendAsync(routeDirectory).ConfigureAwait(false); } actionBlock.Complete(); await actionBlock.Completion.ConfigureAwait(false); } return(result); } }
internal async Task LoadTrackData(bool?useMetricUnits, CancellationToken cancellationToken) { List <Task> loadTasks = new List <Task>(); FolderStructure.ContentFolder.RouteFolder routeFolder = FolderStructure.Route(routePath); RouteFile routeFile = new RouteFile(routeFolder.TrackFileName); RouteName = routeFile.Route.Name; UseMetricUnits = useMetricUnits.GetValueOrDefault(routeFile.Route.MilepostUnitsMetric); loadTasks.Add(Task.Run(() => { string tdbFile = routeFolder.TrackDatabaseFile(routeFile); if (!File.Exists(tdbFile)) { Trace.TraceError($"Track Database File not found in {tdbFile}"); return; } TrackDB = new TrackDatabaseFile(tdbFile).TrackDB; }, cancellationToken)); loadTasks.Add(Task.Run(() => { TrackSections = new TrackSectionsFile(routeFolder.TrackSectionFile); if (File.Exists(routeFolder.RouteTrackSectionFile)) { TrackSections.AddRouteTSectionDatFile(routeFolder.RouteTrackSectionFile); } }, cancellationToken)); loadTasks.Add(Task.Run(() => { string rdbFile = routeFolder.RoadTrackDatabaseFile(routeFile); if (!File.Exists(rdbFile)) { Trace.TraceError($"Road Database File not found in {rdbFile}"); return; } RoadTrackDB = new RoadDatabaseFile(rdbFile).RoadTrackDB; }, cancellationToken)); loadTasks.Add(Task.Run(() => SignalConfig = new SignalConfigurationFile(routeFolder.SignalConfigurationFile, routeFolder.ORSignalConfigFile), cancellationToken)); await Task.WhenAll(loadTasks).ConfigureAwait(false); }
public static async Task<IEnumerable<TestActivity>> GetTestActivities(Dictionary<string, string> folders, CancellationToken token) { if (null == folders) throw new ArgumentNullException(nameof(folders)); using (SemaphoreSlim addItem = new SemaphoreSlim(1)) { List<TestActivity> result = new List<TestActivity>(); TransformManyBlock<KeyValuePair<string, string>, (Folder, string)> inputBlock = new TransformManyBlock<KeyValuePair<string, string>, (Folder, string)> (folderName => { Folder folder = new Folder(folderName.Key, folderName.Value); string routesDirectory = folder.ContentFolder.RoutesFolder; if (Directory.Exists(routesDirectory)) { return Directory.EnumerateDirectories(routesDirectory).Select(r => (folder, r)); } else return Array.Empty<(Folder, string)>(); }, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = Environment.ProcessorCount, CancellationToken = token }); TransformManyBlock<(Folder, string), (Folder, Route, string)> routeBlock = new TransformManyBlock<(Folder folder, string routeDirectory), (Folder, Route, string)> (routeFile => { if (FolderStructure.Route(routeFile.routeDirectory).IsValid) { Route route = new Route(routeFile.routeDirectory); string activitiesDirectory = route.RouteFolder.ActivitiesFolder; if (Directory.Exists(activitiesDirectory)) { return Directory.EnumerateFiles(activitiesDirectory, "*.act").Select(a => (routeFile.folder, route, a)); } } return Array.Empty<(Folder, Route, string)>(); }, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = Environment.ProcessorCount, CancellationToken = token }); TransformBlock<(Folder, Route, string), TestActivity> activityBlock = new TransformBlock<(Folder folder, Route route, string activity), TestActivity> (activityInput => { Activity activity = Simplified.Activity.FromPathShallow(activityInput.activity); return activity != null ? new TestActivity(activityInput.folder, activityInput.route, activity) : null; }, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = Environment.ProcessorCount, CancellationToken = token }); ActionBlock<TestActivity> actionBlock = new ActionBlock<TestActivity> (async activity => { if (null == activity) return; try { await addItem.WaitAsync(token).ConfigureAwait(false); result.Add(activity); } finally { addItem.Release(); } }); inputBlock.LinkTo(routeBlock, new DataflowLinkOptions { PropagateCompletion = true }); routeBlock.LinkTo(activityBlock, new DataflowLinkOptions { PropagateCompletion = true }); activityBlock.LinkTo(actionBlock, new DataflowLinkOptions { PropagateCompletion = true }); foreach (KeyValuePair<string, string> folder in folders) await inputBlock.SendAsync(folder).ConfigureAwait(false); inputBlock.Complete(); await actionBlock.Completion.ConfigureAwait(false); return result; } }