1 | package de.uka.ipd.sdq.pcmsolver.visitors; |
2 | |
3 | |
4 | import java.util.HashMap; |
5 | |
6 | import org.apache.log4j.Logger; |
7 | import org.eclipse.emf.common.util.BasicEList; |
8 | import org.eclipse.emf.common.util.EList; |
9 | |
10 | import de.uka.ipd.sdq.context.computed_usage.ExternalCallOutput; |
11 | import de.uka.ipd.sdq.context.computed_usage.Input; |
12 | import de.uka.ipd.sdq.pcm.parameter.CharacterisedVariable; |
13 | import de.uka.ipd.sdq.pcm.parameter.VariableCharacterisation; |
14 | import de.uka.ipd.sdq.pcm.parameter.VariableUsage; |
15 | import de.uka.ipd.sdq.pcmsolver.transformations.ContextWrapper; |
16 | import de.uka.ipd.sdq.stoex.AbstractNamedReference; |
17 | import de.uka.ipd.sdq.stoex.Expression; |
18 | import de.uka.ipd.sdq.stoex.NamespaceReference; |
19 | import de.uka.ipd.sdq.stoex.Variable; |
20 | import de.uka.ipd.sdq.stoex.analyser.visitors.ExpressionSolveVisitor; |
21 | import de.uka.ipd.sdq.stoex.analyser.visitors.TypeEnum; |
22 | |
23 | public class ExpressionParameterSolverVisitor extends ExpressionSolveVisitor { |
24 | |
25 | private static Logger logger = Logger |
26 | .getLogger(ExpressionParameterSolverVisitor.class.getName()); |
27 | |
28 | //private Context context; |
29 | private ContextWrapper contextWrapper; |
30 | |
31 | public ExpressionParameterSolverVisitor(HashMap<Expression, TypeEnum> typeAnn, ContextWrapper ctxWrp){ |
32 | super(typeAnn); |
33 | this.contextWrapper = ctxWrp; |
34 | } |
35 | |
36 | /* (non-Javadoc) |
37 | * @see de.uka.ipd.sdq.stoex.analyser.visitors.ExpressionSolveVisitor#caseVariable(de.uka.ipd.sdq.stoex.Variable) |
38 | * |
39 | * Solves a parametric dependency. For a given variable, it tries to |
40 | * determine it's actual specification (e.g. probability distribution or |
41 | * constant) by looking it up in the usage context. |
42 | */ |
43 | @Override |
44 | public Object caseVariable(Variable var){ |
45 | AbstractNamedReference anr = var.getId_Variable(); |
46 | CharacterisedVariable chVar = (CharacterisedVariable)var; |
47 | |
48 | //Contains both input parameters specified in the interface and component parameters. |
49 | EList<VariableUsage> vuList = new BasicEList<VariableUsage>(); |
50 | Input input = contextWrapper.getCompUsgCtx().getInput_ComputedUsageContext(); |
51 | if (input !=null){ |
52 | vuList.addAll(input.getParameterChacterisations_Input()); |
53 | } |
54 | |
55 | EList<ExternalCallOutput> ecoList = contextWrapper.getCompUsgCtx().getExternalCallOutput_ComputedUsageContext(); |
56 | for (ExternalCallOutput eco : ecoList){ |
57 | // TODO: recognise scopes |
58 | EList<VariableUsage> vuList2 = eco.getParameterCharacterisations_ExternalCallOutput(); |
59 | vuList.addAll(vuList2); |
60 | } |
61 | |
62 | String soughtParameterName = getFullParameterName(anr); |
63 | for (VariableUsage vu : vuList){ |
64 | AbstractNamedReference ref = vu.getNamedReference__VariableUsage(); |
65 | String currentParameterName = getFullParameterName(ref); |
66 | |
67 | if (currentParameterName.equals(soughtParameterName)){ |
68 | EList<VariableCharacterisation> varCharList = vu.getVariableCharacterisation_VariableUsage(); |
69 | |
70 | for (VariableCharacterisation vc : varCharList){ // iterate over a parameter's characterisations |
71 | if (vc.getType() == chVar.getCharacterisationType()){ |
72 | String specification = vc.getSpecification_VariableCharacterisation().getSpecification(); |
73 | Expression expr = ExpressionHelper.parseToExpression(specification); |
74 | return expr; |
75 | } |
76 | } |
77 | throw new RuntimeException("Variable Characterisation missing in Usage Context ("+soughtParameterName+")!"); |
78 | } |
79 | } |
80 | throw new RuntimeException("Variable Characterisation missing in Usage Context ("+soughtParameterName+")!"); |
81 | } |
82 | |
83 | /** |
84 | * @param currentParameterName |
85 | * @param ref |
86 | * @return |
87 | */ |
88 | private String getFullParameterName(AbstractNamedReference ref) { |
89 | String name = ""; |
90 | while (ref instanceof NamespaceReference){ |
91 | NamespaceReference nsRef = (NamespaceReference)ref; |
92 | name += nsRef.getReferenceName() + "."; |
93 | ref = nsRef.getInnerReference_NamespaceReference(); |
94 | } |
95 | return name += ref.getReferenceName(); |
96 | } |
97 | } |