Generated on Sat Feb 7 2015 02:01:13 for Gecode by doxygen 1.8.9.1
ast.hh
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Guido Tack <tack@gecode.org>
5  *
6  * Copyright:
7  * Guido Tack, 2007
8  *
9  * Last modified:
10  * $Date: 2014-10-01 03:55:43 +0200 (Wed, 01 Oct 2014) $ by $Author: tack $
11  * $Revision: 14235 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #ifndef __GECODE_FLATZINC_AST_HH__
39 #define __GECODE_FLATZINC_AST_HH__
40 
41 #include <vector>
42 #include <string>
43 #include <iostream>
44 #include <cstdlib>
45 
51 namespace Gecode { namespace FlatZinc { namespace AST {
52 
53  class Call;
54  class Array;
55  class Atom;
56  class SetLit;
57 
60  private:
61  std::string _what;
62  public:
63  TypeError() : _what("") {}
64  TypeError(std::string what) : _what(what) {}
65  std::string what(void) const { return _what; }
66  };
67 
72  public:
74  virtual ~Node(void);
75 
77  void append(Node* n);
78 
80  bool hasAtom(const std::string& id);
82  bool isInt(int& i);
84  bool isFloat(double& i);
86  bool isCall(const std::string& id);
88  Call* getCall(void);
90  bool hasCall(const std::string& id);
92  Call* getCall(const std::string& id);
94  Array* getArray(void);
96  Atom* getAtom(void);
98  std::string getVarName(void);
100  int getIntVar(void);
102  int getBoolVar(void);
104  int getFloatVar(void);
106  int getSetVar(void);
107 
109  int getInt(void);
111  bool getBool(void);
113  double getFloat(void);
115  SetLit *getSet(void);
116 
118  std::string getString(void);
119 
121  bool isIntVar(void);
123  bool isBoolVar(void);
125  bool isSetVar(void);
127  bool isFloatVar(void);
129  bool isInt(void);
131  bool isFloat(void);
133  bool isBool(void);
135  bool isString(void);
137  bool isArray(void);
139  bool isSet(void);
141  bool isAtom(void);
142 
144  virtual void print(std::ostream&) = 0;
145  };
146 
149  public:
150  bool b;
151  BoolLit(bool b0) : b(b0) {}
152  virtual void print(std::ostream& os) {
153  os << "b(" << (b ? "true" : "false") << ")";
154  }
155  };
158  public:
159  int i;
160  IntLit(int i0) : i(i0) {}
161  virtual void print(std::ostream& os) {
162  os << "i("<<i<<")";
163  }
164  };
167  public:
168  double d;
169  FloatLit(double d0) : d(d0) {}
170  virtual void print(std::ostream& os) {
171  os << "f("<<d<<")";
172  }
173  };
176  public:
177  bool interval;
178  int min; int max;
179  std::vector<int> s;
180  SetLit(void) {}
181  SetLit(int min0, int max0) : interval(true), min(min0), max(max0) {}
182  SetLit(const std::vector<int>& s0) : interval(false), s(s0) {}
183  explicit SetLit(SetLit* s0) : interval(s0->interval), min(s0->min), max(s0->max), s(s0->s) {}
184  bool empty(void) const {
185  return ( (interval && min>max) || (!interval && s.size() == 0));
186  }
187  virtual void print(std::ostream& os) {
188  os << "s()";
189  }
190  };
191 
193  class GECODE_VTABLE_EXPORT Var : public Node {
194  public:
195  int i; //< Index
196  std::string n; //< Name
198  Var(int i0, const std::string& n0) : i(i0), n(n0) {}
199  };
202  public:
204  BoolVar(int i0, const std::string& n0="") : Var(i0,n0) {}
205  virtual void print(std::ostream& os) {
206  os << "xb("<<i<<")";
207  }
208  };
210  class GECODE_VTABLE_EXPORT IntVar : public Var {
211  public:
212  IntVar(int i0, const std::string& n0="") : Var(i0,n0) {}
213  virtual void print(std::ostream& os) {
214  os << "xi("<<i<<")";
215  }
216  };
219  public:
220  FloatVar(int i0, const std::string& n0="") : Var(i0,n0) {}
221  virtual void print(std::ostream& os) {
222  os << "xf("<<i<<")";
223  }
224  };
226  class GECODE_VTABLE_EXPORT SetVar : public Var {
227  public:
228  SetVar(int i0, const std::string& n0="") : Var(i0,n0) {}
229  virtual void print(std::ostream& os) {
230  os << "xs("<<i<<")";
231  }
232  };
233 
235  class GECODE_VTABLE_EXPORT Array : public Node {
236  public:
237  std::vector<Node*> a;
238  Array(const std::vector<Node*>& a0)
239  : a(a0) {}
241  : a(1) { a[0] = n; }
242  Array(int n=0) : a(n) {}
243  virtual void print(std::ostream& os) {
244  os << "[";
245  for (unsigned int i=0; i<a.size(); i++) {
246  a[i]->print(os);
247  if (i<a.size()-1)
248  os << ", ";
249  }
250  os << "]";
251  }
252  ~Array(void) {
253  for (int i=a.size(); i--;)
254  delete a[i];
255  }
256  };
257 
259  class GECODE_VTABLE_EXPORT Call : public Node {
260  public:
261  std::string id;
263  Call(const std::string& id0, Node* args0)
264  : id(id0), args(args0) {}
265  ~Call(void) { delete args; }
266  virtual void print(std::ostream& os) {
267  os << id << "("; args->print(os); os << ")";
268  }
269  Array* getArgs(unsigned int n) {
270  Array *a = args->getArray();
271  if (a->a.size() != n)
272  throw TypeError("arity mismatch");
273  return a;
274  }
275  };
276 
279  public:
280  Node* a;
282  ArrayAccess(Node* a0, Node* idx0)
283  : a(a0), idx(idx0) {}
284  ~ArrayAccess(void) { delete a; delete idx; }
285  virtual void print(std::ostream& os) {
286  a->print(os);
287  os << "[";
288  idx->print(os);
289  os << "]";
290  }
291  };
292 
294  class GECODE_VTABLE_EXPORT Atom : public Node {
295  public:
296  std::string id;
297  Atom(const std::string& id0) : id(id0) {}
298  virtual void print(std::ostream& os) {
299  os << id;
300  }
301  };
302 
305  public:
306  std::string s;
307  String(const std::string& s0) : s(s0) {}
308  virtual void print(std::ostream& os) {
309  os << "s(\"" << s << "\")";
310  }
311  };
312 
313  inline
314  Node::~Node(void) {}
315 
316  inline void
317  Node::append(Node* newNode) {
318  Array* a = dynamic_cast<Array*>(this);
319  if (!a)
320  throw TypeError("array expected");
321  a->a.push_back(newNode);
322  }
323 
324  inline bool
325  Node::hasAtom(const std::string& id) {
326  if (Array* a = dynamic_cast<Array*>(this)) {
327  for (int i=a->a.size(); i--;)
328  if (Atom* at = dynamic_cast<Atom*>(a->a[i]))
329  if (at->id == id)
330  return true;
331  } else if (Atom* a = dynamic_cast<Atom*>(this)) {
332  return a->id == id;
333  }
334  return false;
335  }
336 
337  inline bool
338  Node::isCall(const std::string& id) {
339  if (Call* a = dynamic_cast<Call*>(this)) {
340  if (a->id == id)
341  return true;
342  }
343  return false;
344  }
345 
346  inline Call*
348  if (Call* a = dynamic_cast<Call*>(this))
349  return a;
350  throw TypeError("call expected");
351  }
352 
353  inline bool
354  Node::hasCall(const std::string& id) {
355  if (Array* a = dynamic_cast<Array*>(this)) {
356  for (int i=a->a.size(); i--;)
357  if (Call* at = dynamic_cast<Call*>(a->a[i]))
358  if (at->id == id) {
359  return true;
360  }
361  } else if (Call* a = dynamic_cast<Call*>(this)) {
362  return a->id == id;
363  }
364  return false;
365  }
366 
367  inline bool
368  Node::isInt(int& i) {
369  if (IntLit* il = dynamic_cast<IntLit*>(this)) {
370  i = il->i;
371  return true;
372  }
373  return false;
374  }
375 
376  inline bool
377  Node::isFloat(double& d) {
378  if (FloatLit* fl = dynamic_cast<FloatLit*>(this)) {
379  d = fl->d;
380  return true;
381  }
382  return false;
383  }
384 
385  inline Call*
386  Node::getCall(const std::string& id) {
387  if (Array* a = dynamic_cast<Array*>(this)) {
388  for (int i=a->a.size(); i--;)
389  if (Call* at = dynamic_cast<Call*>(a->a[i]))
390  if (at->id == id)
391  return at;
392  } else if (Call* a = dynamic_cast<Call*>(this)) {
393  if (a->id == id)
394  return a;
395  }
396  throw TypeError("call expected");
397  }
398 
399  inline Array*
401  if (Array* a = dynamic_cast<Array*>(this))
402  return a;
403  throw TypeError("array expected");
404  }
405 
406  inline Atom*
408  if (Atom* a = dynamic_cast<Atom*>(this))
409  return a;
410  throw TypeError("atom expected");
411  }
412 
413  inline std::string
415  if (Var* a = dynamic_cast<Var*>(this))
416  return a->n;
417  throw TypeError("variable expected");
418  }
419  inline int
421  if (IntVar* a = dynamic_cast<IntVar*>(this))
422  return a->i;
423  throw TypeError("integer variable expected");
424  }
425  inline int
427  if (BoolVar* a = dynamic_cast<BoolVar*>(this))
428  return a->i;
429  throw TypeError("bool variable expected");
430  }
431  inline int
433  if (FloatVar* a = dynamic_cast<FloatVar*>(this))
434  return a->i;
435  throw TypeError("integer variable expected");
436  }
437  inline int
439  if (SetVar* a = dynamic_cast<SetVar*>(this))
440  return a->i;
441  throw TypeError("set variable expected");
442  }
443  inline int
444  Node::getInt(void) {
445  if (IntLit* a = dynamic_cast<IntLit*>(this))
446  return a->i;
447  throw TypeError("integer literal expected");
448  }
449  inline bool
451  if (BoolLit* a = dynamic_cast<BoolLit*>(this))
452  return a->b;
453  throw TypeError("bool literal expected");
454  }
455  inline double
457  if (FloatLit* a = dynamic_cast<FloatLit*>(this))
458  return a->d;
459  throw TypeError("float literal expected");
460  }
461  inline SetLit*
462  Node::getSet(void) {
463  if (SetLit* a = dynamic_cast<SetLit*>(this))
464  return a;
465  throw TypeError("set literal expected");
466  }
467  inline std::string
469  if (String* a = dynamic_cast<String*>(this))
470  return a->s;
471  throw TypeError("string literal expected");
472  }
473  inline bool
475  return (dynamic_cast<IntVar*>(this) != NULL);
476  }
477  inline bool
479  return (dynamic_cast<BoolVar*>(this) != NULL);
480  }
481  inline bool
483  return (dynamic_cast<SetVar*>(this) != NULL);
484  }
485  inline bool
487  return (dynamic_cast<FloatVar*>(this) != NULL);
488  }
489  inline bool
490  Node::isInt(void) {
491  return (dynamic_cast<IntLit*>(this) != NULL);
492  }
493  inline bool
494  Node::isBool(void) {
495  return (dynamic_cast<BoolLit*>(this) != NULL);
496  }
497  inline bool
499  return (dynamic_cast<FloatLit*>(this) != NULL);
500  }
501  inline bool
502  Node::isSet(void) {
503  return (dynamic_cast<SetLit*>(this) != NULL);
504  }
505  inline bool
507  return (dynamic_cast<String*>(this) != NULL);
508  }
509  inline bool
511  return (dynamic_cast<Array*>(this) != NULL);
512  }
513  inline bool
514  Node::isAtom(void) {
515  return (dynamic_cast<Atom*>(this) != NULL);
516  }
517 
518  inline Node*
520  if (Array* a = dynamic_cast<Array*>(n)) {
521  if (a->a.size() == 1) {
522  Node *ret = a->a[0];
523  a->a[0] = NULL;
524  delete a;
525  return ret;
526  }
527  }
528  return n;
529  }
530 
531 }}}
532 
533 #endif
534 
535 // STATISTICS: flatzinc-any
SetVar(int i0, const std::string &n0="")
Definition: ast.hh:228
Var(int i0, const std::string &n0)
Constructor.
Definition: ast.hh:198
virtual void print(std::ostream &os)
Output string representation.
Definition: ast.hh:221
Float literal node.
Definition: ast.hh:166
std::string what(void) const
Definition: ast.hh:65
virtual void print(std::ostream &os)
Output string representation.
Definition: ast.hh:213
Boolean variable node.
Definition: ast.hh:201
bool isBool(void)
Test if node is a Boolean node.
Definition: ast.hh:494
bool getBool(void)
Cast this node to a Boolean node.
Definition: ast.hh:450
Array(const std::vector< Node * > &a0)
Definition: ast.hh:238
SetLit(const std::vector< int > &s0)
Definition: ast.hh:182
virtual void print(std::ostream &os)
Output string representation.
Definition: ast.hh:298
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1662
SetLit * getSet(void)
Cast this node to a set literal node.
Definition: ast.hh:462
std::string getVarName(void)
Return name of variable represented by this node.
Definition: ast.hh:414
Call * getCall(void)
Return function call.
Definition: ast.hh:347
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:57
int getFloatVar(void)
Cast this node to a Float variable node.
Definition: ast.hh:432
virtual void print(std::ostream &os)
Output string representation.
Definition: ast.hh:308
bool isAtom(void)
Test if node is an atom node.
Definition: ast.hh:514
bool isInt(void)
Test if node is an integer node.
Definition: ast.hh:490
Node * extractSingleton(Node *n)
Definition: ast.hh:519
SetLit(int min0, int max0)
Definition: ast.hh:181
Atom(const std::string &id0)
Definition: ast.hh:297
Variable node base class.
Definition: ast.hh:193
bool isSetVar(void)
Test if node is a set variable node.
Definition: ast.hh:482
bool isBoolVar(void)
Test if node is a Boolean variable node.
Definition: ast.hh:478
Node representing an array access
Definition: ast.hh:278
std::vector< Node * > a
Definition: ast.hh:237
Atom * getAtom(void)
Cast this node to an Atom node.
Definition: ast.hh:407
virtual void print(std::ostream &os)
Output string representation.
Definition: ast.hh:161
Array * getArray(void)
Cast this node to an array node.
Definition: ast.hh:400
Gecode::IntSet d(v, 7)
int getSetVar(void)
Cast this node to a set variable node.
Definition: ast.hh:438
double getFloat(void)
Cast this node to a Float node.
Definition: ast.hh:456
ArrayAccess(Node *a0, Node *idx0)
Definition: ast.hh:282
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
virtual ~Node(void)
Destructor.
Definition: ast.hh:314
String(const std::string &s0)
Definition: ast.hh:307
Node representing an atom
Definition: ast.hh:294
int getIntVar(void)
Cast this node to an integer variable node.
Definition: ast.hh:420
int getBoolVar(void)
Cast this node to a Boolean variable node.
Definition: ast.hh:426
virtual void print(std::ostream &os)
Output string representation.
Definition: ast.hh:285
bool isSet(void)
Test if node is a set literal node.
Definition: ast.hh:502
std::vector< int > s
Definition: ast.hh:179
virtual void print(std::ostream &os)
Output string representation.
Definition: ast.hh:170
virtual void print(std::ostream &os)
Output string representation.
Definition: ast.hh:229
Array * getArgs(unsigned int n)
Definition: ast.hh:269
Float variable node.
Definition: ast.hh:218
Set variable node
Definition: ast.hh:226
virtual void print(std::ostream &os)
Output string representation.
Definition: ast.hh:205
bool isIntVar(void)
Test if node is an integer variable node.
Definition: ast.hh:474
bool empty(void) const
Definition: ast.hh:184
bool isString(void)
Test if node is a string node.
Definition: ast.hh:506
Boolean literal node.
Definition: ast.hh:148
bool hasCall(const std::string &id)
Test if node is function call or array containing function call id.
Definition: ast.hh:354
int getInt(void)
Cast this node to an integer node.
Definition: ast.hh:444
void print(std::basic_ostream< Char, Traits > &s, bool assigned, IL &lb, IU &ub, unsigned int cardMin, unsigned int cardMax)
Print set view.
Definition: print.hpp:67
Integer variable node.
Definition: ast.hh:210
virtual void print(std::ostream &)=0
Output string representation.
Exception signaling type error
Definition: ast.hh:59
bool hasAtom(const std::string &id)
Test if node has atom with id.
Definition: ast.hh:325
virtual void print(std::ostream &os)
Output string representation.
Definition: ast.hh:187
bool isFloatVar(void)
Test if node is a float variable node.
Definition: ast.hh:486
Set literal node
Definition: ast.hh:175
Call(const std::string &id0, Node *args0)
Definition: ast.hh:263
bool isArray(void)
Test if node is an array node.
Definition: ast.hh:510
IntVar(int i0, const std::string &n0="")
Definition: ast.hh:212
Gecode toplevel namespace
bool isFloat(void)
Test if node is a float node.
Definition: ast.hh:498
FloatVar(int i0, const std::string &n0="")
Definition: ast.hh:220
Node representing a function call
Definition: ast.hh:259
#define GECODE_VTABLE_EXPORT
Definition: support.hh:76
A node in a FlatZinc abstract syntax tree.
Definition: ast.hh:71
TypeError(std::string what)
Definition: ast.hh:64
virtual void print(std::ostream &os)
Output string representation.
Definition: ast.hh:243
bool isCall(const std::string &id)
Test if node is function call with id.
Definition: ast.hh:338
void append(Node *n)
Append n to an array node.
Definition: ast.hh:317
BoolVar(int i0, const std::string &n0="")
Constructor.
Definition: ast.hh:204
std::string getString(void)
Cast this node to a string node.
Definition: ast.hh:468
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.
Integer literal node.
Definition: ast.hh:157
virtual void print(std::ostream &os)
Output string representation.
Definition: ast.hh:152
virtual void print(std::ostream &os)
Output string representation.
Definition: ast.hh:266