From 442f66d7ac71a6031cf9352c41bb2b1a69706498 Mon Sep 17 00:00:00 2001
From: jerray <jawher.jerray@eurecom.fr>
Date: Mon, 6 Nov 2023 19:15:38 +0100
Subject: [PATCH] update cli pattern handling

---
 src/main/java/cli/PatternHandling.java | 111 ++++++++++++++++---------
 1 file changed, 73 insertions(+), 38 deletions(-)

diff --git a/src/main/java/cli/PatternHandling.java b/src/main/java/cli/PatternHandling.java
index 488bb91bf6..c9cfcf2bc3 100644
--- a/src/main/java/cli/PatternHandling.java
+++ b/src/main/java/cli/PatternHandling.java
@@ -1,14 +1,18 @@
 package cli;
 
+import java.io.File;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Vector;
 
 import tmltranslator.TMLMapping;
 import tmltranslator.TMLMappingTextSpecification;
 import tmltranslator.TMLModeling;
 import tmltranslator.TMLTask;
+import tmltranslator.patternhandling.PatternCloneTask;
+import tmltranslator.patternhandling.PatternConfiguration;
 
 /**
  * Class PatternHandling
@@ -19,6 +23,11 @@ import tmltranslator.TMLTask;
  */
 
 public class PatternHandling extends Command {
+    private final static String NO_NAME_NO_TASK_FOR_PATTERN = "No name or no tasks are giving for the pattern";
+
+    private final static String PATTERN_NOT_EXIST = "Pattern does not exist";
+    private final static String CLONE_TASK_EXIST = "Cloned task already exists";
+    private final static String TASK_TO_CLONE_NOT_EXIST = "Task to clone does not exist";
     // PatternHandling commands
     private final static String CREATE = "create";
     private final static String CONFIGURE = "select-configure";
@@ -29,6 +38,11 @@ public class PatternHandling extends Command {
     private String patternName, patternsPath = "../ttool/build/resources/main/patterns/";
     private HashSet<String> selectedTasks = new HashSet<String>();
     private HashSet<String> allTasksOfModel = new HashSet<String>();
+
+
+    private Vector<String> listPatterns = new Vector<String>();
+    private String selectedPatternPath, selectedPatternName;
+    private PatternConfiguration patternConfiguration;
         
     public PatternHandling() {
 
@@ -75,7 +89,7 @@ public class PatternHandling extends Command {
                 return "[OPTION]... [NAME] [TASKS]\n"
                 + "-n NAME\tset a name for the pattern to create\n"
                 + "-a TASKS\tselect tasks to add to the pattern (seperated by a space)\n"
-                + "-l \tget the list of selected tasks to add to the pattern\n"
+                + "-l \tget the list of selected tasks to be added to the pattern\n"
                 + "-t \tget all tasks of the model\n"
                 + "-r TASKS\tremove selected tasks\n"
                 + "-m \tmake the pattern\n";
@@ -125,7 +139,7 @@ public class PatternHandling extends Command {
                                 if (allTasksOfModel.contains(argumentOfOption)) {
                                     selectedTasks.add(argumentOfOption);
                                 } else {
-                                    interpreter.print("The task " + argumentOfOption + "does not exist, it will not be added.");
+                                    interpreter.print("The task " + argumentOfOption + " does not exist, it will not be added.");
                                 }
                             }
                             break;
@@ -137,9 +151,9 @@ public class PatternHandling extends Command {
                             if (argumentsOfOption.size() != 0) {
                                 return Interpreter.BAD;
                             }
-                            interpreter.print("The selected tasks are :\n");
+                            interpreter.print("The selected tasks are :");
                             String printSelectedTasks = "";
-                            for (int ind = 0; i < selectedTasks.size(); i++) {
+                            for (int ind = 0; ind < selectedTasks.size(); ind++) {
                                 if (ind == 0) {
                                     printSelectedTasks = (String) selectedTasks.toArray()[ind];
                                 } else {
@@ -157,9 +171,9 @@ public class PatternHandling extends Command {
                                 return Interpreter.BAD;
                             }
                             
-                            interpreter.print("This model contains " + allTasksOfModel.size() + " task(s) :\n");
+                            interpreter.print("This model contains " + allTasksOfModel.size() + " task(s) :");
                             String printTasks = "";
-                            for (int ind = 0; i < allTasksOfModel.size(); i++) {
+                            for (int ind = 0; ind < allTasksOfModel.size(); ind++) {
                                 if (ind == 0) {
                                     printTasks = (String) allTasksOfModel.toArray()[ind];
                                 } else {
@@ -177,7 +191,11 @@ public class PatternHandling extends Command {
                                 return Interpreter.BAD;
                             }
                             for (String argumentOfOption : argumentsOfOption) {
-                                selectedTasks.add(argumentOfOption);
+                                if (allTasksOfModel.contains(argumentOfOption)) {
+                                    selectedTasks.remove(argumentOfOption);
+                                } else {
+                                    interpreter.print("The task " + argumentOfOption + "does not exist, it will not be removed.");
+                                }
                             }
                             break;
                         case "-m":
@@ -185,14 +203,15 @@ public class PatternHandling extends Command {
                                 argumentsOfOption.add(commands[i+1]);
                                 i += 1;
                             }
-                            for (String argumentOfOption : argumentsOfOption) {
-                                if (allTasksOfModel.contains(argumentOfOption)) {
-                                    selectedTasks.remove(argumentOfOption);
-                                } else {
-                                    interpreter.print("The task " + argumentOfOption + "does not exist, it will not be removed.");
-                                }
+                            if (argumentsOfOption.size() != 0) {
+                                return Interpreter.BAD;
                             }
-                            interpreter.mgui.gtm.createPattern(new ArrayList<String>(selectedTasks), patternName, patternsPath);
+                            if (patternName != null && !patternName.equals("") && selectedTasks.size() != 0) {
+                                interpreter.mgui.gtm.createPattern(new ArrayList<String>(selectedTasks), patternName, patternsPath);
+                            } else {
+                                return NO_NAME_NO_TASK_FOR_PATTERN;
+                            }
+                            
                             break;
                         default:
                             return Interpreter.BAD;
@@ -204,7 +223,7 @@ public class PatternHandling extends Command {
 
         // Create Pattern
         Command configurePattern = new Command() {
-            final String [] options = {"-s", "-p", "-ct", "-rct", "-co", "-mt", "-mc", "-s", "-m"};
+            final String [] options = {"-s", "-p", "-ct", "-rct", "-co", "-plnc", "-pl", "-t", "-cpl", "-cpd","-cpm","-cpr","-tcl","-tcm", "-tcn", "-tcr", "-mcl", "-mcm", "-mcn", "-mcr", "-ua", "-m"};
             public String getCommand() {
                 return CONFIGURE;
             }
@@ -223,7 +242,9 @@ public class PatternHandling extends Command {
                 + "-p \tget the list of all the created patterns\n"
                 + "-ct TASK1 TASK2\tclone TASK1 from TASK2\n"
                 + "-rct TASK\t remove the clone task TASK\n"
-                + "-co TASK1.PORT1 TASK2.PORT2\t connect PORT1 of TASK1 with PORT2 of TASK2\n"
+                + "-co TASK1.PORT1 TASK2.PORT2 [NEW] [C] [WA] [SA]\t connect PORT1 of TASK1 (pattern side) with PORT2 of TASK2 (model side). Other possible options:\n\t*Set NEW to specify that this is a new port that needs to be created.\n\t*Set C to ensure confidentiality for this channel\n\t*Set WA to ensure weak authenticity for this channel\n\t*Set SA to ensure strong authenticity for this channel\n"
+                + "-plnc \tget the list of pattern's ports that are not yet connected\n"
+                + "-pl PORT \tget list of available ports in model that could be used to connect with PORT (pattern side)\n"
                 + "-t \tget all tasks of the model\n"
                 + "-cpl \tget list of ports to be configured\n"
                 + "-cpd PORT\tchoose removing PORT as decision\n"
@@ -263,7 +284,7 @@ public class PatternHandling extends Command {
                     List<String> argumentsOfOption = new ArrayList<String>();
                     
                     switch (commands[i]) {
-                        case "-n":
+                        case "-s":
                             while (i+1 < commands.length && !Arrays.asList(options).contains(commands[i+1])) {
                                 argumentsOfOption.add(commands[i+1]);
                                 i += 1;
@@ -271,44 +292,48 @@ public class PatternHandling extends Command {
                             if (argumentsOfOption.size() != 1) {
                                 return Interpreter.BAD;
                             }
-                            patternName = argumentsOfOption.get(0);
+                            listPatterns = getFoldersName(patternsPath);
+                            if (listPatterns.contains(argumentsOfOption.get(0))) {
+                                selectedPatternName = argumentsOfOption.get(0);
+                            } else {
+                                selectedPatternName = null;
+                                return PATTERN_NOT_EXIST;
+                            }
                             break;
-                        case "-a":
+                        case "-p":
                             while (i+1 < commands.length && !Arrays.asList(options).contains(commands[i+1])) {
                                 argumentsOfOption.add(commands[i+1]);
                                 i += 1;
                             }
-                            if (argumentsOfOption.size() == 0) {
+                            if (argumentsOfOption.size() != 0) {
                                 return Interpreter.BAD;
                             }
-                            for (String argumentOfOption : argumentsOfOption) {
-                                if (allTasksOfModel.contains(argumentOfOption)) {
-                                    selectedTasks.add(argumentOfOption);
-                                } else {
-                                    interpreter.print("The task " + argumentOfOption + "does not exist, it will not be added.");
-                                }
+                            listPatterns = getFoldersName(patternsPath);
+                            interpreter.print("There is " + listPatterns.size() + " pattern(s):\n");
+                            int cont = 1;
+                            for (String pattern : listPatterns) {
+                                interpreter.print(cont + ". " + pattern + "\n");
+                                cont++;
                             }
                             break;
-                        case "-l":
+                        case "-ct":
                             while (i+1 < commands.length && !Arrays.asList(options).contains(commands[i+1])) {
                                 argumentsOfOption.add(commands[i+1]);
                                 i += 1;
                             }
-                            if (argumentsOfOption.size() != 0) {
+                            if (argumentsOfOption.size() != 2) {
                                 return Interpreter.BAD;
                             }
-                            interpreter.print("The selected tasks are :\n");
-                            String printSelectedTasks = "";
-                            for (int ind = 0; i < selectedTasks.size(); i++) {
-                                if (ind == 0) {
-                                    printSelectedTasks = (String) selectedTasks.toArray()[ind];
-                                } else {
-                                    printSelectedTasks = printSelectedTasks + "\t" + (String) selectedTasks.toArray()[ind];
-                                }
+                            if (allTasksOfModel.contains(argumentsOfOption.get(0))) {
+                                return CLONE_TASK_EXIST;
                             }
-                            interpreter.print(printSelectedTasks);
+                            if (!allTasksOfModel.contains(argumentsOfOption.get(1))) {
+                                return TASK_TO_CLONE_NOT_EXIST;
+                            }
+                            PatternCloneTask patterClone = new PatternCloneTask(argumentsOfOption.get(0), argumentsOfOption.get(1));
+                            patternConfiguration.addClonedTasks(patterClone);
                             break;
-                        case "-t":
+                        case "-rct":
                             while (i+1 < commands.length && !Arrays.asList(options).contains(commands[i+1])) {
                                 argumentsOfOption.add(commands[i+1]);
                                 i += 1;
@@ -412,4 +437,14 @@ public class PatternHandling extends Command {
         addAndSortSubcommand(configurePattern);
         addAndSortSubcommand(diplodocusLoadTMAP);
     }
+
+    Vector<String> getFoldersName(String path) {
+        Vector<String> folders = new Vector<String>();
+        File directoryPath = new File(path);
+        String[] directories = directoryPath.list((dir, name) -> new File(dir, name).isDirectory());
+        if (directories != null) {
+            folders = new Vector<String>(Arrays.asList(directories));
+        }
+        return folders;
+    }
 }
-- 
GitLab