Skip to content

Commit dafaacf

Browse files
authored
Merge pull request #38010 from mantidproject/ewm6817-create-TableWorkspaceValidator
Create TableWorkspaceValidator
2 parents 3cfb5b0 + 33744bf commit dafaacf

File tree

9 files changed

+221
-1
lines changed

9 files changed

+221
-1
lines changed

Framework/DataObjects/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ set(SRC_FILES
4747
src/SplittersWorkspace.cpp
4848
src/TableColumn.cpp
4949
src/TableWorkspace.cpp
50+
src/TableWorkspaceNotEmptyValidator.cpp
5051
src/TimeSplitter.cpp
5152
src/VectorColumn.cpp
5253
src/Workspace2D.cpp
@@ -128,6 +129,8 @@ set(INC_FILES
128129
inc/MantidDataObjects/SplittersWorkspace.h
129130
inc/MantidDataObjects/TableColumn.h
130131
inc/MantidDataObjects/TableWorkspace.h
132+
inc/MantidDataObjects/TableWorkspaceValidator.h
133+
inc/MantidDataObjects/TableWorkspaceNotEmptyValidator.h
131134
inc/MantidDataObjects/TableWorkspace_fwd.h
132135
inc/MantidDataObjects/TimeSplitter.h
133136
inc/MantidDataObjects/VectorColumn.h
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Mantid Repository : https://github.com/mantidproject/mantid
2+
//
3+
// Copyright © 2015 ISIS Rutherford Appleton Laboratory UKRI,
4+
// NScD Oak Ridge National Laboratory, European Spallation Source,
5+
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6+
// SPDX - License - Identifier: GPL - 3.0 +
7+
#pragma once
8+
9+
#include "MantidDataObjects/TableWorkspaceValidator.h"
10+
11+
namespace Mantid {
12+
namespace DataObjects {
13+
14+
/**
15+
A validator which checks that the table workspace is not empty.
16+
*/
17+
class MANTID_DATAOBJECTS_DLL TableWorkspaceNotEmptyValidator : public TableWorkspaceValidator {
18+
public:
19+
explicit TableWorkspaceNotEmptyValidator();
20+
/// Gets the type of the validator
21+
std::string getType() const { return "notempty"; }
22+
/// Clone the current state
23+
Kernel::IValidator_sptr clone() const override;
24+
25+
private:
26+
/// Check for validity.
27+
std::string checkValidity(const TableWorkspace_sptr &value) const override;
28+
};
29+
30+
} // namespace DataObjects
31+
} // namespace Mantid
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Mantid Repository : https://github.com/mantidproject/mantid
2+
//
3+
// Copyright © 2015 ISIS Rutherford Appleton Laboratory UKRI,
4+
// NScD Oak Ridge National Laboratory, European Spallation Source,
5+
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6+
// SPDX - License - Identifier: GPL - 3.0 +
7+
#pragma once
8+
9+
#include "MantidAPI/DllConfig.h"
10+
#include "MantidDataObjects/TableWorkspace.h"
11+
#include "MantidKernel/TypedValidator.h"
12+
13+
namespace Mantid {
14+
namespace DataObjects {
15+
16+
/**
17+
An interface for those validators that require the MatrixWorkspace interface
18+
*/
19+
class MANTID_DATAOBJECTS_DLL TableWorkspaceValidator : public Kernel::TypedValidator<TableWorkspace_sptr> {};
20+
21+
} // namespace DataObjects
22+
} // namespace Mantid
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Mantid Repository : https://github.com/mantidproject/mantid
2+
//
3+
// Copyright &copy; 2018 ISIS Rutherford Appleton Laboratory UKRI,
4+
// NScD Oak Ridge National Laboratory, European Spallation Source,
5+
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6+
// SPDX - License - Identifier: GPL - 3.0 +
7+
#include <utility>
8+
9+
#include "MantidDataObjects/TableWorkspaceNotEmptyValidator.h"
10+
11+
namespace Mantid::DataObjects {
12+
13+
/** Constructor
14+
*
15+
*/
16+
TableWorkspaceNotEmptyValidator::TableWorkspaceNotEmptyValidator() : TableWorkspaceValidator() {}
17+
18+
/**
19+
* Clone the current state
20+
*/
21+
Kernel::IValidator_sptr TableWorkspaceNotEmptyValidator::clone() const {
22+
return std::make_shared<TableWorkspaceNotEmptyValidator>(*this);
23+
}
24+
25+
/** Checks that the workspace is not empty
26+
*
27+
* @return A user level description of the error or "" for no error
28+
*/
29+
std::string TableWorkspaceNotEmptyValidator::checkValidity(const TableWorkspace_sptr &value) const {
30+
if (value->columnCount() == 0) {
31+
return "The workspace must have at least 1 column";
32+
}
33+
if (value->rowCount() == 0) {
34+
return "The workspace must have at least 1 row";
35+
}
36+
37+
return "";
38+
}
39+
40+
} // namespace Mantid::DataObjects

Framework/PythonInterface/mantid/dataobjects/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ set(EXPORT_FILES
3333
src/Exports/NoShape.cpp
3434
src/Exports/PeakShapeSpherical.cpp
3535
src/Exports/PeakShapeEllipsoid.cpp
36+
src/Exports/WorkspaceValidators.cpp
3637
)
3738

3839
set(MODULE_DEFINITION ${CMAKE_CURRENT_BINARY_DIR}/dataobjects.cpp)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Mantid Repository : https://github.com/mantidproject/mantid
2+
//
3+
// Copyright &copy; 2018 ISIS Rutherford Appleton Laboratory UKRI,
4+
// NScD Oak Ridge National Laboratory, European Spallation Source,
5+
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6+
// SPDX - License - Identifier: GPL - 3.0 +
7+
8+
#include "MantidAPI/ITableWorkspace.h"
9+
#include "MantidDataObjects/TableWorkspace.h"
10+
#include "MantidDataObjects/TableWorkspaceNotEmptyValidator.h"
11+
#include "MantidDataObjects/TableWorkspaceValidator.h"
12+
#include "MantidPythonInterface/core/TypedValidatorExporter.h"
13+
#include <boost/python/class.hpp>
14+
15+
using Mantid::API::ITableWorkspace_sptr;
16+
using Mantid::Kernel::TypedValidator;
17+
using Mantid::PythonInterface::TypedValidatorExporter;
18+
using namespace boost::python;
19+
20+
/// This is the base TypedValidator for most of the WorkspaceValidators
21+
void export_TableWorkspaceValidator() {
22+
using Mantid::DataObjects::TableWorkspace_sptr;
23+
using Mantid::DataObjects::TableWorkspaceValidator;
24+
TypedValidatorExporter<TableWorkspace_sptr>::define("TableWorkspaceValidator");
25+
TypedValidatorExporter<ITableWorkspace_sptr>::define("ITableWorkspaceValidator");
26+
27+
class_<TableWorkspaceValidator, bases<TypedValidator<TableWorkspace_sptr>>, boost::noncopyable>(
28+
"TableWorkspaceValidator", no_init);
29+
30+
class_<TypedValidator<ITableWorkspace_sptr>, boost::noncopyable>("ITableWorkspaceValidator", no_init);
31+
}
32+
/// Export a validator derived from a TableWorkspaceValidator that has a no-arg
33+
/// constructor
34+
#define EXPORT_WKSP_VALIDATOR_NO_ARG(ValidatorType, DocString) \
35+
class_<ValidatorType, bases<TableWorkspaceValidator>, boost::noncopyable>(#ValidatorType, init<>(DocString));
36+
/// Export a validator derived from a MatrixWorkspaceValidator that has a
37+
/// single-arg constructor
38+
#define EXPORT_WKSP_VALIDATOR_ARG(ValidatorType, ArgType, ArgName, DocString) \
39+
class_<ValidatorType, bases<TableWorkspaceValidator>, boost::noncopyable>(#ValidatorType, \
40+
init<ArgType>(arg(ArgName), DocString));
41+
/// Export a validator derived from a MatrixWorkspaceValidator that has a
42+
/// single-arg constructor
43+
/// with a default argument
44+
#define EXPORT_WKSP_VALIDATOR_DEFAULT_ARG(ValidatorType, ArgType, ArgName, DefaultValue, DocString) \
45+
class_<ValidatorType, bases<TableWorkspaceValidator>, boost::noncopyable>( \
46+
#ValidatorType, init<ArgType>(arg(ArgName) = DefaultValue, DocString));
47+
48+
void export_WorkspaceValidators() {
49+
using namespace Mantid::DataObjects;
50+
51+
EXPORT_WKSP_VALIDATOR_NO_ARG(TableWorkspaceNotEmptyValidator, "Checks that the workspace is not empty");
52+
}

Framework/PythonInterface/test/python/mantid/dataobjects/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# mantid.dataobjects tests
22

33
set(TEST_PY_FILES EventListTest.py GroupingWorkspaceTest.py SpecialWorkspace2DTest.py Workspace2DPickleTest.py
4-
PeakShapes.py
4+
PeakShapes.py WorkspaceValidatorsTest.py
55
)
66

77
check_tests_valid(${CMAKE_CURRENT_SOURCE_DIR} ${TEST_PY_FILES})
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Mantid Repository : https://github.com/mantidproject/mantid
2+
#
3+
# Copyright &copy; 2024 ISIS Rutherford Appleton Laboratory UKRI,
4+
# NScD Oak Ridge National Laboratory, European Spallation Source,
5+
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6+
# SPDX - License - Identifier: GPL - 3.0 +
7+
"""
8+
Test construction of the WorkspaceValidators
9+
"""
10+
import unittest
11+
import testhelpers
12+
13+
from mantid.dataobjects import TableWorkspaceNotEmptyValidator
14+
15+
from mantid.simpleapi import CreateEmptyTableWorkspace
16+
from mantid.api import ITableWorkspace, PythonAlgorithm, WorkspaceProperty
17+
from mantid.kernel import Direction
18+
19+
20+
class WorkspaceValidatorsTest(unittest.TestCase):
21+
22+
def test_TableWorkspaceNotEmptyValidator_construction(self):
23+
"""
24+
Test that the TableWorkspaceNotEmptyValidator can be constructed
25+
"""
26+
27+
# test the validator can be constructed without error
28+
testhelpers.assertRaisesNothing(self, TableWorkspaceNotEmptyValidator)
29+
30+
def test_TableWorkspaceNotEmptyValidator_usage(self):
31+
32+
class TestAlgorithm(PythonAlgorithm):
33+
"""Mock an extended algorithm that uses TableWorkspaceNotEmptyValidator"""
34+
35+
def PyInit(self):
36+
self.declareProperty(
37+
WorkspaceProperty(
38+
name="InputWorkspace", defaultValue="", direction=Direction.Input, validator=TableWorkspaceNotEmptyValidator()
39+
)
40+
)
41+
42+
def PyExec(self):
43+
pass
44+
45+
# setup the algorithm
46+
alg = TestAlgorithm()
47+
alg.PyInit()
48+
49+
# setup the table workspace
50+
tableWS = CreateEmptyTableWorkspace()
51+
52+
# test the validator rejects an 0-column 0-row TableWorkspace
53+
self.assertRaises(ValueError, alg.setProperty, "InputWorkspace", tableWS)
54+
55+
# test the validator rejects a n-column, 0-row TableWorkspace
56+
tableWS.addColumn("int", "values0")
57+
self.assertRaises(ValueError, alg.setProperty, "InputWorkspace", tableWS)
58+
59+
tableWS.addColumn("int", "values1")
60+
tableWS.addColumn("int", "values2")
61+
self.assertRaises(ValueError, alg.setProperty, "InputWorkspace", tableWS)
62+
63+
# test the validator accepts a filled TableWorkspace
64+
tableWS.addRow([1, 2, 3])
65+
tableWS.addRow([4, 5, 6])
66+
testhelpers.assertRaisesNothing(self, alg.setProperty, "InputWorkspace", tableWS)
67+
68+
69+
if __name__ == "__main__":
70+
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Adds `TableWorkspaceNotEmptyValidator`

0 commit comments

Comments
 (0)