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

Revision 1453, 5.6 KB checked in by wojtekp, 10 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                                long devCount = dev[i].getCount() > 1 ? dev[i].getCount() : 1;
69                                for(int j = 0; j < devCount; j++){
70                                        Device device =  DeviceFactory.createDevice(new DeviceDescription(dev[i]));
71                                        this.devices.add(device);
72                                }
73                        }
74                }
75        }
76
77        public String getCompResourceParameterValue(String name){
78                return getParameters().get(name).get(0).getContent();
79        }
80
81        /*private Properties initProperties(schemas.Parameter[] parameters){
82                Properties prop = new Properties();
83               
84                for(int i = 0; i < parameters.length; i++){
85                        schemas.Parameter parameter = parameters[i];
86                        List values = new ArrayList();
87                        if(parameter.getParameterTypeSequence().getProperty() != null)
88                        {
89                                Map<String, List<StringValueWithUnit>> properties = new HashMap<String, List<StringValueWithUnit>>();
90                                List<StringValueWithUnit> propValues = new ArrayList<StringValueWithUnit>();
91                                for(int j = 0; j < parameter.getParameterTypeSequence().getPropertyCount(); j++){
92                                        schemas.Property property = parameter.getParameterTypeSequence().getProperty(j);
93                                        for(int k = 0; k < property.getStringValueWithUnitCount(); k++){
94                                                propValues.add(property.getStringValueWithUnit(k));
95                                        }
96                                        properties.put(property.getName(), propValues);
97                                }
98                                values.add(properties);
99                        }else {
100                                for(int j = 0; j < parameter.getStringValueWithUnitCount(); j++){
101                                        values.add(parameter.getStringValueWithUnit(j));
102                                }
103                        }
104                        prop.put(parameter.getName(), values);
105                }
106                return prop;
107        }*/
108
109
110        public List<Device> getDevices() {
111                return devices;
112        }
113
114
115        public void addResourceUnit(ResourceUnit unit) {
116                if (this.resUnits == null)
117                        this.resUnits = new HashMap<ResourceUnitName, List<ResourceUnit>>(2);
118                List<ResourceUnit> list = null;
119                if (this.resUnits.containsKey(unit.getName())) {
120                        list = this.resUnits.get(unit.getName());
121                } else {
122                        list = new ArrayList<ResourceUnit>(1);
123                        this.resUnits.put(unit.getName(), list);
124                }
125                list.add(unit);
126        }
127       
128        public ResourceUnit getResourceUnit(ResourceUnitName unitName) throws NoSuchFieldException {
129                return getResourceUnitList(unitName).get(0);
130        }
131
132        public List<ResourceUnit> getResourceUnitList(ResourceUnitName unitName) throws NoSuchFieldException {
133                if (resUnits.containsKey(unitName))
134                        return resUnits.get(unitName);
135                else
136                        throw new NoSuchFieldException("Resource unit " + unitName + " is not available in resource " + this.id);
137        }
138
139        public Collection<ResourceUnit> getResourceUnit() {
140                if (resUnits == null)
141                        return null;
142                List<ResourceUnit> values = new ArrayList<ResourceUnit>();
143                Collection<List<ResourceUnit>> lists = resUnits.values();
144                Iterator<List<ResourceUnit>> itr = lists.iterator();
145
146                while (itr.hasNext()) {
147                        List<ResourceUnit> list = itr.next();
148                        values.addAll(list);
149                }
150
151                return values;
152        }
153
154        public Map<ResourceUnitName, List<ResourceUnit>> getResourceUnits() {
155                return resUnits;
156        }
157}
Note: See TracBrowser for help on using the repository browser.