Newer
Older
Peterson, Peter
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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
213
214
215
216
//----------------------------------------------------------------------
// 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"
#include "MantidAPI/Algorithm.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/LibraryManager.h"
#ifdef _WIN32
#include <winsock2.h>
#endif
using namespace std;
namespace Mantid
{
namespace API
{
/// Default constructor
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
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;
}
/// Destructor
FrameworkManagerImpl::~FrameworkManagerImpl()
{
// 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.
*/
void FrameworkManagerImpl::clear()
{
clearAlgorithms();
clearData();
clearInstruments();
}
/**
* Clear memory associated with the AlgorithmManager
*/
void FrameworkManagerImpl::clearAlgorithms()
{
AlgorithmManager::Instance().clear();
}
/**
* 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
* @param version The version of the algorithm
* @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();
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
* form "Property1=Value1;Property2=Value2;..."
* @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
*/
IAlgorithm* FrameworkManagerImpl::createAlgorithm(const std::string& algName,const std::string& propertiesArray, const int& version)
{
// Use the previous method to create the algorithm
IAlgorithm *alg = AlgorithmManager::Instance().create(algName,version).get();//createAlgorithm(algName);
alg->setProperties(propertiesArray);
return alg;
}
/** 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)
{
// Make use of the previous method for algorithm creation and property setting
IAlgorithm *alg = createAlgorithm(algName, propertiesArray,version);
// Now execute the algorithm
alg->execute();
return alg;
}
/** 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)
{
Workspace *space;
try
{
space = AnalysisDataService::Instance().retrieve(wsName).get();
}
catch (Kernel::Exception::NotFoundError&)
{
throw Kernel::Exception::NotFoundError("Unable to retrieve workspace",wsName);
}
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;
}
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;
}
} // namespace API
} // Namespace Mantid