/** * clear_orders: * * Removes all orders from the order queue. * */ static void clear_orders(void) { for (int button = 0; button < N_BUTTONS; button++) { for (int floor = 0; floor < N_FLOORS; floor++) { remove_order(button, floor); } } }
void Batch::Behavior(void) { std::vector<Order*>::iterator begin; std::vector<Order*>::iterator end; DEBUG("NEW BATCH:\t\t"); assert(chef_fac.Busy()); assert(chef_fac.in == this); /* facitily is allready seized by this process */ //Seize(chef_fac); /* no need for high priority from now */ Priority = DEFAULT_PRIORITY; #ifndef NDEBUG_PRINT //prints IDs of order processes in this batch std::cout << "\tIDs: "; for(std::vector<Order*>::iterator it = orders.begin(); it != orders.end(); ++it) { if ((*it)->get_batch() == nullptr) std::cout << (*it)->get_id() << "(D), "; else std::cout << (*it)->get_id() << ", "; } std::cout << std::endl; #endif //NDEBUG_PRINT //////////////////////////////////////////////////////////// //preparation //////////////////////////////////////////////////////////// DEBUG("PREPARATION\t\t"); /* * loop through all orders in batch to simulate * pizza preparation * duration: 2-3 minutes for each pizza */ begin = orders.begin(); end = orders.end(); for(std::vector<Order*>::iterator it = begin; it != end; ++it) { //for every pizza in batch if ((*it)->get_batch() == nullptr) { /* order no longer in batch because of timer * expiration (customer canceled order) */ } else { /* order not canceled yet * going to wait (prepare pizza) * 4 to 5 minutes for every pizza */ Wait(Uniform(PREP_TIME_FROM, PREP_TIME_TO)); if ((*it)->get_batch() == nullptr) { /* order canceled durign waiting (preparation )*/ } else { /* order was not canceled duritg preparation */ } } } /* * release chef facility by batch * facility is now ready to be seized by first order * in its queue to create new batch */ Release(chef_fac); DEBUG("PREPARATION DONE\t"); /* * loop through all orders in batch to simulate * pizza preparation error rate */ begin = orders.begin(); end = orders.end(); for(std::vector<Order*>::iterator it = begin; it != end; ++it) { //for every pizza in batch if ((*it)->get_batch() != nullptr) { double rnd = Uniform(0.0, 100.0); // 0 - 100 % if (rnd <= PREP_ERR_RATE) { DEBUG("PREPARATION ERROR\t"); /* there was an error, pizza have to be removed * from batch, order will be canceled * (no reparation for now) */ assert((*it)->get_batch() == this); remove_order(*it); } } } #ifndef NDEBUG begin = orders.begin(); end = orders.end(); for(std::vector<Order*>::iterator it = begin; it != end; ++it) assert((*it)->get_batch() == this || (*it)->get_batch() == nullptr); #endif //NDEBUG //////////////////////////////////////////////////////////// //baking //////////////////////////////////////////////////////////// /* if batch is not empty, simulate pizza baking * duration: exactly 4 minutes for batch */ if (!is_empty()) { Seize(oven_fac); DEBUG("BAKING\t\t\t"); Wait(BAKING_TIME); Release(oven_fac); DEBUG("BAKING DONE\t\t"); /* simulate error in baking */ double rnd = Uniform(0.0, 100.0); // 0 - 100 % if (rnd <= BAKING_ERR_RATE) { DEBUG("BAKING ERROR\t\t"); /* there was an error in baking, batch and all its * orders will be canceled * (no reparation for now) */ begin = orders.begin(); end = orders.end(); for(std::vector<Order*>::iterator it = begin; it != end; ++it) { //for every pizza in batch if ((*it)->get_batch() != nullptr) { assert((*it)->get_batch() == this); remove_order(*it); } } assert(this->is_empty()); } } //////////////////////////////////////////////////////////// //delivery //////////////////////////////////////////////////////////// /* if batch is not empty, simulate pizza delivery * duration: exactly 10 - 15 minutes for each pizza */ if (!is_empty()) { Enter(delivery_store); DEBUG("DELIVERY\t\t"); begin = orders.begin(); end = orders.end(); for(std::vector<Order*>::iterator it = begin; it != end; ++it) { //for every pizza in batch if ((*it)->get_batch() == nullptr) { /* order no longer in batch because of timer * expiration (customer canceled order) */ } else { /* order not canceled yet */ Wait(Uniform(DELIV_TIME_FROM, DELIV_TIME_TO)); if ((*it)->get_batch() == nullptr) { /* order canceled durign delivery */ } else { /* order was not canceled during delivery */ } } } Leave(delivery_store); DEBUG("DELIVERY DONE\t\t"); } //////////////////////////////////////////////////////////// //finishing process //////////////////////////////////////////////////////////// /* * loop through all orders in batch to * create statistics * (they are all passivated) */ DEBUG("FINISHED BATCH:\t\t"); //std::cout << "\tIDs: "; begin = orders.begin(); end = orders.end(); for(std::vector<Order*>::iterator it = begin; it != end; ++it) { //for every pizza in batch if ((*it)->get_batch() == nullptr) { //deleted //std::cout << (*it)->get_id() << "(D), "; /* * timeout is allready canceled */ } else { //finished //std::cout << (*it)->get_id() << ", "; /* cancel timeout, process succesfully finished */ if ((*it)->t != nullptr) (*it)->t->Cancel(); (*it)->t = nullptr; } } //std::cout << std::endl; }