| 1 | package de.uka.ipd.sdq.dsexplore.helper; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.List; |
| 5 | |
| 6 | import org.apache.log4j.Logger; |
| 7 | import org.eclipse.emf.ecore.EObject; |
| 8 | import org.eclipse.emf.ecore.util.EcoreUtil; |
| 9 | import org.eclipse.emf.ecore.util.EcoreUtil.EqualityHelper; |
| 10 | |
| 11 | import de.uka.ipd.sdq.dsexplore.opt4j.start.Opt4JStarter; |
| 12 | import de.uka.ipd.sdq.pcm.allocation.AllocationContext; |
| 13 | import de.uka.ipd.sdq.pcm.core.composition.AssemblyContext; |
| 14 | import de.uka.ipd.sdq.pcm.core.entity.Entity; |
| 15 | import de.uka.ipd.sdq.pcm.designdecision.AllocationDegree; |
| 16 | import de.uka.ipd.sdq.pcm.designdecision.AssembledComponentDegree; |
| 17 | import de.uka.ipd.sdq.pcm.designdecision.Candidate; |
| 18 | import de.uka.ipd.sdq.pcm.designdecision.Candidates; |
| 19 | import de.uka.ipd.sdq.pcm.designdecision.CapacityDegree; |
| 20 | import de.uka.ipd.sdq.pcm.designdecision.Choice; |
| 21 | import de.uka.ipd.sdq.pcm.designdecision.ClassChoice; |
| 22 | import de.uka.ipd.sdq.pcm.designdecision.ClassDegree; |
| 23 | import de.uka.ipd.sdq.pcm.designdecision.DecisionSpace; |
| 24 | import de.uka.ipd.sdq.pcm.designdecision.DegreeOfFreedomInstance; |
| 25 | import de.uka.ipd.sdq.pcm.designdecision.ExchangeComponentRule; |
| 26 | import de.uka.ipd.sdq.pcm.designdecision.ProcessingResourceDegree; |
| 27 | import de.uka.ipd.sdq.pcm.designdecision.ResourceContainerReplicationDegree; |
| 28 | import de.uka.ipd.sdq.pcm.designdecision.ResourceContainerReplicationDegreeWithComponentChange; |
| 29 | import de.uka.ipd.sdq.pcm.designdecision.util.designdecisionSwitch; |
| 30 | import de.uka.ipd.sdq.pcm.repository.BasicComponent; |
| 31 | import de.uka.ipd.sdq.pcm.repository.PassiveResource; |
| 32 | import de.uka.ipd.sdq.pcm.repository.Repository; |
| 33 | import de.uka.ipd.sdq.pcm.repository.RepositoryComponent; |
| 34 | import de.uka.ipd.sdq.pcm.resourceenvironment.ProcessingResourceSpecification; |
| 35 | import de.uka.ipd.sdq.pcm.resourceenvironment.ResourceContainer; |
| 36 | import de.uka.ipd.sdq.pcm.resourcetype.ProcessingResourceType; |
| 37 | import de.uka.ipd.sdq.pcmsolver.models.PCMInstance; |
| 38 | |
| 39 | /** |
| 40 | * This class fixes the references of designdecision models. The problem |
| 41 | * addressed is, that if you separately load a PCM model and a designdecision |
| 42 | * model, the object identities of the loaded elements are not the same. For |
| 43 | * example, a basic component A may be represented by a Java object A1 when |
| 44 | * loaded directly from the repository file. When a degree of freedom model is |
| 45 | * loaded, and the PCM instance is accessed usingthe references in that model, |
| 46 | * the PCM model is loaded again and the component A is represented by a second |
| 47 | * Java object A2. Calling equals on A1 and A2 results in false. Thus, |
| 48 | * collection.contains(A1) will fail even if A2 is contained. |
| 49 | * |
| 50 | * Thus, this class fixes the references by replacing each reference to A2 to |
| 51 | * a reference to A1, so that only one consistent object tree represents the |
| 52 | * models at the end. |
| 53 | * |
| 54 | * XXX: Maybe this could be done more generically based on EObject and the structural |
| 55 | * features, without being meta model dependent. |
| 56 | * |
| 57 | * One solution idea was to just use the same EMF resource set when loading more models |
| 58 | * and then resolve proxies using ECoreUtil#resolveAll. Although this worked before 2011-02-24, |
| 59 | * it did not work anymore afterwards, so I reenabled this switch. |
| 60 | * |
| 61 | * @author martens |
| 62 | */ |
| 63 | public class FixDesignDecisionReferenceSwitch extends designdecisionSwitch<EObject> { |
| 64 | |
| 65 | protected static Logger logger = Logger |
| 66 | .getLogger(FixDesignDecisionReferenceSwitch.class.getName()); |
| 67 | |
| 68 | public void fixEntitiesForDomain(List<EObject> eListToUpdate, List<Entity> memoryEntityList) { |
| 69 | |
| 70 | List<Entity> newList = new ArrayList<Entity>(); |
| 71 | |
| 72 | for (EObject entity : eListToUpdate) { |
| 73 | Entity rightOne = EMFHelper.retrieveEntityByID(memoryEntityList, entity); |
| 74 | newList.add(rightOne); |
| 75 | } |
| 76 | |
| 77 | eListToUpdate.clear(); |
| 78 | eListToUpdate.addAll(newList); |
| 79 | } |
| 80 | |
| 81 | private PCMInstance initialInstance; |
| 82 | |
| 83 | public FixDesignDecisionReferenceSwitch(PCMInstance initialInstance2) { |
| 84 | this.initialInstance = initialInstance2; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public EObject caseAllocationDegree(AllocationDegree object) { |
| 89 | String id = ((AllocationContext)object.getPrimaryChanged()).getId(); |
| 90 | List<AllocationContext> acs = this.initialInstance.getAllocation().getAllocationContexts_Allocation(); |
| 91 | AllocationContext rightOne = (AllocationContext)EMFHelper.retrieveEntityByID(acs,id); |
| 92 | |
| 93 | if (rightOne == null){ |
| 94 | throw new RuntimeException("Cannot find AllocationContext "+id+" in the specified PCM ALlocation Model. Maybe the design decision file does not match the analysed PCM instance?"); |
| 95 | } |
| 96 | |
| 97 | object.setPrimaryChanged(rightOne); |
| 98 | |
| 99 | |
| 100 | List<Entity> allCurrentServers = new ArrayList<Entity>(); |
| 101 | allCurrentServers.addAll(this.initialInstance.getResourceEnvironment().getResourceContainer_ResourceEnvironment()); |
| 102 | |
| 103 | fixEntitiesForDomain(object.getClassDesignOptions(), allCurrentServers); |
| 104 | |
| 105 | return object; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public EObject caseCandidate(Candidate object) { |
| 110 | List<Choice> choices = object.getChoices(); |
| 111 | for (Choice choice : choices) { |
| 112 | doSwitch(choice); |
| 113 | } |
| 114 | |
| 115 | return object; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public EObject caseCandidates(Candidates object) { |
| 120 | |
| 121 | object.setProblem(Opt4JStarter.getProblem().getEMFProblem()); |
| 122 | |
| 123 | for (Candidate candidate: object.getCandidate()) { |
| 124 | doSwitch(candidate); |
| 125 | } |
| 126 | return object; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public EObject caseChoice(Choice object) { |
| 131 | |
| 132 | DecisionSpace inMemoryProblem = Opt4JStarter.getProblem().getEMFProblem(); |
| 133 | |
| 134 | DegreeOfFreedomInstance originalDegree = object.getDegreeOfFreedomInstance(); |
| 135 | doSwitch(originalDegree); |
| 136 | |
| 137 | boolean foundDegree = false; |
| 138 | |
| 139 | for (DegreeOfFreedomInstance inMemoryDegree : inMemoryProblem.getDegreesOfFreedom()) { |
| 140 | // Need to initialize this every time anew, because otherwise it will remember old comparisons and compares with previous matches. |
| 141 | EqualityHelper equalityHelper = new EqualityHelper(); |
| 142 | if (equalityHelper.equals(inMemoryDegree, originalDegree)){ |
| 143 | object.setDegreeOfFreedomInstance(inMemoryDegree); |
| 144 | foundDegree = true; |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (!foundDegree){ |
| 150 | throw new RuntimeException("Fixing design decision references failed, could not find in memory degree for "+object.getDegreeOfFreedomInstance()); |
| 151 | } |
| 152 | return object; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public EObject caseClassChoice(ClassChoice object) { |
| 157 | |
| 158 | //First need to fix the references of the degrees of freedom |
| 159 | caseChoice(object); |
| 160 | |
| 161 | // Fix the chosen entity. Is one of the degree of freedoms domain (which must have been fixed before). |
| 162 | DegreeOfFreedomInstance degree = object.getDegreeOfFreedomInstance(); |
| 163 | if (degree instanceof ClassDegree){ |
| 164 | ClassDegree enumDegree = (ClassDegree)degree; |
| 165 | Entity inMemoryEntity = EMFHelper.retrieveEntityByID(enumDegree.getClassDesignOptions(), object.getChosenValue()); |
| 166 | |
| 167 | if (inMemoryEntity == null){ |
| 168 | throw new RuntimeException("Cannot find Entity "+((Entity)object.getChosenValue()).getId()+" in the specified PCM ALlocation Model. Maybe the design decision file does not match the analysed PCM instance?"); |
| 169 | } |
| 170 | |
| 171 | object.setChosenValue(inMemoryEntity); |
| 172 | } else { |
| 173 | throw new RuntimeException("Invalid enumeration choice encountered: Referenced degree of freedom must be of type ClassDegree."); |
| 174 | } |
| 175 | |
| 176 | return object; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public EObject caseAssembledComponentDegree( |
| 181 | AssembledComponentDegree object) { |
| 182 | |
| 183 | String id = ((Entity)object.getPrimaryChanged()).getId(); |
| 184 | List<AssemblyContext> acs = this.initialInstance.getSystem().getAssemblyContexts__ComposedStructure(); |
| 185 | AssemblyContext rightOne = (AssemblyContext)EMFHelper.retrieveEntityByID(acs, id); |
| 186 | |
| 187 | if (rightOne == null){ |
| 188 | throw new RuntimeException("Cannot find AssemblyContext "+id+" in the specified PCM ALlocation Model. Maybe the design decision file does not match the analysed PCM instance?"); |
| 189 | } |
| 190 | |
| 191 | object.setPrimaryChanged(rightOne); |
| 192 | |
| 193 | List<Entity> allCurrentComponents = new ArrayList<Entity>(); |
| 194 | List<Repository> repositories = this.initialInstance.getRepositories(); |
| 195 | for (Repository repository : repositories) { |
| 196 | allCurrentComponents.addAll(repository.getComponents__Repository()); |
| 197 | } |
| 198 | |
| 199 | fixEntitiesForDomain(object.getClassDesignOptions(), allCurrentComponents); |
| 200 | |
| 201 | return object; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | @Override |
| 206 | public EObject caseDecisionSpace(DecisionSpace object) { |
| 207 | |
| 208 | try { |
| 209 | for (DegreeOfFreedomInstance dd : object.getDegreesOfFreedom()) { |
| 210 | doSwitch(dd); |
| 211 | }; |
| 212 | } catch (ClassCastException e){ |
| 213 | logger.error("Class cast exception when visiting .designdecision model. Please check your model for validity using the Ecore tree editor. References might be broken."); |
| 214 | throw e; |
| 215 | } |
| 216 | return object; |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public EObject caseProcessingResourceDegree(ProcessingResourceDegree object) { |
| 221 | String id = ((ResourceContainer)object.getPrimaryChanged()).getId(); |
| 222 | |
| 223 | List<ResourceContainer> rcs = this.initialInstance.getResourceEnvironment().getResourceContainer_ResourceEnvironment(); |
| 224 | ResourceContainer rightOne = (ResourceContainer)EMFHelper.retrieveEntityByID(rcs, id); |
| 225 | |
| 226 | if (rightOne == null){ |
| 227 | throw new RuntimeException("Cannot find ResourceContainer "+id+" in the specified PCM ALlocation Model. Maybe the design decision file does not match the analysed PCM instance?"); |
| 228 | } |
| 229 | |
| 230 | object.setPrimaryChanged(rightOne); |
| 231 | |
| 232 | ProcessingResourceType originalType = object.getProcessingresourcetype(); |
| 233 | List<ProcessingResourceSpecification> resources = rightOne.getActiveResourceSpecifications_ResourceContainer(); |
| 234 | boolean foundResourceType = false; |
| 235 | for (ProcessingResourceSpecification resource : resources) { |
| 236 | if (EMFHelper.checkIdentity(resource.getActiveResourceType_ActiveResourceSpecification(), originalType)){ |
| 237 | object.setProcessingresourcetype(resource.getActiveResourceType_ActiveResourceSpecification()); |
| 238 | foundResourceType = true; |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | if (!foundResourceType){ |
| 243 | throw new RuntimeException("Fixing design decision references failed, could not find in memory resource type for "+object.getProcessingresourcetype()); |
| 244 | } |
| 245 | |
| 246 | return object; |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public EObject caseCapacityDegree(CapacityDegree object) { |
| 251 | |
| 252 | String id = ((Entity)object.getPrimaryChanged()).getId(); |
| 253 | |
| 254 | List<Repository> repos = this.initialInstance.getRepositories(); |
| 255 | for (Repository repository : repos) { |
| 256 | List<RepositoryComponent> components = repository.getComponents__Repository(); |
| 257 | for (RepositoryComponent component : components) { |
| 258 | if (component instanceof BasicComponent){ |
| 259 | List<PassiveResource> prs = ((BasicComponent)component).getPassiveResource_BasicComponent(); |
| 260 | PassiveResource rightOne = (PassiveResource)EMFHelper.retrieveEntityByID(prs, id); |
| 261 | if (rightOne != null){ |
| 262 | object.setPrimaryChanged(rightOne); |
| 263 | return object; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | //logger.warn("Could not find "+object.getPrimaryChanged().getEntityName()+" in design decision reference switch, errors may occur."); |
| 269 | throw new RuntimeException("Cannot find PassiveResource "+id+" in the specified PCM ALlocation Model. Maybe the design decision file does not match the analysed PCM instance?"); |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public EObject caseResourceContainerReplicationDegree( |
| 274 | ResourceContainerReplicationDegree object) { |
| 275 | String id = ((Entity)object.getPrimaryChanged()).getId(); |
| 276 | List<ResourceContainer> acs = this.initialInstance.getResourceEnvironment().getResourceContainer_ResourceEnvironment(); |
| 277 | ResourceContainer rightOne = (ResourceContainer)EMFHelper.retrieveEntityByID(acs,id); |
| 278 | |
| 279 | if (rightOne == null){ |
| 280 | throw new RuntimeException("Cannot find ResourceContainer "+id+" in the specified PCM ResourceEnvironment Model. Maybe the design decision file does not match the analysed PCM instance?"); |
| 281 | } |
| 282 | |
| 283 | object.setPrimaryChanged(rightOne); |
| 284 | |
| 285 | return object; |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public EObject caseResourceContainerReplicationDegreeWithComponentChange( |
| 290 | ResourceContainerReplicationDegreeWithComponentChange object) { |
| 291 | |
| 292 | caseResourceContainerReplicationDegree(object); |
| 293 | |
| 294 | for (ExchangeComponentRule rule : object.getExchangeComponentRule()) { |
| 295 | String id = rule.getAllocationContext().getId(); |
| 296 | |
| 297 | List<AllocationContext> acs = this.initialInstance.getAllocation().getAllocationContexts_Allocation(); |
| 298 | AllocationContext rightOne = (AllocationContext)EMFHelper.retrieveEntityByID(acs, id); |
| 299 | rule.setAllocationContext(rightOne); |
| 300 | |
| 301 | List<Entity> allCurrentComponents = new ArrayList<Entity>(); |
| 302 | List<Repository> repositories = this.initialInstance.getRepositories(); |
| 303 | for (Repository repository : repositories) { |
| 304 | allCurrentComponents.addAll(repository.getComponents__Repository()); |
| 305 | } |
| 306 | |
| 307 | List<RepositoryComponent> newList = new ArrayList<RepositoryComponent>(); |
| 308 | |
| 309 | for (RepositoryComponent entity : rule.getRepositoryComponent()) { |
| 310 | RepositoryComponent rightOne2 = (RepositoryComponent)EMFHelper.retrieveEntityByID(allCurrentComponents, entity); |
| 311 | newList.add(rightOne2); |
| 312 | } |
| 313 | |
| 314 | rule.getRepositoryComponent().clear(); |
| 315 | rule.getRepositoryComponent().addAll(newList); |
| 316 | } |
| 317 | |
| 318 | return object; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | } |