1 | package simulator.lists.slotList; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.Collections; |
---|
5 | import java.util.List; |
---|
6 | |
---|
7 | import org.joda.time.DateTime; |
---|
8 | |
---|
9 | import schedframe.resources.ResourceProvider; |
---|
10 | import schedframe.resources.ResourceStateDescription; |
---|
11 | import schedframe.resources.units.Processors; |
---|
12 | import schedframe.resources.units.ResourceUnit; |
---|
13 | import schedframe.scheduling.TimeResourceAllocation; |
---|
14 | import schedframe.scheduling.utils.ResourceParameterName; |
---|
15 | |
---|
16 | /** |
---|
17 | * |
---|
18 | * @author Marcin Krystek |
---|
19 | * |
---|
20 | */ |
---|
21 | public class TimeResOptIterator implements SlotsGanttIterator { |
---|
22 | |
---|
23 | protected SlotsGantt list; |
---|
24 | protected ArrayList<TimeResourceAllocation> newSlots; |
---|
25 | protected int currentPosition = 0; |
---|
26 | |
---|
27 | public void visit(SlotsGantt list) { |
---|
28 | this.list = list; |
---|
29 | this.newSlots = new ArrayList<TimeResourceAllocation>(); |
---|
30 | int totalAmount = -1; |
---|
31 | String resName = null; |
---|
32 | ArrayList<Integer> cpuCnt = new ArrayList<Integer>(list.size()); |
---|
33 | |
---|
34 | try { |
---|
35 | |
---|
36 | for(int i = 0; i < this.list.size(); i++){ |
---|
37 | ResourceUnit unit = list.get(i).getAllocatedResource(). |
---|
38 | getResourceUnit(ResourceParameterName.CPUCOUNT); |
---|
39 | cpuCnt.add(unit.getUsedAmount()); |
---|
40 | if(i == 0) { |
---|
41 | totalAmount = unit.getAmount(); |
---|
42 | resName = unit.getResourceId(); |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | List<Integer> uniqeCpuCnt = getUniqeResourceValues(cpuCnt); |
---|
47 | ArrayList<ArrayList<Integer>> ll = new ArrayList<ArrayList<Integer>>(uniqeCpuCnt.size()); |
---|
48 | for(int i = 0; i < uniqeCpuCnt.size(); i++){ |
---|
49 | ArrayList<Integer> slotIndexes = new ArrayList<Integer>(); |
---|
50 | int cnt = uniqeCpuCnt.get(i); |
---|
51 | for(int j = 0; j < cpuCnt.size(); j++){ |
---|
52 | if(cnt <= cpuCnt.get(j)){ |
---|
53 | slotIndexes.add(j); |
---|
54 | } |
---|
55 | } |
---|
56 | ll.add(slotIndexes); |
---|
57 | } |
---|
58 | |
---|
59 | for(int i = 0; i < ll.size(); i++){ |
---|
60 | ArrayList<Integer> l = ll.get(i); |
---|
61 | |
---|
62 | for(int j = 0; j < l.size(); j++){ |
---|
63 | int compareRresult = 0; |
---|
64 | TimeResourceAllocation slot = list.get(l.get(j)); |
---|
65 | DateTime startTime = slot.getStart(); |
---|
66 | DateTime endTime = slot.getEnd(); |
---|
67 | |
---|
68 | while(compareRresult == 0 && j < l.size() - 1){ |
---|
69 | endTime = slot.getEnd(); |
---|
70 | slot = list.get(l.get(++j)); |
---|
71 | compareRresult = endTime.compareTo(slot.getStart()); |
---|
72 | } |
---|
73 | |
---|
74 | if(compareRresult == 0) |
---|
75 | endTime = slot.getEnd(); |
---|
76 | else |
---|
77 | j--; |
---|
78 | |
---|
79 | ResourceStateDescription rsd = new ResourceStateDescription((ResourceProvider)null); |
---|
80 | rsd.addResourceUnit(new Processors(resName, totalAmount, uniqeCpuCnt.get(i))); |
---|
81 | TimeResourceAllocation newSlot = new TimeResourceAllocation(rsd, startTime, endTime); |
---|
82 | |
---|
83 | this.newSlots.add(newSlot); |
---|
84 | } |
---|
85 | } |
---|
86 | } catch(NoSuchFieldException e){ |
---|
87 | e.printStackTrace(); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | protected List<Integer> getUniqeResourceValues(List<Integer> list){ |
---|
93 | ArrayList<Integer> localList = new ArrayList<Integer>(list.size()); |
---|
94 | for(int i = 0; i < list.size(); i++){ |
---|
95 | localList.add(list.get(i)); |
---|
96 | } |
---|
97 | Collections.sort(localList); |
---|
98 | ArrayList<Integer> ret = new ArrayList<Integer>(); |
---|
99 | |
---|
100 | int prev = localList.get(0); |
---|
101 | if(prev != 0) |
---|
102 | ret.add(prev); |
---|
103 | |
---|
104 | for(int i = 1; i < localList.size(); i++){ |
---|
105 | int current = localList.get(i); |
---|
106 | if(prev != current){ |
---|
107 | prev = current; |
---|
108 | if(current != 0) ret.add(current); |
---|
109 | } |
---|
110 | } |
---|
111 | return ret; |
---|
112 | } |
---|
113 | |
---|
114 | public boolean hasNext() { |
---|
115 | return (currentPosition < newSlots.size()); |
---|
116 | } |
---|
117 | |
---|
118 | public TimeResourceAllocation next() { |
---|
119 | return newSlots.get(currentPosition++); |
---|
120 | } |
---|
121 | |
---|
122 | public void remove() { |
---|
123 | throw new RuntimeException("Removing object by "+this.getClass() +" is not supported."); |
---|
124 | } |
---|
125 | |
---|
126 | public void reset(){ |
---|
127 | currentPosition = 0; |
---|
128 | } |
---|
129 | } |
---|