Search in sources :

Example 1 with ResourceAvailabilityEvent

use of de.hpi.bpt.scylla.simulation.event.ResourceAvailabilityEvent in project scylla by bptlab.

the class SimulationUtils method scheduleNextResourceAvailableEvent.

/**
 * Creates and schedules a DesmoJ event which represents a resource instance which returns from idle.
 *
 * @param model
 *            the simulation model
 * @param resourceObject
 *            the resource instance returning from idle
 * @param currentDateTime
 *            the current date time
 * @param currentlyInTimetableItem
 *            true if the resource instance is currently active
 */
public static void scheduleNextResourceAvailableEvent(SimulationModel model, ResourceObject resourceObject, ZonedDateTime currentDateTime, boolean currentlyInTimetableItem) {
    boolean showInTrace = model.traceIsOn();
    TimeUnit timeUnit = DateTimeUtils.getReferenceTimeUnit();
    long currentTime = DateTimeUtils.getTimeInstant(currentDateTime).getTimeRounded(timeUnit);
    List<TimetableItem> timetable = resourceObject.getTimetable();
    if (timetable == null) {
        return;
    }
    int index = DateTimeUtils.getTimeTableIndexWithinOrNext(currentDateTime, timetable);
    if (currentlyInTimetableItem) {
        index++;
        if (index == timetable.size()) {
            index = 0;
        }
    }
    TimetableItem nextTimetableItem = timetable.get(index);
    DayOfWeek weekday = nextTimetableItem.getWeekdayFrom();
    LocalTime time = nextTimetableItem.getBeginTime();
    ZonedDateTime nextDateTime = DateTimeUtils.getNextZonedDateTime(currentDateTime, weekday, time);
    long durationToNextResourceAvailableEvent = DateTimeUtils.getDuration(currentDateTime, nextDateTime);
    if (model.getEndDateTime() != null) {
        long endTimeRelativeToGlobalStart = DateTimeUtils.getDuration(model.getStartDateTime(), model.getEndDateTime());
        if (currentTime + durationToNextResourceAvailableEvent >= endTimeRelativeToGlobalStart) {
            return;
        }
    }
    ResourceAvailabilityEvent event = new ResourceAvailabilityEvent(model, resourceObject, showInTrace);
    event.schedule(new TimeSpan(durationToNextResourceAvailableEvent, timeUnit));
}
Also used : TimeSpan(desmoj.core.simulator.TimeSpan) DayOfWeek(java.time.DayOfWeek) LocalTime(java.time.LocalTime) ZonedDateTime(java.time.ZonedDateTime) TimeUnit(java.util.concurrent.TimeUnit) ResourceAvailabilityEvent(de.hpi.bpt.scylla.simulation.event.ResourceAvailabilityEvent) TimetableItem(de.hpi.bpt.scylla.model.global.resource.TimetableItem)

Aggregations

TimetableItem (de.hpi.bpt.scylla.model.global.resource.TimetableItem)1 ResourceAvailabilityEvent (de.hpi.bpt.scylla.simulation.event.ResourceAvailabilityEvent)1 TimeSpan (desmoj.core.simulator.TimeSpan)1 DayOfWeek (java.time.DayOfWeek)1 LocalTime (java.time.LocalTime)1 ZonedDateTime (java.time.ZonedDateTime)1 TimeUnit (java.util.concurrent.TimeUnit)1