Alex Bikfalvi
SimStream Documentation
Sim.cpp
00001 #include "Headers.h" 00002 #include "Sim.h" 00003 #include "Console.h" 00004 00005 CSim::CSim(char* name, CSimModel* model) 00006 { 00007 assert(name); 00008 assert(model); 00009 this->name = name; 00010 this->model = model; 00011 00012 // Set the handler to the model 00013 this->model->Init(this); 00014 00015 // Create the event list 00016 this->events = new CSimEventList(this->model); 00017 00018 // Create the calls list 00019 this->calls = new CSimCalls(); 00020 } 00021 00022 CSim::~CSim() 00023 { 00024 delete this->events; 00025 delete this->calls; 00026 } 00027 00028 __uint32 CSim::Run(time_t& duration) 00029 { 00030 this->time = 0; 00031 00032 __uint32 oldPercent = 1; 00033 __uint32 newPercent; 00034 __time div = 100000; 00035 time_t startTime; 00036 time_t currentTime; 00037 time_t endTime; 00038 00039 ::time(&startTime); 00040 00041 printf("\n%s : Start simulation (max time = %lf)\n", this->name, this->model->MaxTime()); 00042 00043 while((this->events->HasEvents()) && (time <= this->model->MaxTime())) 00044 { 00045 newPercent = (__uint32)(100*time/this->model->MaxTime()); 00046 if(newPercent != oldPercent) 00047 { 00048 ::time(¤tTime); 00049 00050 endTime = startTime + (time_t)(time?((currentTime-startTime)*this->model->MaxTime()/time):(currentTime-startTime)); 00051 00052 #if !defined(_RELEASEASSERT) && !defined(_RELEASELOG) && !defined(_DEBUG) && !defined(LOG) 00053 printf("%s : %u percent complete (time is %.3lf of %.lf, %ld seconds passed, %ld seconds remaining)\t\t", this->name, newPercent, time, this->model->MaxTime(), currentTime-startTime, endTime - currentTime); 00054 CConsole::MoveCursorBeginLine(); 00055 printf("\n"); 00056 #endif 00057 oldPercent = newPercent; 00058 } 00059 00060 // Execute current event 00061 CSimEvent* evt = this->events->GetEvent(time); 00062 assert(evt); 00063 00064 this->model->Run(evt); 00065 00066 delete evt; 00067 00068 // Execute current calls 00069 this->calls->Execute(); 00070 } 00071 00072 // Finalize model 00073 this->model->Finalize(); 00074 00075 // Clean all events 00076 this->events->Clean(); 00077 00078 // Clean all calls 00079 this->calls->Clean(); 00080 00081 ::time(&endTime); 00082 00083 duration = endTime - startTime; 00084 00085 return this->events->Size(); 00086 } 00087 00088 __time CSim::Time() 00089 { 00090 return this->time; 00091 } 00092 00093 void CSim::ScheduleEventNow(CSimEvent* evt) 00094 { 00095 assert(evt); 00096 this->events->Add(this->time, evt); 00097 } 00098 00099 void CSim::ScheduleEventAt(__time time, CSimEvent* evt) 00100 { 00101 assert(evt); 00102 assert(time >= this->time); 00103 this->events->Add(time, evt); 00104 } 00105 00106 void CSim::ScheduleEventAfter(__time delay, CSimEvent* evt) 00107 { 00108 assert(evt); 00109 assert(this->time + delay >= this->time); 00110 this->events->Add(this->time + delay, evt); 00111 } 00112 00113 void CSim::CancelEvent(__time time, CSimEvent* evt) 00114 { 00115 assert(evt); 00116 if(time < this->time) return; // If event is in the past, do nothing 00117 00118 this->events->Cancel(time, evt); 00119 delete evt; 00120 } 00121 00122 void CSim::operator()(ICall* call) 00123 { 00124 assert(call); 00125 // Add call to the calls list 00126 this->calls->Add(call); 00127 }
Last updated: February 8, 2011