public static int ComparisonID(SubStrategy firstSubStrategy, SubStrategy secondSubStrategy) { if (firstSubStrategy != null) { if (secondSubStrategy != null) { if (firstSubStrategy.Name == "MainStrategy") { return -1; } else if (secondSubStrategy.Name == "MainStrategy") { return -1; } else return firstSubStrategy.ToString().CompareTo(secondSubStrategy.ToString()); } else return 1; } else { if (secondSubStrategy != null) return -1; else return 0; } }
/// <summary> /// Met à jour la sous stratégie. /// Si l'élément existe dans la stratégie actuelle, elle est mise à jour. /// Si l'élément n'existe pas, l'élément est ajouté /// </summary> /// <param name="newSubStrategy">Nouvelles données à ajouter</param> public void UpdateSubStrategy(SubStrategy newSubStrategy) { bool isUpdated = false; // Verification des paramètres d'entrée if (newSubStrategy != null) { // Verification des données internes if (_subStrategies.Count() > 0) { // On cherche l'élément dans les données internes for (int i = 0; i < _subStrategies.Count(); i++) { if (_subStrategies[i].Name == newSubStrategy.Name) { _subStrategies[i] = newSubStrategy; isUpdated = true; } } } // Si l'élément n'a pas été mis à jour (non trouvé) on l'ajoute if (isUpdated == false) { _subStrategies.Add(newSubStrategy); } } }
/// <summary> /// Met à jour les données de la MainStrategy à partir de l'objet passé en paramètre /// </summary> /// <param name="newStrategy">Données à mettre à jour</param> public void UpdateMainStrategy(SubStrategy newStrategy) { if ((newStrategy != null) && (_subStrategies.Count() > 0)) { _subStrategies[0] = newStrategy.Clone(); _subStrategies[0].Name = PrivateConst.mainStrategyName; } }
/// <summary> /// Récupère la stratégie principale /// </summary> /// <returns>MainStrategy ou null si pas de stratégie n'a été définie</returns> public SubStrategy GetMainStrategy() { SubStrategy Ret = null; if (_subStrategies.Count() > 0) { Ret = _subStrategies[0]; } return(Ret); }
/// <summary> /// Permet de retourner une copie de l'objet courant /// </summary> /// <returns>Une copie de l'objet courant</returns> public SubStrategy Clone() { SubStrategy newObject = new SubStrategy(_subStrategyName, _subStrategyID); if (_actions != null) { foreach (RobotAction currentRobotAction in _actions) { newObject.AddAction(currentRobotAction); } } return(newObject); }
/// <summary> /// Permet de recupérer la stratégie ayant le nom 'subStrategyName' /// </summary> /// <param name="subStrategyName">Nom de la stratégie</param> /// <returns>Null si l'objet n'est pas trouvé</returns> public SubStrategy GetSubStrategy(String subStrategyName) { SubStrategy Ret = null; if (subStrategyName != null) { foreach (SubStrategy currentSubStrategy in _subStrategies) { if (currentSubStrategy.Name == subStrategyName) { Ret = currentSubStrategy; } } } return(Ret); }
public static int ComparisonID(SubStrategy firstSubStrategy, SubStrategy secondSubStrategy) { if (firstSubStrategy != null) { if (secondSubStrategy != null) { if (firstSubStrategy.Name == "MainStrategy") { return(-1); } else if (secondSubStrategy.Name == "MainStrategy") { return(-1); } else { return(firstSubStrategy.ToString().CompareTo(secondSubStrategy.ToString())); } } else { return(1); } } else { if (secondSubStrategy != null) { return(-1); } else { return(0); } } }
private void CreateNewSubStrategy() { SubStrategy newSubStrategy = null; // On récupère un ID int newID = GetFreeSubStrategyID(); if (newID > 0) { // On essaye de charger la stratégie newSubStrategy = new SubStrategy("NewSubStrategy", newID); _currentStrategy.AddSubStrategy(newSubStrategy); _mainModel.UpdateRobotActionList(); } }
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> /// Permet de retourner une copie de l'objet courant /// </summary> /// <returns>Une copie de l'objet courant</returns> public SubStrategy Clone() { SubStrategy newObject = new SubStrategy(_subStrategyName, _subStrategyID); if (_actions != null) { foreach (RobotAction currentRobotAction in _actions) { newObject.AddAction(currentRobotAction); } } return newObject; }
/// <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> /// Ajoute un nouvel objet. Si l'objet est déjà défini, on le met juste à jour /// </summary> /// <param name="newStrategy">Objet à ajouter</param> public void AddSubStrategy(SubStrategy newStrategy) { UpdateSubStrategy(newStrategy); }
/// <summary> /// Export current strategy into an external file (given as parameter) /// </summary> /// <param name="outputFileName">filename of output file</param> /// <param name="patternFileName">pattern file to use for the exportation</param> /// <returns></returns> public int Export(String outputFileName, String patternFileName) { int Ret = -1; int iLine = 0; // iterator bool loopSubStrategyPatternFlag = false; // Flag pour indiquer si la ligne en cours est une ligne de sub-stratégie pattern String currentLine = ""; // Ligne courrante (pour copie et/ou analyse) StructuredFile outputFile = new StructuredFile(); // Fichier de sortie TextFile patternFile = new TextFile(); // Fichier pattern TextFile outputPatternFile = new TextFile(); // Fichier pattern à utiliser pour écrire le fichier de sortie List <String> loopSubStrategyPattern = new List <string>(); // Lignes à répéter pour les subStratégies dans le fichier de sortie try { // Verification des paramètres d'entrée et des paramètres internes if ((outputFileName != null) && (patternFileName != null) && (_subStrategies != null)) { // Chargement du fichier model patternFile.Load(patternFileName); // Verification du fichier pattern et création du fichier réel de sortie if (patternFile.Count() > 0) { // On parcourt tout le fichier et on copie toutes les lignes qui n'appartiennent pas à boucle de stratégie for (iLine = 0; iLine < patternFile.Count(); iLine++) { // Lecture de la ligne currentLine = patternFile.GetLine(iLine); // Analyse de la ligne // Est-ce dans une boucle de lecture de pattern if (loopSubStrategyPatternFlag == true) { if (loopSubStrategyPatternFlag == true) { // A-t-on trouver le flag de fin de boucle if (currentLine.ToUpper().Contains(PrivateConst.subStrategyEndTag.ToUpper()) == true) { // La fin de boucle a été trouvée. On abaisse le flag loopSubStrategyPatternFlag = false; // On duplique le modèle en fonction des données en interne // Pour toutes les sub-stratégies for (int iSubStrategy = 0; iSubStrategy < _subStrategies.Count(); iSubStrategy++) { SubStrategy currentSubStrategy = _subStrategies[iSubStrategy]; // Export des données de la sous-stratégie for (int i = 0; i < currentSubStrategy.Count(); i++) { // Ecriture des infos de la stratégie foreach (String patternLineToAdd in loopSubStrategyPattern) { outputPatternFile.AddLine(patternLineToAdd); } } // Ecriture d'un saut de ligne pour la mise en page outputPatternFile.AddLine(""); } // On ajoute la Tag de fin de boucle outputPatternFile.AddLine(currentLine); } else { // Nous ajoutons la ligne courante au pattern loopSubStrategyPattern.Add(currentLine); } } } else { if (currentLine.ToUpper().Contains(PrivateConst.subStrategyBeginTag.ToUpper()) == true) { loopSubStrategyPatternFlag = true; } // Nous sommes en lecture normale, cette ligne sera copiée dans le fichier de pattern final outputPatternFile.AddLine(currentLine); } } } // Si le fichier pattern est prêt, nous écrivons les données dans le fichier de sortie if (outputPatternFile.Count() > 0) { StructuredFileGroup groupToAdd = null; // Creation des données génériques groupToAdd = new StructuredFileGroup(0); // Creation du groupe générique groupToAdd.AddKey(new StructuredFileKey(PrivateConst.TAG_StrategyName, _strategyName)); groupToAdd.AddKey(new StructuredFileKey(PrivateConst.TAG_DefaultSpeed, _defaultSpeed)); groupToAdd.AddKey(new StructuredFileKey(PrivateConst.TAG_DefaultPivotSpeed, _defaultPivotSpeed)); groupToAdd.AddKey(new StructuredFileKey(PrivateConst.TAG_InitialPosX, _initialPos.param2)); groupToAdd.AddKey(new StructuredFileKey(PrivateConst.TAG_InitialPosY, _initialPos.param3)); groupToAdd.AddKey(new StructuredFileKey(PrivateConst.TAG_InitialPosA, _initialPos.param4)); // Ajout du groupe outputFile.AddGroup(groupToAdd); // Creation des données spécifiques if (_subStrategies.Count() > 0) { // Pour chaque sous stratégies foreach (SubStrategy currentSubStrategy in _subStrategies) { String currentSubStrategyName = currentSubStrategy.Name; if (currentSubStrategy.Count() > 0) { // Pour chacune des actions de la stratégie foreach (RobotAction currentAction in currentSubStrategy.GetAllActions()) { groupToAdd = currentAction.Export(); groupToAdd.AddKey(new StructuredFileKey(PrivateConst.TAG_SubStrategyName, currentSubStrategyName)); outputFile.AddGroup(groupToAdd); } } } } // Ecriture du fichier final outputFile.SetPatternFile(outputPatternFile); outputFile.Export(outputFileName); // Sauvegarde des données Save(); } } } catch (Exception ex) { _debugTool.WriteLine("Strategy Export() : " + ex.Message); Ret = -1; } return(Ret); }
/// <summary> /// Charge les informations de la stratégie donnée en paramètre /// </summary> /// <param name="strategyName">Nom de la stratégie à chargée</param> public void Load(String strategyName) { try { String inputDir = Directory.GetCurrentDirectory().ToString() + "/" + PrivateConst.outputDir + strategyName + "/"; StructuredFile mainFile = new StructuredFile(); StructuredFileGroup currentGroup = null; StructuredFileKey currentKey = null; SubStrategy currentSubStrategy = null; // Verification du répertoire d'import if (Directory.Exists(inputDir) == true) { // Chargement des données générale mainFile.Load(inputDir + strategyName + ".strategy"); if (mainFile != null) { // Lecture des informations générales --------------------------------------------------------- // On ne charge les données que si le fichier de base est disponible currentGroup = mainFile.GetGroupByID(0); if (currentGroup != null) { // Chargement des données générale currentKey = currentGroup.GetFirstKey(PrivateConst.mainStrategyName); if (currentKey != null) { _strategyName = currentKey.valueString; } else { _strategyName = "NewStrategy"; } // InitPosX currentKey = currentGroup.GetFirstKey(PrivateConst.TAG_InitialPosX); if (currentKey != null) { _initialPos.param2 = currentKey.valueString; } else { _initialPos.param2 = "1500"; } // InitPosY currentKey = currentGroup.GetFirstKey(PrivateConst.TAG_InitialPosY); if (currentKey != null) { _initialPos.param3 = currentKey.valueString; } else { _initialPos.param3 = "1000"; } // InitPosA currentKey = currentGroup.GetFirstKey(PrivateConst.TAG_InitialPosA); if (currentKey != null) { _initialPos.param4 = currentKey.valueString; } else { _initialPos.param4 = "0"; } // DefaultSpeed currentKey = currentGroup.GetFirstKey(PrivateConst.TAG_DefaultSpeed); if (currentKey != null) { _defaultSpeed = currentKey.valueInt; } else { _defaultSpeed = 50; } // DefaultPivotSpeed currentKey = currentGroup.GetFirstKey(PrivateConst.TAG_DefaultPivotSpeed); if (currentKey != null) { _defaultPivotSpeed = currentKey.valueInt; } else { _defaultPivotSpeed = 50; } } // Lecture des sous-stratégies ---------------------------------------------------------------- // Récupération des noms de fichier IEnumerable <String> inputFiles = Directory.EnumerateFiles(inputDir, "*.sfile"); foreach (String currentFile in inputFiles) { // Chargement du fichier currentSubStrategy = new SubStrategy("NewSubStrategy", 1); currentSubStrategy.Load(currentFile, -1); if (currentSubStrategy.Name == PrivateConst.mainStrategyName) { _subStrategies[0] = currentSubStrategy; } else { _subStrategies.Add(currentSubStrategy); } } } } else { _debugTool.WriteLine("Strategy.cs = Directory '" + strategyName + "' not found !"); } } catch (Exception ex) { _debugTool.WriteLine("Strategy.cs = " + ex.Message); } }
private void LoadNewSubStrategy() { String importFile = null; SubStrategy subStrategyToImport = null; // Lance la fenetre d'import OpenFileDialog importWindow = new OpenFileDialog(); importWindow.InitialDirectory = Directory.GetCurrentDirectory(); importWindow.Filter = "OufffTEAM StructuredFile|*.sfile"; if (importWindow.ShowDialog() == DialogResult.OK) importFile = importWindow.FileName; // On récupère un ID int newID = GetFreeSubStrategyID(); if ((importFile != null) && (newID > 0)) { // On essaye de charger la stratégie subStrategyToImport = new SubStrategy("NewStrategy", newID); if (subStrategyToImport.Load(importFile, newID) > 0) { _currentStrategy.AddSubStrategy(subStrategyToImport); _mainModel.UpdateRobotActionList(); } } importWindow.Dispose(); }
/// <summary> /// Met à jour la sous stratégie. /// Si l'élément existe dans la stratégie actuelle, elle est mise à jour. /// Si l'élément n'existe pas, l'élément est ajouté /// </summary> /// <param name="newSubStrategy">Nouvelles données à ajouter</param> public void UpdateSubStrategy(SubStrategy newSubStrategy) { bool isUpdated = false; // Verification des paramètres d'entrée if (newSubStrategy != null) { // Verification des données internes if (_subStrategies.Count() > 0) { // On cherche l'élément dans les données internes for (int i = 0; i < _subStrategies.Count(); i++) { if (_subStrategies[i].Name == newSubStrategy.Name) { _subStrategies[i] = newSubStrategy; isUpdated = true; } } } // Si l'élément n'a pas été mis à jour (non trouvé) on l'ajoute if(isUpdated == false) _subStrategies.Add(newSubStrategy); } }
/// <summary> /// Charge les informations de la stratégie donnée en paramètre /// </summary> /// <param name="strategyName">Nom de la stratégie à chargée</param> public void Load(String strategyName) { try { String inputDir = Directory.GetCurrentDirectory().ToString() + "/" + PrivateConst.outputDir + strategyName + "/"; StructuredFile mainFile = new StructuredFile(); StructuredFileGroup currentGroup = null; StructuredFileKey currentKey = null; SubStrategy currentSubStrategy = null; // Verification du répertoire d'import if (Directory.Exists(inputDir) == true) { // Chargement des données générale mainFile.Load(inputDir + strategyName + ".strategy"); if (mainFile != null) { // Lecture des informations générales --------------------------------------------------------- // On ne charge les données que si le fichier de base est disponible currentGroup = mainFile.GetGroupByID(0); if (currentGroup != null) { // Chargement des données générale currentKey = currentGroup.GetFirstKey(PrivateConst.mainStrategyName); if (currentKey != null) _strategyName = currentKey.valueString; else _strategyName = "NewStrategy"; // InitPosX currentKey = currentGroup.GetFirstKey(PrivateConst.TAG_InitialPosX); if (currentKey != null) _initialPos.param2 = currentKey.valueString; else _initialPos.param2 = "1500"; // InitPosY currentKey = currentGroup.GetFirstKey(PrivateConst.TAG_InitialPosY); if (currentKey != null) _initialPos.param3 = currentKey.valueString; else _initialPos.param3 = "1000"; // InitPosA currentKey = currentGroup.GetFirstKey(PrivateConst.TAG_InitialPosA); if (currentKey != null) _initialPos.param4 = currentKey.valueString; else _initialPos.param4 = "0"; // DefaultSpeed currentKey = currentGroup.GetFirstKey(PrivateConst.TAG_DefaultSpeed); if (currentKey != null) _defaultSpeed = currentKey.valueInt; else _defaultSpeed = 50; // DefaultPivotSpeed currentKey = currentGroup.GetFirstKey(PrivateConst.TAG_DefaultPivotSpeed); if (currentKey != null) _defaultPivotSpeed = currentKey.valueInt; else _defaultPivotSpeed = 50; } // Lecture des sous-stratégies ---------------------------------------------------------------- // Récupération des noms de fichier IEnumerable<String> inputFiles = Directory.EnumerateFiles(inputDir, "*.sfile"); foreach (String currentFile in inputFiles) { // Chargement du fichier currentSubStrategy = new SubStrategy("NewSubStrategy", 1); currentSubStrategy.Load(currentFile, -1); if (currentSubStrategy.Name == PrivateConst.mainStrategyName) _subStrategies[0] = currentSubStrategy; else _subStrategies.Add(currentSubStrategy); } } } else { _debugTool.WriteLine("Strategy.cs = Directory '" + strategyName + "' not found !"); } } catch (Exception ex) { _debugTool.WriteLine("Strategy.cs = " + ex.Message); } }
/// <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; }