Skip to content

Commit

Permalink
Refs #8534. Merge remote-tracking branch 'origin/master' into feature…
Browse files Browse the repository at this point in the history
…/8534_muon_new_workflow_alg
  • Loading branch information
arturbekasov committed Dec 3, 2013
2 parents cc5158e + d8ad95a commit 8b88c8a
Show file tree
Hide file tree
Showing 30 changed files with 971 additions and 651 deletions.
13 changes: 8 additions & 5 deletions Code/Mantid/Framework/API/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ set ( SRC_FILES
src/Run.cpp
src/Sample.cpp
src/SampleEnvironment.cpp
src/ScopedWorkspace.cpp
src/ScriptRepository.cpp
src/ScriptRepositoryFactory.cpp
src/SpectraAxis.cpp
Expand Down Expand Up @@ -241,6 +242,7 @@ set ( INC_FILES
inc/MantidAPI/Run.h
inc/MantidAPI/Sample.h
inc/MantidAPI/SampleEnvironment.h
inc/MantidAPI/ScopedWorkspace.h
inc/MantidAPI/ScriptRepository.h
inc/MantidAPI/ScriptRepositoryFactory.h
inc/MantidAPI/SingleValueParameter.h
Expand Down Expand Up @@ -288,25 +290,25 @@ 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
IEventListTest.h
IFunction1DTest.h
IFunctionMDTest.h
IkedaCarpenterModeratorTest.h
ILiveListenerTest.h
IMDWorkspaceTest.h
ISpectrumTest.h
IkedaCarpenterModeratorTest.h
ImmutableCompositeFunctionTest.h
ImplicitFunctionParserFactoryTest.h
IncreasingAxisValidatorTest.h
InstrumentDataServiceTest.h
LiveListenerFactoryTest.h
LogManagerTest.h
LogManagerTest.h
MDGeometryTest.h
MatrixWorkspaceMDIteratorTest.h
MemoryManagerTest.h
Expand All @@ -324,6 +326,7 @@ set ( TEST_FILES
RunTest.h
SampleEnvironmentTest.h
SampleTest.h
ScopedWorkspaceTest.h
SpectraAxisTest.h
SpectrumDetectorMappingTest.h
TextAxisTest.h
Expand Down
89 changes: 89 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/ScopedWorkspace.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#ifndef MANTID_API_SCOPEDWORKSPACE_H_
#define MANTID_API_SCOPEDWORKSPACE_H_

#include <string>

#include "MantidKernel/ClassMacros.h"
#include "MantidKernel/System.h"
#include "MantidAPI/Workspace.h"

namespace Mantid
{
namespace API
{
/** ScopedWorkspace : scoped workspace ADS entry.
This class is provided for situations when you need a workspace to be in the ADS to run an
algorithm, but you don't really need to keep it there after the algorithm has finished. In
these circumstances you can create ScopedWorkspace, set it's name as a workspace property
for the algorithm and retrieve it when algorithm has finished. The workspace will be
removed from the ADS when the object goes out of scope, or exception is thrown.
Primarily, it was created to overcome some limitations of WorkspaceProperties, but it can be
useful in other places, e.g. tests.
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>
*/
class DLLExport ScopedWorkspace
{
public:
/// Empty constructor
ScopedWorkspace();

/// Workspace constructor
ScopedWorkspace(Workspace_sptr ws);

/// Destructor
virtual ~ScopedWorkspace();

/// Returns ADS name of the workspace
std::string name() const { return m_name; }

/// Retrieve workspace from the ADS
Workspace_sptr retrieve() const;

/// Operator for conversion to boolean
operator bool() const;

/// Make ADS entry to point to the given workspace
void set(Workspace_sptr newWS);

private:
DISABLE_COPY_AND_ASSIGN(ScopedWorkspace);

/// ADS name of the workspace
const std::string m_name;

/// Generates a tricky name which is unique within ADS
static std::string generateUniqueName();

/// Generates a random alpha-numeric string
static std::string randomString(size_t len);

/// Length of workspace names generated
static const size_t NAME_LENGTH;
};


} // namespace API
} // namespace Mantid

#endif /* MANTID_API_SCOPEDWORKSPACE_H_ */
10 changes: 10 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/WorkspaceProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ namespace Mantid
*/
virtual boost::shared_ptr<TYPE>& operator=( const boost::shared_ptr<TYPE>& value )
{
std::string wsName = value->name();
if ( this->direction() == Kernel::Direction::Input && !wsName.empty() )
{
m_workspaceName = wsName;
}
return Kernel::PropertyWithValue< boost::shared_ptr<TYPE> >::operator=( value );
}

Expand Down Expand Up @@ -215,6 +220,11 @@ namespace Mantid
boost::shared_ptr<TYPE> typed = boost::dynamic_pointer_cast<TYPE>(value);
if(typed)
{
std::string wsName = typed->name();
if ( this->direction() == Kernel::Direction::Input && !wsName.empty() )
{
m_workspaceName = wsName;
}
Kernel::PropertyWithValue< boost::shared_ptr<TYPE> >::m_value = typed;
}
else
Expand Down
134 changes: 134 additions & 0 deletions Code/Mantid/Framework/API/src/ScopedWorkspace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include "MantidAPI/ScopedWorkspace.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/WorkspaceGroup.h"

namespace Mantid
{
namespace API
{

const size_t ScopedWorkspace::NAME_LENGTH = 16;

//----------------------------------------------------------------------------------------------
/**
* Empty constructor
*/
ScopedWorkspace::ScopedWorkspace() :
m_name( generateUniqueName() )
{}

/**
* Workspace constructor
*/
ScopedWorkspace::ScopedWorkspace(Workspace_sptr ws) :
m_name( generateUniqueName() )
{
set(ws);
}

//----------------------------------------------------------------------------------------------
/**
* Destructor
*/
ScopedWorkspace::~ScopedWorkspace()
{
AnalysisDataServiceImpl& ads = AnalysisDataService::Instance();

// When destructed, remove workspace from the ADS if was added and still exists
if ( ads.doesExist(m_name) )
{
if ( ads.retrieveWS<WorkspaceGroup>(m_name) )
{
// If is a group, need to remove all the members as well
ads.deepRemoveGroup(m_name);
}
else
{
ads.remove(m_name);
}
}
}

/**
* Operator for conversion to boolean. Returns true if workspace was created for the
* name and it is not null workspace.
*/
ScopedWorkspace::operator bool() const
{
AnalysisDataServiceImpl& ads = AnalysisDataService::Instance();

if ( ads.doesExist(m_name) )
return ads.retrieveWS<Workspace>(m_name);
else
return false;
}

/**
* Retrieve workspace from the ADS. Null pointer returned if nothing was added
* under the name.
*/
Workspace_sptr ScopedWorkspace::retrieve() const
{
AnalysisDataServiceImpl& ads = AnalysisDataService::Instance();

if ( ads.doesExist(m_name) )
{
return ads.retrieveWS<Workspace>(m_name);
}

return Workspace_sptr();
}

/**
* Make ADS entry to point to the given workspace.
*/
void ScopedWorkspace::set(Workspace_sptr newWS)
{
AnalysisDataServiceImpl& ads = AnalysisDataService::Instance();

if ( ! newWS->name().empty() && ads.doesExist( newWS->name() ) )
throw std::invalid_argument( "Workspace is already in the ADS under the name " + newWS->name() );

ads.add(m_name, newWS);
}

/**
* Generates a tricky name which is unique within ADS.
*/
std::string ScopedWorkspace::generateUniqueName()
{
std::string newName;

do
{
// __ makes it hidden in the MantidPlot
newName = "__ScopedWorkspace_" + randomString(NAME_LENGTH);
}
while( AnalysisDataService::Instance().doesExist(newName) );

return newName;
}

/**
* Generates random alpha-numeric string.
* @param len :: Length of the string
* @return Random string of the given length
*/
std::string ScopedWorkspace::randomString(size_t len)
{
static const std::string alphabet = "0123456789abcdefghijklmnopqrstuvwxyz";

std::string result;
result.reserve(len);

while(result.size() != len)
{
size_t randPos = ( (rand() % (alphabet.size() - 1)));
result.push_back(alphabet[randPos]);
}

return result;
}

} // namespace API
} // namespace Mantid
1 change: 1 addition & 0 deletions Code/Mantid/Framework/API/test/AlgorithmTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ class AlgorithmTest : public CxxTest::TestSuite
/// All groups are the same size
void test_processGroups_allSameSize()
{
Mantid::API::AnalysisDataService::Instance().clear();
WorkspaceGroup_sptr group = do_test_groups("A", "A_1,A_2,A_3",
"B", "B_1,B_2,B_3", "C", "C_1,C_2,C_3");

Expand Down

0 comments on commit 8b88c8a

Please sign in to comment.