Commit aedabcbb authored by Kasper Marstal's avatar Kasper Marstal
Browse files

ENH: Factor out NetworkBuilder factory mechanism; ENH: Add Logger to SuperElastixFilter

parent 3f51456f
......@@ -22,6 +22,7 @@
#include "selxConfigurationReader.h"
#include "selxAnyFileReader.h"
#include "selxAnyFileWriter.h"
#include "selxLogger.h"
#include <boost/algorithm/string.hpp>
......@@ -56,6 +57,11 @@ main( int ac, char * av[] )
// instantiate a SuperElastixFilter that is loaded with default components
selx::SuperElastixFilter::Pointer superElastixFilter = selx::SuperElastixFilter::New();
// add logger
selx::SuperElastixFilter::LoggerPointer itkLogger = selx::SuperElastixFilter::LoggerType::New();
itkLogger->Get()->AddConsole();
superElastixFilter->SetLogger( itkLogger );
fs::path configurationPath;
VectorOfStringsType inputPairs;
VectorOfStringsType outputPairs;
......
......@@ -37,8 +37,6 @@
#include "selxInterfaces.h"
#include "selxInterfaceTraits.h"
#include "selxLogger.h"
namespace selx
{
template< class ComponentList >
......@@ -59,11 +57,9 @@ public:
typedef std::map<
std::string, RegistrationControllerStartInterface::Pointer > RegistrationControllerStartInterfaceMapType;
NetworkBuilder();
NetworkBuilder( const Logger & logger );
virtual ~NetworkBuilder() {}
virtual std::unique_ptr< NetworkBuilderBase > ConstructNewDerivedInstance( void );
virtual bool AddBlueprint( const std::unique_ptr< Blueprint > & blueprint );
/** Read configuration at the blueprints nodes and edges and return true if all components could be uniquely selected*/
......@@ -119,7 +115,7 @@ protected:
// A selector for each node, that each can hold multiple instantiated components. Ultimately is should be 1 component each.
ComponentSelectorContainerType m_ComponentSelectorContainer;
bool m_isConfigured;
Logger m_Logger;
const Logger & m_Logger;
private:
};
......
......@@ -20,23 +20,16 @@
#include "selxNetworkBuilder.h"
#include "selxKeys.h"
#include "selxSuperElastixComponent.h"
#include "selxLogger.h"
namespace selx
{
template< typename ComponentList >
NetworkBuilder< ComponentList >::NetworkBuilder() : m_isConfigured( false ), m_Blueprint( new Blueprint )
NetworkBuilder< ComponentList >::NetworkBuilder( const Logger & logger ) : m_Logger(logger), m_isConfigured( false ), m_Blueprint( new Blueprint )
{
}
template< typename ComponentList >
std::unique_ptr< NetworkBuilderBase >
NetworkBuilder< ComponentList >::ConstructNewDerivedInstance( void )
{
return std::unique_ptr< NetworkBuilderBase >( new NetworkBuilder< ComponentList > );
}
template< typename ComponentList >
bool
NetworkBuilder< ComponentList >::AddBlueprint( const std::unique_ptr< Blueprint > & blueprint )
......@@ -544,4 +537,5 @@ NetworkBuilder< ComponentList >::GetRealizedNetwork()
return NetworkContainer( components, outputObjectsMap );
}
}
} // end namespace selx
......@@ -36,6 +36,7 @@
#include "selxNetworkContainer.h"
#include "selxAnyFileReader.h"
#include "selxAnyFileWriter.h"
#include "selxLogger.h"
namespace selx
{
......@@ -61,9 +62,6 @@ public:
virtual ~NetworkBuilderBase() {}
/** To create a derived NetworkBuilder from a NetworkBuilderBase pointer without knowing the template arguments of the derived*/
virtual std::unique_ptr< NetworkBuilderBase > ConstructNewDerivedInstance( void ) = 0;
virtual bool AddBlueprint( const std::unique_ptr< Blueprint > & blueprint ) = 0;
/** Read configuration at the blueprints nodes and edges and return true if all components could be uniquely selected*/
......
/*=========================================================================
*
* Copyright Leiden University Medical Center, Erasmus University Medical
* Center and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef NetworkBuilderFactory_h
#define NetworkBuilderFactory_h
#include "selxNetworkBuilderFactoryBase.h"
namespace selx
{
template< class ComponentList >
class NetworkBuilderFactory : public NetworkBuilderFactoryBase
{
public:
NetworkBuilderFactory() {}
virtual ~NetworkBuilderFactory() {}
virtual std::unique_ptr< NetworkBuilderBase > New( const Logger & logger );
};
} // end namespace selx
#ifndef ITK_MANUAL_INSTANTIATION
#include "selxNetworkBuilderFactory.hxx"
#endif
#endif // NetworkBuilderFactory_h
/*=========================================================================
*
* Copyright Leiden University Medical Center, Erasmus University Medical
* Center and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include "selxNetworkBuilder.h"
#include "selxNetworkBuilderFactory.h"
namespace selx
{
template< typename ComponentList >
std::unique_ptr< NetworkBuilderBase >
NetworkBuilderFactory< ComponentList >::New( const Logger & logger )
{
return std::unique_ptr< NetworkBuilderBase >( new NetworkBuilder< ComponentList >( logger ) );
}
} // end namespace selx
/*=========================================================================
*
* Copyright Leiden University Medical Center, Erasmus University Medical
* Center and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef NetworkBuilderFactoryBase_h
#define NetworkBuilderFactoryBase_h
namespace selx
{
class NetworkBuilderFactoryBase
{
public:
NetworkBuilderFactoryBase() {}
virtual ~NetworkBuilderFactoryBase() {}
/** To create a derived NetworkBuilderFactory from a NetworkBuilderFactoryBase pointer without knowing the template arguments of the derived*/
virtual std::unique_ptr< NetworkBuilderBase > New( const Logger & logger ) = 0;
private:
};
} // end namespace selx
#endif // NetworkBuilderBase_h
......@@ -18,6 +18,7 @@
*=========================================================================*/
#include "selxNetworkBuilder.h"
#include "selxLogger.h"
//#include "ComponentFactory.h"
#include "selxTransformComponent1.h"
......@@ -73,16 +74,17 @@ public:
BlueprintPointer blueprint;
Logger * logger = new Logger();
};
TEST_F( NetworkBuilderTest, Create )
{
NetworkBuilderPointer networkBuilderA = NetworkBuilderPointer( new NetworkBuilder< CustomComponentList >() );
NetworkBuilderPointer networkBuilderA = NetworkBuilderPointer( new NetworkBuilder< CustomComponentList >( *logger ) );
}
TEST_F( NetworkBuilderTest, Configure )
{
NetworkBuilderPointer networkBuilder = NetworkBuilderPointer( new NetworkBuilder< CustomComponentList >() );
NetworkBuilderPointer networkBuilder = NetworkBuilderPointer( new NetworkBuilder< CustomComponentList >( *logger ) );
networkBuilder->AddBlueprint( blueprint );
bool allUniqueComponents;
......@@ -92,7 +94,7 @@ TEST_F( NetworkBuilderTest, Configure )
TEST_F( NetworkBuilderTest, Connect )
{
NetworkBuilderPointer networkBuilder = NetworkBuilderPointer( new NetworkBuilder< CustomComponentList >() );
NetworkBuilderPointer networkBuilder = NetworkBuilderPointer( new NetworkBuilder< CustomComponentList >( *logger ) );
networkBuilder->AddBlueprint( blueprint );
EXPECT_NO_THROW( bool allUniqueComponents = networkBuilder->Configure() );
bool success;
......@@ -213,7 +215,7 @@ TEST_F( NetworkBuilderTest, DeduceComponentsFromConnections )
blueprint->SetConnection( "ResampleFilter", "Controller", { {} } ); //ReconnectTransformInterface
blueprint->SetConnection( "TransformDisplacementFilter", "Controller", { {} } ); //ReconnectTransformInterface
std::unique_ptr< NetworkBuilderBase > networkBuilder( new NetworkBuilder< RegisterComponents >() );
std::unique_ptr< NetworkBuilderBase > networkBuilder( new NetworkBuilder< RegisterComponents >( *logger ) );
networkBuilder->AddBlueprint( blueprint );
bool allUniqueComponents;
EXPECT_NO_THROW( allUniqueComponents = networkBuilder->Configure() );
......
......@@ -22,6 +22,7 @@
#include "itkProcessObject.h"
#include "selxBlueprint.h"
#include "selxLogger.h"
#include "selxAnyFileReader.h"
#include "selxAnyFileWriter.h"
......@@ -35,7 +36,10 @@
namespace selx
{
class NetworkBuilderBase; // forward declaration, hiding implementation details and speeding up compilation time (PIMPL idiom)
// Forward declaration, hiding implementation details and speeding up compilation time (PIMPL idiom)
class NetworkBuilderBase;
class NetworkBuilderFactoryBase;
class SuperElastixFilter : public itk::ProcessObject
{
......@@ -63,8 +67,13 @@ public:
typedef BlueprintType::Pointer BlueprintPointer;
typedef BlueprintType::ConstPointer BlueprintConstPointer;
typedef itk::UniquePointerDataObjectDecorator< Logger > LoggerType;
typedef LoggerType::Pointer LoggerPointer;
typedef LoggerType::ConstPointer LoggerConstPointer;
// Setting a Blueprint creates a pipeline such that when SuperElastixFilter is updated it checks if the blueprint has been modified and if so, SuperElastixFilter resets its internals and start building the blueprint from scratch
itkSetObjectMacro( Blueprint, BlueprintType );
itkSetObjectMacro( Logger, LoggerType );
// Adding a Blueprint composes SuperElastixFilter' internal blueprint (accessible by Set/Get Blueprint) with the otherBlueprint.
// void AddBlueprint(BlueprintPointer otherBlueprint);
......@@ -108,6 +117,10 @@ public:
void Update( void ) ITK_OVERRIDE;
void SetLogger( LoggerPointer logger );
LoggerPointer GetLogger();
protected:
// default constructor for API library use
......@@ -120,8 +133,11 @@ protected:
virtual void GenerateData( void ) ITK_OVERRIDE;
std::unique_ptr< NetworkBuilderFactoryBase > m_NetworkBuilderFactory;
std::unique_ptr< NetworkBuilderBase > m_NetworkBuilder;
LoggerPointer m_Logger;
private:
//TODO make const correct
......
......@@ -29,7 +29,7 @@
namespace selx
{
template< typename ComponentList >
template< typename ComponentTypeList >
class SuperElastixFilterCustomComponents : public SuperElastixFilter
{
public:
......
......@@ -21,19 +21,21 @@
#define selxSuperElastixFilterCustomComponents_hxx
#include "selxSuperElastixFilterCustomComponents.h"
#include "selxNetworkBuilder.h"
#include "selxNetworkBuilderFactory.h"
namespace selx
{
/**
* ********************* Constructor *********************
*/
template< typename ComponentTypeList >
SuperElastixFilterCustomComponents< ComponentTypeList >
::SuperElastixFilterCustomComponents( void ) : SuperElastixFilter( true )
{
//std::make_unique<NetworkBuilder<ComponentTypeList>>();
m_NetworkBuilder = std::unique_ptr< NetworkBuilder< ComponentTypeList >>( new NetworkBuilder< ComponentTypeList > );
m_NetworkBuilderFactory = std::unique_ptr< NetworkBuilderFactory< ComponentTypeList >>( new NetworkBuilderFactory< ComponentTypeList > );
m_Logger = LoggerType::New();
} // end Constructor
} // namespace elx
......
......@@ -19,6 +19,7 @@
#include "selxSuperElastixFilter.h"
#include "selxNetworkBuilder.h"
#include "selxNetworkBuilderFactory.h"
#include "selxDefaultComponents.h"
namespace selx
......@@ -32,7 +33,8 @@ SuperElastixFilter
{
// The default constructor registers the default components.
//std::make_unique<NetworkBuilder<DefaultComponents>>();
m_NetworkBuilder = std::unique_ptr< NetworkBuilder< DefaultComponents >>( new NetworkBuilder< DefaultComponents > );
m_NetworkBuilderFactory = std::unique_ptr< NetworkBuilderFactory< DefaultComponents >>( new NetworkBuilderFactory< DefaultComponents > );
m_Logger = LoggerType::New();
} // end Constructor
......@@ -68,7 +70,7 @@ SuperElastixFilter
{
// Was Blueprint modified by Set() or by AddBlueprint?
// delete previous blueprint and start all over with new one
m_NetworkBuilder = m_NetworkBuilder->ConstructNewDerivedInstance();
m_NetworkBuilder = m_NetworkBuilderFactory->New( *this->m_Logger->Get() );
this->m_NetworkBuilder->AddBlueprint( this->m_Blueprint->Get() );
this->m_AllUniqueComponents = this->m_NetworkBuilder->Configure();
this->m_IsBlueprintParsedOnce = true;
......@@ -287,4 +289,21 @@ SuperElastixFilter
this->GenerateOutputInformation();
this->GenerateData();
}
void
SuperElastixFilter
::SetLogger( LoggerPointer logger )
{
this->m_Logger = logger;
}
SuperElastixFilter::LoggerPointer
SuperElastixFilter
::GetLogger( void )
{
return m_Logger;
}
} // namespace elx
Markdown is supported
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