1 | package schedframe.resources.computing.description; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.Collection; |
---|
5 | import java.util.HashMap; |
---|
6 | import java.util.Iterator; |
---|
7 | import java.util.List; |
---|
8 | import java.util.Map; |
---|
9 | |
---|
10 | import schedframe.Parameters; |
---|
11 | import schedframe.resources.ResourceTypeFactory; |
---|
12 | import schedframe.resources.devices.Device; |
---|
13 | import schedframe.resources.devices.description.DeviceDescription; |
---|
14 | import schedframe.resources.devices.description.PhysicalResourceDescription; |
---|
15 | import schedframe.resources.units.ResourceUnit; |
---|
16 | import schedframe.resources.units.ResourceUnitFactory; |
---|
17 | import schedframe.resources.units.ResourceUnitName; |
---|
18 | import schedframe.resources.utils.ResourceIdGenerator; |
---|
19 | import schemas.ComputingResource; |
---|
20 | |
---|
21 | |
---|
22 | public class ComputingResourceDescription extends PhysicalResourceDescription implements ExecutingResourceDescription { |
---|
23 | |
---|
24 | protected Map<ResourceUnitName, List<ResourceUnit>> resUnits; |
---|
25 | protected List<Device> devices; |
---|
26 | |
---|
27 | public ComputingResourceDescription(ComputingResource computingResource) { |
---|
28 | |
---|
29 | super(ResourceTypeFactory.createResourceType(computingResource.getClazz())); |
---|
30 | this.category = computingResource.getType(); |
---|
31 | |
---|
32 | initId(computingResource); |
---|
33 | |
---|
34 | if (computingResource.getComputingResourceTypeChoiceSequence() != null) { |
---|
35 | initResourceUnits(computingResource.getComputingResourceTypeChoiceSequence().getResourceUnit()); |
---|
36 | |
---|
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 = new Device(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 | } |
---|