1 #ifndef __DIADES__PETRI__BOUNDEDNET_HH_ 2 #define __DIADES__PETRI__BOUNDEDNET_HH_ 10 #include <unordered_set> 59 return "Diades::Petri::BoundedNet";
94 list<Arc>::const_iterator
_it;
121 return _it->sourceRef();
123 return _it->targetRef();
140 list<Arc>::const_iterator _it2 = _it;
153 return &_it->sourceRef();
155 return &_it->targetRef();
164 return (_pre == it.
_pre) && (_it == it.
_it);
173 return !(*
this == it);
230 return (_it == it.
_it) && (_net == it.
_net) && (_type == it.
_type);
239 return !(*
this == it);
290 NotPostponable, Postponable
379 require(Exception, !name.empty(),
"setName: the given name is empty.");
389 ensure(Exception, !_name.empty(),
"name: empty name -> invalid net");
397 unsigned int id()
const {
446 s <<
"__p" << p.id();
447 _labelOfNode[p] =
"";
448 setNameOfPlace(p, s.str());
449 ensure(Exception, p.valid(),
"newPlace: invalid returned place");
450 ensure(Exception, &p.owner() == &graph(),
"newPlace: invalid returned place");
461 Place newPlace(
const string & name);
467 void deletePlace(Place p);
475 require(Exception, place.valid(),
"setLabelOfPlace: invalid place");
476 require(Exception, &place.owner() == &graph(),
477 "setLabelOfPLace: this place is not own by the current net");
478 require(Exception, _nodeType[place] == P,
"setLabelOfPlace: this is not a place but a transition");
479 _labelOfNode[place] = label;
514 require(Exception, p.valid(),
"labelOfPlace: invalid place");
515 require(Exception, &p.owner() == &graph(),
516 "labelOfPLace: this place is not own by the current net");
517 require(Exception, _nodeType[p] == P,
"labelOfPlace: this is not a place but a transition");
518 return _labelOfNode[p];
528 require(Exception, p.valid(),
"nameOfPlace: invalid place");
529 require(Exception, &p.owner() == &_graph,
"nameOfPlace: the place does not belong to the Petri Net");
530 require(Exception, _nodeType[p] == P,
"nameOfPlace: this is not a place but a transition");
531 return _nameOfNode[p];
542 require(Exception, p.valid(),
"setNameOfPlace: invalid place");
543 require(Exception, &p.owner() == &_graph,
"setNameOfPlace: the place does not belong to the Petri Net");
544 require(Exception, _nodeType[p] == P,
"setNameOfPlace: this is not a place but a transition");
545 if (_placeOfName.find(name) == _placeOfName.end()) {
546 _nameOfNode[p] = name;
547 _placeOfName[name] = p;
558 auto it = _placeOfName.find(name);
559 if (it == _placeOfName.end()) {
594 virtual bool enables(Transition t,
const Marking & marking)
const;
606 virtual bool fire(Transition t,
const Marking & marking,
Marking & newMarking)
const;
616 virtual Transition newTransition(
const set<Place> & pre,
const set<Place> & post);
629 virtual Transition
newTransition(
const set<Place> & pre,
const set<Place> & post,
const string & name) {
630 require(Exception, !name.empty(),
"newTransition: empty name");
632 if (_transitionOfName.find(name) == _transitionOfName.end()) {
633 trans = newTransition(pre, post);
634 setNameOfTransition(trans, name);
646 require(Exception, p.valid(),
"setInhibition: invalid place");
647 require(Exception, &p.owner() == &_graph,
"setInhibition: the place does not belong to the Petri Net");
648 require(Exception, _nodeType[p] == P,
"setInhibition: this is not a place but a transition");
649 require(Exception, t.valid(),
"setInhibition: invalid transition");
650 require(Exception, &t.owner() == &_graph,
"setInhibition: the transition does not belong to the Petri Net");
651 require(Exception, _nodeType[t] == T,
"setInhibition: this is not a transition but a place");
652 _inhibitors[p].insert(t);
653 _inhibited[t].insert(p);
661 const std::unordered_set<Transition> &
inhibitors(Place p)
const {
662 return _inhibitors[p];
672 require(Exception, p.valid(),
"unsetInhibition: invalid place");
673 require(Exception, &p.owner() == &_graph,
"unsetInhibition: the place does not belong to the Petri Net");
674 require(Exception, _nodeType[p] == P,
"unsetInhibition: this is not a place but a transition");
675 require(Exception, t.valid(),
"unsetInhibition: invalid transition");
676 require(Exception, &t.owner() == &_graph,
"unsetInhibition: the transition does not belong to the Petri Net");
677 require(Exception, _nodeType[t] == T,
"unsetInhibition: this is not a transition but a place");
678 _inhibitors[p].erase(t);
679 _inhibited[t].erase(p);
691 require(Exception, p.valid(),
"inhibits: invalid place");
692 require(Exception, &p.owner() == &_graph,
"inhibits: the place does not belong to the Petri Net");
693 require(Exception, _nodeType[p] == P,
"inhibits: this is not a place but a transition");
694 require(Exception, t.valid(),
"inhibits: invalid transition");
695 require(Exception, &t.owner() == &_graph,
"inhibits: the transition does not belong to the Petri Net");
696 require(Exception, _nodeType[t] == T,
"inhibits: this is not a transition but a place");
697 return _inhibitors[p].find(t) != _inhibitors[p].end();
707 return _nbTransitions;
727 void addPre(Transition transition, Place place) {
728 require(Exception, place.valid(),
"addPre: invalid place");
729 require(Exception, &place.owner() == &_graph,
"addPre: the place does not belong to the Petri Net");
730 require(Exception, _nodeType[place] == P,
"addPre: this is not a place but a transition");
731 require(Exception, transition.valid(),
"addPre: invalid transition");
732 require(Exception, &transition.owner() == &_graph,
"addPre: the transition does not belong to the Petri Net");
733 require(Exception, _nodeType[transition] == T,
"addPre: this is not a transition but a place");
734 _graph.
newEdge(place, transition);
744 void addPost(Transition transition, Place place) {
745 require(Exception, place.valid(),
"addPost: invalid place");
746 require(Exception, &place.owner() == &_graph,
"addPost: the place does not belong to the Petri Net");
747 require(Exception, _nodeType[place] == P,
"addPost: this is not a place but a transition");
748 require(Exception, transition.valid(),
"addPost: invalid transition");
749 require(Exception, &transition.owner() == &_graph,
"addPost: the transition does not belong to the Petri Net");
750 require(Exception, _nodeType[transition] == T,
"addPost: this is not a transition but a place");
751 _graph.
newEdge(transition, place);
763 require(Exception, place.valid(),
"preTransBegin: invalid place");
764 require(Exception, &place.owner() == &_graph,
"preTransBegin: the place does not belong to the Petri Net");
765 require(Exception, _nodeType[place] == P,
"preTransBegin: this is not a place but a transition");
778 require(Exception, place.valid(),
"preTransEnd: invalid place");
779 require(Exception, &place.owner() == &_graph,
"preTransEnd: the place does not belong to the Petri Net");
780 require(Exception, _nodeType[place] == P,
"preTransEnd: this is not a place but a transition");
793 require(Exception, place.valid(),
"postTransBegin: invalid place");
794 require(Exception, &place.owner() == &_graph,
"postTransBegin: the place does not belong to the Petri Net");
795 require(Exception, _nodeType[place] == P,
"postTransBegin: this is not a place but a transition");
808 require(Exception, place.valid(),
"postTransEnd: invalid place");
809 require(Exception, &place.owner() == &_graph,
"postTransEnd: the place does not belong to the Petri Net");
810 require(Exception, _nodeType[place] == P,
"postTransEnd: this is not a place but a transition");
820 require(Exception, transition.valid(),
"preBegin: invalid transition");
821 require(Exception, &transition.owner() == &_graph,
"preBegin: the transition does not belong to the Petri Net");
822 require(Exception, _nodeType[transition] == T,
"preBegin: this is not a transition but a place");
832 require(Exception, transition.valid(),
"preEnd: invalid transition");
833 require(Exception, &transition.owner() == &_graph,
"preEnd: the transition does not belong to the Petri Net");
834 require(Exception, _nodeType[transition] == T,
"preEnd: this is not a transition but a place");
844 require(Exception, transition.valid(),
"postBegin: invalid transition");
845 require(Exception, &transition.owner() == &_graph,
"postBegin: the transition does not belong to the Petri Net");
846 require(Exception, _nodeType[transition] == T,
"postBegin: this is not a transition but a place");
857 require(Exception, transition.valid(),
"postEnd: invalid transition");
858 require(Exception, &transition.owner() == &_graph,
"postEnd: the transition does not belong to the Petri Net");
859 require(Exception, _nodeType[transition] == T,
"postEnd: this is not a transition but a place");
887 require(Exception, transition.valid(),
"setNameOfTransition: invalid transition");
888 require(Exception, &transition.owner() == &_graph,
"setNameOfTransition: the transition does not belong to the Petri Net");
889 require(Exception, _nodeType[transition] == T,
"setNameOfTransition: this is not a transition but a place");
890 require(Exception, !name.empty(),
"setNameOfTransition: empty name");
891 if (_transitionOfName.find(name) == _transitionOfName.end()) {
892 _nameOfNode[transition] = name;
893 _transitionOfName[name] = transition;
903 require(Exception, transition.valid(),
"nameOfTransition: invalid transition");
904 require(Exception, &transition.owner() == &_graph,
"nameOfTransition: the transition does not belong to the Petri Net");
905 require(Exception, _nodeType[transition] == T,
"nameOfTransition: this is not a transition but a place");
906 return _nameOfNode[transition];
915 require(Exception, !name.empty(),
"transitionOfName: empty name");
916 auto it = _transitionOfName.find(name);
917 if (it == _transitionOfName.end()) {
929 bool inPre(Transition t, Place p)
const;
938 bool inPost(Transition t, Place p)
const;
954 virtual Transition duplicateTransition(Transition t);
981 return _initialMarking;
989 return _initialMarking;
997 return !_initialMarking.
empty();
1016 virtual void net2Dot(
const string & fileName)
const;
1027 void net2Tina(ostream & os)
const;
1036 void net2Tina(
const string & filename)
const;
1043 void printPrettyMarking(ostream & os,
const Marking & marking)
const;
1058 void net2TinaWithHeader(ostream & os,
const string & netHeader)
const;
1074 require(Exception, transition.valid(),
"nameOfTransition2Dot: invalid transition");
1075 require(Exception, &transition.owner() == &_graph,
"nameOfTransition2Dot: the transition does not belong to the Petri Net");
1076 require(Exception, _nodeType[transition] == T,
"nameOfTransition2Dot: this is not a transition but a place");
1086 os << header <<
" " << name() <<
"\n";
1094 virtual void net2TinaTransition(ostream & os,
1095 Transition trans)
const;
1103 Transition transition)
const {
1104 require(Exception, transition.valid(),
"net2TinaTransitionInterval: invalid transition");
1105 require(Exception, &transition.owner() == &_graph,
"net2TinaTransitionInterval: the transition does not belong to the Petri Net");
1106 require(Exception, _nodeType[transition] == T,
"net2TinaTransitionInterval: this is not a transition but a place");
1117 require(Exception, transition.valid(),
"net2TinaTransitionInterval: invalid transition");
1118 require(Exception, &transition.owner() == &_graph,
"net2TinaTransitionInterval: the transition does not belong to the Petri Net");
1119 require(Exception, _nodeType[transition] == T,
"net2TinaTransitionInterval: this is not a transition but a place");
1152 virtual void appendNet(
const BoundedNet & net,
1165 template<
typename Iterator>
1170 Place result = newPlace();
1171 while(first != last)
1175 while(!inhibitors(current).empty())
1177 auto transition = *inhibitors(current).begin();
1178 setInhibition(result,transition);
1179 unsetInhibition(current,transition);
1181 if(initialMarking().getMark(current)!=0)
1183 initialMarking().mark(result,initialMarking().getMark(result) + initialMarking().getMark(current));
1184 initialMarking().unmark(current);
1186 if(!labelOfPlace(current).empty())
1188 setLabelOfPlace(result,labelOfPlace(result)+
" " + labelOfPlace(current));
1190 for(
auto it = preTransBegin(current); it != preTransEnd(current); ++it)
1192 addPost(*it,result);
1194 for(
auto it = postTransBegin(current); it != postTransEnd(current); ++it)
1198 deletePlace(current);
1204 return *beginOfPlaces();
ConstIterator(list< Arc >::const_iterator it, bool pre)
Diades::Graph::Node * operator->(void) const
Diades::Graph::Node & operator*() const
const string & nameOfPlace(Place p) const
Transition transitionOfName(const string &name) const
bool operator==(const ConstIterator &it) const
ConstIterator postBegin(Transition transition) const
void addPost(Transition transition, Place place)
Edge newEdge(Node source, Node target)
Diades::Graph::NodeIterator _it
EdgeSizeType numberOfEdges() const
PerTypeIterator PlaceIterator
const Marking & initialMarking() const
virtual void writeEndOfDotFile(ostream &os) const
virtual void net2TinaTransitionName(ostream &os, Transition transition) const
const Place & operator*() const
Diades::Graph::Node Place
#define ensure(Exception, expr, message)
ConstIterator preTransEnd(Place place) const
PlaceIterator endOfPlaces() const
const Place * operator->(void) const
bool operator!=(const ConstIterator &it) const
Place mergePlaces(Iterator first, Iterator last)
void setNameOfTransition(Transition transition, const string &name)
BoundedNet::NodeType _type
virtual Transition newTransition(const set< Place > &pre, const set< Place > &post, const string &name)
void setName(const string &name)
const std::unordered_set< Transition > & inhibitors(Place p) const
bool operator==(const PerTypeIterator &it) const
PerTypeIterator TransitionIterator
ConstIterator postTransEnd(Place place) const
TransitionIterator beginOfTransitions() const
ConstIterator preBegin(Transition transition) const
const Diades::Graph::Graph & graph() const
AutFsm::Transition Transition
GraphIterator< Node > NodeIterator
void deleteTransition(Component &comp, InputIterator first, InputIterator last)
void addPre(Transition transition, Place place)
virtual void nameOfTransition2Dot(ostream &os, Transition transition) const
unordered_map< string, Diades::Graph::Node > _transitionOfName
virtual void unsetInhibition(Place p, Transition t)
TransitionIterator endOfTransitions() const
Diades::Graph::NodeMap< int > _postponable
void setLabelOfPlace(Place place, const string &label)
Diades::Graph::NodeMap< NodeType > _nodeType
virtual void net2TinaTransitionInterval(ostream &os, Transition transition) const
#define require(Exception, expr, message)
Place getPlace(const string &name) const
Namespace of the Diades project.
ConstIterator operator++(int)
Diades::Graph::NodeMap< string > _nameOfNode
set< Transition >::const_iterator TransitionEventIterator
bool operator!=(const PerTypeIterator &it) const
size_t numberOfArcs() const
const string & labelOfPlace(const Place &p) const
ConstIterator on the Net.
bool inhibits(Place p, Transition t) const
Marking & initialMarking()
ConstIterator & operator++()
list< Arc >::const_iterator _it
Diades::Utils::Exception< BoundedNet > Exception
Iterators over the places or the transitions of the Net.
PlaceIterator beginOfPlaces() const
ConstIterator preEnd(Transition transition) const
size_t numberOfTransitions() const
virtual void net2TinaEnd(ostream &os, const string &header) const
Diades::Graph::Graph _graph
ConstIterator preTransBegin(Place place) const
virtual void setInhibition(Place p, Transition t)
ConstIterator postEnd(Transition transition) const
ConstIterator postTransBegin(Place place) const
Diades::Graph::NodeMap< string > _labelOfNode
const string & name() const
void setNameOfPlace(Place p, const string &name)
unordered_map< string, Diades::Graph::Node > _placeOfName
Diades::Graph::NodeMap< std::unordered_set< Transition > > _inhibitors
size_t numberOfPlaces() const
const string & nameOfTransition(Transition transition) const
Diades::Graph::NodeMap< std::unordered_set< Place > > _inhibited