1 | package de.uka.ipd.sdq.simucomframework.resources; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.Collection; |
5 | import java.util.HashMap; |
6 | import java.util.Iterator; |
7 | import java.util.List; |
8 | |
9 | import org.apache.log4j.Logger; |
10 | |
11 | import de.uka.ipd.sdq.scheduler.ISchedulableProcess; |
12 | import de.uka.ipd.sdq.simucomframework.SimuComSimProcess; |
13 | import de.uka.ipd.sdq.simucomframework.exceptions.ResourceContainerIsMissingRequiredResourceType; |
14 | import de.uka.ipd.sdq.simucomframework.model.SimuComModel; |
15 | |
16 | /** |
17 | * Base class for simulated resource container. A resource container corresponds |
18 | * to PCM resource container in such that they contain an arbitrary amount |
19 | * of active resources |
20 | * @author Steffen Becker |
21 | * |
22 | */ |
23 | public abstract class AbstractSimulatedResourceContainer { |
24 | |
25 | protected static Logger logger = |
26 | Logger.getLogger(AbstractSimulatedResourceContainer.class.getName()); |
27 | protected SimuComModel myModel = null; |
28 | protected String myContainerID = null; |
29 | |
30 | // TODO: Multiple Resources with Scheduler |
31 | protected HashMap<String, AbstractScheduledResource> activeResources = new HashMap<String, AbstractScheduledResource>(); |
32 | |
33 | // Resources can also be lookup by the provided resource interface id |
34 | protected HashMap<String, String> activeResourceProvidedInterfaces = new HashMap<String, String>(); |
35 | |
36 | public AbstractSimulatedResourceContainer(SimuComModel myModel, String myContainerID) { |
37 | super(); |
38 | this.myModel = myModel; |
39 | this.myContainerID = myContainerID; |
40 | logger.info("Simulated Resource Container created. ContainerID "+myContainerID); |
41 | } |
42 | |
43 | /** |
44 | * Demand processing of a resource demand by a given type of active resources |
45 | * In future versions this has to control schedulers of resource types which |
46 | * exist in multiple instances |
47 | * @param requestingProcess The thread requesting the processing of a resouce demand |
48 | * @param typeID ID of the resource type to which the demand is directed. Same as the |
49 | * PCM resource type IDs |
50 | * @param demand The demand in units processable by the resource. The resource is |
51 | * responsible itself for converting this demand into time spans |
52 | */ |
53 | public void loadActiveResource(SimuComSimProcess requestingProcess, String typeID, double demand) { |
54 | AbstractScheduledResource resource = activeResources.get(typeID); |
55 | if (resource == null) { |
56 | throw new ResourceContainerIsMissingRequiredResourceType(typeID); |
57 | } |
58 | resource.consumeResource(requestingProcess, 1, demand); |
59 | } |
60 | |
61 | /** |
62 | * Demand processing of a resource demand by a given type of active resource and a resource interface operation |
63 | * @param requestingProcess The thread requesting the processing of a resource demand |
64 | * @param typeID ID of the resource provided interface to which the demand is directed. |
65 | * @param resourceServiceID the id of the resource service to be called. |
66 | * @param demand The demand in units processable by the resource. The resource is |
67 | * responsible itself for converting this demand into time spans |
68 | */ |
69 | public void loadActiveResource(SimuComSimProcess requestingProcess, String providedInterfaceID, int resourceServiceID, double demand) { |
70 | AbstractScheduledResource resource = null; |
71 | String typeID = activeResourceProvidedInterfaces.get(providedInterfaceID); |
72 | if (typeID != null) { |
73 | resource = activeResources.get(typeID); |
74 | } |
75 | if (resource == null) { |
76 | throw new ResourceContainerIsMissingRequiredResourceType(typeID); |
77 | } |
78 | resource.consumeResource(requestingProcess, resourceServiceID, demand); |
79 | } |
80 | |
81 | /** |
82 | * Retrieves all active resources in this resource container. |
83 | * @return all active resources |
84 | */ |
85 | public Collection<AbstractScheduledResource> getActiveResources() { |
86 | return activeResources.values(); |
87 | } |
88 | |
89 | /** |
90 | * Retrieves the HashMap with all all active resources in this resource container. |
91 | * @return all active resources |
92 | */ |
93 | public HashMap<String, AbstractScheduledResource> getAllActiveResources() { |
94 | return activeResources; |
95 | } |
96 | |
97 | /** |
98 | * Retrieves all active resources in this resource container which are currently unavailable. |
99 | * @return all unavailable active resources |
100 | */ |
101 | public List<AbstractScheduledResource> getFailedResources() { |
102 | List<AbstractScheduledResource> resultList = new ArrayList<AbstractScheduledResource>(); |
103 | Iterator<AbstractScheduledResource> iterator = getActiveResources().iterator(); |
104 | while(iterator.hasNext()){ |
105 | AbstractScheduledResource resource = iterator.next(); |
106 | if(!resource.isAvailable() && resource.isRequiredByContainer()){ |
107 | resultList.add(resource); |
108 | } |
109 | } |
110 | return resultList; |
111 | } |
112 | |
113 | /** |
114 | * @return The ID of the resource container itself |
115 | */ |
116 | public String getResourceContainerID() { |
117 | return myContainerID; |
118 | } |
119 | |
120 | } |