use of de.hpi.bpt.scylla.model.global.resource.TimetableItem in project scylla by bptlab.
the class DateTimeUtils method getAvailabilityTime.
/**
* Calculates the duration of availability from the start datetime of simulation to the given end datetime (which is
* calculated from the provided time instant which describes the time relative to simulation start). The calculation
* considers the timetable.
*
* @param timetable
* the timetable
* @param timeInstant
* the time instant from which the end datetime is calculated
* @return the duration of availability
*/
public static long getAvailabilityTime(List<TimetableItem> timetable, TimeInstant timeInstant) {
long availabilityTime = 0;
ZonedDateTime endDateTime = getDateTime(timeInstant);
if (timetable == null) {
return timeInstant.getTimeRounded(timeUnit);
} else if (timetable.isEmpty()) {
return availabilityTime;
}
int index = getTimeTableIndexWithinOrNext(startDateTime, timetable);
ZonedDateTime dateTime = startDateTime;
while (dateTime.compareTo(endDateTime) < 0) {
TimetableItem item = timetable.get(index);
DayOfWeek untilWeekday = item.getWeekdayTo();
LocalTime untilTime = item.getEndTime();
if (isWithin(dateTime, item)) {
untilWeekday = endDateTime.getDayOfWeek();
untilTime = endDateTime.toLocalTime();
}
ZonedDateTime dateTimeUntilEnd = getNextOrSameZonedDateTime(dateTime, untilWeekday, untilTime);
DayOfWeek startWeekday = item.getWeekdayFrom();
LocalTime startTime = item.getBeginTime();
if (isWithin(dateTime, item)) {
startWeekday = dateTime.getDayOfWeek();
startTime = dateTime.toLocalTime();
}
ZonedDateTime dateTimeOfStart = getNextOrSameZonedDateTime(dateTime, startWeekday, startTime);
if (dateTime.compareTo(dateTimeOfStart) < 0) {
// i.e. dateTime is before dateTimeOfStart
dateTime = dateTimeOfStart;
}
if (dateTimeUntilEnd.compareTo(endDateTime) > 0) {
// end of timetable item is after end of simulation
dateTimeUntilEnd = endDateTime;
}
long durationUntilEnd = chronoUnit.between(dateTime, dateTimeUntilEnd);
availabilityTime += durationUntilEnd;
dateTime = dateTimeUntilEnd;
if (!dateTime.equals(endDateTime)) {
// prepare next timetable item
index++;
if (index == timetable.size()) {
index = 0;
}
TimetableItem nextItem = timetable.get(index);
untilWeekday = nextItem.getWeekdayFrom();
untilTime = nextItem.getBeginTime();
dateTime = getNextZonedDateTime(dateTime, untilWeekday, untilTime);
}
}
return availabilityTime;
}
use of de.hpi.bpt.scylla.model.global.resource.TimetableItem in project scylla by bptlab.
the class DateTimeUtils method intersectTimetables.
/**
* Calculates the intersection between two timetables.
*
* @param timetable1
* first timetable
* @param timetable2
* second timetable
* @return the intersection between the two timetables
*/
public static List<TimetableItem> intersectTimetables(List<TimetableItem> timetable1, List<TimetableItem> timetable2) {
if (timetable1 == null) {
// null means "anytime", so choose timetable2
return timetable2;
} else if (timetable2 == null) {
// null means "anytime", so choose timetable1
return timetable1;
}
Collections.sort(timetable1, comparatorByWeekdayFromAndBeginTimeAsc);
Collections.sort(timetable2, comparatorByWeekdayFromAndBeginTimeAsc);
List<TimetableItem> intersection = new ArrayList<TimetableItem>();
int index1 = 0;
int index2 = 0;
while (index1 < timetable1.size() && index2 < timetable2.size()) {
TimetableItem item1 = timetable1.get(index1);
TimetableItem item2 = timetable2.get(index2);
TimetableItem result = intersectTimetableItems(item1, item2);
if (result != null) {
intersection.add(result);
}
int diff = compareWeekdayTime(item1.getWeekdayTo(), item1.getEndTime(), item2.getWeekdayTo(), item2.getEndTime());
if (diff <= 0) {
// move to next interval from timetable1 if it ends earlier
index1++;
} else {
index2++;
}
}
return intersection;
}
use of de.hpi.bpt.scylla.model.global.resource.TimetableItem in project scylla by bptlab.
the class DateTimeUtils method intersectTimetableItems.
/**
* Calculates the intersection between two timetable items.
*
* @param item1
* first timetable item
* @param item2
* second timetable item
* @return the intersection between the two timetable items
*/
public static TimetableItem intersectTimetableItems(TimetableItem item1, TimetableItem item2) {
TimetableItem intersectedItem = null;
int diff;
diff = compareWeekdayTime(item1.getWeekdayFrom(), item1.getBeginTime(), item2.getWeekdayFrom(), item2.getBeginTime());
DayOfWeek startDay;
LocalTime startTime;
if (diff > 0) {
// item2 is earlier, so take item1
startDay = item1.getWeekdayFrom();
startTime = item1.getBeginTime();
diff = compareWeekdayTime(startDay, startTime, item2.getWeekdayTo(), item2.getEndTime());
if (diff >= 0) {
// start of item1 is after end of item2, so there is no overlap
return null;
}
} else {
startDay = item2.getWeekdayFrom();
startTime = item2.getBeginTime();
diff = compareWeekdayTime(startDay, startTime, item1.getWeekdayTo(), item1.getEndTime());
if (diff >= 0) {
// start of item2 is after end of item1, so there is no overlap
return null;
}
}
DayOfWeek endDay;
LocalTime endTime;
diff = compareWeekdayTime(item1.getWeekdayTo(), item1.getEndTime(), item2.getWeekdayTo(), item2.getEndTime());
if (diff <= 0) {
// item1 is earlier, so take it
endDay = item1.getWeekdayTo();
endTime = item1.getEndTime();
} else {
endDay = item2.getWeekdayTo();
endTime = item2.getEndTime();
}
intersectedItem = new TimetableItem(startDay, endDay, startTime, endTime);
return intersectedItem;
}
use of de.hpi.bpt.scylla.model.global.resource.TimetableItem in project scylla by bptlab.
the class ResourceQueue method pollAvailable.
public List<ResourceObject> pollAvailable(TimeInstant retrievalTime) {
Set<ResourceObject> unavailableResourceObjects = new HashSet<ResourceObject>();
List<ResourceObject> availableResourceObjects = new ArrayList<ResourceObject>();
ResourceObject e = super.poll();
// looking for an available resource
while (e != null) {
ZonedDateTime retrievalDateTime = DateTimeUtils.getDateTime(retrievalTime);
List<TimetableItem> timetable = e.getTimetable();
if (timetable == null) {
// null == available at any time
availableResourceObjects.add(e);
} else {
boolean isWithinAnyTimetable = e.isAvailable(retrievalDateTime);
if (isWithinAnyTimetable) {
// available at certain times
availableResourceObjects.add(e);
} else {
// not available
unavailableResourceObjects.add(e);
}
}
e = super.poll();
}
super.addAll(unavailableResourceObjects);
return availableResourceObjects;
}
use of de.hpi.bpt.scylla.model.global.resource.TimetableItem in project scylla by bptlab.
the class SimulationModel method convertToResourceObjects.
private void convertToResourceObjects(Map<String, Resource> resources) throws InstantiationException {
resourceObjects = new HashMap<String, ResourceQueue>();
for (String resourceType : resources.keySet()) {
Resource resource = resources.get(resourceType);
int quantity = resource.getQuantity();
ResourceQueue resQueue = new ResourceQueue(quantity);
if (resource instanceof DynamicResource) {
DynamicResource dynResource = (DynamicResource) resource;
Map<String, DynamicResourceInstance> resourceInstances = dynResource.getResourceInstances();
for (String resourceInstanceName : resourceInstances.keySet()) {
DynamicResourceInstance instance = resourceInstances.get(resourceInstanceName);
double cost = instance.getCost();
TimeUnit timeUnit = instance.getTimeUnit();
List<TimetableItem> timetable = instance.getTimetable();
ResourceObject resObject = new ResourceObject(resourceType, resourceInstanceName, cost, timeUnit, timetable);
resQueue.add(resObject);
boolean availableAtStart = resObject.isAvailable(startDateTime);
SimulationUtils.scheduleNextResourceAvailableEvent(this, resObject, startDateTime, availableAtStart);
}
} else {
throw new InstantiationException("Type of resource " + resourceType + " not supported.");
}
resourceObjects.put(resourceType, resQueue);
}
}
Aggregations