示例#1
0
        public void GetDuplicateCalcManagerHouseholdTest()
        {
            using (var wd1 = new WorkingDir("GetDuplicateCalcManagerHouseholdTest1"))
            {
                CalculateOneHousehold(wd1.WorkingDirectory);
                using (var wd2 = new WorkingDir("GetDuplicateCalcManagerHouseholdTest2"))
                {
                    CalculateOneHousehold(wd2.WorkingDirectory);

                    var hhkeys   = HouseholdKeyLogger.Load(wd1.SqlResultLoggingService);
                    var afts1    = new CalcDeviceDtoLogger(wd1.SqlResultLoggingService);
                    var aft1     = afts1.Load(hhkeys.Where(x => x.KeyType == HouseholdKeyType.Household).ToList());
                    var afts2    = new CalcDeviceDtoLogger(wd2.SqlResultLoggingService);
                    var aft2     = afts2.Load(hhkeys.Where(x => x.KeyType == HouseholdKeyType.Household).ToList());
                    var devices1 = aft1.Select(x => x.Name).OrderBy(x => x).ToList();
                    var devices2 = aft2.Select(x => x.Name).OrderBy(x => x).ToList();
                    devices1.Should().BeEquivalentTo(devices2);

                    var rfel1 = new ResultFileEntryLogger(wd1.SqlResultLoggingService);
                    var rfes1 = rfel1.Load();
                    var rfel2 = new ResultFileEntryLogger(wd2.SqlResultLoggingService);
                    var rfes2 = rfel2.Load();
                    rfes1.Should().BeEquivalentTo(rfes2, o => o.Excluding(
                                                      x => x.SelectedMemberPath.EndsWith("FullFileName", StringComparison.InvariantCultureIgnoreCase)));

                    CompareCsv(rfes1, rfes2);
                    wd1.CleanUp();
                    wd2.CleanUp();
                }
            }
        }
        public ResultFileEntry GetRfeByFilename([JetBrains.Annotations.NotNull] string filename)
        {
            ResultFileEntryLogger rfel = new ResultFileEntryLogger(_wd.SqlResultLoggingService);
            var rfes = rfel.Load();

            return(rfes.Single(x => x.FileName == filename));
        }
示例#3
0
/*
 *      private  void ProcessResultfilesForSettlement(List<CalculationResult> results, string path,ObservableCollection<ResultFileEntry> settlementResults, [CanBeNull] Dispatcher dispatcher,Simulator sim) {
 *          try {
 *              {
 *                  //todo: change this to read the ltdict from the database
 *                  var ltdict = CalcLoadTypeFactory.MakeLoadTypes(
 *                      sim.LoadTypes.MyItems, _internalStepSize, LoadTypePriority.All);
 *                  //var calcLoadTypes = new List<CalcLoadType>(ltdict.LtDict.Values);
 *                  throw new NotImplementedException("xxx");
 * //                  var srfg = new SettlementResultFileGenerator(results,new FileFactoryAndTracker(path, "(none)"), settlementResults, calcLoadTypes.ToArray(),dispatcher);
 * //                  srfg.Run();
 *              }
 *          }
 *          catch (Exception e) {
 *              MessageWindows.ShowDebugMessage(e);
 *              Logger.Exception(e);
 *              if (Config.IsInUnitTesting) {
 *                  throw;
 *              }
 *          }
 *      }
 */
        public void ProcessResults()
        {
            SqlResultLoggingService srls = new SqlResultLoggingService(_resultPath);

            // for a single household / house
            //if (_calculationEntries.Count == 1) // && (_calculationEntries[0].CalcObject.GetType() == typeof (Household)
            {
                if (_deleteDatFiles)
                {
                    ResultFileEntryLogger rfel = new ResultFileEntryLogger(srls);
                    var resultFileEntries      = rfel.Load();
                    DeleteDatFiles(resultFileEntries);
                }
                //
                return;
            }
            // for a settlement
            //throw new LPGException("xxx - no settlement calculation anymore");
            //var results = new List<CalculationResult>();
            //foreach (var entry in _calculationEntries) {
            //results.Add(entry.CalculationResult);
            //}

            //var resultFileEntries = new ObservableCollection<ResultFileEntry>();
            //var everythingok = false;

            /*            if (results.Count > 0 && results[0] != null) {
             *              everythingok = true;
             *              throw new LPGException("xxx");
             *              //ProcessResultfilesForSettlement(results, _resultPath, resultFileEntries,_csps.Dispatcher, sim);
             *
             *          }*/

            /*
             * if (_deleteDatFiles) {
             *  foreach (var entry in _calculationEntries) {
             *      DeleteDatFiles(entry);
             *  }
             * }
             * if (_calculationEntries.Count > 0) {
             *  if (_csps.Dispatcher != null && Thread.CurrentThread != _csps.Dispatcher.Thread) {
             *      _csps.Dispatcher.BeginInvoke(DispatcherPriority.Normal, _csps.ReportFinishFuncForHouseAndSettlement,
             *           everythingok, _csps.CalcTarget.Name, resultFileEntries);
             *  }
             *  else {
             *      _csps.ReportFinishFuncForHouseAndSettlement( everythingok, _csps.CalcTarget.Name,
             *          resultFileEntries);
             *  }
             * }
             * else {
             *  if (_csps.Dispatcher != null) {
             *      _csps.Dispatcher.BeginInvoke(DispatcherPriority.Normal, _csps.ReportFinishFuncForHouseAndSettlement,
             *          null, false, string.Empty, resultFileEntries);
             *  }
             *  else {
             *      _csps.ReportFinishFuncForHouseAndSettlement(null, false, string.Empty, resultFileEntries);
             *  }
             * }*/
        }
示例#4
0
        public ResultPresenter([NotNull] ApplicationPresenter applicationPresenter, [NotNull] ResultView view, [NotNull] string householdname,
                               [NotNull] string resultPath) : base(view, "HeaderString", applicationPresenter)
        {
            ResultPath     = resultPath;
            _householdname = householdname;
            SqlResultLoggingService srls = new SqlResultLoggingService(resultPath);
            ResultFileEntryLogger   rfel = new ResultFileEntryLogger(srls);
            var rfes = rfel.Load();

            _resultFiles = new ObservableCollection <ResultFileEntry>(rfes);
            foreach (var entry in _resultFiles)
            {
                _filteredResultFiles.Add(entry);
            }

            ResultPath = resultPath;
        }
        public void ReadExistingFilesFromSql()
        {
            SqlResultLoggingService srls        = new SqlResultLoggingService(_baseResultpath);
            HouseholdKeyLogger      hhKeyLogger = new HouseholdKeyLogger(srls);
            var hhkes = hhKeyLogger.Load();

            foreach (HouseholdKeyEntry entry in hhkes)
            {
                HouseholdRegistry.AddSavedEntry(entry);
            }

            var rfel = new ResultFileEntryLogger(srls);
            var rfes = rfel.Load();

            ResultFileList.AddExistingEntries(rfes);
            _inputDataLogger.AddSaver(rfel);
        }
        public void FileFactoryAndTrackerTest()
        {
            Config.IsInUnitTesting = true;
            using (var wd = new WorkingDir(Utili.GetCurrentMethodAndClass()))
            {
                wd.InputDataLogger.AddSaver(new HouseholdKeyLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new ResultFileEntryLogger(wd.SqlResultLoggingService));
                CalcParameters  calcParameters = CalcParametersFactory.MakeGoodDefaults();
                var             clt            = new CalcLoadType("calcloadtype", "kwh", "kW", 0.001, true, Guid.NewGuid().ToStrGuid());
                BitArray        isSick         = new BitArray(calcParameters.InternalTimesteps);
                BitArray        isOnVacation   = new BitArray(calcParameters.InternalTimesteps);
                var             personDto      = CalcPersonDto.MakeExamplePerson();
                Random          r  = new Random();
                Mock <ILogFile> lf = new Mock <ILogFile>();
                using (CalcRepo calcRepo = new CalcRepo(rnd: r, lf: lf.Object, calcParameters: calcParameters))
                {
                    CalcLocation cloc = new CalcLocation("blub", Guid.NewGuid().ToStrGuid());
                    var          cp   = new CalcPerson(personDto,
                                                       cloc, isSick, isOnVacation, calcRepo);

                    /*"personname", 1, 1, null, 1, PermittedGender.Female, lf: null,
                     * householdKey: "hh1", startingLocation: null, traitTag: "traittag",
                     * householdName: "hhname0",calcParameters:calcParameters,isSick:isSick, guid:Guid.NewGuid().ToStrGuid());
                     */
                    var fft = new FileFactoryAndTracker(wd.WorkingDirectory, "testhh", wd.InputDataLogger);
                    fft.RegisterHousehold(new HouseholdKey("hh1"), "test key", HouseholdKeyType.Household, "desc", null, null);
                    fft.RegisterHousehold(Constants.GeneralHouseholdKey, "general key", HouseholdKeyType.General, "desc", null, null);
                    fft.MakeFile <StreamWriter>("file1", "desc", true, ResultFileID.Actions,
                                                new HouseholdKey("hh1"), TargetDirectory.Charts, TimeSpan.FromMinutes(1),
                                                CalcOption.HouseholdContents,
                                                clt.ConvertToLoadTypeInformation(),
                                                cp.MakePersonInformation());
                    //fft.ResultFileList.WriteResultEntries(wd.WorkingDirectory);
                    ResultFileEntryLogger rfel = new ResultFileEntryLogger(wd.SqlResultLoggingService);
                    var rfes = rfel.Load();
                    rfes.Count.Should().BeGreaterThan(0);
                    //ResultFileList.ReadResultEntries(wd.WorkingDirectory);
                    fft.GetResultFileEntry(ResultFileID.Actions, clt.Name, new HouseholdKey("hh1"), cp.MakePersonInformation(), null);
                    fft.Dispose();
                }
                wd.CleanUp();
            }
        }
        // ReSharper disable once CollectionNeverUpdated.Local


        public static void CheckForResultfile(string wd, CalcOption option)
        {
            var srls         = new SqlResultLoggingService(wd);
            var rfel         = new ResultFileEntryLogger(srls);
            var rfes         = rfel.Load();
            var foundOptions = new List <CalcOption>();

            //collect all the file keys

            foreach (var rfe in rfes)
            {
                if (!File.Exists(rfe.FullFileName))
                {
                    throw new LPGException("File " + rfe.FullFileName +
                                           " was registered, but is not actually present.");
                }

                foundOptions.Add(rfe.EnablingCalcOption);
            }

            // ReSharper disable once CollectionNeverQueried.Local
            var allTables   = new List <ResultTableDefinition>();
            var hhKeyLogger = new HouseholdKeyLogger(srls);
            var keys        = hhKeyLogger.Load();

            foreach (var key in keys)
            {
                if (!srls.FilenameByHouseholdKey.ContainsKey(key.HHKey))
                {
                    continue;
                }

                var fn = srls.FilenameByHouseholdKey[key.HHKey];
                if (!File.Exists(fn.Filename))
                {
                    continue;
                }

                var tables = srls.LoadTables(key.HHKey);
                allTables.AddRange(tables);
                foreach (var table in tables)
                {
                    foundOptions.Add(table.EnablingOption);
                }
            }

            foundOptions = foundOptions.Distinct().ToList();

            var optionsThatDontResultInFiles = new List <CalcOption>();

            optionsThatDontResultInFiles.Add(CalcOption.MakePDF);
            optionsThatDontResultInFiles.Add(CalcOption.ActionCarpetPlot);
            optionsThatDontResultInFiles.Add(CalcOption.HouseholdPlan);
            optionsThatDontResultInFiles.Add(CalcOption.CalculationFlameChart);
            optionsThatDontResultInFiles.Add(CalcOption.MakeGraphics);
            optionsThatDontResultInFiles.Add(CalcOption.LogErrorMessages);
            optionsThatDontResultInFiles.Add(CalcOption.EnergyCarpetPlot);
            optionsThatDontResultInFiles.Add(CalcOption.DurationCurve);
            optionsThatDontResultInFiles.Add(CalcOption.TransportationDeviceCarpetPlot);
            optionsThatDontResultInFiles.Add(CalcOption.LocationCarpetPlot);
            if (!optionsThatDontResultInFiles.Contains(option))
            {
                if (!foundOptions.Contains(option))
                {
                    throw new LPGException("Option found that doesn't result in any files");
                }
            }

            var fftd           = new FileFactoryAndTrackerDummy();
            var cp             = new CalculationProfiler();
            var container      = PostProcessingManager.RegisterEverything(wd, cp, fftd);
            var enabledOptions = new HashSet <CalcOption>();

            enabledOptions.Add(option);
            using (var scope = container.BeginLifetimeScope()) {
                var odm = scope.Resolve <OptionDependencyManager>();
                odm.EnableRequiredOptions(enabledOptions);
            }

            foreach (var enabledOption in enabledOptions)
            {
                foundOptions.Remove(enabledOption);
            }

            if (foundOptions.Contains(CalcOption.BasicOverview))
            {
                foundOptions.Remove(CalcOption.BasicOverview);
            }

            if (foundOptions.Count > 0)
            {
                var s = string.Join("\n", foundOptions.Select(x => x.ToString()));
                throw new LPGException("found stuff that was not requested:" + s);
            }

            var filesthatdontneedtoregister = new List <string>();

            filesthatdontneedtoregister.Add("finished.flag");
            filesthatdontneedtoregister.Add("log.commandlinecalculation.txt");
            filesthatdontneedtoregister.Add("results.general.sqlite");
            filesthatdontneedtoregister.Add("results.general.sqlite-wal");
            filesthatdontneedtoregister.Add("results.general.sqlite-shm");
            filesthatdontneedtoregister.Add("results.hh1.sqlite");
            filesthatdontneedtoregister.Add("results.hh1.sqlite-shm");
            filesthatdontneedtoregister.Add("results.hh1.sqlite-wal");
            filesthatdontneedtoregister.Add("results.house.sqlite");
            filesthatdontneedtoregister.Add("results.house.sqlite-shm");
            filesthatdontneedtoregister.Add("results.house.sqlite-wal");
            filesthatdontneedtoregister.Add("calculationprofiler.json");
            //check if all files are registered
            var di              = new DirectoryInfo(wd);
            var files           = di.GetFiles("*.*", SearchOption.AllDirectories);
            var registeredFiles = rfes.Select(x => x.FullFileName).ToList();

            foreach (var file in files)
            {
                if (filesthatdontneedtoregister.Contains(file.Name.ToLower(CultureInfo.InvariantCulture)))
                {
                    continue;
                }

                if (!registeredFiles.Contains(file.FullName))
                {
                    throw new LPGException("Found unregistered file: " + file.FullName);
                }
            }


            //foundKeys = foundKeys.Distinct().ToList();
            //if (_resultFileIdsByCalcOption.ContainsKey(option)) {
            //    var rl = _resultFileIdsByCalcOption[option];
            //    foreach (var key in rl.ResultKeys) {
            //        if (!foundKeys.Contains(key)) {
            //            throw new LPGException("in the found keys the file " + key + " is missing.");
            //        }
            //    }

            //    foreach (var key in foundKeys) {
            //        if (!rl.ResultKeys.Contains(key)) {
            //            throw new LPGException("in the result list keys the file " + key + " is missing.");
            //        }
            //    }
            //}

            //else {
            //    //todo: this needs to be done differently. need to disable as much as possible except the given calc option first.
            //    var sb = new StringBuilder();
            //    sb.Append("--------------");
            //    sb.AppendLine();
            //    sb.Append("_resultFileIdsByCalcOption.Add(CalcOption." + option + ",  ResultList.Make(CalcOption.");
            //    sb.Append(option).Append(", ");
            //    foreach (var key in foundKeys) {
            //        sb.AppendLine();
            //        sb.Append("\"").Append(key).Append("\", ");
            //    }

            //    sb.Remove(sb.Length - 2, 2);
            //    sb.Append("));");
            //    sb.AppendLine();
            //    sb.Append("--------------");
            //    Logger.Info(sb.ToString());
            //    //throw new LPGException(sb.ToString());
            //}
        }
        private string GetFilenameForHouseholdKey([JetBrains.Annotations.NotNull] HouseholdKey key)
        {
            if (FilenameByHouseholdKey.ContainsKey(key))
            {
                return(FilenameByHouseholdKey[key].Filename);
            }

            bool isMainDatabase = key == Constants.GeneralHouseholdKey;

            string newName = Path.Combine(_basePath, "Results." + key + ".sqlite");

            FilenameByHouseholdKey.Add(key, new FileEntry(newName));
            FileInfo fi = new FileInfo(newName);

            FilenameByHouseholdKey[key].DescriptionTableWritten = true;
            if (fi.Exists && fi.Length > 1000)
            {
                return(newName);
            }

            using (SQLiteConnection dbcon = new SQLiteConnection(MakeconnectionString(newName))) {
                dbcon.Open();
                {
                    FieldDefinition        fd1    = new FieldDefinition("TableName", "Text");
                    FieldDefinition        fd2    = new FieldDefinition("Description", "Text");
                    FieldDefinition        fd3    = new FieldDefinition("ResultTableID", "Integer");
                    FieldDefinition        fd4    = new FieldDefinition("EnablingOption", "Integer");
                    List <FieldDefinition> fields = new List <FieldDefinition> {
                        fd1,
                        fd2,
                        fd3,
                        fd4
                    };
                    MakeTableForListOfFields(fields, dbcon, Constants.TableDescriptionTableName);
                }

                if (isMainDatabase)
                {
                    //MainFilename = newName;
                    {
                        FieldDefinition fd1 = new FieldDefinition("Filename", "Text");
                        //FieldDefinition fd3 = new FieldDefinition("ID", "INTEGER");
                        FieldDefinition        fd2    = new FieldDefinition("HouseholdKey", "Text");
                        List <FieldDefinition> fields = new List <FieldDefinition> {
                            fd1,
                            fd2
                        };
                        //fields.Add(fd3);
                        MakeTableForListOfFields(fields, dbcon, nameof(DatabaseList));
                    }
                }

                dbcon.Close();
            }
            ResultFileEntry rfe = new ResultFileEntry("Database", fi, false, ResultFileID.SqliteResultFiles, key.Key, null
                                                      , CalcOption.BasicOverview);
            ResultFileEntryLogger rfel = new ResultFileEntryLogger(this);

            rfel.Run(key, rfe);

            /* DatabaseList dbl = new DatabaseList(key.Key, null, newName)
             * {
             *   HouseholdKey = key.Key,
             *   Filename = newName
             * };*/
            var row = RowBuilder.Start("HouseholdKey", key.Key).Add("Filename", newName).ToDictionary();

            SaveDictionaryToDatabaseNewConnection(row, "DatabaseList", Constants.GeneralHouseholdKey);
            return(newName);
        }