/* * 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.Result { using System; using System.Collections.Generic; using System.Xml.Serialization; using OpenTraffic.Model.Graph; using OpenTraffic.Model.Route; using OpenTraffic.Model.Simulator; [Serializable] public class ResultFrame { #region XmlExport [XmlAttribute(AttributeName = "StartTime")] public float StartTime { get; set; } [XmlAttribute(AttributeName = "StopTime")] public float StopTime { get; set; } [XmlAttribute(AttributeName = "Bin")] public int Bin { get; set; } #endregion [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] [XmlArray("ResultRoutes")] [XmlArrayItem(ElementName = "rr")] public ResultRoute[] XmlExportRoutes { get { int nRoutes = 0; foreach (KeyValuePair> k in this.routes) { nRoutes += k.Value.Count; } int i = 0; ResultRoute[] list = new ResultRoute[nRoutes]; foreach (KeyValuePair> k in this.routes) { foreach (KeyValuePair r in k.Value) { list[i++] = r.Value; } } return list; } set { foreach (ResultRoute r in value) { Update(r); } } } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] [XmlArray("ResultLinks")] [XmlArrayItem(ElementName = "rl")] public ResultLink[] XmlExportLinks { get { ResultLink[] array = new ResultLink[this.links.Values.Count]; int i = 0; foreach (ResultLink l in links.Values) { array[i++] = l; } return array; } set { foreach (ResultLink r in value) { links.Add(r.LinkId, r); } } } [XmlIgnore] public Dictionary links { get; private set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] [XmlIgnore] public Dictionary> routes { get; private set; } public ResultFrame() { links = new Dictionary(); routes = new Dictionary>(); } public ResultFrame(int bin, float startTime, float stopTime) { StartTime = startTime; StopTime = stopTime; Bin = bin; routes = new Dictionary>(); links = new Dictionary(); } public void AddFrame(ResultFrame f) { if (f != null) { if (f.StartTime != StartTime || f.StopTime != StopTime) { throw new NotImplementedException("Sorry cannot not add results to a results with different bins"); } foreach (KeyValuePair link in f.links) { links[link.Key] = new ResultLink(link.Value); } foreach (KeyValuePair> kv in f.routes) { foreach (KeyValuePair rr in kv.Value) { Update(rr.Value); } } } } public bool Within(float time) { return ((time >= StartTime) && (time < StopTime)); } public ResultLink GetResultLink(int id) { ResultLink rl; if (!links.TryGetValue(id, out rl)) { rl = new ResultLink(id); links.Add(id, rl); } return rl; } public ResultLink GetResultLink(IGraphEdge l) { if (l != null) { return GetResultLink(l.Id); } return null; } public void RegisterStartQueing(Vehicle v, float time) { if (v != null) { ResultLink l = GetResultLink(v.GetLink()); l.RegisterStartQueing(v, time); } } public void RegisterArrivalInBin(Vehicle v, float time) { if (v != null) { ResultLink l = GetResultLink(v.GetLink()); l.RegisterArrival(v, time); } } public void RegisterDepartureOutBin(Vehicle v) { if (v != null) { ResultLink l = GetResultLink(v.GetLink()); l.RegisterDepartureOutBin(v); } } public void RegisterDepartureInBin(Vehicle v, float time) { if (v != null) { ResultLink l = GetResultLink(v.GetLink()); l.RegisterDepartureInBin(v, time); } } public void RegisterNetworkStatus(List links) { if (links != null) { foreach (ILinkSimulator l in links) { GetResultLink(l.NetworkLink).RegisterLinkStatus(l); } } } public void Merge(ResultFrame f, float factor, List ls) { if (f != null && ls != null) { foreach (TrafficModelLink l in ls) { ResultLink frl = f.links.ContainsKey(l.Id) ? f.links[l.Id] : null; ResultLink rl = links.ContainsKey(l.Id) ? links[l.Id] : null; if (rl != null) { rl.Merge(frl, factor, l); } else { links.Add(l.Id, new ResultLink(frl)); } } } } public int NumberOfCarsOnLink(IGraphEdge l) { return GetResultLink(l).NumberOfCarsOnLink; } public void SetTravelTime(IGraphEdge l, float travel) { GetResultLink(l).TravelTime = travel; } public void SetFlowIn(IGraphEdge l, float flow) { GetResultLink(l).FlowIn = flow; } public void SetFlowOut(IGraphEdge l, float flow) { GetResultLink(l).FlowOut = flow; } public void SetQueuingTime(IGraphEdge l, float time) { GetResultLink(l).QueuingTime = time; } public void SetNumberOfVehicleInQueue(IGraphEdge l, float cars) { GetResultLink(l).NumberOfCarsQueue = cars; } public float GetTravelTime(IGraphEdge l) { return GetResultLink(l).TravelTime; } public float GetFlowOut(IGraphEdge l) { return GetResultLink(l).FlowOut; } public float GetFlowIn(IGraphEdge l) { return GetResultLink(l).FlowIn; } public float GetQueuingTime(IGraphEdge l) { return GetResultLink(l).QueuingTime; } public float GetNumberofVehicleInQueue(IGraphEdge l) { return GetResultLink(l).NumberOfCarsQueue; } public string ToStringStart() { TimeSpan ts = new TimeSpan((long)StartTime * 1000 * 10000); return ts.Hours + ":" + ts.Minutes; } public override string ToString() { string str = System.Environment.NewLine + "FRAME " + new TimeSpan((long)StartTime * 1000 * 10000) + System.Environment.NewLine; return str; } public ResultRoute Update(ResultRoute r) { if (r != null) { if (!routes.ContainsKey(r.cl)) routes.Add(r.cl, new Dictionary()); if (!routes[r.cl].ContainsKey(r.routeId)) routes[r.cl].Add(r.routeId, r); else routes[r.cl][r.routeId] = r; return r; } return null; } public ResultRoute GetRoute(VehicleClass c, TrafficRoute r) { if (c != null && r != null) { if (routes.ContainsKey(c.IdRegister) && routes[c.IdRegister].ContainsKey(r.Id)) { return routes[c.IdRegister][r.Id]; } return Update(new ResultRoute(0, c, r, 0)); } return null; } } }