1 | package simulator.lists.slotList; |
---|
2 | |
---|
3 | |
---|
4 | import java.util.ArrayList; |
---|
5 | import java.util.Collection; |
---|
6 | |
---|
7 | import schedframe.resources.ResourceStateDescription; |
---|
8 | import schedframe.resources.units.ResourceUnit; |
---|
9 | import schedframe.scheduling.TimeResourceAllocation; |
---|
10 | import schedframe.scheduling.utils.ResourceParameterName; |
---|
11 | |
---|
12 | |
---|
13 | /** |
---|
14 | * |
---|
15 | * @author Marcin Krystek |
---|
16 | * |
---|
17 | */ |
---|
18 | public class SlotsGantt extends ArrayList<TimeResourceAllocation>{ |
---|
19 | |
---|
20 | private static final long serialVersionUID = -4334870725617831686L; |
---|
21 | |
---|
22 | public SlotsGantt(){ |
---|
23 | super(); |
---|
24 | } |
---|
25 | |
---|
26 | public SlotsGantt(Collection<TimeResourceAllocation> collection){ |
---|
27 | super(collection); |
---|
28 | } |
---|
29 | |
---|
30 | public void accept(SlotsGanttIterator iterator){ |
---|
31 | iterator.visit(this); |
---|
32 | } |
---|
33 | |
---|
34 | /** |
---|
35 | * Checks whether a slot can added to the Slot Gannt |
---|
36 | * @param tra a slot to be checked (contains required amount of resources and time period) |
---|
37 | * @return true if the slot fits to available resources; false otherwise |
---|
38 | * |
---|
39 | * @author Ariel |
---|
40 | */ |
---|
41 | public boolean checkSlot(TimeResourceAllocation tra) { |
---|
42 | |
---|
43 | if (tra == null) |
---|
44 | return false; |
---|
45 | |
---|
46 | int slotsCount; |
---|
47 | if ((slotsCount = size()) < 1) |
---|
48 | return false; |
---|
49 | |
---|
50 | long startTimeLong = tra.getStart().getMillis(); |
---|
51 | long endTimeLong = tra.getEnd().getMillis(); |
---|
52 | float reqRes = 0; |
---|
53 | try { |
---|
54 | reqRes = tra.getAllocatedResource().getResourceUnit(ResourceParameterName.CPUCOUNT).getUsedAmount(); |
---|
55 | } catch (NoSuchFieldException e1) { |
---|
56 | e1.printStackTrace(); |
---|
57 | } |
---|
58 | |
---|
59 | TimeResourceAllocation slot; |
---|
60 | |
---|
61 | //find first slot |
---|
62 | int startSlot = 0; //TODO: binarySearch(startTimeLong); |
---|
63 | while ((slot = get(startSlot)).getEnd().getMillis() <= startTimeLong) |
---|
64 | startSlot++; |
---|
65 | |
---|
66 | //walk through slots and check |
---|
67 | for (int i = startSlot; i < slotsCount; i++) { |
---|
68 | |
---|
69 | float freeRes = 0; |
---|
70 | ResourceUnit resUnit = null; |
---|
71 | try { |
---|
72 | resUnit = slot.getAllocatedResource().getResourceUnit(ResourceParameterName.CPUCOUNT); |
---|
73 | } catch (NoSuchFieldException e) { |
---|
74 | e.printStackTrace(); |
---|
75 | } |
---|
76 | freeRes = resUnit.getFreeAmount(); |
---|
77 | |
---|
78 | if (reqRes > freeRes) |
---|
79 | return false; |
---|
80 | |
---|
81 | if (endTimeLong <= slot.getEnd().getMillis()) |
---|
82 | return true; |
---|
83 | |
---|
84 | slot = get(i); |
---|
85 | } |
---|
86 | |
---|
87 | return false; |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * Adds a slot |
---|
92 | * @param tra a slot to be added (contains required amount of resources and time period) |
---|
93 | * |
---|
94 | * @author Ariel |
---|
95 | */ |
---|
96 | public void addSlot(TimeResourceAllocation tra) { |
---|
97 | |
---|
98 | if (tra == null) |
---|
99 | return; |
---|
100 | |
---|
101 | if (size() < 1) |
---|
102 | return; |
---|
103 | |
---|
104 | long startTimeLong = tra.getStart().getMillis(); |
---|
105 | long endTimeLong = tra.getEnd().getMillis(); |
---|
106 | |
---|
107 | TimeResourceAllocation slot; |
---|
108 | try { |
---|
109 | //find first slot |
---|
110 | int startSlot = 0; |
---|
111 | while ((slot = get(startSlot)).getEnd().getMillis() <= startTimeLong) |
---|
112 | startSlot++; |
---|
113 | |
---|
114 | //change of the first slot |
---|
115 | if (startTimeLong > slot.getStart().getMillis()) { |
---|
116 | |
---|
117 | if (endTimeLong > slot.getEnd().getMillis()) { |
---|
118 | ResourceStateDescription resState = new ResourceStateDescription(slot.getAllocatedResource()); |
---|
119 | ResourceUnit ru = resState.getResourceUnit(ResourceParameterName.CPUCOUNT); |
---|
120 | ru.setUsedAmount(ru.getUsedAmount() + tra.getAllocatedResource(). |
---|
121 | getResourceUnit(ResourceParameterName.CPUCOUNT). |
---|
122 | getUsedAmount()); |
---|
123 | TimeResourceAllocation newSlot = new TimeResourceAllocation(resState, tra.getStart(), slot.getEnd()); |
---|
124 | add(startSlot++, newSlot); |
---|
125 | } |
---|
126 | |
---|
127 | slot.setEnd(tra.getStart()); |
---|
128 | } |
---|
129 | |
---|
130 | //walk through slots and add |
---|
131 | while (endTimeLong > (slot = get(startSlot++)).getEnd().getMillis()) { |
---|
132 | |
---|
133 | ResourceUnit ru = slot.getAllocatedResource().getResourceUnit(ResourceParameterName.CPUCOUNT); |
---|
134 | ru.setUsedAmount(ru.getUsedAmount() + tra.getAllocatedResource(). |
---|
135 | getResourceUnit(ResourceParameterName.CPUCOUNT). |
---|
136 | getUsedAmount()); |
---|
137 | |
---|
138 | } |
---|
139 | |
---|
140 | //change of last slot |
---|
141 | |
---|
142 | if (endTimeLong < slot.getEnd().getMillis()) { |
---|
143 | ResourceStateDescription resState = new ResourceStateDescription(slot.getAllocatedResource()); |
---|
144 | TimeResourceAllocation newSlot = new TimeResourceAllocation(resState, tra.getEnd(), slot.getEnd()); |
---|
145 | add(startSlot, newSlot); |
---|
146 | } |
---|
147 | slot.setEnd(tra.getEnd()); |
---|
148 | ResourceUnit ru = slot.getAllocatedResource().getResourceUnit(ResourceParameterName.CPUCOUNT); |
---|
149 | ru.setUsedAmount(ru.getUsedAmount() + tra.getAllocatedResource(). |
---|
150 | getResourceUnit(ResourceParameterName.CPUCOUNT). |
---|
151 | getUsedAmount()); |
---|
152 | } catch(NoSuchFieldException e){ |
---|
153 | e.printStackTrace(); |
---|
154 | } |
---|
155 | |
---|
156 | } |
---|
157 | |
---|
158 | } |
---|