| 1 | package de.uka.ipd.sdq.spa.basicsolver.operations; |
| 2 | |
| 3 | import java.util.Iterator; |
| 4 | |
| 5 | import de.uka.ipd.sdq.probfunction.math.IProbabilityDensityFunction; |
| 6 | import de.uka.ipd.sdq.probfunction.math.IProbabilityMassFunction; |
| 7 | import de.uka.ipd.sdq.probfunction.math.ISample; |
| 8 | import de.uka.ipd.sdq.probfunction.math.ISamplePDF; |
| 9 | import de.uka.ipd.sdq.probfunction.math.ManagedPDF; |
| 10 | import de.uka.ipd.sdq.probfunction.math.exception.ConfigurationNotSetException; |
| 11 | import de.uka.ipd.sdq.probfunction.math.exception.ProbabilityFunctionException; |
| 12 | |
| 13 | public class PDFPerformanceOps { |
| 14 | |
| 15 | public PDFPerformanceOps() { |
| 16 | super(); |
| 17 | } |
| 18 | |
| 19 | public ManagedPDF computeIteration(ManagedPDF usage, |
| 20 | IProbabilityMassFunction iterations) |
| 21 | throws ProbabilityFunctionException, ConfigurationNotSetException { |
| 22 | ISamplePDF innerPDF = usage.getSamplePdfFrequencyDomain(); |
| 23 | |
| 24 | ISamplePDF resultPDF = ManagedPDF.createZeroFunction().getSamplePdfFrequencyDomain(); |
| 25 | ISamplePDF tempPDF = ManagedPDF.createDiracImpulse().getSamplePdfFrequencyDomain(); |
| 26 | |
| 27 | int pos = 0; |
| 28 | for (Iterator<ISample> iter = iterations.getSamples().iterator(); iter |
| 29 | .hasNext();) { |
| 30 | ISample sample = iter.next(); |
| 31 | Integer nextPos = (Integer) sample.getValue(); |
| 32 | while (pos < nextPos) { |
| 33 | tempPDF = (ISamplePDF)tempPDF.mult(innerPDF); |
| 34 | pos++; |
| 35 | } |
| 36 | resultPDF = (ISamplePDF) resultPDF.add(tempPDF.scale(sample.getProbability())); |
| 37 | } |
| 38 | return new ManagedPDF(resultPDF, true); |
| 39 | } |
| 40 | |
| 41 | public ManagedPDF computeAlternative(ManagedPDF leftRU, double leftProbability, |
| 42 | ManagedPDF rightRU, double rightProbability) |
| 43 | throws ProbabilityFunctionException { |
| 44 | return performOperation(BinaryOperation.ALTERNATIVE, leftRU, |
| 45 | leftProbability, rightRU, rightProbability); |
| 46 | } |
| 47 | |
| 48 | public ManagedPDF computeSequence(ManagedPDF leftRU, ManagedPDF rightRU) |
| 49 | throws ProbabilityFunctionException { |
| 50 | return performOperation(BinaryOperation.SEQUENCE, leftRU, 0, rightRU, 0); |
| 51 | } |
| 52 | |
| 53 | public ManagedPDF computeParallel(ManagedPDF leftRU, ManagedPDF rightRU) |
| 54 | throws ProbabilityFunctionException { |
| 55 | return performOperation(BinaryOperation.PARALLEL, leftRU, 0, rightRU, 0); |
| 56 | } |
| 57 | |
| 58 | |
| 59 | protected ManagedPDF performOperation(BinaryOperation op, |
| 60 | ManagedPDF leftRU, double leftProbability, |
| 61 | ManagedPDF rightRU, double rightProbability) |
| 62 | throws ProbabilityFunctionException { |
| 63 | |
| 64 | IProbabilityDensityFunction resultPDF = null; |
| 65 | IProbabilityDensityFunction leftPDF = leftRU.getPdfFrequencyDomain(); |
| 66 | IProbabilityDensityFunction rightPDF = rightRU.getPdfFrequencyDomain(); |
| 67 | |
| 68 | switch (op) { |
| 69 | case SEQUENCE: |
| 70 | resultPDF = leftPDF.mult(rightPDF); |
| 71 | break; |
| 72 | case ALTERNATIVE: |
| 73 | resultPDF = leftPDF.scale(leftProbability).add(rightPDF.scale(rightProbability)); |
| 74 | break; |
| 75 | case PARALLEL: |
| 76 | // TODO add correct computation for parallel |
| 77 | resultPDF = leftPDF.mult(rightPDF); |
| 78 | break; |
| 79 | default: |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | return new ManagedPDF(resultPDF, true); |
| 84 | } |
| 85 | |
| 86 | } |