private void AddToQueue(BusStopEvents evnt)
 {
     try
     {
         _lstBusStopevents.Add(evnt);
         _lstBusStopevents = _lstBusStopevents.OrderBy(x => x.TimeofExecution).ToList();
     }
     catch (Exception ex)
     {
         ApplicationLog.Instance.WriteException(ex);
     }
 }
Ejemplo n.º 2
0
 private void timer2_Tick(object sender, EventArgs e, BusStopEvents _boardinEvent, Label _lblPassengerCount)
 {
     lblTimerValue.Text      = _timerCounter.ToString();
     _boardinEvent           = _boardinEvent.ExecuteEvent();
     _lblPassengerCount.Text = _boardinEvent.NumberofPersonInQueueAtStop.ToString();
     CheckMainCounterStopCondition();
     PlotPassengerQueue();
     if (_boardinEvent.NumberofPersonInQueueAtStop.Equals(0))
     {
         timer2.Stop();
         ApplicationLog.Instance.WriteInfo(string.Format("All passengers have boarded at stop# {0}.", _boardinEvent.BusStopNumber));
         timer1.Start();
     }
 }
Ejemplo n.º 3
0
        public virtual BusStopEvents ExecuteEvent()
        {
            BusStopEvents _busStopevent = null;

            try
            {
                _busStopevent = new BusStopEvents()
                {
                    BusStopNumber = BusStopNumber
                };
            }
            catch (Exception ex)
            {
                ApplicationLog.Instance.WriteException(ex);
            }
            return(_busStopevent);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a base event on which child properties will be created from child classes.
        /// </summary>
        /// <param name="BusStopNumber"></param>
        /// <param name="eventType"></param>
        /// <returns></returns>
        public virtual BusStopEvents CreateEvent(int BusStopNumber)
        {
            BusStopEvents evnt = null;

            try
            {
                evnt = new BusStopEvents()
                {
                    BusStopNumber = BusStopNumber
                };
            }
            catch (Exception ex)
            {
                ApplicationLog.Instance.WriteException(ex);
            }
            return(evnt);
        }
        /// <summary>
        /// Simulation will begin here.
        /// </summary>
        public void SimulateEvents()
        {
            int _simulationTime = 7200;

            ApplicationLog.Instance.WriteInfo(string.Format("Simulation started for {0} seconds.", _simulationTime));
            if (_lstBusStopevents != null && _lstBusStopevents.Count > 0)
            {
                int    _clockTime            = 1;
                double _boardinTimeExecution = 0;
                do
                {
                    BusStopEvents _event     = _lstBusStopevents[0];
                    BusStopEvents _nextEvent = null;
                    if (_event != null)
                    {
                        int _currentPassangersAtStop = 0;
                        if (_dicNumberOfPersonsByStopNumber.Keys.Contains(_event.BusStopNumber))
                        {
                            _dicNumberOfPersonsByStopNumber.TryGetValue(_event.BusStopNumber, out _currentPassangersAtStop);
                        }
                        int _eventBusStopNumber = _event.BusStopNumber;
                        _event.NumberofPersonInQueueAtStop = _currentPassangersAtStop;
                        _lstBusStopevents.Remove(_event);
                        switch (_event.TypeofEvent)
                        {
                            #region EventType.PersonArrival
                        case EventType.PersonArrival:
                            //Execute the current event
                            _event = _event.ExecuteEvent();
                            _dicNumberOfPersonsByStopNumber[_eventBusStopNumber] = _event.NumberofPersonInQueueAtStop;
                            //Create Person Arrival event for next person.
                            _nextEvent = new PersonArrivalEvent()
                            {
                                NumberofPersonInQueueAtStop = _currentPassangersAtStop,
                                ClockTime = _clockTime
                            };
                            _nextEvent = _nextEvent.CreateEvent(_eventBusStopNumber) as PersonArrivalEvent;
                            break;
                            #endregion

                            #region EventType.BusArrial
                        case EventType.BusArrial:
                            //TODO:: While creating a new Bus event, read the lstEventsToExecute.
                            IEnumerable <BusArrivalEvent> _lstSameTimeBusses = _lstBusStopevents.Where(x => x.TypeofEvent.Equals(EventType.BusArrial) && x.TimeofExecution.Equals(_event.TimeofExecution) && x.BusStopNumber.Equals(_event.BusStopNumber)) as IEnumerable <BusArrivalEvent>;
                            if (_lstSameTimeBusses != null && _lstSameTimeBusses.Any())
                            {
                                IEnumerable <int> _lstWaitingBusNumbers = _lstSameTimeBusses.Select(x => x.BusNumber);
                                string            sBusNumbers           = string.Empty;
                                if (_lstWaitingBusNumbers != null && _lstWaitingBusNumbers.Any())
                                {
                                    foreach (int nbusnum in _lstWaitingBusNumbers)
                                    {
                                        sBusNumbers = string.Format("{0},{1}", sBusNumbers, nbusnum);
                                    }
                                }
                                if (!string.IsNullOrEmpty(sBusNumbers))
                                {
                                    ApplicationLog.Instance.WriteInfo(string.Format("Busesses {0} are waiting at {1} becuase another bus is already boarding passengers.", sBusNumbers, _event.BusStopNumber));
                                }
                            }

                            _event     = _event.ExecuteEvent();
                            _nextEvent = new BusArrivalEvent();

                            //Bus arrival for next bus stop.
                            _nextEvent = _event.CreateEvent(_eventBusStopNumber + 1);
                            _nextEvent.NumberofPersonInQueueAtStop = _currentPassangersAtStop;
                            _nextEvent.ClockTime  = _clockTime;
                            _boardinTimeExecution = _nextEvent.TimeofExecution;
                            break;
                            #endregion

                            #region EventType.PersonBoarding
                        case EventType.PersonBoarding:
                            _event = _event.ExecuteEvent();

                            //Make passengers at bus stop 0 since they are all boarded.
                            _dicNumberOfPersonsByStopNumber[_eventBusStopNumber] = 0;
                            _event.TimeofExecution = _boardinTimeExecution;
                            _boardinTimeExecution  = 0;
                            //Boarding event for next bus stop
                            _nextEvent = _event.CreateEvent(_eventBusStopNumber + 1);

                            break;
                            #endregion
                        }
                        AddToQueue(_nextEvent);
                    }
                    //Console.WriteLine(_simulationTime);
                    _simulationTime -= 1;
                    _clockTime      += 1;
                }while (_simulationTime > 0);

                ApplicationLog.Instance.WriteInfo(string.Format("Simulation ended after {0} seconds.", _simulationTime));
            }
            else
            {
                ApplicationLog.Instance.WriteError("Stopping simulation due to in appropriate initialization");
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Simulation will begin here.
        /// </summary>
        public void SimulateEvents()
        {
            if (_lstBusStopevents != null && _lstBusStopevents.Count > 0)
            {
                BusStopEvents _event     = _lstBusStopevents[0];
                BusStopEvents _nextEvent = null;
                if (_event != null)
                {
                    int _eventBusStopNumber = _event.BusStopNumber;
                    _txtStop           = Controls.Find(string.Format("txtStop{0}", _event.BusStopNumber), true).FirstOrDefault() as TextBox;
                    _lblPassengerCount = Controls.Find(string.Format("lblPassengerCount{0}", _event.BusStopNumber), true).FirstOrDefault() as Label;

                    int _currentPassangersAtStop = 0;
                    int.TryParse(_lblPassengerCount.Text, out _currentPassangersAtStop);

                    _txtStop.Text = _event.TypeofEvent.ToString();

                    _event.NumberofPersonInQueueAtStop = _currentPassangersAtStop;
                    _lstBusStopevents.Remove(_event);

                    switch (_event.TypeofEvent)
                    {
                        #region EventType.PersonArrival
                    case EventType.PersonArrival:
                        //Execute the current event
                        _event = _event.ExecuteEvent();
                        _lblPassengerCount.Text = _event.NumberofPersonInQueueAtStop.ToString();
                        //Create Person Arrival event for next person.
                        _nextEvent = new PersonArrivalEvent()
                        {
                            NumberofPersonInQueueAtStop = _currentPassangersAtStop,
                            ClockTime = _timerCounter
                        };
                        _nextEvent = _nextEvent.CreateEvent(_eventBusStopNumber) as PersonArrivalEvent;
                        _dicNumberOfPersonsByStopNumber[_eventBusStopNumber] = _event.NumberofPersonInQueueAtStop;
                        break;
                        #endregion

                        #region EventType.BusArrial
                    case EventType.BusArrial:
                        //TODO:: While creating a new Bus event, read the lstEventsToExecute.
                        IEnumerable <BusArrivalEvent> _lstSameTimeBusses = _lstBusStopevents.Where(x => x.TypeofEvent.Equals(EventType.BusArrial) && x.TimeofExecution.Equals(_event.TimeofExecution) && x.BusStopNumber.Equals(_event.BusStopNumber)) as IEnumerable <BusArrivalEvent>;
                        if (_lstSameTimeBusses != null && _lstSameTimeBusses.Any())
                        {
                            IEnumerable <int> _lstWaitingBusNumbers = _lstSameTimeBusses.Select(x => x.BusNumber);
                            string            sBusNumbers           = string.Empty;
                            if (_lstWaitingBusNumbers != null && _lstWaitingBusNumbers.Any())
                            {
                                foreach (int nbusnum in _lstWaitingBusNumbers)
                                {
                                    sBusNumbers = string.Format("{0},{1}", sBusNumbers, nbusnum);
                                }
                            }
                            if (!string.IsNullOrEmpty(sBusNumbers))
                            {
                                ApplicationLog.Instance.WriteInfo(string.Format("Busesses {0} are waiting at {1} becuase another bus is already boarding passengers.", sBusNumbers, _event.BusStopNumber));
                            }
                        }

                        _event     = _event.ExecuteEvent();
                        _nextEvent = new BusArrivalEvent();

                        //Bus arrival for next bus stop.
                        _nextEvent = _event.CreateEvent(_eventBusStopNumber + 1);
                        _nextEvent.NumberofPersonInQueueAtStop = _currentPassangersAtStop;
                        _nextEvent.ClockTime  = _timerCounter;
                        _boardinTimeExecution = _nextEvent.TimeofExecution;
                        BusArrivalEvent _event1 = _event as BusArrivalEvent;
                        chartBusArrival.Series[string.Format("BusNumber{0}", _event1.BusNumber)].Points.AddXY(_timerCounter, _event.BusStopNumber);

                        break;
                        #endregion

                        #region EventType.PersonBoarding
                    case EventType.PersonBoarding:
                        timer1.Stop();

                        //int _nStoredPassngers = 0;
                        //_dicNumberOfPersonsByStopNumber.TryGetValue(_eventBusStopNumber, out _nStoredPassngers);
                        //if (_nStoredPassngers < _event.NumberofPersonInQueueAtStop)
                        //    _dicNumberOfPersonsByStopNumber[_eventBusStopNumber] = _event.NumberofPersonInQueueAtStop;

                        ApplicationLog.Instance.WriteInfo(string.Format("There are {0} passengers at stop# {1} and hence bus will be at stop for {2}.", _event.NumberofPersonInQueueAtStop, _event.BusStopNumber, _event.BoardingTime * _event.NumberofPersonInQueueAtStop));
                        timer2.Tick    += (sender, e) => timer2_Tick(sender, e, _event, _lblPassengerCount);
                        timer2.Interval = _busTerminalConfiguration.TimerInterval;
                        timer2.Start();
                        //Make passengers at bus stop 0 since they are all boarded.
                        _event.TimeofExecution = _boardinTimeExecution;
                        _boardinTimeExecution  = 0;
                        //Boarding event for next bus stop
                        _nextEvent = _event.CreateEvent(_eventBusStopNumber + 1);

                        break;
                        #endregion
                    }
                    AddToQueue(_nextEvent);
                }

                CheckMainCounterStopCondition();
            }
            else
            {
                ApplicationLog.Instance.WriteError("Stopping simulation due to in appropriate initialization");
            }
        }