public void TestMultiplePickingShouldAlwaysGiveSameResult()
        {
            using (DatabaseSetup db = new DatabaseSetup(Utili.GetCurrentMethodAndClass()))
            {
                Simulator sim = new Simulator(db.ConnectionString);
                for (int i = 0; i < 100; i++)
                {
                    Random r = new Random(5);
                    DeviceCategoryPicker     dcp     = new DeviceCategoryPicker(r, null);
                    Location                 loc     = new Location("bla", -1, string.Empty, Guid.NewGuid().ToStrGuid());
                    List <IAssignableDevice> devices = new List <IAssignableDevice>();
                    devices.Add(sim.RealDevices[0]);
                    dcp.GetOrPickDevice(sim.DeviceActionGroups[1], loc, EnergyIntensityType.Random, devices, sim.DeviceActions.It);
                }
            }
            //ObservableCollection<RealDevice> allDevices = new ObservableCollection<RealDevice>();
            //DeviceCategory dc = new DeviceCategory("dc", -1, "bla", false, allDevices, Guid.NewGuid().ToStrGuid());
            //RealDevice rd = new RealDevice("bla", 0, string.Empty, dc, "desc", false, true, string.Empty, Guid.NewGuid().ToStrGuid(), -1);
            //allDevices.Add(rd);

            //List<IAssignableDevice> devices = new List<IAssignableDevice>
            //{
            //    rd
            //};
            //ObservableCollection<DeviceAction> deviceActions = new ObservableCollection<DeviceAction>();
            //Logger.Info("put in a rd, get back the same rd");
            //RealDevice result = dcp.GetOrPickDevice(rd, loc, EnergyIntensityType.EnergyIntensive, devices, deviceActions);
            //(result).Should().Be(rd);
            //// put in an dc with one rd, get the rd 1
            //Logger.Info("put in a rd, get back null, since the rd is already there as hhdevloc");
            //RealDevice result2 = dcp.GetOrPickDevice(dc, loc, EnergyIntensityType.EnergyIntensive, devices,
            //    deviceActions);
            //(result2).Should().Be(null);
        }
        public void DeviceCategoryPickerTestRealDevices()
        {
            Random               r   = new Random(5);
            DeviceSelection      ds  = new DeviceSelection("ds", 1, string.Empty, string.Empty, Guid.NewGuid().ToStrGuid());
            DeviceCategoryPicker dcp = new DeviceCategoryPicker(r, ds);
            ObservableCollection <RealDevice> allDevices = new ObservableCollection <RealDevice>();
            DeviceCategory dc = new DeviceCategory("dc", -1, "bla", false, allDevices, Guid.NewGuid().ToStrGuid());
            RealDevice     rd = new RealDevice("bla", 0, string.Empty, dc, "desc", false, true, string.Empty, Guid.NewGuid().ToStrGuid(), -1);

            allDevices.Add(rd);
            Location loc = new Location("bla", -1, string.Empty, Guid.NewGuid().ToStrGuid());
            List <IAssignableDevice> devices = new List <IAssignableDevice>
            {
                rd
            };
            ObservableCollection <DeviceAction> deviceActions = new ObservableCollection <DeviceAction>();

            Logger.Info("put in a rd, get back the same rd");
            RealDevice result = dcp.GetOrPickDevice(rd, loc, EnergyIntensityType.EnergyIntensive, devices, deviceActions);

            rd.Should().Be(result);
            // put in an dc with one rd, get the rd 1
            Logger.Info("put in a rd, get back null, since the rd is already there as hhdevloc");
            RealDevice result2 = dcp.GetOrPickDevice(dc, loc, EnergyIntensityType.EnergyIntensive, devices,
                                                     deviceActions);

            result2.Should().BeNull();
        }
Ejemplo n.º 3
0
        public void MakeCalcLocationsTest()
        {
            var locations = new List <Location>();
            var loc       = new Location("loc", 1, string.Empty, Guid.NewGuid().ToStrGuid());

            locations.Add(loc);
            Random r = new Random(1);
            DeviceCategoryPicker picker = new DeviceCategoryPicker(r, null);
            //var cp = new CalcFactoryParameters(picker);
            //var dict =new Dictionary<CalcLocation, List<IAssignableDevice>>();
            var deviceActions = new ObservableCollection <DeviceAction>();
            //var locdict = new Dictionary<Location, CalcLocation>();
            CalcParameters cp = CalcParametersFactory.MakeGoodDefaults();
            //var mock = new Mock<IOnlineDeviceActivationProcessor>();
            //var iodap = mock.Object;
            var locationDtoDict         = new CalcLoadTypeDtoDictionary(new Dictionary <VLoadType, CalcLoadTypeDto>());
            var ltDict                  = new CalcLoadTypeDictionary(new Dictionary <CalcLoadTypeDto, CalcLoadType>());
            CalcLocationDtoFactory cldt = new CalcLocationDtoFactory(cp, picker, locationDtoDict);
            Dictionary <CalcLocationDto, List <IAssignableDevice> > deviceLocationDict = new Dictionary <CalcLocationDto, List <IAssignableDevice> >();
            LocationDtoDict          calclocdict = new LocationDtoDict();
            List <DeviceCategoryDto> devcat      = new List <DeviceCategoryDto>();

            using CalcRepo calcRepo = new CalcRepo();
            //devcat.Add(new DeviceCategoryDto(dc.FullPath, Guid.NewGuid().ToStrGuid()));
            var locdtos = cldt.MakeCalcLocations(locations,
                                                 new HouseholdKey("hh1"),
                                                 EnergyIntensityType.EnergyIntensive,
                                                 deviceLocationDict,
                                                 deviceActions,
                                                 calclocdict,
                                                 devcat);

            locdtos.Count.Should().Be(1);
            locdtos[0].Name.Should().Be(loc.Name);
            CalcLocationFactory clf = new CalcLocationFactory(ltDict, calcRepo);
            //"HH1", EnergyIntensityType.EnergySaving, dict,deviceActions,
            DtoCalcLocationDict dcl = new DtoCalcLocationDict();
            var calclocs            = clf.MakeCalcLocations(locdtos, dcl, calcRepo);

            calclocs.Count.Should().Be(1);
            calclocs[0].Name.Should().Be(loc.Name);
            calclocs[0].Guid.Should().Be(locdtos[0].Guid);
        }
        public void DeviceCategoryPickerDeviceActionGroupAutoDev()
        {
            Random               r      = new Random(2);
            DeviceSelection      ds     = new DeviceSelection("ds", 1, string.Empty, string.Empty, Guid.NewGuid().ToStrGuid());
            DeviceCategoryPicker picker = new DeviceCategoryPicker(r, ds);
            // device stuff
            ObservableCollection <RealDevice> allDevices = new ObservableCollection <RealDevice>();
            DeviceCategory    dc  = new DeviceCategory("dc", -1, "bla", false, allDevices, Guid.NewGuid().ToStrGuid());
            RealDevice        rd1 = new RealDevice("device1", 0, string.Empty, dc, "desc", false, true, string.Empty, Guid.NewGuid().ToStrGuid(), -1);
            RealDevice        rd2 = new RealDevice("device2", 0, string.Empty, dc, "desc", false, true, string.Empty, Guid.NewGuid().ToStrGuid(), -1);
            DeviceActionGroup dag = new DeviceActionGroup("Dag1", string.Empty, "blub", Guid.NewGuid().ToStrGuid(), -1);
            DeviceAction      da1 = new DeviceAction("da1", -1, "blub", string.Empty, dag, rd1, Guid.NewGuid().ToStrGuid());
            DeviceAction      da2 = new DeviceAction("da2", -1, "blub", string.Empty, dag, rd2, Guid.NewGuid().ToStrGuid());
            ObservableCollection <DeviceAction> deviceActions = new ObservableCollection <DeviceAction>
            {
                da1,
                da2
            };

            allDevices.Add(rd1);
            List <IAssignableDevice> otherDevicesAtLocation = new List <IAssignableDevice>
            {
                dag
            };

            DeviceAction pickedDeviceAction = picker.GetAutoDeviceActionFromGroup(dag, otherDevicesAtLocation,
                                                                                  EnergyIntensityType.Random, deviceActions, 5);

            Logger.Info("Device Action 1 " + pickedDeviceAction);
            for (int i = 0; i < 50; i++)
            {
                DeviceAction deviceAction2 = picker.GetAutoDeviceActionFromGroup(dag, otherDevicesAtLocation,
                                                                                 EnergyIntensityType.Random, deviceActions, 5);
                Logger.Info("Device Action  " + i + " " + deviceAction2);
                pickedDeviceAction.Should().Be(deviceAction2);
            }
        }
        public void RunMakeCalcTest()
        {
            //TODO: fix the container registering
            var builder = new ContainerBuilder();

            using (var wd = new WorkingDir(Utili.GetCurrentMethodAndClass())) {
                var path        = wd.WorkingDirectory;
                var inputlogger = wd.InputDataLogger;
                wd.InputDataLogger.AddSaver(new CalcPersonDtoLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new CalcAffordanceDtoLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new CalcVariableDtoLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new BridgeDayEntryLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new HouseholdDtoLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new HouseholdKeyLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new CalcSiteDtoLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new CalcTransportationDeviceDtoLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new CalcTravelRouteDtoLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new ColumnEntryLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new TransportationStatusLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new TransportationEventLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new ResultFileEntryLogger(wd.SqlResultLoggingService));

                using (var db = new DatabaseSetup(Utili.GetCurrentMethodAndClass())) {
                    var sim = new Simulator(db.ConnectionString);

                    var mhh       = sim.ModularHouseholds[0];
                    var r         = new Random(1);
                    var dcp       = new DeviceCategoryPicker(r, null);
                    var ltdtoDict = CalcLoadTypeDtoFactory.MakeLoadTypes(sim.LoadTypes.It, new TimeSpan(0, 1, 0),
                                                                         LoadTypePriority.RecommendedForHouseholds);
                    var ltdict = CalcLoadTypeFactory.MakeLoadTypes(ltdtoDict);
                    //var picker = new DeviceCategoryPicker(r,null);
                    var nr         = new NormalRandom(0, 0.1, r);
                    var parameters = CalcParametersFactory.MakeGoodDefaults().SetStartDate(2018, 1, 1)
                                     .SetEndDate(new DateTime(2018, 1, 1, 2, 0, 0)).SetSettlingDays(0).EnableShowSettlingPeriod();
                    builder.Register(x => parameters).As <CalcParameters>().SingleInstance();
                    builder.Register(x => new DateStampCreator(parameters)).As <DateStampCreator>().SingleInstance();
                    builder.Register(c => ltdict).As <CalcLoadTypeDictionary>().SingleInstance();
                    builder.Register(c => ltdtoDict).As <CalcLoadTypeDtoDictionary>().SingleInstance();
                    builder.Register(c => new NormalRandom(0, 1, r)).As <NormalRandom>().SingleInstance();
                    builder.Register(c => new FileFactoryAndTracker(path, mhh.Name, inputlogger))
                    .As <FileFactoryAndTracker>().SingleInstance();
                    builder.Register(c => new SqlResultLoggingService(path)).As <SqlResultLoggingService>()
                    .SingleInstance();
                    builder.Register(c => inputlogger).As <IInputDataLogger>().As <InputDataLogger>().SingleInstance();

                    builder.Register(c => new OnlineLoggingData(c.Resolve <DateStampCreator>(),
                                                                c.Resolve <IInputDataLogger>(), c.Resolve <CalcParameters>())).As <OnlineLoggingData>().As <IOnlineLoggingData>()
                    .SingleInstance();

                    builder.Register(c => new LogFile(parameters, c.Resolve <FileFactoryAndTracker>())).As <ILogFile>()
                    .SingleInstance();
                    builder.RegisterType <OnlineDeviceActivationProcessor>().As <IOnlineDeviceActivationProcessor>()
                    .SingleInstance();
                    builder.RegisterType <CalcModularHouseholdFactory>().As <CalcModularHouseholdFactory>()
                    .SingleInstance();
                    builder.Register(x => new DeviceCategoryPicker(r, null)).As <IDeviceCategoryPicker>()
                    .SingleInstance();

                    builder.Register(x => r).As <Random>().SingleInstance();
                    builder.RegisterType <CalcDeviceFactory>().As <CalcDeviceFactory>().SingleInstance();
                    builder.RegisterType <CalcDeviceDtoFactory>().As <CalcDeviceDtoFactory>().SingleInstance();
                    builder.RegisterType <CalcLocationFactory>().As <CalcLocationFactory>().SingleInstance();
                    builder.RegisterType <CalcLocationDtoFactory>().As <CalcLocationDtoFactory>().SingleInstance();
                    builder.RegisterType <CalcPersonFactory>().As <CalcPersonFactory>().SingleInstance();
                    builder.RegisterType <CalcPersonDtoFactory>().As <CalcPersonDtoFactory>().SingleInstance();
                    builder.RegisterType <CalcAffordanceFactory>().As <CalcAffordanceFactory>().SingleInstance();
                    builder.RegisterType <CalcAffordanceDtoFactory>().As <CalcAffordanceDtoFactory>().SingleInstance();
                    builder.RegisterType <CalcTransportationFactory>().As <CalcTransportationFactory>().SingleInstance();
                    builder.RegisterType <CalcModularHouseholdDtoFactory>().As <CalcModularHouseholdDtoFactory>()
                    .SingleInstance();
                    builder.RegisterType <CalcVariableDtoFactory>().As <CalcVariableDtoFactory>().SingleInstance();
                    builder.RegisterType <CalcTransportationDtoFactory>().As <CalcTransportationDtoFactory>()
                    .SingleInstance();
                    builder.RegisterType <VacationDtoFactory>().As <VacationDtoFactory>().SingleInstance();
                    builder.RegisterType <AvailabilityDtoRepository>().As <AvailabilityDtoRepository>().SingleInstance();
                    builder.RegisterType <CalcVariableRepository>().As <CalcVariableRepository>().SingleInstance();
                    builder.RegisterType <CalcRepo>().As <CalcRepo>().SingleInstance();
                    var container = builder.Build();
                    using (var scope = container.BeginLifetimeScope()) {
                        var hhdtofac = scope.Resolve <CalcModularHouseholdDtoFactory>();

                        var tds = sim.TransportationDeviceSets[0];
                        tds.SaveToDB();
                        var trs = sim.TravelRouteSets[0];
                        trs.SaveToDB();
                        var css = sim.ChargingStationSets[0];
                        css.SaveToDB();

                        /*Site home = sim.Sites.CreateNewItem(sim.ConnectionString);
                         * home.Name = "home";
                         * home.SaveToDB();
                         * Site outside = sim.Sites.CreateNewItem(sim.ConnectionString);
                         * outside.Name = "outside";
                         * outside.SaveToDB();
                         * home.AddLocation(sim.Locations.SafeFindByName("Living room", FindMode.IgnoreCase));
                         * home.AddLocation(sim.Locations.SafeFindByName("Kitchen", FindMode.IgnoreCase));
                         * home.AddLocation(sim.Locations.SafeFindByName("Bath", FindMode.IgnoreCase));
                         * home.AddLocation(sim.Locations.SafeFindByName("Bedroom", FindMode.IgnoreCase));
                         * home.AddLocation(sim.Locations.SafeFindByName("Children's room", FindMode.IgnoreCase));
                         * outside.AddLocation(sim.Locations.SafeFindByName("Dance Studio", FindMode.IgnoreCase));
                         * outside.AddLocation(sim.Locations.SafeFindByName("Supermarket", FindMode.IgnoreCase));
                         * outside.AddLocation(sim.Locations.SafeFindByName("Garden", FindMode.IgnoreCase));
                         * outside.AddLocation(sim.Locations.SafeFindByName("Museum", FindMode.IgnoreCase));
                         * outside.AddLocation(sim.Locations.SafeFindByName("School", FindMode.IgnoreCase));
                         * outside.AddLocation(sim.Locations.SafeFindByName("Food Market", FindMode.IgnoreCase));
                         * outside.AddLocation(sim.Locations.SafeFindByName("Sidewalk", FindMode.IgnoreCase));
                         * outside.AddLocation(sim.Locations.SafeFindByName("Office Workplace 1", FindMode.IgnoreCase));
                         * TravelRoute tr = new TravelRoute(null, db.ConnectionString, "tr1", "desc", home, outside);
                         * tr.SaveToDB();
                         * trs.AddRoute(tr);*/
                        var fft = scope.Resolve <FileFactoryAndTracker>();
                        fft.RegisterGeneralHouse();
                        var ctf = scope.Resolve <CalcTransportationFactory>();
                        //LogFile lf = new LogFile(wd.WorkingDirectory, "hh1", true);
                        //lf.RegisterKey("hh1", "hh1-prettyname");
                        var dtohh = hhdtofac.MakeCalcModularHouseholdDto(sim, mhh,
                                                                         sim.TemperatureProfiles[0], new HouseholdKey("hh1"), sim.GeographicLocations[0],
                                                                         out var dtolocs, tds, trs,
                                                                         EnergyIntensityType.Random, css);
                        var cvdto = scope.Resolve <CalcVariableDtoFactory>();
                        var cvr   = scope.Resolve <CalcVariableRepository>();
                        foreach (var v in cvdto.VariableDtos.Values)
                        {
                            cvr.RegisterVariable(new CalcVariable(v.Name, v.Guid, v.Value,
                                                                  v.LocationName, v.LocationGuid, v.HouseholdKey));
                        }

                        var cmhf = scope.Resolve <CalcModularHouseholdFactory>();
                        //CalcTransportationDtoFactory dtoFactory = new CalcTransportationDtoFactory(ltdtoDict);
                        //dtoFactory.MakeTransportationDtos(sim, sim.ModularHouseholds[0], tds, trs, out var sites,out var transportationDevices, out var routes, dtohh.LocationDtos, dtohh.HouseholdKey);
                        var calcRepo = scope.Resolve <CalcRepo>();
                        var chh      = cmhf.MakeCalcModularHousehold(dtohh, out var dtoCalcLocationDict, null, null,
                                                                     calcRepo);
                        //ctf.MakeTransportation(dtohh,dtoCalcLocationDict,chh);
                        if (chh.TransportationHandler == null)
                        {
                            throw new LPGException("no transportation handler");
                        }

                        var          src        = chh.TransportationHandler.CalcSites[0].Locations[0];
                        var          dst        = chh.TransportationHandler.CalcSites[1].Locations[0];
                        const string personname = "personname";
                        var          ts         = new TimeStep(1, parameters);
                        dst.Affordances[0].IsBusy(ts, src, personname, false);
                        dst.Affordances[0].Activate(ts, personname, src, out var personTimeProfile);
                        fft.Dispose();
                    }

                    db.Cleanup();
                }

                wd.CleanUp();
            }
        }
Ejemplo n.º 6
0
        public void MakeCalcLocationsTestWithDevice()
        {
            var            builder        = new ContainerBuilder();
            var            r              = new Random(1);
            CalcParameters calcParameters = CalcParametersFactory.MakeGoodDefaults().SetStartDate(2018, 1, 1)
                                            .SetEndDate(new DateTime(2018, 1, 1, 2, 0, 0)).SetSettlingDays(0).EnableShowSettlingPeriod();
            var picker = new DeviceCategoryPicker(r, null);

            builder.Register(x => picker).As <DeviceCategoryPicker>().SingleInstance();
            //var nr = new NormalRandom(0, 1, r);
            var locations = new List <Location>();
            var loc       = new Location("loc", 1, string.Empty, Guid.NewGuid().ToStrGuid());

            locations.Add(loc);
            var devices = new ObservableCollection <RealDevice>();
            var dc      = new DeviceCategory("dc", -1, string.Empty, false, devices, Guid.NewGuid().ToStrGuid(), 1, true);
            List <DeviceCategoryDto> devcat = new List <DeviceCategoryDto> {
                new DeviceCategoryDto(dc.FullPath, Guid.NewGuid().ToStrGuid())
            };
            var rd = new RealDevice("rd", 1, string.Empty, dc, string.Empty, false, false, string.Empty, Guid.NewGuid().ToStrGuid(), 1);

            loc.AddDevice(rd, false);
            var deviceLocationDict = new Dictionary <CalcLocationDto, List <IAssignableDevice> >();
            var allDeviceActions   = new ObservableCollection <DeviceAction>();

            //CalcLoadTypeDictionary cltd = CalcLoadTypeFactory.MakeLoadTypes(new ObservableCollection<VLoadType>(),calcParameters.InternalStepsize, calcParameters.LoadTypePriority);
            builder.Register(x => new DateStampCreator(x.Resolve <CalcParameters>())).As <DateStampCreator>().SingleInstance();
            builder.Register(x => new CalcLoadTypeDtoDictionary(new Dictionary <VLoadType, CalcLoadTypeDto>())).As <CalcLoadTypeDtoDictionary>()
            .SingleInstance();
            builder.Register(x => new CalcLoadTypeDictionary(new Dictionary <CalcLoadTypeDto, CalcLoadType>())).As <CalcLoadTypeDictionary>()
            .SingleInstance();
            builder.Register(x => new DeviceCategoryPicker(r, null)).As <IDeviceCategoryPicker>().SingleInstance();
            builder.Register(_ => calcParameters).As <CalcParameters>().SingleInstance();
            //builder.RegisterType<CalcLocationFactory>().As<CalcLocationFactory>().SingleInstance();
            Mock <IOnlineDeviceActivationProcessor> odapmock = new Mock <IOnlineDeviceActivationProcessor>();

            builder.Register(x => odapmock.Object).As <IOnlineDeviceActivationProcessor>().SingleInstance();
            builder.Register(x => r).As <Random>().SingleInstance();
            builder.RegisterType <CalcDeviceFactory>().As <CalcDeviceFactory>().SingleInstance();
            builder.RegisterType <CalcLocationFactory>().As <CalcLocationFactory>().SingleInstance();
            builder.RegisterType <CalcLocationDtoFactory>().As <CalcLocationDtoFactory>();
            builder.RegisterType <CalcRepo>().As <CalcRepo>().SingleInstance();
            var container = builder.Build();

            using var scope = container.BeginLifetimeScope();
            var             calcRepo    = scope.Resolve <CalcRepo>();
            var             cldt        = scope.Resolve <CalcLocationDtoFactory>();
            LocationDtoDict calclocdict = new LocationDtoDict();
            var             locdtos     = cldt.MakeCalcLocations(locations,
                                                                 new HouseholdKey("HH1"),
                                                                 EnergyIntensityType.EnergyIntensive,
                                                                 deviceLocationDict,
                                                                 allDeviceActions,
                                                                 calclocdict,
                                                                 devcat);

            //CalcDeviceFactory cdf = scope.Resolve<CalcDeviceFactory>();
            CalcLocationFactory clf = scope.Resolve <CalcLocationFactory>();
            DtoCalcLocationDict dtl = new DtoCalcLocationDict();
            var clocations          = clf.MakeCalcLocations(locdtos, dtl, calcRepo);

            clocations.Count.Should().Be(1);
            clocations[0].LightDevices.Count.Should().Be(2);
        }