private static TravelRouteSet AdjustTravelDistancesBasedOnModifiers([NotNull] TravelRouteSet travelrouteset,
                                                                            [NotNull] Simulator sim,
                                                                            [NotNull] House house,
                                                                            [NotNull] HouseholdData householdData,
                                                                            int householdidx)
        {
            Stopwatch sw = Stopwatch.StartNew();

            if (householdData.TransportationDistanceModifiers == null)
            {
                throw new LPGException("Was null even though this was checked before the function was called.");
            }
            var newName = travelrouteset.Name + "(" + house.Name + " - " + householdData.Name + " " + householdidx + ")";
            var adjustedTravelrouteset = new TravelRouteSet(newName, null, sim.ConnectionString, travelrouteset.Description, Guid.NewGuid().ToStrGuid());

            adjustedTravelrouteset.SaveToDB();
            sim.TravelRouteSets.It.Add(adjustedTravelrouteset);
            int adjustingDistances = 0;

            foreach (TravelRouteSetEntry oldTravelRouteSetEntry in travelrouteset.TravelRoutes)
            {
                bool addUnmodifiedRoute = true;
                foreach (var modifier in householdData.TransportationDistanceModifiers)
                {
                    string modRouteKey = modifier.RouteKey?.ToLower(CultureInfo.InvariantCulture);
                    if (oldTravelRouteSetEntry.TravelRoute.RouteKey?.ToLower(CultureInfo.InvariantCulture) == modRouteKey)
                    {
                        Logger.Info("Adjusting distances for key " + modifier.RouteKey + "-" + modifier.StepKey + ", total routes in the db: " + sim.TravelRoutes.It.Count);
                        var modStepKey    = modifier.StepKey?.ToLower(CultureInfo.InvariantCulture);
                        var oldRouteSteps = oldTravelRouteSetEntry.TravelRoute.Steps.Where(x => x.StepKey?.ToLower(CultureInfo.InvariantCulture) == modStepKey).ToList();
                        if (oldRouteSteps.Count > 0)
                        {
                            MakeNewAdjustedRoute(sim, oldTravelRouteSetEntry, adjustingDistances, modRouteKey, modifier, adjustedTravelrouteset);
                            addUnmodifiedRoute = false;
                            adjustingDistances++;
                        }
                    }
                }

                if (addUnmodifiedRoute)
                {
                    adjustedTravelrouteset.AddRoute(oldTravelRouteSetEntry.TravelRoute);
                }
            }
            //Config.ShowDeleteMessages = true;
            travelrouteset = adjustedTravelrouteset;
            adjustedTravelrouteset.SaveToDB();
            sw.Stop();
            Logger.Info("Total distances adjusted: " + adjustingDistances + ". This took " + sw.Elapsed.TotalSeconds.ToString("F2", CultureInfo.InvariantCulture) + " seconds.");
            return(travelrouteset);
        }
예제 #2
0
        public void TravelRouteSetTest()
        {
            using (DatabaseSetup db = new DatabaseSetup(Utili.GetCurrentMethodAndClass()))
            {
                db.ClearTable(TravelRouteSet.TableName);
                db.ClearTable(TravelRouteSetEntry.TableName);

                TravelRouteSet set = new TravelRouteSet("set1", null, db.ConnectionString, "desc", System.Guid.NewGuid().ToStrGuid());
                set.SaveToDB();
                Site a = new Site("a", null, db.ConnectionString, "desc", System.Guid.NewGuid().ToStrGuid());
                a.SaveToDB();
                Site b = new Site("b", null, db.ConnectionString, "desc", System.Guid.NewGuid().ToStrGuid());
                b.SaveToDB();
                TravelRoute route = new TravelRoute(null, db.ConnectionString, "routename", "routedesc", a, b, System.Guid.NewGuid().ToStrGuid(), null);
                route.SaveToDB();
                set.AddRoute(route);
                //loading
                ObservableCollection <TravelRoute> routes = new ObservableCollection <TravelRoute>
                {
                    route
                };
                ObservableCollection <TravelRouteSet> sets = new ObservableCollection <TravelRouteSet>();
                TravelRouteSet.LoadFromDatabase(sets, db.ConnectionString, false, routes);
                db.Cleanup();
                (sets.Count).Should().Be(1);
            }
        }