[477] | 1 | package schedframe.resources.computing.description; |
---|
| 2 | |
---|
| 3 | import java.util.ArrayList; |
---|
[1207] | 4 | import java.util.Collection; |
---|
| 5 | import java.util.HashMap; |
---|
| 6 | import java.util.Iterator; |
---|
[477] | 7 | import java.util.List; |
---|
[1207] | 8 | import java.util.Map; |
---|
[477] | 9 | |
---|
| 10 | import schedframe.Parameters; |
---|
| 11 | import schedframe.resources.ResourceTypeFactory; |
---|
[1207] | 12 | import schedframe.resources.devices.Device; |
---|
[1282] | 13 | import schedframe.resources.devices.DeviceFactory; |
---|
[1207] | 14 | import schedframe.resources.devices.description.DeviceDescription; |
---|
| 15 | import schedframe.resources.devices.description.PhysicalResourceDescription; |
---|
[477] | 16 | import schedframe.resources.units.ResourceUnit; |
---|
| 17 | import schedframe.resources.units.ResourceUnitFactory; |
---|
[1207] | 18 | import schedframe.resources.units.ResourceUnitName; |
---|
[477] | 19 | import schedframe.resources.utils.ResourceIdGenerator; |
---|
| 20 | import schemas.ComputingResource; |
---|
| 21 | |
---|
| 22 | |
---|
[1207] | 23 | public class ComputingResourceDescription extends PhysicalResourceDescription implements ExecutingResourceDescription { |
---|
[477] | 24 | |
---|
[1207] | 25 | protected Map<ResourceUnitName, List<ResourceUnit>> resUnits; |
---|
| 26 | protected List<Device> devices; |
---|
| 27 | |
---|
[477] | 28 | public ComputingResourceDescription(ComputingResource computingResource) { |
---|
| 29 | |
---|
| 30 | super(ResourceTypeFactory.createResourceType(computingResource.getClazz())); |
---|
[815] | 31 | this.category = computingResource.getType(); |
---|
| 32 | |
---|
[477] | 33 | initId(computingResource); |
---|
| 34 | |
---|
| 35 | if (computingResource.getComputingResourceTypeChoiceSequence() != null) { |
---|
| 36 | initResourceUnits(computingResource.getComputingResourceTypeChoiceSequence().getResourceUnit()); |
---|
| 37 | initProfiles(computingResource.getComputingResourceTypeChoiceSequence().getProfile()); |
---|
| 38 | initLocation(computingResource.getComputingResourceTypeChoiceSequence().getLocation()); |
---|
[1207] | 39 | initDevices(computingResource.getComputingResourceTypeChoiceSequence().getDevice()); |
---|
[477] | 40 | this.parameters = extractParameters(computingResource.getComputingResourceTypeChoiceSequence().getParameter()); |
---|
[1291] | 41 | } else { |
---|
| 42 | initLoadProfile(null); |
---|
[477] | 43 | } |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | private void initId(ComputingResource computingResource){ |
---|
[1052] | 47 | this.id = computingResource.getName() != null ? computingResource.getName() : type.getName(); |
---|
[1344] | 48 | if(computingResource.getCount() >= 1 || computingResource.getName() == null){ |
---|
[1052] | 49 | this.id = id + "_" + String.valueOf(ResourceIdGenerator.getId(type.getName())); |
---|
[477] | 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 | |
---|
[1207] | 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++){ |
---|
[1453] | 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 | } |
---|
[477] | 73 | } |
---|
| 74 | } |
---|
| 75 | } |
---|
| 76 | |
---|
[1207] | 77 | public String getCompResourceParameterValue(String name){ |
---|
| 78 | return getParameters().get(name).get(0).getContent(); |
---|
[477] | 79 | } |
---|
[815] | 80 | |
---|
[477] | 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 | |
---|
[1207] | 110 | public List<Device> getDevices() { |
---|
| 111 | return devices; |
---|
[477] | 112 | } |
---|
| 113 | |
---|
[1207] | 114 | |
---|
| 115 | public void addResourceUnit(ResourceUnit unit) { |
---|
| 116 | if (this.resUnits == null) |
---|
[1423] | 117 | this.resUnits = new HashMap<ResourceUnitName, List<ResourceUnit>>(2); |
---|
[1207] | 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); |
---|
[815] | 126 | } |
---|
| 127 | |
---|
[1207] | 128 | public ResourceUnit getResourceUnit(ResourceUnitName unitName) throws NoSuchFieldException { |
---|
| 129 | return getResourceUnitList(unitName).get(0); |
---|
[477] | 130 | } |
---|
| 131 | |
---|
[1207] | 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); |
---|
[512] | 137 | } |
---|
[477] | 138 | |
---|
[1207] | 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(); |
---|
[815] | 145 | |
---|
[1207] | 146 | while (itr.hasNext()) { |
---|
| 147 | List<ResourceUnit> list = itr.next(); |
---|
| 148 | values.addAll(list); |
---|
| 149 | } |
---|
[815] | 150 | |
---|
[1207] | 151 | return values; |
---|
[815] | 152 | } |
---|
[1207] | 153 | |
---|
| 154 | public Map<ResourceUnitName, List<ResourceUnit>> getResourceUnits() { |
---|
| 155 | return resUnits; |
---|
[815] | 156 | } |
---|
[477] | 157 | } |
---|