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; } }
/// <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; } } }