void FleetRepReactor::onAttributeIs() {
	
	// Create scheduleChanges activity
	if(scheduleReactor_)
		activityManagerInstance()->activityDel("schedule");
	Activity::Ptr scheduleChanges = activityManagerInstance()->activityNew("schedule");
	scheduleReactor_ = new ScheduleChangesReactor(scheduleChanges.ptr(), fleet_, scheduledAttrs_);
	scheduleChanges->nextTimeIs(currentTimeRoundedDownTo24() + scheduledAttrs_.startTime.value());
	scheduleChanges->lastNotifieeIs(scheduleReactor_.ptr());
	scheduleChanges->statusIs(Activity::queued);
	
	// Create unscheduleChanges activity
	if(unscheduleReactor_)
		activityManagerInstance()->activityDel("unschedule");
	Activity::Ptr unscheduleChanges = activityManagerInstance()->activityNew("unschedule");
	unscheduleReactor_ = new UnscheduleChangesReactor(unscheduleChanges.ptr(), fleet_, scheduledAttrs_);
	unscheduleChanges->nextTimeIs(currentTimeRoundedDownTo24() + scheduledAttrs_.endTime.value());
	unscheduleChanges->lastNotifieeIs(unscheduleReactor_.ptr());
	unscheduleChanges->statusIs(Activity::queued);

}
Esempio n. 2
0
void Segment::readyForShipmentIs(bool b) {
	if (b == false || shipmentQ_.empty()) return;
	
	Activity::Manager::Ptr manager = activityManagerInstance();
	string name = "forward shipment #";
	ostringstream oss;
	oss << rand() << rand() << rand();
	name.append(oss.str());
	Activity::Ptr fwd = manager->activityNew(name);

	ShipmentQueue shipments;
	NumVehicles currentVehicles(capacity());
    Fleet::Ptr fleet = engine_->fleet();
	NumPackages vehicleCapacity(fleet->capacity(mode()).value());
	NumPackages totalCapacity(
		currentVehicles.value() * vehicleCapacity.value());
	NumPackages packageCount(0);

	NumVehicles vehicles(0);
    Dollar costPerTrip = this->length().value()*this->difficulty().value()*fleet->speed(mode()).value();

	for (Shipment::Ptr s = shipmentQ_.front(); !shipmentQ_.empty() && packageCount < totalCapacity; ) {
		NumPackages load = s->load();
		if (load.value() > totalCapacity.value()) { // split shipment
			++shipmentsFragmented_;
			s->loadIs(load.value() - totalCapacity.value());
			load = totalCapacity;
			shipmentQ_.push_front( Shipment::ShipmentNew( s->name(), s->source(), s->destination(), s->origSize(), load, s->timeShipped() ));
		}
		// now we're guaranteed to have at least one shipment at the
		// front of the queue that we can send off
        Shipment::Ptr shipment = shipmentQ_.front();
        shipment->costIs(shipment->cost().value() + costPerTrip.value());
		shipments.push_front(shipment);
		shipmentQ_.pop_front();
		packageCount = packageCount.value() + load.value();
		totalCapacity = totalCapacity.value() - load.value();
	}

	vehicles = packageCount.value() / vehicleCapacity.value() +
		((packageCount.value() % vehicleCapacity.value() > 0) ? 1 : 0);
    capacity_ = capacity_.value() - vehicles.value();

	fwd->lastNotifieeIs(new ForwardShipmentReactor(manager,
								fwd.ptr(), 0, this, shipments, vehicles)); 
	Time timeTaken = this->length().value() / fleet->speed(mode()).value();
	fwd->nextTimeIs(manager->now().value() + timeTaken.value());
	fwd->statusIs(Activity::nextTimeScheduled);
}