From 6178193377089a88b025f335b724650d31de8aa6 Mon Sep 17 00:00:00 2001
From: Stuart Campbell <campbellsi@ornl.gov>
Date: Thu, 7 Apr 2011 18:33:08 +0000
Subject: [PATCH] Added getInstrument() to ConfigService instance. refs #2760

---
 .../Kernel/inc/MantidKernel/ConfigService.h   |  4 ++
 .../Framework/Kernel/src/ConfigService.cpp    | 46 +++++++++++++++++++
 .../Framework/Kernel/src/FacilityInfo.cpp     |  2 +-
 .../Framework/Kernel/test/ConfigServiceTest.h | 39 ++++++++++++++++
 4 files changed, 90 insertions(+), 1 deletion(-)

diff --git a/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h b/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h
index c27cb547749..bcdc7d70bb0 100644
--- a/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h
+++ b/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h
@@ -41,6 +41,7 @@ namespace Mantid
     //----------------------------------------------------------------------
     class Logger;
     class FacilityInfo;
+    class InstrumentInfo;
 
     /** The ConfigService class provides a simple facade to access the Configuration functionality of the Mantid Framework.
         The class gathers information from config files and the system variables.  
@@ -171,6 +172,9 @@ namespace Mantid
       /// Get a facility
       const FacilityInfo& Facility(const std::string& fName)const;
 
+      /// Look for an instrument
+      const InstrumentInfo & getInstrument(const std::string& instrumentName = "") const;
+
       /// Add an observer for a notification
       void addObserver(const Poco::AbstractObserver& observer)const;
 
diff --git a/Code/Mantid/Framework/Kernel/src/ConfigService.cpp b/Code/Mantid/Framework/Kernel/src/ConfigService.cpp
index 1e1ccda4448..696f584537d 100644
--- a/Code/Mantid/Framework/Kernel/src/ConfigService.cpp
+++ b/Code/Mantid/Framework/Kernel/src/ConfigService.cpp
@@ -1108,6 +1108,52 @@ void ConfigServiceImpl::updateFacilities(const std::string& fName)
 
 }
 
+/**
+ * Returns instruments with given name
+ * @param  iName Instrument name
+ * @return the instrument information object
+ * @throw NotFoundError if iName was not found
+ */
+const InstrumentInfo & ConfigServiceImpl::getInstrument(const std::string& instrumentName) const
+{
+
+  // TODO: Change this to use getFacility()
+  // Let's first search for the instrument in our default facility
+  std::string defaultFacility = ConfigService::Instance().getString("default.facility");
+
+  if (!defaultFacility.empty())
+  {
+    try
+    {
+      g_log.debug() << "Looking for " << instrumentName << " at " << defaultFacility << "." << std::endl;
+      return Facility(defaultFacility).Instrument(instrumentName);
+    }
+    catch (Exception::NotFoundError e)
+    {
+      // Well the instName doesn't exist for this facility
+      // Move along, there's nothing to see here...
+    }
+  }
+
+  // Now let's look through the other facilities
+  std::vector<FacilityInfo*>::const_iterator it = m_facilities.begin();
+  for (; it != m_facilities.end(); ++it)
+  {
+    try
+    {
+      g_log.debug() << "Looking for " << instrumentName << " at " << (**it).name() << "." << std::endl;
+      return (**it).Instrument(instrumentName);
+    }
+    catch (Exception::NotFoundError e)
+    {
+      // Well the instName doesn't exist for this facility...
+      // Move along, there's nothing to see here...
+    }
+  }
+  g_log.error("Instrument " + instrumentName + " not found");
+  throw Exception::NotFoundError("Instrument", instrumentName);
+}
+
 /** Get the default `
  * @return the facility information object
  */
diff --git a/Code/Mantid/Framework/Kernel/src/FacilityInfo.cpp b/Code/Mantid/Framework/Kernel/src/FacilityInfo.cpp
index 834d3a42747..5d4ca8d7835 100644
--- a/Code/Mantid/Framework/Kernel/src/FacilityInfo.cpp
+++ b/Code/Mantid/Framework/Kernel/src/FacilityInfo.cpp
@@ -178,7 +178,7 @@ const InstrumentInfo & FacilityInfo::Instrument(const std::string& iName)const
   {
     if (it->shortName() == iname)
     {
-      g_log.debug() << "Instrument " << iName << " found as " << it->name() << " at " << name() << "." << std::endl;
+      g_log.debug() << "Instrument '" << iName << "' found as " << it->name() << " at " << name() << "." << std::endl;
       return *it;
     }
   }
diff --git a/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h b/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h
index eb007429d4a..f26dd3f7b75 100644
--- a/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h
+++ b/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h
@@ -6,6 +6,7 @@
 #include "MantidKernel/ConfigService.h"
 #include "MantidKernel/Logger.h"
 #include "MantidKernel/TestChannel.h"
+#include "MantidKernel/InstrumentInfo.h"
 #include <Poco/Path.h>
 #include <Poco/File.h>
 #include <boost/shared_ptr.hpp>
@@ -93,6 +94,44 @@ public:
     
   }
 
+
+  void testInstrumentSearch()
+  {
+    // Set a default facility
+    //ConfigService::Instance().setFacility("SNS");
+
+    // Try and find some instruments from a facility
+    TS_ASSERT_EQUALS(ConfigService::Instance().getInstrument("BASIS").name(),"BASIS");
+    TS_ASSERT_EQUALS(ConfigService::Instance().getInstrument("REF_L").name(),"REF_L");
+
+    // Now find some from other facilities
+    TS_ASSERT_EQUALS(ConfigService::Instance().getInstrument("OSIRIS").name(),"OSIRIS");
+    TS_ASSERT_EQUALS(ConfigService::Instance().getInstrument("BIOSANS").name(),"BIOSANS");
+    TS_ASSERT_EQUALS(ConfigService::Instance().getInstrument("NGSANS").name(),"NGSANS");
+
+    // Check we throw the correct error for a nonsense beamline.
+    //TS_ASSERT_THROWS(ConfigService::Instance().getInstrument("MyBeamline").name(), NotFoundError);
+
+    // Now find by using short name
+    TS_ASSERT_EQUALS(ConfigService::Instance().getInstrument("BSS").name(), "BASIS");
+    TS_ASSERT_EQUALS(ConfigService::Instance().getInstrument("MAR").name(), "MARI");
+    TS_ASSERT_EQUALS(ConfigService::Instance().getInstrument("PG3").name(), "POWGEN");
+    TS_ASSERT_EQUALS(ConfigService::Instance().getInstrument("OSI").name(), "OSIRIS");
+//    TS_ASSERT_EQUALS(ConfigService::Instance().getInstrument("HiResSANS").name(), "GPSANS");
+
+    // Now find some with the wrong case
+    TS_ASSERT_EQUALS(ConfigService::Instance().getInstrument("baSis").name(),"BASIS");
+    TS_ASSERT_EQUALS(ConfigService::Instance().getInstrument("TOPaZ").name(),"TOPAZ");
+    TS_ASSERT_EQUALS(ConfigService::Instance().getInstrument("Seq").name(),"SEQUOIA");
+    TS_ASSERT_EQUALS(ConfigService::Instance().getInstrument("eqsans").name(),"EQ-SANS");
+
+    // Set the default instrument
+    ConfigService::Instance().setString("default.instrument", "OSIRIS");
+    TS_ASSERT_EQUALS(ConfigService::Instance().getInstrument("").name(), "OSIRIS");
+    TS_ASSERT_EQUALS(ConfigService::Instance().getInstrument().name(), "OSIRIS");
+
+  }
+
   void TestSystemValues()
   {
     //we cannot test the return values here as they will differ based on the environment.
-- 
GitLab