From 117a193e308698c7a298055947fb5cdd7453dea7 Mon Sep 17 00:00:00 2001 From: Martin Killenberg <martin.killenberg@desy.de> Date: Thu, 10 Aug 2017 13:19:10 +0200 Subject: [PATCH] importing locations works --- include/VariableMapper.h | 2 ++ src/VariableMapper.cc | 30 ++++++++++++++++++++++++---- tests/src/testVariableMapper.cpp | 34 +++++++++++++++++--------------- 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/include/VariableMapper.h b/include/VariableMapper.h index ec88ad3..d81aed4 100644 --- a/include/VariableMapper.h +++ b/include/VariableMapper.h @@ -66,6 +66,8 @@ namespace ChimeraTK{ protected: VariableMapper()=default; + std::set< std::string > _inputVariables; + void processLocation(xmlpp::Node const * locationNode); void processProperty(xmlpp::Node const * propertyNode, std::string locationName); void processLocationImport(xmlpp::Node const * importNode, std::string locationName); diff --git a/src/VariableMapper.cc b/src/VariableMapper.cc index 234cdc5..c6db75f 100644 --- a/src/VariableMapper.cc +++ b/src/VariableMapper.cc @@ -2,6 +2,7 @@ #include <libxml++/libxml++.h> #include <iostream> +#include <regex> namespace ChimeraTK{ void print_indentation(unsigned int indentation) @@ -156,12 +157,33 @@ void print_node(const xmlpp::Node* node, unsigned int indentation = 0) void VariableMapper::processLocationImport(xmlpp::Node const * importNode, std::string locationName){ for (auto const & node : importNode->get_children()){ const xmlpp::TextNode* nodeAsText = dynamic_cast<const xmlpp::TextNode*>(node); - std::cout << "Importing location: " << nodeAsText->get_content() << std::endl; - // loop source tree, cut beginning, replace / with _ and add a property + std::string importSource = nodeAsText->get_content(); + std::cout << "Importing in location '"<<locationName <<"': " << importSource << std::endl; + + // loop source tree, cut beginning, replace / with _ and add a property + for (auto const & processVariable : _inputVariables){ + if (_inputSortedDescriptions.find(processVariable) != _inputSortedDescriptions.end()){ + std::cout << processVariable << " alread in the map. Not importing" << std::endl; + continue; + } + + if ( processVariable.find( importSource+"/") == 0 ){ + // processVariable starts with wanted source + std::cout << "importing " << processVariable << " from " << importSource << " into " + << locationName << std::endl; + auto propertyNameSource = processVariable.substr( importSource.size() + 1); // add the slash to be removed + std::cout << "propertyNameSource " << propertyNameSource << std::endl; + auto propertyName = std::regex_replace(propertyNameSource, std::regex("/"), "."); + std::cout << "new property name is " << propertyName << std::endl; + _inputSortedDescriptions[processVariable] = PropertyDescription(locationName, propertyName); + } + } } } void VariableMapper::prepareOutput(std::string xmlFile, std::set< std::string > inputVariables){ + _inputVariables=inputVariables; + xmlpp::DomParser parser; // parser.set_validate(); parser.set_substitute_entities(); //We just want the text to be resolved/unescaped automatically. @@ -175,8 +197,8 @@ void print_node(const xmlpp::Node* node, unsigned int indentation = 0) //Walk the tree: const xmlpp::Node* rootNode = parser.get_document()->get_root_node(); //deleted by DomParser. - std::cout << "****************************\nPredefined printout in "<< xmlFile<<":\n" << std::endl; - print_node(rootNode); + // std::cout << "****************************\nPredefined printout in "<< xmlFile<<":\n" << std::endl; + // print_node(rootNode); std::cout << "\n My interpretation for "<< xmlFile << "\n===================================" << std::endl; diff --git a/tests/src/testVariableMapper.cpp b/tests/src/testVariableMapper.cpp index 0227d89..5d0bf6e 100644 --- a/tests/src/testVariableMapper.cpp +++ b/tests/src/testVariableMapper.cpp @@ -64,9 +64,11 @@ std::set< std::string > generateInputVariables(){ template <typename Map> bool mapCompare (Map const &lhs, Map const &rhs) { // No predicate needed because there is operator== for pairs already. - std::cout << "lhs.size() " << lhs.size() << ", rhs.size() " << rhs.size() << std::endl; - return lhs.size() == rhs.size() - && std::equal(lhs.begin(), lhs.end(), rhs.begin()); + if ( lhs.size() != rhs.size() ){ + std::cout << "Map size comparison failed: lhs.size() " << lhs.size() << ", rhs.size() " << rhs.size() << std::endl; + return false; + } + return std::equal(lhs.begin(), lhs.end(), rhs.begin()); } void testXmlParsing(std::string xmlFile, std::map< std::string, VariableMapper::PropertyDescription > propertyMap){ @@ -82,27 +84,27 @@ BOOST_AUTO_TEST_CASE( testRename ){ } BOOST_AUTO_TEST_CASE( testImportLocation ){ - testXmlParsing("variableTreeXml/importLocation.xml", { {"/A/a/di", {"B","a_di"}}, - {"/A/a/do", {"B","a_do"}}, + testXmlParsing("variableTreeXml/importLocation.xml", { {"/A/a/di", {"B","a.di"}}, + {"/A/a/do", {"B","a.do"}}, {"/A/b", {"B","b"}} }); } BOOST_AUTO_TEST_CASE( testImportAll ){ - testXmlParsing("variableTreeXml/importAll.xml", { {"/A/a/di", {"A","a_di"}}, - {"/A/a/do", {"A","a_do"}}, + testXmlParsing("variableTreeXml/importAll.xml", { {"/A/a/di", {"A","a.di"}}, + {"/A/a/do", {"A","a.do"}}, {"/A/b", {"A","b"}}, - {"/B/a/dr", {"B","a_dr"}}, - {"/B/c/de", {"B","c_de"}}, - {"/B/c/gne", {"B","c_gne"}}, - {"/C/a/da", {"C","a_da"}}, - {"/C/b/ge", {"C","b_ge"}}, - {"/C/c/be", {"C","c_be"}}, - {"/C/c/de", {"C","c_de"}}, + {"/B/a/dr", {"B","a.dr"}}, + {"/B/c/de", {"B","c.de"}}, + {"/B/c/gne", {"B","c.gne"}}, + {"/C/a/da", {"C","a.da"}}, + {"/C/b/ge", {"C","b.ge"}}, + {"/C/c/be", {"C","c.be"}}, + {"/C/c/de", {"C","c.de"}}, {"/DIRECT/DOUBLE", {"DIRECT","DOUBLE"}}, - {"/DIRECT/DOUBLE", {"DIRECT","DOUBLE_ARRAY"}}, + {"/DIRECT/DOUBLE_ARRAY", {"DIRECT","DOUBLE_ARRAY"}}, {"/DIRECT/INT", {"DIRECT","INT"}}, - {"/DIRECT/INT", {"DIRECT","INT_ARRAY"}} + {"/DIRECT/INT_ARRAY", {"DIRECT","INT_ARRAY"}} }); } -- GitLab