#include "adevs.h" #include #include #include #include using namespace std; using namespace adevs; /* A model of a person. The lifecycle stages are born -> midlife -> end of life -> death. */ class Person: public Model { public: Person(string name):m_name(name),stage(0){} Time init(SimEnv* env) { cout << name() << " is born @ " << env->now().real << "," << env->now().logical << endl; /* The person will be midlife in 20 to 60 years. The time advance must be added to now() and not the other way round. Recall that + does not commute. */ return env->now()+Time(20+rand()%40,0); } /* This is called twice. Once when the person reaches midlife and again just prior to death. */ Time update(SimEnv* env) { /* Advance the life stage */ stage++; cout << name() << " is " << ((stage==1) ? "midlife" : "end of life") << " @ " << env->now().real << "," << env->now().logical << endl; /* Advance to end of life in 0 to 40 years plus some number of logical time increments. Notice that this will always move us into the future. */ if (stage == 1) return env->now()+Time(rand()%41,rand()%12+1); /* We have reached the end. Return an infinite time of next event and remove the model. */ env->remove(this); return adevs_inf