9539d5c6642bf62e045494532f4bc83bf191976e
[libdai.git] / include / dai / properties.h
1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
4
5 This file is part of libDAI.
6
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 /// \file
24 /// \brief Defines the Property and PropertySet classes
25 /// \todo Improve documentation
26
27
28 #ifndef __defined_libdai_properties_h
29 #define __defined_libdai_properties_h
30
31
32 #include <iostream>
33 #include <sstream>
34 #include <boost/any.hpp>
35 #include <map>
36 #include <vector>
37 #include <cassert>
38 #include <typeinfo>
39 #include <dai/exceptions.h>
40 #include <dai/util.h>
41
42
43 namespace dai {
44
45
46 /// Type of the key of a Property
47 typedef std::string PropertyKey;
48
49 /// Type of the value of a Property
50 typedef boost::any PropertyValue;
51
52 /// A Property is a pair of a key and a corresponding value
53 typedef std::pair<PropertyKey, PropertyValue> Property;
54
55
56 /// Writes a Property object to an output stream
57 std::ostream& operator<< (std::ostream & os, const Property & p);
58
59
60 /// Represents a set of properties, mapping keys (of type PropertyKey) to values (of type PropertyValue)
61 class PropertySet : private std::map<PropertyKey, PropertyValue> {
62 public:
63 /// Default constructor
64 PropertySet() {}
65
66 /// Construct PropertySet from a string
67 PropertySet( const std::string &s ) {
68 std::stringstream ss;
69 ss << s;
70 ss >> *this;
71 }
72
73 /// Gets a property
74 const PropertyValue & Get(const PropertyKey &key) const {
75 PropertySet::const_iterator x = find(key);
76 #ifdef DAI_DEBUG
77 if( x == this->end() )
78 std::cerr << "PropertySet::Get cannot find property " << key << std::endl;
79 #endif
80 assert( x != this->end() );
81 return x->second;
82 }
83
84 /// Sets a property
85 PropertySet & Set(const PropertyKey &key, const PropertyValue &val) { this->operator[](key) = val; return *this; }
86
87 /// Set properties according to those in newProps, overriding properties that already exist with new values
88 PropertySet & Set( const PropertySet &newProps ) {
89 const std::map<PropertyKey, PropertyValue> *m = &newProps;
90 foreach(value_type i, *m) {
91 Set(i.first, i.second);
92 }
93 return *this;
94 }
95
96 /// Gets a property, casted as ValueType
97 template<typename ValueType>
98 ValueType GetAs(const PropertyKey &key) const {
99 try {
100 return boost::any_cast<ValueType>(Get(key));
101 } catch( const boost::bad_any_cast & ) {
102 std::cerr << "Cannot cast property " << key << " to ";
103 std::cerr << typeid(ValueType).name() << std::endl;
104 return boost::any_cast<ValueType>(Get(key));
105 }
106 }
107
108 /// Converts a property from string to ValueType (if necessary)
109 template<typename ValueType>
110 void ConvertTo(const PropertyKey &key) {
111 PropertyValue val = Get(key);
112 if( val.type() != typeid(ValueType) ) {
113 assert( val.type() == typeid(std::string) );
114
115 std::stringstream ss;
116 ss << GetAs<std::string>(key);
117 ValueType result;
118 ss >> result;
119
120 Set(key, result);
121 }
122 }
123
124 /// Converts a property from string to ValueType (if necessary)
125 template<typename ValueType>
126 ValueType getStringAs(const PropertyKey &key) const {
127 PropertyValue val = Get(key);
128 if( val.type() == typeid(ValueType) ) {
129 return boost::any_cast<ValueType>(val);
130 } else if( val.type() == typeid(std::string) ) {
131 std::stringstream ss;
132 ss << GetAs<std::string>(key);
133 ValueType result;
134 ss >> result;
135 return result;
136 } else {
137 DAI_THROW(IMPOSSIBLE_TYPECAST);
138 return ValueType();
139 }
140 }
141
142 /// Converts a property from ValueType to string (if necessary)
143 template<typename ValueType>
144 PropertySet & setAsString(const PropertyKey &key, ValueType &val) {
145 if( val.type() == typeid(std::string) ) {
146 return Set(key, val);
147 } else {
148 std::stringstream ss (std::stringstream::out);
149 ss << val;
150 return Set(key, ss.str());
151 }
152 }
153
154 /// Shorthand for (temporarily) adding properties, e.g. PropertySet p()("method","BP")("verbose",1)("tol",1e-9)
155 PropertySet operator()(const PropertyKey &key, const PropertyValue &val) const { PropertySet copy = *this; return copy.Set(key,val); }
156
157 std::vector< PropertyKey > keys() const {
158 std::vector< PropertyKey > result;
159 result.reserve(size());
160 PropertySet::const_iterator i = begin();
161 for ( ; i != end(); ++i) {
162 result.push_back(i->first);
163 }
164 return result;
165 }
166
167 /// Check if a property with the given key exists
168 bool hasKey(const PropertyKey &key) const { PropertySet::const_iterator x = find(key); return (x != this->end()); }
169
170 /// Returns a set containing all keys
171 std::set<PropertyKey> allKeys() const {
172 std::set<PropertyKey> res;
173 const_iterator i;
174 for( i = begin(); i != end(); i++ )
175 res.insert( i->first );
176 return res;
177 }
178
179 // Friends
180 friend std::ostream& operator<< (std::ostream & os, const PropertySet & ps);
181 friend std::istream& operator>> (std::istream& is, PropertySet & ps);
182 };
183
184
185 } // end of namespace dai
186
187
188 #endif