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

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