Commit a3666615 authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum
Browse files

[CHANGE] - updated module communication and infrastructure

parent b7b2264f
...@@ -64,6 +64,7 @@ int main( int argc, char* argv[] ) ...@@ -64,6 +64,7 @@ int main( int argc, char* argv[] )
// init the kernel // init the kernel
WKernel kernel = WKernel( argc, argv, gui ); WKernel kernel = WKernel( argc, argv, gui );
kernel.getRootContainer()->getModuleReadySignal()->connect( boost::bind( &WGUI::slotAddDatasetToBrowser, gui, _1 ) );
return kernel.run(); return kernel.run();
} }
...@@ -112,6 +112,8 @@ public: ...@@ -112,6 +112,8 @@ public:
{ {
return findProp( prop )->getValue< T >(); return findProp( prop )->getValue< T >();
} }
// TODO(schurade): do not use NULL as return value in this case. Throw exception instead.
return 0; return 0;
} }
...@@ -121,6 +123,8 @@ public: ...@@ -121,6 +123,8 @@ public:
{ {
return findProp( prop )->getMin< T >(); return findProp( prop )->getMin< T >();
} }
// TODO(schurade): do not use NULL as return value in this case. Throw exception instead.
return 0; return 0;
} }
...@@ -130,6 +134,8 @@ public: ...@@ -130,6 +134,8 @@ public:
{ {
return findProp( prop )->getMax< T >(); return findProp( prop )->getMax< T >();
} }
// TODO(schurade): do not use NULL as return value in this case. Throw exception instead.
return 0; return 0;
} }
......
...@@ -49,12 +49,21 @@ void WThreadedRunner::wait( bool requestFinish ) ...@@ -49,12 +49,21 @@ void WThreadedRunner::wait( bool requestFinish )
{ {
if( requestFinish ) if( requestFinish )
{ {
m_FinishRequested = requestFinish; // first notify
notifyStop(); notifyStop();
// then signal it
m_FinishRequested = requestFinish;
m_stopCondition.notify_all();
} }
m_Thread->join(); m_Thread->join();
} }
void WThreadedRunner::waitForStop()
{
m_stopCondition.wait( m_stopConditionMutex );
}
void WThreadedRunner::threadMain() void WThreadedRunner::threadMain()
{ {
} }
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#ifndef WTHREADEDRUNNER_H #ifndef WTHREADEDRUNNER_H
#define WTHREADEDRUNNER_H #define WTHREADEDRUNNER_H
#include <boost/thread.hpp>
#include <boost/thread/thread.hpp> #include <boost/thread/thread.hpp>
/** /**
...@@ -91,6 +92,21 @@ protected: ...@@ -91,6 +92,21 @@ protected:
*/ */
void sleep( const int t ) const; void sleep( const int t ) const;
/**
* Let the thread sleep until a stop request was given.
*/
void waitForStop();
/**
* Condition mutex used by m_stopCondition.
*/
boost::mutex m_stopConditionMutex;
/**
* Condition getting fired whenever the thread should quit. This is useful for waiting for stop requests.
*/
boost::condition_variable_any m_stopCondition;
private: private:
}; };
......
...@@ -29,7 +29,6 @@ ...@@ -29,7 +29,6 @@
#include "WDataHandler.h" #include "WDataHandler.h"
#include "WSubject.h" #include "WSubject.h"
#include "exceptions/WDHNoSuchDataSet.h" #include "exceptions/WDHNoSuchDataSet.h"
#include "WLoaderManager.h"
WDataHandler::WDataHandler() WDataHandler::WDataHandler()
{ {
...@@ -58,29 +57,3 @@ unsigned int WDataHandler::getNumberOfSubjects() const ...@@ -58,29 +57,3 @@ unsigned int WDataHandler::getNumberOfSubjects() const
return m_subjects.size(); return m_subjects.size();
} }
void WDataHandler::loadDataSets( std::vector< std::string > fileNames )
{
WLoaderManager lm;
for ( size_t i = 0 ; i < fileNames.size() ; ++i)
{
try
{
lm.load( fileNames[i], shared_from_this() );
}
catch( WDHException e )
{
std::cerr << "Error :: DataHandler :: " << e.what() << std::endl;
}
}
}
void WDataHandler::signalLoadFinished( boost::shared_ptr< WDataSet > data )
{
m_signalAddDataset( data );
}
boost::signal1< void, boost::shared_ptr< WDataSet > >* WDataHandler::getSignalAddDataset()
{
return &m_signalAddDataset;
}
...@@ -77,21 +77,6 @@ public: ...@@ -77,21 +77,6 @@ public:
*/ */
unsigned int getNumberOfSubjects() const; unsigned int getNumberOfSubjects() const;
/**
* Calls WLoaderManager to load the given input file.
*/
void loadDataSets( std::vector< std::string > fileNames );
/**
* is called from a loader object and signals a pointer to the loaded object
*/
void signalLoadFinished( boost::shared_ptr< WDataSet > data );
/**
*
*/
boost::signal1< void, boost::shared_ptr< WDataSet > >* getSignalAddDataset();
protected: protected:
private: private:
...@@ -99,11 +84,6 @@ private: ...@@ -99,11 +84,6 @@ private:
* A container for all WSubjects. * A container for all WSubjects.
*/ */
std::vector< boost::shared_ptr< WSubject > > m_subjects; std::vector< boost::shared_ptr< WSubject > > m_subjects;
/**
* boost signal object for signaling the addition of a dataset to the datahandler
*/
boost::signal1< void, boost::shared_ptr< WDataSet > > m_signalAddDataset;
}; };
/** /**
......
...@@ -56,6 +56,4 @@ void WLoader::commitDataSet( boost::shared_ptr< WDataSet > data ) ...@@ -56,6 +56,4 @@ void WLoader::commitDataSet( boost::shared_ptr< WDataSet > data )
subject = m_dataHandler->getSubject( 0 ); subject = m_dataHandler->getSubject( 0 );
} }
subject->addDataSet( data ); subject->addDataSet( data );
m_dataHandler->signalLoadFinished( data );
} }
...@@ -53,11 +53,11 @@ public: ...@@ -53,11 +53,11 @@ public:
}; };
/** /**
* This function is automatically called when creating a new thread for the * Loads the dataset.
* loader with boost::thread. The method is pure virtual here to ensure *
* that subclasses implement it. * \return the dataset loaded.
*/ */
virtual void operator()() = 0; virtual boost::shared_ptr< WDataSet > load() = 0;
protected: protected:
/** /**
......
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <boost/thread.hpp>
#include <boost/filesystem.hpp>
#include "WLoaderManager.h"
#include "exceptions/WDHException.h"
#include "io/WLoaderNIfTI.h"
#include "io/WLoaderBiosig.h"
#include "io/WLoaderEEGASCII.h"
#include "io/WLoaderFibers.h"
std::string getSuffix( std::string name )
{
boost::filesystem::path p( name );
return p.extension();
}
void WLoaderManager::load( std::string fileName, boost::shared_ptr< WDataHandler > dataHandler ) throw( WDHException )
{
std::string suffix = getSuffix( fileName );
if( suffix == ".nii" || suffix == ".gz" )
{
if( suffix == ".gz" ) // it may be a NIfTI file too
{
boost::filesystem::path p( fileName );
p.replace_extension( "" );
suffix = getSuffix( p.string() );
assert( suffix == ".nii" && "currently only nii files may be gzipped" );
}
WLoaderNIfTI niiLoader( fileName, dataHandler );
boost::thread loaderThread( niiLoader );
}
else if( suffix == ".edf" )
{
WLoaderBiosig biosigLoader( fileName, dataHandler );
boost::thread loaderThread( biosigLoader );
}
else if( suffix == ".asc" )
{
WLoaderEEGASCII eegAsciiLoader( fileName, dataHandler );
boost::thread loaderThread( eegAsciiLoader );
}
else if( suffix == ".vtk" )
{
// This is a dummy implementation.
// You need to provide a real implementation here if you want to load vtk.
std::cout << "VTK not implemented yet" << std::endl;
assert( 0 );
}
else if( suffix == ".fib" )
{
WLoaderFibers fibLoader( fileName, dataHandler );
boost::thread loaderThread( fibLoader );
}
else
{
throw WDHException( "Unknown file type: '" + suffix + "'" );
}
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WLOADERMANAGER_H
#define WLOADERMANAGER_H
#include <string>
#include <boost/shared_ptr.hpp>
#include "exceptions/WDHException.h"
class WDataHandler;
/**
* Decouples file loading from the rest of OpenWalnut into a single thread.
* \ingroup dataHandler
*/
class WLoaderManager
{
public:
/**
* Selects correct loader for fileName and creates loader thread.
*/
void load( std::string fileName, boost::shared_ptr< WDataHandler > dataHandler ) throw( WDHException );
protected:
private:
};
#endif // WLOADERMANAGER_H
...@@ -56,7 +56,7 @@ void WLoaderBiosig::fillSegment( std::vector<std::vector<double> >* segment, bio ...@@ -56,7 +56,7 @@ void WLoaderBiosig::fillSegment( std::vector<std::vector<double> >* segment, bio
} }
} }
void WLoaderBiosig::biosigLoader() boost::shared_ptr< WDataSet> WLoaderBiosig::load()
{ {
std::cout << "BIOSIGLOADER" << std::endl; std::cout << "BIOSIGLOADER" << std::endl;
hd = sopen( m_fileName.c_str(), "r", 0 ); hd = sopen( m_fileName.c_str(), "r", 0 );
...@@ -131,9 +131,6 @@ void WLoaderBiosig::biosigLoader() ...@@ -131,9 +131,6 @@ void WLoaderBiosig::biosigLoader()
std::cout << "BIOSIG loading done." << std::endl; std::cout << "BIOSIG loading done." << std::endl;
std::cout << "===================================" << std::endl; std::cout << "===================================" << std::endl;
return eeg;
} }
void WLoaderBiosig::operator()()
{
biosigLoader();
}
...@@ -49,13 +49,14 @@ public: ...@@ -49,13 +49,14 @@ public:
WLoaderBiosig( std::string fileName, boost::shared_ptr< WDataHandler > dataHandler ); WLoaderBiosig( std::string fileName, boost::shared_ptr< WDataHandler > dataHandler );
/** /**
* This function is automatically called when creating a new thread for the * Loads the dataset.
* loader with boost::thread. *
* \return the dataset loaded.
*/ */
virtual void operator()(); virtual boost::shared_ptr< WDataSet > load();
protected: protected:
private: private:
void biosigLoader();
void fillSegment( std::vector<std::vector<double> >* segment, biosig_data_type* data ); void fillSegment( std::vector<std::vector<double> >* segment, biosig_data_type* data );
/** /**
......
...@@ -39,7 +39,7 @@ WLoaderEEGASCII::WLoaderEEGASCII( std::string fileName, boost::shared_ptr< WData ...@@ -39,7 +39,7 @@ WLoaderEEGASCII::WLoaderEEGASCII( std::string fileName, boost::shared_ptr< WData
{ {
} }
void WLoaderEEGASCII::operator()() boost::shared_ptr< WDataSet > WLoaderEEGASCII::load()
{ {
std::ifstream in( m_fileName.c_str() ); std::ifstream in( m_fileName.c_str() );
if( in.fail() ) if( in.fail() )
...@@ -84,4 +84,6 @@ void WLoaderEEGASCII::operator()() ...@@ -84,4 +84,6 @@ void WLoaderEEGASCII::operator()()
boost::shared_ptr< WEEG > eeg = boost::shared_ptr< WEEG >( new WEEG( segments, lib, labels ) ); boost::shared_ptr< WEEG > eeg = boost::shared_ptr< WEEG >( new WEEG( segments, lib, labels ) );
eeg->setFileName( m_fileName ); eeg->setFileName( m_fileName );
commitDataSet( eeg ); commitDataSet( eeg );
return eeg;
} }
...@@ -45,10 +45,13 @@ public: ...@@ -45,10 +45,13 @@ public:
WLoaderEEGASCII( std::string fileName, boost::shared_ptr< WDataHandler > dataHandler ); WLoaderEEGASCII( std::string fileName, boost::shared_ptr< WDataHandler > dataHandler );
/** /**
* This function is automatically called when creating a new thread for the * Loads the dataset.
* loader with boost::thread. *
* \return the dataset loaded.
*/ */
virtual void operator()(); virtual boost::shared_ptr< WDataSet > load();
void operator()();
protected: protected:
private: private:
......
...@@ -54,7 +54,7 @@ WLoaderFibers::~WLoaderFibers() throw() ...@@ -54,7 +54,7 @@ WLoaderFibers::~WLoaderFibers() throw()
{ {
} }
void WLoaderFibers::operator()() throw() boost::shared_ptr< WDataSet > WLoaderFibers::load()
{ {
using boost::shared_ptr; using boost::shared_ptr;
using std::vector; using std::vector;
...@@ -81,6 +81,7 @@ void WLoaderFibers::operator()() throw() ...@@ -81,6 +81,7 @@ void WLoaderFibers::operator()() throw()
commitDataSet( fibers ); commitDataSet( fibers );
assert( !m_ifs->is_open() ); assert( !m_ifs->is_open() );
return fibers;
} }
void WLoaderFibers::readHeader() throw( WDHIOFailure, WDHException ) void WLoaderFibers::readHeader() throw( WDHIOFailure, WDHException )
......
...@@ -61,10 +61,11 @@ public: ...@@ -61,10 +61,11 @@ public:
virtual ~WLoaderFibers() throw(); virtual ~WLoaderFibers() throw();
/** /**
* This function is automatically called when creating a new thread for the * Loads the dataset.
* loader with boost::thread. It calls the methods of the NIfTI I/O library. *
* \return the dataset loaded.
*/ */
virtual void operator()() throw(); virtual boost::shared_ptr< WDataSet > load();
protected: protected:
/** /**
......
...@@ -73,8 +73,7 @@ wmath::WMatrix< double > WLoaderNIfTI::convertMatrix( const mat44& in ) ...@@ -73,8 +73,7 @@ wmath::WMatrix< double > WLoaderNIfTI::convertMatrix( const mat44& in )
return out; return out;
} }
boost::shared_ptr< WDataSet > WLoaderNIfTI::load()
void WLoaderNIfTI::operator()()
{ {
nifti_image* header = nifti_image_read( m_fileName.c_str(), 0 ); nifti_image* header = nifti_image_read( m_fileName.c_str(), 0 );
int columns = header->dim[1]; int columns = header->dim[1];
...@@ -142,6 +141,8 @@ void WLoaderNIfTI::operator()() ...@@ -142,6 +141,8 @@ void WLoaderNIfTI::operator()()
boost::shared_ptr< WDataSet > newDataSet = boost::shared_ptr< WDataSet >( new WDataSetSingle( newValueSet, newGrid ) ); boost::shared_ptr< WDataSet > newDataSet = boost::shared_ptr< WDataSet >( new WDataSetSingle( newValueSet, newGrid ) );
newDataSet->setFileName( m_fileName ); newDataSet->setFileName( m_fileName );
commitDataSet( newDataSet ); commitDataSet( newDataSet );
return newDataSet;
} }
......
...@@ -50,10 +50,11 @@ public: ...@@ -50,10 +50,11 @@ public:
WLoaderNIfTI( std::string fileName, boost::shared_ptr< WDataHandler > dataHandler ); WLoaderNIfTI( std::string fileName, boost::shared_ptr< WDataHandler > dataHandler );
/** /**
* This function is automatically called when creating a new thread for the * Loads the dataset.
* loader with boost::thread. It calls the methods of the NIfTI I/O library. *
* \return the dataset loaded.
*/ */
virtual void operator()(); virtual boost::shared_ptr< WDataSet > load();
protected: protected:
private: private:
......
...@@ -51,7 +51,7 @@ public: ...@@ -51,7 +51,7 @@ public:
TS_ASSERT_EQUALS( dataHandler->getNumberOfSubjects(), 0 ); TS_ASSERT_EQUALS( dataHandler->getNumberOfSubjects(), 0 );
WLoaderEEGASCII eegASCIILoader( fileName, dataHandler ); WLoaderEEGASCII eegASCIILoader( fileName, dataHandler );
boost::thread loaderThread( eegASCIILoader ); eegASCIILoader.load();
std::clock_t startTime; std::clock_t startTime;
startTime = std::clock(); startTime = std::clock();
......
...@@ -276,7 +276,7 @@ public: ...@@ -276,7 +276,7 @@ public:
{ {
TS_ASSERT_EQUALS( m_dataHandler->getNumberOfSubjects(), 0 ); TS_ASSERT_EQUALS( m_dataHandler->getNumberOfSubjects(), 0 );
WLoaderFibers loader( "fixtures/Fibers/valid_small_example.fib", m_dataHandler ); WLoaderFibers loader( "fixtures/Fibers/valid_small_example.fib", m_dataHandler );
loader(); loader.load();
TS_ASSERT_EQUALS( m_dataHandler->getNumberOfSubjects(), 1 ); TS_ASSERT_EQUALS( m_dataHandler->getNumberOfSubjects(), 1 );
}