From b7f7c8e1ab414fe97afc31f2b19b681dfba7689d Mon Sep 17 00:00:00 2001
From: imcovangent <I.vanGent@tudelft.nl>
Date: Thu, 17 May 2018 17:45:50 +0200
Subject: [PATCH] Added a function to add default values in the note attribute
 based on a reference XML file.

Former-commit-id: 4f607873732c364923683fc7ca0130eacd901163
---
 examples/scripts/ssbj_mda.py | 16 +++++++++++++++-
 examples/scripts/ssbj_mdo.py |  3 +++
 kadmos/graph/graph_data.py   | 35 ++++++++++++++++++++++++++++++++++-
 kadmos/graph/graph_kadmos.py |  1 -
 4 files changed, 52 insertions(+), 3 deletions(-)

diff --git a/examples/scripts/ssbj_mda.py b/examples/scripts/ssbj_mda.py
index 4fda9c659..5e4e31bd1 100644
--- a/examples/scripts/ssbj_mda.py
+++ b/examples/scripts/ssbj_mda.py
@@ -89,13 +89,27 @@ R_label = R[0].split('/')[-1]
 f_R = '/data_schema/mdo_data/objectives/R'
 rcg.add_mathematical_function([[R[0], R_label]], 'F[R]', [[f_R, '-{}'.format(R_label), 'Python']])
 
+# Add default values of the variables based on a reference file
+rcg.add_variable_default_values(os.path.join(kb_dir, 'ssbj', 'SSBJ-base.xml'))
+
 # Define the function order
 function_order = ['Structures', 'Aerodynamics', 'Propulsion', 'Performance',
                   'C[sigmas]', 'C[Theta]', 'C[dpdx]', 'C[prop]', 'F[R]']
 
-# Create a DSM and a VISTOMS visualization of the RCG
+# Create a CMDOWS, DSM and a VISTOMS visualization of the RCG
 rcg.create_dsm('RCG_extended', include_system_vars=True, destination_folder=pdf_dir, function_order=function_order)
 rcg.vistoms_create(vistoms_dir, function_order=function_order)
+
+# Save CMDOWS and KDMS file
+rcg.save('RCG', destination_folder=kdms_dir)
+rcg.save('RCG',
+         file_type='cmdows',
+         description='RCG CMDOWS file of the super-sonic business jet test case optimization problem',
+         creator='Lukas Mueller',
+         version='0.1',
+         destination_folder=cmdows_dir,
+         pretty_print=True,
+         integrity=True)
 all_graphs.append(rcg)
 
 # On to the wrapping of the MDAO architectures
diff --git a/examples/scripts/ssbj_mdo.py b/examples/scripts/ssbj_mdo.py
index ffe0196a1..9e0c79512 100644
--- a/examples/scripts/ssbj_mdo.py
+++ b/examples/scripts/ssbj_mdo.py
@@ -88,6 +88,9 @@ R_label = R[0].split('/')[-1]
 f_R = '/data_schema/mdo_data/objectives/R'
 rcg.add_mathematical_function([[R[0], R_label]], 'F[R]', [[f_R, '-{}'.format(R_label), 'Python']])
 
+# Add default values of the variables based on a reference file
+rcg.add_variable_default_values(os.path.join(kb_dir, 'ssbj', 'SSBJ-base.xml'))
+
 function_order = ['Structures', 'Aerodynamics', 'Propulsion', 'Performance',
                   'C[sigmas]', 'C[Theta]', 'C[dpdx]', 'C[prop]', 'F[R]']
 
diff --git a/kadmos/graph/graph_data.py b/kadmos/graph/graph_data.py
index 3d192006f..308b5b3c2 100644
--- a/kadmos/graph/graph_data.py
+++ b/kadmos/graph/graph_data.py
@@ -4,6 +4,7 @@ import copy
 import logging
 import distutils.util
 import numbers
+import os
 import re
 import random
 
@@ -14,6 +15,7 @@ import numpy as np
 
 from types import NoneType
 
+from lxml import etree
 
 from kadmos.utilities.strings import get_correctly_extended_latex_label
 from ..utilities import prompting
@@ -24,7 +26,7 @@ from ..utilities.testing import check
 from ..utilities.plotting import AnnoteFinder
 from ..utilities.xmls import Element
 
-from graph_kadmos import KadmosGraph
+from graph_kadmos import KadmosGraph, _parse_check
 
 from mixin_mdao import MdaoMixin
 from mixin_kechain import KeChainMixin
@@ -1145,6 +1147,37 @@ class DataGraph(KadmosGraph):
         return n_feedback_loops, n_disciplines_in_feedback
 
 
+    # ---------------------------------------------------------------------------------------------------------------- #
+    #                                          XML-HANDLING METHODS                                                    #
+    # ---------------------------------------------------------------------------------------------------------------- #
+    def add_variable_default_values(self, xml_file):
+        """Method to add the default value of the variables based on a reference XML file containing those values.
+
+        :param xml_file: path (absolute or local) to the XML file containing the default values
+        :type xml_file: file
+        :return: enriched graph with default values of the variables stored as attributes
+        :rtype: self
+        """
+        # Check the input XML file and parse it
+        assert os.path.isfile(xml_file), "Could not find the XML file {}".format(xml_file)
+        _parse_check([[xml_file]])
+        xml_file = etree.parse(xml_file)
+
+        # Get all variables in the graph
+        var_nodes = self.find_all_nodes(category='variable')
+
+        # For each variable, check whether it exists in the reference XML file and add the value.
+        for var_node in var_nodes:
+            # Get the element in the xml_file
+            els = xml_file.xpath(var_node)
+            if els:
+                el = els[0]
+                default_value = el.text
+                if default_value:
+                    self.nodes[var_node]['note'] = default_value
+        return
+
+
 class RepositoryConnectivityGraph(DataGraph):
 
     PATHS_LIMIT = 1e4    # limit check for select_function_combination_from method
diff --git a/kadmos/graph/graph_kadmos.py b/kadmos/graph/graph_kadmos.py
index c55cbe5ee..30c92e925 100644
--- a/kadmos/graph/graph_kadmos.py
+++ b/kadmos/graph/graph_kadmos.py
@@ -1061,7 +1061,6 @@ class KadmosGraph(nx.DiGraph, EquationMixin, VistomsMixin):
 
             if instance > 1:
                 cmdows_parameter.add('relatedInstanceUID', self.get_first_node_instance(graph_parameter))
-
             else:
                 cmdows_parameter.add('description', self.nodes[graph_parameter].get('description'),
                                      only_add_if_valued=True)
-- 
GitLab