/// <summary>
 /// Генерация нечетких тенденций
 /// </summary>
 /// <param name="seriesId"></param>
 public static void GenerateFuzzyTrends(int seriesId)
 {
     using (var _context = new DissertationDbContext())
         using (var transaction = _context.Database.BeginTransaction())
         {
             try
             {
                 foreach (FuzzyTrendLabel elem in Enum.GetValues(typeof(FuzzyTrendLabel)))
                 {
                     _context.FuzzyTrends.Add(ModelConvector.ToFuzzyTrend(new FuzzyTrendBindingModel
                     {
                         SeriesId  = seriesId,
                         TrendName = elem,
                         Weight    = Converter.ToFuzzyTrendLabelWeight(elem)
                     }));
                     _context.SaveChanges();
                 }
                 transaction.Commit();
             }
             catch (Exception)
             {
                 transaction.Rollback();
                 throw;
             }
         }
 }
        private bool MakeTest(FileInfo file)
        {
            using (var _context = new DissertationDbContext())
            {
                try
                {
                    var entity = ModelConvector.ToSeriesDescription(new SeriesDescriptionBindingModel
                    {
                        SeriesName   = file.Name,
                        NeedForecast = true
                    });
                    _context.SeriesDescriptions.Add(entity);
                    _context.SaveChanges();

                    #region Временной ряд
                    var res = CreateFuzzyLabel(file.FullName, entity.Id);
                    if (!res)
                    {
                        return(false);
                    }
                    _context.LogDatas.Add(new LogData
                    {
                        DateLog         = DateTime.Now,
                        MessageLogType  = "Info",
                        MessageLogTitle = file.Name,
                        MessageLog      = string.Format("{0} нечеткие метки получены", file.Name)
                    });

                    res = CreateFuzzyTrend(file.FullName, entity.Id);
                    if (!res)
                    {
                        return(false);
                    }
                    _context.LogDatas.Add(new LogData
                    {
                        DateLog         = DateTime.Now,
                        MessageLogType  = "Info",
                        MessageLogTitle = file.Name,
                        MessageLog      = string.Format("{0} нечеткие тенденции получены", file.Name)
                    });

                    res = CreateRuleTrend(file.FullName, entity.Id);
                    if (!res)
                    {
                        return(false);
                    }
                    _context.LogDatas.Add(new LogData
                    {
                        DateLog         = DateTime.Now,
                        MessageLogType  = "Info",
                        MessageLogTitle = file.Name,
                        MessageLog      = string.Format("{0} правила для тенденций получены", file.Name)
                    });

                    res = CreatePointTrend(file.FullName, entity.Id);
                    if (!res)
                    {
                        return(false);
                    }
                    _context.LogDatas.Add(new LogData
                    {
                        DateLog         = DateTime.Now,
                        MessageLogType  = "Info",
                        MessageLogTitle = file.Name,
                        MessageLog      = string.Format("{0} точки для тенденций получены", file.Name)
                    });

                    res = GenerateSituationsByEntropy(file.FullName, entity.Id);
                    if (!res)
                    {
                        return(false);
                    }
                    _context.LogDatas.Add(new LogData
                    {
                        DateLog         = DateTime.Now,
                        MessageLogType  = "Info",
                        MessageLogTitle = file.Name,
                        MessageLog      = string.Format("{0} ситуации по энтропии получены", file.Name)
                    });

                    res = GenerateSituationsByFuzzy(file.FullName, entity.Id);
                    if (!res)
                    {
                        return(false);
                    }
                    _context.LogDatas.Add(new LogData
                    {
                        DateLog         = DateTime.Now,
                        MessageLogType  = "Info",
                        MessageLogTitle = file.Name,
                        MessageLog      = string.Format("{0} ситуации по нечеткости получены", file.Name)
                    });
                    #endregion

                    lock (runForecast)
                    {
                        _countPoints = 0;
                        _points      = new List <PointInfo>();
                        var test = ModelConvector.ToDiagnosticTest(new DiagnosticTestBindingModel
                        {
                            TestNumber  = "1",
                            FileName    = file.FullName,
                            TypeFile    = TypeFile.Текстовый,
                            DatasInFile = new List <TypeDataInFile> {
                                TypeDataInFile.ЧисловоеЗначение
                            },
                            SeriesDiscriptionId   = entity.Id,
                            CountPointsForMemmory = 5
                        });
                        test.DateTest = DateTime.Now;
                        test.Count    = 0;
                        _context.DiagnosticTests.Add(test);
                        _context.SaveChanges();
                        _context.LogDatas.Add(new LogData
                        {
                            DateLog         = DateTime.Now,
                            MessageLogType  = "Info",
                            MessageLogTitle = file.Name,
                            MessageLog      = string.Format("{0} создали тест", file.Name)
                        });

                        var values = LoadFromTxt(file.FullName);

                        if (values != null && values.Count > 1)
                        {
                            _context.LogDatas.Add(new LogData
                            {
                                DateLog         = DateTime.Now,
                                MessageLogType  = "Info",
                                MessageLogTitle = file.Name,
                                MessageLog      = string.Format("{0} загрузили данные", file.Name)
                            });
                            for (int i = 0; i < values.Count - 1; ++i)
                            {
                                AddNewPoint(new PointInfo
                                {
                                    SeriesDiscriptionId = entity.Id,
                                    DiagnosticTestId    = test.Id,
                                    Value = values[i]
                                }, entity.Id);
                            }
                            _context.LogDatas.Add(new LogData
                            {
                                DateLog         = DateTime.Now,
                                MessageLogType  = "Info",
                                MessageLogTitle = file.Name,
                                MessageLog      = string.Format("{0} обработали данные", file.Name)
                            });

                            test.Count = _countPoints;
                            var lastPoint = _points[_points.Count - 1];
                            lastPoint.IsLast = true;
                            ClearPoint(lastPoint);
                            _context.PointInfos.Add(lastPoint);
                            _context.SaveChanges();
                            var preLastPoint = _points[_points.Count - 2];
                            ClearPoint(preLastPoint);
                            _context.PointInfos.Add(preLastPoint);
                            _context.SaveChanges();

                            _context.LogDatas.Add(new LogData
                            {
                                DateLog         = DateTime.Now,
                                MessageLogType  = "Info",
                                MessageLogTitle = file.Name,
                                MessageLog      = string.Format("{0} сохранили последние точки", file.Name)
                            });
                            _context.SaveChanges();

                            var forecast  = mdt.GetForecast(test.Id);
                            var forecasts = string.Join(";", mdt.GetForecastByPointTrend(test.Id));
                            _context.LogDatas.Add(new LogData
                            {
                                DateLog         = DateTime.Now,
                                MessageLogType  = "Info",
                                MessageLogTitle = file.Name,
                                MessageLog      = string.Format("{0} получили прогноз", file.Name)
                            });
                            var realValue = values[values.Count - 1];
                            _context.ExperimentFileResults.Add(new ExperimentFileResult
                            {
                                DateExperiment   = DateTime.Now,
                                Forecast         = forecast,
                                ForecastsByPoint = forecasts,
                                RealValue        = realValue,
                                FileName         = file.Name
                            });
                            _context.SaveChanges();

                            _context.LogDatas.Add(new LogData
                            {
                                DateLog         = DateTime.Now,
                                MessageLogType  = "Info",
                                MessageLogTitle = file.Name,
                                MessageLog      = string.Format("{0} сохранили прогноз", file.FullName)
                            });
                            _context.SaveChanges();
                        }
                        else
                        {
                            _context.SaveChanges();
                            _context.LogDatas.Add(new LogData
                            {
                                DateLog         = DateTime.Now,
                                MessageLogType  = "Error",
                                MessageLogTitle = file.Name,
                                MessageLog      = string.Format("{0} не получили точек", file.FullName)
                            });
                            _context.SaveChanges();
                        }
                    }
                    return(true);
                }
                catch (Exception ex)
                {
                    _context.LogDatas.Add(new LogData
                    {
                        DateLog        = DateTime.Now,
                        MessageLog     = string.Format("MakeTest {0}: {1}", file.FullName, ex.Message),
                        MessageLogType = "Error"
                    });
                    _context.SaveChanges();
                    return(false);
                }
            }
        }