1 | package de.uka.ipd.sdq.simucomframework; |
2 | |
3 | import java.util.HashMap; |
4 | |
5 | import de.uka.ipd.sdq.scheduler.IPassiveResource; |
6 | import de.uka.ipd.sdq.simucomframework.exceptions.ResourceContainerNotFound; |
7 | import de.uka.ipd.sdq.simucomframework.model.SimuComModel; |
8 | import de.uka.ipd.sdq.simucomframework.resources.AbstractSimulatedResourceContainer; |
9 | import de.uka.ipd.sdq.simucomframework.resources.SimulatedLinkingResourceContainer; |
10 | import de.uka.ipd.sdq.simucomframework.resources.SimulatedResourceContainer; |
11 | import de.uka.ipd.sdq.simucomframework.variables.StackContext; |
12 | |
13 | /** |
14 | * Context of each simulation thread. This context inherits a stack context and |
15 | * enriches it with information on the simulated execution environment. |
16 | * |
17 | * @author Steffen Becker |
18 | * |
19 | */ |
20 | public abstract class Context extends StackContext { |
21 | |
22 | /** |
23 | * Central registry which contains all simulated resources |
24 | */ |
25 | private ResourceRegistry registry = null; |
26 | |
27 | /** |
28 | * AssemblyContextID -> ResourceContainer Object (Deployment Link) |
29 | */ |
30 | private HashMap<String, AbstractSimulatedResourceContainer> assemblyLinkHash = new HashMap<String, AbstractSimulatedResourceContainer>(); |
31 | |
32 | /** |
33 | * AssemblyContextID -> PassiveRessource |
34 | */ |
35 | private HashMap<String, IPassiveResource> assemblyPassiveResourceHash = new HashMap<String, IPassiveResource>(); |
36 | |
37 | /** |
38 | * The thread to which this context belongs |
39 | */ |
40 | private SimuComSimProcess myThread = null; |
41 | |
42 | /** |
43 | * Simulation model |
44 | */ |
45 | private SimuComModel myModel = null; |
46 | |
47 | /** |
48 | * Initialise a new context for the given simulation model |
49 | * |
50 | * @param myModel |
51 | * The simulation model used in this context |
52 | */ |
53 | public Context(SimuComModel myModel) { |
54 | if (myModel != null) { // This is for the prototype mapping, where we |
55 | // don't need resources |
56 | this.registry = myModel.getResourceRegistry(); |
57 | this.myModel = myModel; |
58 | initialiseAssemblyContextLookup(); |
59 | } else { |
60 | stack.createAndPushNewStackFrame(); |
61 | } |
62 | } |
63 | |
64 | public long getSessionId() { |
65 | return myThread.getCurrentSessionId(); |
66 | } |
67 | |
68 | /** |
69 | * Lookup method to find the resource container in which the given |
70 | * components assembly context is deployed |
71 | * |
72 | * @param assemblyContextID |
73 | * The ID of the assembly context for which its deployment is |
74 | * queried |
75 | * @return The resource container in which the given assembly context is |
76 | * deployed |
77 | */ |
78 | public AbstractSimulatedResourceContainer findResource( |
79 | String assemblyContextID) { |
80 | AbstractSimulatedResourceContainer container = assemblyLinkHash |
81 | .get(assemblyContextID); |
82 | if (container == null) |
83 | throw new ResourceContainerNotFound( |
84 | "Resource container for assembly context " |
85 | + assemblyContextID |
86 | + " not found. Check your allocation model. " |
87 | + "Note that a SubSystem must only be used once in one System, using one several times may cause this error. "); |
88 | return container; |
89 | } |
90 | |
91 | /** |
92 | * Lookup method to find the linking resource container that belongs to the |
93 | * given container id. |
94 | * |
95 | * @param linkingResourceContainerID |
96 | * the container id |
97 | * @return the linking resource container |
98 | */ |
99 | public SimulatedLinkingResourceContainer findLinkingResource( |
100 | String linkingResourceContainerID) { |
101 | AbstractSimulatedResourceContainer container = registry |
102 | .getResourceContainer(linkingResourceContainerID); |
103 | if ((container == null) |
104 | || !(container instanceof SimulatedLinkingResourceContainer)) { |
105 | throw new ResourceContainerNotFound( |
106 | "Linking resource container for container ID " |
107 | + linkingResourceContainerID + " not found."); |
108 | } |
109 | return (SimulatedLinkingResourceContainer) container; |
110 | } |
111 | |
112 | /** |
113 | * Create a deployment relationship between the given assembly context and |
114 | * the given resource container |
115 | * |
116 | * @param assemblyContextID |
117 | * ID of the assembly context to allocate |
118 | * @param resourceContainerID |
119 | * ID of the resource container on which the assembly context is |
120 | * allocated |
121 | */ |
122 | protected void linkAssemblyContextAndResourceContainer( |
123 | String assemblyContextID, String resourceContainerID) { |
124 | assert registry.containsResourceContainer(resourceContainerID); |
125 | AbstractSimulatedResourceContainer container = registry |
126 | .getResourceContainer(resourceContainerID); |
127 | assemblyLinkHash.put(assemblyContextID, container); |
128 | } |
129 | |
130 | public IPassiveResource getPassiveRessourceInContext( |
131 | String assemblyContextID, String passiveResourceName, |
132 | String passiveResourceID, |
133 | AbstractSimulatedResourceContainer resourceContainer, int capacity) { |
134 | IPassiveResource pr = assemblyPassiveResourceHash.get(assemblyContextID |
135 | + passiveResourceID); |
136 | |
137 | if (pr == null) { |
138 | pr = ((SimulatedResourceContainer) resourceContainer) |
139 | .createPassiveResource(passiveResourceName, |
140 | passiveResourceID, assemblyContextID, |
141 | passiveResourceID + ":" + assemblyContextID, |
142 | capacity); |
143 | assemblyPassiveResourceHash.put(assemblyContextID |
144 | + passiveResourceID, pr); |
145 | } |
146 | |
147 | return pr; |
148 | } |
149 | |
150 | /** |
151 | * Template method to be filled in by the generator. Calles |
152 | * linkAssemblyContextAndResourceContainer to create the deployment |
153 | * specified in the allocation model |
154 | */ |
155 | protected abstract void initialiseAssemblyContextLookup(); |
156 | |
157 | public SimuComSimProcess getThread() { |
158 | return myThread; |
159 | } |
160 | |
161 | public void setSimProcess(SimuComSimProcess process) { |
162 | this.myThread = process; |
163 | } |
164 | |
165 | public SimuComModel getModel() { |
166 | return myModel; |
167 | } |
168 | |
169 | } |