source: DCWoRMS/branches/coolemall/src/schedframe/resources/computing/description/ComputingResourceDescription.java @ 1344

Revision 1344, 5.5 KB checked in by wojtekp, 11 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package schedframe.resources.computing.description;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.HashMap;
6import java.util.Iterator;
7import java.util.List;
8import java.util.Map;
9
10import schedframe.Parameters;
11import schedframe.resources.ResourceTypeFactory;
12import schedframe.resources.devices.Device;
13import schedframe.resources.devices.DeviceFactory;
14import schedframe.resources.devices.description.DeviceDescription;
15import schedframe.resources.devices.description.PhysicalResourceDescription;
16import schedframe.resources.units.ResourceUnit;
17import schedframe.resources.units.ResourceUnitFactory;
18import schedframe.resources.units.ResourceUnitName;
19import schedframe.resources.utils.ResourceIdGenerator;
20import schemas.ComputingResource;
21
22
23public class ComputingResourceDescription extends PhysicalResourceDescription implements ExecutingResourceDescription {
24
25        protected Map<ResourceUnitName, List<ResourceUnit>> resUnits;
26        protected List<Device> devices;
27       
28        public ComputingResourceDescription(ComputingResource computingResource) {
29
30                super(ResourceTypeFactory.createResourceType(computingResource.getClazz()));
31                this.category = computingResource.getType();
32               
33                initId(computingResource);
34
35                if (computingResource.getComputingResourceTypeChoiceSequence() != null) {
36                        initResourceUnits(computingResource.getComputingResourceTypeChoiceSequence().getResourceUnit());
37                        initProfiles(computingResource.getComputingResourceTypeChoiceSequence().getProfile());
38                        initLocation(computingResource.getComputingResourceTypeChoiceSequence().getLocation());
39                        initDevices(computingResource.getComputingResourceTypeChoiceSequence().getDevice());
40                        this.parameters = extractParameters(computingResource.getComputingResourceTypeChoiceSequence().getParameter());
41                } else {
42                        initLoadProfile(null);
43                }
44        }
45
46        private void initId(ComputingResource computingResource){
47                this.id = computingResource.getName() != null ? computingResource.getName() : type.getName();
48                if(computingResource.getCount() >= 1 || computingResource.getName() == null){
49                        this.id = id + "_" + String.valueOf(ResourceIdGenerator.getId(type.getName()));
50                }
51        }
52       
53        private void initResourceUnits(schemas.ResourceUnit[] resourceUnitCharacteristics) {
54                for (int i = 0; i < resourceUnitCharacteristics.length; i++) {
55                        schemas.ResourceUnit resourceUnitCharacteristic = resourceUnitCharacteristics[i];
56                        ResourceUnit resourceUnit = ResourceUnitFactory.createUnit(resourceUnitCharacteristic.getClazz(), this.id,
57                                         Double.valueOf(resourceUnitCharacteristic.getAmount().getContent()).intValue(), 0);
58                        Parameters params = extractParameters(resourceUnitCharacteristic.getParameter());
59                        resourceUnit.init(params);
60                        addResourceUnit(resourceUnit);
61                }
62        }
63       
64        private void initDevices(schemas.Device[] dev) {
65                if (dev != null){
66                        this.devices = new ArrayList<Device>();
67                        for(int i = 0; i < dev.length; i++){
68                                Device device =  DeviceFactory.createDevice(new DeviceDescription(dev[i]));
69                                this.devices.add(device);
70                        }
71                }
72        }
73
74        public String getCompResourceParameterValue(String name){
75                return getParameters().get(name).get(0).getContent();
76        }
77
78        /*private Properties initProperties(schemas.Parameter[] parameters){
79                Properties prop = new Properties();
80               
81                for(int i = 0; i < parameters.length; i++){
82                        schemas.Parameter parameter = parameters[i];
83                        List values = new ArrayList();
84                        if(parameter.getParameterTypeSequence().getProperty() != null)
85                        {
86                                Map<String, List<StringValueWithUnit>> properties = new HashMap<String, List<StringValueWithUnit>>();
87                                List<StringValueWithUnit> propValues = new ArrayList<StringValueWithUnit>();
88                                for(int j = 0; j < parameter.getParameterTypeSequence().getPropertyCount(); j++){
89                                        schemas.Property property = parameter.getParameterTypeSequence().getProperty(j);
90                                        for(int k = 0; k < property.getStringValueWithUnitCount(); k++){
91                                                propValues.add(property.getStringValueWithUnit(k));
92                                        }
93                                        properties.put(property.getName(), propValues);
94                                }
95                                values.add(properties);
96                        }else {
97                                for(int j = 0; j < parameter.getStringValueWithUnitCount(); j++){
98                                        values.add(parameter.getStringValueWithUnit(j));
99                                }
100                        }
101                        prop.put(parameter.getName(), values);
102                }
103                return prop;
104        }*/
105
106
107        public List<Device> getDevices() {
108                return devices;
109        }
110
111
112        public void addResourceUnit(ResourceUnit unit) {
113                if (this.resUnits == null)
114                        this.resUnits = new HashMap<ResourceUnitName, List<ResourceUnit>>(1);
115                List<ResourceUnit> list = null;
116                if (this.resUnits.containsKey(unit.getName())) {
117                        list = this.resUnits.get(unit.getName());
118                } else {
119                        list = new ArrayList<ResourceUnit>(1);
120                        this.resUnits.put(unit.getName(), list);
121                }
122                list.add(unit);
123        }
124       
125        public ResourceUnit getResourceUnit(ResourceUnitName unitName) throws NoSuchFieldException {
126                return getResourceUnitList(unitName).get(0);
127        }
128
129        public List<ResourceUnit> getResourceUnitList(ResourceUnitName unitName) throws NoSuchFieldException {
130                if (resUnits.containsKey(unitName))
131                        return resUnits.get(unitName);
132                else
133                        throw new NoSuchFieldException("Resource unit " + unitName + " is not available in resource " + this.id);
134        }
135
136        public Collection<ResourceUnit> getResourceUnit() {
137                if (resUnits == null)
138                        return null;
139                List<ResourceUnit> values = new ArrayList<ResourceUnit>();
140                Collection<List<ResourceUnit>> lists = resUnits.values();
141                Iterator<List<ResourceUnit>> itr = lists.iterator();
142
143                while (itr.hasNext()) {
144                        List<ResourceUnit> list = itr.next();
145                        values.addAll(list);
146                }
147
148                return values;
149        }
150
151        public Map<ResourceUnitName, List<ResourceUnit>> getResourceUnits() {
152                return resUnits;
153        }
154}
Note: See TracBrowser for help on using the repository browser.