LCOV - code coverage report
Current view: top level - builds/barbot/Cosmos/src/ModelGenerator/GspnParser - Gspn-model.hpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 65 73 89.0 %
Date: 2021-06-16 15:43:28 Functions: 62 79 78.5 %

          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             : #ifndef __Cosmos__Gspn_model__
      29             : #define __Cosmos__Gspn_model__
      30             : 
      31             : #include <stdio.h>
      32             : #include <string>
      33             : #include <map>
      34             : #include <set>
      35             : #include <functional>
      36             : #include <unordered_map>
      37             : #include <vector>
      38             : 
      39             : #include "../expressionStruct.hpp"
      40             : 
      41             : //! A type to discriminate timed and untimmed transition.
      42             : enum TransType {
      43             :   Timed, unTimed
      44             : };
      45             : 
      46             : //! a structure for probability distribution
      47      146484 : struct ProbabiliteDistribution {
      48             :   std::string name;
      49             :   std::vector<expr> Param;
      50         512 :   ProbabiliteDistribution(){};
      51           1 :   ProbabiliteDistribution(const expr &s):name("EXPONENTIAL"),Param(1,s){};
      52             : };
      53             : typedef struct ProbabiliteDistribution Distribution;
      54             : 
      55           0 : struct userDefineDistribution{
      56             :   static std::string polyfile;
      57             :   static size_t nbparam;
      58             :   std::string name;
      59             :   std::string var;
      60             :   std::string cdf;
      61             :   std::string pdf;
      62             :   std::string norm;
      63             :   std::string lowerBound;
      64             :   std::string upperBound;
      65             : };
      66             : 
      67       12972 : struct color {
      68             :   std::string name;
      69             :   size_t id;
      70             :   size_t cc;
      71             :   color(){};
      72         134 :   color(const std::string s,size_t i,size_t c){ name =s;id = i; cc=c;}
      73             : };
      74             : 
      75             : enum ColorClassType {
      76             :     Asymmetric, Cyclic, Continuous
      77             : };
      78           0 : struct colorStaticSubclass {
      79             :     std::string name;
      80             :     size_t id;
      81             :     std::vector<color> colors;
      82             : };
      83         980 : struct colorClass {
      84             :     std::string name;
      85             :     bool isCircular;
      86             :     ColorClassType type;
      87             :     std::vector<color> colors;
      88             :     std::vector<colorStaticSubclass> staticSubclasses;
      89             :     std::string intIntervalName;
      90         233 :     inline std::string cname()const {
      91         233 :         return name+ "_Color_Classe";
      92             :     }
      93         111 :     inline unsigned int size()const {
      94         111 :         return colors.size();
      95             :     }
      96             : };
      97             : 
      98             : 
      99             : #define UNCOLORED_DOMAIN (0)
     100             : 
     101         964 : struct colorDomain {
     102             :     std::string name;
     103             :     std::vector<size_t> colorClassIndex;
     104             :     bool isBounded;
     105        2756 :     inline bool isUncolored()const {
     106        2756 :         return name.size() ==0;
     107             :     }
     108        1850 :     inline std::string cname()const {
     109        1850 :         if(isUncolored())return "int";
     110         678 :         return name+ "_Domain";
     111             :     }
     112          12 :     inline std::string ccname()const {
     113          12 :         if(isUncolored())return "int";
     114          12 :         if (colorClassIndex.size()==1) return name+"_Color_Classe";
     115             :         else {
     116           0 :             std::cerr << "The function ccname only apply to domains with exactly one color, to get the corresponding color class name. An error may occur." << std::endl;
     117           0 :             return name+"_Color_Classe";
     118             :         }
     119             :     }
     120         469 :     inline std::string tokname()const {
     121         469 :         if(isUncolored())return " ";
     122         469 :         return name+ "_Token";
     123             :     }
     124             : 
     125             : };
     126             : 
     127          80 : struct colorVariable {
     128             :   std::string name;
     129             :   size_t type;
     130             : };
     131             : 
     132             : enum varType {
     133             :     CT_SINGLE_COLOR,
     134             :     CT_SINGLE_REAL,
     135             :     CT_VARIABLE,
     136             :     CT_ALL,
     137             :     CT_ALL_SUBCLASS,
     138             :     CV_CLOCK,
     139             :     CV_REAL,
     140             :     CV_INT
     141             : };
     142             : 
     143           0 : struct hybridVariable {
     144             :   std::string name;
     145             :   varType type;
     146             :   bool isTraced;
     147             :   std::string initialValue;
     148           0 :   hybridVariable():type(CV_CLOCK),isTraced(true) {}
     149             : };
     150             : 
     151       38130 : struct coloredToken {
     152             :     expr mult;
     153             :     std::shared_ptr<colorDomain> coldom;
     154             :     std::vector<expr> field;
     155             :     std::vector<varType> Flags;
     156             :     std::vector<int> varIncrement;
     157             :     bool hasAll;
     158         265 :     coloredToken():hasAll(false){};
     159        1133 :     coloredToken(expr st):mult(st),hasAll(false){};
     160          74 :     coloredToken(int i):mult(i),hasAll(false){};
     161             : 
     162             :     std::set<size_t> varSet()const;
     163             :     friend bool operator< (const coloredToken& lhs, const coloredToken& rhs);
     164             :     friend bool operator== (const coloredToken& lhs, const coloredToken& rhs);
     165             : };
     166             : 
     167      146484 : struct transition {
     168             :   size_t id;
     169             :   std::string name;
     170             :   bool isTraced;
     171             :   TransType type;
     172             :   Distribution dist;
     173             :   expr priority;
     174             :   expr weight;
     175             :   bool singleService;
     176             :   bool markingDependant;
     177             :   int nbServers;
     178             :   bool ageMemory;
     179             :   std::set<size_t> varDomain;
     180             :   expr guard;
     181             :   std::string update;
     182         512 :   transition():isTraced(true),guard(true){}
     183           1 :   transition(size_t i,const std::string &n,const expr &p,bool md):id(i),name(n),isTraced(true),
     184             :                                  type(Timed),dist(p),priority(expr(1)),weight(expr(1.0)),singleService(true),
     185           1 :                                  markingDependant(md),nbServers(1),ageMemory(false),guard(true){};
     186             : };
     187             : 
     188             : struct InterfaceSRG {
     189             :   bool isInterface;
     190             :   unsigned int index;
     191         578 :   InterfaceSRG():isInterface(false),index(0){};
     192             : };
     193             : 
     194        6003 : struct place {
     195             :   size_t id;
     196             :   std::string name;
     197             :   bool isTraced;
     198             :   size_t colorDom;
     199             :   std::vector<coloredToken> initMarking;
     200             :   expr Marking;
     201             :   InterfaceSRG isrg;
     202         578 :   place(){ colorDom = UNCOLORED_DOMAIN; isTraced = true; }
     203        2512 :   bool operator < (const place &p)const{ return id < p.id ;}
     204             :   bool operator > (const place &p)const{ return id > p.id ;}
     205             :   bool operator == (const place &p)const{ return id == p.id ;}
     206             : };
     207             : 
     208      377801 : struct arc {
     209           0 :     arc(const expr& val):isEmpty(false),isMarkDep(val.is_markDep()),isColored(false),exprVal(val){};
     210         173 :     arc(size_t v):isEmpty(false),isMarkDep(false),isColored(false),exprVal((int)v){};
     211          34 :     arc():isEmpty(true),isMarkDep(false),isColored(false),exprVal(0){};
     212        1318 :     arc(const expr &val,const std::vector<coloredToken> &vct):isEmpty(false),isMarkDep(val.is_markDep()),exprVal(val),coloredVal(vct){
     213        1318 :         isColored = val.t == ColouredPlaceHolder ;
     214        1318 :     }
     215             :     bool isEmpty;
     216             :     bool isMarkDep;
     217             :     bool isColored;
     218             :     expr exprVal;
     219             : /*    size_t intVal;
     220             :     std::string stringVal;*/
     221             :     std::vector<coloredToken> coloredVal;
     222             : 
     223         534 :     bool containsVar(size_t v) const {
     224         534 :         if (isEmpty) { return false; }
     225          13 :         if (not isColored) { return false; }
     226           4 :         for (const auto& tok : coloredVal) {
     227           4 :             for (size_t i = 0; i < tok.field.size();i++) {
     228           4 :                 if (tok.Flags[i] == CT_VARIABLE and (size_t)tok.field[i].intVal == v) { return true; }
     229             :             }
     230             :         }
     231           0 :         return false;
     232             :     }
     233             : };
     234             : 
     235             : //first is transition, second is place.
     236             : struct classcomp {
     237     3110107 :   bool operator() (const std::pair<size_t,size_t> &lhs,const std::pair<size_t,size_t> &rhs) const
     238             :   {
     239     3110107 :     if(lhs.first == rhs.first){
     240      703239 :       return lhs.second< rhs.second;
     241             :     }
     242     2406868 :     else return lhs.first<rhs.first;
     243             :   }
     244             : };
     245             : 
     246             : //maps place,transition to an arc
     247             : typedef std::map< std::pair<size_t,size_t>, arc, classcomp>  arcStore;
     248             : 
     249         102 : struct GspnType {
     250             :   size_t tr;
     251             :   size_t pl;
     252             :   size_t nbpass;
     253             : 
     254             :   std::set<std::string> TransList;
     255             :   std::vector<colorClass> colClasses;
     256             :   std::vector<colorDomain> colDoms;
     257             :   std::vector<colorVariable> colVars;
     258             :   std::vector<hybridVariable> hybridVars;
     259             : 
     260             :   std::map<std::string, int> PlacesId;
     261             :   std::map<std::string, int> TransId;
     262             : 
     263             :   std::vector<transition> transitionStruct;
     264             :   std::vector<place> placeStruct;
     265             :   std::vector<userDefineDistribution> distribStruct;
     266             : 
     267      185094 :   inline static std::pair<size_t, size_t> arckey(size_t t,size_t p) { return std::make_pair(t,p); };
     268         367 :   inline static size_t get_t(std::pair<size_t, size_t> key){return key.first;};
     269          38 :   inline static size_t get_p(std::pair<size_t, size_t> key){return key.second;};
     270      183603 :   inline static const arc access(const arcStore &h,size_t t, size_t p){
     271      183603 :     static const arc emptyarc;
     272      183603 :     auto a = h.find(arckey(t,p));
     273      183603 :     if(a == h.end()){return emptyarc;}else return a->second;
     274             :   };
     275             :   arcStore inArcsStruct;
     276             :   arcStore outArcsStruct;
     277             :   arcStore inhibArcsStruct;
     278             : 
     279             :   std::map <std::string, int> IntConstant;
     280             :   std::map <std::string, double> RealConstant;
     281             :   std::set <std::string> ExternalConstant;
     282             : 
     283         198 :   inline bool isColored()const{
     284         198 :     return colClasses.size()>0;
     285             :   }
     286             : 
     287          34 :   GspnType(){
     288          34 :     nbpass=0;
     289          34 :     colDoms.push_back(colorDomain());
     290          34 :   }
     291             : 
     292             :   void iterateDom(const std::string &s,const std::string &sop, const std::string &sclos ,const std::string &s2,const std::string &sop2, const std::string &sclos2 ,const colorDomain & cd, size_t prof,std::function<void (const std::string&,const std::string&)> func);
     293             :   void iterateDomVec(std::vector<color> &v, const colorDomain & cd, size_t prof,std::function<void (const std::vector<color>&)> func);
     294             : 
     295             : 
     296             :   void iterateVars(const std::string &s,const std::string &sop, const std::string &sclos ,const std::set<size_t> &varDom, size_t prof,std::function<void (const std::string&)> func);
     297             :   void iterateVars(std::vector<color> &v, const std::set<size_t> &varDom, size_t prof,std::function<void (const std::vector<color>&)> func);
     298             : 
     299             :   bool isInVar(size_t var, size_t tr)const;
     300             :   bool isInVar(const coloredToken &var, size_t tr)const;
     301             : 
     302             :   //Build a set of all token shape. The mapped value is a pair of an id and the domain.
     303             :   std::map<coloredToken, std::pair<size_t,size_t>> computeMacroVar()const;
     304             : 
     305             : protected:
     306             :   std::map<std::string,size_t> uid;
     307             : public:
     308             :   size_t get_uid(const std::string &str);
     309             :   size_t new_uid(const std::string &str);
     310             : };
     311             : void searchreplace(const std::string &in,const std::string &motif,const std::string &rep,std::string &out);
     312             : 
     313             : namespace text_output {
     314             :   std::ostream& operator<<(std::ostream& os, const color& obj);
     315             :   std::ostream& operator<<(std::ostream& os, const colorClass& obj);
     316             :   std::ostream& operator<<(std::ostream& os, const ProbabiliteDistribution& obj);
     317             :   std::ostream& operator<<(std::ostream& os, const userDefineDistribution& obj);
     318             :   std::ostream& operator<<(std::ostream& os, const transition& obj);
     319             :   std::ostream& operator<<(std::ostream& os, const coloredToken& tok);
     320             :   std::ostream& operator<<(std::ostream& os, const place& obj);
     321             :   std::ostream& operator<<(std::ostream& st, const arcStore& as);
     322             :   std::ostream& operator<<(std::ostream& st, const GspnType& tr);
     323             : }
     324             : 
     325             : 
     326             : #endif /* defined(__Cosmos__Gspn_model__) */

Generated by: LCOV version 1.13