diff --git a/Modules/Simulation/Testing/files.cmake b/Modules/Simulation/Testing/files.cmake index 3f5716e326..68a6fdc6b2 100644 --- a/Modules/Simulation/Testing/files.cmake +++ b/Modules/Simulation/Testing/files.cmake @@ -1,6 +1,7 @@ set(MODULE_TESTS + mitkSimulationTemplateTest.cpp ) set(MODULE_CUSTOM_TESTS mitkSimulationTest.cpp ) diff --git a/Modules/Simulation/Testing/mitkSimulationTemplateTest.cpp b/Modules/Simulation/Testing/mitkSimulationTemplateTest.cpp index 929b37aa61..8ee52220dc 100644 --- a/Modules/Simulation/Testing/mitkSimulationTemplateTest.cpp +++ b/Modules/Simulation/Testing/mitkSimulationTemplateTest.cpp @@ -1,87 +1,72 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ -#include #include #include #include -int mitkSimulationTemplateTest(int argc, char* argv[]) +static mitk::DataNode::Pointer CreateDataNode(mitk::SimulationTemplate::Pointer simulationTemplate) { - MITK_TEST_BEGIN("mitkSimulationTemplateTest") - - MITK_TEST_CONDITION_REQUIRED(argc == 2, "Test if command line has argument.") - - MITK_TEST_OUTPUT(<< "Register SimulationObjectFactory.") - mitk::RegisterSimulationObjectFactory(); + mitk::DataNode::Pointer dataNode = mitk::DataNode::New(); + dataNode->SetData(simulationTemplate); + return dataNode; +} +static void CreateSimulation_NotInitialized_ReturnsEmptyString() +{ mitk::SimulationTemplate::Pointer simulationTemplate = mitk::SimulationTemplate::New(); - MITK_TEST_CONDITION_REQUIRED(simulationTemplate.IsNotNull(), "Create simulation template.") - - std::string contents = simulationTemplate->CreateSimulation(); - MITK_TEST_CONDITION(contents.empty(), "Try to create simulation from template.") - - bool boolResult = simulationTemplate->SetProperties(NULL); - MITK_TEST_CONDITION(!boolResult, "Try to set properties of non-existent data node.") - - boolResult = simulationTemplate->SetProperties(mitk::DataNode::New()); - MITK_TEST_CONDITION(!boolResult, "Try to set properties of empty data node.") - - boolResult = simulationTemplate->Parse(""); - MITK_TEST_CONDITION(boolResult, "Parse empty simulation template."); - - boolResult = simulationTemplate->Parse(""); - MITK_TEST_CONDITION(!boolResult, "Try to parse already initialized simulation template.") - - boolResult = simulationTemplate->SetProperties(mitk::DataNode::New()); - MITK_TEST_CONDITION(!boolResult, "Try to set properties of empty data node again.") - - contents = simulationTemplate->CreateSimulation(); - MITK_TEST_CONDITION(contents.empty(), "Create empty simulation.") - - simulationTemplate = mitk::SimulationTemplate::New(); - MITK_TEST_CONDITION_REQUIRED(simulationTemplate.IsNotNull(), "Create another simulation template.") + MITK_TEST_CONDITION(simulationTemplate->CreateSimulation().empty(), "CreateSimulation_NotInitialized_ReturnsEmptyString") +} - contents = ""; - boolResult = simulationTemplate->Parse(contents); - MITK_TEST_CONDITION(boolResult, "Parse static simulation template.") +static void SetProperties_InputIsNull_ReturnsFalse() +{ + mitk::SimulationTemplate::Pointer simulationTemplate = mitk::SimulationTemplate::New(); + MITK_TEST_CONDITION(!simulationTemplate->SetProperties(NULL), "SetProperties_InputIsNull_ReturnsFalse") +} - std::string contents2 = simulationTemplate->CreateSimulation(); - MITK_TEST_CONDITION(contents == contents2, "Create simulation.") +static void SetProperties_NotInitialized_ReturnsFalse() +{ + mitk::SimulationTemplate::Pointer simulationTemplate = mitk::SimulationTemplate::New(); + mitk::DataNode::Pointer dataNode = CreateDataNode(simulationTemplate); + MITK_TEST_CONDITION(!simulationTemplate->SetProperties(dataNode), "SetProperties_NotInitialized_ReturnsFalse") +} - simulationTemplate = mitk::SimulationTemplate::New(); - MITK_TEST_CONDITION_REQUIRED(simulationTemplate.IsNotNull(), "Create another simulation template.") +static void SetProperties_ContainsTemplateAndReference_SetsPropertyAndReturnsTrue() +{ + mitk::SimulationTemplate::Pointer simulationTemplate = mitk::SimulationTemplate::New(); + simulationTemplate->Parse("{id='Atoll' default='Bora'} {'Atoll'}"); + mitk::DataNode::Pointer dataNode = CreateDataNode(simulationTemplate); + bool boolResult = simulationTemplate->SetProperties(dataNode); + std::size_t numProperties = dataNode->GetPropertyList()->GetMap()->size(); + std::string stringResult; + dataNode->GetStringProperty("Atoll", stringResult); + MITK_TEST_CONDITION(boolResult && numProperties == 1 && stringResult == "Bora", "SetProperties_ContainsTemplateAndReference_SetsPropertyAndReturnsTrue") +} - contents = "{'F'}|{id='A'}|{id = 'B' type='string'}|{id ='C' type=\t\t\n\t'int'}|{id= 'D' type='float'}|{id='E' default='E'}|{id='F' type='int', default='1'}|{id='G' type='float' default='0.5'}|{'E'}"; - boolResult = simulationTemplate->Parse(contents); - MITK_TEST_CONDITION(boolResult, "Parse simulation template.") +int mitkSimulationTemplateTest(int, char* []) +{ + mitk::RegisterSimulationObjectFactory(); - mitk::DataNode::Pointer dataNode = mitk::DataNode::New(); - dataNode->SetData(simulationTemplate); - boolResult = simulationTemplate->SetProperties(dataNode); - MITK_TEST_CONDITION(boolResult, "Set properties of corresponding data node.") + MITK_TEST_BEGIN("mitkSimulationTemplateTest") - dataNode->SetStringProperty("A", "A"); - dataNode->SetIntProperty("F", 2); - contents = simulationTemplate->CreateSimulation(); - MITK_TEST_CONDITION(contents == "2|A||0|0|E|2|0.5|E", "Create Simulation") + CreateSimulation_NotInitialized_ReturnsEmptyString(); - // TODOs - // - Ambiguous IDs - // - Syntax errors + SetProperties_InputIsNull_ReturnsFalse(); + SetProperties_NotInitialized_ReturnsFalse(); + SetProperties_ContainsTemplateAndReference_SetsPropertyAndReturnsTrue(); MITK_TEST_END() } diff --git a/Modules/Simulation/Testing/mitkSimulationTest.cpp b/Modules/Simulation/Testing/mitkSimulationTest.cpp index 53df112b09..aeeb9c07ff 100644 --- a/Modules/Simulation/Testing/mitkSimulationTest.cpp +++ b/Modules/Simulation/Testing/mitkSimulationTest.cpp @@ -1,293 +1,292 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include #include #include #include #include #include #include #include -#include template static T lexical_cast(const std::string& string) { std::istringstream sstream(string); T value; sstream >> value; if (sstream.fail()) { MITK_ERROR << "Lexical cast failed for '" << string << "'!"; exit(EXIT_FAILURE); } return value; } static mitk::Simulation::Pointer LoadSimulation(const std::string& filename) { mitk::DataNode::Pointer dataNode = mitk::IOUtil::LoadDataNode(filename); mitk::Simulation::Pointer simulation = dynamic_cast(dataNode->GetData()); if (simulation.IsNull()) mitkThrow() << "Could not load '" << filename << "'!"; return simulation; } static bool IsActiveSimulation(mitk::Simulation::Pointer simulation) { if (simulation.IsNull()) mitkThrow() << "Invalid argument (null pointer)!"; sofa::simulation::Simulation::SPtr sofaSimulation = simulation->GetSimulation(); mitk::SimulationDrawTool* drawTool = simulation->GetDrawTool(); sofa::simulation::Simulation* activeSimulation = sofa::simulation::getSimulation(); sofa::core::visual::DrawTool*& activeDrawTool = sofa::core::visual::VisualParams::defaultInstance()->drawTool(); return sofaSimulation == activeSimulation && drawTool == activeDrawTool; } static bool NoActiveSimulation() { sofa::simulation::Simulation* activeSimulation = sofa::simulation::getSimulation(); sofa::core::visual::DrawTool*& activeDrawTool = sofa::core::visual::VisualParams::defaultInstance()->drawTool(); return activeSimulation == NULL && activeDrawTool == NULL; } static void DrawSimulation(mitk::Simulation::Pointer simulation, bool updateContext = true) { if (simulation.IsNull()) mitkThrow() << "Invalid argument (null pointer)!"; sofa::simulation::Node::SPtr rootNode = simulation->GetRootNode(); if (!rootNode) mitkThrow() << "Invalid argument (simulation is not initialized)!"; simulation->SetAsActiveSimulation(); if (updateContext) rootNode->execute(sofa::core::ExecParams::defaultInstance()); sofa::simulation::Simulation::SPtr sofaSimulation = simulation->GetSimulation(); sofaSimulation->updateVisual(rootNode.get()); sofaSimulation->draw(sofa::core::visual::VisualParams::defaultInstance(), rootNode.get()); } static vtkIdType GetNumberOfPolys(mitk::Surface::Pointer surface, unsigned int t = 0) { vtkIdType numPolys = 0; if (surface.IsNotNull()) { vtkPolyData* polyData = surface->GetVtkPolyData(); if (polyData != NULL) numPolys = polyData->GetNumberOfPolys(); } return numPolys; } -static void SetActiveSimulation_SimulationIsNull_NoActiveSimulation() +static void SetActiveSimulation_InputIsNull_NoActiveSimulation() { mitk::Simulation::SetActiveSimulation(NULL); - MITK_TEST_CONDITION(NoActiveSimulation(), "SetActiveSimulation_SimulationIsNull_NoActiveSimulation") + MITK_TEST_CONDITION(NoActiveSimulation(), "SetActiveSimulation_InputIsNull_NoActiveSimulation") } -static void SetAsActiveSimulation_SimulationIsNotInitialized_IsActiveSimulation() +static void SetAsActiveSimulation_NotInitialized_IsActiveSimulation() { mitk::Simulation::Pointer simulation = mitk::Simulation::New(); simulation->SetAsActiveSimulation(); - MITK_TEST_CONDITION(IsActiveSimulation(simulation), "SetAsActiveSimulation_SimulationIsNotInitialized_IsActiveSimulation") + MITK_TEST_CONDITION(IsActiveSimulation(simulation), "SetAsActiveSimulation_NotInitialized_IsActiveSimulation") } -static void SetAsActiveSimulation_SimulationIsInitialized_IsActiveSimulation(const std::string& filename) +static void SetAsActiveSimulation_Initialized_IsActiveSimulation(const std::string& filename) { mitk::Simulation::Pointer simulation = LoadSimulation(filename); simulation->SetAsActiveSimulation(); - MITK_TEST_CONDITION(IsActiveSimulation(simulation), "SetAsActiveSimulation_SimulationIsInitialized_IsActiveSimulation") + MITK_TEST_CONDITION(IsActiveSimulation(simulation), "SetAsActiveSimulation_Initialized_IsActiveSimulation") } -static void GetRootNode_SimulationIsNotInitialized_ReturnsNull() +static void GetRootNode_NotInitialized_ReturnsNull() { mitk::Simulation::Pointer simulation = mitk::Simulation::New(); - MITK_TEST_CONDITION(!simulation->GetRootNode(), "GetRootNode_SimulationIsNotInitialized_ReturnsNull") + MITK_TEST_CONDITION(!simulation->GetRootNode(), "GetRootNode_NotInitialized_ReturnsNull") } -static void GetRootNode_SimulationIsInitialized_ReturnsRootNode(const std::string& filename) +static void GetRootNode_Initialized_ReturnsRootNode(const std::string& filename) { mitk::Simulation::Pointer simulation = LoadSimulation(filename); - MITK_TEST_CONDITION(simulation->GetRootNode(), "GetRootNode_SimulationIsInitialized_ReturnsRootNode") + MITK_TEST_CONDITION(simulation->GetRootNode(), "GetRootNode_Initialized_ReturnsRootNode") } -static void GetDefaultDT_SimulationIsNotInitialized_ReturnsZero() +static void GetDefaultDT_NotInitialized_ReturnsZero() { mitk::Simulation::Pointer simulation = mitk::Simulation::New(); - MITK_TEST_CONDITION(mitk::Equal(simulation->GetDefaultDT(), 0.0), "GetDefaultDT_SimulationIsNotInitialized_ReturnsZero") + MITK_TEST_CONDITION(mitk::Equal(simulation->GetDefaultDT(), 0.0), "GetDefaultDT_NotInitialized_ReturnsZero") } -static void GetDefaultDT_SimulationIsInitialized_ReturnsDefaultDT(const std::string& filename, double dt) +static void GetDefaultDT_Initialized_ReturnsDefaultDT(const std::string& filename, double dt) { mitk::Simulation::Pointer simulation = LoadSimulation(filename); - MITK_TEST_CONDITION(mitk::Equal(simulation->GetDefaultDT(), dt), "GetDefaultDT_SimulationIsInitialized_ReturnsDefaultDT") + MITK_TEST_CONDITION(mitk::Equal(simulation->GetDefaultDT(), dt), "GetDefaultDT_Initialized_ReturnsDefaultDT") } static void GetDrawTool_Always_ReturnsDrawTool() { mitk::Simulation::Pointer simulation = mitk::Simulation::New(); MITK_TEST_CONDITION(simulation->GetDrawTool() != NULL, "GetDrawTool_Always_ReturnsDrawTool") } static void GetSimulation_Always_ReturnsSimulation() { mitk::Simulation::Pointer simulation = mitk::Simulation::New(); MITK_TEST_CONDITION(simulation->GetSimulation(), "GetSimulation_Always_ReturnsSimulation") } static void SetRootNode_InputIsNull_RootNodeIsNull() { mitk::Simulation::Pointer simulation = mitk::Simulation::New(); simulation->SetRootNode(NULL); MITK_TEST_CONDITION(!simulation->GetRootNode(), "SetRootNode_InputIsNull_RootNodeIsNull") } static void SetRootNode_InputIsValid_RootNodeEqualsInput() { mitk::Simulation::Pointer simulation = mitk::Simulation::New(); sofa::simulation::Node::SPtr rootNode = sofa::core::objectmodel::SPtr_dynamic_cast(sofa::core::objectmodel::New()); simulation->SetRootNode(rootNode.get()); MITK_TEST_CONDITION(simulation->GetRootNode() == rootNode, "SetRootNode_InputIsValid_RootNodeEqualsInput") } static void SetDefaultDT_InputIsPositive_DefaultDTEqualsInput() { mitk::Simulation::Pointer simulation = mitk::Simulation::New(); const double dt = 0.1; simulation->SetDefaultDT(dt); MITK_TEST_CONDITION(mitk::Equal(simulation->GetDefaultDT(), dt), "SetDefaultDT_InputIsPositive_DefaultDTEqualsInput") } static void SetDefaultDT_InputIsNegative_DefaultDTIsZero() { mitk::Simulation::Pointer simulation = mitk::Simulation::New(); simulation->SetDefaultDT(-0.1); MITK_TEST_CONDITION(mitk::Equal(simulation->GetDefaultDT(), 0.0), "SetDefaultDT_InputIsNegative_DefaultDTIsZero") } -static void TakeSnapshot_SimulationIsNotInitialized_ReturnsNull() +static void TakeSnapshot_NotInitialized_ReturnsNull() { mitk::Simulation::Pointer simulation = mitk::Simulation::New(); MITK_TEST_CONDITION(simulation->TakeSnapshot().IsNull(), "TakeSnapshot_SimulationIsNotInitialized_ReturnsNull"); } static void TakeSnapshot_SimulationWasDrawn_ReturnsSurface(const std::string& filename, vtkIdType numPolys) { mitk::Simulation::Pointer simulation = LoadSimulation(filename); DrawSimulation(simulation); mitk::Surface::Pointer snapshot = simulation->TakeSnapshot(); MITK_TEST_CONDITION(GetNumberOfPolys(snapshot) == numPolys, "TakeSnapshot_SimulationWasDrawn_ReturnsSurface"); } -static void AppendSnapshot_SimulationIsNotInitialized_ReturnsFalse() +static void AppendSnapshot_NotInitialized_ReturnsFalse() { mitk::Simulation::Pointer simulation = mitk::Simulation::New(); mitk::Surface::Pointer record = mitk::Surface::New(); - MITK_TEST_CONDITION(!simulation->AppendSnapshot(record), "AppendSnapshot_SimulationIsNotInitialized_ReturnsFalse"); + MITK_TEST_CONDITION(!simulation->AppendSnapshot(record), "AppendSnapshot_NotInitialized_ReturnsFalse"); } static void AppendSnapshot_InputIsNull_ReturnsFalse(const std::string& filename) { mitk::Simulation::Pointer simulation = mitk::Simulation::New(); MITK_TEST_CONDITION(!simulation->AppendSnapshot(NULL), "AppendSnapshot_InputIsNull_ReturnsFalse"); } static void AppendSnapshot_InputIsEmpty_AppendsSnapshotAndReturnsTrue(const std::string& filename, vtkIdType numPolys) { mitk::Simulation::Pointer simulation = LoadSimulation(filename); DrawSimulation(simulation); mitk::Surface::Pointer record = mitk::Surface::New(); MITK_TEST_CONDITION(simulation->AppendSnapshot(record) && GetNumberOfPolys(record) == numPolys, "AppendSnapshot_InputIsEmpty_AppendsSnapshotAndReturnsTrue") } static void AppendSnapshot_InputIsNotEmpty_AppendsSnapshotAndReturnsTrue(const std::string& filename, vtkIdType numPolys) { mitk::Simulation::Pointer simulation = LoadSimulation(filename); DrawSimulation(simulation); mitk::Surface::Pointer record = mitk::Surface::New(); simulation->AppendSnapshot(record); MITK_TEST_CONDITION(simulation->AppendSnapshot(record) && GetNumberOfPolys(record, 1) == numPolys, "AppendSnapshot_InputIsNotEmpty_AppendsSnapshotAndReturnsTrue") } int mitkSimulationTest(int argc, char* argv[]) { if (argc != 4) { MITK_ERROR << "Invalid argument count!\n" << "Usage: mitkSimulationTest
\n" << " Path to simulation scene\n" << "
Default time step\n" << " Total polygon count of all visual models"; return EXIT_FAILURE; } const std::string filename = argv[1]; const double dt = lexical_cast(argv[2]); const vtkIdType numPolys = lexical_cast(argv[3]); mitk::RegisterSimulationObjectFactory(); MITK_TEST_BEGIN("mitkSimulationTest") - SetActiveSimulation_SimulationIsNull_NoActiveSimulation(); - SetAsActiveSimulation_SimulationIsNotInitialized_IsActiveSimulation(); - SetAsActiveSimulation_SimulationIsInitialized_IsActiveSimulation(filename); + SetActiveSimulation_InputIsNull_NoActiveSimulation(); + SetAsActiveSimulation_NotInitialized_IsActiveSimulation(); + SetAsActiveSimulation_Initialized_IsActiveSimulation(filename); - GetRootNode_SimulationIsNotInitialized_ReturnsNull(); - GetRootNode_SimulationIsInitialized_ReturnsRootNode(filename); + GetRootNode_NotInitialized_ReturnsNull(); + GetRootNode_Initialized_ReturnsRootNode(filename); - GetDefaultDT_SimulationIsNotInitialized_ReturnsZero(); - GetDefaultDT_SimulationIsInitialized_ReturnsDefaultDT(filename, dt); + GetDefaultDT_NotInitialized_ReturnsZero(); + GetDefaultDT_Initialized_ReturnsDefaultDT(filename, dt); GetDrawTool_Always_ReturnsDrawTool(); GetSimulation_Always_ReturnsSimulation(); SetRootNode_InputIsNull_RootNodeIsNull(); SetRootNode_InputIsValid_RootNodeEqualsInput(); SetDefaultDT_InputIsPositive_DefaultDTEqualsInput(); SetDefaultDT_InputIsNegative_DefaultDTIsZero(); - TakeSnapshot_SimulationIsNotInitialized_ReturnsNull(); + TakeSnapshot_NotInitialized_ReturnsNull(); TakeSnapshot_SimulationWasDrawn_ReturnsSurface(filename, numPolys); - AppendSnapshot_SimulationIsNotInitialized_ReturnsFalse(); + AppendSnapshot_NotInitialized_ReturnsFalse(); AppendSnapshot_InputIsNull_ReturnsFalse(filename); AppendSnapshot_InputIsEmpty_AppendsSnapshotAndReturnsTrue(filename, numPolys); AppendSnapshot_InputIsNotEmpty_AppendsSnapshotAndReturnsTrue(filename, numPolys); MITK_TEST_END() } diff --git a/Modules/Simulation/mitkSimulationTemplate.cpp b/Modules/Simulation/mitkSimulationTemplate.cpp index 8877c30c63..57888996fc 100644 --- a/Modules/Simulation/mitkSimulationTemplate.cpp +++ b/Modules/Simulation/mitkSimulationTemplate.cpp @@ -1,321 +1,321 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkSimulationTemplate.h" #include #include typedef std::vector > TemplateIndex; typedef std::vector > VariableContents; static TemplateIndex CreateTemplateIndex(const std::string& contents) { TemplateIndex templateIndex; std::string::size_type begin = 0; begin = contents.find_first_of('{', 0); while (begin != std::string::npos) { std::string::size_type end = contents.find_first_of('}', begin); if (end == std::string::npos) mitkThrow() << "Expected closing brace before end of file!"; templateIndex.push_back(std::make_pair(begin, ++end - begin)); begin = contents.find_first_of('{', end); } return templateIndex; } static std::vector ParseStaticContents(const std::string& contents, const TemplateIndex& templateIndex) { std::vector staticContents; std::string::size_type index = 0; for (TemplateIndex::const_iterator it = templateIndex.begin(); it != templateIndex.end(); ++it) { staticContents.push_back(contents.substr(index, it->first - index)); index = it->first + it->second; } staticContents.push_back(contents.substr(index)); return staticContents; } static std::string ParseValue(const std::string& templ, const std::string& valueName) { const char* spaces = " \t\n"; std::string::size_type index = templ.find(valueName); if (index != std::string::npos) { index += valueName.length(); index = templ.find_first_not_of(spaces, index); if (index != std::string::npos && templ[index] == '=') { ++index; index = templ.find_first_not_of(spaces, index); if (index != std::string::npos && templ[index] == '\'') { ++index; std::string::size_type length = templ.find_first_of('\'', index); if (length != std::string::npos) { length -= index; return templ.substr(index, length); } } } } return ""; } template T FromString(const std::string& string) { std::istringstream stream(string); T value; stream >> value; return value; } static std::pair ParseReference(const std::string& ref) { std::string id = "{ref}"; mitk::StringProperty::Pointer property = mitk::StringProperty::New(ref.substr(2, ref.length() - 4)); return std::make_pair(id, property); } static std::pair ParseTemplate(const std::string& templ) { if (templ.length() > 4 && templ[1] == '\'') { return ParseReference(templ); } else { std::string id = ParseValue(templ, "id"); if (!id.empty()) { std::string type = ParseValue(templ, "type"); if (type.empty()) type = "string"; mitk::BaseProperty::Pointer property; std::string defaultValue = ParseValue(templ, "default"); if (type == "float") { float value = !defaultValue.empty() ? FromString(defaultValue) : 0.0; property = mitk::FloatProperty::New(value); } else if (type == "int") { int value = !defaultValue.empty() ? FromString(defaultValue) : 0.0; property = mitk::IntProperty::New(value); } else if (type == "string") { std::string value = !defaultValue.empty() ? defaultValue : ""; property = mitk::StringProperty::New(value); } if (property.IsNotNull()) return std::make_pair(id, property); } } std::string emptyString; mitk::BaseProperty::Pointer nullPointer; return std::make_pair(emptyString, nullPointer); } static VariableContents ParseVariableContents(const std::string& contents, const TemplateIndex& templateIndex) { VariableContents variableContents; for (TemplateIndex::const_iterator it = templateIndex.begin(); it != templateIndex.end(); ++it) { std::string templ = contents.substr(it->first, it->second); std::pair variableContent = ParseTemplate(templ); if (variableContent.first.empty() || variableContent.second.IsNull()) mitkThrow() << "Could not parse " << templ << "!"; variableContents.push_back(variableContent); } return variableContents; } template class FirstEqualTo { public: FirstEqualTo(const T1& value) : m_Value(value) { } bool operator()(const std::pair& pair) const { return pair.first == m_Value; } private: T1 m_Value; }; mitk::SimulationTemplate::SimulationTemplate() : m_IsInitialized(false) { } mitk::SimulationTemplate::~SimulationTemplate() { } std::string mitk::SimulationTemplate::CreateSimulation() const { if (!m_IsInitialized) { - MITK_ERROR << "Simulation template is not initialized!"; + MITK_DEBUG << "Simulation template is not initialized!"; return ""; } std::string contents; for (VariableContents::size_type i = 0; i < m_VariableContents.size(); ++i) { contents += m_StaticContents[i]; if (m_VariableContents[i].first == "{ref}") { VariableContents::const_iterator it = std::find_if(m_VariableContents.begin(), m_VariableContents.end(), FirstEqualTo(m_VariableContents[i].second->GetValueAsString())); if (it == m_VariableContents.end()) { - MITK_ERROR << "Template '" << m_VariableContents[i].second << "' not found!"; + MITK_DEBUG << "Template '" << m_VariableContents[i].second << "' not found!"; return ""; } contents += it->second->GetValueAsString(); } else { contents += m_VariableContents[i].second->GetValueAsString(); } } contents += m_StaticContents.back(); return contents; } bool mitk::SimulationTemplate::Parse(const std::string& contents) { if (m_IsInitialized) { - MITK_ERROR << "Simulation template is already initialized!"; + MITK_DEBUG << "Simulation template is already initialized!"; return false; } TemplateIndex templateIndex = CreateTemplateIndex(contents); std::vector staticContents = ParseStaticContents(contents, templateIndex); VariableContents variableContents = ParseVariableContents(contents, templateIndex); std::swap(m_StaticContents, staticContents); std::swap(m_VariableContents, variableContents); m_IsInitialized = true; return true; } bool mitk::SimulationTemplate::RequestedRegionIsOutsideOfTheBufferedRegion() { return false; } bool mitk::SimulationTemplate::SetProperties(mitk::DataNode::Pointer dataNode) const { if (dataNode.IsNull()) { - MITK_ERROR << "Data node does not exist!"; + MITK_DEBUG << "Data node does not exist!"; return false; } if (!m_IsInitialized) { - MITK_ERROR << "Simulation template is not initialized!"; + MITK_DEBUG << "Simulation template is not initialized!"; return false; } if (dynamic_cast(dataNode->GetData()) != this) { - MITK_ERROR << "Data node does not own this simulation template!"; + MITK_DEBUG << "Data node does not own this simulation template!"; return false; } for(VariableContents::const_iterator it = m_VariableContents.begin(); it != m_VariableContents.end(); ++it) { if (it->first != "{ref}") dataNode->SetProperty(it->first.c_str(), it->second.GetPointer()); } return true; } void mitk::SimulationTemplate::SetRequestedRegion(const itk::DataObject*) { } void mitk::SimulationTemplate::SetRequestedRegionToLargestPossibleRegion() { } void mitk::SimulationTemplate::UpdateOutputInformation() { if (this->GetSource().IsNotNull()) this->GetSource()->UpdateOutputInformation(); } bool mitk::SimulationTemplate::VerifyRequestedRegion() { return true; }