Reranker Framework (ReFr)
Reranking framework for structure prediction and discriminative language modeling
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
environment-impl.H
Go to the documentation of this file.
1 // Copyright 2012, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 // -----------------------------------------------------------------------------
30 //
31 //
36 
37 #ifndef RERANKER_ENVIRONMENT_IMPL_H_
38 #define RERANKER_ENVIRONMENT_IMPL_H_
39 
40 #include <sstream>
41 #include <string>
42 #include <unordered_map>
43 #include <unordered_set>
44 #include <typeinfo>
45 
46 #include "environment.H"
47 
48 namespace reranker {
49 
50 using std::ostringstream;
51 using std::string;
52 using std::unordered_map;
53 using std::unordered_set;
54 
59 class EnvironmentImpl : public Environment {
60  public:
65  EnvironmentImpl(int debug = 0);
66 
68  virtual ~EnvironmentImpl() {
69  for (unordered_map<string, VarMapBase *>::iterator it = var_map_.begin();
70  it != var_map_.end(); ++it) {
71  delete it->second;
72  }
73  }
74 
77  virtual bool Defined(const string &varname) const {
78  unordered_map<string, string>::const_iterator it = types_.find(varname);
79  return it != types_.end();
80  }
81 
84  virtual void ReadAndSet(const string &varname, StreamTokenizer &st,
85  const string type);
86 
87  virtual const string &GetType(const string &varname) const {
88  unordered_map<string, string>::const_iterator type_it =
89  types_.find(varname);
90  if (type_it == types_.end()) {
91  // Error or warning.
92  }
93  return type_it->second;
94  }
95 
96  virtual VarMapBase *GetVarMap(const string &varname) {
97  return GetVarMapForType(GetType(varname));
98  }
99 
101  virtual VarMapBase *GetVarMapForType(const string &type) {
102  string lookup_type = type;
103  // First, check if this is a concrete Factory-constructible type.
104  // If so, map to its abstract type name.
105  unordered_map<string, string>::const_iterator factory_type_it =
106  concrete_to_factory_type_.find(type);
107  if (factory_type_it != concrete_to_factory_type_.end()) {
108  lookup_type = factory_type_it->second;
109  }
110 
111  unordered_map<string, VarMapBase *>::const_iterator var_map_it =
112  var_map_.find(lookup_type);
113  if (var_map_it == var_map_.end()) {
114  return NULL;
115  }
116  return var_map_it->second;
117  }
118 
120  virtual void Print(ostream &os) const {
121  for (unordered_map<string, VarMapBase *>::const_iterator var_map_it =
122  var_map_.begin();
123  var_map_it != var_map_.end(); ++var_map_it) {
124  var_map_it->second->Print(os);
125  }
126  }
127 
129  virtual void PrintFactories(ostream &os) const;
130 
132  virtual Environment *Copy() const {
133  EnvironmentImpl *new_env = new EnvironmentImpl(*this);
134  // Now go through and create copies of each VarMap.
135  for (unordered_map<string, VarMapBase *>::iterator new_env_var_map_it =
136  new_env->var_map_.begin();
137  new_env_var_map_it != new_env->var_map_.end(); ++new_env_var_map_it) {
138  new_env_var_map_it->second = new_env_var_map_it->second->Copy(new_env);
139  }
140  return new_env;
141  }
142 
151  template<typename T>
152  bool Get(const string &varname, T *value) const;
153 
154  private:
156  string InferType(const string &varname,
157  const StreamTokenizer &st, bool is_vector,
158  bool *is_object_type);
159 
161  unordered_map<string, string> types_;
162 
165  unordered_map<string, VarMapBase *> var_map_;
166 
169  unordered_map<string, string> concrete_to_factory_type_;
170 
171  int debug_;
172 };
173 
174 template<typename T>
175 bool
176 EnvironmentImpl::Get(const string &varname, T *value) const {
177  unordered_map<string, string>::const_iterator type_it =
178  types_.find(varname);
179  if (type_it == types_.end()) {
180  if (debug_ >= 1) {
181  ostringstream err_ss;
182  err_ss << "Environment::Get: error: no value for variable "
183  << varname;
184  cerr << err_ss.str() << endl;
185  }
186  return false;
187  }
188 
189  // Now that we have the type, look up the VarMap.
190  const string &type = type_it->second;
191  unordered_map<string, VarMapBase*>::const_iterator var_map_it =
192  var_map_.find(type);
193 
194  if (var_map_it == var_map_.end()) {
195  ostringstream err_ss;
196  err_ss << "Environment::Get: error: types_ and var_map_ data members "
197  << "are out of sync";
198  throw std::runtime_error(err_ss.str());
199  }
200 
201  // Do a dynamic_cast down to the type-specific VarMap.
202  VarMapBase *var_map = var_map_it->second;
203  VarMap<T> *typed_var_map = dynamic_cast<VarMap<T> *>(var_map);
204 
205  if (typed_var_map == NULL) {
206  ostringstream err_ss;
207  err_ss << "Environment::Get: error: no value for variable "
208  << varname << " of type " << typeid(*value).name()
209  << "; perhaps you meant " << type << "?";
210  cerr << err_ss.str() << endl;
211  return false;
212  }
213  bool success = typed_var_map->Get(varname, value);
214  if (!success) {
215  ostringstream err_ss;
216  err_ss << "Environment::Get: error: no value for variable "
217  << varname << " of type " << typeid(*value).name()
218  << "; types_ and var_map_ data members are out of sync";
219  throw std::runtime_error(err_ss.str());
220  }
221  return success;
222  }
223 
224 } // namespace reranker
225 
226 #endif
A container to hold the mapping between named variables of a specific type and their values...
Definition: environment.H:256
A simple class for tokenizing a stream of tokens for the formally specified language used to construc...
virtual void ReadAndSet(const string &varname, StreamTokenizer &st, const string type)
Sets the specified variable to the value obtained from the following tokens available from the specif...
bool Get(const string &varname, T *value) const
Assigns the value of the specified variable to the object pointed to by the value parameter...
Definition: environment.H:277
virtual ~EnvironmentImpl()
Destroys this environment.
A base class for a mapping from variables of a specific type to their values.
Definition: environment.H:65
virtual void PrintFactories(ostream &os) const
Prints out a human-readable string with the names of all abstract base types and their concrete imple...
virtual const string & GetType(const string &varname) const
Retrieves the type name of the specified variable.
Provides an interface for an Environment, which contains a map from variables of a specific type (pri...
virtual void Print(ostream &os) const
Prints a human-readable string of all the variables in this environment, their types and...
virtual Environment * Copy() const
Returns a copy of this environment.
EnvironmentImpl(int debug=0)
Constructs a new, empty environment.
bool Get(const string &varname, T *value) const
Retrieves the value of the variable with the specified name and puts into into the object pointed to ...
Provides a set of named variables and their types, as well as the values for those variables...
An interface for an environment in which variables of various types are mapped to their values...
Definition: environment.H:125
virtual VarMapBase * GetVarMap(const string &varname)
Retrieves the VarMap instance for the specified variable.
virtual VarMapBase * GetVarMapForType(const string &type)
Retrieves the VarMap instance for the specified type.
virtual bool Defined(const string &varname) const
Returns whether the specified variable has been defined in this environment.