File: StateReporter.C

package info (click to toggle)
maria 1.3.5-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,980 kB
  • ctags: 5,458
  • sloc: cpp: 43,402; yacc: 8,080; ansic: 436; sh: 404; lisp: 395; makefile: 291; perl: 21
file content (624 lines) | stat: -rw-r--r-- 17,502 bytes parent folder | download | duplicates (5)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
// Reporting successor states -*- c++ -*-

/** @file StateReporter.C
 * Interface for reporting successor states
 */

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

#ifdef __GNUC__
# pragma implementation "StateReporter.h"
#endif // __GNUC__

#include "StateSetReporter.h"
#include "States.h"
#include "Net.h"
#include "Transition.h"
#include "Valuation.h"
#include "GlobalMarking.h"
#include "FullSet.h"
#include "SyncStates.h"
#ifdef EXPR_COMPILE
# include "Compilation.h"
# include "Printer.h"
#endif // EXPR_COMPILE

/** Flag: has the analysis been interrupted? */
volatile bool interrupted = false;

StateReporter::StateReporter (
#ifdef EXPR_COMPILE
			      const class Compilation* compilation,
#endif // EXPR_COMPILE
			      const class Net& net_,
			      const class Printer& printer_,
			      unsigned maxerrors,
			      bool compress,
			      bool local,
			      bool flattened) :
#ifdef EXPR_COMPILE
  myCompilation (compilation),
#endif // EXPR_COMPILE
  net (net_), printer (printer_),
  myMaxErrors (maxerrors), myNumErrors (0), myNumLocal (0),
  myEnabled (net.getNumEnabled ()
	     ? new char[net.getNumEnabled ()]
	     : 0),
  mySync (0),
  myFatal (false), mySrcDeadlock (true),
  myPriority (0), myStateBuf (), mySrc (0), mySrcSize (0),
  myFlattened (flattened),
  myLocal (local ? new class FullSet () : 0),
  mySuppressed (compress ? new class States () : 0),
  mySuccessors (None), myMinSize (size_t (-1L)), myMaxSize (0)
{
  if (net.getNumEnabled ())
    memset (myEnabled, 0, net.getNumEnabled ());
  if (myLocal && !myLocal->init (true)) {
    delete myLocal;
    *const_cast<class FullSet**>(&myLocal) = 0;
  }
}

StateReporter::StateReporter (const class StateReporter& old) :
#ifdef EXPR_COMPILE
  myCompilation (old.myCompilation),
#endif // EXPR_COMPILE
  net (old.net), printer (old.printer),
  myMaxErrors (old.myMaxErrors), myNumErrors (0), myNumLocal (0),
  myEnabled (net.getNumEnabled ()
	     ? new char[net.getNumEnabled ()]
	     : 0),
  mySync (0),
  myFatal (false), mySrcDeadlock (true),
  myPriority (0), myStateBuf (), mySrc (0), mySrcSize (0),
  myFlattened (old.myFlattened),
  myLocal (old.myLocal ? new class FullSet () : 0),
  mySuppressed (old.mySuppressed ? new class States () : 0),
  mySuccessors (None), myMinSize (size_t (-1L)), myMaxSize (0)
{
  if (net.getNumEnabled ())
    memset (myEnabled, 0, net.getNumEnabled ());
  if (myLocal && !myLocal->init (true)) {
    delete myLocal;
    *const_cast<class FullSet**>(&myLocal) = 0;
  }
}

StateReporter::~StateReporter ()
{
  delete[] myEnabled;
  delete[] mySrc;
  delete mySuppressed;
  delete myLocal;
}

void
StateReporter::enabled (const class Transition& transition)
{
  assert (!myPriority || transition.getPriority () == myPriority);
  mySrcDeadlock = false;
  myPriority = transition.getPriority ();
}

#ifdef EXPR_COMPILE
/** Report an encoded state
 * @param buf	the encoded state
 * @param size	length of the encoded state, in bytes
 * @param err	errNone if the state is not rejected
 * @param tr	the number of the transition that fired
 * @param ftr	the number of the transition in the flattened net
 * @param hide	flag: is the transition hidden?
 * @param ctx	the call-back context
 */
static void
addState (const void* buf, size_t size, enum Error err,
	  unsigned tr, unsigned ftr, int hide, void* ctx)
{
  class StateReporter& reporter = *static_cast<class StateReporter*>(ctx);
  if (err && err != errComp)
    reporter.flagFatal ();
  reporter.addState (buf, size, err != errNone, hide,
		     reporter.isFlattened () ? ftr : tr);
}

/** Report a potential synchronisation state
 * @param tr	the number of the transition that fired
 * @param ctx	the call-back context
 */
static void
syncState (unsigned tr, void* ctx)
{
  class StateReporter& reporter = *static_cast<class StateReporter*>(ctx);
  reporter.reportSync (reporter.net.getTransition (tr));
}

void
StateReporter::analyze (const class Transition& transition)
{
  if (unsigned errors =
      myCompilation->eventAnalyze (transition.getRootIndex (),
				   this, myEnabled)) {
    enabled (transition);
    reject ();
    printer.printQuoted (transition.getName ());
    printer.delimiter (':');
    printer.print (errors);
    printer.printRaw (errors == 1 ? " error" : " errors");
    printer.finish ();
  }
}

void
StateReporter::addState (const void* buf, size_t size,
			 bool rejected, bool hidden, unsigned tr)
{
  const class Transition& t = net.getTransition (tr);
  enabled (t);
  report (buf, size, rejected, hidden);
}
#endif // EXPR_COMPILE

void
StateReporter::analyze (enum SearchKind kind)
{
#ifdef EXPR_COMPILE
  if (myCompilation) {
    myCompilation->linkAddState (&::addState);
    myCompilation->linkReporter (&::syncState, isGraph (), myFlattened);
    myCompilation->getFatal () = &myFatal;
    myCompilation->getFlattened () = myFlattened;
  }
#endif // EXPR_COMPILE
  if (myFlattened)
    analyzeFlattened (kind);
  else
    analyzeModular (kind);
}

void
StateReporter::analyzeFlattened (enum SearchKind kind)
{
  assert (myFlattened);
  const unsigned numTr = net.getNumAllTransitions ();
#ifdef EXPR_COMPILE
  if (myCompilation) {
    while (!interrupted && !myFatal) {
      if (!popCompiled (kind == Breadth))
	break;
      mySrcDeadlock = true, myPriority = 0;
      for (unsigned i = 0; !interrupted && !myFatal && i < numTr; i++) {
	const class Transition& t = net.getTransition (i);
	if (!myPriority || t.getPriority () == myPriority)
	  analyze (t);
      }
      if (mySrcDeadlock && !interrupted && !myFatal) {
	switch (myCompilation->stateDeadlock (net.getIndex ())) {
	case errNone:
	  break;
	default:
	  myFatal = true;
	  // fall through
	case errComp:
	  reportError (true);
	}
      }
      if (suppress ())
	continue;
      addEvents ();
      if (kind >= Single)
	break;
    }
    return;
  }
#endif // EXPR_COMPILE
  while (!interrupted && !myFatal) {
    class GlobalMarking* m = pop (kind == Breadth);
    if (!m)
      break;
    mySrcDeadlock = true, myPriority = 0;
    for (unsigned i = 0; !interrupted && !myFatal && i < numTr; i++) {
      const class Transition& t = net.getTransition (i);
      if (!t.getNumParents () &&
	  (!myPriority || t.getPriority () == myPriority))
	t.analyze (*m, *this);
    }
    if (mySrcDeadlock && !interrupted && !myFatal) {
      switch (net.isDeadlock (*m, true)) {
      case Net::OK:
	break;
      case Net::Fatal:
	myFatal = true;
	// fall through
      case Net::Error:
	reportError (true);
      }
    }
    delete m;
    if (suppress ())
      continue;
    addEvents ();
    if (kind >= Single)
      break;
  }
}

void
StateReporter::analyzeModular (enum SearchKind kind)
{
  assert (!myFlattened);
  /** state spaces of the child nets */
  class FullSet* childset = initModules ();
  /** the synchronising state space */
  class SyncStates* sync = childset ? new class SyncStates (net) : 0;
  unsigned i;
  const unsigned numTr = net.getNumTransitions ();
  while (!interrupted && !myFatal) {
    class GlobalMarking* m =
#ifdef EXPR_COMPILE
      myCompilation ? 0 :
#endif // EXPR_COMPILE
      pop (kind == Breadth);
    if (
#ifdef EXPR_COMPILE
	myCompilation ? !popCompiled (kind == Breadth) :
#endif // EXPR_COMPILE
	!m)
      break;
    mySrcDeadlock = true, myPriority = 0;
    for (i = 0; !interrupted && !myFatal && i < numTr; i++) {
      const class Transition& t = net.getTransition (i);
      if ((!myPriority || t.getPriority () == myPriority) &&
	  !t.getNumChildren ()) {
#ifdef EXPR_COMPILE
	if (myCompilation)
	  analyze (t);
	else
#endif // EXPR_COMPILE
	  t.analyze (*m, *this);
      }
      // synchronisation transitions (which have children) are analysed
      // by SyncStates::sync (), called via analyzeModules () below
    }
    if (childset) {
#ifdef EXPR_COMPILE
      if (myCompilation)
	analyzeModules (*sync, childset);
      else
#endif // EXPR_COMPILE
	analyzeModules (*m, *sync, childset);
    }

    if (mySrcDeadlock && !interrupted && !myFatal) {
#ifdef EXPR_COMPILE
      if (myCompilation) {
	switch (myCompilation->stateDeadlock (net.getIndex ())) {
	case errNone:
	  break;
	default:
	  myFatal = true;
	  // fall through
	case errComp:
	  reportError (true);
	}
      } else
#endif // EXPR_COMPILE
      switch (net.isDeadlock (*m, false)) {
      case Net::OK:
	break;
      case Net::Fatal:
	myFatal = true;
	// fall through
      case Net::Error:
	reportError (true);
      }
    }

    delete m;
    if (suppress ())
      continue;
    addEvents ();
    if (kind >= Single)
      break;
  }
  for (i = net.getNumChildren(); i--; )
    myNumLocal += childset[i].getNumStates ();

  delete[] childset;
  delete sync;
}

class FullSet*
StateReporter::initModules () const
{
  unsigned i = myFlattened ? 0 : net.getNumChildren ();
  class FullSet* childset = i ? new class FullSet[i] : 0;
  while (i--) {
    if (!childset[i].init (false)) {
      assert (false);
      delete[] childset;
      return 0;
    }
  }
  return childset;
}

void
StateReporter::analyzeModules (const class GlobalMarking& m,
			       class SyncStates& sync,
			       class FullSet* childset)
{
  assert (net.getNumChildren () > 0);
  unsigned i;

  // analyze child state spaces
  for (i = net.getNumChildren (); !interrupted && !myFatal && i--; ) {
    const class Net& child = net.getChild (i);
    childset[i].clear ();
    m.encode (myStateBuf, child);
#ifdef SYNC_CACHE
    if (sync.lookup (i, myStateBuf.getBuf (), myStateBuf.getNumWords ()))
      continue; // the modular state has already been analyzed
#endif // SYNC_CACHE
    myStateBuf.deflate ();
    if (!childset[i].add (myStateBuf.getBuf (), myStateBuf.getNumBytes ()))
      assert (false); // this should be a new state
    // to do: support safety-LTL property checking for the modules
    class StateSetReporter reporter (
#ifdef EXPR_COMPILE
				     myCompilation,
#endif
				     child, printer, 0, false, false, false,
				     0, 0, false, childset[i], 0);
    reporter.mySync = &sync;
    reporter.analyzeModular (Breadth);
    // to do: report the error graphically, show step from mySrc to myStateBuf
    if (reporter.myFatal)
      myFatal = true;
    if (reporter.getNumErrors ())
      reportError (false);
    myNumLocal += reporter.myNumLocal;
  }

  // find out enabled synchronisation transitions and synchronise
  for (i = net.getNumTransitions (); i--; ) {
    const class Transition& t = net.getTransition (i);
    if ((!myPriority || t.getPriority () == myPriority) &&
	t.getNumChildren ())
      sync.sync (m, t, *this);
  }

#ifndef SYNC_CACHE
  sync.cleanup ();
#endif // !SYNC_CACHE
}

#ifdef EXPR_COMPILE
void
StateReporter::analyzeModules (class SyncStates& sync,
			       class FullSet* childset)
{
  assert (net.getNumChildren () > 0);
  assert (!!myCompilation);
  unsigned i = net.getNumChildren ();
  word_t** srcs = new word_t*[i];

  // analyze child state spaces
  for (; !interrupted && !myFatal && i--; ) {
    const class Net& child = net.getChild (i);
    childset[i].clear ();
    unsigned bytes;
    const void* buf =
      myCompilation->stateProject (child.getIndex (), 0, 0, bytes);
    word_t* data = const_cast<word_t*>(static_cast<const word_t*>(buf));
    unsigned numWords = bytes ? (bytes - 1) / sizeof (word_t) + 1 : 0;
    /* back up the source state */
    BitPacker::inflate (data[numWords - 1], (-bytes) % sizeof (word_t));
    srcs[i] = bytes ? new word_t[numWords] : 0;
    memcpy (srcs[i], data, numWords * sizeof (word_t));
#ifdef SYNC_CACHE
    if (sync.lookup (i, data, numWords))
      continue; // the modular state has already been analyzed
#endif // SYNC_CACHE
    BitPacker::deflate (data[numWords - 1], (-bytes) % sizeof (word_t));
    if (!childset[i].add (buf, bytes))
      assert (false); // this should be a new state

    // to do: support safety-LTL property checking for the modules
    class StateSetReporter reporter (myCompilation,
				     child, printer, 0, false, false, false,
				     0, 0, false, childset[i], 0);
    reporter.mySync = &sync;
    reporter.analyzeModular (Breadth);
    /* restore the source state */
    myCompilation->stateDecode (child.getIndex (), srcs[i], 0);
    // to do: report the error graphically, show step from mySrc to myStateBuf
    if (reporter.myFatal)
      myFatal = true;
    if (reporter.getNumErrors ())
      reportError (false);
    myNumLocal += reporter.myNumLocal;
  }

  // find out enabled synchronisation transitions and synchronise
  for (i = net.getNumTransitions (); i--; ) {
    const class Transition& t = net.getTransition (i);
    if ((myPriority && t.getPriority () != myPriority) ||
	!t.getNumChildren ())
      continue;
    sync.sync (t, *this, srcs);
  }

  for (i = net.getNumChildren (); i--; )
    delete[] srcs[i];
  delete[] srcs;

# ifndef SYNC_CACHE
  sync.cleanup ();
# endif // !SYNC_CACHE
}

void
StateReporter::sync (const class Transition& transition)
{
  assert (transition.getNumChildren () > 0);
  unsigned bytes;
  const void* buf = myCompilation->stateProject
    (transition.getNet ()->getIndex (), 0, 0, bytes);
  unsigned numWords = (bytes - 1) / sizeof (word_t) + 1;
  word_t* s = numWords ? new word_t[numWords] : 0;
  memcpy (s, buf, bytes);
  BitPacker::inflate (s[numWords - 1], (-bytes) % sizeof (word_t));
  setSyncSource (s, bytes);
  analyze (transition);
}
#endif // EXPR_COMPILE

bool
StateReporter::suppress ()
{
  if (myLocal && myLocal->getNumStates ())
    return true;
  if (mySuppressed) {
    switch (mySuccessors) {
    case One:
      return true;
    case Many:
      if (const size_t num = mySuppressed->size ()) {
	const struct States::state& s = (*mySuppressed)[num - 1];
	do_addState (s.buf, s.size);
	mySuccessors = None;
      }
      // fall through
    case None:
      mySuppressed->clear ();
    }
  }
  return false;
}

bool
StateReporter::report (const class Transition& transition,
		       const class Valuation& valuation,
		       const class GlobalMarking& marking)
{
  card_t errorplace;
  assert (valuation.isOK ());
  enabled (transition);
  if (myEnabled) transition.logEnabled (valuation, myEnabled);
  if (marking.encode (myStateBuf, *net.getInitMarking (), &errorplace)) {
    enum Net::Status status = net.isReject (marking, myFlattened);
    switch (status) {
    case Net::OK:
      break;
    case Net::Fatal:
      flagFatal ();
      // fall through
    case Net::Error:
      break;
    }
    return report (transition, valuation, marking, status != Net::OK);
  }

  assert (!!net.getPlace (errorplace));
  reject (transition, valuation, marking, 0, net.getPlace (errorplace));
  return true;
}

void
StateReporter::reportSync (const class Transition& transition)
{
  assert (mySync && transition.getNumParents ());
  assert (!myFlattened);
  enabled (transition);
  mySync->add (transition, mySrc,
	       (mySrcSize + (sizeof (word_t) - 1)) / sizeof (word_t));
}

bool
StateReporter::addState (const void* state,
			 size_t size,
			 bool hidden)
{
  if (size > myMaxSize) myMaxSize = size;
  if (size < myMinSize) myMinSize = size;
  if (hidden && myLocal)
    return myLocal->add (state, size), false; // local state suppressed
  if (!mySuccessors && mySuppressed && mySuppressed->size () &&
      mySuppressed->add (state, size))
    return mySuccessors = One, false; // path compression applied
  mySuccessors = Many;
  return do_addState (state, size);
}

word_t*
StateReporter::popState (bool tail)
{
  delete[] mySrc;
  if (myLocal) {
    if ((mySrc = myLocal->pop (tail, mySrcSize)))
      return mySrc;
    if (myLocal->getNumStates ())
      addEvents ();
    myLocal->clear ();
  }
  if (mySuppressed) {
    if (const size_t num = mySuppressed->size ()) {
      const struct States::state& s = (*mySuppressed)[num - 1];
      switch (mySuccessors) {
      case One:
	mySrc = new word_t[1 + (s.size - 1) / sizeof (word_t)];
	memcpy (mySrc, s.buf, mySrcSize = s.size);
	mySuccessors = None;
	return mySrc;
      case Many:
	do_addState (s.buf, s.size);
	mySuccessors = None;
	// fall through
      case None:
	mySuppressed->clear ();
      }
    }
  }
  if ((mySrc = do_popState (tail, mySrcSize)) && mySuppressed)
    if (!mySuppressed->add (mySrc, mySrcSize))
      assert (false);
  return mySrc;
}

void
StateReporter::addEvents ()
{
}

void
StateReporter::reject (const class Transition& transition,
		       const class Valuation& valuation,
		       const class GlobalMarking& marking,
		       const char* reason,
		       const class Place* place)
{
  if (valuation.getError () == errFatal)
    flagFatal ();
  reject ();
  transition.report (valuation, marking, reason, place, printer);
  enabled (transition);
}