source: xssim/src/test/rewolucja/resources/ProcessingElements.java @ 104

Revision 104, 4.9 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package test.rewolucja.resources;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.Iterator;
6import java.util.List;
7import java.util.ListIterator;
8
9import schedframe.resources.units.ResourceUnit;
10import schedframe.scheduling.utils.ResourceParameterName;
11import test.rewolucja.resources.physical.base.ComputingResource;
12
13public class ProcessingElements extends ResourceUnit implements List<ComputingResource> {
14
15        protected List<ComputingResource> resources;
16
17        public ProcessingElements(String resName){
18                super(ResourceParameterName.PROCESSINGELEMENTS);
19                resourceId = resName;
20                resources =  null;
21        }
22       
23        public ProcessingElements(String resName, List<ComputingResource> resources){
24                this(resName);
25                this.resources = resources;
26        }
27
28
29        public ResourceType getResourceType(){
30                if(resources != null && resources.size() > 0)
31                        return resources.get(0).getType();
32                else return null;
33        }
34       
35        public int getAmount(){
36                return this.resources.size();
37        }
38       
39        public int getSpeed(){
40                int peCnt = getAmount();
41               
42                double avgSpeed = 0;
43                for(int i = 0; i < peCnt; i++){
44                        avgSpeed += resources.get(i).getResourceCharacteristic().getResourceUnit(ResourceParameterName.CPUSPEED).getAmount();
45                }
46                avgSpeed = avgSpeed / peCnt;
47                int speed = (int) Math.round(avgSpeed);
48                return speed;
49        }
50       
51        @Override
52        public int compareTo(ResourceUnit arg0) {
53                // TODO Auto-generated method stub
54                return 0;
55        }
56
57        @Override
58        public boolean add(ComputingResource e) {
59                if(resources == null)
60                        resources = new ArrayList<ComputingResource>(1);
61                return resources.add(e);
62
63        }
64
65        @Override
66        public void add(int index, ComputingResource element) {
67                if(resources == null)
68                        resources = new ArrayList<ComputingResource>(1);
69                resources.add(index, element);
70        }
71
72        @Override
73        public boolean addAll(Collection<? extends ComputingResource> c) {
74                if(resources == null)
75                        resources = new ArrayList<ComputingResource>(c.size());
76                return resources.addAll(c);
77        }
78
79        @Override
80        public boolean addAll(int index, Collection<? extends ComputingResource> c) {
81                return resources.addAll(index, c);
82        }
83
84        @Override
85        public void clear() {
86                // TODO Auto-generated method stub
87
88        }
89
90        @Override
91        public boolean contains(Object o) {
92                return resources.contains(o);
93        }
94
95        @Override
96        public boolean containsAll(Collection<?> c) {
97                return resources.containsAll(c);
98        }
99
100        @Override
101        public ComputingResource get(int index) {
102                return resources.get(index);
103        }
104
105        @Override
106        public int indexOf(Object o) {
107                return resources.indexOf(o);
108        }
109
110        @Override
111        public boolean isEmpty() {
112                return resources.isEmpty();
113        }
114
115        @Override
116        public Iterator<ComputingResource> iterator() {
117                return resources.iterator();
118        }
119
120        @Override
121        public int lastIndexOf(Object o) {
122                return resources.lastIndexOf(o);
123        }
124
125        @Override
126        public ListIterator<ComputingResource> listIterator() {
127                return resources.listIterator();
128        }
129
130        @Override
131        public ListIterator<ComputingResource> listIterator(int index) {
132                return resources.listIterator(index);
133        }
134
135        @Override
136        public boolean remove(Object o) {
137                // TODO Auto-generated method stub
138                return false;
139        }
140
141        @Override
142        public ComputingResource remove(int index) {
143                // TODO Auto-generated method stub
144                return null;
145        }
146
147        @Override
148        public boolean removeAll(Collection<?> c) {
149                // TODO Auto-generated method stub
150                return false;
151        }
152
153        @Override
154        public boolean retainAll(Collection<?> c) {
155                // TODO Auto-generated method stub
156                return false;
157        }
158
159        @Override
160        public ComputingResource set(int index, ComputingResource element) {
161                // TODO Auto-generated method stub
162                return null;
163        }
164
165        @Override
166        public int size() {
167                return resources.size();
168        }
169
170        @Override
171        public List<ComputingResource> subList(int fromIndex, int toIndex) {
172                return resources.subList(fromIndex, toIndex);
173        }
174
175        @Override
176        public Object[] toArray() {
177                return resources.toArray();
178        }
179
180        @Override
181        public <T> T[] toArray(T[] a) {
182                return resources.toArray(a);
183        }
184
185       
186        protected int getAmount(ResourceStatus status){
187                int sum = 0;
188                for(int i = 0; i < this.resources.size(); i++){
189                        if(resources.get(i).getStatus() == status){
190                                sum++;
191                        }
192                }
193                return sum;
194        }
195       
196        public int getFreeAmount(){
197                return getAmount(ResourceStatus.FREE);
198        }
199
200        public int getUsedAmount(){
201                return getAmount(ResourceStatus.BUSY);
202        }
203       
204        @Override
205        public void setUsedAmount(int amount) {
206                int cnt = getAmount(ResourceStatus.BUSY);
207                int delta = amount - cnt;
208
209                if(delta > 0){
210                                for(int i = 0; i < this.resources.size() && delta > 0; i++){
211                                        ComputingResource r = this.resources.get(i);
212                                        if(r.getStatus() == ResourceStatus.FREE){
213                                                r.setStatus(ResourceStatus.BUSY);
214                                                delta--;
215                                        }
216                                }
217                } else if(delta < 0) {
218                        for(int i = this.resources.size() - 1; i >= 0 && delta < 0; i--){
219                                ComputingResource r = this.resources.get(i);
220                                if(r.getStatus() == ResourceStatus.BUSY){
221                                        r.setStatus(ResourceStatus.FREE);
222                                        delta++;
223                                }
224                        }
225                }
226        }
227
228        @Override
229        public ResourceUnit toDiscrete() throws ClassNotFoundException {
230                // TODO Auto-generated method stub
231                return null;
232        }
233
234}
Note: See TracBrowser for help on using the repository browser.