1 | package de.uka.ipd.sdq.spa.basicsolver.visitor; |
2 | |
3 | import java.util.Hashtable; |
4 | |
5 | import de.uka.ipd.sdq.probfunction.math.IProbabilityDensityFunction; |
6 | import de.uka.ipd.sdq.probfunction.math.exception.FunctionNotInFrequencyDomainException; |
7 | import de.uka.ipd.sdq.spa.basicsolver.visitor.perfhandler.PerformanceAlternativeHandler; |
8 | import de.uka.ipd.sdq.spa.basicsolver.visitor.perfhandler.PerformanceHandlerFactory; |
9 | import de.uka.ipd.sdq.spa.basicsolver.visitor.perfhandler.PerformanceLoopHandler; |
10 | import de.uka.ipd.sdq.spa.basicsolver.visitor.perfhandler.PerformanceSequenceHandler; |
11 | import de.uka.ipd.sdq.spa.basicsolver.visitor.perfhandler.PerformanceSymbolHandler; |
12 | import de.uka.ipd.sdq.spa.expression.Alternative; |
13 | import de.uka.ipd.sdq.spa.expression.Expression; |
14 | import de.uka.ipd.sdq.spa.expression.Loop; |
15 | import de.uka.ipd.sdq.spa.expression.Sequence; |
16 | import de.uka.ipd.sdq.spa.expression.Symbol; |
17 | import de.uka.ipd.sdq.spa.expression.util.ExpressionSwitch; |
18 | |
19 | public class PerformanceVisitor { |
20 | |
21 | private PerformanceAlternativeHandler altHandler; |
22 | |
23 | private PerformanceLoopHandler loopHandler; |
24 | |
25 | private PerformanceSequenceHandler seqHandler; |
26 | |
27 | private PerformanceSymbolHandler symHandler; |
28 | |
29 | private Hashtable<Expression, IProbabilityDensityFunction> pdfTable; |
30 | |
31 | private ExpressionSwitch exprswitch = new ExpressionSwitch() { |
32 | |
33 | @Override |
34 | public Object caseAlternative(Alternative alternative) { |
35 | doSwitch(alternative.getLeftOption().getRegexp()); |
36 | doSwitch(alternative.getRightOption().getRegexp()); |
37 | altHandler.handle(alternative); |
38 | return alternative; |
39 | } |
40 | |
41 | @Override |
42 | public Object caseLoop(Loop loop) { |
43 | doSwitch(loop.getRegExp()); |
44 | loopHandler.handle(loop); |
45 | return loop; |
46 | } |
47 | |
48 | @Override |
49 | public Object caseSequence(Sequence sequence) { |
50 | doSwitch(sequence.getLeftRegExp()); |
51 | doSwitch(sequence.getRightRegExp()); |
52 | seqHandler.handle(sequence); |
53 | return sequence; |
54 | } |
55 | |
56 | @Override |
57 | public Object caseSymbol(Symbol symbol) { |
58 | symHandler.handle(symbol); |
59 | return symbol; |
60 | } |
61 | |
62 | }; |
63 | |
64 | |
65 | private PerformanceVisitor(){ |
66 | super(); |
67 | } |
68 | |
69 | public PerformanceVisitor(PerformanceAlternativeHandler altHandler, |
70 | PerformanceLoopHandler loopHandler, |
71 | PerformanceSequenceHandler seqHandler, |
72 | PerformanceSymbolHandler symHandler, Hashtable<Expression, IProbabilityDensityFunction> pdfTable) { |
73 | this(); |
74 | this.altHandler = altHandler; |
75 | this.loopHandler = loopHandler; |
76 | this.seqHandler = seqHandler; |
77 | this.symHandler = symHandler; |
78 | this.pdfTable = pdfTable; |
79 | } |
80 | |
81 | public PerformanceVisitor(PerformanceHandlerFactory factory) { |
82 | this(); |
83 | this.altHandler = factory.createAlternativeHandler(); |
84 | this.seqHandler = factory.createSequenceHandler(); |
85 | this.loopHandler = factory.createLoopHandler(); |
86 | this.symHandler = factory.createSymbolHandler(); |
87 | pdfTable = factory.getPdfTable(); |
88 | } |
89 | |
90 | public IProbabilityDensityFunction getResponseTime( |
91 | Expression expression) { |
92 | try { |
93 | if (pdfTable.get(expression) == null) { |
94 | exprswitch.doSwitch(expression); |
95 | } |
96 | return pdfTable.get(expression).getInverseFourierTransform(); |
97 | } catch (FunctionNotInFrequencyDomainException e) { |
98 | e.printStackTrace(); |
99 | System.exit(1); |
100 | } |
101 | return null; |
102 | } |
103 | |
104 | public void reset() { |
105 | pdfTable.clear(); |
106 | } |
107 | |
108 | public void setPDFTable( |
109 | Hashtable<Expression, IProbabilityDensityFunction> pdfTable) { |
110 | this.pdfTable = pdfTable; |
111 | loopHandler.setPdfTable(pdfTable); |
112 | altHandler.setPdfTable(pdfTable); |
113 | seqHandler.setPdfTable(pdfTable); |
114 | symHandler.setPdfTable(pdfTable); |
115 | } |
116 | |
117 | public AlternativeHandler getAltHandler() { |
118 | return altHandler; |
119 | } |
120 | |
121 | public void setAltHandler(PerformanceAlternativeHandler altHandler) { |
122 | this.altHandler = altHandler; |
123 | } |
124 | |
125 | public LoopHandler getLoopHandler() { |
126 | return loopHandler; |
127 | } |
128 | |
129 | public void setLoopHandler(PerformanceLoopHandler loopHandler) { |
130 | this.loopHandler = loopHandler; |
131 | } |
132 | |
133 | public SequenceHandler getSeqHandler() { |
134 | return seqHandler; |
135 | } |
136 | |
137 | public void setSeqHandler(PerformanceSequenceHandler seqHandler) { |
138 | this.seqHandler = seqHandler; |
139 | } |
140 | |
141 | public SymbolHandler getSymHandler() { |
142 | return symHandler; |
143 | } |
144 | |
145 | public void setSymHandler(PerformanceSymbolHandler symHandler) { |
146 | this.symHandler = symHandler; |
147 | } |
148 | } |