/* * Copyright (c) 2005-2006 Erik Tigerholm * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ namespace OpenTraffic.Model.Importer { using System; using System.Collections.Generic; using System.IO; using OpenTraffic.Model.Route; public class ContramRoute : Contram, IImporterModel { public override string GetName() { return "Contram Network"; } public override string GetExtensions() { return "Contram Network|*.rte;"; } private Dictionary indexLookup; public ContramRoute() { indexLookup = new Dictionary(); commands.Add("INDEX", this.LoadIndex); commands.Add("ROUTES", this.LoadRoute); } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "1")] public void LoadFile(FileInfo file, OpenTraffic.Model.TrafficModel m) { Lookup.Get().Clear(); foreach (TrafficModelLink l in m.TrafficModelLinks) { Lookup.Get().Add(l.Name, l); } foreach (TrafficModelConnector l in m.TrafficModelConnectors) { Lookup.Get().Add(l.Name, l); } try { LoadFile(file, m, true); } catch (Exception) { m.Routes.Clear(); throw new Exception("Datafile not possible to read. Is it the correct route file for this network?"); } int failed = 0; for (int i = 0; i < m.Routes.Count; i++) { if (!m.Routes[i].IsValid()) { failed++; m.Routes.RemoveAt(i); i--; } } if (failed != 0) { Console.WriteLine("Route Failed: " + failed + "/" + m.Routes.Count); } } private void LoadIndex(string line, OpenTraffic.Model.TrafficModel m, Dictionary d, string commandName) { String[] cols = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); int index = Int32.Parse(cols[d["reference"]]); indexLookup.Add(index, Lookup.Get().GetObject(cols[d["link_identifier"]])); } private void LoadRoute(string line, OpenTraffic.Model.TrafficModel m, Dictionary d, string commandName) { String[] cols = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); int routeid = Int32.Parse(cols[d["route"]]); TrafficModelLink[] links = new TrafficModelLink[cols.Length - 2 - d["link_references"]]; TrafficModelConnector source = (TrafficModelConnector)indexLookup[Int32.Parse(cols[d["link_references"]])]; TrafficModelConnector target = (TrafficModelConnector)indexLookup[Int32.Parse(cols[cols.Length - 1])]; int index = 0; for (int i = d["link_references"] + 1; i < cols.Length - 1; i++) { links[index] = (TrafficModelLink)indexLookup[Int32.Parse(cols[i])]; index++; } RouteContainer route = new RouteContainer(routeid, source, target, links); m.Routes.Add(route); } } }