public void Run() { vissim = new Vissim(); //Load Vissim net work vissim.LoadNet(VissimSimulatorFilePath, false); //initialize the cellular network cellularNetwork.LoadFromFile(CellLinkRelationFilePath, Delimiter); CollectorWorker worker = new CollectorWorker(VissimEventsFilePath, cellularTowerEvents); //collector task: collecting the data from cellular events Task collectorTask = Task.Factory.StartNew(() => worker.Run(), token); //simulation thread: including vissim simulation, events generation and detection Task simulator = Task.Factory.StartNew(() => Execute(), token); try { Task.WaitAll(simulator, collectorTask); } catch (Exception ex) { Console.WriteLine(string.Format("there are some exceptions happened: {0}", ex.Message)); throw ex; } }
public void Run() { Vissim vissim = new Vissim(); ///Load Vissim net work VissimSimulator.LoadNet(@"C:\Users\Public\Documents\PTV Vision\PTV Vissim 6\Examples Demo\Urban Intersection Beijing.CN\Intersection Beijing.inpx"); ///Read table contains Cellular tower information and correspoing link information var cellTowerInformation = new StreamReader(File.OpenRead(@"C:\test.csv")); CellularTowerEvent cte = new CellularTowerEvent(); while (!cellTowerInformation.EndOfStream) { var line = cellTowerInformation.ReadLine(); var values = line.Split(';'); cte.CellularTowerId = CellTower.AddLink(Int32.Parse(values[0])); cte.LocationId = Location.AddCellTower(Int32.Parse(values[1])); } ///Generate the random event, when vehicle passing a fixed location and the Timespan is satisfied. foreach (IVehicle vehicle in vissim.Net.Vehicles) { ///Select Random Vehicle int vehiclePossible = rnd.Next(0, 10); //Only Selected Vehicle can generate the Event. if (vehiclePossible == 0) { Event evet = new Event(); ///Create random event type. int i = rnd.Next(0, 1); evet.TimeSpan = EventFactory.CreateTimeSpan(); evet.EventType = EventFactory.CreateEventType; cte.Event = evet; } ///record the event information CollectorWorker collect = new CollectorWorker(); if (vehicle.Location == cte.CellTowerId & cte.Event.TimeSpan == simulationTime) { collect.ProcessEvent(cte); } ///Make the program check the all vehicle in Vissim network every 1 sec. for (int i = 0; i < simulationTime; i++) { vissim.Simulation.RunSingleStep(); foreach (CollectorWorker worker in CollectorWorkers) { Task workerTask = Task.Run(() => { worker.ProcessEvent(CellularTowerEvents); }); workerTask.Wait(); } } }
/// <summary> /// This method attempts to create the OUTPUT QRACLE table. /// If will do nothing but print an error if the table already exists. /// </summary> //public void TryCreateTbale() //{ // using (SqlConnection con = new SqlConnection()) // { // con.ConnectionString = ConfigurationManager.AppSettings["SqlConnectionString"]; // con.Open(); // try // { // using (SqlCommand command = new SqlCommand( // "CREATE TBALE OUTPUT1(LocationId INT, CellularTowerId INT, EventType TEXT, EventTimeSpan TEXT)", con)) // { // command.ExecuteNonQuery(); // } // } // catch // { // Console.WriteLine("Table already exists, or something wrong with the connection"); // } // } //} public void Run() { vissim = new Vissim(); ///Load Vissim net work vissim.LoadNet(VissimSimulatorFilePath, false); //initialize the cellular network cellularNetwork.LoadFromFile(CellLinkRelationFilePath, Delimiter); ///initialize the table //TryCreateTbale(); //set up the collector threads. For now, only need one thread on this //for now, we only need 1 worker to collect the event using (StreamWriter writer = new StreamWriter(VissimEventsFilePath)) { CollectorWorker worker = new CollectorWorker(writer); Task collectorTask = Task.Factory.StartNew(() => { foreach (CellularTowerEvent cEvent in cellularTowerEvents.GetConsumingEnumerable()) { worker.Process(cEvent); } }); //simulation thread: including vissim simulation, events generation and detection Task simulator = Task.Factory.StartNew(() => { for (int currentTick = 0; currentTick < SimulationTicks; currentTick++) { foreach (IVehicle vehicle in vissim.Net.Vehicles) { //get the vehicle id+ int vehicleId = (int)vehicle.AttValue["No"]; //get the current vehicle link ILane lane = vehicle.Lane; string linkId = lane.AttValue["Link"]; //Console.WriteLine(string.Format("vehicle {0} at link {1}", vehicleId, linkId)); //first check if this vehicle has event if (vehicleEvents.ContainsKey(vehicleId.ToString())) { CellularTowerEvent cEvent = DetectEvent(vehicleId.ToString(), linkId, currentTick); if (cEvent != null) { cellularTowerEvents.Add(cEvent); } } else //if no vehicle event, that means this is new vehicle entering the vissim network { GenerateEvent(vehicleId.ToString(), currentTick); } } //make the Vissim simulation move forward one tick vissim.Simulation.RunSingleStep(); } }); try { Task.WaitAll(simulator, collectorTask); } catch (Exception ex) { Console.WriteLine(string.Format("there are some exceptions happened: {0}", ex.Message)); throw ex; } } }