34 #include "../common/WLogger.h"
36 #include "WModuleCombiner.h"
37 #include "WModuleFactory.h"
38 #include "combiner/WApplyCombiner.h"
39 #include "exceptions/WPrototypeNotUnique.h"
40 #include "exceptions/WPrototypeUnknown.h"
74 std::set< std::string > names;
76 while( listIter != l->get().end() )
81 if( names.count( ( *listIter )->getName() ) )
84 "\" is not unique. Modules have to have a unique name. Ignoring this module." ),
85 "ModuleFactory", LL_ERROR );
87 l->get().erase( listIter++ );
92 names.insert( ( *listIter )->getName() );
109 return ( ticket->get().count( module ) != 0 );
119 wlog::debug(
"ModuleFactory" ) <<
"Creating new instance of prototype \"" << prototype->getName() <<
"\".";
127 throw WPrototypeUnknown( std::string(
"Could not clone module \"" + prototype->getName() +
"\" since it is no prototype." ) );
134 std::shared_ptr< WModule > clone = std::shared_ptr< WModule >( prototype->factory() );
136 clone->setUUID( uuid );
140 clone->setLocalPath( prototype->getLocalPath() );
148 module->initialize();
168 std::shared_ptr< WModule > ret = std::shared_ptr< WModule >();
169 for( std::set< std::shared_ptr< WModule > >::const_iterator listIter = l->get().begin(); listIter != l->get().end();
172 if( ( *listIter )->getName() == name )
187 if( ret == std::shared_ptr< WModule >() )
189 throw WPrototypeUnknown( std::string(
"Could not find prototype \"" + name +
"\"." ) );
202 std::vector< WModule::ConstSPtr > ret;
208 for( std::set< std::shared_ptr< WModule > >::const_iterator listIter = l->get().begin(); listIter != l->get().end();
211 if( ( *listIter )->getType() == type )
213 ret.push_back( *listIter );
227 WCombinerTypes::WCompatiblesList compatibles;
233 bool addModulesWithoutInput = !module;
235 if( addModulesWithoutInput )
242 if(
dynamic_cast< const WDataModule*
>( ( *listIter ).get() ) )
249 if( pcons.size() == 0 )
252 WCombinerTypes::WOneToOneCombiners lComp;
255 lComp.push_back( std::shared_ptr< WApplyCombiner >(
new WApplyCombiner( module,
"", *listIter,
"" ) ) );
258 compatibles.push_back( WCombinerTypes::WCompatiblesGroup( ( *listIter ), lComp ) );
270 WCombinerTypes::WOneToOneCombiners lComp = WApplyCombiner::createCombinerList< WApplyCombiner >( module, ( *listIter ) );
273 if( lComp.size() != 0 )
275 compatibles.push_back( WCombinerTypes::WCompatiblesGroup( ( *listIter ), lComp ) );
284 std::sort( compatibles.begin(), compatibles.end(), WCombinerTypes::compatiblesSort );
291 WCombinerTypes::WCompatiblesList compatibles;
301 WCombinerTypes::WOneToOneCombiners lComp;
304 lComp.push_back( std::shared_ptr< WApplyCombiner >(
new WApplyCombiner( *listIter ) ) );
307 compatibles.push_back( WCombinerTypes::WCompatiblesGroup( ( *listIter ), lComp ) );
314 std::sort( compatibles.begin(), compatibles.end(), WCombinerTypes::compatiblesSort );
326 UuidModuleMap::const_iterator it = r->get().find( uuid );
327 if( it != r->get().end() )
330 std::weak_ptr< WModule > m = ( *it ).second;
343 std::vector< WDataModule::SPtr > result;
346 for( std::vector< WDataModule::SPtr >::const_iterator iter = dataModules.begin(); iter != dataModules.end(); ++iter )
349 std::vector< WDataModuleInputFilter::ConstSPtr > filters = ( *iter )->
getInputFilter();
350 for( std::vector< WDataModuleInputFilter::ConstSPtr >::const_iterator filterIter = filters.begin(); filterIter != filters.end();
354 if( ( *filterIter )->matches( input ) )
356 result.push_back( *iter );
Base class for all combiners which apply one connection between two connectors of two modules.
Base for all data loader modules.
virtual std::vector< WDataModuleInputFilter::ConstSPtr > getInputFilter() const =0
Return a list of input filters.
void addLogMessage(std::string message, std::string source="", LogLevel level=LL_DEBUG)
Appends a log message to the logging queue.
static WLogger * getLogger()
Returns pointer to the currently running logger instance.
const std::shared_ptr< WModule > isPrototypeAvailable(std::string name)
Searches a prototype by name.
PrototypeSharedContainerType m_prototypes
The module prototypes available.
static std::shared_ptr< WModuleFactory > m_instance
Singleton instance of WModuleFactory.
WSharedAssociativeContainer< UuidModuleMap > m_uuidModuleMap
Keep track of uuids of each created module.
std::shared_ptr< WModuleFactory > SPtr
Shared pointer to a WModule.
WCombinerTypes::WCompatiblesList getCompatiblePrototypes(std::shared_ptr< WModule > module=std::shared_ptr< WModule >())
Returns a set of module combiners with module combinations compatible with the specified one.
WCombinerTypes::WCompatiblesList getAllPrototypes()
Creates a list of WApplyCombiner for all modules known by the factory.
PrototypeSharedContainerType::ReadTicket getPrototypes() const
This method gives read access to the list of all prototypes.
static WModule::SPtr findByUUID(std::string uuid)
Find a module instance by UUID.
std::shared_ptr< WModule > create(std::shared_ptr< WModule > prototype, std::string uuid="")
Create a new and initialized module using the specified prototype.
std::set< std::shared_ptr< WModule > >::const_iterator PrototypeContainerConstIteratorType
Const iterator for the prototype set.
const std::shared_ptr< WModule > getPrototypeByInstance(std::shared_ptr< WModule > instance)
Finds a prototype using an instance of a module.
static SPtr getModuleFactory()
Returns instance of the module factory to use to create modules.
std::set< std::shared_ptr< WModule > >::iterator PrototypeContainerIteratorType
Iterator for the prototype set.
std::vector< WDataModule::SPtr > getDataModulePrototypesByInput(WDataModuleInput::ConstSPtr input) const
Query a list of WDataModule prototypes depending on given input.
void load()
Loads the modules and creates prototypes.
WModuleLoader::SPtr m_moduleLoader
Loader class managing dynamically loaded modules in OpenWalnut.
virtual ~WModuleFactory()
Destructor.
static void initializeModule(std::shared_ptr< WModule > module)
This method uses a newly created instance of WModule and initializes it properly.
const std::shared_ptr< WModule > getPrototypeByName(std::string name)
Finds a prototype using the specified name.
std::vector< std::shared_ptr< ModuleType > > getPrototypesByType() const
Get all module prototypes which are of a certain type.
bool checkPrototype(std::shared_ptr< WModule > module, PrototypeSharedContainerType::ReadTicket ticket)
Checks whether the specified module is a prototype or an instantiated module.
static bool isPrototype(std::shared_ptr< WModule > module)
Checks whether the specified module is a prototype or an instantiated module.
WModuleFactory()
Constructors are protected because this is a Singleton.
static std::shared_ptr< WModuleLoader > getModuleLoader()
Returns instance of the module loader.
Loads module prototypes from shared objects in a given directory and injects it into the module facto...
std::shared_ptr< WModule > SPtr
Shared pointer to a WModule.
std::vector< std::shared_ptr< WModuleInputConnector > > InputConnectorList
The type for the list of input connectors.
Thrown whenever an unknown prototype is specified.
This class provides a common interface for thread-safe access to associative containers (set,...
std::pair< Iterator, bool > insert(const value_type &x)
Inserts the specified element.
std::shared_ptr< WSharedObjectTicketRead< T > > ReadTicket
Type for read tickets.
ReadTicket getReadTicket() const
Returns a ticket to get read access to the contained data.
std::shared_ptr< WSharedObjectTicketWrite< T > > WriteTicket
Type for write tickets.
WriteTicket getWriteTicket(bool suppressNotify=false) const
Returns a ticket to get write access to the contained data.
WStreamedLogger debug(const std::string &source)
Logging a debug message.