Skip to content

Commit

Permalink
refs #5626. New search features in parammap.
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenArnold committed Jul 19, 2012
1 parent c018b89 commit 9ef93b8
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,15 @@ namespace Geometry
/// Get a parameter with a given name and optional type
boost::shared_ptr<Parameter> get(const IComponent* comp,const std::string& name,
const std::string & type = "")const;
/// Finds the parameter in the map via the parameter type.
boost::shared_ptr<Parameter> ParameterMap::getByType(const IComponent* comp, const std::string& type) const;
/// Use get() recursively to see if can find param in all parents of comp.
boost::shared_ptr<Parameter> getRecursive(const IComponent* comp, const char * name) const;
/// Use get() recursively to see if can find param in all parents of comp and given type
boost::shared_ptr<Parameter> getRecursive(const IComponent* comp,const std::string& name,
const std::string & type = "")const;
/// Looks recursively upwards in the component tree for the first instance of a parameter with a specified type.
boost::shared_ptr<Parameter> getRecursiveByType(const IComponent* comp, const std::string& type) const;

/** Get the values of a given parameter of all the components that have the name: compName
* @tparam The parameter type
Expand Down
55 changes: 55 additions & 0 deletions Code/Mantid/Framework/Geometry/src/Instrument/ParameterMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,39 @@ namespace Mantid
return result;
}

/** Look for a parameter in the given component by the type of the parameter.
* @param comp :: Component to which parameter is related
* @param type :: Parameter type
* @returns The typed parameter if it exists or a NULL shared pointer if not
*/
Parameter_sptr ParameterMap::getByType(const IComponent* comp, const std::string& type) const
{
Parameter_sptr result = Parameter_sptr();
PARALLEL_CRITICAL(ParameterMap_get)
{
if( !m_map.empty() )
{
const ComponentID id = comp->getComponentID();
pmap_cit it_found = m_map.find(id);
if( it_found->first && it_found != m_map.end() )
{
pmap_cit itr = m_map.lower_bound(id);
pmap_cit itr_end = m_map.upper_bound(id);
for( ; itr != itr_end; ++itr )
{
Parameter_sptr param = itr->second;
if( boost::iequals(param->type(), type) )
{
result = param;
break;
}
}
}
}
}
return result;
}

/** FASTER LOOKUP in multithreaded loops. Find a parameter by name, recursively going up
* the component tree to higher parents.
* @param comp :: The component to start the search with
Expand All @@ -451,6 +484,28 @@ namespace Mantid
return Parameter_sptr();
}

/** Looks recursively upwards in the component tree for the first instance of a component with a matching type.
* @param comp :: The component to start the search with
* @param type :: Parameter type
* @returns the first matching parameter.
*/
Parameter_sptr ParameterMap::getRecursiveByType(const IComponent* comp, const std::string& type) const
{
boost::shared_ptr<const IComponent> compInFocus(comp,NoDeleting());
while( compInFocus != NULL )
{
Parameter_sptr param = getByType(compInFocus.get(), type);
if (param)
{
return param;
}
compInFocus = compInFocus->getParent();
}
//Nothing was found!
return Parameter_sptr();
}


/**
* Find a parameter by name, recursively going up the component tree
* to higher parents.
Expand Down
91 changes: 91 additions & 0 deletions Code/Mantid/Framework/Geometry/test/ParameterMapTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,97 @@ class ParameterMapTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS(pmap.size(), 0);
}

void test_lookup_via_type_returns_null_if_fails()
{
// Add a parameter for the first component of the instrument
IComponent_sptr comp = m_testInstrument->getChild(0);
// Create the parameter map with a single boolean type.
ParameterMap pmap;
TS_ASSERT_EQUALS(pmap.size(), 0);
pmap.addBool(comp.get(), "A", true);
TS_ASSERT_EQUALS(pmap.size(), 1);
// Try to find double type parameters, of which there should be none.
Parameter_sptr fetchedValue = pmap.getByType(comp.get(), "double");
TSM_ASSERT("Should not be able to find a double type parameter", fetchedValue == NULL);
}

void test_lookup_via_type()
{
// Add a parameter for the first component of the instrument
IComponent_sptr comp = m_testInstrument->getChild(0);
// Create the parameter map and add some new parameters.
ParameterMap pmap;
TS_ASSERT_EQUALS(pmap.size(), 0);
pmap.addDouble(comp.get(), "A", 1.2);
pmap.addBool(comp.get(), "B", true);
TS_ASSERT_EQUALS(pmap.size(), 2);

// Test the ability to correctly fetch the double argument by type.
Parameter_sptr fetchedValue1 = pmap.getByType(comp.get(), "double");
TS_ASSERT(fetchedValue1);
TS_ASSERT_EQUALS("A", fetchedValue1->name());
TS_ASSERT_DELTA(1.2, fetchedValue1->value<double>(), DBL_EPSILON);

// Test the ability to correctly fetch the bool argument by type.
Parameter_sptr fetchedValue2 = pmap.getByType(comp.get(), "bool");
TS_ASSERT(fetchedValue2);
TS_ASSERT_EQUALS("B", fetchedValue2->name());
TS_ASSERT_EQUALS(true, fetchedValue2->value<bool>());
}

void test_lookup_recursive_by_type_finds_on_current()
{
IComponent_sptr component = m_testInstrument;

//Add something to the parent component ONLY.
ParameterMap pmap;
pmap.addBool(component.get(), "A", true);

//Find it via the component
Parameter_sptr fetchedValue = pmap.getRecursiveByType(component.get(), "bool");
TS_ASSERT(fetchedValue != NULL);
TS_ASSERT_EQUALS("A", fetchedValue->name());
TS_ASSERT_EQUALS("bool", fetchedValue->type());
TS_ASSERT_EQUALS(true, fetchedValue->value<bool>());
}

void test_lookup_recursive_by_type_finds_on_parent_if_not_on_current()
{
IComponent_sptr childComponent = m_testInstrument->getChild(0);
IComponent_sptr parentComponent = m_testInstrument;

//Add something to the parent component ONLY.
ParameterMap pmap;
pmap.addBool(parentComponent.get(), "A", true);

//Find it via the child
Parameter_sptr fetchedValue = pmap.getRecursiveByType(childComponent.get(), "bool");
TS_ASSERT(fetchedValue != NULL);
TS_ASSERT_EQUALS("A", fetchedValue->name());
TS_ASSERT_EQUALS("bool", fetchedValue->type());
TS_ASSERT_EQUALS(true, fetchedValue->value<bool>());
}

void test_lookup_recursive_by_type_finds_on_current_in_preference_to_parent()
{
IComponent_sptr childComponent = m_testInstrument->getChild(0);
IComponent_sptr parentComponent = m_testInstrument;

//Add something to the child component.
ParameterMap pmap;
pmap.addBool(childComponent.get(), "A", false);

//Add something with the SAME TYPE TO THE PARENT TOO.
pmap.addBool(parentComponent.get(), "B", true);

//Find it via the child
Parameter_sptr fetchedValue = pmap.getRecursiveByType(childComponent.get(), "bool");
TS_ASSERT(fetchedValue != NULL);
TSM_ASSERT_EQUALS("Has not searched through parameters with the correct priority", "A", fetchedValue->name());
TSM_ASSERT_EQUALS("Has not searched through parameters with the correct priority","bool", fetchedValue->type());
TSM_ASSERT_EQUALS("Has not searched through parameters with the correct priority",false, fetchedValue->value<bool>());
}

private:
Instrument_sptr m_testInstrument;
};
Expand Down

0 comments on commit 9ef93b8

Please sign in to comment.