[477] | 1 | package simulator.reader; |
---|
| 2 | |
---|
| 3 | import java.util.ArrayList; |
---|
| 4 | |
---|
| 5 | import org.qcg.broker.schemas.exception.UnknownParameter; |
---|
| 6 | |
---|
| 7 | import schedframe.Parameter; |
---|
| 8 | import schedframe.Parameters; |
---|
| 9 | import schedframe.Property; |
---|
| 10 | import schemas.ComputingResource; |
---|
| 11 | import schemas.ComputingResourceTemplate; |
---|
| 12 | import schemas.ComputingResourceTypeChoiceSequence; |
---|
| 13 | import schemas.Environment; |
---|
| 14 | import schemas.Resources; |
---|
| 15 | import schemas.Templates; |
---|
| 16 | import schemas.TimeEstimationPlugin; |
---|
| 17 | |
---|
| 18 | public class EnvironmentWrapper { |
---|
| 19 | |
---|
| 20 | Environment environment; |
---|
| 21 | |
---|
| 22 | public void wrap(Environment environment) { |
---|
| 23 | this.environment = environment; |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | public Templates getTemplates() throws UnknownParameter{ |
---|
| 27 | if(environment == null) |
---|
| 28 | throw new UnknownParameter("Environment parameters are not defined."); |
---|
| 29 | return environment.getTemplates(); |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | public ComputingResourceTemplate getTemplate(String templateId) throws UnknownParameter{ |
---|
| 33 | Templates templates = getTemplates() ; |
---|
| 34 | |
---|
| 35 | if(templates == null) |
---|
| 36 | return null; |
---|
| 37 | |
---|
| 38 | for (int i = 0; i < templates.getComputingResourceTemplateCount(); i++){ |
---|
| 39 | schemas.ComputingResourceTemplate template = templates.getComputingResourceTemplate(i); |
---|
| 40 | if(template.getName().equals(templateId)){ |
---|
| 41 | return template; |
---|
| 42 | } |
---|
| 43 | } |
---|
| 44 | return null; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | public Resources getResources() throws UnknownParameter{ |
---|
| 48 | if(environment == null) |
---|
| 49 | throw new UnknownParameter("Environment parameters are not defined."); |
---|
| 50 | return environment.getResources(); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | public ComputingResource[] getComputingResources() throws UnknownParameter{ |
---|
| 54 | |
---|
| 55 | ArrayList<ComputingResource>tab = null; |
---|
| 56 | |
---|
| 57 | if(environment == null) |
---|
| 58 | throw new UnknownParameter("Environment parameters are not defined."); |
---|
| 59 | Resources resources = getResources(); |
---|
| 60 | if(resources == null) |
---|
| 61 | return null; |
---|
| 62 | |
---|
| 63 | tab = new ArrayList<ComputingResource>(); |
---|
| 64 | |
---|
| 65 | for(int i = 0; i < resources.getComputingResourceCount(); i++){ |
---|
| 66 | ComputingResource compRes = resources.getComputingResource(i); |
---|
| 67 | tab.add(compRes); |
---|
| 68 | } |
---|
| 69 | if(tab.size() == 0) |
---|
| 70 | return null; |
---|
| 71 | else |
---|
| 72 | return tab.toArray(new ComputingResource[0]); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | public TimeEstimationPlugin getTimeEstimationPlugin() throws UnknownParameter{ |
---|
| 76 | if(environment == null) |
---|
| 77 | throw new UnknownParameter("Environment parameters are not defined."); |
---|
| 78 | return environment.getTimeEstimationPlugin(); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | public boolean initWithCompResTemplate(ComputingResource compRes, ComputingResourceTemplate template){ |
---|
| 82 | |
---|
| 83 | compRes.setClazz(template.getClazz()); |
---|
| 84 | |
---|
| 85 | compRes.setComputingResourceTypeChoiceSequence(new ComputingResourceTypeChoiceSequence()); |
---|
| 86 | compRes.getComputingResourceTypeChoiceSequence().setComputingResource(template.getComputingResource()); |
---|
| 87 | compRes.getComputingResourceTypeChoiceSequence().setResourceUnit(template.getResourceUnit()); |
---|
| 88 | compRes.getComputingResourceTypeChoiceSequence().setProfile(template.getProfile()); |
---|
| 89 | compRes.getComputingResourceTypeChoiceSequence().setParameter(template.getParameter()); |
---|
| 90 | |
---|
| 91 | return true; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | public static Parameters extractParameters(schemas.Parameter[] parameters){ |
---|
| 95 | |
---|
| 96 | Parameters params = null; |
---|
| 97 | |
---|
| 98 | if(parameters.length != 0) |
---|
| 99 | params = new Parameters(); |
---|
| 100 | |
---|
| 101 | for(int i = 0; i < parameters.length; i++){ |
---|
| 102 | schemas.Parameter parameter = parameters[i]; |
---|
| 103 | Parameter param = new Parameter(parameter.getName()); |
---|
| 104 | if(parameter.getParameterTypeSequence() != null && parameter.getParameterTypeSequence().getProperty() != null) |
---|
| 105 | { |
---|
| 106 | int propertyCount = parameter.getParameterTypeSequence().getPropertyCount(); |
---|
| 107 | for(int j = 0; j < propertyCount; j++){ |
---|
| 108 | schemas.Property property = parameter.getParameterTypeSequence().getProperty(j); |
---|
| 109 | Property prop = new Property(property.getName()); |
---|
| 110 | int stringValueWithUnitCount = property.getStringValueWithUnitCount(); |
---|
| 111 | for(int k = 0; k < stringValueWithUnitCount; k++){ |
---|
| 112 | prop.add(property.getStringValueWithUnit(k)); |
---|
| 113 | } |
---|
| 114 | param.addProperty(prop); |
---|
| 115 | } |
---|
| 116 | } else { |
---|
| 117 | int stringValueWithUnitCount = parameter.getStringValueWithUnitCount(); |
---|
| 118 | for(int j = 0; j < stringValueWithUnitCount; j++){ |
---|
| 119 | param.add(parameter.getStringValueWithUnit(j)); |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | params.put(parameter.getName(), param); |
---|
| 123 | } |
---|
| 124 | return params; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | } |
---|