| 1 | package de.uka.ipd.sdq.stoex.analyser.probfunction; |
| 2 | |
| 3 | import java.util.List; |
| 4 | |
| 5 | |
| 6 | import de.uka.ipd.sdq.probfunction.ContinuousPDF; |
| 7 | import de.uka.ipd.sdq.probfunction.ExponentialDistribution; |
| 8 | import de.uka.ipd.sdq.probfunction.GammaDistribution; |
| 9 | import de.uka.ipd.sdq.probfunction.LognormalDistribution; |
| 10 | import de.uka.ipd.sdq.probfunction.NormalDistribution; |
| 11 | import de.uka.ipd.sdq.probfunction.ProbfunctionFactory; |
| 12 | import de.uka.ipd.sdq.probfunction.math.IGammaDistribution; |
| 13 | import de.uka.ipd.sdq.probfunction.math.ILognormalDistribution; |
| 14 | |
| 15 | import de.uka.ipd.sdq.probfunction.math.impl.ProbabilityFunctionFactoryImpl; |
| 16 | import de.uka.ipd.sdq.stoex.DoubleLiteral; |
| 17 | import de.uka.ipd.sdq.stoex.Expression; |
| 18 | import de.uka.ipd.sdq.stoex.IntLiteral; |
| 19 | import de.uka.ipd.sdq.stoex.NegativeExpression; |
| 20 | import de.uka.ipd.sdq.stoex.NumericLiteral; |
| 21 | import de.uka.ipd.sdq.stoex.Unary; |
| 22 | |
| 23 | public class ProbfunctionHelper { |
| 24 | |
| 25 | public static final String LOGNORM = "Lognorm"; |
| 26 | public static final String LOGNORM2 = "LognormMoments"; |
| 27 | public static final String GAMMA = "Gamma"; |
| 28 | public static final String GAMMA2 = "GammaMoments"; |
| 29 | public static final String NORM = "Norm"; |
| 30 | |
| 31 | public static final Object EXP = "Exp"; |
| 32 | public static final Object POIS = "Pois"; |
| 33 | public static final Object UNIINT = "UniInt"; |
| 34 | public static final Object UNIDOUBLE = "UniDouble"; |
| 35 | public static final Object BINOM = "Binom"; |
| 36 | |
| 37 | public static ContinuousPDF createFunction(List<Expression> parameters, String type, ProbfunctionFactory probFuncFactory) { |
| 38 | |
| 39 | try { |
| 40 | |
| 41 | if (type.equals(LOGNORM)){ |
| 42 | LognormalDistribution lognorm = probFuncFactory.createLognormalDistribution(); |
| 43 | lognorm.setMu(getDoubleValue((Unary)parameters.get(0))); |
| 44 | lognorm.setSigma(getDoubleValue((NumericLiteral)parameters.get(1))); |
| 45 | return lognorm; |
| 46 | } else if (type.equals(LOGNORM2)){ |
| 47 | LognormalDistribution lognorm = probFuncFactory.createLognormalDistribution(); |
| 48 | double mean = getDoubleValue((NumericLiteral)parameters.get(0)); |
| 49 | double stdev = getDoubleValue((NumericLiteral)parameters.get(1)); |
| 50 | double variance = stdev * stdev ; |
| 51 | ILognormalDistribution dist = ProbabilityFunctionFactoryImpl.getInstance().getPDFFactory().createLognormalDistributionFromMoments(mean, variance); |
| 52 | lognorm.setMu(dist.getMu()); |
| 53 | lognorm.setSigma(dist.getSigma()); |
| 54 | return lognorm; |
| 55 | } else if (type.equals(GAMMA)){ |
| 56 | GammaDistribution gamma = probFuncFactory.createGammaDistribution(); |
| 57 | gamma.setAlpha(getDoubleValue((NumericLiteral)parameters.get(0))); |
| 58 | gamma.setTheta(getDoubleValue((NumericLiteral)parameters.get(1))); |
| 59 | return gamma; |
| 60 | } else if (type.equals(GAMMA2)){ |
| 61 | GammaDistribution gamma = probFuncFactory.createGammaDistribution(); |
| 62 | double mean = getDoubleValue((NumericLiteral)parameters.get(0)); |
| 63 | double coeffVar = getDoubleValue((NumericLiteral)parameters.get(1)); |
| 64 | IGammaDistribution dist = ProbabilityFunctionFactoryImpl.getInstance().getPDFFactory().createGammaDistributionFromMoments(mean, coeffVar); |
| 65 | gamma.setAlpha(dist.getAlpha()); |
| 66 | gamma.setTheta(dist.getTheta()); |
| 67 | return gamma; |
| 68 | } else if (type.equals(NORM)){ |
| 69 | NormalDistribution expo = probFuncFactory.createNormalDistribution(); |
| 70 | double mean = getDoubleValue((NumericLiteral)parameters.get(0)); |
| 71 | double sigma = getDoubleValue((NumericLiteral)parameters.get(1)); |
| 72 | expo.setMu(mean); |
| 73 | expo.setSigma(sigma); |
| 74 | return expo; |
| 75 | } else if (type.equals(EXP)){ |
| 76 | ExponentialDistribution exp = probFuncFactory.createExponentialDistribution(); |
| 77 | exp.setRate(getDoubleValue((NumericLiteral)parameters.get(0))); |
| 78 | return exp; |
| 79 | } else throw new UnsupportedOperationException("Function "+type+" not supported!"); |
| 80 | } catch (IndexOutOfBoundsException e){ |
| 81 | throw new UnsupportedOperationException("Function "+type+" needs more parameters. See help and stacktrace.", e); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private static double getDoubleValue(Unary unary) { |
| 86 | if (unary instanceof DoubleLiteral){ |
| 87 | return ((DoubleLiteral)unary).getValue(); |
| 88 | } else if (unary instanceof IntLiteral){ |
| 89 | return ((IntLiteral)unary).getValue(); |
| 90 | } else if (unary instanceof NegativeExpression){ |
| 91 | return (-1.0) * getDoubleValue(((NegativeExpression)unary).getInner()); |
| 92 | } else { |
| 93 | throw new RuntimeException("Unknown numeric literal "+unary.getClass().getName()+" encountered. Cannot handle it, aborting. Please contact developers."); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public static boolean isFunctionWithTwoParameterID(String id){ |
| 98 | if (id.equals(LOGNORM) |
| 99 | || id.equals(LOGNORM2) |
| 100 | || id.equals(NORM) |
| 101 | || id.equals(GAMMA) |
| 102 | || id.equals(GAMMA2)) |
| 103 | return true; |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | public static boolean isFunctionWithOneParameterID(String id){ |
| 108 | if (id.equals(EXP) |
| 109 | || id.equals(BINOM) |
| 110 | || id.equals(POIS) |
| 111 | || id.equals(UNIINT)) |
| 112 | return true; |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | public static boolean isFunctionID(String id){ |
| 117 | if (isFunctionWithOneParameterID(id) |
| 118 | || isFunctionWithTwoParameterID(id)) |
| 119 | return true; |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | } |