Example #1
0
        /// <summary>
        /// Validates model files and config.xml.
        /// </summary>
        /// <param name="configPath"></param>
        /// <returns>List of model paths.</returns>
        public void RunSetup(out List <string> ModelPaths, out Dictionary <string, bool> VisualizationStatuses, string configPath = null)
        {
            ModelPaths            = null;
            VisualizationStatuses = null;
            while (true)
            {
                while (!FindConfigPath(ref configPath)) //While can't find config file.
                {
                    CreateConfig(configPath);
                }

                XmlDocument configFile = new XmlDocument();
                configFile.Load(configPath);

                #region Make sure visalizers node exists in config.xml.
                if (!GetVisualizerNodes(configFile, out XmlNode visualizerNodes))
                {
                    configFile.DocumentElement.AppendChild(configFile.CreateNode(XmlNodeType.Element, "visualizers", null));
                    if (!GetVisualizerNodes(configFile, out visualizerNodes))
                    {
                        throw new Exception("Error, \"visualizers\" node could not be added and/or does not exist.");
                    }
                    configFile.Save(configPath);
                }
                #endregion

                VisualizationStatuses = GetVisualizerStatuses(visualizerNodes);
                IEnumerable <Type>        VisualizerTypes           = AbstractVisualizer.GetAllSubclasses(typeof(AbstractVisualizer));
                Dictionary <string, bool> MissingVisualizerStatuses = new Dictionary <string, bool>();
                foreach (Type t in VisualizerTypes)
                {
                    string typeName = t.ToString();
                    if (!VisualizationStatuses.ContainsKey(typeName))
                    {
                        MissingVisualizerStatuses.Add(typeName, true);
                    }
                }
                if (MissingVisualizerStatuses.Count > 0)
                {
                    AddVisualizersToConfig(visualizerNodes, MissingVisualizerStatuses);
                    configFile.Save(configPath);
                }
                VisualizationStatuses = GetVisualizerStatuses(visualizerNodes);

                string modelDir;
                bool?  modelDirFound;

                while ((modelDirFound = GetModelDirectory(configFile, out modelDir)) != true) //While the directory that model files are stored in can't be found.
                {
                    if (modelDir == null)
                    {
                        Console.ForegroundColor = ConsoleColor.Black;
                        Console.BackgroundColor = ConsoleColor.Red;
                        Console.WriteLine("The model directory provided in config.xml is invalid.");
                        Console.ResetColor();
                        InvalidModelPathProvided(configFile);
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Black;
                        Console.BackgroundColor = ConsoleColor.Red;
                        Console.WriteLine("No model directory provided in config.xml");
                        Console.ResetColor();
                        InvalidModelPathProvided(configFile);
                    }
                }

                while (GetDataPath(configFile, out string dataPath) != true) //While data can't be found.
                {
                    if (dataPath == null)
                    {
                        Console.ForegroundColor = ConsoleColor.Black;
                        Console.BackgroundColor = ConsoleColor.Red;
                        Console.WriteLine("The data path provided in config.xml is invalid.");
                        Console.WriteLine("Please provide a new data path or place a file in the following path: ");
                        Console.WriteLine("\t" + configFile.SelectSingleNode("configuration").SelectSingleNode("settings").SelectSingleNode("dataPath").FirstChild.Value);
                        Console.ResetColor();
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Black;
                        Console.BackgroundColor = ConsoleColor.Red;
                        Console.WriteLine("No data path provided in config.xml");
                        Console.WriteLine("Please provide a new data path.");
                        Console.ResetColor();
                    }
                    while (YesNoPrompt("Would you like to provide a new data path?"))
                    {
                        Console.ForegroundColor = ConsoleColor.Black;
                        Console.BackgroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("Please provide a data path: ");
                        Console.ResetColor();
                        string response = Console.ReadLine();
                        if (File.Exists(response))
                        {
                            configFile.SelectSingleNode("configuration").SelectSingleNode("settings").SelectSingleNode("dataPath").FirstChild.Value = response;
                            configFile.Save(configPath);
                            Console.ForegroundColor = ConsoleColor.Black;
                            Console.BackgroundColor = ConsoleColor.Green;
                            Console.WriteLine("Data path added to config.xml.");
                            Console.ResetColor();
                            break;
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Black;
                            Console.BackgroundColor = ConsoleColor.Red;
                            Console.WriteLine("Invalid file path.");
                            Console.ResetColor();
                        }
                    }
                }

                #region Make sure models node exists in config.xml.
                if (!GetModelNodes(configFile, out XmlNode modelNodes))
                {
                    configFile.DocumentElement.AppendChild(configFile.CreateNode(XmlNodeType.Element, "models", null));
                    if (!GetModelNodes(configFile, out modelNodes))
                    {
                        throw new Exception("Error, \"models\" node could not be added and/or does not exist.");
                    }
                    configFile.Save(configPath);
                }
                #endregion

                bool firstLoop = true;

                while ((ModelPaths = GetModelPaths(modelNodes)).Count == 0 || firstLoop) //While there are no models in config.xml OR first run of loop.
                {
                    string[] modelFilesInDir = Directory.GetFiles(modelDir);

                    if (modelFilesInDir.Length > 0)
                    {
                        if (!GetModelNodes(configFile, out modelNodes))
                        {
                            configFile.AppendChild(configFile.CreateNode(XmlNodeType.Element, "models", null));
                            if (!GetModelNodes(configFile, out modelNodes))
                            {
                                throw new Exception("Error, \"models\" node could not be added and/or does not exist.");
                            }
                        }
                        AddModelsToConfig(modelNodes, modelFilesInDir);
                        configFile.Save(configPath);
                    }
                    if (!firstLoop)
                    {
                        Console.ForegroundColor = ConsoleColor.Black;
                        Console.BackgroundColor = ConsoleColor.Red;
                        Console.WriteLine("Model directory could not be found and no direct model paths are available.");
                        Console.ResetColor();

                        PromptChangeModelDirAndAddDirectModelPaths(configFile);
                        configFile.Save(configPath);

                        Console.ForegroundColor = ConsoleColor.Black;
                        Console.BackgroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("Please provide new model files in the following directory if needed: ");
                        Console.WriteLine('"' + configFile.DocumentElement.SelectSingleNode("settings").SelectSingleNode("modelDirectory").FirstChild.Value + '"');
                        Console.ResetColor();
                    }

                    firstLoop = false;
                }

                bool?AreModelPathsValid;

                while ((AreModelPathsValid = CheckModelPathsValid(ref ModelPaths, out List <string> missingModelPaths)) != true)
                {
                    if (AreModelPathsValid == null)
                    {
                        Console.ForegroundColor = ConsoleColor.Black;
                        Console.BackgroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("Some model paths provided in config.xml are invalid.");
                        Console.ResetColor();
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Black;
                        Console.BackgroundColor = ConsoleColor.Red;
                        Console.WriteLine("All model paths provided in config.xml are invalid.");
                        Console.ResetColor();
                    }

                    if (YesNoPrompt("Can you provide paths to missing models?"))
                    {
                        for (int pathIndex = 0; pathIndex < missingModelPaths.Count; ++pathIndex)
                        {
                            Console.ForegroundColor = ConsoleColor.Black;
                            Console.BackgroundColor = ConsoleColor.Yellow;
                            Console.WriteLine("Please provide a new model path to replace this missing path, or press return to skip: ");
                            Console.WriteLine("\t\"" + missingModelPaths[pathIndex] + "\"");
                            Console.ResetColor();

                            string response = Console.ReadLine();
                            if (response != "")
                            {
                                if (File.Exists(response))
                                {
                                    Type responseModelType = GetModelTypeFromFile(File.ReadAllText(response));

                                    if (GetModelByModelPath(modelNodes, missingModelPaths[pathIndex], out XmlNode foundNode))
                                    {
                                        if (responseModelType == GetModelTypeFromNode(foundNode))
                                        {
                                            ReplaceModelPath(modelNodes, response, missingModelPaths[pathIndex]);
                                            missingModelPaths.RemoveAt(pathIndex);
                                            --pathIndex;
                                            configFile.Save(configPath);
                                            Console.ForegroundColor = ConsoleColor.Black;
                                            Console.BackgroundColor = ConsoleColor.Green;
                                            Console.WriteLine("Model path replaced.");
                                            Console.ResetColor();
                                        }
                                        else
                                        {
                                            Console.ForegroundColor = ConsoleColor.Black;
                                            Console.BackgroundColor = ConsoleColor.Red;
                                            Console.WriteLine("Model file provided is not the same type as the model being replaced.");
                                            Console.ResetColor();
                                            --pathIndex;
                                        }
                                    }
                                    else
                                    {
                                        throw new Exception("Missing model path could not be found.");
                                    }
                                }
                                else
                                {
                                    Console.ForegroundColor = ConsoleColor.Black;
                                    Console.BackgroundColor = ConsoleColor.Red;
                                    Console.WriteLine("File provided does not exist.");
                                    Console.ResetColor();
                                    --pathIndex;
                                }
                            }
                            else
                            {
                                Console.WriteLine("Skipped.");
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Model path replacement skipped.");
                    }

                    foreach (string path in missingModelPaths)
                    {
                        ModelPaths.Add(path);
                    }

                    if (CheckModelPathsValid(ref ModelPaths, out missingModelPaths) != true)
                    {
                        if (YesNoPrompt("Do you wish to delete any of the models with missing paths from config.xml?"))
                        {
                            for (int pathIndex = 0; pathIndex < missingModelPaths.Count; ++pathIndex)
                            {
                                if (YesNoPrompt("Do you wish to delete the model with the invalid path \"" + missingModelPaths[pathIndex] + "\" from config.xml?"))
                                {
                                    GetModelByModelPath(modelNodes, missingModelPaths[pathIndex], out XmlNode modelToRemove);
                                    modelNodes.RemoveChild(modelToRemove);
                                    missingModelPaths.RemoveAt(pathIndex);
                                    pathIndex--;
                                    configFile.Save(configPath);

                                    Console.ForegroundColor = ConsoleColor.Black;
                                    Console.BackgroundColor = ConsoleColor.Green;
                                    Console.WriteLine("Model information deleted.");
                                    Console.ResetColor();
                                }
                                else
                                {
                                    Console.WriteLine("Skipped.");
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("Deletion of invalid models skipped.");
                        }
                    }

                    foreach (string path in missingModelPaths)
                    {
                        ModelPaths.Add(path);
                    }

                    if (CheckModelPathsValid(ref ModelPaths, out missingModelPaths) != true)
                    {
                        Console.ForegroundColor = ConsoleColor.Black;
                        Console.BackgroundColor = ConsoleColor.Red;
                        Console.WriteLine("There are still invalid models specified in config.xml.");
                        Console.WriteLine("All invalid models must be resolved to continue.");
                        Console.ResetColor();
                    }

                    foreach (string path in missingModelPaths)
                    {
                        ModelPaths.Add(path);
                    }
                }

                if ((ModelPaths = GetModelPaths(modelNodes)).Count > 0)
                {
                    Console.ForegroundColor = ConsoleColor.Black;
                    Console.BackgroundColor = ConsoleColor.Green;
                    Console.WriteLine("All model paths valid.");
                    Console.ResetColor();

                    ModelPaths = GetModelPaths(modelNodes);
                    return;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Black;
                    Console.BackgroundColor = ConsoleColor.Red;
                    Console.WriteLine("No valid models are available in config.xml.");
                    Console.ResetColor();

                    GetModelDirectory(configFile, out string path);
                    if (!YesNoPrompt("Can you supply model files in the current model directory \"" + path + "\"?"))
                    {
                        Console.ForegroundColor = ConsoleColor.Black;
                        Console.BackgroundColor = ConsoleColor.Red;
                        Console.WriteLine("Please contact a software administrator to obtain missing model files.");
                        Console.WriteLine("Press any key to exit");
                        Console.ReadKey();
                        Environment.Exit(0);
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Black;
                        Console.BackgroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("Please place model files in the model directory. Press return once complete.");
                        Console.ResetColor();
                        Console.ReadLine();
                    }
                }
            }
        }
Example #2
0
 /// <summary>
 /// Worker constructor
 /// </summary>
 /// <param name="v">The v.</param>
 public Worker(AbstractVisualizer v)
 {
     theActivityQueue = new Queue<Activity>();
     judge = new Judge();
     logger = new Logger(theActivityQueue);
     this.visualizer = v;
 }