package simulator.lists.slotList; import java.util.ArrayList; import java.util.Collection; import schedframe.resources.ResourceStateDescription; import schedframe.resources.units.ResourceUnit; import schedframe.scheduling.TimeResourceAllocation; import schedframe.scheduling.utils.ResourceParameterName; /** * * @author Marcin Krystek * */ public class SlotsGantt extends ArrayList{ private static final long serialVersionUID = -4334870725617831686L; public SlotsGantt(){ super(); } public SlotsGantt(Collection collection){ super(collection); } public void accept(SlotsGanttIterator iterator){ iterator.visit(this); } /** * Checks whether a slot can added to the Slot Gannt * @param tra a slot to be checked (contains required amount of resources and time period) * @return true if the slot fits to available resources; false otherwise * * @author Ariel */ public boolean checkSlot(TimeResourceAllocation tra) { if (tra == null) return false; int slotsCount; if ((slotsCount = size()) < 1) return false; long startTimeLong = tra.getStart().getMillis(); long endTimeLong = tra.getEnd().getMillis(); float reqRes = 0; try { reqRes = tra.getAllocatedResource().getResourceUnit(ResourceParameterName.CPUCOUNT).getUsedAmount(); } catch (NoSuchFieldException e1) { e1.printStackTrace(); } TimeResourceAllocation slot; //find first slot int startSlot = 0; //TODO: binarySearch(startTimeLong); while ((slot = get(startSlot)).getEnd().getMillis() <= startTimeLong) startSlot++; //walk through slots and check for (int i = startSlot; i < slotsCount; i++) { float freeRes = 0; ResourceUnit resUnit = null; try { resUnit = slot.getAllocatedResource().getResourceUnit(ResourceParameterName.CPUCOUNT); } catch (NoSuchFieldException e) { e.printStackTrace(); } freeRes = resUnit.getFreeAmount(); if (reqRes > freeRes) return false; if (endTimeLong <= slot.getEnd().getMillis()) return true; slot = get(i); } return false; } /** * Adds a slot * @param tra a slot to be added (contains required amount of resources and time period) * * @author Ariel */ public void addSlot(TimeResourceAllocation tra) { if (tra == null) return; if (size() < 1) return; long startTimeLong = tra.getStart().getMillis(); long endTimeLong = tra.getEnd().getMillis(); TimeResourceAllocation slot; try { //find first slot int startSlot = 0; while ((slot = get(startSlot)).getEnd().getMillis() <= startTimeLong) startSlot++; //change of the first slot if (startTimeLong > slot.getStart().getMillis()) { if (endTimeLong > slot.getEnd().getMillis()) { ResourceStateDescription resState = new ResourceStateDescription(slot.getAllocatedResource()); ResourceUnit ru = resState.getResourceUnit(ResourceParameterName.CPUCOUNT); ru.setUsedAmount(ru.getUsedAmount() + tra.getAllocatedResource(). getResourceUnit(ResourceParameterName.CPUCOUNT). getUsedAmount()); TimeResourceAllocation newSlot = new TimeResourceAllocation(resState, tra.getStart(), slot.getEnd()); add(startSlot++, newSlot); } slot.setEnd(tra.getStart()); } //walk through slots and add while (endTimeLong > (slot = get(startSlot++)).getEnd().getMillis()) { ResourceUnit ru = slot.getAllocatedResource().getResourceUnit(ResourceParameterName.CPUCOUNT); ru.setUsedAmount(ru.getUsedAmount() + tra.getAllocatedResource(). getResourceUnit(ResourceParameterName.CPUCOUNT). getUsedAmount()); } //change of last slot if (endTimeLong < slot.getEnd().getMillis()) { ResourceStateDescription resState = new ResourceStateDescription(slot.getAllocatedResource()); TimeResourceAllocation newSlot = new TimeResourceAllocation(resState, tra.getEnd(), slot.getEnd()); add(startSlot, newSlot); } slot.setEnd(tra.getEnd()); ResourceUnit ru = slot.getAllocatedResource().getResourceUnit(ResourceParameterName.CPUCOUNT); ru.setUsedAmount(ru.getUsedAmount() + tra.getAllocatedResource(). getResourceUnit(ResourceParameterName.CPUCOUNT). getUsedAmount()); } catch(NoSuchFieldException e){ e.printStackTrace(); } } }