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

ENH: made ComponentSelector templated over ComponentList to get rid of itk

factory SuperElastix/SuperElastix#25
parent fc99630c
......@@ -26,10 +26,11 @@
namespace selx
{
/** \class ComponentFactor
* \brief Create instances of MetaImageIO objects using an object factory.
* \ingroup ITKIOMeta
/** \class ComponentSelector
* \brief A Component factory that accepts criteria, possibly in multiple passes, to construct and return the right Component
*/
template <class ComponentList>
class ComponentSelector : public itk::Object
{
public:
......@@ -91,8 +92,8 @@ private:
};
} // end namespace selx
//#ifndef ITK_MANUAL_INSTANTIATION
//#include "ComponentSelector.hxx"
//#endif
#ifndef ITK_MANUAL_INSTANTIATION
#include "selxComponentSelector.hxx"
#endif
#endif
......@@ -16,14 +16,15 @@
* limitations under the License.
*
*=========================================================================*/
//#ifndef ComponentSelector_hxx
//#define ComponentSelector_hxx
#ifndef selxComponentSelector_hxx
#define selxComponentSelector_hxx
#include "selxComponentSelector.h"
namespace selx
{
ComponentSelector::ComponentSelector()
template <class ComponentList>
ComponentSelector<ComponentList>::ComponentSelector()
{
std::list< itk::LightObject::Pointer > allobjects
= itk::ObjectFactoryBase::CreateAllInstance( "ComponentBase" );
......@@ -40,12 +41,13 @@ ComponentSelector::ComponentSelector()
}
}
ComponentSelector::~ComponentSelector()
template <class ComponentList>
ComponentSelector<ComponentList>::~ComponentSelector()
{
}
void ComponentSelector::ComponentName(const std::string name)
template <class ComponentList>
void ComponentSelector<ComponentList>::ComponentName(const std::string name)
{
for (const auto & component : this->m_PossibleComponents)
{
......@@ -53,27 +55,27 @@ void ComponentSelector::ComponentName(const std::string name)
}
}
template <class ComponentList>
void
ComponentSelector::AddCriterion( const CriterionType & criterion )
ComponentSelector<ComponentList>::AddCriterion(const CriterionType & criterion)
{
this->m_PossibleComponents.remove_if([ & ]( ComponentBasePointer component ){
return !component->MeetsCriterion( criterion );
} );
}
template <class ComponentList>
void
ComponentSelector::AddAcceptingInterfaceCriteria( const InterfaceCriteriaType & interfaceCriteria )
ComponentSelector<ComponentList>::AddAcceptingInterfaceCriteria(const InterfaceCriteriaType & interfaceCriteria)
{
this->m_PossibleComponents.remove_if([ & ]( ComponentBasePointer component ){
return 0 == component->CountAcceptingInterfaces( interfaceCriteria );
} );
}
template <class ComponentList>
void
ComponentSelector::AddProvidingInterfaceCriteria( const InterfaceCriteriaType & interfaceCriteria )
ComponentSelector<ComponentList>::AddProvidingInterfaceCriteria(const InterfaceCriteriaType & interfaceCriteria)
{
this->m_PossibleComponents.remove_if([ & ]( ComponentBasePointer component ){
return 0 == component->CountProvidingInterfaces( interfaceCriteria );
......@@ -82,8 +84,9 @@ ComponentSelector::AddProvidingInterfaceCriteria( const InterfaceCriteriaType &
// CompatibleInterfaces
template <class ComponentList>
unsigned int
ComponentSelector::RequireAcceptingInterfaceFrom( ComponentBasePointer other, const InterfaceCriteriaType & interfaceCriteria )
ComponentSelector<ComponentList>::RequireAcceptingInterfaceFrom(ComponentBasePointer other, const InterfaceCriteriaType & interfaceCriteria)
{
this->m_PossibleComponents.remove_if([ & ]( ComponentBasePointer component ){
auto status = component->CanAcceptConnectionFrom( other, interfaceCriteria );
......@@ -92,9 +95,9 @@ ComponentSelector::RequireAcceptingInterfaceFrom( ComponentBasePointer other, co
return 0;
}
template <class ComponentList>
unsigned int
ComponentSelector::RequireProvidingInterfaceTo( ComponentBasePointer other, const InterfaceCriteriaType & interfaceCriteria )
ComponentSelector<ComponentList>::RequireProvidingInterfaceTo(ComponentBasePointer other, const InterfaceCriteriaType & interfaceCriteria)
{
this->m_PossibleComponents.remove_if([ & ]( ComponentBasePointer component ){
auto status = other->CanAcceptConnectionFrom( component, interfaceCriteria );
......@@ -103,9 +106,9 @@ ComponentSelector::RequireProvidingInterfaceTo( ComponentBasePointer other, cons
return 0;
}
ComponentSelector::ComponentBasePointer
ComponentSelector::GetComponent()
template <class ComponentList>
typename ComponentSelector<ComponentList>::ComponentBasePointer
ComponentSelector<ComponentList>::GetComponent()
{
//TODO check if Modified
//this->UpdatePossibleComponents();
......@@ -120,15 +123,16 @@ ComponentSelector::GetComponent()
}
}
template <class ComponentList>
unsigned int
ComponentSelector::NumberOfComponents()
ComponentSelector<ComponentList>::NumberOfComponents()
{
return this->m_PossibleComponents.size();
}
template <class ComponentList>
void
ComponentSelector::PrintComponents(void)
ComponentSelector<ComponentList>::PrintComponents(void)
{
/*
for (auto & component : this->m_PossibleComponents)
......@@ -144,4 +148,4 @@ ComponentSelector::PrintComponents(void)
}
} // end namespace selx
//#endif
#endif //selxComponentSelector_hxx
......@@ -89,10 +89,11 @@ protected:
typedef ComponentBase::CriterionType CriterionType;
typedef ComponentBase::ParameterValueType ParameterValueType;
typedef ComponentSelector::Pointer ComponentSelectorPointer;
typedef typename ComponentSelector<ComponentList> ComponentSelectorType;
typedef typename ComponentSelectorType::Pointer ComponentSelectorPointer;
typedef std::map< ComponentNameType, ComponentSelectorPointer > ComponentSelectorContainerType;
typedef ComponentSelectorContainerType::iterator ComponentSelectorIteratorType;
typedef typename ComponentSelectorContainerType::iterator ComponentSelectorIteratorType;
/** Read configuration at the blueprints nodes and try to find instantiated components */
virtual void ApplyComponentConfiguration();
......
......@@ -144,7 +144,7 @@ NetworkBuilder<ComponentList>::ApplyComponentConfiguration()
for( auto const & name : componentNames )
{
std::cout << " Blueprint Node: " << name << std::endl;
ComponentSelectorPointer currentComponentSelector = ComponentSelector::New();
ComponentSelectorPointer currentComponentSelector = ComponentSelectorType::New();
currentComponentSelector->ComponentName(name); // Todo via constructor
Blueprint::ParameterMapType currentProperty = this->m_Blueprint->GetComponent( name );
......
......@@ -19,6 +19,7 @@
#include "gtest/gtest.h"
#include "selxTypeList.h"
#include "selxTransformComponent1.h"
//#include "itkTransformComponent1Factory.h"
......@@ -45,8 +46,9 @@ public:
typedef ComponentBase::ParameterValueType ParameterValueType;
//typedef std::map<std::string, std::string> CriteriaType;
//typedef std::pair<std::string, std::string> CriterionType;
typedef TypeList<> ComponentList;
typedef ComponentSelector<ComponentList>::Pointer NodePointer;
typedef ComponentSelector::Pointer NodePointer;
virtual void SetUp()
{
......@@ -102,7 +104,7 @@ TEST_F( ComponentFactoryTest, SetEmptyCriteria )
CriterionType emptyCriterion; // = CriterionType();
ASSERT_NO_THROW( Node1 = ComponentSelector::New() );
ASSERT_NO_THROW(Node1 = ComponentSelector<ComponentList>::New());
EXPECT_NO_THROW( Node1->AddCriterion( emptyCriterion ) );
ComponentType::Pointer Node1Component;
......@@ -118,7 +120,7 @@ TEST_F( ComponentFactoryTest, SetSufficientCriteria )
EXPECT_NO_THROW( ComponentFactory< MetricComponent1 >::RegisterOneFactory() );
CriterionType criterion2 = { "ComponentInput", { "Transform" } };
ASSERT_NO_THROW( Node2 = ComponentSelector::New() );
ASSERT_NO_THROW(Node2 = ComponentSelector<ComponentList>::New());
ASSERT_NO_THROW( Node2->AddCriterion( criterion2 ) );
ComponentType::Pointer Node2Component;
......@@ -138,7 +140,7 @@ TEST_F( ComponentFactoryTest, AddCriteria )
CriterionType criterion1( { "ComponentOutput", { "Transform" } } );
Node1 = ComponentSelector::New();
Node1 = ComponentSelector<ComponentList>::New();
EXPECT_NO_THROW( Node1->AddCriterion( nonSelectiveCriterion ) );
ComponentType::Pointer Node1Component;
......@@ -172,14 +174,14 @@ TEST_F( ComponentFactoryTest, InterfacedObjects )
// " 6 Component objects available to the NetworkBuilder."
EXPECT_EQ( registeredComponents.size(), 6 );
NodePointer Node3 = ComponentSelector::New();
NodePointer Node3 = ComponentSelector<ComponentList>::New();
Node3->AddAcceptingInterfaceCriteria( { { "NameOfInterface", "MetricDerivativeInterface" } } );
ComponentType::Pointer Node3Component;
EXPECT_NO_THROW( Node3Component = Node3->GetComponent() );
EXPECT_STREQ( Node3Component->GetNameOfClass(), "GDOptimizer3rdPartyComponent" );
NodePointer Node4 = ComponentSelector::New();
NodePointer Node4 = ComponentSelector<ComponentList>::New();
Node4->AddProvidingInterfaceCriteria( { { "NameOfInterface", "MetricDerivativeInterface" } } );
ComponentType::Pointer Node4Component;
EXPECT_NO_THROW( Node4Component = Node4->GetComponent() );
......@@ -200,7 +202,7 @@ TEST_F( ComponentFactoryTest, UnknownComponent )
// Setup the criterion for a component that does not exist in our data base
CriterionType criterion( { "NameOfClass", { "DoYouHaveThisComponent?" } } );
NodePointer Node = ComponentSelector::New();
NodePointer Node = ComponentSelector<ComponentList>::New();
Node->AddCriterion( criterion );
ComponentType::Pointer NodeComponent;
......
......@@ -38,7 +38,6 @@ set( ${MODULE}_SOURCE_FILES
${${MODULE}_SOURCE_DIR}/ComponentInterface/src/selxComponentBase.cxx
${${MODULE}_SOURCE_DIR}/ComponentInterface/src/selxCheckTemplateProperties.cxx
${${MODULE}_SOURCE_DIR}/ComponentInterface/src/selxNetworkContainer.cxx
${${MODULE}_SOURCE_DIR}/ComponentInterface/src/selxComponentSelector.cxx
${${MODULE}_SOURCE_DIR}/ConfigurationReader/src/selxConfigurationReader.cxx
${${MODULE}_SOURCE_DIR}/Filter/src/selxSuperElastixFilter.cxx
${${MODULE}_SOURCE_DIR}/Logger/src/selxLogger.cxx
......
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