1 | package de.uka.ipd.sdq.pcmsolver.visitors; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.Collection; |
5 | import java.util.List; |
6 | |
7 | import org.apache.log4j.Logger; |
8 | import org.eclipse.emf.common.util.EList; |
9 | import org.eclipse.emf.ecore.util.EcoreUtil; |
10 | |
11 | import de.uka.ipd.sdq.context.computed_usage.ComputedUsageFactory; |
12 | import de.uka.ipd.sdq.context.computed_usage.ExternalCallInput; |
13 | import de.uka.ipd.sdq.context.computed_usage.ExternalCallOutput; |
14 | import de.uka.ipd.sdq.context.computed_usage.Input; |
15 | import de.uka.ipd.sdq.context.computed_usage.Output; |
16 | import de.uka.ipd.sdq.pcm.core.CoreFactory; |
17 | import de.uka.ipd.sdq.pcm.core.PCMRandomVariable; |
18 | import de.uka.ipd.sdq.pcm.parameter.ParameterFactory; |
19 | import de.uka.ipd.sdq.pcm.parameter.VariableCharacterisation; |
20 | import de.uka.ipd.sdq.pcm.parameter.VariableUsage; |
21 | import de.uka.ipd.sdq.pcmsolver.transformations.ContextWrapper; |
22 | import de.uka.ipd.sdq.stoex.AbstractNamedReference; |
23 | import de.uka.ipd.sdq.stoex.NamespaceReference; |
24 | import de.uka.ipd.sdq.stoex.StoexFactory; |
25 | import de.uka.ipd.sdq.stoex.VariableReference; |
26 | |
27 | public class VariableUsageHelper { |
28 | |
29 | private static ParameterFactory parameterFactory = ParameterFactory.eINSTANCE; |
30 | private static ComputedUsageFactory compUsageFactory = ComputedUsageFactory.eINSTANCE; |
31 | |
32 | protected static Logger logger = Logger.getLogger(VariableUsageHelper.class |
33 | .getName()); |
34 | |
35 | public static void copySolvedVariableUsageToUsageContext(ContextWrapper ctxWrp, VariableUsage oldUsage) { |
36 | VariableUsage newUsage = getCopiedSolvedVariableUsage(ctxWrp, oldUsage); |
37 | ctxWrp.getCompUsgCtx().getInput_ComputedUsageContext().getParameterChacterisations_Input().add(newUsage); |
38 | } |
39 | |
40 | /** |
41 | * Create new VariableUsages, fill it with the solved values of the passed VariableUsages and |
42 | * add it to the input parameter. |
43 | * This method assumes that it is called last with the variables with the highest priority, |
44 | * because if a variable name is already defined in the input, it deletes the old |
45 | * definition and uses the new ones from the parameter variableUsages. |
46 | */ |
47 | public static void copySolvedVariableUsageToInput(Input input,ContextWrapper oldContextWrapper, Collection<VariableUsage> variableUsages) { |
48 | for (VariableUsage compParams : variableUsages) { |
49 | VariableUsage newUsage = getCopiedSolvedVariableUsage( |
50 | oldContextWrapper, compParams); |
51 | |
52 | //check if there already exists a variable with the same name in this set of variable usages. |
53 | String varName = getFullParameterName(newUsage.getNamedReference__VariableUsage()); |
54 | |
55 | List<VariableUsage> usagesToBeOverwritten = new ArrayList<VariableUsage>(5); |
56 | for (VariableUsage usage : input.getParameterChacterisations_Input()){ |
57 | if (getFullParameterName(usage.getNamedReference__VariableUsage()).equals(varName)){ |
58 | // replace the old definition, it could be overwritten e.g. in UserData. So it is ok if one definition occurs several times. |
59 | // XXX: Catch cases where there should only be one definition? |
60 | usagesToBeOverwritten.add(usage); |
61 | //throw new RuntimeException("Multiple definition of variable '" + varName + "' in a SEFF of assembly context "+((ComputedUsageContext)input.eContainer()).getAssemblyContext_ComputedUsageContext().getEntityName()); |
62 | logger.info("Variable "+varName+" is defined twice, one definition has been overwritten."); |
63 | } |
64 | } |
65 | input.getParameterChacterisations_Input().removeAll(usagesToBeOverwritten); |
66 | |
67 | input.getParameterChacterisations_Input().add(newUsage); |
68 | } |
69 | |
70 | } |
71 | |
72 | public static void copySolvedVariableUsageToOutput( |
73 | ContextWrapper contextWrapper, VariableUsage vu) { |
74 | Output output = contextWrapper.getCompUsgCtx().getOutput_ComputedUsageContext(); |
75 | if (output == null) { |
76 | output = compUsageFactory.createOutput(); |
77 | contextWrapper.getCompUsgCtx().setOutput_ComputedUsageContext(output); |
78 | } |
79 | VariableUsage newUsage = getCopiedSolvedVariableUsage(contextWrapper, vu); |
80 | output.getParameterCharacterisations_Output().add(newUsage); |
81 | } |
82 | |
83 | public static void copySolvedVariableUsageToExternalCallInput( |
84 | ContextWrapper contextWrapper, ExternalCallInput eci, VariableUsage vu) { |
85 | VariableUsage newUsage = getCopiedSolvedVariableUsage(contextWrapper, vu); |
86 | eci.getParameterCharacterisations_ExternalCallInput().add(newUsage); |
87 | } |
88 | |
89 | /** |
90 | * Adds the usage information from {@link VariableUsage} vu to the |
91 | * {@link ExternalCallOutput} eco. |
92 | * @param contextWrapper |
93 | * @param eco |
94 | * @param vu |
95 | */ |
96 | public static void copySolvedVariableUsageToExternalCallOutput( |
97 | ContextWrapper contextWrapper, ExternalCallOutput eco, |
98 | VariableUsage vu) { |
99 | VariableUsage newUsage = getCopiedSolvedVariableUsage(contextWrapper, vu); |
100 | eco.getParameterCharacterisations_ExternalCallOutput().add(newUsage); |
101 | } |
102 | |
103 | private static VariableUsage getCopiedSolvedVariableUsage( |
104 | ContextWrapper ctxWrp, VariableUsage oldUsage) { |
105 | VariableUsage newUsage = parameterFactory.createVariableUsage(); |
106 | |
107 | newUsage.setNamedReference__VariableUsage(getReferenceCopy(oldUsage.getNamedReference__VariableUsage())); |
108 | //newUsage.setNamedReference_VariableUsage(oldUsage.getNamedReference_VariableUsage()); |
109 | |
110 | EList<VariableCharacterisation> varCharList = oldUsage.getVariableCharacterisation_VariableUsage(); |
111 | for (VariableCharacterisation oldVC : varCharList){ |
112 | |
113 | String specification = oldVC.getSpecification_VariableCharacterisation().getSpecification(); |
114 | String solvedSpecification = ExpressionHelper |
115 | .getSolvedExpressionAsString(specification, ctxWrp); |
116 | |
117 | VariableCharacterisation solvedCharacterisation = parameterFactory |
118 | .createVariableCharacterisation(); |
119 | solvedCharacterisation.setType(oldVC.getType()); |
120 | |
121 | PCMRandomVariable rv = CoreFactory.eINSTANCE.createPCMRandomVariable(); |
122 | rv.setSpecification(solvedSpecification); |
123 | solvedCharacterisation.setSpecification_VariableCharacterisation(rv); |
124 | |
125 | newUsage.getVariableCharacterisation_VariableUsage().add(solvedCharacterisation); |
126 | |
127 | } |
128 | return newUsage; |
129 | } |
130 | |
131 | |
132 | |
133 | public static AbstractNamedReference getReferenceCopy(AbstractNamedReference anr){ |
134 | if(anr == null) { |
135 | return null; |
136 | } |
137 | if (anr instanceof NamespaceReference){ |
138 | NamespaceReference nr = (NamespaceReference)anr; |
139 | NamespaceReference newRef = StoexFactory.eINSTANCE.createNamespaceReference(); |
140 | newRef.setReferenceName(nr.getReferenceName()); |
141 | newRef.setInnerReference_NamespaceReference(getReferenceCopy(nr.getInnerReference_NamespaceReference())); |
142 | return newRef; |
143 | } else if (anr instanceof VariableReference){ |
144 | VariableReference vr = (VariableReference)anr; |
145 | VariableReference varRef = StoexFactory.eINSTANCE.createVariableReference(); |
146 | varRef.setReferenceName(vr.getReferenceName()); |
147 | return varRef; |
148 | } else { |
149 | throw new IllegalArgumentException("Unsupported Reference type "+anr.getClass().getName()); |
150 | } |
151 | } |
152 | |
153 | public static void copyVariableUsageToInput(Input input, VariableUsage vu) { |
154 | // TODO: support dependencies on output parameters |
155 | VariableUsage newUsage = parameterFactory.createVariableUsage(); |
156 | newUsage.setNamedReference__VariableUsage(getReferenceCopy(vu.getNamedReference__VariableUsage())); |
157 | |
158 | EList<VariableCharacterisation> characterisations = vu.getVariableCharacterisation_VariableUsage(); |
159 | for (VariableCharacterisation oldCharacterisation : characterisations){ |
160 | String specification = oldCharacterisation.getSpecification_VariableCharacterisation().getSpecification(); |
161 | String solvedSpecification = ExpressionHelper |
162 | .getSolvedExpressionAsString(specification, null); |
163 | |
164 | VariableCharacterisation solvedCharacterisation = parameterFactory |
165 | .createVariableCharacterisation(); |
166 | solvedCharacterisation.setType(oldCharacterisation.getType()); |
167 | |
168 | PCMRandomVariable rv = CoreFactory.eINSTANCE.createPCMRandomVariable(); |
169 | rv.setSpecification(solvedSpecification); |
170 | solvedCharacterisation.setSpecification_VariableCharacterisation(rv); |
171 | |
172 | newUsage.getVariableCharacterisation_VariableUsage().add(solvedCharacterisation); |
173 | |
174 | } |
175 | input.getParameterChacterisations_Input().add(newUsage); |
176 | } |
177 | |
178 | /** |
179 | * Returns all characterisation from the first argument that are also specified in the second argument. |
180 | * @param vu1 |
181 | * @param vu2 |
182 | * @return A list that is not null, but may be empty. |
183 | */ |
184 | public static List<VariableCharacterisation> getCommonCharacterisationsFromFirst(VariableUsage vu1, VariableUsage vu2){ |
185 | if (vu1 == vu2){ |
186 | return vu1.getVariableCharacterisation_VariableUsage(); |
187 | } |
188 | |
189 | if (describeSameReference(vu1.getNamedReference__VariableUsage(),vu2.getNamedReference__VariableUsage())){ |
190 | return vu1.getVariableCharacterisation_VariableUsage(); |
191 | } |
192 | |
193 | List<VariableCharacterisation> characterisations1 = vu1.getVariableCharacterisation_VariableUsage(); |
194 | List<VariableCharacterisation> characterisations2 = vu2.getVariableCharacterisation_VariableUsage(); |
195 | |
196 | //Return all characterisations that are there in both lists from list 1 |
197 | //or better select all from vu1 that are not in vu2 |
198 | List<VariableCharacterisation> listFrom1ThatAreAlsoIn2 = new ArrayList<VariableCharacterisation>(); |
199 | for (VariableCharacterisation variableCharacterisation1 : characterisations1) { |
200 | for (VariableCharacterisation variableCharacterisation2 : characterisations2) { |
201 | if (variableCharacterisation1.getType().equals(variableCharacterisation2.getType())){ |
202 | listFrom1ThatAreAlsoIn2.add(variableCharacterisation1); |
203 | } |
204 | } |
205 | } |
206 | |
207 | return listFrom1ThatAreAlsoIn2; |
208 | |
209 | } |
210 | |
211 | private static boolean describeSameReference( |
212 | AbstractNamedReference ref1, |
213 | AbstractNamedReference ref2) { |
214 | |
215 | if (!ref1.getReferenceName().equals(ref2.getReferenceName())){ |
216 | return false; |
217 | } |
218 | |
219 | if (ref1 instanceof VariableReference |
220 | && ref2 instanceof VariableReference) |
221 | return true; |
222 | |
223 | if (ref1 instanceof NamespaceReference |
224 | && ref2 instanceof NamespaceReference){ |
225 | NamespaceReference nref1 = (NamespaceReference)ref1; |
226 | NamespaceReference nref2 = (NamespaceReference)ref2; |
227 | return describeSameReference(nref1.getInnerReference_NamespaceReference(), nref2.getInnerReference_NamespaceReference()); |
228 | } |
229 | return false; |
230 | |
231 | } |
232 | |
233 | /** |
234 | * Copies the passed list using the EcoreUtils class. |
235 | * @param originalVariableUsages. A list containing VariableUsage |
236 | * @return A copy of the list. |
237 | */ |
238 | public static Collection<VariableUsage> copyVariableUsageList(List<VariableUsage> originalVariableUsages) { |
239 | return EcoreUtil.copyAll(originalVariableUsages); |
240 | } |
241 | |
242 | /** |
243 | * Copies the passed list using the EcoreUtils class. |
244 | * @param variableCharacterisations. A list containing VariableCharacterisation |
245 | * @return A copy of the list. |
246 | */ |
247 | public static Collection<VariableCharacterisation> copyVariableCharacterisations( |
248 | List<VariableCharacterisation> variableCharacterisations) { |
249 | return EcoreUtil.copyAll(variableCharacterisations); |
250 | } |
251 | |
252 | private static String getFullParameterName(AbstractNamedReference ref) { |
253 | String name = ""; |
254 | while (ref instanceof NamespaceReference) { |
255 | NamespaceReference nsRef = (NamespaceReference) ref; |
256 | name += nsRef.getReferenceName() + "."; |
257 | ref = nsRef.getInnerReference_NamespaceReference(); |
258 | } |
259 | return name += ref.getReferenceName(); |
260 | } |
261 | } |