Commit e58a351e authored by Floris Berendsen's avatar Floris Berendsen
Browse files

ENH: initial ConfigurationReader

parent a12504ce
#ifndef ConfigurationReader_h
#define ConfigurationReader_h
#include "selxBlueprint.h"
//#include "selxMacro.h"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/info_parser.hpp>
#include <boost/foreach.hpp>
#include <string>
#include <set>
#include <exception>
#include <iostream>
#include <boost/algorithm/string.hpp>
#include <vector>
#include <map>
namespace selx {
class ConfigurationReader
{
public:
//selxNewMacro( ConfigurationReader, itk::DataObject );
typedef Blueprint::ParameterKeyType ParameterKeyType;
typedef Blueprint::ParameterValueType ParameterValueType;
typedef Blueprint::ParameterMapType ParameterMapType;
void loadxml(const std::string &filename);
private:
typedef const boost::property_tree::ptree::basic_ptree& ComponentOrConnectionTreeType;
ParameterValueType VectorizeValues(ComponentOrConnectionTreeType componentOrConnectionTree);
};
}
#endif // #define ConfigurationReader_h
\ No newline at end of file
#ifndef ConfigurationReader_cxx
#define ConfigurationReader_cxx
#include "selxConfigurationReader.h"
#include <ostream>
namespace selx {
// Declared outside of the class body, so it is a free function
std::ostream& operator<<(std::ostream& out, const Blueprint::ParameterValueType& vector){
out << " : { ";
for (auto const & value : vector)
{
out << value << " ";
}
out << " }";
return out;
}
// Declared outside of the class body, so it is a free function
std::ostream& operator<<(std::ostream& out, const Blueprint::ParameterMapType& val){
for (auto const & mapPair : val)
{
out << mapPair.first << " : { ";
for (auto const & value : mapPair.second)
{
out << value << " ";
}
out << "}, ";
}
return out;
}
ConfigurationReader::ParameterValueType ConfigurationReader::VectorizeValues(ComponentOrConnectionTreeType componentOrConnectionTree)
{
std::string propertySingleValue = componentOrConnectionTree.data();
ParameterValueType propertyMultiValue;
for (auto const & value : componentOrConnectionTree)
{
propertyMultiValue.push_back(value.second.data());
}
if (propertyMultiValue.size() > 0)
{
if (propertySingleValue != "")
{
throw std::invalid_argument("XML tree should have either 1 unnamed element or multiple named properties");
}
}
else
{
propertyMultiValue.push_back(propertySingleValue);
}
return propertyMultiValue;
}
void ConfigurationReader::loadxml(const std::string &filename)
{
// Create empty property tree object
using boost::property_tree::ptree;
ptree pt;
read_xml(filename, pt, boost::property_tree::xml_parser::trim_whitespace);
write_json("../parameterfile2.json", pt);
BOOST_FOREACH(ptree::value_type &v, pt.equal_range("Component"))
{
//std::string componentName = v.second.data();
std::string componentName;
ParameterMapType componentPropertyMap;
for (auto const & elm : v.second)
{
const std::string & componentKey = elm.first;
if (componentKey == "Name")
{
componentName = elm.second.data();
}
ParameterValueType propertyMultiValue = VectorizeValues(elm.second);
std::string propertyKey = elm.first;
componentPropertyMap[propertyKey] = propertyMultiValue;
}
std::cout << "blueprint->AddComponent(""" << componentName << """, {" << componentPropertyMap << "});" << std::endl;
}
BOOST_FOREACH(ptree::value_type &v, pt.equal_range("Connection"))
{
std::string connectionName = v.second.data();
if (connectionName != "")
{
std::cout << "warning connectionName """ << connectionName << """ is ignored" << std::endl;
}
std::string outName;
std::string inName;
ParameterMapType componentPropertyMap;
for (auto const & elm : v.second)
{
const std::string & connectionKey = elm.first;
if (connectionKey == "Out")
{
outName = elm.second.data();
}
else if (connectionKey == "In")
{
inName = elm.second.data();
}
else
{
ParameterValueType propertyMultiValue = VectorizeValues(elm.second);
std::string propertyKey = elm.first;
componentPropertyMap[propertyKey] = propertyMultiValue;
}
}
std::cout << "blueprint->AddConnection(""" << outName << """, """ << inName << """, {" << componentPropertyMap << "});" << std::endl;
}
}
} // namespace selx
#endif // ConfigurationReader_cxx
#include "selxConfigurationReader.h"
#include "gtest/gtest.h"
using namespace selx;
class ConfigurationReaderTest : public ::testing::Test {
public:
virtual void SetUp() {
}
};
TEST_F( ConfigurationReaderTest, ReadXML )
{
ConfigurationReader reader;
EXPECT_NO_THROW(reader.loadxml("parameterfile2.xml"));
}
......@@ -6,11 +6,12 @@ set( ${MODULE}_INCLUDE_DIRS
${${MODULE}_SOURCE_DIR}/Blueprints/include
${${MODULE}_SOURCE_DIR}/ParameterObject/include
${${MODULE}_SOURCE_DIR}/ComponentInterface/include
${${MODULE}_SOURCE_DIR}/ConfigurationReader/include
)
# Collect header files for Visual Studio Project
# http://stackoverflow.com/questions/8316104/specify-how-cmake-creates-visual-studio-project
file(GLOB ${MODULE}_HEADER_FILES "${${MODULE}_SOURCE_DIR}/*/include/*.*")
#@Kasper: file GLOB is generally disencouraged for source files (missing files are not detected and CMake doesn't know if the project has to be updated when due to a checkout extra source files are in the tree), but since this I think is not really an issue for header files...
#I read: CMake will figure out that they're headers; it won't try to build them. http://stackoverflow.com/questions/8316104/specify-how-cmake-creates-visual-studio-project
# Export libraries
set( ${MODULE}_LIBRARIES
......@@ -23,6 +24,7 @@ set( ${MODULE}_TESTS
${${MODULE}_SOURCE_DIR}/ComponentInterface/test/selxComponentFactoryTest.cxx
${${MODULE}_SOURCE_DIR}/ComponentInterface/test/selxComponentInterfaceTest.cxx
${${MODULE}_SOURCE_DIR}/ComponentInterface/test/selxOverlordTest.cxx
${${MODULE}_SOURCE_DIR}/ConfigurationReader/test/selxConfigurationReaderTest.cxx
)
# Module source files
......@@ -31,6 +33,7 @@ set( ${MODULE}_SOURCE_FILES
${${MODULE}_SOURCE_DIR}/ComponentInterface/src/ComponentBase.cxx
${${MODULE}_SOURCE_DIR}/ComponentInterface/src/Overlord.cxx
${${MODULE}_SOURCE_DIR}/ComponentInterface/src/ComponentSelector.cxx
${${MODULE}_SOURCE_DIR}/ConfigurationReader/src/selxConfigurationReader.cxx
)
# Compile library
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment