| 1 | package de.uka.ipd.sdq.simucomframework.variables.cache; |
| 2 | |
| 3 | import java.util.Iterator; |
| 4 | |
| 5 | import org.antlr.runtime.ANTLRStringStream; |
| 6 | import org.antlr.runtime.CommonTokenStream; |
| 7 | import org.antlr.runtime.RecognitionException; |
| 8 | import org.eclipse.emf.ecore.EObject; |
| 9 | |
| 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.probfunction.math.IProbabilityFunction; |
| 13 | import de.uka.ipd.sdq.stoex.Expression; |
| 14 | import de.uka.ipd.sdq.stoex.analyser.visitors.ExpressionInferTypeVisitor; |
| 15 | import de.uka.ipd.sdq.stoex.analyser.visitors.NonProbabilisticExpressionInferTypeVisitor; |
| 16 | import de.uka.ipd.sdq.stoex.analyser.visitors.TypeEnum; |
| 17 | |
| 18 | /** |
| 19 | * An entry in the stoex cache containing all information given by parsing and |
| 20 | * analysing the stoex |
| 21 | * @author Steffen Becker |
| 22 | * |
| 23 | */ |
| 24 | /** |
| 25 | * @author Steffen Becker |
| 26 | * |
| 27 | */ |
| 28 | public class StoExCacheEntry { |
| 29 | |
| 30 | private final String spec; |
| 31 | private final Expression parsedExpression; |
| 32 | private final ExpressionInferTypeVisitor typeInferer; |
| 33 | private final ProbFunctionCache probFunctionCache; |
| 34 | |
| 35 | /** |
| 36 | * Parse the expression and store its AST as well as its infered types |
| 37 | * @param spec The stoex to parse and store |
| 38 | */ |
| 39 | public StoExCacheEntry(String spec) { |
| 40 | this.spec = spec; |
| 41 | PCMStoExLexer lexer = new PCMStoExLexer( |
| 42 | new ANTLRStringStream(spec)); |
| 43 | Expression formula = null; |
| 44 | try { |
| 45 | formula = new PCMStoExParser(new CommonTokenStream(lexer)).expression(); |
| 46 | } catch (RecognitionException e) { |
| 47 | throw new RuntimeException("Expression not parsable \""+spec+"\"",e); |
| 48 | } catch (Exception e) { |
| 49 | throw new RuntimeException("Expression not parsable \""+spec+"\"",e); |
| 50 | } |
| 51 | try { |
| 52 | typeInferer = new NonProbabilisticExpressionInferTypeVisitor(); |
| 53 | typeInferer.doSwitch(formula); |
| 54 | } catch (Exception e) { |
| 55 | throw new RuntimeException("Expression not parsable \""+spec+"\"",e); |
| 56 | } |
| 57 | this.parsedExpression = formula; |
| 58 | this.probFunctionCache = new ProbFunctionCache(formula); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @return The AST of the cached stoex |
| 63 | */ |
| 64 | public Expression getParsedExpression() { |
| 65 | return parsedExpression; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @return Original stoex as string |
| 70 | */ |
| 71 | public String getSpec() { |
| 72 | return spec; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @return Type inferer for this stoex |
| 77 | */ |
| 78 | public ExpressionInferTypeVisitor getTypeInferer() { |
| 79 | return typeInferer; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @param e Return the infered type of the given subexpression of this |
| 84 | * stoex |
| 85 | * @return The type of the given subexpression or null if the subexpression |
| 86 | * is not part of this stoex |
| 87 | */ |
| 88 | public TypeEnum getInferedType(Expression e) { |
| 89 | return typeInferer.getType(e); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @return Probfunction cache used to cache the probfunctions of this stoex |
| 94 | */ |
| 95 | public ProbFunctionCache getProbFunctionCache() { |
| 96 | return probFunctionCache; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get the probfunction from the probfunction cache |
| 101 | * @param e A subexpression which represents a probfunction literal |
| 102 | * @return The probfunction for the given subexpression |
| 103 | */ |
| 104 | public IProbabilityFunction getProbFunction(EObject e) { |
| 105 | assert contains(getParsedExpression().eAllContents(),e); |
| 106 | return probFunctionCache.getProbFunction(e); |
| 107 | } |
| 108 | |
| 109 | private boolean contains(Iterator<EObject> i, EObject e) { |
| 110 | while (i.hasNext()) { |
| 111 | EObject n = i.next(); |
| 112 | if (n == e) return true; |
| 113 | } |
| 114 | return false; |
| 115 | } |
| 116 | } |