OpenWalnut  1.5.0dev
WModuleFactory.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <algorithm>
26 #include <iostream>
27 #include <memory>
28 #include <set>
29 #include <string>
30 #include <vector>
31 
32 #include <typeinfo>
33 
34 #include "../common/WLogger.h"
35 #include "WModule.h"
36 #include "WModuleCombiner.h"
37 #include "WModuleFactory.h"
38 #include "combiner/WApplyCombiner.h"
39 #include "exceptions/WPrototypeNotUnique.h"
40 #include "exceptions/WPrototypeUnknown.h"
41 
42 // factory instance as singleton
43 std::shared_ptr< WModuleFactory > WModuleFactory::m_instance = std::shared_ptr< WModuleFactory >();
44 
46  m_prototypes(),
47  m_moduleLoader( new WModuleLoader() )
48 {
49  // initialize members
50 }
51 
53 {
54  // cleanup
55 }
56 
57 std::shared_ptr< WModuleLoader > WModuleFactory::getModuleLoader()
58 {
59  return WModuleFactory::getModuleFactory()->m_moduleLoader;
60 }
61 
63 {
64  // load modules
65  WLogger::getLogger()->addLogMessage( "Loading Modules", "ModuleFactory", LL_INFO );
66 
67  // operation must be exclusive
69 
70  // Load the dynamic modules here:
71  m_moduleLoader->load( l );
72 
73  // initialize every module in the set
74  std::set< std::string > names; // helper to find duplicates
75  PrototypeContainerIteratorType listIter = l->get().begin();
76  while( listIter != l->get().end() )
77  {
78  WLogger::getLogger()->addLogMessage( "Initializing module prototype: \"" + ( *listIter )->getName() + "\"", "ModuleFactory", LL_DEBUG );
79 
80  // that should not happen. Names should not occur multiple times since they are unique
81  if( names.count( ( *listIter )->getName() ) )
82  {
83  WLogger::getLogger()->addLogMessage( std::string( "Module \"" + ( *listIter )->getName() +
84  "\" is not unique. Modules have to have a unique name. Ignoring this module." ),
85  "ModuleFactory", LL_ERROR );
86  // we remove the module from the prototype list
87  l->get().erase( listIter++ );
88  continue;
89  }
90  else
91  {
92  names.insert( ( *listIter )->getName() );
93  initializeModule( ( *listIter ) );
94  ++listIter;
95  }
96  }
97  WLogger::getLogger()->addLogMessage( "Loading Modules Done", "ModuleFactory", LL_INFO );
98 }
99 
100 bool WModuleFactory::isPrototype( std::shared_ptr< WModule > module )
101 {
102  // for this a read lock is sufficient, gets unlocked if it looses scope
103  PrototypeSharedContainerType::ReadTicket l = getModuleFactory()->m_prototypes.getReadTicket();
104  return getModuleFactory()->checkPrototype( module, l );
105 }
106 
107 bool WModuleFactory::checkPrototype( std::shared_ptr< WModule > module, PrototypeSharedContainerType::ReadTicket ticket )
108 {
109  return ( ticket->get().count( module ) != 0 );
110 }
111 
112 std::shared_ptr< WModule > WModuleFactory::create( std::string prototype, std::string uuid )
113 {
114  return create( getPrototypeByName( prototype ), uuid );
115 }
116 
117 std::shared_ptr< WModule > WModuleFactory::create( std::shared_ptr< WModule > prototype, std::string uuid )
118 {
119  wlog::debug( "ModuleFactory" ) << "Creating new instance of prototype \"" << prototype->getName() << "\".";
120 
121  // for this a read lock is sufficient, gets unlocked if it looses scope
123 
124  // ensure this one is a prototype and nothing else
125  if( !checkPrototype( prototype, l ) )
126  {
127  throw WPrototypeUnknown( std::string( "Could not clone module \"" + prototype->getName() + "\" since it is no prototype." ) );
128  }
129 
130  // explicitly unlock
131  l.reset();
132 
133  // call prototypes factory function
134  std::shared_ptr< WModule > clone = std::shared_ptr< WModule >( prototype->factory() );
135  // set uuid and keep track of it
136  clone->setUUID( uuid );
137  m_uuidModuleMap.insert( UuidModuleMap::value_type( clone->getUUID(), clone ) );
138 
139  // init module
140  clone->setLocalPath( prototype->getLocalPath() ); // prototype and clone have the same local path.
141  initializeModule( clone );
142 
143  return clone;
144 }
145 
146 void WModuleFactory::initializeModule( std::shared_ptr< WModule > module )
147 {
148  module->initialize();
149 }
150 
151 std::shared_ptr< WModuleFactory > WModuleFactory::getModuleFactory()
152 {
153  if( !m_instance )
154  {
155  m_instance = std::shared_ptr< WModuleFactory >( new WModuleFactory() );
156  }
157 
158  return m_instance;
159 }
160 
161 
162 const std::shared_ptr< WModule > WModuleFactory::isPrototypeAvailable( std::string name )
163 {
164  // for this a read lock is sufficient, gets unlocked if it looses scope
166 
167  // find first and only prototype (ensured during load())
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();
170  ++listIter )
171  {
172  if( ( *listIter )->getName() == name )
173  {
174  ret = ( *listIter );
175  break;
176  }
177  }
178 
179  return ret;
180 }
181 
182 const std::shared_ptr< WModule > WModuleFactory::getPrototypeByName( std::string name )
183 {
184  std::shared_ptr< WModule > ret = isPrototypeAvailable( name );
185 
186  // if not found -> throw
187  if( ret == std::shared_ptr< WModule >() )
188  {
189  throw WPrototypeUnknown( std::string( "Could not find prototype \"" + name + "\"." ) );
190  }
191 
192  return ret;
193 }
194 
195 const std::shared_ptr< WModule > WModuleFactory::getPrototypeByInstance( std::shared_ptr< WModule > instance )
196 {
197  return getPrototypeByName( instance->getName() );
198 }
199 
200 std::vector< WModule::ConstSPtr > WModuleFactory::getPrototypesByType( MODULE_TYPE type )
201 {
202  std::vector< WModule::ConstSPtr > ret;
203 
204  // for this a read lock is sufficient, gets unlocked if it looses scope
206 
207  // find first and only prototype (ensured during load())
208  for( std::set< std::shared_ptr< WModule > >::const_iterator listIter = l->get().begin(); listIter != l->get().end();
209  ++listIter )
210  {
211  if( ( *listIter )->getType() == type )
212  {
213  ret.push_back( *listIter );
214  }
215  }
216 
217  return ret;
218 }
219 
221 {
222  return m_prototypes.getReadTicket();
223 }
224 
225 WCombinerTypes::WCompatiblesList WModuleFactory::getCompatiblePrototypes( std::shared_ptr< WModule > module )
226 {
227  WCombinerTypes::WCompatiblesList compatibles;
228 
229  // for this a read lock is sufficient, gets unlocked if it looses scope
231 
232  // has the module an output? If not, return.
233  bool addModulesWithoutInput = !module;
234 
235  if( addModulesWithoutInput )
236  {
237  // First, add all modules with no input connector.
238  for( PrototypeContainerConstIteratorType listIter = l->get().begin(); listIter != l->get().end();
239  ++listIter )
240  {
241  // ignore data modules
242  if( dynamic_cast< const WDataModule* >( ( *listIter ).get() ) )
243  {
244  continue;
245  }
246 
247  // get connectors of this prototype
248  WModule::InputConnectorList pcons = ( *listIter )->getInputConnectors();
249  if( pcons.size() == 0 )
250  {
251  // the modules which match every time need their own groups
252  WCombinerTypes::WOneToOneCombiners lComp;
253 
254  // NOTE: it is OK here to use the variable module even if it is NULL as the combiner in this case only adds the specified module
255  lComp.push_back( std::shared_ptr< WApplyCombiner >( new WApplyCombiner( module, "", *listIter, "" ) ) );
256 
257  // add this list
258  compatibles.push_back( WCombinerTypes::WCompatiblesGroup( ( *listIter ), lComp ) );
259  }
260  }
261  }
262 
263  // if NULL was specified, only return all modules without any inputs
264  if( module )
265  {
266  // go through every prototype
267  for( PrototypeContainerConstIteratorType listIter = l->get().begin(); listIter != l->get().end();
268  ++listIter )
269  {
270  WCombinerTypes::WOneToOneCombiners lComp = WApplyCombiner::createCombinerList< WApplyCombiner >( module, ( *listIter ) );
271 
272  // add the group
273  if( lComp.size() != 0 )
274  {
275  compatibles.push_back( WCombinerTypes::WCompatiblesGroup( ( *listIter ), lComp ) );
276  }
277  }
278  }
279 
280  // unlock. No locking needed for further steps.
281  l.reset();
282 
283  // sort the compatibles
284  std::sort( compatibles.begin(), compatibles.end(), WCombinerTypes::compatiblesSort );
285 
286  return compatibles;
287 }
288 
289 WCombinerTypes::WCompatiblesList WModuleFactory::getAllPrototypes()
290 {
291  WCombinerTypes::WCompatiblesList compatibles;
292 
293  // for this a read lock is sufficient, gets unlocked if it looses scope
295 
296  // Add all modules.
297  for( PrototypeContainerConstIteratorType listIter = l->get().begin(); listIter != l->get().end();
298  ++listIter )
299  {
300  // the modules which match every time need their own groups
301  WCombinerTypes::WOneToOneCombiners lComp;
302 
303  // NOTE: it is OK here to use the variable module even if it is NULL as the combiner in this case only adds the specified module
304  lComp.push_back( std::shared_ptr< WApplyCombiner >( new WApplyCombiner( *listIter ) ) );
305 
306  // add this list
307  compatibles.push_back( WCombinerTypes::WCompatiblesGroup( ( *listIter ), lComp ) );
308  }
309 
310  // unlock. No locking needed for further steps.
311  l.reset();
312 
313  // sort the compatibles
314  std::sort( compatibles.begin(), compatibles.end(), WCombinerTypes::compatiblesSort );
315 
316  return compatibles;
317 }
318 
320 {
321  SPtr f = getModuleFactory();
322  // unlocked upon destruction
324 
325  // find
326  UuidModuleMap::const_iterator it = r->get().find( uuid );
327  if( it != r->get().end() )
328  {
329  // found. Return locked weak_ptr.
330  std::weak_ptr< WModule > m = ( *it ).second;
331  return m.lock();
332  }
333  else
334  {
335  return WModule::SPtr();
336  }
337 }
338 
340 {
341  // get all data module prototypes
342  std::vector< WDataModule::SPtr > dataModules = WModuleFactory::getModuleFactory()->getPrototypesByType< WDataModule >();
343  std::vector< WDataModule::SPtr > result;
344 
345  // go through and
346  for( std::vector< WDataModule::SPtr >::const_iterator iter = dataModules.begin(); iter != dataModules.end(); ++iter )
347  {
348  // get the filters
349  std::vector< WDataModuleInputFilter::ConstSPtr > filters = ( *iter )->getInputFilter();
350  for( std::vector< WDataModuleInputFilter::ConstSPtr >::const_iterator filterIter = filters.begin(); filterIter != filters.end();
351  ++filterIter )
352  {
353  // if a filter matches, this data module is a candidate
354  if( ( *filterIter )->matches( input ) )
355  {
356  result.push_back( *iter );
357  break;
358  }
359  }
360  }
361  return result;
362 }
Base class for all combiners which apply one connection between two connectors of two modules.
std::shared_ptr< const WDataModuleInput > ConstSPtr
Convenience typedef for a std::shared_ptr< const WDataModuleInput >.
Base for all data loader modules.
Definition: WDataModule.h:47
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.
Definition: WLogger.cpp:84
static WLogger * getLogger()
Returns pointer to the currently running logger instance.
Definition: WLogger.cpp:64
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...
Definition: WModuleLoader.h:43
std::shared_ptr< WModule > SPtr
Shared pointer to a WModule.
Definition: WModule.h:106
std::vector< std::shared_ptr< WModuleInputConnector > > InputConnectorList
The type for the list of input connectors.
Definition: WModule.h:96
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.
Definition: WSharedObject.h:65
ReadTicket getReadTicket() const
Returns a ticket to get read access to the contained data.
std::shared_ptr< WSharedObjectTicketWrite< T > > WriteTicket
Type for write tickets.
Definition: WSharedObject.h:70
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.
Definition: WLogger.h:331