private void AddToQueue(BusStopEvents evnt)
 {
     try
     {
         _lstBusStopevents.Add(evnt);
         _lstBusStopevents = _lstBusStopevents.OrderBy(x => x.TimeofExecution).ToList();
     }
     catch (Exception ex)
     {
         ApplicationLog.Instance.WriteException(ex);
     }
 }
 public override BusStopEvents ExecuteEvent(BusStopEvents evnt)
 {
     try
     {
         //increase number of NumberofPersonInQueueAtStop randomly between 1 - 5 and and same has to be updated in dicNumberOfPersonsByStopNumber upon returning
         evnt.NumberofPersonInQueueAtStop = NumberofPersonInQueueAtStop + NextTimeIntervalGenerator.GetPersons(); //TODO::Randomize this
         ApplicationLog.Instance.WriteInfo(string.Format("New person(s) have arrived at stop#: {0} and total number of passnegers are {1}.", evnt.BusStopNumber, evnt.NumberofPersonInQueueAtStop));
     }
     catch (Exception ex)
     {
         ApplicationLog.Instance.WriteException(ex);
     }
     return(evnt);
 }
 public virtual BusStopEvents ExecuteEvent(BusStopEvents evnt)
 {
     try
     {
         evnt = new BusStopEvents()
         {
             BusStopNumber = BusStopNumber
         };
     }
     catch (Exception ex)
     {
         ApplicationLog.Instance.WriteException(ex);
     }
     return(evnt);
 }
示例#4
0
        public override BusStopEvents ExecuteEvent(BusStopEvents evnt)
        {
            //Todo:: check if any other bus is coming to bus stop and mark it as waiting. How to handle with 0 passanger and no overtaking?
            BusArrivalEvent _event = null;

            try
            {
                _event = evnt as BusArrivalEvent;
                ApplicationLog.Instance.WriteInfo(string.Format("Bus# {0} arrived at stop# {1}.", _event.BusNumber, _event.BusStopNumber));
            }
            catch (Exception ex)
            {
                ApplicationLog.Instance.WriteException(ex);
            }
            return(_event);
        }
        /// <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);
        }
        public override BusStopEvents ExecuteEvent(BusStopEvents evnt)
        {
            BoardingEvent _event = null;

            try
            {
                _event = evnt as BoardingEvent;
                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, BoardingTime * _event.NumberofPersonInQueueAtStop));
                do
                {
                    ApplicationLog.Instance.WriteInfo(string.Format("Boarding passenger in Bus# {0} at stop# {1}.", BusNumber, _event.BusStopNumber));
                    _event.NumberofPersonInQueueAtStop -= 1;
                }while (_event.NumberofPersonInQueueAtStop > 0);
                ApplicationLog.Instance.WriteInfo(string.Format("All passengers have boarded Bus# {0} at stop# {1}.", BusNumber, _event.BusStopNumber));
            }
            catch (Exception ex)
            {
                ApplicationLog.Instance.WriteException(ex);
            }
            return(_event);
        }
        public void SimulateEvents()
        {
            NextTimeIntervalGenerator.SinglePersonIncrement = Configuration.SinglePersonIncrement;
            int _simulationTime = Configuration.MaxSimulationTime;

            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(_event);
                            _dicNumberOfPersonsByStopNumber[_eventBusStopNumber] = _event.NumberofPersonInQueueAtStop;
                            //Create Person Arrival event for next person.
                            _nextEvent = new PersonArrivalEvent()
                            {
                                NumberofPersonInQueueAtStop = _currentPassangersAtStop,
                                ClockTime          = _clockTime,
                                AverageArrivalTime = Configuration.MeanInterArrivalRate
                            };
                            _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(_event);
                            _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(_event);

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

                            //if (_event.NumberofPersonInQueueAtStop != 0)
                            //{
                            //	_event = _event.ExecuteEvent(_event);

                            //	//Make passengers at bus stop 0 since they are all boarded.
                            //	_dicNumberOfPersonsByStopNumber[_eventBusStopNumber] = _event.NumberofPersonInQueueAtStop;
                            //	_event.TimeofExecution = _clockTime + _event.BoardingTime;
                            //	//Boarding event for next bus stop
                            //	_nextEvent = _event.CreateEvent(_eventBusStopNumber);
                            //}
                            //else
                            //{
                            //	//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
                        }
                        if (_nextEvent != null)
                        {
                            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");
            }
        }