public void CheckRouteCompletenessTest()
        {
            TravelRouteSet trs   = new TravelRouteSet("trs", 1, "", "", Guid.NewGuid().ToStrGuid());
            List <Site>    sites = new List <Site>();

            //test with empty
            CalcTransportationDtoFactory.CheckRouteCompleteness(trs, sites);

            //test with one route
            Site sitea = new Site("sitea", 1, "", "", Guid.NewGuid().ToStrGuid());
            Site siteb = new Site("siteb", 2, "", "", Guid.NewGuid().ToStrGuid());

            sites.Add(sitea);
            sites.Add(siteb);
            trs.AddRoute(new TravelRoute(1, "", "Route1", "desc", sitea, siteb, Guid.NewGuid().ToStrGuid(), null), false);
            CalcTransportationDtoFactory.CheckRouteCompleteness(trs, sites);

            //one route missing
            Site sitec = new Site("sitec", 2, "", "", Guid.NewGuid().ToStrGuid());

            sites.Add(sitec);
            trs.AddRoute(new TravelRoute(1, "", "Route2", "desc", siteb, sitec, Guid.NewGuid().ToStrGuid(), null), false);
            Action crashfunction1 = () => CalcTransportationDtoFactory.CheckRouteCompleteness(trs, sites);

            crashfunction1.Should().Throw <DataIntegrityException>();
            //add missing route
            trs.AddRoute(new TravelRoute(1, "", "Route3", "desc", sitea, sitec, Guid.NewGuid().ToStrGuid(), null), false);
            CalcTransportationDtoFactory.CheckRouteCompleteness(trs, sites);
        }
        private static void MakeNewAdjustedRoute([NotNull] Simulator sim,
                                                 [NotNull] TravelRouteSetEntry oldTravelRouteSetEntry,
                                                 int adjustingDistances,
                                                 [CanBeNull] string modRouteKey,
                                                 [NotNull] TransportationDistanceModifier modifier,
                                                 [NotNull] TravelRouteSet adjustedTravelrouteset)
        {
            var         oldRoute = oldTravelRouteSetEntry.TravelRoute;
            TravelRoute newRoute = new TravelRoute(null,
                                                   sim.ConnectionString,
                                                   oldRoute.Name + " adjustment " + adjustingDistances,
                                                   oldRoute.Description,
                                                   oldRoute.SiteA,
                                                   oldRoute.SiteB,
                                                   Guid.NewGuid().ToStrGuid(),
                                                   oldRoute.RouteKey);

            newRoute.SaveToDB();
            sim.TravelRoutes.It.Add(newRoute);
            foreach (var step in oldRoute.Steps)
            {
                double distance = step.Distance;
                if (step.StepKey?.ToLower(CultureInfo.InvariantCulture) == modRouteKey)
                {
                    distance = modifier.NewDistanceInMeters;
                }
                newRoute.AddStep(step.Name, step.TransportationDeviceCategory, distance, step.StepNumber, step.StepKey, false);
            }


            newRoute.SaveToDB();
            //Logger.Info("Adjusted route " + newRoute.Name);
            adjustedTravelrouteset.AddRoute(newRoute);
        }
Esempio n. 3
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);
            }
        }
        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);
        }