CUBA
cpda.hh
1 /******************************************************************************
2  * cpda.hh
3  *
4  * @date : Aug 27, 2016
5  * @author: TODO
6  *
7  * This file defines the data structures of a pushdown system (PDS), and a
8  * concurrent pushdown system (CPDS):
9  * 1. A PDS is a tuple (Q, L, D) consisting of three finite sets, called the
10  * of control states Q, the stack alphabet L, and the pushdown program D,
11  * respectively.
12  * 2. A CPDS over n threads is a tuple (Q, L, D1, ..., Dn) such that, for
13  * each i = 1..n, (Q, L, Di) is a PDS.
14  ******************************************************************************/
15 
16 #ifndef DS_CPDA_HH_
17 #define DS_CPDA_HH_
18 
19 #include "fsa.hh"
20 #include "pda.hh"
21 
22 namespace ruba {
23 
25 using size_n = ushort;
27 using size_k = ushort;
29 using id_thread_state = uint;
30 
31 using id_thread = size_n;
32 using ctx_bound = size_k;
33 
37 using concurrent_pushdown_automata = vector<pushdown_automaton>;
38 
40 using stack_vec = vector<pda_stack>;
41 
46 public:
47  visible_state(const pda_state& s, const size_n& n);
48  visible_state(const pda_state& s, const vector<pda_state>& L);
50 
51  const vector<pda_alpha>& get_local() const {
52  return L;
53  }
54 
55  pda_state get_state() const {
56  return s;
57  }
58 
59 private:
60  pda_state s;
61  vector<pda_alpha> L;
62 };
63 
70 inline ostream& operator<<(ostream& os, const visible_state& s) {
71  os << "(" << s.get_state() << prop::SHARED_LOCAL_DELIMITER;
72  if (s.get_local().size() > 0) {
73  if (s.get_local()[0] == alphabet::EPSILON)
74  os << alphabet::OPT_EPSILON;
75  else
76  os << s.get_local()[0];
77  }
78  for (uint i = 1; i < s.get_local().size(); ++i) {
79  cout << ",";
80  if (s.get_local()[i] == alphabet::EPSILON)
81  os << alphabet::OPT_EPSILON;
82  else
83  os << s.get_local()[i];
84  }
85  cout << ")";
86  return os;
87 }
88 
95 inline bool operator<(const visible_state& s1, const visible_state& s2) {
96  if (s1.get_state() == s2.get_state()) {
97  return algs::compare(s1.get_local(), s2.get_local()) == -1;
98  }
99  return s1.get_state() < s2.get_state();
100 }
101 
108 inline bool operator>(const visible_state& s1, const visible_state& s2) {
109  return s2 < s1;
110 }
111 
118 inline bool operator==(const visible_state& s1, const visible_state& s2) {
119  if (s1.get_state() == s2.get_state()) {
120  return algs::compare(s1.get_local(), s2.get_local()) == 0;
121  }
122  return false;
123 }
124 
131 inline bool operator!=(const visible_state& s1, const visible_state& s2) {
132  return !(s1 == s2);
133 }
134 
140 public:
141  explicit_state(const pda_state& s, const size_n& n);
142  explicit_state(const pda_state& s, const stack_vec& W);
143  explicit_state(const explicit_state& c);
144  ~explicit_state();
145 
146  pda_state get_state() const {
147  return s;
148  }
149 
150  const stack_vec& get_stacks() const {
151  return W;
152  }
153 
154  visible_state top();
155  visible_state top() const;
156 
157 private:
158  pda_state s;
159  stack_vec W;
160 };
161 
168 inline ostream& operator<<(ostream& os, const explicit_state& c) {
169  os << "(" << c.get_state() << prop::SHARED_LOCAL_DELIMITER;
170  if (c.get_stacks().size() > 0)
171  os << c.get_stacks()[0];
172  for (uint i = 1; i < c.get_stacks().size(); ++i)
173  os << prop::THREAD_DELIMITER << c.get_stacks()[i];
174  os << ")";
175  return os;
176 }
177 
184 inline bool operator<(const explicit_state& g1, const explicit_state& g2) {
185  return g1.top() < g2.top();
186 }
187 
194 inline bool operator>(const explicit_state& g1, const explicit_state& g2) {
195  return g2 < g1;
196 }
197 
204 inline bool operator==(const explicit_state& g1, const explicit_state& g2) {
205  if (g1.get_state() == g2.get_state()) {
206  auto iw1 = g1.get_stacks().cbegin();
207  auto iw2 = g2.get_stacks().cbegin();
208  while (iw1 != g1.get_stacks().cend()) {
209  if (*iw1 != *iw2)
210  return false;
211  ++iw1, ++iw2;
212  }
213  return true;
214  }
215  return false;
216 }
217 
224 inline bool operator!=(const explicit_state& g1, const explicit_state& g2) {
225  return !(g1 == g2);
226 }
227 
234 public:
235  explicit_state_tid(const pda_state& s, const size_n& n);
236  explicit_state_tid(const id_thread& id, const ctx_bound& k,
237  const pda_state& s, const size_n& n);
238  explicit_state_tid(const id_thread& id, const pda_state& s,
239  const stack_vec& W);
240  explicit_state_tid(const id_thread& id, const ctx_bound& k,
241  const pda_state& s, const stack_vec& W);
244 
245  ctx_bound get_context_k() const {
246  return k;
247  }
248 
249  void set_context_k(const ctx_bound& k) {
250  this->k = k;
251  }
252 
253  id_thread get_thread_id() const {
254  return id;
255  }
256 
257 private:
259  id_thread id;
261  ctx_bound k;
262 };
263 
270 inline ostream& operator<<(ostream& os, const explicit_state_tid& c) {
271  if (c.get_thread_id() == c.get_stacks().size())
272  os << "t=" << "* ";
273  else
274  os << "t=" << c.get_thread_id() << " ";
275  os << "<" << c.get_state() << prop::SHARED_LOCAL_DELIMITER;
276  if (c.get_stacks().size() > 0)
277  os << c.get_stacks()[0];
278  for (uint i = 1; i < c.get_stacks().size(); ++i)
279  os << prop::THREAD_DELIMITER << c.get_stacks()[i];
280  os << ">";
281  return os;
282 }
283 
290 inline bool operator<(const explicit_state_tid& g1,
291  const explicit_state_tid& g2) {
292  return g1.top() < g2.top();
293 }
294 
301 inline bool operator>(const explicit_state_tid& g1,
302  const explicit_state_tid& g2) {
303  return g2 < g1;
304 }
305 
312 inline bool operator==(const explicit_state_tid& g1,
313  const explicit_state_tid& g2) {
314  if (g1.get_state() == g2.get_state()) {
315  auto iw1 = g1.get_stacks().cbegin();
316  auto iw2 = g2.get_stacks().cbegin();
317  while (iw1 != g1.get_stacks().cend()) {
318  if (*iw1 != *iw2)
319  return false;
320  ++iw1, ++iw2;
321  }
322  return true;
323  }
324  return false;
325 }
326 
333 inline bool operator!=(const explicit_state_tid& g1,
334  const explicit_state_tid& g2) {
335  return !(g1 == g2);
336 }
337 
338 
353 public:
354  store_automaton(const fsa_state_set& states, const fsa_alphabet& alphabet,
355  const fsa_delta& transitions, const fsa_state_set& start,
356  const fsa_state& accept);
358 
359  static fsa_state create_interm_state();
360 private:
361  static fsa_state interm_s;
362 };
363 
370 public:
371  symbolic_state(const pda_state& q, const vector<store_automaton>& W);
372  symbolic_state(const pda_state& q, const size_n&n,
373  const store_automaton& A);
374  ~symbolic_state();
375 
376  pda_state get_state() const {
377  return q;
378  }
379 
380  const vector<store_automaton>& get_automata() const {
381  return W;
382  }
383 
384 private:
386  pda_state q;
388  vector<store_automaton> W;
389 };
390 
397 inline ostream& operator<<(ostream& os, const symbolic_state& c) {
398  os << "<" << c.get_state() << "|";
399  if (c.get_automata().size() > 0) {
400  for (uint i = 0; i < c.get_automata().size() - 1; ++i) {
401  os << "A" << i << ",";
402  }
403  os << "A" << c.get_automata().size() - 1 << ">\n";
404  for (uint i = 0; i < c.get_automata().size(); ++i) {
405 
406  os << "A" << i << ": " << c.get_automata()[i] << "\n";
407  }
408  } else {
409  os << ">" << endl;
410  }
411  return os;
412 }
415 using finite_machine = map<thread_visible_state, deque<transition<thread_visible_state, thread_visible_state>>>;
416 
418 using concurrent_finite_machine = vector<finite_machine>;
419 
423 enum class sequence {
424  CONVERGENT, DIVERGENT, REACHABLE, UNKNOWN
425 };
426 
427 }
428 /* namespace ruba */
429 
430 #endif /* DS_CPDA_HH_ */
Definition: cpda.hh:352
Definition: cpda.hh:233
visible_state(const pda_state &s, const size_n &n)
Definition: cpda.cc:91
Definition: cpda.hh:369
ushort size_n
the size of threads
Definition: cpda.hh:25
Definition: cpda.cc:10
unordered_map< fsa_state, set< fsa_transition > > fsa_delta
the data structure of FSA transitions
Definition: fsa.hh:125
bool operator>(const visible_state &s1, const visible_state &s2)
Definition: cpda.hh:108
~symbolic_state()
Definition: cpda.cc:301
bool operator==(const visible_state &s1, const visible_state &s2)
Definition: cpda.hh:118
bool operator!=(const visible_state &s1, const visible_state &s2)
Definition: cpda.hh:131
ushort size_k
the size of context switches
Definition: cpda.hh:27
sequence
Definition: cpda.hh:423
static fsa_state create_interm_state()
Definition: cpda.cc:263
explicit_state_tid(const pda_state &s, const size_n &n)
Definition: cpda.cc:180
set< fsa_alpha > fsa_alphabet
a set of fsa_alpha
Definition: fsa.hh:20
store_automaton(const fsa_state_set &states, const fsa_alphabet &alphabet, const fsa_delta &transitions, const fsa_state_set &start, const fsa_state &accept)
Definition: cpda.cc:244
ostream & operator<<(ostream &os, const visible_state &s)
Definition: cpda.hh:70
Definition: cpda.hh:139
uint pda_state
define the control state of PDSs
Definition: pda.hh:17
map< thread_visible_state, deque< transition< thread_visible_state, thread_visible_state > >> finite_machine
Definition: cpda.hh:415
Definition: fsa.hh:130
bool operator<(const visible_state &s1, const visible_state &s2)
Definition: cpda.hh:95
explicit_state(const pda_state &s, const size_n &n)
Definition: cpda.cc:119
vector< pushdown_automaton > concurrent_pushdown_automata
Definition: cpda.hh:37
Definition: pda.hh:29
set< fsa_state > fsa_state_set
a set of fsa_state
Definition: fsa.hh:16
Definition: cpda.hh:45
uint id_thread_state
thread id
Definition: cpda.hh:29
visible_state top()
Definition: cpda.cc:167
vector< finite_machine > concurrent_finite_machine
Concurrent finite machine: a vector of finite state machine.
Definition: cpda.hh:418
~explicit_state()
Definition: cpda.cc:147
vector< pda_stack > stack_vec
the set of stacks in CPDS
Definition: cpda.hh:40
symbolic_state(const pda_state &q, const vector< store_automaton > &W)
Definition: cpda.cc:281
static int compare(const vector< T > &v1, const vector< T > &v2)
Definition: utilities.hh:47
~visible_state()
Definition: cpda.cc:109
~store_automaton()
Definition: cpda.cc:253