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

Revision 1282, 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                }
42        }
43
44        private void initId(ComputingResource computingResource){
45                this.id = computingResource.getName() != null ? computingResource.getName() : type.getName();
46                if(computingResource.getCount() > 1 || computingResource.getName() == null){
47                        this.id = id + "_" + String.valueOf(ResourceIdGenerator.getId(type.getName()));
48                }
49        }
50       
51        private void initResourceUnits(schemas.ResourceUnit[] resourceUnitCharacteristics) {
52                for (int i = 0; i < resourceUnitCharacteristics.length; i++) {
53                        schemas.ResourceUnit resourceUnitCharacteristic = resourceUnitCharacteristics[i];
54                        ResourceUnit resourceUnit = ResourceUnitFactory.createUnit(resourceUnitCharacteristic.getClazz(), this.id,
55                                         Double.valueOf(resourceUnitCharacteristic.getAmount().getContent()).intValue(), 0);
56                        Parameters params = extractParameters(resourceUnitCharacteristic.getParameter());
57                        resourceUnit.init(params);
58                        addResourceUnit(resourceUnit);
59                }
60        }
61       
62        private void initDevices(schemas.Device[] dev) {
63                if (dev != null){
64                        this.devices = new ArrayList<Device>();
65                        for(int i = 0; i < dev.length; i++){
66                                Device device =  DeviceFactory.createDevice(new DeviceDescription(dev[i]));
67                                this.devices.add(device);
68                        }
69                }
70        }
71
72        public String getCompResourceParameterValue(String name){
73                return getParameters().get(name).get(0).getContent();
74        }
75
76        /*private Properties initProperties(schemas.Parameter[] parameters){
77                Properties prop = new Properties();
78               
79                for(int i = 0; i < parameters.length; i++){
80                        schemas.Parameter parameter = parameters[i];
81                        List values = new ArrayList();
82                        if(parameter.getParameterTypeSequence().getProperty() != null)
83                        {
84                                Map<String, List<StringValueWithUnit>> properties = new HashMap<String, List<StringValueWithUnit>>();
85                                List<StringValueWithUnit> propValues = new ArrayList<StringValueWithUnit>();
86                                for(int j = 0; j < parameter.getParameterTypeSequence().getPropertyCount(); j++){
87                                        schemas.Property property = parameter.getParameterTypeSequence().getProperty(j);
88                                        for(int k = 0; k < property.getStringValueWithUnitCount(); k++){
89                                                propValues.add(property.getStringValueWithUnit(k));
90                                        }
91                                        properties.put(property.getName(), propValues);
92                                }
93                                values.add(properties);
94                        }else {
95                                for(int j = 0; j < parameter.getStringValueWithUnitCount(); j++){
96                                        values.add(parameter.getStringValueWithUnit(j));
97                                }
98                        }
99                        prop.put(parameter.getName(), values);
100                }
101                return prop;
102        }*/
103
104
105        public List<Device> getDevices() {
106                return devices;
107        }
108
109
110        public void addResourceUnit(ResourceUnit unit) {
111                if (this.resUnits == null)
112                        this.resUnits = new HashMap<ResourceUnitName, List<ResourceUnit>>(1);
113                List<ResourceUnit> list = null;
114                if (this.resUnits.containsKey(unit.getName())) {
115                        list = this.resUnits.get(unit.getName());
116                } else {
117                        list = new ArrayList<ResourceUnit>(1);
118                        this.resUnits.put(unit.getName(), list);
119                }
120                list.add(unit);
121        }
122       
123        public ResourceUnit getResourceUnit(ResourceUnitName unitName) throws NoSuchFieldException {
124                return getResourceUnitList(unitName).get(0);
125        }
126
127        public List<ResourceUnit> getResourceUnitList(ResourceUnitName unitName) throws NoSuchFieldException {
128                if (resUnits.containsKey(unitName))
129                        return resUnits.get(unitName);
130                else
131                        throw new NoSuchFieldException("Resource unit " + unitName + " is not available in resource " + this.id);
132        }
133
134        public Collection<ResourceUnit> getResourceUnit() {
135                if (resUnits == null)
136                        return null;
137                List<ResourceUnit> values = new ArrayList<ResourceUnit>();
138                Collection<List<ResourceUnit>> lists = resUnits.values();
139                Iterator<List<ResourceUnit>> itr = lists.iterator();
140
141                while (itr.hasNext()) {
142                        List<ResourceUnit> list = itr.next();
143                        values.addAll(list);
144                }
145
146                return values;
147        }
148
149        public Map<ResourceUnitName, List<ResourceUnit>> getResourceUnits() {
150                return resUnits;
151        }
152}
Note: See TracBrowser for help on using the repository browser.