private static IEnumerable<FrequencyBox> LoadPoiFrequencies() { var frequencyBoxes = new List<FrequencyBox>(); using (var ctx = new CityContainer()) { // ReSharper disable LoopCanBeConvertedToQuery foreach (var poiType in ctx.PoiTypes) // ReSharper restore LoopCanBeConvertedToQuery frequencyBoxes.Add(new FrequencyBox { Content = poiType.Code, Frequency = poiType.Frequency.ToString() }); } return frequencyBoxes; }
public static void GeneratePOI(string cfgName, IEnumerable<PoiObject> poiObjects) { using (var ctx = new CityContainer()) { var configuration = ctx.Configurations.Single(c => c.Name == cfgName); var lastPoiType = string.Empty; try { foreach (var poiObject in poiObjects) { var poi = new Poi { Configuration = configuration, Vicinity = poiObject.Vicinity, Name = poiObject.Name, MapPoint = new MapPoint { Lat = poiObject.Latitude, Lng = poiObject.Longitude } }; foreach (var type in poiObject.Types) { lastPoiType = type; string poiType = lastPoiType; poi.Type.Add(ctx.PoiTypes.Single(t => t.Code == poiType)); } ctx.Pois.AddObject(poi); } ctx.SaveChanges(); } catch (Exception e) { Logger.LogError("Unsupported POI type '{0}'. Please consider adding this " + "type to poi types configuration file.\n{1}", lastPoiType, e.Message); } } }
private void UpdatePoiFrequencies() { using(var ctx = new CityContainer()) { foreach (var poiFrequencyBox in PoiFrequencyBoxes) { var poiBoxName = poiFrequencyBox.Content.ToString(); var poiType = ctx.PoiTypes.Single(t => t.Code == poiBoxName); double freqDouble; if (!double.TryParse(poiFrequencyBox.Frequency, out freqDouble)) { MessageBox.Show(string.Format("Invalid frequency value for POI '{0}", poiBoxName)); return; } poiType.Frequency = freqDouble; } ctx.SaveChanges(); } }
public static void InsertStaticDataIfNeeded() { if(StaticDataExists()) { Logger.LogInfo("Static data already exists. Skipping static data generation."); return; } var poiList = File.ReadLines(Properties.Settings.Default.poi_file); var currencies = File.ReadLines(Properties.Settings.Default.currencies_file); var banks = File.ReadLines(Properties.Settings.Default.banks_file); var simulationMethods = SymulationType.List(); using(var ctx = new CityContainer()) { foreach (var poi in poiList) ctx.PoiTypes.AddObject(new PoiType {Code = poi}); foreach(var currency in currencies) ctx.Currencies.AddObject(new Currency{Code = currency}); foreach (var bank in banks) ctx.Banks.AddObject(new Bank {Name = bank}); foreach (var simulationMethod in simulationMethods) ctx.SelectionMethods.AddObject(new SelectionMethod {Name = simulationMethod}); ctx.SaveChanges(); } InsertPeopleTemplates(); }
public static bool TableExistsSqlCE(string tableName) { var sqlCeTableExists = @"SELECT COUNT(TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='" + tableName + "'"; using (var ctx = new CityContainer()) { var exists = ctx.ExecuteStoreQuery<int>(sqlCeTableExists).SingleOrDefault(); return exists == 1 ? true : false; } }
public static bool StaticDataExists() { using (var ctx = new CityContainer()) { return ctx.PoiTypes.Count() != 0; } }
public static bool SaveConfiguration(ConfigurationData data) { using (var ctx = new CityContainer()) { var configurationExists = ctx.Configurations.Any(c => c.Name == data.ConfigurationName); if (configurationExists) return false; var poiTypes = ctx.PoiTypes.Where(t => data.PoiTypes.Contains(t.Code)); var newConfiguration = new Configuration { Name = data.ConfigurationName, SelectionMethod = ctx.SelectionMethods.Single(m => m.Name == data.SimulationMethodName), SimulationStartDate = data.SimulationStartDate, SimulationEndDate = data.SimulationEndDate, PersonDemandsPerDay = data.DemandsPerPersonPerDay, CreditCardBalanceMin = data.CreditCardBalanceMin, CreditCardBalanceMax = data.CreditCardBalanceMax, CreditCardLimitMin = data.CreditCardLimitMin, CreditCardLimitMax = data.CreditCardLimitMax, TransactionValueMin = data.TransactionValueMin, TransactionValueMax = data.TransactionValueMax }; // Assign POI circle areas foreach (var circleAreaObject in data.CircleAreas) { newConfiguration.CircleAreas.Add(new CircleArea { Configuration = newConfiguration, Lat = circleAreaObject.Pushpin.Location.Latitude, Lng = circleAreaObject.Pushpin.Location.Longitude, Range = circleAreaObject.Range }); } // Assign POI types for configuration foreach (var poiType in poiTypes) newConfiguration.PoiTypes.Add(poiType); // Assign Districts for configuration foreach (var polygon in data.Polygons) { var district = new District { Configuration = newConfiguration, Population = polygon.Population, Name = polygon.DistrictName }; foreach (var location in polygon.Locations) { district.MapPoints.Add(new MapPoint { Lat = location.Latitude, Lng = location.Longitude }); } } ctx.Configurations.AddObject(newConfiguration); ctx.SaveChanges(); return true; } }
public static bool RemoveConfiguration(string cfgName) { try { using (var ctx = new CityContainer()) { ctx.ContextOptions.LazyLoadingEnabled = false; Logger.LogInfo("Removing configuration '{0}'", cfgName); var configuration = ctx.Configurations //.Include("Districts.Addresses.Residents.Demands.PoiTypes") //.Include("Districts.Addresses.Residents.CreditCard") .Include("Pois") //poi .Include("CircleAreas") .Include("Pois.Type") .Include("PoiTypes") .Include("Pois.MapPoint") .Include("Districts.MapPoints") .Include("Districts.Addresses.MapPoint") .Single(s => s.Name == cfgName); foreach (var poi in configuration.Pois) { poi.Type.Clear(); var mapPoint = poi.MapPoint; ctx.Attach(mapPoint); ctx.DeleteObject(mapPoint); } configuration.PoiTypes.Clear(); foreach (var mapPoint in configuration.Districts.SelectMany(district => district.Addresses.Select( address => address.MapPoint))) { ctx.Attach(mapPoint); ctx.DeleteObject(mapPoint); } ctx.Configurations.DeleteObject(configuration); ctx.SaveChanges(); } return true; } catch (Exception) { return false; } }
public static ConfigurationData LoadConfiguration(string configurationName) { var data = new ConfigurationData(); using(var ctx = new CityContainer()) { var configuration = ctx.Configurations.Single(c => c.Name == configurationName); data.ConfigurationName = configurationName; // Load POI circle areas var circleAreaObjects = new List<CircleAreaObject>(); // ReSharper disable LoopCanBeConvertedToQuery foreach (var circleArea in configuration.CircleAreas) // ReSharper restore LoopCanBeConvertedToQuery circleAreaObjects.Add(new CircleAreaObject(circleArea.Lat, circleArea.Lng, circleArea.Range)); data.CircleAreas = circleAreaObjects; // Load POI types data.PoiTypes = configuration.PoiTypes.Select(poiType => poiType.Code).ToList(); var polygons = new List<MapDistrict>(); foreach (var district in configuration.Districts) { var newPolygon = new MapDistrict {DistrictName = district.Name, Population = district.Population, Locations = new LocationCollection()}; foreach (var mapPoint in district.MapPoints) newPolygon.Locations.Add(new Location(mapPoint.Lat, mapPoint.Lng)); polygons.Add(newPolygon); } data.Polygons = polygons; // simulation settings data.SimulationMethodName = configuration.SelectionMethod.Name; data.DemandsPerPersonPerDay = configuration.PersonDemandsPerDay; data.SimulationStartDate = configuration.SimulationStartDate; data.SimulationEndDate = configuration.SimulationEndDate; data.CreditCardBalanceMin = configuration.CreditCardBalanceMin; data.CreditCardBalanceMax = configuration.CreditCardBalanceMax; data.CreditCardLimitMin = configuration.CreditCardLimitMin; data.CreditCardLimitMax = configuration.CreditCardLimitMax; data.TransactionValueMin = configuration.TransactionValueMin; data.TransactionValueMax = configuration.TransactionValueMax; } return data; }
public static IEnumerable<string> ListSimulationTypes() { var typeNames = new List<string>(); using(var ctx = new CityContainer()) { typeNames.AddRange(ctx.SelectionMethods.Select(m => m.Name)); } return typeNames; }
public static IEnumerable<string> ListConfigurations() { var cfgNames = new List<string>(); using (var ctx = new CityContainer()) cfgNames.AddRange(ctx.Configurations.Select(configuration => configuration.Name)); return cfgNames; }
public static void GeneratePOI(string cfgName, string poiXml) { using (var ctx = new CityContainer()) { var configuration = ctx.Configurations.Single(c => c.Name == cfgName); var document = XDocument.Load(poiXml); var results = from c in document.Descendants("result") select new { // ReSharper disable PossibleNullReferenceException Vicinity = c.Element("vicinity").Value, Name = c.Element("name").Value, Lat = c.Element("geometry").Element("location").Element("lat").Value, Lng = c.Element("geometry").Element("location").Element("lng").Value, Types = c.Elements("type").Select(v => v.Value) // ReSharper restore PossibleNullReferenceException }; foreach (var result in results) { var poi = new Poi { Configuration = configuration, Vicinity = result.Vicinity, Name = result.Name, MapPoint = new MapPoint { Lat = CommonUtilities.ParseDouble(result.Lat), Lng = CommonUtilities.ParseDouble(result.Lng) } }; var lastPoiType = string.Empty; try { foreach (var type in result.Types) { lastPoiType = type; var poiType = lastPoiType; poi.Type.Add(ctx.PoiTypes.Single(t => t.Code == poiType)); } ctx.Pois.AddObject(poi); } catch (Exception e) { Logger.LogError("Unsupported POI type '{0}'. Please consider adding this type to poi types configuration file.\n{2}", lastPoiType, e.Message); } } ctx.SaveChanges(); } File.Delete(poiXml); }
public static void InsertPeopleTemplates() { var maleEntries = File.ReadLines(Properties.Settings.Default.male_file); var femaleEntries = File.ReadLines(Properties.Settings.Default.female_file); using(var ctx = new CityContainer()) { // Add male templates foreach (var split in maleEntries.Select(male => male.Split(';'))) ctx.PersonTemplates.AddObject( new PersonTemplate { FirstName = split[0], LastName = split[1], IsMale = true }); // Add female templates foreach (var split in femaleEntries.Select(female => female.Split(';'))) ctx.PersonTemplates.AddObject( new PersonTemplate { FirstName = split[0], LastName = split[1], IsMale = false }); ctx.SaveChanges(); } }
private void GenerateTransactions(string cfgName, ProgressWindow progress) { using(var ctx = new CityContainer()) { var selectionMethodName = ctx.Configurations.Single(c => c.Name == cfgName).SelectionMethod.Name; Logger.LogInfo("Using POI selection method: {0}", selectionMethodName); var demandsInConfiguration = ctx.Demands.Where(d => d.Person.Address.District.Configuration.Name == cfgName); var totalTransactions = demandsInConfiguration.Count(); foreach (var demand in demandsInConfiguration) { if (progress.Worker.CancellationPending) { progress.Args.Cancel = true; _simulationCancelled = true; return; } var demandType = demand.PoiType.Code; var matchingPois = ctx.Pois.Where(p => p.Type.Select(k => k.Code).Contains(demandType)); var selectedPoi = SelectPoi(matchingPois, demand.Person.Address.MapPoint, selectionMethodName); if (selectedPoi == null) continue; var transactionValue = StatisticalData.TransactionValue; var creditCard = demand.Person.CreditCard; var balanceAfterTransaction = creditCard.Balance - transactionValue; if (balanceAfterTransaction < creditCard.Limit) continue; creditCard.Balance -= transactionValue; creditCard.Balance = Math.Round(creditCard.Balance, 2); var transactionDateTime = demand.DateTime .AddHours(Random.Next(0,24)) .AddMinutes(Random.Next(0,60)) .AddSeconds(Random.Next(0,60)); var transaction = new Transaction { Demand = demand, CreditCard = creditCard, Poi = selectedPoi, Value = transactionValue, DateTime = transactionDateTime }; ctx.Transactions.AddObject(transaction); demand.Fulfilled = true; ++progress.Current; progress.InvokeUpdate(totalTransactions, GeneratingTransactionsMsg); } progress.InvokeUpdate(totalTransactions, GeneratingTransactionsMsg + PersistingMsg); ctx.SaveChanges(); } }
private void DownloadPois(string cfgName, ProgressWindow progress) { var poiTypes = new List<string>(); using(var ctx = new CityContainer()) { var cfg = ctx.Configurations.Single(c => c.Name == cfgName); poiTypes.AddRange(cfg.PoiTypes.Select(poiType => poiType.Code)); } var allPois = new HashSet<PoiObject>(); foreach (var area in AreaSelectorPageInstance.Areas) { if (progress.Worker.CancellationPending) { progress.Args.Cancel = true; _simulationCancelled = true; return; } var hashPois = MapsQueryEngine.Query( area.Latitude, area.Longitude, (int)(area.Range * 1000), poiTypes); foreach (var poi in hashPois) allPois.Add(poi); ++progress.Current; progress.InvokeUpdate(AreaSelectorPageInstance.Areas.Count, GettingPoisMsg); } progress.InvokeUpdate(AreaSelectorPageInstance.Areas.Count, GettingPoisMsg + PersistingMsg); DBUtilities.GeneratePOI(cfgName, allPois); }
private static IEnumerable<CheckBox> LoadPoiData() { // Cannot be converted to Linq because of automatic object disposing. var customBoxes = new List<CheckBox>(); using (var ctx = new CityContainer()) { // ReSharper disable LoopCanBeConvertedToQuery foreach (var poiType in ctx.PoiTypes) // ReSharper restore LoopCanBeConvertedToQuery { var content = string.Format("{0} ({1})", poiType.Code, poiType.Frequency); customBoxes.Add(new CheckBox { Content = content, Name = poiType.Code}); } } return customBoxes; }
private static void GeneratePeopleData(string cfgName, ProgressWindow progress) { using (var ctx = new CityContainer()) { var cfgDistricts = ctx.Districts.Where(d => d.Configuration.Name == cfgName); var totalAddresses = cfgDistricts.Select(d => d.Population).Sum(); foreach (var district in cfgDistricts) { Logger.LogInfo("generating people for district."); var locations = GeoUtilities.MapPointsToLocations(district.MapPoints); for (var i = 0; i < district.Population; i++) { if (progress.Worker.CancellationPending) { progress.Args.Cancel = true; _simulationCancelled = true; return; } Logger.LogInfo("generating person {0} / {1}", i + 1, district.Population); var bankId = Random.Next(1, ctx.Banks.Count() + 1); var currencyId = Random.Next(1, ctx.Currencies.Count() + 1); var ccNumber = StatisticalData.CreditCardNumber; var personCreditCard = new CreditCard { Balance = StatisticalData.CreditCardBalance, Limit = StatisticalData.CreditCardLimit, Bank = ctx.Banks.Single(b => b.Id == bankId), Currency = ctx.Currencies.Single(c => c.Id == currencyId), Number = ccNumber }; var templateId = Random.Next(1, ctx.PersonTemplates.Count() + 1); var person = new Person { Template = ctx.PersonTemplates.Single(t => t.Id == templateId), CreditCard = personCreditCard }; var addressGeo = GeoUtilities.RandomLocationWithinPolygon(locations); var address = new Address { District = district, MapPoint = new MapPoint { Lat = addressGeo.Latitude, Lng = addressGeo.Longitude } }; address.Residents.Add(person); ctx.Addresses.AddObject(address); ++progress.Current; progress.InvokeUpdate(totalAddresses, GeneratingPeopleMsg); } } progress.InvokeUpdate(totalAddresses, GeneratingPeopleMsg + PersistingMsg); ctx.SaveChanges(); } }
private static void GenerateDemands(string cfgName, ProgressWindow progress) { try { Logger.LogInfo("Adding demand pool..."); using (var ctx = new CityContainer()) { var cfg = ctx.Configurations.Single(c => c.Name == cfgName); var peopleInConfiguration = ctx.People.Where(p => p.Address.District.Configuration.Name == cfgName).OrderBy(o => o.Id); //poi types from poi list (not configuration) because some poi types might be missing (not found) var poiTypeCollections = ctx.Pois.Select(p => p.Type); var poiSet = new HashSet<PoiType>(); foreach (var poiTypeCollection in poiTypeCollections) foreach (var poiType in poiTypeCollection) poiSet.Add(poiType); var statisticalData = new StatisticalData(); var totalDays = cfg.SimulationEndDate.Subtract(cfg.SimulationStartDate).Days + 1; var totalPeople = peopleInConfiguration.Count(); var demandsPerDay = (int) (totalPeople * cfg.PersonDemandsPerDay); var totalDemands = totalDays * demandsPerDay; for (var dayNr = 0; dayNr < totalDays; dayNr++) { var currentDate = cfg.SimulationStartDate.AddDays(dayNr); for (var demandNr = 0; demandNr < demandsPerDay; demandNr++) { if (progress.Worker.CancellationPending) { progress.Args.Cancel = true; _simulationCancelled = true; return; } var randomPersonIndex = Random.Next(0, totalPeople); var person = peopleInConfiguration.Skip(randomPersonIndex).First(); var demandDateTime = currentDate .AddHours(Random.Next(0, 24)) .AddMinutes(Random.Next(0, 60)) .AddSeconds(Random.Next(0, 60)); var demand = new Demand { Person = person, DateTime = demandDateTime, PoiType = statisticalData.TakePoiByDistribution(poiSet) }; ctx.Demands.AddObject(demand); ++progress.Current; progress.InvokeUpdate(totalDemands, GeneratingDemandsMsg); } } progress.InvokeUpdate(totalDemands, GeneratingDemandsMsg + PersistingMsg); ctx.SaveChanges(); // Persist after each day. } Logger.LogInfo("Demands pool added."); } catch(Exception e1) { Logger.LogInfo(e1.Message); } }