Net.cpp 13.9 KB
Newer Older
Hiba Ouni's avatar
parser    
Hiba Ouni committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <ext/hash_map>
#include <algorithm>
#include "Net.h"
#define TAILLEBUFF 500
/***********************************************************/
/*                      class Node                         */
/***********************************************************/

void  Node::addPre(int node,int valuation){
  pair<int,int> x(node,valuation);
  pre.push_back(x);
}

void Node::addPost(int node,int valuation){
  pair<int,int> x(node,valuation);
  post.push_back(x);
}

void  Node::addInhibitor(int node,int valuation){
  pair<int,int> x(node,valuation);
  inhibitor.push_back(x);
}

void  Node::addPreAuto(int node,int valuation){
  pair<int,int> x(node,valuation);
  preAuto.push_back(x);
}

void Node::addPostAuto(int node,int valuation){
  pair<int,int> x(node,valuation);
  postAuto.push_back(x);
}

void Node::addReset(int node){
  reset.push_back(node);
}

/***********************************************************/
/*                      class RdPE                         */
/***********************************************************/
net::net(const char *f,const char*Formula_trans, const char* Int_trans){
  cout<<"CREATION D'UN NOUVEAU SOUS-RESEAU \n";
  if(create(f)){
    for (vector<class Place>::iterator p=places.begin();p!=places.end();p++){
      sort(p->pre.begin(),p->pre.end());
      sort(p->post.begin(),p->post.end());
    }
    for (vector<class Transition>::iterator p=transitions.begin();p!=transitions.end();p++){
      sort(p->pre.begin(),p->pre.end());
      sort(p->post.begin(),p->post.end());
    }
  }
  else{
    places.clear();
    transitions.clear();
    placeName.clear();
    transitionName.clear();
  }
 if(strlen(Formula_trans)>0)
  {
    //    cout<<"transitions de la formule non vide \n";
    Set_Formula_Trans(Formula_trans);
    if(strlen(Int_trans)>0)
    {
      Set_Interface_Trans(Int_trans);
      //cout<<"transitions de l'interface non vide \n";
    }
    cout<<"______________66666666666666666666666______________________\n";
    set_union(InterfaceTrans.begin(), InterfaceTrans.end(),Formula_Trans.begin(),Formula_Trans.end(), inserter(Observable, Observable.begin()));
      Set_Non_Observables();
  }
  else
	  for(unsigned int i=0;i<transitions.size();i++)
		  Observable.insert(i);
cout<<"FIN CREATION \n";
}
/*---------------------------------Init Set of  transitions ------------------------------*/
/*---------------------------------Set_formula_trans()------------------*/
bool net::Set_Formula_Trans(const char * f)
{
	FILE *in;
	int i,nb;
	//	cout<<"ici set formula transitions \n";
	int pos_trans (TRANSITIONS, string);
	char Buff[TAILLEBUFF], *z;
	in = fopen(f,"r");
	if (in == NULL)
	{
		cout << "file " << f << " doesn't exist" << endl;
		exit(1);
	}
	int nb_formula_trans;
	fscanf(in,"%d\n",&nb_formula_trans);
  	nb = fread(Buff,1,TAILLEBUFF-1,in);
  	Buff[nb] = '\0';
	z = strtok(Buff, " \t\n");
	cout<<"taille "<<TAILLEBUFF<<" buff "<<Buff<<" z: "<<z<<endl;
	for(i= 0;i<nb_formula_trans;i++)
	{
	cout<<" z: "<<z<<endl;
		if(z == NULL)
		{
			cout << "error in formula trans format "<< endl;
			return false;
	  	}
		string tmp(z);
		int pos=pos_trans(transitions,tmp);
		if(pos==-1)
		{
		    cout<<z<<"    Error??? : observale transition "<<tmp<<" doesn't exist \n";
		    //return false;
		}
		else
		  Formula_Trans.insert(pos);
		/*cout<<"insertion de :"<<transitions[pos].name<<endl;*/
		z = strtok(NULL, " \t\n");
		if (z == NULL)
		{
  			nb = fread(Buff,1,TAILLEBUFF-1,in);
  			Buff[nb] = '\0';
			z = strtok(Buff, " \t\n");
		}
	}
	fclose(in);
	return true;
}
/*---------------------------------Set_Interface_trans()------------------*/
bool net::Set_Interface_Trans(const char * f)
{
	FILE *in;
	int i,nb;
	int pos_trans (TRANSITIONS, string);
	char Buff[TAILLEBUFF],* z;
	in = fopen(f,"r");
	if (in == NULL)
	{
		cout << "file " << f << " doesn't exist" << endl;
		exit(1);
	}
	int int_trans;
	fscanf(in,"%d\n",&int_trans);
  	nb = fread(Buff,1,TAILLEBUFF-1,in);
  	Buff[nb] = '\0';
	z = strtok(Buff, " \t\n");
	cout<<"taille "<<TAILLEBUFF<<" buff "<<Buff<<" z: "<<z<<endl;
	for(i= 0;i<int_trans;i++)
	{
	cout<<" z: "<<z<<endl;
		if(z == NULL)
		{
			cout << "error in interface format "<< endl;
			return false;
	  	}
		string tmp(z);
		int pos=pos_trans(transitions,tmp);
		//if(Formula_Trans.find(pos)==Formula_Trans.end())
		if(pos==-1)
		{
			cout<<z<<"         Error??? : interface transition doesn't exist \n";
		//	return false;
		}
		else
		  InterfaceTrans.insert(pos);
		z = strtok(NULL, " \t\n");
		if (z == NULL)
		{
  			nb = fread(Buff,1,TAILLEBUFF-1,in);
  			Buff[nb] = '\0';
			z = strtok(Buff, " \t\n");
		}
	}
	fclose(in);
	return true;
}
/*---------------------------------Set_Non_Observables()------------------*/
void net::Set_Non_Observables()
{
	NonObservable.clear();
	for(unsigned int i=0;i<transitions.size();i++)
		if(Observable.find(i)==Observable.end())
		{

			NonObservable.insert(i);
		}
}
/*-----------------------pos_trans()--------------------*/
int pos_trans(TRANSITIONS T, string trans)
{
	int pos=0;
	//	cout<<"on cherche "<<trans<<" dans :\n";
	for(TRANSITIONS::const_iterator i=T.begin();!(i==T.end());i++,pos++)
	  {
	    //  cout<<i->name<<"   ";
	    if(i->name==trans)
			return pos;
	      }
	//cout<<"Non trouve :\n";
	return -1;
}
/* ------------------------------ operator<< -------------------------------- */
ostream& operator<<(ostream& os, const Set& s)
{
	bool b = false;

	if (s.size())
	{
		for (Set::const_iterator i=s.begin(); !(i==s.end()); i++)
		{
			if (b)
				os << ", ";
			else
				os << "{";
			os  << *i<<" ";
			b = true;
		}
		os << "}";
	}
	else
		os << "empty set";
	return os;

}
/*----------------------------------------------------------------------*/
bool net::addPlace(const string &place,int marking,int capacity){
  map<string,int>::const_iterator pi=placeName.find(place);
  if(pi==placeName.end()){
    placeName[place]=places.size();
    Place p(place,marking, capacity);
    places.push_back(p);
    return true;
  }
  else
    return false;
}

bool net::addQueue(const string &place,int capacity){
  map<string,int>::const_iterator pi=placeName.find(place);
  if(pi==placeName.end()){
    placeName[place]=places.size();
    Place p(place,-1, capacity);
    places.push_back(p);
    return true;
  }
  else
    return false;
}

bool net::addLossQueue(const string &place,int capacity){
  map<string,int>::const_iterator pi=placeName.find(place);
  if(pi==placeName.end()){
    placeName[place]=places.size();
    Place p(place,-2, capacity);
    places.push_back(p);
    return true;
  }
  else
    return false;
}

bool net::addTrans(const string &trans){
  map<string,int>::const_iterator ti=transitionName.find(trans);
  if(ti==transitionName.end()){
    transitionName[trans]=transitions.size();
    Transition t(trans);
    transitions.push_back(t);
    return true;
  }
  else
    return false;
}

bool net::addPre(const string &place,const string &trans,int valuation){
  int p,t;
  map<string,int>::const_iterator pi=placeName.find(place);
  if(pi==placeName.end() || places[pi->second].isQueue())
    return false;
  else
    p=pi->second;
  map<string,int>::const_iterator ti=transitionName.find(trans);
  if(ti==transitionName.end())
    return false;
  else
    t=ti->second;
  transitions[t].addPre(p,valuation);
  places[p].addPost(t,valuation);
  return true;
}

bool net::addPost(const string &place,const string &trans,int valuation){
  int p,t;
  map<string,int>::const_iterator pi=placeName.find(place);
  if(pi==placeName.end() || places[pi->second].isQueue())
    return false;
  else
    p=pi->second;
  map<string,int>::const_iterator ti=transitionName.find(trans);
  if(ti==transitionName.end())
    return false;
  else
    t=ti->second;
  transitions[t].addPost(p,valuation);
  places[p].addPre(t,valuation);
  return true;
}

bool net::addPreQueue(const string &place,const string &trans,int valuation){
  int p,t;
  map<string,int>::const_iterator pi=placeName.find(place);
  if(pi==placeName.end() || !places[pi->second].isQueue())
    return false;
  else
    p=pi->second;
  map<string,int>::const_iterator ti=transitionName.find(trans);
  if(ti==transitionName.end())
    return false;
  else
    t=ti->second;
  transitions[t].addPre(p,valuation);
  places[p].addPost(t,valuation);
  return true;
}

bool net::addPostQueue(const string &place,const string &trans,int valuation){
  int p,t;
  map<string,int>::const_iterator pi=placeName.find(place);
  if(pi==placeName.end() || !places[pi->second].isQueue())
    return false;
  else
    p=pi->second;
  map<string,int>::const_iterator ti=transitionName.find(trans);
  if(ti==transitionName.end())
    return false;
  else
    t=ti->second;
  transitions[t].addPost(p,valuation);
  places[p].addPre(t,valuation);
  return true;
}

bool net::addInhibitor(const string &place,const string &trans,int valuation){
  int p,t;
  map<string,int>::const_iterator pi=placeName.find(place);
  if(pi==placeName.end())
    return false;
  else
    p=pi->second;
  map<string,int>::const_iterator ti=transitionName.find(trans);
  if(ti==transitionName.end())
    return false;
  else
    t=ti->second;
  transitions[t].addInhibitor(p,valuation);
  places[p].addInhibitor(t,valuation);
  return true;
}

bool net::addPreAuto(const string &place,const string &trans,const string &valuation){
  int p,t,v;
  map<string,int>::const_iterator pi=placeName.find(place);
  if(pi==placeName.end() || places[pi->second].isQueue())
    return false;
  else
    p=pi->second;
  map<string,int>::const_iterator ti=transitionName.find(trans);
  if(ti==transitionName.end())
    return false;
  else
    t=ti->second;
  map<string,int>::const_iterator pv=placeName.find(valuation);
  if(pv==placeName.end() || places[pv->second].isQueue())
    return false;
  else
    v=pv->second;
  transitions[t].addPreAuto(p,v);
  places[p].addPostAuto(t,v);
  return true;
}

bool net::addPostAuto(const string &place,const string &trans,const string &valuation){
  int p,t,v;
  map<string,int>::const_iterator pi=placeName.find(place);
  if(pi==placeName.end() || places[pi->second].isQueue())
    return false;
  else
    p=pi->second;
  map<string,int>::const_iterator ti=transitionName.find(trans);
  if(ti==transitionName.end())
    return false;
  else
    t=ti->second;
  map<string,int>::const_iterator pv=placeName.find(valuation);
  if(pv==placeName.end() || places[pi->second].isQueue())
    return false;
  else
    v=pv->second;
  transitions[t].addPostAuto(p,v);
  places[p].addPreAuto(t,v);
  return true;
}

bool net::addReset(const string &place,const string &trans){
  int p,t;
  map<string,int>::const_iterator pi=placeName.find(place);
  if(pi==placeName.end())
    return false;
  else
    p=pi->second;
  map<string,int>::const_iterator ti=transitionName.find(trans);
  if(ti==transitionName.end())
    return false;
  else
    t=ti->second;
  transitions[t].addReset(p);
  places[p].addReset(t);
  return true;
}

/* Visualisation */
ostream& operator<<(ostream &os,const net &R){

/* affichage nombre de places et de transitions */
  os<<"***************************"<<endl;
  os<<"Nombre de places     :"<<R.nbPlace()<<endl;
  os<<"Nombre de transitions:"<<R.nbTransition()<<endl;

/* affichage de la liste des places */
  os<<"********** places **********"<<endl;
  int i=0;
  for (vector<class Place>::const_iterator p=R.places.begin();p!=R.places.end();p++,i++){
    if (p->isQueue()) {
       	os<<"queue " << setw(4)<<i<<":"<<p->name<<", cp("<<p->capacity<<")";
		if (p->isLossQueue())
			cout << " loss";
		cout << endl;
	}
	else
    	os<<"place " << setw(4)<<i<<":"<<p->name<<":"<<p->marking<<" <..>, cp(" << p->capacity<<")"<<endl;
  }
 os<<"********** transitions  de la formule  **********"<<endl;
  for(Set::const_iterator h=R.Formula_Trans.begin();!(h==R.Formula_Trans.end());h++)
	  cout<<R.transitions[*h].name<<endl;
  os<<"********** transitions  de l'interface  **********"<<endl;
  for(Set::const_iterator h=R.InterfaceTrans.begin();!(h==R.InterfaceTrans.end());h++)
	  cout<<R.transitions[*h].name<<endl;
  os<<"Nombre de transitions observable:"<<R.Observable.size()<<endl;
  os<<"********** transitions observables **********"<<endl;
  for(Set::const_iterator h=R.Observable.begin();!(h==R.Observable.end());h++)
	  cout<<R.transitions[*h].name<<endl;
	    os<<"Nombre de transitions non observees:"<<R.NonObservable.size()<<endl;
  os<<"********** transitions  non observees **********"<<endl;
  for(Set::const_iterator h=R.NonObservable.begin();!(h==R.NonObservable.end());h++)
	  cout<<R.transitions[*h].name<<endl;
  i=0;
   os<<"Nombre global de transitions :"<<R.nbTransition()<<endl;
  os<<"********** transitions  **********"<<endl;
  for (TRANSITIONS::const_iterator t=R.transitions.begin();t!=R.transitions.end();t++,i++)
{
    os<<setw(4)<<i<<":"<<t->name<<endl;

    // os<<"        IN  { ";
    //for (vector< pair<int,int> >::const_iterator a=p->pre.begin();a!=p->pre.end();a++)
    //  if (R.places[a->first].isQueue())
    //  os<<R.places[a->first].name<<":<"<<a->second<<">;";
    //  else
    //  os<<R.places[a->first].name<<":"<<a->second<<";";
    //    for (vector< pair<int,int> >::const_iterator a=p->inhibitor.begin();a!=p->inhibitor.end();a++)
    //os<<R.places[a->first].name<<"<"<<a->second<<";";
    //for (vector< pair<int,int> >::const_iterator a=p->preAuto.begin();a!=p->preAuto.end();a++)
    //os<<R.places[a->first].name<<":"<<R.places[a->second].name<<";";
    //for (vector<int>::const_iterator a=p->reset.begin();a!=p->reset.end();a++)
    //os<<R.places[*a].name<<": reset;";
    //os<<" }"<<endl;

    //os<<"        OUT { ";
    //for (vector< pair<int,int> >::const_iterator a=p->post.begin();a!=p->post.end();a++)
    //  if (R.places[a->first].isQueue())
    //  os<<R.places[a->first].name<<":<"<<a->second<<">;";
    //  else
    //  os<<R.places[a->first].name<<":"<<a->second<<";";
    //for (vector< pair<int,int> >::const_iterator a=p->postAuto.begin();a!=p->postAuto.end();a++)
    //os<<R.places[a->first].name<<":"<<R.places[a->second].name<<";";
    //os<<" }"<<endl;
  }
  return(os);
}