DiaDes  0.1
DIAgnosis of Discrete-Event System
Marking.hh
Go to the documentation of this file.
1 #ifndef __DIADES__PETRI__MARKING__HH__
2 #define __DIADES__PETRI__MARKING__HH__
3 
4 #include <diades/graph/Graph.hh>
5 #include <iostream>
6 
7 namespace Diades
8 {
9  namespace Petri
10  {
11 
17  typedef Diades::Graph::Node Place;
18 
19  class Marking
20  {
21  private:
22  std::unordered_map<Place, unsigned> _marking;
23  mutable std::string _hashSequence;
24  mutable bool _hasHash;
25  public:
26 
27  Marking() : _marking(), _hashSequence(), _hasHash(true)
28  {
29  }
30 
31  Marking(const Marking & marking) : _marking(marking._marking), _hashSequence(marking._hashSequence), _hasHash(marking._hasHash)
32  {
33  }
34 
35  Marking(Marking && marking) : _marking(std::move(marking._marking)), _hashSequence(std::move(marking._hashSequence)), _hasHash(std::move(marking._hasHash))
36  {
37  }
38 
39 
40  Marking & operator=(const Marking & marking)
41  {
42  if(&marking != this)
43  {
44  _marking = marking._marking;
45  _hashSequence = marking._hashSequence;
46  _hasHash = marking._hasHash;
47  }
48  return *this;
49  }
50 
51  void
52  increaseMark(Place p)
53  {
54  _hasHash = false;
55  auto it = _marking.find(p);
56  if(it == _marking.end())
57  {
58  _marking[p] = 1;
59  }
60  else
61  {
62  it->second++;
63  }
64  }
65 
66  bool
67  decreaseMark(Place p)
68  {
69  _hasHash = false;
70  auto it = _marking.find(p);
71  bool possible = false;
72  if(it != _marking.end())
73  {
74  possible = true;
75  if(it->second == 1)
76  {
77  _marking.erase(it);
78  }
79  else
80  {
81  it->second--;
82  }
83  }
84  return possible;
85  }
86 
87  void
88  mark(Place p, unsigned tokens)
89  {
90  _hasHash = false;
91  if(tokens == 0)
92  {
93  unmark(p);
94  }
95  else
96  {
97  _marking[p] = tokens;
98  }
99  }
100 
101  void
102  unmark(Place p)
103  {
104  _hasHash = false;
105  _marking.erase(p);
106  }
107 
108  unsigned
109  getMark(Place p) const
110  {
111  auto it = _marking.find(p);
112  if(it == _marking.end())
113  {
114  return 0;
115  }
116  return it->second;
117  }
118 
119  bool
120  empty() const
121  {
122  return _marking.empty();
123  }
124 
125  void
127  {
128  _hashSequence = "";
129  _hasHash = true;
130  ;
131  _marking.clear();
132  }
133 
134  std::unordered_map<Place, unsigned>::const_iterator
135  begin() const
136  {
137  return _marking.begin();
138  }
139 
140  std::unordered_map<Place, unsigned>::const_iterator
141  end() const
142  {
143  return _marking.end();
144  }
145 
146  bool operator==(const Marking & marking) const
147  {
148  if(this == &marking)
149  {
150  return true;
151  }
152  return _marking == marking._marking;
153  }
154 
155  void
157  {
158  _hashSequence = "";
159  stringstream stream;
160  for(auto & p : _marking)
161  {
162  stream << "_" << p.first.id() << "_" << p.second;
163  }
164  _hashSequence = stream.str();
165  _hasHash = true;
166  }
167 
168  const string &
170  {
171  if(!_hasHash)
172  {
174  }
175  return _hashSequence;
176  }
177 
178  bool
179  hitsNoneOf(const vector<Marking> & markings)
180  {
181  bool result = true;
182  unsigned i = 0;
183  while(result && (i < markings.size()))
184  {
185  result = !hits(markings[i]);
186  ++i;
187  }
188  std::cout << "Does " << getHashSequence() << " hit none of the right faulty markings ? " << result << std::endl;
189  return result;
190  }
191 
192  bool
193  hits(const Marking & marking) const
194  {
195  std::cout << "Does " << getHashSequence() << " hit " << marking.getHashSequence() << " ? ";
196  bool result = true;
197  auto it = marking.begin();
198  while(result && (it != marking.end()))
199  {
200  result = getMark(it->first) >= it->second;
201  ++it;
202  }
203  std::cout << result << std::endl;
204  return result;
205  }
206 
207 
208 
209 
210  };
211  };
212 
213 };
214 
215 
216 
217 namespace std
218 {
219 
220  template <>
221  class hash<Diades::Petri::Marking>
222  {
223  public:
224 
225  size_t operator()(const Diades::Petri::Marking & m) const
226  {
227  return hash<string>()(m.getHashSequence());
228  }
229  };
230 }
231 
232 
233 
234 
235 
236 #endif
bool decreaseMark(Place p)
Definition: Marking.hh:67
Marking & operator=(const Marking &marking)
Definition: Marking.hh:40
void computeHashSequence() const
Definition: Marking.hh:156
Marking(const Marking &marking)
Definition: Marking.hh:31
std::unordered_map< Place, unsigned >::const_iterator end() const
Definition: Marking.hh:141
bool operator==(const Marking &marking) const
Definition: Marking.hh:146
STL namespace.
Diades::Graph::Node Place
Definition: BoundedNet.hh:24
void unmark(Place p)
Definition: Marking.hh:102
unsigned getMark(Place p) const
Definition: Marking.hh:109
bool hitsNoneOf(const vector< Marking > &markings)
Definition: Marking.hh:179
std::string _hashSequence
Definition: Marking.hh:23
void increaseMark(Place p)
Definition: Marking.hh:52
Namespace of the Diades project.
size_t operator()(const Diades::Petri::Marking &m) const
Definition: Marking.hh:225
bool hits(const Marking &marking) const
Definition: Marking.hh:193
void mark(Place p, unsigned tokens)
Definition: Marking.hh:88
const string & getHashSequence() const
Definition: Marking.hh:169
bool empty() const
Definition: Marking.hh:120
std::unordered_map< Place, unsigned >::const_iterator begin() const
Definition: Marking.hh:135
Marking(Marking &&marking)
Definition: Marking.hh:35
std::unordered_map< Place, unsigned > _marking
Definition: Marking.hh:22