| 1 | package de.uka.ipd.sdq.codegen.simucontroller.workflow.jobs; |
| 2 | |
| 3 | import java.util.Iterator; |
| 4 | import java.util.List; |
| 5 | |
| 6 | import org.apache.log4j.Logger; |
| 7 | import org.eclipse.core.runtime.CoreException; |
| 8 | import org.eclipse.core.runtime.IProgressMonitor; |
| 9 | import org.eclipse.emf.common.util.URI; |
| 10 | import org.eclipse.emf.ecore.EClass; |
| 11 | import org.eclipse.emf.ecore.EObject; |
| 12 | import org.eclipse.emf.ecore.resource.Resource; |
| 13 | import org.eclipse.emf.ecore.resource.ResourceSet; |
| 14 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| 15 | |
| 16 | import de.uka.ipd.sdq.codegen.simucontroller.runconfig.AbstractSimulationWorkflowConfiguration; |
| 17 | import de.uka.ipd.sdq.pcm.core.PCMRandomVariable; |
| 18 | import de.uka.ipd.sdq.pcm.core.entity.Entity; |
| 19 | import de.uka.ipd.sdq.pcm.usagemodel.ClosedWorkload; |
| 20 | import de.uka.ipd.sdq.pcm.usagemodel.OpenWorkload; |
| 21 | import de.uka.ipd.sdq.workflow.IBlackboardInteractingJob; |
| 22 | import de.uka.ipd.sdq.workflow.exceptions.JobFailedException; |
| 23 | import de.uka.ipd.sdq.workflow.exceptions.RollbackFailedException; |
| 24 | import de.uka.ipd.sdq.workflow.exceptions.UserCanceledException; |
| 25 | import de.uka.ipd.sdq.workflow.mdsd.blackboard.MDSDBlackboard; |
| 26 | import de.uka.ipd.sdq.workflow.pcm.blackboard.PCMResourceSetPartition; |
| 27 | import de.uka.ipd.sdq.workflow.pcm.jobs.LoadPCMModelsIntoBlackboardJob; |
| 28 | import de.uka.ipd.sdq.workflow.pcm.runconfig.SensitivityAnalysisConfiguration; |
| 29 | |
| 30 | public class TransformPCMForSensitivityAnalysisJob implements |
| 31 | IBlackboardInteractingJob<MDSDBlackboard> { |
| 32 | |
| 33 | private MDSDBlackboard blackboard; |
| 34 | private Logger logger = Logger |
| 35 | .getLogger(TransformPCMForSensitivityAnalysisJob.class); |
| 36 | private List<SensitivityAnalysisConfiguration> sconfList; |
| 37 | |
| 38 | public TransformPCMForSensitivityAnalysisJob( |
| 39 | AbstractSimulationWorkflowConfiguration configuration) throws CoreException { |
| 40 | super(); |
| 41 | this.sconfList = configuration.getSensitivityAnalysisConfigurations(); |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * (non-Javadoc) |
| 46 | * |
| 47 | * @see |
| 48 | * de.uka.ipd.sdq.codegen.workflow.IBlackboardInteractingJob#setBlackbard |
| 49 | * (de.uka.ipd.sdq.codegen.workflow.Blackboard) |
| 50 | */ |
| 51 | public void setBlackboard(MDSDBlackboard blackboard) { |
| 52 | this.blackboard = blackboard; |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * (non-Javadoc) |
| 57 | * |
| 58 | * @see |
| 59 | * de.uka.ipd.sdq.codegen.workflow.IJob#execute(org.eclipse.core.runtime |
| 60 | * .IProgressMonitor) |
| 61 | */ |
| 62 | public void execute(IProgressMonitor monitor) throws JobFailedException, |
| 63 | UserCanceledException { |
| 64 | PCMResourceSetPartition pcmPartition = (PCMResourceSetPartition) this.blackboard |
| 65 | .getPartition(LoadPCMModelsIntoBlackboardJob.PCM_MODELS_PARTITION_ID); |
| 66 | |
| 67 | for (SensitivityAnalysisConfiguration sconf : this.sconfList){ |
| 68 | applyParamterChange(sconf, pcmPartition); |
| 69 | } |
| 70 | |
| 71 | } |
| 72 | |
| 73 | private void applyParamterChange(SensitivityAnalysisConfiguration sconf, PCMResourceSetPartition pcmPartition) |
| 74 | throws JobFailedException { |
| 75 | if (sconf.getVariable() != null && sconf.getVariable().equals("")) |
| 76 | throw new JobFailedException( |
| 77 | "Bad Sensitivity Analysis Job Configuration, no variable specified."); |
| 78 | |
| 79 | URI uri = URI.createURI(sconf.getVariable()); |
| 80 | |
| 81 | ResourceSet rs = new ResourceSetImpl(); |
| 82 | Resource resource = rs.getResource(uri, true); |
| 83 | String uriString = sconf.getVariable(); |
| 84 | String varUri = uriString.substring(uriString.indexOf("#")+1); |
| 85 | EObject variableToChange = resource.getEObject(varUri); |
| 86 | |
| 87 | EObject o = null; |
| 88 | for (Resource r : pcmPartition.getResourceSet().getResources()) { |
| 89 | if (o == null) { |
| 90 | o = findEObject(r.getContents().get(0), variableToChange); |
| 91 | if (o != null) { |
| 92 | logger |
| 93 | .info("Altering PCM variable for sensitivity analysis"); |
| 94 | alterObject(o, sconf); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | if (o == null) |
| 99 | throw new JobFailedException( |
| 100 | "Sensistivity analaysis transformation was unable to find the sensitivity variable"); |
| 101 | } |
| 102 | |
| 103 | public void rollback(IProgressMonitor monitor) |
| 104 | throws RollbackFailedException { |
| 105 | } |
| 106 | |
| 107 | public String getName() { |
| 108 | return "Inplace Transformation job for Sensitivity Analysis"; |
| 109 | } |
| 110 | |
| 111 | // --- |
| 112 | |
| 113 | private void alterObject(EObject o, SensitivityAnalysisConfiguration sconf) { |
| 114 | //DecimalFormat df = new DecimalFormat("0.00000000", new DecimalFormatSymbols(Locale.US)); |
| 115 | //String value = df.format( sconf.getCurrent() ); |
| 116 | |
| 117 | double value = sconf.getCurrent(); |
| 118 | |
| 119 | if (o instanceof PCMRandomVariable) { |
| 120 | PCMRandomVariable pcmRandVar = (PCMRandomVariable) o; |
| 121 | if ((int)value == value){ |
| 122 | pcmRandVar.setSpecification(String.valueOf((int)value)); |
| 123 | } else { |
| 124 | pcmRandVar.setSpecification(String.valueOf(value)); |
| 125 | } |
| 126 | |
| 127 | } |
| 128 | if (o instanceof ClosedWorkload) { |
| 129 | ClosedWorkload cw = (ClosedWorkload) o; |
| 130 | cw.setPopulation((int)sconf.getCurrent()); |
| 131 | } |
| 132 | if (o instanceof OpenWorkload) { |
| 133 | OpenWorkload ow = (OpenWorkload) o; |
| 134 | ow.getInterArrivalTime_OpenWorkload().setSpecification("Exp(" + value + ")"); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Finds object in tree based on the {@link #entityBasedEquals(EObject, EObject)} equality definition. |
| 140 | * |
| 141 | * @param objectTreeToSearchIn |
| 142 | * @param eObjectToFind |
| 143 | * @return |
| 144 | */ |
| 145 | private EObject findEObject(EObject objectTreeToSearchIn, EObject eObjectToFind) { |
| 146 | Iterator<EObject> it = objectTreeToSearchIn.eAllContents(); |
| 147 | |
| 148 | while (it.hasNext()) { |
| 149 | EObject eo = it.next(); |
| 150 | |
| 151 | if (entityBasedEquals(eo, eObjectToFind)) { |
| 152 | return eo; |
| 153 | } |
| 154 | } |
| 155 | return null; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Finds object based on the following recursive equality definition: |
| 160 | * |
| 161 | * Two objects are equal iff (1) they have both instance of {@link Entity} |
| 162 | * and have the same id, or (2) they are not both instances of |
| 163 | * {@link Entity} and they are instances of the same {@link EClass} and |
| 164 | * their parents are of equal {@link EClass} and their parents are equal |
| 165 | * based on this definition. |
| 166 | */ |
| 167 | private boolean entityBasedEquals(EObject eObject1, EObject eObject2) { |
| 168 | |
| 169 | if (eObject1 == eObject2){ |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | if (eObject1 != null && eObject2 != null){ |
| 174 | |
| 175 | if (eObject1 instanceof Entity && eObject2 instanceof Entity) { |
| 176 | Entity entity1 = (Entity) eObject1; |
| 177 | Entity entity2 = (Entity) eObject2; |
| 178 | |
| 179 | if (entity1.getId().equals(entity2.getId())) { |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | } else { |
| 184 | |
| 185 | //they must have the same class. |
| 186 | EClass eClass1 = eObject1.eClass(); |
| 187 | if (eClass1 != eObject2.eClass()){ |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | // objects could be the same, check parents |
| 192 | EObject parent1 = eObject1.eContainer(); |
| 193 | EObject parent2 = eObject2.eContainer(); |
| 194 | |
| 195 | return entityBasedEquals(parent1, parent2); |
| 196 | |
| 197 | } |
| 198 | |
| 199 | |
| 200 | } |
| 201 | |
| 202 | return false; |
| 203 | |
| 204 | } |
| 205 | |
| 206 | } |