Exemplo n.º 1
0
        /// <summary>
        /// Creates all the path edges from the list of LocationMappers
        /// </summary>
        /// <returns></returns>
        private async Task CreatePathEdgesFromMap(SimulationExtension SimExt)
        {
            await Task.Run(() =>
            {
                //SimSig.SimulationExtension SimExt = new SimulationExtension(this._Simulation.ID, this._SQLConnector);

                foreach (MapperLocationNode MappedLocNode in this._LocationNodeMapper)
                {
                    LocationNode FromLocationNode = null;

                    //Check the MappedLocNode has a next location node to create

                    if (string.IsNullOrEmpty(MappedLocNode.NextLocationNode.SimSigCode) == false)
                    {
                        //Get a list of MapperLocationNode objects with a location node created
                        List <MapperLocationNode> MappersWithLocationNodes = this._LocationNodeMapper.Where(y => y.LocationNode != null).ToList();

                        //Get the From Location Node
                        if (MappedLocNode.LocationNode == null)
                        {
                            //If not already tagged in the MapperLocationNode search through the list to find it
                            FromLocationNode = MappersWithLocationNodes.Find(x => x.LocationNode.SimID == this._Simulation.ID &&
                                                                             x.LocationNode.EraID == this._TemplateSimEra.ID &&
                                                                             x.LocationNode.Version.ID == this._Version.ID &&
                                                                             String.CompareOrdinal(x.LocationNode.LocationSimSigCode, MappedLocNode.SimSigCode) == 0 &&
                                                                             String.CompareOrdinal(x.LocationNode.Platform, MappedLocNode.Platform) == 0 &&
                                                                             String.CompareOrdinal(x.LocationNode.Path, MappedLocNode.Path) == 0 &&
                                                                             String.CompareOrdinal(x.LocationNode.Line, MappedLocNode.Line) == 0).LocationNode;
                        }
                        else
                        {
                            FromLocationNode = MappedLocNode.LocationNode;
                        }

                        //Get the to location node
                        LocationNode ToNodeLocation = MappersWithLocationNodes.FirstOrDefault(x => x.LocationNode.SimID == this._Simulation.ID &&
                                                                                              x.LocationNode.EraID == this._TemplateSimEra.ID &&
                                                                                              x.LocationNode.Version.ID == this._Version.ID &&
                                                                                              String.CompareOrdinal(x.LocationNode.LocationSimSigCode, MappedLocNode.NextLocationNode.SimSigCode) == 0 &&
                                                                                              String.CompareOrdinal(x.LocationNode.Platform, MappedLocNode.NextLocationNode.Platform) == 0 &&
                                                                                              String.CompareOrdinal(x.LocationNode.Path, MappedLocNode.NextLocationNode.Path) == 0 &&
                                                                                              String.CompareOrdinal(x.LocationNode.Line, MappedLocNode.NextLocationNode.Line) == 0).LocationNode;

                        //Create the path edge
                        if (ToNodeLocation != null)
                        {
                            //Build new LocationNode
                            SimSig.PathEdge NewPathEdge = new PathEdge(FromLocationNode, ToNodeLocation, this._SQLConnector);
                            NewPathEdge.SaveToSQLDB();
                        }
                    }
                }
            }).ConfigureAwait(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates all the location nodes from the list of LocationMappers
        /// </summary>
        /// <returns></returns>
        private async Task CreateLocationNodesFromMap(SimulationExtension SimExt)
        {
            await Task.Run(() =>
            {
                //SimSig.SimulationExtension SimExt = new SimulationExtension(this._Simulation.ID, this._SQLConnector);

                foreach (MapperLocationNode MappedLocNode in this._LocationNodeMapper)
                {
                    //Get the location
                    Location NodeLocation = GetLocationFromMapperBySimSigCode(MappedLocNode.SimSigCode);
                    //Build new LocationNode
                    MappedLocNode.CreateLocationNode(ref SimExt, this._SQLConnector, NodeLocation, this._Version, this._TemplateSimEra);
                }
            }).ConfigureAwait(false);
        }
Exemplo n.º 3
0
        public async Task <QueuerResponseStatus> Execute()
        {
            bool DebugMode = Convert.ToBoolean(this._Config["debugMode"]);

            //Add process started Response
            this._Responses.Add(new QueuerResponse(QueuerResponseStatus.Running, "processStarted", null));

            //Check user was authenticated at time of queue
            if (this._Authenticated == false)
            {
                this._Responses.Add(new QueuerResponse(QueuerResponseStatus.CompletedWithWarning, "processUserNotAuthenticatedAtQueue", null));
                return(QueuerResponseStatus.CompletedWithWarning);
            }

            try
            {
                //Initialise simulation
                this._Simulation = new Simulation(this.Config["simName"].ToString(), this.Config["simDescription"] == null ? string.Empty: this.Config["simDescription"].ToString(), this.Config["simWikiLink"] == null ? string.Empty : this.Config["simWikiLink"].ToString(), this.Config["simSimSigCode"].ToString(), this._SQLConnector);

                //Declare the individual async tasks
                Task <WTT>  SourceWTT        = this.LoadTimeTable();
                Task <bool> SimulationExists = this.CheckSimulationExists();

                //Declare list of all the async tasks
                List <Task> allTasks = new List <Task> {
                    SourceWTT, SimulationExists
                };

                while (allTasks.Any())
                {
                    Task finished = await Task.WhenAny(allTasks).ConfigureAwait(false);

                    if (finished == SourceWTT)
                    {
                        if (DebugMode)
                        {
                            this._Responses.Add(new QueuerResponse(QueuerResponseStatus.DebugMesssage, "Completed build the WTT object from the source WTT file", null));
                        }
#if DEBUG
                        Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- Completed reading WTT");
#endif
                        this._TimeTable = SourceWTT.Result;
                    }
                    else if (finished == SimulationExists)
                    {
                        if (DebugMode)
                        {
                            this._Responses.Add(new QueuerResponse(QueuerResponseStatus.DebugMesssage, "Completed checking if the simulation already exists inthe GroundFrame.SQL database", null));
                        }
#if DEBUG
                        Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- Completed checking whether the sim Exists");
#endif
                    }
                    allTasks.Remove(finished);
                }

                //Check is simulation already exists in the data. If it does save the simulation to the GroundFrame.SQL database
                if (SimulationExists.Result == true)
                {
                    this._Responses.Add(new QueuerResponse(QueuerResponseStatus.CompletedWithWarning, "processSimulationAlreadyExists", null));
#if DEBUG
                    Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- The simulation already exists so the process is stopped");
#endif
                    return(QueuerResponseStatus.CompletedWithWarning);
                }
                else
                {
                    try
                    {
                        this._Simulation.SaveToSQLDB();
                        //Now load the Extension version so we can keep track of the nodes
                        this._SimExt = new SimulationExtension(this._Simulation.ID, this._SQLConnector);

                        if (DebugMode)
                        {
                            this._Responses.Add(new QueuerResponse(QueuerResponseStatus.DebugMesssage, $"Simulation saved to the GroundFrame.SQL database. ID = {this._Simulation.ID}.", null));
                        }
#if DEBUG
                        Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- Simulation saved to GroundFrame.SQL database");
#endif
                    }
                    catch (Exception Ex)
                    {
                        ResourceManager ExceptionMessageResources = new ResourceManager("GroundFrame.Core.Resources.ExceptionResources", Assembly.GetExecutingAssembly());
                        string          ExceptionMessage          = ExceptionMessageResources.GetString("QueuerErrorSavingSimulation", Globals.UserSettings.GetCultureInfo());
                        throw new Exception(ExceptionMessage, Ex);
                    }
                }

                //Build the next stage of the task by setting the individual lasts
                Task <List <MapperLocation> >     GetLocationMapperTask     = this.GetLocationMapperFromWTT();     //Gets the location mapper list from the WTT
                Task <List <MapperLocationNode> > GetLocationNodeMapperTask = this.GetLocationNodeMapperFromWTT(); //Gets the location node mapper list from the WTT
                Task <SimSig.Version>             SimVersionTask            = this.GetSimSigVersion();             //Gets SimSig version requested from the GroundFrame.SQL database
                Task <SimSig.SimulationEra>       TemplateSimEraTask        = this.GetSimTemplateEra();            //Gets the era template for the simulation

                //Build All List task
                allTasks = new List <Task> {
                    GetLocationMapperTask, GetLocationNodeMapperTask, SimVersionTask, TemplateSimEraTask
                };

                //Wait for the tasks to finish
                while (allTasks.Any())
                {
                    Task finished = await Task.WhenAny(allTasks).ConfigureAwait(false);

                    if (finished == GetLocationMapperTask)
                    {
                        if (DebugMode)
                        {
                            this._Responses.Add(new QueuerResponse(QueuerResponseStatus.DebugMesssage, "Completed getting the Location Mapper from the source WTT", null));
                        }
#if DEBUG
                        Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- Completed getting Location Mapper");
#endif
                        _LocationMapper = GetLocationMapperTask.Result;
                    }
                    else if (finished == GetLocationNodeMapperTask)
                    {
                        if (DebugMode)
                        {
                            this._Responses.Add(new QueuerResponse(QueuerResponseStatus.DebugMesssage, "Completed getting the Location Node Mapper from the source WTT", null));
                        }
#if DEBUG
                        Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- Completed getting Location Node Mapper");
#endif
                        _LocationNodeMapper = GetLocationNodeMapperTask.Result;
                    }
                    else if (finished == SimVersionTask)
                    {
                        if (DebugMode)
                        {
                            this._Responses.Add(new QueuerResponse(QueuerResponseStatus.DebugMesssage, "Completed getting the Simulation version from the GroundFrame.SQL database", null));
                        }
#if DEBUG
                        Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- Completed getting SimSig version");
#endif
                        _Version = SimVersionTask.Result;
                    }
                    else if (finished == TemplateSimEraTask)
                    {
                        if (DebugMode)
                        {
                            this._Responses.Add(new QueuerResponse(QueuerResponseStatus.DebugMesssage, "Completed getting the Simulation template era from the GroundFrame.SQL database", null));
                        }
#if DEBUG
                        Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- Completed getting the template era");
#endif
                        _TemplateSimEra = TemplateSimEraTask.Result;
                    }
                    allTasks.Remove(finished);
                }

                //Next create the locations in the GroundFrame.SQL database
                await CreateLocationsFromMap().ConfigureAwait(false);

                if (DebugMode)
                {
                    this._Responses.Add(new QueuerResponse(QueuerResponseStatus.DebugMesssage, "Completed creating the locations in the GroundFrame.SQL database", null));
                }
#if DEBUG
                using SimulationExtension SimExtentionLocations = new SimulationExtension(this._Simulation.ID, this._SQLConnector);
                Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- The Simulation contains {SimExtentionLocations.Locations.Count} location(s).");
#endif

                //Next create the location nodes in the GroundFrame.SQL database
                await CreateLocationNodesFromMap(this._SimExt).ConfigureAwait(false);

                if (DebugMode)
                {
                    this._Responses.Add(new QueuerResponse(QueuerResponseStatus.DebugMesssage, "Completed creating the location nodes in the GroundFrame.SQL database", null));
                }
#if DEBUG
                Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- The Simulation contains {this._SimExt.LocationNodes.Count} location nodes(s).");
#endif

                //Next create the path edges in the GroundFrame.SQL database
                await CreatePathEdgesFromMap(this._SimExt).ConfigureAwait(false);

                if (DebugMode)
                {
                    this._Responses.Add(new QueuerResponse(QueuerResponseStatus.DebugMesssage, "Completed creating the path edges in the GroundFrame.SQL database", null));
                }
#if DEBUG
                Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- The Simulation contains {this._SimExt.LocationNodes.Sum(x => x.PathEdges.Count)} path edge(s).");
#endif

                this._Responses.Add(new QueuerResponse(QueuerResponseStatus.Success, "processSuccess", null));
                return(QueuerResponseStatus.Success);
            }
            catch (AggregateException Ex)
            {
                ResourceManager ExceptionMessageResources = new ResourceManager("GroundFrame.Core.Resources.ExceptionResources", Assembly.GetExecutingAssembly());
                string          ExceptionMessage          = ExceptionMessageResources.GetString("QueuerGenericFailureMessage", Globals.UserSettings.GetCultureInfo());

                foreach (Exception Inner in Ex.InnerExceptions)
                {
                    this._Responses.Add(new QueuerResponse(QueuerResponseStatus.Failed, ExceptionMessage, Inner));
                }

                return(QueuerResponseStatus.Failed);
            }

            catch (Exception Ex)
            {
                ResourceManager ExceptionMessageResources = new ResourceManager("GroundFrame.Core.Resources.ExceptionResources", Assembly.GetExecutingAssembly());
                string          ExceptionMessage          = ExceptionMessageResources.GetString("QueuerGenericFailureMessage", Globals.UserSettings.GetCultureInfo());
                this._Responses.Add(new QueuerResponse(QueuerResponseStatus.Failed, ExceptionMessage, Ex));
                return(QueuerResponseStatus.Failed);
            }
        }