Ejemplo n.º 1
0
 private void MakeSubAffordances([NotNull] DtoCalcLocationDict locations, [NotNull] CalcVariableRepository variableRepository,
                                 [NotNull] CalcAffordanceDto affordancedto, [NotNull] CalcAffordance caff)
 {
     foreach (CalcSubAffordanceDto sdto in affordancedto.SubAffordance)
     {
         CalcLocation      subaffLocation     = locations.GetCalcLocationByGuid(sdto.LocGuid);
         List <CalcDesire> satisfactionValues = MakeCalcDesires(sdto.Satisfactionvalues);
         var varOps = MakeVariableOps(locations, sdto.VariableOps, variableRepository);
         //subaffordances have no time limits
         BitArray          isBusySub = new BitArray(_calcRepo.CalcParameters.InternalTimesteps, false);
         CalcSubAffordance csuf      = new CalcSubAffordance(sdto.Name,
                                                             subaffLocation,
                                                             satisfactionValues,
                                                             sdto.MiniumAge, sdto.MaximumAge,
                                                             sdto.Delaytimesteps,
                                                             sdto.PermittedGender,
                                                             sdto.AffCategory,
                                                             sdto.IsInterruptable,
                                                             sdto.IsInterrupting,
                                                             caff,
                                                             varOps,
                                                             sdto.Weight,
                                                             sdto.SourceTrait,
                                                             sdto.Guid, isBusySub, variableRepository, caff.BodilyActivityLevel, _calcRepo);
         caff.SubAffordances.Add(csuf);
     }
 }
        public void CalcAffordanceDtoLoggerTest()
        {
            using (WorkingDir wd = new WorkingDir(Utili.GetCurrentMethodAndClass()))
            {
                CalcAffordanceDtoLogger ael    = new CalcAffordanceDtoLogger(wd.SqlResultLoggingService);
                HouseholdKey            key    = new HouseholdKey("hhkey");
                List <IDataSaverBase>   savers = new List <IDataSaverBase>
                {
                    ael
                };
                InputDataLogger idl = new InputDataLogger(savers.ToArray());
                //CalcLoadTypeDto cldto = new CalcLoadTypeDto("loadtype", 1, "kw", "kwh", 1, true, "guid");
                CalcProfileDto cpd = new CalcProfileDto("name", 1, ProfileType.Absolute, "source", Guid.NewGuid().ToStrGuid());
                AvailabilityDataReferenceDto adrd  = new AvailabilityDataReferenceDto("blub", "availguid".ToStrGuid());
                CalcAffordanceDto            cadto = new CalcAffordanceDto("blub", 1, cpd, "locname", "locguid".ToStrGuid(), true, new List <CalcDesireDto>(),
                                                                           1, 100, PermittedGender.All, true, 1, 1, 1, 1, "affcat", false, false, new List <CalcAffordanceVariableOpDto>(),
                                                                           new List <VariableRequirementDto>(), ActionAfterInterruption.GoBackToOld,
                                                                           "timelimitname", 100, true, "srctrait", "guid".ToStrGuid(), adrd, key, BodilyActivityLevel.Low);

                cadto.AddDeviceTuple("devname", "deviguid".ToStrGuid(), cpd, "calcloadtypename", "loadtypeguid".ToStrGuid(), 1, new TimeSpan(0, 1, 0), 1, 1);
                List <IHouseholdKey> aes = new List <IHouseholdKey>
                {
                    cadto
                };
                idl.SaveList(aes);
                var res = ael.Load(key);
                var s1  = JsonConvert.SerializeObject(aes, Formatting.Indented);
                var s2  = JsonConvert.SerializeObject(res, Formatting.Indented);
                s1.Should().Be(s2);
                wd.CleanUp();
            }
        }
Ejemplo n.º 3
0
        private List <CalcAffordance.DeviceEnergyProfileTuple> MakeDeviceEnergyProfileTuples([ItemNotNull][NotNull] List <CalcDevice> devices,
                                                                                             [NotNull] CalcAffordanceDto affordancedto)
        {
            List <CalcAffordance.DeviceEnergyProfileTuple> deviceEnergyProfiles =
                new List <CalcAffordance.DeviceEnergyProfileTuple>();

            foreach (DeviceEnergyProfileTupleDto dto in affordancedto.Energyprofiles)
            {
                StrGuid      devguid = dto.CalcDeviceGuid;
                CalcDevice   device  = devices.Single(x => x.Guid == devguid);
                CalcProfile  cp      = CalcDeviceFactory.MakeCalcProfile(dto.TimeProfile, _calcRepo.CalcParameters);
                CalcLoadType clt     = _loadTypeDictionary.GetLoadtypeByGuid(dto.CalcLoadTypeGuid);
                CalcAffordance.DeviceEnergyProfileTuple dep = new CalcAffordance.DeviceEnergyProfileTuple(device,
                                                                                                          cp, clt, dto.TimeOffset, _calcRepo.CalcParameters.InternalStepsize, dto.Multiplier, dto.Probability);
                deviceEnergyProfiles.Add(dep);
            }

            return(deviceEnergyProfiles);
        }
Ejemplo n.º 4
0
        private List <CalcAffordanceDto> GetCalcAffordancesAtLocation([NotNull] CalcLocationDto calcloc,
                                                                      [NotNull] List <AffordanceWithTimeLimit> affordancesAtLocation,
                                                                      TimeSpan internalStepSize, int timeStepsPerHour,
                                                                      [NotNull] TemperatureProfile temperatureProfile,
                                                                      [NotNull] CalcLoadTypeDtoDictionary ltdict,
                                                                      [NotNull] GeographicLocation geographicLocation, [NotNull] Random rnd,
                                                                      [NotNull][ItemNotNull] List <VacationTimeframe> vacationTimeframes,
                                                                      [NotNull] string holidayKey,
                                                                      [NotNull][ItemNotNull] ObservableCollection <DeviceAction> allDeviceActions,
                                                                      [NotNull]
                                                                      LocationDtoDict locDict,
                                                                      [NotNull] out List <DateTime> bridgeDays, [NotNull] HouseholdKey householdKey,
                                                                      [NotNull][ItemNotNull] List <CalcDeviceDto> deviceDtosAtLocation,
                                                                      [ItemNotNull][NotNull] List <DeviceCategoryDto> deviceCategoryDtos)
        {
            List <CalcAffordanceDto> createdAffordances = new List <CalcAffordanceDto>();

            bridgeDays = new List <DateTime>();
            foreach (AffordanceWithTimeLimit aff in affordancesAtLocation)
            {
                var affordanceName = CalcAffordanceFactory.FixAffordanceName(aff.Affordance.Name, _cp.CSVCharacter);
                if (aff.Affordance.PersonProfile == null)
                {
                    throw new DataIntegrityException("Person profile on " + aff.Affordance.PrettyName + " was null. Please fix.");
                }

                var cp1 = CalcDeviceDtoFactory.GetCalcProfileDto(aff.Affordance.PersonProfile);
                List <CalcDesireDto> calcDesires = MakeCalcDesires(timeStepsPerHour, aff, affordanceName);
                List <CalcAffordanceVariableOpDto> variableOps          = MakeVariableOps(calcloc, locDict, householdKey, aff);
                List <VariableRequirementDto>      variableRequirements =
                    MakeVariableRequirements(calcloc, locDict, householdKey, aff);

                MakeAffordanceTimelimit(temperatureProfile, geographicLocation, rnd, vacationTimeframes,
                                        holidayKey, bridgeDays, aff, out var availabilityDataReference);

                //make the affordance
                Logger.Debug("Converting the time limit to a bitarray for the affordance " + aff.Affordance.Name);
                //caff.IsBusyArray = tmparr;
                string timeLimitName = "";

                if (aff.TimeLimit != null)
                {
                    timeLimitName = aff.TimeLimit.Name;
                }

                var caff = new CalcAffordanceDto(affordanceName, aff.Affordance.IntID, cp1, calcloc.Name, calcloc.Guid,
                                                 aff.Affordance.RandomDesireResults, calcDesires, aff.Affordance.MinimumAge,
                                                 aff.Affordance.MaximumAge,
                                                 aff.Affordance.PermittedGender,
                                                 aff.Affordance.NeedsLight, (double)aff.Affordance.TimeStandardDeviation,
                                                 aff.Affordance.CarpetPlotColor.R, aff.Affordance.CarpetPlotColor.G, aff.Affordance.CarpetPlotColor.B,
                                                 aff.Affordance.AffCategory,
                                                 aff.Affordance.IsInterruptable, aff.Affordance.IsInterrupting, variableOps, variableRequirements,
                                                 aff.Affordance.ActionAfterInterruption, timeLimitName, aff.Weight,
                                                 aff.Affordance.RequireAllDesires, aff.SrcTraitName,
                                                 Guid.NewGuid().ToStrGuid(), availabilityDataReference, householdKey, aff.Affordance.BodilyActivityLevel);
                foreach (var devtup in aff.Affordance.AffordanceDevices)
                {
                    MakeAffordanceDevices(calcloc, internalStepSize, ltdict, allDeviceActions, aff, caff, devtup, deviceDtosAtLocation,
                                          deviceCategoryDtos);
                }

                MakeSubAffordances(caff, aff.Affordance, timeStepsPerHour, internalStepSize, calcloc,
                                   locDict, _cp.CSVCharacter, aff.Weight, aff.SrcTraitName, householdKey);
                createdAffordances.Add(caff);
            }
            return(createdAffordances);
        }
Ejemplo n.º 5
0
        private void MakeSubAffordances([NotNull] CalcAffordanceDto caff, [NotNull] Affordance aff, int timeStepsPerHour,
                                        TimeSpan internalStepSize, [NotNull] CalcLocationDto calcloc,
                                        [NotNull] LocationDtoDict locDict,
                                        [NotNull] string csvCharacter, int weight, [NotNull] string srcTrait,
                                        [NotNull] HouseholdKey householdKey)
        {
            // Subaffordanzen durchgehen
            foreach (var affsubaff in aff.SubAffordances)
            {
                var calcDesires = new List <CalcDesireDto>();
                if (affsubaff.SubAffordance == null)
                {
                    throw new LPGException("SubAffordance was null");
                }

                var name = CalcAffordanceFactory.FixAffordanceName(affsubaff.SubAffordance.Name, csvCharacter);
                // f�r alle desires calcdesires anlegen
                foreach (var subaffDesire in affsubaff.SubAffordance.SubAffordanceDesires)
                {
                    if (subaffDesire.Desire == null)
                    {
                        throw new LPGException("Desire was null");
                    }

                    var cd = new CalcDesireDto(name + " - " + subaffDesire.Name, subaffDesire.Desire.IntID, 0, 0,
                                               subaffDesire.SatisfactionValue, 0, timeStepsPerHour, subaffDesire.Desire.CriticalThreshold,
                                               null, srcTrait, "");
                    calcDesires.Add(cd);
                }

                var minutesperstep   = (decimal)internalStepSize.TotalMinutes;
                var delayTimeInSteps = (int)(affsubaff.DelayTime / minutesperstep);
                // variables
                var calcAffordanceVariableOps = new List <CalcAffordanceVariableOpDto>();
                foreach (var affVariableOp in affsubaff.SubAffordance.SubAffordanceVariableOps)
                {
                    CalcLocationDto loc = null;
                    if (affVariableOp.LocationMode == VariableLocationMode.OtherLocation)
                    {
                        if (affVariableOp.Location != null && locDict.SimulateLocation(affVariableOp.Location))
                        {
                            loc = locDict.GetDtoForLocation(affVariableOp.Location);
                        }
                    }
                    else
                    {
                        loc = calcloc;
                    }

                    if (loc != null && affVariableOp.Variable != null)
                    {
                        if (affVariableOp.Location == null)
                        {
                            throw new LPGException("Variable Location was null");
                        }
                        var variable =
                            _variableRepository.RegisterVariableIfNotRegistered(affVariableOp.Variable,
                                                                                affVariableOp.Location, householdKey, locDict);
                        calcAffordanceVariableOps.Add(new CalcAffordanceVariableOpDto(affVariableOp.Variable.Name,
                                                                                      affVariableOp.Value, loc.Name, loc.Guid, affVariableOp.VariableAction, affVariableOp.ExecutionTime,
                                                                                      variable.Guid));
                    }
                }

                // CalcSubaffordanz anlegen
                var affName = CalcAffordanceFactory.FixAffordanceName(aff.Name, csvCharacter);
                // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                var csubaff = new CalcSubAffordanceDto(name + " (" + affName + ")", affsubaff.IntID, calcloc.Name, calcloc.Guid,
                                                       calcDesires, affsubaff.SubAffordance.MinimumAge, affsubaff.SubAffordance.MaximumAge,
                                                       delayTimeInSteps, affsubaff.SubAffordance.PermittedGender, aff.AffCategory,
                                                       affsubaff.SubAffordance.IsInterruptable, affsubaff.SubAffordance.IsInterrupting,
                                                       calcAffordanceVariableOps, weight, srcTrait, Guid.NewGuid().ToStrGuid());
                caff.SubAffordance.Add(csubaff);
            }
        }
Ejemplo n.º 6
0
        private void MakeAffordanceDevices([NotNull] CalcLocationDto calcloc, TimeSpan internalStepSize,
                                           [NotNull] CalcLoadTypeDtoDictionary ltdict,
                                           [NotNull][ItemNotNull] ObservableCollection <DeviceAction> allDeviceActions,
                                           AffordanceWithTimeLimit aff, [NotNull] CalcAffordanceDto caff, [NotNull] AffordanceDevice devtup,
                                           [NotNull][ItemNotNull] List <CalcDeviceDto> devicesAtLocation,
                                           [ItemNotNull][NotNull] List <DeviceCategoryDto> deviceCategoryDtos)
        {
            if (devtup.Device == null)
            {
                throw new LPGException("Device was null");
            }
            // pick the device itself
            var pickedDevice = _picker.GetDeviceDtoForAffordance(devtup.Device, devicesAtLocation,
                                                                 calcloc.ID, allDeviceActions, deviceCategoryDtos);

            if (pickedDevice == null)
            {
                throw new DataIntegrityException(
                          "Affordance " + aff.Affordance.Name + " has broken devices. Please fix",
                          aff.Affordance);
            }

            // find the device in the calc devices
            var dev = devicesAtLocation.Single(calcDevice => calcDevice.Name == pickedDevice.Name);

            if (dev == null)
            {
                throw new DataIntegrityException(
                          "Affordance " + aff.Affordance.Name + " has broken devices. Please fix",
                          aff.Affordance);
            }

            if (devtup.Device == null)
            {
                throw new LPGException("Device was null");
            }

            switch (devtup.Device.AssignableDeviceType)
            {
            case AssignableDeviceType.Device:
            case AssignableDeviceType.DeviceCategory:
            {
                if (devtup.LoadType == null)
                {
                    throw new LPGException("No load type set");
                }
                if (ltdict.SimulateLoadtype(devtup.LoadType))
                {
                    if (devtup.TimeProfile == null)
                    {
                        throw new DataIntegrityException(
                                  "Affordance " + aff.Affordance.Name + " has broken time profiles. Please fix.",
                                  aff.Affordance);
                    }

                    var newprof =
                        CalcDeviceDtoFactory.GetCalcProfileDto(devtup.TimeProfile);
                    CalcLoadTypeDto loadtype = ltdict.GetLoadtypeDtoByLoadType(devtup.LoadType);
                    caff.AddDeviceTuple(dev.Name, dev.Guid, newprof, loadtype.Name, loadtype.Guid,
                                        devtup.TimeOffset, internalStepSize, 1, devtup.Probability);
                }
            }
            break;

            case AssignableDeviceType.DeviceAction:
            case AssignableDeviceType.DeviceActionGroup:
            {
                DeviceAction da;
                // if it's a device action group, then go back to the picker to select a specific
                // device action based on the available devices
                if (devtup.Device.AssignableDeviceType == AssignableDeviceType.DeviceActionGroup)
                {
                    da =
                        _picker.GetDeviceActionFromGroup(devtup.Device, devicesAtLocation,
                                                         allDeviceActions);
                }
                else
                {
                    da = (DeviceAction)devtup.Device;
                }
                if (da == null)
                {
                    throw new LPGException("Device action was null");
                }

                if (da.Profiles.Count == 0)
                {
                    throw new DataIntegrityException(
                              "The device action " + da.Name + " has no time profiles. Please fix", da);
                }

                foreach (var profile in da.Profiles)
                {
                    if (profile.VLoadType == null)
                    {
                        throw new LPGException("Profile was null");
                    }

                    if (ltdict.SimulateLoadtype(profile.VLoadType))
                    {
                        if (profile.Timeprofile == null)
                        {
                            throw new DataIntegrityException(
                                      "The device action " + da.Name + " has broken time profiles. Please fix.",
                                      da);
                        }

                        if (profile.VLoadType == null)
                        {
                            throw new DataIntegrityException(
                                      "The device action " + da.Name + " has broken load types. Please fix.", da);
                        }

                        var newprof =
                            CalcDeviceDtoFactory.GetCalcProfileDto(profile.Timeprofile);
                        CalcLoadTypeDto loadType = ltdict.GetLoadtypeDtoByLoadType(profile.VLoadType);
                        caff.AddDeviceTuple(dev.Name, dev.Guid, newprof, loadType.Name, loadType.Guid,
                                            profile.TimeOffset + devtup.TimeOffset, internalStepSize, profile.Multiplier,
                                            devtup.Probability);
                    }
                }
            }
            break;

            default:
                throw new LPGException(
                          "Missing an AssignableDeviceType at GetCalcAffordancesAtLocation! Please report to the programmer.");
            }
        }