Skip to content

Commit

Permalink
Merge branch 'master' into feature/7614_interface_categories
Browse files Browse the repository at this point in the history
Refs #7614.

Conflicts:
	Code/Mantid/MantidPlot/src/ApplicationWindow.h
  • Loading branch information
PeterParker committed Oct 25, 2013
2 parents d6187a2 + 0d27872 commit 66f1940
Show file tree
Hide file tree
Showing 111 changed files with 13,910 additions and 1,860 deletions.
8 changes: 8 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/Algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ class MANTID_API_DLL Algorithm : public IAlgorithm, public Kernel::PropertyManag
virtual const std::string categorySeparator() const {return ";";}
/// function to return any aliases to the algorithm; A default implementation is provided
virtual const std::string alias() const {return "";}

const std::string workspaceMethodName() const;
const std::vector<std::string> workspaceMethodOn() const;
const std::string workspaceMethodInputProperty() const;

/// Algorithm ID. Unmanaged algorithms return 0 (or NULL?) values. Managed ones have non-zero.
AlgorithmID getAlgorithmID()const{return m_algorithmID;}

Expand Down Expand Up @@ -267,6 +272,9 @@ class MANTID_API_DLL Algorithm : public IAlgorithm, public Kernel::PropertyManag
/// Method defining summary, optional
virtual void initDocs() {};

/// Returns a semi-colon separated list of workspace types to attach this algorithm
virtual const std::string workspaceMethodOnTypes() const { return ""; }

void cacheWorkspaceProperties();

friend class AlgorithmProxy;
Expand Down
4 changes: 4 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/AlgorithmProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ namespace Mantid
void enableHistoryRecordingForChild(const bool) {};
void setRethrows(const bool rethrow);

const std::string workspaceMethodName() const;
const std::vector<std::string> workspaceMethodOn() const;
const std::string workspaceMethodInputProperty() const;

/** @name PropertyManager methods */
//@{
/// Set the property value
Expand Down
7 changes: 7 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/FileLoaderRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ namespace Mantid
m_log.debug() << "Registered '" << nameVersion.first << "' version '" << nameVersion.second << "' as file loader\n";
}

/// Unsubscribe a named algorithm and version from the loader registration
void unsubscribe(const std::string &name, const int version = -1);

/// Returns the name of an Algorithm that can load the given filename
const boost::shared_ptr<IAlgorithm> chooseLoader(const std::string &filename) const;
/// Checks whether the given algorithm can load the file
Expand Down Expand Up @@ -121,6 +124,10 @@ namespace Mantid
}
};

/// Remove a named algorithm & version from the given map
void removeAlgorithm(const std::string & name, const int version,
std::multimap<std::string,int> & typedLoaders);

/// The list of names. The index pointed to by LoaderFormat defines a set for that format
std::vector<std::multimap<std::string,int> > m_names;
/// Total number of names registered
Expand Down
10 changes: 10 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/IAlgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ class MANTID_API_DLL IAlgorithm : virtual public Kernel::IPropertyManager
/// function to return any aliases of the algorithm.
virtual const std::string alias() const = 0;

/** @name Algorithms As Methods */
///@{
/// Returns a name that will be used when attached as a workspace method. Empty string indicates do not attach
virtual const std::string workspaceMethodName() const = 0;
/// Returns a set of class names that will have the method attached. Empty list indicates all types
virtual const std::vector<std::string> workspaceMethodOn() const = 0;
/// Returns the name of the input workspace property used by the calling object
virtual const std::string workspaceMethodInputProperty() const = 0;
///@}

/// Algorithm ID. Unmanaged algorithms return 0 (or NULL?) values. Managed ones have non-zero.
virtual AlgorithmID getAlgorithmID()const = 0;

Expand Down
42 changes: 41 additions & 1 deletion Code/Mantid/Framework/API/src/Algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ namespace Mantid
{
namespace API
{
namespace
{
/// Separator for workspace types in workspaceMethodOnTypes member
const std::string WORKSPACE_TYPES_SEPARATOR = ";";
}

// Doxygen can't handle member specialization at the moment: https://bugzilla.gnome.org/show_bug.cgi?id=406027
// so we have to ignore them
///@cond
Expand Down Expand Up @@ -213,7 +219,7 @@ namespace Mantid
Poco::StringTokenizer tokenizer(category(), categorySeparator(),
Poco::StringTokenizer::TOK_TRIM | Poco::StringTokenizer::TOK_IGNORE_EMPTY);
Poco::StringTokenizer::Iterator h = tokenizer.begin();

for (; h != tokenizer.end(); ++h)
{
res.push_back(*h);
Expand All @@ -227,6 +233,40 @@ namespace Mantid
return res;
}

/**
* @return A string giving the method name that should be attached to a workspace
*/
const std::string Algorithm::workspaceMethodName() const
{
return "";
}

/**
*
* @return A list of workspace class names that should have the workspaceMethodName attached
*/
const std::vector<std::string> Algorithm::workspaceMethodOn() const
{
Poco::StringTokenizer tokenizer(this->workspaceMethodOnTypes(), WORKSPACE_TYPES_SEPARATOR,
Poco::StringTokenizer::TOK_TRIM | Poco::StringTokenizer::TOK_IGNORE_EMPTY);
std::vector<std::string> res;
res.reserve(tokenizer.count());
for( auto iter = tokenizer.begin(); iter != tokenizer.end(); ++iter )
{
res.push_back(*iter);
}

return res;
}

/**
* @return The name of the property that the calling object will be passed to.
*/
const std::string Algorithm::workspaceMethodInputProperty() const
{
return "";
}


//=============================================================================================
//================================== Initialization ===========================================
Expand Down
27 changes: 27 additions & 0 deletions Code/Mantid/Framework/API/src/AlgorithmProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,33 @@ namespace Mantid
if(m_alg) m_alg->setRethrows(rethrow);
}

/**
* @return A string giving the method name that should be attached to a workspace
*/
const std::string AlgorithmProxy::workspaceMethodName() const
{
if(m_alg) return m_alg->workspaceMethodName();
else return "";
}

/**
* @return A set of workspace class names that should have the workspaceMethodName attached
*/
const std::vector<std::string> AlgorithmProxy::workspaceMethodOn() const
{
if(m_alg) return m_alg->workspaceMethodOn();
else return std::vector<std::string>();
}

/**
* @return The name of the property that the calling object will be passed to
*/
const std::string AlgorithmProxy::workspaceMethodInputProperty() const
{
if(m_alg) return m_alg->workspaceMethodInputProperty();
else return "";
}

/**
* Override setPropertyValue
* @param name The name of the property
Expand Down
42 changes: 42 additions & 0 deletions Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace Mantid
const int version = it->second;
logger.debug() << "Checking " << name << " version " << version << std::endl;

// Use static cast for speed. Checks have been done at registration to check the types
auto alg = boost::static_pointer_cast<FileLoaderType>(factory.create(name, version)); // highest version
try
{
Expand All @@ -75,6 +76,20 @@ namespace Mantid
// Public members
//----------------------------------------------------------------------------------------------

/**
* If the name does not exist then it does nothing
* @param name Name of the algorithm to remove from the search list
* @aparam version An optional version to remove. -1 indicates remove all (Default=-1)
*/
void FileLoaderRegistryImpl::unsubscribe(const std::string &name, const int version)
{
auto iend = m_names.end();
for(auto it = m_names.begin(); it != iend; ++it)
{
removeAlgorithm(name, version, *it);
}
}

/**
* Queries each registered algorithm and asks it how confident it is that it can
* load the given file. The name of the one with the highest confidence is returned.
Expand Down Expand Up @@ -161,5 +176,32 @@ namespace Mantid
{
}

/**
* @param name A string containing the algorithm name
* @param version The version to remove. -1 indicates all instances
* @param typedLoaders A map of names to version numbers
**/
void FileLoaderRegistryImpl::removeAlgorithm(const std::string & name, const int version,
std::multimap<std::string,int> & typedLoaders)
{
if(version == -1) // remove all
{
typedLoaders.erase(name);
}
else // find the right version
{
auto range = typedLoaders.equal_range(name);
for(auto ritr = range.first; ritr != range.second; ++ritr)
{
if(ritr->second == version)
{
typedLoaders.erase(ritr);
break;
}
}
}
}


} // namespace API
} // namespace Mantid
20 changes: 20 additions & 0 deletions Code/Mantid/Framework/API/test/AlgorithmProxyTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class ToyAlgorithmProxy : public Algorithm
int version() const { return 1;} ///< Algorithm's version for identification
const std::string category() const { return "ProxyCat";} ///< Algorithm's category for identification
const std::string alias() const { return "Dog";} ///< Algorithm's alias
const std::string workspaceMethodName() const { return "toyalgorithm"; }
const std::string workspaceMethodOnTypes() const { return "MatrixWorkspace;ITableWorkspace"; }
const std::string workspaceMethodInputProperty() const { return "InputWorkspace"; }

void init()
{
Expand Down Expand Up @@ -192,6 +195,23 @@ class AlgorithmProxyTest : public CxxTest::TestSuite
TS_ASSERT( obs.progress );
TS_ASSERT( obs.finish );
}

void test_WorkspaceMethodFunctionsReturnProxiedContent()
{
IAlgorithm_sptr alg = AlgorithmManager::Instance().create("ToyAlgorithmProxy");

TS_ASSERT_EQUALS("toyalgorithm", alg->workspaceMethodName());

auto types = alg->workspaceMethodOn();
TS_ASSERT_EQUALS(2, types.size());
if(types.size() == 2)
{
TS_ASSERT_EQUALS("MatrixWorkspace", types[0]);
TS_ASSERT_EQUALS("ITableWorkspace", types[1]);
}
TS_ASSERT_EQUALS("InputWorkspace", alg->workspaceMethodInputProperty());
}

};

#endif /*ALGORITHMPROXYTEST_H_*/
28 changes: 28 additions & 0 deletions Code/Mantid/Framework/API/test/AlgorithmTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ class AlgorithmWithValidateInputs : public Algorithm
const std::string name() const { return "StubbedWorkspaceAlgorithm2";}
int version() const { return 1;}
const std::string category() const { return "Cat;Leopard;Mink";}
const std::string workspaceMethodName() const { return "methodname"; }
const std::string workspaceMethodOnTypes() const { return "MatrixWorkspace;ITableWorkspace"; }
const std::string workspaceMethodInputProperty() const { return "InputWorkspace"; }

void init()
{
declareProperty("PropertyA", 12);
Expand Down Expand Up @@ -242,6 +246,30 @@ class AlgorithmTest : public CxxTest::TestSuite
TS_ASSERT( alg.isExecuted() );
}

void test_WorkspaceMethodFunctionsReturnEmptyByDefault()
{
StubbedWorkspaceAlgorithm alg;

TS_ASSERT_EQUALS("", alg.workspaceMethodName());
TS_ASSERT_EQUALS(std::vector<std::string>(), alg.workspaceMethodOn());
TS_ASSERT_EQUALS("", alg.workspaceMethodInputProperty());
}

void test_WorkspaceMethodsReturnTypesCorrectly()
{
AlgorithmWithValidateInputs alg;

TS_ASSERT_EQUALS("methodname", alg.workspaceMethodName());
auto types = alg.workspaceMethodOn();
TS_ASSERT_EQUALS(2, types.size());
if(types.size() == 2)
{
TS_ASSERT_EQUALS("MatrixWorkspace", types[0]);
TS_ASSERT_EQUALS("ITableWorkspace", types[1]);
}
TS_ASSERT_EQUALS("InputWorkspace", alg.workspaceMethodInputProperty());
}

void testStringization()
{
//Set the properties so that we know what they are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class DLLExport CloneWorkspace : public API::Algorithm
virtual const std::string category() const { return "Utility\\Workspaces"; }

private:
const std::string workspaceMethodName() const { return "clone"; }
const std::string workspaceMethodInputProperty() const { return "InputWorkspace"; }

/// Sets documentation strings for this algorithm
virtual void initDocs();
/// Initialisation code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class DLLExport ConvertUnits : public API::Algorithm
virtual const std::string category() const { return "Transforms\\Units";}

private:
const std::string workspaceMethodName() const { return "convertUnits"; }
const std::string workspaceMethodInputProperty() const { return "InputWorkspace"; }

/// Sets documentation strings for this algorithm
virtual void initDocs();
// Overridden Algorithm methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ namespace Mantid
/// Overridden exec
void exec();

const std::string workspaceMethodName() const { return "delete"; }
const std::string workspaceMethodInputProperty() const { return "Workspace"; }
};

} // namespace Algorithm
Expand Down

0 comments on commit 66f1940

Please sign in to comment.