-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re #5496. Added algorithm for recording algorithm calls
- Loading branch information
1 parent
7ed5a88
commit 1713814
Showing
6 changed files
with
271 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/RecordPythonScript.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#ifndef MANTID_ALGORITHMS_RECORDPYTHONSCRIPT_H_ | ||
#define MANTID_ALGORITHMS_RECORDPYTHONSCRIPT_H_ | ||
|
||
#include "MantidKernel/System.h" | ||
#include "MantidAlgorithms/GeneratePythonScript.h" | ||
#include "MantidAPI/AlgorithmObserver.h" | ||
|
||
namespace Mantid | ||
{ | ||
namespace Algorithms | ||
{ | ||
|
||
/** RecordPythonScript : TODO: DESCRIPTION | ||
An Algorithm to generate a Python script file to reproduce the history of a workspace. | ||
Properties: | ||
<ul> | ||
<li>Filename - the name of the file to write to. </li> | ||
<li>InputWorkspace - the workspace name who's history is to be saved.</li> | ||
</ul> | ||
Copyright © 2011 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory | ||
This file is part of Mantid. | ||
Mantid is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 3 of the License, or | ||
(at your option) any later version. | ||
Mantid 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 General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid> | ||
Code Documentation is available at: <http://doxygen.mantidproject.org> | ||
*/ | ||
class DLLExport RecordPythonScript : public Algorithms::GeneratePythonScript, public API::AlgorithmObserver | ||
{ | ||
public: | ||
RecordPythonScript(); | ||
virtual ~RecordPythonScript() {} | ||
|
||
/// Algorithm's name for identification | ||
virtual const std::string name() const { return "RecordPythonScript";}; | ||
/// Algorithm's version for identification | ||
virtual int version() const { return 1;}; | ||
/// Algorithm's category for identification | ||
virtual const std::string category() const { return "Utility;PythonAlgorithms";} | ||
|
||
private: | ||
/// Sets documentation strings for this algorithm | ||
virtual void initDocs(); | ||
/// Initialise the properties | ||
void init(); | ||
/// Run the algorithm | ||
void exec(); | ||
/** Handler of the start notifications. Must be overriden in inherited classes. | ||
@param alg :: Shared Pointer to the algorithm sending the notification. | ||
*/ | ||
void startingHandle(API::IAlgorithm_sptr alg); | ||
/// buffer for the script | ||
std::string m_generatedScript; | ||
}; | ||
|
||
|
||
} // namespace Algorithms | ||
} // namespace Mantid | ||
|
||
#endif /* MANTID_ALGORITHMS_RECORDPYTHONSCRIPT_H_ */ |
126 changes: 126 additions & 0 deletions
126
Code/Mantid/Framework/Algorithms/src/RecordPythonScript.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/*WIKI* | ||
*WIKI*/ | ||
#include "MantidAlgorithms/RecordPythonScript.h" | ||
#include "MantidKernel/System.h" | ||
#include "MantidAPI/FileProperty.h" | ||
#include "MantidAPI/AlgorithmManager.h" | ||
#include "MantidAPI/AlgorithmHistory.h" | ||
#include "MantidAPI/AlgorithmProxy.h" | ||
|
||
#include <fstream> | ||
|
||
using namespace Mantid::Kernel; | ||
using namespace Mantid::API; | ||
|
||
namespace Mantid | ||
{ | ||
namespace Algorithms | ||
{ | ||
|
||
// Register the algorithm into the AlgorithmFactory | ||
DECLARE_ALGORITHM(RecordPythonScript) | ||
|
||
|
||
//---------------------------------------------------------------------------------------------- | ||
/// Constructor | ||
RecordPythonScript::RecordPythonScript() : Algorithms::GeneratePythonScript(), API::AlgorithmObserver() | ||
{ | ||
} | ||
|
||
//---------------------------------------------------------------------------------------------- | ||
/// Sets documentation strings for this algorithm | ||
void RecordPythonScript::initDocs() | ||
{ | ||
this->setWikiSummary("An Algorithm to generate a Python script file to reproduce the history of a workspace."); | ||
this->setOptionalMessage("An Algorithm to generate a Python script file to reproduce the history of a workspace."); | ||
} | ||
|
||
//---------------------------------------------------------------------------------------------- | ||
/** Initialize the algorithm's properties. | ||
*/ | ||
void RecordPythonScript::init() | ||
{ | ||
std::vector<std::string> exts; | ||
exts.push_back(".py"); | ||
|
||
declareProperty(new API::FileProperty("Filename","", API::FileProperty::Save, exts), | ||
"The file into which the Python script will be generated."); | ||
} | ||
|
||
//---------------------------------------------------------------------------------------------- | ||
/** Execute the algorithm. | ||
*/ | ||
void RecordPythonScript::exec() | ||
{ | ||
observeStarting(); | ||
// Keep going until you get cancelled | ||
while (true) | ||
{ | ||
try | ||
{ | ||
// Exit if the user presses cancel | ||
interruption_point(); | ||
} | ||
catch(...) | ||
{ | ||
break; | ||
} | ||
progress( 0.0, "Recording..." ); | ||
|
||
// Sleep for 50 msec | ||
Poco::Thread::sleep(50); | ||
} | ||
|
||
// save the script to a file | ||
const std::string filename = getPropertyValue("Filename"); | ||
std::ofstream file(filename.c_str(), std::ofstream::trunc); | ||
|
||
if (NULL == file) | ||
{ | ||
throw Exception::FileError("Unable to create file: " , filename); | ||
} | ||
|
||
file << m_generatedScript; | ||
file.flush(); | ||
file.close(); | ||
} | ||
|
||
/** Handler of the start notifications. Adds an algorithm call to the script. | ||
* @param alg :: Shared pointer to the starting algorithm. | ||
*/ | ||
void RecordPythonScript::startingHandle(API::IAlgorithm_sptr alg) | ||
{ | ||
std::cerr << "Started " << alg->name() << std::endl; | ||
auto props= alg->getProperties(); | ||
for( auto p = props.begin(); p != props.end(); ++p) | ||
{ | ||
std::cerr << (**p).name() << '=' << (**p).value() << std::endl; | ||
} | ||
// Get the details of this algorithm history. | ||
const std::string name = alg->name(); | ||
const int version = alg->version(); | ||
|
||
std::string algString; | ||
for(auto p = props.begin() ; p != props.end(); ++p) | ||
{ | ||
std::string paramString = (**p).name() + "='" + (**p).value() + "'"; | ||
|
||
// Miss out parameters that are empty. | ||
if(paramString.length() != 0) | ||
{ | ||
if(algString.length() != 0) | ||
{ | ||
algString += ","; | ||
} | ||
algString += paramString; | ||
} | ||
} | ||
|
||
m_generatedScript += name + "(" + algString + ")\n"; | ||
} | ||
|
||
|
||
} // namespace Algorithms | ||
} // namespace Mantid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters