| 1 | package de.uka.ipd.sdq.dsexplore.opt4j.optimizer.heuristic.startingPopulation; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | |
| 6 | import org.eclipse.emf.ecore.EObject; |
| 7 | |
| 8 | import de.uka.ipd.sdq.dsexplore.launch.DSEWorkflowConfiguration; |
| 9 | import de.uka.ipd.sdq.dsexplore.opt4j.start.Opt4JStarter; |
| 10 | import de.uka.ipd.sdq.pcm.allocation.AllocationContext; |
| 11 | import de.uka.ipd.sdq.pcm.core.entity.Entity; |
| 12 | import de.uka.ipd.sdq.pcm.designdecision.AllocationDegree; |
| 13 | import de.uka.ipd.sdq.pcm.designdecision.DegreeOfFreedomInstance; |
| 14 | import de.uka.ipd.sdq.pcm.resourceenvironment.ResourceContainer; |
| 15 | |
| 16 | /** |
| 17 | * Class defines methods to get all resource containers and allocation contexts |
| 18 | * @author Tom Beyer |
| 19 | * |
| 20 | */ |
| 21 | public abstract class AbstractStartingPopulationHeuristic implements IStartingPoulationHeuristic { |
| 22 | |
| 23 | /** |
| 24 | * Resource containers found by getResourceContainers(). Caching used to here to avoid |
| 25 | * Unnecessary calculations |
| 26 | */ |
| 27 | ArrayList<ResourceContainer> resourceContainers; |
| 28 | |
| 29 | /** |
| 30 | * Allocation contexts found by getAllocationContexts(). Caching used to here to avoid |
| 31 | * Unnecessary calculations |
| 32 | */ |
| 33 | ArrayList<AllocationContext> allocationContexts; |
| 34 | |
| 35 | public AbstractStartingPopulationHeuristic(DSEWorkflowConfiguration configuration) { |
| 36 | super(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @return All resource containers based on the current Opt4J Problem (via Opt4JStarter.getProblem()) |
| 41 | */ |
| 42 | protected ArrayList<ResourceContainer> getResourceContainers() { |
| 43 | if (resourceContainers != null) { |
| 44 | return resourceContainers; |
| 45 | } else { |
| 46 | ArrayList<ResourceContainer> resourceContainers = new ArrayList<ResourceContainer>(); |
| 47 | |
| 48 | Collection<DegreeOfFreedomInstance> degreesOfFreedom = Opt4JStarter.getProblem().getDesignDecisions(); |
| 49 | for (DegreeOfFreedomInstance DegreeOfFreedomInstance : degreesOfFreedom) { |
| 50 | if (DegreeOfFreedomInstance instanceof AllocationDegree) { |
| 51 | AllocationDegree allocationDegree = (AllocationDegree) DegreeOfFreedomInstance; |
| 52 | for (EObject entity : allocationDegree.getClassDesignOptions()) { |
| 53 | if (entity instanceof ResourceContainer) { |
| 54 | if (!resourceContainers.contains(entity)) { |
| 55 | resourceContainers.add((ResourceContainer) entity); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | this.resourceContainers = resourceContainers; |
| 62 | return resourceContainers; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @return All allocation contexts based on the current Opt4J Problem (via Opt4JStarter.getProblem()) |
| 68 | */ |
| 69 | protected ArrayList<AllocationContext> getAllocationContexts() { |
| 70 | if (allocationContexts != null) { |
| 71 | return allocationContexts; |
| 72 | } else { |
| 73 | ArrayList<AllocationContext> allocationContexts = new ArrayList<AllocationContext>(); |
| 74 | Collection<DegreeOfFreedomInstance> degreesOfFreedom = Opt4JStarter.getProblem().getDesignDecisions(); |
| 75 | for (DegreeOfFreedomInstance DegreeOfFreedomInstance : degreesOfFreedom) { |
| 76 | if (DegreeOfFreedomInstance instanceof AllocationDegree) { |
| 77 | AllocationDegree allocationDegree = (AllocationDegree) DegreeOfFreedomInstance; |
| 78 | EObject entity = allocationDegree.getPrimaryChanged(); |
| 79 | if (entity instanceof AllocationContext) { |
| 80 | if (!allocationContexts.contains(entity)) { |
| 81 | allocationContexts.add((AllocationContext) entity); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | this.allocationContexts = allocationContexts; |
| 87 | return allocationContexts; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | } |