1 | package de.uka.ipd.sdq.pcmsolver.handler; |
2 | |
3 | import org.apache.log4j.Logger; |
4 | import org.eclipse.emf.common.util.EList; |
5 | |
6 | import de.uka.ipd.sdq.pcm.parameter.VariableCharacterisation; |
7 | import de.uka.ipd.sdq.pcm.parameter.VariableCharacterisationType; |
8 | import de.uka.ipd.sdq.pcm.parameter.VariableUsage; |
9 | import de.uka.ipd.sdq.pcm.repository.Parameter; |
10 | import de.uka.ipd.sdq.pcm.seff.CollectionIteratorAction; |
11 | import de.uka.ipd.sdq.pcmsolver.visitors.SeffVisitor; |
12 | import de.uka.ipd.sdq.stoex.AbstractNamedReference; |
13 | import de.uka.ipd.sdq.stoex.NamespaceReference; |
14 | |
15 | /** |
16 | * @author Koziolek |
17 | * |
18 | */ |
19 | public class CollectionIteratorActionHandler extends AbstractLoopActionHandler{ |
20 | |
21 | private static Logger logger = Logger.getLogger(CollectionIteratorActionHandler.class.getName()); |
22 | |
23 | public CollectionIteratorActionHandler(SeffVisitor seffVisitor) { |
24 | super(seffVisitor); |
25 | } |
26 | |
27 | /** |
28 | * @param loop |
29 | */ |
30 | public void handle(CollectionIteratorAction collIterAction) { |
31 | Parameter parameter = collIterAction.getParameter_CollectionIteratorAction(); |
32 | String parameterName = parameter.getParameterName(); |
33 | |
34 | String iterCount = getIterationExpression(parameterName); |
35 | if (iterCount == null){ |
36 | logger.error("Skipping execution of loop body!"); |
37 | return; |
38 | } |
39 | |
40 | storeToUsageContext(collIterAction, iterCount); |
41 | |
42 | visitLoopBody(collIterAction, iterCount); |
43 | } |
44 | |
45 | |
46 | |
47 | /** |
48 | * Fetches the expression describing the number of loop iteration |
49 | * from the usage context. It is the NUMBER_OF_ELEMENTS of the parameter |
50 | * in the SEFF, whose name is passed here as input parameter. |
51 | * |
52 | * @param soughtParameterName |
53 | * @return |
54 | */ |
55 | private String getIterationExpression(String soughtParameterName){ |
56 | EList<VariableUsage> vuList = visitor.getContextWrapper().getCompUsgCtx().getInput_ComputedUsageContext().getParameterChacterisations_Input(); |
57 | |
58 | for (VariableUsage vu : vuList){ // iterate over parameters |
59 | String currentParameterName = ""; |
60 | AbstractNamedReference ref = vu.getNamedReference__VariableUsage(); |
61 | while (ref instanceof NamespaceReference){ |
62 | NamespaceReference nsRef = (NamespaceReference)ref; |
63 | currentParameterName += nsRef.getReferenceName() + "."; |
64 | ref = nsRef.getInnerReference_NamespaceReference(); |
65 | } |
66 | currentParameterName += ref.getReferenceName(); |
67 | |
68 | if (currentParameterName.equals(soughtParameterName)){ |
69 | EList<VariableCharacterisation> varChars = vu.getVariableCharacterisation_VariableUsage(); |
70 | for (VariableCharacterisation vc : varChars){ // iterate over a parameter's characterisations |
71 | if (vc.getType() == VariableCharacterisationType.NUMBER_OF_ELEMENTS){ |
72 | return vc.getSpecification_VariableCharacterisation().getSpecification(); |
73 | } |
74 | } |
75 | logger.error("Variable Characterisation NUMBER_OF_ELEMENTS missing " + |
76 | "in Usage Context for parameter "+soughtParameterName+")! " + |
77 | "It is needed for a CollectionIteratorAction."); |
78 | return null; |
79 | } |
80 | } |
81 | logger.error("Variable "+soughtParameterName+" missing " + |
82 | "in Usage Context!"); |
83 | return null; |
84 | } |
85 | |
86 | } |