Newer
Older
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#ifndef HISTORYVIEWTEST_H_
#define HISTORYVIEWTEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/DataProcessorAlgorithm.h"
#include "MantidAPI/ScriptBuilder.h"
#include "MantidTestHelpers/FakeObjects.h"
using namespace Mantid::API;
using namespace Mantid::Kernel;
class ScriptBuilderTest : public CxxTest::TestSuite
{
/// Use a fake algorithm object instead of a dependency on a real one.
class SubAlgorithm : public Algorithm
{
public:
SubAlgorithm() : Algorithm() {}
virtual ~SubAlgorithm() {}
const std::string name() const { return "SubAlgorithm";}
int version() const { return 1;}
const std::string category() const { return "Cat;Leopard;Mink";}
const std::string workspaceMethodName() const { return "methodname"; }
const std::string workspaceMethodOnTypes() const { return "MatrixWorkspace;ITableWorkspace"; }
const std::string workspaceMethodInputProperty() const { return "InputWorkspace"; }
void init()
{
declareProperty("PropertyA", "Hello");
declareProperty("PropertyB", "World");
}
void exec()
{
//nothing to do!
}
};
// basic algorithm. This acts as a child called for other DataProcessorAlgorithms
class BasicAlgorithm : public Algorithm
{
public:
BasicAlgorithm() : Algorithm() {}
virtual ~BasicAlgorithm() {}
const std::string name() const { return "BasicAlgorithm";}
int version() const { return 1;}
const std::string category() const { return "Cat;Leopard;Mink";}
const std::string workspaceMethodName() const { return "methodname"; }
const std::string workspaceMethodOnTypes() const { return "MatrixWorkspace;ITableWorkspace"; }
const std::string workspaceMethodInputProperty() const { return "InputWorkspace"; }
void init()
{
declareProperty("PropertyA", "Hello");
declareProperty("PropertyB", "World");
}
void exec()
{
// the history from this should never be stored
auto alg = createChildAlgorithm("SubAlgorithm");
alg->initialize();
alg->setProperty("PropertyA", "I Don't exist!");
alg->execute();
}
};
//middle layer algorithm executed by a top level algorithm
class NestedAlgorithm : public DataProcessorAlgorithm
{
public:
NestedAlgorithm() : DataProcessorAlgorithm() {}
virtual ~NestedAlgorithm() {}
const std::string name() const { return "NestedAlgorithm";}
int version() const { return 1;}
const std::string category() const { return "Cat;Leopard;Mink";}
const std::string workspaceMethodName() const { return "methodname"; }
const std::string workspaceMethodOnTypes() const { return "MatrixWorkspace;ITableWorkspace"; }
const std::string workspaceMethodInputProperty() const { return "InputWorkspace"; }
void init()
{
declareProperty("PropertyA", 13);
declareProperty("PropertyB", 42);
}
void exec()
{
auto alg = createChildAlgorithm("BasicAlgorithm");
alg->initialize();
alg->setProperty("PropertyA", "FirstOne");
alg->execute();
alg = createChildAlgorithm("BasicAlgorithm");
alg->initialize();
alg->setProperty("PropertyA", "SecondOne");
alg->execute();
}
};
//top level algorithm which executes -> NestedAlgorithm which executes -> BasicAlgorithm
class TopLevelAlgorithm : public DataProcessorAlgorithm
{
public:
TopLevelAlgorithm() : DataProcessorAlgorithm() {}
virtual ~TopLevelAlgorithm() {}
const std::string name() const { return "TopLevelAlgorithm";}
int version() const { return 1;}
const std::string category() const { return "Cat;Leopard;Mink";}
const std::string workspaceMethodName() const { return "methodname"; }
const std::string workspaceMethodOnTypes() const { return "Workspace;MatrixWorkspace;ITableWorkspace"; }
const std::string workspaceMethodInputProperty() const { return "InputWorkspace"; }
void init()
{
declareProperty(new WorkspaceProperty<MatrixWorkspace>("InputWorkspace", "", Direction::Input));
declareProperty(new WorkspaceProperty<MatrixWorkspace>("OutputWorkspace","", Direction::Output));
}
void exec()
{
auto alg = createChildAlgorithm("NestedAlgorithm");
alg->initialize();
alg->execute();
alg = createChildAlgorithm("NestedAlgorithm");
alg->initialize();
alg->execute();
boost::shared_ptr<MatrixWorkspace> output(new WorkspaceTester());
setProperty("OutputWorkspace", output);
}
};
private:
public:
void setUp()
{
Mantid::API::AlgorithmFactory::Instance().subscribe<TopLevelAlgorithm>();
Mantid::API::AlgorithmFactory::Instance().subscribe<NestedAlgorithm>();
Mantid::API::AlgorithmFactory::Instance().subscribe<BasicAlgorithm>();
Mantid::API::AlgorithmFactory::Instance().subscribe<SubAlgorithm>();
}
void tearDown()
{
Mantid::API::AlgorithmFactory::Instance().unsubscribe("TopLevelAlgorithm",1);
Mantid::API::AlgorithmFactory::Instance().unsubscribe("NestedAlgorithm",1);
Mantid::API::AlgorithmFactory::Instance().unsubscribe("BasicAlgorithm",1);
Mantid::API::AlgorithmFactory::Instance().unsubscribe("SubAlgorithm",1);
}
void test_Build_Simple()
{
std::string result[] = {
"",
"TopLevelAlgorithm(InputWorkspace='test_input_workspace', OutputWorkspace='test_output_workspace')",
""
};
boost::shared_ptr<WorkspaceTester> input(new WorkspaceTester());
AnalysisDataService::Instance().addOrReplace("test_input_workspace", input);
auto alg = AlgorithmFactory::Instance().create("TopLevelAlgorithm", 1);
alg->initialize();
alg->setRethrows(true);
alg->setProperty("InputWorkspace", input);
alg->setPropertyValue("OutputWorkspace", "test_output_workspace");
alg->execute();
auto ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("test_output_workspace");
auto wsHist = ws->getHistory();
ScriptBuilder builder(ws->getHistory());
std::string scriptText = builder.build();
std::vector<std::string> scriptLines;
boost::split(scriptLines, scriptText, boost::is_any_of("\n"));
int i=0;
for (auto it = scriptLines.begin(); it != scriptLines.end(); ++it, ++i)
{
TS_ASSERT_EQUALS(*it, result[i])
}
AnalysisDataService::Instance().remove("test_output_workspace");
AnalysisDataService::Instance().remove("test_input_workspace");
}
void test_Build_Unrolled()
{
std::string result[] = {
"# Child algorithms of TopLevelAlgorithm",
"## Child algorithms of NestedAlgorithm",
"",
"BasicAlgorithm(PropertyA='FirstOne')",
"BasicAlgorithm(PropertyA='SecondOne')",
"",
"## End of child algorithms of NestedAlgorithm",
"## Child algorithms of NestedAlgorithm",
"",
"BasicAlgorithm(PropertyA='FirstOne')",
"BasicAlgorithm(PropertyA='SecondOne')",
"",
"## End of child algorithms of NestedAlgorithm",
"",
"# End of child algorithms of TopLevelAlgorithm",
"",
};
boost::shared_ptr<WorkspaceTester> input(new WorkspaceTester());
AnalysisDataService::Instance().addOrReplace("test_input_workspace", input);
auto alg = AlgorithmFactory::Instance().create("TopLevelAlgorithm", 1);
alg->initialize();
alg->setRethrows(true);
alg->setProperty("InputWorkspace", input);
alg->setPropertyValue("OutputWorkspace", "test_output_workspace");
alg->execute();
auto ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("test_output_workspace");
auto wsHist = ws->getHistory();
HistoryView view(ws->getHistory());
view.unrollAll();
ScriptBuilder builder(view);
std::string scriptText = builder.build();
std::vector<std::string> scriptLines;
boost::split(scriptLines, scriptText, boost::is_any_of("\n"));
int i=0;
for (auto it = scriptLines.begin(); it != scriptLines.end(); ++it, ++i)
{
TS_ASSERT_EQUALS(*it, result[i])
}
AnalysisDataService::Instance().remove("test_output_workspace");
AnalysisDataService::Instance().remove("test_input_workspace");
}
void test_Partially_Unrolled()
{
std::string result[] = {
"# Child algorithms of TopLevelAlgorithm",
"## Child algorithms of NestedAlgorithm",
"",
"BasicAlgorithm(PropertyA='FirstOne')",
"BasicAlgorithm(PropertyA='SecondOne')",
"",
"## End of child algorithms of NestedAlgorithm",
"",
"NestedAlgorithm()",
"",
"# End of child algorithms of TopLevelAlgorithm",
"",
};
boost::shared_ptr<WorkspaceTester> input(new WorkspaceTester());
AnalysisDataService::Instance().addOrReplace("test_input_workspace", input);
auto alg = AlgorithmFactory::Instance().create("TopLevelAlgorithm", 1);
alg->initialize();
alg->setRethrows(true);
alg->setProperty("InputWorkspace", input);
alg->setPropertyValue("OutputWorkspace", "test_output_workspace");
alg->execute();
auto ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("test_output_workspace");
auto wsHist = ws->getHistory();
HistoryView view(ws->getHistory());
view.unroll(0);
view.unroll(1);
ScriptBuilder builder(view);
std::string scriptText = builder.build();
std::vector<std::string> scriptLines;
boost::split(scriptLines, scriptText, boost::is_any_of("\n"));
int i=0;
for (auto it = scriptLines.begin(); it != scriptLines.end(); ++it, ++i)
{
TS_ASSERT_EQUALS(*it, result[i])
}
AnalysisDataService::Instance().remove("test_output_workspace");
AnalysisDataService::Instance().remove("test_input_workspace");
}
};
#endif