Beispiel #1
0
        public static Routing.RoutingNetwork BuildNetwork(ref Carpools.CarpoolParser CParser, ref Traffic.TrafficParser TParser, List <Carpooler> CarPoolers, List <TrafficReport> TrafficReport, double TrafficPropagationMaxDistance, int CarpoolingMaxConnectionsPerTNode)
        {
            Routing.RoutingNetwork routingNetwork = null;

            string Path = System.IO.Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["dataPath"] + "\\" + ConfigurationManager.AppSettings["NetworkFileName"]);

            if (!File.Exists(Path))
            {
                log.Info("Routing network bin file not exist: " + Path + " The road network will be generated from the osm map");

                // Construct the road network.
                string OSMXMLMapFilePath = System.IO.Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["dataPath"] + ConfigurationManager.AppSettings["OSMXMLMapFile"]);
                log.Info("Parsing " + OSMXMLMapFilePath + " OSM.");

                routingNetwork = new Routing.RoutingNetwork();
                OSM.XMLParser Parser = new OSM.XMLParser();
                Parser.LoadOSMMap(OSMXMLMapFilePath, ref routingNetwork);

                // Construct the transportation network.
                log.Info("Parsing GTFS.");

                GTFS.DBParser GTFSParser = new GTFS.DBParser();

                if (CarPoolers != null)
                {
                    routingNetwork.CarpoolingMaxConnectionsPerTNode = CarpoolingMaxConnectionsPerTNode;
                }

                GTFSParser.LoadTransportationNetwork(ref routingNetwork);
                log.Info("Attaching GTFS.");

                routingNetwork.ConnectTransportation();

                /* Add data to the Stops Carpooling connections */
                routingNetwork.CreateNetworkForCarpoolingRides();

                //routingNetwork.Serialize();
            }
            else
            {
                log.Info("Routing network: " + Path);
                log.Info("DeSrializing Netwrok.");
                routingNetwork = Routing.RoutingNetwork.DeSerialize();
            }

            /* Construct Carpooling network */
            if (CarPoolers != null)
            {
                log.Info("Constructing Carpooling network (" + CarPoolers.Count + " rides)");
                CParser = new CarpoolParser(routingNetwork, CarPoolers);
                //CParser.BuildCarpoolRoutesFromXML();
                //CParser.BuildCarpoolRoutesFromJson();
                CParser.ConnectWithRoadNetwork();
            }

            /* Update the network considering the traffic reports */
            if (TrafficReport != null)
            {
                routingNetwork.TrafficPropagationMaxDistance = TrafficPropagationMaxDistance;
                log.Info("Updating the network considering the traffic reports (" + TrafficReport.Count + " reports)");
                TParser = new TrafficParser(routingNetwork, TrafficReport);
                TParser.UpdateNetworkWithTrafficReport();
            }

            return(routingNetwork);
        }
        void updateCarpoolingNetwork(CarPoolerDataVersioned CarPoolingTmp)
        {
            CarpoolParser CParserTmp = null;

            //JUST FOR TESTING: simulate a new data version
            //CarPoolingTmp.Version.version++;

            if ((Program.CarPooling.Version != null) && (CarPoolingTmp.Version != null) &&
                ((CarPoolingTmp.Version.version != Program.CarPooling.Version.version) ||
                 (CarPoolingTmp.Version.nightly_version != Program.CarPooling.Version.nightly_version)))
            {
                log.Info("New Carpooling data are available (prevVersion:" + Program.CarPooling.Version.version + ", newVersion:" + CarPoolingTmp.Version.version + ")");
                log.Info("New Carpooling data are available (nightlyPrevVersion:" + Program.CarPooling.Version.nightly_version + ", newNightlyVersion:" + CarPoolingTmp.Version.nightly_version + ")");

                bool updateExternalRides = false;
                if ((DateTime.Now.Hour >= Program.ExtCarpoolingAvailableUpdateTimeFrom) && (DateTime.Now.Hour < Program.ExtCarpoolingAvailableUpdateTimeTo))
                {
                    updateExternalRides = true;
                }

                /* Get the carpooling rides list from the backend web service */
                CarPoolingTmp.Carpoolers = HTTPRequests.getCarpoolingDataFromBackend(productionRoutingNetwork.MinPoint, productionRoutingNetwork.MaxPoint, updateExternalRides);

                ////JUST FOR TESTING: modify element
                //CarPoolingTmp.Carpoolers.First().Id = "pizza";

                ////Just for testing: add new element
                //Carpooler test = new Carpooler("drtgwet", 2789, 4);
                //test.WayPoints.Add(new Point(46.013456, 8.943813));
                //test.WayPoints.Add(new Point(46.011596, 8.964293));
                //CarPoolingTmp.Carpoolers.Add(test);

                ////Just for testing: remove element
                //Carpooler testremove = CarPoolingTmp.Carpoolers.First();
                //CarPoolingTmp.Carpoolers.Remove(testremove);

                ////Just for testing: add modified element
                //Carpooler testaddandchange = CarPoolingTmp.Carpoolers.First();
                //testaddandchange.WayPoints.Add(new Point(1, 1));
                //testaddandchange.WayPoints.Add(new Point(2, 2));
                //CarPoolingTmp.Carpoolers.Add(testaddandchange);


                /* Get differences between the old version and the new one */
                CarpoolDiff CarPoolingDiff = CarpoolParser.checkDifferences(Program.CarPooling.Carpoolers, CarPoolingTmp.Carpoolers, updateExternalRides);

                if ((CarPoolingDiff.ElementsToRemove.Count > 0) || (CarPoolingDiff.ElementsToAdd.Count > 0))
                {
                    /* Update the network only when there are no pending threads (Monitor.Wait) */
                    lock (_ActiveWorkersLock)
                    {
                        while (_CountOfActiveWorkers > 0)
                        {
                            Monitor.Wait(_ActiveWorkersLock);
                        }

                        log.Info("Updating the production network with the carpooling rides");

                        /* Remove the OLD Carpooling connections from the network */
                        if (CarPoolingDiff.ElementsToRemove.Count > 0)
                        {
                            log.Info("Removing " + CarPoolingDiff.ElementsToRemove.Count + " deleted carpooling connections from the processNetwork");

                            foreach (Carpooler el in CarPoolingDiff.ElementsToRemove)
                            {
                                productionRoutingNetwork.RemoveConnection(el);

                                /* Update the original CParser and Carpooling classes */
                                Program.CParser.CarPoolers.Remove(el);
                            }

                            log.Info("ProcessNetwork updated! (" + CarPoolingDiff.ElementsToRemove.Count + " old carpooling connections deleted)");

                            /* Force the network update before processing the next request */
                            updateRN = true;
                        }

                        /* Add the NEW Carpooling connections to the network */
                        if (CarPoolingDiff.ElementsToAdd.Count > 0)
                        {
                            /* Construct the new Carpooling network */
                            log.Info("Adding " + CarPoolingDiff.ElementsToAdd.Count + " new carpooling connections to the processNetwork");
                            CParserTmp = new CarpoolParser(productionRoutingNetwork, CarPoolingDiff.ElementsToAdd);
                            CParserTmp.ConnectWithRoadNetwork();

                            /* Update the original CParser and Carpooling classes */
                            foreach (Carpooler el in CarPoolingDiff.ElementsToAdd)
                            {
                                Program.CParser.CarPoolers.Add(el);
                            }

                            log.Info("Processnetwork updated! (" + CarPoolingDiff.ElementsToAdd.Count + " new carpooling connections added)");

                            /* Force the network update before processing the next request */
                            updateRN = true;
                        }

                        log.Info("Production network updated");
                    }
                }

                if (CarPoolingDiff.ElementsToIgnore.Count > 0)
                {
                    log.Info(CarPoolingDiff.ElementsToIgnore.Count + " rides unchanged");
                }

                /* If there are no modifications too, just update the version number (this happens for example if the driver quickly
                 * enable and disable a ride, so the version number changes but the content doesn't)
                 */
                /* Update the carpooling version */
                Program.CarPooling.Version = new CarpoolerVersion(CarPoolingTmp.Version.version, CarPoolingTmp.Version.timestampVersion, CarPoolingTmp.Version.nameSite,
                                                                  CarPoolingTmp.Version.nightly_version, CarPoolingTmp.Version.nightly_timestampVersion);
            }
            else
            {
                if (CarPoolingTmp.Version != null)
                {
                    //log.Info("Carpooling version not changed: version=" + CarPoolingTmp.Version.version + " timestamp=" + CarPoolingTmp.Version.timestampVersion);
                }
            }
        }