1 | /** |
2 | * |
3 | */ |
4 | package de.fzi.se.quality.util; |
5 | |
6 | import java.util.logging.Logger; |
7 | |
8 | import de.fzi.se.quality.qualityannotation.PCMRE; |
9 | import de.fzi.se.quality.qualityannotation.PCMRECategory; |
10 | import de.fzi.se.quality.qualityannotation.PCMREInterface; |
11 | import de.fzi.se.quality.qualityannotation.PCMRERequestCategory; |
12 | import de.fzi.se.quality.qualityannotation.PCMREResourceInterface; |
13 | import de.fzi.se.quality.qualityannotation.PCMREResourceSignature; |
14 | import de.fzi.se.quality.qualityannotation.PCMRERole; |
15 | import de.fzi.se.quality.qualityannotation.PCMRESignature; |
16 | import de.fzi.se.quality.qualityannotation.Precision; |
17 | import de.fzi.se.quality.qualityannotation.QualityAnnotation; |
18 | import de.fzi.se.quality.qualityannotation.REPrecision; |
19 | import de.fzi.se.quality.qualityannotation.RequiredElement; |
20 | import de.fzi.se.quality.util.EMFHelper; |
21 | import de.uka.ipd.sdq.pcm.core.entity.ResourceRequiredRole; |
22 | import de.uka.ipd.sdq.pcm.repository.InfrastructureInterface; |
23 | import de.uka.ipd.sdq.pcm.repository.InfrastructureRequiredRole; |
24 | import de.uka.ipd.sdq.pcm.repository.InfrastructureSignature; |
25 | import de.uka.ipd.sdq.pcm.repository.Interface; |
26 | import de.uka.ipd.sdq.pcm.repository.OperationRequiredRole; |
27 | import de.uka.ipd.sdq.pcm.repository.OperationSignature; |
28 | import de.uka.ipd.sdq.pcm.repository.Role; |
29 | import de.uka.ipd.sdq.pcm.repository.Signature; |
30 | import de.uka.ipd.sdq.pcm.resourcetype.ResourceInterface; |
31 | import de.uka.ipd.sdq.pcm.resourcetype.ResourceSignature; |
32 | |
33 | /**Lookup functions for elements in QualityAnnotation instances. |
34 | * @author groenda |
35 | * |
36 | */ |
37 | public class QualityAnnotationLookup { |
38 | /** Logger for this class. */ |
39 | public static final Logger logger = Logger.getLogger(QualityAnnotationLookup.class.getCanonicalName()); |
40 | |
41 | /** The quality annotation which is used for lookups. */ |
42 | private QualityAnnotation qualityAnnotation; |
43 | |
44 | public QualityAnnotationLookup(QualityAnnotation qualityAnnotation) { |
45 | this.qualityAnnotation = qualityAnnotation; |
46 | } |
47 | |
48 | /**Retrieves a required element for the given PCM entity. |
49 | * @param category The category for which the required element is requested. |
50 | * @return The required element. {@code null} if no matching element was found. |
51 | */ |
52 | public PCMRECategory getRequiredElement(final PCMRERequestCategory category) { |
53 | int found = 0; |
54 | PCMRECategory foundRE; |
55 | PCMRECategory result = null; |
56 | for (RequiredElement re : qualityAnnotation.getStipulatedREPrecisions()) { |
57 | if (re instanceof PCMRECategory) { |
58 | foundRE = (PCMRECategory) re; |
59 | if (foundRE.getCategory().equals(category)) { |
60 | result = foundRE; |
61 | found++; |
62 | } |
63 | } |
64 | } |
65 | if (found > 1) { |
66 | throw new IllegalStateException("Cardinality of [0..1] breached. " + found + " request elements satisfied the search condition. "); |
67 | } |
68 | return result; |
69 | } |
70 | |
71 | /**Retrieves a required element for the given PCM entity. |
72 | * @param iface The interface for which the required element is requested. |
73 | * @return The required element. {@code null} if no matching element was found. |
74 | */ |
75 | public PCMREInterface getRequiredElement(final Interface iface) { |
76 | int found = 0; |
77 | PCMREInterface foundRE; |
78 | PCMREInterface result = null; |
79 | for (RequiredElement re : qualityAnnotation.getStipulatedREPrecisions()) { |
80 | if (re instanceof PCMREInterface) { |
81 | foundRE = (PCMREInterface) re; |
82 | if (foundRE.getInterface() == iface) { |
83 | result = foundRE; |
84 | found++; |
85 | } |
86 | } |
87 | } |
88 | if (found > 1) { |
89 | throw new IllegalStateException("Cardinality of [0..1] breached. " + found + " request elements satisfied the search condition. "); |
90 | } |
91 | return result; |
92 | } |
93 | |
94 | /**Retrieves a required element for the given PCM entity. |
95 | * @param role The interface for which the required element is requested. |
96 | * @return The required element. {@code null} if no matching element was found. |
97 | */ |
98 | public PCMRERole getRequiredElement(final Role role) { |
99 | int found = 0; |
100 | PCMRERole foundRE; |
101 | PCMRERole result = null; |
102 | for (RequiredElement re : qualityAnnotation.getStipulatedREPrecisions()) { |
103 | if (re instanceof PCMRERole) { |
104 | foundRE = (PCMRERole) re; |
105 | if (foundRE.getRole() == role) { |
106 | result = foundRE; |
107 | found++; |
108 | } |
109 | } |
110 | } |
111 | if (found > 1) { |
112 | throw new IllegalStateException("Cardinality of [0..1] breached. " + found + " request elements satisfied the search condition. "); |
113 | } |
114 | return result; |
115 | } |
116 | |
117 | /**Retrieves a required element for the given PCM entity. |
118 | * @param signature The interface for which the required element is requested. |
119 | * @return The required element. {@code null} if no matching element was found. |
120 | */ |
121 | public PCMRESignature getRequiredElement(final Signature signature) { |
122 | int found = 0; |
123 | PCMRESignature foundRE; |
124 | PCMRESignature result = null; |
125 | for (RequiredElement re : qualityAnnotation.getStipulatedREPrecisions()) { |
126 | if (re instanceof PCMRESignature) { |
127 | foundRE = (PCMRESignature) re; |
128 | if (foundRE.getSignature() == signature) { |
129 | result = foundRE; |
130 | found++; |
131 | } |
132 | } |
133 | } |
134 | if (found > 1) { |
135 | throw new IllegalStateException("Cardinality of [0..1] breached. " + found + " request elements satisfied the search condition. "); |
136 | } |
137 | return result; |
138 | } |
139 | |
140 | /**Gets the stated precision for the number of calls. |
141 | * @param signature Signature of the resource service. |
142 | * @param role Role of the resource service. {@code null} if unknown. |
143 | * @return The stated precision. |
144 | */ |
145 | public Precision getPCMREPrecisionNoC(InfrastructureSignature signature, InfrastructureRequiredRole role) { |
146 | REPrecision precision = getPCMREPrecision(signature, role); |
147 | return precision==null ? null : precision.getDefaultPrecisionNumberOfCalls(); |
148 | } |
149 | |
150 | /**Gets the stated precision for the call parameters. |
151 | * @param signature Signature of the resource service. |
152 | * @param role Role of the resource service. {@code null} if unknown. |
153 | * @return The stated precision. |
154 | */ |
155 | public Precision getPCMREPrecisionCP(InfrastructureSignature signature, InfrastructureRequiredRole role) { |
156 | REPrecision precision = getPCMREPrecision(signature, role); |
157 | return precision==null ? null : precision.getDefaultPrecisionCallParameter(); |
158 | } |
159 | |
160 | /**Gets the stated precision for the number of calls. |
161 | * @param signature Signature of the operation. |
162 | * @param role Role of the interface. {@code null} if unknown. |
163 | * @return The stated precision. |
164 | */ |
165 | public Precision getPCMREPrecisionNoC(OperationSignature signature, OperationRequiredRole role) { |
166 | REPrecision precision = getPCMREPrecision(signature, role); |
167 | return precision==null ? null : precision.getDefaultPrecisionNumberOfCalls(); |
168 | } |
169 | |
170 | /**Gets the stated precision for the number of calls. |
171 | * @param signature Signature of the operation. |
172 | * @param role Role of the interface. {@code null} if unknown. |
173 | * @return The stated precision. |
174 | */ |
175 | public Precision getPCMREPrecisionCP(OperationSignature signature, OperationRequiredRole role) { |
176 | REPrecision precision = getPCMREPrecision(signature, role); |
177 | return precision==null ? null : precision.getDefaultPrecisionCallParameter(); |
178 | } |
179 | |
180 | /**Gets the statement for a required element. |
181 | * @param signature Signature of the operation. |
182 | * @param role Role of the interface. {@code null} if unknown. |
183 | * @return The stated required element. |
184 | */ |
185 | public REPrecision getPCMREPrecision(OperationSignature signature, OperationRequiredRole role) { |
186 | PCMRERequestCategory category = PCMRERequestCategory.COMPONENT; |
187 | Interface iface = signature.getInterface__OperationSignature(); |
188 | PCMRE current = getPCMRE(signature, role, category, iface); |
189 | return current==null ? null : current.getPrecision(); |
190 | } |
191 | |
192 | /**Gets the statement for a required element. |
193 | * @param signature Signature of the resource service. |
194 | * @param role Role of the resource service. {@code null} if unknown. |
195 | * @return The stated required element. |
196 | */ |
197 | public REPrecision getPCMREPrecision(InfrastructureSignature signature, InfrastructureRequiredRole role) { |
198 | PCMRERequestCategory category = PCMRERequestCategory.INFRASTRUCTURE; |
199 | InfrastructureInterface iface = signature==null ? null : signature.getInfrastructureInterface__InfrastructureSignature(); |
200 | PCMRE current = getPCMRE(signature, role, category, iface); |
201 | return current==null ? null : current.getPrecision(); |
202 | } |
203 | |
204 | /**Gets the statement for a required element. |
205 | * @param signature Signature of the resource service. |
206 | * @param role Role of the resource service. {@code null} if unknown. |
207 | * @return The stated required element. |
208 | */ |
209 | public REPrecision getPCMREPrecision(ResourceSignature signature, ResourceRequiredRole role) { |
210 | PCMRERequestCategory category = PCMRERequestCategory.RESOURCE; |
211 | ResourceInterface iface = signature==null ? null : signature.getResourceInterface__ResourceSignature(); |
212 | PCMRE current = getPCMRE(signature, role, category, iface); |
213 | return current==null ? null : current.getPrecision(); |
214 | } |
215 | |
216 | /**Gets the statement for a required element. |
217 | * @param signature Signature of the resource service. |
218 | * @param role Role of the resource service. {@code null} if unknown. |
219 | * @return The stated required element. |
220 | */ |
221 | public REPrecision getPCMREPrecisionCategoryResourceDemand() { |
222 | PCMRERequestCategory category = PCMRERequestCategory.RESOURCE_DEMAND; |
223 | PCMRECategory current = getRequiredElement(category); |
224 | return current==null ? null : current.getPrecision(); |
225 | } |
226 | |
227 | /**Gets the statement for a required element. |
228 | * @param signature Signature of the resource service. |
229 | * @param role Role of the resource service. {@code null} if unknown. |
230 | * @return The stated required element. |
231 | */ |
232 | public REPrecision getPCMREPrecisionCategoryComponentInternal() { |
233 | PCMRERequestCategory category = PCMRERequestCategory.COMPONENT_INTERNAL; |
234 | PCMRECategory current = getRequiredElement(category); |
235 | return current==null ? null : current.getPrecision(); |
236 | } |
237 | |
238 | /**Gets the most specific PCM Required Element for the given target. |
239 | * @param signature Requested Signature. |
240 | * @param role Required role. |
241 | * @param category Requested Category. |
242 | * @param iface Requested Interface. |
243 | * @return {@link PCMRE} or {@code null} if none could be found. |
244 | */ |
245 | private PCMRE getPCMRE(Signature signature, |
246 | Role role, PCMRERequestCategory category, |
247 | Interface iface) { |
248 | assert(category == PCMRERequestCategory.INFRASTRUCTURE || category == PCMRERequestCategory.COMPONENT); |
249 | |
250 | PCMRE current = null; |
251 | PCMRECategory reCategory = null; |
252 | reCategory = getRequiredElement(category); |
253 | if (reCategory != null) { |
254 | current = reCategory; |
255 | PCMREInterface reInterface = null; |
256 | for (RequiredElement req : EMFHelper.getObjectsByType(reCategory.getChildREs(), PCMREInterface.class)) { |
257 | reInterface = (PCMREInterface) req; |
258 | if (reInterface.getInterface() == iface) { |
259 | current = reInterface; |
260 | PCMRERole reRole = null; |
261 | for (RequiredElement req2 : reInterface.getChildREs()) { |
262 | reRole = (PCMRERole) req2; |
263 | if (reRole.getRole() == role) { |
264 | current = reRole; |
265 | PCMRESignature reSignature = null; |
266 | for (RequiredElement req3 : EMFHelper.getObjectsByType(reRole.getChildREs(), PCMRESignature.class)) { |
267 | reSignature = (PCMRESignature) req3; |
268 | if (reSignature.getSignature() == signature) { |
269 | current = reSignature; |
270 | } |
271 | } |
272 | } |
273 | } |
274 | } |
275 | } |
276 | } else { |
277 | String msg = "Could not find a RE Precision for the category " + category + ". Add an RE Precision for this category."; |
278 | logger.severe(msg); |
279 | throw new IllegalArgumentException(msg); |
280 | } |
281 | if (current == null) { |
282 | String msg = "Could not find a RE Precision for signature " + signature + " and the role " + role + "."; |
283 | logger.severe(msg); |
284 | throw new IllegalArgumentException(msg); |
285 | } |
286 | return current; |
287 | } |
288 | |
289 | /**Gets the most specific PCM Required Element for the given target. |
290 | * @param signature Requested Resource Signature. |
291 | * @param role Required role. |
292 | * @param category Requested Category. |
293 | * @param iface Requested Resource Interface. |
294 | * @return {@link PCMRE} or {@code null} if none could be found. |
295 | */ |
296 | private PCMRE getPCMRE(ResourceSignature signature, |
297 | Role role, PCMRERequestCategory category, |
298 | ResourceInterface iface) { |
299 | assert(category == PCMRERequestCategory.RESOURCE); |
300 | |
301 | PCMRE current = null; |
302 | PCMRECategory reCategory = null; |
303 | reCategory = getRequiredElement(category); |
304 | if (reCategory != null) { |
305 | current = reCategory; |
306 | PCMREResourceInterface reInterface = null; |
307 | for (RequiredElement req : EMFHelper.getObjectsByType(reCategory.getChildREs(), PCMREResourceInterface.class)) { |
308 | if (req instanceof PCMREResourceInterface) { |
309 | reInterface = (PCMREResourceInterface) req; |
310 | if (reInterface.getResourceInterface() == iface) { |
311 | current = reInterface; |
312 | PCMRERole reRole = null; |
313 | for (RequiredElement req2 : reInterface.getChildREs()) { |
314 | reRole = (PCMRERole) req2; |
315 | if (reRole.getRole() == role) { |
316 | current = reRole; |
317 | PCMREResourceSignature reSignature = null; |
318 | for (RequiredElement req3 : EMFHelper.getObjectsByType(reRole.getChildREs(), PCMREResourceSignature.class)) { |
319 | reSignature = (PCMREResourceSignature) req3; |
320 | if (reSignature.getResourceSignature() == signature) { |
321 | current = reSignature; |
322 | } |
323 | } |
324 | } |
325 | } |
326 | } |
327 | } |
328 | } |
329 | } else { |
330 | String msg = "Could not find a RE Precision for the category " + category + ". Add an RE Precision for this category."; |
331 | logger.severe(msg); |
332 | throw new IllegalArgumentException(msg); |
333 | } |
334 | if (current == null) { |
335 | String msg = "Could not find a RE Precision for signature " + signature + " and the role " + role + "."; |
336 | logger.severe(msg); |
337 | throw new IllegalArgumentException(msg); |
338 | } |
339 | return current; |
340 | } |
341 | } |