1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.dsexplore.qml.pcm.reader; |
5 | |
6 | import java.util.ArrayList; |
7 | import java.util.HashMap; |
8 | import java.util.Iterator; |
9 | import java.util.List; |
10 | |
11 | import org.opt4j.core.SatisfactionConstraint; |
12 | import org.opt4j.core.Constraint.Direction; |
13 | import org.opt4j.core.Objective.Sign; |
14 | |
15 | import de.uka.ipd.sdq.dsexplore.qml.contract.QMLContract.Constraint; |
16 | import de.uka.ipd.sdq.dsexplore.qml.contract.QMLContract.Criterion; |
17 | import de.uka.ipd.sdq.dsexplore.qml.contract.QMLContract.EnumOperator; |
18 | import de.uka.ipd.sdq.dsexplore.qml.contract.QMLContract.EvaluationAspect; |
19 | import de.uka.ipd.sdq.dsexplore.qml.contract.QMLContract.GenericQMLContract; |
20 | import de.uka.ipd.sdq.dsexplore.qml.contract.QMLContract.Goal; |
21 | import de.uka.ipd.sdq.dsexplore.qml.contract.QMLContract.NumericLiteral; |
22 | import de.uka.ipd.sdq.dsexplore.qml.contract.QMLContract.Objective; |
23 | import de.uka.ipd.sdq.dsexplore.qml.contract.QMLContract.Restriction; |
24 | import de.uka.ipd.sdq.dsexplore.qml.contract.QMLContract.SimpleQMLContract; |
25 | import de.uka.ipd.sdq.dsexplore.qml.contracttype.QMLContractType.EnumRelationSemantics; |
26 | import de.uka.ipd.sdq.dsexplore.qml.contracttype.QMLContractType.QMLContractType; |
27 | import de.uka.ipd.sdq.dsexplore.qml.declarations.QMLDeclarations.QMLDeclarations; |
28 | import de.uka.ipd.sdq.dsexplore.qml.handling.QMLConstantsContainer; |
29 | import de.uka.ipd.sdq.dsexplore.qml.pcm.datastructures.EvaluationAspectWithContext; |
30 | import de.uka.ipd.sdq.dsexplore.qml.pcm.datastructures.builder.InfeasibilityConstraintBuilder; |
31 | import de.uka.ipd.sdq.dsexplore.qml.pcm.datastructures.builder.ObjectiveBuilder; |
32 | import de.uka.ipd.sdq.dsexplore.qml.pcm.datastructures.builder.SatisfactionConstraintBuilder; |
33 | import de.uka.ipd.sdq.dsexplore.qml.profile.QMLProfile.Requirement; |
34 | import de.uka.ipd.sdq.dsexplore.qml.profile.QMLProfile.SimpleQMLProfile; |
35 | import de.uka.ipd.sdq.dsexplore.qml.reader.QMLDeclarationsReader; |
36 | import de.uka.ipd.sdq.pcm.usagemodel.UsageModel; |
37 | import de.uka.ipd.sdq.pcm.usagemodel.UsageScenario; |
38 | |
39 | /** |
40 | * Reads PCM specific QML definitions. It is used by quality attribute evaluators to get |
41 | * the in QML defined objectives and constraints. Currently, the QML based mating heuristics |
42 | * currently uses the retranslation to get the original information about the defined constraints. |
43 | * |
44 | * @see QMLDeclarationsReader |
45 | * @author noorshams |
46 | * |
47 | */ |
48 | public class PCMDeclarationsReader { |
49 | |
50 | |
51 | protected final String PCMContractTypePath; |
52 | protected final String[] PCMProfilePaths; |
53 | |
54 | protected static HashMap<String, EvaluationAspectWithContext> retranslationMap = new HashMap<String, EvaluationAspectWithContext>(); |
55 | |
56 | protected QMLDeclarationsReader qmlReader = new QMLDeclarationsReader();; |
57 | protected QMLContractType pcmContractType; |
58 | //Refined are not supported yet |
59 | protected List<SimpleQMLProfile> pcmProfiles = new ArrayList<SimpleQMLProfile>(); |
60 | protected List<EvaluationAspectWithContext> pcmConstraints = new ArrayList<EvaluationAspectWithContext>(); |
61 | protected List<EvaluationAspectWithContext> pcmObjectives = new ArrayList<EvaluationAspectWithContext>(); |
62 | |
63 | protected String pcmContractTypeId = null; |
64 | |
65 | |
66 | public PCMDeclarationsReader(String PCMProfilePath) { |
67 | |
68 | this.PCMContractTypePath = QMLConstantsContainer.STANDARD_CONTRACT_TYPE_PATH; |
69 | this.PCMProfilePaths = new String[]{PCMProfilePath}; |
70 | |
71 | init(); |
72 | } |
73 | |
74 | protected void init() { |
75 | |
76 | initPCMContractType(); |
77 | initPCMProfiles(); |
78 | initPCMConstraintsAndObjectives(); |
79 | |
80 | initPCMContractTypeID(); |
81 | } |
82 | |
83 | private void initPCMContractType() { |
84 | //XXX: Be sure to read the declarations correctly |
85 | QMLDeclarations declarations = qmlReader.getQMLDeclarations(PCMContractTypePath); |
86 | List<QMLContractType> ctList = qmlReader.getQMLContractTypes(declarations); |
87 | if(declarations != null && ctList.size() > 1) { |
88 | throw new RuntimeException("The pcm contract type definition contains multiple contract types."); |
89 | } |
90 | pcmContractType = ctList.get(0); |
91 | } |
92 | |
93 | private void initPCMProfiles() { |
94 | //XXX: Be sure to read the declarations correctly |
95 | //TODO: Support refinements |
96 | for (int i = 0; i < PCMProfilePaths.length; i++) { |
97 | String PCMProfilePath = PCMProfilePaths[i]; |
98 | QMLDeclarations declarations = qmlReader.getQMLDeclarations(PCMProfilePath); |
99 | pcmProfiles.addAll(qmlReader.getSimpleQMLProfiles(declarations)); |
100 | if(declarations != null && qmlReader.getRefinedQMLProfiles(declarations).size() > 0) { |
101 | throw new RuntimeException("Refined QML profiles not supported!"); |
102 | } |
103 | } |
104 | } |
105 | |
106 | private void initPCMConstraintsAndObjectives(){ |
107 | for (SimpleQMLProfile simpleProfile : pcmProfiles) { |
108 | //XXX: Only Simple Profiles atm! Adjust this, if refinements are possible! |
109 | for (Requirement requirement : simpleProfile.getRequirements()) { |
110 | for (GenericQMLContract contract : requirement.getRequireContract()) { |
111 | if (contract instanceof SimpleQMLContract) { |
112 | SimpleQMLContract simpleContract = (SimpleQMLContract) contract; |
113 | for (Criterion criterion : simpleContract.getCriteria()) { |
114 | for (EvaluationAspect aspect : criterion.getAspects()) { |
115 | if (criterion instanceof Objective) { |
116 | this.pcmObjectives.add(new EvaluationAspectWithContext(simpleContract.getContractType(), criterion.getDimension(), criterion, aspect, simpleProfile.getUsageModel(), requirement)); |
117 | } else { |
118 | //criterion instanceof Constraint |
119 | this.pcmConstraints.add(new EvaluationAspectWithContext(simpleContract.getContractType(), criterion.getDimension(), criterion, aspect, simpleProfile.getUsageModel(), requirement)); |
120 | } |
121 | } |
122 | } |
123 | } else { |
124 | //TODO: Support Refinements |
125 | throw new RuntimeException("Unsupported QML Contract! Simple QML Contract expected"); |
126 | } |
127 | } |
128 | } |
129 | } |
130 | } |
131 | |
132 | private void initPCMContractTypeID() { |
133 | pcmContractTypeId = pcmContractType.getId(); |
134 | } |
135 | |
136 | public List<EvaluationAspectWithContext> getDimensionObjectiveContextsForUsageModel(UsageModel usageModel, String dimensionId) { |
137 | return new ArrayList<EvaluationAspectWithContext>(getDimensionCriterionContextsForUsageModel(usageModel, dimensionId, Objective.class)); |
138 | } |
139 | |
140 | public List<EvaluationAspectWithContext> getDimensionConstraintContextsForUsageModel(UsageModel usageModel, String dimensionId) { |
141 | return new ArrayList<EvaluationAspectWithContext>(getDimensionCriterionContextsForUsageModel(usageModel, dimensionId, Constraint.class)); |
142 | } |
143 | |
144 | protected List<EvaluationAspectWithContext> getDimensionCriterionContextsForUsageModel(UsageModel usageModel, String dimensionId, Class<? extends Criterion> CriterionClass) { |
145 | List<EvaluationAspectWithContext> returnList = new ArrayList<EvaluationAspectWithContext>(); |
146 | List<EvaluationAspectWithContext> tmpList = getDimensionCriterionContexts(dimensionId, CriterionClass); |
147 | for (EvaluationAspectWithContext aspect : tmpList) { |
148 | if(equalUsageModels(usageModel, aspect.getUsageModel())){ |
149 | returnList.add(aspect); |
150 | } |
151 | } |
152 | |
153 | return returnList; |
154 | } |
155 | |
156 | protected boolean equalUsageModels(UsageModel um1, UsageModel um2){ |
157 | if (um1 == um2) { // 'pointer' equality |
158 | return true; |
159 | } else if (um1.getUsageScenario_UsageModel().size() != 0 && um2.getUsageScenario_UsageModel().size() != 0) { |
160 | // compare UsageModels: as every Scenario is contained by exactly 1 UsageModel, |
161 | // the Models are considered equal, if the first UsageScenario of UM1 is in UM2 |
162 | |
163 | String us1_id = um1.getUsageScenario_UsageModel().get(0).getId(); |
164 | for (Iterator<UsageScenario> iterator2 = um2.getUsageScenario_UsageModel().iterator(); iterator2 |
165 | .hasNext();) { |
166 | UsageScenario us2 = iterator2 |
167 | .next(); |
168 | if(us2.getId().equals(us1_id)) { |
169 | return true; |
170 | } |
171 | } |
172 | return false; |
173 | } else { |
174 | return false; |
175 | } |
176 | } |
177 | |
178 | protected List<EvaluationAspectWithContext> getDimensionCriterionContexts(String dimensionId, Class<? extends Criterion> CriterionClass) { |
179 | |
180 | List<EvaluationAspectWithContext> list = new ArrayList<EvaluationAspectWithContext>(); |
181 | List<EvaluationAspectWithContext> criterionList; |
182 | if (CriterionClass == Objective.class) { |
183 | criterionList = pcmObjectives; |
184 | } else { |
185 | //CriterionClass == Constraint.class |
186 | criterionList = pcmConstraints; |
187 | } |
188 | |
189 | for (Iterator<EvaluationAspectWithContext> iterator = criterionList.iterator(); iterator.hasNext();) { |
190 | EvaluationAspectWithContext aspect = iterator.next(); |
191 | if(aspect.getContractType().getId().equals(pcmContractTypeId)) { |
192 | if (aspect.getDimension().getId().equals(dimensionId)) { |
193 | list.add(aspect); |
194 | } |
195 | } |
196 | } |
197 | |
198 | return list; |
199 | } |
200 | |
201 | /* |
202 | * The following methods are used to transform the QML definitions to |
203 | * criteria objects that can be used for the optimization. Always get it translated here as it |
204 | * won't be reversible otherwise. |
205 | */ |
206 | |
207 | public org.opt4j.core.InfeasibilityConstraint translateEvalAspectToInfeasibilityConstraint(EvaluationAspectWithContext aspectContext, InfeasibilityConstraintBuilder builder) { |
208 | EvaluationAspect aspect = aspectContext.getEvaluationAspect(); |
209 | org.opt4j.core.InfeasibilityConstraint constraint; |
210 | if(aspect.getAspectRequirement() instanceof Restriction){ |
211 | if (((Restriction)aspect.getAspectRequirement()).getOperator() == EnumOperator.LESS) { |
212 | if (((Restriction)aspect.getAspectRequirement()).getAspectRequirementLiteral() instanceof NumericLiteral) { |
213 | constraint = builder.createInfeasibilityConstraint(aspect.getId(), |
214 | Direction.less, |
215 | ((NumericLiteral)((Restriction)aspect.getAspectRequirement()).getAspectRequirementLiteral()).getValue()); |
216 | } else { |
217 | //TODO: Handle Enums and Sets |
218 | throw new RuntimeException("Unsupported Constraint literal in aspect. Only numeric literals are supported so far."); |
219 | } |
220 | } else { |
221 | // TODO: Extend and remove Exception |
222 | throw new RuntimeException("Unsupported constraint operator in aspect. Only LESS (<) supported so far."); |
223 | } |
224 | } else { |
225 | throw new RuntimeException("Aspect must have aspect requirement of type Restriction to derive InfeasibilityConstraint."); |
226 | } |
227 | |
228 | retranslationMap.put(constraint.getName(), aspectContext); |
229 | return constraint; |
230 | } |
231 | |
232 | public SatisfactionConstraint translateEvalAspectToSatisfactionConstraint(EvaluationAspectWithContext aspectContext, org.opt4j.core.Objective objective, SatisfactionConstraintBuilder builder){ |
233 | EvaluationAspect aspect = aspectContext.getEvaluationAspect(); |
234 | SatisfactionConstraint constraint = null; |
235 | if (((Goal)aspect.getAspectRequirement()) == null) { |
236 | if(objective.getSign() == Sign.MIN) { |
237 | constraint = builder.createSatisfactionConstraint( |
238 | aspect.getId(), |
239 | Direction.less, |
240 | Double.NEGATIVE_INFINITY, |
241 | objective); |
242 | } else { |
243 | //Sign == MAX |
244 | constraint = builder.createSatisfactionConstraint( |
245 | aspect.getId(), |
246 | Direction.greater, |
247 | Double.POSITIVE_INFINITY, |
248 | objective); |
249 | } |
250 | } else if(aspect.getAspectRequirement() instanceof Goal){ |
251 | if (((Goal)aspect.getAspectRequirement()).getAspectRequirementLiteral() instanceof NumericLiteral) { |
252 | if(objective.getSign() == Sign.MIN) { |
253 | constraint = builder.createSatisfactionConstraint( |
254 | aspect.getId(), |
255 | Direction.less, |
256 | ((NumericLiteral)((Goal)aspect.getAspectRequirement()).getAspectRequirementLiteral()).getValue(), |
257 | objective); |
258 | } else { |
259 | //Sign == MAX |
260 | constraint = builder.createSatisfactionConstraint( |
261 | aspect.getId(), |
262 | Direction.greater, |
263 | ((NumericLiteral)((Goal)aspect.getAspectRequirement()).getAspectRequirementLiteral()).getValue(), |
264 | objective); |
265 | } |
266 | } else { |
267 | //TODO: Handle Enums and Sets |
268 | throw new RuntimeException("Unsupported Goal literal in aspect. Only numeric literals supported in Goal aspect requirements so far."); |
269 | } |
270 | } else { |
271 | throw new RuntimeException("Aspect must have aspect requirement of type Goal to derive SatisfactionConstraint!"); |
272 | } |
273 | |
274 | retranslationMap.put(constraint.getName(), aspectContext); |
275 | return constraint; |
276 | } |
277 | |
278 | public org.opt4j.core.Objective translateEvalAspectToObjective(String qualityAttribute, EvaluationAspectWithContext aspectContext, ObjectiveBuilder builder) { |
279 | //Make sure, the aspect IS an objective |
280 | org.opt4j.core.Objective objective; |
281 | if(aspectContext.getDimension().getType().getRelationSemantics().getRelSem() == EnumRelationSemantics.DECREASING) { |
282 | objective = builder.createObjective(qualityAttribute, org.opt4j.core.Objective.Sign.MIN); |
283 | } else { |
284 | //INCREASING |
285 | objective = builder.createObjective(qualityAttribute, org.opt4j.core.Objective.Sign.MAX); |
286 | } |
287 | |
288 | retranslationMap.put(objective.getName(), aspectContext); |
289 | return objective; |
290 | } |
291 | |
292 | |
293 | /* |
294 | * Static method for global uniqueness of retranslationMap. Otherwise one |
295 | * would have to save all existing PCMDeclarationsReader objects or have to ensure that it is |
296 | * a Singleton to be able to retrieve the original information on a Criterion. |
297 | * |
298 | * As the "Guice injection chain" is broken on several parts, where this class is needed, |
299 | * annotating the class with @Singleton is not possible unless you get this class explicitly injected |
300 | * retrieving and calling *the* Injector. However, the Injector is encapsulated in the Opt4J Task |
301 | * object, thus you would have to get the Opt4J Task to get the Injector to get this class injected... |
302 | * I don't think this would be a very elegant way... |
303 | * |
304 | */ |
305 | public static EvaluationAspectWithContext retranslateCriterionToEvaluationAspect(org.opt4j.core.Criterion criterion){ |
306 | return retranslationMap.get(criterion.getName()); |
307 | } |
308 | } |