Skip to content
Snippets Groups Projects
FrameworkManager.cpp 6.36 KiB
Newer Older
//----------------------------------------------------------------------
// 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"
#include "MantidAPI/InstrumentDataService.h"
#include "MantidKernel/ConfigService.h"
#include "MantidAPI/IAlgorithm.h"
Nick Draper's avatar
Nick Draper committed
#include "MantidAPI/Algorithm.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/LibraryManager.h"
Nick Draper's avatar
Nick Draper committed
namespace API
FrameworkManagerImpl::FrameworkManagerImpl() : g_log(Kernel::Logger::get("FrameworkManager"))
#ifdef _WIN32
    WSADATA wsaData;
    WSAStartup(MAKEWORD(2,2), &wsaData);
#endif

#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

Roman Tolchenov's avatar
Roman Tolchenov committed
  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;
FrameworkManagerImpl::~FrameworkManagerImpl()
Nick Draper's avatar
Nick Draper committed
//	std::cerr << "FrameworkManager destroyed." << std::endl;
//	g_log.debug() << "FrameworkManager destroyed." << std::endl;
/** Clears all memory associated with the AlgorithmManager
 *  and with the Analysis & Instrument data services.
{
  clearAlgorithms();
  clearData();
  clearInstruments();
}

/**
 * Clear memory associated with the AlgorithmManager
 */
void FrameworkManagerImpl::clearAlgorithms()
}

/**
 * Clear memory associated with the ADS
 */
void FrameworkManagerImpl::clearData()
{
  AnalysisDataService::Instance().clear();
}

/**
 * Clear memory associated with the IDS
 */
void FrameworkManagerImpl::clearInstruments()
{
  InstrumentDataService::Instance().clear();
/** Creates and initialises an instance of an algorithm
 * 
 *  @param algName The name of the algorithm required
Dickon Champion's avatar
Dickon Champion committed
 *  @param version The version of the algorithm
 *  @return A pointer to the created algorithm
 * 
 *  @throw NotFoundError Thrown if algorithm requested is not registered
 */
Dickon Champion's avatar
Dickon Champion committed
IAlgorithm* FrameworkManagerImpl::createAlgorithm(const std::string& algName, const int& version)
Nick Draper's avatar
Nick Draper committed
{ 
Dickon Champion's avatar
Dickon Champion committed
   IAlgorithm* alg = AlgorithmManager::Instance().create(algName,version).get();
Matt Clarke's avatar
Matt Clarke committed
   return alg;
/** 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 
Dickon Champion's avatar
Dickon Champion committed
 *                         form "Property1=Value1;Property2=Value2;..."
Dickon Champion's avatar
Dickon Champion committed
 *  @param version The version of the algorithm
 *  @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
 */ 
Dickon Champion's avatar
Dickon Champion committed
IAlgorithm* FrameworkManagerImpl::createAlgorithm(const std::string& algName,const std::string& propertiesArray, const int& version)
Dickon Champion's avatar
Dickon Champion committed
  IAlgorithm *alg = AlgorithmManager::Instance().create(algName,version).get();//createAlgorithm(algName);
/** 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 
Dickon Champion's avatar
Dickon Champion committed
 *                         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
 */ 
Dickon Champion's avatar
Dickon Champion committed
IAlgorithm* FrameworkManagerImpl::exec(const std::string& algName, const std::string& propertiesArray, const int& version)
  // Make use of the previous method for algorithm creation and property setting
Dickon Champion's avatar
Dickon Champion committed
  IAlgorithm *alg = createAlgorithm(algName, propertiesArray,version);
/** 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
 */
Workspace* FrameworkManagerImpl::getWorkspace(const std::string& wsName)
    space = AnalysisDataService::Instance().retrieve(wsName).get();
  catch (Kernel::Exception::NotFoundError&)
    throw Kernel::Exception::NotFoundError("Unable to retrieve workspace",wsName);
Nick Draper's avatar
Nick Draper committed
/** 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;
	}
	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;
Nick Draper's avatar
Nick Draper committed
}

Nick Draper's avatar
Nick Draper committed
} // namespace API