source: xssim/branches/tpiontek/src/test/rewolucja/resources/ProcessingElements.java @ 258

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