Skip to content

Commit

Permalink
refs #3763 added allowedValue function to CompositeValidator.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
abuts committed Nov 2, 2011
1 parent 0c69668 commit 6b9f6e4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
Expand Up @@ -4,6 +4,7 @@
#include "MantidKernel/System.h"
#include "MantidKernel/IValidator.h"
#include <vector>
#include <set>


namespace Mantid
Expand Down Expand Up @@ -56,7 +57,40 @@ namespace Kernel
// IValidator methods
///Gets the type of the validator
std::string getType() const { return "composite"; }

/** returns allowed values as intersection of non-empty sets of allowed values for child validators;
*
* not very consistent but reasonable as non-list validators often return empty sets of allowed values;
* primary purpose -- return only one set of values from single list validator placed within a composite validator; */
std::set<std::string> allowedValues() const
{
std::set<std::string> elem_unique;
std::multiset<std::string> elem_all;
// how many validators return non-empty list of allowed values
int n_combinations(0);
for (unsigned int i=0; i < m_children.size(); ++i)
{
std::set<std::string> subs = m_children[i]->allowedValues();
if ( subs.empty())continue;
elem_unique.insert(subs.begin(),subs.end());
elem_all.insert(subs.begin(),subs.end());
n_combinations++;
}


// empty or single set of allowed values
if(n_combinations<2)return elem_unique;
// there is more then one combination and we have to identify its union;
for(std::set<std::string>::const_iterator its=elem_unique.begin();its!=elem_unique.end();its++){
std::multiset<std::string>::iterator im = elem_all.find(*its);
elem_all.erase(im);
}
std::set<std::string> rez;
for(std::multiset<std::string>::const_iterator im=elem_all.begin();im!=elem_all.end();im++){
rez.insert(*im);
}
return rez;

}
// ------------------------------------------------------------------------------------
Kernel::IValidator<TYPE>* clone()
{
Expand Down
Expand Up @@ -43,7 +43,7 @@ class MANTID_KERNEL_DLL ListValidator : public IValidator<std::string>
ListValidator();
explicit ListValidator(const std::vector<std::string>& values);
explicit ListValidator(const std::set<std::string>& values);
virtual ~ListValidator();
virtual ~ListValidator();
IValidator<std::string>* clone();

std::set<std::string> allowedValues() const;
Expand Down
1 change: 0 additions & 1 deletion Code/Mantid/Framework/Kernel/src/CompositeValidator.cpp
Expand Up @@ -9,7 +9,6 @@ namespace Kernel
{



} // namespace Mantid
} // namespace Kernel

33 changes: 33 additions & 0 deletions Code/Mantid/Framework/Kernel/test/CompositeValidatorTest.h
Expand Up @@ -9,6 +9,7 @@

#include "MantidKernel/CompositeValidator.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/ListValidator.h"

using namespace Mantid;
using namespace Mantid::Kernel;
Expand All @@ -24,19 +25,51 @@ class CompositeValidatorTest : public CxxTest::TestSuite
BoundedValidator<int> * val2 = new BoundedValidator<int>(900, 2000);
CompositeValidator<int> comp;
comp.add(val1);
//
TS_ASSERT( comp.allowedValues().empty());

TS_ASSERT( comp.isValid(150).empty() );
TS_ASSERT( comp.isValid(950).empty() );
TS_ASSERT( !comp.isValid(1200).empty() );
comp.add(val2);
TS_ASSERT( !comp.isValid(150).empty() ); // This one is now blocked by validator 2
TS_ASSERT( comp.isValid(950).empty() );
TS_ASSERT( !comp.isValid(1200).empty() );
//
TS_ASSERT( comp.allowedValues().empty());

// Test cloning
IValidator<int> * comp2 = comp.clone();
TS_ASSERT( !comp2->isValid(150).empty() );
TS_ASSERT( comp2->isValid(950).empty() );

TS_ASSERT( comp2->allowedValues().empty());
}
void test_isListObtained()
{
std::vector<std::string> allowed_val1(3);
allowed_val1[0]="a1"; allowed_val1[1]="b2"; allowed_val1[2]="c";

ListValidator * val1 = new ListValidator(allowed_val1);
CompositeValidator<std::string> comp;
comp.add(val1);

std::set<std::string> allowed=comp.allowedValues();
TS_ASSERT_EQUALS(allowed_val1.size(),allowed.size());

std::vector<std::string> allowed_val2(3);
allowed_val2[0]="a2"; allowed_val2[1]="b2"; allowed_val2[2]="c2";

ListValidator * val2 = new ListValidator(allowed_val2);
comp.add(val2);

std::set<std::string> allowed2=comp.allowedValues();
TS_ASSERT_EQUALS(1,allowed2.size());
TS_ASSERT_EQUALS("b2",*(allowed2.begin()));


}


};

Expand Down

0 comments on commit 6b9f6e4

Please sign in to comment.