-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCAutomaton.cpp
1367 lines (1091 loc) · 40.8 KB
/
SCAutomaton.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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-----------------------------------------------------------------------------+
| |
| SCL - Simulation Class Library |
| |
| (c) 1994-98 Marc Diefenbruch, Wolfgang Textor |
| University of Essen, Germany |
| |
+---------------+-------------------+---------------+-------------------+------+
| Module | File | Created | Project | |
+---------------+-------------------+---------------+-------------------+------+
| SCAutomaton | SCAutomaton.cc | 5. Jul 1994 | SCL | |
+---------------+-------------------+---------------+-------------------+------+
| |
| Change Log |
| |
| Nr. Date Description |
| ----- -------- ------------------------------------------------------ |
| 001 |
| 000 05.07.94 Neu angelegt |
| |
+-----------------------------------------------------------------------------*/
/* Lineal
00000000001111111111222222222233333333334444444444555555555566666666667777777777
01234567890123456789012345678901234567890123456789012345678901234567890123456789
*/
#include <stdarg.h>
#include <string.h>
#include "SCStream.h"
#include "SCAutomaton.h"
#include "SCDataType.h"
#include "SCIndet.h"
#include "SCMachine.h"
#include "SCSignal.h"
#include "SCProcessType.h"
#include "SCRequestType.h"
#include "SCScheduler.h"
#include "SCEnabledTransition.h"
#include "SCStateType.h"
#include "SCSignalType.h"
#include "SCTimerControl.h"
#include "SCTimerType.h"
#include "SCTimer.h"
#include "SCTransition.h"
#include "SCDebug.h"
#include "SCPath.h"
#include "SCMem.h"
#include "SCProcedure.h"
#include "SCProcess.h"
#include "SCTraceControl.h"
#if _SC_DMALLOC
#include <dmalloc.h>
#endif
#if _SC_NOINLINES
#include "SCAutomaton.inl.h"
#endif
#if _SC_PROFILING
extern int timer_ineff;
extern int remove_timer_ineff;
extern int trans_ineff;
#endif
SCAutomatonTable *SCAutomaton::automatonTable = NULL;
/*----- Construktor -----*/
SCAutomaton::SCAutomaton(SCStateType *startState,
SCDataType *processParameters,
const SCObjectType pObjectType,
const SCBoolean varSize,
const SCObject* pParent) :
SCRunnable (SCScheduler::NewProcessID(), // runnableID
false, // sleeping
pObjectType, // object type
pParent), // parent
parameters(processParameters),
lastState (startState),
recalculateAwakeDelay(true),
lastInputData(NULL),
maxEC(0),
enablingConditions(NULL),
isInState(true),
enabledTransitionList(NULL),
variableSize(varSize)
{
creationTime = Now();
stateTime = Now();
nextWakeupTime = Now();
// Eintragung in die Automaten-Lookup-Tabelle erfolgt im generierten Code,
// damit das Objekt erst fertig konstruiert wird, bevor es
// zugreifbar wird!
}
// Der folgende Konstruktor legt einen "leeren" Prozess an.
// Dies dient als Vorbereitung fuer ein folgendes Restore
SCAutomaton::SCAutomaton(const SCObjectType pObjectType,
const SCBoolean varSize,
const SCObject *pParent) :
SCRunnable(pObjectType, pParent), // Invoke constructor of
// base class
variableSize(varSize)
{
lastInputData = NULL;
enablingConditions = NULL;
parameters = NULL;
enabledTransitionList = NULL;
// Eintragung in die Automaten-Lookup-Tabelle erfolgt im generierten Code,
// damit das Objekt erst fertig konstruiert wird, bevor es
// zugreifbar wird!
}
SCAutomaton::~SCAutomaton (void)
{
if (lastInputData)
delete lastInputData;
if (parameters)
delete parameters;
if (enablingConditions)
delete[] enablingConditions;
if (enabledTransitionList)
delete enabledTransitionList;
}
// Die folgende Methode wird in SCProcedure redefiniert:
SCProcess *SCAutomaton::GetOwner (void) const
{
return (GetType() == SC_PROCESS) ? (SCProcess *)this : (SCProcess *)NULL;
}
/*----- Member-Funktionen -----*/
void SCAutomaton::SetEnablingConditions(SCStateType *theState,
SCNatural numEC,
...) // executed by process
{
va_list args;
SCNatural z_i;
SCNatural index;
if (enablingConditions)
{
assert(maxEC > 0);
delete[] enablingConditions;
#if _SC_VALIDATION_OPTIMIZE
currentHistorySize -= (sizeof(SCBoolean) * maxEC);
#endif
}
if (numEC)
{
maxEC = theState->GetMaxTransitionID();
enablingConditions = new SCBoolean[maxEC];
assert(enablingConditions);
for (z_i = 0; z_i < maxEC; z_i++)
enablingConditions[z_i] = true;
va_start (args, numEC);
for (z_i = 0; z_i < numEC; z_i++)
{
index = va_arg (args, int) - kSCTransitionIDBase;
enablingConditions[index] = va_arg (args, int);
}
va_end (args);
#if _SC_VALIDATION_OPTIMIZE
currentHistorySize += (sizeof(SCBoolean) * maxEC);
#endif
}
else
{
enablingConditions = NULL;
maxEC = 0;
}
}
SCTransitionID SCAutomaton::State (SCStateType * theState, // executed by process
const SCDuration awakeDelay,
SCSignalType ** signalRead)
{
SCTransitionID tid;
SCNatural scheduleHow;
assert (theState != NULL);
assert(GetOwner());
#if _SC_DEBUGOUTPUT
scDebugLog << Now() << ": " << *this << " entering " << *theState;
if (awakeDelay != kSCNoAwakeDelay)
scDebugLog << ", awake delay: " << awakeDelay << std::endl;
else
scDebugLog << std::endl;
#endif
#if _SC_VALIDATION_DEBUG
scValidationDebugLog << "SCAutomaton::State(): " << *this;
scValidationDebugLog << " entering " << *theState;
scValidationDebugLog << ", callScheduler: " << (GetOwner()->GetCallScheduler() ? "yes" : "no") << std::endl;
#endif
/////////////////////////////////////////////////
// 1. Initialization of variables, tracing, etc.
/////////////////////////////////////////////////
isInState = true;
stateTime = Now();
theState->SetReached(); // mark state as reached
// the senderID will get a new value when the next
// transition is executed. it is not used till
// then so we reset senderID here to a neutral value
// to be nice to the validator:
if (!theState->IsIntermediate()) // intermediate states
{ // should not change senderID !
GetOwner()->SetSender(kSCNoProcessID);
if (IsTraceOn())
{
SCTraceControl::LogEvent(scTraceStateChange, GetOwner(), theState, awakeDelay);
}
}
if (lastInputData) // destroy old signal parameters
{
delete lastInputData;
lastInputData = NULL;
}
if (theState != lastState) // Check for state change.
{
lastState = theState; // Mark state change.
recalculateAwakeDelay = true; // if state changed we must
// recalculate the awake time
// (if delay given)
}
////////////////////////////////////////////////////////////////////////
// 2. Calculate next wakeup time and the enabled transitions of this
// process. The member nextWakeupTime is only used in the
// EnabledTransitions() method. It is first initialized in the
// constructor of SCAutomaton.
// The variable scheduleHow discriminates three cases:
// 1. Process is passive and waiting for an input (kSCScheduleWaiting)
// 2. Process is active because a consumable signal is in the queue
// (kSCScheduleNow)
// 3. Process is active because the current state has spontaneous
// transitions (kSCScheduleTime)
////////////////////////////////////////////////////////////////////////
scheduleHow = kSCScheduleWaiting; // this is the fallback value
// used if nothing else (signal
// consume or spontaneous
// transition) can happen
// Method EnabledTransitions() uses nextWakeupTime so
// calculate it before calling EnabledTransitions() below:
if (theState->HasSpontaneous() &&
awakeDelay != kSCNoAwakeDelay)
{
scheduleHow = kSCScheduleTime;
if (recalculateAwakeDelay)
{
nextWakeupTime = Now() + awakeDelay; // awake time is now valid, so
recalculateAwakeDelay = false; // don't recalculate it until
// state changed
}
}
// Maybe the Receive()-method has already build the
// enabledTransitionList, so we only do it here
// if enabledTransitionList == NULL.
if (!enabledTransitionList &&
GetOwner()->GetCallScheduler())
{
enabledTransitionList = EnabledTransitions();
}
// if the process has enabled transitions we can activate
// it now:
if (enabledTransitionList)
scheduleHow = kSCScheduleNow;
/////////////////////////////////////////////////////////
// 3. Suspending to give scheduler the control to decide
// about the next active runnable. By the call of
// Schedule() (see below) this process is
// could be reinserted in the activeQueue,
// depending on the value of scheduleHow.
/////////////////////////////////////////////////////////
if (GetOwner()->GetCallScheduler()) // callScheduler is false if
{ // backtracking from Request state
// or if State() is called
// first time after DYNAMIC
// creation of the process
#if _SC_VALIDATION_DEBUG
scValidationDebugLog << "SCAutomaton::State(): " << *this;
scValidationDebugLog << " suspending in " << *theState << std::endl;
#endif
Schedule (scheduleHow, nextWakeupTime); // only if scheduleHow = kSCScheduleTime
// the nextWakeupTime
// is really used!
Suspend(); // May lead to backtracking!
#if _SC_VALIDATION_DEBUG
scValidationDebugLog << "SCAutomaton::State(): " << *this;
scValidationDebugLog << " resuming in " << *theState << std::endl;
#endif
}
else
{
GetOwner()->SetCallScheduler (true);
}
assert(GetOwner()->GetCallScheduler() == true);
/////////////////////////////////////////////////////////////////////
// 4. Testing if a backtracking took place. Three possible
// cases:
// a) No backtracking => isInState = true
// b) Backtracking to request-intermediate-state => isInState = false
// c) Backtracking to another process state => isInState = true
//
// In case of b) we must leave this method and bring the control
// flow to the correct point in the generated code.
// In case of a) or c) we can proceed, because the control
// flow is already at the correct point.
// NOTE: In case of backtracking the data of the processes
// is already restored, we need only a correction of the
// control flow!
/////////////////////////////////////////////////////////////////////
if (!isInState) // backtracking to Request state?
{ // this IS possible!!
#if _SC_VALIDATION_DEBUG
scValidationDebugLog << "SCAutomaton::State(): " << *this;
scValidationDebugLog << " leaving " << *theState;
scValidationDebugLog << " and go to request/call state" << std::endl;
#endif
return (kSCNoTransition); // bring control flow to the right
// position in the generated code!
}
////////////////////////////////////
// 5. Choose Transition to execute:
////////////////////////////////////
tid = Transition (signalRead); // sets isInState to false
// it's not sufficient to set
// isInState to false HERE,
// since after backtracking only
// Transition() is called!
#if _SC_DEBUGOUTPUT
scDebugLog << Now() << ": " << *this << " leaving " << *theState << std::endl;
#endif
return (tid); // return ID of transition
}
SCTransitionID SCAutomaton::Transition (SCSignalType **signalRead)
{
SCTransition * transition;
SCSignal * sig;
SCTransitionID tid;
SCEnabledTransition * enabTrans;
assert(SCScheduler::GetIndet());
enabTrans = SCScheduler::GetIndet()->ChooseTransition ();
// choose a transition for
// execution
if (enabTrans == NULL)
{
#if _SC_DEBUGOUTPUT
scDebugLog << Now() << ": " << *this;
scDebugLog << " executing idle transition" << std::endl;
#endif
*signalRead = NULL; // don't confuse generated code
isInState = false;
recalculateAwakeDelay = true; // new awake delay must be set
return kSCNoTransition;
}
assert (enabTrans != NULL);
transition = enabTrans->GetTransition();
sig = enabTrans->GetSignal();
delete enabTrans;
if (transition == NULL) // implicit signal consumption ?
{
assert (sig);
#if _SC_DEBUGOUTPUT
scDebugLog << Now() << ": " << *this;
scDebugLog << " dropped " << *sig << std::endl;
#endif
GetOwner()->GetInputQueue()->Remove (sig);
if (IsTraceOn())
{
SCTraceControl::LogEvent (scTraceSignalDrop, GetOwner(), sig);
}
SCScheduler::GetIndet()->LogError (scErrorSignalDrop, GetOwner(), sig);
*signalRead = NULL; // don't confuse generated code
isInState = false;
delete sig;
return kSCNoTransition;
}
transition->SetExecuted(); // mark transition as executed
tid = transition->GetID();
#if _SC_DEBUGOUTPUT
scDebugLog << Now() << ": " << *this;
if (sig)
{
scDebugLog << " consuming " << *sig << std::endl;
}
else if (transition->GetPriority() == kSCPrioInputNone)
{
scDebugLog << " got signal none" << std::endl;
}
else
{
scDebugLog << " got continuous signal" << std::endl;
}
scDebugLog << Now() << ": " << *this;
scDebugLog << " executing " << *transition << std::endl;
#endif
if (sig) // signal consuming input ?
{
GetOwner()->GetInputQueue()->Remove (sig); // remove signal from queue
if (IsTraceOn())
{
SCTraceControl::LogEvent (scTraceSignalConsume, GetOwner(), sig, transition);
}
*signalRead = (SCSignalType *)sig->GetSignalType();
assert (*signalRead);
GetOwner()->SetSender (sig->GetSenderID());
lastInputData = sig->RetrieveData();
delete sig; // delete signal object
}
else // input none or continuous signal!
{
*signalRead = NULL;
if (!lastState->IsIntermediate()) // intermediate states should
{ // NOT change the sender variable!
GetOwner()->SetSender (GetOwner()->Self());
}
if (IsTraceOn())
{
if (transition->GetPriority() == kSCPrioInputNone) // input none?
SCTraceControl::LogEvent (scTraceSpontTrans, GetOwner(), transition);
else // continuous signal!
SCTraceControl::LogEvent (scTraceContSignal, GetOwner(), transition);
}
recalculateAwakeDelay = true; // new awake delay must be set
}
isInState = false;
return (tid); // return ID of transition
}
SCEnabledTransitionList *SCAutomaton::EnabledTransitions (const SCBoolean useInputTail) const
{
SCTransitionList * transitions;
SCEnabledTransitionList * enabTransList = new SCEnabledTransitionList;
SCBoolean useNones;
assert (lastState != NULL);
useNones = lastState->HasSpontaneous() &&
Now() >= nextWakeupTime;
//////////////////////////////////////////
// 1. get transitions of priority inputs:
//////////////////////////////////////////
transitions = lastState->GetPriorityInputs();
if (!transitions->IsEmpty())
SignalEnabledTransitions (enabTransList, true, useInputTail);
if (enabTransList->IsEmpty()) // no priority inputs possible?
{
////////////////////////////////////////
// 2. get transitions of normal inputs:
////////////////////////////////////////
transitions = lastState->GetNormalInputs();
if (!transitions->IsEmpty())
SignalEnabledTransitions (enabTransList, false, useInputTail);
}
////////////////////////////////////////
// 3. add spontaneous transitions:
// (priority inputs does NOT disable
// spontaneous transitions!)
////////////////////////////////////////
if (useNones)
{
transitions = lastState->GetInputNones();
if (!transitions->IsEmpty())
SpontaneousEnabledTransitions (transitions, enabTransList);
////////////////////////////////////////
// 4. use continuous signal transitions:
// (these are only enabled if no
// signal consuming transitions active!)
////////////////////////////////////////
if (enabTransList->IsEmpty())
{
transitions = lastState->GetContSignals();
if (!transitions->IsEmpty())
SpontaneousEnabledTransitions (transitions, enabTransList);
}
}
if (enabTransList->IsEmpty())
{
delete enabTransList;
enabTransList = NULL;
}
return (enabTransList);
}
//
// Output an Prozess-ID:
//
void SCAutomaton::Output (const SCProcessID receiverID, // executed by process
const SCSignalType *const signalType,
SCDataType * data,
const SCDuration delay)
{
SCProcess * receiver;
SCSignal * signal;
receiver = (SCProcess*) SCScheduler::GetRunnableFromID (receiverID);
if (!receiver) // kein Prozess zur angegebenen ID ? (ist bei
{ // gestoppten Process der Fall)
#if _SC_DEBUGOUTPUT
scDebugLog << std::endl << Now() << ": " << *this << " cannot send signal <";
scDebugLog << signalType->GetName() << "> since process ID is invalid";
scDebugLog << " (#" << receiverID << ")" << std::endl << std::endl;
#endif
if (IsTraceOn())
SCTraceControl::LogEvent (scTraceSignalNotSent, GetOwner(), signalType);
SCScheduler::GetIndet()->LogError (scErrorNoSignalReceiver, GetOwner(), signalType);
return;
}
if (!delay)
{
Output (receiver, signalType, data);
}
else
{
signal = new SCSignal (GetOwner()->Self(),
GetOwner()->GetType(),
signalType, data, 0);
assert (signal);
#if _SC_DEBUGOUTPUT
scDebugLog << Now() << ": " << *this << " sending ";
scDebugLog << *signal << " to " << *receiver;
scDebugLog << ", delay: " << delay << std::endl;
#endif
SCPath::GetPath()->SpawnDelayed (receiver,
signal,
Now() + delay);
if (IsTraceOn())
SCTraceControl::LogEvent (scTraceSignalSend, GetOwner(), receiver, signal, delay);
}
}
//
// Output an Prozesstyp:
//
void SCAutomaton::Output (SCProcessType * receiverType, // executed by process
const SCSignalType *const signalType,
SCDataType * data,
const SCDuration delay)
{
SCProcess * receiver = NULL;
SCSignal * signal;
receiver = receiverType->GetAProcess();
if (!receiver) // kein Prozess zum angegebenen Prozesstyp ?
{
#if _SC_DEBUGOUTPUT
scDebugLog << std::endl << Now() << ": " << *this << " cannot send signal <";
scDebugLog << signalType->GetName() << "> since no living instance of ";
scDebugLog << *receiverType << std::endl << std::endl;
#endif
if (IsTraceOn())
SCTraceControl::LogEvent (scTraceSignalNotSent, GetOwner(), signalType);
SCScheduler::GetIndet()->LogError (scErrorNoSignalReceiver, GetOwner(), signalType);
return;
}
if (!delay)
{
Output (receiver, signalType, data); // Output an Instanz
}
else
{
signal = new SCSignal (GetOwner()->Self(), // sender ID
GetOwner()->GetType(), // sender type
signalType, // signal type
data, // signal data
0); // timer ID
#if _SC_DEBUGOUTPUT
scDebugLog << Now() << ": " << *this << " sending ";
scDebugLog << *signal << " to " << *receiver;
scDebugLog << ", delay: " << delay << std::endl;
#endif
SCPath::GetPath()->SpawnDelayed (receiverType,
signal,
Now() + delay);
if (IsTraceOn())
SCTraceControl::LogEvent (scTraceSignalSend, GetOwner(), receiver, signal, delay);
}
}
//
// Output an Prozess-Instanz:
//
void SCAutomaton::Output (SCProcess * receiver, // executed by process
const SCSignalType *const signalType,
SCDataType * data)
{
SCSignal * signal;
assert(receiver);
signal = new SCSignal (GetOwner()->Self(), // sender ID
GetOwner()->GetType(), // sender type
signalType, // signal type
data, // signal data
0); // timer ID
assert(signal);
#if _SC_DEBUGOUTPUT
scDebugLog << Now() << ": " << *this << " sending ";
scDebugLog << *signal << " to " << *receiver << std::endl;
#endif
receiver->Receive (signal);
if (IsTraceOn())
{
SCTraceControl::LogEvent (scTraceSignalSend, GetOwner(), receiver, signal);
}
}
void SCAutomaton::Request (SCMachine *machine, // executed by process
const SCRequestType *const requestType,
const SCDuration serviceAmount,
const SCNatural priority)
{
assert (machine != NULL);
assert (!isInState);
assert (serviceAmount >= 0);
#if _SC_DEBUGOUTPUT
scDebugLog << Now() << ": " << *this << " requesting <";
scDebugLog << requestType->GetName() << "> from ";
scDebugLog << *machine << ", amount: ";
scDebugLog << serviceAmount << ", priority: " << priority << std::endl;
#endif
machine->NewRequest (this, requestType, serviceAmount, priority);
#if _SC_VALIDATION_DEBUG
scValidationDebugLog << "SCAutomaton::Request(): " << *this;
scValidationDebugLog << " suspending in request method" << std::endl;
#endif
Schedule (kSCScheduleBlocked); // Block until request is completed.
Suspend();
#if _SC_VALIDATION_DEBUG
scValidationDebugLog << "SCAutomaton::Request(): " << *this;
scValidationDebugLog << " resuming in request method" << std::endl;
if (isInState) // backtracking to normal state?
{ // this IS possible!!
scValidationDebugLog << "SCAutomaton::Request(): " << *this;
scValidationDebugLog << " leaving request state and go to ";
scValidationDebugLog << *lastState << std::endl;
}
#endif
assert(GetOwner()->GetCallScheduler() == true);
}
void SCAutomaton::Create (SCProcessType * ptype,
SCDataType * actualParams) // executed by process
{
assert(ptype);
GetOwner()->SetOffspring (SCScheduler::Create (ptype,
GetOwner(),
actualParams));
if (GetOwner()->Offspring() != kSCNoProcessID)
SCScheduler::GetIndet()->Create (GetOwner()->Offspring()); // store IDs of created instance
}
void SCAutomaton::Call (SCProcedure *newProcedure)
{
assert(newProcedure);
assert(!isInState);
#if _SC_DEBUGOUTPUT
scDebugLog << Now() << ": " << *this << " calls ";
scDebugLog << *newProcedure << std::endl;
#endif
SCScheduler::Call (newProcedure, this);
SCScheduler::GetIndet()->Create (newProcedure->GetID());
#if _SC_VALIDATION_DEBUG
scValidationDebugLog << "SCAutomaton::Call(): " << *this;
scValidationDebugLog << " suspending in call method" << std::endl;
#endif
Schedule (kSCScheduleBlocked); // Block until procedure returns.
Suspend();
#if _SC_VALIDATION_DEBUG
scValidationDebugLog << "SCAutomaton::Call(): " << *this;
scValidationDebugLog << " resuming in call method" << std::endl;
if (isInState) // backtracking to normal state?
{ // this IS possible!!
scValidationDebugLog << "SCAutomaton::Call(): " << *this;
scValidationDebugLog << " leaving call state and go to ";
scValidationDebugLog << *lastState << std::endl;
}
#endif
}
void SCAutomaton::SetTimer (const SCTime when, // executed by process
const SCTimerType *const timerType,
SCDataType * data)
{
SCTimer * timer;
SCTimerCons * elem;
SCTimerSaveList * timerQueue = GetOwner()->GetTimerQueue();
ResetTimer(timerType, data, false); // SET implies RESET!
timer = new SCTimer (GetOwner(),
when < Now() ? Now() : when,
timerType,
data,
timerQueue);
assert (timer);
#if _SC_DEBUGOUTPUT
scDebugLog << Now() << ": " << *GetOwner();
scDebugLog << " setting " << *timer << std::endl;
#endif
for (elem = timerQueue->Head();
elem != NULL;
elem = elem->Next())
{
if ((*elem)()->GetTimeout() >= when)
{
timerQueue->InsertBefore (timer, elem);
break;
}
}
if (!elem) // neues Maximum?
{
timerQueue->InsertAfter (timer);
}
if (IsTraceOn())
SCTraceControl::LogEvent (scTraceTimerSet, GetOwner(), timer);
SCTimerControl::GetTimerControl()->Reschedule();
}
void SCAutomaton::ResetTimer (const SCTimerType *const timerType, // executed by process
SCDataType * data,
SCBoolean rescheduleTimerControl)
{
SCTimerCons * removeThis;
SCTimerSaveList * timerQueue = GetOwner()->GetTimerQueue();
//
// test if timer is in timer queue then
// timer has not fired yet and we need no
// call to RemoveInputTimer():
//
removeThis = GetOwner()->LocateTimer (timerType, data);
if (removeThis != NULL)
{
#if _SC_DEBUGOUTPUT
scDebugLog << Now() << ": " << *GetOwner();
scDebugLog << " resetting " << *(*removeThis)() << std::endl;
#endif
if (IsTraceOn())
SCTraceControl::LogEvent (scTraceTimerReset, GetOwner(), (*removeThis)());
delete timerQueue->Remove (removeThis);
}
else
{
RemoveInputTimer (timerType, data);
}
if (rescheduleTimerControl) // Reset not invoked by new Set ?
SCTimerControl::GetTimerControl()->Reschedule();
}
SCNatural SCAutomaton::Decision (const SCNatural numOfArgs, ...) const // executed by process
{
va_list args;
SCNatural numOfTrues = 0;
SCNatural z_i;
SCNatural lastTrue = 0;
SCBoolean *argVec;
SCNatural selection;
#if _SC_DEBUGOUTPUT
scDebugLog << Now() << ": " << *this;
scDebugLog << " got decision with " << numOfArgs << " cases: ";
#endif
argVec = new SCBoolean[numOfArgs];
assert(argVec);
va_start (args, numOfArgs);
for (z_i = 0; z_i < numOfArgs; z_i++)
{
argVec[z_i] = va_arg (args, int);
if (argVec[z_i])
{
numOfTrues++;
lastTrue = z_i;
#if _SC_DEBUGOUTPUT
scDebugLog << "T ";
#endif
}
else
{
#if _SC_DEBUGOUTPUT
scDebugLog << "F ";
#endif
}
}
va_end (args);
selection = SCScheduler::GetIndet()->ChooseOneTrue (numOfArgs, numOfTrues, lastTrue, argVec);
#if _SC_DEBUGOUTPUT
scDebugLog << std::endl;
scDebugLog << Now() << ": " << *this;
scDebugLog << " choose number " << selection << ".\n";
#endif
delete[] argVec;
return (selection);
}
void SCAutomaton::Assert (const SCBoolean theAssertion) const
{
if (!theAssertion) // assertion failed ?
{
SCScheduler::GetIndet()->LogError (scErrorAssertionFailed);
}
}
/* Private Memberfunktionen */
SCNatural SCAutomaton::SignalEnabledTransitions (SCEnabledTransitionList *enabTransList,
const SCBoolean priorityInputs,
const SCBoolean useInputTail) const
{
SCTransitionList *transitions;
SCTransitionListTable *transitionsTable;
SCSignalSaveList * inQueue = GetOwner()->GetInputQueue();
SCTransition * t;
SCSignalCons * curMsgCons;
SCSignal * curMsg;
SCEnabledTransition * newEnabTrans;
const SCSignalID * saveSet;
SCSignalID signalID;
SCNatural saveSetSize;
SCNatural numPossibleTrans;
SCBoolean saved;
SCBoolean saveAll;
SCBoolean condition;
if (inQueue->IsEmpty()) // input queue is empty ?
{
return (0);
}
if (priorityInputs)
{
saveSetSize = 0;
saveSet = NULL;
saveAll = true;
transitionsTable = lastState->GetPriorityInputsTable();
assert (transitionsTable);
}
else
{
saveSetSize = lastState->GetSaveSetSize();