Exemplo n.º 1
0
        public CleaningPlanResultsDto ExecuteCleaningPlan(CleaningPlanInstructionsDto cleanPlanInstructions)
        {
            _navigator = new Navigator(cleanPlanInstructions.Map);

            var instructions        = cleanPlanInstructions.Instructions;
            var startingCoordinates = new Coordinate(CurrentPosition.X, CurrentPosition.Y);

            _navigator.SetVisited(startingCoordinates);

            while (Battery.HasCapacity && instructions.Count > 0)
            {
                var instruction = instructions.Dequeue();

                try
                {
                    ExecuteInstruction(instruction);
                }
                catch (Exception ex)
                {
                    if (ex is CollisionException || ex is OutOfBatteryException)
                    {
                        try
                        {
                            ExecuteBackOffStrategy();
                        }
                        catch (Exception e)
                        {
                            if (e is BackOffStrategyFailedException || e is OutOfBatteryException)
                            {
                                break;
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            var navigatorResults = _navigator.ExportResults();

            var result = new CleaningPlanResultsDto
            {
                Visited = navigatorResults.Visited,
                Cleaned = navigatorResults.Cleaned,
                Final   = CurrentPosition,
                Battery = Battery.Level
            };

            return(result);
        }
Exemplo n.º 2
0
        public CleaningPlanResultsDto ExecuteCleaningProcess(RobotDto robotConfiguration)
        {
            var position = new RobotPosition(robotConfiguration.Start.X, robotConfiguration.Start.Y, robotConfiguration.Start.Facing);
            var robot    = new Robot(position, robotConfiguration.Battery);

            var instructions = new CleaningPlanInstructionsDto
            {
                Map          = robotConfiguration.Map,
                Instructions = new Queue <string>(robotConfiguration.Commands)
            };

            return(robot.ExecuteCleaningPlan(instructions));
        }