| 1 | package de.uka.ipd.sdq.pcmsolver.transformations; |
| 2 | |
| 3 | import org.apache.log4j.Logger; |
| 4 | |
| 5 | import de.uka.ipd.sdq.pcm.core.CoreFactory; |
| 6 | import de.uka.ipd.sdq.pcm.core.PCMRandomVariable; |
| 7 | import de.uka.ipd.sdq.pcm.seff.seff_performance.ParametricResourceDemand; |
| 8 | import de.uka.ipd.sdq.probfunction.ProbabilityDensityFunction; |
| 9 | import de.uka.ipd.sdq.probfunction.ProbabilityFunction; |
| 10 | import de.uka.ipd.sdq.probfunction.ProbabilityMassFunction; |
| 11 | import de.uka.ipd.sdq.probfunction.ProbfunctionFactory; |
| 12 | import de.uka.ipd.sdq.probfunction.math.IContinousPDF; |
| 13 | import de.uka.ipd.sdq.probfunction.math.IProbabilityDensityFunction; |
| 14 | import de.uka.ipd.sdq.probfunction.math.ManagedPDF; |
| 15 | import de.uka.ipd.sdq.probfunction.math.ManagedPMF; |
| 16 | import de.uka.ipd.sdq.probfunction.math.PDFConfiguration; |
| 17 | import de.uka.ipd.sdq.probfunction.math.exception.ConfigurationNotSetException; |
| 18 | import de.uka.ipd.sdq.probfunction.math.exception.DomainNotNumbersException; |
| 19 | import de.uka.ipd.sdq.probfunction.math.exception.FunctionNotInTimeDomainException; |
| 20 | import de.uka.ipd.sdq.stoex.DoubleLiteral; |
| 21 | import de.uka.ipd.sdq.stoex.Expression; |
| 22 | import de.uka.ipd.sdq.stoex.FunctionLiteral; |
| 23 | import de.uka.ipd.sdq.stoex.IntLiteral; |
| 24 | import de.uka.ipd.sdq.stoex.NumericLiteral; |
| 25 | import de.uka.ipd.sdq.stoex.ProbabilityFunctionLiteral; |
| 26 | import de.uka.ipd.sdq.stoex.analyser.probfunction.ProbfunctionHelper; |
| 27 | import de.uka.ipd.sdq.stoex.analyser.visitors.StoExPrettyPrintVisitor; |
| 28 | |
| 29 | /** |
| 30 | * Wraps the actual content of an expression to allow computation with it. |
| 31 | * Only supports probability density function or double so far. |
| 32 | * Allows to access the expression as a PDF or to get the mean value. |
| 33 | * @author martens |
| 34 | * |
| 35 | */ |
| 36 | public class ExpressionToPDFWrapper { |
| 37 | ProbabilityDensityFunction pdf; |
| 38 | Double meanValue; |
| 39 | Double standardDeviation; |
| 40 | boolean originalPDF; |
| 41 | |
| 42 | protected static Logger logger = Logger.getLogger("de.uka.ipd.sdq.pcmsolver.transformations"); |
| 43 | |
| 44 | public ExpressionToPDFWrapper(ProbabilityDensityFunction pdf){ |
| 45 | this.pdf = pdf; |
| 46 | this.originalPDF = true; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Can create a wrapper for {@link ProbabilityFunctionLiteral}s and {@link NumericLiteral}s. |
| 51 | * @param rdExpression |
| 52 | * @return |
| 53 | */ |
| 54 | public static ExpressionToPDFWrapper createExpressionToPDFWrapper( |
| 55 | Expression rdExpression) { |
| 56 | if (rdExpression instanceof ProbabilityFunctionLiteral){ |
| 57 | ProbabilityFunctionLiteral probFuncLit = (ProbabilityFunctionLiteral) rdExpression; |
| 58 | ProbabilityFunction pf = probFuncLit.getFunction_ProbabilityFunctionLiteral(); |
| 59 | if (pf instanceof ProbabilityMassFunction){ |
| 60 | ProbabilityMassFunction pmf = (ProbabilityMassFunction)pf; |
| 61 | ManagedPMF managedPmf = new ManagedPMF(pmf); |
| 62 | double value = managedPmf.getExpectedValueDouble(); |
| 63 | return new ExpressionToPDFWrapper(value); |
| 64 | } else { |
| 65 | ProbabilityDensityFunction pdf = (ProbabilityDensityFunction)pf; |
| 66 | return new ExpressionToPDFWrapper(pdf); |
| 67 | } |
| 68 | } else if (rdExpression instanceof FunctionLiteral){ |
| 69 | FunctionLiteral fLit = (FunctionLiteral) rdExpression; |
| 70 | ProbabilityDensityFunction pdf = ProbfunctionHelper.createFunction(fLit.getParameters_FunctionLiteral(), fLit.getId(), ProbfunctionFactory.eINSTANCE); |
| 71 | return new ExpressionToPDFWrapper(pdf); |
| 72 | } else if (rdExpression instanceof NumericLiteral){ |
| 73 | Double value = getDoubleValueForNumericLiteral((NumericLiteral)rdExpression); |
| 74 | return new ExpressionToPDFWrapper(value); |
| 75 | } else { |
| 76 | StoExPrettyPrintVisitor printer = new StoExPrettyPrintVisitor(); |
| 77 | String solvedExprString = null; |
| 78 | if (rdExpression != null){ |
| 79 | solvedExprString = (String)printer.doSwitch(rdExpression); |
| 80 | } |
| 81 | throw new IllegalArgumentException("Handling expression "+solvedExprString+" in the ResourceDemandWrapper failed, could not cast it to "+ProbabilityFunctionLiteral.class+" or "+ FunctionLiteral.class); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | public ExpressionToPDFWrapper(Double meanValue){ |
| 86 | this.meanValue = meanValue; |
| 87 | this.standardDeviation = new Double(0); |
| 88 | this.originalPDF = false; |
| 89 | } |
| 90 | |
| 91 | public ProbabilityDensityFunction getPDF(){ |
| 92 | if (pdf == null && meanValue != null){ |
| 93 | ProbabilityDensityFunction pdfLit = convertLiteralsToPDFs(this.meanValue); |
| 94 | this.pdf = pdfLit; |
| 95 | } |
| 96 | return pdf; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | public Double getMeanValue(){ |
| 101 | if (meanValue == null && pdf != null){ |
| 102 | ManagedPDF mpdf = new ManagedPDF(pdf); |
| 103 | try { |
| 104 | Double value = new Double(mpdf.getPdfTimeDomain().getArithmeticMeanValue()); |
| 105 | this.meanValue = value; |
| 106 | } catch (DomainNotNumbersException e) { |
| 107 | ContextWrapper.logger.error("Error calculating arithmetic mean value.", e); |
| 108 | e.printStackTrace(); |
| 109 | } catch (FunctionNotInTimeDomainException e) { |
| 110 | ContextWrapper.logger.error("Error calculating arithmetic mean value.", e); |
| 111 | e.printStackTrace(); |
| 112 | } catch (RuntimeException e){ |
| 113 | logger.error("Could not get mean value of PDF "+pdf.toString()); |
| 114 | throw e; |
| 115 | } |
| 116 | } |
| 117 | return meanValue; |
| 118 | } |
| 119 | |
| 120 | public Double getStandardDeviation(){ |
| 121 | if (this.standardDeviation == null && this.pdf != null){ |
| 122 | ManagedPDF mpdf = new ManagedPDF(pdf); |
| 123 | try { |
| 124 | IProbabilityDensityFunction probFunction = mpdf.getPdfTimeDomain(); |
| 125 | if (probFunction instanceof IContinousPDF){ |
| 126 | Double stdev = new Double(((IContinousPDF)probFunction).getStandardDeviation()); |
| 127 | this.standardDeviation = stdev; |
| 128 | } else { |
| 129 | this.standardDeviation = Double.NaN; |
| 130 | } |
| 131 | |
| 132 | } catch (DomainNotNumbersException e) { |
| 133 | ContextWrapper.logger.error("Error calculating arithmetic mean value.", e); |
| 134 | e.printStackTrace(); |
| 135 | } catch (FunctionNotInTimeDomainException e) { |
| 136 | ContextWrapper.logger.error("Error calculating arithmetic mean value.", e); |
| 137 | e.printStackTrace(); |
| 138 | } |
| 139 | } |
| 140 | return this.standardDeviation; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /** |
| 145 | * Converts the passed double to a PCMRandomVariable with a DoublePDF inside. |
| 146 | * Modifies the passed {@link PCMRandomVariable} and sets a DoublePDF "around" the |
| 147 | * resource demand if the resource demand was a constant. The |
| 148 | * DoublePDF depends on the distance in {@link PDFConfiguration#getCurrentConfiguration()}. |
| 149 | * @param actResDemSpecification |
| 150 | * @param rv |
| 151 | * @return |
| 152 | */ |
| 153 | private ProbabilityDensityFunction convertLiteralsToPDFs(Double demand) { |
| 154 | |
| 155 | double distance = 0.1; |
| 156 | try { |
| 157 | distance = PDFConfiguration.getCurrentConfiguration().getDistance(); |
| 158 | } catch (ConfigurationNotSetException e) { |
| 159 | e.printStackTrace(); |
| 160 | throw new RuntimeException("Converting literal to pdf failed, wring initialisation. ",e); |
| 161 | } |
| 162 | //Ensure that demand is larger than 0 (for the included loop to terminate). |
| 163 | //Hopefully, a negative demand is caught elsewhere... |
| 164 | if (demand > 0 && distance > 0){ |
| 165 | while (demand-distance<=0) distance/=10; |
| 166 | } |
| 167 | Double firstValue = new Double(demand-distance); |
| 168 | String newDemand = "DoublePDF[(" + |
| 169 | firstValue.toString()+ |
| 170 | ";0.0)("+demand+";1.0)(" + |
| 171 | new Double(demand+distance).toString()+";0.0)]"; |
| 172 | |
| 173 | PCMRandomVariable rv = CoreFactory.eINSTANCE.createPCMRandomVariable(); |
| 174 | rv.setSpecification(newDemand); |
| 175 | |
| 176 | ProbabilityFunctionLiteral exp = (ProbabilityFunctionLiteral)rv.getExpression(); |
| 177 | return (ProbabilityDensityFunction) exp.getFunction_ProbabilityFunctionLiteral(); |
| 178 | |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Returns whether this resource demand is a derived pdf (i.e. it has been created using a mean value) |
| 183 | * or whether it is an original pdf (i.e. the mean value is derived). |
| 184 | * @return true if it has been created using {@link #ResourceDemandWrapper(ParametricResourceDemand, ProbabilityDensityFunction)}, |
| 185 | * false if this has been created using {@link #ResourceDemandWrapper(ParametricResourceDemand, Double)}. |
| 186 | */ |
| 187 | public boolean isOriginalPDF(){ |
| 188 | return this.originalPDF; |
| 189 | } |
| 190 | |
| 191 | private static Double getDoubleValueForNumericLiteral(NumericLiteral rdExpression) { |
| 192 | double value = 0; |
| 193 | if (rdExpression instanceof DoubleLiteral){ |
| 194 | value = ((DoubleLiteral) rdExpression).getValue() ; |
| 195 | } else if (rdExpression instanceof IntLiteral){ |
| 196 | value = ((IntLiteral) rdExpression).getValue(); |
| 197 | } else throw new RuntimeException("Unknown type of numeric literal: "+rdExpression.getClass()); |
| 198 | return value; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | } |