File: Token.h

package info (click to toggle)
maria 1.3.4-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,956 kB
  • ctags: 5,485
  • sloc: cpp: 43,267; yacc: 8,080; sh: 460; ansic: 436; lisp: 395; makefile: 288; perl: 21
file content (308 lines) | stat: -rw-r--r-- 9,557 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Unified token -*- c++ -*-

#ifndef TOKEN_H_
# define TOKEN_H_
# ifdef __GNUC__
#  pragma interface
# endif // __GNUC__

/** @file Token.h
 * Tokens used in the unification process
 */

/* Copyright  1999-2003 Marko Mkel (msmakela@tcs.hut.fi).

   This file is part of MARIA, a reachability analyzer and model checker
   for high-level Petri nets.

   MARIA is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   MARIA is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   The GNU General Public License is often shipped with GNU software, and
   is generally kept in a file called COPYING or LICENSE.  If you do not
   have a copy of the license, write to the Free Software Foundation,
   59 Temple Place, Suite 330, Boston, MA 02111 USA. */

# include "GlobalMarking.h"
# include "Marking.h"
# include "s_list.h"

/** List of tokens to be unified */
struct unif
{
  /** next token */
  struct unif* next;
  /** variables that are bound from this token */
  class VariableSet* vars;
  /** flag: does the token have variable multiplicity? */
  bool varMult;
  /** index of the input place in the net */
  unsigned place;
  /** the token */
  const class Marking* m;
  /** flag: is the token multiset-valued? */
  bool isSet;
};

/** Unified token */
class Token
{
  typedef PlaceMarking::iterator iterator;
  typedef PlaceMarking::const_iterator const_iterator;

public:
  /** Constructor
   * @param cardinality		cardinality of the token
   * @param placeMarking	the PlaceMarking containing the actual token(s)
   * @param unifier		unification structure
   */
  Token (card_t cardinality,
	 class PlaceMarking& placeMarking,
	 const struct unif& unifier) :
    myIsReserved (false), myCardinality (cardinality),
    myPlaceMarking (placeMarking), myValue (0), myUnifier (unifier),
    myIterator (placeMarking.begin ()), myUnifiedUndefined (0)
  {
    assert (myCardinality > 0);
    assert (myUnifier.isSet || !myPlaceMarking.empty ());
  }
private:
  /** Copy constructor */
  Token (const class Token& old);
  /** Assignment operator */
  class Token& operator= (const class Token& old);
public:
  /** Destructor */
  ~Token ();

  /** Get the unification structure */
  const struct unif& getUnifier () const { return myUnifier; }

  /** Determine the cardinality of the actual token */
  card_t getCardinality () const { return myCardinality; }

  /** Find the next candidate for the concrete token
   * @return	true if a candidate was found
   */
  bool getConcrete () {
    assert (!myIsReserved);
    for (; myIterator != end (); myIterator++)
      if (myCardinality <= myPlaceMarking.getCount (myIterator))
	return true;
    return false;
  }

  /** Find the next candidate for the concrete token, disregarding multiplicity
   * @return	true if a candidate was found
   */
  bool getConcreteUnfold () {
    assert (!myIsReserved);
    return myIterator != end ();
  }

  /** Get the value of the concrete token candidate */
  const class Value& getValue () const {
    assert (myIterator != myPlaceMarking.end ());
    return PlaceMarking::getValue (myIterator);
  }

  /** Try to bind the token to a concrete token (and set myIterator to it)
   * @param valuation	the valuation to observe
   * @return		true if the token could be bound
   */
  bool isBindable (const class Valuation& valuation);

  /** Try to bind the token to a concrete token, disregarding multiplicity
   * @param valuation	the valuation to observe
   * @return		true if the token could be bound
   */
  bool isBindableUnfold (const class Valuation& valuation);

private:
  const_iterator begin () const { return myPlaceMarking.begin (); }
  const_iterator end () const { return myPlaceMarking.end (); }
  iterator begin () { return myPlaceMarking.begin (); }
  iterator end () { return myPlaceMarking.end (); }
public:

  /** Advance the iterator to the concrete tokens
   * @return	false if the iterator reached the end
   */
  bool next () {
    assert (!myIsReserved);
    assert (myIterator != end ());
    return ++myIterator != end ();
  }

  /** Determine whether the token has been bound to a concrete token */
  bool isReserved () const { return myIsReserved; }
  /** Bind the token to a concrete token */
  void reserve () {
    assert (!myIsReserved);
    if (myValue)
      myPlaceMarking -= *myValue;
    else
      myPlaceMarking.reserve (myIterator, myCardinality);
    myIsReserved = true;
  }
  /** Remove the reservation (binding) of the concrete token */
  void free () {
    assert (myIsReserved);
    if (myValue) {
      if (!myPlaceMarking.add (*myValue, 1))
	assert (false);
    }
    else
      myPlaceMarking.free (myIterator, myCardinality);
    myIsReserved = false;
  }
  /** Undefine the variables that were unified from this token
   * @param valuation	the valuation where the variables are defined
   */
  void undefine (class Valuation& valuation);

  /** Set the "reserved" status (needed by the unfolding algorithm) */
  void setReservedUnfold (bool reserved) { myIsReserved = reserved; }
  /** Augment the collection of removed tokens
   * @param pm	the collection
   * @return	true if the operation succeeded
   */
  bool copyRemoved (class PlaceMarking& pm) const {
    assert (myIsReserved);
    return myValue
      ? pm.add (*myValue, 1)
      : pm.add (*PlaceMarking::getValue (myIterator).copy (), myCardinality);
  }

  /** Determine whether the valuations are compatible with the concrete tokens
   * @param valuation	the valuation to observe
   * @return		true if the token is compatible
   */
  bool isCompatible (const class Valuation& valuation) const {
    return myUnifier.m->getToken ()->isCompatible (getValue (), valuation);
  }

  /** Update the set of unified possibly undefined variables
   * @param valuation	the valuation to observe
   */
  void addUnified (const class Valuation& valuation);
  /** See if a possibly undefined variable is already unified */
  bool isUnified (const class VariableDefinition& var) const;
# ifndef NDEBUG
  /** Assert that the token can be popped from the stack */
  void assertPop () const {
    assert (this && myCardinality);
    assert (myUnifier.isSet || myIterator != myPlaceMarking.end ());
  }
  /** Assert that none of the variables to unify have values
   * @param valuation	the valuation to observe
   */
  void assertUndefined (const class Valuation& valuation) const;
  /** Assert that all of the variables to unify have values
   * @param valuation	the valuation to observe
   */
  void assertDefined (const class Valuation& valuation) const;
# endif // NDEBUG

private:
  /** Flag: has the token been reserved from the place? */
  bool myIsReserved;
  /** Cardinality of the actual token */
  card_t myCardinality;
  /** PlaceMarking to which the Token belongs */
  class PlaceMarking& myPlaceMarking;
  /** PlaceMarking to which the Token evaluates */
  class PlaceMarking* myValue;
  /** the unification structure */
  const struct unif& myUnifier;
  /** Iterator to the PlaceMarking */
  iterator myIterator;
  /** Previously unified possibly undefined variables */
  class VariableSet* myUnifiedUndefined;
};

/** List of unified tokens */
class TokenList
{
public:
  /** List of tokens */
  typedef slist<class Token*> List;
  /** Iterator to the list of tokens */
  typedef List::iterator iterator;
  /** Constant iterator to the list of tokens */
  typedef List::const_iterator const_iterator;

  /** Constructor */
  TokenList () : myList () {}
private:
  /** Copy constructor */
  TokenList (const class TokenList& old);
  /** Assignment operator */
  class TokenList& operator= (const class TokenList& old);
public:
  /** Destructor */
  ~TokenList () {
    for (iterator i = begin (); i != end (); i++)
      delete *i;
  }

  /** @name Accessors to the token list */
  /*@{*/
  bool empty () const { return myList.empty (); }
  size_t size () const { return myList.size (); }
  iterator begin () { return myList.begin (); }
  iterator end () { return myList.end (); }
  const_iterator begin () const { return myList.begin (); }
  const_iterator end () const { return myList.end (); }

  void insert (class Token& token) { myList.push_front (&token); }
  void clear () {
    for (iterator i = begin (); i != end (); i++)
      delete *i;
    myList.clear ();
  }
  /*@}*/

  /** Peek at the topmost token on the stack */
  const class Token& top () const {
    assert (!empty ());
    return **begin ();
  }
  /** Pop a token from the stack */
  class Token& pop () {
    assert (!empty ());
    iterator i = begin (); class Token* token = *i; myList.erase (i);
#ifndef NDEBUG
    token->assertPop ();
#endif // NDEBUG
    return *token;
  }
  /** Push a token to the stack */
  void push (class Token& token) {
    myList.push_front (&token);
  }

  /** Determine whether the valuations are compatible with the concrete tokens
   * @param valuation	the valuation to observe
   * @return		true if all tokens are compatible
   */
  bool isCompatible (const class Valuation& valuation) const {
    for (const_iterator i = begin (); i != end (); i++)
      if (!(*i)->isCompatible (valuation))
	return false;
    return true;
  }

private:
  /** The list of tokens */
  List myList;
};

#endif // TOKEN_H_