public async Task <DataContentModel> ReadDataToModelAsync(string filePath)
        {
            if (string.IsNullOrWhiteSpace(filePath))
            {
                Logger.LogError("Can not read file with empty path");
                throw new ArgumentException("Unspecified file name", nameof(filePath));
            }

            DataContentModel model = new DataContentModel
            {
                FilePath = filePath
            };

            try
            {
                model.ContentStrings = await File.ReadAllLinesAsync(filePath);

                Logger.LogInformation("Read file {filePath}", filePath);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Error reading file {filePath}", filePath);
                throw;
            }
            return(model);
        }
Exemple #2
0
        public async Task FileDataProvider_ReturnFileContentModel()
        {
            //Arrange
            Mock <ILogger <FileDataProvider> > moq = new Mock <ILogger <FileDataProvider> >(MockBehavior.Loose);

            FileDataProvider fileDataProvider       = new FileDataProvider(moq.Object);
            string           path                   = Path.GetTempPath();
            string           tempDirectory          = Path.Combine(path, "testfiledataprovidertest2");
            string           tempFileName           = Path.Combine(tempDirectory, "file1.txt");
            DataContentModel assertFileContentModel = new DataContentModel()
            {
                FilePath       = tempFileName,
                ContentStrings = new string[]
                {
                    "Line1",
                    "Line2",
                    "Line3"
                }
            };

            if (Directory.Exists(tempDirectory))
            {
                Directory.Delete(tempDirectory, true);
            }
            Directory.CreateDirectory(tempDirectory);
            await File.WriteAllLinesAsync(tempFileName, assertFileContentModel.ContentStrings);

            //Act
            DataContentModel resultModel = await fileDataProvider.ReadDataToModelAsync(tempFileName);

            Directory.Delete(tempDirectory, true);

            //Assert
            Assert.Equal(assertFileContentModel, resultModel);
        }
Exemple #3
0
        public static bool TryMapTo(this DataContentModel fromModel, out IEnumerable <Matrix <int> > toModel)
        {
            if (fromModel == null)
            {
                throw new ArgumentNullException(nameof(fromModel));
            }

            //Right command contains minimum 3 lines: operation, empty and matrix
            if (fromModel.ContentStrings.Length < 3 ||
                string.IsNullOrWhiteSpace(fromModel.ContentStrings[0]) ||
                !string.IsNullOrWhiteSpace(fromModel.ContentStrings[1]) ||
                string.IsNullOrWhiteSpace(fromModel.ContentStrings[2]))
            {
                toModel = null;
                return(false);
            }

            List <string>        serializedMatrix = new List <string>();
            List <Matrix <int> > lstMatrices      = new List <Matrix <int> >();

            for (int index = 2; index < fromModel.ContentStrings.Length; index++)
            {
                if (string.IsNullOrWhiteSpace(fromModel.ContentStrings[index]))
                {
                    continue;
                }
                serializedMatrix.Add(fromModel.ContentStrings[index]);

                //If this string is last or next string is empty, convert collection to matrix
                if (index == fromModel.ContentStrings.Length - 1 ||
                    string.IsNullOrWhiteSpace(fromModel.ContentStrings[index + 1]))
                {
                    if (serializedMatrix.TryMapTo(out Matrix <int> matrix))
                    {
                        lstMatrices.Add(matrix);
                        serializedMatrix = new List <string>();
                        continue;
                    }
                    toModel = null;
                    return(false);
                }
            }
            toModel = lstMatrices;
            return(true);
        }
        public async Task WriteFileAsync(DataContentModel fileContent)
        {
            if (fileContent == null)
            {
                throw new ArgumentNullException(nameof(fileContent));
            }

            try
            {
                await File.WriteAllLinesAsync(fileContent.FilePath, fileContent.ContentStrings);

                Logger.LogInformation("Write result to file {filePath}", fileContent.FilePath);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Error writing to file {filePath}", fileContent.FilePath);
                throw;
            }
        }
Exemple #5
0
        public static bool TryMapTo(this DataContentModel fromModel, out IProcessorCommand toModel)
        {
            if (fromModel == null)
            {
                throw new ArgumentNullException(nameof(fromModel));
            }

            //Right command contains minimum 3 lines: operation, empty and matrix
            if (fromModel.ContentStrings.Length < 3 ||
                !string.IsNullOrWhiteSpace(fromModel.ContentStrings[1]) ||
                string.IsNullOrWhiteSpace(fromModel.ContentStrings[2]))
            {
                //It is not command file, skip
                toModel = null;
                return(false);
            }

            string strOperation = fromModel.ContentStrings[0];

            if (!Enum.TryParse(strOperation, true, out ProcessorCommandFabric.Operator operation))
            {
                //It is not command file, skip
                toModel = null;
                return(false);
            }

            IProcessorCommand processor = ProcessorCommandFabric.GetProcessor(operation);

            if (!fromModel.TryMapTo(out IEnumerable <Matrix <int> > matrices))
            {
                //It is a command file, but wrong data, return BadCommand
                toModel = ProcessorCommandFabric.GetBadProcessor(fromModel.FilePath);
                return(true);
            }
            processor.Source = matrices;
            processor.Id     = fromModel.FilePath;
            toModel          = processor;
            return(true);
        }
Exemple #6
0
        private TransformBlock <string, DataContentModel> GetReadDataProducer(ExecutionDataflowBlockOptions options)
        {
            var readData = new TransformBlock <string, DataContentModel>
                           (
                async dataPath =>
            {
                Interlocked.Increment(ref filesFound);
                try
                {
                    DataContentModel result = await DataProvider.ReadDataToModelAsync(dataPath);
                    Interlocked.Increment(ref filesRead);
                    return(result);
                }
                catch (Exception ex)
                {
                    Logger.LogError("Error reading file {id} ({error})", dataPath, ex.Message);
                    return(null);
                }
            },
                options
                           );

            return(readData);
        }
Exemple #7
0
        public static bool TryMapTo(this IProcessorCommand fromModel, out DataContentModel toModel)
        {
            if (fromModel == null)
            {
                throw new ArgumentNullException(nameof(fromModel));
            }

            if (fromModel is BadProcessorCommand || !fromModel.IsCalculated)
            {
                toModel = null;
                return(false);
            }
            toModel = new DataContentModel
            {
                FilePath = fromModel.Id + "_result.txt"
            };
            List <string> lstToModel  = new List <string>();
            bool          firstMatrix = true;

            foreach (Matrix <int> matrix in fromModel.Result)
            {
                if (!firstMatrix)
                {
                    lstToModel.Add(string.Empty);
                }
                if (!matrix.TryMapTo(out IEnumerable <string> lstMatrix))
                {
                    toModel = null;
                    return(false);
                }
                lstToModel.AddRange(lstMatrix);
                firstMatrix = false;
            }
            toModel.ContentStrings = lstToModel.ToArray();
            return(true);
        }