source: src/dgp/boubble.hh

Last change on this file was 519eaf5, checked in by Tomasz Obrebski <obrebski@…>, 10 years ago

Bug fixes: bubbles,props

  • Property mode set to 100644
File size: 5.9 KB
Line 
1#ifndef _BOUBBLE_HH_
2#define _BOUBBLE_HH_
3
4#include <list>
5#include <iostream>
6#include <sstream>
7
8#include "thesymbols.hh"
9
10
11enum Dir {UP=0,DOWN=1,AT_TARGET=2};
12
13// class BoubbleAction
14// {
15// public:
16//   void apply() {};
17// private:
18 
19// };
20
21
22
23class Boubble
24{
25public:
26  Boubble() {};
27  Boubble(list<Role> u, list<Role> d, LongRel l, int s=-1, bool r=false);
28  Boubble(const char* pathstr, const char* l, int s=-1, bool r=false);
29  //Boubble(const Boubble& b) {_src=b._src; _upath=b._upath; _dpath=b._dpath; _rel=b._rel; _reverse=b._reverse; };
30
31  Dir dir() const;
32  LongRel rel() const;
33  int src() const;
34  void src(int s);
35  bool reverse() const;
36  void reverse(bool b);
37
38  Role next();
39
40  Boubble* step(Role r, Dir d);
41  Boubble* reversed();
42 
43  bool is_at_target() const;
44
45  bool operator==(Boubble const& b) const;
46  bool operator!=(Boubble const& b) const;
47  bool operator<(Boubble const& b) const;
48
49  void as_cstr(char* s);
50  friend std::ostream& operator<<(std::ostream&, const Boubble&);
51
52private:
53
54  int                  _src;
55  list<Role>           _upath;
56  list<Role>           _dpath;
57  LongRel              _rel;
58  bool                 _reverse;
59
60};
61
62//----------------------------------------------------------------------------------------------------
63
64inline
65Boubble::Boubble(list<Role> u, list<Role> d, LongRel l, int s, bool r)
66  : _upath(u), _dpath(d), _rel(l), _src(s), _reverse(r) {}
67
68//----------------------------------------------------------------------------------------------------
69
70inline
71Boubble::Boubble(const char* pathstr, const char* l, int s, bool r)
72{
73  Dir dir = UP;
74  const char* p = pathstr;
75  while(*p)
76    {
77      if(*p=='^') { dir = DOWN; p++; }
78      else if(isalpha(*p))
79        {
80          char buf[80];
81          sscanf(p,"%[a-zA-Z0-9_]",buf);
82          dir == UP ? _upath.push_back(Role(buf)) : _dpath.push_back(Role(buf));
83          p += strlen(buf);
84        }
85      else
86        p++;
87    }
88 
89  _rel = LongRel(l);
90  _src = s;
91  _reverse = r;
92}
93
94//----------------------------------------------------------------------------------------------------
95
96inline
97Dir Boubble::dir() const
98{ 
99  if(!_upath.empty())
100    return UP;
101  else if(!_dpath.empty())
102    return DOWN;
103  else return AT_TARGET;
104}
105
106//----------------------------------------------------------------------------------------------------
107
108inline
109LongRel Boubble::rel() const
110{ return _rel; }
111
112//----------------------------------------------------------------------------------------------------
113
114inline
115int Boubble::src() const
116{ return _src; }
117
118//----------------------------------------------------------------------------------------------------
119
120inline
121void Boubble::src(int s)
122{ _src=s; }
123
124//----------------------------------------------------------------------------------------------------
125
126inline
127bool Boubble::reverse() const
128{ return _reverse; }
129
130//----------------------------------------------------------------------------------------------------
131
132inline
133void Boubble::reverse(bool b)
134{ _reverse=b; }
135
136//----------------------------------------------------------------------------------------------------
137
138inline
139Role Boubble::next()
140{ 
141  if(!_upath.empty())
142    return _upath.front();
143  else if(!_dpath.empty())
144    return _dpath.front();
145  else return Role("NULL"); 
146}
147
148//----------------------------------------------------------------------------------------------------
149
150inline
151Boubble* Boubble::step(Role r, Dir d)
152{
153  if(d==UP && !_upath.empty() && _upath.front() == r)
154    {
155      Boubble* newboubble = new Boubble(_upath,_dpath,_rel,_src,_reverse);
156      newboubble->_upath.pop_front();
157      return newboubble;
158    }
159 
160  if(d==DOWN && _upath.empty() && !_dpath.empty() && _dpath.front() == r)
161    {
162      Boubble* newboubble = new Boubble(_upath,_dpath,_rel,_src,_reverse);
163      newboubble->_dpath.pop_front();
164      return newboubble;
165    }
166  return NULL;
167}
168
169//----------------------------------------------------------------------------------------------------
170
171inline
172Boubble* Boubble::reversed()
173{
174  Boubble* newboubble = new Boubble(_dpath,_upath,_rel,-1,!_reverse);
175  newboubble->_upath.reverse();
176  newboubble->_dpath.reverse();
177  // cout << *this << "-----" << *newboubble << endl;
178  return newboubble;
179}
180
181//----------------------------------------------------------------------------------------------------
182
183inline
184bool Boubble::is_at_target() const
185{ return _upath.empty() && _dpath.empty(); }
186
187//----------------------------------------------------------------------------------------------------
188
189inline
190bool Boubble::operator==(Boubble const& b) const
191{
192  return _src==b._src && _upath==b._upath && _dpath==b._dpath && _rel==b._rel;
193}
194
195//----------------------------------------------------------------------------------------------------
196
197inline
198bool Boubble::operator!=(Boubble const& b) const
199{
200  return !(*this==b);
201}
202
203//----------------------------------------------------------------------------------------------------
204
205inline
206bool Boubble::operator<(Boubble const& b) const
207{
208  if(_src < b._src) return true;
209  if(_rel < b._rel) return true;
210  if(this < &b) return true;
211  return false;
212}
213
214//----------------------------------------------------------------------------------------------------
215
216inline
217std::ostream& operator<<(std::ostream& o, const Boubble& b)
218{
219  o << "[";
220
221  o << b._src << "|";
222
223  bool cont=false;
224  for(list<Role>::const_iterator i = b._upath.begin(); i != b._upath.end(); ++i)
225    {
226      if(cont) o << ',';
227      o << i->str();
228      cont = true;
229    }
230  o << '^';
231  cont=false;
232  for(list<Role>::const_iterator i = b._dpath.begin(); i != b._dpath.end(); ++i)
233    {
234      if(cont) o << ',';
235      o << i->str();
236      cont = true;
237    }
238  o << ':';
239  o << b._rel.str();
240  o << "]";
241  if(b.reverse()) o << "!";
242  return o;
243}
244
245//----------------------------------------------------------------------------------------------------
246
247inline
248void Boubble::as_cstr(char* s)
249{
250  stringstream oss;
251  oss << *this;
252  strcpy(s,oss.str().c_str());
253}
254
255//====================================================================================================
256
257#endif
Note: See TracBrowser for help on using the repository browser.