1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.pcmsolver.handler; |
5 | |
6 | import java.util.List; |
7 | |
8 | import org.apache.log4j.Logger; |
9 | |
10 | import de.uka.ipd.sdq.pcm.seff.GuardedBranchTransition; |
11 | import de.uka.ipd.sdq.pcmsolver.visitors.ExpressionHelper; |
12 | import de.uka.ipd.sdq.pcmsolver.visitors.SeffVisitor; |
13 | import de.uka.ipd.sdq.probfunction.ProbabilityMassFunction; |
14 | import de.uka.ipd.sdq.probfunction.Sample; |
15 | import de.uka.ipd.sdq.stoex.Expression; |
16 | import de.uka.ipd.sdq.stoex.ProbabilityFunctionLiteral; |
17 | |
18 | /** |
19 | * @author Koziolek |
20 | * |
21 | */ |
22 | public class GuardedBranchTransitionHandler extends AbstractBranchTransitionHandler{ |
23 | |
24 | private static Logger logger = Logger.getLogger(GuardedBranchTransitionHandler.class.getName()); |
25 | |
26 | /** |
27 | * @param context |
28 | * @param _visitor |
29 | * @param nextHandler |
30 | */ |
31 | public GuardedBranchTransitionHandler(SeffVisitor seffVisitor) { |
32 | super(seffVisitor); |
33 | } |
34 | |
35 | public void handle(GuardedBranchTransition bt){ |
36 | double solvedBranchProb = getBranchProbFromExpression(bt); |
37 | logger.debug("SolvedBranchProb:" + solvedBranchProb); |
38 | |
39 | storeToUsageContext(bt, solvedBranchProb); |
40 | |
41 | // Don't solve branch transitions that can never be reached. This |
42 | // releases the modeler from the duty to provide all character |
43 | // parameterizations for branches that are never reached (see Bug 615) |
44 | if (solvedBranchProb > 0.0) { |
45 | visitChildBehaviour(bt); |
46 | } |
47 | |
48 | // TODO: recognise scopes |
49 | // int lastElement = visitor.getContextWrapper().getCurrentEvaluatedBranchConditions().size()-1; |
50 | // visitor.getContextWrapper().getCurrentEvaluatedBranchConditions().remove(lastElement); |
51 | } |
52 | |
53 | /** |
54 | * @param bt |
55 | * @param solvedBranchProb |
56 | * @return |
57 | */ |
58 | private double getBranchProbFromExpression(GuardedBranchTransition bt) { |
59 | String specification = bt.getBranchCondition_GuardedBranchTransition().getSpecification(); |
60 | Expression solvedExpression = ExpressionHelper.getSolvedExpression(specification,visitor.getContextWrapper()); |
61 | |
62 | ProbabilityFunctionLiteral pfl = (ProbabilityFunctionLiteral)solvedExpression; |
63 | ProbabilityMassFunction pmf = (ProbabilityMassFunction)pfl.getFunction_ProbabilityFunctionLiteral(); |
64 | |
65 | double solvedBranchProb = 1.0; |
66 | List<Sample> points = pmf.getSamples(); |
67 | for (Sample point : points) { |
68 | String bool = point.getValue().toString(); |
69 | if (bool.toLowerCase().equals("true")) { |
70 | solvedBranchProb = point.getProbability(); |
71 | } |
72 | } |
73 | |
74 | // integrate already evaluated branch conditions |
75 | solvedBranchProb = adjustToScope(solvedBranchProb); |
76 | |
77 | // visitor.getContextWrapper().getCurrentEvaluatedBranchConditions().add(solvedBranchProb); |
78 | return solvedBranchProb; |
79 | } |
80 | |
81 | /** |
82 | * Include already evaluated branch conditions. |
83 | * |
84 | * @param solvedBranchProb |
85 | * @return |
86 | */ |
87 | private double adjustToScope(double solvedBranchProb) { |
88 | //TODO |
89 | // ArrayList list = myContext.getCurrentEvaluatedBranchConditions(); |
90 | // for (Object o : list) solvedBranchProb /= (Double)o; |
91 | return solvedBranchProb; |
92 | } |
93 | |
94 | } |