DiaDes  0.1
DIAgnosis of Discrete-Event System
PetriNet.hh
Go to the documentation of this file.
1 #ifndef __DIADES__PETRI__PETRINET_HH_
2 #define __DIADES__PETRI__PETRINET_HH_
3 
4 #include <sstream>
5 #include <graph/Graph.hh>
6 #include <graph/NodeMap.hh>
7 #include <utils/Substring.hh>
8 #include <set>
9 #include "PetriNetException.hh"
10 #include "Event.hh"
11 
12 
13 
14 using namespace std;
15 
16 
17 namespace Diades
18 {
19  namespace Petri
20  {
27 
34 
35 
42 
43 
44 
45  class Zsl;
46  class ZslFactory;
47  class Vdd;
48  class VddFactory;
49 
61  class PetriNet
62  {
63  protected:
66 
67 
68 
69  public:
70 
76  typedef enum {P,T} NodeType;
77 
78 
79  public:
80 
89  class Iterator
90  {
91  private:
92  list<Arc>::const_iterator _it;
93  bool _pre;
94  public:
106  Iterator(list<Arc>::const_iterator it, bool pre):
107  _it(it),_pre(pre){}
108 
115  Place operator *() const
116  {
117  if(_pre)
118  {
119  return _it->source();
120  }
121  return _it->target();
122  }
123 
128  Iterator & operator ++()
129  {
130  ++_it;
131  return *this;
132  }
133 
138  Iterator operator ++(int)
139  {
140  list<Arc>::const_iterator _it2= _it;
141  ++_it;
142  return Iterator(_it2,_pre);
143  }
144 
151  Place operator ->(void) const
152  {
153  if(_pre)
154  {
155  return _it->source();
156  }
157  return _it->target();
158  }
159 
165  bool operator ==(const Iterator & it) const
166  {
167  return (_pre == it._pre) && (_it == it._it);
168  }
169 
170 
176  bool operator !=(const Iterator & it) const
177  {
178  return !(*this == it);
179  }
180  };
181 
182  public:
183 
193  {
194 
195  private:
197  const PetriNet * _net;
199 
200  public:
210 
217  Diades::Graph::Node operator *() const { return *_it; }
218 
225  Diades::Graph::Node operator ->(void) const { return *_it; }
226 
232  bool operator == (const PerTypeIterator & it) const
233  {
234  return (_it == it._it) && (_net == it._net) && (_type == it._type);
235  }
236 
242  bool operator != (const PerTypeIterator & it) const
243  {
244  return !(*this == it);
245  }
246 
253  PerTypeIterator & operator ++();
254 
261  PerTypeIterator operator ++(int);
262  };
263 
264  friend class Iterator;
265  friend class PerTypeIterator;
266 
267  public:
274 
281 
282 
288  typedef set<Event>::const_iterator EventIterator;
289 
294  typedef set<Transition>::const_iterator TransitionEventIterator;
295 
296  protected:
297  typedef enum{NotPostponable,Postponable} TransitionType;
299  Graph::NodeMap<NodeType> _nodeType;
300  Graph::NodeMap<string> _labelNode;
301  Graph::NodeMap<string> _infoNode;
302  Graph::NodeMap<Event> _event;
303  Graph::NodeMap<int> _postponable;
304  string _name;
305  mutable const Vdd * _initialMarking;
307  unordered_map<string,Graph::Node> _placeOfLabel;
308  vector<set<Transition> > _transitionsOfEventId;
309  mutable Graph::NodeMap<const Zsl *> _preZslOfTransition;
310  mutable Graph::NodeMap<const Zsl *> _incidenceZslOfTransition;
311  mutable const Vdd * _reachableNodes;
312  set<Event> _eventList;
313 
314 
315 
316  private:
317  int _nbPlace;
318  int _nbTrans;
319 
320  protected:
321  mutable bool _readOnly;
322 
323  private:
324  unsigned int _id;
325 
326  public:
327 
328 
329 
334 
338  PetriNet();
339 
362  PetriNet(const PetriNet & p1, const PetriNet & p2,
363  const set<Event> & synchronization, bool strict = true);
364 
365 
369  virtual ~PetriNet() {
370  _graph.destroy();
371  }
372 
374 
375 
376 
381 
382 
387  void setName(const string & name)
388  {
389  require(PetriNetInvalid, !_readOnly, "setName: read only");
390  if(!_readOnly) { _name = name; }
391  else { printWarning("setName"); }
392  }
393 
398  const string & name() const { return _name; }
399 
400 
405  unsigned int id() const
406  {
407  return _id;
408  }
409 
414  void setId(unsigned id)
415  {
416  _id = id;
417  }
418 
423  int numberOfPlaces() const { return _nbPlace; }
428  int numberOfTransitions() const { return _nbTrans; }
429 
433  void clear();
434 
439  const Diades::Graph::Graph & graph() const { return _graph; }
440 
442 
443 
444 
449 
454  EventIterator beginOfEvents() const
455  {
456  return _eventList.begin();
457  }
458 
459 
464  EventIterator endOfEvents() const
465  {
466  return _eventList.end();
467  }
468 
474  const Event & getEvent(Transition t) const
475  {
476  require(PetriNetInvalid,t.valid(),"getEvent: invalid transition");
477  require(PetriNetInvalid,t.owner() == _graph,"getEvent: the transition does not belong to the Petri Net");
478  require(PetriNetInvalid, _nodeType[t] == T, "getEvent; this is not a transition but a place" );
479  return _event[t];
480  }
481 
488  virtual void setEvent(Transition t, const Event & event)
489  {
490  require(PetriNetInvalid,t.valid(),"setEvent: invalid transition");
491  require(PetriNetInvalid,t.owner() == _graph,
492  "setEvent: the transition does not belong to the Petri Net");
493  require(PetriNetInvalid, _nodeType[t] == T,
494  "setEvent; this is not a transition but a place" );
495  require(PetriNetInvalid, !_readOnly, "setEvent: read only");
496  if(!_readOnly)
497  {
498  _eventList.insert(event);
499  _event[t] = event;
500  if(_transitionsOfEventId.size() <= event.id())
501  {
502  _transitionsOfEventId.resize(event.id()+1);
503  }
504  _transitionsOfEventId[event.id()].insert(t);
505  }
506  else
507  {
508  printWarning("setEvent");
509  }
510  }
511 
512 
518  bool containsEvent(const Event & event) const
519  {
520  if(_transitionsOfEventId.size() <= event.id())
521  {
522  return false;
523  }
524  return !_transitionsOfEventId[event.id()].empty();
525  }
526 
528 
533 
540  Place newPlace() {
541  require(PetriNetInvalid, !_readOnly, "newPlace: read only");
542  Place p;
543  if(!_readOnly)
544  {
545  ++_nbPlace;
546  p = _graph.newNode();
547  _nodeType[p] = P;
548  stringstream s;
549  s << "__p" << p.id();
550  s >> _labelNode[p];
551  _infoNode[p] = "";
552  }
553  else
554  {
555  printWarning("newPlace");
556  }
557  ensure(PetriNetInvalid, p.valid(), "newPlace: invalid returned place");
558  ensure(PetriNetInvalid, p.owner() == graph(), "newPlace: invalid returned place");
559  return p;
560  }
561 
568  Place newPlace(const string & label);
569 
570 
571 
572 
573 
578  PlaceIterator beginOfPlaces() const
579  {
580  return PerTypeIterator(this,_graph.nodeBegin(),P);
581  }
582 
583 
588  PlaceIterator endOfPlaces() const
589  {
590  return PerTypeIterator(this,_graph.nodeEnd(),P);
591  }
592 
593 
594 
595 
601  const string & labelOfPlace(const Place & p) const
602  {
603  return _labelNode[p];
604  }
605 
612  const string & getInfoLabel(Place p) const
613  {
614  require(PetriNetInvalid,p.valid(),"getInfoLabel: invalid place");
615  require(PetriNetInvalid,p.owner() == _graph,"getInfoLabel: the place does not belong to the Petri Net");
616  require(PetriNetInvalid, _nodeType[p] == P, "getInfoLabel: this is not a place but a transition" );
617  return _infoNode[p];
618  }
619 
620 
621 
628  void setInfoLabel(Place p, const string & info)
629  {
630  require(PetriNetInvalid,p.valid(),"setInfoLabel: invalid place");
631  require(PetriNetInvalid,p.owner() == _graph,"setInfoLabel: the place does not belong to the Petri Net");
632  require(PetriNetInvalid, _nodeType[p] == P, "setInfoLabel: this is not a place but a transition" );
633  require(PetriNetInvalid, !_readOnly, "setInfoLabel: read only");
634  _infoNode[p] = info;
635  }
636 
637 
638 
639 
641 
642 
647 
648 
649 
656  virtual Transition newTransition(const set<Place> & pre, const set<Place> & post);
665  virtual Transition newTransition(const set<Place> & pre, const set<Place> & post, const string & label)
666  {
667  require(PetriNetInvalid, !_readOnly, "newTransition: read only");
668  Transition trans;
669  if(!_readOnly)
670  {
671  trans = newTransition(pre,post); _labelNode[trans] = label;
672  }
673  else
674  {
675  printWarning("newTransition");
676  }
677  return trans;
678  }
679 
687  void addPre(Transition transition, Place place)
688  {
689  _graph.newEdge(place,transition);
690  }
691 
699  void addPost(Transition transition, Place place)
700  {
701  _graph.newEdge(transition,place);
702  }
703 
710  bool pre(const Vdd * marking, const Transition & t) const;
711 
712  // /**
713  // * @param marking a possible marking of the PetriNet
714  // * @param t a Transition
715  // * @return true if the marking 'marking' is possible after firing the Transition 't'
716  // * @pre marking.owner() == this
717  // */
718  // bool post(const Vdd * marking, const Transition & t) const;
719 
720 
721 
722 
728  Iterator preBegin(const Transition & transition) const
729  {
730  return Iterator(transition.inEdgeBegin(),true);
731  }
732 
738  Iterator preEnd(const Transition & transition) const
739  {
740  return Iterator(transition.inEdgeEnd(),true);
741  }
742 
748  Iterator postBegin(const Transition & transition) const
749  {
750  return Iterator(transition.outEdgeBegin(),false);
751  }
752 
753 
759  Iterator postEnd(const Transition & transition) const
760  {
761  return Iterator(transition.outEdgeEnd(),false);
762  }
763 
764 
769  TransitionIterator beginOfTransitions() const
770  {
771  return PerTypeIterator(this,_graph.nodeBegin(),T);
772  }
773 
774 
779  TransitionIterator endOfTransitions() const
780  {
781  return PerTypeIterator(this,_graph.nodeEnd(),T);
782  }
783 
784 
790  const string & labelOfTransition(const Transition & t) const
791  {
792  return _labelNode[t];
793  }
794 
801  bool inPre(const Transition & t, const Place & p) const;
802 
808  TransitionEventIterator beginOfTransitionWithEvent(const Event & event) const
809  {
810  require(PetriNetInvalid,containsEvent(event),
811  "beginOfTransitionWithEvent: the event is not associated to a transition of this Petri Net");
812  return _transitionsOfEventId[event.id()].begin();
813  }
814 
815 
816 
822  TransitionEventIterator endOfTransitionWithEvent(const Event & event) const
823  {
824  require(PetriNetInvalid,containsEvent(event),
825  "endOfTransitionWithEvent: the event is not associated to a transition of this Petri Net");
826  return _transitionsOfEventId[event.id()].end();
827  }
828 
829 
834  virtual void deleteTransition(Transition t);
835 
843  virtual Transition duplicateTransition(Transition t);
844 
845 
847 
848 
853 
854 
855 
868  void setPostponable(Transition t);
869 
875  bool isPostponable(Transition t) const
876  {
877  require(PetriNetInvalid, t.valid(),"isPostponable: invalid transition");
878  require(PetriNetInvalid, t.owner() == graph(), "isPostponable: the transition does not belong to the Petri Net");
879  return _postponable[t] == Postponable;
880  }
881 
889  void setNotPostponable(Transition t);
890 
891 
908  void synchronize(const PetriNet & p1, const PetriNet & p2,
909  const set<Event> & synchronization, bool strict);
910 
911 
913 
914 
919 
925  void setMarking(const set<Place> & marking);
926 
931  bool isMarked() const
932  {
933  return _initialMarking != 0;
934  }
935 
936 
941  bool isMarked(Place p) const;
942 
943 
949  void mark(Place p);
950 
951 
957  void unmark(Place p);
958 
960 
961 
962 
967 
968 
974  virtual void net2Dot(const string & fileName) const;
975 
976 
984  virtual void net2Tina(ostream & os) const;
985 
986 
991  void printWarning(const string & s) const
992  {
993  cerr << "WARNING: attempt to modify a read-only net (" << s << ")" << endl;
994  }
995 
1000  void printFactory(const string & fileName) const;
1001 
1002 
1004 
1012 
1013 
1019  void lock() const;
1020 
1027  void unlock() const;
1028 
1033  const Vdd * marking() const;
1034 
1043  const Zsl * getPreZsl(Transition t) const;
1044 
1052  const Zsl * getIncidenceZsl(Transition t) const;
1053 
1058  const Vdd * reachableNodes() const;
1059 
1066  void printVdd(const Vdd * result, string fileName);
1067 
1072  {
1073  return _zslFactory;
1074  }
1075 
1076 
1081  {
1082  return _vddFactory;
1083  }
1084 
1086 
1087  };
1088 
1089  };
1090 
1091 };
1092 
1093 #endif
1094 
1095 
void setInfoLabel(Place p, const string &info)
Definition: PetriNet.hh:628
set< Event >::const_iterator EventIterator
Definition: PetriNet.hh:288
TransitionIterator endOfTransitions() const
Definition: PetriNet.hh:779
virtual void setEvent(Transition t, const Event &event)
Definition: PetriNet.hh:488
Iterator(list< Arc >::const_iterator it, bool pre)
Definition: PetriNet.hh:106
bool isPostponable(Transition t) const
Definition: PetriNet.hh:875
Diades::Graph::Node Place
Definition: PetriNet.hh:26
VddFactory * vddFactory() const
Definition: PetriNet.hh:1080
PerTypeIterator TransitionIterator
Definition: PetriNet.hh:273
set< Event > _eventList
Definition: PetriNet.hh:312
Graph::NodeMap< string > _infoNode
Definition: PetriNet.hh:301
STL namespace.
list< Arc >::const_iterator _it
Definition: PetriNet.hh:92
VddFactory * _vddFactory
Definition: PetriNet.hh:65
TransitionEventIterator endOfTransitionWithEvent(const Event &event) const
Definition: PetriNet.hh:822
#define ensure(Exception, expr, message)
Definition: Assertion.hh:64
Iterator preEnd(const Transition &transition) const
Definition: PetriNet.hh:738
Diades::Graph::Edge Arc
Definition: PetriNet.hh:41
EventIterator endOfEvents() const
Definition: PetriNet.hh:464
ZslFactory * zslFactory() const
Definition: PetriNet.hh:1071
set< Transition >::const_iterator TransitionEventIterator
Definition: PetriNet.hh:294
bool isMarked() const
Definition: PetriNet.hh:931
Graph::NodeMap< const Zsl * > _incidenceZslOfTransition
Definition: PetriNet.hh:310
unordered_map< string, Graph::Node > _placeOfLabel
Definition: PetriNet.hh:307
vector< set< Transition > > _transitionsOfEventId
Definition: PetriNet.hh:308
const Vdd * _reachableNodes
Definition: PetriNet.hh:311
GraphIterator< Node > NodeIterator
Definition: GraphData.hh:130
const string & getInfoLabel(Place p) const
Definition: PetriNet.hh:612
TransitionEventIterator beginOfTransitionWithEvent(const Event &event) const
Definition: PetriNet.hh:808
const string & name() const
Definition: PetriNet.hh:398
void deleteTransition(Component &comp, InputIterator first, InputIterator last)
Definition: Component.hh:1162
void addPre(Transition transition, Place place)
Definition: PetriNet.hh:687
size_t id() const
Definition: Event.hh:156
int numberOfPlaces() const
Definition: PetriNet.hh:423
#define require(Exception, expr, message)
Definition: Assertion.hh:56
Namespace of the Diades project.
set< Place > _explicitInitialMarking
Definition: PetriNet.hh:306
const Diades::Graph::Graph & graph() const
Definition: PetriNet.hh:439
Graph::NodeMap< int > _postponable
Definition: PetriNet.hh:303
const string & labelOfTransition(const Transition &t) const
Definition: PetriNet.hh:790
Graph::Graph _graph
Definition: PetriNet.hh:298
void setName(const string &name)
Definition: PetriNet.hh:387
Graph::NodeMap< Event > _event
Definition: PetriNet.hh:302
TransitionIterator beginOfTransitions() const
Definition: PetriNet.hh:769
Graph::NodeMap< const Zsl * > _preZslOfTransition
Definition: PetriNet.hh:309
boost::adjacency_list Graph
Definition: ScaleFree.cc:9
int numberOfTransitions() const
Definition: PetriNet.hh:428
void addPost(Transition transition, Place place)
Definition: PetriNet.hh:699
Iterator postBegin(const Transition &transition) const
Definition: PetriNet.hh:748
Graph::NodeMap< string > _labelNode
Definition: PetriNet.hh:300
Graph::NodeMap< NodeType > _nodeType
Definition: PetriNet.hh:299
const Vdd * _initialMarking
Definition: PetriNet.hh:305
1-bounded Petri nets
Definition: PetriNet.hh:61
Iterator postEnd(const Transition &transition) const
Definition: PetriNet.hh:759
const Event & getEvent(Transition t) const
Definition: PetriNet.hh:474
virtual Transition newTransition(const set< Place > &pre, const set< Place > &post, const string &label)
Definition: PetriNet.hh:665
PlaceIterator beginOfPlaces() const
Definition: PetriNet.hh:578
PerTypeIterator PlaceIterator
Definition: PetriNet.hh:280
const string & labelOfPlace(const Place &p) const
Definition: PetriNet.hh:601
Diades::Graph::Node Transition
Definition: PetriNet.hh:33
Iterator preBegin(const Transition &transition) const
Definition: PetriNet.hh:728
ZslFactory * _zslFactory
Definition: PetriNet.hh:64
PlaceIterator endOfPlaces() const
Definition: PetriNet.hh:588
EventIterator beginOfEvents() const
Definition: PetriNet.hh:454
void setId(unsigned id)
Definition: PetriNet.hh:414
Iterators over the places or the transitions of the PetriNet.
Definition: PetriNet.hh:192
void printWarning(const string &s) const
Definition: PetriNet.hh:991
Iterator on the PetriNet.
Definition: PetriNet.hh:89
unsigned int id() const
Definition: PetriNet.hh:405
bool containsEvent(const Event &event) const
Definition: PetriNet.hh:518