1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.pcmsolver; |
5 | |
6 | import java.io.FileInputStream; |
7 | import java.io.FileNotFoundException; |
8 | import java.io.IOException; |
9 | import java.util.Properties; |
10 | import java.util.concurrent.TimeUnit; |
11 | |
12 | import org.apache.log4j.BasicConfigurator; |
13 | import org.apache.log4j.ConsoleAppender; |
14 | import org.apache.log4j.Logger; |
15 | import org.apache.log4j.PatternLayout; |
16 | import org.eclipse.debug.core.ILaunchConfiguration; |
17 | |
18 | import de.uka.ipd.sdq.pcm.usagemodel.UsageScenario; |
19 | import de.uka.ipd.sdq.pcmsolver.models.PCMInstance; |
20 | import de.uka.ipd.sdq.pcmsolver.transformations.pcm2regex.ExpressionPrinter; |
21 | import de.uka.ipd.sdq.pcmsolver.transformations.pcm2regex.TransformUsageModelVisitor; |
22 | import de.uka.ipd.sdq.pcmsolver.visitors.UsageModelVisitor; |
23 | import de.uka.ipd.sdq.pcmsolver.visualisation.JFVisualisation; |
24 | import de.uka.ipd.sdq.probfunction.math.IProbabilityDensityFunction; |
25 | import de.uka.ipd.sdq.probfunction.math.IProbabilityFunctionFactory; |
26 | import de.uka.ipd.sdq.probfunction.math.ISamplePDF; |
27 | import de.uka.ipd.sdq.probfunction.math.PDFConfiguration; |
28 | import de.uka.ipd.sdq.probfunction.math.exception.ConfigurationNotSetException; |
29 | import de.uka.ipd.sdq.probfunction.math.exception.ProbabilityFunctionException; |
30 | import de.uka.ipd.sdq.probfunction.math.exception.UnknownPDFTypeException; |
31 | import de.uka.ipd.sdq.spa.expression.Expression; |
32 | |
33 | /** |
34 | * TODO: It seems that this class is never used, and the functionality expected here is implemented elsewhere. |
35 | * |
36 | * The DependencySolver (DS) is a tool to substitute parameter names inside PCM |
37 | * stochastic expressions with characterisations originating from the usage |
38 | * model. In the usage model, the domain expert has to specify a variable |
39 | * characterisation (e.g., a constant or probability distribution) for each |
40 | * RequiredCharacterisation specified by component developers in Interfaces (see |
41 | * Chapter 4.1.4 of Heiko's dissertation). |
42 | * |
43 | * The DS propagates these characterisations through all elements of a PCM |
44 | * instance and inserts them into guard specifications, parametric loop |
45 | * iterations, parametric resource demands, and parameter usages specified by |
46 | * the component developer. Then, it solves the resulting stochastic |
47 | * expressions, so that they become constant values or probability |
48 | * distributions, and stores them, so that they can be used for a transformation |
49 | * into a performance model. |
50 | * |
51 | * The subsection on the dependencySolver in Heiko's dissertation first |
52 | * describes the expected input and produced output of the DS. Then, it |
53 | * describes the traversal of PCM instances, which depends on the evaluated |
54 | * parameter characterisations. Finally, it shows the process of solving |
55 | * dependencies, before giving an example. |
56 | * |
57 | * @see Heiko's dissertation, section 6.2 at |
58 | * http://docserver.bis.uni-oldenburg.de |
59 | * /_publikationen/dissertation/2008/kozpar08/pdf/kozpar08.pdf |
60 | * @author Koziolek |
61 | * |
62 | */ |
63 | public class DependencySolver { |
64 | |
65 | private static final int DOMAIN_SIZE = 32; |
66 | |
67 | private static final double DISTANCE = 0.1; |
68 | |
69 | |
70 | protected IProbabilityFunctionFactory iProbFuncFactory = |
71 | IProbabilityFunctionFactory.eINSTANCE; |
72 | |
73 | private Properties config; |
74 | private PCMInstance currentModel; |
75 | private static Logger logger = |
76 | Logger.getLogger(DependencySolver.class.getName()); |
77 | |
78 | public DependencySolver(Properties config){ |
79 | this.config = config; |
80 | logger.debug("Loading PCM Instance"); |
81 | currentModel = new PCMInstance(config); |
82 | |
83 | runDSolver(); |
84 | |
85 | Expression result = runPcm2RegEx(); |
86 | |
87 | //IProbabilityDensityFunction iPDF = runCalculation(result); |
88 | |
89 | //visualize(iPDF); |
90 | |
91 | } |
92 | |
93 | |
94 | public DependencySolver(ILaunchConfiguration configuration){ |
95 | PatternLayout myLayout = new PatternLayout("%d{HH:mm:ss,SSS} [%t] %-5p %c - %m%n"); |
96 | ConsoleAppender myAppender = new ConsoleAppender(myLayout); |
97 | BasicConfigurator.configure(myAppender); |
98 | |
99 | //currentModel = new PCMInstance(configuration); |
100 | |
101 | runDSolver(); |
102 | |
103 | Expression result = runPcm2RegEx(); |
104 | |
105 | //IProbabilityDensityFunction iPDF = runCalculation(result); |
106 | |
107 | //visualize(iPDF); |
108 | |
109 | } |
110 | |
111 | |
112 | |
113 | |
114 | /** |
115 | * @param result |
116 | */ |
117 | private IProbabilityDensityFunction runCalculation(Expression result) { |
118 | long timeBeforeCalc = System.nanoTime(); |
119 | // ClassicSPASolver solver = new ClassicSPASolver(); |
120 | // ManagedPDF resultPDF = solver.getResponseTime(result); |
121 | |
122 | // PerformanceHandlerFactory perfHandFac = new PerformanceHandlerFactory(DOMAIN_SIZE); |
123 | // PerformanceVisitor perfVisitor = new PerformanceVisitor(perfHandFac); |
124 | // IProbabilityDensityFunction iPDF = perfVisitor.getResponseTime(result); |
125 | |
126 | long timeAfterCalc = System.nanoTime(); |
127 | long duration3 = TimeUnit.NANOSECONDS.toMillis(timeAfterCalc-timeBeforeCalc); |
128 | logger.debug("Finished Calculation, Duration: "+ duration3 + " ms"); |
129 | // return iPDF; |
130 | //return resultPDF.getPdfTimeDomain(); |
131 | return null; |
132 | } |
133 | |
134 | /** |
135 | * @param iPDF |
136 | */ |
137 | private void visualize(IProbabilityDensityFunction iPDF) { |
138 | long timeBeforeVis = System.nanoTime(); |
139 | ISamplePDF samplePDF = null; |
140 | try { |
141 | samplePDF = iProbFuncFactory.transformToSamplePDF(iPDF); |
142 | } catch (UnknownPDFTypeException e1) { |
143 | // TODO Auto-generated catch block |
144 | e1.printStackTrace(); |
145 | } |
146 | |
147 | |
148 | try { |
149 | //JFVisualisation vis = new JFVisualisation(DISTANCE); |
150 | double dist = 0.0; |
151 | try { |
152 | dist = PDFConfiguration.getCurrentConfiguration().getDistance(); |
153 | } catch (ConfigurationNotSetException e) { |
154 | // TODO Auto-generated catch block |
155 | e.printStackTrace(); |
156 | } |
157 | JFVisualisation vis = new JFVisualisation(dist); |
158 | vis.addSamplePDF(samplePDF,"Execution Time"); |
159 | vis.visualizeOverlay(); |
160 | } catch (ProbabilityFunctionException e) { |
161 | e.printStackTrace(); |
162 | } |
163 | long timeAfterVis = System.nanoTime(); |
164 | long duration = TimeUnit.NANOSECONDS.toMillis(timeAfterVis-timeBeforeVis); |
165 | logger.debug("Finished Visualisati on, Duration: "+ duration + " ms"); |
166 | |
167 | } |
168 | |
169 | /** |
170 | * @return |
171 | */ |
172 | private Expression runPcm2RegEx() { |
173 | long timeBeforeTransform = System.nanoTime(); |
174 | Expression result = pcm2RegEx(currentModel); |
175 | long timeAfterTransform = System.nanoTime(); |
176 | long duration2 = TimeUnit.NANOSECONDS.toMillis(timeAfterTransform-timeBeforeTransform); |
177 | logger.debug("Finished Transform, Duration: "+ duration2 + " ms"); |
178 | return result; |
179 | } |
180 | |
181 | /** |
182 | * |
183 | */ |
184 | private void runDSolver() { |
185 | long startTime = System.nanoTime(); |
186 | visitScenarioEMFSwitch(); |
187 | currentModel.saveToFiles("SolvedDSolverExample1"); |
188 | long timeAfterDSolve = System.nanoTime(); |
189 | long duration = TimeUnit.NANOSECONDS.toMillis(timeAfterDSolve-startTime); |
190 | logger.debug("Finished Traversal, Saving; Duration: "+ duration + " ms"); |
191 | } |
192 | |
193 | /** |
194 | * |
195 | */ |
196 | private Expression pcm2RegEx(PCMInstance currentModel) { |
197 | TransformUsageModelVisitor umVisit = new TransformUsageModelVisitor(currentModel); |
198 | UsageScenario us = (UsageScenario)currentModel.getUsageModel().getUsageScenario_UsageModel().get(0); |
199 | Expression result = null; |
200 | try { |
201 | result = (Expression)umVisit.doSwitch(us.getScenarioBehaviour_UsageScenario()); |
202 | } catch (Exception e) { |
203 | logger.error("Usage Scenario caused Exception!" + e.getMessage()); |
204 | e.printStackTrace(); |
205 | } |
206 | |
207 | ExpressionPrinter expPrinter = new ExpressionPrinter(); |
208 | expPrinter.doSwitch(result); |
209 | System.out.println(); |
210 | |
211 | return result; |
212 | } |
213 | |
214 | private void visitScenarioEMFSwitch(){ |
215 | UsageModelVisitor visitor = new UsageModelVisitor(currentModel); |
216 | try { |
217 | UsageScenario us = (UsageScenario) currentModel.getUsageModel() |
218 | .getUsageScenario_UsageModel().get(0); |
219 | visitor.doSwitch(us.getScenarioBehaviour_UsageScenario()); |
220 | } catch (Exception e) { |
221 | logger.error("Usage Scenario caused Exception!" + e.getMessage()); |
222 | e.printStackTrace(); |
223 | } |
224 | |
225 | } |
226 | |
227 | /** |
228 | * @param args |
229 | */ |
230 | public static void main(String[] args) { |
231 | configureLogger(); |
232 | DependencySolver dsolver = new DependencySolver(getConfig(args)); |
233 | } |
234 | |
235 | private static void configureLogger() { |
236 | PatternLayout myLayout = new PatternLayout("%d{HH:mm:ss,SSS} [%t] %-5p %c - %m%n"); |
237 | ConsoleAppender myAppender = new ConsoleAppender(myLayout); |
238 | BasicConfigurator.configure(myAppender); |
239 | } |
240 | |
241 | public static Properties getConfig(String[] args) { |
242 | Properties configFromFile = new Properties(); |
243 | if (args.length != 1) { |
244 | System.out.println("Usage: DependencySolver <configfile.xml>"); |
245 | System.exit(-1); |
246 | } else { |
247 | // read XML configuration file |
248 | configFromFile = new Properties(); |
249 | try { |
250 | FileInputStream fis = new FileInputStream(args[0]); |
251 | configFromFile.loadFromXML(fis); |
252 | } catch (FileNotFoundException e) { |
253 | // TODO Auto-generated catch block |
254 | e.printStackTrace(); |
255 | } catch (IOException e) { |
256 | // TODO Auto-generated catch block |
257 | e.printStackTrace(); |
258 | } |
259 | } |
260 | return configFromFile; |
261 | } |
262 | |
263 | } |