1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.spa.basicsolver.visitor.perfhandler; |
5 | |
6 | import java.util.Hashtable; |
7 | import java.util.Iterator; |
8 | |
9 | import de.uka.ipd.sdq.probfunction.math.IProbabilityDensityFunction; |
10 | import de.uka.ipd.sdq.probfunction.math.IProbabilityFunctionFactory; |
11 | import de.uka.ipd.sdq.probfunction.math.IProbabilityMassFunction; |
12 | import de.uka.ipd.sdq.probfunction.math.ISample; |
13 | import de.uka.ipd.sdq.probfunction.math.ISamplePDF; |
14 | import de.uka.ipd.sdq.probfunction.math.IUnit; |
15 | import de.uka.ipd.sdq.probfunction.math.exception.FunctionNotInTimeDomainException; |
16 | import de.uka.ipd.sdq.probfunction.math.exception.FunctionsInDifferenDomainsException; |
17 | import de.uka.ipd.sdq.probfunction.math.exception.ProbabilityFunctionException; |
18 | import de.uka.ipd.sdq.probfunction.math.exception.UnknownPDFTypeException; |
19 | import de.uka.ipd.sdq.spa.basicsolver.visitor.LoopHandler; |
20 | import de.uka.ipd.sdq.spa.expression.Expression; |
21 | import de.uka.ipd.sdq.spa.expression.Loop; |
22 | |
23 | /** |
24 | * @author Ihssane |
25 | * |
26 | */ |
27 | public class PerformanceLoopHandler implements LoopHandler { |
28 | |
29 | private Hashtable<Expression, IProbabilityDensityFunction> pdfTable; |
30 | |
31 | private static IProbabilityFunctionFactory pfFactory = IProbabilityFunctionFactory.eINSTANCE; |
32 | |
33 | protected PerformanceLoopHandler( |
34 | Hashtable<Expression, IProbabilityDensityFunction> pdfTable) { |
35 | super(); |
36 | this.pdfTable = pdfTable; |
37 | } |
38 | |
39 | /** |
40 | * the iterations of a loop have to be specified as a probability mass |
41 | * functions whose samples are integer values (type Integer) |
42 | * |
43 | * @throws UnknownPDFTypeException |
44 | * @throws FunctionNotInTimeDomainException |
45 | * @throws FunctionsInDifferenDomainsException |
46 | */ |
47 | public void handle(Loop loop) { |
48 | try { |
49 | |
50 | ISamplePDF innerPDF = pfFactory.transformToSamplePDF(pdfTable |
51 | .get(loop.getRegExp())); |
52 | IProbabilityMassFunction iterations = pfFactory.transformToPMF(loop.getIterationsPMF()); |
53 | ISamplePDF result = solveIterative(iterations, innerPDF); |
54 | pdfTable.put(loop, result); |
55 | |
56 | } catch (ProbabilityFunctionException e) { |
57 | e.printStackTrace(); |
58 | System.exit(1); |
59 | } |
60 | |
61 | } |
62 | |
63 | public static ISamplePDF solveIterative(IProbabilityMassFunction iterations, ISamplePDF innerFourier) throws ProbabilityFunctionException { |
64 | int numSamplingPoints = innerFourier.getValuesAsDouble().size(); |
65 | double distance = innerFourier.getDistance(); |
66 | IUnit unit = innerFourier.getUnit(); |
67 | |
68 | ISamplePDF result = (ISamplePDF)pfFactory.createZeroFunction(numSamplingPoints, |
69 | distance, unit).getFourierTransform(); |
70 | |
71 | ISamplePDF dirac = pfFactory.createDiracImpulse(numSamplingPoints, |
72 | distance, unit); |
73 | |
74 | IProbabilityDensityFunction tmp = dirac.getFourierTransform(); |
75 | |
76 | |
77 | int pos = 0; |
78 | for (Iterator<ISample> iter = iterations.getSamples().iterator(); iter |
79 | .hasNext();) { |
80 | ISample sample = iter.next(); |
81 | Integer nextPos = (Integer) sample.getValue(); |
82 | while (pos < nextPos) { |
83 | tmp = tmp.mult(innerFourier); |
84 | pos++; |
85 | } |
86 | result = (ISamplePDF) result.add(tmp.scale(sample |
87 | .getProbability())); |
88 | } |
89 | return result; |
90 | } |
91 | |
92 | public Hashtable<Expression, IProbabilityDensityFunction> getPdfTable() { |
93 | return pdfTable; |
94 | } |
95 | |
96 | public void setPdfTable( |
97 | Hashtable<Expression, IProbabilityDensityFunction> pdfTable) { |
98 | this.pdfTable = pdfTable; |
99 | } |
100 | |
101 | protected PerformanceLoopHandler() { |
102 | super(); |
103 | } |
104 | |
105 | } |