1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.reliability.solver.sensitivity; |
5 | |
6 | import java.util.ArrayList; |
7 | import java.util.List; |
8 | |
9 | import org.eclipse.emf.common.util.EList; |
10 | import org.eclipse.emf.ecore.EObject; |
11 | |
12 | import de.uka.ipd.sdq.pcm.usagemodel.Branch; |
13 | import de.uka.ipd.sdq.pcm.usagemodel.BranchTransition; |
14 | import de.uka.ipd.sdq.pcm.usagemodel.UsagemodelFactory; |
15 | import de.uka.ipd.sdq.sensitivity.DoubleParameterVariation; |
16 | |
17 | /** |
18 | * Provides sensitivity support to alter the branch probability of a |
19 | * probabilistic usage branch transition. |
20 | * |
21 | * @author brosch |
22 | * |
23 | */ |
24 | public class UsageBranchSensitivity extends MarkovSensitivity { |
25 | |
26 | /** |
27 | * The base value. |
28 | */ |
29 | private double baseValue; |
30 | |
31 | /** |
32 | * The ID of the involved branch behaviour. |
33 | */ |
34 | private String behaviourId; |
35 | |
36 | /** |
37 | * The ID of the usage branch to alter. |
38 | */ |
39 | private String branchId; |
40 | |
41 | /** |
42 | * The affected probabilistic branch transition; |
43 | */ |
44 | private BranchTransition transition = null; |
45 | |
46 | /** |
47 | * The constructor. |
48 | * |
49 | * @param name |
50 | * the name of the sensitivity analysis |
51 | * @param branchId |
52 | * the id of the usage branch to alter |
53 | * @param behaviourId |
54 | * the id of the involved branch behaviour |
55 | * @param variation |
56 | * the parameter variation |
57 | */ |
58 | public UsageBranchSensitivity(final String name, final String branchId, |
59 | final String behaviourId, final DoubleParameterVariation variation) { |
60 | |
61 | // Initialize base variables: |
62 | super(name, variation); |
63 | |
64 | // Further initialization: |
65 | this.branchId = branchId; |
66 | this.behaviourId = behaviourId; |
67 | } |
68 | |
69 | /** |
70 | * Alters the model according to the next sensitivity analysis step. |
71 | * |
72 | * @return indicates if the model could be successfully altered |
73 | */ |
74 | protected boolean alterModel() { |
75 | |
76 | // Check validity: |
77 | if (transition == null) { |
78 | return false; |
79 | } |
80 | |
81 | // Set the branch probability: |
82 | transition.setBranchProbability(calculator.calculateCurrentDoubleValue( |
83 | getDoubleVariation(), getCurrentStepNumber(), baseValue)); |
84 | |
85 | // Everything ok: |
86 | return true; |
87 | } |
88 | |
89 | /** |
90 | * Extracts the relevant sensitivity information from the given model. |
91 | */ |
92 | protected void extractSensitivityInformation() { |
93 | |
94 | // Retrieve all BranchTransitions in the PCM Repository: |
95 | if (getModel().getUsageModel() == null) { |
96 | // No usage model found! |
97 | logger.error("No PCM UsageModel found."); |
98 | return; |
99 | } |
100 | |
101 | // Search for the relevant branch: |
102 | Branch branch = null; |
103 | EList<EObject> branches = helper.getElements( |
104 | getModel().getUsageModel(), UsagemodelFactory.eINSTANCE |
105 | .createBranch().eClass()); |
106 | for (EObject object : branches) { |
107 | if (((Branch) object).getId().equals(branchId)) { |
108 | branch = (Branch) object; |
109 | break; |
110 | } |
111 | } |
112 | if (branch == null) { |
113 | logger.error("No Branch with ID \"" + branchId + "\" found."); |
114 | return; |
115 | } |
116 | |
117 | // Search for the relevant branch transition: |
118 | for (BranchTransition branchTransition : branch |
119 | .getBranchTransitions_Branch()) { |
120 | if (branchTransition.getBranchedBehaviour_BranchTransition() |
121 | .getId().equals(behaviourId)) { |
122 | transition = branchTransition; |
123 | baseValue = branchTransition.getBranchProbability(); |
124 | return; |
125 | } |
126 | } |
127 | if (transition == null) { |
128 | logger.error("No BranchTransition with associated behaviour ID \"" |
129 | + behaviourId + "\" found."); |
130 | } |
131 | } |
132 | |
133 | /** |
134 | * Builds the headings strings for logging. |
135 | * |
136 | * @return the log headings strings |
137 | */ |
138 | protected List<List<String>> getLogHeadingsMulti() { |
139 | |
140 | // Create a result list: |
141 | List<List<String>> resultList = new ArrayList<List<String>>(); |
142 | |
143 | // Create the headings: |
144 | ArrayList<String> headings = new ArrayList<String>(); |
145 | headings.add("Branch Name"); |
146 | headings.add("Branch ID"); |
147 | headings.add("Scenario Behaviour Name"); |
148 | headings.add("Scenario Behaviour ID"); |
149 | headings.add("Branch Probability"); |
150 | resultList.add(headings); |
151 | |
152 | // Return the result: |
153 | return resultList; |
154 | } |
155 | |
156 | /** |
157 | * Builds the results strings for sensitivity logging. |
158 | * |
159 | * @return the results strings |
160 | */ |
161 | protected List<String> getLogSingleResultsMulti() { |
162 | |
163 | // Create a result list: |
164 | List<String> resultList = new ArrayList<String>(); |
165 | |
166 | // Create the result strings: |
167 | resultList.add(transition.getBranch_BranchTransition().getEntityName()); |
168 | resultList.add(branchId); |
169 | resultList.add(transition.getBranchedBehaviour_BranchTransition() |
170 | .getEntityName()); |
171 | resultList.add(behaviourId); |
172 | resultList.add(calculator.getCurrentLogEntry(getDoubleVariation(), |
173 | getCurrentStepNumber())); |
174 | |
175 | // Return the result: |
176 | return resultList; |
177 | } |
178 | } |