source: xssim/trunk/src/simulator/lists/slotList/TimeResOptIterator.java @ 104

Revision 104, 3.5 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package simulator.lists.slotList;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.List;
6
7import org.joda.time.DateTime;
8
9import schedframe.resources.ResourceProvider;
10import schedframe.resources.ResourceStateDescription;
11import schedframe.resources.units.Processors;
12import schedframe.resources.units.ResourceUnit;
13import schedframe.scheduling.TimeResourceAllocation;
14import schedframe.scheduling.utils.ResourceParameterName;
15
16/**
17 *
18 * @author Marcin Krystek
19 *
20 */
21public 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}
Note: See TracBrowser for help on using the repository browser.