From c7bf9fd0d1f4595dc55d6aaf9196e8e62a25714d Mon Sep 17 00:00:00 2001
From: Nick Draper <nick.draper@stfc.ac.uk>
Date: Thu, 29 Oct 2015 11:01:05 +0000
Subject: [PATCH] Allow the simple format for livedata algorithms

re #14042
---
 Framework/API/src/FrameworkManager.cpp        | 38 +---------------
 Framework/API/test/AlgorithmPropertyTest.h    |  2 +-
 .../inc/MantidKernel/IPropertyManager.h       | 11 +++++
 .../Kernel/inc/MantidKernel/PropertyManager.h |  3 ++
 .../inc/MantidKernel/PropertyManagerOwner.h   |  5 +++
 Framework/Kernel/src/PropertyManager.cpp      | 45 +++++++++++++++++++
 Framework/Kernel/src/PropertyManagerOwner.cpp | 13 ++++++
 Framework/LiveData/src/LiveDataAlgorithm.cpp  |  2 +-
 .../LiveData/test/LiveDataAlgorithmTest.h     |  2 +-
 Framework/LiveData/test/StartLiveDataTest.h   |  4 +-
 10 files changed, 84 insertions(+), 41 deletions(-)

diff --git a/Framework/API/src/FrameworkManager.cpp b/Framework/API/src/FrameworkManager.cpp
index 26848f7a02f..588496fd1d5 100644
--- a/Framework/API/src/FrameworkManager.cpp
+++ b/Framework/API/src/FrameworkManager.cpp
@@ -309,42 +309,8 @@ FrameworkManagerImpl::createAlgorithm(const std::string &algName,
   IAlgorithm *alg = AlgorithmManager::Instance()
                         .create(algName, version)
                         .get(); // createAlgorithm(algName);
-
-  ::Json::Value propertyJson;
-  // Split up comma-separated properties
-  typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
-
-  boost::char_separator<char> sep(";");
-  tokenizer propPairs(propertiesArray, sep);
-  int index = 0;
-  // Iterate over the properties
-  for (tokenizer::iterator it = propPairs.begin(); it != propPairs.end();
-       ++it) {
-    // Pair of the type "
-    std::string pair = *it;
-
-    size_t n = pair.find('=');
-    if (n != std::string::npos) {
-      // Normal "PropertyName=value" string.
-      std::string propName = "";
-      std::string value = "";
-
-      // Extract the value string
-      if (n < pair.size() - 1) {
-        propName = pair.substr(0, n);
-        value = pair.substr(n + 1, pair.size() - n - 1);
-      } else {
-        // String is "PropertyName="
-        propName = pair.substr(0, n);
-        value = "";
-      }
-      // Set it
-      propertyJson[propName] = value;
-    }
-    index++;
-  }
-  ::Json::FastWriter writer;
-  alg->setProperties(writer.write(propertyJson));
+  alg->setPropertiesWithSimpleString(propertiesArray);
+  
   return alg;
 }
 
diff --git a/Framework/API/test/AlgorithmPropertyTest.h b/Framework/API/test/AlgorithmPropertyTest.h
index 9d60a2b1cf9..c71dc4bad63 100644
--- a/Framework/API/test/AlgorithmPropertyTest.h
+++ b/Framework/API/test/AlgorithmPropertyTest.h
@@ -96,7 +96,7 @@ public:
 
   void test_An_Invalid_String_Returns_An_Appropriate_Error() {
     AlgorithmProperty testProp("CalculateStep");
-    TS_ASSERT_EQUALS(testProp.setValue("ComplexSum()"),
+    TS_ASSERT_EQUALS(testProp.setValue("{\"name\":\"ComplexSum\"}"),
                      "Algorithm not registered ComplexSum");
   }
 
diff --git a/Framework/Kernel/inc/MantidKernel/IPropertyManager.h b/Framework/Kernel/inc/MantidKernel/IPropertyManager.h
index 63da9c9dfe3..d95afcfed62 100644
--- a/Framework/Kernel/inc/MantidKernel/IPropertyManager.h
+++ b/Framework/Kernel/inc/MantidKernel/IPropertyManager.h
@@ -70,6 +70,17 @@ public:
   virtual void removeProperty(const std::string &name,
                               const bool delproperty = true) = 0;
 
+  /** Sets all the declared properties from a string.
+    @param propertiesString :: A list of name = value pairs separated by a
+     semicolon
+    @param ignoreProperties :: A set of names of any properties NOT to set
+    from the propertiesArray
+  */
+  virtual void
+  setPropertiesWithSimpleString(const std::string &propertiesString,
+                const std::set<std::string> &
+                    ignoreProperties = std::set<std::string>()) = 0;
+
   /** Sets all the declared properties from a string.
       @param propertiesJson :: A string of name = value pairs formatted
         as a json name value pair collection
diff --git a/Framework/Kernel/inc/MantidKernel/PropertyManager.h b/Framework/Kernel/inc/MantidKernel/PropertyManager.h
index 52778dc9466..4df872b3748 100644
--- a/Framework/Kernel/inc/MantidKernel/PropertyManager.h
+++ b/Framework/Kernel/inc/MantidKernel/PropertyManager.h
@@ -80,6 +80,9 @@ public:
   void setProperties(
       const ::Json::Value &jsonValue,
       const std::set<std::string> &ignoreProperties = std::set<std::string>());
+  void setPropertiesWithSimpleString(
+      const std::string &propertiesString,
+      const std::set<std::string> &ignoreProperties = std::set<std::string>());
   void setPropertyValue(const std::string &name, const std::string &value);
   void setPropertyOrdinal(const int &index, const std::string &value);
 
diff --git a/Framework/Kernel/inc/MantidKernel/PropertyManagerOwner.h b/Framework/Kernel/inc/MantidKernel/PropertyManagerOwner.h
index 3849c00dae0..b1a63444c9c 100644
--- a/Framework/Kernel/inc/MantidKernel/PropertyManagerOwner.h
+++ b/Framework/Kernel/inc/MantidKernel/PropertyManagerOwner.h
@@ -69,6 +69,11 @@ public:
       const ::Json::Value &jsonValue,
       const std::set<std::string> &ignoreProperties = std::set<std::string>());
 
+  //sets all the declared properties using a simple string format
+  void setPropertiesWithSimpleString(
+      const std::string &propertiesString,
+      const std::set<std::string> &ignoreProperties = std::set<std::string>());
+
   void setPropertyValue(const std::string &name, const std::string &value);
   void setPropertyOrdinal(const int &index, const std::string &value);
 
diff --git a/Framework/Kernel/src/PropertyManager.cpp b/Framework/Kernel/src/PropertyManager.cpp
index 62a9f1b90e3..666c7c2f27d 100644
--- a/Framework/Kernel/src/PropertyManager.cpp
+++ b/Framework/Kernel/src/PropertyManager.cpp
@@ -263,6 +263,51 @@ void PropertyManager::setProperties(
   }
 }
 
+/** Sets all the declared properties from a string.
+  @param propertiesString :: A list of name = value pairs separated by a
+    semicolon
+  @param ignoreProperties :: A set of names of any properties NOT to set
+  from the propertiesArray
+*/
+void PropertyManager::setPropertiesWithSimpleString(
+    const std::string &propertiesString,
+    const std::set<std::string> &ignoreProperties) {
+  ::Json::Value propertyJson;
+  // Split up comma-separated properties
+  typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
+
+  boost::char_separator<char> sep(";");
+  tokenizer propPairs(propertiesString, sep);
+  int index = 0;
+  // Iterate over the properties
+  for (tokenizer::iterator it = propPairs.begin(); it != propPairs.end();
+       ++it) {
+    // Pair of the type "
+    std::string pair = *it;
+
+    size_t n = pair.find('=');
+    if (n != std::string::npos) {
+      // Normal "PropertyName=value" string.
+      std::string propName = "";
+      std::string value = "";
+
+      // Extract the value string
+      if (n < pair.size() - 1) {
+        propName = pair.substr(0, n);
+        value = pair.substr(n + 1, pair.size() - n - 1);
+      } else {
+        // String is "PropertyName="
+        propName = pair.substr(0, n);
+        value = "";
+      }
+      // Set it
+      propertyJson[propName] = value;
+    }
+    index++;
+  }
+  setProperties(propertyJson, ignoreProperties);
+}
+
 //-----------------------------------------------------------------------------------------------
 /** Set the value of a property by string
  *  N.B. bool properties must be set using 1/0 rather than true/false
diff --git a/Framework/Kernel/src/PropertyManagerOwner.cpp b/Framework/Kernel/src/PropertyManagerOwner.cpp
index bcc461b7065..e7ff0b08bfe 100644
--- a/Framework/Kernel/src/PropertyManagerOwner.cpp
+++ b/Framework/Kernel/src/PropertyManagerOwner.cpp
@@ -68,6 +68,19 @@ void PropertyManagerOwner::setProperties(
   m_properties->setProperties(jsonValue, ignoreProperties);
 }
 
+/** Sets all the declared properties from a string.
+  @param propertiesString :: A list of name = value pairs separated by a
+    semicolon
+  @param ignoreProperties :: A set of names of any properties NOT to set
+  from the propertiesArray
+*/
+void PropertyManagerOwner::setPropertiesWithSimpleString(
+    const std::string &propertiesString,
+    const std::set<std::string> &ignoreProperties) {
+  m_properties->setPropertiesWithSimpleString(propertiesString,
+                                              ignoreProperties);
+}
+
 /** Set the value of a property by string
 *  N.B. bool properties must be set using 1/0 rather than true/false
 *  @param name :: The name of the property (case insensitive)
diff --git a/Framework/LiveData/src/LiveDataAlgorithm.cpp b/Framework/LiveData/src/LiveDataAlgorithm.cpp
index 6c5e3385785..5a3390e0770 100644
--- a/Framework/LiveData/src/LiveDataAlgorithm.cpp
+++ b/Framework/LiveData/src/LiveDataAlgorithm.cpp
@@ -252,7 +252,7 @@ IAlgorithm_sptr LiveDataAlgorithm::makeAlgorithm(bool postProcessing) {
     ignoreProps.insert("OutputWorkspace");
 
     // ...and pass it the properties
-    alg->setProperties(props, ignoreProps);
+    alg->setPropertiesWithSimpleString(props, ignoreProps);
 
     // Warn if someone put both values.
     if (!script.empty())
diff --git a/Framework/LiveData/test/LiveDataAlgorithmTest.h b/Framework/LiveData/test/LiveDataAlgorithmTest.h
index 9f43790162b..0e2b7344b38 100644
--- a/Framework/LiveData/test/LiveDataAlgorithmTest.h
+++ b/Framework/LiveData/test/LiveDataAlgorithmTest.h
@@ -122,7 +122,7 @@ public:
       TS_ASSERT_THROWS_NOTHING(
           alg.setPropertyValue(prefix + "ProcessingAlgorithm", "Rebin"));
       TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue(
-          prefix + "ProcessingProperties", "{\"Params\":\"0,100,1000\"}"));
+          prefix + "ProcessingProperties", "Params=0,100,1000"));
 
       procAlg = alg.makeAlgorithm(post > 0);
       TSM_ASSERT("Non-NULL algorithm pointer", procAlg);
diff --git a/Framework/LiveData/test/StartLiveDataTest.h b/Framework/LiveData/test/StartLiveDataTest.h
index 88d8af3bc52..023343ebd11 100644
--- a/Framework/LiveData/test/StartLiveDataTest.h
+++ b/Framework/LiveData/test/StartLiveDataTest.h
@@ -84,7 +84,7 @@ public:
     // Declare all algorithms, e.g. Rebin
     FrameworkManager::Instance();
     EventWorkspace_sptr ws;
-    ws = doExecEvent("Replace", 0, "Rebin", "{\"Params\":\"40e3, 1e3, 60e3\"}");
+    ws = doExecEvent("Replace", 0, "Rebin", "Params=40e3, 1e3, 60e3");
     TS_ASSERT_EQUALS(ws->getNumberHistograms(), 2);
     TS_ASSERT_EQUALS(ws->getNumberEvents(), 200);
     // Check that rebin was called
@@ -120,7 +120,7 @@ public:
     FrameworkManager::Instance();
     AlgorithmManager::Instance().clear();
     EventWorkspace_sptr ws;
-    ws = doExecEvent("Replace", 1, "Rebin", "{\"Params\":\"40e3, 1e3, 60e3\"}");
+    ws = doExecEvent("Replace", 1, "Rebin", "Params=40e3, 1e3, 60e3");
 
     TS_ASSERT_EQUALS(ws->getNumberHistograms(), 2);
     TS_ASSERT_EQUALS(ws->getNumberEvents(), 200);
-- 
GitLab