source: DCWoRMS/trunk/src/schedframe/resources/computing/description/ComputingResourceDescription.java @ 752

Revision 752, 11.7 KB checked in by wojtekp, 12 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.List;
5
6import schedframe.Parameter;
7import schedframe.Parameters;
8import schedframe.Property;
9import schedframe.resources.ResourceTypeFactory;
10import schedframe.resources.computing.location.Location;
11import schedframe.resources.computing.profiles.energy.airthroughput.AirThroughputProfile;
12import schedframe.resources.computing.profiles.energy.airthroughput.AirThroughputState;
13import schedframe.resources.computing.profiles.energy.airthroughput.plugin.AirThroughputEstimationPlugin;
14import schedframe.resources.computing.profiles.energy.airthroughput.plugin.DefaultAirThroughputEstimationPlugin;
15import schedframe.resources.computing.profiles.energy.power.PState;
16import schedframe.resources.computing.profiles.energy.power.PowerProfile;
17import schedframe.resources.computing.profiles.energy.power.PowerState;
18import schedframe.resources.computing.profiles.energy.power.PowerStateNameFactory;
19import schedframe.resources.computing.profiles.energy.power.Transition;
20import schedframe.resources.computing.profiles.energy.power.plugin.EnergyEstimationPlugin;
21import schedframe.resources.units.ResourceUnit;
22import schedframe.resources.units.ResourceUnitFactory;
23import schedframe.resources.utils.ResourceIdGenerator;
24import schemas.ComputingResource;
25import schemas.Profile;
26import simulator.utils.InstanceFactory;
27import example.energy.DefaultEnergyEstimationPlugin;
28
29public class ComputingResourceDescription extends ExecutingResourceDescription {
30
31        //protected Map<ResourceUnitName, List<AbstractResourceUnit>> resUnits;
32        protected PowerProfile powerProfile;
33        protected AirThroughputProfile airThroughputProfile;
34        protected Location location;
35        protected String category;
36        //protected Parameters parameters;
37
38        public ComputingResourceDescription(ComputingResource computingResource) {
39
40                super(ResourceTypeFactory.createResourceType(computingResource.getClazz()));
41
42                initId(computingResource);
43                this.category = computingResource.getType();
44
45                if (computingResource.getComputingResourceTypeChoiceSequence() != null) {
46                        initResourceUnits(computingResource.getComputingResourceTypeChoiceSequence().getResourceUnit());
47                        initProfiles(computingResource.getComputingResourceTypeChoiceSequence().getProfile());
48                        initLocation(computingResource.getComputingResourceTypeChoiceSequence().getLocation());
49                        this.parameters = extractParameters(computingResource.getComputingResourceTypeChoiceSequence().getParameter());
50                }
51        }
52
53        private void initId(ComputingResource computingResource){
54                this.id = computingResource.getName() != null ? computingResource.getName() : type.toString();
55                if(computingResource.getCount() > 1 || computingResource.getName() == null){
56                        this.id = id + "_" + String.valueOf(ResourceIdGenerator.getId(type));
57                }
58        }
59       
60        private void initResourceUnits(schemas.ResourceUnit[] resourceUnitCharacteristics) {
61                for (int i = 0; i < resourceUnitCharacteristics.length; i++) {
62                        schemas.ResourceUnit resourceUnitCharacteristic = resourceUnitCharacteristics[i];
63                        ResourceUnit resourceUnit = ResourceUnitFactory.createUnit(resourceUnitCharacteristic.getClazz(), this.id,
64                                         Double.valueOf(resourceUnitCharacteristic.getAmount().getContent()).intValue(), 0);
65                        Parameters params = extractParameters(resourceUnitCharacteristic.getParameter());
66                        resourceUnit.init(params);
67                        addResourceUnit(resourceUnit);
68                }
69        }
70
71        private void initProfiles(Profile profile) {
72                if (profile != null) {
73                        initPowerProfile(profile.getPowerProfile());
74                        initAirThroughputProfile(profile.getAirThroughputProfile());
75                }
76        }
77       
78        private void initPowerProfile(schemas.PowerProfile powerProfileCharacteristic) {
79               
80                if (powerProfileCharacteristic != null) {
81                        EnergyEstimationPlugin energyEstimationPlugin = null;
82                        List<PowerState> powerStates = null;
83                        List<PState> pStates = null;
84                        if(powerProfileCharacteristic.getEnergyEstimationPlugin() != null){
85                                String  energyEstimationPluginName = powerProfileCharacteristic.getEnergyEstimationPlugin().getName();
86                                if(energyEstimationPluginName != null) {
87                                        energyEstimationPlugin = (EnergyEstimationPlugin) InstanceFactory.createInstance(
88                                                        energyEstimationPluginName, EnergyEstimationPlugin.class);                     
89                                } else {
90                                        energyEstimationPlugin = new DefaultEnergyEstimationPlugin();
91                                }
92                                Parameters params = extractParameters(powerProfileCharacteristic.getEnergyEstimationPlugin().getParameter());
93                                energyEstimationPlugin.init(params);
94                        }
95                       
96                        if(powerProfileCharacteristic.getPowerStates() != null) {
97                                powerStates = new ArrayList<PowerState>();
98                                int powerStateCount = powerProfileCharacteristic.getPowerStates().getPowerStateCount();
99                                for (int i = 0; i < powerStateCount ; i++) {
100                                        schemas.PowerState ps = powerProfileCharacteristic.getPowerStates().getPowerState(i);
101                                        List<Transition> transitions = new ArrayList<Transition>();
102                                        int transitionCount = ps.getTransitionCount();
103                                        for (int j = 0; j < transitionCount; j++) {
104                                                schemas.Transition t = ps.getTransition(j);
105                                                Transition transition = new Transition(PowerStateNameFactory.createPowerStateName(t.getTo()), t
106                                                                .getPowerUsage().getContent(), t.getTime().getContent());
107                                                Parameters params = extractParameters(t.getParameter());
108                                                transition.init(params);
109                                                transitions.add(transition);
110                                        }
111                                        PowerState powerState = new PowerState(PowerStateNameFactory.createPowerStateName(ps.getName()), ps
112                                                        .getPowerUsage().getContent(), transitions);
113                                        Parameters params = extractParameters(ps.getParameter());
114                                        powerState.init(params);
115                                        powerStates.add(powerState);
116                                }
117                        }
118                       
119                        if(powerProfileCharacteristic.getParameter() != null){
120                                pStates = new ArrayList<PState>();
121                                int parameterCount =  powerProfileCharacteristic.getParameterCount();
122                                for(int i = 0; i < parameterCount; i++){
123                                        schemas.Parameter parameter = powerProfileCharacteristic.getParameter(i);
124                                        if(parameter.getName().equals("pState")){
125                                                PState.Builder builder =  new PState.Builder();
126                                                int propertyCount = parameter.getParameterTypeSequence().getPropertyCount();
127                                                for(int j = 0; j < propertyCount; j++){
128                                                        schemas.Property property = parameter.getParameterTypeSequence().getProperty(j);
129                                                        if(property.getName().equals("name")){
130                                                                 builder = builder.name(property.getStringValueWithUnit(0).getContent());
131                                                        } else if (property.getName().equals("frequency")){
132                                                                 builder = builder.frequency(Double.valueOf(property.getStringValueWithUnit(0).getContent()));
133                                                        } else if (property.getName().equals("voltage")){
134                                                                builder = builder.voltage(Double.valueOf(property.getStringValueWithUnit(0).getContent()));
135                                                        } else if (property.getName().equals("powerUsage")){
136                                                                builder = builder.powerUsage(Double.valueOf(property.getStringValueWithUnit(0).getContent()));
137                                                        }
138                                                }
139                                                PState pState = builder.build();
140                                                pStates.add(pState);
141                                        }
142                                }       
143                        }
144                        this.powerProfile = new PowerProfile(energyEstimationPlugin, powerStates, pStates);
145                        Parameters params = extractParameters(powerProfileCharacteristic.getParameter());
146                        this.powerProfile.init(params);
147                }
148        }
149
150        private void initAirThroughputProfile(schemas.AirThroughputProfile airThroughputProfile) {
151                if (airThroughputProfile != null) {
152                       
153                        AirThroughputEstimationPlugin airThroughputEstimationPlugin = null;
154                        List<AirThroughputState> airThroughputStates = null;
155                        if(airThroughputProfile.getAirThroughputEstimationPlugin() != null){
156                                String airThroughputEstimationPluginName = airThroughputProfile.getAirThroughputEstimationPlugin().getName();
157                                if(airThroughputEstimationPluginName != null) {
158                                        airThroughputEstimationPlugin = (AirThroughputEstimationPlugin) InstanceFactory.createInstance(
159                                                        airThroughputEstimationPluginName, AirThroughputEstimationPlugin.class);                       
160                                } else {
161                                        airThroughputEstimationPlugin = new DefaultAirThroughputEstimationPlugin();
162                                }
163                                Parameters params = extractParameters(airThroughputProfile.getAirThroughputEstimationPlugin().getParameter());
164                                airThroughputEstimationPlugin.init(params);
165                        }
166                        if(airThroughputProfile.getAirThroughputStates() != null){
167                                airThroughputStates = new ArrayList<AirThroughputState>();
168                                int airThrouhputStateCount =  airThroughputProfile.getAirThroughputStates().getAirThroughputStateCount();
169                                for (int i = 0; i < airThrouhputStateCount; i++) {
170                                        schemas.AirThroughputState ats = airThroughputProfile.getAirThroughputStates().getAirThroughputState(i);
171                                        AirThroughputState airThroughputState = new AirThroughputState(ats.getName(), ats.getValue()
172                                                        .getContent(), ats.getPowerUsage().getContent());
173                                        Parameters params = extractParameters(ats.getParameter());
174                                        airThroughputState.init(params);
175                                        airThroughputStates.add(airThroughputState);
176                                }
177                        }
178                        this.airThroughputProfile = new AirThroughputProfile(airThroughputEstimationPlugin, airThroughputStates);
179                        Parameters params = extractParameters(airThroughputProfile.getParameter());
180                        this.airThroughputProfile.init(params);
181                }
182        }
183       
184        private void initLocation(schemas.Location l) {
185                if (location != null) {
186                        this.location = new Location(l.getHorizontal(), l.getVertical(), l.getDepth());
187                        Parameters params = extractParameters(l.getParameter());
188                        this.location.init(params);
189                }
190        }
191
192
193        /*private Properties initProperties(schemas.Parameter[] parameters){
194                Properties prop = new Properties();
195               
196                for(int i = 0; i < parameters.length; i++){
197                        schemas.Parameter parameter = parameters[i];
198                        List values = new ArrayList();
199                        if(parameter.getParameterTypeSequence().getProperty() != null)
200                        {
201                                Map<String, List<StringValueWithUnit>> properties = new HashMap<String, List<StringValueWithUnit>>();
202                                List<StringValueWithUnit> propValues = new ArrayList<StringValueWithUnit>();
203                                for(int j = 0; j < parameter.getParameterTypeSequence().getPropertyCount(); j++){
204                                        schemas.Property property = parameter.getParameterTypeSequence().getProperty(j);
205                                        for(int k = 0; k < property.getStringValueWithUnitCount(); k++){
206                                                propValues.add(property.getStringValueWithUnit(k));
207                                        }
208                                        properties.put(property.getName(), propValues);
209                                }
210                                values.add(properties);
211                        }else {
212                                for(int j = 0; j < parameter.getStringValueWithUnitCount(); j++){
213                                        values.add(parameter.getStringValueWithUnit(j));
214                                }
215                        }
216                        prop.put(parameter.getName(), values);
217                }
218                return prop;
219        }*/
220       
221        private Parameters extractParameters(schemas.Parameter[] parameters){
222               
223                Parameters params = null;
224               
225                if(parameters.length != 0)
226                        params = new Parameters();
227               
228                for(int i = 0; i < parameters.length; i++){
229                        schemas.Parameter parameter = parameters[i];
230                        Parameter param = new Parameter(parameter.getName());
231                        if(parameter.getParameterTypeSequence() != null && parameter.getParameterTypeSequence().getProperty() != null)
232                        {                                                       
233                                int propertyCount = parameter.getParameterTypeSequence().getPropertyCount();
234                                for(int j = 0; j < propertyCount; j++){
235                                        schemas.Property property = parameter.getParameterTypeSequence().getProperty(j);
236                                        Property prop = new Property(property.getName());
237                                        int stringValueWithUnitCount = property.getStringValueWithUnitCount();
238                                        for(int k = 0; k < stringValueWithUnitCount; k++){
239                                                prop.add(property.getStringValueWithUnit(k));
240                                        }
241                                        param.addProperty(prop);
242                                }
243                        } else {
244                                int stringValueWithUnitCount =  parameter.getStringValueWithUnitCount();
245                                for(int j = 0; j < stringValueWithUnitCount; j++){
246                                        param.add(parameter.getStringValueWithUnit(j));
247                                }
248                        }
249                        params.put(parameter.getName(), param);
250                }
251                return params;
252        }
253
254        public PowerProfile getPowerProfile() {
255                return powerProfile;
256        }
257
258        public AirThroughputProfile getAirThroughputProfile() {
259                return airThroughputProfile;
260        }
261
262        public Location getLocation() {
263                return location;
264        }
265
266        public String getCategory() {
267                return category;
268        }
269
270}
Note: See TracBrowser for help on using the repository browser.