Improved HAK (added 'maxtime' property)
[libdai.git] / src / properties.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
9 */
10
11
12 #include <iostream>
13 #include <dai/properties.h>
14 #include <dai/exceptions.h>
15
16
17 namespace dai {
18
19
20 std::ostream& operator<< (std::ostream & os, const Property & p) {
21 os << p.first << "=";
22 if( p.second.type() == typeid(size_t) )
23 os << boost::any_cast<size_t>(p.second);
24 else if( p.second.type() == typeid(int) )
25 os << boost::any_cast<int>(p.second);
26 else if( p.second.type() == typeid(std::string) )
27 os << boost::any_cast<std::string>(p.second);
28 else if( p.second.type() == typeid(double) )
29 os << boost::any_cast<double>(p.second);
30 else if( p.second.type() == typeid(long double) )
31 os << boost::any_cast<long double>(p.second);
32 else if( p.second.type() == typeid(bool) )
33 os << boost::any_cast<bool>(p.second);
34 else if( p.second.type() == typeid(PropertySet) )
35 os << boost::any_cast<PropertySet>(p.second);
36 else
37 DAI_THROW(UNKNOWN_PROPERTY_TYPE);
38 return( os );
39 }
40
41
42 /// Writes a PropertySet object to an output stream
43 std::ostream& operator<< (std::ostream & os, const PropertySet & ps) {
44 os << "[";
45 for( PropertySet::const_iterator p = ps.begin(); p != ps.end(); p++ ) {
46 if( p != ps.begin() )
47 os << ",";
48 os << (Property)*p;
49 }
50 os << "]";
51 return os;
52 }
53
54
55 /// Reads a PropertySet object from an input stream, storing values as strings
56 std::istream& operator>> (std::istream& is, PropertySet & ps) {
57 ps = PropertySet();
58
59 std::string s;
60 is >> s;
61
62 // Check whether s is of the form "[.*]"
63 if( (s.length() < 2) || (s.at(0) != '[') || (s.at(s.length()-1)) != ']' )
64 DAI_THROWE(MALFORMED_PROPERTY,"Malformed PropertySet: " + s);
65
66 size_t N = s.length() - 1;
67 for( size_t token_start = 1; token_start < N; ) {
68 size_t token_end;
69
70 // scan until '=' is found
71 for( token_end = token_start + 1; token_end < N; token_end++ )
72 if( s[token_end] == '=' )
73 break;
74 // we found a key
75 std::string key = s.substr(token_start, token_end - token_start);
76 if( token_end == N )
77 DAI_THROWE(MALFORMED_PROPERTY,"Malformed Property: " + key);
78
79 token_start = token_end + 1;
80 // scan until matching ',' is found
81 int level = 0;
82 for( token_end = token_start; token_end < N; token_end++ ) {
83 if( s[token_end] == '[' )
84 level++;
85 else if( s[token_end] == ']' )
86 level--;
87 else if( (s[token_end] == ',') && (level == 0) )
88 break;
89 }
90 if( !(level == 0) )
91 DAI_THROWE(MALFORMED_PROPERTY,"Malformed Property: " + s.substr(token_start, token_end - token_start));
92 // we found a vlue
93 std::string value = s.substr(token_start, token_end - token_start);
94
95 // store the key,value pair
96 ps.set(key,value);
97
98 // go on with the next one
99 token_start = token_end + 1;
100 }
101
102 return is;
103 }
104
105
106 } // end of namespace dai