public void CalcAffordanceVariableTestSet()
        {
            var deviceCategoryGuid = Guid.NewGuid().ToStrGuid();
            //var r = new Random(0);
            //var nr = new NormalRandom(0, 0.1, r);
            const int stepcount = 150;

            Config.IsInUnitTesting = true;
            DateTime startdate = new DateTime(2018, 1, 1);
            DateTime enddate   = startdate.AddMinutes(stepcount);
            //_calcParameters.InitializeTimeSteps(startdate, enddate, new TimeSpan(0, 1, 0), 3, false);
            CalcParameters calcParameters =
                CalcParametersFactory.MakeGoodDefaults().SetStartDate(startdate).SetEndDate(enddate);
            var timeStep = new TimeSpan(0, 1, 0);
            var cp       = new CalcProfile("profile", Guid.NewGuid().ToStrGuid(), timeStep, ProfileType.Absolute, "blub");

            cp.AddNewTimepoint(new TimeSpan(0), 100);
            cp.AddNewTimepoint(new TimeSpan(0, 10, 0), 0);
            cp.ConvertToTimesteps();
            var variables    = new List <CalcAffordanceVariableOp>();
            var variableReqs = new List <VariableRequirement>();
            var loc          = new CalcLocation("loc", Guid.NewGuid().ToStrGuid());
            CalcVariableRepository calcVariableRepository = new CalcVariableRepository();
            var          variableGuid = Guid.NewGuid().ToStrGuid();
            HouseholdKey key          = new HouseholdKey("hh1");
            CalcVariable cv           = new CalcVariable("varname", variableGuid, 0, loc.Name, loc.Guid, key);

            calcVariableRepository.RegisterVariable(cv);
            variables.Add(new CalcAffordanceVariableOp(cv.Name, 1, loc, VariableAction.SetTo,
                                                       VariableExecutionTime.Beginning, variableGuid));
            BitArray     isBusy = new BitArray(100, false);
            Random       rnd    = new Random();
            NormalRandom nr     = new NormalRandom(0, 1, rnd);

            using CalcRepo calcRepo = new CalcRepo(calcParameters: calcParameters, odap: new Mock <IOnlineDeviceActivationProcessor>().Object, normalRandom: nr, rnd: rnd);
            var aff = new CalcAffordance("bla", cp, loc, false, new List <CalcDesire>(), 0, 99,
                                         PermittedGender.All, false, 0.1, new ColorRGB(0, 0, 0), "bla", false, false, variables, variableReqs,
                                         ActionAfterInterruption.GoBackToOld, "bla", 100, false, "", Guid.NewGuid().ToStrGuid(),
                                         calcVariableRepository,
                                         new List <CalcAffordance.DeviceEnergyProfileTuple>(), isBusy, BodilyActivityLevel.Low, calcRepo);
            var lt       = new CalcLoadType("load", "unit1", "unit2", 1, true, Guid.NewGuid().ToStrGuid());
            var cdl      = new CalcDeviceLoad("cdl", 1, lt, 1, 0.1);
            var devloads = new List <CalcDeviceLoad> {
                cdl
            };
            CalcDeviceDto cdd = new CalcDeviceDto("device", deviceCategoryGuid, key,
                                                  OefcDeviceType.Device, "category", string.Empty,
                                                  Guid.NewGuid().ToStrGuid(), loc.Guid, loc.Name);
            var cd = new CalcDevice(devloads, loc,
                                    cdd, calcRepo);

            //loc.Variables.Add("Variable1", 0);
            aff.AddDeviceTuple(cd, cp, lt, 0, timeStep, 10, 1);
            TimeStep ts = new TimeStep(0, 0, false);

            aff.IsBusy(ts, loc, "name");
            //var variableOperator = new VariableOperator();
            aff.Activate(ts, "blub", loc, out var _);
            calcVariableRepository.GetValueByGuid(variableGuid).Should().Be(1);
        }
 private static void RegisterAllDtoVariables([NotNull] CalcVariableDtoFactory cvrdto, [NotNull] CalcVariableRepository variableRepository)
 {
     foreach (var v in cvrdto.GetAllVariableDtos())
     {
         variableRepository.RegisterVariable(new CalcVariable(v.Name, v.Guid, v.Value, v.LocationName,
                                                              v.LocationGuid, v.HouseholdKey));
     }
 }
        public void VariableLogfileTests1()
        {
            using (WorkingDir wd = new WorkingDir(Utili.GetCurrentMethodAndClass()))
            {
                wd.InputDataLogger.AddSaver(new VariableEntryLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new HouseholdKeyLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new ResultFileEntryLogger(wd.SqlResultLoggingService));
                CalcParameters calcParameters = CalcParametersFactory.MakeGoodDefaults().EnableShowSettlingPeriod();
                using (var fft = new FileFactoryAndTracker(wd.WorkingDirectory, "blub", wd.InputDataLogger))
                {
                    HouseholdKey key = new HouseholdKey("hh1");
                    fft.RegisterGeneralHouse();
                    fft.RegisterHousehold(key, "householdname", HouseholdKeyType.Household, "desc", null, null);
                    DateStampCreator dsc = new DateStampCreator(calcParameters);
                    using (IOnlineLoggingData old = new OnlineLoggingData(dsc, wd.InputDataLogger, calcParameters))
                    {
                        CalcVariableRepository cvr = new CalcVariableRepository();
                        //using (LogFile lf = new LogFile(calcParameters,fft, old,wd.SqlResultLoggingService, true))
                        CalcLocation cloc = new CalcLocation("loc1", Guid.NewGuid().ToStrGuid());
                        CalcVariable cv   = new CalcVariable("mycalcvar", Guid.NewGuid().ToStrGuid(),
                                                             0, cloc.Name, cloc.Guid, key);
                        cvr.RegisterVariable(cv);
                        TimeStep ts = new TimeStep(0, calcParameters);
                        old.AddVariableStatus(new CalcVariableEntry(cv.Name, cv.Guid, 1, cv.LocationName, cv.LocationGuid,
                                                                    cv.HouseholdKey, ts));
                        old.AddVariableStatus(new CalcVariableEntry(cv.Name, cv.Guid, 2, cv.LocationName, cv.LocationGuid,
                                                                    cv.HouseholdKey, ts));
                        old.AddVariableStatus(new CalcVariableEntry(cv.Name, cv.Guid, 3, cv.LocationName, cv.LocationGuid,
                                                                    cv.HouseholdKey, ts));
                        old.FinalSaveToDatabase();
                    }
                    VariableEntryLogger vel = new VariableEntryLogger(wd.SqlResultLoggingService);
                    var varEntries          = vel.Read(key);
                    foreach (CalcVariableEntry entry in varEntries)
                    {
                        Logger.Info(entry.Value.ToString(CultureInfo.InvariantCulture));
                    }
                    varEntries[0].Value.Should().Be(1);
                    varEntries[1].Value.Should().Be(2);
                    varEntries[2].Value.Should().Be(3);
                }

                /*old.
                 * cloc.Add();
                 * cloc[0].Variables.Add("trig1", 1.0);
                 * tlf.WriteLine(0, cloc);
                 * cloc[0].Variables["trig1"] = 2.0;
                 * tlf.WriteLine(1, cloc);
                 * lf.Close(null);*/
                wd.CleanUp();
            }
        }
示例#4
0
        public void CheckResultingProfile()
        {
            using var wd = new WorkingDir(Utili.GetCurrentMethodAndClass());
            wd.InputDataLogger.AddSaver(new ColumnEntryLogger(wd.SqlResultLoggingService));
            wd.InputDataLogger.AddSaver(new ResultFileEntryLogger(wd.SqlResultLoggingService));
            wd.InputDataLogger.AddSaver(new HouseholdKeyLogger(wd.SqlResultLoggingService));
            DateTime       startdate             = new DateTime(2018, 1, 1);
            DateTime       enddate               = startdate.AddMinutes(100);
            CalcParameters calculationParameters = CalcParametersFactory.MakeGoodDefaults().SetStartDate(startdate).SetEndDate(enddate).EnableShowSettlingPeriod();
            var            profile               = new CalcProfile("profile", Guid.NewGuid().ToStrGuid(), new TimeSpan(0, 1, 0), ProfileType.Relative, "blub");

            profile.AddNewTimepoint(new TimeSpan(0), 0.01);
            profile.AddNewTimepoint(new TimeSpan(1, 0, 0), 0.01);
            profile.ConvertToTimesteps();
            var cloadtype = new CalcLoadType("loadtype", "power", "sum", 1, true, Guid.NewGuid().ToStrGuid());
            var loads     = new List <CalcDeviceLoad>();
            var cdl       = new CalcDeviceLoad("cdevload", 100, cloadtype, 100, 0.1);

            loads.Add(cdl);
            var          r   = new Random(5);
            HouseholdKey key = new HouseholdKey("hh1");
            var          nr  = new NormalRandom(0, 1, r);

            using var fft = new FileFactoryAndTracker(wd.WorkingDirectory, "householdname", wd.InputDataLogger);
            fft.RegisterHousehold(key, "hh1", HouseholdKeyType.Household, "desc", null, null);
            fft.RegisterHousehold(Constants.GeneralHouseholdKey, "general", HouseholdKeyType.General, "desc", null, null);

            //SqlResultLoggingService srls = new SqlResultLoggingService(wd.WorkingDirectory);
            DateStampCreator dsc = new DateStampCreator(calculationParameters);

            using OnlineLoggingData old = new OnlineLoggingData(dsc, wd.InputDataLogger, calculationParameters);
            var odap = new OnlineDeviceActivationProcessor(old, calculationParameters, fft);

            using CalcRepo calcRepo = new CalcRepo(odap: odap, normalRandom: nr, calcParameters: calculationParameters);
            var location = new CalcLocation("calcloc", Guid.NewGuid().ToStrGuid());
            CalcVariableRepository crv = new CalcVariableRepository();
            var          variableGuid  = Guid.NewGuid().ToStrGuid();
            CalcVariable cv            = new CalcVariable("varname", variableGuid, 0, location.Name, location.Guid, key);

            crv.RegisterVariable(cv);
            VariableRequirement vreq = new VariableRequirement(cv.Name, 0, location.Name, location.Guid,
                                                               VariableCondition.Equal, crv, variableGuid);
            List <VariableRequirement> requirements = new List <VariableRequirement>
            {
                vreq
            };
            var           deviceCategoryGuid = Guid.NewGuid().ToStrGuid();
            CalcDeviceDto cdd = new CalcDeviceDto("autodevnamename", deviceCategoryGuid, key,
                                                  OefcDeviceType.AutonomousDevice, "device category", "", Guid.NewGuid().ToStrGuid(),
                                                  location.Guid, location.Name);
            var cad = new CalcAutoDev(profile, cloadtype, loads,
                                      0.8, 1, location,
                                      requirements, cdd, calcRepo);

            for (var i = 0; i < 100; i++)
            {
                TimeStep ts = new TimeStep(i, calculationParameters);
                if (!cad.IsBusyDuringTimespan(ts, 1, 0.7, cloadtype))
                {
                    cad.Activate(ts);
                }
                var rows = odap.ProcessOneTimestep(ts);
                foreach (var energyFileRow in rows)
                {
                    foreach (var energyEntry in energyFileRow.EnergyEntries)
                    {
                        Logger.Info(energyEntry.ToString(CultureInfo.CurrentCulture));
                    }
                }
            }
        }