Skip to content

Commit

Permalink
refs #8043. New WorkspaceListProperty type.
Browse files Browse the repository at this point in the history
Basic type template and tests only at this stage.
  • Loading branch information
OwenArnold committed Sep 30, 2013
1 parent 3f745d6 commit 00dea52
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 4 deletions.
11 changes: 7 additions & 4 deletions Code/Mantid/Framework/API/CMakeLists.txt
Expand Up @@ -106,6 +106,7 @@ set ( SRC_FILES
src/WorkspaceFactory.cpp
src/WorkspaceGroup.cpp
src/WorkspaceHistory.cpp
src/WorkspaceListProperty.cpp
src/WorkspaceOpOverloads.cpp
src/WorkspaceProperty.cpp
)
Expand Down Expand Up @@ -259,6 +260,7 @@ set ( INC_FILES
inc/MantidAPI/WorkspaceHistory.h
inc/MantidAPI/WorkspaceIterator.h
inc/MantidAPI/WorkspaceIteratorCode.h
inc/MantidAPI/WorkspaceListProperty.h
inc/MantidAPI/WorkspaceOpOverloads.h
inc/MantidAPI/WorkspaceProperty.h
inc/MantidAPI/WorkspaceValidators.h
Expand Down Expand Up @@ -288,9 +290,9 @@ set ( TEST_FILES
FilePropertyTest.h
FrameworkManagerTest.h
FuncMinimizerFactoryTest.h
FunctionAttributeTest.h
FunctionDomainTest.h
FunctionFactoryTest.h
FunctionAttributeTest.h
FunctionDomainTest.h
FunctionFactoryTest.h
FunctionPropertyTest.h
FunctionTest.h
FunctionValuesTest.h
Expand All @@ -305,7 +307,7 @@ set ( TEST_FILES
IncreasingAxisValidatorTest.h
InstrumentDataServiceTest.h
LiveListenerFactoryTest.h
LogManagerTest.h
LogManagerTest.h
MDGeometryTest.h
MatrixWorkspaceMDIteratorTest.h
MemoryManagerTest.h
Expand All @@ -331,6 +333,7 @@ set ( TEST_FILES
WorkspaceFactoryTest.h
WorkspaceGroupTest.h
WorkspaceHistoryTest.h
WorkspaceListPropertyTest.h
WorkspaceOpOverloadsTest.h
WorkspacePropertyTest.h
)
Expand Down
129 changes: 129 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/WorkspaceListProperty.h
@@ -0,0 +1,129 @@
#ifndef MANTID_API_WORKSPACELISTPROPERTY_H_
#define MANTID_API_WORKSPACELISTPROPERTY_H_

#include <vector>
#include <algorithm>
#include <boost/algorithm/string.hpp>
#include "MantidKernel/System.h"
#include "MantidAPI/WorkspaceProperty.h"


namespace Mantid
{
namespace API
{

/** WorkspaceListProperty : Property with value that constrains the contents to be a list of workspaces of a single type.
Copyright &copy; 2013 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://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
template <typename TYPE = MatrixWorkspace>
class DLLExport WorkspaceListProperty : public Mantid::Kernel::PropertyWithValue<std::vector<boost::shared_ptr<TYPE> > >
{
public:

/// Typedef the value type of this property with value.
typedef std::vector<boost::shared_ptr<TYPE> > WorkspacePropertyListType;

/**
* Helper method to split a comma separated string into a vector of strings.
*/
std::vector<std::string> namesToVector(std::string names)
{
names.erase(std::remove_if(names.begin(), names.end(), (int(*)(int))std::isspace), names.end());
std::vector<std::string> splitNames;
boost::split(splitNames, names, boost::is_any_of(","));
return splitNames;
}

/** Constructor.
* Sets the property and workspace names but initialises the workspace pointers to null.
* @param name :: The name to assign to the property
* @param wsNames :: The names of the workspaces
* @param direction :: Whether this is a Direction::Input, Direction::Output or Direction::InOut (Input & Output) workspace
* @param validator :: The (optional) validator to use for this property
* @throw std::out_of_range if the direction argument is not a member of the Direction enum (i.e. 0-2)
*/
explicit WorkspaceListProperty( const std::string &name, const std::string &wsNames, const unsigned int direction,
Mantid::Kernel::IValidator_sptr validator = Mantid::Kernel::IValidator_sptr(new Kernel::NullValidator)) :
Mantid::Kernel::PropertyWithValue<WorkspacePropertyListType>( name, WorkspacePropertyListType(0), validator, direction ),
m_optional(PropertyMode::Mandatory), m_workspaceNames(0)
// ,m_workspaceName( wsName ), m_initialWSName( wsName ), m_optional(PropertyMode::Mandatory), m_locking(LockMode::Lock)
{
m_workspaceNames = namesToVector(wsNames);
}

/** Constructor.
* Sets the property and workspace names but initialises the workspace pointers to null.
* @param name :: The name to assign to the property
* @param wsNames :: The name of the workspace
* @param direction :: Whether this is a Direction::Input, Direction::Output or Direction::InOut (Input & Output) workspace
* @param optional :: Flag to determine whether this property is optional or not.
* @param validator :: The (optional) validator to use for this property
* @throw std::out_of_range if the direction argument is not a member of the Direction enum (i.e. 0-2)
*/
explicit WorkspaceListProperty( const std::string &name, const std::string &wsNames, const unsigned int direction, const PropertyMode::Type optional,
Mantid::Kernel::IValidator_sptr validator = Mantid::Kernel::IValidator_sptr(new Kernel::NullValidator)) :
Mantid::Kernel::PropertyWithValue<WorkspacePropertyListType>( name, WorkspacePropertyListType(0), validator, direction ),
m_optional(optional), m_workspaceNames(0)
{
m_workspaceNames = namesToVector(wsNames);
}


/// Copy constructor, the default name stored in the new object is the same as the default name from the original object
WorkspaceListProperty( const WorkspaceListProperty& right ) :
Kernel::PropertyWithValue< WorkspacePropertyListType >( right ),
m_optional(right.m_optional)
{
}

/// Is the input workspace property optional?
virtual bool isOptional() const
{
return m_optional == PropertyMode::Optional;
}

/**
* Getter for the workspace names.
*/
std::vector<std::string> getWorkspaceNames() const
{
return m_workspaceNames;
}

virtual ~WorkspaceListProperty()
{
}

private:
/// Flag indicating whether the type is optional or not.
PropertyMode::Type m_optional;
/// Keys to the workspaces in the ADS
std::vector<std::string> m_workspaceNames;

};


} // namespace API
} // namespace Mantid

#endif /* MANTID_API_WORKSPACELISTPROPERTY_H_ */
23 changes: 23 additions & 0 deletions Code/Mantid/Framework/API/src/WorkspaceListProperty.cpp
@@ -0,0 +1,23 @@
#include "MantidAPI/Workspace.h"
#include "MantidAPI/WorkspaceListProperty.h"
#include "MantidAPI/IMDWorkspace.h"
#include "MantidAPI/IEventWorkspace.h"
#include "MantidAPI/IMDEventWorkspace.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidAPI/ISplittersWorkspace.h"

namespace Mantid
{
namespace API
{
///@cond TEMPLATE
template MANTID_API_DLL class Mantid::API::WorkspaceListProperty<Mantid::API::Workspace>;
template MANTID_API_DLL class Mantid::API::WorkspaceListProperty<Mantid::API::IEventWorkspace>;
template MANTID_API_DLL class Mantid::API::WorkspaceListProperty<Mantid::API::IMDEventWorkspace>;
template MANTID_API_DLL class Mantid::API::WorkspaceListProperty<Mantid::API::IMDWorkspace>;
template MANTID_API_DLL class Mantid::API::WorkspaceListProperty<Mantid::API::MatrixWorkspace>;
template MANTID_API_DLL class Mantid::API::WorkspaceListProperty<Mantid::API::ITableWorkspace>;
template MANTID_API_DLL class Mantid::API::WorkspaceListProperty<Mantid::API::ISplittersWorkspace>;
///@endcond TEMPLATE
} // namespace API
} // namespace Mantid
51 changes: 51 additions & 0 deletions Code/Mantid/Framework/API/test/WorkspaceListPropertyTest.h
@@ -0,0 +1,51 @@
#ifndef MANTID_API_WORKSPACELISTPROPERTYTEST_H_
#define MANTID_API_WORKSPACELISTPROPERTYTEST_H_

#include <cxxtest/TestSuite.h>

#include "MantidAPI/WorkspaceListProperty.h"
#include "MantidAPI/Workspace.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/IMDHistoWorkspace.h"
#include "MantidAPI/IMDEventWorkspace.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidAPI/IEventWorkspace.h"
#include "MantidAPI/ISplittersWorkspace.h"

using namespace Mantid::API;
using namespace Mantid::Kernel;

class WorkspaceListPropertyTest : 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 WorkspaceListPropertyTest *createSuite() { return new WorkspaceListPropertyTest(); }
static void destroySuite( WorkspaceListPropertyTest *suite ) { delete suite; }


void testConstructionMinimalistic()
{
TS_ASSERT_THROWS_NOTHING(WorkspaceListProperty<Workspace>("MyWorkspaceProperty", "MyWorkspace", Direction::Input));
TS_ASSERT_THROWS_NOTHING(WorkspaceListProperty<MatrixWorkspace>("MyWorkspaceProperty", "MyWorkspace", Direction::Input));
TS_ASSERT_THROWS_NOTHING(WorkspaceListProperty<IMDHistoWorkspace>("MyWorkspaceProperty", "MyWorkspace", Direction::Input));
TS_ASSERT_THROWS_NOTHING(WorkspaceListProperty<IMDEventWorkspace>("MyWorkspaceProperty", "MyWorkspace", Direction::Input));
TS_ASSERT_THROWS_NOTHING(WorkspaceListProperty<ITableWorkspace>("MyWorkspaceProperty", "MyWorkspace", Direction::Input));
TS_ASSERT_THROWS_NOTHING(WorkspaceListProperty<ISplittersWorkspace>("MyWorkspaceProperty", "MyWorkspace", Direction::Input));
}

void testConstruction()
{
WorkspaceListProperty<Workspace> workspaceListProperty("MyWorkspaceProperty", "MyWorkspace1, MyWorkspace2", Direction::Input, PropertyMode::Mandatory);
auto workspaceNames = workspaceListProperty.getWorkspaceNames();
TS_ASSERT_EQUALS(2, workspaceNames.size());
TS_ASSERT_EQUALS(workspaceNames.front(), "MyWorkspace1");
TS_ASSERT_EQUALS(workspaceNames.back(), "MyWorkspace2");
TS_ASSERT_EQUALS(workspaceListProperty.isOptional(), false)
}


};


#endif /* MANTID_API_WORKSPACELISTPROPERTYTEST_H_ */
Expand Up @@ -174,6 +174,12 @@ void toValue(const std::string& strvalue, std::vector<T>& value)
}
}

template <typename T>
void toValue(const std::string& strvalue, std::vector<boost::shared_ptr<T> >&)
{
throw boost::bad_lexical_cast();
}

template <typename T>
void toValue(const std::string& strvalue, std::vector<std::vector<T> >& value, const std::string & outerDelimiter = ",",
const std::string & innerDelimiter = "+")
Expand Down

0 comments on commit 00dea52

Please sign in to comment.