/* * 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.Result; public class ContramResult : Contram, IImporter { public override string GetName() { return "Contram Result"; } public override string GetExtensions() { return "Contram Result|*.res;"; } private ResultData result; public ContramResult() { commands.Add("NETWORK", this.LoadNetwork); commands.Add("SUMMARY_NETWORK", this.LoadSummaryNetwork); commands.Add("TURNING", this.LoadTurning); commands.Add("TRAFFIC", this.LoadTraffic); } public ResultData LoadResult( FileInfo file, OpenTraffic.Model.TrafficModel model) { if (model == null || model.TimeSlices == null || model.TimeSlices.Count == 0) { throw new Exception("No original timeslice." + " This network do not seem to come from a Contram network"); } Result.TimeSliceFactory rf = new Result.TimeSliceFactory(); rf.Times = model.TimeSlices; result = rf.CreateResult(); result.Name = "Contram"; // result.HasFlowOut = false; result.HasNumberOfCars = false; result.HasNumberOfVehicleInQueue = true; result.HasQueuingTime = true; result.HasEstimatedFlow = false; Lookup.Get().Clear(); foreach (TrafficModelLink l in model.TrafficModelLinks) { Lookup.Get().Add(l.Name, l); } foreach (TrafficModelConnector l in model.TrafficModelConnectors) { Lookup.Get().Add(l.Name, l); } try { LoadFile(file, model, true); return result; } catch (Exception exp) { throw new Exception("Datafile not possible to read. Is it the correct result file for this network?",exp); } } // // * U/D/link t/s arrival capacity pcu rho Lfin Lmin // Lmax congest travel queuing distance time queuing speed stop private void LoadNetwork(string line, TrafficModel m, Dictionary d, string commandName) { String[] values = SplitLine(line); string linkId = GetString("U/D/link", d, values); TrafficModelLink link = Lookup.Get().GetObject(linkId); int bin = GetInt("t/s", d, values); Result.ResultLink rl = result.GetLinkResultFromBin(bin - 1, link); rl.FlowIn = GetFloat("arrival", d, values) / (60 * 60); rl.NumberOfCarsQueue = GetFloat("Lfin", d, values); float travelTime = 60 * GetFloat("travel", d, values); float queueTime = 60 * GetFloat(d["travel"] + 1, values); rl.TravelTime = travelTime - queueTime; rl.QueuingTime = queueTime; } private float NumberOfCars; private void LoadSummaryNetwork(string line, TrafficModel m, Dictionary d, string commandName) { String[] values = SplitLine(line); float f = GetFloat("arrivals", d, values); NumberOfCars += f; } private void LoadTurning(string line, TrafficModel m, Dictionary d, string commandName) { // * U/N/link N/D/link t/s volume(veh/hr) String[] values = SplitLine(line); int bin = GetInt("t/s", d, values); TrafficModelLink linkIn = Lookup.Get().GetObject(GetString("N/D/link", d, values)); TrafficModelLink linkOut = Lookup.Get().GetObject(GetString("U/N/link", d, values)); float volume = GetFloat("volume(veh/hr)", d, values) / (60 * 60); Result.ResultLink rl2 = result.GetLinkResultFromBin(bin - 1, linkIn); if (rl2.EstimatedFlowOut != -1) rl2.FlowIn = 0.0f; rl2.EstimatedFlowOut = - 1; rl2.FlowIn += volume; Result.ResultLink rl = result.GetLinkResultFromBin(bin - 1, linkOut); rl.FlowOut += volume; } private void LoadTraffic(string line, TrafficModel m, Dictionary d, string commandName) { //* origin destination class route t/s p/s volume/hr split distance time perceived toll //* - - - - - - - - (km) (hr) cost(units) (units) String[] values = SplitLine(line); // int org = GetInt("origin", d, values); // int dst = GetInt("destination", d, values); int cla = GetInt("class", d, values); int routeId = GetInt("route", d, values); int bin = GetInt("t/s", d, values) - 1; float split = -GetFloat("split", d, values); float flow = GetFloat("volume/hr", d, values); Result.ResultFrame frame = result.Frames[bin]; float vehicles = flow / 3600 * (frame.StopTime - frame.StartTime); Result.ResultRoute rr = new Result.ResultRoute(split, cla, routeId, 0.0f); rr.vehicles = vehicles; frame.Update(rr); } } }