LCOV - code coverage report
Current view: top level - builds/barbot/Cosmos/src/ModelGenerator/GspnParser - Gspn-model.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 82 170 48.2 %
Date: 2021-06-16 15:43:28 Functions: 14 24 58.3 %

          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 Gspn-model.hpp                                                         *
      24             :  * Created by Benoit Barbot on 09/06/15.                                       *
      25             :  *******************************************************************************
      26             :  */
      27             : 
      28             : #include <fstream>
      29             : 
      30             : #include "Gspn-model.hpp"
      31             : 
      32             : using namespace std;
      33             : 
      34             : 
      35             : size_t userDefineDistribution::nbparam = 1;
      36          35 : string userDefineDistribution::polyfile;
      37             : 
      38             : 
      39          54 : bool operator< (const coloredToken& lhs, const coloredToken& rhs){
      40          54 :   if(lhs.field.size() < rhs.field.size())return true;
      41         106 :   for(size_t i=0; i<lhs.field.size(); i++ ){
      42          54 :     if(lhs.field[i] < rhs.field[i])return true;
      43          54 :     if(lhs.Flags[i] < rhs.Flags[i])return true;
      44          54 :     if(lhs.varIncrement[i] < rhs.varIncrement[i])return true;
      45             :   }
      46          52 :   return false;
      47             : }
      48             : 
      49          57 : bool operator== (const coloredToken& lhs, const coloredToken& rhs){
      50          57 :   if(lhs.field.size() != rhs.field.size())return false;
      51         106 :   for(size_t i=0; i<lhs.field.size(); i++ ){
      52          55 :     if(lhs.field[i] != rhs.field[i])return false;
      53          55 :     if(lhs.Flags[i] != rhs.Flags[i])return false;
      54          55 :     if(lhs.varIncrement[i] != rhs.varIncrement[i])return false;
      55             :   }
      56          51 :   return true;
      57             : }
      58             : 
      59          28 : set<size_t> coloredToken::varSet()const{
      60          28 :     set<size_t> s;
      61          56 :     for(size_t i=0; i<field.size(); i++ )
      62          28 :         if( Flags[i] == CT_VARIABLE )
      63          28 :             s.insert(field[i].intVal);
      64          28 :     return s;
      65             : }
      66             : 
      67        1519 : void searchreplace(const string &in,const string &motif,const string &rep,string &out){
      68        1519 :   out = in;
      69        1519 :   size_t pos = out.find(motif);
      70        1519 :   while (pos != string::npos) {
      71           0 :     out.replace(pos, motif.size(), rep);
      72           0 :     pos = out.find(motif,pos);
      73             :   }
      74        1519 : }
      75             : 
      76             : 
      77          36 : void GspnType::iterateDom(const string &s,const string &sop, const string &sclos ,const string &s2,const string &sop2, const string &sclos2 ,const colorDomain & cd, size_t prof,function<void (const string&,const string&)> func){
      78          36 :   if(prof == cd.colorClassIndex.size() ){func(s,s2);}
      79             :   else{
      80          36 :     for(const auto &cc : colClasses[cd.colorClassIndex[prof]].colors )
      81         128 :       iterateDom(s+ sop + cc.name + sclos, sop, sclos,
      82          96 :                  s2+sop2+ "Color_"+colClasses[cd.colorClassIndex[prof]].name+"_"+cc.name+sclos2,sop2, sclos2, cd, prof+1, func);
      83             :   }
      84             : 
      85          36 : }
      86             : 
      87          72 : void GspnType::iterateDomVec(vector<color> &v, const colorDomain & cd, size_t prof,function<void (const vector<color>&)> func){
      88          72 :   if(prof == cd.colorClassIndex.size() ){func(v);}
      89             :   else{
      90          72 :     for(const auto &cc : colClasses[cd.colorClassIndex[prof]].colors ){
      91          64 :       v.push_back(cc);
      92          64 :       iterateDomVec(v, cd, prof+1, func);
      93          64 :       v.pop_back();
      94             :     }
      95             :   }
      96             : 
      97          72 : }
      98             : 
      99          27 : void GspnType::iterateVars(const string &s,const string &sop, const string &sclos ,const set<size_t> &vd, size_t prof,function<void (const string&)> func){
     100          27 :   if(prof == colVars.size() ){func(s);}
     101             :   else{
     102           3 :     if (vd.count(prof)>0) {
     103          27 :       for(const auto &cc : colClasses[colDoms[colVars[prof].type].colorClassIndex[0]].colors )
     104          24 :         iterateVars(s+ sop + cc.name + sclos, sop, sclos, vd, prof+1, func);
     105             :     }else{
     106           0 :       iterateVars(s, sop, sclos,vd, prof+1, func);
     107             :     }
     108             :   }
     109          27 : }
     110             : 
     111          27 : void GspnType::iterateVars(vector<color> &v,const set<size_t> &vd, size_t prof,function<void (const vector<color>&)> func){
     112          27 :   if(prof == colVars.size() ){func(v);}
     113             :   else{
     114           3 :     if (vd.count(prof)>0) {
     115          27 :       for(const auto &cc : colClasses[colDoms[colVars[prof].type].colorClassIndex[0]].colors ){
     116          24 :         v.push_back(cc);
     117          24 :         iterateVars(v, vd, prof+1, func);
     118          24 :         v.pop_back();
     119             :       }
     120             :     }else{
     121           0 :       iterateVars(v,vd, prof+1, func);
     122             :     }
     123             :   }
     124          27 : }
     125             : 
     126           0 : bool GspnType::isInVar(size_t var, size_t tr)const {
     127           0 :   for(const auto &arcmap : inArcsStruct )if( get_t(arcmap.first) ==tr  ){
     128           0 :       if(arcmap.second.containsVar(var))return true;
     129             :     }
     130           0 :   return false;
     131             : }
     132             : 
     133          28 : bool GspnType::isInVar(const coloredToken &var, size_t tr)const {
     134         369 :   for(const auto &arcmap : inArcsStruct )if( get_t(arcmap.first) ==tr  ){
     135          36 :       for(const auto &tok:arcmap.second.coloredVal)
     136          31 :         if( tok ==  var  )return true;
     137             :     }
     138           2 :   return false;
     139             : }
     140             : 
     141             : 
     142           4 : map<coloredToken,pair<size_t,size_t>> GspnType::computeMacroVar()const{
     143           4 :     map<coloredToken,pair<size_t,size_t>> macroVar;
     144           4 :     size_t id = 0;
     145          42 :     for(const auto &arc: inArcsStruct){
     146          38 :         const auto &domain = placeStruct[get_p(arc.first)].colorDom;
     147          38 :         if(!colDoms[domain].isUncolored())
     148          58 :             for(const auto &tok: arc.second.coloredVal){
     149          29 :             if( macroVar.count(tok)==0 ){
     150           4 :                 macroVar.insert(make_pair(tok, make_pair(id++, domain)));
     151             :             }
     152             :         }
     153             :   }
     154           4 :   return macroVar;
     155             : }
     156             : 
     157         440 : size_t GspnType::get_uid(const string &str){
     158             :   //cout << "debug:\t" << str;
     159         440 :   if(uid.count(str)>0){
     160             :     //cout << " -> " << uid[str] << endl;
     161         312 :     return uid[str];
     162             :   }
     163         128 :   size_t it = uid.size();
     164         128 :   uid[str] = it;
     165             :   //cout << " -> " << uid[str] << endl;
     166         128 :   return it;
     167             : }
     168             : 
     169           0 : size_t GspnType::new_uid(const string &str){
     170           0 :   const auto i = uid.size();
     171           0 :   return get_uid(str+"U"+ to_string(i));
     172             : }
     173             : 
     174             : namespace text_output{
     175           0 :   ostream& operator<< (ostream& st, const Distribution& d){
     176           0 :     st << d.name << "(";
     177           0 :     for(const auto &e:d.Param)st << e << ", ";
     178           0 :     st << ")";
     179           0 :     return st;
     180             :   }
     181             : 
     182          62 :     ostream& operator<<(ostream& os, const coloredToken &tok){
     183          62 :         os << tok.mult << "<";
     184         124 :         for(size_t i=0; i< tok.field.size();i++ ){
     185          62 :             switch(tok.Flags[i]){
     186           0 :                 case CT_SINGLE_COLOR: os << "Col_classe_" << tok.field[i] << ", ";continue;
     187           0 :                 case CT_ALL: os << "ALL , ";continue;
     188           0 :                 case CT_ALL_SUBCLASS: os << "StaticSubClass_" << tok.field[i] << ", ";continue;
     189          62 :                 case CT_VARIABLE: os << "var_" << tok.field[i] << " + " <<tok.varIncrement[i] << ", "; continue;
     190           0 :                 case CT_SINGLE_REAL: os << tok.field[i]; continue;
     191           0 :                 default: os << "badly formed";
     192             :             }
     193             :         }
     194          62 :         os << ">";
     195          62 :         return os;
     196             :     }
     197             :     
     198           0 :     ostream& operator<<(ostream& os, const place& obj){
     199           0 :         os << "place " << obj.name << "(" << obj.id << "){" << endl;
     200           0 :         os << "\tmarking:" << obj.Marking;
     201           0 :         for(const auto &tok: obj.initMarking)os << tok << ",";
     202           0 :         os << endl;
     203           0 :         return os;
     204             :     }
     205             : 
     206             :     
     207             :     
     208           0 :   ostream& operator<< (ostream& st, const color& c){
     209           0 :     st << c.name;
     210           0 :     return st;
     211             :   }
     212             : 
     213           0 :   ostream& operator<< (ostream& st, const colorClass& cc){
     214           0 :     st << cc.name << "{\ncontent:{";
     215           0 :     for( const auto& c: cc.colors){
     216           0 :       st << c << ", ";
     217             :     }
     218           0 :     st << "}"<<endl;
     219           0 :     for( const auto& css: cc.staticSubclasses){
     220           0 :       st<< css.name << "{";
     221           0 :       for (const auto& c: css.colors){
     222           0 :         st << c << ", ";
     223             :       }
     224           0 :       st<< "}"<< endl;
     225             :     }
     226           0 :     st << "}"<< endl;
     227           0 :     return st;
     228             :   }
     229             :     
     230             :   
     231           0 :   ostream& operator<< (ostream& st, const transition& tr){
     232           0 :     st << "transition " << tr.name << "{" << endl;
     233           0 :     st << "\tid:\t" << tr.id << endl;
     234           0 :     st << "\tisTraced:\t" << tr.isTraced << endl;
     235           0 :     st << "\tdist:\t" << tr.dist << endl;
     236           0 :     st << "\tpriority:\t" << tr.priority << endl;
     237           0 :     st << "\tweight:\t" << tr.weight << endl;
     238             : 
     239           0 :     st << "\tvarDomain:\t{";
     240           0 :     for(auto v: tr.varDomain)st << v << ", ";
     241           0 :     st << "}"<< endl;
     242           0 :     st << "\tguard:\t" << tr.guard << endl;
     243           0 :     st << "\tupdate:\t" << tr.update << endl;
     244             : 
     245             : 
     246           0 :     st << "}" << endl;
     247           0 :     return st;
     248             :   }
     249             : 
     250           0 :   ostream& operator<<(ostream& os, const userDefineDistribution& obj){
     251           0 :     os << "UserDefineDistribution " << obj.name << "{" << endl;
     252           0 :     os << "\tvar:" << obj.var << endl;
     253           0 :     os << "\tcdf:" << obj.cdf << endl;
     254           0 :     os << "\tpdf:" << obj.pdf << endl;
     255           0 :     os << "\tlowerBound:" << obj.lowerBound << endl << "}" << endl;
     256           0 :     return os;
     257             :   }
     258             : 
     259           0 :   ostream& operator<<(ostream& os, const arcStore& obj){
     260           0 :     for(const auto& arc:obj){
     261           0 :       os << "(" << GspnType::get_p(arc.first) << ")-- ";
     262           0 :       if(arc.second.isColored){
     263           0 :         for(const auto& tok: arc.second.coloredVal)
     264           0 :           os << tok;
     265           0 :       } else os << arc.second.exprVal;
     266           0 :       os << " --(" << GspnType::get_t(arc.first)<< ")"<< endl;
     267             :     }
     268           0 :     return os;
     269             :   }
     270             : 
     271           0 :   ostream& operator<< (ostream& st, const GspnType& gspn){
     272           0 :     st << "#### Internal Memory Model of GSPN #######" << endl;
     273           0 :     st << "colClasses{"<< endl;
     274           0 :     for(const auto& cc: gspn.colClasses) st << cc;
     275           0 :     st << "}\n" << endl;
     276           0 :     st << "transitionStruct{" << endl;
     277           0 :     for(const auto& tr: gspn.transitionStruct) st<< tr;
     278           0 :     st << "}\n";
     279           0 :     st << "placeStruct{" << endl;
     280           0 :     for(const auto& tr: gspn.placeStruct) st<< tr;
     281           0 :     st << "}\n";
     282           0 :     st << "inArcs{" << endl << gspn.inArcsStruct << "}"<<endl;
     283           0 :     st << "outArcs{" << endl << gspn.outArcsStruct << "}"<<endl;
     284           0 :     st << "inhibArcs{" << endl << gspn.inhibArcsStruct << "}"<<endl;
     285           0 :     st << "##########################################" << endl;
     286           0 :     return st;
     287             :   }
     288         105 : }

Generated by: LCOV version 1.13