-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCIndetSim.cpp
424 lines (335 loc) · 12.5 KB
/
SCIndetSim.cpp
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
/*-----------------------------------------------------------------------------+
| |
| SCL - Simulation Class Library |
| |
| (c) 1994-98 Marc Diefenbruch, Wolfgang Textor |
| Universitaet Essen |
| |
+---------------+-------------------+---------------+-------------------+------+
| Module | File | Created | Project | |
+---------------+-------------------+---------------+-------------------+------+
| SCIndetSim | SCIndetSim.cc | 25. Apr 1996 | SCL | |
+---------------+-------------------+---------------+-------------------+------+
| |
| Change Log |
| |
| Nr. Date Description |
| ----- -------- ------------------------------------------------------ |
| 001 |
| 000 --.--.-- Neu angelegt |
| |
+-----------------------------------------------------------------------------*/
/* Lineal
00000000001111111111222222222233333333334444444444555555555566666666667777777777
01234567890123456789012345678901234567890123456789012345678901234567890123456789
*/
#include "SCBasicTypes.h"
#include "SCStream.h"
#include "SCIndetSim.h"
#include "SCProcess.h"
#include "SCSignal.h"
#include "SCProcessType.h"
#include "SCActiveRunnable.h"
#include "SCTimeEvent.h"
#include "SCDebug.h"
#if _SC_SIMULATION_DEBUG
#include "SCEnabledTransition.h"
#endif
#include "SCTraceControl.h"
#if _SC_DMALLOC
#include <dmalloc.h>
#endif
#if _SC_NOINLINES
#include "SCIndetSim.inl.h"
#endif
SCIndetSim::SCIndetSim (SCTime maxTime,
SCBoolean errors,
SCNatural numErrors,
SCBoolean deadCode,
SCNatural depth,
SCName2PropFuncPtr * qAtom):
SCIndet(scIndetSimulation, maxTime, false,
errors, numErrors, deadCode, qAtom),
maxDepth(depth),
enabledTransitionList(NULL)
{
randomizer = new SCRandUniform (0.0, 1.0);
assert(randomizer);
}
SCIndetSim::~SCIndetSim (void)
{
if (randomizer)
delete randomizer;
if (enabledTransitionList)
delete enabledTransitionList;
}
SCRunnable *SCIndetSim::ChooseRunnable (SCTimeEventSaveList *activeQueue)
{
SCRunnable * runnable;
SCAutomaton * automaton;
SCActiveRunnable * activeRunnable;
SCActiveRunnableList * activeRunnableList;
SCNatural choice;
SCTimeEvent * timeEvent;
///////////////////////////////////////////////////////
// 1. Check if state is faulty: //
///////////////////////////////////////////////////////
if (IsErrorState(activeQueue))
{
return NULL;
}
///////////////////////////////////////////////////////
// 2. Check if any backtracking conditions are true: //
///////////////////////////////////////////////////////
if (IsLimitingState(activeQueue))
{
return NULL;
}
activeRunnableList = ActiveRunnables(activeQueue);
assert(!activeRunnableList->IsEmpty());
choice = NonDetChoice (activeRunnableList->NumOfElems());
activeRunnable = activeRunnableList->Remove(choice);
assert(activeRunnable);
delete activeRunnableList;
runnable = activeRunnable->GetRunnable();
assert (runnable);
delete activeRunnable;
if (!runnable->IsAutomaton()) // machines or timer are
{ // always a good choice!
#if _SC_SIMULATION_DEBUG
scSimulationDebugLog << "SCIndetSim::ChooseRunnable(): choose ";
scSimulationDebugLog << *runnable << std::endl;
#endif
executedTransitions++; // count pseudo-transitions
}
else // runnable IS automaton!
{
automaton = (SCAutomaton *)runnable;
// if the automaton is suspended in its State() method
// we need the set of enabled transitions for
// the ChooseTransition() method (in simulation we don't
// determine the enabled transition in method ActiveRunnables()
// like in validation, because we want to avoid the
// overhead produced by calcualting the enabled transitions
// for all processes if we finally uses only one process!):
if (automaton->IsInState())
{
#if _SC_SIMULATION_DEBUG
scSimulationDebugLog << "SCIndetSim::ChooseRunnable(): choose ";
scSimulationDebugLog << *automaton << "." << std::endl;
#endif
enabledTransitionList = automaton->RetrieveEnabledTransitions();
// if we have continuous signals (whose enabling conditions
// are all false) in timed states, it is possible that
// enabledTransitionList _IS_ NULL!
// assert (enabledTransitionList);
}
#if _SC_SIMULATION_DEBUG
else
{
scSimulationDebugLog << "SCIndetSim::ChooseRunnable(): ";
scSimulationDebugLog << "choose requesting or calling ";
scSimulationDebugLog << *automaton << "." << std::endl;
}
#endif
}
timeEvent = activeQueue->LookupRunnable(runnable);
if (!timeEvent)
{
// if this happens then something is wrong
// with SCTable::Find()!
SCScheduler::outputStream << std::endl << "Can't find time event in active queue for runnable ";
SCScheduler::outputStream << *runnable << std::endl;
assert(timeEvent);
return NULL;
}
delete activeQueue->Remove(timeEvent); // remove runnable of choosen active
// runnable from activeQueue
return runnable; // return runnable to the Scheduler
}
_SCINLINE_ SCEnabledTransition *SCIndetSim::ChooseTransition (void)
{
SCEnabledTransition * enabledTransition = NULL;
SCNatural choice;
#if _SC_SIMULATION_DEBUG
SCNatural numOfTransitionsLeft; // only for display purpose
#endif
if (!enabledTransitionList) // possible for continuous signals
{ // in timed states (see above)
return NULL;
}
assert(enabledTransitionList);
assert(!enabledTransitionList->IsEmpty());
choice = NonDetChoice (enabledTransitionList->NumOfElems());
enabledTransition = enabledTransitionList->Remove(choice);
// take random elem
assert(enabledTransition);
#if _SC_SIMULATION_DEBUG
numOfTransitionsLeft = enabledTransitionList->NumOfElems();
// store for later display
#endif
delete enabledTransitionList;
enabledTransitionList = NULL;
#if _SC_SIMULATION_DEBUG
scSimulationDebugLog << "SCIndetSim::ChooseTransition(): executing ";
scSimulationDebugLog << *enabledTransition;
scSimulationDebugLog << " (";
scSimulationDebugLog << numOfTransitionsLeft;
scSimulationDebugLog << " enabled transitions left)" << std::endl;
#endif
executedTransitions++;
return (enabledTransition); // give it back to Transition()
// method of SCProcess
}
_SCINLINE_ SCBoolean SCIndetSim::LogError (SCErrorCode errorCode) // deadlock or
{ // violated assertion
if (!FindErrors() || NumOfErrors() >= MaxNumOfErrors())
return false;
numOfErrors++;
switch (errorCode)
{
case scErrorDeadlock:
SCScheduler::errorStream << std::endl << "DEADLOCK DETECTED ! " << std::endl;
numOfDeadlocks++;
break;
case scErrorAssertionFailed:
SCScheduler::errorStream << std::endl << "VIOLATED ASSERTION ! " << std::endl;
numOfFailedAssertions++;
break;
default:
break;
}
return true;
}
_SCINLINE_ SCBoolean SCIndetSim::LogError (SCErrorCode errorCode, // signal drop or
const SCProcess *process, // signal rejected
const SCSignal *signal)
{
if (!FindErrors() || NumOfErrors() >= MaxNumOfErrors())
return false;
numOfErrors++;
switch (errorCode)
{
case scErrorSignalDrop:
SCScheduler::errorStream << std::endl << "PROCESS " << *process << " " << *process->GetLastState() << " DROPPED " << *signal << std::endl;
numOfSignalDrops++;
break;
case scErrorSignalLose:
SCScheduler::errorStream << std::endl << "PROCESS " << *process << " REJECTED " << *signal << std::endl;
numOfSignalLosses++;
break;
default:
break;
}
return true;
}
_SCINLINE_ SCBoolean SCIndetSim::LogError (SCErrorCode errorCode, // no receiver
const SCProcess *process,
const SCSignalType *signal)
{
if (!FindErrors() || NumOfErrors() >= MaxNumOfErrors())
return false;
numOfErrors++;
switch (errorCode)
{
case scErrorNoSignalReceiver:
SCScheduler::errorStream << std::endl << "PROCESS " << *process << " CAN'T SEND " << *signal << std::endl;
numOfMissingReceivers++;
break;
default:
break;
}
return true;
}
_SCINLINE_ SCBoolean SCIndetSim::LogError (SCErrorCode errorCode, // process creation
const SCProcess *father, // failed
const SCProcessType *offspringType)
{
if (!FindErrors() || NumOfErrors() >= MaxNumOfErrors())
return false;
numOfErrors++;
switch (errorCode)
{
case scErrorProcessCreationFailed:
SCScheduler::errorStream << std::endl << "INSTANCE CREATION OF PROCESS TYPE " << *offspringType << " FAILED, CREATOR = " << *father << std::endl;
numOfFailedProcessCreations++;
break;
default:
break;
}
return true;
}
SCErrorCode SCIndetSim::IsErrorState (SCTimeEventSaveList *activeQueue)
{
if (activeQueue->IsEmpty()) // deadlock ?
{
SCTraceControl::LogEvent (scTraceDeadlock);
LogError(scErrorDeadlock);
return scErrorDeadlock;
}
return scErrorNoError;
}
SCBoolean SCIndetSim::IsLimitingState (SCTimeEventSaveList *activeQueue)
{
(void)activeQueue;
if (hasStopTime && // time limit given?
SCScheduler::GetCurrentTime() >= stopTime) // time limit reached?
{
return true;
}
if (maxDepth && // max depth given?
executedTransitions >= maxDepth) // max depth reached?
{
return true;
}
if (stopCondition && stopCondition->Evaluate() == true)
{
SCScheduler::outputStream << std::endl << "Stop condition '" << stopCondition->name;
SCScheduler::outputStream << "' is fulfilled!" << std::endl;
return true;
}
return false;
}
SCStream& operator<< (SCStream& pStream,
const SCIndetSim& pData)
{
return pData.Display(pStream); // virtuelle Funktion aufrufen
}
SCStream& SCIndetSim::Display (SCStream& pStream) const
{
if (executedTransitions == 0) // called for start message?
{
if (type == scIndetSimulation)
{
pStream << std::endl << "This is QSIM version ";
pStream << SCL_SIMULATOR_VERSION << std::endl << std::endl;
}
SCIndet::Display(pStream);
if (stopCondition)
{
pStream << "Stop condition: " << stopCondition->name << std::endl;
}
switch (SCScheduler::GetProgress())
{
case kSCMaximumProgress:
pStream << "Simulation progress: maximum" << std::endl << std::endl;
break;
case kSCFairProgress:
pStream << "Simulation progress: fair" << std::endl << std::endl;
break;
default:
assert(false);
}
}
else // called for end message!
{
if (type == scIndetSimulation)
{
pStream << std::endl << "Simulator ";
}
SCIndet::Display(pStream);
pStream << "Reached model time: " << SCScheduler::GetCurrentTime();
pStream << std::endl << std::endl;
}
return pStream;
}