/* * 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.Mezzo { using System; using System.Collections; using System.Collections.Generic; class DemandCommand : Command { private Dictionary> demands; private int time; private int nTime; private List times; private Dictionary classes; private TrafficGenerator.ODTrafficGenerator trafficGenerator; // private TrafficModel model; private float calcFlow = 0; public DemandCommand( Dictionary classes, TrafficGenerator.ODTrafficGenerator trafficGen) { this.classes = classes; this.trafficGenerator = trafficGen; // this.model = model; demands = new Dictionary>(); times = new List(); } private class DemandKey { public int StartZoneId; public int StopZoneId; public DemandKey(int start, int stop) { this.StartZoneId = start; this.StopZoneId = stop; } // Not special good one but anyaway public override int GetHashCode() { return StartZoneId.GetHashCode() + StopZoneId.GetHashCode(); } public override bool Equals(object obj) { if (obj != null && obj.GetType() == this.GetType()) { DemandKey dk = (DemandKey)obj; return StartZoneId == dk.StartZoneId && StopZoneId == dk.StopZoneId; } return false; } } private void OverrideValue(int start, int stop, float rate) { DemandKey dk = new DemandKey(start, stop); if (!demands.ContainsKey(dk)) { demands.Add(dk, new List()); demands[dk].Add(0); } if (demands[dk].Count > nTime) { demands[dk][nTime] = rate; } else { for (int i = demands[dk].Count; i < nTime; i++) { demands[dk].Add(demands[dk][0]); } demands[dk].Add(rate); } } public void BeforeList(Dictionary prop) { System.Diagnostics.Debug.Assert(prop["scale"] == "1"); if (!prop.ContainsKey("loadtime")) { times.Add(0); nTime = 0; } else { time = int.Parse(prop["loadtime"]); times.Add(time); nTime++; } // System.Console.WriteLine(time+"TotFlow: "+ this.calcFlow); calcFlow = 0; } public void LoadItem(ArrayList values) { int start = MezzoImporter.GetInt(0, values); int stop = MezzoImporter.GetInt(1, values); float rate = MezzoImporter.GetFloat(2, values) / 60 / 60; if (start == 604) { calcFlow += MezzoImporter.GetFloat(2, values); // System.Console.WriteLine("#" + nTime + " " + start + " -> " + stop + " at rate " + Mezzo.GetFloat(2, values)); } OverrideValue(start, stop, rate); } public void Close() { foreach (KeyValuePair> kv in demands) { for (int i = kv.Value.Count - 1; i < nTime; i++) { demands[kv.Key].Add(demands[kv.Key][0]); } } foreach (KeyValuePair> kv in demands) { TrafficZone z1 = (TrafficZone)Lookup.Get().GetObject(kv.Key.StartZoneId); TrafficZone z2 = (TrafficZone)Lookup.Get().GetObject(kv.Key.StopZoneId); foreach (KeyValuePair c in classes) { float[] rates = new float[kv.Value.Count]; for (int i = 0; i < rates.Length; i++) { rates[i] = kv.Value[0] * c.Value; } this.trafficGenerator.Demands.Add(new Demand(z1, z2, c.Key, rates)); } } for (int i = 1; i < times.Count; i++) { trafficGenerator.Times.Add(new TimeSlice(times[i - 1], times[i])); } float f = times[times.Count - 1]; trafficGenerator.Times.Add(new TimeSlice(f, f + 60 * 60)); float t = 0.0f; foreach (Demand d in trafficGenerator.Demands) { if (d.Origin.Id == 604) { t += d.Demands[0] * 60 * 60; } } // Console.WriteLine("timeDepend:" + t); } } }