-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgdevs.cpp
More file actions
384 lines (332 loc) · 11.6 KB
/
gdevs.cpp
File metadata and controls
384 lines (332 loc) · 11.6 KB
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
/*******************************************************************
*
* DESCRIPTION: Atomic Model Gdevs
*
* AUTHOR: AMD & GC
*
* EMAIL:
*
* DATE: 21/11/1999
*
*******************************************************************/
/** include files **/
#include "gdevs.h" // class Gdevs
#include "message.h" // class ExternalMessage, InternalMessage
#include "parsimu.h" // ParallelMainSimulator::Instance().getParameter( ... )
using namespace std;
/** public functions **/
/*******************************************************************
* Function Name: Gdevs
* Description:
********************************************************************/
Gdevs::Gdevs( const string &name )
: Atomic( name )
{
// myGp contiene la estructura del modelo DEVS
myGP = new GdevsParser(name);
// agrego los ports del modelo a la simulación
std::list<GdevsParser::GdevsPort>::iterator cursor2;
for( cursor2 = myGP->inports.begin() ; cursor2 != myGP->inports.end() ; cursor2 ++ )
addInputPort( *cursor2 );
for( cursor2 = myGP->outports.begin() ; cursor2 != myGP->outports.end() ; cursor2 ++ )
addOutputPort( *cursor2 );
}
/*******************************************************************
* Function Name: ~Gdevs
* Description: Destructor
********************************************************************/
Gdevs::~Gdevs()
{
delete myGP;
}
/*******************************************************************
* Function Name: initFunction
* Description: Resetea la lista
* Precondition: El tiempo del proximo evento interno es Infinito
********************************************************************/
Model &Gdevs::initFunction()
{
myGP->estadoActual = myGP->burbujas.front();
return *this ;
}
/*******************************************************************
* Function Name: externalFunction
* Description:
********************************************************************/
Model &Gdevs::externalFunction( const ExternalMessage &msg )
{
GdevsParser::GdevsEstado estado;
estado = myGP->externa( myGP->estadoActual, msg.port().name(), msg.value() );
if( estado != "" ) {
myGP->estadoActual = estado;
cout << "Transicion EXTERNA, Hora: " << msg.time() << " ,estado destino:"<< myGP->estadoActual<< endl ;
GdevsParser::GdevsEstado estado1 = myGP->interna( myGP->estadoActual );
if( estado1 != "" ) {
cout << "Luego de la transición se queda en holdin con estado destino " << estado1 << endl<< endl;
//cout << "Timeout:" << myGP->timeout << endl;
holdIn(active, myGP->timeout );
} else {
//cout << "sin timeout" << endl;
passivate();
}
}
else cout << "Transición externa no definida!" << endl;
// cout << "Transicion EXTERNA, Hora: " << msg.time() << " ,estado destino:"<< myGP->estadoActual<< endl << endl ;
return *this;
}
/*******************************************************************
* Function Name: internalFunction
* Description:
********************************************************************/
Model &Gdevs::internalFunction( const InternalMessage &msg )
{
// cout << "Empieza internal function" << endl;
GdevsParser::GdevsEstado estado;
estado = myGP->interna( myGP->estadoActual );
if( estado != "" ) {
myGP->estadoActual = estado;
cout << "Transicion INTERNA, Hora: " << msg.time() << " ,estado destino:"<< myGP->estadoActual<< endl ;
GdevsParser::GdevsEstado estado1 = myGP->interna( myGP->estadoActual );
if( estado1 != "" ) {
cout << "Luego de la transición se queda en holdin con estado destino " << estado1 << endl<< endl;
//cout << "Timeout:" << myGP->timeout << endl;
holdIn(active, myGP->timeout );
} else {
//cout << "sin timeout" << endl;
passivate();
}
}
else cout << "Transición interna no definida!" << endl;
//cout << "Transición interna- Estado post trans. :"<< myGP->estadoActual<< endl <<endl ;
return *this ;
}
/*******************************************************************
* Function Name: outputFunction
* Description:
********************************************************************/
Model &Gdevs::outputFunction( const InternalMessage &msg )
{
GdevsParser::GdevsPort portname;
int valor;
myGP->salida( myGP->estadoActual, portname, valor);
sendOutput( msg.time(), port(portname), valor) ;
return *this ;
}
// copylist
// copia una lista de Ini::IdList a una.
// El tipo de la lista destino se especifica como Template
template <class T> static copylist( const Ini::IdList &l, std::list<T> &lista2 )
{
std::list<T>::const_iterator cursor ;
lista2.insert (lista2.end(), l.begin(), l.end());
// descomentar para ver el resultado
//for( cursor = lista2.begin() ; cursor != lista2.end() ; cursor ++ )
//{
// cout << '[' << (*cursor) << ']' << endl ;
//}
}
template <class T> static int find( const T &e, std::list<T> &x )
{
std::list<T>::const_iterator cursor ;
int status = 0;
for( cursor = x.begin() ; cursor != x.end() ; cursor ++ )
{
if ( e == *cursor ) {
status = 1;
break;
}
}
return status;
}
// Parsea las transiciones externas del archivo ini.
GdevsParser::parseTransExt( )
{
const Ini::IdList &l = ParallelMainSimulator::Instance().getParameters(modelName, "ext");
Ini::IdList::const_iterator cursor ;
// recorre las transiciones por separado
int campo=0;
transicion t;
for( cursor = l.begin() ; cursor != l.end() ; cursor ++ )
{
switch(campo) {
case 0: // origen
if( find<GdevsEstado>( *cursor, burbujas ) ) {
t.origen = *cursor;
} else // error
cout << "Estado origen invalido : " << *cursor << endl;
break;
case 1: // destino
if( find<GdevsEstado>( *cursor, burbujas ) ) {
t.destino = *cursor;
} else // error
cout << "Estado destino invalido : " << *cursor << endl;
break;
case 2: // port
if( find<GdevsPort>( *cursor, inports ) ) {
t.port = *cursor;
} else // error
cout << "Port invalido : " << *cursor << endl;
break;
case 3: // valor
if( atoi((*cursor).c_str()) > 0 ) {
t.valor = atoi((*cursor).c_str());
} else // error
cout << "Valor inválido : " << *cursor << endl;
transExterna.insert(transExterna.end(), t);
break;
}
campo++;
campo %= 4;
}
if (campo != 0)
cout << "Transición incompleta" << endl;
// descomente para ver las transiciones
//list<transicion>::iterator cursor2;
//for( cursor2 = transExterna.begin() ; cursor2 != transExterna.end() ; cursor2 ++ )
// cout << "Externa=Origen:" << ((*cursor2).origen) << ". Destino:"
// << ((*cursor2).destino) << ". Port:"<< ((*cursor2).port)
// << ". Valor:" << ((*cursor2).valor) << endl ;
}
// Parsea las transiciones externas del archivo ini.
GdevsParser::parseTransInt( )
{
const Ini::IdList &l = ParallelMainSimulator::Instance().getParameters(modelName, "int");
Ini::IdList::const_iterator cursor ;
//lista2.insert (lista2.end(), l.begin(), l.end());
int campo=0;
transicion t;
for( cursor = l.begin() ; cursor != l.end() ; cursor ++ )
{
switch(campo) {
case 0: // origen
if( find<GdevsEstado>( *cursor, burbujas ) ) {
t.origen = *cursor;
} else // error
cout << "Estado origen invalido : " << *cursor << endl;
break;
case 1: // destino
if( find<GdevsEstado>( *cursor, burbujas ) ) {
t.destino = *cursor;
} else // error
cout << "Estado destino invalido : " << *cursor << endl;
break;
case 2: // port
if( find<GdevsPort>( *cursor, outports ) ) {
t.port = *cursor;
} else // error
cout << "Port invalido : " << *cursor << endl;
break;
case 3: // valor
if( atoi((*cursor).c_str()) > 0 ) {
t.valor = atoi((*cursor).c_str());
} else // error
cout << "Valor inválido : " << *cursor << endl;
transInterna.insert(transInterna.end(), t);
break;
}
campo++;
campo %= 4;
}
if (campo != 0)
cout << "Transición incompleta" << endl;
// descomente para ver las transiciones
//list<transicion>::iterator cursor2;
//for( cursor2 = transInterna.begin() ; cursor2 != transInterna.end() ; cursor2 ++ )
// cout << "Interna=Origen:" << ((*cursor2).origen) << ". Destino:"
// << ((*cursor2).destino) << ". Port:"<< ((*cursor2).port)
// << ". Valor:" << ((*cursor2).valor) << endl ;
}
GdevsParser::loadData() {
copylist<GdevsParser::GdevsEstado>(ParallelMainSimulator::Instance().getParameters(modelName, "burbuja"), burbujas);
copylist<GdevsParser::GdevsPort>(ParallelMainSimulator::Instance().getParameters(modelName, "inport"), inports);
copylist<GdevsParser::GdevsPort>(ParallelMainSimulator::Instance().getParameters(modelName, "outport"), outports);
parseTransExt();
parseTransInt();
}
// busca un transición externa que parta de estado y con entrada valor por port. Devuelve la
// primera transición que encuentra.
GdevsParser::GdevsEstado GdevsParser::externa( GdevsEstado estado, GdevsPort port, int valor )
{
//cout << "entro en GP externa" << endl;
std::list<transicion>::const_iterator cursor ;
int hallo=0;
for( cursor = transExterna.begin() ; cursor != transExterna.end() ; cursor ++ )
{
//cout << "valor(msg,cursor)" << valor <<" , " << (*cursor).valor << endl;
if( estado == estadoActual
&& estado == (*cursor).origen
&& port == (*cursor).port
&& valor == (*cursor).valor
) {
hallo = 1;
break;
}
//cout << "port: " << port << " valor (msg , cursor): " << valor <<","<< (*cursor).valor << endl;
}
if( hallo ) {
//cout << "SALIO !!! port: " << port << " valor: " << valor << " destino: " << (*cursor).destino << endl;
return (*cursor).destino;
}
else {
//cout << "salio por no hallado de GP externa" << endl;
return "";
}
}
// busca la transición interna que sale de estado
GdevsParser::GdevsEstado GdevsParser::interna( GdevsEstado estado )
{
std::list<transicion>::const_iterator cursor ;
int hallo=0;
//cout << "entro en GP interna" << endl;
for( cursor = transInterna.begin() ; cursor != transInterna.end() ; cursor ++ )
{
if( estado == (*cursor).origen
) {
hallo = 1;
break;
}
//cout << "Estado (inicial, destino)" << estado <<","<< (*cursor).destino << endl;
}
if( hallo ) {
//cout << "SALIO !!! Estado (inicial, destino)" << estado <<","<< (*cursor).destino << endl;
return (*cursor).destino;
}
else return "";
}
// devuelve el port y el valor de salida de la transición interna que sale del estado
GdevsParser::salida( GdevsEstado estado, GdevsPort &port, int &valor )
{
std::list<transicion>::const_iterator cursor ;
int hallo=0;
for( cursor = transInterna.begin() ; cursor != transInterna.end() ; cursor ++ )
{
if( estado == (*cursor).origen
) {
hallo = 1;
break;
}
}
if( hallo ) {
port = (*cursor).port;
valor = (*cursor).valor;
}
// return "";
}
/*const Port &Gdevs::PortByName( string &name )
{
PortList::const_iterator cursor ;
int hallo=0;
for( cursor = outputPorts().begin() ; cursor != outputPorts().end() ; cursor ++ )
{
if( name == (*cursor).name()
) {
hallo = 1;
break;
}
}
if( hallo ) {
return (*cursor);
} else return NULL;
// return "";
}
*/