Newer
Older
Russell Taylor
committed
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include <boost/tokenizer.hpp>
#include <string>
#include <iostream>
#include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/AnalysisDataService.h"
Russell Taylor
committed
#include "MantidAPI/InstrumentDataService.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/LibraryManager.h"
Russell Taylor
committed
Roman Tolchenov
committed
#ifdef _WIN32
#include <winsock2.h>
#endif
Russell Taylor
committed
using namespace std;
Russell Taylor
committed
namespace Mantid
{
Russell Taylor
committed
{
/// Default constructor
FrameworkManagerImpl::FrameworkManagerImpl() : g_log(Kernel::Logger::get("FrameworkManager"))
Russell Taylor
committed
{
Roman Tolchenov
committed
#ifdef _WIN32
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
#endif
Steve Williams
committed
#ifdef _MSC_VER
// This causes the exponent to consist of two digits (Windows Visual Studio normally 3, Linux default 2), where two digits are not sufficient I presume it uses more
_set_output_format(_TWO_DIGIT_EXPONENT);
#endif
std::string pluginDir = Kernel::ConfigService::Instance().getString("plugins.directory");
if (pluginDir.length() > 0)
{
Mantid::Kernel::LibraryManager::Instance().OpenAllLibraries(pluginDir, false);
}
g_log.debug() << "FrameworkManager created." << std::endl;
Russell Taylor
committed
}
Matt Clarke
committed
FrameworkManagerImpl::~FrameworkManagerImpl()
Russell Taylor
committed
{
// g_log.debug() << "FrameworkManager destroyed." << std::endl;
Russell Taylor
committed
}
Russell Taylor
committed
/** Clears all memory associated with the AlgorithmManager
* and with the Analysis & Instrument data services.
Matt Clarke
committed
void FrameworkManagerImpl::clear()
Gigg, Martyn Anthony
committed
{
clearAlgorithms();
clearData();
clearInstruments();
}
/**
* Clear memory associated with the AlgorithmManager
*/
void FrameworkManagerImpl::clearAlgorithms()
Dickon Champion
committed
{
Matt Clarke
committed
AlgorithmManager::Instance().clear();
Gigg, Martyn Anthony
committed
}
/**
* Clear memory associated with the ADS
*/
void FrameworkManagerImpl::clearData()
{
AnalysisDataService::Instance().clear();
Gigg, Martyn Anthony
committed
}
/**
* Clear memory associated with the IDS
*/
void FrameworkManagerImpl::clearInstruments()
{
Russell Taylor
committed
InstrumentDataService::Instance().clear();
Dickon Champion
committed
}
Russell Taylor
committed
/** Creates and initialises an instance of an algorithm
*
* @param algName The name of the algorithm required
* @return A pointer to the created algorithm
*
* @throw NotFoundError Thrown if algorithm requested is not registered
*/
IAlgorithm* FrameworkManagerImpl::createAlgorithm(const std::string& algName, const int& version)
IAlgorithm* alg = AlgorithmManager::Instance().create(algName,version).get();
Russell Taylor
committed
}
/** Creates an instance of an algorithm and sets the properties provided
*
* @param algName The name of the algorithm required
* @param propertiesArray A single string containing properties in the
* @return A pointer to the created algorithm
*
* @throw NotFoundError Thrown if algorithm requested is not registered
* @throw std::invalid_argument Thrown if properties string is ill-formed
*/
IAlgorithm* FrameworkManagerImpl::createAlgorithm(const std::string& algName,const std::string& propertiesArray, const int& version)
Russell Taylor
committed
{
Russell Taylor
committed
// Use the previous method to create the algorithm
IAlgorithm *alg = AlgorithmManager::Instance().create(algName,version).get();//createAlgorithm(algName);
Freddie Akeroyd
committed
alg->setProperties(propertiesArray);
Russell Taylor
committed
return alg;
Russell Taylor
committed
}
/** Creates an instance of an algorithm, sets the properties provided and
* then executes it.
*
* @param algName The name of the algorithm required
* @param propertiesArray A single string containing properties in the
* form "Property1=Value1;Property2=Value2;..."
* @param version The version of the algorithm
* @return A pointer to the executed algorithm
*
* @throw NotFoundError Thrown if algorithm requested is not registered
* @throw std::invalid_argument Thrown if properties string is ill-formed
* @throw runtime_error Thrown if algorithm cannot be executed
*/
IAlgorithm* FrameworkManagerImpl::exec(const std::string& algName, const std::string& propertiesArray, const int& version)
Russell Taylor
committed
{
Russell Taylor
committed
// Make use of the previous method for algorithm creation and property setting
IAlgorithm *alg = createAlgorithm(algName, propertiesArray,version);
Dickon Champion
committed
Russell Taylor
committed
// Now execute the algorithm
Russell Taylor
committed
alg->execute();
Russell Taylor
committed
return alg;
Russell Taylor
committed
}
/** Returns a shared pointer to the workspace requested
*
* @param wsName The name of the workspace
* @return A pointer to the workspace
*
* @throw NotFoundError If workspace is not registered with analysis data service
*/
Matt Clarke
committed
Workspace* FrameworkManagerImpl::getWorkspace(const std::string& wsName)
Russell Taylor
committed
{
Workspace *space;
Dickon Champion
committed
try
{
space = AnalysisDataService::Instance().retrieve(wsName).get();
Dickon Champion
committed
}
Russell Taylor
committed
catch (Kernel::Exception::NotFoundError&)
Russell Taylor
committed
{
Russell Taylor
committed
throw Kernel::Exception::NotFoundError("Unable to retrieve workspace",wsName);
Russell Taylor
committed
}
return space;
}
/** Removes and deletes a workspace from the data service store.
*
* @param wsName The user-given name for the workspace
* @return true if the workspace was found and deleted
*
* @throw NotFoundError Thrown if workspace cannot be found
*/
bool FrameworkManagerImpl::deleteWorkspace(const std::string& wsName)
{
bool retVal = false;
boost::shared_ptr<Workspace> ws_sptr;
try
{
ws_sptr=AnalysisDataService::Instance().retrieve(wsName);
}
catch(Kernel::Exception::NotFoundError&ex)
g_log.error()<<ex.what()<<std::endl;
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
}
boost::shared_ptr<WorkspaceGroup> ws_grpsptr=boost::dynamic_pointer_cast<WorkspaceGroup>(ws_sptr);
if(ws_grpsptr)
{
//selected workspace is a group workspace
ws_grpsptr->deepRemoveAll();
retVal = true;
}
else
{
//delete from the ADS
try
{
AnalysisDataService::Instance().remove(wsName);
retVal = true;
}
catch (Kernel::Exception::NotFoundError&)
{
//workspace was not found
g_log.error()<<"Workspace "<<wsName<<" could not be found."<<std::endl;
retVal = false;
}
}
return retVal;
Russell Taylor
committed
} // Namespace Mantid