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.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 //
38 
39 #ifndef RERANKER_ENVIRONMENT_H_
40 #define RERANKER_ENVIRONMENT_H_
41 
42 #define VAR_MAP_DEBUG 1
43 
44 #include <sstream>
45 #include <vector>
46 
47 #include "stream-init.H"
48 #include "stream-tokenizer.H"
49 
50 namespace reranker {
51 
52 using std::cerr;
53 using std::endl;
54 using std::ostream;
55 using std::ostringstream;
56 using std::shared_ptr;
57 using std::unordered_map;
58 using std::unordered_set;
59 using std::vector;
60 
61 class Environment;
62 
65 class VarMapBase {
66  public:
75  VarMapBase(const string &name, Environment *env, bool is_primitive) :
76  name_(name), env_(env), is_primitive_(is_primitive) { }
77 
78  virtual ~VarMapBase() { }
79 
82  virtual bool IsPrimitive() const { return is_primitive_; }
83 
85  virtual const string &Name() const { return name_; }
86 
89  virtual bool Defined(const string &varname) const = 0;
90 
95  virtual void ReadAndSet(const string &varname, StreamTokenizer &st) = 0;
96 
100  virtual void Print(ostream &os) const = 0;
101 
103  virtual VarMapBase *Copy(Environment *env) const = 0;
104 
105  protected:
108  void SetMembers(const string &name, Environment *env, bool is_primitive) {
109  name_ = name;
110  env_ = env;
111  is_primitive_ = is_primitive;
112  }
113 
115  string name_;
121 };
122 
125 class Environment {
126  public:
127  virtual ~Environment() { }
128 
131  virtual bool Defined(const string &varname) const = 0;
132 
135  virtual void ReadAndSet(const string &varname, StreamTokenizer &st,
136  const string type = "") = 0;
137 
139  virtual const string &GetType(const string &varname) const = 0;
140 
142  virtual VarMapBase *GetVarMap(const string &varname) = 0;
143 
162  virtual VarMapBase *GetVarMapForType(const string &type) = 0;
163 
166  virtual void Print(ostream &os) const = 0;
167 
169  virtual Environment *Copy() const = 0;
170 
174  virtual void PrintFactories(ostream &os) const = 0;
175 
177  static Environment *CreateEmpty();
178 };
179 
184 template <typename T>
185 class ValueString {
186  public:
187  string ToString(const T &value) const {
188  ostringstream oss;
189  oss << value;
190  return oss.str();
191  }
192 };
193 
196 template<>
197 class ValueString<string> {
198  public:
199  string ToString(const string &value) const {
200  return "\"" + value + "\"";
201  }
202 };
203 
206 template<>
207 class ValueString<bool> {
208  public:
209  string ToString(const bool &value) const {
210  return value ? "true" : "false";
211  }
212 };
213 
217 template<typename T>
218 class ValueString<shared_ptr<T> > {
219  public:
220  string ToString(const shared_ptr<T> &value) const {
221  ostringstream oss;
222  oss << "<" << typeid(shared_ptr<T>).name() << ":" << value.get() << ">";
223  return oss.str();
224  }
225 };
226 
231 template <typename T>
232 class ValueString<vector<T> > {
233  public:
234  string ToString(const vector<T> &value) const {
235  ostringstream oss;
236  oss << "{";
237  typename vector<T>::const_iterator it = value.begin();
238  ValueString<T> value_string;
239  if (it != value.end()) {
240  oss << value_string.ToString(*it);
241  ++it;
242  }
243  for (; it != value.end(); ++it) {
244  oss << ", " << value_string.ToString(*it);
245  }
246  oss << "}";
247  return oss.str();
248  }
249 };
250 
255 template <typename T>
256 class VarMap : public VarMapBase {
257  public:
265  VarMap(const string &name, Environment *env, bool is_primitive = true) :
266  VarMapBase(name, env, is_primitive) { }
267 
268  virtual ~VarMap() { }
269 
270  // Accessor methods
271 
277  bool Get(const string &varname, T *value) const {
278  typename unordered_map<string, T>::const_iterator it = vars_.find(varname);
279  if (it == vars_.end()) {
280  return false;
281  } else {
282  *value = it->second;
283  return true;
284  }
285  }
286 
288  virtual bool Defined(const string &varname) const {
289  return vars_.find(varname) != vars_.end();
290  }
291 
293  void Set(const string &varname, T value) {
294  vars_[varname] = value;
295  }
296 
298  virtual void ReadAndSet(const string &varname, StreamTokenizer &st) {
299  if (VAR_MAP_DEBUG >= 1) {
300  cerr << "VarMap<" << Name() << ">::ReadAndSet: about to set varname "
301  << varname << " of type " << typeid(T).name() << endl;
302  }
303 
304  // First check if next token is an identifier and is a variable in
305  // the environment, set varname to its value.
306  if (env_->Defined(st.Peek())) {
307  VarMapBase *var_map = env_->GetVarMap(st.Peek());
308  VarMap<T> *typed_var_map = dynamic_cast<VarMap<T> *>(var_map);
309  if (typed_var_map != NULL) {
310  // Finally consume variable.
311  string rhs_variable = st.Next();
312  T value = T();
313  // Retrieve rhs variable's value.
314  bool success = typed_var_map->Get(rhs_variable, &value);
315  // Set varname to the same value.
316  if (VAR_MAP_DEBUG >= 1) {
317  cerr << "VarMap<" << Name() << ">::ReadAndSet: setting variable "
318  << varname << " to same value as rhs variable " << rhs_variable
319  << endl;
320  }
321  if (success) {
322  Set(varname, value);
323  } else {
324  // Error: we couldn't find the varname in this VarMap.
325  }
326  } else {
327  // Error: inferred or declared type of varname is different
328  // from the type of the rhs variable.
329  }
330  } else {
331  T value;
332  Initializer<T> initializer(&value);
333  initializer.Init(st, env_);
334  Set(varname, value);
335 
336  if (VAR_MAP_DEBUG >= 1) {
337  ValueString<T> value_string;
338  cerr << "VarMap<" << Name() << ">::ReadAndSet: set varname "
339  << varname << " to value " << value_string.ToString(value)<< endl;
340  }
341  }
342  }
343 
345  virtual void Print(ostream &os) const {
346  ValueString<T> value_string;
347  for (typename unordered_map<string, T>::const_iterator it = vars_.begin();
348  it != vars_.end(); ++it) {
349  const T& value = it->second;
350  os << Name() << " "
351  << it->first
352  << " = "
353  << value_string.ToString(value)
354  << ";\n";
355  }
356  os.flush();
357  }
358 
360  virtual VarMapBase *Copy(Environment *env) const {
361  VarMap<T> *var_map_copy = new VarMap<T>(*this);
362  var_map_copy->SetMembers(name_, env, is_primitive_);
363  return var_map_copy;
364  }
365  private:
366  unordered_map<string, T> vars_;
367 };
368 
369 } // namespace reranker
370 
371 #endif
string ToString(const T &value) const
Definition: environment.H:187
virtual Environment * Copy() const =0
Returns a copy of this environment.
A container to hold the mapping between named variables of a specific type and their values...
Definition: environment.H:256
virtual VarMapBase * GetVarMapForType(const string &type)=0
Retrieves the VarMap instance for the specified type, or NULL if there is no such VarMap...
virtual bool Defined(const string &varname) const
Returns whether the specified variable has a definition in this environment.
Definition: environment.H:288
void SetMembers(const string &name, Environment *env, bool is_primitive)
To allow proper implementation of Copy in VarMapBase implementation, since we don't get copying of ba...
Definition: environment.H:108
#define VAR_MAP_DEBUG
Definition: environment.H:42
virtual VarMapBase * Copy(Environment *env) const =0
Returns a newly constructed copy of this VarMap.
A simple class for tokenizing a stream of tokens for the formally specified language used to construc...
virtual bool Defined(const string &varname) const =0
Returns whether the specified variable has been defined in this environment.
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 void ReadAndSet(const string &varname, StreamTokenizer &st)=0
Reads the next value (primitive or spec for constructing a Factory-constructible object) from the spe...
A base class for a mapping from variables of a specific type to their values.
Definition: environment.H:65
VarMap(const string &name, Environment *env, bool is_primitive=true)
Constructs a mapping from variables of a particular type to their values.
Definition: environment.H:265
virtual ~VarMapBase()
Definition: environment.H:78
void Set(const string &varname, T value)
Sets the specified variable to the specified value.
Definition: environment.H:293
VarMapBase(const string &name, Environment *env, bool is_primitive)
Constructs a base class for a concrete implementation providing a mapping from variables of a particu...
Definition: environment.H:75
virtual bool IsPrimitive() const
Returns whether this instance contains primitive or primtive vector variables.
Definition: environment.H:82
string ToString(const string &value) const
Definition: environment.H:199
Provides the StreamTokenizer class.
string ToString(const bool &value) const
Definition: environment.H:209
string Next()
Returns the next token in the token stream.
Provides a generic dynamic object factory.
virtual void ReadAndSet(const string &varname, StreamTokenizer &st, const string type="")=0
Sets the specified variable to the value obtained from the following tokens available from the specif...
A template class that helps print out values with ostream& operator support and vectors of those valu...
Definition: environment.H:185
virtual ~VarMap()
Definition: environment.H:268
virtual const string & GetType(const string &varname) const =0
Retrieves the type name of the specified variable.
An interface for an environment in which variables of various types are mapped to their values...
Definition: environment.H:125
virtual void Init(StreamTokenizer &st, Environment *env=NULL)
Definition: stream-init.H:87
virtual bool Defined(const string &varname) const =0
Returns whether the specified variable has a definition in this environment.
Environment * env_
The Environment that holds this VarMap instance.
Definition: environment.H:117
A class to initialize a Factory-constructible object.
Definition: stream-init.H:83
virtual VarMapBase * Copy(Environment *env) const
Returns a newly constructed copy of this VarMap.
Definition: environment.H:360
string Peek() const
Returns the next token that would be returned by the Next method.
string name_
The type name of this VarMap.
Definition: environment.H:115
virtual void Print(ostream &os) const
Prints out a human-readable string to the specified output stream containing the variables, their type and, if primitive, their values.
Definition: environment.H:345
virtual const string & Name() const
Returns the type name of the variables of this instance.
Definition: environment.H:85
virtual void ReadAndSet(const string &varname, StreamTokenizer &st)
Reads the next value (primitive or spec for constructing a Factory-constructible object) from the spe...
Definition: environment.H:298
static Environment * CreateEmpty()
A static factory method to create a new, empty Environment instance.
Definition: environment.C:43
virtual void Print(ostream &os) const =0
Prints a human-readable string of all the variables in this environment, their types and...
string ToString(const shared_ptr< T > &value) const
Definition: environment.H:220
virtual VarMapBase * GetVarMap(const string &varname)=0
Retrieves the VarMap instance for the specified variable.
string ToString(const vector< T > &value) const
Definition: environment.H:234
virtual void PrintFactories(ostream &os) const =0
Prints out a human-readable string with the names of all abstract base types and their concrete imple...
bool is_primitive_
Whether this VarMap instance holds variables of primitive type or vector of primitives.
Definition: environment.H:120
virtual void Print(ostream &os) const =0
Prints out a human-readable string to the specified output stream containing the variables, their type and, if primitive, their values.