Various fixes
authorJoris Mooij <joris.mooij@tuebingen.mpg.de>
Mon, 7 Sep 2009 14:30:51 +0000 (16:30 +0200)
committerJoris Mooij <joris.mooij@tuebingen.mpg.de>
Mon, 7 Sep 2009 14:30:51 +0000 (16:30 +0200)
include/dai/exceptions.h
include/dai/factorgraph.h
include/dai/prob.h
include/dai/properties.h
src/exceptions.cpp

index e13120f..bf5c338 100644 (file)
@@ -88,12 +88,13 @@ class Exception : public std::runtime_error {
                    INVALID_EVIDENCE_FILE,
                    INVALID_EMALG_FILE,
                    UNKNOWN_PARAMETER_ESTIMATION_METHOD,
+                   OBJECT_NOT_FOUND,
                    NUM_ERRORS};  // NUM_ERRORS should be the last entry
 
         /// Constructor
         Exception( Code _code, const std::string& msg="", const std::string& detailedMsg="" ) : std::runtime_error(ErrorStrings[_code] + " [" +  msg + "]"), errorcode(_code) { 
             if( !detailedMsg.empty() ) 
-                std::cerr << "EXCEPTION: " << detailedMsg << std::endl; 
+                std::cerr << "ERROR: " << detailedMsg << std::endl; 
         }
         
         /// Copy constructor
index 8ca8ef6..fc88e4d 100644 (file)
@@ -159,7 +159,8 @@ class FactorGraph {
         /// Returns the index of a particular variable
         size_t findVar( const Var &n ) const {
             size_t i = find( vars().begin(), vars().end(), n ) - vars().begin();
-            assert( i != nrVars() );
+            if( i == nrVars() )
+                DAI_THROW(OBJECT_NOT_FOUND);
             return i;
         }
 
@@ -172,12 +173,13 @@ class FactorGraph {
         }
 
         /// Returns index of the first factor that depends on the variables
-        size_t findFactor(const VarSet &ns) const {
+        size_t findFactor( const VarSet &ns ) const {
             size_t I;
             for( I = 0; I < nrFactors(); I++ )
                 if( factor(I).vars() == ns )
                     break;
-            assert( I != nrFactors() );
+            if( I == nrFactors() )
+                DAI_THROW(OBJECT_NOT_FOUND);
             return I;
         }
 
index ed3a932..d7e483f 100644 (file)
@@ -261,6 +261,11 @@ template <typename T> class TProb {
         TProb<T>& operator-= (const TProb<T> & q) {
             DAI_DEBASSERT( size() == q.size() );
             std::transform( _p.begin(), _p.end(), q._p.begin(), _p.begin(), std::minus<T>() );
+            return *this;
+        }
+
+        /// Return *this minus q (sizes should be identical)
+        Prob<T> operator- (const TProb<T> & q) const {
             DAI_DEBASSERT( size() == q.size() );
             TProb<T> diff( *this );
             diff -= q;
index 00efd07..47e43cb 100644 (file)
@@ -98,7 +98,7 @@ class PropertySet : private std::map<PropertyKey, PropertyValue> {
             try {
                 return boost::any_cast<ValueType>(Get(key));
             } catch( const boost::bad_any_cast & ) {
-                DAI_THROWE(IMPOSSIBLE_TYPECAST,"Cannot cast value of property " + key + " to desired type.");
+                DAI_THROWE(IMPOSSIBLE_TYPECAST,"Cannot cast value of property '" + key + "' to desired type.");
                 return ValueType();
             }
         }
@@ -112,7 +112,7 @@ class PropertySet : private std::map<PropertyKey, PropertyValue> {
                 try {
                     Set(key, boost::lexical_cast<ValueType>(GetAs<std::string>(key)));
                 } catch(boost::bad_lexical_cast &) {
-                    DAI_THROWE(IMPOSSIBLE_TYPECAST,"Cannot cast value of property " + key + " from string to desired type.");
+                    DAI_THROWE(IMPOSSIBLE_TYPECAST,"Cannot cast value of property '" + key + "' from string to desired type.");
                 }
             }
         }
@@ -127,10 +127,10 @@ class PropertySet : private std::map<PropertyKey, PropertyValue> {
                 try {
                     return boost::lexical_cast<ValueType>(GetAs<std::string>(key));
                 } catch(boost::bad_lexical_cast &) {
-                    DAI_THROWE(IMPOSSIBLE_TYPECAST,"Cannot cast value of property " + key + " from string to desired type.");
+                    DAI_THROWE(IMPOSSIBLE_TYPECAST,"Cannot cast value of property '" + key + "' from string to desired type.");
                 }
             } else
-                DAI_THROWE(IMPOSSIBLE_TYPECAST,"Cannot cast value of property " + key + " from string to desired type.");
+                DAI_THROWE(IMPOSSIBLE_TYPECAST,"Cannot cast value of property '" + key + "' from string to desired type.");
             return ValueType();
         }
 
@@ -140,7 +140,7 @@ class PropertySet : private std::map<PropertyKey, PropertyValue> {
             try {
                 return Set( key, boost::lexical_cast<std::string>(val) );
             } catch( boost::bad_lexical_cast & ) {
-                DAI_THROWE(IMPOSSIBLE_TYPECAST,"Cannot cast value of property " + key + " to string.");
+                DAI_THROWE(IMPOSSIBLE_TYPECAST,"Cannot cast value of property '" + key + "' to string.");
             }
         }
 
index 3530c32..b39353e 100644 (file)
@@ -44,7 +44,8 @@ namespace dai {
         "Quantity not normalizable",
         "Invalid Evidence file",
         "Invalid Expectation-Maximization file",
-        "Unrecognized parameter estimation method"
+        "Unrecognized parameter estimation method",
+        "Requested object not found"
     };