From 5a84c42bcb6118ea1b3caac6bf550b93d3355150 Mon Sep 17 00:00:00 2001
From: baigner <benedikt.aigner@rwth-aachen.de>
Date: Mon, 16 Apr 2018 18:58:35 +0200
Subject: [PATCH] VISTOMS update: User can clean up unused functions after
 removing unused outputs in step 1.11

Former-commit-id: ecd12fb90290d8c5b9bf8be06137cc2a6cc1a88f
---
 kadmos/vistoms/templates/VISTOMS.html | 84 +++++++++++++++------------
 kadmos/vistoms/vistoms.py             | 36 +++++++++---
 2 files changed, 76 insertions(+), 44 deletions(-)

diff --git a/kadmos/vistoms/templates/VISTOMS.html b/kadmos/vistoms/templates/VISTOMS.html
index a563cc8a2..d755b97ae 100644
--- a/kadmos/vistoms/templates/VISTOMS.html
+++ b/kadmos/vistoms/templates/VISTOMS.html
@@ -20243,52 +20243,62 @@
                     .on("mousedown", function()
                     {
                         bootbox.hideAll();
-                        bootbox.confirm("Are you sure you want to do this?", function(sure)
+                        bootbox.prompt(
                         {
-                            if (sure)
+                            title: "<p><b>Remove unused outputs</b></p>" 
+                                  +"<p>Do you want to clean up the graph afterwards?</p>" 
+                                  +"<b>CAUTION:</b> This will remove all unused competences automatically as well!",
+                            inputType: 'select',
+                            value: "True",
+                            inputOptions: [{text:'yes', value:'True'}, {text:'no', value:'False'}],
+                            callback: function (selection) 
                             {
-                                var bootboxContent = {title: "Enrich FPG", message: '<p>Please be patient...</p>'};
-                                var xhr = $.ajax({
-                                    type: 'POST',
-                                    url: '/kadmosRemoveUnusedOutputs',
-                                    data: {'graphID':graphID, 'currentOrder':nodeOrder},
-                                    success: function(result)
-                                    {
-                                        if (result.includes("ERROR:"))
-                                        {
-                                            bootboxContent.message = result
-                                            kadmosErrorMessage(bootboxContent);
-                                        }
-                                        else
+                                if (selection)
+                                {
+                                    var bootboxContent = {title: "Enrich FPG", message: '<p>Please be patient...</p>'};
+                                    var xhr = $.ajax({
+                                        type: 'POST',
+                                        url: '/kadmosRemoveUnusedOutputs',
+                                        data: {'graphID':graphID, 'currentOrder':nodeOrder, 'cleanUp':selection},
+                                        success: function(result)
                                         {
-                                            var updatedData = {};
-                                            updatedData = data;
-                                            var graphData = JSON.parse(result);             
-                                            for (var i = 0; i < updatedData.graphs.length; i++)
+                                            if (result.includes("ERROR:"))
                                             {
-                                                if (graphID == updatedData.graphs[i].id)
+                                                bootboxContent.message = result
+                                                kadmosErrorMessage(bootboxContent);
+                                            }
+                                            else
+                                            {
+                                                var updatedData = {};
+                                                updatedData = data;
+                                                var graphData = JSON.parse(result);             
+                                                for (var i = 0; i < updatedData.graphs.length; i++)
                                                 {
-                                                    updatedData.graphs[i] = graphData.graphs[0];   
+                                                    if (graphID == updatedData.graphs[i].id)
+                                                    {
+                                                        updatedData.graphs[i] = graphData.graphs[0];   
+                                                    }
                                                 }
+                                                
+                                                clearView();
+                                                makeKadmosMenu(updatedData);
+                                                xdsm_script(updatedData,graphID);
+                                                
+                                                bootboxContent.message = "Success!"
+                                                kadmosSuccessMessage(bootboxContent)
                                             }
-                                            
-                                            clearView();
-                                            makeKadmosMenu(updatedData);
-                                            xdsm_script(updatedData,graphID);
-                                            
-                                            bootboxContent.message = "Success!"
-                                            kadmosSuccessMessage(bootboxContent)
+                                        },
+                                        error: function(result)
+                                        {
+                                            bootboxContent.message = result
+                                            kadmosErrorMessage(bootboxContent);
                                         }
-                                    },
-                                    error: function(result)
-                                    {
-                                        bootboxContent.message = result
-                                        kadmosErrorMessage(bootboxContent);
-                                    }
-                                });
-                                kadmosHavePatience(xhr, bootboxContent)
+                                    });
+                                    kadmosHavePatience(xhr, bootboxContent)
+                                }
+                            
                             }
-                        });
+                        })
                     })
                 //####################################################################################################################
                 
diff --git a/kadmos/vistoms/vistoms.py b/kadmos/vistoms/vistoms.py
index 6b95aae72..fd51ad13c 100644
--- a/kadmos/vistoms/vistoms.py
+++ b/kadmos/vistoms/vistoms.py
@@ -153,7 +153,6 @@ def kadmosExportAllGraphs():
         fileType = request.form['fileType']
 
         if not os.path.isdir(path):
-            print path + " does not exist yet!"
             os.makedirs(os.path.dirname(path))
 
         for aFile in os.listdir(UPLOAD_FOLDER):
@@ -1317,6 +1316,11 @@ def kadmosRemoveUnusedOutputs():
         # get request form
         graphID = request.form['graphID']
         function_order = request.form['currentOrder'].split(',')
+        cleanUp_str = request.form['cleanUp']
+        if cleanUp_str == 'True':
+            cleanUp = True
+        else:
+            cleanUp = False
 
         # Save previous graph as backup before making the changes
         savePreviousGraph(graphID)
@@ -1337,11 +1341,24 @@ def kadmosRemoveUnusedOutputs():
 
             fpg.graph['problem_formulation']['function_order'] = function_order
 
-            output_nodes = fpg.find_all_nodes(subcategory='all outputs')
-            for output_node in output_nodes:
-                if 'problem_role' not in fpg.node[output_node]:
-                    fpg.remove_node(output_node)
-
+            # Cleaning of the graph needs to be done in a while loop, to entirely remove all unused elements
+            another_run = True
+            while another_run:
+                another_run = False
+                # Delete unused variables
+                output_nodes = fpg.find_all_nodes(subcategory='all outputs')
+                for output_node in output_nodes:
+                    if 'problem_role' not in fpg.node[output_node]:
+                        fpg.remove_node(output_node)
+                        another_run = True
+                # Delete unnecessary functions automatically if the user wants to
+                if cleanUp:
+                    function_nodes = fpg.find_all_nodes(category='function')
+                    for function_node in function_nodes:
+                        if not fpg.out_edges(function_node):
+                            fpg.remove_function_nodes(function_node)
+                            function_order.remove(function_node)
+                            another_run = True
 
             # Add the function problem roles (pre-coupling, coupled, post-coupling)
             fpg.add_function_problem_roles()
@@ -1435,7 +1452,12 @@ def kadmosImposeMDAOArchitecture():
         mdao_architecture = request.form['mdao_architecture']
         doe_method = request.form['doe_method']
         coupling_decomposition = request.form['coupling_decomposition']
-        allow_unconverged_couplings = bool(request.form['allow_unconverged_couplings'])
+        allow_unconverged_couplings_str = request.form['allow_unconverged_couplings']
+        if allow_unconverged_couplings_str == 'True':
+            allow_unconverged_couplings = True
+        else:
+            allow_unconverged_couplings = False
+
 
         # Save previous graph as backup before making the changes
         savePreviousGraph(graphID)
-- 
GitLab