File: Graph.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 (326 lines) | stat: -rw-r--r-- 10,091 bytes parent folder | download | duplicates (6)
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Persistent reachability graph storage -*- c++ -*-

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

# include "BitBuffer.h"

# include <map>
# include <assert.h>
# include "file.h"

/** @file Graph.h
 * Persistent, lossless reachability graph storage
 */

/* 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. */

/** Persistent reachability graph storage */
class Graph
{
public:
  /** File offset */
  typedef long fpos_t;
  /** nonexistent file offset */
# define FPOS_NONE (Graph::fpos_t (-1))
  /** special offset indicating a deadlock state */
# define FPOS_DEAD (Graph::fpos_t (-2))
  /** Graph files */
  struct files {
    /** Directory */
    file_t directory;
    /** State storage */
    file_t states;
    /** Arc storage */
    FILE* arcs;
    /** Predecessor state storage */
    file_t preds;
  };

  /** Addition status */
  class AddStatus
  {
  public:
    /** Constructor
     * @param isNew_	flag: was the state just added to the graph?
     * @param number_	the state number
     */
    AddStatus (bool isNew_, card_t number_) :
      isNew (isNew_), number (number_) {
    }
    /** Copy constructor */
    AddStatus (const class AddStatus& old) :
      isNew (old.isNew), number (old.number) {
    }
    /** Assignment operator */
    class AddStatus& operator= (const class AddStatus& old) {
      isNew = old.isNew, number = old.number;
      return *this;
    }
    /** Destructor */
    ~AddStatus () {}
    /** flag: was the state just added to the graph? */
    bool isNew;
    /** the state number */
    card_t number;
  };

  /** Constructor
   * @param net		the net whose reachability graph this is
   */
  explicit Graph (const class Net& net);
  /** Constructor
   * @param net		the net whose reachability graph this is
   * @param filename	name of the Petri Net file (NULL=read from graph file)
   * @param filebase	base name for the graph files (generated if NULL)
   */
  explicit Graph (const class Net& net,
		  const char* filename, const char* filebase);
private:
  /** Copy constructor */
  explicit Graph (const class Graph& old);
  /** Assignment operator */
  class Graph& operator= (const class Graph& old);
public:
  /** Destructor */
  ~Graph ();

  /** Determine the net whose reachability graph this is */
  const class Net& getNet () const { return myNet; }
  /** Determine the number of arcs generated */
  unsigned getNumArcs () const
# ifdef USE_MMAP
    ;
# else // USE_MMAP
  { return myNumArcs; }
# endif // USE_MMAP

  /** Determine the name of the Petri Net */
  const char* getFilename () const { return myFilename; }
  /** Determine the base name for the graph files */
  const char* getFilebase () const { return myFilebase; }

  /** Open the reachability graph files
   * @param regenerate	flag: true=ignore existing graph files
   * @return		true if the files could be opened; false on error
   */
  bool openFiles (bool regenerate);

  /** Set the LSTS output handler
   * @param lsts	the output handler (NULL=none)
   */
  void setLSTS (class LSTS* lsts);
  /** Get the LSTS output handler */
  class LSTS* getLSTS () const { return myLSTS; }

  /** Add an encoded state to the graph
   * @param buf		the encoded state
   * @param bytes	size of the encoded state in bytes
   * @return		{false,number} if the state already exists
   *			{true,number} if the state was added
   */
  const class AddStatus add (const void* buf,
			     size_t bytes);

  /** Look up a state in the graph
   * @param buf		the encoded state
   * @param bytes	size of the encoded state in bytes
   * @return		the state number, or CARD_T_MAX if not found
   */
  card_t lookup (const void* buf, size_t bytes) const;

  /** Add an event to the graph
   * @param source	the source state number
   * @param target	the target state number
   */
  void add (card_t source, card_t target);

  /** Update added events to the arc file and flag the source state processed
   * @param source	the source state number
   * @param buf		the encoded events
   * @param bytes	size of the encoded events in bytes
   */
  void addEvents (card_t source,
		  const void* buf,
		  size_t bytes);

  /** Get the number of states in the reachability graph */
  size_t getNumStates () const
# ifdef USE_MMAP
    ;
# else // USE_MMAP
  { return myNumStates; }
# endif // USE_MMAP

public:
  /** Fetch an encoded state from the reachability graph
   * @param number	number of the state
   * @param length	(output) length of the encoded state in bytes
   * @param erroneous	(output) flag: erroneous state
   * @return		the encoded state, or NULL
   */
  word_t* fetchState (card_t number, size_t& length, bool* erroneous) const;

  /** Get a state by number
   * @param number	number of the state
   * @return		the decoded state
   */
  class GlobalMarking* fetchState (card_t number) const;
  
  /** Get the number of predecessor states of a state
   * @param state	number of the state
   * @return		number of directly preceding states
   */
  card_t getNumPredecessors (card_t state) const;

  /** Fetch an offset to the predecessor records for a state
   * @param state	number of the state
   * @return		offset to the event storage (FPOS_NONE if none)
   */
  fpos_t getPredecessors (card_t state) const;

  /** Fetch predecessor state numbers for a state
   * @param pos		(in/out) offset to the event storage
   * @return		number of the source state (pos==FPOS_NONE at end)
   */
  card_t getPredecessor (fpos_t& pos) const;

  /** Fetch the successor arcs for a state
   * @param state	number of the state
   * @param data	(output) the encoded successors
   * @return		amount and numbers of successor states
   */
  card_t* getSuccessors (card_t state, word_t** data = 0) const;

  /** Evaluate a condition in a state
   * @param state	the state
   * @param cond	the condition (NULL=true)
   * @return		whether the condition holds
   */
  bool eval (card_t state,
	     const class Expression* cond) const;

  /** Tree of paths */
  typedef std::map<card_t,card_t> PathMap;

  /** Extract a path from a leaf to the root of a tree
   * @param tree	the tree
   * @param dest	the leaf of the tree
   * @return		the path
   */
  static card_t* toPath (const PathMap& tree,
			 card_t dest);

  /** Determine the shortest path from a state to a state
   * @param state	the source state
   * @param target	the target state
   * @param pathc	condition that must hold in every state on the path
   * @return		the amount and numbers of the states on the path
   */
  card_t* path (card_t state, card_t target,
		const class Expression* pathc) const;

  /** Determine the shortest path to a state fulfilling a condition
   * @param state	the source state
   * @param cond	the condition
   * @param pathc	condition that must hold in every state on the path
   * @return		the amount and numbers of the states on the path
   */
  card_t* path (card_t state, const class Expression& cond,
		const class Expression* pathc) const;

  /** Determine the shortest path from a state fulfilling a condition
   * @param state	the target state
   * @param cond	the condition
   * @param pathc	condition that must hold in every state on the path
   * @return		the amount and numbers of the states on the path
   */
  card_t* rpath (card_t state, const class Expression& cond,
		 const class Expression* pathc) const;

  /** Determine the shortest path to a loop state
   * @param state	the source state
   * @param loop	the amount and numbers of the states forming the loop
   * @param pathc	condition that must hold in every state on the path
   * @return		the amount and numbers of the states on the path
   */
  card_t* path (card_t state,
		const card_t* loop,
		const class Expression* pathc) const;

  /** Flag a state erroneous
   * @param number	the state number
   * @return		whether the state already was flagged erroneous
   */
  bool flagErroneous (card_t number);

  /** Determine whether a state is erroneous
   * @param number	the state number
   */
  bool isErroneous (card_t number) const;

  /** Determine whether a state has been processed
   * @param number	the state number
   */
  bool isProcessed (card_t number) const;

private:
  /** Add a reverse arc to the graph
   * @param source	source state number
   * @param target	target state number
   */
  void addReverse (card_t source, card_t target);

private:
  /** The net whose reachability graph this is */
  const class Net& myNet;
  /** Buffer for successor state numbers of events */
  card_t* mySucc;
# ifndef USE_MMAP
  /** Number of arcs */
  unsigned myNumArcs;
  /** Number of states */
  unsigned myNumStates;
# endif // !USE_MMAP
  /** The state hash */
  class BTree* myStates;
# if defined __sgi || defined __DECCXX
public:
# endif // __sgi || __DECCXX
  /** The graph files */
  struct files myFiles;
# if defined __sgi || defined __DECCXX
private:
# endif // __sgi || __DECCXX
  /** Offset of the state directory in myDirectory */
  fpos_t myDirectoryOffset;
  /** Name of the Petri Net */
  const char* myFilename;
  /** Base name for the graph files */
  const char* myFilebase;
  /** Labelled state transition system output for newly generated states */
  class LSTS* myLSTS;
};

#endif // GRAPH_H_