1 | package de.uka.ipd.sdq.spa.basicsolver; |
2 | |
3 | import java.util.Hashtable; |
4 | import java.util.List; |
5 | |
6 | import de.uka.ipd.sdq.probfunction.math.IProbabilityFunctionFactory; |
7 | import de.uka.ipd.sdq.probfunction.math.IProbabilityMassFunction; |
8 | import de.uka.ipd.sdq.probfunction.math.ManagedPDF; |
9 | import de.uka.ipd.sdq.probfunction.math.exception.ConfigurationNotSetException; |
10 | import de.uka.ipd.sdq.spa.basicsolver.operations.RUPerformanceOps; |
11 | import de.uka.ipd.sdq.spa.expression.Acquire; |
12 | import de.uka.ipd.sdq.spa.expression.Alternative; |
13 | import de.uka.ipd.sdq.spa.expression.Expression; |
14 | import de.uka.ipd.sdq.spa.expression.Loop; |
15 | import de.uka.ipd.sdq.spa.expression.Parallel; |
16 | import de.uka.ipd.sdq.spa.expression.Release; |
17 | import de.uka.ipd.sdq.spa.expression.Sequence; |
18 | import de.uka.ipd.sdq.spa.expression.Symbol; |
19 | import de.uka.ipd.sdq.spa.expression.util.ExpressionSwitch; |
20 | import de.uka.ipd.sdq.spa.resourcemodel.ActiveResource; |
21 | import de.uka.ipd.sdq.spa.resourcemodel.ResourceUsage; |
22 | |
23 | public class PerformanceSolver { |
24 | |
25 | private IProbabilityFunctionFactory pfFactory = IProbabilityFunctionFactory.eINSTANCE; |
26 | |
27 | private RUPerformanceOps performanceOps; |
28 | |
29 | private ExpressionSwitch exprSwitch = new ExpressionSwitch() { |
30 | |
31 | @Override |
32 | public Object caseAcquire(Acquire object) { |
33 | System.err.println("Acquire ignored in performance computation!"); |
34 | return super.caseAcquire(object); |
35 | } |
36 | |
37 | @SuppressWarnings("unchecked") |
38 | @Override |
39 | public Object caseAlternative(Alternative object) { |
40 | |
41 | Hashtable<ActiveResource, ManagedPDF> leftRUs = (Hashtable<ActiveResource, ManagedPDF>) doSwitch(object.getLeftOption().getRegexp()); |
42 | Hashtable<ActiveResource, ManagedPDF> rightRUs = (Hashtable<ActiveResource, ManagedPDF>) doSwitch(object.getRightOption().getRegexp()); |
43 | double leftProb = object.getLeftOption().getProbability(); |
44 | double rightProb = object.getRightOption().getProbability(); |
45 | return performanceOps.computeAlternative(leftRUs, leftProb , rightRUs, rightProb); |
46 | } |
47 | |
48 | @SuppressWarnings("unchecked") |
49 | @Override |
50 | public Object caseLoop(Loop loop) { |
51 | Hashtable<ActiveResource, ManagedPDF> innerRUs = (Hashtable<ActiveResource, ManagedPDF>) doSwitch(loop.getRegExp()); |
52 | IProbabilityMassFunction iterations = pfFactory.transformToPMF( loop.getIterationsPMF() ); |
53 | try { |
54 | return performanceOps.computeIteration(innerRUs, iterations); |
55 | } catch (ConfigurationNotSetException e) { |
56 | e.printStackTrace(); |
57 | System.exit(-1); |
58 | return null; |
59 | } |
60 | } |
61 | |
62 | @SuppressWarnings("unchecked") |
63 | @Override |
64 | public Object caseParallel(Parallel object) { |
65 | Hashtable<ActiveResource, ManagedPDF> leftRUs = (Hashtable<ActiveResource, ManagedPDF>) doSwitch(object.getLeftTask()); |
66 | Hashtable<ActiveResource, ManagedPDF> rightRUs = (Hashtable<ActiveResource, ManagedPDF>) doSwitch(object.getRightTask()); |
67 | return performanceOps.computeParallel(leftRUs, rightRUs); |
68 | } |
69 | |
70 | @Override |
71 | public Object caseRelease(Release object) { |
72 | System.err.println("Release ignored in performance prediction!"); |
73 | return super.caseRelease(object); |
74 | } |
75 | |
76 | @SuppressWarnings("unchecked") |
77 | @Override |
78 | public Object caseSequence(Sequence object) { |
79 | Hashtable<ActiveResource, ManagedPDF> leftRUs = (Hashtable<ActiveResource, ManagedPDF>) doSwitch(object.getLeftRegExp()); |
80 | Hashtable<ActiveResource, ManagedPDF> rightRUs = (Hashtable<ActiveResource, ManagedPDF>) doSwitch(object.getRightRegExp()); |
81 | return performanceOps.computeSequence(leftRUs, rightRUs); |
82 | } |
83 | |
84 | @SuppressWarnings("unchecked") |
85 | @Override |
86 | public Object caseSymbol(Symbol symbol) { |
87 | List<ResourceUsage> resourceUsageList = (List<ResourceUsage>) symbol.getResourceUsages(); |
88 | return performanceOps.getResourceUsageTimes(resourceUsageList); |
89 | } |
90 | }; |
91 | |
92 | public PerformanceSolver(RUPerformanceOps performanceOps){ |
93 | this.performanceOps = performanceOps; |
94 | } |
95 | |
96 | public PerformanceSolver(){ |
97 | this.performanceOps = new RUPerformanceOps(); |
98 | } |
99 | |
100 | @SuppressWarnings("unchecked") |
101 | public Hashtable<ActiveResource, ManagedPDF> getResourceUsageTimes(Expression expression){ |
102 | return (Hashtable<ActiveResource, ManagedPDF>) exprSwitch.doSwitch(expression); |
103 | } |
104 | |
105 | } |