From 8321341049f325f777c264250390fd400e44ada1 Mon Sep 17 00:00:00 2001
From: Nick Draper <nick.draper@stfc.ac.uk>
Date: Mon, 19 Oct 2015 15:12:32 +0100
Subject: [PATCH] Added a new algorithm AddHistoryNote

re #13867
---
 Framework/Algorithms/CMakeLists.txt           | 12 ++-
 .../inc/MantidAlgorithms/AddHistoryNote.h     | 50 +++++++++++++
 Framework/Algorithms/src/AddHistoryNote.cpp   | 67 +++++++++++++++++
 .../Algorithms/test/AddHistoryNoteTest.h      | 74 +++++++++++++++++++
 docs/source/algorithms/AddHistoryNote-v1.rst  | 41 ++++++++++
 5 files changed, 240 insertions(+), 4 deletions(-)
 create mode 100644 Framework/Algorithms/inc/MantidAlgorithms/AddHistoryNote.h
 create mode 100644 Framework/Algorithms/src/AddHistoryNote.cpp
 create mode 100644 Framework/Algorithms/test/AddHistoryNoteTest.h
 create mode 100644 docs/source/algorithms/AddHistoryNote-v1.rst

diff --git a/Framework/Algorithms/CMakeLists.txt b/Framework/Algorithms/CMakeLists.txt
index 9231a99e1f9..7be05f6f589 100644
--- a/Framework/Algorithms/CMakeLists.txt
+++ b/Framework/Algorithms/CMakeLists.txt
@@ -3,6 +3,7 @@ set ( SRC_FILES
 	# src/CountEventsInPulses.cpp
 	# src/UpdatePeakParameterTable.cpp
 	src/AbsorptionCorrection.cpp
+	src/AddHistoryNote.cpp
 	src/AddLogDerivative.cpp
 	src/AddNote.cpp
 	src/AddPeak.cpp
@@ -126,12 +127,12 @@ set ( SRC_FILES
 	src/FlatPlateAbsorption.cpp
 	src/GeneralisedSecondDifference.cpp
 	src/GenerateEventsFilter.cpp
+	src/GenerateIPythonNotebook.cpp
 	src/GeneratePeaks.cpp
 	src/GeneratePythonScript.cpp
 	src/GetAllEi.cpp
 	src/GetDetOffsetsMultiPeaks.cpp
 	src/GetDetectorOffsets.cpp
-	src/GetAllEi.cpp
 	src/GetEi.cpp
 	src/GetEi2.cpp
 	src/GetTimeSeriesLogInformation.cpp
@@ -272,13 +273,14 @@ set ( SRC_FILES
 	src/WienerSmooth.cpp
 	src/WorkspaceJoiners.cpp
 	src/XDataConverter.cpp
-    src/GenerateIPythonNotebook.cpp)
+)
 
 set ( INC_FILES
 	#	inc/MantidAlgorithms/Q1DTOF.h
 	# inc/MantidAlgorithms/CountEventsInPulses.h
 	# inc/MantidAlgorithms/UpdatePeakParameterTable.h
 	inc/MantidAlgorithms/AbsorptionCorrection.h
+	inc/MantidAlgorithms/AddHistoryNote.h
 	inc/MantidAlgorithms/AddLogDerivative.h
 	inc/MantidAlgorithms/AddNote.h
 	inc/MantidAlgorithms/AddPeak.h
@@ -401,11 +403,12 @@ set ( INC_FILES
 	inc/MantidAlgorithms/FixGSASInstrumentFile.h
 	inc/MantidAlgorithms/FlatPlateAbsorption.h
 	inc/MantidAlgorithms/GSLFunctions.h
-	inc/MantidAlgorithms/GetAllEi.h
 	inc/MantidAlgorithms/GeneralisedSecondDifference.h
 	inc/MantidAlgorithms/GenerateEventsFilter.h
+	inc/MantidAlgorithms/GenerateIPythonNotebook.h
 	inc/MantidAlgorithms/GeneratePeaks.h
 	inc/MantidAlgorithms/GeneratePythonScript.h
+	inc/MantidAlgorithms/GetAllEi.h
 	inc/MantidAlgorithms/GetDetOffsetsMultiPeaks.h
 	inc/MantidAlgorithms/GetDetectorOffsets.h
 	inc/MantidAlgorithms/GetEi.h
@@ -549,7 +552,7 @@ set ( INC_FILES
 	inc/MantidAlgorithms/WienerSmooth.h
 	inc/MantidAlgorithms/WorkspaceJoiners.h
 	inc/MantidAlgorithms/XDataConverter.h
-    inc/MantidAlgorithms/GenerateIPythonNotebook.h)
+)
 
 set(SRC_UNITY_IGNORE_FILES src/AlignDetectors.cpp
     src/FFTSmooth.cpp
@@ -566,6 +569,7 @@ endif(UNITY_BUILD)
 set ( TEST_FILES
 	# CountEventsInPulsesTest.h
 	# UpdatePeakParameterTableTest.h
+	AddHistoryNoteTest.h
 	AddLogDerivativeTest.h
 	AddNoteTest.h
 	AddPeakTest.h
diff --git a/Framework/Algorithms/inc/MantidAlgorithms/AddHistoryNote.h b/Framework/Algorithms/inc/MantidAlgorithms/AddHistoryNote.h
new file mode 100644
index 00000000000..d2cc0011a82
--- /dev/null
+++ b/Framework/Algorithms/inc/MantidAlgorithms/AddHistoryNote.h
@@ -0,0 +1,50 @@
+#ifndef MANTID_ALGORITHMS_ADDHISTORYNOTE_H_
+#define MANTID_ALGORITHMS_ADDHISTORYNOTE_H_
+
+#include "MantidAlgorithms/DllConfig.h"
+#include "MantidAPI/Algorithm.h"
+namespace Mantid {
+namespace Algorithms {
+
+/** AddHistoryNote : Adds a note into the history record of a workspace
+
+  Copyright &copy; 2015 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge
+  National Laboratory & European Spallation Source
+
+  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://github.com/mantidproject/mantid>
+  Code Documentation is available at: <http://doxygen.mantidproject.org>
+*/
+class DLLExport AddHistoryNote : public API::Algorithm {
+public:
+  AddHistoryNote();
+  virtual ~AddHistoryNote();
+
+  virtual const std::string name() const;
+  virtual int version() const;
+  virtual const std::string category() const;
+  virtual const std::string summary() const;
+
+private:
+  void init();
+  void exec();
+};
+
+} // namespace Algorithms
+} // namespace Mantid
+
+#endif /* MANTID_ALGORITHMS_ADDHISTORYNOTE_H_ */
\ No newline at end of file
diff --git a/Framework/Algorithms/src/AddHistoryNote.cpp b/Framework/Algorithms/src/AddHistoryNote.cpp
new file mode 100644
index 00000000000..cb184023f8c
--- /dev/null
+++ b/Framework/Algorithms/src/AddHistoryNote.cpp
@@ -0,0 +1,67 @@
+#include "MantidAlgorithms/AddHistoryNote.h"
+#include "MantidKernel/MandatoryValidator.h"
+
+namespace Mantid {
+namespace Algorithms {
+
+using Mantid::Kernel::Direction;
+using Mantid::Kernel::MandatoryValidator;
+using Mantid::API::WorkspaceProperty;
+using Mantid::API::Workspace;
+
+// Register the algorithm into the AlgorithmFactory
+DECLARE_ALGORITHM(AddHistoryNote)
+
+//----------------------------------------------------------------------------------------------
+/** Constructor
+ */
+AddHistoryNote::AddHistoryNote() {}
+
+//----------------------------------------------------------------------------------------------
+/** Destructor
+ */
+AddHistoryNote::~AddHistoryNote() {}
+
+//----------------------------------------------------------------------------------------------
+
+/// Algorithms name for identification. @see Algorithm::name
+const std::string AddHistoryNote::name() const { return "AddHistoryNote"; }
+
+/// Algorithm's version for identification. @see Algorithm::version
+int AddHistoryNote::version() const { return 1; }
+
+/// Algorithm's category for identification. @see Algorithm::category
+const std::string AddHistoryNote::category() const {
+  return "DataHandling";
+}
+
+/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
+const std::string AddHistoryNote::summary() const {
+  return "Adds a note into the history record of a workspace";
+}
+
+//----------------------------------------------------------------------------------------------
+/** Initialize the algorithm's properties.
+ */
+void AddHistoryNote::init() {
+  declareProperty(
+      new WorkspaceProperty<Workspace>("Workspace", "", Direction::InOut),
+      "An InOut workspace that will store the new history record");
+
+  declareProperty(
+      "Note", "", boost::make_shared<MandatoryValidator<std::string>>(),
+      "The note you want to store in the history of the workspace", Direction::Input);
+
+  //always record history for this algorithm
+  enableHistoryRecordingForChild(true);
+}
+
+//----------------------------------------------------------------------------------------------
+/** Execute the algorithm.
+ */
+void AddHistoryNote::exec() {
+  //do nothing
+}
+
+} // namespace Algorithms
+} // namespace Mantid
diff --git a/Framework/Algorithms/test/AddHistoryNoteTest.h b/Framework/Algorithms/test/AddHistoryNoteTest.h
new file mode 100644
index 00000000000..6c67084cbb6
--- /dev/null
+++ b/Framework/Algorithms/test/AddHistoryNoteTest.h
@@ -0,0 +1,74 @@
+#ifndef MANTID_ALGORITHMS_ADDHISTORYNOTETEST_H_
+#define MANTID_ALGORITHMS_ADDHISTORYNOTETEST_H_
+
+#include <cxxtest/TestSuite.h>
+
+#include "MantidAlgorithms/AddHistoryNote.h"
+#include "MantidAPI/Workspace.h"
+#include "MantidTestHelpers/WorkspaceCreationHelper.h"
+
+using Mantid::Algorithms::AddHistoryNote;
+using namespace Mantid::API;
+
+class AddHistoryNoteTest : public CxxTest::TestSuite {
+public:
+  // This pair of boilerplate methods prevent the suite being created statically
+  // This means the constructor isn't called when running other tests
+  static AddHistoryNoteTest *createSuite() { return new AddHistoryNoteTest(); }
+  static void destroySuite( AddHistoryNoteTest *suite ) { delete suite; }
+
+
+  void test_Init()
+  {
+    AddHistoryNote alg;
+    TS_ASSERT_THROWS_NOTHING( alg.initialize() )
+    TS_ASSERT( alg.isInitialized() )
+  }
+
+  void test_exec()
+  {
+    std::string wsName = "AddHistoryNoteTest_Exec_workspace";
+    // Create test input
+    auto ws = WorkspaceCreationHelper::Create2DWorkspace(10, 10);
+    AnalysisDataService::Instance().add(wsName,ws);
+    //and an identical ws for comparison later
+    auto ws2 = WorkspaceCreationHelper::Create2DWorkspace(10, 10);
+
+    AddHistoryNote alg;
+    TS_ASSERT_THROWS_NOTHING( alg.initialize() )
+    TS_ASSERT( alg.isInitialized() )
+    TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Workspace", wsName) );
+    TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Note", 
+      "The next algorithm is doing ws equals 1/ws") );
+    TS_ASSERT_THROWS_NOTHING( alg.execute(); );
+    TS_ASSERT( alg.isExecuted() );
+
+
+    // Retrieve the workspace from data service.
+    MatrixWorkspace_sptr outputWS;
+    TS_ASSERT_THROWS_NOTHING(
+        outputWS = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
+            wsName));
+    TS_ASSERT(outputWS);
+    if (!outputWS)
+      return;
+
+    IAlgorithm_sptr lastAlgorithm = outputWS->getHistory().lastAlgorithm();
+    
+    TS_ASSERT_EQUALS(lastAlgorithm->getPropertyValue("Workspace"),
+      alg.getPropertyValue("Workspace"));
+    TS_ASSERT_EQUALS(lastAlgorithm->getPropertyValue("Note"),
+      alg.getPropertyValue("Note"));
+    std::cout << alg.getPropertyValue("Note") << std::endl;
+    TSM_ASSERT("The workspace has been altered by AddHistoryNote",Mantid::API::equals(ws, ws2));
+    
+    AnalysisDataService::Instance().remove(wsName);
+  }
+  
+
+
+
+};
+
+
+#endif /* MANTID_ALGORITHMS_ADDHISTORYNOTETEST_H_ */
\ No newline at end of file
diff --git a/docs/source/algorithms/AddHistoryNote-v1.rst b/docs/source/algorithms/AddHistoryNote-v1.rst
new file mode 100644
index 00000000000..d937b524505
--- /dev/null
+++ b/docs/source/algorithms/AddHistoryNote-v1.rst
@@ -0,0 +1,41 @@
+
+.. algorithm::
+
+.. summary::
+
+.. alias::
+
+.. properties::
+
+Description
+-----------
+
+This simple algorithm just adds a record to the history of a workspace and is a simple way of annotating your workflow.
+
+It does not change the data within a workspace in any way.
+
+
+Usage
+-----
+
+**Example - AddHistoryNote**
+
+.. testcode:: AddHistoryNoteExample
+
+   ws = CreateSampleWorkspace()
+
+   AddHistoryNote(ws,"The next algorithm is doing 1/ws")
+
+   # Print the result
+   print ws.getHistory().lastAlgorithm().getPropertyValue("Note")
+
+Output:
+
+.. testoutput:: AddHistoryNoteExample
+
+  The next algorithm is doing 1/ws
+
+.. categories::
+
+.. sourcelink::
+
-- 
GitLab