Пример #1
0
 /// <summary>
 /// The RoverGraph constructor stores the injected IDirectionsInfoContainer as well as
 /// the ParsedCoordinatesData of the IParser.
 /// </summary>
 /// <param name="fileParser">The IParsed containing the grid and rover data.</param>
 /// <param name="directionsInfoContainer">The injected IDirectionsInfoContainer object that defines the directions in which the IGraphNodes are connected.</param>
 public RoverGraph(IParser fileParser, IDirectionsInfoContainer directionsInfoContainer)
 {
     _directionsInfoContainer = directionsInfoContainer;
     // Set the upper right boundary
     _upperRightCoordinateBoundary = fileParser.ParsedCoordinatesData;
     _graphData = PopulateGraphNodes();
     ConnectAllNodes();
 }
Пример #2
0
        /// <summary>
        /// Reads the input text file sequentially and passes the rover information to a Parse function
        /// which parses the data into meaningful representative objects. Does all error checking
        /// regarding the input file.
        /// </summary>
        /// <param name="strFilePath">The windows path (relative or absolute) to the input text file.</param>
        /// <returns>An IList of IParsedRoverData objects representing rover information.</returns>
        private IList <IParsedRoverData> DoReadFile(string strFilePath)
        {
            IList <IParsedRoverData> parsedRoverDataObjects = new List <IParsedRoverData>();
            StreamReader             streamReader;

            try
            {
                using (streamReader = File.OpenText(strFilePath))
                {
                    // The first line is the top-right coordinates
                    string currentLine = streamReader.ReadLine();
                    if (currentLine == null)
                    {
                        throw new Exception("Input file is empty");
                    }
                    Regex properCoordinatesInputPattern = new Regex("^[0-9]+ [0-9]+$");
                    if (!properCoordinatesInputPattern.IsMatch(currentLine))
                    {
                        throw new Exception("Mal-formed or missing top-right coordinates data encountered in input file");
                    }
                    string[] result = currentLine.Trim().Split(' ');
                    int      xCoord = Convert.ToInt32(result[0]);
                    int      yCoord = Convert.ToInt32(result[1]);
                    _parsedCoordinatesData = new ParsedCoordinatesData(xCoord, yCoord);

                    // Now handle all other lines per usual for rover data
                    string roverInitialLocation;
                    string roverControlCommands;
                    bool   isRoverDataPresent = false;
                    while ((roverInitialLocation = streamReader.ReadLine()) != null)
                    {
                        isRoverDataPresent = true;
                        if ((roverControlCommands = streamReader.ReadLine()) == null)
                        {
                            throw new Exception("Missing rover data encountered in input file");
                        }
                        parsedRoverDataObjects.Add(Parse(roverInitialLocation.ToUpper(), roverControlCommands.ToUpper()));
                    }

                    if (!isRoverDataPresent)
                    {
                        throw new Exception("No rover data encountered in input file");
                    }
                }
            }
            catch (FileNotFoundException ex)
            {
                throw new Exception("The input file cannot be found.", ex);
            }
            catch (Exception ex)
            {
                throw new Exception("The input file cannot be read (" + ex.Message + ")", ex);
            }

            return(parsedRoverDataObjects);
        }