LCOV - code coverage report
Current view: top level - builds/barbot/Cosmos/src/Simulator - SPNBase.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 120 171 70.2 %
Date: 2021-06-16 15:43:28 Functions: 10 21 47.6 %

          Line data    Source code
       1             : /*******************************************************************************
       2             :  *                                                                             *
       3             :  * Cosmos:(C)oncept et (O)utils (S)tatistique pour les (Mo)deles               *
       4             :  * (S)tochastiques                                                             *
       5             :  *                                                                             *
       6             :  * Copyright (C) 2009-2012 LSV & LACL                                          *
       7             :  * Authors: Paolo Ballarini BenoƮt Barbot & Hilal Djafri                       *
       8             :  * Website: http://www.lsv.ens-cachan.fr/Software/cosmos                       *
       9             :  *                                                                             *
      10             :  * This program is free software; you can redistribute it and/or modify        *
      11             :  * it under the terms of the GNU General Public License as published by        *
      12             :  * the Free Software Foundation; either version 3 of the License, or           *
      13             :  * (at your option) any later version.                                         *
      14             :  *                                                                             *
      15             :  * This program is distributed in the hope that it will be useful,             *
      16             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of              *
      17             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
      18             :  * GNU General Public License for more details.                                *
      19             :  *                                                                             *
      20             :  * You should have received a copy of the GNU General Public License along     *
      21             :  * with this program; if not, write to the Free Software Foundation, Inc.,     *
      22             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 *
      23             :  * file spn_orig.cpp created by Benoit Barbot on 03/09/15.                     *
      24             :  *******************************************************************************
      25             :  */
      26             : 
      27             : #include "SPNBase.hpp"
      28             : #include "timeGenBis.hpp"
      29             : 
      30             : using namespace std;
      31             : 
      32             : template<class S, class EQT>
      33          26 : const vector<size_t> SPNBase<S, EQT>::getNbTransition()const {
      34          26 :     auto nbtrans = vector<size_t>();
      35          26 :     for( let t : Transition)nbtrans.push_back(t.bindingList.size());
      36          26 :     return nbtrans;
      37             : }
      38             : 
      39             : template<class S,class EQT>
      40           0 : const vector<string> SPNBase<S, EQT>::getTransitionLabels()const {
      41           0 :     auto nbtrans = vector<string>();
      42           0 :     for( let t : Transition)nbtrans.push_back(t.label);
      43           0 :     return nbtrans;
      44             : }
      45             : 
      46             : template<class S, class EQT>
      47         134 : const string SPNBase<S, EQT>::getTransitionLabel(size_t i)const {
      48         134 :     return Transition[i].label;
      49             : }
      50             : 
      51             : /**
      52             :  * Fill the event queue with the initially enabled transition
      53             :  */
      54             : template<class S, class EQT>
      55      182552 : void SPNBase<S,EQT>::initialEventsQueue(EventsQueue &EQ,timeGen &TG) {
      56             :     //Check each transition. If a transition is enabled then his fire
      57             :     //time is simulated and added to the structure.
      58             :     
      59      365104 :     Event E;
      60             :     
      61     4052112 :     for(const auto &t : this->Transition) {
      62     3869560 :         if (P.verbose > 5) {
      63           0 :             std::cerr << "Initial Event for transition " << t.Id << "(" << shift+t.Id << "," << t.label << ") : \n";
      64             :         }
      65     8635570 :         for(auto &bindex : t.bindingList){
      66     4766010 :             if (P.verbose > 5) {
      67           0 :                 std::cerr << "Transition " << t.label << ": ";
      68           0 :                 bindex.print();
      69           0 :                 std::cerr << "\n";
      70             :             }
      71     4766010 :             if (IsEnabled(t.Id,bindex)) {
      72      894002 :                 generateEvent(0.0,E, t.Id ,bindex,TG, *(static_cast<S*>(this)));
      73      894002 :                 EQ.insert(E);
      74             :             }
      75             :         }
      76             :     }
      77      182552 : }
      78             : 
      79             : template<class S, class EQT>
      80       39550 : void SPNBase<S, EQT>::initialEventsQueue(EventsQueueSet &EQ,timeGen &TG) {
      81             :     //Check each transition. If a transition is enabled then his fire
      82             :     //time is simulated and added to the structure.
      83             :     
      84       79100 :     Event E;
      85       79100 :     abstractBindingIterator absMkIt(Marking);
      86             :     
      87      132100 :     for(const auto &t : Transition) {
      88       92550 :         if (P.verbose > 5) {
      89           0 :             std::cerr << "Initial Event for transition " << t.Id << "(" << shift+t.Id << ","<< t.label << ") : \n";
      90             :         }
      91       92550 :         size_t it = t.Id;
      92             :         
      93             :         /* const auto &it = (const size_t) t.Id; */
      94      379550 :         for(absMkIt.reset(it, Marking); absMkIt.isValid(); absMkIt.next(it, Marking)) {
      95      574000 :             const auto& bindex =  absMkIt.getBinding();
      96             :             if (false && P.verbose > 5) {
      97             :                 std::cerr << "Transition " << t.label << ": ";
      98             :                 bindex.print();
      99             :                 std::cerr << "\n";
     100             :             }
     101      287000 :             if (IsEnabled(t.Id,bindex)) {
     102      287000 :                 generateEvent(0.0,E, t.Id ,bindex,TG, *(static_cast<S*>(this)));
     103      287000 :                 EQ.insert(E);
     104             :             }
     105             :         }
     106             :     }
     107       39550 : }
     108             : 
     109             : 
     110             : /**
     111             :  * Update the enabling transition of the SPN, and update the event queue.
     112             :  * @param E1_transitionNum the number of the transition which last
     113             :  * occured in the SPN.
     114             :  * @param b is the binding of the last transition.
     115             :  */
     116             : template<class S, class EQT>
     117    44924779 : void SPNBase<S,EQT>::update(double ctime,size_t tr, const abstractBinding& lb,EventsQueue &EQ,timeGen &TG){
     118             :     //This function update the Petri net according to a transition.
     119             :     //In particular it update the set of enabled transition.
     120             :     
     121             :     //check if the current transition is still enabled
     122    44924779 :     if(Transition[tr].DistTypeIndex != SYNC){
     123   137598520 :         for(const auto &bindex : Transition[tr].bindingList){
     124    92673741 :             bool Nenabled = IsEnabled(tr, bindex);
     125    92673741 :             bool NScheduled = EQ.isScheduled(shift +tr, bindex.idcount);
     126             :             
     127    92673741 :             if (Nenabled && NScheduled && lb.idcount == bindex.idcount ) {
     128    36960538 :                 generateEvent(ctime,F, tr, bindex,TG, *(static_cast<S*>(this)));
     129    36960538 :                 EQ.replace(F); //replace the transition with the new generated time
     130    55713203 :             } else if (Nenabled && !NScheduled) {
     131           0 :                 generateEvent(ctime,F, tr, bindex,TG, *(static_cast<S*>(this)));
     132           0 :                 EQ.insert(F);
     133    55713203 :             } else if (!Nenabled && NScheduled) {
     134     8058076 :                 EQ.remove(shift+tr,bindex.idcount );
     135             :             }
     136             :         }
     137             :     }else{
     138             :         //Do not remove it it is not in the queue anyway
     139           0 :         EQ.remove(shift+tr,lb.idcount );
     140             :     }
     141             :     
     142             :     // Possibly adding Events corresponding to newly enabled-transitions
     143             :     //const auto &net = PossiblyEn();
     144    75586620 :     for (size_t t=0; PossiblyEnabled[lastTransition][t] != -1;t++) {
     145    30661841 :         const auto &it = PossiblyEnabled[lastTransition][t];
     146    30661841 :         size_t bindnum = 0;
     147    30661841 :         const abstractBinding *bindex = nextPossiblyEnabledBinding(it, lb, &bindnum);
     148   159319427 :         while (bindex != NULL){
     149             :             if(false && P.verbose > 4){
     150             :                 std::cerr << "consider for enabling: " << Transition[it].label << ",";
     151             :                 bindex->print();
     152             :                 cerr << endl;
     153             :             }
     154             :             
     155             :             //for(vector<abstractBinding>::const_iterator bindex = Transition[*it].bindingList.begin() ;
     156             :             //  bindex != Transition[*it].bindingList.end() ; ++bindex){
     157    64328793 :             if (IsEnabled(it,*bindex)) {
     158    26659611 :                 if (!EQ.isScheduled(shift + it,bindex->idcount)) {
     159     9629218 :                     if(P.verbose > 4){
     160           0 :                         cerr << "->New transition enabled: " << Transition[it].label << ",";
     161           0 :                         bindex->print();
     162           0 :                         cerr << endl;
     163             :                     }
     164     9629218 :                     if(!EQ.restart(ctime,shift + it,bindex->idcount)){
     165     9629218 :                         generateEvent(ctime,F, it, *bindex,TG, *(static_cast<S*>(this)));
     166     9629218 :                         EQ.insert(F);
     167             :                     }
     168             :                     
     169             :                 } else {
     170    17030393 :                     if (Transition[it].MarkingDependent) {
     171     1918421 :                         generateEvent(ctime,F, it,*bindex,TG, *(static_cast<S*>(this)));
     172     1918421 :                         EQ.replace(F);
     173             :                     }
     174             :                 }
     175             :             }
     176    64328793 :             bindex = nextPossiblyEnabledBinding(it, lb, &bindnum);
     177             :         }
     178             :     }
     179             :     
     180             :     // Possibly removing Events corresponding to newly disabled-transitions
     181             :     //const auto &ndt = PossiblyDis();
     182             :     //for (const auto &it : ndt) {
     183    52275703 :     for (size_t t=0; PossiblyDisabled[lastTransition][t] != -1;t++) {
     184     7350924 :         const auto &it = PossiblyDisabled[lastTransition][t];
     185     7350924 :         size_t bindnum = 0;
     186     7350924 :         const abstractBinding *bindex = nextPossiblyDisabledBinding(it, lb, &bindnum);
     187    36450460 :         while (bindex != NULL){
     188             :             if(false && P.verbose > 4){
     189             :                 cerr << "consider for disabling: " << Transition[it].label << ",";
     190             :                 bindex->print();
     191             :                 cerr << endl;
     192             :             }
     193             :             
     194    14549768 :             if (Transition[it].DistTypeIndex == SYNC) {
     195           0 :                 if (P.verbose > 4) {
     196           0 :                     cerr << "Transition is a SYNC transition. Consider for enabling instead." << endl;
     197             :                 }
     198           0 :                 if (IsEnabled(it, *bindex)){
     199           0 :                     if (!EQ.isScheduled(shift + it,bindex->idcount)) {
     200           0 :                         if (P.verbose > 4) {
     201           0 :                             cerr << "->New transition enabled: " << Transition[it].label << ",";
     202           0 :                             bindex->print();
     203           0 :                             cerr << endl;
     204             :                         }
     205             :                     }
     206           0 :                     if (!EQ.restart(ctime,shift + it,bindex->idcount)) {
     207           0 :                         generateEvent(ctime,F, it, *bindex,TG, *(static_cast<S*>(this)));
     208           0 :                         EQ.insert(F);
     209             :                     }
     210             :                 }
     211             :             }
     212             :             else {
     213             :                 
     214             :                 //for(vector<abstractBinding>::const_iterator bindex = Transition[*it].bindingList.begin() ;
     215             :                 //      bindex != Transition[*it].bindingList.end() ; ++bindex){
     216    14549768 :                 if (EQ.isScheduled(shift + it, bindex->idcount)) {
     217     4027062 :                     if (!IsEnabled(it, *bindex )){
     218     2110363 :                         if(P.verbose > 4){
     219           0 :                             cerr << "<-New transition disabled: " << Transition[it].label << ",";
     220           0 :                             bindex->print();
     221           0 :                             cerr << endl;
     222             :                         }
     223     2110363 :                         if(Transition[it].AgeMemory){
     224           0 :                             EQ.pause(ctime, shift +it, bindex->idcount);
     225     2110363 :                         }else EQ.remove(shift +it,bindex->idcount);
     226             :                     }else {
     227     1916699 :                         if (Transition[it].MarkingDependent) {
     228      829652 :                             generateEvent(ctime,F, shift +it,*bindex,TG, *(static_cast<S*>(this)));
     229      829652 :                             EQ.replace(F);
     230             :                         }
     231             :                     }
     232             :                 }
     233             :             }
     234    14549768 :             bindex = nextPossiblyDisabledBinding(it, lb, &bindnum);
     235             :         }
     236             :     }
     237             :     
     238             :     // Update transition which have no precondition on the Marking
     239    77718346 :     for (size_t t=0; FreeMarkDepT[lastTransition][t]!= -1;t++) {
     240    32793567 :         const auto &it = FreeMarkDepT[lastTransition][t];
     241             :         //const auto &fmd = FreeMarkingDependant();
     242             :         //for (const auto &it : fmd) {
     243    65587134 :         for(const auto &bindex : Transition[it].bindingList){
     244             :             //if (IsEnabled(it,bindex)) {
     245    32793567 :             if (EQ.isScheduled(shift +it, bindex.idcount)) {
     246     9900617 :                 generateEvent(ctime,F, shift +it,bindex,TG, *(static_cast<S*>(this)));
     247     9900617 :                 EQ.replace(F);
     248             :             }
     249             :             //}
     250             :         }
     251             :         
     252             :     }
     253             :     //assert(cerr<< "assert!"<< endl);
     254             :     
     255             :     /*
     256             :      //In Debug mode check that transition are scheduled iff they are enabled
     257             :      for (const auto &t : Transition){
     258             :      for(const auto &bindex : t.bindingList){
     259             :      if (IsEnabled(t.Id, bindex) !=
     260             :      EQ.isScheduled(t.Id, bindex.idcount)){
     261             :      cerr << "IsEnabled(" << t.label << ",";
     262             :      bindex.print();
     263             :      cerr <<")" << endl;
     264             :      if(EQ.isScheduled(t.Id, bindex.idcount)){
     265             :      cerr << "Scheduled and not enabled!"<< endl;
     266             :      }else{
     267             :      cerr << "Enabled and not scheduled!" << endl;
     268             :      }
     269             :      assert(IsEnabled(t.Id, bindex) ==
     270             :      EQ.isScheduled(t.Id, bindex.idcount));
     271             :      }
     272             :      }
     273             :      }
     274             :      */
     275             :     
     276    44924779 : }
     277             : 
     278             : 
     279             : template<class S, class EQT>
     280     7316451 : void SPNBase<S,EQT>::cleanNewlyDisabled(size_t tr, EventsQueueSet & EQ){
     281     7316451 :     const auto& bset = EQ.getScheduledBinding(tr);
     282    43544091 :     for(auto it = bset.begin(); it != bset.end(); ){
     283    36227640 :         const auto &binding = *it;
     284    36227640 :         it++;
     285    36227640 :         Event &e = EQ.getEvent(tr, binding.first);
     286    36227640 :         bool stillEnabled = IsEnabled(tr, e.binding );
     287    36227640 :         if( ! stillEnabled) {
     288             :             if (false &&  P.verbose > 4) {
     289             :                 std::cerr << "Removing :" << Transition[tr].label  << " : ";
     290             :                 e.binding.print();
     291             :                 std::cerr << endl;
     292             :             }
     293     1533820 :             EQ.remove(shift+tr,binding.first);
     294             :         }
     295             :     }
     296             :     
     297     7316451 : }
     298             : 
     299             : template<class S, class EQT>
     300     6581599 : void SPNBase<S,EQT>::update(double ctime,size_t tr, const abstractBinding& lb,EventsQueueSet &EQ,timeGen &TG){
     301             :     //This function update the Petri net according to a transition.
     302             :     //In particular it update the set of enabled transition.
     303             :     
     304             :     //check if the current transition is still enabled
     305    13163198 :     abstractBindingIterator absMkIt(Marking);
     306             :     
     307     6581599 :     cleanNewlyDisabled( tr, EQ);
     308             :     
     309             :     //for(const auto &bindex : Transition[E1_transitionNum].bindingList){
     310     6581599 :     if(Transition[tr].DistTypeIndex != SYNC){
     311     6581599 :         absMkIt.reset(tr, Marking);
     312    76154151 :         for(; absMkIt.isValid(); absMkIt.next(tr, Marking)) {
     313    69572552 :             const auto& bindex = absMkIt.getBinding();
     314             :             // bindex = Transition[E1_transitionNum].bindingList[tmpbindex];
     315             :             // bindex type abstractBinding
     316             :             
     317             :             
     318    34786276 :             if (P.verbose > 4) {
     319           0 :                 std::cerr << "Transition :" << Transition[tr].label  << "\tbinding:";
     320           0 :                 bindex.print();
     321           0 :                 std::cerr << endl;
     322             :             }
     323             :             
     324    34786276 :             bool Nenabled = IsEnabled(tr, bindex);
     325    34786276 :             bool NScheduled = EQ.isScheduled(shift+tr, bindex.idcount);
     326    34786276 :             if (P.verbose > 4) {
     327           0 :                 std::cerr << "Current status: IsEnabled ? " << Nenabled << ". Is Scheduled ? " << NScheduled << ".\n";
     328             :             }
     329             :             
     330    34786276 :             if (Nenabled && NScheduled && lb.idcount == bindex.idcount ) {
     331     5346000 :                 generateEvent(ctime,F, tr, bindex,TG, *(static_cast<S*>(this)));
     332     5346000 :                 EQ.replace(F); //replace the transition with the new generated time
     333    29440276 :             } else if (Nenabled && !NScheduled) {
     334           0 :                 generateEvent(ctime,F, tr, bindex,TG, *(static_cast<S*>(this)));
     335           0 :                 EQ.insert(F);
     336    29440276 :             } else if (!Nenabled && NScheduled) {
     337           0 :                 EQ.remove(shift+tr,bindex.idcount );
     338             :             }
     339             :         }
     340             :     }else{
     341             :         //Do not remove it, it is not in the queue anyway
     342           0 :         EQ.remove(shift+tr,lb.idcount );
     343             :     }
     344             :     //absMkIt.reset();
     345             :     
     346             :     // Possibly adding Events corresponding to newly enabled-transitions
     347             :     //const auto &net = PossiblyEn();
     348     8540222 :     for (size_t t=0; PossiblyEnabled[lastTransition][t] != -1;t++) {
     349     1958623 :         size_t it = (size_t) PossiblyEnabled[lastTransition][t];
     350             :         
     351     1958623 :         if(P.verbose > 4)std::cerr << "consider for enabling: " << Transition[it].label << ",";
     352     6501754 :         for(absMkIt.reset(it, Marking); absMkIt.isValid(); absMkIt.next(it, Marking)) {
     353     9086262 :             const auto& bindex =  absMkIt.getBinding();
     354             :             
     355     4543131 :             if(P.verbose > 4){
     356             :                 //std::cerr << "consider for enabling: " << Transition[it].label << ",";
     357           0 :                 bindex.print();
     358           0 :                 cerr << endl;
     359             :             }
     360             :             
     361             :             //for(vector<abstractBinding>::const_iterator bindex = Transition[*it].bindingList.begin() ;
     362             :             //  bindex != Transition[*it].bindingList.end() ; ++bindex){
     363     4543131 :             if (IsEnabled(it,bindex)) {
     364     2852392 :                 if (!EQ.isScheduled(shift+it,bindex.idcount)) {
     365     1311915 :                     if(P.verbose > 4){
     366           0 :                         cerr << "->New transition enabled: " << Transition[it].label << ",";
     367           0 :                         bindex.print();
     368           0 :                         cerr << endl;
     369             :                     }
     370     1311915 :                     if(!EQ.restart(ctime,shift+it,bindex.idcount)){
     371     1311915 :                         generateEvent(ctime,F, it, bindex,TG, *(static_cast<S*>(this)));
     372     1311915 :                         EQ.insert(F);
     373             :                     }
     374             :                     
     375             :                 } else {
     376     1540477 :                     if (Transition[it].MarkingDependent) {
     377           0 :                         generateEvent(ctime,F, it,bindex,TG, *(static_cast<S*>(this)));
     378           0 :                         EQ.replace(F);
     379             :                     }
     380             :                 }
     381             :             }
     382             :         }
     383             :     }
     384             :     
     385             :     // Possibly removing Events corresponding to newly disabled-transitions
     386             :     //const auto &ndt = PossiblyDis();
     387             :     //for (const auto &it : ndt) {
     388     7316451 :     for (size_t t=0; PossiblyDisabled[lastTransition][t] != -1;t++) {
     389             :         //const auto &it = PossiblyDisabled[lastTransition][t];
     390      734852 :         size_t it = (size_t) PossiblyDisabled[lastTransition][t];
     391      734852 :         cleanNewlyDisabled(it, EQ);
     392             :     }
     393             :     
     394             :     
     395             :     // Update transition which have no precondition on the Marking
     396     6581599 :     for (size_t t=0; FreeMarkDepT[lastTransition][t]!= -1;t++) {
     397             :         //const auto &it = FreeMarkDepT[lastTransition][t];
     398           0 :         size_t it = (size_t) FreeMarkDepT[lastTransition][t];
     399             :         //const auto &fmd = FreeMarkingDependant();
     400             :         //for (const auto &it : fmd) {
     401             :         
     402           0 :         for(absMkIt.reset(it, Marking); absMkIt.isValid(); absMkIt.next(it, Marking)) {
     403           0 :             const auto& bindex =  absMkIt.getBinding();
     404             :             //if (IsEnabled(it,bindex)) {
     405           0 :             if (EQ.isScheduled(it, bindex.idcount)) {
     406           0 :                 generateEvent(ctime,F, it,bindex,TG, *(static_cast<S*>(this)));
     407           0 :                 EQ.replace(F);
     408             :             }
     409             :             //}
     410             :         }
     411             :         
     412             :     }
     413             :     //assert(cerr<< "assert!"<< endl);
     414             :     
     415             :     /*
     416             :      //In Debug mode check that transition are scheduled iff they are enabled
     417             :      for (const auto &t : Transition){
     418             :      for(const auto &bindex : t.bindingList){
     419             :      if (IsEnabled(t.Id, bindex) !=
     420             :      EQ.isScheduled(t.Id, bindex.idcount)){
     421             :      cerr << "IsEnabled(" << t.label << ",";
     422             :      bindex.print();
     423             :      cerr <<")" << endl;
     424             :      if(EQ.isScheduled(t.Id, bindex.idcount)){
     425             :      cerr << "Scheduled and not enabled!"<< endl;
     426             :      }else{
     427             :      cerr << "Enabled and not scheduled!" << endl;
     428             :      }
     429             :      assert(IsEnabled(t.Id, bindex) ==
     430             :      EQ.isScheduled(t.Id, bindex.idcount));
     431             :      }
     432             :      }
     433             :      }
     434             :      */
     435     6581599 : }
     436             : 
     437             : 
     438             : 

Generated by: LCOV version 1.13