| 1 | package de.uka.ipd.sdq.probfunction.math; |
| 2 | |
| 3 | import java.util.Iterator; |
| 4 | import java.util.List; |
| 5 | |
| 6 | import org.antlr.runtime.ANTLRStringStream; |
| 7 | import org.antlr.runtime.CommonTokenStream; |
| 8 | import org.antlr.runtime.RecognitionException; |
| 9 | |
| 10 | import de.uka.ipd.sdq.probfunction.ProbabilityMassFunction; |
| 11 | import de.uka.ipd.sdq.probfunction.math.exception.StringNotPDFException; |
| 12 | import de.uka.ipd.sdq.probfunction.print.ProbFunctionPrettyPrint; |
| 13 | import de.uka.ipd.sdq.stoex.ProbabilityFunctionLiteral; |
| 14 | import de.uka.ipd.sdq.stoex.parser.StochasticExpressionsLexer; |
| 15 | import de.uka.ipd.sdq.stoex.parser.StochasticExpressionsParser; |
| 16 | |
| 17 | /** |
| 18 | * To be continued... |
| 19 | * |
| 20 | * @author jens |
| 21 | * |
| 22 | */ |
| 23 | public class ManagedPMF { |
| 24 | |
| 25 | private IProbabilityFunctionFactory pfFactory = IProbabilityFunctionFactory.eINSTANCE; |
| 26 | |
| 27 | private IProbabilityMassFunction pmfTimeDomain; |
| 28 | |
| 29 | private ProbabilityMassFunction modelPmf; |
| 30 | |
| 31 | private String pmfAsString; |
| 32 | |
| 33 | private ManagedPMF() { |
| 34 | reset(); |
| 35 | } |
| 36 | |
| 37 | public ManagedPMF(IProbabilityMassFunction pmf) { |
| 38 | this(); |
| 39 | pmfTimeDomain = pmf; |
| 40 | } |
| 41 | |
| 42 | public ManagedPMF(ProbabilityMassFunction modelPMF) { |
| 43 | this(); |
| 44 | this.modelPmf = modelPMF; |
| 45 | } |
| 46 | |
| 47 | private void reset() { |
| 48 | pmfTimeDomain = null; |
| 49 | modelPmf = null; |
| 50 | pmfAsString = null; |
| 51 | } |
| 52 | |
| 53 | public IProbabilityMassFunction getPmfTimeDomain() { |
| 54 | if (pmfTimeDomain == null) { |
| 55 | pmfTimeDomain = pfFactory.transformToPMF(modelPmf); |
| 56 | } |
| 57 | return pmfTimeDomain; |
| 58 | } |
| 59 | |
| 60 | public ProbabilityMassFunction getModelPmf() { |
| 61 | if (modelPmf == null) { |
| 62 | modelPmf = pfFactory.transformToModelPMF(pmfTimeDomain); |
| 63 | } |
| 64 | return modelPmf; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public String toString() { |
| 69 | if (pmfAsString == null) { |
| 70 | ProbabilityMassFunction pmf = getModelPmf(); |
| 71 | ProbFunctionPrettyPrint pp = new ProbFunctionPrettyPrint(); |
| 72 | pmfAsString = (String) pp.doSwitch(pmf); |
| 73 | } |
| 74 | return pmfAsString; |
| 75 | } |
| 76 | |
| 77 | @SuppressWarnings("unchecked") |
| 78 | public Object getMaxValue() { |
| 79 | IProbabilityMassFunction pmf = getPmfTimeDomain(); |
| 80 | if (pmf.hasOrderedDomain()) { |
| 81 | Object max = null; |
| 82 | for (Iterator iter = pmf.getSamples().iterator(); iter.hasNext();) { |
| 83 | ISample sample = (ISample) iter.next(); |
| 84 | Object obj = sample.getValue(); |
| 85 | if ((max == null) || ( ((Comparable)max).compareTo(obj) < 0)) { |
| 86 | max = obj; |
| 87 | } |
| 88 | } |
| 89 | return max; |
| 90 | } else { |
| 91 | return null; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | public Long getExpectedValue() { |
| 96 | IProbabilityMassFunction pmf = getPmfTimeDomain(); |
| 97 | List<ISample> sampleList = pmf.getSamples(); |
| 98 | double result = 0.0; |
| 99 | for (ISample sample : sampleList){ |
| 100 | Integer value = (Integer)sample.getValue(); |
| 101 | result += value.doubleValue() * sample.getProbability(); |
| 102 | } |
| 103 | return new Long(Math.round(result)); |
| 104 | } |
| 105 | |
| 106 | public Double getExpectedValueDouble() { |
| 107 | IProbabilityMassFunction pmf = getPmfTimeDomain(); |
| 108 | List<ISample> sampleList = pmf.getSamples(); |
| 109 | double result = 0.0; |
| 110 | for (ISample sample : sampleList){ |
| 111 | Number value = (Number)sample.getValue(); |
| 112 | result += value.doubleValue() * sample.getProbability(); |
| 113 | } |
| 114 | return result; |
| 115 | } |
| 116 | |
| 117 | private static ProbabilityFunctionLiteral parse(String s) throws RecognitionException { |
| 118 | try{ |
| 119 | int iterInt = Integer.parseInt(s); |
| 120 | s = "IntPMF[("+iterInt+";1.0)]"; |
| 121 | } |
| 122 | catch(NumberFormatException e){ |
| 123 | } |
| 124 | |
| 125 | StochasticExpressionsLexer lexer = new StochasticExpressionsLexer( |
| 126 | new ANTLRStringStream(s)); |
| 127 | StochasticExpressionsParser parser = new StochasticExpressionsParser( |
| 128 | new CommonTokenStream(lexer)); |
| 129 | return (ProbabilityFunctionLiteral)parser.expression(); |
| 130 | } |
| 131 | |
| 132 | @SuppressWarnings("deprecation") |
| 133 | public static ManagedPMF createFromString(String pmfAsString) |
| 134 | throws RecognitionException, |
| 135 | StringNotPDFException { |
| 136 | ProbabilityFunctionLiteral value = parse(pmfAsString); |
| 137 | try { |
| 138 | ProbabilityMassFunction pmf = (ProbabilityMassFunction) value |
| 139 | .getFunction_ProbabilityFunctionLiteral(); |
| 140 | return new ManagedPMF(pmf); |
| 141 | } catch (ClassCastException e) { |
| 142 | throw new StringNotPDFException(); |
| 143 | } |
| 144 | } |
| 145 | } |