| 1 | package de.uka.ipd.sdq.pcmsolver.visitors; |
| 2 | |
| 3 | import java.util.HashMap; |
| 4 | |
| 5 | import org.antlr.runtime.ANTLRStringStream; |
| 6 | import org.antlr.runtime.CommonTokenStream; |
| 7 | import org.antlr.runtime.RecognitionException; |
| 8 | |
| 9 | import de.uka.ipd.sdq.pcm.core.PCMRandomVariable; |
| 10 | import de.uka.ipd.sdq.pcm.stochasticexpressions.parser.PCMStoExLexer; |
| 11 | import de.uka.ipd.sdq.pcm.stochasticexpressions.parser.PCMStoExParser; |
| 12 | import de.uka.ipd.sdq.pcmsolver.transformations.ContextWrapper; |
| 13 | import de.uka.ipd.sdq.pcmsolver.transformations.ExpressionToPDFWrapper; |
| 14 | import de.uka.ipd.sdq.stoex.Expression; |
| 15 | import de.uka.ipd.sdq.stoex.analyser.visitors.ExpressionInferTypeVisitor; |
| 16 | import de.uka.ipd.sdq.stoex.analyser.visitors.StoExPrettyPrintVisitor; |
| 17 | import de.uka.ipd.sdq.stoex.analyser.visitors.TypeEnum; |
| 18 | |
| 19 | public class ExpressionHelper { |
| 20 | |
| 21 | /** |
| 22 | * @param specification |
| 23 | */ |
| 24 | public static Expression parseToExpression(String specification) { |
| 25 | Expression expression = null; |
| 26 | PCMStoExLexer lexer = new PCMStoExLexer( |
| 27 | new ANTLRStringStream(specification)); |
| 28 | PCMStoExParser parser = new PCMStoExParser(new CommonTokenStream(lexer)); |
| 29 | try { |
| 30 | expression = parser.expression(); |
| 31 | } catch (RecognitionException e) { |
| 32 | // TODO Auto-generated catch block |
| 33 | e.printStackTrace(); |
| 34 | } |
| 35 | return expression; |
| 36 | } |
| 37 | |
| 38 | public static String getSolvedExpressionAsString(String specification, ContextWrapper ctxWrp){ |
| 39 | Expression solvedExpression = getSolvedExpression(specification, ctxWrp); |
| 40 | |
| 41 | StoExPrettyPrintVisitor printer = new StoExPrettyPrintVisitor(); |
| 42 | String solvedExprString = (String)printer.doSwitch(solvedExpression); |
| 43 | |
| 44 | if (solvedExpression == null){ |
| 45 | throw new RuntimeException("Could not print solved expression "+specification); |
| 46 | } |
| 47 | |
| 48 | return solvedExprString; |
| 49 | } |
| 50 | |
| 51 | public static Expression getSolvedExpression(String specification, |
| 52 | ContextWrapper ctxWrp) { |
| 53 | Expression expr = parseToExpression(specification); |
| 54 | |
| 55 | ExpressionInferTypeVisitor inferTypeVisitor = new ExpressionInferTypeVisitor(); |
| 56 | inferTypeVisitor.doSwitch(expr); |
| 57 | |
| 58 | HashMap<Expression, TypeEnum> typeAnnotation = inferTypeVisitor |
| 59 | .getTypeAnnotation(); |
| 60 | |
| 61 | ExpressionParameterSolverVisitor solveVisitor = new ExpressionParameterSolverVisitor( |
| 62 | typeAnnotation, ctxWrp); |
| 63 | |
| 64 | return (Expression) solveVisitor.doSwitch(expr); |
| 65 | } |
| 66 | |
| 67 | public static HashMap<Expression,TypeEnum> getTypeAnnotation(Expression expr){ |
| 68 | ExpressionInferTypeVisitor inferTypeVisitor = |
| 69 | new ExpressionInferTypeVisitor(); |
| 70 | inferTypeVisitor.doSwitch(expr); |
| 71 | return inferTypeVisitor.getTypeAnnotation(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * calculated the mean value for a solved expression |
| 76 | * @param expression |
| 77 | * @return |
| 78 | * @throws IllegalArgumentException if the passed expression has not been solved before. |
| 79 | */ |
| 80 | public static double meanValue(Expression expression) throws IllegalArgumentException { |
| 81 | ExpressionToPDFWrapper wrapper = ExpressionToPDFWrapper.createExpressionToPDFWrapper(expression); |
| 82 | return wrapper.getMeanValue(); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | public static double getMeanValue(PCMRandomVariable variable){ |
| 87 | ExpressionToPDFWrapper expToPDF = ExpressionToPDFWrapper.createExpressionToPDFWrapper(variable.getExpression()); |
| 88 | return expToPDF.getMeanValue(); |
| 89 | } |
| 90 | } |