public void RegisterFile([NotNull] string fileName, [NotNull] string description, bool displayResultFileEntry, ResultFileID rfid1,
                                 [NotNull] HouseholdKey householdKey, TargetDirectory targetDirectory, CalcOption enablingOption,
                                 [CanBeNull] string additionalFileIndex = null)
        {
            if (!HouseholdRegistry.IsHouseholdRegistered(householdKey))
            {
                throw new LPGException("Forgotten Household Key: " + householdKey);
            }

            if (fileName.Contains(".."))
            {
                throw new LPGException("Filename with two dots. This looks bad. Please fix: " + fileName);
            }
            if (fileName.Contains("  "))
            {
                throw new LPGException("Filename with two spaces. This looks bad. Please fix: " + fileName);
            }
            string          fullFileName = Path.Combine(GetFullPathForTargetdirectry(targetDirectory), fileName);
            FileInfo        fi           = new FileInfo(fullFileName);
            ResultFileEntry rfe          = new ResultFileEntry(description, fi, displayResultFileEntry, rfid1, householdKey.Key,
                                                               additionalFileIndex, enablingOption);

            _inputDataLogger.Save(rfe);
            ResultFileList.ResultFiles.Add(rfe.HashKey, rfe);
        }
        public void FinalSaveToDatabase()
        {
            if (_actionEntries.Count > 0 && _calcParameters.Options.Contains(CalcOption.ActionEntries))
            {
                _idl.SaveList(_actionEntries.ConvertAll(x => (IHouseholdKey)x));
                _actionEntries.Clear();
            }

            if (_affordanceActivationEntries.Count > 0 && _calcParameters.Options.Contains(CalcOption.DeviceActivations))
            {
                _idl.SaveList(_affordanceActivationEntries.ConvertAll(x => (IHouseholdKey)x));
                _affordanceActivationEntries.Clear();
            }

            if (_columnEntries.Count > 0)
            {
                _idl.Save(_columnEntries);
                _columnEntries.Clear();
            }

            if (_locationEntries.Count > 0 && _calcParameters.Options.Contains(CalcOption.LocationsEntries))
            {
                _idl.SaveList(_locationEntries.ConvertAll(x => (IHouseholdKey)x));
                _locationEntries.Clear();
            }

            if (_transportationStatuses.Count > 0)
            {
                _idl.SaveList(_transportationStatuses.ConvertAll(x => (IHouseholdKey)x));
                _transportationStatuses.Clear();
            }

            if (_transportationEvents.Count > 0)
            {
                _idl.SaveList(_transportationEvents.ConvertAll(x => (IHouseholdKey)x));
                _transportationEvents.Clear();
            }
            if (_personStatus.Count > 0)
            {
                _idl.SaveList(_personStatus.ConvertAll(x => (IHouseholdKey)x));
                _personStatus.Clear();
            }
            if (_transportationDeviceState.Count > 0)
            {
                _idl.SaveList(_transportationDeviceState.ConvertAll(x => (IHouseholdKey)x));
                _transportationDeviceState.Clear();
            }

            if (_chargingStationStates.Count > 0)
            {
                _idl.SaveList(_chargingStationStates.ConvertAll(x => (IHouseholdKey)x));
                _chargingStationStates.Clear();
            }
            if (_variableEntries.Count > 0)
            {
                _idl.SaveList(_variableEntries.ConvertAll(x => (IHouseholdKey)x));
                _variableEntries.Clear();
            }
        }
        protected override void PerformActualStep(IStepParameters parameters)
        {
            var calcParameters          = _repository.CalcParameters;
            HouseholdStepParameters hhp = (HouseholdStepParameters)parameters;
            var entry = hhp.Key;

            if (entry.KeyType != HouseholdKeyType.Household)
            {
                return;
            }

            var householdKey = entry.HHKey;

            _calculationProfiler.StartPart(Utili.GetCurrentMethodAndClass());


            Logger.Info("Starting to complete list of heat gains by human activity at each time step...");

            var singletimestepEntries = _repository.ReadSingleTimestepActionEntries(householdKey)
                                        .OrderBy(x => x.TimeStep)
                                        .ToList();

            if (singletimestepEntries.Count == 0)
            {
                throw new LPGException("No file for actions each time step found");
            }
            var actionEntries = _repository.ReadActionEntries(householdKey);

            if (actionEntries.Count == 0)
            {
                throw new LPGException("");
            }
            var actionEntryDict = new Dictionary <StrGuid, ActionEntry>();

            foreach (var ae in actionEntries)
            {
                actionEntryDict.Add(ae.ActionEntryGuid, ae);
            }

            BodilyActivityLevelStatistics bals = new BodilyActivityLevelStatistics(householdKey);

            foreach (BodilyActivityLevel level in Enum.GetValues(typeof(BodilyActivityLevel)))
            {
                bals.ActivityLevels.Add(level, new List <double>(new double[calcParameters.OfficalTimesteps]));
            }
            foreach (var actionEntry in singletimestepEntries)
            {
                var ae = actionEntryDict[actionEntry.ActionEntryGuid];
                var ts = actionEntry.TimeStep;
                bals.ActivityLevels[ae.BodilyActivityLevel][ts] += 1;
            }
            _inputDataLogger.Save(householdKey, bals);
            _calculationProfiler.StopPart(Utili.GetCurrentMethodAndClass());
        }
        public void RegisterHousehold([NotNull] HouseholdKey key, [NotNull] string name, HouseholdKeyType type, [NotNull] IInputDataLogger idl, [NotNull] string description,
                                      [CanBeNull] string houseName, [CanBeNull] string houseDescription)
        {
            if (_householdKeys.Contains(key))
            {
                return;
                //throw new LPGException("Key was already registered: " + key.Key);
            }

            HouseholdKeyEntry hhke = new HouseholdKeyEntry(key, name, type, description, houseName, houseDescription);

            _householdKeys.Add(key);
            //_householdEntries.Add(hhke);
            idl.Save(hhke);
        }
        /*
         * [JetBrains.Annotations.NotNull]
         * private Dictionary<HouseholdKey, Dictionary<CalcLoadTypeDto, Dictionary<string, Dictionary<string, double>>>>
         *  _energyUseListByHouseholdAffordancePersonAndLoadtype;
         *
         * [JetBrains.Annotations.NotNull]
         * private readonly Dictionary<HouseholdKey, Dictionary<CalcLoadTypeDto, Dictionary<string, List<double>>>>
         *  _energyUseListByHouseholdAffordanceAndLoadtype;
         *
         * [JetBrains.Annotations.NotNull]
         * public Dictionary<HouseholdKey, Dictionary<CalcLoadTypeDto, Dictionary<string, double>>> EnergyUseByHouseholdAffordanceAndLoadtype { get; }
         */
        /*
         * public void RegisterDeviceActivation([JetBrains.Annotations.NotNull] HouseholdKey householdKey, [JetBrains.Annotations.NotNull] string affordanceName,
         *                                   [JetBrains.Annotations.NotNull] CalcLoadTypeDto loadType, double value,
         *                                   [JetBrains.Annotations.NotNull] double[] allvalues, [JetBrains.Annotations.NotNull] string activatorName)
         * {
         *
         *  if (!EnergyUseByHouseholdAffordanceAndLoadtype.ContainsKey(householdKey)) {
         *      EnergyUseByHouseholdAffordanceAndLoadtype.Add(householdKey,
         *          new Dictionary<CalcLoadTypeDto, Dictionary<string, double>>());
         *      _energyUseListByHouseholdAffordanceAndLoadtype.Add(householdKey,
         *          new Dictionary<CalcLoadTypeDto, Dictionary<string, List<double>>>());
         *      _energyUseListByHouseholdAffordancePersonAndLoadtype.Add(householdKey,
         *          new Dictionary<CalcLoadTypeDto, Dictionary<string, Dictionary<string, double>>>());
         *  }
         *
         *  if (!EnergyUseByHouseholdAffordanceAndLoadtype[householdKey].ContainsKey(loadType)) {
         *      EnergyUseByHouseholdAffordanceAndLoadtype[householdKey].Add(loadType, new Dictionary<string, double>());
         *      _energyUseListByHouseholdAffordanceAndLoadtype[householdKey]
         *          .Add(loadType, new Dictionary<string, List<double>>());
         *      _energyUseListByHouseholdAffordancePersonAndLoadtype[householdKey]
         *          .Add(loadType, new Dictionary<string, Dictionary<string, double>>());
         *  }
         *
         *  if (!_energyUseListByHouseholdAffordancePersonAndLoadtype[householdKey][loadType]
         *      .ContainsKey(affordanceName)) {
         *      _energyUseListByHouseholdAffordancePersonAndLoadtype[householdKey][loadType]
         *          .Add(affordanceName, new Dictionary<string, double>());
         *  }
         *
         *  if (!_energyUseListByHouseholdAffordancePersonAndLoadtype[householdKey][loadType][affordanceName]
         *      .ContainsKey(activatorName)) {
         *      _energyUseListByHouseholdAffordancePersonAndLoadtype[householdKey][loadType][affordanceName]
         *          .Add(activatorName, 0);
         *  }
         *
         *  if (!EnergyUseByHouseholdAffordanceAndLoadtype[householdKey][loadType].ContainsKey(affordanceName)) {
         *      EnergyUseByHouseholdAffordanceAndLoadtype[householdKey][loadType].Add(affordanceName, 0);
         *      _energyUseListByHouseholdAffordanceAndLoadtype[householdKey][loadType]
         *          .Add(affordanceName, new List<double>());
         *  }
         *
         *  EnergyUseByHouseholdAffordanceAndLoadtype[householdKey][loadType][affordanceName] += value;
         *
         *  _energyUseListByHouseholdAffordanceAndLoadtype[householdKey][loadType][affordanceName].AddRange(allvalues);
         *  _energyUseListByHouseholdAffordancePersonAndLoadtype[householdKey][loadType][affordanceName][activatorName]
         +=
         *      value;
         *  if (_energyUseListByHouseholdAffordanceAndLoadtype[householdKey][loadType][affordanceName].Count >
         *      _maxTime) {
         *      _maxTime = _energyUseListByHouseholdAffordanceAndLoadtype[householdKey][loadType][affordanceName].Count;
         *  }
         * }*/
        /*
         * public void WriteResults([JetBrains.Annotations.NotNull] List<CalcAffordanceTaggingSetDto> affTaggingSets)
         * {
         *  if (_wroteResultsAlready)
         *  {
         *      return;
         *  }
         *
         *  foreach (var householdDict in
         *      EnergyUseByHouseholdAffordanceAndLoadtype)
         *  {
         *      foreach (var loadTypeDict in householdDict.Value)
         *      {
         *          using (var sw =
         *              _fft.MakeFile<StreamWriter>(
         *                  "AffordanceEnergyUse." + householdDict.Key + "." + loadTypeDict.Key.FileName + ".csv",
         *                  "Energy use per affordance " + loadTypeDict.Key.Name, true,
         *                  ResultFileID.AffordanceEnergyUse,
         *                  householdDict.Key, TargetDirectory.Reports, _calcParameters.InternalStepsize,
         *                  loadTypeDict.Key.ConvertToLoadTypeInformation()))
         *          {
         *              var header = "Affordance" + _calcParameters.CSVCharacter + "Energy use [" +
         *                           loadTypeDict.Key.UnitOfSum + "]" + _calcParameters.CSVCharacter;
         *              foreach (var set in affTaggingSets)
         *              {
         *                  if (set.LoadTypes.Contains(loadTypeDict.Key))
         *                  {
         *                      header += set.Name + _calcParameters.CSVCharacter;
         *                  }
         *              }
         *
         *              sw.WriteLine(header);
         *              foreach (var affordanceTuple in loadTypeDict.Value)
         *              {
         *                  var sb = new StringBuilder();
         *                  sb.Append(affordanceTuple.Key);
         *                  sb.Append(_calcParameters.CSVCharacter);
         *                  sb.Append(affordanceTuple.Value * loadTypeDict.Key.ConversionFactor);
         *                  foreach (var set in affTaggingSets)
         *                  {
         *                      if (set.LoadTypes.Contains(loadTypeDict.Key))
         *                      {
         *                          sb.Append(_calcParameters.CSVCharacter);
         *                          if (set.AffordanceToTagDict.ContainsKey(affordanceTuple.Key))
         *                          {
         *                              sb.Append(set.AffordanceToTagDict[affordanceTuple.Key]);
         *                          }
         *                          else
         *                          {
         *                              sb.Append(affordanceTuple.Key);
         *                          }
         *                      }
         *                  }
         *
         *                  sw.WriteLine(sb);
         *              }
         *          }
         *      }
         *  }
         *
         *  foreach (
         *      var
         *          householdDict in _energyUseListByHouseholdAffordancePersonAndLoadtype)
         *  {
         *      foreach (var loadTypeDict in
         *          householdDict.Value)
         *      {
         *          using (var sw =
         *              _fft.MakeFile<StreamWriter>(
         *                  "AffordanceEnergyUsePerPerson." + householdDict.Key + "." + loadTypeDict.Key.FileName +
         *                  ".csv", "Energy use per affordance per Person for " + loadTypeDict.Key.Name, true,
         *                  ResultFileID.AffordancePersonEnergyUse, householdDict.Key,
         *                  TargetDirectory.Reports, _calcParameters.InternalStepsize,
         *                  loadTypeDict.Key.ConvertToLoadTypeInformation()))
         *          {
         *              sw.WriteLine("Energy use per affordance per Person in [" + loadTypeDict.Key.UnitOfSum + "]");
         *              var header = "Affordance" + _calcParameters.CSVCharacter;
         *              var persons = new List<string>();
         *
         *              foreach (var affDict in loadTypeDict.Value)
         *              {
         *                  foreach (var person in affDict.Value)
         *                  {
         *                      if (!persons.Contains(person.Key))
         *                      {
         *                          persons.Add(person.Key);
         *                      }
         *                  }
         *              }
         *
         *              foreach (var person in persons)
         *              {
         *                  header += person + _calcParameters.CSVCharacter;
         *              }
         *
         *              sw.WriteLine(header);
         *
         *              foreach (var affdict in loadTypeDict.Value)
         *              {
         *                  var line = affdict.Key + _calcParameters.CSVCharacter;
         *
         *                  foreach (var person in persons)
         *                  {
         *                      if (affdict.Value.ContainsKey(person))
         *                      {
         *                          line += affdict.Value[person] * loadTypeDict.Key.ConversionFactor +
         *                                  _calcParameters.CSVCharacter;
         *                      }
         *                      else
         *                      {
         *                          line += 0 + _calcParameters.CSVCharacter;
         *                      }
         *                  }
         *
         *                  sw.WriteLine(line);
         *              }
         *          }
         *      }
         *  }
         *
         *  _wroteResultsAlready = true;
         * }*/

        protected override void PerformActualStep(IStepParameters parameters)
        {
            HouseholdLoadtypeStepParameters hsp = (HouseholdLoadtypeStepParameters)parameters;

            if (hsp.Key.HHKey == Constants.GeneralHouseholdKey || hsp.Key.HHKey == Constants.HouseKey)
            {
                return;
            }

            var deviceActivations = Repository.LoadDeviceActivations(hsp.Key.HHKey);

            if (deviceActivations.Count == 0)
            {
                throw new LPGException("No device activations were found");
            }
            Dictionary <string, AffordanceEnergyUseEntry> energyUseEntries = new Dictionary <string, AffordanceEnergyUseEntry>();

            foreach (DeviceActivationEntry ae in deviceActivations)
            {
                string key = AffordanceEnergyUseEntry.MakeKey(ae.HouseholdKey, ae.LoadTypeGuid, ae.AffordanceName,
                                                              ae.ActivatorName);
                if (!energyUseEntries.ContainsKey(key))
                {
                    energyUseEntries.Add(key, new AffordanceEnergyUseEntry(
                                             ae.HouseholdKey, ae.LoadTypeGuid, ae.AffordanceName, ae.ActivatorName,
                                             ae.LoadTypeName));
                }
                var aeu = energyUseEntries[key];
                aeu.EnergySum += ae.TotalEnergySum;
                aeu.TotalActivationDurationInSteps += ae.DurationInSteps;
                aeu.NumberOfActivations++;
            }

            if (energyUseEntries.Count == 0)
            {
                throw new LPGException("No energy use entries were found.");
            }
            _logger.Save(hsp.Key.HHKey, energyUseEntries.Values.ToList());
        }