From 0b9e1fbb343a07201f728f4168e2f29d9fce87c7 Mon Sep 17 00:00:00 2001
From: baigner <benedikt.aigner@rwth-aachen.de>
Date: Tue, 17 Apr 2018 17:24:59 +0200
Subject: [PATCH] New data structure try 5: Interpretation of new tree data in
 the html file works for XDSM for all variable categories. edgeBundles and
 sankeyDiagram still missing!

Former-commit-id: cf5f14bc83b0168549234fa14bf8868d8a2a27e2
---
 kadmos/graph/mixin_vistoms.py         | 105 ++++-------
 kadmos/vistoms/templates/VISTOMS.html | 249 +++++---------------------
 2 files changed, 75 insertions(+), 279 deletions(-)

diff --git a/kadmos/graph/mixin_vistoms.py b/kadmos/graph/mixin_vistoms.py
index 74d28a514..aa671389e 100644
--- a/kadmos/graph/mixin_vistoms.py
+++ b/kadmos/graph/mixin_vistoms.py
@@ -426,11 +426,6 @@ class VistomsMixin(object):
         graph_data_entry['xdsm'] = xdsm_dict
 
         # Benedikt: New schema function
-        # Get variable tree based on schema NEW
-        variable_tree_dataschema_new = self._get_variable_tree_dataschema_new(full_graph, reference_file)
-        # Write dictionary entry
-        graph_data_entry['variableSchemes']['schema_new'] = variable_tree_dataschema_new
-
         # Get variable tree based on schema
         variable_tree_dataschema = self._get_variable_tree_dataschema(full_graph, reference_file)
         # Write dictionary entry
@@ -767,51 +762,6 @@ class VistomsMixin(object):
         logger.debug('Successfully created xdsm.')
         return xdsm_dict
 
-    def _get_variable_tree_dataschema_new(self, full_graph, reference_file):
-        """ Function to determine the variable tree based on the data schema.
-
-        :param full_graph: dictionary with graph data
-        :type full_graph: dict
-        :param reference_file: file with reference values
-        :type reference_file: file
-        :return: variable tree definition
-        :rtype: dict
-        """
-        # Settings
-        coordinator_str = self.COORDINATOR_STRING
-
-        logger.debug('Creating schema variable tree...')
-
-        variable_tree_dataschema = []
-        if reference_file:
-            # Parse reference XML
-            reference_xml = etree.parse(reference_file)
-            # Check validity of the CPACS file
-            # noinspection PyUnusedLocal
-            reference_valid = recursively_unique_attribute(reference_xml)
-        for key in full_graph:
-            if key is not 'attributes' and key is not coordinator_str:
-                if self.node[key]['category'] == 'variable':
-                    # Determine element element value and dimension based on reference file
-                    if reference_file:
-                        # Check if the variable node is actually a related node
-                        if 'related_to_schema_node' in self.node[key]:
-                            xpath = self.node[key]['related_to_schema_node']
-                        else:
-                            xpath = key
-                        # Get element details
-                        # noinspection PyUnboundLocalVariable
-                        var_value, var_dim = get_element_details(reference_xml, xpath)
-                    else:
-                        var_value = 'unknown'
-                        var_dim = None
-                        xpath = key
-                    print xpath
-                    var_dict = dict(xpath=xpath, value=var_value, dimension=var_dim, type='variable')
-                    variable_tree_dataschema.append(var_dict)
-        logger.debug('Successfully created schema variable tree.')
-        return variable_tree_dataschema
-
 
     def _get_variable_tree_dataschema(self, full_graph, reference_file):
         """ Function to determine the variable tree based on the data schema.
@@ -829,7 +779,7 @@ class VistomsMixin(object):
 
         logger.debug('Creating schema variable tree...')
 
-        variable_tree_dataschema = dict()
+        variable_tree_dataschema = []
         if reference_file:
             # Parse reference XML
             reference_xml = etree.parse(reference_file)
@@ -852,8 +802,9 @@ class VistomsMixin(object):
                     else:
                         var_value = 'unknown'
                         var_dim = None
-                    var_dict = get_element_dict(key, var_value, var_dim, include_reference_data=True)
-                    variable_tree_dataschema = merge(variable_tree_dataschema, var_dict)
+                        xpath = key
+                    var_dict = dict(xPath=xpath, value=var_value, dimension=var_dim, type='variable')
+                    variable_tree_dataschema.append(var_dict)
         logger.debug('Successfully created schema variable tree.')
         return variable_tree_dataschema
 
@@ -871,7 +822,7 @@ class VistomsMixin(object):
 
         logger.debug('Creating system level categorized variable tree...')
 
-        variable_tree_categorized_system_level = dict()
+        variable_tree_categorized_system_level = []
         for key in full_graph:
             if key is not 'attributes' and key is not coordinator_str:
                 if self.nodes[key]['category'] == 'variable':
@@ -885,8 +836,9 @@ class VistomsMixin(object):
                         key = '/systemVariables/outputs' + key
                     else:
                         key = '/systemVariables/holes' + key
-                    var_dict = get_element_dict(key)
-                    variable_tree_categorized_system_level = merge(variable_tree_categorized_system_level, var_dict)
+                    xpath = key
+                    var_dict = dict(xPath=xpath, type='variable')
+                    variable_tree_categorized_system_level.append(var_dict)
         logger.debug('Successfully created system level categorized variable tree.')
         return variable_tree_categorized_system_level
 
@@ -903,14 +855,15 @@ class VistomsMixin(object):
         coordinator_str = self.COORDINATOR_STRING
 
         logger.debug('Creating node level categorized variable tree...')
-        variable_tree_categorized_node_level = dict()
+        variable_tree_categorized_node_level = []
         for key in full_graph:
             if key is not 'attributes' and key is not coordinator_str:
                 if self.nodes[key]['category'] == 'variable':
                     subcategory = self.nodes[key]['subcategory']
                     key = '/variables/' + make_camel_case(subcategory) + key
-                    var_dict = get_element_dict(key)
-                    variable_tree_categorized_node_level = merge(variable_tree_categorized_node_level, var_dict)
+                    xpath = key
+                    var_dict = dict(xPath=xpath, type='variable')
+                    variable_tree_categorized_node_level.append(var_dict)
         logger.debug('Successfully created node level categorized variable tree.')
         return variable_tree_categorized_node_level
 
@@ -927,27 +880,31 @@ class VistomsMixin(object):
         coordinator_str = self.COORDINATOR_STRING
 
         logger.debug('Creating role level categorized variable tree...')
-        variable_tree_categorized_role_level = dict()
+        variable_tree_categorized_role_level = []
         # Create empty start tree
         key = '/variables/architectureRoles'
-        var_dict = get_element_dict(key)
-        variable_tree_categorized_role_level = merge(variable_tree_categorized_role_level, var_dict)
+        xpath = key
+        var_dict = dict(xPath=xpath, type='variable')
+        variable_tree_categorized_role_level.append(var_dict)
         key = '/variables/problemRoles'
-        var_dict = get_element_dict(key)
-        variable_tree_categorized_role_level = merge(variable_tree_categorized_role_level, var_dict)
+        xpath = key
+        var_dict = dict(xPath=xpath, type='variable')
+        variable_tree_categorized_role_level.append(var_dict)
         for key in full_graph:
             if key is not 'attributes' and key is not coordinator_str:
                 if self.nodes[key]['category'] == 'variable':
                     if 'problem_role' in self.nodes[key]:
                         prob_role = self.nodes[key]['problem_role']
                         new_key = '/variables/problemRoles/' + make_camel_case(prob_role) + 's' + key
-                        var_dict = get_element_dict(new_key)
-                        variable_tree_categorized_role_level = merge(variable_tree_categorized_role_level, var_dict)
+                        xpath = new_key
+                        var_dict = dict(xPath=xpath, type='variable')
+                        variable_tree_categorized_role_level.append(var_dict)
                     if 'architecture_role' in self.nodes[key]:
                         arch_role = self.nodes[key]['architecture_role']
                         new_key = '/variables/architectureRoles/' + make_camel_case(arch_role) + 's' + key
-                        var_dict = get_element_dict(new_key)
-                        variable_tree_categorized_role_level = merge(variable_tree_categorized_role_level, var_dict)
+                        xpath = new_key
+                        var_dict = dict(xPath=xpath, type='variable')
+                        variable_tree_categorized_role_level.append(var_dict)
         logger.debug('Successfully created role level categorized variable tree.')
         return variable_tree_categorized_role_level
 
@@ -961,19 +918,21 @@ class VistomsMixin(object):
         """
 
         logger.debug('Creating function level categorized variable tree...')
-        variable_tree_categorized_function_level = dict()
+        variable_tree_categorized_function_level = []
         for item in function_data_list:
             name = item['name']
             inputs = item['input']
             outputs = item['output']
             for inp in inputs:
                 key = '/functions/' + name + '/inputs' + inp
-                var_dict = get_element_dict(key)
-                variable_tree_categorized_function_level = merge(variable_tree_categorized_function_level, var_dict)
+                xpath = key
+                var_dict = dict(xPath=xpath, type='variable')
+                variable_tree_categorized_function_level.append(var_dict)
             for output in outputs:
                 key = '/functions/' + name + '/outputs' + output
-                var_dict = get_element_dict(key)
-                variable_tree_categorized_function_level = merge(variable_tree_categorized_function_level, var_dict)
+                xpath = key
+                var_dict = dict(xPath=xpath, type='variable')
+                variable_tree_categorized_function_level.append(var_dict)
         logger.debug('Successfully created function level categorized variable tree...')
         return variable_tree_categorized_function_level
 
diff --git a/kadmos/vistoms/templates/VISTOMS.html b/kadmos/vistoms/templates/VISTOMS.html
index 36849b1df..4482a87b2 100644
--- a/kadmos/vistoms/templates/VISTOMS.html
+++ b/kadmos/vistoms/templates/VISTOMS.html
@@ -20586,7 +20586,7 @@
                     
 					var treeLayoutSVG = treeLayoutdiv.append("svg").attr("class","treeLayoutSVG")
 					var treeLayout = treeLayoutSVG.append("g").attr("class","treeLayout");
-					maketreeLayout(emptyArray, treeLayout, treeLayoutSVG, treeLayoutdiv, headLine, "schema_new");
+					maketreeLayout(emptyArray, treeLayout, treeLayoutSVG, treeLayoutdiv, headLine, aCategory);
 				}
 				
 				var childrenItems = [];
@@ -20679,7 +20679,7 @@
 								name = "Input tree view: " + k.id + "; Categorization: " + categoryDescr;
 								if (d.to == k.id)
 								{
-									array = array + ", " + d.name;
+									array = array + "," + d.name;
 								}
 							}
 							else if (io=="out")
@@ -20687,7 +20687,7 @@
 								name = "Output tree view:" + k.id + "; Categorization: " + categoryDescr;
 								if (d.from == k.id)
 								{
-									array = array + ", " + d.name;
+									array = array + "," + d.name;
 								}
 							}
 						})
@@ -21098,157 +21098,6 @@
                     }
 				})	
 				
-				
-				function prune_tree(parentXPath, objArray, xPathList) 
-				{ 
-					var firstElement = "/"+xPathList.split("/")[1]+"/";
-					for (var i = 0; i < objArray.length; ++i) 
-					{
-						var obj = objArray[i];
-						var objXPath;
-						
-						//aigner: If object has children append a "/" as xPath separator
-						//else append comma as indicator for last element in the xPath
-						if (obj.children){objXPath = parentXPath+objArray[i].name+"/";}
-						else{objXPath =  parentXPath+objArray[i].name;}
-						var objXPathFromFirst = objXPath.split(firstElement);
-						
-						// console.log("Current node ->")
-						// console.log(firstElement+objXPathFromFirst[1])
-						// console.log(xPathList)
-												
-						var delete_it = true;
-						if(objXPathFromFirst[1]==undefined)
-						{
-							delete_it = false;
-						}
-						else
-						{
-							if (include(xPathList, firstElement+objXPathFromFirst[1]))
-							{
-								delete_it = false;
-							}
-						}
-						if (delete_it==true)
-						{
-							// console.log("delete ->")
-							// console.log( objXPathFromFirst[1])
-							// console.log(xPathList)
-							
-							objArray.splice(i, 1);
-							i--;
-						}
-						else
-						{
-							// console.log("do not delete ->")
-							// console.log(objXPath);
-							if (obj.children)
-							{
-								if (obj.children.length==0)
-								{
-									objArray.splice(i, 1);
-									i--;
-									prune_tree(parentXPath, objArray, xPathList)
-								}
-								else{prune_tree(objXPath, obj.children, xPathList);}
-								
-							}
-						}
-					}
-				}
-				function getPrunedLeafNodes(parentXPath, objArray, xPathList, leafNodes) 
-				{ 
-					var firstElement = "/"+xPathList.split("/")[1]+"/";
-					for (var i = 0; i < objArray.length; ++i) 
-					{
-						var obj = objArray[i];
-						var objXPath;
-						
-						//aigner: If object has children append a "/" as xPath separator
-						//else append comma as indicator for last element in the xPath
-						if (obj.children){objXPath = parentXPath+objArray[i].name+"/";}
-						else{objXPath =  parentXPath+objArray[i].name;}
-						var objXPathFromFirst = objXPath.split(firstElement);
-						
-						// console.log("Current node ->")
-						// console.log(firstElement+objXPathFromFirst[1])
-						// console.log(xPathList)
-												
-						var delete_it = true;
-						if(objXPathFromFirst[1]==undefined)
-						{
-							delete_it = false;
-						}
-						else
-						{
-							if (include(xPathList, firstElement+objXPathFromFirst[1]))
-							{
-								delete_it = false;
-							}
-						}
-						if (delete_it==true)
-						{
-							// console.log("delete ->")
-							// console.log( objXPathFromFirst[1])
-							// console.log(xPathList)
-							
-							objArray.splice(i, 1);
-							i--;
-						}
-						else
-						{
-							// console.log("do not delete ->")
-							// console.log(objXPath);
-							if (obj.children)
-							{
-								if (obj.children.length==0)
-								{
-									objArray.splice(i, 1);
-									i--;
-									getPrunedLeafNodes(parentXPath, objArray, xPathList, leafNodes)
-								}
-								else{getPrunedLeafNodes(objXPath, obj.children, xPathList, leafNodes);}
-							}
-							else 
-							{
-								obj.xPath = objXPath;
-								leafNodes.push(obj);
-							};
-						}
-					}
-				}
-				function clean_tree(objArray)
-				{
-					for (var i = 0; i < objArray.length; ++i) 
-					{
-						var obj = objArray[i];
-						if (obj.children)
-						{
-							if (obj.children.length==0)
-							{
-								objArray.splice(i, 1);
-								i--;
-							}
-							clean_tree(obj.children);
-						}
-					}
-				}
-				function countElements(node, counter) 
-				{
-					counter ++;
-					if (node.children)
-					{
-						if (node.children.length !== 0)
-						{
-							node.children.forEach(function(n)
-							{
-								counter = countElements(n, counter);
-							});
-						}
-					}
-					return counter;
-				}
-				
 				function prepareTreeLayout(xdsms, refName)
 				{			
 					function showEdgeTree(data,aVarCategory,categoryDescr)
@@ -21477,20 +21326,38 @@
 				
 				function maketreeLayout(pipeData, layout, svg, div, aName, aVarCategory)
 				{			
-                    var schema = currentGraph.variableSchemes[aVarCategory];
-
-                    
-                    var treeData = (JSON.parse(JSON.stringify(schema)));				
-                    //aigner: Here, the minimalized tree is created!
-                    //Transferred variables: 
-                    //1) parentXPath --> xPath until current nodes
-                    //2) objArray --> Array of current nodes
-                    //3) xPathList --> Array of actually used pipeLine data
-                    
+                    var treeData = (JSON.parse(JSON.stringify(currentGraph.variableSchemes[aVarCategory])));				
+                    //aigner: Here, the minimalized tree is created!                    
                     
                     //aigner: HIER WEITER!!!
                     //aigner:New implementation start
                     //######################################################################
+                    //Function to prune the tree according to list of xPaths that are actually there
+                    function prune_tree(aPipeData,aTreeData)
+                    {
+                        console.log(aPipeData)
+                        console.log(aTreeData)
+                        var firstEl = aPipeData[0].split("/")[1]
+                        for (i=0; i < aTreeData.length; ++i) 
+                        {
+                            var treeElement = aTreeData[i];
+                            var relevant_xPath = "/"+firstEl+treeElement.xPath.split(firstEl)[1]
+                            if(aPipeData.indexOf(relevant_xPath)<=-1)
+                            {
+                                aTreeData.splice(i,1);
+                                i--;
+                            }
+                        }
+                    }
+                    //The tree will only be pruned if there is pipeData, such as in an edge or for the input of a tool
+                    if (pipeData)
+                    {
+                        var cleanPipeData = pipeData.split(",")
+                        var index = cleanPipeData.indexOf("");
+                        if (index > -1) {cleanPipeData.splice(index, 1);}
+                        prune_tree(cleanPipeData, treeData);
+                    }
+                    
                     function childExists(name, children){
                         var exists = false
                         children.forEach(function(child)
@@ -21503,32 +21370,26 @@
                         return exists;
                     }
                     
-                    
+                    //Function to create the tree layout by appending children to the layout
                     function appendChildren(anElement, aParent, xPath_list)
                     {
                         if (xPath_list.length > 0)
                         {
                             //If element has no children yet, initialize children array
                             if (!aParent.children){aParent.children = [];}
-                            //remove the first element from the xPath list
-                            let newXPath_list = JSON.parse(JSON.stringify(xPath_list));
-                            newXPath_list.shift();
-                            <!-- console.log(xPath_list) -->
-                            <!-- console.log(newXPath_list) -->
                             //Create a new child element, if it does not exist yet
                             if (!childExists(xPath_list[0], aParent.children))
                             {
                                 //Create a new child object
-                                var newChild = {level: aParent.level+1, name: xPath_list[0], type: "variable"};
+                                var newChild = {level: aParent.level+1, name: xPath_list[0], type: "variable", xPath: aParent.xPath+"/"+xPath_list[0]};
                                 aParent.children.push(newChild);
                             }
                             aParent.children.forEach(function(aChild)
                             {
                                 if (xPath_list[0] == aChild.name)
                                 {
-                                    <!-- console.log("Children will be appended for: " + aChild.name) -->
-                                    <!-- console.log(newXPath_list) -->
-                                    <!-- console.log(anElement.xpath) -->
+                                    let newXPath_list = JSON.parse(JSON.stringify(xPath_list));
+                                    newXPath_list.shift();
                                     appendChildren(anElement, aChild, newXPath_list)
                                 }
                             })
@@ -21540,56 +21401,32 @@
                     }
                     
                     var newTree = {};
-                    function buildTree(root, elements)
+                    //function builds tree layout from xPaths of the elements
+                    function buildTree(root, aTreeData)
                     {
-                        elements.forEach(function(element)
+                        aTreeData.forEach(function(treeElement)
                         {
-                            var xPath_split = element.xpath.split("/");
+                            var xPath_split = treeElement.xPath.split("/");
                             var index = xPath_split.indexOf("");
                             if (index > -1) {xPath_split.splice(index, 1);}
                             root.level = 0
                             root.name = xPath_split[0]
+                            root.xPath = "/"+xPath_split[0]
                             <!-- console.log("####################") -->
-                            <!-- console.log(element.xpath) -->
+                            <!-- console.log(treeElement.xPath) -->
                             let newXPath_split = JSON.parse(JSON.stringify(xPath_split));
                             newXPath_split.shift();
-                            appendChildren(element, root, newXPath_split)
+                            appendChildren(treeElement, root, newXPath_split)
                             <!-- console.log("DONE!") -->
                             <!-- console.log("####################") -->
                         })
                     }
+                    //build tree layout for vistoms
                     buildTree(newTree, treeData)                    
                     treeData = newTree
                     //######################################################################
                     //aigner:New implementation end
                     
-                    
-                    function getXPathForAllNodes(aNode, anxPath)
-                    {
-                        if (anxPath!="")
-                        {aNode.xPath = anxPath+"/"+aNode.name;}
-                        else{aNode.xPath = "/"+aNode.name;}
-                        
-                        if (aNode.children)
-                        {
-                            aNode.children.forEach(function (aChild)
-                            {getXPathForAllNodes(aChild, aNode.xPath)})
-                        }
-                    }
-                    getXPathForAllNodes(treeData, "")
-                    
-                    if (pipeData)
-                    {
-                        prune_tree("/"+treeData.name+"/", treeData.children, pipeData);
-                        //aigner: count all descendants of a node
-                        var firstEl = pipeData.split("/")[1];
-                        if(firstEl != treeData.name)
-                        {
-                            var cleanOperations = countElements(treeData,0)
-                            for (var i=0; i<cleanOperations; i++){clean_tree(treeData.children);}
-                        }
-                    }
-                    
                     // Set the dimensions and margins of the diagram
                     var margin = {top: 20, right: 90, bottom: 20, left: 90},
                         width = 960 - margin.left - margin.right,
-- 
GitLab