DiaDes  0.1
DIAgnosis of Discrete-Event System
Net.hh
Go to the documentation of this file.
1 #ifndef __DIADES__PETRI__NET_HH_
2 #define __DIADES__PETRI__NET_HH_
3 
4 #include <sstream>
5 #include <diades/graph/Graph.hh>
10 #include <set>
12 
13 
14 
15 using namespace std;
16 
17 
18 namespace Diades
19 {
20  namespace Petri
21  {
27  typedef Diades::Graph::Node Place;
28 
35 
36 
42  typedef Diades::Graph::Edge Arc;
43 
44 
45 
46  class Zsl;
47  class ZslFactory;
48  class Vdd;
49  class VddFactory;
50 
62  class Net
63  {
64  public:
65 
71  static string typeName() { return "Diades::Petri::Net"; }
72 
78 
79 
80 
81  protected:
84 
85 
86 
87  public:
88 
94  typedef enum {P,T} NodeType;
95 
96 
97  public:
98 
108  {
109  private:
110  list<Arc>::const_iterator _it;
111  bool _pre;
112  public:
124  ConstIterator(list<Arc>::const_iterator it, bool pre):
125  _it(it),_pre(pre){}
126 
133  const Place & operator *() const
134  {
135  if(_pre)
136  {
137  return _it->sourceRef();
138  }
139  return _it->targetRef();
140  }
141 
146  ConstIterator & operator ++()
147  {
148  ++_it;
149  return *this;
150  }
151 
156  ConstIterator operator ++(int)
157  {
158  list<Arc>::const_iterator _it2= _it;
159  ++_it;
160  return ConstIterator(_it2,_pre);
161  }
162 
169  const Place * operator ->(void) const
170  {
171  if(_pre)
172  {
173  return &_it->sourceRef();
174  }
175  return &_it->targetRef();
176  }
177 
183  bool operator ==(const ConstIterator & it) const
184  {
185  return (_pre == it._pre) && (_it == it._it);
186  }
187 
188 
194  bool operator !=(const ConstIterator & it) const
195  {
196  return !(*this == it);
197  }
198  };
199 
200  public:
201 
211  {
212 
213  private:
215  const Net * _net;
217 
218  public:
227  PerTypeIterator(const Net * net, Graph::NodeIterator it, Net::NodeType type);
228 
235  Diades::Graph::Node & operator *() const { return *_it; }
236 
243  Diades::Graph::Node * operator ->(void) const { return &(*_it); }
244 
250  bool operator == (const PerTypeIterator & it) const
251  {
252  return (_it == it._it) && (_net == it._net) && (_type == it._type);
253  }
254 
260  bool operator != (const PerTypeIterator & it) const
261  {
262  return !(*this == it);
263  }
264 
271  PerTypeIterator & operator ++();
272 
279  PerTypeIterator operator ++(int);
280  };
281 
282  friend class Iterator;
283  friend class PerTypeIterator;
284 
285  public:
292 
299 
300 
301 
302 
303 
304 
305 
311  typedef set<Event>::const_iterator EventIterator;
312 
317  typedef set<Transition>::const_iterator TransitionEventIterator;
318 
319 
320  protected:
321  typedef enum{NotPostponable,Postponable} TransitionType;
323  Graph::NodeMap<NodeType> _nodeType;
324  Graph::NodeMap<string> _labelNode;
325  Graph::NodeMap<string> _infoNode;
326  Graph::NodeMap<Event> _event;
327  Graph::NodeMap<int> _postponable;
328  string _name;
329  mutable const Vdd * _initialMarking;
331  unordered_map<string,Graph::Node> _placeOfLabel;
332  vector<set<Transition> > _transitionsOfEventId;
333  mutable Graph::ConstNodeMap<const Zsl *> _preZslOfTransition;
334  mutable Graph::ConstNodeMap<const Zsl *> _incidenceZslOfTransition;
335  mutable const Vdd * _reachableNodes;
336  set<Event> _eventList;
337 
338 
339 
340  private:
341  size_t _nbPlaces;
343 
344  protected:
345  mutable bool _readOnly;
346 
347  private:
348  unsigned int _id;
349 
350  public:
351 
352 
353 
358 
362  Net();
363 
387  Net(const Net & p1, const Net & p2,
388  const set<Event> & synchronization, bool strict = true);
389 
390 
394  virtual ~Net() {
395  }
396 
398 
399 
400 
405 
406 
411  void setName(const string & name)
412  {
413  require(Exception, !_readOnly, "setName: read only");
414  if(!_readOnly) { _name = name; }
415  else { printWarning("setName"); }
416  }
417 
422  const string & name() const { return _name; }
423 
424 
429  unsigned int id() const
430  {
431  return _id;
432  }
433 
438  void setId(unsigned id)
439  {
440  _id = id;
441  }
442 
443 
444 
448  void clear();
449 
454  const Diades::Graph::Graph & graph() const { return _graph; }
455 
457 
458 
459 
464 
469  EventIterator beginOfEvents() const
470  {
471  return _eventList.begin();
472  }
473 
474 
479  EventIterator endOfEvents() const
480  {
481  return _eventList.end();
482  }
483 
489  const Event & getEvent(Transition t) const
490  {
491 
492  require(Exception,t.valid(),"getEvent: invalid transition");
493  require(Exception,&t.owner() == &_graph,"getEvent: the transition does not belong to the Petri Net");
494  require(Exception, _nodeType[t] == T, "getEvent; this is not a transition but a place" );
495  return _event[t];
496  }
497 
504  virtual void setEvent(Transition t, Event event)
505  {
506  require(Exception,t.valid(),"setEvent: invalid transition");
507  require(Exception,&t.owner() == &_graph,
508  "setEvent: the transition does not belong to the Petri Net");
509  require(Exception, _nodeType[t] == T,
510  "setEvent; this is not a transition but a place" );
511  require(Exception, !_readOnly, "setEvent: read only");
512  if(!_readOnly)
513  {
514  _eventList.insert(event);
515  _event[t] = event;
516  if(_transitionsOfEventId.size() <= event)
517  {
518  _transitionsOfEventId.resize(event+1);
519  }
520  _transitionsOfEventId[event].insert(t);
521  }
522  else
523  {
524  printWarning("setEvent");
525  }
526  }
527 
528 
534  bool containsEvent(Event event) const
535  {
536  if(_transitionsOfEventId.size() <= event)
537  {
538  return false;
539  }
540  return !_transitionsOfEventId[event].empty();
541  }
542 
544 
549 
557  Place newPlace() {
558  require(Exception, !_readOnly, "newPlace: read only");
559  Place p;
560  if(!_readOnly)
561  {
562  ++_nbPlaces;
563  p = _graph.newNode();
564  _nodeType[p] = P;
565  stringstream s;
566  s << "__p" << p.id();
567  s >> _labelNode[p];
568  _infoNode[p] = "";
569  }
570  else
571  {
572  printWarning("newPlace");
573  }
574  ensure(Exception, p.valid(), "newPlace: invalid returned place");
575  ensure(Exception, &p.owner() == &graph(), "newPlace: invalid returned place");
576  return p;
577  }
578 
579 
586  Place newPlace(const string & label);
587 
588 
589 
590  void setLabelOfPlace(const Place & place, const string & label)
591  {
592  require(Exception, !_readOnly, "setLabelOfPlace: read only");
593  require(Exception,_placeOfLabel.find(label) == _placeOfLabel.end(),
594  "setLabelOfPlace: this label already exists in the net");
595  _labelNode[place] = label;
596  _placeOfLabel[label] = place;
597  }
598 
599 
600 
605  PlaceIterator beginOfPlaces() const
606  {
607  return PerTypeIterator(this,_graph.nodeBegin(),P);
608  }
609 
610 
615  PlaceIterator endOfPlaces() const
616  {
617  return PerTypeIterator(this,_graph.nodeEnd(),P);
618  }
619 
620 
627  size_t numberOfPlaces() const
628  {
629  return _nbPlaces;
630  }
631 
632 
638  const string & labelOfPlace(const Place & p) const
639  {
640  return _labelNode[p];
641  }
642 
649  const string & getInfoLabel(Place p) const
650  {
651  require(Exception,p.valid(),"getInfoLabel: invalid place");
652  require(Exception,&p.owner() == &_graph,"getInfoLabel: the place does not belong to the Petri Net");
653  require(Exception, _nodeType[p] == P, "getInfoLabel: this is not a place but a transition" );
654  return _infoNode[p];
655  }
656 
657 
658 
665  void setInfoLabel(Place p, const string & info)
666  {
667  require(Exception,p.valid(),"setInfoLabel: invalid place");
668  require(Exception,&p.owner() == &_graph,"setInfoLabel: the place does not belong to the Petri Net");
669  require(Exception, _nodeType[p] == P, "setInfoLabel: this is not a place but a transition" );
670  require(Exception, !_readOnly, "setInfoLabel: read only");
671  _infoNode[p] = info;
672  }
673 
674 
675 
676 
678 
679 
684 
685 
686 
695  virtual bool enables(const Transition & t,const set<Place> & marking);
696 
697 
698 
699 
700 
707  virtual Transition newTransition(const set<Place> & pre, const set<Place> & post);
708 
709 
710  public:
719  virtual Transition newTransition(const set<Place> & pre, const set<Place> & post, const string & label)
720  {
721  require(Exception, !_readOnly, "newTransition: read only");
722  Transition trans;
723  if(!_readOnly)
724  {
725  trans = newTransition(pre,post);
726  setLabelOfTransition(trans,label);
727  }
728  else
729  {
730  printWarning("newTransition");
731  }
732  return trans;
733  }
734 
735 
742  size_t numberOfTransitions() const
743  {
744  return _nbTransitions;
745  }
746 
753  size_t numberOfArcs() const
754  {
755  return _graph.numberOfEdges();
756  }
757 
765  void addPre(Transition transition, Place place)
766  {
767  _graph.newEdge(place,transition);
768  }
769 
777  void addPost(Transition transition, Place place)
778  {
779  _graph.newEdge(transition,place);
780  }
781 
788  bool pre(const Vdd * marking, const Transition & t) const;
789 
790  // /**
791  // * @param marking a possible marking of the Net
792  // * @param t a Transition
793  // * @return true if the marking 'marking' is possible after firing the Transition 't'
794  // * @pre marking.owner() == this
795  // */
796  // bool post(const Vdd * marking, const Transition & t) const;
797 
798 
807  ConstIterator preTransBegin(const Place & place) const
808  {
809  return ConstIterator(place.inEdgeBegin(),true);
810  }
811 
812 
821  ConstIterator preTransEnd(const Place & place) const
822  {
823  return ConstIterator(place.inEdgeEnd(),true);
824  }
825 
834  ConstIterator postTransBegin(const Place & place) const
835  {
836  return ConstIterator(place.outEdgeBegin(),false);
837  }
838 
839 
848  ConstIterator postTransEnd(const Place & place) const
849  {
850  return ConstIterator(place.outEdgeEnd(),false);
851  }
852 
858  ConstIterator preBegin(const Transition & transition) const
859  {
860  return ConstIterator(transition.inEdgeBegin(),true);
861  }
862 
868  ConstIterator preEnd(const Transition & transition) const
869  {
870  return ConstIterator(transition.inEdgeEnd(),true);
871  }
872 
878  ConstIterator postBegin(const Transition & transition) const
879  {
880  return ConstIterator(transition.outEdgeBegin(),false);
881  }
882 
883 
889  ConstIterator postEnd(const Transition & transition) const
890  {
891  return ConstIterator(transition.outEdgeEnd(),false);
892  }
893 
894 
899  TransitionIterator beginOfTransitions() const
900  {
901  return PerTypeIterator(this,_graph.nodeBegin(),T);
902  }
903 
904 
909  TransitionIterator endOfTransitions() const
910  {
911  return PerTypeIterator(this,_graph.nodeEnd(),T);
912  }
913 
914 
915  void setLabelOfTransition(const Transition & t, const string & label)
916  {
917  require(Exception, !_readOnly, "setLabelOfTransition: read only");
918  _labelNode[t] = label;
919  }
920 
926  const string & labelOfTransition(const Transition & t) const
927  {
928  return _labelNode[t];
929  }
930 
937  bool inPre(const Transition & t, const Place & p) const;
938 
944  TransitionEventIterator beginOfTransitionWithEvent(Event event) const
945  {
946  require(Exception,containsEvent(event),
947  "beginOfTransitionWithEvent: the event is not associated to a transition of this Petri Net");
948  return _transitionsOfEventId[event].begin();
949  }
950 
951 
952 
958  TransitionEventIterator endOfTransitionWithEvent(Event event) const
959  {
960  require(Exception,containsEvent(event),
961  "endOfTransitionWithEvent: the event is not associated to a transition of this Petri Net");
962  return _transitionsOfEventId[event].end();
963  }
964 
965 
970  virtual void deleteTransition(Transition t);
971 
979  virtual Transition duplicateTransition(Transition t);
980 
981 
983 
984 
989 
990 
991 
1004  void setPostponable(Transition t);
1005 
1011  bool isPostponable(Transition t) const
1012  {
1013  require(Exception, t.valid(),"isPostponable: invalid transition");
1014  require(Exception, &t.owner() == &graph(), "isPostponable: the transition does not belong to the Petri Net");
1015  return _postponable[t] == Postponable;
1016  }
1017 
1025  void setNotPostponable(Transition t);
1026 
1027 
1045  void synchronize(const Net & p1, const Net & p2,
1046  const set<Event> & synchronization, bool strict);
1047 
1048 
1049 
1051 
1052 
1057 
1063  void setMarking(const set<Place> & marking);
1064 
1069  bool isMarked() const
1070  {
1071  return _initialMarking != 0;
1072  }
1073 
1074 
1079  bool isMarked(Place p) const;
1080 
1081 
1087  void mark(Place p);
1088 
1089 
1095  void unmark(Place p);
1096 
1097 
1103  const set<Place> & initialMarking() const
1104  {
1105  return _explicitInitialMarking;
1106  }
1107 
1108 
1109 
1111 
1112 
1113 
1118 
1119 
1125  virtual void net2Dot(const string & fileName) const;
1126 
1127 
1135  virtual void net2Tina(ostream & os) const;
1136 
1137 
1142  void printWarning(const string & s) const
1143  {
1144  cerr << "WARNING: attempt to modify a read-only net (" << s << ")" << endl;
1145  }
1146 
1151  void printFactory(const string & fileName) const;
1152 
1153 
1155 
1163 
1164 
1170  void lock() const;
1171 
1178  void unlock() const;
1179 
1184  const Vdd * marking() const;
1185 
1194  const Zsl * getPreZsl(Transition t) const;
1195 
1203  const Zsl * getIncidenceZsl(Transition t) const;
1204 
1209  const Vdd * reachableNodes() const;
1210 
1217  void printVdd(const Vdd * result, string fileName);
1218 
1223  {
1224  return _zslFactory;
1225  }
1226 
1227 
1232  {
1233  return _vddFactory;
1234  }
1235 
1237 
1238  };
1239 
1240  };
1241 
1242 };
1243 
1244 #endif
1245 
1246 
TransitionIterator endOfTransitions() const
Definition: Net.hh:909
set< Place > _explicitInitialMarking
Definition: Net.hh:330
VddFactory * _vddFactory
Definition: Net.hh:83
void setLabelOfTransition(const Transition &t, const string &label)
Definition: Net.hh:915
Graph::NodeIterator _it
Definition: Net.hh:214
size_t numberOfTransitions() const
Definition: Net.hh:742
ConstIterator preEnd(const Transition &transition) const
Definition: Net.hh:868
const set< Place > & initialMarking() const
Definition: Net.hh:1103
bool synchronize(const std::vector< typename Ptr< Fsm >::ConstP > &components, const SynchronisationRules< Fsm > &rules, Fsm &synchronisation, StateCreator &creator)
Definition: Synchronize.hh:36
static string typeName()
Definition: Net.hh:71
ConstIterator preBegin(const Transition &transition) const
Definition: Net.hh:858
string _name
Definition: Net.hh:328
PerTypeIterator PlaceIterator
Definition: Net.hh:298
EventIterator beginOfEvents() const
Definition: Net.hh:469
ConstIterator postBegin(const Transition &transition) const
Definition: Net.hh:878
const Vdd * _initialMarking
Definition: Net.hh:329
vector< set< Transition > > _transitionsOfEventId
Definition: Net.hh:332
ConstIterator postTransBegin(const Place &place) const
Definition: Net.hh:834
bool isMarked() const
Definition: Net.hh:1069
Place newPlace()
Definition: Net.hh:557
EventIterator endOfEvents() const
Definition: Net.hh:479
STL namespace.
virtual ~Net()
Definition: Net.hh:394
Diades::Utils::Exception< Net > Exception
Definition: Net.hh:77
Diades::Graph::Node Place
Definition: BoundedNet.hh:24
Graph::ConstNodeMap< const Zsl * > _incidenceZslOfTransition
Definition: Net.hh:334
TransitionEventIterator endOfTransitionWithEvent(Event event) const
Definition: Net.hh:958
virtual void setEvent(Transition t, Event event)
Definition: Net.hh:504
#define ensure(Exception, expr, message)
Definition: Assertion.hh:98
PlaceIterator beginOfPlaces() const
Definition: Net.hh:605
bool isPostponable(Transition t) const
Definition: Net.hh:1011
const string & labelOfTransition(const Transition &t) const
Definition: Net.hh:926
const Event & getEvent(Transition t) const
Definition: Net.hh:489
Graph::NodeMap< int > _postponable
Definition: Net.hh:327
set< Event >::const_iterator EventIterator
Definition: Net.hh:311
Graph::NodeMap< string > _infoNode
Definition: Net.hh:325
Graph::NodeMap< string > _labelNode
Definition: Net.hh:324
virtual Transition newTransition(const set< Place > &pre, const set< Place > &post, const string &label)
Definition: Net.hh:719
void setLabelOfPlace(const Place &place, const string &label)
Definition: Net.hh:590
list< Arc >::const_iterator _it
Definition: Net.hh:110
AutFsm::Transition Transition
Definition: Run.cc:73
void setId(unsigned id)
Definition: Net.hh:438
DdAutFsm::EventPropertyId Event
Definition: TrimState.cc:139
void addPre(Transition transition, Place place)
Definition: Net.hh:765
GraphIterator< Node > NodeIterator
Definition: GraphInt.hh:139
ZslFactory * zslFactory() const
Definition: Net.hh:1222
void deleteTransition(Component &comp, InputIterator first, InputIterator last)
Definition: Component.hh:1381
Graph::NodeMap< NodeType > _nodeType
Definition: Net.hh:323
PlaceIterator endOfPlaces() const
Definition: Net.hh:615
void printWarning(const string &s) const
Definition: Net.hh:1142
#define require(Exception, expr, message)
Definition: Assertion.hh:90
const string & getInfoLabel(Place p) const
Definition: Net.hh:649
Namespace of the Diades project.
ConstIterator postEnd(const Transition &transition) const
Definition: Net.hh:889
unordered_map< string, Graph::Node > _placeOfLabel
Definition: Net.hh:331
bool containsEvent(Event event) const
Definition: Net.hh:534
PerTypeIterator TransitionIterator
Definition: Net.hh:291
Graph::Graph _graph
Definition: Net.hh:322
void setName(const string &name)
Definition: Net.hh:411
VddFactory * vddFactory() const
Definition: Net.hh:1231
const string & labelOfPlace(const Place &p) const
Definition: Net.hh:638
Diades::Graph::Edge Arc
Definition: BoundedNet.hh:39
const string & name() const
Definition: Net.hh:422
ConstIterator on the Net.
Iterators over the places or the transitions of the Net.
Definition: Net.hh:210
boost::adjacency_list Graph
Definition: ScaleFree.cc:9
const Vdd * _reachableNodes
Definition: Net.hh:335
bool _readOnly
Definition: Net.hh:345
size_t _nbPlaces
Definition: Net.hh:341
1-bounded Petri nets
Definition: Net.hh:62
const Diades::Graph::Graph & graph() const
Definition: Net.hh:454
ConstIterator preTransEnd(const Place &place) const
Definition: Net.hh:821
ConstIterator preTransBegin(const Place &place) const
Definition: Net.hh:807
set< Transition >::const_iterator TransitionEventIterator
Definition: Net.hh:317
void setInfoLabel(Place p, const string &info)
Definition: Net.hh:665
size_t numberOfArcs() const
Definition: Net.hh:753
Graph::NodeMap< Event > _event
Definition: Net.hh:326
ZslFactory * _zslFactory
Definition: Net.hh:82
size_t numberOfPlaces() const
Definition: Net.hh:627
ConstIterator postTransEnd(const Place &place) const
Definition: Net.hh:848
ConstIterator(list< Arc >::const_iterator it, bool pre)
Definition: Net.hh:124
TransitionEventIterator beginOfTransitionWithEvent(Event event) const
Definition: Net.hh:944
unsigned int _id
Definition: Net.hh:348
unsigned int id() const
Definition: Net.hh:429
size_t _nbTransitions
Definition: Net.hh:342
void addPost(Transition transition, Place place)
Definition: Net.hh:777
set< Event > _eventList
Definition: Net.hh:336
TransitionIterator beginOfTransitions() const
Definition: Net.hh:899
Graph::ConstNodeMap< const Zsl * > _preZslOfTransition
Definition: Net.hh:333