예제 #1
0
        public void CreateSettlementFromPreview([NotNull] Simulator sim, [NotNull] SettlementTemplate template)
        {
            if (string.IsNullOrEmpty(template.NewName))
            {
                Logger.Error("The new name for the settlement can not be empty.");
                return;
            }
            var sett = sim.Settlements.CreateNewItem(sim.ConnectionString);

            sett.Name = GetSettlementName(sim, template);
            sett.EnergyIntensityType = EnergyIntensityType.AsOriginal;
            sett.Source = "Generated by " + template.Name + " at " + DateTime.Now.ToShortDateString() + " " +
                          DateTime.Now.ToShortTimeString();
            sett.CreationType = CreationType.TemplateCreated;
            if (template.TemperatureProfile != null)
            {
                sett.TemperatureProfile = template.TemperatureProfile;
            }
            if (template.GeographicLocation != null)
            {
                sett.GeographicLocation = template.GeographicLocation;
            }

            sett.SaveToDB();
            var creationcount = 1;

            foreach (var housePreviewEntry in _previewHouseEntries)
            {
                // look if this entry already exits
                House newhouse = null;
                foreach (var house in sim.Houses.It)
                {
                    var foundMissingEntry = false;
                    if (house.HouseType != housePreviewEntry.HouseType)
                    {
                        continue;
                    }
                    var calcObjects = house.Households.Select(x => x.CalcObject).ToList();
                    foreach (var household in housePreviewEntry.Households)
                    {
                        if (!calcObjects.Contains(household))
                        {
                            foundMissingEntry = true;
                            break;
                        }
                    }
                    if (!foundMissingEntry && sett.Households.All(x => x.CalcObject != house))
                    {
                        newhouse = house;
                    }
                }
                if (newhouse == null)
                {
                    Logger.Info("Creating new house " + creationcount);
                    newhouse                     = sim.Houses.CreateNewItem(sim.ConnectionString);
                    newhouse.HouseType           = housePreviewEntry.HouseType;
                    newhouse.EnergyIntensityType = housePreviewEntry.EnergyIntensityType;
                    newhouse.Name                = GetHouseName("(" + sett.Name + ") House", ref creationcount, sim,
                                                                housePreviewEntry.Households, housePreviewEntry.HouseType);
                    newhouse.CreationType = CreationType.TemplateCreated;
                    foreach (var household in housePreviewEntry.Households)
                    {
                        newhouse.AddHousehold(household, null, null, null);
                    }
                    newhouse.Source = "Generated by " + template.Name + " at " + DateTime.Now.ToShortDateString() +
                                      " " + DateTime.Now.ToShortTimeString();
                    if (template.TemperatureProfile != null)
                    {
                        newhouse.TemperatureProfile = template.TemperatureProfile;
                    }
                    if (template.GeographicLocation != null)
                    {
                        newhouse.GeographicLocation = template.GeographicLocation;
                    }
                    newhouse.SaveToDB();

                    creationcount++;
                }
                sett.AddHousehold(newhouse, 1);
            }
        }
        public void HouseLoadCreationAndSaveTest()
        {
            Config.ShowDeleteMessages = false;
            Logger.Threshold          = Severity.Error;
            using (var db = new DatabaseSetup(Utili.GetCurrentMethodAndClass()))
            {
                db.ClearTable(House.TableName);
                db.ClearTable(HouseTypeDevice.TableName);
                db.ClearTable(HouseHousehold.TableName);
                var loadTypes          = db.LoadLoadTypes();
                var houses             = new ObservableCollection <House>();
                var devices            = new ObservableCollection <RealDevice>();
                var deviceCategories   = new ObservableCollection <DeviceCategory>();
                var timeBasedProfiles  = new ObservableCollection <TimeBasedProfile>();
                var timeLimits         = new ObservableCollection <TimeLimit>();
                var persons            = new ObservableCollection <Person>();
                var temperaturProfiles = new ObservableCollection <TemperatureProfile>();
                var geoLocs            = db.LoadGeographicLocations(out _, timeLimits);
                var variables          = db.LoadVariables();
                var energyStorages     = db.LoadEnergyStorages(loadTypes, variables);
                var trafoDevices       = db.LoadTransformationDevices(loadTypes,
                                                                      variables);

                var dateprofiles       = db.LoadDateBasedProfiles();
                var locations          = db.LoadLocations(devices, deviceCategories, loadTypes);
                var generators         = db.LoadGenerators(loadTypes, dateprofiles);
                var deviceActionGroups = db.LoadDeviceActionGroups();
                var deviceActions      = db.LoadDeviceActions(timeBasedProfiles, devices,
                                                              loadTypes, deviceActionGroups);
                var houseTypes = db.LoadHouseTypes(devices, deviceCategories, timeBasedProfiles,
                                                   timeLimits, loadTypes, trafoDevices, energyStorages, generators, locations, deviceActions,
                                                   deviceActionGroups, variables);
                var householdTraits   = new ObservableCollection <HouseholdTrait>();
                var deviceSelections  = new ObservableCollection <DeviceSelection>();
                var vacations         = db.LoadVacations();
                var householdTags     = db.LoadHouseholdTags();
                var traitTags         = db.LoadTraitTags();
                var modularHouseholds = db.LoadModularHouseholds(householdTraits,
                                                                 deviceSelections, persons, vacations, householdTags, traitTags);
                db.LoadTransportation(locations, out var transportationDeviceSets, out var travelRouteSets,
                                      out var _,
                                      out var _, loadTypes, out var chargingStationSets);
                House.LoadFromDatabase(houses, db.ConnectionString, temperaturProfiles, geoLocs, houseTypes,
                                       modularHouseholds, chargingStationSets, transportationDeviceSets, travelRouteSets, false);
                (houses.Count).Should().Be(0);
                var house = new House("haus1", "blub", null, null, null, db.ConnectionString, EnergyIntensityType.Random,
                                      "Testing", CreationType.ManuallyCreated, Guid.NewGuid().ToStrGuid());
                house.SaveToDB();
                House.LoadFromDatabase(houses, db.ConnectionString, temperaturProfiles, geoLocs, houseTypes,
                                       modularHouseholds, chargingStationSets, transportationDeviceSets, travelRouteSets, false);
                (houses.Count).Should().Be(1);
                // ModularHousehold hh = new ModularHousehold("bla", null, "blub", db.ConnectionString, null, "test",null,null, EnergyIntensityType.Random,CreationType.TemplateCreated);
                //h/h.SaveToDB();
                //households.Add(hh);
                var house2 = houses[0];
                (house2.Households.Count).Should().Be(0);

                house.AddHousehold(modularHouseholds[0], null, null, null);
                house.AddHousehold(modularHouseholds[0], null, null, null);
                house.SaveToDB();
                houses.Clear();
                House.LoadFromDatabase(houses, db.ConnectionString, temperaturProfiles, geoLocs, houseTypes,
                                       modularHouseholds, chargingStationSets, transportationDeviceSets, travelRouteSets, false);
                (houses.Count).Should().Be(1);
                var house3 = houses[0];
                (house3.Households.Count).Should().Be(2);
                house3.DeleteHouseholdFromDB(house3.Households[0]);
                houses.Clear();

                House.LoadFromDatabase(houses, db.ConnectionString, temperaturProfiles, geoLocs, houseTypes,
                                       modularHouseholds, chargingStationSets, transportationDeviceSets, travelRouteSets, false);
                (houses.Count).Should().Be(1);
                var house4 = houses[0];
                (house4.Households.Count).Should().Be(1);
                db.Cleanup();
            }
        }