static void Main(string[] args) { bool Validated = true; // ############################################################################################################################ // ##### ActiveSensors // ############################################################################################################################ #region ActiveSensors ActiveSensors testActiveSensors = new ActiveSensors(); // Test 0200 : ActiveSensors __________________________________________________________________________________________________ try { DisplaySeparator(); Console.WriteLine("Test 0200 - Verification des objets ActiveSensors : \n"); if (testActiveSensors.Activated == "(APP_PARAM_STRATEGYFLAG_NONE)") { Console.Write(" Ok\n"); } else { Console.Write(" Error !\n"); Validated = false; } testActiveSensors.ActivateSensors("APP_PARAM_STRATEGYFLAG_COLLISION_FRONT"); testActiveSensors.ActivateSensors("APP_PARAM_STRATEGYFLAG_COLLISION_FRONT"); testActiveSensors.ActivateSensors("APP_PARAM_STRATEGYFLAG_COLLISION_REAR"); if (testActiveSensors.Activated == "(APP_PARAM_STRATEGYFLAG_COLLISION_FRONT + APP_PARAM_STRATEGYFLAG_COLLISION_REAR)") { Console.Write(" Ok\n"); } else { Console.Write(" Error !\n"); Validated = false; } testActiveSensors.DesactivateSensors("APP_PARAM_STRATEGYFLAG_COLLISION_FRONT"); if (testActiveSensors.Activated == "(APP_PARAM_STRATEGYFLAG_COLLISION_REAR)") { Console.Write(" Ok\n"); } else { Console.Write(" Error !\n"); Validated = false; } testActiveSensors.DesactivateAllSensors(); if (testActiveSensors.Activated == "(APP_PARAM_STRATEGYFLAG_NONE)") { Console.Write(" Ok\n"); } else { Console.Write(" Error !\n"); Validated = false; } testActiveSensors.ActivateSensors("APP_PARAM_STRATEGYFLAG_COLLISION_FRONT"); testActiveSensors.ForceSensors("APP_PARAM_STRATEGYFLAG_COLLISION_REAR"); if (testActiveSensors.Activated == "(APP_PARAM_STRATEGYFLAG_COLLISION_REAR)") { Console.Write(" Ok\n"); } else { Console.Write(" Error !\n"); Validated = false; } } catch (Exception ex) { Console.WriteLine("Test 0200 Failed !!"); Console.WriteLine(ex.Message); Validated = false; } #endregion // ############################################################################################################################ // ##### RobotAction // ############################################################################################################################ #region RobotAction RobotAction testAction = new RobotAction(); // Test 0201 : RobotAction ____________________________________________________________________________________________________ try { DisplaySeparator(); Console.WriteLine("Test 0201 - Verification des objets Action : \n"); Console.WriteLine(" -> Verification des private"); // ------------------------------------------------------------------------------------------ if (testAction.CheckPrivateFunction()) { Console.Write(" Ok\n"); } else { Console.Write(" Error !\n"); Validated = false; } // ------------------------------------------------------------------------------------------ Console.WriteLine(" -> Test de la fonction d'export() : "); testAction.ID = 1; testAction.cmd = EnumCmd.Mvt_UseMixedMode; testAction.cmdType = EnumCmdType.CmdType_Blocking; testAction.param1 = "11"; testAction.param2 = "12"; testAction.param3 = "13"; testAction.param4 = "14"; testAction.nextID = 100; testAction.timeoutID = 200; StructuredFileGroup output = testAction.Export(); if (output != null) { Console.Write(" Ok\n"); } else { Console.Write(" Error !\n"); Validated = false; } // ------------------------------------------------------------------------------------------ Console.WriteLine(" -> Test de la fonction d'import() : "); testAction = new RobotAction(); StructuredFileGroup inputData = new StructuredFileGroup(10); inputData.AddKey(new StructuredFileKey("Cmd", "Mvt_UseMixedMode")); inputData.AddKey(new StructuredFileKey("CmdType", "Blocking")); inputData.AddKey(new StructuredFileKey("Param1", 50)); inputData.AddKey(new StructuredFileKey("Param2", 100)); inputData.AddKey(new StructuredFileKey("Param3", 200)); inputData.AddKey(new StructuredFileKey("Param4", 90.0)); inputData.AddKey(new StructuredFileKey("ActiveSensors", "COLLISION_FRONT")); inputData.AddKey(new StructuredFileKey("nextID", 10)); inputData.AddKey(new StructuredFileKey("timeoutID", 30)); if (testAction.Import(inputData) == true) { Console.Write(" Ok\n"); } else { Console.Write(" Error !\n"); Validated = false; } } catch (Exception ex) { Console.WriteLine("Test 0201 Failed !!"); Console.WriteLine(ex.Message); Validated = false; } #endregion // ############################################################################################################################ // ##### SubStrategy // ############################################################################################################################ #region SubStrategy // Test 0202 : SubStrategy ____________________________________________________________________________________________________ try { DisplaySeparator(); // ------------------------------------------------------------------------------------------ SubStrategy strategy1 = new SubStrategy("Strategy 1", 1); SubStrategy strategy2 = new SubStrategy("Strategy 2", 1); // Creation de l'action 1 RobotAction Action1 = new RobotAction(1); Action1.cmd = EnumCmd.Mvt_UseMixedMode; Action1.cmdType = EnumCmdType.CmdType_Blocking; Action1.param1 = "1"; Action1.param2 = "2"; Action1.param3 = "3"; Action1.param4 = "4"; Action1.nextID = -1; Action1.timeoutID = -1; RobotAction Action10 = new RobotAction(10); Action1.cmd = EnumCmd.Mvt_UseMixedMode; Action1.cmdType = EnumCmdType.CmdType_Blocking; Action1.param1 = "10"; Action1.param2 = "20"; Action1.param3 = "30"; Action1.param4 = "40"; Action1.nextID = -1; Action1.timeoutID = -1; RobotAction Action2 = new RobotAction(2); Action2.cmd = EnumCmd.App_Wait; Action2.cmdType = EnumCmdType.CmdType_Blocking; Action2.param1 = "4"; Action2.param2 = "3"; Action2.param3 = "2"; Action2.param4 = "1"; Action2.nextID = -1; Action2.timeoutID = -1; strategy1.AddAction(Action1); strategy1.AddAction(Action2); strategy1.AddAction(Action10); strategy2.AddAction(Action1); strategy2.AddAction(Action2); strategy2.AddAction(Action10); Console.WriteLine("Test 0202 - SubStrategy : "); if (strategy1.Count() == 3) { Console.Write(" Ok\n"); } else { Console.Write(" Error !\n"); Validated = false; } Console.WriteLine(" -> Suppression d'une action : "); strategy1.RemoveAction(10); strategy2.RemoveAction(20); if ((strategy1.Count() == 2) && (strategy2.Count() == 3)) { Console.Write(" Ok\n"); } else { Console.Write(" Error !\n"); Validated = false; } Console.WriteLine(" -> Recherche d'une action : "); if ((strategy1.GetAction(1) != null) && (strategy1.GetAction(20) == null)) { Console.Write(" Ok\n"); } else { Console.Write(" Error !\n"); Validated = false; } Console.WriteLine(" -> Ajout d'une action existante : "); strategy2.AddAction(Action1); if (strategy2.Count() == 3) { Console.Write(" Ok\n"); } else { Console.Write(" Error !\n"); Validated = false; } Console.WriteLine(" -> Update d'une action existante : "); if ((strategy1.UpdateAction(1, Action1) == true) && (strategy1.UpdateAction(1, Action2) == false) && (strategy1.UpdateAction(50, new RobotAction(40)) == false)) { Console.Write(" Ok\n"); } else { Console.Write(" Error !\n"); Validated = false; } } catch (Exception ex) { Console.WriteLine("Test 0202 Failed !!"); Console.WriteLine(ex.Message); Validated = false; } #endregion // ############################################################################################################################ // ##### Strategy // ############################################################################################################################ #region Strategy // Test 0203 : Strategy _______________________________________________________________________________________________________ try { DisplaySeparator(); // ------------------------------------------------------------------------------------------ SubStrategy subStrategy1 = new SubStrategy("Strategy 1", 1); SubStrategy subStrategy2 = new SubStrategy("Strategy 2", 1); Strategy testStrategy = new Strategy("Strategy"); Strategy testLoadStrategy = new Strategy("LoadStrategy"); // Creation de l'action 1 RobotAction Action1 = new RobotAction(1); Action1.cmd = EnumCmd.Mvt_UseMixedMode; Action1.cmdType = EnumCmdType.CmdType_Blocking; Action1.param1 = "1"; Action1.param2 = "2"; Action1.param3 = "3"; Action1.param4 = "4"; Action1.nextID = -1; Action1.timeoutID = -1; RobotAction Action10 = new RobotAction(10); Action1.cmd = EnumCmd.Mvt_UseMixedMode; Action1.cmdType = EnumCmdType.CmdType_Blocking; Action1.param1 = "10"; Action1.param2 = "20"; Action1.param3 = "30"; Action1.param4 = "40"; Action1.nextID = -1; Action1.timeoutID = -1; RobotAction Action2 = new RobotAction(2); Action2.cmd = EnumCmd.App_Wait; Action2.cmdType = EnumCmdType.CmdType_Blocking; Action2.param1 = "4"; Action2.param2 = "3"; Action2.param3 = "2"; Action2.param4 = "1"; Action2.nextID = -1; Action2.timeoutID = -1; subStrategy1.AddAction(Action1); subStrategy1.AddAction(Action2); subStrategy1.AddAction(Action10); subStrategy2.AddAction(Action1); subStrategy2.AddAction(Action2); subStrategy2.AddAction(Action10); Console.WriteLine("Test 0203 - Strategy : "); Console.WriteLine(" -> Add + Update : "); testStrategy.UpdateMainStrategy(subStrategy1); testStrategy.UpdateSubStrategy(subStrategy1); Action1.cmd = EnumCmd.Mvt_Stop; subStrategy1.UpdateAction(Action1.ID, Action1); testStrategy.UpdateSubStrategy(subStrategy1); testStrategy.AddSubStrategy(subStrategy2); if (testStrategy.GetAllSubStrategy().Count == 2) { Console.Write(" Ok\n"); } else { Console.Write(" Error !\n"); Validated = false; } Console.WriteLine(" -> Remove : "); testStrategy.RemoveSubStrategy("Strategy 2"); if (testStrategy.GetAllSubStrategy().Count == 1) { Console.Write(" Ok\n"); } else { Console.Write(" Error !\n"); Validated = false; } Console.WriteLine(" -> Remove : "); testStrategy.Clear(); if (testStrategy.GetAllSubStrategy() == null) { Console.Write(" Ok\n"); } else { Console.Write(" Error !\n"); Validated = false; } Console.WriteLine(" -> Save : "); testStrategy.UpdateMainStrategy(subStrategy1); testStrategy.AddSubStrategy(subStrategy1); testStrategy.AddSubStrategy(subStrategy2); testStrategy.Save(); if (true) { Console.Write(" Ok (Verification manuelle du dossier de sortie)\n"); } Console.WriteLine(" -> Load : "); testLoadStrategy.Load("Strategy"); if (testLoadStrategy.GetAllSubStrategy().Count == 2) { Console.Write(" Ok\n"); } else { Console.Write(" Error !\n"); Validated = false; } Console.WriteLine(" -> Export : "); RobotAction Action100 = new RobotAction(100); Action100.cmd = EnumCmd.App_Wait; Action100.cmdType = EnumCmdType.CmdType_Blocking; Action100.param1 = "40"; Action100.param2 = "30"; Action100.param3 = "20"; Action100.param4 = "10"; Action100.nextID = 101; Action100.timeoutID = 901; RobotAction Action101 = new RobotAction(101); Action101.cmd = EnumCmd.Mvt_UseMixedMode; Action101.cmdType = EnumCmdType.CmdType_NonBlocking; Action101.param1 = "41"; Action101.param2 = "31"; Action101.param3 = "21"; Action101.param4 = "11.5"; Action101.nextID = 102; Action101.timeoutID = 902; RobotAction Action102 = new RobotAction(102); Action102.cmd = EnumCmd.MvtSimple_MoveInMM; Action102.cmdType = EnumCmdType.CmdType_Blocking; Action102.param1 = "42"; Action102.param2 = "32"; Action102.param3 = "22"; Action102.param4 = "12"; Action102.nextID = 103; Action102.timeoutID = 903; RobotAction Action103 = new RobotAction(103); Action103.cmd = EnumCmd.Mvt_UsePivotMode; Action103.cmdType = EnumCmdType.CmdType_NonBlocking; Action103.param1 = "43"; Action103.param2 = "33"; Action103.param3 = "23"; Action103.param4 = "13"; Action103.nextID = 104; Action103.timeoutID = 904; RobotAction Action200 = new RobotAction(200); Action200.cmd = EnumCmd.Mvt_UseSpline; Action200.cmdType = EnumCmdType.CmdType_NonBlocking; Action200.param1 = "44"; Action200.param2 = "34"; Action200.param3 = "24"; Action200.param4 = "14"; Action103.nextID = -1; Action200.timeoutID = 905; SubStrategy subStrategyExport1 = new SubStrategy("Export Strategy 1", 1); SubStrategy subStrategyExport2 = new SubStrategy("Export Strategy 2", 1); subStrategyExport1.AddAction(Action100); subStrategyExport1.AddAction(Action101); subStrategyExport2.AddAction(Action102); subStrategyExport2.AddAction(Action103); subStrategyExport2.AddAction(Action200); Strategy testExportStrategy = new Strategy("EXPORT_STRATEGY"); testExportStrategy.AddSubStrategy(subStrategyExport1); testExportStrategy.AddSubStrategy(subStrategyExport2); testExportStrategy.Export("./Data/TestOutputStrategy.c", "./patternFiles/ImportPatternFile.spattern"); if (true) { Console.Write(" Ok\n"); } else { Console.Write(" Error !\n"); Validated = false; } Console.WriteLine(" -> Import : "); Strategy testImportStrategy = new Strategy("ImportStrategy"); testImportStrategy.Import("./Data/TestOutputStrategy.c", "./patternFiles/ImportPatternFile.spattern"); testExportStrategy.Export("./Data/TestOutputStrategy_2.c", "./patternFiles/ImportPatternFile.spattern"); if (true) { Console.Write(" Ok\n"); } else { Console.Write(" Error !\n"); Validated = false; } } catch (Exception ex) { Console.WriteLine("Test 0203 Failed !!"); Console.WriteLine(ex.Message); Validated = false; } #endregion // Fin du programme ----------------------------------------------------------------------------- DisplaySeparator(); Console.Write("\nTest terminé. Etat du module : "); if (Validated == true) { Console.Write("Validé ! \n"); } else { Console.Write("Invalide !\n"); } Console.Read(); }
/// <summary> /// Import a new strategy from an external file (given as parameter) /// </summary> /// <param name="inputFileName">filename of internal file</param> /// <param name="patternFileName">pattern file to use for the importation</param> /// <returns></returns> public int Import(String inputFileName, String patternFileName) { int Ret = -1; int patternLineIndex = 0; int importLineIndex = 0; int minLineIndex = 0; int nbLineToRemove = 0; int currentLineIndex = 0; int counter = 0; TextFile patternFile = new TextFile(); // Fichier pattern pour l'import des données TextFile fileToImport = new TextFile(); // Fichier à importer StructuredFile importFile = new StructuredFile(); // Fichier utiliser pour lire les données List <String> patternLoop = new List <string>(); // Pour stocker les lignes du pattern à repeter dans la rubrique LOOP String lineRead = ""; // Variable pour lire les données du fichier StructuredFileKey currentKey = null; try { // Verification des paramètres if ((inputFileName != null) && (patternFileName != null)) { // Lecture du fichier modele patternFile.Load(patternFileName); // Lecture du fichier à importer fileToImport.Load(inputFileName); // Si les fichiers sont valides, on les prépare pour les importer if ((patternFile.Count() > 0) && (fileToImport.Count() > 0)) { #region PreparationDesFichiers // Suppression des tabulations patternFile.ReplaceInFile("\t", ""); fileToImport.ReplaceInFile("\t", ""); // Suppression des lignes inutiles patternFile.RemoveEmptyLine(); fileToImport.RemoveEmptyLine(); // Mise en correspondance des fichiers // Recherche de l'index du début de boucle importLineIndex = fileToImport.GetFirstIndexLine(PrivateConst.subStrategyBeginTag, 0); patternLineIndex = patternFile.GetFirstIndexLine(PrivateConst.subStrategyBeginTag, 0); // Verification de la presence de la boucle dans les 2 fichiers if ((importLineIndex >= 0) && (patternLineIndex >= 0)) { // Lecture du minimum de ligne minLineIndex = Math.Min(importLineIndex, patternLineIndex); // Verification si un ecart existe dans le positionnement du tag de debut de boucle if (importLineIndex != patternLineIndex) { // Suppression des lignes en trop dans le fichier d'import nbLineToRemove = importLineIndex - minLineIndex; for (int i = 0; i < nbLineToRemove; i++) { fileToImport.RemoveLine(minLineIndex); } // Suppression des lignes en trop dans le fichier de modèle nbLineToRemove = patternLineIndex - minLineIndex; for (int i = 0; i < nbLineToRemove; i++) { patternFile.RemoveLine(minLineIndex); } } // Creation des boucles // 1 - Lecture des lignes du modèle do { // Lecture de la ligne lineRead = patternFile.GetLine(minLineIndex + 1); // Verification du contenu if ((lineRead != null) && (lineRead.Contains(PrivateConst.subStrategyBeginTag) == false) && (lineRead.Contains(PrivateConst.subStrategyEndTag) == false)) { patternLoop.Add(lineRead); // Ajout de la ligne courante patternFile.RemoveLine(minLineIndex + 1); } }while ((lineRead != null) && (lineRead.Contains(PrivateConst.subStrategyEndTag) == false)); if (patternLoop.Count() > 0) { // 2 - Duplication des données du modele en fonction des données du fichier à importer currentLineIndex = minLineIndex + 1; // On se place au début de la boucle counter = 0; // Initialisation du compteur pour dupliquer le pattern do { // Lecture de la ligne lineRead = fileToImport.GetLine(currentLineIndex); if ((lineRead != null) && (lineRead.Contains(PrivateConst.subStrategyBeginTag) == false) && (lineRead.Contains(PrivateConst.subStrategyEndTag) == false)) { // Verification du compteur if (counter >= patternLoop.Count()) { counter = 0; } // Ajout de la ligne de pattern en conséquence patternFile.AddLine(patternLoop[counter], currentLineIndex); counter = counter + 1; } currentLineIndex = currentLineIndex + 1; }while ((lineRead != null) && (lineRead.Contains(PrivateConst.subStrategyEndTag) == false)); } } // Ajout des lignes dans le fichier d'import si les tailles ne correspondent pas while (patternFile.Count() > fileToImport.Count()) { fileToImport.AddLine("EMPTY LINE"); } // Ajout des lignes dans le fichier pattern si les tailles ne correspondent pas while (fileToImport.Count() > patternFile.Count()) { patternFile.AddLine("EMPTY LINE"); } #endregion #region LectureDesDonnees // Ajout du fichier pattern importFile.SetPatternFile(patternFile); // Lecture du fichier importFile.Import(fileToImport); #endregion #region TraitementDesDonnees if (importFile.Count() > 0) { // Lecture des groupes un par un foreach (StructuredFileGroup currentGroup in importFile.GetAllGroup()) { // Lecture de la clé contenant le nom de la Stategy currentKey = currentGroup.GetFirstKey(PrivateConst.TAG_SubStrategyName); // Si la clé courante fait partie d'une stratégie if (currentKey != null) { // Si le group actuel appartient à une stratégie, lecture du nom de la stratégie String subStrategyName = currentKey.valueString; RobotAction actionToImport = new RobotAction(); // Si il s'agit d'une action à ajouter à la stratégie if (actionToImport.Import(currentGroup) == true) { bool isAdded = false; for (int i = 0; i < _subStrategies.Count(); i++) { // Si la clé lue appartient à la stratégie courante, on l'ajoute if (subStrategyName == _subStrategies[i].Name) { _subStrategies[i].AddAction(actionToImport); isAdded = true; } } // Si la clé n'a pas été ajoutée, on l'ajoute if (isAdded == false) { int subStrategyID = Convert.ToInt32(Math.Floor(actionToImport.ID / 100.0)); SubStrategy subStrategyToAdd = new SubStrategy(subStrategyName, subStrategyID); subStrategyToAdd.AddAction(actionToImport); _subStrategies.Add(subStrategyToAdd); } } } else { // Sinon la clé est ajoutée aux variables globales de l'appli (si la clé est nécessaire) // Lecture du nom de la stratégie currentKey = currentGroup.GetFirstKey(PrivateConst.TAG_StrategyName); if (currentKey != null) { _strategyName = currentKey.valueString; } // Variables pour la position initiale currentKey = currentGroup.GetFirstKey(PrivateConst.TAG_InitialPosX); if (currentKey != null) { _initialPos.param2 = currentKey.valueString; } currentKey = currentGroup.GetFirstKey(PrivateConst.TAG_InitialPosY); if (currentKey != null) { _initialPos.param3 = currentKey.valueString; } currentKey = currentGroup.GetFirstKey(PrivateConst.TAG_InitialPosA); if (currentKey != null) { _initialPos.param4 = currentKey.valueString; } // Valeur par défaut currentKey = currentGroup.GetFirstKey(PrivateConst.TAG_DefaultSpeed); if (currentKey != null) { _defaultSpeed = currentKey.valueInt; } // Valeur par défaut currentKey = currentGroup.GetFirstKey(PrivateConst.TAG_DefaultPivotSpeed); if (currentKey != null) { _defaultPivotSpeed = currentKey.valueInt; } } } } #endregion } } } catch (Exception ex) { _debugTool.WriteLine("Strategy Import() : " + ex.Message); Ret = -1; } return(Ret); }
/// <summary> /// Charge les informations de la substrategy à partir du fichier spécifié en paramètre /// Le chargement d'un fichier détruit toutes les données précédemment sauvegardée /// </summary> /// <param name="filename">Nom du fichier pour charger le fichier</param> /// <param name="forcedID">ID à utiliser à la place de l'ID du fichier</param> /// <returns>Retourne le nombre d'action chargée, -1 en cas d'erreur</returns> public int Load(String filename, int forcedID) { int Ret = -1; StructuredFile inputFile = new StructuredFile(); StructuredFileGroup globalGroup = null; List <StructuredFileGroup> actionsGroup = null; StructuredFileKey currentKey = null; RobotAction currentAction = null; if (filename != null) { // Ouverture du fichier inputFile.Load(filename); // Destruction des données actuelles _subStrategyName = "Not Set"; _actions = null; // Chargement des données génériques globalGroup = inputFile.GetGroupByID(0); if (globalGroup != null) { currentKey = globalGroup.GetFirstKey("SubStrategyName"); if (currentKey != null) { _subStrategyName = currentKey.valueString; } if (forcedID > 0) { // Un ID a été imposé, nous l'utilisons _subStrategyID = forcedID; } else { // Nous utilisons l'ID du fichier currentKey = globalGroup.GetFirstKey("SubStrategyID"); if (currentKey != null) { _subStrategyID = currentKey.valueInt; } } } // Chargement des données liées aux actions actionsGroup = inputFile.GetAllGroup(); if (actionsGroup != null) { Ret = 0; // Lecture de tous les groupes pour extraire les données liées aux actions foreach (StructuredFileGroup currentGroup in actionsGroup) { // Nous ne devons extraire que les données des groupes > à 0 if (currentGroup.ID > 0) { currentAction = new RobotAction(); currentAction.Import(currentGroup); // On ajoute l'objet que l'on vient d'importer s'il est valide if (currentAction.cmd != EnumCmd.NotSet) { if (_actions == null) { _actions = new List <RobotAction>(); } // Verification des ID currentAction.ID = CheckID(currentAction.ID); currentAction.nextID = CheckLinkID(currentAction.nextID); currentAction.timeoutID = CheckLinkID(currentAction.timeoutID); _actions.Add(currentAction); Ret = Ret + 1; } } } } } SortRobotActions(); return(Ret); }